source: GTP/trunk/Lib/Geom/shared/GTGeometry/src/libs/gfx/math/Vec3.h @ 1025

Revision 1025, 5.2 KB checked in by gumbau, 18 years ago (diff)

namespace simplif

Line 
1#ifndef GFXMATH_VEC3_INCLUDED // -*- C++ -*-
2#define GFXMATH_VEC3_INCLUDED
3
4/************************************************************************
5
6  3D Vector class.
7
8  $Id: Vec3.h,v 1.6 1997/03/17 22:52:26 garland Exp $
9
10 ************************************************************************/
11
12#include "../std.h"
13
14namespace simplif
15{
16        class Vec3 {
17        private:
18                real elt[3];
19
20        protected:
21                inline void copy(const Vec3& v);
22
23        public:
24                //
25                // Standard constructors
26                //
27                Vec3(real x=0, real y=0, real z=0) { elt[0]=x; elt[1]=y; elt[2]=z; }
28        #ifdef GFXMATH_VEC2_INCLUDED
29                Vec3(const Vec2& v, real z) { elt[0]=v[0]; elt[1]=v[1]; elt[2]=z; }
30        #endif
31                Vec3(const Vec3& v) { copy(v); }
32                Vec3(const real *v) { elt[0]=v[0]; elt[1]=v[1]; elt[2]=v[2]; }
33
34                //
35                // Access methods
36                //
37        #ifdef SAFETY
38                real& operator()(int i)       { assert(i>=0 && i<3); return elt[i]; }
39                real  operator()(int i) const { assert(i>=0 && i<3); return elt[i]; }
40        #else
41                real& operator()(int i)       { return elt[i]; }
42                real  operator()(int i) const { return elt[i]; }
43        #endif
44                real& operator[](int i)       { return elt[i]; }
45                real  operator[](int i) const { return elt[i]; }
46
47                real *raw()             { return elt; }
48                const real *raw() const { return elt; }
49
50                //
51                // Comparison operators
52                //
53                inline bool operator==(const Vec3& v) const;
54                inline bool operator!=(const Vec3& v) const;
55
56                //
57                // Assignment and in-place arithmetic methods
58                //
59                inline void set(real x, real y, real z) { elt[0]=x; elt[1]=y; elt[2]=z; }
60                inline Vec3& operator=(const Vec3& v);
61                inline Vec3& operator+=(const Vec3& v);
62                inline Vec3& operator-=(const Vec3& v);
63                inline Vec3& operator*=(real s);
64                inline Vec3& operator/=(real s);
65
66                //
67                // Binary arithmetic methods
68                //
69                inline Vec3 operator+(const Vec3& v) const;
70                inline Vec3 operator-(const Vec3& v) const;
71                inline Vec3 operator-() const;
72
73                inline Vec3 operator*(real s) const;
74                inline Vec3 operator/(real s) const;
75                inline real operator*(const Vec3& v) const;
76                inline Vec3 operator^(const Vec3& v) const;
77        };
78
79
80
81        ////////////////////////////////////////////////////////////////////////
82        //
83        // Method definitions
84        //
85
86        inline void Vec3::copy(const Vec3& v)
87        {
88                elt[0]=v.elt[0]; elt[1]=v.elt[1]; elt[2]=v.elt[2];
89        }
90
91        inline bool Vec3::operator==(const Vec3& v) const
92        {
93                real dx=elt[X]-v[X],  dy=elt[Y]-v[Y],  dz=elt[Z]-v[Z];
94                return (dx*dx + dy*dy + dz*dz) < FEQ_EPS2;
95        }
96
97        inline bool Vec3::operator!=(const Vec3& v) const
98        {
99                real dx=elt[X]-v[X],  dy=elt[Y]-v[Y],  dz=elt[Z]-v[Z];
100                return (dx*dx + dy*dy + dz*dz) > FEQ_EPS2;
101        }
102
103        inline Vec3& Vec3::operator=(const Vec3& v)
104        {
105                copy(v);
106                return *this;
107        }
108
109        inline Vec3& Vec3::operator+=(const Vec3& v)
110        {
111                elt[0] += v[0];   elt[1] += v[1];   elt[2] += v[2];
112                return *this;
113        }
114
115        inline Vec3& Vec3::operator-=(const Vec3& v)
116        {
117                elt[0] -= v[0];   elt[1] -= v[1];   elt[2] -= v[2];
118                return *this;
119        }
120
121        inline Vec3& Vec3::operator*=(real s)
122        {
123                elt[0] *= s;   elt[1] *= s;   elt[2] *= s;
124                return *this;
125        }
126
127        inline Vec3& Vec3::operator/=(real s)
128        {
129                elt[0] /= s;   elt[1] /= s;   elt[2] /= s;
130                return *this;
131        }
132
133
134        inline Vec3 Vec3::operator+(const Vec3& v) const
135        {
136                return Vec3(elt[0]+v[0], elt[1]+v[1], elt[2]+v[2]);
137        }
138
139        inline Vec3 Vec3::operator-(const Vec3& v) const
140        {
141                return Vec3(elt[0]-v[0], elt[1]-v[1], elt[2]-v[2]);
142        }
143
144        inline Vec3 Vec3::operator-() const
145        {
146                return Vec3(-elt[0], -elt[1], -elt[2]);
147        }
148
149        inline Vec3 Vec3::operator*(real s) const
150        {
151                return Vec3(elt[0]*s, elt[1]*s, elt[2]*s);
152        }
153
154        inline Vec3 Vec3::operator/(real s) const
155        {
156                return Vec3(elt[0]/s, elt[1]/s, elt[2]/s);
157        }
158
159        inline real Vec3::operator*(const Vec3& v) const
160        {
161                return elt[0]*v[0] + elt[1]*v[1] + elt[2]*v[2];
162        }
163
164        inline Vec3 Vec3::operator^(const Vec3& v) const
165        {
166                Vec3 w( elt[1]*v[2] - v[1]*elt[2],
167                   -elt[0]*v[2] + v[0]*elt[2],
168                        elt[0]*v[1] - v[0]*elt[1] );
169                return w;
170        }
171
172        // Make scalar multiplication commutative
173        inline Vec3 operator*(real s, const Vec3& v) { return v*s; }
174
175
176
177        ////////////////////////////////////////////////////////////////////////
178        //
179        // Primitive function definitions
180        //
181
182        inline real norm(const Vec3& v)
183        {
184                return sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
185        }
186
187        inline real norm2(const Vec3& v)
188        {
189                return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
190        }
191
192        inline real length(const Vec3& v) { return norm(v); }
193
194
195        inline real unitize(Vec3& v)
196        {
197                real l=norm2(v);
198                if( l!=1.0 && l!=0.0 )
199                {
200                l = sqrt(l);
201                v /= l;
202                }
203                return l;
204        }
205       
206
207        ////////////////////////////////////////////////////////////////////////
208        //
209        // Misc. function definitions
210        //
211
212/*      inline ostream& operator<<(ostream& out, const Vec3& v)
213        {
214                return out << "[" << v[0] << " " << v[1] << " " << v[2] << "]";
215        }
216
217        inline istream& operator>>(istream& in, Vec3& v)
218        {
219                return in >> "[" >> v[0] >> v[1] >> v[2] >> "]";
220        }
221
222        #ifdef GFXGL_INCLUDED
223        inline void glV(const Vec3& v) { glVertex(v[X], v[Y], v[Z]); }
224        inline void glN(const Vec3& v) { glNormal(v[X], v[Y], v[Z]); }
225        inline void glC(const Vec3& v) { glColor(v[X], v[Y], v[Z]); }
226        #endif*/
227}
228
229
230// GFXMATH_VEC3_INCLUDED
231#endif
Note: See TracBrowser for help on using the repository browser.