1 | #ifndef MXVEC3_INCLUDED // -*- C++ -*-
|
---|
2 | #define MXVEC3_INCLUDED
|
---|
3 | #if !defined(__GNUC__)
|
---|
4 | # pragma once
|
---|
5 | #endif
|
---|
6 |
|
---|
7 | /************************************************************************
|
---|
8 |
|
---|
9 | 3D Vector class
|
---|
10 |
|
---|
11 | Copyright (C) 1998 Michael Garland. See "COPYING.txt" for details.
|
---|
12 |
|
---|
13 | $Id: MxVec3.h,v 1.1 2002/09/24 16:53:54 wimmer Exp $
|
---|
14 |
|
---|
15 | ************************************************************************/
|
---|
16 |
|
---|
17 | #include "MxMath.h"
|
---|
18 | #include "MxVec2.h"
|
---|
19 |
|
---|
20 | #ifdef MIX_USE_TVEC
|
---|
21 | #include "MxVector.h"
|
---|
22 |
|
---|
23 | template<class T>
|
---|
24 | class MxTVec3 : public MxVBlock<T, 3>
|
---|
25 | {
|
---|
26 | public:
|
---|
27 | MxTVec3() { }
|
---|
28 | MxTVec3(T x, T y, T z) { (*this)[0]=x; (*this)[1]=y; (*this)[2]=z; }
|
---|
29 | MxTVec3(const MxVBlock<T, 3>& v) : MxVBlock<T, 3>(v) { }
|
---|
30 | MxTVec3(const float *v) {(*this)[0]=v[0];(*this)[1]=v[1];(*this)[2]=v[2];}
|
---|
31 | MxTVec3(const double *v) {(*this)[0]=v[0];(*this)[1]=v[1];(*this)[2]=v[2];}
|
---|
32 |
|
---|
33 | inline MxTVec3<T> operator^(const MxTVec3<T>& v) const
|
---|
34 | { MxTVec3<T> r; mxv_cross3(r, *this, v); return r; }
|
---|
35 | };
|
---|
36 |
|
---|
37 | typedef MxTVec3<double> Vec3;
|
---|
38 |
|
---|
39 | #else
|
---|
40 |
|
---|
41 | class Vec3 {
|
---|
42 | private:
|
---|
43 | double elt[3];
|
---|
44 |
|
---|
45 | protected:
|
---|
46 | inline void copy(const Vec3& v);
|
---|
47 |
|
---|
48 | public:
|
---|
49 | //
|
---|
50 | // Standard constructors
|
---|
51 | //
|
---|
52 | Vec3(double x=0, double y=0, double z=0) { elt[0]=x; elt[1]=y; elt[2]=z; }
|
---|
53 | Vec3(const Vec2& v, double z) { elt[0]=v[0]; elt[1]=v[1]; elt[2]=z; }
|
---|
54 | Vec3(const Vec3& v) { copy(v); }
|
---|
55 | Vec3(const float *v) { elt[0]=v[0]; elt[1]=v[1]; elt[2]=v[2]; }
|
---|
56 | Vec3(const double *v) { elt[0]=v[0]; elt[1]=v[1]; elt[2]=v[2]; }
|
---|
57 |
|
---|
58 | //
|
---|
59 | // Access methods
|
---|
60 | //
|
---|
61 | double& operator()(uint i) { AssertBound(i<3); return elt[i]; }
|
---|
62 | double operator()(uint i) const { AssertBound(i<3); return elt[i]; }
|
---|
63 | #ifdef __GNUC__
|
---|
64 | double& operator[](uint i) { return elt[i]; }
|
---|
65 | double operator[](uint i) const { return elt[i]; }
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | operator double*() { return elt; }
|
---|
69 | operator const double*() const { return elt; }
|
---|
70 | // operator Vec2&() { return *((Vec2 *)this); }
|
---|
71 |
|
---|
72 | //
|
---|
73 | // Comparison operators
|
---|
74 | //
|
---|
75 | inline bool operator==(const Vec3& v) const;
|
---|
76 | inline bool operator!=(const Vec3& v) const;
|
---|
77 |
|
---|
78 | //
|
---|
79 | // Assignment and in-place arithmetic methods
|
---|
80 | //
|
---|
81 | inline void set(double x,double y,double z) {elt[0]=x; elt[1]=y; elt[2]=z;}
|
---|
82 | inline Vec3& operator=(const Vec3& v);
|
---|
83 | inline Vec3& operator+=(const Vec3& v);
|
---|
84 | inline Vec3& operator-=(const Vec3& v);
|
---|
85 | inline Vec3& operator*=(double s);
|
---|
86 | inline Vec3& operator/=(double s);
|
---|
87 |
|
---|
88 | //
|
---|
89 | // Binary arithmetic methods
|
---|
90 | //
|
---|
91 | inline Vec3 operator+(const Vec3& v) const;
|
---|
92 | inline Vec3 operator-(const Vec3& v) const;
|
---|
93 | inline Vec3 operator-() const;
|
---|
94 |
|
---|
95 | inline Vec3 operator*(double s) const;
|
---|
96 | inline Vec3 operator/(double s) const;
|
---|
97 | inline double operator*(const Vec3& v) const;
|
---|
98 | inline Vec3 operator^(const Vec3& v) const;
|
---|
99 | };
|
---|
100 |
|
---|
101 |
|
---|
102 |
|
---|
103 | ////////////////////////////////////////////////////////////////////////
|
---|
104 | //
|
---|
105 | // Method definitions
|
---|
106 | //
|
---|
107 |
|
---|
108 | inline void Vec3::copy(const Vec3& v)
|
---|
109 | {
|
---|
110 | elt[0]=v.elt[0]; elt[1]=v.elt[1]; elt[2]=v.elt[2];
|
---|
111 | }
|
---|
112 |
|
---|
113 | inline bool Vec3::operator==(const Vec3& v) const
|
---|
114 | {
|
---|
115 | double dx=elt[X]-v[X], dy=elt[Y]-v[Y], dz=elt[Z]-v[Z];
|
---|
116 | return (dx*dx + dy*dy + dz*dz) < FEQ_EPS2;
|
---|
117 | }
|
---|
118 |
|
---|
119 | inline bool Vec3::operator!=(const Vec3& v) const
|
---|
120 | {
|
---|
121 | double dx=elt[X]-v[X], dy=elt[Y]-v[Y], dz=elt[Z]-v[Z];
|
---|
122 | return (dx*dx + dy*dy + dz*dz) > FEQ_EPS2;
|
---|
123 | }
|
---|
124 |
|
---|
125 | inline Vec3& Vec3::operator=(const Vec3& v)
|
---|
126 | {
|
---|
127 | copy(v);
|
---|
128 | return *this;
|
---|
129 | }
|
---|
130 |
|
---|
131 | inline Vec3& Vec3::operator+=(const Vec3& v)
|
---|
132 | {
|
---|
133 | elt[0] += v[0]; elt[1] += v[1]; elt[2] += v[2];
|
---|
134 | return *this;
|
---|
135 | }
|
---|
136 |
|
---|
137 | inline Vec3& Vec3::operator-=(const Vec3& v)
|
---|
138 | {
|
---|
139 | elt[0] -= v[0]; elt[1] -= v[1]; elt[2] -= v[2];
|
---|
140 | return *this;
|
---|
141 | }
|
---|
142 |
|
---|
143 | inline Vec3& Vec3::operator*=(double s)
|
---|
144 | {
|
---|
145 | elt[0] *= s; elt[1] *= s; elt[2] *= s;
|
---|
146 | return *this;
|
---|
147 | }
|
---|
148 |
|
---|
149 | inline Vec3& Vec3::operator/=(double s)
|
---|
150 | {
|
---|
151 | elt[0] /= s; elt[1] /= s; elt[2] /= s;
|
---|
152 | return *this;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | inline Vec3 Vec3::operator+(const Vec3& v) const
|
---|
157 | {
|
---|
158 | return Vec3(elt[0]+v[0], elt[1]+v[1], elt[2]+v[2]);
|
---|
159 | }
|
---|
160 |
|
---|
161 | inline Vec3 Vec3::operator-(const Vec3& v) const
|
---|
162 | {
|
---|
163 | return Vec3(elt[0]-v[0], elt[1]-v[1], elt[2]-v[2]);
|
---|
164 | }
|
---|
165 |
|
---|
166 | inline Vec3 Vec3::operator-() const
|
---|
167 | {
|
---|
168 | return Vec3(-elt[0], -elt[1], -elt[2]);
|
---|
169 | }
|
---|
170 |
|
---|
171 | inline Vec3 Vec3::operator*(double s) const
|
---|
172 | {
|
---|
173 | return Vec3(elt[0]*s, elt[1]*s, elt[2]*s);
|
---|
174 | }
|
---|
175 |
|
---|
176 | inline Vec3 Vec3::operator/(double s) const
|
---|
177 | {
|
---|
178 | return Vec3(elt[0]/s, elt[1]/s, elt[2]/s);
|
---|
179 | }
|
---|
180 |
|
---|
181 | inline double Vec3::operator*(const Vec3& v) const
|
---|
182 | {
|
---|
183 | return elt[0]*v[0] + elt[1]*v[1] + elt[2]*v[2];
|
---|
184 | }
|
---|
185 |
|
---|
186 | inline Vec3 Vec3::operator^(const Vec3& v) const
|
---|
187 | {
|
---|
188 | Vec3 w( elt[1]*v[2] - v[1]*elt[2],
|
---|
189 | -elt[0]*v[2] + v[0]*elt[2],
|
---|
190 | elt[0]*v[1] - v[0]*elt[1] );
|
---|
191 | return w;
|
---|
192 | }
|
---|
193 | #endif
|
---|
194 |
|
---|
195 | // Make scalar multiplication commutative
|
---|
196 | inline Vec3 operator*(double s, const Vec3& v) { return v*s; }
|
---|
197 |
|
---|
198 |
|
---|
199 |
|
---|
200 | ////////////////////////////////////////////////////////////////////////
|
---|
201 | //
|
---|
202 | // Primitive function definitions
|
---|
203 | //
|
---|
204 |
|
---|
205 | inline double norm(const Vec3& v)
|
---|
206 | {
|
---|
207 | return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
|
---|
208 | }
|
---|
209 |
|
---|
210 | inline double norm2(const Vec3& v)
|
---|
211 | {
|
---|
212 | return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
|
---|
213 | }
|
---|
214 |
|
---|
215 | inline double unitize(Vec3& v)
|
---|
216 | {
|
---|
217 | double l=norm2(v);
|
---|
218 | if( l!=1.0 && l!=0.0 )
|
---|
219 | {
|
---|
220 | l = sqrt(l);
|
---|
221 | v /= l;
|
---|
222 | }
|
---|
223 | return l;
|
---|
224 | }
|
---|
225 |
|
---|
226 | inline Vec2 proj(const Vec3& v)
|
---|
227 | {
|
---|
228 | Vec2 u(v[0], v[1]);
|
---|
229 | if( v[2]!=1.0 && v[2]!=0.0 )
|
---|
230 | u /= v[2];
|
---|
231 | return u;
|
---|
232 | }
|
---|
233 |
|
---|
234 |
|
---|
235 |
|
---|
236 | ////////////////////////////////////////////////////////////////////////
|
---|
237 | //
|
---|
238 | // Misc. function definitions
|
---|
239 | //
|
---|
240 |
|
---|
241 | inline ostream& operator<<(ostream& out, const Vec3& v)
|
---|
242 | {
|
---|
243 | return out << v[0] << " " << v[1] << " " << v[2];
|
---|
244 | }
|
---|
245 |
|
---|
246 | inline istream& operator>>(istream& in, Vec3& v)
|
---|
247 | {
|
---|
248 | return in >> v[0] >> v[1] >> v[2];
|
---|
249 | }
|
---|
250 |
|
---|
251 | #ifdef MXGL_INCLUDED
|
---|
252 | inline void glV(const Vec3& v) { glVertex3d(v[X], v[Y], v[Z]); }
|
---|
253 | inline void glN(const Vec3& v) { glNormal3d(v[X], v[Y], v[Z]); }
|
---|
254 | inline void glC(const Vec3& v) { glColor3d(v[X], v[Y], v[Z]); }
|
---|
255 | #endif
|
---|
256 |
|
---|
257 |
|
---|
258 | // MXVEC3_INCLUDED
|
---|
259 | #endif
|
---|