-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathvector.cpp
More file actions
195 lines (173 loc) · 3.75 KB
/
Copy pathvector.cpp
File metadata and controls
195 lines (173 loc) · 3.75 KB
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
// Copyright (c) 2000, 2001, 2002, 2003 by David Scherer and others.
// Copyright (c) 2003, 2004 by Jonathan Brandmeyer and others.
// See the file license.txt for complete license terms.
// See the file authors.txt for a complete list of contributors.
// Most functions are inlined in the parent header.
#include "util/vector.hpp"
#include "util/tmatrix.hpp"
#include <istream>
#include <ostream>
#include <sstream>
namespace cvisual {
vector
vector::cross( const vector& v) const throw()
{
vector ret( this->y*v.z - this->z*v.y
, this->z*v.x - this->x*v.z
, this->x*v.y - this->y*v.x);
return ret;
}
vector
vector::norm( void) const throw()
{
double magnitude = mag();
if (magnitude)
// This step ensures that vector(0,0,0).norm() returns vector(0,0,0)
// instead of NaN
magnitude = 1.0 / magnitude;
return vector( x*magnitude, y*magnitude, z*magnitude);
}
// TODO: Figure out why this doesn't quite work...
double
vector::stable_mag(void) const
{
double ret = 0.0;
double x1 = std::fabs(x);
double x2 = std::fabs(y);
double x3 = std::fabs(z);
// sort the temporaries into descending order.
if (x1 < x2)
std::swap( x1, x2);
if (x2 < x3) {
std::swap(x2, x3);
if (x1 < x2)
std::swap( x1, x2);
}
if (x1 == 0.0)
return 0.0;
if (x2 == 0.0)
return x1;
// at this point, ret is equal to the length of an R2 vector.
ret = x1 / std::cos( std::atan( x1/ x2));
if (x3 == 0.0)
return ret;
ret = ret / std::cos( std::atan( ret/x3));
return ret;
}
// Evaluate the determinant:
// | x1, y1, z1 |
// | x2, y2, z2 |
// | x3, y3, z3 |
double
vector::dot_b_cross_c( const vector& b, const vector& c) const throw()
{
return ( this->x*(b.y*c.z - b.z*c.y)
- this->y*(b.x*c.z - b.z*c.x)
+ this->z*(b.x*c.y - b.y*c.x)
);
}
/* Vector triple product.
*/
vector
vector::cross_b_cross_c( const vector& b, const vector& c) const throw()
{
return (this->dot( c) * b - this->dot( b) * c);
}
// Vector projections
double
vector::comp( const vector& v) const throw()
{
return (this->dot( v) / v.mag());
}
vector
vector::proj( const vector& v) const throw()
{
return (this->dot( v)/v.mag2() * v);
}
double
vector::diff_angle( const vector& v) const throw()
{
double magfirst = this->mag2();
double magsecond = v.mag2();
if (magfirst == 0.0 || magsecond == 0.0)
return (double) 0.0;
return std::acos( this->dot( v) / std::sqrt(magfirst*magsecond) );
}
std::string
vector::repr( void) const
{
std::stringstream ret;
ret.precision( std::numeric_limits<double>::digits10);
// Since this function is intended to produce Python code that can be used to
// rebuild this object, we use the full precision of the data type here.
ret << "vector(" << x << ", " << y << ", " << z << ")";
return ret.str();
}
vector
vector::rotate( double angle, vector axis) throw()
{
tmatrix R = rotation( angle, axis.norm());
return R * *this;
}
double
vector::py_getitem( int index) const
{
switch (index) {
case -3:
return x;
case -2:
return y;
case -1:
return z;
case 0:
return x;
case 1:
return y;
case 2:
return z;
default:
std::ostringstream s;
s << "vector index out of bounds: " << index;
throw std::out_of_range( s.str() );
}
}
void
vector::py_setitem(int index, double value)
{
switch (index) {
case -3:
x = value;
break;
case -2:
y = value;
break;
case -1:
z = value;
break;
case 0:
x = value;
break;
case 1:
y = value;
break;
case 2:
z = value;
break;
default:
std::ostringstream s;
s << "vector index out of bounds: " << index;
throw std::out_of_range( s.str() );
}
}
bool
vector::stl_cmp( const vector& v) const
{
if (this->x != v.x) {
return this->x < v.x;
}
else if (this->y != v.y) {
return this->y < v.y;
}
else return this->z < v.z;
}
} // !namespace cvisual