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

Go to the SVN repository for this file.

1 #ifndef GUI_MATH___VECT2___HPP
2 #define GUI_MATH___VECT2___HPP
3 
4 /* $Id: vect2.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 #include <corelib/ncbistd.hpp>
36 #include <gui/utils/math.hpp>
37 
38 /** @addtogroup GUI_MATH
39  *
40  * @{
41  */
42 
44 
45 
46 template <class T>
47 class CVect2
48 {
49 public:
50  typedef T TVecType;
51 
52 public:
53  // ctors
54  CVect2();
56  CVect2(T, T);
57 
58  //
59  // operators
60 
61  // operator: indexing
62  T operator[] (int i) const { return m_Xy[i]; }
63  T& operator[] (int i) { return m_Xy[i]; }
64 
65  // operators: math
68 
71 
73  // cross product undefined in 2D
74  //CVect2<T>& operator*= (const CVect2<T>&); // cross product!
75 
77 
78  //
79  // named functions
80 
81  // Set Value
82  void Set(T x, T y);
83 
84  // return length of vector
85  float Length() const;
86 
87  // return length of vector squared
88  float Length2() const;
89 
90  // return true if the length of the vector is 0
91  bool Vanishing() const;
92 
93  // normalize a vector
94  void Normalize();
95 
96  // return a unit vector in the direction of the current vector
98 
99  // test whether a passed vector is parallel or normal to current
100  bool Parallel(const CVect2<T>&) const;
101  bool Normal(const CVect2<T>&) const;
102 
103  // perform dot product
104  T Dot(const CVect2<T>&) const;
105 
106  // accessor functions
107  T& X() { return m_Xy[0]; }
108  const T& X() const { return m_Xy[0]; }
109  T& Y() { return m_Xy[1]; }
110  const T& Y() const { return m_Xy[1]; }
111 
112  const T* GetData() const { return m_Xy; }
113 
114 protected:
115  T m_Xy[2];
116 };
117 
118 
120 
121 //
122 // global operations
123 // this is included after the class declaration
124 #include <gui/utils/globals.hpp>
125 
127 
128 
129 //
130 // default ctor
131 //
132 template <class T> inline
134 {
135  m_Xy[0] = m_Xy[1] = (T)0;
136 }
137 
138 
139 //
140 // conversion ctors
141 //
142 template <class T> inline
144 {
145  m_Xy[0] = m_Xy[1] = val;
146 }
147 
148 
149 template <class T> inline
151 {
152  m_Xy[0] = x;
153  m_Xy[1] = y;
154 }
155 
156 
157 //
158 // operator+ (scalar)
159 //
160 template <class T> inline CVect2<T>&
162 {
163  X() += scalar;
164  Y() += scalar;
165 
166  return *this;
167 }
168 
169 //
170 // operator+ (vector)
171 //
172 template <class T> inline CVect2<T>&
174 {
175  X() += v.X();
176  Y() += v.Y();
177 
178  return *this;
179 }
180 
181 
182 //
183 // operator- (scalar)
184 //
185 template <class T> inline CVect2<T>&
187 {
188  X() -= scalar;
189  Y() -= scalar;
190 
191  return *this;
192 }
193 
194 //
195 // operator- (vector)
196 //
197 template <class T> inline CVect2<T>&
199 {
200  X() -= v.X();
201  Y() -= v.Y();
202 
203  return *this;
204 }
205 
206 
207 //
208 // operator*= (scalar)
209 //
210 template <class T> inline CVect2<T>&
212 {
213  X() *= scalar;
214  Y() *= scalar;
215 
216  return *this;
217 }
218 
219 
220 //
221 // operator* (vector)
222 // (cross product!!!)
223 //
224 /*
225 template <class T> inline CVect2<T>&
226 CVect2<T>::operator*= (const CVect2<T>& v)
227 {
228  *this = Cross(v);
229  return *this;
230 }
231 */
232 
233 //
234 // dot(vector)
235 //
236 template <class T> inline T
237 CVect2<T>::Dot(const CVect2<T>& v) const
238 {
239  return (T)(X() * v.X() + Y() * v.Y());
240 }
241 
242 //
243 // operator/= (scalar)
244 //
245 template <class T> inline CVect2<T>&
247 {
248  scalar = T(1) / scalar;
249  X() *= scalar;
250  Y() *= scalar;
251 
252  return *this;
253 }
254 
255 //
256 // Set()
257 // set x and y values
258 //
259 template <class T> inline void
261 {
262  m_Xy[0] = x;
263  m_Xy[1] = y;
264 }
265 
266 //
267 // length()
268 // return the length of a vector
269 //
270 template <class T> inline float
272 {
273  return ::sqrt(Length2());
274 }
275 
276 
277 template <class T> inline float
279 {
280  return (X() * X() + Y() * Y());
281 }
282 
283 
284 template <class T> inline bool
286 {
287  return (Length2() == 0.0);
288 }
289 
290 
291 //
292 // normalize()
293 // converts a vector to a unit vector
294 //
295 template <class T> inline void
297 {
298  typedef NCBI_PROMOTE(T, float) TFloat;
299  TFloat len = TFloat(Length());
300 
301  if (len != TFloat(0)) {
302  TFloat f = TFloat(1) / len;
303 
304  X() = (X() * f);
305  Y() = (Y() * f);
306  }
307 }
308 
309 
310 //
311 // direction()
312 // returns a unit vector in the direction of the current vector
313 //
314 template <class T> inline CVect2<T>
316 {
317  return CVect2<T>(*this, true);
318 }
319 
320 
321 //
322 // parallel()
323 // tests to see whether the passed vector is parallel to the current vector
324 //
325 template <class T> inline bool
327 {
328  typedef NCBI_PROMOTE(T, float) TFloat;
329  TFloat result = (TFloat)Dot(v);
330  TFloat l = Length() * v.Length();
331 
332  if (result == l || result == -l) {
333  return true;
334  }
335 
336  return false;
337 }
338 
339 
340 //
341 // normal()
342 // tests to see whether the passed vector is nromal to the current vector
343 //
344 template <class T> inline bool
346 {
347  typedef NCBI_PROMOTE(T, float) TFloat;
348  TFloat result = (TFloat)Dot(v);
349  if (result == TFloat(0) &&
350  Length() != TFloat(0) &&
351  v.Length() != TFloat(0)) {
352  return true;
353  }
354  return false;
355 }
356 
357 
359 
360 /* @} */
361 
362 #endif // GUI_MATH___VECT2___HPP///
Definition: vect2.hpp:48
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define T(s)
Definition: common.h:230
CVect2(T val)
Definition: vect2.hpp:143
const T & Y() const
Definition: vect2.hpp:110
T TVecType
Definition: vect2.hpp:50
T operator[](int i) const
Definition: vect2.hpp:62
const T & X() const
Definition: vect2.hpp:108
CVect2(T, T)
Definition: vect2.hpp:150
float Length() const
Definition: vect2.hpp:271
CVect2< T > & operator+=(T)
Definition: vect2.hpp:161
bool Normal(const CVect2< T > &) const
Definition: vect2.hpp:345
T Dot(const CVect2< T > &) const
Definition: vect2.hpp:237
T m_Xy[2]
Definition: vect2.hpp:115
CVect2< T > & operator-=(T)
Definition: vect2.hpp:186
bool Parallel(const CVect2< T > &) const
Definition: vect2.hpp:326
CVect2< T > Direction() const
Definition: vect2.hpp:315
void Set(T x, T y)
Definition: vect2.hpp:260
const T * GetData() const
Definition: vect2.hpp:112
bool Vanishing() const
Definition: vect2.hpp:285
T & X()
Definition: vect2.hpp:107
CVect2< T > & operator/=(T)
Definition: vect2.hpp:246
CVect2< T > & operator*=(T)
Definition: vect2.hpp:211
CVect2()
Definition: vect2.hpp:133
void Normalize()
Definition: vect2.hpp:296
NCBI_PROMOTE(T, U) operator*(const CVect2< T > &v1
float Length2() const
Definition: vect2.hpp:278
T & Y()
Definition: vect2.hpp:109
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
int i
int len
double f(double x_, const double &y_)
Definition: njn_root.hpp:188
else result
Definition: token2.c:20
float Dot(iterator1 iter1, iterator1 end1, iterator2 iter2, iterator2 end2)
Dot-product similarity.
Modified on Wed May 22 11:28:39 2024 by modify_doxy.py rev. 669887