forked from SCIInstitute/ImplicitFunction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvec3.cpp
305 lines (256 loc) · 6.34 KB
/
vec3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files ( the "Software" ), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall
// be included in all copies or substantial portions of the
// Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
// KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
//-------------------------------------------------------------------
//-------------------------------------------------------------------
#include "vec3.h"
#include <cmath>
#include <iostream>
#include <iomanip>
#include <sstream>
using namespace std;
// static variables
vec3 vec3::zero(0,0,0);
vec3 vec3::unitX(1,0,0);
vec3 vec3::unitY(0,1,0);
vec3 vec3::unitZ(0,0,1);
const double vec3::PI = 3.14159265;
vec3::vec3() : data_{0,0,0}
{
}
vec3::vec3(double x, double y, double z) : data_{x,y,z}
{
}
bool vec3::operator!=(const vec3 &a) const
{
if(this->x() != a.x() ||
this->y() != a.y() ||
this->z() != a.z())
return true;
else
return false;
}
bool vec3::operator==(const vec3 &a) const
{
return(this->x() == a.x() &&
this->y() == a.y() &&
this->z() == a.z());
}
bool vec3::operator<=(const vec3 &a) const
{
return *this < a || *this == a;
}
bool vec3::operator>=(const vec3 &a) const
{
return *this > a || *this == a;
}
bool vec3::operator<(const vec3 &a) const
{
if (x() < a.x())
return true;
if (x() > a.x())
return false;
// equal x
if (y() < a.y())
return true;
if (y() > a.y())
return false;
// equal x and y
if (z() < a.z())
return true;
if (z() > a.z())
return false;
return false;
}
bool vec3::operator>(const vec3 &a) const
{
return a < *this;
}
vec3& vec3::operator=(const vec3 &a)
{
this->x() = a.x();
this->y() = a.y();
this->z() = a.z();
return *this;
}
vec3& vec3::operator+=(const vec3 &a)
{
this->x() += a.x();
this->y() += a.y();
this->z() += a.z();
return *this;
}
vec3& vec3::operator*=(double c)
{
this->x() *= c;
this->y() *= c;
this->z() *= c;
return *this;
}
vec3& vec3::operator/=(double c)
{
// TODO: throw exception if c == 0
this->x() /= c;
this->y() /= c;
this->z() /= c;
return *this;
}
double& vec3::operator[](const size_t idx)
{
return data_[idx];
}
// TODO: Restructure this class so this lookup is FASTER
double vec3::operator[](const size_t idx) const
{
return data_[idx];
}
double vec3::dot(const vec3 &b) const
{
return this->x()*b.x() + this->y()*b.y() + this->z()*b.z();
}
vec3 vec3::cross(const vec3 &b)
{
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());
}
vec3 cross(const vec3 &a, const vec3 &b)
{
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());
}
double dot(const vec3 &a, const vec3 &b)
{
return a.x()*b.x() + a.y()*b.y() + a.z()*b.z();
}
double length(const vec3 &a)
{
return sqrt(a.x()*a.x() + a.y()*a.y() + a.z()*a.z());
}
double lengthSquared(const vec3 &a)
{
return a.x()*a.x() + a.y()*a.y() + a.z()*a.z();
}
double distance(const vec3 &a, const vec3 &b)
{
double xdiff = a.x()-b.x(), ydiff = a.y()-b.y(), zdiff = a.z()-b.z();
return sqrt(xdiff*xdiff + ydiff*ydiff + zdiff*zdiff);
}
double L1(const vec3 &a)
{
return a.x() + a.y() + a.z();
}
double L2(const vec3 &a)
{
return length(a);
}
vec3 normalize(const vec3 &v1)
{
return v1 / length(v1);
}
vec3 normalize(const vec3 &v1, float epsilon)
{
return v1 / (length(v1) + epsilon);
}
double vec2polar(const vec3 &a)
{
if (a.x() > 0)
{
if(a.y() >= 0)
return atan(a.y() / a.x());
else
return atan(a.y() / a.x()) + 2*vec3::PI;
}
else if (a.x() < 0)
{
return atan(a.y()/a.x()) + vec3::PI;
}
else
{
if(a.y() > 0)
return vec3::PI/2;
else if (a.y() < 0)
return 3*vec3::PI/2;
else
return 0;
}
}
vec3 operator+(const vec3 &a, const vec3 &b)
{
return vec3(a.x()+b.x(), a.y()+b.y(), a.z()+b.z());
}
vec3 operator-(const vec3 &a, const vec3 &b)
{
return vec3(a.x()-b.x(), a.y()-b.y(), a.z()-b.z());
}
vec3 operator*(const vec3 &a, double s)
{
return vec3(s*a.x(), s*a.y(), s*a.z());
}
vec3 operator*(double s, const vec3 &a)
{
return vec3(s*a.x(), s*a.y(), s*a.z());
}
vec3 operator/(const vec3 &a, double s)
{
// TODO: throw exception if s == 0
return vec3(a.x()/s, a.y()/s, a.z()/s);
}
std::ostream &operator<<(std::ostream &stream, const vec3 &v)
{
stream << std::fixed;
return stream << std::setprecision(3) << v.x() << " " << v.y() << " " << v.z();
}
double clamp(double value, double min, double max)
{
if (value < min)
return min;
else if (value > max)
return max;
else
return value;
}
std::string vec3::toString() const
{
std::stringstream ss;
ss << "[" << std::setprecision(5) << this->x() << ", " << this->y() << ", " << this->z() << "]";
return ss.str();
}
vec3 vec3::min(const vec3 &a, const vec3 &b)
{
return vec3((a.x() < b.x()) ? a.x() : b.x(),
(a.y() < b.y()) ? a.y() : b.y(),
(a.z() < b.z()) ? a.z() : b.z());
}
vec3 vec3::max(const vec3 &a, const vec3 &b)
{
return vec3((a.x() > b.x()) ? a.x() : b.x(),
(a.y() > b.y()) ? a.y() : b.y(),
(a.z() > b.z()) ? a.z() : b.z());
}
double angleBetween(const vec3 &a, const vec3 &b)
{
return acos( dot(a,b) / ( L2(a)*L2(b) ) );
}
double angleBetween(const vec3 &a, const vec3 &b, float epsilon)
{
return acos( dot(a,b) / ( ( L2(a)*L2(b) ) + epsilon ) );
}