SCIRun  5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Vector.h
Go to the documentation of this file.
1 /*
2  For more information, please see: http://software.sci.utah.edu
3 
4  The MIT License
5 
6  Copyright (c) 2009 Scientific Computing and Imaging Institute,
7  University of Utah.
8 
9 
10  Permission is hereby granted, free of charge, to any person obtaining a
11  copy of this software and associated documentation files (the "Software"),
12  to deal in the Software without restriction, including without limitation
13  the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  and/or sell copies of the Software, and to permit persons to whom the
15  Software is furnished to do so, subject to the following conditions:
16 
17  The above copyright notice and this permission notice shall be included
18  in all copies or substantial portions of the Software.
19 
20  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  DEALINGS IN THE SOFTWARE.
27 */
28 
29 /// @todo Documentation Core/GeometryPrimitives/Vector.h
30 
31 ///////////////////////////
32 // PORTED SCIRUN v4 CODE //
33 ///////////////////////////
34 
35 #ifndef CORE_GEOMETRY_VECTOR_H
36 #define CORE_GEOMETRY_VECTOR_H
37 
38 #include <cmath>
39 #include <algorithm>
43 
44 namespace SCIRun {
45 namespace Core {
46 namespace Geometry {
47 
48  /// @todo move to math header
49  template <typename T>
50  inline const T& Min(const T& t1, const T& t2, const T& t3)
51  {
52  return std::min(std::min(t1,t2), t3);
53  }
54 
55  template <typename T>
56  inline const T& Max(const T& t1, const T& t2, const T& t3)
57  {
58  return std::max(std::max(t1,t2), t3);
59  }
60 
61 class Point;
62 
63 class Vector
64 {
65  private:
66  double d_[3];
67  public:
68  inline explicit Vector(const Point&);
69  inline Vector(double x, double y, double z)
70  { d_[0] = x; d_[1] = y; d_[2] = z; }
71  inline Vector(const Vector&);
72  inline Vector();
73  inline explicit Vector(double init)
74  { d_[0] = init; d_[1] = init; d_[2] = init; }
75  inline double length() const;
76  inline double length2() const;
77  friend inline double Dot(const Vector&, const Vector&);
78  friend inline double Dot(const Point&, const Vector&);
79  friend inline double Dot(const Vector&, const Point&);
80  inline Vector& operator=(const Vector&);
81  inline Vector& operator=(const double&);
82  inline Vector& operator=(const int&);
83 
84  //Note vector[0]=vector.x();vector[1]=vector.y();vector[2]=vector.z()
85  inline double& operator[](int idx)
86  {
87  return d_[idx];
88  }
89 
90  //Note vector[0]=vector.x();vector[1]=vector.y();vector[2]=vector.z()
91  inline double operator[](int idx) const
92  {
93  return d_[idx];
94  }
95 
96  inline Vector operator*(const double) const;
97  inline Vector operator*(const Vector&) const;
98  inline Vector& operator*=(const double);
99  inline Vector& operator*=(const Vector&);
100  inline Vector operator/(const double) const;
101  inline Vector operator/(const Vector&) const;
102  inline Vector& operator/=(const double);
103  inline Vector operator+(const Vector&) const;
104  inline Vector& operator+=(const Vector&);
105  inline Vector operator-() const;
106  inline Vector operator-(const Vector&) const;
107  inline Vector operator-(const Point&) const;
108  inline Vector& operator-=(const Vector&);
109  inline double normalize();
110  inline double safe_normalize();
111  SCISHARE Vector normal() const;
112  SCISHARE Vector safe_normal() const;
113  friend inline Vector Cross(const Vector&, const Vector&);
114  friend inline Vector Abs(const Vector&);
115  inline void x(double);
116  inline double x() const;
117  inline void y(double);
118  inline double y() const;
119  inline void z(double);
120  inline double z() const;
121 
122  inline void u(double);
123  inline double u() const;
124  inline void v(double);
125  inline double v() const;
126  inline void w(double);
127  inline double w() const;
128 
129  SCISHARE void rotz90(const int);
130  SCISHARE std::string get_string() const;
131 
132  friend class Point;
133 
134  friend inline Vector Interpolate(const Vector&, const Vector&, double);
135 
136  SCISHARE void find_orthogonal(Vector&, Vector&) const;
138 
139  inline const Point &point() const;
140  inline Point &asPoint() const;
141 
142  inline double minComponent() const;
143  inline double maxComponent() const;
144 
145  inline void Set(double x, double y, double z);
146 };
147 
148 SCISHARE bool operator==(const Vector& v1, const Vector& v2);
149 SCISHARE bool operator!=(const Vector& v1, const Vector& v2);
150 SCISHARE std::ostream& operator<<(std::ostream& os, const Vector& p);
151 SCISHARE std::istream& operator>>(std::istream& os, Vector& p);
152 
154 {
155  d_[0] = 0.0;
156  d_[1] = 0.0;
157  d_[2] = 0.0;
158 }
159 
160 inline Vector::Vector(const Vector& p)
161 {
162  d_[0] = p.d_[0];
163  d_[1] = p.d_[1];
164  d_[2] = p.d_[2];
165 }
166 
168 {
169  d_[0]=v.d_[0];
170  d_[1]=v.d_[1];
171  d_[2]=v.d_[2];
172  return *this;
173 }
174 
175 inline void Vector::x(double d)
176 {
177  d_[0]=d;
178 }
179 
180 inline double Vector::x() const
181 {
182  return d_[0];
183 }
184 
185 inline void Vector::y(double d)
186 {
187  d_[1]=d;
188 }
189 
190 inline double Vector::y() const
191 {
192  return d_[1];
193 }
194 
195 inline void Vector::z(double d)
196 {
197  d_[2]=d;
198 }
199 
200 inline double Vector::z() const
201 {
202  return d_[2];
203 }
204 
205 inline Vector& Vector::operator*=(const double d)
206 {
207  d_[0]*=d;
208  d_[1]*=d;
209  d_[2]*=d;
210  return *this;
211 }
212 
213 // Allows for double * Vector so that everything doesn't have to be
214 // Vector * double
215 inline Vector operator*(const double s, const Vector& v) {
216  return v*s;
217 }
218 
219 inline Vector Min(const Vector &v1, const Vector &v2)
220 {
221  return Vector(std::min(v1.x(), v2.x()),
222  std::min(v1.y(), v2.y()),
223  std::min(v1.z(), v2.z()));
224 }
225 
226 inline Vector Max(const Vector &v1, const Vector &v2)
227 {
228  return Vector(std::max(v1.x(), v2.x()),
229  std::max(v1.y(), v2.y()),
230  std::max(v1.z(), v2.z()));
231 }
232 
233 SCISHARE void Pio( Piostream&, Vector& );
234 
235 inline
237 {
238  double l = std::sqrt(d_[0]*d_[0] + d_[1]*d_[1] + d_[2]*d_[2]);
239  if (l > 0.0)
240  {
241  d_[0]/=l;
242  d_[1]/=l;
243  d_[2]/=l;
244  }
245  return l;
246 }
247 
248 inline double Vector::length2() const
249 {
250  return d_[0]*d_[0]+d_[1]*d_[1]+d_[2]*d_[2];
251 }
252 
253 
254 inline double Vector::minComponent() const
255 {
256  return Min(d_[0], d_[1], d_[2]);
257 }
258 
259 inline double Vector::maxComponent() const
260 {
261  return Max(d_[0], d_[1], d_[2]);
262 }
263 
264 inline Vector Vector::operator/(const double d) const
265 {
266  return Vector(d_[0]/d, d_[1]/d, d_[2]/d);
267 }
268 
269 inline Vector Vector::operator/(const Vector& v2) const
270 {
271  return Vector(d_[0]/v2.d_[0], d_[1]/v2.d_[1], d_[2]/v2.d_[2]);
272 }
273 
274 inline Vector Vector::operator+(const Vector& v2) const
275 {
276  return Vector(d_[0]+v2.d_[0], d_[1]+v2.d_[1], d_[2]+v2.d_[2]);
277 }
278 
279 inline Vector Vector::operator*(const Vector& v2) const
280 {
281  return Vector(d_[0]*v2.d_[0], d_[1]*v2.d_[1], d_[2]*v2.d_[2]);
282 }
283 
284 inline Vector Vector::operator-(const Vector& v2) const
285 {
286  return Vector(d_[0]-v2.d_[0], d_[1]-v2.d_[1], d_[2]-v2.d_[2]);
287 }
288 
289 
290 inline Vector& Vector::operator=(const double& d)
291 {
292  d_[0] = d;
293  d_[1] = d;
294  d_[2] = d;
295  return *this;
296 }
297 
298 inline Vector& Vector::operator=(const int& d)
299 {
300  d_[0] = static_cast<double>(d);
301  d_[1] = static_cast<double>(d);
302  d_[2] = static_cast<double>(d);
303  return *this;
304 }
305 
306 inline bool operator<(Vector v1, Vector v2)
307 {
308  return(v1.length()<v2.length());
309 }
310 
311 inline bool operator<=(Vector v1, Vector v2)
312 {
313  return(v1.length()<=v2.length());
314 }
315 
316 inline bool operator>(Vector v1, Vector v2)
317 {
318  return(v1.length()>v2.length());
319 }
320 
321 inline bool operator>=(Vector v1, Vector v2)
322 {
323  return(v1.length()>=v2.length());
324 }
325 
326 
327 
328 inline Vector Vector::operator*(const double s) const
329 {
330  return Vector(d_[0]*s, d_[1]*s, d_[2]*s);
331 }
332 
334 {
335  d_[0] *= v.d_[0];
336  d_[1] *= v.d_[1];
337  d_[2] *= v.d_[2];
338  return *this;
339 }
340 
341 
342 
343 
344 inline Vector& Vector::operator+=(const Vector& v2)
345 {
346  d_[0]+=v2.d_[0];
347  d_[1]+=v2.d_[1];
348  d_[2]+=v2.d_[2];
349  return *this;
350 }
351 
352 inline Vector& Vector::operator-=(const Vector& v2)
353 {
354  d_[0]-=v2.d_[0];
355  d_[1]-=v2.d_[1];
356  d_[2]-=v2.d_[2];
357  return *this;
358 }
359 
360 inline Vector Vector::operator-() const
361 {
362  return Vector(-d_[0],-d_[1],-d_[2]);
363 }
364 
365 inline double Vector::length() const
366 {
367  return sqrt(d_[0]*d_[0]+d_[1]*d_[1]+d_[2]*d_[2]);
368 }
369 
370 inline Vector Abs(const Vector& v)
371 {
372  double x=v.d_[0]<0?-v.d_[0]:v.d_[0];
373  double y=v.d_[1]<0?-v.d_[1]:v.d_[1];
374  double z=v.d_[2]<0?-v.d_[2]:v.d_[2];
375  return Vector(x,y,z);
376 }
377 
378 inline Vector Cross(const Vector& v1, const Vector& v2)
379 {
380  return Vector(v1.d_[1]*v2.d_[2]-v1.d_[2]*v2.d_[1],
381  v1.d_[2]*v2.d_[0]-v1.d_[0]*v2.d_[2],
382  v1.d_[0]*v2.d_[1]-v1.d_[1]*v2.d_[0]);
383 }
384 
385 inline Vector Interpolate(const Vector& v1, const Vector& v2,
386  double weight)
387 {
388  double weight1=1.0-weight;
389  return Vector(v2.d_[0]*weight+v1.d_[0]*weight1,
390  v2.d_[1]*weight+v1.d_[1]*weight1,
391  v2.d_[2]*weight+v1.d_[2]*weight1);
392 }
393 
394 
395 
396 inline Vector& Vector::operator/=(const double d)
397 {
398  d_[0]/=d;
399  d_[1]/=d;
400  d_[2]/=d;
401  return *this;
402 }
403 
404 inline void Vector::u(double d)
405 {
406  d_[0]=d;
407 }
408 
409 inline double Vector::u() const
410 {
411  return d_[0];
412 }
413 
414 inline void Vector::v(double d)
415 {
416  d_[1]=d;
417 }
418 
419 inline double Vector::v() const
420 {
421  return d_[1];
422 }
423 
424 inline void Vector::w(double d)
425 {
426  d_[2]=d;
427 }
428 
429 inline double Vector::w() const
430 {
431  return d_[2];
432 }
433 
434 
435 
436 inline
438 {
439  double l=sqrt(d_[0]*d_[0] + d_[1]*d_[1] + d_[2]*d_[2]);
440  if (l > 0.0)
441  {
442  d_[0]/=l;
443  d_[1]/=l;
444  d_[2]/=l;
445  }
446  return l;
447 }
448 
449 
450 
451 
452 inline const Point &Vector::point() const
453 {
454  return reinterpret_cast<const Point &>(*this);
455 }
456 
457 inline Point &Vector::asPoint() const
458 {
459  return reinterpret_cast<Point &>(const_cast<Vector &>(*this));
460 }
461 
462 
463 
464 inline void Vector::Set(double x, double y, double z)
465 {
466  d_[0] = x;
467  d_[1] = y;
468  d_[2] = z;
469 }
470 
472 
473 }}
474 /// @todo: This one is obsolete when dynamic compilation will be abandoned
475 const std::string& Vector_get_h_file_path();
476 }
477 
478 #endif
bool operator<=(Tensor t1, Tensor t2)
Definition: Tensor.h:141
Vector()
Definition: Vector.h:153
Vector & operator/=(const double)
Definition: Vector.h:396
Point Min(const Point &p1, const Point &p2)
Definition: Point.h:194
Vector operator-() const
Definition: Vector.h:360
SCISHARE const TypeDescription * get_type_description(Tensor *)
Definition: Tensor.cc:405
Vector operator*(const double) const
Definition: Vector.h:328
Definition: Persistent.h:89
const std::string & Vector_get_h_file_path()
Definition: Vector.cc:190
SCISHARE Vector safe_normal() const
Vector & operator=(const Vector &)
Definition: Vector.h:167
Definition: Point.h:49
double y() const
Definition: Vector.h:190
Definition: TypeDescription.h:45
#define SCISHARE
Definition: share.h:39
SCISHARE std::string get_string() const
friend Vector Interpolate(const Vector &, const Vector &, double)
Definition: Vector.h:385
Point & asPoint() const
Definition: Vector.h:457
void Set(double x, double y, double z)
Definition: Vector.h:464
Vector operator+(const Vector &) const
Definition: Vector.h:274
SCISHARE std::istream & operator>>(std::istream &os, Point &p)
Definition: Point.cc:105
Definition: Vector.h:63
double u() const
Definition: Vector.h:409
double & operator[](int idx)
Definition: Vector.h:85
Vector(double init)
Definition: Vector.h:73
double length2() const
Definition: Vector.h:248
double maxComponent() const
Definition: Vector.h:259
SCISHARE std::ostream & operator<<(std::ostream &os, const Point &p)
Definition: Point.cc:99
Vector & operator-=(const Vector &)
Definition: Vector.h:352
Vector Cross(const Vector &v1, const Vector &v2)
Definition: Vector.h:378
double minComponent() const
Definition: Vector.h:254
SCISHARE void find_orthogonal(Vector &, Vector &) const
Definition: Vector.cc:59
bool operator>=(Tensor t1, Tensor t2)
Definition: Tensor.h:151
Vector(double x, double y, double z)
Definition: Vector.h:69
Point Interpolate(const Point &v1, const Point &v2, double weight)
Definition: PointVectorOperators.h:156
double x() const
Definition: Vector.h:180
void x(double)
Definition: Vector.h:175
SCISHARE bool check_find_orthogonal(Vector &, Vector &) const
double operator[](int idx) const
Definition: Vector.h:91
v
Definition: readAllFields.py:42
double normalize()
Definition: Vector.h:437
SCISHARE bool operator!=(const Point &p1, const Point &p2)
Definition: Point.cc:49
void y(double)
Definition: Vector.h:185
double safe_normalize()
Definition: Vector.h:236
Point Max(const Point &p1, const Point &p2)
Definition: Point.h:202
double w() const
Definition: Vector.h:429
Vector operator/(const double) const
Definition: Vector.h:264
double length() const
Definition: Vector.h:365
double z() const
Definition: Vector.h:200
bool operator>(Tensor t1, Tensor t2)
Definition: Tensor.h:146
SCISHARE Vector normal() const
double v() const
Definition: Vector.h:419
Vector Abs(const Vector &v)
Definition: Vector.h:370
Vector & operator+=(const Vector &)
Definition: Vector.h:344
SCISHARE bool operator==(const Point &p1, const Point &p2)
Definition: Point.cc:44
Vector & operator*=(const double)
Definition: Vector.h:205
Point operator*(double d, const Point &p)
Definition: Point.h:182
SCISHARE void rotz90(const int)
void z(double)
Definition: Vector.h:195
const Point & point() const
Definition: Vector.h:452
bool operator<(Tensor t1, Tensor t2)
Definition: Tensor.h:136
friend Vector Cross(const Vector &, const Vector &)
Definition: Vector.h:378
SCISHARE void Pio(Piostream &, BBox &)
Definition: BBox.cc:134
friend Vector Abs(const Vector &)
Definition: Vector.h:370
friend double Dot(const Vector &, const Vector &)
Definition: PointVectorOperators.h:89