-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinalg3d.h
276 lines (229 loc) · 8.61 KB
/
linalg3d.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
#ifndef __GEOM_H
#define __GEOM_H
/*
* One file long C++ library of linear algebra primitives for
* simple 3D programs
*
* Copyright (C) 2001-2003 by Jarno Elonen
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. The authors make no representations
* about the suitability of this software for any purpose.
* It is provided "as is" without express or implied warranty.
*/
#include <cmath>
#define EPSILON 0.00001f
#define PI 3.1415926
#define Deg2Rad(Ang) ((float)( Ang * PI / 180.0 ))
#define Rad2Deg(Ang) ((float)( Ang * 180.0 / PI ))
// =========================================
// 3-vector
// =========================================
namespace TPS{
class Vec
{
public:
// Position
float x, y, z;
// Default constructor
Vec()
: x( 0 ), y( 0 ), z( 0 ) {}
// Element constructor
Vec( float x, float y, float z )
: x( x ), y( y ), z( z ) {}
// Copy constructor
Vec( const Vec& a )
: x( a.x ), y( a.y ), z( a.z ) {}
// Norm (len^2)
inline float norm() const { return x*x + y*y + z*z; }
// Length of the vector
inline float len() const { return (float)sqrt(norm()); }
Vec &operator += ( const Vec &src ) { x += src.x; y += src.y; z += src.z; return *this; }
Vec operator + ( const Vec &src ) const { Vec tmp( *this ); return ( tmp += src ); }
Vec &operator -= ( const Vec &src ) { x -= src.x; y -= src.y; z -= src.z; return *this; }
Vec operator - ( const Vec &src ) const { Vec tmp( *this ); return ( tmp -= src ); }
Vec operator - () const { return Vec(-x,-y,-z); }
Vec &operator *= ( const float src ) { x *= src; y *= src; z *= src; return *this; }
Vec operator * ( const float src ) const { Vec tmp( *this ); return ( tmp *= src ); }
Vec &operator /= ( const float src ) { x /= src; y /= src; z /= src; return *this; }
Vec operator / ( const float src ) const { Vec tmp( *this ); return ( tmp /= src ); }
bool operator == ( const Vec& b) const { return ((*this)-b).norm() < EPSILON; }
//bool operator == ( const Vec& b) const { return x==b.x && y==b.y && z==b.z; }
};
// Left hand float multplication
inline Vec operator * ( const float src, const Vec& v ) { Vec tmp( v ); return ( tmp *= src ); }
// Dot product
inline float dot( const Vec& a, const Vec& b )
{ return a.x*b.x + a.y*b.y + a.z*b.z; }
// Cross product
inline Vec cross( const Vec &a, const Vec &b )
{ return Vec( a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x ); }
// =========================================
// 4 x 4 matrix
// =========================================
class Mtx
{
public:
// 4x4, [[0 1 2 3] [4 5 6 7] [8 9 10 11] [12 13 14 15]]
float data[ 16 ];
// Creates an identity matrix
Mtx()
{
for ( int i = 0; i < 16; ++i )
data[ i ] = 0;
data[ 0 + 0 ] = data[ 4 + 1 ] = data[ 8 + 2 ] = data[ 12 + 3 ] = 1;
}
// Returns the transpose of this matrix
Mtx transpose() const
{
Mtx m;
int idx = 0;
for ( int row = 0; row < 4; ++row )
for ( int col = 0; col < 4; ++col, ++idx )
m.data[ idx ] = data[ row + ( col * 4 ) ];
return m;
}
// Operators
float operator () ( unsigned column, unsigned row )
{ return data[ column + ( row * 4 ) ]; }
};
// Creates a scale matrix
Mtx scale( const Vec &scale )
{
Mtx m;
m.data[ 0 + 0 ] = scale.x;
m.data[ 4 + 1 ] = scale.y;
m.data[ 8 + 2 ] = scale.z;
return m;
}
// Creates a translation matrix
Mtx translate( const Vec &moveAmt )
{
Mtx m;
m.data[ 0 + 3 ] = moveAmt.x;
m.data[ 4 + 3 ] = moveAmt.y;
m.data[ 8 + 3 ] = moveAmt.z;
return m;
}
// Creates an euler rotation matrix (by X-axis)
Mtx rotateX( float ang )
{
float s = ( float ) sin( Deg2Rad( ang ) );
float c = ( float ) cos( Deg2Rad( ang ) );
Mtx m;
m.data[ 4 + 1 ] = c; m.data[ 4 + 2 ] = -s;
m.data[ 8 + 1 ] = s; m.data[ 8 + 2 ] = c;
return m;
}
// Creates an euler rotation matrix (by Y-axis)
Mtx rotateY( float ang )
{
float s = ( float ) sin( Deg2Rad( ang ) );
float c = ( float ) cos( Deg2Rad( ang ) );
Mtx m;
m.data[ 0 + 0 ] = c; m.data[ 0 + 2 ] = s;
m.data[ 8 + 0 ] = -s; m.data[ 8 + 2 ] = c;
return m;
}
// Creates an euler rotation matrix (by Z-axis)
Mtx rotateZ( float ang )
{
float s = ( float ) sin( Deg2Rad( ang ) );
float c = ( float ) cos( Deg2Rad( ang ) );
Mtx m;
m.data[ 0 + 0 ] = c; m.data[ 0 + 1 ] = -s;
m.data[ 4 + 0 ] = s; m.data[ 4 + 1 ] = c;
return m;
}
// Creates an euler rotation matrix (pitch/head/roll (x/y/z))
Mtx rotate( float pitch, float head, float roll )
{
float sp = ( float ) sin( Deg2Rad( pitch ) );
float cp = ( float ) cos( Deg2Rad( pitch ) );
float sh = ( float ) sin( Deg2Rad( head ) );
float ch = ( float ) cos( Deg2Rad( head ) );
float sr = ( float ) sin( Deg2Rad( roll ) );
float cr = ( float ) cos( Deg2Rad( roll ) );
Mtx m;
m.data[ 0 + 0 ] = cr * ch - sr * sp * sh;
m.data[ 0 + 1 ] = -sr * cp;
m.data[ 0 + 2 ] = cr * sh + sr * sp * ch;
m.data[ 4 + 0 ] = sr * ch + cr * sp * sh;
m.data[ 4 + 1 ] = cr * cp;
m.data[ 4 + 2 ] = sr * sh - cr * sp * ch;
m.data[ 8 + 0 ] = -cp * sh;
m.data[ 8 + 1 ] = sp;
m.data[ 8 + 2 ] = cp * ch;
return m;
}
// Creates an arbitraty rotation matrix
Mtx makeRotationMatrix( const Vec &dir, const Vec &up )
{
Vec x = cross( up, dir ), y = cross( dir, x ), z = dir;
Mtx m;
m.data[ 0 ] = x.x; m.data[ 1 ] = x.y; m.data[ 2 ] = x.z;
m.data[ 4 ] = y.x; m.data[ 5 ] = y.y; m.data[ 6 ] = y.z;
m.data[ 8 ] = z.x; m.data[ 9 ] = z.y; m.data[ 10 ] = z.z;
return m;
}
// Transforms a vector by a matrix
inline Vec operator * ( const Vec& v, const Mtx& m )
{
return Vec(
m.data[ 0 ] * v.x + m.data[ 1 ] * v.y + m.data[ 2 ] * v.z + m.data[ 3 ],
m.data[ 4 ] * v.x + m.data[ 5 ] * v.y + m.data[ 6 ] * v.z + m.data[ 7 ],
m.data[ 8 ] * v.x + m.data[ 9 ] * v.y + m.data[ 10 ] * v.z + m.data[ 11 ] );
}
// Multiplies a matrix by another matrix
Mtx operator * ( const Mtx& a, const Mtx& b )
{
Mtx ans;
for ( int aRow = 0; aRow < 4; ++aRow )
for ( int bCol = 0; bCol < 4; ++bCol )
{
int aIdx = aRow * 4;
int bIdx = bCol;
float val = 0;
for ( int idx = 0; idx < 4; ++idx, ++aIdx, bIdx += 4 )
val += a.data[ aIdx ] * b.data[ bIdx ];
ans.data[ bCol + aRow * 4 ] = val;
}
return ans;
}
// =========================================
// Plane
// =========================================
class Plane
{
public:
enum PLANE_EVAL
{
EVAL_COINCIDENT,
EVAL_IN_BACK_OF,
EVAL_IN_FRONT_OF,
EVAL_SPANNING
};
Vec normal;
float d;
// Default constructor
Plane(): normal( 0,1,0 ), d( 0 ) {}
// Vector form constructor
// normal = normalized normal of the plane
// pt = any point on the plane
Plane( const Vec& normal, const Vec& pt )
: normal( normal ), d( dot( -normal, pt )) {}
// Copy constructor
Plane( const Plane& a )
: normal( a.normal ), d( a.d ) {}
// Classifies a point (<0 == back, 0 == on plane, >0 == front)
float classify( const Vec& pt ) const
{
float f = dot( normal, pt ) + d;
return ( f > -EPSILON && f < EPSILON ) ? 0 : f;
}
};
}
#endif