root/trunk/dep/include/g3dlite/G3D/Vector2.h

Revision 2, 9.2 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/**
2  @file Vector2.h
3 
4  2D vector class
5 
6  @maintainer Morgan McGuire, matrix@graphics3d.com
7 
8  @created 2001-06-02
9  @edited  2006-01-14
10  Copyright 2000-2006, Morgan McGuire.
11  All rights reserved.
12*/
13
14#ifndef G3D_VECTOR2_H
15#define G3D_VECTOR2_H
16
17#include "G3D/platform.h"
18#include "G3D/g3dmath.h"
19#include "Vector2int16.h"
20#include <string>
21
22namespace G3D {
23
24class Vector2;   
25class Vector3;
26class Vector4;
27
28/**
29 Do not subclass-- this implementation makes assumptions about the
30 memory layout.
31 */
32class Vector2 {
33private:
34    // Hidden operators
35    bool operator<(const Vector2&) const;
36    bool operator>(const Vector2&) const;
37    bool operator<=(const Vector2&) const;
38    bool operator>=(const Vector2&) const;
39
40public:
41    // coordinates
42    float x, y;
43
44    // construction
45    Vector2();
46    Vector2(float x, float y);
47    Vector2(float coordinate[2]);
48    Vector2(double coordinate[2]);
49    Vector2(const Vector2& rkVector);
50    Vector2(const class Vector2int16& v); 
51
52    float& operator[] (int i);
53    const float& operator[] (int i) const;
54    operator float* ();
55    operator const float* () const;
56
57    // assignment and comparison
58    Vector2& operator= (const Vector2& rkVector);
59    bool operator== (const Vector2& rkVector) const;
60    bool operator!= (const Vector2& rkVector) const;
61    unsigned int hashCode() const;
62    bool fuzzyEq(const Vector2& other) const;
63    bool fuzzyNe(const Vector2& other) const;
64    /** Returns true if this vector has finite length */
65    bool isFinite() const;
66
67    /** Returns true if this vector has length == 0 */
68    bool isZero() const;
69
70    /** Returns true if this vector has length == 1 */
71    bool isUnit() const;
72
73    // arithmetic operations
74    Vector2 operator+ (const Vector2& rkVector) const;
75    Vector2 operator- (const Vector2& rkVector) const;
76    Vector2 operator* (float fScalar) const;
77    Vector2 operator* (const Vector2& rkVector) const;
78    Vector2 operator/ (const Vector2& rkVector) const;
79    Vector2 operator/ (float fScalar) const;
80    Vector2 operator- () const;
81
82    inline float sum() const {
83        return x + y;
84    }
85
86    /**
87     Linear interpolation
88     */
89    inline Vector2 lerp(const Vector2& v, float alpha) const {
90        return (*this) + (v - *this) * alpha; 
91    }
92
93    inline Vector2 clamp(const Vector2& low, const Vector2& high) const {
94        return Vector2(
95            G3D::clamp(x, low.x, high.x),
96            G3D::clamp(y, low.y, high.y));
97    }
98
99    inline Vector2 clamp(float low, float high) const {
100        return Vector2(
101            (float)G3D::clamp(x, low, high),
102            (float)G3D::clamp(y, low, high));
103    }
104
105    // arithmetic updates
106    Vector2& operator+= (const Vector2& rkVector);
107    Vector2& operator-= (const Vector2& rkVector);
108    Vector2& operator*= (float fScalar);
109    Vector2& operator/= (float fScalar);
110    Vector2& operator*= (const Vector2& rkVector);
111    Vector2& operator/= (const Vector2& rkVector);
112
113    // vector operations
114    float length() const;
115    Vector2 direction() const;
116    /**
117     Potentially less accurate but faster than direction().
118     Only works if System::hasSSE is true.
119     */
120    Vector2 fastDirection() const {
121        return direction();
122    }
123
124    float squaredLength () const;
125    float dot (const Vector2& rkVector) const;
126    float unitize (float fTolerance = 1e-06);
127
128    Vector2 min(const Vector2 &v) const;
129    Vector2 max(const Vector2 &v) const;
130
131    // Random unit vector
132    static Vector2 random();
133
134    // Special values.
135    // Intentionally not inlined: see Matrix3::identity() for details.
136    static const Vector2& zero();
137    inline static const Vector2& one() { static const Vector2 v(1, 1); return v; }
138    static const Vector2& unitX();
139    static const Vector2& unitY();
140    static const Vector2& inf(); 
141    static const Vector2& nan();
142    /** smallest (most negative) representable vector */
143    static const Vector2& minFinite(); 
144    /** Largest representable vector */
145    static const Vector2& maxFinite();
146
147
148
149    // Deprecated. See Matrix3::identity() for details.
150    /** @deprecated Use Vector2::zero() */
151    static const Vector2 ZERO;
152    /** @deprecated Use Vector2::unitX() */
153    static const Vector2 UNIT_S;
154    /** @deprecated Use Vector2::unitY() */
155    static const Vector2 UNIT_T;
156
157    std::string toString() const;
158
159    // 2-char swizzles
160
161    Vector2 xx() const;
162    Vector2 yx() const;
163    Vector2 xy() const;
164    Vector2 yy() const;
165
166    // 3-char swizzles
167
168    Vector3 xxx() const;
169    Vector3 yxx() const;
170    Vector3 xyx() const;
171    Vector3 yyx() const;
172    Vector3 xxy() const;
173    Vector3 yxy() const;
174    Vector3 xyy() const;
175    Vector3 yyy() const;
176
177    // 4-char swizzles
178
179    Vector4 xxxx() const;
180    Vector4 yxxx() const;
181    Vector4 xyxx() const;
182    Vector4 yyxx() const;
183    Vector4 xxyx() const;
184    Vector4 yxyx() const;
185    Vector4 xyyx() const;
186    Vector4 yyyx() const;
187    Vector4 xxxy() const;
188    Vector4 yxxy() const;
189    Vector4 xyxy() const;
190    Vector4 yyxy() const;
191    Vector4 xxyy() const;
192    Vector4 yxyy() const;
193    Vector4 xyyy() const;
194    Vector4 yyyy() const;
195
196};
197
198inline Vector2 operator*(double s, const Vector2& v) {
199    return v * (float)s;
200}
201
202inline Vector2 operator*(float s, const Vector2& v) {
203    return v * s;
204}
205
206inline Vector2 operator*(int s, const Vector2& v) {
207    return v * (float)s;
208}
209
210
211inline unsigned int hashCode(const G3D::Vector2& v) {
212     return v.hashCode();
213}
214
215
216inline Vector2::Vector2 () : x(0.0f), y(0.0f) {
217}
218
219
220inline Vector2::Vector2(float _x, float _y) : x(_x), y(_y) {
221}
222
223
224inline Vector2::Vector2 (float afCoordinate[2]) {
225    x = afCoordinate[0];
226    y = afCoordinate[1];
227}
228
229
230
231inline Vector2::Vector2 (double afCoordinate[2]) {
232    x = (float)afCoordinate[0];
233    y = (float)afCoordinate[1];
234}
235
236
237inline Vector2::Vector2 (const Vector2& rkVector) {
238    x = rkVector.x;
239    y = rkVector.y;
240}
241
242
243inline Vector2::Vector2 (const Vector2int16& v) : x(v.x), y(v.y) {
244}
245
246
247inline float& Vector2::operator[] (int i) {
248    return ((float*)this)[i];
249}
250
251
252inline const float& Vector2::operator[] (int i) const {
253    return ((float*)this)[i];
254}
255
256
257inline Vector2::operator float* () {
258    return (float*)this;
259}
260
261inline Vector2::operator const float* () const {
262    return (float*)this;
263}
264
265
266inline Vector2& Vector2::operator= (const Vector2& rkVector) {
267    x = rkVector.x;
268    y = rkVector.y;
269    return *this;
270}
271
272
273inline bool Vector2::operator== (const Vector2& rkVector) const {
274    return ( x == rkVector.x && y == rkVector.y);
275}
276
277
278inline bool Vector2::operator!= (const Vector2& rkVector) const {
279    return ( x != rkVector.x || y != rkVector.y);
280}
281
282
283inline Vector2 Vector2::operator+ (const Vector2& rkVector) const {
284    return Vector2(x + rkVector.x, y + rkVector.y);
285}
286
287
288inline Vector2 Vector2::operator- (const Vector2& rkVector) const {
289    return Vector2(x - rkVector.x, y - rkVector.y);
290}
291
292
293inline Vector2 Vector2::operator* (float fScalar) const {
294    return Vector2(fScalar*x, fScalar*y);
295}
296
297
298
299inline Vector2 Vector2::operator- () const {
300    return Vector2( -x, -y);
301}
302
303
304
305inline Vector2& Vector2::operator+= (const Vector2& rkVector) {
306    x += rkVector.x;
307    y += rkVector.y;
308    return *this;
309}
310
311
312
313inline Vector2& Vector2::operator-= (const Vector2& rkVector) {
314    x -= rkVector.x;
315    y -= rkVector.y;
316    return *this;
317}
318
319
320
321inline Vector2& Vector2::operator*= (float fScalar) {
322    x *= fScalar;
323    y *= fScalar;
324    return *this;
325}
326
327
328
329
330inline Vector2& Vector2::operator*= (const Vector2& rkVector) {
331    x *= rkVector.x;
332    y *= rkVector.y;
333    return *this;
334}
335
336
337
338inline Vector2& Vector2::operator/= (const Vector2& rkVector) {
339    x /= rkVector.x;
340    y /= rkVector.y;
341    return *this;
342}
343
344
345inline Vector2 Vector2::operator* (const Vector2& rkVector) const {
346    return Vector2(x * rkVector.x, y * rkVector.y);
347}
348
349
350
351inline Vector2 Vector2::operator/ (const Vector2& rkVector) const {
352    return Vector2(x / rkVector.x, y / rkVector.y);
353}
354
355
356inline float Vector2::squaredLength () const {
357    return x*x + y*y;
358}
359
360
361inline float Vector2::length () const {
362    return sqrtf(x*x + y*y);
363}
364
365
366inline Vector2 Vector2::direction () const {
367    float lenSquared = x * x + y * y;
368
369    if (lenSquared != 1.0f) {
370        return *this / sqrtf(lenSquared);
371    } else {
372        return *this;
373    }
374}
375
376
377
378inline float Vector2::dot (const Vector2& rkVector) const {
379    return x*rkVector.x + y*rkVector.y;
380}
381
382
383
384inline Vector2 Vector2::min(const Vector2 &v) const {
385    return Vector2(G3D::min(v.x, x), G3D::min(v.y, y));
386}
387
388
389
390inline Vector2 Vector2::max(const Vector2 &v) const {
391    return Vector2(G3D::max(v.x, x), G3D::max(v.y, y));
392}
393
394
395
396inline bool Vector2::fuzzyEq(const Vector2& other) const {
397    return G3D::fuzzyEq((*this - other).squaredLength(), 0);
398}
399
400
401
402inline bool Vector2::fuzzyNe(const Vector2& other) const {
403    return G3D::fuzzyNe((*this - other).squaredLength(), 0);
404}
405
406
407
408inline bool Vector2::isFinite() const {
409    return G3D::isFinite(x) && G3D::isFinite(y);
410}
411
412
413
414inline bool Vector2::isZero() const {
415    return (x == 0.0f) && (y == 0.0f);
416}
417
418
419
420inline bool Vector2::isUnit() const {
421    return squaredLength() == 1.0f;
422}
423
424}
425
426// Intentionally outside namespace to avoid operator overloading confusion
427inline G3D::Vector2 operator*(double s, const G3D::Vector2& v) {
428    return v * (float)s;
429}
430inline G3D::Vector2 operator*(int s, const G3D::Vector2& v) {
431    return v * (float)s;
432}
433
434
435inline unsigned int hashCode(const G3D::Vector2& v);
436
437
438#endif
Note: See TracBrowser for help on using the browser.