TrueReality  v0.1.1912
Vec4Impl.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 Vec4Impl::NUM_OF_COMPONENTS = 4;
26 
29  : mVec()
30  {
31  }
32 
35  : mVec(x, y, z, w)
36  {
37  }
38 
41  : mVec(v[0], v[1], v[2], v[3])
42  {
43  }
44 
46  const osg::Vec4Impl& Vec4Impl::GetOSGVector() const
47  {
48  return mVec;
49  }
50 
52  Vec4Impl::value_type Vec4Impl::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] + mVec._v[3] * mVec._v[3]);
56  }
57 
59  Vec4Impl::value_type Vec4Impl::Length2() const
60  {
61  return (mVec._v[0] * mVec._v[0] + mVec._v[1] * mVec._v[1] + mVec._v[2] * mVec._v[2] + mVec._v[3] * mVec._v[3]);
62  }
63 
65  Vec4Impl::value_type Vec4Impl::Normalize()
66  {
67  value_type norm = Vec4Impl::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  mVec._v[3] *= inv;
75  }
76  return norm;
77  }
78 
80  Vec4Impl::value_type& Vec4Impl::X()
81  {
82  return mVec._v[0];
83  }
84 
86  Vec4Impl::value_type& Vec4Impl::Y()
87  {
88  return mVec._v[1];
89  }
90 
92  Vec4Impl::value_type& Vec4Impl::Z()
93  {
94  return mVec._v[2];
95  }
96 
98  Vec4Impl::value_type& Vec4Impl::W()
99  {
100  return mVec._v[3];
101  }
102 
104  Vec4Impl::value_type Vec4Impl::X() const
105  {
106  return mVec._v[0];
107  }
108 
110  Vec4Impl::value_type Vec4Impl::Y() const
111  {
112  return mVec._v[1];
113  }
114 
116  Vec4Impl::value_type Vec4Impl::Z() const
117  {
118  return mVec._v[2];
119  }
120 
122  Vec4Impl::value_type Vec4Impl::W() const
123  {
124  return mVec._v[3];
125  }
126 
128  Vec4Impl::value_type& Vec4Impl::R()
129  {
130  return mVec._v[0];
131  }
132 
134  Vec4Impl::value_type& Vec4Impl::G()
135  {
136  return mVec._v[1];
137  }
138 
140  Vec4Impl::value_type& Vec4Impl::B()
141  {
142  return mVec._v[2];
143  }
144 
146  Vec4Impl::value_type& Vec4Impl::A()
147  {
148  return mVec._v[3];
149  }
150 
152  Vec4Impl::value_type Vec4Impl::R() const
153  {
154  return mVec._v[0];
155  }
156 
158  Vec4Impl::value_type Vec4Impl::G() const
159  {
160  return mVec._v[1];
161  }
162 
164  Vec4Impl::value_type Vec4Impl::B() const
165  {
166  return mVec._v[2];
167  }
168 
170  Vec4Impl::value_type Vec4Impl::A() const
171  {
172  return mVec._v[3];
173  }
174 
176  void Vec4Impl::Set(value_type x, value_type y, value_type z, value_type w)
177  {
178  mVec._v[0] = x; mVec._v[1] = y; mVec._v[2] = z; mVec._v[3] = w;
179  }
180 
184  void Vec4Impl::Set(const Vec4Impl& v)
185  {
186  mVec._v[0] = v[0]; mVec._v[1] = v[1]; mVec._v[2] = v[2]; mVec._v[3] = v[3];
187  }
188 
190  void Vec4Impl::SetRGBA(value_type r, value_type g, value_type b, value_type a)
191  {
192  mVec._v[0] = r; mVec._v[1] = g; mVec._v[2] = b; mVec._v[3] = a;
193  }
194 
196  void Vec4Impl::Lerp(const Vec4Impl& to, value_type alpha)
197  {
198  mVec._v[0] = trUtil::Math::Lerp<value_type>(mVec._v[0], to.X(), alpha);
199  mVec._v[1] = trUtil::Math::Lerp<value_type>(mVec._v[1], to.Y(), alpha);
200  mVec._v[2] = trUtil::Math::Lerp<value_type>(mVec._v[2], to.Z(), alpha);
201  mVec._v[3] = trUtil::Math::Lerp<value_type>(mVec._v[3], to.W(), alpha);
202  }
203 
205  Vec4Impl Vec4Impl::Lerp(const Vec4Impl& from, const Vec4Impl& to, value_type alpha)
206  {
207  return Vec4Impl(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), trUtil::Math::Lerp<value_type>(from.W(), to.W(), alpha));
208  }
209 
211  std::string Vec4Impl::ToString(int precision)
212  {
213  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) + ", " + trUtil::StringUtils::ToString<value_type>(mVec._v[3], precision) + ">");
214  }
215 
217  Vec4Impl::value_type* Vec4Impl::Ptr()
218  {
219  return mVec.ptr();
220  }
221 
223  const Vec4Impl::value_type* Vec4Impl::Ptr() const
224  {
225  return mVec.ptr();
226  }
227 
229  Vec4Impl::value_type& Vec4Impl::operator [] (int i)
230  {
231  return mVec[i];
232  }
233 
235  Vec4Impl::value_type Vec4Impl::operator [] (int i) const
236  {
237  return mVec[i];
238  }
239 
241  void Vec4Impl::operator = (const Vec4Impl& v)
242  {
243  mVec._v[0] = v[0]; mVec._v[1] = v[1]; mVec._v[2] = v[2]; mVec._v[3] = v[3];
244  }
245 
247  void Vec4Impl::operator = (const osg::Vec4Impl& v)
248  {
249  mVec._v[0] = v[0]; mVec._v[1] = v[1]; mVec._v[2] = v[2]; mVec._v[3] = v[3];
250  }
251 
253  bool Vec4Impl::operator == (const Vec4Impl& v) const
254  {
255  return mVec._v[0] == v[0] && mVec._v[1] == v[1] && mVec._v[2] == v[2] && mVec._v[3] == v[3];
256  }
257 
259  bool Vec4Impl::operator != (const Vec4Impl& v) const
260  {
261  return mVec._v[0] != v[0] || mVec._v[1] != v[1] || mVec._v[2] != v[2] || mVec._v[3] != v[3];
262  }
263 
265  bool Vec4Impl::operator < (const Vec4Impl& v) const
266  {
267  if (mVec._v[0]<v[0]) return true;
268  else if (mVec._v[0]>v[0]) return false;
269  else if (mVec._v[1]<v[1]) return true;
270  else if (mVec._v[1]>v[1]) return false;
271  else if (mVec._v[2]<v[2]) return true;
272  else if (mVec._v[2]>v[2]) return false;
273  else return (mVec._v[3]<v[3]);
274  }
275 
277  bool Vec4Impl::operator > (const Vec4Impl& v) const
278  {
279  if (mVec._v[0]>v[0]) return true;
280  else if (mVec._v[0]<v[0]) return false;
281  else if (mVec._v[1]>v[1]) return true;
282  else if (mVec._v[1]<v[1]) return false;
283  else if (mVec._v[2]>v[2]) return true;
284  else if (mVec._v[2]<v[2]) return false;
285  else return (mVec._v[3]>v[3]);
286  }
287 
289  Vec4Impl::value_type Vec4Impl::operator * (const Vec4Impl& rhs) const
290  {
291  return mVec._v[0] * rhs[0] + mVec._v[1] * rhs[1] + mVec._v[2] * rhs[2] + mVec._v[3] * rhs[3];
292  }
293 
295  const Vec4Impl Vec4Impl::operator * (Vec4Impl::value_type rhs) const
296  {
297  return Vec4Impl(mVec._v[0] * rhs, mVec._v[1] * rhs, mVec._v[2] * rhs, mVec._v[3] * rhs);
298  }
299 
301  Vec4Impl& Vec4Impl::operator *= (Vec4Impl::value_type rhs)
302  {
303  mVec._v[0] *= rhs;
304  mVec._v[1] *= rhs;
305  mVec._v[2] *= rhs;
306  mVec._v[3] *= rhs;
307  return *this;
308  }
309 
311  const Vec4Impl Vec4Impl::operator / (Vec4Impl::value_type rhs) const
312  {
313  return Vec4Impl(mVec._v[0] / rhs, mVec._v[1] / rhs, mVec._v[2] / rhs, mVec._v[3] / rhs);
314  }
315 
317  Vec4Impl& Vec4Impl::operator /= (Vec4Impl::value_type rhs)
318  {
319  mVec._v[0] /= rhs;
320  mVec._v[1] /= rhs;
321  mVec._v[2] /= rhs;
322  mVec._v[3] /= rhs;
323  return *this;
324  }
325 
327  const Vec4Impl Vec4Impl::operator + (const Vec4Impl& rhs) const
328  {
329  return Vec4Impl(mVec._v[0] + rhs[0], mVec._v[1] + rhs[1], mVec._v[2] + rhs[2], mVec._v[3] + rhs[3]);
330  }
331 
333  Vec4Impl& Vec4Impl::operator += (const Vec4Impl& rhs)
334  {
335  mVec._v[0] += rhs[0];
336  mVec._v[1] += rhs[1];
337  mVec._v[2] += rhs[2];
338  mVec._v[3] += rhs[3];
339  return *this;
340  }
341 
343  const Vec4Impl Vec4Impl::operator - (const Vec4Impl& rhs) const
344  {
345  return Vec4Impl(mVec._v[0] - rhs[0], mVec._v[1] - rhs[1], mVec._v[2] - rhs[2], mVec._v[3] - rhs[3]);
346  }
347 
349  Vec4Impl& Vec4Impl::operator -= (const Vec4Impl& rhs)
350  {
351  mVec._v[0] -= rhs[0];
352  mVec._v[1] -= rhs[1];
353  mVec._v[2] -= rhs[2];
354  mVec._v[3] -= rhs[3];
355  return *this;
356  }
357 
359  const Vec4Impl Vec4Impl::operator - () const
360  {
361  return Vec4Impl(-1 * mVec._v[0], -1 * mVec._v[1], -1 * mVec._v[2], -1 * mVec._v[3]);
362  }
363 
365  Vec4Impl::operator osg::Vec4Impl () const
366  {
367  return mVec;
368  }
369 
371  Vec4Impl::operator osg::Vec4Impl& ()
372  {
373  return mVec;
374  }
375 
377  Vec4Impl::operator const osg::Vec4Impl& () const
378  {
379  return mVec;
380  }
381 
383  Vec4Impl::operator osg::Vec4Impl* ()
384  {
385  return &mVec;
386  }
387 
389  std::ostream& operator << (std::ostream& ios, const Vec4Impl& vec)
390  {
391  ios << "<" << vec.X() << ", " << vec.Y() << ", " << vec.Z() << ", " << vec.Z() << ">";
392  return ios;
393  }
394 
396  Vec4Impl ComponentMultiply(const Vec4Impl& lhs, const Vec4Impl& rhs)
397  {
398  return Vec4Impl(lhs[0] * rhs[0], lhs[1] * rhs[1], lhs[2] * rhs[2], lhs[3] * rhs[3]);
399  }
400 
402  Vec4Impl ComponentDivide(const Vec4Impl& lhs, const Vec4Impl& rhs)
403  {
404  return Vec4Impl(lhs[0] / rhs[0], lhs[1] / rhs[1], lhs[2] / rhs[2], lhs[3] / rhs[3]);
405  }
406 }
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.
osg::Vec4i mVec
Definition: Vec4i.h:338
#define Vec4Impl
Definition: Vec4b.cpp:25
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.
T Lerp(T from, T to, T alpha)
Linear Interpolation function.
Definition: Math.h:191
int value_type
Data type of vector components.
Definition: Vec4i.h:43