Changeset 167 in flairsrc for trunk/lib/FlairCore/src/Vector3D.cpp
 Timestamp:
 04/12/17 13:59:38 (6 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/lib/FlairCore/src/Vector3D.cpp
r15 r167 23 23 #include "Object.h" 24 24 #include <math.h> 25 //#include "Vector3DSpinBox.h"26 25 27 26 namespace flair { 28 27 namespace core { 29 30 Vector3D::Vector3D(float inX, float inY, float inZ) : x(inX), y(inY), z(inZ) {} 31 32 Vector3D::~Vector3D() {} 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>&); 39 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>&); 50 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 33 59 /* 34 60 void Vector3D::operator=(const gui::Vector3DSpinBox *vector) { … … 38 64 z=vect.z; 39 65 }*/ 40 41 Vector3D &Vector3D::operator=(const Vector3D &vector) { 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) { 42 69 x = vector.x; 43 70 y = vector.y; … … 45 72 return (*this); 46 73 } 47 48 Vector3D &Vector3D::operator+=(const Vector3D&vector) {74 */ 75 template <typename T> Vector3D<T> &Vector3D<T>::operator+=(const Vector3D<T> &vector) { 49 76 x += vector.x; 50 77 y += vector.y; … … 53 80 } 54 81 55 Vector3D &Vector3D::operator=(const Vector3D&vector) {82 template <typename T> Vector3D<T> &Vector3D<T>::operator=(const Vector3D<T> &vector) { 56 83 x = vector.x; 57 84 y = vector.y; … … 60 87 } 61 88 62 float &Vector3D::operator[](size_t idx) {89 template <typename T> T &Vector3D<T>::operator[](size_t idx) { 63 90 if (idx == 0) { 64 91 return x; … … 73 100 } 74 101 75 const float &Vector3D::operator[](size_t idx) const {102 template <typename T> const T &Vector3D<T>::operator[](size_t idx) const { 76 103 if (idx == 0) { 77 104 return x; … … 86 113 } 87 114 88 Vector3D CrossProduct(const Vector3D &vectorA, const Vector3D&vectorB) {89 return Vector3D (vectorA.y * vectorB.z  vectorA.z * vectorB.y,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, 90 117 vectorA.z * vectorB.x  vectorA.x * vectorB.z, 91 118 vectorA.x * vectorB.y  vectorA.y * vectorB.x); 92 119 } 93 120 94 float DotProduct(const Vector3D &vectorA, const Vector3D&vectorB) {121 template <typename T> float DotProduct(const Vector3D<T> &vectorA, const Vector3D<T> &vectorB) { 95 122 return vectorA.x * vectorB.x + vectorA.y * vectorB.y + vectorA.z * vectorB.z; 96 123 } 97 124 98 Vector3D operator+(const Vector3D &vectorA, const Vector3D&vectorB) {99 return Vector3D (vectorA.x + vectorB.x, vectorA.y + vectorB.y,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, 100 127 vectorA.z + vectorB.z); 101 128 } 102 129 103 Vector3D operator(const Vector3D &vectorA, const Vector3D&vectorB) {104 return Vector3D (vectorA.x  vectorB.x, vectorA.y  vectorB.y,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, 105 132 vectorA.z  vectorB.z); 106 133 } 107 134 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,135 template <typename T> Vector3D<T> operator(const Vector3D<T> &vector) { 136 return Vector3D<T>(vector.x, vector.y, vector.z); 137 } 138 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, 114 141 vectorA.z * vectorB.z); 115 142 } 116 143 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) {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); 146 } 147 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); 150 } 151 152 template <typename T> Vector3D<T> operator/(const Vector3D<T> &vector, float coeff) { 126 153 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); 128 155 } else { 129 156 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 161 template <typename T> void Vector3D<T>::RotateX(float value) { 135 162 float y_tmp; 136 163 y_tmp = y * cosf(value)  z * sinf(value); … … 139 166 } 140 167 141 void Vector3D::RotateXDeg(float value) { RotateX(Euler::ToRadian(value)); }142 143 void Vector3D::RotateY(float value) {168 template <typename T> void Vector3D<T>::RotateXDeg(float value) { RotateX(Euler::ToRadian(value)); } 169 170 template <typename T> void Vector3D<T>::RotateY(float value) { 144 171 float x_tmp; 145 172 x_tmp = x * cosf(value) + z * sinf(value); … … 148 175 } 149 176 150 void Vector3D::RotateYDeg(float value) { RotateY(Euler::ToRadian(value)); }151 152 void Vector3D::RotateZ(float value) {177 template <typename T> void Vector3D<T>::RotateYDeg(float value) { RotateY(Euler::ToRadian(value)); } 178 179 template <typename T> void Vector3D<T>::RotateZ(float value) { 153 180 float x_tmp; 154 181 x_tmp = x * cosf(value)  y * sinf(value); … … 157 184 } 158 185 159 void Vector3D::RotateZDeg(float value) { RotateZ(Euler::ToRadian(value)); }160 161 void Vector3D::Rotate(const RotationMatrix &matrix) {162 floata[3] = {0, 0, 0};163 floatb[3] = {x, y, z};186 template <typename T> void Vector3D<T>::RotateZDeg(float value) { RotateZ(Euler::ToRadian(value)); } 187 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}; 164 191 165 192 for (int i = 0; i < 3; i++) { … … 174 201 } 175 202 176 void Vector3D::Rotate(const Quaternion &quaternion) {203 template <typename T> void Vector3D<T>::Rotate(const Quaternion &quaternion) { 177 204 RotationMatrix matrix; 178 205 quaternion.ToRotationMatrix(matrix); … … 180 207 } 181 208 182 void Vector3D::To2Dxy(Vector2D&vector) const {209 template <typename T> void Vector3D<T>::To2Dxy(Vector2D<T> &vector) const { 183 210 vector.x = x; 184 211 vector.y = y; 185 212 } 186 213 187 Vector2D Vector3D::To2Dxy(void) const {188 Vector2D vect;214 template <typename T> Vector2D<T> Vector3D<T>::To2Dxy(void) const { 215 Vector2D<T> vect; 189 216 To2Dxy(vect); 190 217 return vect; 191 218 } 192 219 193 float Vector3D::GetNorm(void) const { return sqrt(x * x + y * y + z * z); }194 195 void Vector3D::Normalize(void) {220 template <typename T> float Vector3D<T>::GetNorm(void) const { return sqrt(x * x + y * y + z * z); } 221 222 template <typename T> void Vector3D<T>::Normalize(void) { 196 223 float n = GetNorm(); 197 224 if (n != 0) { … … 202 229 } 203 230 204 void Vector3D::Saturate(const Vector3D &min, const Vector3D&max) {231 template <typename T> void Vector3D<T>::Saturate(const Vector3D<T> &min, const Vector3D<T> &max) { 205 232 if (x < min.x) 206 233 x = min.x; … … 219 246 } 220 247 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) {248 template <typename T> void Vector3D<T>::Saturate(float min, float max) { 249 Saturate(Vector3D<T>(min, min, min), Vector3D<T>(max, max, max)); 250 } 251 252 template <typename T> void Vector3D<T>::Saturate(const Vector3D<T> &value) { 226 253 float x = fabs(value.x); 227 254 float y = fabs(value.y); 228 255 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 259 template <typename T> void Vector3D<T>::Saturate(float value) { 233 260 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)); 235 262 } 236 263
Note:
See TracChangeset
for help on using the changeset viewer.