File: vec3.h

package info (click to toggle)
flying 6.20-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, lenny
  • size: 752 kB
  • ctags: 1,873
  • sloc: cpp: 10,966; makefile: 189
file content (271 lines) | stat: -rw-r--r-- 6,754 bytes parent folder | download | duplicates (4)
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
#ifndef _vec3_h
#define _vec3_h

#ifndef _real_h
#	include "real.h"
#endif

//
// -------------------------------------------------------------------------
// class Vec3 : 3-dimensionale Vektorklasse
// -------------------------------------------------------------------------
//
/*

						Z
					   |   Y
					   |  /
					   | /
					   |/
					---|------------X
					  /|

 */

#if (Vec3IsVector)

#ifndef _vector_h
#	include "vector.h"
#endif

class Vec3 : public Vector {
	public:

		const Real &X() const 		{ return data[0]; }
		const Real &Y() const 		{ return data[1]; }
		const Real &Z() const 		{ return data[2]; }

		Vec3( const Real &l, const Real &b );	// Polarkoordinaten-Konstruktor (Norm)
		Vec3( double x, double y, double z ) : Vector( 3, x, y, z ) {}
		Vec3( const Vector &v ) : Vector(v)		{ Resize(3); }
		Vec3()											{}

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

		static Real AngleRad( const Real &x, const Real &y );

		void GetPolarRad( Real *l, Real *b ) const ;
		void GetPolarRad( Real *l, Real *b, Real *r );
		void GetPolarDeg( Real *l, Real *b ) const {
			GetPolarRad(l,b);
			*l *= Real(180/M_PI);
			*b *= Real(180/M_PI);
		}
		void GetPolarDeg( Real *l, Real *b, Real *r ) {
			GetPolarRad(l,b,r);
			*l *= Real(180/M_PI);
			*b *= Real(180/M_PI);
		}

		Vec3 XTurnAngleRad( const Real &angle ) const;
		Vec3 XTurnAngleDeg( const Real &angle ) const
			{ return XTurnAngleRad(angle/Real(180/M_PI)); }
		Vec3 YTurnAngleRad( const Real &angle ) const;
		Vec3 YTurnAngleDeg( const Real &angle ) const
			{ return YTurnAngleRad(angle/Real(180/M_PI)); }
		Vec3 ZTurnAngleRad( const Real &angle ) const;
		Vec3 ZTurnAngleDeg( const Real &angle ) const
			{ return ZTurnAngleRad(angle/Real(180/M_PI)); }


		Real AngleRadTo( Vec3 &d );
		Real AngleDegTo( Vec3 &d )		{ return AngleRadTo(d)*Real(180.0/M_PI); }
};

#else

class Vec3 {
	private:
		Real	x_val;
		Real	y_val;
		Real	z_val;
//	protected:
		Real	norm;		// Cache zur optimierten Berechnen von Norm() wird der Wert
	public:

		const Real &X() const 		{ return x_val; }
		const Real &Y() const 		{ return y_val; }
		const Real &Z() const 		{ return z_val; }

		int	IsZero() const			{ return X()==0.0&&Y()==0.0&&Z()==0.0; }

		Vec3( const Real &l, const Real &b );	// Polarkoordinaten-Konstruktor (Norm)
		Vec3( const Real &x, const Real &y, const Real &z, Real n=0.0 )
											{ x_val=x; y_val=y; z_val=z; norm=n; }
		Vec3( const Vec3 &v )		{ x_val=v.X(); y_val=v.Y(); z_val=v.Z();
											  norm=v.norm; }
		Vec3()							{ }

		static Real AngleRad( const Real &x, const Real &y );

		void GetPolarRad( Real *l, Real *b ) const ;
		void GetPolarRad( Real *l, Real *b, Real *r );
		void GetPolarDeg( Real *l, Real *b ) const {
			GetPolarRad(l,b);
			*l *= Real(180/M_PI);
			*b *= Real(180/M_PI);
		}
		void GetPolarDeg( Real *l, Real *b, Real *r ) {
			GetPolarRad(l,b,r);
			*l *= Real(180/M_PI);
			*b *= Real(180/M_PI);
		}

		Vec3 XTurnAngleRad( const Real &angle ) const;
		Vec3 XTurnAngleDeg( const Real &angle ) const
			{ return XTurnAngleRad(angle/Real(180/M_PI)); }
		Vec3 YTurnAngleRad( const Real &angle ) const;
		Vec3 YTurnAngleDeg( const Real &angle ) const
			{ return YTurnAngleRad(angle/Real(180/M_PI)); }
		Vec3 ZTurnAngleRad( const Real &angle ) const;
		Vec3 ZTurnAngleDeg( const Real &angle ) const
			{ return ZTurnAngleRad(angle/Real(180/M_PI)); }

		Real SqrNorm() const;
		Real Norm();
		Vec3 Norm1();

		Real AngleRadTo( Vec3 &d );
		Real AngleDegTo( Vec3 &d )		{ return AngleRadTo(d)*Real(180.0/M_PI); }

	inline const Vec3& operator=(const Vec3 &v);

	// Binary Operator Functions

	inline Vec3 operator+(const Vec3&) const;
	inline Vec3 operator-(const Vec3&) const;

#ifndef __TURBOC__
	friend inline Real operator*(const Vec3&, const Vec3&);
	friend inline Vec3 operator*(const Real&, const Vec3&);
	friend inline int operator==(const Vec3&, const Vec3&);
	friend inline int operator!=(const Vec3&, const Vec3&);
#else
	friend Real operator*(const Vec3&, const Vec3&);
	friend Vec3 operator*(const Real&, const Vec3&);
	friend int operator==(const Vec3&, const Vec3&);
	friend int operator!=(const Vec3&, const Vec3&);
#endif
	inline Vec3 operator*(const Real&) const;
	inline Vec3 operator/(const Real&) const;

	inline const Vec3& operator+=(const Vec3&);
	inline const Vec3& operator-=(const Vec3&);
	inline const Vec3& operator*=(const Real&);
	inline const Vec3& operator/=(const Real&);
	inline Vec3 operator+() const;
	inline Vec3 operator-() const;

};

inline const Vec3& Vec3::operator=(const Vec3 &v) {
	x_val = v.x_val;
	y_val = v.y_val;
	z_val = v.z_val;
	norm  = 0.0;
	return *this;
}

inline Vec3 Vec3::operator+() const
{
	return *this;
}

inline Vec3 Vec3::operator-() const
{
	return Vec3(-x_val, -y_val, -z_val, norm);
}


// Definitions of compound-assignment operator member functions

inline const Vec3& Vec3::operator+=(const Vec3& z2)
{
	x_val += z2.x_val;
	y_val += z2.y_val;
	z_val += z2.z_val;
	norm   = 0.0;
	return *this;
}

inline const Vec3& Vec3::operator-=(const Vec3& z2)
{
	x_val -= z2.x_val;
	y_val -= z2.y_val;
	z_val -= z2.z_val;
	norm   = 0.0;
	return *this;
}

inline const Vec3& Vec3::operator*=(const Real& val)
{
	x_val *= val;
	y_val *= val;
	z_val *= val;
	norm  *= val;
	return *this;
}

inline const Vec3& Vec3::operator/=(const Real& val)
{
	x_val /= val;
	y_val /= val;
	z_val /= val;
	norm  /= val;
	return *this;
}


// Definitions of non-member binary operator functions

inline Vec3 Vec3::operator+(const Vec3& z2) const
{
		  return Vec3(x_val + z2.x_val, y_val + z2.y_val, z_val + z2.z_val);
}
inline Vec3 Vec3::operator-(const Vec3& z2) const
{
		  return Vec3(x_val - z2.x_val, y_val - z2.y_val, z_val - z2.z_val);
}


inline Real operator*(const Vec3& z1, const Vec3& z2)
{
		  return z1.x_val*z2.x_val + z1.y_val*z2.y_val + z1.z_val*z2.z_val;
}
inline Vec3 Vec3::operator*(const Real& val2) const
{
		  return Vec3(x_val*val2, y_val*val2, z_val*val2, norm*val2);
}
inline Vec3 operator*(const Real& val, const Vec3& z2)
{
		  return Vec3(z2.x_val*val, z2.y_val*val, z2.z_val*val);
}

inline Vec3 Vec3::operator/(const Real& val) const
{
		  return Vec3(x_val/val, y_val/val, z_val/val, norm/val);
}

inline int operator==(const Vec3& z1, const Vec3& z2)
{
		  return z1.x_val == z2.x_val && z1.y_val == z2.y_val && z1.z_val == z2.z_val;
}

inline int operator!=(const Vec3& z1, const Vec3& z2)
{
		  return z1.x_val != z2.x_val || z1.y_val != z2.y_val || z1.z_val != z2.z_val;
}

inline Real Vec3::SqrNorm() const	{ return X()*X()+Y()*Y()+Z()*Z(); }
inline Real Vec3::Norm() {
	if (norm==0.0)	norm=sqrt(SqrNorm());
	return norm;
}
inline Vec3 Vec3::Norm1() { return *this / Norm(); }

#endif

extern Vec3 Vec3Zero;

#endif