Changeset 167 in flair-src for trunk/lib/FlairCore/src/Vector3D.cpp


Ignore:
Timestamp:
Apr 12, 2017, 1:59:38 PM (8 years ago)
Author:
Sanahuja Guillaume
Message:

modifs pour template vectors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lib/FlairCore/src/Vector3D.cpp

    r15 r167  
    2323#include "Object.h"
    2424#include <math.h>
    25 //#include "Vector3DSpinBox.h"
    2625
    2726namespace flair {
    2827namespace core {
    29 
    30 Vector3D::Vector3D(float inX, float inY, float inZ) : x(inX), y(inY), z(inZ) {}
    31 
    32 Vector3D::~Vector3D() {}
     28 
     29template class Vector3D<double>;
     30template Vector3D<double> operator+(const Vector3D<double>&, const Vector3D<double>&);
     31template Vector3D<double> operator-(const Vector3D<double>&, const Vector3D<double>&);
     32template Vector3D<double> operator-(const Vector3D<double>&);
     33template Vector3D<double> operator/(const Vector3D<double>&, float);
     34template Vector3D<double> operator*(const Vector3D<double>&, const Vector3D<double>&);
     35template Vector3D<double> operator*(const Vector3D<double>&, float);
     36template Vector3D<double> operator*(float, const Vector3D<double>&);
     37template Vector3D<double> CrossProduct(const Vector3D<double>&, const Vector3D<double>&);
     38template float DotProduct(const Vector3D<double>&, const Vector3D<double>&);
     39
     40template class Vector3D<float>;
     41template Vector3D<float> operator+(const Vector3D<float>&, const Vector3D<float>&);
     42template Vector3D<float> operator-(const Vector3D<float>&, const Vector3D<float>&);
     43template Vector3D<float> operator-(const Vector3D<float>&);
     44template Vector3D<float> operator/(const Vector3D<float>&, float);
     45template Vector3D<float> operator*(const Vector3D<float>&, const Vector3D<float>&);
     46template Vector3D<float> operator*(const Vector3D<float>&, float);
     47template Vector3D<float> operator*(float, const Vector3D<float>&);
     48template Vector3D<float> CrossProduct(const Vector3D<float>&, const Vector3D<float>&);
     49template float DotProduct(const Vector3D<float>&, const Vector3D<float>&);
     50
     51template <typename T> Vector3D<T>::Vector3D(T inX, T inY, T inZ) {
     52  x=inX;
     53  y=inY;
     54  z=inZ;
     55}
     56
     57template <typename T> Vector3D<T>::~Vector3D() {}
     58
    3359/*
    3460void Vector3D::operator=(const gui::Vector3DSpinBox *vector) {
     
    3864    z=vect.z;
    3965}*/
    40 
    41 Vector3D &Vector3D::operator=(const Vector3D &vector) {
     66/*
     67template<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) {
    4269  x = vector.x;
    4370  y = vector.y;
     
    4572  return (*this);
    4673}
    47 
    48 Vector3D &Vector3D::operator+=(const Vector3D &vector) {
     74*/
     75template <typename T> Vector3D<T> &Vector3D<T>::operator+=(const Vector3D<T> &vector) {
    4976  x += vector.x;
    5077  y += vector.y;
     
    5380}
    5481
    55 Vector3D &Vector3D::operator-=(const Vector3D &vector) {
     82template <typename T> Vector3D<T> &Vector3D<T>::operator-=(const Vector3D<T> &vector) {
    5683  x -= vector.x;
    5784  y -= vector.y;
     
    6087}
    6188
    62 float &Vector3D::operator[](size_t idx) {
     89template <typename T> T &Vector3D<T>::operator[](size_t idx) {
    6390  if (idx == 0) {
    6491    return x;
     
    73100}
    74101
    75 const float &Vector3D::operator[](size_t idx) const {
     102template <typename T> const T &Vector3D<T>::operator[](size_t idx) const {
    76103  if (idx == 0) {
    77104    return x;
     
    86113}
    87114
    88 Vector3D CrossProduct(const Vector3D &vectorA, const Vector3D &vectorB) {
    89   return Vector3D(vectorA.y * vectorB.z - vectorA.z * vectorB.y,
     115template <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,
    90117                  vectorA.z * vectorB.x - vectorA.x * vectorB.z,
    91118                  vectorA.x * vectorB.y - vectorA.y * vectorB.x);
    92119}
    93120
    94 float DotProduct(const Vector3D &vectorA, const Vector3D &vectorB) {
     121template <typename T> float DotProduct(const Vector3D<T> &vectorA, const Vector3D<T> &vectorB) {
    95122  return vectorA.x * vectorB.x + vectorA.y * vectorB.y + vectorA.z * vectorB.z;
    96123}
    97124
    98 Vector3D operator+(const Vector3D &vectorA, const Vector3D &vectorB) {
    99   return Vector3D(vectorA.x + vectorB.x, vectorA.y + vectorB.y,
     125template <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,
    100127                  vectorA.z + vectorB.z);
    101128}
    102129
    103 Vector3D operator-(const Vector3D &vectorA, const Vector3D &vectorB) {
    104   return Vector3D(vectorA.x - vectorB.x, vectorA.y - vectorB.y,
     130template <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,
    105132                  vectorA.z - vectorB.z);
    106133}
    107134
    108 Vector3D operator-(const Vector3D &vector) {
    109   return Vector3D(-vector.x, -vector.y, -vector.z);
    110 }
    111 
    112 Vector3D operator*(const Vector3D &vectorA, const Vector3D &vectorB) {
    113   return Vector3D(vectorA.x * vectorB.x, vectorA.y * vectorB.y,
     135template <typename T> Vector3D<T> operator-(const Vector3D<T> &vector) {
     136  return Vector3D<T>(-vector.x, -vector.y, -vector.z);
     137}
     138
     139template <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,
    114141                  vectorA.z * vectorB.z);
    115142}
    116143
    117 Vector3D operator*(const Vector3D &vector, float coeff) {
    118   return Vector3D(vector.x * coeff, vector.y * coeff, vector.z * coeff);
    119 }
    120 
    121 Vector3D operator*(float coeff, const Vector3D &vector) {
    122   return Vector3D(vector.x * coeff, vector.y * coeff, vector.z * coeff);
    123 }
    124 
    125 Vector3D operator/(const Vector3D &vector, float coeff) {
     144template <typename T> Vector3D<T> operator*(const Vector3D<T> &vector, float coeff) {
     145  return Vector3D<T>(vector.x * coeff, vector.y * coeff, vector.z * coeff);
     146}
     147
     148template <typename T> Vector3D<T> operator*(float coeff, const Vector3D<T> &vector) {
     149  return Vector3D<T>(vector.x * coeff, vector.y * coeff, vector.z * coeff);
     150}
     151
     152template <typename T> Vector3D<T> operator/(const Vector3D<T> &vector, float coeff) {
    126153  if (coeff != 0) {
    127     return Vector3D(vector.x / coeff, vector.y / coeff, vector.z / coeff);
     154    return Vector3D<T>(vector.x / coeff, vector.y / coeff, vector.z / coeff);
    128155  } else {
    129156    printf("Vector3D: err divinding by 0\n");
    130     return Vector3D(0, 0, 0);
    131   }
    132 }
    133 
    134 void Vector3D::RotateX(float value) {
     157    return Vector3D<T>(0, 0, 0);
     158  }
     159}
     160
     161template <typename T> void Vector3D<T>::RotateX(float value) {
    135162  float y_tmp;
    136163  y_tmp = y * cosf(value) - z * sinf(value);
     
    139166}
    140167
    141 void Vector3D::RotateXDeg(float value) { RotateX(Euler::ToRadian(value)); }
    142 
    143 void Vector3D::RotateY(float value) {
     168template <typename T> void Vector3D<T>::RotateXDeg(float value) { RotateX(Euler::ToRadian(value)); }
     169
     170template <typename T> void Vector3D<T>::RotateY(float value) {
    144171  float x_tmp;
    145172  x_tmp = x * cosf(value) + z * sinf(value);
     
    148175}
    149176
    150 void Vector3D::RotateYDeg(float value) { RotateY(Euler::ToRadian(value)); }
    151 
    152 void Vector3D::RotateZ(float value) {
     177template <typename T> void Vector3D<T>::RotateYDeg(float value) { RotateY(Euler::ToRadian(value)); }
     178
     179template <typename T> void Vector3D<T>::RotateZ(float value) {
    153180  float x_tmp;
    154181  x_tmp = x * cosf(value) - y * sinf(value);
     
    157184}
    158185
    159 void Vector3D::RotateZDeg(float value) { RotateZ(Euler::ToRadian(value)); }
    160 
    161 void Vector3D::Rotate(const RotationMatrix &matrix) {
    162   float a[3] = {0, 0, 0};
    163   float b[3] = {x, y, z};
     186template <typename T> void Vector3D<T>::RotateZDeg(float value) { RotateZ(Euler::ToRadian(value)); }
     187
     188template <typename T> void Vector3D<T>::Rotate(const RotationMatrix &matrix) {
     189  T a[3] = {0, 0, 0};
     190  T b[3] = {x, y, z};
    164191
    165192  for (int i = 0; i < 3; i++) {
     
    174201}
    175202
    176 void Vector3D::Rotate(const Quaternion &quaternion) {
     203template <typename T> void Vector3D<T>::Rotate(const Quaternion &quaternion) {
    177204  RotationMatrix matrix;
    178205  quaternion.ToRotationMatrix(matrix);
     
    180207}
    181208
    182 void Vector3D::To2Dxy(Vector2D &vector) const {
     209template <typename T> void Vector3D<T>::To2Dxy(Vector2D<T> &vector) const {
    183210  vector.x = x;
    184211  vector.y = y;
    185212}
    186213
    187 Vector2D Vector3D::To2Dxy(void) const {
    188   Vector2D vect;
     214template <typename T> Vector2D<T> Vector3D<T>::To2Dxy(void) const {
     215  Vector2D<T> vect;
    189216  To2Dxy(vect);
    190217  return vect;
    191218}
    192219
    193 float Vector3D::GetNorm(void) const { return sqrt(x * x + y * y + z * z); }
    194 
    195 void Vector3D::Normalize(void) {
     220template <typename T> float Vector3D<T>::GetNorm(void) const { return sqrt(x * x + y * y + z * z); }
     221
     222template <typename T> void Vector3D<T>::Normalize(void) {
    196223  float n = GetNorm();
    197224  if (n != 0) {
     
    202229}
    203230
    204 void Vector3D::Saturate(const Vector3D &min, const Vector3D &max) {
     231template <typename T> void Vector3D<T>::Saturate(const Vector3D<T> &min, const Vector3D<T> &max) {
    205232  if (x < min.x)
    206233    x = min.x;
     
    219246}
    220247
    221 void Vector3D::Saturate(float min, float max) {
    222   Saturate(Vector3D(min, min, min), Vector3D(max, max, max));
    223 }
    224 
    225 void Vector3D::Saturate(const Vector3D &value) {
     248template <typename T> void Vector3D<T>::Saturate(float min, float max) {
     249  Saturate(Vector3D<T>(min, min, min), Vector3D<T>(max, max, max));
     250}
     251
     252template <typename T> void Vector3D<T>::Saturate(const Vector3D<T> &value) {
    226253  float x = fabs(value.x);
    227254  float y = fabs(value.y);
    228255  float z = fabs(value.z);
    229   Saturate(Vector3D(-x, -y, -z), Vector3D(x, y, z));
    230 }
    231 
    232 void Vector3D::Saturate(float value) {
     256  Saturate(Vector3D<T>(-x, -y, -z), Vector3D<T>(x, y, z));
     257}
     258
     259template <typename T> void Vector3D<T>::Saturate(float value) {
    233260  float sat = fabs(value);
    234   Saturate(Vector3D(-sat, -sat, -sat), Vector3D(sat, sat, sat));
     261  Saturate(Vector3D<T>(-sat, -sat, -sat), Vector3D<T>(sat, sat, sat));
    235262}
    236263
Note: See TracChangeset for help on using the changeset viewer.