Geom Software - C++ Programming and Geometry Libraries
WOF Documentation pages v1.19
Vector3.h
1 // Copyright (C) Geom Software e.U, Bernhard Kornberger, Graz/Austria
2 //
3 // This file is part of the WOF software. WOF is commercial software.
4 // Users holding a license may use this file in accordance with the
5 // License Agreement.
6 //
7 // This software is provided AS IS with NO WARRANTY OF ANY KIND,
8 // INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
9 // FOR A PARTICULAR PURPOSE.
10 //
11 // Please contact the author if any conditions of this licensing are
12 // not clear to you.
13 //
14 // Author: Bernhard Kornberger, bkorn (at) geom.at
15 // http://www.geom.at
16 
17 #pragma once
18 #include <iostream>
19 #include "wof_api_definitions.h" // for CLASS_DECLSPEC
20 
21 namespace GEOM_WOF {
22 
23 
26 class CLASS_DECLSPEC Vector3
27 {
28 public:
29 
34 Vector3(const double x_,const double y_,const double z_):valX(x_),valY(y_),valZ(z_)
35 {}
36 
41 Vector3():valX(0),valY(0),valZ(0)
42 {}
43 
48 Vector3(const Vector3& v_):valX(v_.x()),valY(v_.y()),valZ(v_.z())
49 {}
50 
55 bool isDegenerate() const
56 {
57  return (valX==0 && valY==0 && valZ==0);
58 }
59 
61 bool isNV() const
62 {
63  double sq(valX*valX+valY*valY+valZ*valZ);
64  double err(1.0-sq);
65  return (err>-.03 && err<.03);
66 }
67 
68 
73 void xyz(double& x_,double& y_,double& z_) const
74 {
75  x_=valX;
76  y_=valY;
77  z_=valZ;
78 }
83 void reset()
84 {
85  valX=0;
86  valY=0;
87  valZ=0;
88 }
93 inline double x() const
94 {
95  return valX;
96 }
97 
98 
102 inline double y() const
103 {
104  return valY;
105 }
106 
107 
108 
112 inline double z() const
113 {
114  return valZ;
115 }
116 
123 void set(const double x_,const double y_,const double z_);
124 
129 void add(const Vector3& other)
130 {
131  valX+=other.valX;
132  valY+=other.valY;
133  valZ+=other.valZ;
134 }
135 void sub(const Vector3& other)
136 {
137  valX-=other.valX;
138  valY-=other.valY;
139  valZ-=other.valZ;
140 }
141 void div(double div)
142 {
143  valX/=div;
144  valY/=div;
145  valZ/=div;
146 }
147 void mul(double mul)
148 {
149  valX*=mul;
150  valY*=mul;
151  valZ*=mul;
152 }
153 
158 double sqLength() const;
159 
160 
161 
166 int getMaxAbsIndex() const;
167 
168 
173 double getMaxComponent() const
174 {
175  if(valX>=valY && valX>=valZ) return valX;
176  if(valY>=valZ) return valY;
177  else return valZ;
178 }
183 double getMaxAbsComponent() const;
184 
185 
191 double getCartesian(int i) const;
192 
193 
198 double length() const;
199 
204 double operator*(const Vector3& other) const;
205 
206 
207 
208 
213 Vector3 operator*(double val) const;
214 
219 Vector3 operator/(double val) const;
220 
223 Vector3& operator=(const Vector3& other)
224 {
225  valX=other.valX;
226  valY=other.valY;
227  valZ=other.valZ;
228  return *this;
229 }
230 
232 void addOwnCoords(double& x,double& y,double& z) const
233 {
234  x+=valX;
235  y+=valY;
236  z+=valZ;
237 }
238 
239 Vector3& operator+=(const Vector3& other);
240 Vector3& operator-=(const Vector3& other);
241 Vector3& operator/=(double div);
242 Vector3& operator*=(double mul);
243 
244 
245 
246 protected:
247 
248 double valX;
249 double valY;
250 double valZ;
251 };
252 
253 
254 
255 
256 // Free functions
257 inline std::ostream &operator<<(std::ostream &stream, const Vector3& vec)
258 {
259  stream << "Vector3: "<<vec.x()<<", "<<vec.y()<<", "<<vec.z();
260  return stream;
261 }
262 
263 
266 inline Vector3 crossProduct(const Vector3& vec0,const Vector3& vec1)
267 {
268 
269  double x=vec0.y() * vec1.z() - vec0.z() * vec1.y();
270  double y=vec0.z() * vec1.x() - vec0.x() * vec1.z();
271  double z=vec0.x() * vec1.y() - vec0.y() * vec1.x();
272  return Vector3(x,y,z);
273 }
274 
275 
278 // CLASS_DECLSPEC
279 // Vector3 normalize(const Vector3& other);
280 // Free function
281 inline Vector3 normalize(const Vector3& other)
282 {
283  double len(other.length());
284  if(len>0)
285  {
286  return Vector3(other.x()/len,other.y()/len,other.z()/len);
287  }
288  else
289  {
290 #ifdef DEVMODE
291  std::cerr<<"\n\n** warning: normalize(const Vector3& other), Null length vector! Will segfault now"<<std::endl;
292  std::cerr<<"other.x()="<<other.x()<<std::endl;
293  std::cerr<<"other.y()="<<other.y()<<std::endl;
294  std::cerr<<"other.z()="<<other.z()<<std::endl;
295  if(other.x()==0.0) std::cerr<<"other.x()==0.0"<<std::endl;
296  if(other.y()==0.0) std::cerr<<"other.y()==0.0"<<std::endl;
297  if(other.z()==0.0) std::cerr<<"other.z()==0.0"<<std::endl;
298  // Deliberate segfault (to get a quick backtrace)
299  int* i(NULL);
300  ++*i;
301 #endif
302  return Vector3(0,0,0);
303  }
304 }
305 
306 
307 inline Vector3 operator-(const Vector3& in)
308 {
309  return Vector3(-in.x(),-in.y(),-in.z());
310 }
311 
312 inline Vector3 operator*(double d,const Vector3& vec)
313 {
314  return Vector3(d*vec.x(),d*vec.y(),d*vec.z());
315 }
316 
317 
318 inline Vector3 operator+(const Vector3& vec0,const Vector3& vec1)
319 {
320  return Vector3(vec0.x()+vec1.x(), vec0.y()+vec1.y() , vec0.z()+vec1.z());
321 }
322 
323 inline Vector3 operator-(const Vector3& vec0,const Vector3& vec1)
324 {
325  return Vector3(vec0.x()-vec1.x(), vec0.y()-vec1.y() , vec0.z()-vec1.z());
326 }
327 
328 
329 
330 } // (namespace)
3D Vector
Definition: Vector3.h:27
double z() const
Get the z-value.
Definition: Vector3.h:112
void set(const double x_, const double y_, const double z_)
Set x,y,z.
double sqLength() const
Get the squared length of the vector.
double operator*(const Vector3 &other) const
Scalar product.
double getCartesian(int i) const
Get component i.
Vector3 & operator=(const Vector3 &other)
Assignment operator.
Definition: Vector3.h:223
Vector3(const Vector3 &v_)
Copy constructor.
Definition: Vector3.h:48
double x() const
Get the x-value.
Definition: Vector3.h:93
void add(const Vector3 &other)
Add a Vector3 to the present one.
Definition: Vector3.h:129
Vector3(const double x_, const double y_, const double z_)
Constructor.
Definition: Vector3.h:34
double getMaxComponent() const
Get max component.
Definition: Vector3.h:173
void reset()
reset
Definition: Vector3.h:83
Vector3()
Default constructor.
Definition: Vector3.h:41
double getMaxAbsComponent() const
Get max absolute component.
int getMaxAbsIndex() const
Get max index.
Vector3 operator*(double val) const
Multiply by a scalar value.
Vector3 operator/(double val) const
Divide by a scalar value.
bool isDegenerate() const
isDegenerate
Definition: Vector3.h:55
void xyz(double &x_, double &y_, double &z_) const
Get x,y,z.
Definition: Vector3.h:73
double length() const
Get the length of the vector.
double y() const
Get the y-value.
Definition: Vector3.h:102
Definition: Point3.h:23
Vector3 normalize(const Vector3 &other)
Normalize.
Definition: Vector3.h:281
Vector3 crossProduct(const Vector3 &vec0, const Vector3 &vec1)
Cross product.
Definition: Vector3.h:266