00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 #ifndef _MATH_3D_H_
00043 #define _MATH_3D_H_
00044
00045
00046
00047
00048
00049
00050 #include <vector>
00051 #include "DataStructures/DynamicArrayTemplate.h"
00052
00053
00054
00055
00056
00057
00073 struct Vec3d
00074 {
00075 float x, y, z;
00076 };
00077
00093 struct Mat3d
00094 {
00095 float r1, r2, r3, r4, r5, r6, r7, r8, r9;
00096 };
00097
00105 struct Transformation3d
00106 {
00107 Mat3d rotation;
00108 Vec3d translation;
00109 };
00110
00118 struct Quaternion
00119 {
00121 Vec3d v;
00123 float w;
00124 };
00125
00126 typedef std::vector<Vec3d> Vec3dList;
00127 typedef CDynamicArrayTemplate<Vec3d> CVec3dArray;
00128
00129
00130
00131
00132
00133
00134
00139 namespace Math3d
00140 {
00141 bool LoadFromFile(Vec3d &vector, const char *pFilePath);
00142 bool LoadFromFile(Mat3d &matrix, const char *pFilePath);
00143 bool LoadFromFile(Transformation3d &transformation, const char *pFilePath);
00144 bool SaveToFile(const Vec3d &vector, const char *pFilePath);
00145 bool SaveToFile(const Mat3d &matrix, const char *pFilePath);
00146 bool SaveToFile(const Transformation3d &transformation, const char *pFilePath);
00147
00148 void SetVec(Vec3d &vec, float x, float y, float z);
00149 void SetVec(Vec3d &vec, const Vec3d &sourceVector);
00150 void SetMat(Mat3d &matrix, float r1, float r2, float r3, float r4, float r5, float r6, float r7, float r8, float r9);
00151 void SetMat(Mat3d &matrix, const Mat3d &sourceMatrix);
00152 void SetRotationMat(Mat3d &matrix, const Vec3d &axis, float theta);
00153 void SetRotationMat(Mat3d &matrix, float alpha, float beta, float gamma);
00154 void SetRotationMat(Mat3d &matrix, const Vec3d &rotation);
00155 void SetRotationMatYZX(Mat3d &matrix, const Vec3d &rotation);
00156 void SetRotationMatX(Mat3d &matrix, float theta);
00157 void SetRotationMatY(Mat3d &matrix, float theta);
00158 void SetRotationMatZ(Mat3d &matrix, float theta);
00159 void SetRotationMatAxis(Mat3d &matrix, const Vec3d &axis, float theta);
00160
00161 void MulMatVec(const Mat3d &matrix, const Vec3d &vec, Vec3d &result);
00162 void MulMatVec(const Mat3d &matrix, const Vec3d &vector1, const Vec3d &vector2, Vec3d &result);
00163 void MulMatMat(const Mat3d &matrix1, const Mat3d &matrix2, Mat3d &result);
00164
00165 void MulVecTransposedVec(const Vec3d &vector1, const Vec3d &vector2, Mat3d &result);
00166
00167 void RotateVec(const Vec3d &vec, const Vec3d &rotation, Vec3d &result);
00168 void RotateVecYZX(const Vec3d &vec, const Vec3d &rotation, Vec3d &result);
00169 void TransformVec(const Vec3d &vec, const Vec3d &rotation, const Vec3d &translation, Vec3d &result);
00170 void TransformVecYZX(const Vec3d &vec, const Vec3d &rotation, const Vec3d &translation, Vec3d &result);
00171
00172 void MulVecScalar(const Vec3d &vec, float scalar, Vec3d &result);
00173 void MulMatScalar(const Mat3d &matrix, float scalar, Mat3d &result);
00174
00175 void AddMatMat(const Mat3d &matrix1, const Mat3d &matrix2, Mat3d &matrix);
00176 void AddToMat(Mat3d &matrix, const Mat3d &matrixToAdd);
00177 void SubtractMatMat(const Mat3d &matrix1, const Mat3d &matrix2, Mat3d &result);
00178
00179 void AddVecVec(const Vec3d &vector1, const Vec3d &vector2, Vec3d &result);
00180 void SubtractVecVec(const Vec3d &vector1, const Vec3d &vector2, Vec3d &result);
00181 void AddToVec(Vec3d &vec, const Vec3d &vectorToAdd);
00182 void SubtractFromVec(Vec3d &vec, const Vec3d &vectorToSubtract);
00183
00184 void CrossProduct(const Vec3d &vector1, const Vec3d &vector2, Vec3d &result);
00185 float ScalarProduct(const Vec3d &vector1, const Vec3d &vector2);
00186 float SquaredLength(const Vec3d &vec);
00187 float Length(const Vec3d &vec);
00188 float Distance(const Vec3d &vector1, const Vec3d &vector2);
00189 float SquaredDistance(const Vec3d &vector1, const Vec3d &vector2);
00190 float Angle(const Vec3d &vector1, const Vec3d &vector2);
00191 float Angle(const Vec3d &vector1, const Vec3d &vector2, const Vec3d &axis);
00192 float EvaluateForm(const Vec3d &matrix1, const Mat3d &matrix2);
00193
00194 void NormalizeVec(Vec3d &vec);
00195
00196 void Transpose(const Mat3d &matrix, Mat3d &result);
00197 void Invert(const Mat3d &matrix, Mat3d &result);
00198 float Det(const Mat3d &matrix);
00199
00200 void SetTransformation(Transformation3d &transformation, const Vec3d &rotation, const Vec3d &translation);
00201 void SetTransformation(Transformation3d &transformation, const Transformation3d &sourceTransformation);
00202 void Invert(const Transformation3d &input, Transformation3d &result);
00203 void MulTransTrans(const Transformation3d &transformation1, const Transformation3d &transformation2, Transformation3d &result);
00204 void MulTransVec(const Transformation3d &transformation, const Vec3d &vec, Vec3d &result);
00205
00206 void MulQuatQuat(const Quaternion &quat1, const Quaternion &quat2, Quaternion &result);
00207 void RotateVecQuaternion(const Vec3d &vec, const Vec3d &axis, float theta, Vec3d &result);
00208 void RotateVecAngleAxis(const Vec3d &vec, const Vec3d &axis, float theta, Vec3d &result);
00209 void GetAxisAndAngle(const Mat3d &R, Vec3d &axis, float &angle);
00210
00211 void Average(const Vec3d &vector1, const Vec3d &vector2, Vec3d &result);
00212
00213 void Mean(const CVec3dArray &vectorList, Vec3d &result);
00214 void Mean(const Vec3d *pVectors, int nVectors, Vec3d &result);
00215
00216 extern const Vec3d zero_vec;
00217 extern const Mat3d unit_mat;
00218 extern const Mat3d zero_mat;
00219 }
00220
00221
00222
00223 #endif