NCBI C++ ToolKit
vect4.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_MATH___VECT4___HPP
2 #define GUI_MATH___VECT4___HPP
3 
4 /* $Id: vect4.hpp 27010 2012-12-07 16:37:16Z falkrb $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software / database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software / database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Mike DiCuccio
30  *
31  * File Description:
32  *
33  */
34 
35 
36 #include <corelib/ncbistd.hpp>
37 #include <gui/utils/math.hpp>
38 #include <gui/utils/vect3.hpp>
39 
40 /** @addtogroup GUI_MATH
41  *
42  * @{
43  */
44 
46 
47 template <class T>
48 class CVect4
49 {
50 public:
51  typedef T TVecType;
52 
53 public:
54  // ctors
55  CVect4();
57  CVect4(T, T, T, T);
58 
59  // copy ctors
60  CVect4(const CVect3<T>&);
61 
62  //
63  // operators
64 
65  // operator: assignment
67 
68  // operator: indexing
69  const T& operator[] (size_t i) const { return m_Xyzw[i]; }
70  T& operator[] (size_t i) { return m_Xyzw[i]; }
71 
72  // operators: math
75 
78 
80  CVect4<T>& operator*= (const CVect4<T>&); // cross product!
81 
83 
84  //
85  // named functions
86 
87 
88  // Set Value
89  void Set(T x, T y, T z, T w);
90 
91  // return length of vector
92  float Length() const;
93  float Length2() const;
94  // return true if the length of the vector is 0
95  bool Vanishing() const;
96 
97  // normalize a vector
98  void Normalize();
99 
100  // return a unit vector in the direction of the current vector
102 
103  // test whether a passed vector is parallel or normal to current
104  bool Parallel(const CVect4<T>&) const;
105  bool Normal(const CVect4<T>&) const;
106 
107  // perform dot product
108  T Dot(const CVect4<T>&) const;
109 
110  // accessor functions
111  T& X() { return m_Xyzw[0]; }
112  const T& X() const { return m_Xyzw[0]; }
113  T& Y() { return m_Xyzw[1]; }
114  const T& Y() const { return m_Xyzw[1]; }
115  T& Z() { return m_Xyzw[2]; }
116  const T& Z() const { return m_Xyzw[2]; }
117  T& W() { return m_Xyzw[3]; }
118  const T& W() const { return m_Xyzw[3]; }
119 
120  const T* GetData() const { return m_Xyzw; }
121 
122 private:
123  T m_Xyzw[4];
124 
125 };
126 
127 
129 
130 //
131 // global operations
132 // this is included after the class declaration
133 #include <gui/utils/globals.hpp>
134 
136 
137 
138 //
139 // default ctor
140 //
141 template <class T> inline
143 {
144  m_Xyzw[0] = m_Xyzw[1] = m_Xyzw[2] = m_Xyzw[3] = (T)0;
145 }
146 
147 
148 //
149 // conversion ctors
150 //
151 template <class T> inline
153 {
154  m_Xyzw[0] = m_Xyzw[1] = m_Xyzw[2] = m_Xyzw[3] = val;
155 }
156 
157 
158 template <class T> inline
159 CVect4<T>::CVect4 (T x, T y, T z, T w)
160 {
161  m_Xyzw[0] = x;
162  m_Xyzw[1] = y;
163  m_Xyzw[2] = z;
164  m_Xyzw[3] = w;
165 }
166 
167 
168 //
169 // copy ctor
170 //
171 template <class T> inline
173 {
174  *this = v;
175 }
176 
177 
178 //
179 // assignment operator
180 //
181 template <class T> inline CVect4<T>&
183 {
184  X() = v.X();
185  Y() = v.Y();
186  Z() = v.Z();
187  W() = (T)1;
188 
189  return *this;
190 }
191 
192 
193 //
194 // operator+ (scalar)
195 //
196 template <class T> inline CVect4<T>&
198 {
199  X() += scalar / W();
200  Y() += scalar / W();
201  Z() += scalar / W();
202 
203  return *this;
204 }
205 
206 //
207 // operator+ (vector)
208 //
209 template <class T> inline CVect4<T>&
211 {
212  X() += v.X();
213  Y() += v.Y();
214  Z() += v.Z();
215  W() += v.W();
216 
217  return *this;
218 }
219 
220 
221 //
222 // operator- (scalar)
223 //
224 template <class T> inline CVect4<T>&
226 {
227  X() -= scalar / W();
228  Y() -= scalar / W();
229  Z() -= scalar / W();
230 
231  return *this;
232 }
233 
234 //
235 // operator- (vector)
236 //
237 template <class T> inline CVect4<T>&
239 {
240  X() -= v.X();
241  Y() -= v.Y();
242  Z() -= v.Z();
243  W() -= v.W();
244 
245  return *this;
246 }
247 
248 
249 //
250 // operator*= (scalar)
251 //
252 template <class T> inline CVect4<T>&
254 {
255  X() *= scalar / W();
256  Y() *= scalar / W();
257  Z() *= scalar / W();
258 
259  return *this;
260 }
261 
262 
263 //
264 // dot(vector)
265 //
266 template <class T> inline T
267 CVect4<T>::Dot(const CVect4<T>& v) const
268 {
269  return (T)(X() * v.X() + Y() * v.Y() + Z() * v.Z() + W() * v.W());
270 }
271 
272 
273 //
274 // operator/= (scalar)
275 //
276 template <class T> inline CVect4<T>&
278 {
279  X() /= scalar / W();
280  Y() /= scalar / W();
281  Z() /= scalar / W();
282 
283  return *this;
284 }
285 
286 //
287 // Set()
288 // set x,y,z, w values
289 //
290 template <class T> inline void
291 CVect4<T>::Set(T x, T y, T z, T w)
292 {
293  m_Xyzw[0] = x;
294  m_Xyzw[1] = y;
295  m_Xyzw[2] = z;
296  m_Xyzw[3] = w;
297 }
298 
299 
300 //
301 // length()
302 // return the length of a vector
303 //
304 template <class T> inline float
306 {
307  return ::sqrt(Length2());
308 }
309 
310 
311 template <class T> inline float
313 {
314  return (X() * X() + Y() * Y() + Z() * Z()) / (W() * W());
315 }
316 
317 
318 template <class T> inline bool
320 {
321  return (Length2() == 0.0);
322 }
323 
324 
325 //
326 // normalize()
327 // converts a vector to a unit vector
328 //
329 template <class T> inline void
331 {
332  if (W()) {
333  X() = (T)(X() / W());
334  Y() = (T)(Y() / W());
335  Z() = (T)(Z() / W());
336  W() = (T)1;
337  }
338 }
339 
340 
341 //
342 // direction()
343 // returns a unit vector in the direction of the current vector
344 //
345 template <class T> inline CVect4<T>
347 {
348  return CVect4<T> (*this, true);
349 }
350 
351 
352 //
353 // parallel()
354 // tests to see whether the passed vector is parallel to the current vector
355 //
356 template <class T> inline bool
358 {
359  typedef NCBI_PROMOTE(T, float) TFloat;
360  TFloat result = (TFloat)Dot(v);
361  TFloat l = Length() * v.Length();
362 
363  if (result == l || result == -l) {
364  return true;
365  }
366 
367  return false;
368 }
369 
370 
371 //
372 // normal()
373 // tests to see whether the passed vector is nromal to the current vector
374 //
375 template <class T> inline bool
377 {
378  typedef NCBI_PROMOTE(T, float) TFloat;
379  TFloat result = (TFloat)dot(v);
380  if (result == TFloat(0) &&
381  Length() != TFloat(0) &&
382  v.Length() != TFloat(0)) {
383  return true;
384  }
385  return false;
386 }
387 
388 
390 
391 /* @} */
392 
393 #endif // GUI_MATH___VECT4___HPP
Definition: vect3.hpp:48
Definition: vect4.hpp:49
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define T(s)
Definition: common.h:230
bool Parallel(const CVect4< T > &) const
Definition: vect4.hpp:357
CVect4< T > & operator+=(T)
Definition: vect4.hpp:197
CVect4()
Definition: vect4.hpp:142
CVect4(T, T, T, T)
Definition: vect4.hpp:159
void Normalize()
Definition: vect4.hpp:330
bool Normal(const CVect4< T > &) const
Definition: vect4.hpp:376
const T * GetData() const
Definition: vect4.hpp:120
T & Z()
Definition: vect3.hpp:114
const T & operator[](size_t i) const
Definition: vect4.hpp:69
T & Z()
Definition: vect4.hpp:115
CVect4< T > & operator*=(T)
Definition: vect4.hpp:253
T & X()
Definition: vect3.hpp:110
CVect4< T > & operator/=(T)
Definition: vect4.hpp:277
float Length2() const
Definition: vect4.hpp:312
CVect4< T > & operator=(const CVect3< T > &)
Definition: vect4.hpp:182
CVect4< T > Direction() const
Definition: vect4.hpp:346
const T & X() const
Definition: vect4.hpp:112
float Length() const
Definition: vect4.hpp:305
T & Y()
Definition: vect3.hpp:112
T m_Xyzw[4]
Definition: vect4.hpp:123
const T & Z() const
Definition: vect4.hpp:116
T Dot(const CVect4< T > &) const
Definition: vect4.hpp:267
const T & W() const
Definition: vect4.hpp:118
T & X()
Definition: vect4.hpp:111
CVect4(const CVect3< T > &)
Definition: vect4.hpp:172
void Set(T x, T y, T z, T w)
Definition: vect4.hpp:291
NCBI_PROMOTE(T, U) operator*(const CVect2< T > &v1
bool Vanishing() const
Definition: vect4.hpp:319
T & Y()
Definition: vect4.hpp:113
CVect4(T val)
Definition: vect4.hpp:152
const T & Y() const
Definition: vect4.hpp:114
CVect4< T > & operator-=(T)
Definition: vect4.hpp:225
T TVecType
Definition: vect4.hpp:51
T & W()
Definition: vect4.hpp:117
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
#define Z
printf format modifier for size_t
Definition: mdb.c:334
int i
else result
Definition: token2.c:20
float Dot(iterator1 iter1, iterator1 end1, iterator2 iter2, iterator2 end2)
Dot-product similarity.
#define W
Definition: crc32.c:85
Modified on Thu Jun 13 17:31:12 2024 by modify_doxy.py rev. 669887