libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moMathVector4.h
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moMathVector4.h
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28  Andrés Colubri
29 
30  Portions taken from
31  Wild Magic Source Code
32  David Eberly
33  http://www.geometrictools.com
34  Copyright (c) 1998-2007
35 
36 *******************************************************************************/
37 
38 #include "moMath.h"
39 
40 #ifndef __MO_MATH_VECTOR4_H__
41 #define __MO_MATH_VECTOR4_H__
42 
43 // moVector4 class ------------------------------------------------------------
44 
45 template <class Real>
47 {
48 public:
49  // construction
50  moVector4 () {} // uninitialized
51  moVector4 (Real fX, Real fY, Real fZ, Real fW) {
52  m_afTuple[0] = fX;
53  m_afTuple[1] = fY;
54  m_afTuple[2] = fZ;
55  m_afTuple[3] = fW;
56  }
57  moVector4 (const Real* afTuple) {
58  m_afTuple[0] = afTuple[0];
59  m_afTuple[1] = afTuple[1];
60  m_afTuple[2] = afTuple[2];
61  m_afTuple[3] = afTuple[3];
62  }
63  moVector4 (const moVector4 & rkV) {
64  m_afTuple[0] = rkV.m_afTuple[0];
65  m_afTuple[1] = rkV.m_afTuple[1];
66  m_afTuple[2] = rkV.m_afTuple[2];
67  m_afTuple[3] = rkV.m_afTuple[3];
68  }
69 
70  // coordinate access
71  inline operator const Real* () const { return m_afTuple; }
72  inline operator Real* () { return m_afTuple; }
73  inline Real operator[] (int i) const { return m_afTuple[i]; }
74  inline Real& operator[] (int i) { return m_afTuple[i]; }
75  inline Real X () const { return m_afTuple[0]; }
76  inline Real& X () { return m_afTuple[0]; }
77  inline Real Y () const { return m_afTuple[1]; }
78  inline Real& Y () { return m_afTuple[1]; }
79  inline Real Z () const { return m_afTuple[2]; }
80  inline Real& Z () { return m_afTuple[2]; }
81  inline Real W () const { return m_afTuple[3]; }
82  inline Real& W () { return m_afTuple[3]; }
83 
84  // assignment
85  inline moVector4 & operator= (const moVector4 & rkV)
86  {
87  m_afTuple[0] = rkV.m_afTuple[0];
88  m_afTuple[1] = rkV.m_afTuple[1];
89  m_afTuple[2] = rkV.m_afTuple[2];
90  m_afTuple[3] = rkV.m_afTuple[3];
91  return *this;
92  }
93 
94  // comparison
95  bool operator== (const moVector4 & rkV) const { return CompareArrays(rkV) == 0; }
96  bool operator!= (const moVector4 & rkV) const { return CompareArrays(rkV) != 0; }
97  bool operator< (const moVector4 & rkV) const { return CompareArrays(rkV) < 0; }
98  bool operator<= (const moVector4 & rkV) const { return CompareArrays(rkV) <= 0; }
99  bool operator> (const moVector4 & rkV) const { return CompareArrays(rkV) > 0; }
100  bool operator>= (const moVector4 & rkV) const { return CompareArrays(rkV) >= 0; }
101 
102  // arithmetic operations
103  inline moVector4 operator+ (const moVector4 & rkV) const
104  {
105  return moVector4 (
106  m_afTuple[0]+rkV.m_afTuple[0],
107  m_afTuple[1]+rkV.m_afTuple[1],
108  m_afTuple[2]+rkV.m_afTuple[2],
109  m_afTuple[3]+rkV.m_afTuple[3]);
110  }
111  inline moVector4 operator- (const moVector4 & rkV) const
112  {
113  return moVector4 (
114  m_afTuple[0]-rkV.m_afTuple[0],
115  m_afTuple[1]-rkV.m_afTuple[1],
116  m_afTuple[2]-rkV.m_afTuple[2],
117  m_afTuple[3]-rkV.m_afTuple[3]);
118  }
119  inline moVector4 operator* (Real fScalar) const
120  {
121  return moVector4 (
122  fScalar*m_afTuple[0],
123  fScalar*m_afTuple[1],
124  fScalar*m_afTuple[2],
125  fScalar*m_afTuple[3]);
126  }
127  inline moVector4 operator/ (Real fScalar) const
128  {
129  moVector4 kQuot;
130 
131  if (fScalar != (Real)0.0)
132  {
133  Real fInvScalar = ((Real)1.0)/fScalar;
134  kQuot.m_afTuple[0] = fInvScalar*m_afTuple[0];
135  kQuot.m_afTuple[1] = fInvScalar*m_afTuple[1];
136  kQuot.m_afTuple[2] = fInvScalar*m_afTuple[2];
137  kQuot.m_afTuple[3] = fInvScalar*m_afTuple[3];
138  }
139  else
140  {
141  kQuot.m_afTuple[0] = moMath<Real>::MAX_REAL;
142  kQuot.m_afTuple[1] = moMath<Real>::MAX_REAL;
143  kQuot.m_afTuple[2] = moMath<Real>::MAX_REAL;
144  kQuot.m_afTuple[3] = moMath<Real>::MAX_REAL;
145  }
146 
147  return kQuot;
148  }
149  inline moVector4 operator- () const
150  {
151  return moVector4(
152  -m_afTuple[0],
153  -m_afTuple[1],
154  -m_afTuple[2],
155  -m_afTuple[3]);
156  }
157 
158  // arithmetic updates
159  inline moVector4 & operator+= (const moVector4 & rkV)
160  {
161  m_afTuple[0] += rkV.m_afTuple[0];
162  m_afTuple[1] += rkV.m_afTuple[1];
163  m_afTuple[2] += rkV.m_afTuple[2];
164  m_afTuple[3] += rkV.m_afTuple[3];
165  return *this;
166  }
167  inline moVector4 & operator-= (const moVector4 & rkV)
168  {
169  m_afTuple[0] -= rkV.m_afTuple[0];
170  m_afTuple[1] -= rkV.m_afTuple[1];
171  m_afTuple[2] -= rkV.m_afTuple[2];
172  m_afTuple[3] -= rkV.m_afTuple[3];
173  return *this;
174  }
175  inline moVector4 & operator*= (Real fScalar)
176  {
177  m_afTuple[0] *= fScalar;
178  m_afTuple[1] *= fScalar;
179  m_afTuple[2] *= fScalar;
180  m_afTuple[3] *= fScalar;
181  return *this;
182  }
183  inline moVector4 & operator/= (Real fScalar)
184  {
185  if (fScalar != (Real)0.0)
186  {
187  Real fInvScalar = ((Real)1.0)/fScalar;
188  m_afTuple[0] *= fInvScalar;
189  m_afTuple[1] *= fInvScalar;
190  m_afTuple[2] *= fInvScalar;
191  m_afTuple[3] *= fInvScalar;
192  }
193  else
194  {
195  m_afTuple[0] = moMath<Real>::MAX_REAL;
196  m_afTuple[1] = moMath<Real>::MAX_REAL;
197  m_afTuple[2] = moMath<Real>::MAX_REAL;
198  m_afTuple[3] = moMath<Real>::MAX_REAL;
199  }
200 
201  return *this;
202  }
203 
204  // vector operations
205  inline Real Length () const
206  {
207  return moMath<Real>::Sqrt(
208  m_afTuple[0]*m_afTuple[0] +
209  m_afTuple[1]*m_afTuple[1] +
210  m_afTuple[2]*m_afTuple[2] +
211  m_afTuple[3]*m_afTuple[3]);
212  }
213  inline Real SquaredLength () const
214  {
215  return
216  m_afTuple[0]*m_afTuple[0] +
217  m_afTuple[1]*m_afTuple[1] +
218  m_afTuple[2]*m_afTuple[2] +
219  m_afTuple[3]*m_afTuple[3];
220  }
221  inline Real Dot (const moVector4 & rkV) const
222  {
223  return
224  m_afTuple[0]*rkV.m_afTuple[0] +
225  m_afTuple[1]*rkV.m_afTuple[1] +
226  m_afTuple[2]*rkV.m_afTuple[2] +
227  m_afTuple[3]*rkV.m_afTuple[3];
228  }
229  inline Real Normalize ()
230  {
231  Real fLength = Length();
232 
233  if (fLength > moMath<Real>::ZERO_TOLERANCE)
234  {
235  Real fInvLength = ((Real)1.0)/fLength;
236  m_afTuple[0] *= fInvLength;
237  m_afTuple[1] *= fInvLength;
238  m_afTuple[2] *= fInvLength;
239  m_afTuple[3] *= fInvLength;
240  }
241  else
242  {
243  fLength = (Real)0.0;
244  m_afTuple[0] = (Real)0.0;
245  m_afTuple[1] = (Real)0.0;
246  m_afTuple[2] = (Real)0.0;
247  m_afTuple[3] = (Real)0.0;
248  }
249 
250  return fLength;
251  }
252 
253  // Cosine between 'this' vector and rkV.
254  inline Real Cosine (const moVector4<Real>& rkV)
255  {
256  Real l = Length();
257  Real lv = rkV.Length();
258  if ((0 < l) && (0 < lv)) return Dot(rkV) / (l * lv);
259  else return 0;
260  }
261  // Angle between 'this' vector and rkV.
262  inline Real Angle (const moVector4<Real>& rkV) {
263 
264  return moMath<Real>::ACos(Cosine(rkV));
265  }
266 
267  // special vectors
268  static const moVector4 ZERO;
269  static const moVector4 UNIT_X; // (1,0,0,0)
270  static const moVector4 UNIT_Y; // (0,1,0,0)
271  static const moVector4 UNIT_Z; // (0,0,1,0)
272  static const moVector4 UNIT_W; // (0,0,0,1)
273  static const moVector4 ONE; // (1,1,1,1)
274 
275 private:
276  // support for comparisons
277  int CompareArrays (const moVector4 & rkV) const {
278  return memcmp(m_afTuple,rkV.m_afTuple,4*sizeof(Real));
279  }
280 
281  Real m_afTuple[4];
282 };
283 
284 
285 // arithmetic operations
286 template <class Real>
287 inline moVector4<Real> operator* (Real fScalar, const moVector4<Real>& rkV)
288 {
289  moVector4<Real> v4(fScalar*rkV[0], fScalar*rkV[1], fScalar*rkV[2], fScalar*rkV[3]);
290  return v4;
291 }
292 
293 #ifndef MO_MACOSX
294 #ifndef MO_WIN32
295 #ifndef MO_RASPBIAN
299 #endif
300 #endif
301 #endif
302 
303 typedef moVector4<MOlong> moVector4i;
304 typedef moVector4<MOfloat> moVector4f;
305 typedef moVector4<MOdouble> moVector4d;
306 
307 moDeclareExportedDynamicArray( moVector4i, moVector4iArray );
308 moDeclareExportedDynamicArray( moVector4f, moVector4fArray );
309 moDeclareExportedDynamicArray( moVector4d, moVector4dArray );
310 
311 
312 #endif
313 
Real & Z()
Definition: moMathVector4.h:80
moVector4< Real > operator*(Real fScalar, const moVector4< Real > &rkV)
bool operator<=(const moMatrix3 &rkM) const
Definition: moMathMatrix.h:960
Real Normalize()
moVector4< MOlong > moVector4i
Real & W()
Definition: moMathVector4.h:82
moVector4< MOfloat > moVector4f
moMatrix3 operator-(const moMatrix3 &rkM) const
static Real ACos(Real fValue)
Definition: moMath.h:81
moMatrix3 operator/(Real fScalar) const
bool operator>(const moMatrix3 &rkM) const
Definition: moMathMatrix.h:966
moVector4(const Real *afTuple)
Definition: moMathVector4.h:57
Real Y() const
Definition: moMathVector4.h:77
Real Angle(const moVector4< Real > &rkV)
static const moVector4 ZERO
Clase base abstracta de donde deben derivar los objetos [virtual pura].
Definition: moAbstract.h:191
static const moVector4 UNIT_Y
Real Length() const
Real SquaredLength() const
Real & X()
Definition: moMathVector4.h:76
#define LIBMOLDEO_API
Definition: moTypes.h:180
static const moVector4 UNIT_X
static const moVector4 ONE
moMatrix3 & operator-=(const moMatrix3 &rkM)
bool operator==(const moMatrix3 &rkM) const
Definition: moMathMatrix.h:942
Real Z() const
Definition: moMathVector4.h:79
static Real Sqrt(Real fValue)
Definition: moMath.h:279
bool operator<(const moMatrix3 &rkM) const
Definition: moMathMatrix.h:954
moDeclareExportedDynamicArray(moVector4i, moVector4iArray)
moMatrix3 & operator/=(Real fScalar)
static const moVector4 UNIT_W
bool operator!=(const moMatrix3 &rkM) const
Definition: moMathMatrix.h:948
Real W() const
Definition: moMathVector4.h:81
moMatrix3 & operator*=(Real fScalar)
bool operator>=(const moMatrix3 &rkM) const
Definition: moMathMatrix.h:972
moVector4(const moVector4 &rkV)
Definition: moMathVector4.h:63
moMatrix3 & operator=(const moMatrix3 &rkM)
static const moVector4 UNIT_Z
Real X() const
Definition: moMathVector4.h:75
Definition: moMath.h:64
moVector4(Real fX, Real fY, Real fZ, Real fW)
Definition: moMathVector4.h:51
Real Dot(const moVector4 &rkV) const
moVector4< MOdouble > moVector4d
moMatrix3 operator+(const moMatrix3 &rkM) const
moMatrix3 & operator+=(const moMatrix3 &rkM)
Real Cosine(const moVector4< Real > &rkV)
const Real * operator[](int iRow) const
Real & Y()
Definition: moMathVector4.h:78