root/trunk/contrib/vmap_extractor_v2/vmapextract/vec3d.h

Revision 2, 3.0 kB (checked in by yumileroy, 17 years ago)

[svn] * Proper SVN structure

Original author: Neo2003
Date: 2008-10-02 16:23:55-05:00

Line 
1#ifndef VEC3D_H
2#define VEC3D_H
3
4#include <iostream>
5#include <cmath>
6
7
8
9class Vec3D {
10public:
11        float x,y,z;
12
13        Vec3D(float x0 = 0.0f, float y0 = 0.0f, float z0 = 0.0f) : x(x0), y(y0), z(z0) {}
14
15        Vec3D(const Vec3D& v) : x(v.x), y(v.y), z(v.z) {}
16
17        Vec3D& operator= (const Vec3D &v) {
18        x = v.x;
19                y = v.y;
20                z = v.z;
21                return *this;
22        }
23
24        Vec3D operator+ (const Vec3D &v) const
25        {
26                Vec3D r(x+v.x,y+v.y,z+v.z);
27        return r;
28        }
29
30        Vec3D operator- (const Vec3D &v) const
31        {
32                Vec3D r(x-v.x,y-v.y,z-v.z);
33                return r;
34        }
35
36        float operator* (const Vec3D &v) const
37        {
38        return x*v.x + y*v.y + z*v.z;
39        }
40
41        Vec3D operator* (float d) const
42        {
43                Vec3D r(x*d,y*d,z*d);
44        return r;
45        }
46
47        friend Vec3D operator* (float d, const Vec3D& v)
48        {
49                return v * d;
50        }
51
52        Vec3D operator% (const Vec3D &v) const
53        {
54        Vec3D r(y*v.z-z*v.y, z*v.x-x*v.z, x*v.y-y*v.x);
55                return r;
56        }
57
58        Vec3D& operator+= (const Vec3D &v)
59        {
60                x += v.x;
61                y += v.y;
62                z += v.z;
63                return *this;
64        }
65
66        Vec3D& operator-= (const Vec3D &v)
67        {
68                x -= v.x;
69                y -= v.y;
70                z -= v.z;
71                return *this;
72        }
73
74        Vec3D& operator*= (float d)
75        {
76                x *= d;
77                y *= d;
78                z *= d;
79                return *this;
80        }
81
82        float lengthSquared() const
83        {
84                return x*x+y*y+z*z;
85        }
86
87        float length() const
88        {
89        return sqrt(x*x+y*y+z*z);
90        }
91
92        Vec3D& normalize()
93        {
94                this->operator*= (1.0f/length());
95                return *this;
96        }
97
98        Vec3D operator~ () const
99        {
100                Vec3D r(*this);
101        r.normalize();
102                return r;
103        }
104
105        friend std::istream& operator>>(std::istream& in, Vec3D& v)
106        {
107                in >> v.x >> v.y >> v.z;
108                return in;
109        }
110
111        operator float*()
112        {
113                return (float*)this;
114        }
115
116};
117
118
119class Vec2D {
120public:
121        float x,y;
122       
123        Vec2D(float x0 = 0.0f, float y0 = 0.0f) : x(x0), y(y0) {}
124
125        Vec2D(const Vec2D& v) : x(v.x), y(v.y) {}
126
127        Vec2D& operator= (const Vec2D &v) {
128        x = v.x;
129                y = v.y;
130                return *this;
131        }
132
133        Vec2D operator+ (const Vec2D &v) const
134        {
135                Vec2D r(x+v.x,y+v.y);
136        return r;
137        }
138
139        Vec2D operator- (const Vec2D &v) const
140        {
141                Vec2D r(x-v.x,y-v.y);
142                return r;
143        }
144
145        float operator* (const Vec2D &v) const
146        {
147        return x*v.x + y*v.y;
148        }
149
150        Vec2D operator* (float d) const
151        {
152                Vec2D r(x*d,y*d);
153        return r;
154        }
155
156        friend Vec2D operator* (float d, const Vec2D& v)
157        {
158                return v * d;
159        }
160
161        Vec2D& operator+= (const Vec2D &v)
162        {
163                x += v.x;
164                y += v.y;
165                return *this;
166        }
167
168        Vec2D& operator-= (const Vec2D &v)
169        {
170                x -= v.x;
171                y -= v.y;
172                return *this;
173        }
174
175        Vec2D& operator*= (float d)
176        {
177                x *= d;
178                y *= d;
179                return *this;
180        }
181
182        float lengthSquared() const
183        {
184                return x*x+y*y;
185        }
186
187        float length() const
188        {
189        return sqrt(x*x+y*y);
190        }
191
192        Vec2D& normalize()
193        {
194                this->operator*= (1.0f/length());
195                return *this;
196        }
197
198        Vec2D operator~ () const
199        {
200                Vec2D r(*this);
201        r.normalize();
202                return r;
203        }
204
205
206        friend std::istream& operator>>(std::istream& in, Vec2D& v)
207        {
208        in >> v.x >> v.y;
209                return in;
210        }
211
212        operator float*()
213        {
214                return (float*)this;
215        }
216
217};
218
219
220inline void rotate(float x0, float y0, float *x, float *y, float angle)
221{
222        float xa = *x - x0, ya = *y - y0;
223        *x = xa*cosf(angle) - ya*sinf(angle) + x0;
224        *y = xa*sinf(angle) + ya*cosf(angle) + y0;
225}
226
227
228
229#endif
230
Note: See TracBrowser for help on using the browser.