TrueReality  v0.1.1912
Vec3Impl.cpp
Go to the documentation of this file.
1 /*
2 * True Reality Open Source Game and Simulation Engine
3 * Copyright © 2017 Acid Rain Studios LLC
4 *
5 * This library is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU Lesser General Public License as published by the Free
7 * Software Foundation; either version 3.0 of the License, or (at your option)
8 * any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13 * details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 *
19 * Author: Maxim Serebrennik
20 */
21 
22 namespace trBase
23 {
25  const int Vec3Impl::NUM_OF_COMPONENTS = 3;
26 
29  : mVec()
30  {
31  }
32 
35  : mVec(x, y, z)
36  {
37  }
38 
41  : mVec(v.x(), v.y(), v.z())
42  {
43  }
44 
46  const osg::Vec3Impl& Vec3Impl::GetOSGVector() const
47  {
48  return mVec;
49  }
50 
52  Vec3Impl::value_type Vec3Impl::Length() const
53  {
54 
55  return sqrt(mVec._v[0] * mVec._v[0] + mVec._v[1] * mVec._v[1] + mVec._v[2] * mVec._v[2]);
56  }
57 
59  Vec3Impl::value_type Vec3Impl::Length2() const
60  {
61  return (mVec._v[0] * mVec._v[0] + mVec._v[1] * mVec._v[1] + mVec._v[2] * mVec._v[2]);
62  }
63 
65  Vec3Impl::value_type Vec3Impl::Normalize()
66  {
67  value_type norm = Vec3Impl::Length();
68  if (norm>0.0)
69  {
70  value_type inv = 1.0 / norm;
71  mVec._v[0] *= inv;
72  mVec._v[1] *= inv;
73  mVec._v[2] *= inv;
74  }
75  return norm;
76  }
77 
79  Vec3Impl::value_type& Vec3Impl::X()
80  {
81  return mVec._v[0];
82  }
83 
85  Vec3Impl::value_type& Vec3Impl::Y()
86  {
87  return mVec._v[1];
88  }
89 
91  Vec3Impl::value_type& Vec3Impl::Z()
92  {
93  return mVec._v[2];
94  }
95 
97  Vec3Impl::value_type Vec3Impl::X() const
98  {
99  return mVec._v[0];
100  }
101 
103  Vec3Impl::value_type Vec3Impl::Y() const
104  {
105  return mVec._v[1];
106  }
107 
109  Vec3Impl::value_type Vec3Impl::Z() const
110  {
111  return mVec._v[2];
112  }
113 
115  Vec3Impl::value_type& Vec3Impl::R()
116  {
117  return mVec._v[0];
118  }
119 
121  Vec3Impl::value_type& Vec3Impl::G()
122  {
123  return mVec._v[1];
124  }
125 
127  Vec3Impl::value_type& Vec3Impl::B()
128  {
129  return mVec._v[2];
130  }
131 
133  Vec3Impl::value_type Vec3Impl::R() const
134  {
135  return mVec._v[0];
136  }
137 
139  Vec3Impl::value_type Vec3Impl::G() const
140  {
141  return mVec._v[1];
142  }
143 
145  Vec3Impl::value_type Vec3Impl::B() const
146  {
147  return mVec._v[2];
148  }
149 
151  void Vec3Impl::Set(value_type x, value_type y, value_type z)
152  {
153  mVec._v[0] = x; mVec._v[1] = y; mVec._v[2] = z;
154  }
155 
159  void Vec3Impl::Set(const Vec3Impl& v)
160  {
161  mVec._v[0] = v[0]; mVec._v[1] = v[1]; mVec._v[2] = v[2];
162  }
163 
165  void Vec3Impl::SetRGB(value_type r, value_type g, value_type b)
166  {
167  mVec._v[0] = r; mVec._v[1] = g; mVec._v[2] = b;
168  }
169 
171  void Vec3Impl::Lerp(const Vec3Impl& to, value_type alpha)
172  {
173  mVec._v[0] = trUtil::Math::Lerp<value_type>(mVec._v[0], to.X(), alpha);
174  mVec._v[1] = trUtil::Math::Lerp<value_type>(mVec._v[1], to.Y(), alpha);
175  mVec._v[2] = trUtil::Math::Lerp<value_type>(mVec._v[2], to.Z(), alpha);
176  }
177 
179  Vec3Impl Vec3Impl::Lerp(const Vec3Impl& from, const Vec3Impl& to, value_type alpha)
180  {
181  return Vec3Impl(trUtil::Math::Lerp<value_type>(from.X(), to.X(), alpha), trUtil::Math::Lerp<value_type>(from.Y(), to.Y(), alpha), trUtil::Math::Lerp<value_type>(from.Z(), to.Z(), alpha));
182  }
183 
185  std::string Vec3Impl::ToString(int precision)
186  {
187  return std::string("<" + trUtil::StringUtils::ToString<value_type>(mVec._v[0], precision) + ", " + trUtil::StringUtils::ToString<value_type>(mVec._v[1], precision) + ", " + trUtil::StringUtils::ToString<value_type>(mVec._v[2], precision) + ">");
188  }
189 
191  Vec3Impl::value_type* Vec3Impl::Ptr()
192  {
193  return mVec.ptr();
194  }
195 
197  const Vec3Impl::value_type* Vec3Impl::Ptr() const
198  {
199  return mVec.ptr();
200  }
201 
203  Vec3Impl::value_type& Vec3Impl::operator [] (int i)
204  {
205  return mVec[i];
206  }
207 
209  Vec3Impl::value_type Vec3Impl::operator [] (int i) const
210  {
211  return mVec[i];
212  }
213 
215  void Vec3Impl::operator = (const Vec3Impl& v)
216  {
217  mVec._v[0] = v[0]; mVec._v[1] = v[1]; mVec._v[2] = v[2];
218  }
219 
221  void Vec3Impl::operator = (const osg::Vec3Impl& v)
222  {
223  mVec._v[0] = v[0]; mVec._v[1] = v[1]; mVec._v[2] = v[2];
224  }
225 
227  bool Vec3Impl::operator == (const Vec3Impl& v) const
228  {
229  return mVec._v[0] == v[0] && mVec._v[1] == v[1] && mVec._v[2] == v[2];
230  }
231 
233  bool Vec3Impl::operator != (const Vec3Impl& v) const
234  {
235  return mVec._v[0] != v[0] || mVec._v[1] != v[1] || mVec._v[2] != v[2];
236  }
237 
239  bool Vec3Impl::operator < (const Vec3Impl& v) const
240  {
241  if (mVec._v[0]<v[0]) return true;
242  else if (mVec._v[0]>v[0]) return false;
243  else if (mVec._v[1]<v[1]) return true;
244  else if (mVec._v[1]>v[1]) return false;
245  else return (mVec._v[2]<v[2]);
246  }
247 
249  bool Vec3Impl::operator > (const Vec3Impl& v) const
250  {
251  if (mVec._v[0]>v[0]) return true;
252  else if (mVec._v[0]<v[0]) return false;
253  else if (mVec._v[1]>v[1]) return true;
254  else if (mVec._v[1]<v[1]) return false;
255  else return (mVec._v[2]>v[2]);
256  }
257 
259  Vec3Impl::value_type Vec3Impl::operator * (const Vec3Impl& rhs) const
260  {
261  return mVec._v[0] * rhs[0] + mVec._v[1] * rhs[1] + mVec._v[2] * rhs[2];
262  }
263 
265  const Vec3Impl Vec3Impl::operator * (Vec3Impl::value_type rhs) const
266  {
267  return Vec3Impl(mVec._v[0] * rhs, mVec._v[1] * rhs, mVec._v[2] * rhs);
268  }
269 
271  Vec3Impl& Vec3Impl::operator *= (Vec3Impl::value_type rhs)
272  {
273  mVec._v[0] *= rhs;
274  mVec._v[1] *= rhs;
275  mVec._v[2] *= rhs;
276  return *this;
277  }
278 
280  const Vec3Impl Vec3Impl::operator / (Vec3Impl::value_type rhs) const
281  {
282  return Vec3Impl(mVec._v[0] / rhs, mVec._v[1] / rhs, mVec._v[2] / rhs);
283  }
284 
286  Vec3Impl& Vec3Impl::operator /= (Vec3Impl::value_type rhs)
287  {
288  mVec._v[0] /= rhs;
289  mVec._v[1] /= rhs;
290  mVec._v[2] /= rhs;
291  return *this;
292  }
293 
295  const Vec3Impl Vec3Impl::operator + (const Vec3Impl& rhs) const
296  {
297  return Vec3Impl(mVec._v[0] + rhs[0], mVec._v[1] + rhs[1], mVec._v[2] + rhs[2]);
298  }
299 
301  Vec3Impl& Vec3Impl::operator += (const Vec3Impl& rhs)
302  {
303  mVec._v[0] += rhs[0];
304  mVec._v[1] += rhs[1];
305  mVec._v[2] += rhs[2];
306  return *this;
307  }
308 
310  const Vec3Impl Vec3Impl::operator - (const Vec3Impl& rhs) const
311  {
312  return Vec3Impl(mVec._v[0] - rhs[0], mVec._v[1] - rhs[1], mVec._v[2] - rhs[2]);
313  }
314 
316  Vec3Impl& Vec3Impl::operator -= (const Vec3Impl& rhs)
317  {
318  mVec._v[0] -= rhs[0];
319  mVec._v[1] -= rhs[1];
320  mVec._v[2] -= rhs[2];
321  return *this;
322  }
323 
325  const Vec3Impl Vec3Impl::operator - () const
326  {
327  return Vec3Impl(-1 * mVec._v[0], -1 * mVec._v[1], -1 * mVec._v[2]);
328  }
329 
331  const Vec3Impl Vec3Impl::operator ^ (const Vec3Impl& rhs) const
332  {
333  return Vec3Impl(mVec._v[1] * rhs[2] - mVec._v[2] * rhs[1],
334  mVec._v[2] * rhs[0] - mVec._v[0] * rhs[2],
335  mVec._v[0] * rhs[1] - mVec._v[1] * rhs[0]);
336  }
337 
339  Vec3Impl::operator osg::Vec3Impl () const
340  {
341  return mVec;
342  }
343 
345  Vec3Impl::operator osg::Vec3Impl& ()
346  {
347  return mVec;
348  }
349 
351  Vec3Impl::operator const osg::Vec3Impl& () const
352  {
353  return mVec;
354  }
355 
357  Vec3Impl::operator osg::Vec3Impl* ()
358  {
359  return &mVec;
360  }
361 
363  std::ostream& operator << (std::ostream& ios, const Vec3Impl& vec)
364  {
365  ios << "<" << vec.X() << ", " << vec.Y() << ", " << vec.Z() << ">";
366  return ios;
367  }
368 
370  Vec3Impl ComponentMultiply(const Vec3Impl& lhs, const Vec3Impl& rhs)
371  {
372  return Vec3Impl(lhs[0] * rhs[0], lhs[1] * rhs[1], lhs[2] * rhs[2]);
373  }
374 
376  Vec3Impl ComponentDivide(const Vec3Impl& lhs, const Vec3Impl& rhs)
377  {
378  return Vec3Impl(lhs[0] / rhs[0], lhs[1] / rhs[1], lhs[2] / rhs[2]);
379  }
380 }
#define Vec3Impl
Definition: Vec3b.cpp:25
std::string operator+(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:193
bool operator==(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:187
bool operator!=(const std::string &s1, const RefStr &s2)
Definition: RefStr.h:199
TR_BASE_EXPORT std::ostream & operator<<(std::ostream &ios, const Matrixd &q)
Stream insertion operator.
std::string ToString(const T &t, int precision=-1)
A utility function to convert a basic type into a string.
Definition: StringUtils.h:334
TR_BASE_EXPORT Vec2b ComponentDivide(const Vec2b &lhs, const Vec2b &rhs)
Divide rhs components by rhs vector components.
TR_BASE_EXPORT Vec2b ComponentMultiply(const Vec2b &lhs, const Vec2b &rhs)
Multiply individual vector components.
int value_type
Data type of vector components.
Definition: Vec3i.h:43
T Lerp(T from, T to, T alpha)
Linear Interpolation function.
Definition: Math.h:191
osg::Vec3i mVec
Definition: Vec3i.h:322