File: MxMath.h

package info (click to toggle)
k3d 0.8.0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 40,692 kB
  • ctags: 39,695
  • sloc: cpp: 171,303; ansic: 24,129; xml: 6,995; python: 5,796; makefile: 726; sh: 22
file content (298 lines) | stat: -rw-r--r-- 6,449 bytes parent folder | download | duplicates (8)
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
#ifndef MXVEC3_INCLUDED
#define MXVEC3_INCLUDED

// MixKit -- Code library for multiresolution surface modeling
// Copyright (c) 1998, Michael Garland
//
// Contact: http://graphics.cs.uiuc.edu/~garland/software/qslim.html
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

/** \file
		\brief Vectors and Matrices
		\author Adapted for K-3D: Romain Behar (romainbehar@yahoo.com)
*/

#include <cmath>
#include <iostream>

#ifndef FEQ_EPS2
#define FEQ_EPS2 1e-12
#endif

enum Axis {X=0, Y=1, Z=2, W=3};


class Vec3 {
private:
	double elt[3];

protected:
	inline void copy(const Vec3& v);

public:
	Vec3(double x=0, double y=0, double z=0) { elt[0]=x; elt[1]=y; elt[2]=z; }
	Vec3(const Vec3& v) { copy(v); }
	Vec3(const double *v) { elt[0]=v[0]; elt[1]=v[1]; elt[2]=v[2]; }

	// Access methods
	operator double*() { return elt; }
	operator const double*() const { return elt; }

	// Comparison operators
	inline bool operator==(const Vec3& v) const;
	inline bool operator!=(const Vec3& v) const;

	// Assignment and in-place arithmetic methods
	inline Vec3& operator=(const Vec3& v);
	inline Vec3& operator+=(const Vec3& v);
	inline Vec3& operator-=(const Vec3& v);
	inline Vec3& operator*=(double s);
	inline Vec3& operator/=(double s);

	// Binary arithmetic methods
	inline Vec3 operator+(const Vec3& v) const;
	inline Vec3 operator-(const Vec3& v) const;
	inline Vec3 operator-() const;

	inline Vec3 operator*(double s) const;
	inline Vec3 operator/(double s) const;
	inline double operator*(const Vec3& v) const;
	inline Vec3 operator^(const Vec3& v) const;

	// Other
	Vec3& Normalize();
	double Length()
	{
		return std::sqrt(elt[0]*elt[0]+elt[1]*elt[1]+elt[2]*elt[2]);
	}
};

class Vec4
{
private:
	double elt[4];

public:
	Vec4(double x=0, double y=0, double z=0, double w=0)
	{
		elt[0]=x; elt[1]=y; elt[2]=z; elt[3]=w;
	}
	Vec4(const Vec3& v,double w)
	{
		elt[0]=v[0];elt[1]=v[1];elt[2]=v[2];elt[3]=w;
	}

	// Access methods
	operator double*() { return elt; }
	operator const double*() const { return elt; }

	// Assignment and in-place arithmetic methods
	inline Vec4& operator=(const Vec4& v);
};

class Mat3
{
private:
	Vec3 row[3];
	inline Vec3 col(int i) const {return Vec3(row[0][i],row[1][i],row[2][i]);}

public:
	Mat3()
	{
		row[0] = Vec3(0, 0, 0);
		row[1] = Vec3(0, 0, 0);
		row[2] = Vec3(0, 0, 0);
	}

	Mat3(const Vec3& r0,const Vec3& r1,const Vec3& r2)
	{
		row[0] = r0;
		row[1] = r1;
		row[2] = r2;
	}

	inline Mat3 operator/(double s) const;
	inline Vec3 operator*(const Vec3& v) const;

	// Matrix operations
	double det();
	Mat3 transpose();
	Mat3 adjoint();
	double invert(Mat3&);
};


////////////////////////////////////////////////////////////////////////
//
// Method definitions
//

inline void Vec3::copy(const Vec3& v)
{
	elt[0]=v.elt[0]; elt[1]=v.elt[1]; elt[2]=v.elt[2];
}

inline bool Vec3::operator==(const Vec3& v) const
{
	double dx=elt[X]-v[X],  dy=elt[Y]-v[Y],  dz=elt[Z]-v[Z];
	return (dx*dx + dy*dy + dz*dz) < FEQ_EPS2;
}

inline bool Vec3::operator!=(const Vec3& v) const
{
	double dx=elt[X]-v[X],  dy=elt[Y]-v[Y],  dz=elt[Z]-v[Z];
	return (dx*dx + dy*dy + dz*dz) > FEQ_EPS2;
}

inline Vec3& Vec3::operator=(const Vec3& v)
{
	copy(v);
	return *this;
}

inline Vec3& Vec3::operator+=(const Vec3& v)
{
	elt[0] += v[0];   elt[1] += v[1];   elt[2] += v[2];
	return *this;
}

inline Vec3& Vec3::operator-=(const Vec3& v)
{
	elt[0] -= v[0];   elt[1] -= v[1];   elt[2] -= v[2];
	return *this;
}

inline Vec3& Vec3::operator*=(double s)
{
	elt[0] *= s;   elt[1] *= s;   elt[2] *= s;
	return *this;
}

inline Vec3& Vec3::operator/=(double s)
{
	elt[0] /= s;   elt[1] /= s;   elt[2] /= s;
	return *this;
}


inline Vec3 Vec3::operator+(const Vec3& v) const
{
	return Vec3(elt[0]+v[0], elt[1]+v[1], elt[2]+v[2]);
}

inline Vec3 Vec3::operator-(const Vec3& v) const
{
	return Vec3(elt[0]-v[0], elt[1]-v[1], elt[2]-v[2]);
}

inline Vec3 Vec3::operator-() const
{
	return Vec3(-elt[0], -elt[1], -elt[2]);
}

inline Vec3 Vec3::operator*(double s) const
{
	return Vec3(elt[0]*s, elt[1]*s, elt[2]*s);
}

inline Vec3 Vec3::operator/(double s) const
{
	return Vec3(elt[0]/s, elt[1]/s, elt[2]/s);
}

inline double Vec3::operator*(const Vec3& v) const
{
	return elt[0]*v[0] + elt[1]*v[1] + elt[2]*v[2];
}

inline Vec3 Vec3::operator^(const Vec3& v) const
{
	Vec3 w( elt[1]*v[2] - v[1]*elt[2],
	   -elt[0]*v[2] + v[0]*elt[2],
	    elt[0]*v[1] - v[0]*elt[1] );
	return w;
}

// Make scalar multiplication commutative
inline Vec3 operator*(double s, const Vec3& v) { return v*s; }


////////////////////////////////////////////////////////////////////////
//
// Primitive function definitions
//

inline double norm(const Vec3& v)
{
	return std::sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
}

inline double norm2(const Vec3& v)
{
	return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
}

inline Vec3& Vec3::Normalize()
{
	if(const double length = norm(*this))
		*this /= length;

	return *this;
}

////////////////////////////////////////////////////////////////////////
//
// Misc. function definitions
//

inline std::ostream& operator<<(std::ostream& out, const Vec3& v)
{
	return out << v[0] << " " << v[1] << " " << v[2];
}

inline std::istream& operator>>(std::istream& in, Vec3& v)
{
	return in >> v[0] >> v[1] >> v[2];
}

////////////////////////////////////////////////////////////////////////
//
// Vec4
//

inline Vec4& Vec4::operator=(const Vec4& v)
{
	elt[0]=v.elt[0]; elt[1]=v.elt[1]; elt[2]=v.elt[2]; elt[3]=v.elt[3];
	return *this;
}

////////////////////////////////////////////////////////////////////////
//
// Mat3
//

inline Mat3 Mat3::operator/(double s) const
{
	return Mat3(row[0]/s, row[1]/s, row[2]/s);
}

inline Vec3 Mat3::operator*(const Vec3& v) const
{
	return Vec3(row[0]*v, row[1]*v, row[2]*v);
}

#endif // MXVEC3_INCLUDED