1 #ifndef _vec3_h
2 #define _vec3_h
3
4 #ifndef _real_h
5 # include "real.h"
6 #endif
7
8 //
9 // -------------------------------------------------------------------------
10 // class Vec3 : 3-dimensionale Vektorklasse
11 // -------------------------------------------------------------------------
12 //
13 /*
14
15 Z
16 | Y
17 | /
18 | /
19 |/
20 ---|------------X
21 /|
22
23 */
24
25 #if (Vec3IsVector)
26
27 #ifndef _vector_h
28 # include "vector.h"
29 #endif
30
31 class Vec3 : public Vector {
32 public:
33
X()34 const Real &X() const { return data[0]; }
Y()35 const Real &Y() const { return data[1]; }
Z()36 const Real &Z() const { return data[2]; }
37
38 Vec3( const Real &l, const Real &b ); // Polarkoordinaten-Konstruktor (Norm)
Vec3(double x,double y,double z)39 Vec3( double x, double y, double z ) : Vector( 3, x, y, z ) {}
Vec3(const Vector & v)40 Vec3( const Vector &v ) : Vector(v) { Resize(3); }
Vec3()41 Vec3() {}
42
43 const Vec3& operator=(const Vector &v)
44 { Vector::operator=(v); return *this; }
45
46 static Real AngleRad( const Real &x, const Real &y );
47
48 void GetPolarRad( Real *l, Real *b ) const ;
49 void GetPolarRad( Real *l, Real *b, Real *r );
GetPolarDeg(Real * l,Real * b)50 void GetPolarDeg( Real *l, Real *b ) const {
51 GetPolarRad(l,b);
52 *l *= Real(180/M_PI);
53 *b *= Real(180/M_PI);
54 }
GetPolarDeg(Real * l,Real * b,Real * r)55 void GetPolarDeg( Real *l, Real *b, Real *r ) {
56 GetPolarRad(l,b,r);
57 *l *= Real(180/M_PI);
58 *b *= Real(180/M_PI);
59 }
60
61 Vec3 XTurnAngleRad( const Real &angle ) const;
XTurnAngleDeg(const Real & angle)62 Vec3 XTurnAngleDeg( const Real &angle ) const
63 { return XTurnAngleRad(angle/Real(180/M_PI)); }
64 Vec3 YTurnAngleRad( const Real &angle ) const;
YTurnAngleDeg(const Real & angle)65 Vec3 YTurnAngleDeg( const Real &angle ) const
66 { return YTurnAngleRad(angle/Real(180/M_PI)); }
67 Vec3 ZTurnAngleRad( const Real &angle ) const;
ZTurnAngleDeg(const Real & angle)68 Vec3 ZTurnAngleDeg( const Real &angle ) const
69 { return ZTurnAngleRad(angle/Real(180/M_PI)); }
70
71
72 Real AngleRadTo( Vec3 &d );
AngleDegTo(Vec3 & d)73 Real AngleDegTo( Vec3 &d ) { return AngleRadTo(d)*Real(180.0/M_PI); }
74 };
75
76 #else
77
78 class Vec3 {
79 private:
80 Real x_val;
81 Real y_val;
82 Real z_val;
83 // protected:
84 Real norm; // Cache zur optimierten Berechnen von Norm() wird der Wert
85 public:
86
X()87 const Real &X() const { return x_val; }
Y()88 const Real &Y() const { return y_val; }
Z()89 const Real &Z() const { return z_val; }
90
IsZero()91 int IsZero() const { return X()==0.0&&Y()==0.0&&Z()==0.0; }
92
93 Vec3( const Real &l, const Real &b ); // Polarkoordinaten-Konstruktor (Norm)
94 Vec3( const Real &x, const Real &y, const Real &z, Real n=0.0 )
95 { x_val=x; y_val=y; z_val=z; norm=n; }
Vec3(const Vec3 & v)96 Vec3( const Vec3 &v ) { x_val=v.X(); y_val=v.Y(); z_val=v.Z();
97 norm=v.norm; }
Vec3()98 Vec3() { }
99
100 static Real AngleRad( const Real &x, const Real &y );
101
102 void GetPolarRad( Real *l, Real *b ) const ;
103 void GetPolarRad( Real *l, Real *b, Real *r );
GetPolarDeg(Real * l,Real * b)104 void GetPolarDeg( Real *l, Real *b ) const {
105 GetPolarRad(l,b);
106 *l *= Real(180/M_PI);
107 *b *= Real(180/M_PI);
108 }
GetPolarDeg(Real * l,Real * b,Real * r)109 void GetPolarDeg( Real *l, Real *b, Real *r ) {
110 GetPolarRad(l,b,r);
111 *l *= Real(180/M_PI);
112 *b *= Real(180/M_PI);
113 }
114
115 Vec3 XTurnAngleRad( const Real &angle ) const;
XTurnAngleDeg(const Real & angle)116 Vec3 XTurnAngleDeg( const Real &angle ) const
117 { return XTurnAngleRad(angle/Real(180/M_PI)); }
118 Vec3 YTurnAngleRad( const Real &angle ) const;
YTurnAngleDeg(const Real & angle)119 Vec3 YTurnAngleDeg( const Real &angle ) const
120 { return YTurnAngleRad(angle/Real(180/M_PI)); }
121 Vec3 ZTurnAngleRad( const Real &angle ) const;
ZTurnAngleDeg(const Real & angle)122 Vec3 ZTurnAngleDeg( const Real &angle ) const
123 { return ZTurnAngleRad(angle/Real(180/M_PI)); }
124
125 Real SqrNorm() const;
126 Real Norm();
127 Vec3 Norm1();
128
129 Real AngleRadTo( Vec3 &d );
AngleDegTo(Vec3 & d)130 Real AngleDegTo( Vec3 &d ) { return AngleRadTo(d)*Real(180.0/M_PI); }
131
132 inline const Vec3& operator=(const Vec3 &v);
133
134 // Binary Operator Functions
135
136 inline Vec3 operator+(const Vec3&) const;
137 inline Vec3 operator-(const Vec3&) const;
138
139 #ifndef __TURBOC__
140 friend inline Real operator*(const Vec3&, const Vec3&);
141 friend inline Vec3 operator*(const Real&, const Vec3&);
142 friend inline int operator==(const Vec3&, const Vec3&);
143 friend inline int operator!=(const Vec3&, const Vec3&);
144 #else
145 friend Real operator*(const Vec3&, const Vec3&);
146 friend Vec3 operator*(const Real&, const Vec3&);
147 friend int operator==(const Vec3&, const Vec3&);
148 friend int operator!=(const Vec3&, const Vec3&);
149 #endif
150 inline Vec3 operator*(const Real&) const;
151 inline Vec3 operator/(const Real&) const;
152
153 inline const Vec3& operator+=(const Vec3&);
154 inline const Vec3& operator-=(const Vec3&);
155 inline const Vec3& operator*=(const Real&);
156 inline const Vec3& operator/=(const Real&);
157 inline Vec3 operator+() const;
158 inline Vec3 operator-() const;
159
160 };
161
162 inline const Vec3& Vec3::operator=(const Vec3 &v) {
163 x_val = v.x_val;
164 y_val = v.y_val;
165 z_val = v.z_val;
166 norm = 0.0;
167 return *this;
168 }
169
170 inline Vec3 Vec3::operator+() const
171 {
172 return *this;
173 }
174
175 inline Vec3 Vec3::operator-() const
176 {
177 return Vec3(-x_val, -y_val, -z_val, norm);
178 }
179
180
181 // Definitions of compound-assignment operator member functions
182
183 inline const Vec3& Vec3::operator+=(const Vec3& z2)
184 {
185 x_val += z2.x_val;
186 y_val += z2.y_val;
187 z_val += z2.z_val;
188 norm = 0.0;
189 return *this;
190 }
191
192 inline const Vec3& Vec3::operator-=(const Vec3& z2)
193 {
194 x_val -= z2.x_val;
195 y_val -= z2.y_val;
196 z_val -= z2.z_val;
197 norm = 0.0;
198 return *this;
199 }
200
201 inline const Vec3& Vec3::operator*=(const Real& val)
202 {
203 x_val *= val;
204 y_val *= val;
205 z_val *= val;
206 norm *= val;
207 return *this;
208 }
209
210 inline const Vec3& Vec3::operator/=(const Real& val)
211 {
212 x_val /= val;
213 y_val /= val;
214 z_val /= val;
215 norm /= val;
216 return *this;
217 }
218
219
220 // Definitions of non-member binary operator functions
221
222 inline Vec3 Vec3::operator+(const Vec3& z2) const
223 {
224 return Vec3(x_val + z2.x_val, y_val + z2.y_val, z_val + z2.z_val);
225 }
226 inline Vec3 Vec3::operator-(const Vec3& z2) const
227 {
228 return Vec3(x_val - z2.x_val, y_val - z2.y_val, z_val - z2.z_val);
229 }
230
231
232 inline Real operator*(const Vec3& z1, const Vec3& z2)
233 {
234 return z1.x_val*z2.x_val + z1.y_val*z2.y_val + z1.z_val*z2.z_val;
235 }
236 inline Vec3 Vec3::operator*(const Real& val2) const
237 {
238 return Vec3(x_val*val2, y_val*val2, z_val*val2, norm*val2);
239 }
240 inline Vec3 operator*(const Real& val, const Vec3& z2)
241 {
242 return Vec3(z2.x_val*val, z2.y_val*val, z2.z_val*val);
243 }
244
245 inline Vec3 Vec3::operator/(const Real& val) const
246 {
247 return Vec3(x_val/val, y_val/val, z_val/val, norm/val);
248 }
249
250 inline int operator==(const Vec3& z1, const Vec3& z2)
251 {
252 return z1.x_val == z2.x_val && z1.y_val == z2.y_val && z1.z_val == z2.z_val;
253 }
254
255 inline int operator!=(const Vec3& z1, const Vec3& z2)
256 {
257 return z1.x_val != z2.x_val || z1.y_val != z2.y_val || z1.z_val != z2.z_val;
258 }
259
SqrNorm()260 inline Real Vec3::SqrNorm() const { return X()*X()+Y()*Y()+Z()*Z(); }
Norm()261 inline Real Vec3::Norm() {
262 if (norm==0.0) norm=sqrt(SqrNorm());
263 return norm;
264 }
Norm1()265 inline Vec3 Vec3::Norm1() { return *this / Norm(); }
266
267 #endif
268
269 extern Vec3 Vec3Zero;
270
271 #endif
272