1 #ifndef VEC3F_HXX
2 #define VEC3F_HXX
3
4 #include "defines.h"
5
6 #ifndef MIN
7 #define MIN(a,b) ((a)<(b)?(a):(b))
8 #define MAX(a,b) ((a)>(b)?(a):(b))
9 #endif
10
11 #define max(a,b) MAX(a,b)
12 #define min(a,b) MIN(a,b)
13
14 using namespace std;
15
16 class Vec3f
17 {
18 public:
19 float x,y,z;
20
21 Vec3f()
22 {x = y = z = 0.0f;};
23 Vec3f(float x,float y, float z)
24 : x(x),y(y),z(z)
25 {};
26 Vec3f(float f)
27 : x(f),y(f),z(f)
28 {};
29
30 inline const float &operator[](const int i) const
31 { return *(&x+i); };
32
33 inline float &operator[](const int i)
34 { return *(&x+i); }; //((float *)(this))[i]; };
35
36 inline Vec3f &operator=(const Vec3f &b)
37 { x = b.x; y = b.y; z = b.z; return *this;};
38
39 inline int MaxDim() const
40 {
41 return (x > y)?((x > z)?0:2):((y > z)?1:2);
42 }
43 inline void SetMin(Vec3f &other)
44 {
45 x = min(x,other.x);
46 y = min(y,other.y);
47 z = min(z,other.z);
48 };
49 inline void SetMax(Vec3f &other)
50 {
51 x = max(x,other.x);
52 y = max(y,other.y);
53 z = max(z,other.z);
54 };
55
56 Vec3f NormTo(float maximum)
57 {
58 maximum /= max(this->x, max(this->y, this->z));
59 return Vec3f(this->x * maximum, this->y * maximum, this->z * maximum);
60 }
61 };
62
63 /*! dot product */
64 inline float Dot(const Vec3f &a, const Vec3f &b)
65 { return a.x*b.x+a.y*b.y+a.z*b.z; };
66
67 /*! component-wise product */
68 inline Vec3f Product(const Vec3f &a, const Vec3f &b)
69 { return Vec3f(a.x*b.x,a.y*b.y,a.z*b.z); };
70
71 /*! vector product */
72 inline Vec3f Cross(const Vec3f &a, const Vec3f &b)
73 { return Vec3f(a.y*b.z-a.z*b.y,
74 a.z*b.x-a.x*b.z,
75 a.x*b.y-a.y*b.x); };
76
77
78 inline Vec3f operator-(const Vec3f &v)
79 { return Vec3f(-v.x,-v.y,-v.z); };
80
81 inline float Length(const Vec3f &v)
82 { return sqrt(Dot(v,v)); };
83
84 /*! still undocumented */
85 inline Vec3f operator*(const float f, const Vec3f &v)
86 { return Vec3f(f*v.x, f*v.y, f*v.z); };
87
88 /*! still undocumented */
89 inline Vec3f operator*(const Vec3f &v, const float f)
90 { return Vec3f(f*v.x, f*v.y, f*v.z); };
91
92 /*! still undocumented */
93 inline void operator*=(Vec3f &v, const float f)
94 { v.x *= f; v.y*=f; v.z*=f; };
95
96 /*! still undocumented */
97 inline void operator*=(Vec3f &v, const Vec3f &f)
98 { v.x *= f.x; v.y*=f.y; v.z*=f.z; };
99
100
101 /*! still undocumented */
102 inline Vec3f operator/(const Vec3f &v, const float f)
103 { return (1/f)*v; };
104
105 /*! still undocumented */
106 inline void operator/=(Vec3f &v, const float f)
107 { v *= (1/f); };
108
109 /*! still undocumented */
110 inline Vec3f operator+(const Vec3f &a, const Vec3f &b)
111 { return Vec3f(a.x+b.x, a.y+b.y, a.z+b.z); };
112
113 inline Vec3f &operator+=(Vec3f &a, const Vec3f &b)
114 {
115 a.x += b.x;
116 a.y += b.y;
117 a.z += b.z;
118 return a;
119 }
120
121 inline Vec3f &operator-=(Vec3f &a, const Vec3f &b)
122 {
123 a.x -= b.x;
124 a.y -= b.y;
125 a.z -= b.z;
126 return a;
127 }
128
129 inline Vec3f operator^(const Vec3f &a, const Vec3f &b)
130 { return Vec3f(a.y*b.z-a.z*b.y,
131 a.z*b.x-a.x*b.z,
132 a.x*b.y-a.y*b.x); };
133
134
135 inline Vec3f operator-(const Vec3f &a, const Vec3f &b)
136 { return Vec3f(a.x-b.x, a.y-b.y, a.z-b.z); };
137
138 inline bool operator==(const Vec3f &a, const Vec3f &b)
139 { return ((a.x==b.x) && (a.y==b.y) && (a.z==b.z)); };
140
141 inline bool operator!=(const Vec3f &a, const Vec3f &b)
142 { return !(a==b); };
143
144 inline void Normalize(Vec3f &v)
145 { v *= (1.f/Length(v)); };
146
147 inline bool Nonzero(const Vec3f &v)
148 { return (v.x || v.y || v.z); };
149
150 inline ostream &operator<<(ostream &o,const Vec3f &v)
151 { o << "(" << v.x << "," << v.y << "," << v.z << ")"; return o; }
152
153 inline Vec3f Min(const Vec3f a, const Vec3f b)
154 { return Vec3f(MIN(a.x,b.x),MIN(a.y,b.y),MIN(a.z,b.z)); }
155
156 inline Vec3f Max(const Vec3f a, const Vec3f b)
157 { return Vec3f(MAX(a.x,b.x),MAX(a.y,b.y),MAX(a.z,b.z)); }
158
159 #endif
syntax highlighted by Code2HTML, v. 0.9.1