-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxvec.h
executable file
·369 lines (309 loc) · 8.19 KB
/
xvec.h
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* Copyright (c) 2007 University of Michigan, Ann Arbor.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that the above copyright notice and this paragraph are
* duplicated in all such forms and that any documentation,
* advertising materials, and other materials related to such
* distribution and use acknowledge that the software was developed
* by the University of Michigan, Ann Arbor. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
*
* Authors: Igor Guskov, Ari Grant, Sugih Jamin
*
*/
#ifndef __XVEC_H__
#define __XVEC_H__
#ifndef _NO_IOSTREAMS
#include <iostream>
#endif
#include <assert.h>
#include <math.h>
// Column type vector class.
template<int dim, class real_type>
class XVec {
public:
// Constructors.
XVec() {
}
explicit XVec(real_type f) {
for(int i=0; i<dim; ++i)
m_v[i] = f;
}
XVec(real_type f0, real_type f1) {
assert(dim>1);
m_v[0] = f0;
m_v[1] = f1;
}
XVec(real_type f0, real_type f1, real_type f2) {
assert(dim>2);
m_v[0] = f0;
m_v[1] = f1;
m_v[2] = f2;
}
XVec(real_type f0, real_type f1, real_type f2, real_type f3) {
assert(dim>3);
m_v[0] = f0;
m_v[1] = f1;
m_v[2] = f2;
m_v[3] = f3;
}
XVec(const XVec& c) {
for(int i=0; i<dim; ++i)
m_v[i] = c.m_v[i];
}
explicit XVec(const real_type* a) {
for(int i=0; i<dim; ++i)
m_v[i] = a[i];
}
template<class oreal_type>
explicit XVec(const XVec<dim, oreal_type>& c) {
for(int i=0; i<dim; ++i)
m_v[i] = static_cast<oreal_type>(c(i));
}
// Useful for OpenGL calls.
operator real_type*() {
return &m_v[0];
}
operator const real_type*() const {
return &m_v[0];
}
// Component-wise comparison.
bool operator==(const XVec& c) const {
for(int i=0; i<dim; ++i)
if(m_v[i]!=c.m_v[i])
return false;
return true;
}
bool operator!=(const XVec& c) const {
return !((*this)==c);
}
XVec& operator=(const XVec& c) {
for(int i=0; i<dim; ++i)
m_v[i] = c.m_v[i];
return *this;
}
// Algebraic operations.
XVec operator+(const XVec& c) const {
XVec pt(*this);
for(int i=0; i<dim; ++i)
pt[i] += c.m_v[i];
return pt;
}
XVec operator-(const XVec& c) const {
XVec pt(*this);
for(int i=0; i<dim; ++i)
pt[i] -= c.m_v[i];
return pt;
}
XVec operator*(real_type s) const {
XVec pt(*this);
for(int i=0; i<dim; ++i)
pt[i] *= s;
return pt;
}
friend XVec operator*(real_type s, const XVec& c) {
XVec pt(c);
for(int i=0; i<dim; ++i)
pt[i] *= s;
return pt;
}
XVec operator/(real_type s) const {
XVec pt(*this);
for(int i=0; i<dim; ++i)
pt[i] /= s;
return pt;
}
XVec& operator+=(const XVec& c) {
for(int i=0; i<dim; ++i)
m_v[i] += c.m_v[i];
return *this;
}
XVec& operator-=(const XVec& c) {
for(int i=0; i<dim; ++i)
m_v[i] -= c.m_v[i];
return *this;
}
XVec& operator*=(real_type s) {
for(int i=0; i<dim; ++i)
m_v[i] *= s;
return *this;
}
XVec& operator/=(real_type s) {
for(int i=0; i<dim; ++i)
m_v[i] /= s;
return *this;
}
XVec operator-() const {
XVec pt(*this);
for(int i=0; i<dim; ++i)
pt[i] = -pt[i];
return pt;
}
// Element-wise multiplication.
XVec operator*(const XVec& c) const {
XVec pt(*this);
for(int i=0; i<dim; ++i)
pt[i] *= c.m_v[i];
return pt;
}
// Element-wise division.
XVec operator/(const XVec& c) const {
XVec pt(*this);
for(int i=0; i<dim; ++i)
pt[i] /= c.m_v[i];
return pt;
}
// Access the components.
real_type& operator() (const int i) {
return m_v[i];
}
real_type operator() (const int i) const {
return m_v[i];
}
real_type& operator[] (const int i) {
return m_v[i];
}
real_type operator[] (const int i) const {
return m_v[i];
}
const real_type& ref() const {
return m_v[0];
}
real_type& x() {
return m_v[0];
}
real_type& y() {
assert(dim>1);
return m_v[1];
}
real_type& z() {
assert(dim>2);
return m_v[2];
}
real_type& w() {
assert(dim>3);
return m_v[3];
}
real_type& red() {
return x();
}
real_type& green() {
return y();
}
real_type& blue() {
return z();
}
real_type& alpha() {
return w();
}
// Updates bounding box corners to include itself.
void bbox(XVec& cmin, XVec& cmax) const {
for(int i=0; i<dim; ++i) {
if(m_v[i] < cmin.m_v[i])
cmin.m_v[i] = m_v[i];
if(m_v[i] > cmax.m_v[i])
cmax.m_v[i] = m_v[i];
}
}
// Dot product.
real_type dot(const XVec& c) const {
real_type d = 0;
for(int i=0; i<dim; ++i)
d += m_v[i] * c.m_v[i];
return d;
}
// Dot product with itself -- vector norm squared.
real_type dot() const {
real_type d = 0;
for(int i=0; i<dim; ++i)
d += m_v[i] * m_v[i];
return d;
}
XVec cross(const XVec& c) const {
// Cross-product is only defined for 3D vectors
// and it is specialized below.
assert(false);
}
// norm of a vector.
real_type norm(void) const {
return sqrtf(dot());
}
// Euclidean distance between two points.
real_type dist(const XVec& c) const {
return (*this - c).norm();
}
void normalize(void) {
real_type mag = norm();
if(fabs(mag) >= 1e-25)
*this *= 1 / mag;
}
XVec dehomogenize() {
assert(dim == 4);
if ((*this).w() == 0.0 || (*this).w() == 1.0) {
return(XVec3f((*this).x(), (*this).y(), (*this).z()));
} else {
return(XVec3f((*this).x()/(*this).w(), (*this).y()/(*this).w(), (*this).z()/(*this).w()));
}
}
// Projection of this onto u
XVec project(const XVec& c) {
return c*dot(c)/c.dot();
}
// LAB3
void rotate(float theta, XVec& axis)
{
assert(dim == 3 || dim == 4);
if (dim == 4) assert (!(axis.m_v[dim-1] || (*this).m_v[dim-1]) || ((*this).m_v[dim-1] == 1.0));
(*this) = (1-cosf(theta))*axis.dot((*this))*axis + (*this)*cosf(theta)+axis.cross((*this))*sinf(theta);
}
protected:
real_type m_v[dim];
};
// This can be done shorter with partial template specialization but some
// compilers do not support it as of now.
template<>
inline XVec<3, float> XVec<3, float>::cross(const XVec<3, float>& c) const {
return XVec<3, float>(m_v[1] * c.m_v[2] - m_v[2] * c.m_v[1],
m_v[2] * c.m_v[0] - m_v[0] * c.m_v[2],
m_v[0] * c.m_v[1] - m_v[1] * c.m_v[0]);
}
template<>
inline XVec<4, float> XVec<4, float>::cross(const XVec<4, float>& c) const {
return XVec<4, float>(m_v[1] * c.m_v[2] - m_v[2] * c.m_v[1],
m_v[2] * c.m_v[0] - m_v[0] * c.m_v[2],
m_v[0] * c.m_v[1] - m_v[1] * c.m_v[0], 0.0);
}
#ifndef _NO_IOSTREAMS
template<int dim, class real_type>
std::ostream& operator<<(std::ostream& os, const XVec<dim, real_type>& c)
{
for(int i=0; i<dim; ++i)
os << c(i) << " ";
return os;
}
template<int dim, class real_type>
std::istream&
operator>>(std::istream& is, XVec<dim, real_type>& f)
{
return is >> f.x() >> f.y() >> f.z();
}
#endif
typedef XVec<2, unsigned char> XVec2b;
typedef XVec<2, double> XVec2d;
typedef XVec<2, float> XVec2f;
typedef XVec<2, int> XVec2i;
typedef XVec<3, unsigned char> XVec3b;
typedef XVec<3, double> XVec3d;
typedef XVec<3, float> XVec3f;
typedef XVec<3, int> XVec3i;
typedef XVec<4, unsigned char> XVec4b;
typedef XVec<4, double> XVec4d;
typedef XVec<4, float> XVec4f;
typedef XVec<4, int> XVec4i;
#endif // __XVEC_H__