Fade2.5D Documentation pages v2.15
Delaunay Features
Vector2.h
Go to the documentation of this file.
1 // Copyright (C) Geom Software e.U, Bernhard Kornberger, Graz/Austria
2 //
3 // This file is part of the Fade2D library. The student license is free
4 // of charge and covers personal non-commercial research. Licensees
5 // holding a commercial license may use this file in accordance with
6 // the Commercial License Agreement.
7 //
8 // This software is provided AS IS with NO WARRANTY OF ANY KIND,
9 // INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
10 // FOR A PARTICULAR PURPOSE.
11 //
12 // Please contact the author if any conditions of this licensing are
13 // not clear to you.
14 //
15 // Author: Bernhard Kornberger, bkorn (at) geom.at
16 // http://www.geom.at
17 
19 
20 #pragma once
21 #include "common.h"
22 
23 #if GEOM_PSEUDO3D==GEOM_TRUE
24  namespace GEOM_FADE25D {
25 #elif GEOM_PSEUDO3D==GEOM_FALSE
26  namespace GEOM_FADE2D {
27 #else
28  #error GEOM_PSEUDO3D is not defined
29 #endif
30 
31 
32 class CLASS_DECLSPEC Vector2;
33 
34 
35 
36 
41 class Vector2
42 {
43 public:
44 
45 #if GEOM_PSEUDO3D==GEOM_TRUE
49  Vector2(const double x_,const double y_,const double z_);
50 
56 
62  Vector2(const Vector2& v_);
63 
64 #else
65 
70  Vector2(const double x_,const double y_);
76  Vector2();
82  Vector2(const Vector2& v_);
83 #endif
85  Vector2& operator=(const Vector2& other);
86 
87 
88 #if GEOM_PSEUDO3D==GEOM_TRUE
92 #endif
94 
99  bool isDegenerate() const;
100 
101 
104  double x() const;
105 
108  double y() const;
109 
110 #if GEOM_PSEUDO3D==GEOM_TRUE
114  double z() const;
115 #endif
116 
117 #if GEOM_PSEUDO3D==GEOM_TRUE
120  void set(const double x_,const double y_,const double z_);
121 
122 #else
125  void set(const double x_,const double y_);
126 #endif
127 
130  double sqLength() const;
131 
132 
133 #if GEOM_PSEUDO3D==GEOM_TRUE
138  int getMaxIndex() const;
139 #endif
140 
144  double length() const;
145 
150 #if GEOM_PSEUDO3D==GEOM_TRUE
151 
152  double operator*(const Vector2& other) const;
153 #else
154 
155  double operator*(const Vector2& other) const;
156 #endif
157 
158 
163 #if GEOM_PSEUDO3D==GEOM_TRUE
164 
165  Vector2 operator*(double val) const;
166 #else
167 
168  Vector2 operator*(double val) const;
169 #endif
170 
175 #if GEOM_PSEUDO3D==GEOM_TRUE
176 
177  Vector2 operator/(double val) const;
178 #else
179 
180  Vector2 operator/(double val) const;
181 #endif
182 
183 
184 
185 protected:
186  double valX;
187  double valY;
188 #if GEOM_PSEUDO3D==GEOM_TRUE
189  double valZ;
190 #endif
191 };
192 
193 
194 
195 
196 // Free functions
201 //CLASS_DECLSPEC
202 inline std::ostream &operator<<(std::ostream &stream, const Vector2& vec)
203 {
204 #if GEOM_PSEUDO3D==GEOM_TRUE
205  stream << "Vector2: "<<vec.x()<<", "<<vec.y()<<", "<<vec.z();
206 #else
207  stream << "Vector2: "<<vec.x()<<", "<<vec.y();
208 #endif
209  return stream;
210 }
211 
212 #if GEOM_PSEUDO3D==GEOM_TRUE
217 //CLASS_DECLSPEC
218 inline Vector2 crossProduct(const Vector2& vec0,const Vector2& vec1)
219 {
220  double x=vec0.y() * vec1.z() - vec0.z() * vec1.y();
221  double y=vec0.z() * vec1.x() - vec0.x() * vec1.z();
222  double z=vec0.x() * vec1.y() - vec0.y() * vec1.x();
223  return Vector2(x,y,z);
224 }
225 #endif
226 
227 
228 // TODO: REMOVE!
229 #if 0
232 inline Vector2 normalize(const Vector2& other)
233 {
234  double len(other.length());
235 #if GEOM_PSEUDO3D==GEOM_TRUE
236  if(len>0)
237  {
238  return Vector2(other.x()/len,other.y()/len,other.z()/len);
239  }
240  else
241  {
242 
243 #ifndef NDEBUG
244  std::cout<<"\n\n** warning: normalize(const Vector2& other), Null length vector!"<<std::endl;// COUTOK
246  //std::cout<<"vec="<<other<<std::endl;
247  //std::cout<<"** debug mode -> deliberate segfault, now:"<<std::endl;
248  //int* i(NULL);
249  //++*i;
250 #endif
251 
252  return Vector2(0,0,0);
253  }
254 #else
255  if(len>0)
256  {
257 
258  return Vector2(other.x()/len,other.y()/len);
259  }
260  else
261  {
262  std::cout<<"warning: normalize(const Vector2& other), Null length vector!"<<std::endl;// COUTOK
263  return Vector2(0,0);
264  }
265 #endif
266 }
267 #endif
268 
274 //CLASS_DECLSPEC
275 inline Vector2 operator-(const Vector2& in)
276 {
277 #if GEOM_PSEUDO3D==GEOM_TRUE
278  return Vector2(-in.x(),-in.y(),-in.z());
279 #else
280  return Vector2(-in.x(),-in.y());
281 #endif
282 }
283 
289 //CLASS_DECLSPEC
290 inline Vector2 operator*(double d,const Vector2& vec)
291 {
292 #if GEOM_PSEUDO3D==GEOM_TRUE
293  return Vector2(d*vec.x(),d*vec.y(),d*vec.z());
294 #else
295  return Vector2(d*vec.x(),d*vec.y());
296 #endif
297 }
298 
304 //CLASS_DECLSPEC
305 inline Vector2 operator+(const Vector2& vec0,const Vector2& vec1)
306 {
307 #if GEOM_PSEUDO3D==GEOM_TRUE
308  return Vector2(vec0.x()+vec1.x(), vec0.y()+vec1.y() , vec0.z()+vec1.z());
309 #else
310  return Vector2(vec0.x()+vec1.x(), vec0.y()+vec1.y() );
311 #endif
312 }
313 
319 //CLASS_DECLSPEC
320 inline Vector2 operator-(const Vector2& vec0,const Vector2& vec1)
321 {
322 #if GEOM_PSEUDO3D==GEOM_TRUE
323  return Vector2(vec0.x()-vec1.x(), vec0.y()-vec1.y() , vec0.z()-vec1.z());
324 #else
325  return Vector2(vec0.x()-vec1.x(), vec0.y()-vec1.y() );
326 #endif
327 }
328 
329 
330 } // (namespace)
Vector2 crossProduct(const Vector2 &vec0, const Vector2 &vec1)
Cross product.
Definition: Vector2.h:218
Vector2 operator-(const Vector2 &in)
Opposite vector.
Definition: Vector2.h:275
Vector2 operator+(const Vector2 &vec0, const Vector2 &vec1)
Addition.
Definition: Vector2.h:305
Vector2 operator*(double d, const Vector2 &vec)
Multiplication with a scalar.
Definition: Vector2.h:290
Vector.
Definition: Vector2.h:42
double sqLength() const
Get the squared length of the vector.
Vector2()
Default constructor.
double x() const
Get the x-value.
bool isDegenerate() const
isDegenerate
double y() const
Get the y-value.
double z() const
Get the z-value.
Vector2 orthogonalVector() const
Get a normalized, orthogonal 2D vector (CCW direction)
double length() const
Get the length of the vector.
Vector2(const double x_, const double y_, const double z_)
Constructor.
Vector2 operator/(double val) const
Division.
Vector2 operator*(double val) const
Multiplication.
double operator*(const Vector2 &other) const
Scalar product.
void set(const double x_, const double y_, const double z_)
Set the values.
Vector2 & operator=(const Vector2 &other)
Assignment operator.
int getMaxIndex() const
Get max index.
Vector2(const Vector2 &v_)
Copy constructor.