]> Creatis software - FrontAlgorithms.git/blob - appli/TempAirwaysAppli/MathLib/vec3.cxx
On my way... it does not work yet, but I think I'm on the good track.
[FrontAlgorithms.git] / appli / TempAirwaysAppli / MathLib / vec3.cxx
1 #include "vec3.h"
2
3
4 Vec3::Vec3() : x(0.0), y(0.0), z(0.0)
5 {
6 }
7
8 Vec3::Vec3(const float& x, const float& y, const float& z)
9 {
10         this->x = x;
11         this->y = y;
12         this->z = z;
13 }
14
15 const float* Vec3::GetVec3() const
16 {
17         float* ret = new float[3];
18         ret[0] = this->x;
19         ret[1] = this->y;
20         ret[2] = this->z;
21         return ret;
22 }
23
24 float* Vec3::GetVec3()
25 {
26         float* ret = new float[3];
27         ret[0] = this->x;
28         ret[1] = this->y;
29         ret[2] = this->z;
30         return ret;
31 }
32
33 Vec3& Vec3::operator=(const Vec3& vec)
34 {
35         this->x = vec.x;
36         this->y = vec.y;
37         this->z = vec.z;
38         return *this;
39 }
40
41 float& Vec3::operator[](unsigned int i)
42 {
43         return (i == 0 ? x : i == 1 ? y : z);
44 }
45
46 const float& Vec3::operator[](unsigned int i) const
47 {
48         return (i == 0 ? this->x : i == 1 ? this->y : this->z);
49 }
50
51 Vec3 Vec3::operator+() const
52 {
53         return *this;
54 }
55
56 Vec3 Vec3::operator-() const
57 {
58         return Vec3(-this->x, -this->y, -this->z);
59 }
60
61 Vec3& Vec3::operator+=(const Vec3& b)
62                                                                                                 {
63         this->x += b.x;
64         this->y += b.y;
65         this->z += b.z;
66         return *this;
67                                                                                                 }
68
69 Vec3& Vec3::operator-=(const Vec3& b)
70                                                                                                 {
71         this->x -= b.x;
72         this->y -= b.y;
73         this->z -= b.z;
74         return *this;
75                                                                                                 }
76
77 Vec3& Vec3::operator*=(const float& k)
78                                                                                                 {
79         this->x *= k;
80         this->y *= k;
81         this->z *= k;
82         return *this;
83                                                                                                 }
84
85 Vec3& Vec3::operator/=(const float& k)
86                                                                                                 {
87         this->x /= k;
88         this->y /= k;
89         this->z /= k;
90         return *this;
91                                                                                                 }
92
93 Vec3 Vec3::operator+(const Vec3& b)
94 {
95         return Vec3(this->x + b.x, this->y + b.y, this->z + b.z);
96 }
97
98 Vec3 Vec3::operator-(const Vec3& b)
99 {
100         return Vec3(this->x - b.x, this->y - b.y, this->z - b.z);
101 }
102
103 Vec3 Vec3::operator*(const float& k)
104 {
105         return Vec3(this->x * k, this->y * k, this->z * k);
106 }
107
108 Vec3 Vec3::operator/(const float& k)
109 {
110         return Vec3(this->x / k, this->y / k, this->z / k);
111 }
112
113 bool Vec3::operator==(const Vec3& b) const
114 {
115         //double epsilon = std::numeric_limits<double>::epsilon();
116         //return (fabs(this->x - b.x) <= epsilon) && (fabs(this->y - b.y) <= epsilon)
117                         //&& (fabs(this->z - b.z) <= epsilon);
118         return this->x == b.x && this->y == b.y && this->z == b.z;
119 }
120
121 bool Vec3::operator!=(const Vec3& b) const
122                                                                                                 {
123         return (!(*this == b));
124                                                                                                 }
125
126 bool Vec3::operator<(const Vec3& b) const
127 {
128         if(this->x < b.x)
129                 return true;
130         else if(this->x > b.x)
131                 return false;
132         else
133         {
134                 if(this->y < b.y)
135                         return true;
136                 else if(this->y > b.y)
137                         return false;
138                 else
139                 {
140                         if(this->z < b.z)
141                                 return true;
142                         return false;
143                 }
144         }
145 }
146
147 bool Vec3::operator>(const Vec3& b)
148 {
149         if(this->x > b.x)
150                 return true;
151         else if(this->x < b.x)
152                 return false;
153         else
154         {
155                 if(this->y > b.y)
156                         return true;
157                 else if(this->y < b.y)
158                         return false;
159                 else
160                 {
161                         if(this->z > b.z)
162                                 return true;
163                         return false;
164                 }
165         }
166 }
167
168 float Vec3::Norm() const
169 {
170         return sqrt(this->x * this->x + this->y * this->y + this->z * this->z);
171 }
172
173 void Vec3::Normalize()
174 {
175         float norm = sqrt(this->x * this->x + this->y * this->y + this->z * this->z);
176         this->x = this->x / norm;
177         this->y = this->y / norm;
178         this->z = this->z / norm;
179 }
180
181 void Vec3::set(const float& x, const float& y, const float& z)
182 {
183         this->x = x;
184         this->y = y;
185         this->z = z;
186 }
187
188 float Vec3::Dot(const Vec3& b) const
189 {
190         return (this->x * b.x + this->y * b.y + this->z * b.z);
191 }
192
193 Vec3 Vec3::Orthogonal(const Vec3& b) const
194 {
195         Vec3 u = Vec3(fabs(b.x), fabs(b.y), fabs(b.z));
196         int i = 0;
197         int j = 1;
198         if ((u.x > u.y) && (u.z > u.y))
199                 j = 2;
200         else
201         {
202                 i = 1;
203                 j = 2;
204                 if (u.x > u.z)
205                         j = 0;
206         }
207         u = Vec3();
208         u[i] = b[j];
209         u[j] = -b[i];
210         return u;
211 }
212
213 Vec3 Vec3::Cross(const Vec3& b) const
214 {
215         return Vec3(this->y * b.z - this->z * b.y, this->z * b.x - this->x * b.z,
216                         this->x * b.y - this->y * b.x);
217 }
218
219
220 float Vec3::EculideanDistance(const Vec3& b) const
221 {
222         return sqrt(
223                         pow(this->x - b.x, 2.0) + pow(this->y - b.y, 2.0)
224                         + pow(this->z - b.z, 2.0));
225 }
226
227 std::ostream&
228 operator<<(std::ostream& os, const Vec3& coord)
229 {
230         os << coord[0] << "  " << coord[1] << "  " << coord[2];
231         return os;
232 }
233