[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 | //#include "Vector3DSpinBox.h"
|
---|
| 26 |
|
---|
[15] | 27 | namespace flair {
|
---|
| 28 | namespace core {
|
---|
[2] | 29 |
|
---|
[15] | 30 | Vector3D::Vector3D(float inX, float inY, float inZ) : x(inX), y(inY), z(inZ) {}
|
---|
[2] | 31 |
|
---|
[15] | 32 | Vector3D::~Vector3D() {}
|
---|
[2] | 33 | /*
|
---|
| 34 | void 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 |
|
---|
| 41 | Vector3D &Vector3D::operator=(const Vector3D &vector) {
|
---|
[15] | 42 | x = vector.x;
|
---|
| 43 | y = vector.y;
|
---|
| 44 | z = vector.z;
|
---|
| 45 | return (*this);
|
---|
[2] | 46 | }
|
---|
| 47 |
|
---|
| 48 | Vector3D &Vector3D::operator+=(const Vector3D &vector) {
|
---|
[15] | 49 | x += vector.x;
|
---|
| 50 | y += vector.y;
|
---|
| 51 | z += vector.z;
|
---|
| 52 | return (*this);
|
---|
[2] | 53 | }
|
---|
| 54 |
|
---|
| 55 | Vector3D &Vector3D::operator-=(const Vector3D &vector) {
|
---|
[15] | 56 | x -= vector.x;
|
---|
| 57 | y -= vector.y;
|
---|
| 58 | z -= vector.z;
|
---|
| 59 | return (*this);
|
---|
[2] | 60 | }
|
---|
| 61 |
|
---|
| 62 | float &Vector3D::operator[](size_t idx) {
|
---|
[15] | 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 | }
|
---|
[2] | 73 | }
|
---|
| 74 |
|
---|
| 75 | const float &Vector3D::operator[](size_t idx) const {
|
---|
[15] | 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 | }
|
---|
[2] | 86 | }
|
---|
| 87 |
|
---|
[15] | 88 | Vector3D 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);
|
---|
[2] | 92 | }
|
---|
| 93 |
|
---|
[15] | 94 | float DotProduct(const Vector3D &vectorA, const Vector3D &vectorB) {
|
---|
| 95 | return vectorA.x * vectorB.x + vectorA.y * vectorB.y + vectorA.z * vectorB.z;
|
---|
[2] | 96 | }
|
---|
| 97 |
|
---|
[15] | 98 | Vector3D operator+(const Vector3D &vectorA, const Vector3D &vectorB) {
|
---|
| 99 | return Vector3D(vectorA.x + vectorB.x, vectorA.y + vectorB.y,
|
---|
| 100 | vectorA.z + vectorB.z);
|
---|
[2] | 101 | }
|
---|
| 102 |
|
---|
[15] | 103 | Vector3D operator-(const Vector3D &vectorA, const Vector3D &vectorB) {
|
---|
| 104 | return Vector3D(vectorA.x - vectorB.x, vectorA.y - vectorB.y,
|
---|
| 105 | vectorA.z - vectorB.z);
|
---|
[2] | 106 | }
|
---|
| 107 |
|
---|
| 108 | Vector3D operator-(const Vector3D &vector) {
|
---|
[15] | 109 | return Vector3D(-vector.x, -vector.y, -vector.z);
|
---|
[2] | 110 | }
|
---|
| 111 |
|
---|
[15] | 112 | Vector3D operator*(const Vector3D &vectorA, const Vector3D &vectorB) {
|
---|
| 113 | return Vector3D(vectorA.x * vectorB.x, vectorA.y * vectorB.y,
|
---|
| 114 | vectorA.z * vectorB.z);
|
---|
[2] | 115 | }
|
---|
| 116 |
|
---|
[15] | 117 | Vector3D operator*(const Vector3D &vector, float coeff) {
|
---|
| 118 | return Vector3D(vector.x * coeff, vector.y * coeff, vector.z * coeff);
|
---|
[2] | 119 | }
|
---|
| 120 |
|
---|
[15] | 121 | Vector3D operator*(float coeff, const Vector3D &vector) {
|
---|
| 122 | return Vector3D(vector.x * coeff, vector.y * coeff, vector.z * coeff);
|
---|
[2] | 123 | }
|
---|
| 124 |
|
---|
[15] | 125 | Vector3D 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 | }
|
---|
[2] | 132 | }
|
---|
| 133 |
|
---|
| 134 | void Vector3D::RotateX(float value) {
|
---|
[15] | 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;
|
---|
[2] | 139 | }
|
---|
| 140 |
|
---|
[15] | 141 | void Vector3D::RotateXDeg(float value) { RotateX(Euler::ToRadian(value)); }
|
---|
[2] | 142 |
|
---|
| 143 | void Vector3D::RotateY(float value) {
|
---|
[15] | 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;
|
---|
[2] | 148 | }
|
---|
| 149 |
|
---|
[15] | 150 | void Vector3D::RotateYDeg(float value) { RotateY(Euler::ToRadian(value)); }
|
---|
[2] | 151 |
|
---|
| 152 | void Vector3D::RotateZ(float value) {
|
---|
[15] | 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;
|
---|
[2] | 157 | }
|
---|
| 158 |
|
---|
[15] | 159 | void Vector3D::RotateZDeg(float value) { RotateZ(Euler::ToRadian(value)); }
|
---|
[2] | 160 |
|
---|
| 161 | void Vector3D::Rotate(const RotationMatrix &matrix) {
|
---|
[15] | 162 | float a[3] = {0, 0, 0};
|
---|
| 163 | float b[3] = {x, y, z};
|
---|
[2] | 164 |
|
---|
[15] | 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];
|
---|
[2] | 168 | }
|
---|
[15] | 169 | }
|
---|
[2] | 170 |
|
---|
[15] | 171 | x = a[0];
|
---|
| 172 | y = a[1];
|
---|
| 173 | z = a[2];
|
---|
[2] | 174 | }
|
---|
| 175 |
|
---|
| 176 | void Vector3D::Rotate(const Quaternion &quaternion) {
|
---|
[15] | 177 | RotationMatrix matrix;
|
---|
| 178 | quaternion.ToRotationMatrix(matrix);
|
---|
| 179 | Rotate(matrix);
|
---|
[2] | 180 | }
|
---|
| 181 |
|
---|
| 182 | void Vector3D::To2Dxy(Vector2D &vector) const {
|
---|
[15] | 183 | vector.x = x;
|
---|
| 184 | vector.y = y;
|
---|
[2] | 185 | }
|
---|
| 186 |
|
---|
| 187 | Vector2D Vector3D::To2Dxy(void) const {
|
---|
[15] | 188 | Vector2D vect;
|
---|
| 189 | To2Dxy(vect);
|
---|
| 190 | return vect;
|
---|
[2] | 191 | }
|
---|
| 192 |
|
---|
[15] | 193 | float Vector3D::GetNorm(void) const { return sqrt(x * x + y * y + z * z); }
|
---|
[2] | 194 |
|
---|
| 195 | void Vector3D::Normalize(void) {
|
---|
[15] | 196 | float n = GetNorm();
|
---|
| 197 | if (n != 0) {
|
---|
| 198 | x = x / n;
|
---|
| 199 | y = y / n;
|
---|
| 200 | z = z / n;
|
---|
| 201 | }
|
---|
[2] | 202 | }
|
---|
| 203 |
|
---|
[15] | 204 | void 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;
|
---|
[2] | 209 |
|
---|
[15] | 210 | if (y < min.y)
|
---|
| 211 | y = min.y;
|
---|
| 212 | if (y > max.y)
|
---|
| 213 | y = max.y;
|
---|
[2] | 214 |
|
---|
[15] | 215 | if (z < min.z)
|
---|
| 216 | z = min.z;
|
---|
| 217 | if (z > max.z)
|
---|
| 218 | z = max.z;
|
---|
[2] | 219 | }
|
---|
| 220 |
|
---|
[15] | 221 | void Vector3D::Saturate(float min, float max) {
|
---|
| 222 | Saturate(Vector3D(min, min, min), Vector3D(max, max, max));
|
---|
[2] | 223 | }
|
---|
| 224 |
|
---|
| 225 | void Vector3D::Saturate(const Vector3D &value) {
|
---|
[15] | 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));
|
---|
[2] | 230 | }
|
---|
| 231 |
|
---|
| 232 | void Vector3D::Saturate(float value) {
|
---|
[15] | 233 | float sat = fabs(value);
|
---|
| 234 | Saturate(Vector3D(-sat, -sat, -sat), Vector3D(sat, sat, sat));
|
---|
[2] | 235 | }
|
---|
| 236 |
|
---|
| 237 | } // end namespace core
|
---|
| 238 | } // end namespace flair
|
---|