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