Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vector3d.h
Go to the documentation of this file.
1 /* *******************************************************************
2  * Illinois Open Source License *
3  * *
4  * University of Illinois/NCSA *
5  * Open Source License *
6  * *
7  * Copyright@2008, University of Illinois. All rights reserved. *
8  * *
9  * Developed by: *
10  * *
11  * Center for Simulation of Advanced Rockets *
12  * *
13  * University of Illinois *
14  * *
15  * www.csar.uiuc.edu *
16  * *
17  * Permission is hereby granted, free of charge, to any person *
18  * obtaining a copy of this software and associated documentation *
19  * files (the "Software"), to deal with the Software without *
20  * restriction, including without limitation the rights to use, *
21  * copy, modify, merge, publish, distribute, sublicense, and/or *
22  * sell copies of the Software, and to permit persons to whom the *
23  * Software is furnished to do so, subject to the following *
24  * conditions: *
25  * *
26  * *
27  * @ Redistributions of source code must retain the above copyright *
28  * notice, this list of conditions and the following disclaimers. *
29  * *
30  * @ Redistributions in binary form must reproduce the above *
31  * copyright notice, this list of conditions and the following *
32  * disclaimers in the documentation and/or other materials *
33  * provided with the distribution. *
34  * *
35  * @ Neither the names of the Center for Simulation of Advanced *
36  * Rockets, the University of Illinois, nor the names of its *
37  * contributors may be used to endorse or promote products derived *
38  * from this Software without specific prior written permission. *
39  * *
40  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
41  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
42  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
43  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
44  * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
45  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
46  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
47  * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
48  *********************************************************************
49  * Please acknowledge The University of Illinois Center for *
50  * Simulation of Advanced Rockets in works and publications *
51  * resulting from this software or its derivatives. *
52  *********************************************************************/
53 /*
54 Orion's Standard Library
55 Orion Sky Lawlor, 11/3/1999
56 NAME: vector3d.h
57 
58 DESCRIPTION: C++ 3-Dimentional vector library (no templates)
59 
60 This file provides various utility routines for easily
61 manipulating 3-D vectors-- included are arithmetic,
62 dot/cross product, magnitude and normalization terms.
63 Most routines are provided right in the header file (for inlining).
64 */
65 
66 #ifndef __OSL_VECTOR_3D_H
67 #define __OSL_VECTOR_3D_H
68 
69 class vector3d;
70 //Polar3d is a point expressed in a 3D spherical coordinate system--
71 // theta is the angle (right-handed about +z) in the x-y axis (longitude);
72 // phi is the angle up (toward +z) from the x-y axis (latitude);
73 // r is the distance of the point from the origin.
74 class polar3d {
75 public:
76  real theta, phi;//Angles in radians
77  real r;//Distance from origin
78 //Nothing too useful can be done here, except convert to/from a vector3d (see below)
79  polar3d() {} //Default constructor
80  polar3d(real Nt,real Np,real Nr) {theta=Nt;phi=Np;r=Nr;}
81  polar3d(const vector3d &v);
82 };
83 
84 //Vector3d is a cartesian vector in 3-space-- an x, y, and z.
85 // For cross products, the space is assumed to be right-handed (x cross y = +z)
86 class vector3d {
87 public:
88  real x,y,z;
89  vector3d(void) {}//Default consructor
90  //Simple 1-value constructor
91  explicit vector3d(const real init) {x=y=z=init;}
92  //Simple 1-value constructor
93  explicit vector3d(int init) {x=y=z=init;}
94  //3-value constructor
95  vector3d(const real Nx,const real Ny,const real Nz) {x=Nx;y=Ny;z=Nz;}
96  //Real array constructor
97  vector3d(const real *arr) {x=arr[0];y=arr[1];z=arr[2];}
98  //Copy constructor
99  vector3d(const vector3d &copy) {x=copy.x;y=copy.y;z=copy.z;}
100  //Polar coordinate constructor
101  vector3d(const polar3d &p);
102  vector3d &operator=(const vector3d &b) {x=b.x;y=b.y;z=b.z;return *this;}
103 
104 
105  //This lets you typecast a vector to a real array
106  operator real *() {return (real *)&x;}
107  operator const real *() const {return (const real *)&x;}
108 
109 //Basic mathematical operators
110  int operator==(const vector3d &b) const {return (x==b.x)&&(y==b.y)&&(z==b.z);}
111  int operator!=(const vector3d &b) const {return (x!=b.x)||(y!=b.y)||(z!=b.z);}
112  vector3d operator+(const vector3d &b) const {return vector3d(x+b.x,y+b.y,z+b.z);}
113  vector3d operator-(const vector3d &b) const {return vector3d(x-b.x,y-b.y,z-b.z);}
114  vector3d operator*(const real scale) const
115  {return vector3d(x*scale,y*scale,z*scale);}
116  friend vector3d operator*(const real scale,const vector3d &v)
117  {return vector3d(v.x*scale,v.y*scale,v.z*scale);}
118  vector3d operator/(const real &div) const
119  {real scale=1.0/div;return vector3d(x*scale,y*scale,z*scale);}
120  vector3d operator-(void) const {return vector3d(-x,-y,-z);}
121  void operator+=(const vector3d &b) {x+=b.x;y+=b.y;z+=b.z;}
122  void operator-=(const vector3d &b) {x-=b.x;y-=b.y;z-=b.z;}
123  void operator*=(const real scale) {x*=scale;y*=scale;z*=scale;}
124  void operator/=(const real div) {real scale=1.0/div;x*=scale;y*=scale;z*=scale;}
125 
126 //Vector-specific operations
127  //Return the square of the magnitude of this vector
128  real magSqr(void) const {return x*x+y*y+z*z;}
129  //Return the magnitude (length) of this vector
130  real mag(void) const {return sqrt(magSqr());}
131 
132  //Return the square of the distance to the vector b
133  real distSqr(const vector3d &b) const
134  {return (x-b.x)*(x-b.x)+(y-b.y)*(y-b.y)+(z-b.z)*(z-b.z);}
135  //Return the distance to the vector b
136  real dist(const vector3d &b) const {return sqrt(distSqr(b));}
137 
138  //Return the dot product of this vector and b
139  real dot(const vector3d &b) const {return x*b.x+y*b.y+z*b.z;}
140  //Return the cosine of the angle between this vector and b
141  real cosAng(const vector3d &b) const {return dot(b)/(mag()*b.mag());}
142 
143  //Return the "direction" (unit vector) of this vector
144  vector3d dir(void) const {return (*this)/mag();}
145  //Return the right-handed cross product of this vector and b
146  vector3d cross(const vector3d &b) const {
147  return vector3d(y*b.z-z*b.y,z*b.x-x*b.z,x*b.y-y*b.x);
148  }
149 
150  //Return the largest coordinate in this vector
151  real max(void) {
152  real big=x;
153  if (big<y) big=y;
154  if (big<z) big=z;
155  return big;
156  }
157 
158  //Make each of this vector's coordinates at least as big
159  // as the given vector's coordinates.
160  void enlarge(const vector3d &by);
161 };
162 
163 //An axis-aligned 3D bounding box
164 class bbox3d {
165 public:
167  bbox3d() {empty();}
168  void empty(void) {min.x=min.y=min.z=1e30;max.x=max.y=max.z=-1e30;}
169  bool isEmpty(void) const {return min.x>max.x;}
170  void add(const vector3d &p) {
171  if (min.x>p.x) min.x=p.x;
172  if (min.y>p.y) min.y=p.y;
173  if (min.z>p.z) min.z=p.z;
174  if (max.x<p.x) max.x=p.x;
175  if (max.y<p.y) max.y=p.y;
176  if (max.z<p.z) max.z=p.z;
177  }
178 };
179 
180 //A halfspace3d is the portion of a 3d plane lying on
181 // one side of the plane (p1,p2,p3).
182 class halfspace3d {
183 public:
184  // n dot p+d==0 on plane point p
185  vector3d n;//Plane normal
187 
188  typedef const vector3d cv;
190  halfspace3d(cv &p1,cv &p2,cv &p3) {init(p1,p2,p3);}
191  halfspace3d(cv &p1,cv &p2,cv &p3,cv &in) {initCheck(p1,p2,p3,in);}
192  //Norm points into the halfspace; p0 is on the line
194 
195  //Set this halfspace to (p1,p2,p3).
196  // inside points are on the right-handed thumb side of p1,p2,p3
197  void init(cv &p1,cv &p2,cv &p3) {
198  n=(p2-p1).cross(p3-p1);
199  d=-n.dot(p1);
200  }
201 
202  //Set this halfspace to (p1,p2,p3) with in inside.
203  void initCheck(cv &p1,cv &p2,cv &p3,cv &in)
204  { init(p1,p2,p3); if (side(in)<0) {n=-n;d=-d;} }
205 
206 
207  //Returns + if inside halfspace, - if outside (and 0 on line).
208  real side(cv &pt) const
209  {return n.dot(pt)+d;}
210 
211  //Return a value t such that pos+t*dir lies on our plane.
212  real intersect(cv &pos,cv &dir) const
213  {return -(d+n.dot(pos))/n.dot(dir);}
214 
215  /*Returns the point that lies on our plane and
216  the line starting at start and going in dir.*/
217  vector3d intersectPt(cv &start,cv &dir) const
218  {
219  return start+dir*intersect(start,dir);
220  }
221 };
222 #endif //__OSL_VECTOR3D_H
223 
224 
225 
*********************************************************************Illinois Open Source License ****University of Illinois NCSA **Open Source License University of Illinois All rights reserved ****Developed by
Definition: roccomf90.h:7
polar3d(real Nt, real Np, real Nr)
Definition: vector3d.h:80
vector3d max
Definition: vector3d.h:166
void operator-=(const vector3d &b)
Definition: vector3d.h:122
vector3d(const real *arr)
Definition: vector3d.h:97
vector3d cross(const vector3d &b) const
Definition: vector3d.h:146
vector3d operator-(void) const
Definition: vector3d.h:120
real dist(const vector3d &b) const
Definition: vector3d.h:136
NVec< 3, T > cross(const NVec< 3, T > &u, const NVec< 3, T > &v)
void initCheck(cv &p1, cv &p2, cv &p3, cv &in)
Definition: vector3d.h:203
halfspace3d(cv &norm, cv &p0)
Definition: vector3d.h:193
void operator*=(const real scale)
Definition: vector3d.h:123
float real
Definition: gridutil.h:70
vector3d operator*(const real scale) const
Definition: vector3d.h:114
int operator==(const vector3d &b) const
Definition: vector3d.h:110
NT p1
real cosAng(const vector3d &b) const
Definition: vector3d.h:141
real x
Definition: vector3d.h:88
vector3d min
Definition: vector3d.h:166
double sqrt(double d)
Definition: double.h:73
real y
Definition: vector3d.h:88
vector3d operator-(const vector3d &b) const
Definition: vector3d.h:113
T norm(const NVec< DIM, T > &v)
halfspace3d(cv &p1, cv &p2, cv &p3)
Definition: vector3d.h:190
real distSqr(const vector3d &b) const
Definition: vector3d.h:133
real intersect(cv &pos, cv &dir) const
Definition: vector3d.h:212
vector3d n
Definition: vector3d.h:185
friend vector3d operator*(const real scale, const vector3d &v)
Definition: vector3d.h:116
*********************************************************************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
vector3d(const real Nx, const real Ny, const real Nz)
Definition: vector3d.h:95
NT p0
*********************************************************************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 ** copy
Definition: roccomf90.h:20
void init(cv &p1, cv &p2, cv &p3)
Definition: vector3d.h:197
void operator/=(const real div)
Definition: vector3d.h:124
halfspace3d(cv &p1, cv &p2, cv &p3, cv &in)
Definition: vector3d.h:191
void empty(void)
Definition: vector3d.h:168
vector3d operator/(const real &div) const
Definition: vector3d.h:118
void enlarge(const vector3d &by)
vector3d intersectPt(cv &start, cv &dir) const
Definition: vector3d.h:217
vector3d(const vector3d &copy)
Definition: vector3d.h:99
real magSqr(void) const
Definition: vector3d.h:128
real mag(void) const
Definition: vector3d.h:130
void operator+=(const vector3d &b)
Definition: vector3d.h:121
real side(cv &pt) const
Definition: vector3d.h:208
bbox3d()
Definition: vector3d.h:167
int operator!=(const vector3d &b) const
Definition: vector3d.h:111
real phi
Definition: vector3d.h:76
real max(void)
Definition: vector3d.h:151
vector3d(const real init)
Definition: vector3d.h:91
polar3d()
Definition: vector3d.h:79
vector3d dir(void) const
Definition: vector3d.h:144
real r
Definition: vector3d.h:77
vector3d(void)
Definition: vector3d.h:89
vector3d & operator=(const vector3d &b)
Definition: vector3d.h:102
real dot(const vector3d &b) const
Definition: vector3d.h:139
bool isEmpty(void) const
Definition: vector3d.h:169
void add(const vector3d &p)
Definition: vector3d.h:170
const vector3d cv
Definition: vector3d.h:188
real z
Definition: vector3d.h:88
vector3d(int init)
Definition: vector3d.h:93
vector3d operator+(const vector3d &b) const
Definition: vector3d.h:112
real theta
Definition: vector3d.h:76