Geom Software - C++ Programming and Geometry Libraries
Fade2.5D Documentation pages v1.73
Vector2.h
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 #pragma once
18 #include "common.h"
19 
20 #if GEOM_PSEUDO3D==GEOM_TRUE
21  namespace GEOM_FADE25D {
22 #elif GEOM_PSEUDO3D==GEOM_FALSE
23  namespace GEOM_FADE2D {
24 #else
25  #error GEOM_PSEUDO3D is not defined
26 #endif
27 
28 class CLASS_DECLSPEC Vector2;
29 
34 class Vector2
35 {
36 public:
37 
38 #if GEOM_PSEUDO3D==GEOM_TRUE
39 
42  Vector2(const double x_,const double y_,const double z_);
43 
48  Vector2();
49 
55  Vector2(const Vector2& v_);
56 
57 #else
58 
63  Vector2(const double x_,const double y_);
69  Vector2();
75  Vector2(const Vector2& v_);
76 #endif
77 
78 
79 
80 #if GEOM_PSEUDO3D==GEOM_TRUE
81 
85 #else
86 
88 #endif
89 
90  Vector2 orthogonalVector() const;
91 
96  bool isDegenerate() const;
97 
98 
101  double x() const;
102 
105  double y() const;
106 
107 #if GEOM_PSEUDO3D==GEOM_TRUE
108 
111  double z() const;
112 #endif
113 
114 #if GEOM_PSEUDO3D==GEOM_TRUE
115 
117  void set(const double x_,const double y_,const double z_);
118 
119 #else
120 
122  void set(const double x_,const double y_);
123 #endif
124 
127  double sqLength() const;
128 
129 
130 #if GEOM_PSEUDO3D==GEOM_TRUE
131 
135  int getMaxIndex() const;
136 #endif
137 
141  double length() const;
142 
145 #if GEOM_PSEUDO3D==GEOM_TRUE
146 
147  double operator*(const Vector2& other) const;
148 #else
149 
150  double operator*(const Vector2& other) const;
151 #endif
152 
153 
156 #if GEOM_PSEUDO3D==GEOM_TRUE
157 
158  Vector2 operator*(double val) const;
159 #else
160 
161  Vector2 operator*(double val) const;
162 #endif
163 
166 #if GEOM_PSEUDO3D==GEOM_TRUE
167 
168  Vector2 operator/(double val) const;
169 #else
170 
171  Vector2 operator/(double val) const;
172 #endif
173 
174 
175 
176 protected:
177  double valX;
178  double valY;
179 #if GEOM_PSEUDO3D==GEOM_TRUE
180  double valZ;
181 #endif
182 };
183 
184 
185 
186 
187 // Free functions
188 
189 CLASS_DECLSPEC
190 inline std::ostream &operator<<(std::ostream &stream, const Vector2& vec)
191 {
192 #if GEOM_PSEUDO3D==GEOM_TRUE
193  stream << "Vector2: "<<vec.x()<<", "<<vec.y()<<", "<<vec.z();
194 #else
195  stream << "Vector2: "<<vec.x()<<", "<<vec.y();
196 #endif
197  return stream;
198 }
199 
200 #if GEOM_PSEUDO3D==GEOM_TRUE
201 
203 CLASS_DECLSPEC
204 inline Vector2 crossProduct(const Vector2& vec0,const Vector2& vec1)
205 {
206 
207  double x=vec0.y() * vec1.z() - vec0.z() * vec1.y();
208  double y=vec0.z() * vec1.x() - vec0.x() * vec1.z();
209  double z=vec0.x() * vec1.y() - vec0.y() * vec1.x();
210  return Vector2(x,y,z);
211 }
212 #endif
213 
214 
215 
216 
219 CLASS_DECLSPEC
220 inline Vector2 normalize(const Vector2& other)
221 {
222  double len(other.length());
223 #if GEOM_PSEUDO3D==GEOM_TRUE
224  if(len>0)
225  {
226  return Vector2(other.x()/len,other.y()/len,other.z()/len);
227  }
228  else
229  {
230 
231 #ifndef NDEBUG
232  std::cout<<"\n\n** warning: normalize(const Vector2& other), Null length vector!"<<std::endl;// COUTOK
234  //std::cout<<"vec="<<other<<std::endl;
235  //std::cout<<"** debug mode -> deliberate segfault, now:"<<std::endl;
236  //int* i(NULL);
237  //++*i;
238 #endif
239 
240  return Vector2(0,0,0);
241  }
242 #else
243  if(len>0)
244  {
245 
246  return Vector2(other.x()/len,other.y()/len);
247  }
248  else
249  {
250  std::cout<<"warning: normalize(const Vector2& other), Null length vector!"<<std::endl;// COUTOK
251  return Vector2(0,0);
252  }
253 #endif
254 }
255 
256 CLASS_DECLSPEC
257 inline Vector2 operator-(const Vector2& in)
258 {
259 #if GEOM_PSEUDO3D==GEOM_TRUE
260  return Vector2(-in.x(),-in.y(),-in.z());
261 #else
262  return Vector2(-in.x(),-in.y());
263 #endif
264 }
265 
266 CLASS_DECLSPEC
267 inline Vector2 operator*(double d,const Vector2& vec)
268 {
269 #if GEOM_PSEUDO3D==GEOM_TRUE
270  return Vector2(d*vec.x(),d*vec.y(),d*vec.z());
271 #else
272  return Vector2(d*vec.x(),d*vec.y());
273 #endif
274 }
275 
276 CLASS_DECLSPEC
277 inline Vector2 operator+(const Vector2& vec0,const Vector2& vec1)
278 {
279 #if GEOM_PSEUDO3D==GEOM_TRUE
280  return Vector2(vec0.x()+vec1.x(), vec0.y()+vec1.y() , vec0.z()+vec1.z());
281 #else
282  return Vector2(vec0.x()+vec1.x(), vec0.y()+vec1.y() );
283 #endif
284 }
285 
286 CLASS_DECLSPEC
287 inline Vector2 operator-(const Vector2& vec0,const Vector2& vec1)
288 {
289 #if GEOM_PSEUDO3D==GEOM_TRUE
290  return Vector2(vec0.x()-vec1.x(), vec0.y()-vec1.y() , vec0.z()-vec1.z());
291 #else
292  return Vector2(vec0.x()-vec1.x(), vec0.y()-vec1.y() );
293 #endif
294 }
295 
296 
297 } // (namespace)
double length() const
Get the length of the vector.
double y() const
Get the y-value.
double z() const
Get the z-value.
Vector.
Definition: Vector2.h:34
Definition: Bbox2.h:23
double x() const
Get the x-value.