[2] | 1 | // %flair:license{
|
---|
[15] | 2 | // This file is part of the Flair framework distributed under the
|
---|
| 3 | // CECILL-C License, Version 1.0.
|
---|
[2] | 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 |
|
---|
[15] | 26 | namespace flair {
|
---|
| 27 | namespace core {
|
---|
[167] | 28 |
|
---|
| 29 | template class Vector3D<double>;
|
---|
| 30 | template Vector3D<double> operator+(const Vector3D<double>&, const Vector3D<double>&);
|
---|
| 31 | template Vector3D<double> operator-(const Vector3D<double>&, const Vector3D<double>&);
|
---|
| 32 | template Vector3D<double> operator-(const Vector3D<double>&);
|
---|
| 33 | template Vector3D<double> operator/(const Vector3D<double>&, float);
|
---|
| 34 | template Vector3D<double> operator*(const Vector3D<double>&, const Vector3D<double>&);
|
---|
| 35 | template Vector3D<double> operator*(const Vector3D<double>&, float);
|
---|
| 36 | template Vector3D<double> operator*(float, const Vector3D<double>&);
|
---|
| 37 | template Vector3D<double> CrossProduct(const Vector3D<double>&, const Vector3D<double>&);
|
---|
| 38 | template float DotProduct(const Vector3D<double>&, const Vector3D<double>&);
|
---|
[2] | 39 |
|
---|
[167] | 40 | template class Vector3D<float>;
|
---|
| 41 | template Vector3D<float> operator+(const Vector3D<float>&, const Vector3D<float>&);
|
---|
| 42 | template Vector3D<float> operator-(const Vector3D<float>&, const Vector3D<float>&);
|
---|
| 43 | template Vector3D<float> operator-(const Vector3D<float>&);
|
---|
| 44 | template Vector3D<float> operator/(const Vector3D<float>&, float);
|
---|
| 45 | template Vector3D<float> operator*(const Vector3D<float>&, const Vector3D<float>&);
|
---|
| 46 | template Vector3D<float> operator*(const Vector3D<float>&, float);
|
---|
| 47 | template Vector3D<float> operator*(float, const Vector3D<float>&);
|
---|
| 48 | template Vector3D<float> CrossProduct(const Vector3D<float>&, const Vector3D<float>&);
|
---|
| 49 | template float DotProduct(const Vector3D<float>&, const Vector3D<float>&);
|
---|
[2] | 50 |
|
---|
[167] | 51 | template <typename T> Vector3D<T>::Vector3D(T inX, T inY, T inZ) {
|
---|
| 52 | x=inX;
|
---|
| 53 | y=inY;
|
---|
| 54 | z=inZ;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | template <typename T> Vector3D<T>::~Vector3D() {}
|
---|
| 58 |
|
---|
[2] | 59 | /*
|
---|
| 60 | void Vector3D::operator=(const gui::Vector3DSpinBox *vector) {
|
---|
| 61 | Vector3D vect=vector->Value();
|
---|
| 62 | x=vect.x;
|
---|
| 63 | y=vect.y;
|
---|
| 64 | z=vect.z;
|
---|
| 65 | }*/
|
---|
[167] | 66 | /*
|
---|
| 67 | template<typename T,typename S> Vector3D<T> &Vector3D<T>::operator=(const Vector3D<S> &vector) {
|
---|
| 68 | //template <typename T> Vector3D<T> &Vector3D<T>::operator=(const Vector3D<T> &vector) {
|
---|
[15] | 69 | x = vector.x;
|
---|
| 70 | y = vector.y;
|
---|
| 71 | z = vector.z;
|
---|
| 72 | return (*this);
|
---|
[2] | 73 | }
|
---|
[167] | 74 | */
|
---|
| 75 | template <typename T> Vector3D<T> &Vector3D<T>::operator+=(const Vector3D<T> &vector) {
|
---|
[15] | 76 | x += vector.x;
|
---|
| 77 | y += vector.y;
|
---|
| 78 | z += vector.z;
|
---|
| 79 | return (*this);
|
---|
[2] | 80 | }
|
---|
| 81 |
|
---|
[167] | 82 | template <typename T> Vector3D<T> &Vector3D<T>::operator-=(const Vector3D<T> &vector) {
|
---|
[15] | 83 | x -= vector.x;
|
---|
| 84 | y -= vector.y;
|
---|
| 85 | z -= vector.z;
|
---|
| 86 | return (*this);
|
---|
[2] | 87 | }
|
---|
| 88 |
|
---|
[167] | 89 | template <typename T> T &Vector3D<T>::operator[](size_t idx) {
|
---|
[15] | 90 | if (idx == 0) {
|
---|
| 91 | return x;
|
---|
| 92 | } else if (idx == 1) {
|
---|
| 93 | return y;
|
---|
| 94 | } else if (idx == 2) {
|
---|
| 95 | return z;
|
---|
| 96 | } else {
|
---|
| 97 | Printf("Vector3D: index %i out of bound\n", idx);
|
---|
| 98 | return z;
|
---|
| 99 | }
|
---|
[2] | 100 | }
|
---|
| 101 |
|
---|
[167] | 102 | template <typename T> const T &Vector3D<T>::operator[](size_t idx) const {
|
---|
[15] | 103 | if (idx == 0) {
|
---|
| 104 | return x;
|
---|
| 105 | } else if (idx == 1) {
|
---|
| 106 | return y;
|
---|
| 107 | } else if (idx == 2) {
|
---|
| 108 | return z;
|
---|
| 109 | } else {
|
---|
| 110 | Printf("Vector3D: index %i out of bound\n", idx);
|
---|
| 111 | return z;
|
---|
| 112 | }
|
---|
[2] | 113 | }
|
---|
| 114 |
|
---|
[167] | 115 | template <typename T> Vector3D<T> CrossProduct(const Vector3D<T> &vectorA, const Vector3D<T> &vectorB) {
|
---|
| 116 | return Vector3D<T>(vectorA.y * vectorB.z - vectorA.z * vectorB.y,
|
---|
[15] | 117 | vectorA.z * vectorB.x - vectorA.x * vectorB.z,
|
---|
| 118 | vectorA.x * vectorB.y - vectorA.y * vectorB.x);
|
---|
[2] | 119 | }
|
---|
| 120 |
|
---|
[167] | 121 | template <typename T> float DotProduct(const Vector3D<T> &vectorA, const Vector3D<T> &vectorB) {
|
---|
[15] | 122 | return vectorA.x * vectorB.x + vectorA.y * vectorB.y + vectorA.z * vectorB.z;
|
---|
[2] | 123 | }
|
---|
| 124 |
|
---|
[167] | 125 | template <typename T> Vector3D<T> operator+(const Vector3D<T> &vectorA, const Vector3D<T> &vectorB) {
|
---|
| 126 | return Vector3D<T>(vectorA.x + vectorB.x, vectorA.y + vectorB.y,
|
---|
[15] | 127 | vectorA.z + vectorB.z);
|
---|
[2] | 128 | }
|
---|
| 129 |
|
---|
[167] | 130 | template <typename T> Vector3D<T> operator-(const Vector3D<T> &vectorA, const Vector3D<T> &vectorB) {
|
---|
| 131 | return Vector3D<T>(vectorA.x - vectorB.x, vectorA.y - vectorB.y,
|
---|
[15] | 132 | vectorA.z - vectorB.z);
|
---|
[2] | 133 | }
|
---|
| 134 |
|
---|
[167] | 135 | template <typename T> Vector3D<T> operator-(const Vector3D<T> &vector) {
|
---|
| 136 | return Vector3D<T>(-vector.x, -vector.y, -vector.z);
|
---|
[2] | 137 | }
|
---|
| 138 |
|
---|
[167] | 139 | template <typename T> Vector3D<T> operator*(const Vector3D<T> &vectorA, const Vector3D<T> &vectorB) {
|
---|
| 140 | return Vector3D<T>(vectorA.x * vectorB.x, vectorA.y * vectorB.y,
|
---|
[15] | 141 | vectorA.z * vectorB.z);
|
---|
[2] | 142 | }
|
---|
| 143 |
|
---|
[167] | 144 | template <typename T> Vector3D<T> operator*(const Vector3D<T> &vector, float coeff) {
|
---|
| 145 | return Vector3D<T>(vector.x * coeff, vector.y * coeff, vector.z * coeff);
|
---|
[2] | 146 | }
|
---|
| 147 |
|
---|
[167] | 148 | template <typename T> Vector3D<T> operator*(float coeff, const Vector3D<T> &vector) {
|
---|
| 149 | return Vector3D<T>(vector.x * coeff, vector.y * coeff, vector.z * coeff);
|
---|
[2] | 150 | }
|
---|
| 151 |
|
---|
[167] | 152 | template <typename T> Vector3D<T> operator/(const Vector3D<T> &vector, float coeff) {
|
---|
[15] | 153 | if (coeff != 0) {
|
---|
[167] | 154 | return Vector3D<T>(vector.x / coeff, vector.y / coeff, vector.z / coeff);
|
---|
[15] | 155 | } else {
|
---|
| 156 | printf("Vector3D: err divinding by 0\n");
|
---|
[167] | 157 | return Vector3D<T>(0, 0, 0);
|
---|
[15] | 158 | }
|
---|
[2] | 159 | }
|
---|
| 160 |
|
---|
[167] | 161 | template <typename T> void Vector3D<T>::RotateX(float value) {
|
---|
[15] | 162 | float y_tmp;
|
---|
| 163 | y_tmp = y * cosf(value) - z * sinf(value);
|
---|
| 164 | z = y * sinf(value) + z * cosf(value);
|
---|
| 165 | y = y_tmp;
|
---|
[2] | 166 | }
|
---|
| 167 |
|
---|
[167] | 168 | template <typename T> void Vector3D<T>::RotateXDeg(float value) { RotateX(Euler::ToRadian(value)); }
|
---|
[2] | 169 |
|
---|
[167] | 170 | template <typename T> void Vector3D<T>::RotateY(float value) {
|
---|
[15] | 171 | float x_tmp;
|
---|
| 172 | x_tmp = x * cosf(value) + z * sinf(value);
|
---|
| 173 | z = -x * sinf(value) + z * cosf(value);
|
---|
| 174 | x = x_tmp;
|
---|
[2] | 175 | }
|
---|
| 176 |
|
---|
[167] | 177 | template <typename T> void Vector3D<T>::RotateYDeg(float value) { RotateY(Euler::ToRadian(value)); }
|
---|
[2] | 178 |
|
---|
[167] | 179 | template <typename T> void Vector3D<T>::RotateZ(float value) {
|
---|
[15] | 180 | float x_tmp;
|
---|
| 181 | x_tmp = x * cosf(value) - y * sinf(value);
|
---|
| 182 | y = x * sinf(value) + y * cosf(value);
|
---|
| 183 | x = x_tmp;
|
---|
[2] | 184 | }
|
---|
| 185 |
|
---|
[167] | 186 | template <typename T> void Vector3D<T>::RotateZDeg(float value) { RotateZ(Euler::ToRadian(value)); }
|
---|
[2] | 187 |
|
---|
[167] | 188 | template <typename T> void Vector3D<T>::Rotate(const RotationMatrix &matrix) {
|
---|
| 189 | T a[3] = {0, 0, 0};
|
---|
| 190 | T b[3] = {x, y, z};
|
---|
[2] | 191 |
|
---|
[15] | 192 | for (int i = 0; i < 3; i++) {
|
---|
| 193 | for (int j = 0; j < 3; j++) {
|
---|
| 194 | a[i] += matrix.m[i][j] * b[j];
|
---|
[2] | 195 | }
|
---|
[15] | 196 | }
|
---|
[2] | 197 |
|
---|
[15] | 198 | x = a[0];
|
---|
| 199 | y = a[1];
|
---|
| 200 | z = a[2];
|
---|
[2] | 201 | }
|
---|
| 202 |
|
---|
[167] | 203 | template <typename T> void Vector3D<T>::Rotate(const Quaternion &quaternion) {
|
---|
[15] | 204 | RotationMatrix matrix;
|
---|
| 205 | quaternion.ToRotationMatrix(matrix);
|
---|
| 206 | Rotate(matrix);
|
---|
[2] | 207 | }
|
---|
| 208 |
|
---|
[167] | 209 | template <typename T> void Vector3D<T>::To2Dxy(Vector2D<T> &vector) const {
|
---|
[15] | 210 | vector.x = x;
|
---|
| 211 | vector.y = y;
|
---|
[2] | 212 | }
|
---|
| 213 |
|
---|
[167] | 214 | template <typename T> Vector2D<T> Vector3D<T>::To2Dxy(void) const {
|
---|
| 215 | Vector2D<T> vect;
|
---|
[15] | 216 | To2Dxy(vect);
|
---|
| 217 | return vect;
|
---|
[2] | 218 | }
|
---|
| 219 |
|
---|
[167] | 220 | template <typename T> float Vector3D<T>::GetNorm(void) const { return sqrt(x * x + y * y + z * z); }
|
---|
[2] | 221 |
|
---|
[167] | 222 | template <typename T> void Vector3D<T>::Normalize(void) {
|
---|
[15] | 223 | float n = GetNorm();
|
---|
| 224 | if (n != 0) {
|
---|
| 225 | x = x / n;
|
---|
| 226 | y = y / n;
|
---|
| 227 | z = z / n;
|
---|
| 228 | }
|
---|
[2] | 229 | }
|
---|
| 230 |
|
---|
[167] | 231 | template <typename T> void Vector3D<T>::Saturate(const Vector3D<T> &min, const Vector3D<T> &max) {
|
---|
[15] | 232 | if (x < min.x)
|
---|
| 233 | x = min.x;
|
---|
| 234 | if (x > max.x)
|
---|
| 235 | x = max.x;
|
---|
[2] | 236 |
|
---|
[15] | 237 | if (y < min.y)
|
---|
| 238 | y = min.y;
|
---|
| 239 | if (y > max.y)
|
---|
| 240 | y = max.y;
|
---|
[2] | 241 |
|
---|
[15] | 242 | if (z < min.z)
|
---|
| 243 | z = min.z;
|
---|
| 244 | if (z > max.z)
|
---|
| 245 | z = max.z;
|
---|
[2] | 246 | }
|
---|
| 247 |
|
---|
[167] | 248 | template <typename T> void Vector3D<T>::Saturate(float min, float max) {
|
---|
| 249 | Saturate(Vector3D<T>(min, min, min), Vector3D<T>(max, max, max));
|
---|
[2] | 250 | }
|
---|
| 251 |
|
---|
[167] | 252 | template <typename T> void Vector3D<T>::Saturate(const Vector3D<T> &value) {
|
---|
[15] | 253 | float x = fabs(value.x);
|
---|
| 254 | float y = fabs(value.y);
|
---|
| 255 | float z = fabs(value.z);
|
---|
[167] | 256 | Saturate(Vector3D<T>(-x, -y, -z), Vector3D<T>(x, y, z));
|
---|
[2] | 257 | }
|
---|
| 258 |
|
---|
[167] | 259 | template <typename T> void Vector3D<T>::Saturate(float value) {
|
---|
[15] | 260 | float sat = fabs(value);
|
---|
[167] | 261 | Saturate(Vector3D<T>(-sat, -sat, -sat), Vector3D<T>(sat, sat, sat));
|
---|
[2] | 262 | }
|
---|
| 263 |
|
---|
| 264 | } // end namespace core
|
---|
| 265 | } // end namespace flair
|
---|