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
moMathDVector.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moMathDVector.cpp
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 "moMathDVector.h"
39 
40 
41 template <class Real>
43 {
44  if (iSize > 0)
45  {
46  m_iSize = iSize;
47  m_afTuple = new Real[m_iSize];
48  memset(m_afTuple,0,m_iSize*sizeof(Real));
49  }
50  else
51  {
52  m_iSize = 0;
53  m_afTuple = 0;
54  }
55 }
56 
57 template <class Real>
58 moDVector<Real>::moDVector (int iSize, const Real* afTuple)
59 {
60  if (iSize > 0)
61  {
62  m_iSize = iSize;
63  m_afTuple = new Real[m_iSize];
64  size_t uiSize = m_iSize*sizeof(Real);
65  memcpy(m_afTuple,afTuple,uiSize);
66  }
67  else
68  {
69  m_iSize = 0;
70  m_afTuple = 0;
71  }
72 }
73 
74 template <class Real>
76 {
77  m_iSize = rkV.m_iSize;
78  if (m_iSize > 0)
79  {
80  m_afTuple = new Real[m_iSize];
81  size_t uiSize = m_iSize*sizeof(Real);
82  memcpy(m_afTuple,rkV.m_afTuple,uiSize);
83  }
84  else
85  {
86  m_afTuple = 0;
87  }
88 }
89 
90 template <class Real>
92 {
93  delete[] m_afTuple;
94 }
95 
96 template <class Real>
97 void moDVector<Real>::SetSize (int iSize)
98 {
99  delete[] m_afTuple;
100  if (iSize > 0)
101  {
102  m_iSize = iSize;
103  m_afTuple = new Real[m_iSize];
104  memset(m_afTuple,0,m_iSize*sizeof(Real));
105  }
106  else
107  {
108  m_iSize = 0;
109  m_afTuple = 0;
110  }
111 }
112 
113 template <class Real>
115 {
116  return m_iSize;
117 }
118 
119 template <class Real>
120 moDVector<Real>::operator const Real* () const
121 {
122  return m_afTuple;
123 }
124 
125 template <class Real>
127 {
128  return m_afTuple;
129 }
130 
131 template <class Real>
133 {
134  //assert(0 <= i && i < m_iSize);
135  return m_afTuple[i];
136 }
137 
138 template <class Real>
140 {
141  //assert(0 <= i && i < m_iSize);
142  return m_afTuple[i];
143 }
144 
145 template <class Real>
147 {
148  if (rkV.m_iSize > 0)
149  {
150  if (m_iSize != rkV.m_iSize)
151  {
152  delete[] m_afTuple;
153  m_iSize = rkV.m_iSize;
154  m_afTuple = new Real[m_iSize];
155  }
156  size_t uiSize = m_iSize*sizeof(Real);
157  memcpy(m_afTuple,rkV.m_afTuple,uiSize);
158  }
159  else
160  {
161  delete[] m_afTuple;
162  m_iSize = 0;
163  m_afTuple = 0;
164  }
165  return *this;
166 }
167 
168 template <class Real>
170 {
171  return memcmp(m_afTuple,rkV.m_afTuple,m_iSize*sizeof(Real));
172 }
173 
174 template <class Real>
176 {
177  return CompareArrays(rkV) == 0;
178 }
179 
180 template <class Real>
182 {
183  return CompareArrays(rkV) != 0;
184 }
185 
186 template <class Real>
188 {
189  return CompareArrays(rkV) < 0;
190 }
191 
192 template <class Real>
194 {
195  return CompareArrays(rkV) <= 0;
196 }
197 
198 template <class Real>
200 {
201  return CompareArrays(rkV) > 0;
202 }
203 
204 template <class Real>
206 {
207  return CompareArrays(rkV) >= 0;
208 }
209 
210 template <class Real>
212 {
213  moDVector<Real> kSum(m_iSize);
214  for (int i = 0; i < m_iSize; i++)
215  {
216  kSum.m_afTuple[i] = m_afTuple[i] + rkV.m_afTuple[i];
217  }
218  return kSum;
219 }
220 
221 template <class Real>
223 {
224  moDVector<Real> kDiff(m_iSize);
225  for (int i = 0; i < m_iSize; i++)
226  {
227  kDiff.m_afTuple[i] = m_afTuple[i] - rkV.m_afTuple[i];
228  }
229  return kDiff;
230 }
231 
232 template <class Real>
234 {
235  moDVector<Real> kProd(m_iSize);
236  for (int i = 0; i < m_iSize; i++)
237  {
238  kProd.m_afTuple[i] = fScalar*m_afTuple[i];
239  }
240  return kProd;
241 }
242 
243 template <class Real>
245 {
246  moDVector<Real> kQuot(m_iSize);
247  int i;
248 
249  if (fScalar != (Real)0.0)
250  {
251  Real fInvScalar = ((Real)1.0)/fScalar;
252  for (i = 0; i < m_iSize; i++)
253  {
254  kQuot.m_afTuple[i] = fInvScalar*m_afTuple[i];
255  }
256  }
257  else
258  {
259  for (i = 0; i < m_iSize; i++)
260  {
262  }
263  }
264 
265  return kQuot;
266 }
267 
268 template <class Real>
270 {
271  moDVector<Real> kNeg(m_iSize);
272  for (int i = 0; i < m_iSize; i++)
273  {
274  kNeg.m_afTuple[i] = -m_afTuple[i];
275  }
276  return kNeg;
277 }
278 
279 template <class Real>
280 moDVector<Real> operator* (Real fScalar, const moDVector<Real>& rkV)
281 {
282  moDVector<Real> kProd(rkV.GetSize());
283  for (int i = 0; i < rkV.GetSize(); i++)
284  {
285  kProd[i] = fScalar*rkV[i];
286  }
287  return kProd;
288 }
289 
290 template <class Real>
292 {
293  for (int i = 0; i < m_iSize; i++)
294  {
295  m_afTuple[i] += rkV.m_afTuple[i];
296  }
297  return *this;
298 }
299 
300 template <class Real>
302 {
303  for (int i = 0; i < m_iSize; i++)
304  {
305  m_afTuple[i] -= rkV.m_afTuple[i];
306  }
307  return *this;
308 }
309 
310 template <class Real>
312 {
313  for (int i = 0; i < m_iSize; i++)
314  {
315  m_afTuple[i] *= fScalar;
316  }
317  return *this;
318 }
319 
320 template <class Real>
322 {
323  int i;
324 
325  if (fScalar != (Real)0.0)
326  {
327  Real fInvScalar = ((Real)1.0)/fScalar;
328  for (i = 0; i < m_iSize; i++)
329  {
330  m_afTuple[i] *= fInvScalar;
331  }
332  }
333  else
334  {
335  for (i = 0; i < m_iSize; i++)
336  {
337  m_afTuple[i] = moMath<Real>::MAX_REAL;
338  }
339  }
340 
341  return *this;
342 }
343 
344 template <class Real>
346 {
347  Real fSqrLen = (Real)0.0;
348  for (int i = 0; i < m_iSize; i++)
349  {
350  fSqrLen += m_afTuple[i]*m_afTuple[i];
351  }
352  return moMath<Real>::Sqrt(fSqrLen);
353 }
354 
355 template <class Real>
357 {
358  Real fSqrLen = (Real)0.0;
359  for (int i = 0; i < m_iSize; i++)
360  {
361  fSqrLen += m_afTuple[i]*m_afTuple[i];
362  }
363  return fSqrLen;
364 }
365 
366 template <class Real>
367 Real moDVector<Real>::Dot (const moDVector& rkV) const
368 {
369  Real fDot = (Real)0.0;
370  for (int i = 0; i < m_iSize; i++)
371  {
372  fDot += m_afTuple[i]*rkV.m_afTuple[i];
373  }
374  return fDot;
375 }
376 
377 template <class Real>
379 {
380  Real fLength = Length();
381  int i;
382 
383  if (fLength > moMath<Real>::ZERO_TOLERANCE)
384  {
385  Real fInvLength = ((Real)1.0)/fLength;
386  for (i = 0; i < m_iSize; i++)
387  {
388  m_afTuple[i] *= fInvLength;
389  }
390  }
391  else
392  {
393  fLength = (Real)0.0;
394  for (i = 0; i < m_iSize; i++)
395  {
396  m_afTuple[i] = (Real)0.0;
397  }
398  }
399 
400  return fLength;
401 }
402 
moDVector & operator-=(const moDVector &rkV)
bool operator==(const moDVector &rkV) const
moDVector operator+(const moDVector &rkV) const
void SetSize(int iSize)
int CompareArrays(const moDVector &rkV) const
Real Length() const
moDVector operator-() const
bool operator<=(const moDVector &rkV) const
moDVector(int iSize=0)
Clase base abstracta de donde deben derivar los objetos [virtual pura].
Definition: moAbstract.h:191
moDVector & operator*=(Real fScalar)
int GetSize() const
Real operator[](int i) const
Real Dot(const moDVector &rkV) const
moDVector & operator/=(Real fScalar)
Real * m_afTuple
Definition: moMathDVector.h:96
moDVector operator/(Real fScalar) const
moDVector< Real > operator*(Real fScalar, const moDVector< Real > &rkV)
static Real Sqrt(Real fValue)
Definition: moMath.h:279
bool operator<(const moDVector &rkV) const
moDVector & operator+=(const moDVector &rkV)
moDVector & operator=(const moDVector &rkV)
moDVector operator*(Real fScalar) const
Definition: moMath.h:64
bool operator>=(const moDVector &rkV) const
bool operator>(const moDVector &rkV) const
bool operator!=(const moDVector &rkV) const
Real Normalize()
Real SquaredLength() const