Cleaver Tetrahedral Meshing  2.2.1
Cleaving algorithm for high quality tetrahedral meshing
All Classes Pages
vec3.cpp
1 //-------------------------------------------------------------------
2 //-------------------------------------------------------------------
3 //
4 // Cleaver - A MultiMaterial Conforming Tetrahedral Meshing Library
5 //
6 // -- vector library
7 //
8 // Author: Jonathan Bronson (bronson@sci.utah.edu)
9 //
10 //-------------------------------------------------------------------
11 //-------------------------------------------------------------------
12 //
13 // Copyright (C) 2011, 2012, Jonathan Bronson
14 // Scientific Computing & Imaging Institute
15 // University of Utah
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 in 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 // The above copyright notice and this permission notice shall
27 // be included in all copies or substantial portions of the
28 // Software.
29 //
30 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
31 // KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
32 // WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
33 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
34 // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
35 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
36 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
37 // USE OR OTHER DEALINGS IN THE SOFTWARE.
38 //-------------------------------------------------------------------
39 //-------------------------------------------------------------------
40 
41 
42 #include "vec3.h"
43 #include <cmath>
44 #include <iostream>
45 #include <iomanip>
46 #include <sstream>
47 
48 using namespace std;
49 namespace cleaver
50 {
51 
52 // static variables
53 vec3 vec3::zero(0,0,0);
54 vec3 vec3::unitX(1,0,0);
55 vec3 vec3::unitY(0,1,0);
56 vec3 vec3::unitZ(0,0,1);
57 
58 vec3::vec3() : x(0), y(0), z(0)
59 {
60 }
61 
62 // default constructor
63 vec3::vec3(double x, double y, double z) : x(x), y(y), z(z)
64 {
65 }
66 
67 // copy constructor
68 vec3::vec3(const vec3 &x) : x(x.x), y(x.y), z(x.z)
69 {
70 }
71 
72 bool vec3::operator!=(const vec3 &a) const
73 {
74  if(this->x != a.x ||
75  this->y != a.y ||
76  this->z != a.z)
77  return true;
78  else
79  return false;
80 }
81 
82 bool vec3::operator==(const vec3 &a) const
83 {
84  return(this->x == a.x &&
85  this->y == a.y &&
86  this->z == a.z);
87 }
88 
89 bool vec3::operator<=(const vec3 &a) const
90 {
91  return ((this->x <= a.x) &&
92  (this->y <= a.y) &&
93  (this->z <= a.z));
94 }
95 
96 bool vec3::operator>=(const vec3 &a) const
97 {
98  return ((this->x >= a.x) &&
99  (this->y >= a.y) &&
100  (this->z >= a.z));
101 }
102 
103 bool vec3::operator<(const vec3 &a) const
104 {
105  return ((this->x < a.x) &&
106  (this->y < a.y) &&
107  (this->z < a.z));
108 }
109 
110 bool vec3::operator>(const vec3 &a) const
111 {
112  return ((this->x > a.x) &&
113  (this->y > a.y) &&
114  (this->z > a.z));
115 }
116 
117 
118 vec3& vec3::operator=(const vec3 &a)
119 {
120  this->x = a.x;
121  this->y = a.y;
122  this->z = a.z;
123 
124  return *this;
125 }
126 
127 vec3& vec3::operator+=(const vec3 &a)
128 {
129  this->x += a.x;
130  this->y += a.y;
131  this->z += a.z;
132 
133  return *this;
134 }
135 
136 vec3& vec3::operator*=(double c)
137 {
138  this->x *= c;
139  this->y *= c;
140  this->z *= c;
141 
142  return *this;
143 }
144 
145 vec3& vec3::operator/=(double c)
146 {
147  this->x /= c;
148  this->y /= c;
149  this->z /= c;
150  return *this;
151 }
152 
153 double& vec3::operator[](const size_t idx)
154 {
155  switch(idx)
156  {
157  case 0: return this->x;
158  case 1: return this->y;
159  case 2: return this->z;
160  default: throw -1; // bad index
161  }
162 }
163 
164 // TODO: Restructure this class so this lookup is FASTER
165 double vec3::operator[](const size_t idx) const
166 {
167  switch(idx)
168  {
169  case 0: return this->x;
170  case 1: return this->y;
171  case 2: return this->z;
172  default: throw -1; // bad index
173  }
174 }
175 
176 double vec3::dot(const vec3 &b) const
177 {
178  return this->x*b.x + this->y*b.y + this->z*b.z;
179 }
180 
181 vec3 vec3::cross(const vec3 &b)
182 {
183  return vec3(this->y*b.z - this->z*b.y, this->z*b.x - this->x*b.z, this->x*b.y - this->y*b.x);
184 }
185 
186 vec3 cross(const vec3 &a, const vec3 &b)
187 {
188  return vec3(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);
189 }
190 
191 double dot(const vec3 &a, const vec3 &b)
192 {
193  return a.x*b.x + a.y*b.y + a.z*b.z;
194 }
195 
196 double L1(const vec3 &a)
197 {
198  return a.x + a.y + a.z;
199 }
200 
201 double L2(const vec3 &a)
202 {
203  return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
204 }
205 
206 double length(const vec3 &a)
207 {
208  return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
209 }
210 
211 
212 
213 vec3 normalize(const vec3 &v1)
214 {
215  return v1 / length(v1);
216 }
217 
218 double vec2polar(const vec3 &a)
219 {
220  if(a.x > 0){
221  if(a.y >= 0)
222  return atan(a.y / a.x);
223  else
224  return atan(a.y / a.x) + 2*PI;
225  }
226  else if(a.x < 0){
227  return atan(a.y/a.x) + PI;
228  }
229  else{
230  if(a.y > 0)
231  return PI/2;
232  else if (a.y < 0)
233  return 3*PI/2;
234  else
235  return 0;
236  }
237 }
238 
239 vec3 operator+(const vec3 &a, const vec3 &b)
240 {
241  return vec3(a.x+b.x, a.y+b.y, a.z+b.z);
242 }
243 
244 vec3 operator-(const vec3 &a, const vec3 &b)
245 {
246  return vec3(a.x-b.x, a.y-b.y, a.z-b.z);
247 }
248 
249 vec3 operator*(const vec3 &a, double s)
250 {
251  return vec3(s*a.x, s*a.y, s*a.z);
252 }
253 
254 vec3 operator*(double s, const vec3 &a)
255 {
256  return vec3(s*a.x, s*a.y, s*a.z);
257 }
258 
259 vec3 operator/(const vec3 &a, double s)
260 {
261  return vec3(a.x/s, a.y/s, a.z/s);
262 }
263 
264 std::ostream &operator<<(std::ostream &stream, const vec3 &v)
265 {
266  stream << std::fixed;
267  //return stream << "[" << std::setprecision(3) << v.x << ", " << v.y << ", " << v.z << "]";
268  return stream << std::setprecision(3) << v.x << " " << v.y << " " << v.z;
269 }
270 
271 double clamp(double value, double min, double max)
272 {
273  if(value < min)
274  return min;
275  else if(value > max)
276  return max;
277  else
278  return value;
279 }
280 
281 int clamp(int value, int min, int max)
282 {
283  if(value < min)
284  return min;
285  else if(value > max)
286  return max;
287  else
288  return value;
289 }
290 
291 std::string vec3::toString() const
292 {
293  std::stringstream ss;
294  ss << "[" << std::setprecision(5) << this->x << ", " << this->y << ", " << this->z << "]";
295  return ss.str();
296 }
297 
298 
299 vec3 vec3::min(const vec3 &a, const vec3 &b)
300 {
301  return vec3((a.x < b.x) ? a.x : b.x,
302  (a.y < b.y) ? a.y : b.y,
303  (a.z < b.z) ? a.z : b.z);
304 }
305 
306 vec3 vec3::max(const vec3 &a, const vec3 &b)
307 {
308  return vec3((a.x > b.x) ? a.x : b.x,
309  (a.y > b.y) ? a.y : b.y,
310  (a.z > b.z) ? a.z : b.z);
311 }
312 
313 double angleBetween(const vec3 &a, const vec3 &b)
314 {
315  return acos( dot(a,b) / (L2(a)*L2(b)) );
316 }
317 
318 }