From b0ac0089aba3d0bc9f4b4e2bd889b377ae01465d Mon Sep 17 00:00:00 2001 From: leo Date: Thu, 18 Oct 2012 18:57:21 +0000 Subject: [PATCH] Removed old matrix class. --- common/lc_math.h | 20 ++ common/matrix.cpp | 401 ------------------------------------- common/matrix.h | 37 ---- common/object.h | 1 - common/piece.cpp | 82 ++++---- common/project.cpp | 148 +++++++------- common/project.h | 7 +- win/leocad.vcxproj | 2 - win/leocad.vcxproj.filters | 6 - 9 files changed, 132 insertions(+), 572 deletions(-) delete mode 100644 common/matrix.cpp delete mode 100644 common/matrix.h diff --git a/common/lc_math.h b/common/lc_math.h index b04f52bf..e8284895 100644 --- a/common/lc_math.h +++ b/common/lc_math.h @@ -399,6 +399,26 @@ inline lcVector4 operator/(const lcVector4& a, const lcVector4& b) return lcVector4(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w); } +inline lcVector4& operator+=(lcVector4& a, const lcVector4& b) +{ + a.x += b.x; + a.y += b.y; + a.z += b.z; + a.w += b.w; + + return a; +} + +inline lcVector4& operator-=(lcVector4& a, const lcVector4& b) +{ + a.x -= b.x; + a.y -= b.y; + a.z -= b.z; + a.w -= b.w; + + return a; +} + inline lcVector4& operator*=(lcVector4& a, float b) { a.x *= b; diff --git a/common/matrix.cpp b/common/matrix.cpp deleted file mode 100644 index 15429a2e..00000000 --- a/common/matrix.cpp +++ /dev/null @@ -1,401 +0,0 @@ -// -// 4x4 Matrix class -// - -#include "lc_global.h" -#include -#include -#include -#include "matrix.h" -#include "lc_math.h" - -// ============================================================================= -// static functions - -static float Identity[16] = { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 }; - -// Perform a 4x4 matrix multiplication (product = a x b). -// WARNING: (product != b) assumed -static void matmul (float *product, const float *a, const float *b) -{ - int i; - -#define A(row,col) a[(col<<2)+row] -#define B(row,col) b[(col<<2)+row] -#define P(row,col) product[(col<<2)+row] - - for (i = 0; i < 4; i++) - { - float ai0=A(i,0), ai1=A(i,1), ai2=A(i,2), ai3=A(i,3); - P(i,0) = ai0 * B(0,0) + ai1 * B(1,0) + ai2 * B(2,0) + ai3 * B(3,0); - P(i,1) = ai0 * B(0,1) + ai1 * B(1,1) + ai2 * B(2,1) + ai3 * B(3,1); - P(i,2) = ai0 * B(0,2) + ai1 * B(1,2) + ai2 * B(2,2) + ai3 * B(3,2); - P(i,3) = ai0 * B(0,3) + ai1 * B(1,3) + ai2 * B(2,3) + ai3 * B(3,3); - } - -#undef A -#undef B -#undef P -} - -// Generate a 4x4 transformation matrix from rotation parameters. -static void rotation_matrix (double angle, float x, float y, float z, float m[] ) -{ - float s, c, mag, xx, yy, zz, xy, yz, zx, xs, ys, zs, one_c; - - s = (float)sin (angle * LC_DTOR); - c = (float)cos (angle * LC_DTOR); - mag = (float)sqrt(x*x + y*y + z*z); - - if (mag == 0) - { - // generate an identity matrix and return - memcpy (m, Identity, sizeof(float[16])); - return; - } - - x /= mag; - y /= mag; - z /= mag; - - xx = x * x; - yy = y * y; - zz = z * z; - xy = x * y; - yz = y * z; - zx = z * x; - xs = x * s; - ys = y * s; - zs = z * s; - one_c = 1.0f - c; - - m[0] = (one_c * xx) + c; - m[4] = (one_c * xy) - zs; - m[8] = (one_c * zx) + ys; - m[12]= 0; - - m[1] = (one_c * xy) + zs; - m[5] = (one_c * yy) + c; - m[9] = (one_c * yz) - xs; - m[13]= 0; - - m[2] = (one_c * zx) - ys; - m[6] = (one_c * yz) + xs; - m[10]= (one_c * zz) + c; - m[14]= 0; - - m[3] = 0; - m[7] = 0; - m[11]= 0; - m[15]= 1; -} - -// ============================================================================= -// Matrix class - -Matrix::Matrix () -{ - LoadIdentity(); -} - -// Create a matrix from axis-angle and a point -Matrix::Matrix (const float *rot, const float *pos) -{ - float tmp[4] = { rot[0], rot[1], rot[2], rot[3]*LC_DTOR }; - float q[4]; - float length, cosA, sinA; - length = (float)sqrt(tmp[0]*tmp[0] + tmp[1]*tmp[1] + tmp[2]*tmp[2]); - - // if zero vector passed in, just return identity quaternion - if (length < 1E-5) - { - q[0] = 0; - q[1] = 0; - q[2] = 0; - q[3] = 1; - return; - } - - tmp[0] /= length; - tmp[1] /= length; - tmp[2] /= length; - - cosA = (float)cos(tmp[3] / 2.0f); - sinA = (float)sin(tmp[3] / 2.0f); - - q[3] = cosA; - q[0] = sinA * tmp[0]; - q[1] = sinA * tmp[1]; - q[2] = sinA * tmp[2]; - - // Now calculate the matrix - float s,xs,ys,zs,wx,wy,wz,xx,xy,xz,yy,yz,zz; - - s = 2.0f / (q[0]*q[0] + q[1]*q[1] + q[2]*q[2] + q[3]*q[3]); - - xs = q[0] * s; ys = q[1] * s; zs = q[2] * s; - wx = q[3] * xs; wy = q[3] * ys; wz = q[3] * zs; - xx = q[0] * xs; xy = q[0] * ys; xz = q[0] * zs; - yy = q[1] * ys; yz = q[1] * zs; zz = q[2] * zs; - - m[0] = 1.0f - (yy + zz); - m[4] = xy - wz; - m[8] = xz + wy; - m[12]= pos[0]; - - m[1] = xy + wz; - m[5] = 1.0f - (xx + zz); - m[9] = yz - wx; - m[13]= pos[1]; - - m[2] = xz - wy; - m[6] = yz + wx; - m[10]= 1.0f - (xx + yy); - m[14]= pos[2]; - - m[3] = 0.0f; - m[7] = 0.0f; - m[11] = 0.0f; - m[15] = 1.0f; -} - -void Matrix::FromFloat (const float* mat) -{ - memcpy (&m[0], mat, sizeof(float[16])); -} - -void Matrix::LoadIdentity () -{ - memcpy (&m[0], &Identity, sizeof(float[16])); -} - -float Matrix::Determinant() const -{ - return m[0]*m[5]*m[10] + m[1]*m[6]*m[8] + m[2]*m[4]*m[9] - m[0]*m[6]*m[9] - m[1]*m[4]*m[10] - m[2]*m[5]*m[8]; -} - -void Matrix::Multiply(const Matrix& m1, const Matrix& m2) -{ - matmul(m, m1.m, m2.m); -} - -void Matrix::Rotate (float angle, float x, float y, float z) -{ - float rm[16]; - - if (angle == 0.0) - return; - - rotation_matrix(angle, x, y, z, rm); - matmul(rm, rm, m); - memcpy (&m[0], &rm[0], sizeof(rm)); -} - -void Matrix::RotateCenter (float angle, float x, float y, float z, float px, float py, float pz) -{ - m[12] -= px; - m[13] -= py; - m[14] -= pz; - - Rotate (angle, x, y, z); - - m[12] += px; - m[13] += py; - m[14] += pz; -} - -void Matrix::Translate (float x, float y, float z) -{ - m[12] = m[0] * x + m[4] * y + m[8] * z + m[12]; - m[13] = m[1] * x + m[5] * y + m[9] * z + m[13]; - m[14] = m[2] * x + m[6] * y + m[10] * z + m[14]; - m[15] = m[3] * x + m[7] * y + m[11] * z + m[15]; -} - -void Matrix::SetTranslation (float x, float y, float z) -{ - m[12] = x; - m[13] = y; - m[14] = z; - m[15] = 1; -} - -void Matrix::GetTranslation (float* x, float* y, float* z) -{ - *x = m[12]; - *y = m[13]; - *z = m[14]; -} - -void Matrix::GetTranslation (float pos[3]) -{ - pos[0] = m[12]; - pos[1] = m[13]; - pos[2] = m[14]; -} - -void Matrix::SetTranslation (float pos[3]) -{ - m[12] = pos[0]; - m[13] = pos[1]; - m[14] = pos[2]; - m[15] = 1; -} - -void Matrix::CreateOld(float mx, float my, float mz, float rx, float ry, float rz) -{ - LoadIdentity(); - Translate(mx, my, mz); - - float rm[16]; - rotation_matrix(rx, 1, 0, 0, rm); - matmul(m, m, rm); - rotation_matrix(ry, 0, 1, 0, rm); - matmul(m, m, rm); - rotation_matrix(rz, 0, 0, 1, rm); - matmul(m, m, rm); -} - -// Transform a point by a 4x4 matrix. out = m * in -void Matrix::TransformPoint(float out[], const float in[3]) -{ - out[0] = m[0]*in[0] + m[4]*in[1] + m[8]*in[2] + m[12]; - out[1] = m[1]*in[0] + m[5]*in[1] + m[9]*in[2] + m[13]; - out[2] = m[2]*in[0] + m[6]*in[1] + m[10]*in[2] + m[14]; -} - -void Matrix::TransformPoints (float p[], int n) -{ - for (int i = 0; i < n*3; i += 3) - { - float tmp[3] = { p[i], p[i+1], p[i+2] }; - TransformPoint (&p[i], tmp); - } -} - -void Matrix::FromLDraw (const float *f) -{ - float trans[16] = { 1,0,0,0, 0,0,-1,0, 0,1,0,0, 0,0,0,1 }; - float t[16] = { 1,0,0,0, 0,0,1,0, 0,-1,0,0, 0,0,0,1 }; - - m[0] = f[3]; m[1] = f[6]; m[2] = f[9]; m[3] = 0.0f; - m[4] = f[4]; m[5] = f[7]; m[6] = f[10]; m[7] = 0.0f; - m[8] = f[5]; m[9] = f[8]; m[10]= f[11]; m[11] = 0.0f; - m[12]= f[0]/25; m[13]= f[1]/25; m[14]= f[2]/25; m[15] = 1.0f; - - matmul (m, m, t); - matmul (trans, trans, m); - memcpy (&m[0], &trans[0], sizeof(m)); -} - -void Matrix::ToLDraw (float *f) const -{ - float trans[16] = { 1,0,0,0, 0,0,-1,0, 0,1,0,0, 0,0,0,1 }; - float tmp[16] = { 1,0,0,0, 0,0,1,0, 0,-1,0,0, 0,0,0,1 }; - - matmul(tmp, tmp, m); - matmul (tmp, tmp, trans); - - f[0] = m[12]*25; f[1] = -m[14]*25; f[2] = m[13]*25; - f[3] = tmp[0]; f[4] = tmp[4]; f[5] = tmp[8]; - f[6] = tmp[1]; f[7] = tmp[5]; f[8] = tmp[9]; - f[9] = tmp[2]; f[10]= tmp[6]; f[11]= tmp[10]; -} - -void Matrix::ToAxisAngle(float *rot) const -{ - Matrix tmp(*this); - - // Normalize. - float inv; - inv = 1.0f / sqrtf(tmp.m[0]*tmp.m[0] + tmp.m[1]*tmp.m[1] + tmp.m[2]*tmp.m[2]); - tmp.m[0] *= inv; tmp.m[1] *= inv; tmp.m[2] *= inv; - inv = 1.0f / sqrtf(tmp.m[4]*tmp.m[4] + tmp.m[5]*tmp.m[5] + tmp.m[6]*tmp.m[6]); - tmp.m[4] *= inv; tmp.m[5] *= inv; tmp.m[6] *= inv; - inv = 1.0f / sqrtf(tmp.m[8]*tmp.m[8] + tmp.m[9]*tmp.m[9] + tmp.m[10]*tmp.m[10]); - tmp.m[8] *= inv; tmp.m[9] *= inv; tmp.m[10] *= inv; - - // Determinant should be 1 for rotation matrices. - if (tmp.Determinant() < 0.0f) - { - tmp.m[0] *= -1.0f; - tmp.m[1] *= -1.0f; - tmp.m[2] *= -1.0f; - } - - float fTrace = tmp.m[0] + tmp.m[5] + tmp.m[10]; - float fCos = 0.5f * (fTrace - 1.0f); - - rot[3] = acosf(fCos); // in [0,PI] - - if (rot[3] > 0.01f) - { - if (fabs (LC_PI - rot[3]) > 0.01f) - { - rot[0] = tmp.m[6] - tmp.m[9]; - rot[1] = tmp.m[8] - tmp.m[2]; - rot[2] = tmp.m[1] - tmp.m[4]; - - inv = 1.0f / sqrtf(rot[0]*rot[0] + rot[1]*rot[1] + rot[2]*rot[2]); - - rot[0] *= inv; - rot[1] *= inv; - rot[2] *= inv; - } - else - { - // angle is PI - float fHalfInverse; - if (tmp.m[0] >= tmp.m[5]) - { - // r00 >= r11 - if (tmp.m[0] >= tmp.m[10]) - { - // r00 is maximum diagonal term - rot[0] = 0.5f * sqrtf(tmp.m[0] - tmp.m[5] - tmp.m[10] + 1.0f); - fHalfInverse = 0.5f / rot[0]; - rot[1] = fHalfInverse * tmp.m[4]; - rot[2] = fHalfInverse * tmp.m[8]; - } - else - { - // r22 is maximum diagonal term - rot[2] = 0.5f * sqrtf(tmp.m[10] - tmp.m[0] - tmp.m[5] + 1.0f); - fHalfInverse = 0.5f / rot[2]; - rot[0] = fHalfInverse * tmp.m[8]; - rot[1] = fHalfInverse * tmp.m[9]; - } - } - else - { - // r11 > r00 - if (tmp.m[5] >= tmp.m[10]) - { - // r11 is maximum diagonal term - rot[1] = 0.5f * sqrtf(tmp.m[5] - tmp.m[0] - tmp.m[10] + 1.0f); - fHalfInverse = 0.5f / rot[1]; - rot[0] = fHalfInverse * tmp.m[4]; - rot[2] = fHalfInverse * tmp.m[9]; - } - else - { - // r22 is maximum diagonal term - rot[2] = 0.5f * sqrtf(tmp.m[10] - tmp.m[0] - tmp.m[5] + 1.0f); - fHalfInverse = 0.5f / rot[2]; - rot[0] = fHalfInverse * tmp.m[8]; - rot[1] = fHalfInverse * tmp.m[9]; - } - } - } - } - else - { - // The angle is 0 and the matrix is the identity. Any axis will - // work, so just use the z-axis. - rot[0] = 0.0f; - rot[1] = 0.0f; - rot[2] = 1.0f; - } - - rot[3] *= LC_RTOD; -} diff --git a/common/matrix.h b/common/matrix.h deleted file mode 100644 index c5f9765d..00000000 --- a/common/matrix.h +++ /dev/null @@ -1,37 +0,0 @@ -#ifndef _MATRIX_H_ -#define _MATRIX_H_ - -class Matrix -{ -public: - Matrix(); - Matrix(const float *rot, const float *pos); - ~Matrix() { }; - - void FromFloat(const float* mat); - void FromLDraw(const float *f); - - void ToLDraw(float *f) const; - void ToAxisAngle(float *rot) const; - - void LoadIdentity(); - void Translate(float x, float y, float z); - void Multiply(const Matrix& m1, const Matrix& m2); - float Determinant() const; - - void GetTranslation(float *x, float *y, float *z); - void SetTranslation(float x, float y, float z); - void GetTranslation(float pos[3]); - void SetTranslation(float pos[3]); - - void TransformPoint(float out[], const float in[3]); - void TransformPoints(float p[], int n); - void CreateOld(float mx, float my, float mz, float rx, float ry, float rz); - void Rotate(float angle, float x, float y, float z); - void RotateCenter(float angle, float x, float y, float z, float px, float py, float pz); - -public: - float m[16]; -}; - -#endif //_MATRIX_H_ diff --git a/common/object.h b/common/object.h index 0158689b..02b25aae 100755 --- a/common/object.h +++ b/common/object.h @@ -3,7 +3,6 @@ #include "lc_math.h" -class Matrix; class Object; /* #define LC_OBJECT_NAME_LEN 80 diff --git a/common/piece.cpp b/common/piece.cpp index 771dea9f..8b77679a 100644 --- a/common/piece.cpp +++ b/common/piece.cpp @@ -9,7 +9,6 @@ #include #include #include "opengl.h" -#include "matrix.h" #include "pieceinf.h" #include "piece.h" #include "group.h" @@ -86,12 +85,12 @@ bool Piece::FileLoad(lcFile& file, char* name) if (version < 9) { lcuint16 time; - float param[4]; lcuint8 type; if (version > 5) { lcuint32 keys; + float param[4]; file.ReadU32(&keys, 1); while (keys--) @@ -121,54 +120,55 @@ bool Piece::FileLoad(lcFile& file, char* name) while (ch--) { - Matrix mat; - if (version > 3) - { - float m[16]; - file.ReadFloats(m, 16); - mat.FromFloat (m); - } - else - { - float move[3], rotate[3]; - file.ReadFloats(move, 3); - file.ReadFloats(rotate, 3); - mat.CreateOld (move[0], move[1], move[2], rotate[0], rotate[1], rotate[2]); - } + lcMatrix44 ModelWorld; - lcuint8 b; - file.ReadU8(&b, 1); - time = b; + if (version > 3) + { + file.ReadFloats(ModelWorld, 16); + } + else + { + lcVector3 Translation; + float Rotation[3]; + file.ReadFloats(Translation, 3); + file.ReadFloats(Rotation, 3); + ModelWorld = lcMatrix44Translation(Translation); + ModelWorld = lcMul(lcMatrix44RotationZ(Rotation[2] * LC_DTOR), lcMul(lcMatrix44RotationY(Rotation[1] * LC_DTOR), lcMul(lcMatrix44RotationX(Rotation[0] * LC_DTOR), ModelWorld))); + } - mat.GetTranslation(¶m[0], ¶m[1], ¶m[2]); - param[3] = 0; - ChangeKey (time, false, true, param, LC_PK_POSITION); - ChangeKey (time, true, true, param, LC_PK_POSITION); + lcuint8 b; + file.ReadU8(&b, 1); + time = b; - mat.ToAxisAngle (param); - ChangeKey (time, false, true, param, LC_PK_ROTATION); - ChangeKey (time, true, true, param, LC_PK_ROTATION); + ChangeKey(1, false, true, ModelWorld.r[3], LC_PK_POSITION); + ChangeKey(1, true, true, ModelWorld.r[3], LC_PK_POSITION); - lcint32 bl; - file.ReadS32(&bl, 1); + lcVector4 AxisAngle = lcMatrix44ToAxisAngle(ModelWorld); + AxisAngle[3] *= LC_RTOD; + + ChangeKey(time, false, true, AxisAngle, LC_PK_ROTATION); + ChangeKey(time, true, true, AxisAngle, LC_PK_ROTATION); + + lcint32 bl; + file.ReadS32(&bl, 1); } } else { - Matrix mat; - float move[3], rotate[3]; - file.ReadFloats(move, 3); - file.ReadFloats(rotate, 3); - mat.CreateOld (move[0], move[1], move[2], rotate[0], rotate[1], rotate[2]); + lcVector3 Translation; + float Rotation[3]; + file.ReadFloats(Translation, 3); + file.ReadFloats(Rotation, 3); + lcMatrix44 ModelWorld = lcMatrix44Translation(Translation); + ModelWorld = lcMul(lcMatrix44RotationZ(Rotation[2] * LC_DTOR), lcMul(lcMatrix44RotationY(Rotation[1] * LC_DTOR), lcMul(lcMatrix44RotationX(Rotation[0] * LC_DTOR), ModelWorld))); - mat.GetTranslation(¶m[0], ¶m[1], ¶m[2]); - param[3] = 0; - ChangeKey (1, false, true, param, LC_PK_POSITION); - ChangeKey (1, true, true, param, LC_PK_POSITION); + ChangeKey(1, false, true, ModelWorld.r[3], LC_PK_POSITION); + ChangeKey(1, true, true, ModelWorld.r[3], LC_PK_POSITION); - mat.ToAxisAngle (param); - ChangeKey (1, false, true, param, LC_PK_ROTATION); - ChangeKey (1, true, true, param, LC_PK_ROTATION); + lcVector4 AxisAngle = lcMatrix44ToAxisAngle(ModelWorld); + AxisAngle[3] *= LC_RTOD; + ChangeKey(1, false, true, AxisAngle, LC_PK_ROTATION); + ChangeKey(1, true, true, AxisAngle, LC_PK_ROTATION); } } } @@ -563,8 +563,6 @@ void Piece::UpdatePosition(unsigned short nTime, bool bAnimation) m_nState &= ~(LC_PIECE_SELECTED|LC_PIECE_FOCUSED); CalculateKeys (nTime, bAnimation); -// if (CalculatePositionRotation(nTime, bAnimation, mPosition, mRotation)) - Matrix mat(mRotation, mPosition); mModelWorld = lcMatrix44FromAxisAngle(lcVector3(mRotation[0], mRotation[1], mRotation[2]), mRotation[3] * LC_DTOR); mModelWorld.SetTranslation(mPosition); diff --git a/common/project.cpp b/common/project.cpp index 65cbe518..e6534d72 100644 --- a/common/project.cpp +++ b/common/project.cpp @@ -11,7 +11,6 @@ #include #include #include "opengl.h" -#include "matrix.h" #include "pieceinf.h" #include "lc_texture.h" #include "piece.h" @@ -443,7 +442,7 @@ bool Project::FileLoad(lcFile* file, bool bUndo, bool bMerge) else { char name[LC_PIECE_NAME_LEN]; - float pos[3], rot[3], param[4]; + float pos[3], rot[3]; lcuint8 color, step, group; file->ReadFloats(pos, 3); @@ -457,16 +456,18 @@ bool Project::FileLoad(lcFile* file, bool bUndo, bool bMerge) if (pInfo != NULL) { Piece* pPiece = new Piece(pInfo); - Matrix mat; pPiece->Initialize(pos[0], pos[1], pos[2], step, 1); pPiece->SetColorCode(lcGetColorCodeFromOriginalColor(color)); pPiece->CreateName(m_pPieces); AddPiece(pPiece); - mat.CreateOld(0,0,0, rot[0],rot[1],rot[2]); - mat.ToAxisAngle(param); - pPiece->ChangeKey(1, false, false, param, LC_PK_ROTATION); - pPiece->ChangeKey(1, true, false, param, LC_PK_ROTATION); + + lcMatrix44 ModelWorld = lcMul(lcMatrix44RotationZ(rot[2] * LC_DTOR), lcMul(lcMatrix44RotationY(rot[1] * LC_DTOR), lcMatrix44RotationX(rot[0] * LC_DTOR))); + lcVector4 AxisAngle = lcMatrix44ToAxisAngle(ModelWorld); + AxisAngle[3] *= LC_RTOD; + + pPiece->ChangeKey(1, false, false, AxisAngle, LC_PK_ROTATION); + pPiece->ChangeKey(1, true, false, AxisAngle, LC_PK_ROTATION); // pPiece->SetGroup((Group*)group); SystemPieceComboAdd(pInfo->m_strDescription); } @@ -952,7 +953,7 @@ void Project::FileReadMPD(lcFile& MPD, PtrArray& FileArray) const } } -void Project::FileReadLDraw(lcFile* file, Matrix* prevmat, int* nOk, int DefColor, int* nStep, PtrArray& FileArray) +void Project::FileReadLDraw(lcFile* file, const lcMatrix44& CurrentTransform, int* nOk, int DefColor, int* nStep, PtrArray& FileArray) { char buf[1024]; @@ -979,9 +980,10 @@ void Project::FileReadLDraw(lcFile* file, Matrix* prevmat, int* nOk, int DefColo &fmat[7], &fmat[8], &fmat[9], &fmat[10], &fmat[11], &tmp[0]) != 15) continue; - Matrix incmat, tmpmat; - incmat.FromLDraw(fmat); - tmpmat.Multiply(*prevmat, incmat); + lcMatrix44 IncludeTransform(lcVector4(fmat[3], fmat[9], -fmat[6], 0.0f), lcVector4(fmat[5], fmat[11], -fmat[8], 0.0f), + lcVector4(-fmat[4], -fmat[10], fmat[7], 0.0f), lcVector4(fmat[0], fmat[2], -fmat[1], 0.0f)); + + IncludeTransform = lcMul(IncludeTransform, CurrentTransform); if (cmd == 1) { @@ -1004,18 +1006,18 @@ void Project::FileReadLDraw(lcFile* file, Matrix* prevmat, int* nOk, int DefColo PieceInfo* pInfo = lcGetPiecesLibrary()->FindPiece(name, false); if (pInfo != NULL) { - float x, y, z, rot[4]; Piece* pPiece = new Piece(pInfo); read = false; - tmpmat.GetTranslation(&x, &y, &z); - pPiece->Initialize(x, y, z, *nStep, 1); + lcVector4 AxisAngle = lcMatrix44ToAxisAngle(IncludeTransform); + AxisAngle[3] *= LC_RTOD; + + pPiece->Initialize(IncludeTransform[3].x, IncludeTransform[3].y, IncludeTransform[3].z, *nStep, 1); pPiece->SetColorCode(cl); pPiece->CreateName(m_pPieces); AddPiece(pPiece); - tmpmat.ToAxisAngle(rot); - pPiece->ChangeKey(1, false, false, rot, LC_PK_ROTATION); - pPiece->ChangeKey(1, true, false, rot, LC_PK_ROTATION); + pPiece->ChangeKey(1, false, false, AxisAngle, LC_PK_ROTATION); + pPiece->ChangeKey(1, true, false, AxisAngle, LC_PK_ROTATION); SystemPieceComboAdd(pInfo->m_strDescription); (*nOk)++; } @@ -1028,7 +1030,7 @@ void Project::FileReadLDraw(lcFile* file, Matrix* prevmat, int* nOk, int DefColo { if (stricmp(FileArray[i]->FileName, pn) == 0) { - FileReadLDraw(&FileArray[i]->File, &tmpmat, nOk, cl, nStep, FileArray); + FileReadLDraw(&FileArray[i]->File, IncludeTransform, nOk, cl, nStep, FileArray); read = false; break; } @@ -1042,7 +1044,7 @@ void Project::FileReadLDraw(lcFile* file, Matrix* prevmat, int* nOk, int DefColo if (tf.Open(pn, "rt")) { - FileReadLDraw(&tf, &tmpmat, nOk, cl, nStep, FileArray); + FileReadLDraw(&tf, IncludeTransform, nOk, cl, nStep, FileArray); read = false; } } @@ -1052,18 +1054,18 @@ void Project::FileReadLDraw(lcFile* file, Matrix* prevmat, int* nOk, int DefColo // Create a placeholder. PieceInfo* Info = lcGetPiecesLibrary()->CreatePlaceholder(tmp); - float x, y, z, rot[4]; Piece* pPiece = new Piece(Info); read = false; - tmpmat.GetTranslation(&x, &y, &z); - pPiece->Initialize(x, y, z, *nStep, 1); + lcVector4 AxisAngle = lcMatrix44ToAxisAngle(IncludeTransform); + AxisAngle[3] *= LC_RTOD; + + pPiece->Initialize(IncludeTransform[3].x, IncludeTransform[3].y, IncludeTransform[3].z, *nStep, 1); pPiece->SetColorCode(cl); pPiece->CreateName(m_pPieces); AddPiece(pPiece); - tmpmat.ToAxisAngle(rot); - pPiece->ChangeKey(1, false, false, rot, LC_PK_ROTATION); - pPiece->ChangeKey(1, true, false, rot, LC_PK_ROTATION); + pPiece->ChangeKey(1, false, false, AxisAngle, LC_PK_ROTATION); + pPiece->ChangeKey(1, true, false, AxisAngle, LC_PK_ROTATION); SystemPieceComboAdd(Info->m_strDescription); (*nOk)++; } @@ -1195,13 +1197,9 @@ bool Project::DoSave(char* lpszPathName, bool bReplace) { if ((pPiece->IsVisible(i, false)) && (pPiece->GetStepShow() == i)) { - float f[12]; - const lcVector3& position = pPiece->mPosition; - const lcVector4& rotation = pPiece->mRotation; - Matrix mat(rotation, position); - mat.ToLDraw(f); + const float* f = pPiece->mModelWorld; sprintf (buf, " 1 %d %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %.2f %s.DAT\r\n", - pPiece->mColorCode, f[0], f[1], f[2], f[3], f[4], f[5], f[6], f[7], f[8], f[9], f[10], f[11], pPiece->mPieceInfo->m_strName); + pPiece->mColorCode, f[12] * 25.0f, -f[14] * 25.0f, f[13] *25.0f, f[0], -f[8], f[4], -f[2], f[10], -f[6], f[1], -f[9], f[5], pPiece->mPieceInfo->m_strName); file.WriteBuffer(buf, strlen(buf)); } } @@ -1387,12 +1385,12 @@ bool Project::OnOpenDocument (const char* lpszPathName) if (datfile || mpdfile) { int ok = 0, step = 1; - Matrix mat; + lcMatrix44 mat = lcMatrix44Identity(); if (mpdfile) - FileReadLDraw(&FileArray[0]->File, &mat, &ok, 16, &step, FileArray); + FileReadLDraw(&FileArray[0]->File, mat, &ok, 16, &step, FileArray); else - FileReadLDraw(&file, &mat, &ok, 16, &step, FileArray); + FileReadLDraw(&file, mat, &ok, 16, &step, FileArray); m_nCurStep = step; SystemUpdateTime(false, m_nCurStep, 255); @@ -4163,16 +4161,12 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) for (Piece* piece = m_pPieces; piece; piece = piece->m_pNext) { int Index = Library->mPieces.FindIndex(piece->mPieceInfo); - float fl[12]; int Color; Color = piece->mColorIndex; const char* Suffix = lcIsColorTranslucent(Color) ? "_clear" : ""; - const lcVector3& pos = piece->mPosition; - const lcVector4& rot = piece->mRotation; - Matrix mat(rot, pos); - mat.ToLDraw(fl); + const float* f = piece->mModelWorld; if (PieceFlags[Index] & LGEO_PIECE_SLOPE) { @@ -4180,12 +4174,12 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) " object {\n %s_slope\n texture { %s normal { bumps 0.3 scale 0.02 } }\n }\n" " matrix <%.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f>\n}\n", PieceTable + Index * LC_PIECE_NAME_LEN, Suffix, &ColorTable[Color * LC_MAX_COLOR_NAME], PieceTable + Index * LC_PIECE_NAME_LEN, &ColorTable[Color * LC_MAX_COLOR_NAME], - -fl[11], -fl[5], fl[8], -fl[9], -fl[3], fl[6], -fl[10], -fl[4], fl[7], pos[1], pos[0], pos[2]); + -f[5], -f[4], -f[6], -f[1], -f[0], -f[2], f[9], f[8], f[10], f[13], f[12], f[14]); } else { sprintf(Line, "object {\n %s%s\n texture { %s }\n matrix <%.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f, %.4f>\n}\n", - PieceTable + Index * LC_PIECE_NAME_LEN, Suffix, &ColorTable[Color * LC_MAX_COLOR_NAME], -fl[11], -fl[5], fl[8], -fl[9], -fl[3], fl[6], -fl[10], -fl[4], fl[7], pos[1], pos[0], pos[2]); + PieceTable + Index * LC_PIECE_NAME_LEN, Suffix, &ColorTable[Color * LC_MAX_COLOR_NAME], -f[5], -f[4], -f[6], -f[1], -f[0], -f[2], f[9], f[8], f[10], f[13], f[12], f[14]); } POVFile.WriteLine(Line); @@ -4314,17 +4308,14 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) for (pPiece = m_pPieces; pPiece; pPiece = pPiece->m_pNext) { - float tmp[3]; - const lcVector3& pos = pPiece->mPosition; - const lcVector4& rot = pPiece->mRotation; - Matrix mat(rot, pos); + const lcMatrix44& ModelWorld = pPiece->mModelWorld; PieceInfo* pInfo = pPiece->mPieceInfo; float* Verts = (float*)pInfo->mMesh->mVertexBuffer.mData; for (int i = 0; i < pInfo->mMesh->mNumVertices * 3; i += 3) { - mat.TransformPoint(tmp, &Verts[i]); - sprintf(Line, "v %.2f %.2f %.2f\n", tmp[0], tmp[1], tmp[2]); + lcVector3 Vertex = lcMul31(lcVector3(Verts[i], Verts[i+1], Verts[i+2]), ModelWorld); + sprintf(Line, "v %.2f %.2f %.2f\n", Vertex[0], Vertex[1], Vertex[2]); OBJFile.WriteLine(Line); } @@ -4895,7 +4886,6 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) if (Wizard.m_Info[i] == NULL) continue; - Matrix mat; Piece* pPiece = new Piece(Wizard.m_Info[i]); lcVector4& Position = Wizard.m_Matrices[i][3]; @@ -5000,26 +4990,33 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) for (i = 0; i < opts.n1DCount; i++) { - lcVector3 pos = pPiece->mPosition; - lcVector4 param = pPiece->mRotation; - Matrix mat(param, pos); + lcMatrix44 ModelWorld; + lcVector3 Position; if (sel == 1) { - mat.GetTranslation(pos); - mat.Rotate(i*opts.fRotate[0], 1, 0, 0); - mat.Rotate(i*opts.fRotate[1], 0, 1, 0); - mat.Rotate(i*opts.fRotate[2], 0, 0, 1); - mat.SetTranslation(pos); + ModelWorld = lcMul(pPiece->mModelWorld, lcMatrix44RotationX(i * opts.fRotate[0] * LC_DTOR)); + ModelWorld = lcMul(ModelWorld, lcMatrix44RotationY(i * opts.fRotate[1] * LC_DTOR)); + ModelWorld = lcMul(ModelWorld, lcMatrix44RotationZ(i * opts.fRotate[2] * LC_DTOR)); + + Position = pPiece->mPosition; } else { - mat.RotateCenter(i*opts.fRotate[0],1,0,0,(bs[0]+bs[3])/2,(bs[1]+bs[4])/2,(bs[2]+bs[5])/2); - mat.RotateCenter(i*opts.fRotate[1],0,1,0,(bs[0]+bs[3])/2,(bs[1]+bs[4])/2,(bs[2]+bs[5])/2); - mat.RotateCenter(i*opts.fRotate[2],0,0,1,(bs[0]+bs[3])/2,(bs[1]+bs[4])/2,(bs[2]+bs[5])/2); + lcVector4 Center((bs[0] + bs[3]) / 2, (bs[1] + bs[4]) / 2, (bs[2] + bs[5]) / 2, 0.0f); + ModelWorld = pPiece->mModelWorld; + + ModelWorld.r[3] -= Center; + ModelWorld = lcMul(ModelWorld, lcMatrix44RotationX(i * opts.fRotate[0] * LC_DTOR)); + ModelWorld = lcMul(ModelWorld, lcMatrix44RotationY(i * opts.fRotate[1] * LC_DTOR)); + ModelWorld = lcMul(ModelWorld, lcMatrix44RotationZ(i * opts.fRotate[2] * LC_DTOR)); + ModelWorld.r[3] += Center; + + Position = lcVector3(ModelWorld.r[3].x, ModelWorld.r[3].y, ModelWorld.r[3].z); } - mat.ToAxisAngle(param); - mat.GetTranslation(pos); + + lcVector4 AxisAngle = lcMatrix44ToAxisAngle(ModelWorld); + AxisAngle[3] *= LC_RTOD; if (i != 0) { @@ -5031,10 +5028,10 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) else pLast = pFirst = new Piece(pPiece->mPieceInfo); - pLast->Initialize(pos[0]+i*opts.fMove[0], pos[1]+i*opts.fMove[1], pos[2]+i*opts.fMove[2], m_nCurStep, m_nCurFrame); + pLast->Initialize(Position[0]+i*opts.fMove[0], Position[1]+i*opts.fMove[1], Position[2]+i*opts.fMove[2], m_nCurStep, m_nCurFrame); pLast->SetColorIndex(pPiece->mColorIndex); - pLast->ChangeKey(1, false, false, param, LC_PK_ROTATION); - pLast->ChangeKey(1, true, false, param, LC_PK_ROTATION); + pLast->ChangeKey(1, false, false, AxisAngle, LC_PK_ROTATION); + pLast->ChangeKey(1, true, false, AxisAngle, LC_PK_ROTATION); } if (opts.nArrayDimension == 0) @@ -5052,10 +5049,10 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) else pLast = pFirst = new Piece(pPiece->mPieceInfo); - pLast->Initialize(pos[0]+i*opts.fMove[0]+j*opts.f2D[0], pos[1]+i*opts.fMove[1]+j*opts.f2D[1], pos[2]+i*opts.fMove[2]+j*opts.f2D[2], m_nCurStep, m_nCurFrame); + pLast->Initialize(Position[0]+i*opts.fMove[0]+j*opts.f2D[0], Position[1]+i*opts.fMove[1]+j*opts.f2D[1], Position[2]+i*opts.fMove[2]+j*opts.f2D[2], m_nCurStep, m_nCurFrame); pLast->SetColorIndex(pPiece->mColorIndex); - pLast->ChangeKey(1, false, false, param, LC_PK_ROTATION); - pLast->ChangeKey(1, true, false, param, LC_PK_ROTATION); + pLast->ChangeKey(1, false, false, AxisAngle, LC_PK_ROTATION); + pLast->ChangeKey(1, true, false, AxisAngle, LC_PK_ROTATION); } if (opts.nArrayDimension == 1) @@ -5071,10 +5068,10 @@ void Project::HandleCommand(LC_COMMANDS id, unsigned long nParam) else pLast = pFirst = new Piece(pPiece->mPieceInfo); - pLast->Initialize(pos[0]+i*opts.fMove[0]+j*opts.f2D[0]+k*opts.f3D[0], pos[1]+i*opts.fMove[1]+j*opts.f2D[1]+k*opts.f3D[1], pos[2]+i*opts.fMove[2]+j*opts.f2D[2]+k*opts.f3D[2], m_nCurStep, m_nCurFrame); + pLast->Initialize(Position[0]+i*opts.fMove[0]+j*opts.f2D[0]+k*opts.f3D[0], Position[1]+i*opts.fMove[1]+j*opts.f2D[1]+k*opts.f3D[1], Position[2]+i*opts.fMove[2]+j*opts.f2D[2]+k*opts.f3D[2], m_nCurStep, m_nCurFrame); pLast->SetColorIndex(pPiece->mColorIndex); - pLast->ChangeKey(1, false, false, param, LC_PK_ROTATION); - pLast->ChangeKey(1, true, false, param, LC_PK_ROTATION); + pLast->ChangeKey(1, false, false, AxisAngle, LC_PK_ROTATION); + pLast->ChangeKey(1, true, false, AxisAngle, LC_PK_ROTATION); } } } @@ -6310,15 +6307,8 @@ void Project::GetPieceInsertPosition(Piece* OffsetPiece, lcVector3& Position, lc lcVector3 Dist(0, 0, OffsetPiece->mPieceInfo->m_fDimensions[2] - m_pCurPiece->m_fDimensions[5]); SnapVector(Dist); - lcVector3 pos = OffsetPiece->mPosition; - const lcVector4& rot = OffsetPiece->mRotation; - - Matrix mat(rot, pos); - mat.Translate(Dist[0], Dist[1], Dist[2]); - mat.GetTranslation(pos); - - Position = lcVector3(pos[0], pos[1], pos[2]); - Rotation = lcVector4(rot[0], rot[1], rot[2], rot[3]); + Position = lcMul31(Dist, OffsetPiece->mModelWorld); + Rotation = OffsetPiece->mRotation; } // Try to find a good starting position/orientation for a new piece. diff --git a/common/project.h b/common/project.h index 2d99f4d9..691d2726 100644 --- a/common/project.h +++ b/common/project.h @@ -7,14 +7,14 @@ #include "array.h" #include "lc_math.h" -typedef enum +enum LC_MOUSE_TRACK { LC_TRACK_NONE, LC_TRACK_START_LEFT, LC_TRACK_LEFT, LC_TRACK_START_RIGHT, LC_TRACK_RIGHT -} LC_MOUSE_TRACK; +}; // Mouse control overlays. enum LC_OVERLAY_MODES @@ -48,7 +48,6 @@ class Light; class Group; class Terrain; class PieceInfo; -class Matrix; class View; class Image; class TexFont; @@ -303,7 +302,7 @@ protected: bool DoFileSave(); bool FileLoad(lcFile* file, bool bUndo, bool bMerge); void FileSave(lcFile* file, bool bUndo); - void FileReadLDraw(lcFile* file, Matrix* prevmat, int* nOk, int DefColor, int* nStep, PtrArray& FileArray); + void FileReadLDraw(lcFile* file, const lcMatrix44& CurrentTransform, int* nOk, int DefColor, int* nStep, PtrArray& FileArray); void FileReadMPD(lcFile& MPD, PtrArray& FileArray) const; public: diff --git a/win/leocad.vcxproj b/win/leocad.vcxproj index 7267065f..d277189c 100644 --- a/win/leocad.vcxproj +++ b/win/leocad.vcxproj @@ -188,7 +188,6 @@ - ../common;../win;../win/zlib @@ -385,7 +384,6 @@ - diff --git a/win/leocad.vcxproj.filters b/win/leocad.vcxproj.filters index 78439b32..cd638b34 100644 --- a/win/leocad.vcxproj.filters +++ b/win/leocad.vcxproj.filters @@ -212,9 +212,6 @@ Common Source Files - - Common Source Files - Common Source Files @@ -747,9 +744,6 @@ Common Header Files - - Common Header Files - Common Header Files