Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
includeLinks/Vector3D.hpp
Go to the documentation of this file.
1 /* *****************************************************************
2  MESQUITE -- The Mesh Quality Improvement Toolkit
3 
4  Copyright 2004 Sandia Corporation and Argonne National
5  Laboratory. Under the terms of Contract DE-AC04-94AL85000
6  with Sandia Corporation, the U.S. Government retains certain
7  rights in this software.
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  (lgpl.txt) along with this library; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 
23  diachin2@llnl.gov, djmelan@sandia.gov, mbrewer@sandia.gov,
24  pknupp@sandia.gov, tleurent@mcs.anl.gov, tmunson@mcs.anl.gov
25 
26  ***************************************************************** */
27 #ifndef MESQUITE_VECTOR3D_HPP
28 #define MESQUITE_VECTOR3D_HPP
29 
30 #include "Mesquite.hpp"
31 
32 #ifdef MSQ_USE_OLD_IO_HEADERS
33  class ostream;
34 #else
35 # include <iosfwd>
36 #endif
37 
38 #ifdef MSQ_USE_OLD_C_HEADERS
39 # include <assert.h>
40 # include <string.h>
41 #else
42 # include <cassert>
43 # include <cstring>
44 #endif
45 
54 namespace Mesquite
55 {
56  class Matrix3D;
57  class MsqError;
58 
64  class Vector3D
65  {
66  public:
67  // Constructors
68  Vector3D();
69  Vector3D(const double& x, const double& y, const double& z);
70  Vector3D(const double xyz[3]);
71  Vector3D(const Vector3D& to_copy);
72 
73  // *** virtual destructor *** Do not use for Vector3D, we need to keep
74  // the deallocation of those objects very fast
75 
76  // Functions to get the coordinates
77  double x() const;
78  double y() const;
79  double z() const;
80  void get_coordinates(double& x, double& y, double& z) const;
81  void get_coordinates(double xyz[3]) const;
82  const double& operator[](size_t index) const; // 0-based
83 
84  // Functions to set the coordinates.
85  void x(const double x);
86  void y(const double y);
87  void z(const double z);
88  void set(const double x, const double y, const double z);
89  void set(const double xyz[3]);
90  void set(const Vector3D& to_copy);
91  // Subscripts on non-consts both get and set coords
92  double& operator[](size_t index); // 0-based
93  Vector3D& operator=(const Vector3D &to_copy);
94  Vector3D& operator=(const double &to_copy);
95 
96  // Functions to modify existing coordinates
97  Vector3D operator-() const; //- unary negation.
98  Vector3D& operator*=(const double scalar);
99  Vector3D& operator/=(const double scalar);
100  Vector3D& operator*=(const Vector3D &rhs); //- cross product
101  Vector3D& operator+=(const Vector3D &rhs);
102  Vector3D& operator-=(const Vector3D &rhs);
103 
104  // Binary operators (like a+b).
105  friend const Vector3D operator+(const Vector3D &lhs,
106  const Vector3D &rhs);
107  friend const Vector3D operator-(const Vector3D &lhs,
108  const Vector3D &rhs);
109  friend const Vector3D operator*(const Vector3D &lhs,
110  const double scalar);
111  friend const Vector3D operator*(const double scalar,
112  const Vector3D &rhs);
113  friend const Vector3D operator/(const Vector3D &lhs,
114  const double scalar); //- lhs / scalar
115  friend double operator%(const Vector3D &v1,
116  const Vector3D &v2);
117  friend double inner(const Vector3D v1[],
118  const Vector3D v2[], int n);
119  friend double operator%(const double scalar,
120  const Vector3D &v2);
121  friend double operator%(const Vector3D &v1,
122  const double scalar);
123  friend const Vector3D operator*(const Vector3D &v1,
124  const Vector3D &v2);
125 
127  friend void eqAx(Vector3D& v, const Matrix3D& A, const Vector3D& x);
129  friend void plusEqAx(Vector3D& v, const Matrix3D& A, const Vector3D& x);
131  friend void plusEqTransAx(Vector3D& v, const Matrix3D& A, const Vector3D& x);
132 
133  // Comparison functions
134  friend bool operator==(const Vector3D &lhs, const Vector3D &rhs);
135  friend bool operator!=(const Vector3D &lhs, const Vector3D &rhs);
136  static double distance_between(const Vector3D& p1,
137  const Vector3D& p2);
138  int within_tolerance_box(const Vector3D &compare_to,
139  double tolerance) const;
140  //- Compare two Vector3Ds to see if they are spatially equal.
141  // Return TRUE if difference in x, y, and z are all within tolerance.
142  // Essentially checks to see if 'this' lies within a box centered on
143  // 'compare_to' with sides of length ('tolerance' * 2).
144 
145  // Length functions
146  inline double length_squared() const;
147  inline double length() const;
148  friend double length(Vector3D* const v,int n);
149  friend double Linf(Vector3D* const v,int n);
150 
151  inline void set_length(const double new_length);
152  inline void normalize();
153 
154  // Utility functions. All angle functions work in radians.
155  static double interior_angle(const Vector3D &a,
156  const Vector3D &b,
157  MsqError& err);
158  //- Interior angle: acos((a%b)/(|a||b|))
159  static Vector3D interpolate(const double param, const Vector3D &p1,
160  const Vector3D &p2);
161  //- Interpolate between two points. Returns (1-param)*v1 + param*v2.
162 
163  const double* to_array() const
164  { return mCoords; }
165 
166  protected:
167  double mCoords[3];
168  };
169 
170  // Constructors
172  {
173  mCoords[0] = 0;
174  mCoords[1] = 0;
175  mCoords[2] = 0;
176  }
177  inline Vector3D::Vector3D(const double &x,
178  const double &y,
179  const double &z)
180  {
181  mCoords[0] = x;
182  mCoords[1] = y;
183  mCoords[2] = z;
184  }
185  inline Vector3D::Vector3D(const double xyz[3])
186  { msq_stdc::memcpy(mCoords, xyz, 3*sizeof(double)); }
187  inline Vector3D::Vector3D(const Vector3D& to_copy)
188  { msq_stdc::memcpy(mCoords, to_copy.mCoords, 3*sizeof(double)); }
189 
190  // Functions to get coordinates
191  inline double Vector3D::x() const
192  { return mCoords[0]; }
193  inline double Vector3D::y() const
194  { return mCoords[1]; }
195  inline double Vector3D::z() const
196  { return mCoords[2]; }
197  inline void Vector3D::get_coordinates(double &x, double &y, double &z) const
198  {
199  x = mCoords[0];
200  y = mCoords[1];
201  z = mCoords[2];
202  }
203  inline void Vector3D::get_coordinates(double xyz[3]) const
204  { msq_stdc::memcpy(xyz, mCoords, 3*sizeof(double)); }
205  inline const double& Vector3D::operator[](size_t index) const
206  {
207  return mCoords[index];
208  }
209 
210  // Functions to set coordinates
211  inline void Vector3D::x( const double x )
212  { mCoords[0] = x; }
213  inline void Vector3D::y( const double y )
214  { mCoords[1] = y; }
215  inline void Vector3D::z( const double z )
216  { mCoords[2] = z; }
217  inline void Vector3D::set(const double x,
218  const double y,
219  const double z)
220  {
221  mCoords[0] = x;
222  mCoords[1] = y;
223  mCoords[2] = z;
224  }
225  inline void Vector3D::set(const double xyz[3])
226  { msq_stdc::memcpy(mCoords, xyz, 3*sizeof(double)); }
227  inline void Vector3D::set(const Vector3D& to_copy)
228  { msq_stdc::memcpy(mCoords, to_copy.mCoords, 3*sizeof(double)); }
229  inline double& Vector3D::operator[](size_t index)
230  { return mCoords[index]; }
231 
232  inline Vector3D& Vector3D::operator=(const Vector3D &to_copy)
233  {
234  mCoords[0] = to_copy.mCoords[0];
235  mCoords[1] = to_copy.mCoords[1];
236  mCoords[2] = to_copy.mCoords[2];
237 // memcpy(mCoords, to_copy.mCoords, 3*sizeof(double));
238  return *this;
239  }
240 
241  inline Vector3D& Vector3D::operator=(const double &to_copy)
242  {
243  mCoords[0] = to_copy;
244  mCoords[1] = to_copy;
245  mCoords[2] = to_copy;
246  return *this;
247  }
248 
249  // Functions that modify existing coordinates
251  {
252  return Vector3D(-mCoords[0], -mCoords[1], -mCoords[2]);
253  }
254  inline Vector3D& Vector3D::operator*=(const double scalar)
255  {
256  mCoords[0] *= scalar;
257  mCoords[1] *= scalar;
258  mCoords[2] *= scalar;
259  return *this;
260  }
262  inline Vector3D& Vector3D::operator/=(const double scalar)
263  {
264  mCoords[0] /= scalar;
265  mCoords[1] /= scalar;
266  mCoords[2] /= scalar;
267  return *this;
268  }
270  {
271  double new_coords[3] =
272  {mCoords[1]*rhs.mCoords[2] - mCoords[2]*rhs.mCoords[1],
273  mCoords[2]*rhs.mCoords[0] - mCoords[0]*rhs.mCoords[2],
274  mCoords[0]*rhs.mCoords[1] - mCoords[1]*rhs.mCoords[0]
275  };
276  msq_stdc::memcpy(mCoords, new_coords, 3*sizeof(double));
277  return *this;
278  }
280  {
281  mCoords[0] += rhs.mCoords[0];
282  mCoords[1] += rhs.mCoords[1];
283  mCoords[2] += rhs.mCoords[2];
284  return *this;
285  }
287  {
288  mCoords[0] -= rhs.mCoords[0];
289  mCoords[1] -= rhs.mCoords[1];
290  mCoords[2] -= rhs.mCoords[2];
291  return *this;
292  }
293 
294  // Binary operators
295  inline const Vector3D operator+(const Vector3D &lhs,
296  const Vector3D &rhs)
297  {
298  return Vector3D(lhs.x() + rhs.x(),
299  lhs.y() + rhs.y(),
300  lhs.z() + rhs.z());
301  }
302  inline const Vector3D operator-(const Vector3D &lhs,
303  const Vector3D &rhs)
304  {
305  return Vector3D(lhs.x() - rhs.x(),
306  lhs.y() - rhs.y(),
307  lhs.z() - rhs.z());
308  }
309  inline const Vector3D operator*(const Vector3D &lhs,
310  const double scalar)
311  {
312  return Vector3D(lhs.x() * scalar,
313  lhs.y() * scalar,
314  lhs.z() * scalar);
315  }
316  inline const Vector3D operator*(const double scalar,
317  const Vector3D &rhs)
318  {
319  return Vector3D(rhs.x() * scalar,
320  rhs.y() * scalar,
321  rhs.z() * scalar);
322  }
323  inline const Vector3D operator/(const Vector3D &lhs,
324  const double scalar)
325  {
326  assert (scalar != 0);
327  return Vector3D(lhs.x() / scalar,
328  lhs.y() / scalar,
329  lhs.z() / scalar);
330  }
331  inline double operator%(const Vector3D &lhs,
332  const Vector3D &rhs) // Dot Product
333  {
334  return( lhs.mCoords[0] * rhs.mCoords[0] +
335  lhs.mCoords[1] * rhs.mCoords[1] +
336  lhs.mCoords[2] * rhs.mCoords[2] );
337  }
338 
340  inline double inner(const Vector3D lhs[],
341  const Vector3D rhs[], int n)
342  {
343  int i;
344  double dot=0;
345  for (i=0; i<n; ++i)
346  dot+= lhs[i].mCoords[0] * rhs[i].mCoords[0] +
347  lhs[i].mCoords[1] * rhs[i].mCoords[1] +
348  lhs[i].mCoords[2] * rhs[i].mCoords[2];
349  return dot;
350  }
351 
352  inline double operator%(const double scalar,
353  const Vector3D &rhs) // Dot Product
354  {
355  return( scalar * (rhs.mCoords[0] + rhs.mCoords[1] + rhs.mCoords[2]) );
356  }
357  inline double operator%(const Vector3D &lhs,
358  const double scalar) // Dot Product
359  {
360  return( scalar * (lhs.mCoords[0] + lhs.mCoords[1] + lhs.mCoords[2]) );
361  }
362  inline const Vector3D operator*(const Vector3D &lhs,
363  const Vector3D &rhs) // Cross Product
364  {
365  return Vector3D(lhs.mCoords[1]*rhs.mCoords[2]-lhs.mCoords[2]*rhs.mCoords[1],
366  lhs.mCoords[2]*rhs.mCoords[0]-lhs.mCoords[0]*rhs.mCoords[2],
367  lhs.mCoords[0]*rhs.mCoords[1]-lhs.mCoords[1]*rhs.mCoords[0]);
368  }
369 
370  // output operator
371  msq_stdio::ostream& operator<<(msq_stdio::ostream &s, const Mesquite::Vector3D &v);
372 
373  inline double Vector3D::distance_between(const Vector3D &p1,
374  const Vector3D &p2)
375  {
376  Vector3D v = p2 - p1;
377  return v.length();
378  }
379  inline int Vector3D::within_tolerance_box(const Vector3D &compare_to,
380  double tolerance) const
381  {
382  return ((msq_stdc::fabs(this->mCoords[0] - compare_to.mCoords[0]) < tolerance) &&
383  (msq_stdc::fabs(this->mCoords[1] - compare_to.mCoords[1]) < tolerance) &&
384  (msq_stdc::fabs(this->mCoords[2] - compare_to.mCoords[2]) < tolerance));
385  }
386 
387  // Length functions
388  inline double Vector3D::length_squared() const
389  {
390  return (mCoords[0]*mCoords[0] +
391  mCoords[1]*mCoords[1] +
392  mCoords[2]*mCoords[2]);
393  }
394  inline double Vector3D::length() const
395  {
396  return msq_stdc::sqrt(mCoords[0]*mCoords[0] +
397  mCoords[1]*mCoords[1] +
398  mCoords[2]*mCoords[2]);
399  }
400  inline double length(Vector3D* const v,int n) // norm for an array of Vector3Ds
401  {
402  double l=0;
403  for (int j=0; j<n; ++j)
404  l += v[j].mCoords[0]*v[j].mCoords[0] +
405  v[j].mCoords[1]*v[j].mCoords[1] +
406  v[j].mCoords[2]*v[j].mCoords[2];
407  return msq_stdc::sqrt(l);
408  }
409  inline double Linf(Vector3D* const v,int n) // max entry for an array of Vector3Ds
410  {
411  double max=0;
412  //loop over the length of the array
413  for(int i=0;i<n;++i){
414  if ( max < msq_stdc::fabs(v[i][0]) ) max=msq_stdc::fabs(v[i][0]) ;
415  if ( max < msq_stdc::fabs(v[i][1]) ) max=msq_stdc::fabs(v[i][1]) ;
416  if ( max < msq_stdc::fabs(v[i][2]) ) max=msq_stdc::fabs(v[i][2]) ;
417  }
418  //return the value of the largest entry in the array
419  return max;
420  }
421  inline void Vector3D::set_length(const double new_length)
422  {
423  double factor = new_length / length();
424  *this *= factor;
425  }
426  inline void Vector3D::normalize()
427  { set_length(1.0); }
428 
429  // Utility functions.
430  inline Vector3D Vector3D::interpolate(const double param,
431  const Vector3D &p1,
432  const Vector3D &p2)
433  {
434  return (1-param)*p1 + param*p2;
435  }
436 
437  inline bool operator==( const Vector3D& v1, const Vector3D&v2 )
438  { return v1.mCoords[0] == v2.mCoords[0] &&
439  v1.mCoords[1] == v2.mCoords[1] &&
440  v1.mCoords[2] == v2.mCoords[2]; }
441 
442  inline bool operator!=( const Vector3D& v1, const Vector3D&v2 )
443  { return v1.mCoords[0] != v2.mCoords[0] ||
444  v1.mCoords[1] != v2.mCoords[1] ||
445  v1.mCoords[2] != v2.mCoords[2]; }
446 
447 } // namespace Mesquite
448 
449 #endif
Vector3D & operator=(const Vector3D &to_copy)
friend const Vector3D operator+(const Vector3D &lhs, const Vector3D &rhs)
static double interior_angle(const Vector3D &a, const Vector3D &b, MsqError &err)
friend bool operator==(const Vector3D &lhs, const Vector3D &rhs)
const Matrix3D operator*(const Matrix3D &A, const Matrix3D &B)
friend double inner(const Vector3D v1[], const Vector3D v2[], int n)
dot product for array
bool operator!=(const Matrix3D &lhs, const Matrix3D &rhs)
void int int REAL REAL * y
Definition: read.cpp:74
Used to hold the error state and return it to the application.
NT rhs
double s
Definition: blastest.C:80
Vector_n max(const Array_n_const &v1, const Array_n_const &v2)
Definition: Vector_n.h:354
NT p1
Vector3D is the object that effeciently stores information about about three-deminsional vectors...
friend void eqAx(Vector3D &v, const Matrix3D &A, const Vector3D &x)
const double & operator[](size_t index) const
double sqrt(double d)
Definition: double.h:73
Vector3D & operator/=(const double scalar)
divides each Vector3D entry by the given scalar.
friend const Vector3D operator*(const Vector3D &lhs, const double scalar)
lhs * scalar
friend double Linf(Vector3D *const v, int n)
L inf norm for array of Vector3Ds.
double length(Vector3D *const v, int n)
friend void plusEqTransAx(Vector3D &v, const Matrix3D &A, const Vector3D &x)
*********************************************************************Illinois Open Source License ****University of Illinois NCSA **Open Source License University of Illinois All rights reserved ****Developed free of to any person **obtaining a copy of this software and associated documentation to deal with the Software without including without limitation the rights to and or **sell copies of the and to permit persons to whom the **Software is furnished to do subject to the following this list of conditions and the following disclaimers ****Redistributions in binary form must reproduce the above **copyright this list of conditions and the following **disclaimers in the documentation and or other materials **provided with the distribution ****Neither the names of the Center for Simulation of Advanced the University of nor the names of its **contributors may be used to endorse or promote products derived **from this Software without specific prior written permission ****THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY **EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES **OF FITNESS FOR A PARTICULAR PURPOSE AND **NONINFRINGEMENT IN NO EVENT SHALL THE CONTRIBUTORS OR **COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN AN ACTION OF TORT OR **ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE **USE OR OTHER DEALINGS WITH THE SOFTWARE v
Definition: roccomf90.h:20
friend double operator%(const Vector3D &v1, const Vector3D &v2)
dot product
rational * A
Definition: vinci_lass.c:67
NVec< 3, double > Vector3D
void get_coordinates(double &x, double &y, double &z) const
void int int int REAL REAL REAL * z
Definition: write.cpp:76
3*3 Matric class, row-oriented, 0-based [i][j] indexing.
bool operator==(const Matrix3D &lhs, const Matrix3D &rhs)
const Matrix3D operator-(const Matrix3D &A, const Matrix3D &B)
blockLoc i
Definition: read.cpp:79
void int int REAL * x
Definition: read.cpp:74
static double distance_between(const Vector3D &p1, const Vector3D &p2)
const NT & n
Vector3D & operator*=(const double scalar)
int within_tolerance_box(const Vector3D &compare_to, double tolerance) const
Vector3D & operator+=(const Vector3D &rhs)
Vector3D & operator-=(const Vector3D &rhs)
double Linf(Vector3D *const v, int n)
friend void plusEqAx(Vector3D &v, const Matrix3D &A, const Vector3D &x)
const Matrix3D operator+(const Matrix3D &A, const Matrix3D &B)
void set_length(const double new_length)
double length_squared() const
j indices j
Definition: Indexing.h:6
static Vector3D interpolate(const double param, const Vector3D &p1, const Vector3D &p2)
const double * to_array() const
msq_stdio::ostream & operator<<(msq_stdio::ostream &s, const Matrix3D &A)
void set(const double x, const double y, const double z)
friend const Vector3D operator/(const Vector3D &lhs, const double scalar)
friend bool operator!=(const Vector3D &lhs, const Vector3D &rhs)
double inner(const Vector3D lhs[], const Vector3D rhs[], int n)
double operator%(const Vector3D &lhs, const Vector3D &rhs)
const Vector3D operator/(const Vector3D &lhs, const double scalar)
Vector3D operator-() const