source: flair-src/trunk/lib/FlairCore/src/Vector3D.cpp@ 48

Last change on this file since 48 was 15, checked in by Bayard Gildas, 8 years ago

sources reformatted with flair-format-dir script

File size: 5.5 KB
Line 
1// %flair:license{
2// This file is part of the Flair framework distributed under the
3// CECILL-C License, Version 1.0.
4// %flair:license}
5// created: 2013/05/02
6// filename: Vector3D.cpp
7//
8// author: Guillaume Sanahuja
9// Copyright Heudiasyc UMR UTC/CNRS 7253
10//
11// version: $Id: $
12//
13// purpose: Class defining a 3D vector
14//
15//
16/*********************************************************************/
17
18#include "Vector3D.h"
19#include "Vector2D.h"
20#include "RotationMatrix.h"
21#include "Euler.h"
22#include "Quaternion.h"
23#include "Object.h"
24#include <math.h>
25//#include "Vector3DSpinBox.h"
26
27namespace flair {
28namespace core {
29
30Vector3D::Vector3D(float inX, float inY, float inZ) : x(inX), y(inY), z(inZ) {}
31
32Vector3D::~Vector3D() {}
33/*
34void Vector3D::operator=(const gui::Vector3DSpinBox *vector) {
35 Vector3D vect=vector->Value();
36 x=vect.x;
37 y=vect.y;
38 z=vect.z;
39}*/
40
41Vector3D &Vector3D::operator=(const Vector3D &vector) {
42 x = vector.x;
43 y = vector.y;
44 z = vector.z;
45 return (*this);
46}
47
48Vector3D &Vector3D::operator+=(const Vector3D &vector) {
49 x += vector.x;
50 y += vector.y;
51 z += vector.z;
52 return (*this);
53}
54
55Vector3D &Vector3D::operator-=(const Vector3D &vector) {
56 x -= vector.x;
57 y -= vector.y;
58 z -= vector.z;
59 return (*this);
60}
61
62float &Vector3D::operator[](size_t idx) {
63 if (idx == 0) {
64 return x;
65 } else if (idx == 1) {
66 return y;
67 } else if (idx == 2) {
68 return z;
69 } else {
70 Printf("Vector3D: index %i out of bound\n", idx);
71 return z;
72 }
73}
74
75const float &Vector3D::operator[](size_t idx) const {
76 if (idx == 0) {
77 return x;
78 } else if (idx == 1) {
79 return y;
80 } else if (idx == 2) {
81 return z;
82 } else {
83 Printf("Vector3D: index %i out of bound\n", idx);
84 return z;
85 }
86}
87
88Vector3D CrossProduct(const Vector3D &vectorA, const Vector3D &vectorB) {
89 return Vector3D(vectorA.y * vectorB.z - vectorA.z * vectorB.y,
90 vectorA.z * vectorB.x - vectorA.x * vectorB.z,
91 vectorA.x * vectorB.y - vectorA.y * vectorB.x);
92}
93
94float DotProduct(const Vector3D &vectorA, const Vector3D &vectorB) {
95 return vectorA.x * vectorB.x + vectorA.y * vectorB.y + vectorA.z * vectorB.z;
96}
97
98Vector3D operator+(const Vector3D &vectorA, const Vector3D &vectorB) {
99 return Vector3D(vectorA.x + vectorB.x, vectorA.y + vectorB.y,
100 vectorA.z + vectorB.z);
101}
102
103Vector3D operator-(const Vector3D &vectorA, const Vector3D &vectorB) {
104 return Vector3D(vectorA.x - vectorB.x, vectorA.y - vectorB.y,
105 vectorA.z - vectorB.z);
106}
107
108Vector3D operator-(const Vector3D &vector) {
109 return Vector3D(-vector.x, -vector.y, -vector.z);
110}
111
112Vector3D operator*(const Vector3D &vectorA, const Vector3D &vectorB) {
113 return Vector3D(vectorA.x * vectorB.x, vectorA.y * vectorB.y,
114 vectorA.z * vectorB.z);
115}
116
117Vector3D operator*(const Vector3D &vector, float coeff) {
118 return Vector3D(vector.x * coeff, vector.y * coeff, vector.z * coeff);
119}
120
121Vector3D operator*(float coeff, const Vector3D &vector) {
122 return Vector3D(vector.x * coeff, vector.y * coeff, vector.z * coeff);
123}
124
125Vector3D operator/(const Vector3D &vector, float coeff) {
126 if (coeff != 0) {
127 return Vector3D(vector.x / coeff, vector.y / coeff, vector.z / coeff);
128 } else {
129 printf("Vector3D: err divinding by 0\n");
130 return Vector3D(0, 0, 0);
131 }
132}
133
134void Vector3D::RotateX(float value) {
135 float y_tmp;
136 y_tmp = y * cosf(value) - z * sinf(value);
137 z = y * sinf(value) + z * cosf(value);
138 y = y_tmp;
139}
140
141void Vector3D::RotateXDeg(float value) { RotateX(Euler::ToRadian(value)); }
142
143void Vector3D::RotateY(float value) {
144 float x_tmp;
145 x_tmp = x * cosf(value) + z * sinf(value);
146 z = -x * sinf(value) + z * cosf(value);
147 x = x_tmp;
148}
149
150void Vector3D::RotateYDeg(float value) { RotateY(Euler::ToRadian(value)); }
151
152void Vector3D::RotateZ(float value) {
153 float x_tmp;
154 x_tmp = x * cosf(value) - y * sinf(value);
155 y = x * sinf(value) + y * cosf(value);
156 x = x_tmp;
157}
158
159void Vector3D::RotateZDeg(float value) { RotateZ(Euler::ToRadian(value)); }
160
161void Vector3D::Rotate(const RotationMatrix &matrix) {
162 float a[3] = {0, 0, 0};
163 float b[3] = {x, y, z};
164
165 for (int i = 0; i < 3; i++) {
166 for (int j = 0; j < 3; j++) {
167 a[i] += matrix.m[i][j] * b[j];
168 }
169 }
170
171 x = a[0];
172 y = a[1];
173 z = a[2];
174}
175
176void Vector3D::Rotate(const Quaternion &quaternion) {
177 RotationMatrix matrix;
178 quaternion.ToRotationMatrix(matrix);
179 Rotate(matrix);
180}
181
182void Vector3D::To2Dxy(Vector2D &vector) const {
183 vector.x = x;
184 vector.y = y;
185}
186
187Vector2D Vector3D::To2Dxy(void) const {
188 Vector2D vect;
189 To2Dxy(vect);
190 return vect;
191}
192
193float Vector3D::GetNorm(void) const { return sqrt(x * x + y * y + z * z); }
194
195void Vector3D::Normalize(void) {
196 float n = GetNorm();
197 if (n != 0) {
198 x = x / n;
199 y = y / n;
200 z = z / n;
201 }
202}
203
204void Vector3D::Saturate(const Vector3D &min, const Vector3D &max) {
205 if (x < min.x)
206 x = min.x;
207 if (x > max.x)
208 x = max.x;
209
210 if (y < min.y)
211 y = min.y;
212 if (y > max.y)
213 y = max.y;
214
215 if (z < min.z)
216 z = min.z;
217 if (z > max.z)
218 z = max.z;
219}
220
221void Vector3D::Saturate(float min, float max) {
222 Saturate(Vector3D(min, min, min), Vector3D(max, max, max));
223}
224
225void Vector3D::Saturate(const Vector3D &value) {
226 float x = fabs(value.x);
227 float y = fabs(value.y);
228 float z = fabs(value.z);
229 Saturate(Vector3D(-x, -y, -z), Vector3D(x, y, z));
230}
231
232void Vector3D::Saturate(float value) {
233 float sat = fabs(value);
234 Saturate(Vector3D(-sat, -sat, -sat), Vector3D(sat, sat, sat));
235}
236
237} // end namespace core
238} // end namespace flair
Note: See TracBrowser for help on using the repository browser.