From c2199eb6b02ebca4975a932a97ef8937cdcee782 Mon Sep 17 00:00:00 2001 From: Leonardo Zide Date: Sun, 1 Oct 2023 16:24:42 -0700 Subject: [PATCH] Added serialization helper functions. --- common/camera.cpp | 46 ++++----------- common/light.cpp | 142 +++++++++++++--------------------------------- common/object.cpp | 117 +++++++++++++++++++++++--------------- common/object.h | 9 ++- common/piece.cpp | 4 +- 5 files changed, 129 insertions(+), 189 deletions(-) diff --git a/common/camera.cpp b/common/camera.cpp index ae8a0a15..ca820514 100644 --- a/common/camera.cpp +++ b/common/camera.cpp @@ -142,20 +142,9 @@ void lcCamera::SaveLDraw(QTextStream& Stream) const Stream << QLatin1String("0 !LEOCAD CAMERA FOV ") << m_fovy << QLatin1String(" ZNEAR ") << m_zNear << QLatin1String(" ZFAR ") << m_zFar << LineEnding; - if (mPositionKeys.GetSize() > 1) - mPositionKeys.SaveKeysLDraw(Stream, "CAMERA POSITION_KEY "); - else - Stream << QLatin1String("0 !LEOCAD CAMERA POSITION ") << mPosition[0] << ' ' << mPosition[1] << ' ' << mPosition[2] << LineEnding; - - if (mTargetPositionKeys.GetSize() > 1) - mTargetPositionKeys.SaveKeysLDraw(Stream, "CAMERA TARGET_POSITION_KEY "); - else - Stream << QLatin1String("0 !LEOCAD CAMERA TARGET_POSITION ") << mTargetPosition[0] << ' ' << mTargetPosition[1] << ' ' << mTargetPosition[2] << LineEnding; - - if (mUpVectorKeys.GetSize() > 1) - mUpVectorKeys.SaveKeysLDraw(Stream, "CAMERA UP_VECTOR_KEY "); - else - Stream << QLatin1String("0 !LEOCAD CAMERA UP_VECTOR ") << mUpVector[0] << ' ' << mUpVector[1] << ' ' << mUpVector[2] << LineEnding; + SaveAttribute(Stream, mPosition, mPositionKeys, "CAMERA", "POSITION"); + SaveAttribute(Stream, mTargetPosition, mTargetPositionKeys, "CAMERA", "TARGET_POSITION"); + SaveAttribute(Stream, mUpVector, mUpVectorKeys, "CAMERA", "UP_VECTOR"); Stream << QLatin1String("0 !LEOCAD CAMERA "); @@ -176,7 +165,7 @@ bool lcCamera::ParseLDrawLine(QTextStream& Stream) Stream >> Token; if (Token == QLatin1String("HIDDEN")) - SetHidden(true); + SetHidden(true); else if (Token == QLatin1String("ORTHOGRAPHIC")) SetOrtho(true); else if (Token == QLatin1String("FOV")) @@ -185,27 +174,12 @@ bool lcCamera::ParseLDrawLine(QTextStream& Stream) Stream >> m_zNear; else if (Token == QLatin1String("ZFAR")) Stream >> m_zFar; - else if (Token == QLatin1String("POSITION")) - { - Stream >> mPosition[0] >> mPosition[1] >> mPosition[2]; - mPositionKeys.ChangeKey(mPosition, 1, true); - } - else if (Token == QLatin1String("TARGET_POSITION")) - { - Stream >> mTargetPosition[0] >> mTargetPosition[1] >> mTargetPosition[2]; - mTargetPositionKeys.ChangeKey(mTargetPosition, 1, true); - } - else if (Token == QLatin1String("UP_VECTOR")) - { - Stream >> mUpVector[0] >> mUpVector[1] >> mUpVector[2]; - mUpVectorKeys.ChangeKey(mUpVector, 1, true); - } - else if (Token == QLatin1String("POSITION_KEY")) - mPositionKeys.LoadKeysLDraw(Stream); - else if (Token == QLatin1String("TARGET_POSITION_KEY")) - mTargetPositionKeys.LoadKeysLDraw(Stream); - else if (Token == QLatin1String("UP_VECTOR_KEY")) - mUpVectorKeys.LoadKeysLDraw(Stream); + else if (LoadAttribute(Stream, Token, mPosition, mPositionKeys, "POSITION")) + continue; + else if (LoadAttribute(Stream, Token, mTargetPosition, mTargetPositionKeys, "TARGET_POSITION")) + continue; + else if (LoadAttribute(Stream, Token, mUpVector, mUpVectorKeys, "UP_VECTOR")) + continue; else if (Token == QLatin1String("NAME")) { mName = Stream.readAll().trimmed(); diff --git a/common/light.cpp b/common/light.cpp index db09a6f2..0f3fe33f 100644 --- a/common/light.cpp +++ b/common/light.cpp @@ -144,52 +144,33 @@ void lcLight::SaveLDraw(QTextStream& Stream) const const float Numbers[12] = { Matrix[12], -Matrix[14], Matrix[13], Matrix[0], -Matrix[8], Matrix[4], -Matrix[2], Matrix[10], -Matrix[6], Matrix[1], -Matrix[9], Matrix[5] }; if (mPositionKeys.GetSize() > 1) - mPositionKeys.SaveKeysLDraw(Stream, "LIGHT POSITION_KEY "); + mPositionKeys.SaveKeysLDraw(Stream, "LIGHT", "POSITION"); else Stream << QLatin1String("0 !LEOCAD LIGHT POSITION ") << Numbers[0] << ' ' << Numbers[1] << ' ' << Numbers[2] << LineEnding; if (!IsPointLight()) { if (mRotationKeys.GetSize() > 1) - mRotationKeys.SaveKeysLDraw(Stream, "LIGHT ROTATION_KEY "); + mRotationKeys.SaveKeysLDraw(Stream, "LIGHT", "ROTATION"); else Stream << QLatin1String("0 !LEOCAD LIGHT ROTATION ") << Numbers[3] << ' ' << Numbers[4] << ' ' << Numbers[5] << ' ' << Numbers[6] << ' ' << Numbers[7] << ' ' << Numbers[8] << ' ' << Numbers[9] << ' ' << Numbers[10] << ' ' << Numbers[11] << LineEnding; } - if (mColorKeys.GetSize() > 1) - mColorKeys.SaveKeysLDraw(Stream, "LIGHT COLOR_KEY "); - else - Stream << QLatin1String("0 !LEOCAD LIGHT COLOR ") << mColor[0] << ' ' << mColor[1] << ' ' << mColor[2] << LineEnding; - - if (mSizeKeys.GetSize() > 1) - mSizeKeys.SaveKeysLDraw(Stream, "LIGHT SIZE_KEY "); - else - Stream << QLatin1String("0 !LEOCAD LIGHT SIZE ") << mSize[0] << mSize[1] << LineEnding; + SaveAttribute(Stream, mColor, mColorKeys, "LIGHT", "COLOR"); + SaveAttribute(Stream, mSize, mSizeKeys, "LIGHT", "SIZE"); + SaveAttribute(Stream, mPower, mPowerKeys, "LIGHT", "POWER"); if (!mPOVRayLight) { - if (mLightDiffuseKeys.GetSize() > 1) - mLightDiffuseKeys.SaveKeysLDraw(Stream, "LIGHT DIFFUSE_KEY "); - else - Stream << QLatin1String("0 !LEOCAD LIGHT DIFFUSE ") << mLightDiffuse << LineEnding; - - if (mLightSpecularKeys.GetSize() > 1) - mLightSpecularKeys.SaveKeysLDraw(Stream, "LIGHT SPECULAR_KEY "); - else - Stream << QLatin1String("0 !LEOCAD LIGHT SPECULAR ") << mLightSpecular << LineEnding; + SaveAttribute(Stream, mLightDiffuse, mLightDiffuseKeys, "LIGHT", "DIFFUSE"); + SaveAttribute(Stream, mLightSpecular, mLightSpecularKeys, "LIGHT", "SPECULAR"); } - if (mSpotExponentKeys.GetSize() > 1) - mSpotExponentKeys.SaveKeysLDraw(Stream, "LIGHT POWER_KEY "); - else - Stream << QLatin1String("0 !LEOCAD LIGHT POWER ") << (mPOVRayLight ? mPOVRayExponent : mSpotExponent) << LineEnding; +// SaveAttribute(Stream, (mPOVRayLight ? mPOVRayExponent : mSpotExponent), mSpotExponentKeys, "LIGHT", "POWER"); if (mEnableCutoff && !mPOVRayLight) { - if (mSpotCutoffKeys.GetSize() > 1) - mSpotCutoffKeys.SaveKeysLDraw(Stream, "LIGHT CUTOFF_DISTANCE_KEY "); - else - Stream << QLatin1String("0 !LEOCAD LIGHT CUTOFF_DISTANCE ") << mSpotCutoff << LineEnding; + SaveAttribute(Stream, mSpotCutoff, mSpotCutoffKeys, "LIGHT", "CUTOFF_DISTANCE"); } switch (mLightType) @@ -199,39 +180,18 @@ void lcLight::SaveLDraw(QTextStream& Stream) const break; case lcLightType::Spot: - if (mSpotConeAngleKeys.GetSize() > 1) - mSpotConeAngleKeys.SaveKeysLDraw(Stream, "LIGHT SPOT_CONE_ANGLE_KEY "); - else - Stream << QLatin1String("0 !LEOCAD LIGHT SPOT_CONE_ANGLE ") << mSpotConeAngle << LineEnding; - - if (mSpotPenumbraAngleKeys.GetSize() > 1) - mSpotPenumbraAngleKeys.SaveKeysLDraw(Stream, "LIGHT SPOT_PENUMBRA_ANGLE_KEY "); - else - Stream << QLatin1String("0 !LEOCAD LIGHT SPOT_PENUMBRA_ANGLE ") << mSpotPenumbraAngle << LineEnding; - - if (mSpotTightnessKeys.GetSize() > 1) - mSpotTightnessKeys.SaveKeysLDraw(Stream, "SPOT_TIGHTNESS_KEY "); - else - Stream << QLatin1String("0 !LEOCAD LIGHT SPOT_TIGHTNESS ") << mSpotTightness << LineEnding; - + SaveAttribute(Stream, mSpotConeAngle, mSpotConeAngleKeys, "LIGHT", "SPOT_CONE_ANGLE"); + SaveAttribute(Stream, mSpotPenumbraAngle, mSpotPenumbraAngleKeys, "LIGHT", "SPOT_PENUMBRA_ANGLE"); + SaveAttribute(Stream, mSpotTightness, mSpotTightnessKeys, "LIGHT", "SPOT_TIGHTNESS"); break; case lcLightType::Directional: - if (mSpotExponentKeys.GetSize() > 1) - mSpotExponentKeys.SaveKeysLDraw(Stream, "LIGHT STRENGTH_KEY "); - else - Stream << QLatin1String("0 !LEOCAD LIGHT STRENGTH ") << mSpotExponent << LineEnding; - + SaveAttribute(Stream, mSpotExponent, mSpotExponentKeys, "LIGHT", "STRENGTH"); break; case lcLightType::Area: if (mPOVRayLight) - { - if (mAreaGridKeys.GetSize() > 1) - mAreaGridKeys.SaveKeysLDraw(Stream, "LIGHT AREA_GRID_KEY "); - else - Stream << QLatin1String("0 !LEOCAD LIGHT AREA_ROWS ") << mAreaGrid[0] << QLatin1String(" AREA_COLUMNS ") << mAreaGrid[1] << LineEnding; - } + SaveAttribute(Stream, mAreaGrid, mAreaGridKeys, "LIGHT", "AREA_GRID"); Stream << QLatin1String("0 !LEOCAD LIGHT AREA_SHAPE ") << gLightAreaShapes[static_cast(mAreaShape)] << LineEnding; @@ -344,35 +304,18 @@ bool lcLight::ParseLDrawLine(QTextStream& Stream) } else if (Token == QLatin1String("ROTATION_KEY")) mRotationKeys.LoadKeysLDraw(Stream); // todo: convert from ldraw - else if (Token == QLatin1String("COLOR")) - { - Stream >> mColor[0] >> mColor[1] >> mColor[2]; - mColorKeys.ChangeKey(mColor, 1, true); - } - else if (Token == QLatin1String("COLOR_KEY")) - mColorKeys.LoadKeysLDraw(Stream); - else if (Token == QLatin1String("SPOT_CONE_ANGLE")) - { - Stream >> mSpotConeAngle; - mSpotConeAngleKeys.ChangeKey(mSpotConeAngle, 1, true); - } - else if (Token == QLatin1String("SPOT_CONE_ANGLE_KEY")) - mSpotConeAngleKeys.LoadKeysLDraw(Stream); - else if (Token == QLatin1String("SPOT_PENUMBRA_ANGLE")) - { - Stream >> mSpotPenumbraAngle; - mSpotPenumbraAngleKeys.ChangeKey(mSpotPenumbraAngle, 1, true); - } - else if (Token == QLatin1String("SPOT_PENUMBRA_ANGLE_KEY")) - mSpotPenumbraAngleKeys.LoadKeysLDraw(Stream); - else if (Token == QLatin1String("SPOT_TIGHTNESS")) - { - mPOVRayLight = true; - Stream >> mSpotTightness; - mSpotTightnessKeys.ChangeKey(mSpotTightness, 1, true); - } - else if (Token == QLatin1String("SPOT_TIGHTNESS_KEY")) - mSpotTightnessKeys.LoadKeysLDraw(Stream); + else if (LoadAttribute(Stream, Token, mColor, mColorKeys, "COLOR")) + continue; + else if (LoadAttribute(Stream, Token, mSize, mSizeKeys, "SIZE")) + continue; + else if (LoadAttribute(Stream, Token, mPower, mPowerKeys, "POWER")) + continue; + else if (LoadAttribute(Stream, Token, mSpotConeAngle, mSpotConeAngleKeys, "SPOT_CONE_ANGLE")) + continue; + else if (LoadAttribute(Stream, Token, mSpotPenumbraAngle, mSpotPenumbraAngleKeys, "SPOT_PENUMBRA_ANGLE")) + continue; + else if (LoadAttribute(Stream, Token, mSpotTightness, mSpotTightnessKeys, "SPOT_TIGHTNESS")) + continue; else if (Token == QLatin1String("AREA_SHAPE")) { QString AreaShape; @@ -387,27 +330,20 @@ bool lcLight::ParseLDrawLine(QTextStream& Stream) } } } - else if (Token == QLatin1String("SIZE")) - { - Stream >> mSize[0] >> mSize[1]; - mSizeKeys.ChangeKey(mSize, 1, true); - } - else if (Token == QLatin1String("SIZE_KEY")) - mSizeKeys.LoadKeysLDraw(Stream); - else if (Token == QLatin1String("POWER") || Token == QLatin1String("STRENGTH")) - { - if (mPOVRayLight) - { - Stream >> mPOVRayExponent; - mSpotExponentKeys.ChangeKey(mPOVRayExponent, 1, true); - } - else - { - Stream >> mSpotExponent; - mSpotExponentKeys.ChangeKey(mSpotExponent, 1, true); - } - } +// else if (Token == QLatin1String("POWER") || Token == QLatin1String("STRENGTH")) +// { +// if (mPOVRayLight) +// { +// Stream >> mPOVRayExponent; +// mSpotExponentKeys.ChangeKey(mPOVRayExponent, 1, true); +// } +// else +// { +// Stream >> mSpotExponent; +// mSpotExponentKeys.ChangeKey(mSpotExponent, 1, true); +// } +// } else if (Token == QLatin1String("AREA_ROWS")) { mPOVRayLight = true; diff --git a/common/object.cpp b/common/object.cpp index c8eedc93..fefc71f6 100644 --- a/common/object.cpp +++ b/common/object.cpp @@ -1,6 +1,22 @@ #include "lc_global.h" #include "object.h" +#define LC_OBJECT_ATTRIBUTE(T) \ + template void lcObjectKeyArray::SaveKeysLDraw(QTextStream& Stream, const char* ObjectName, const char* VariableName) const; \ + template void lcObjectKeyArray::LoadKeysLDraw(QTextStream& Stream); \ + template const T& lcObjectKeyArray::CalculateKey(lcStep Step) const; \ + template void lcObjectKeyArray::ChangeKey(const T& Value, lcStep Step, bool AddKey); \ + template void lcObjectKeyArray::InsertTime(lcStep Start, lcStep Time); \ + template void lcObjectKeyArray::RemoveTime(lcStep Start, lcStep Time); \ + template void lcObject::SaveAttribute(QTextStream& Stream, const T& Variable, const lcObjectKeyArray& Keys, const char* ObjectName, const char* VariableName) const; \ + template bool lcObject::LoadAttribute(QTextStream& Stream, const QString& Token, T& Variable, lcObjectKeyArray& Keys, const char* VariableName) + +LC_OBJECT_ATTRIBUTE(float); +LC_OBJECT_ATTRIBUTE(lcVector2); +LC_OBJECT_ATTRIBUTE(lcVector3); +LC_OBJECT_ATTRIBUTE(lcVector4); +LC_OBJECT_ATTRIBUTE(lcMatrix33); + lcObject::lcObject(lcObjectType ObjectType) : mObjectType(ObjectType) { @@ -10,59 +26,32 @@ lcObject::~lcObject() { } -template void lcObjectKeyArray::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const; -template void lcObjectKeyArray::LoadKeysLDraw(QTextStream& Stream); -template const float& lcObjectKeyArray::CalculateKey(lcStep Step) const; -template void lcObjectKeyArray::ChangeKey(const float& Value, lcStep Step, bool AddKey); -template void lcObjectKeyArray::InsertTime(lcStep Start, lcStep Time); -template void lcObjectKeyArray::RemoveTime(lcStep Start, lcStep Time); - -template void lcObjectKeyArray::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const; -template void lcObjectKeyArray::LoadKeysLDraw(QTextStream& Stream); -template const lcVector3& lcObjectKeyArray::CalculateKey(lcStep Step) const; -template void lcObjectKeyArray::ChangeKey(const lcVector3& Value, lcStep Step, bool AddKey); -template void lcObjectKeyArray::InsertTime(lcStep Start, lcStep Time); -template void lcObjectKeyArray::RemoveTime(lcStep Start, lcStep Time); - -template void lcObjectKeyArray::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const; -template void lcObjectKeyArray::LoadKeysLDraw(QTextStream& Stream); -template const lcVector4& lcObjectKeyArray::CalculateKey(lcStep Step) const; -template void lcObjectKeyArray::ChangeKey(const lcVector4& Value, lcStep Step, bool AddKey); -template void lcObjectKeyArray::InsertTime(lcStep Start, lcStep Time); -template void lcObjectKeyArray::RemoveTime(lcStep Start, lcStep Time); - -template void lcObjectKeyArray::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const; -template void lcObjectKeyArray::LoadKeysLDraw(QTextStream& Stream); -template const lcMatrix33& lcObjectKeyArray::CalculateKey(lcStep Step) const; -template void lcObjectKeyArray::ChangeKey(const lcMatrix33& Value, lcStep Step, bool AddKey); -template void lcObjectKeyArray::InsertTime(lcStep Start, lcStep Time); -template void lcObjectKeyArray::RemoveTime(lcStep Start, lcStep Time); - -template void lcObjectKeyArray::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const; -template void lcObjectKeyArray::LoadKeysLDraw(QTextStream& Stream); -template const int& lcObjectKeyArray::CalculateKey(lcStep Step) const; -template void lcObjectKeyArray::ChangeKey(const int& Value, lcStep Step, bool AddKey); -template void lcObjectKeyArray::InsertTime(lcStep Start, lcStep Time); -template void lcObjectKeyArray::RemoveTime(lcStep Start, lcStep Time); - -template void lcObjectKeyArray::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const; -template void lcObjectKeyArray::LoadKeysLDraw(QTextStream& Stream); -template const lcVector2& lcObjectKeyArray::CalculateKey(lcStep Step) const; -template void lcObjectKeyArray::ChangeKey(const lcVector2& Value, lcStep Step, bool AddKey); -template void lcObjectKeyArray::InsertTime(lcStep Start, lcStep Time); -template void lcObjectKeyArray::RemoveTime(lcStep Start, lcStep Time); - template -void lcObjectKeyArray::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const +static void SaveFloatValue(QTextStream& Stream, const T& Value) { constexpr int Count = sizeof(T) / sizeof(float); + for (int ValueIndex = 0; ValueIndex < Count; ValueIndex++) + Stream << ((const float*)&Value)[ValueIndex] << ' '; +} + +template +static void LoadFloatValue(QTextStream& Stream, T& Value) +{ + constexpr int Count = sizeof(T) / sizeof(float); + + for (int ValueIdx = 0; ValueIdx < Count; ValueIdx++) + Stream >> ((float*)&Value)[ValueIdx]; +} + +template +void lcObjectKeyArray::SaveKeysLDraw(QTextStream& Stream, const char* ObjectName, const char* VariableName) const +{ for (const lcObjectKey& Key : mKeys) { - Stream << QLatin1String("0 !LEOCAD ") << KeyName << Key.Step << ' '; + Stream << QLatin1String("0 !LEOCAD ") << ObjectName << ' ' << VariableName << "_KEY " << Key.Step << ' '; - for (int ValueIdx = 0; ValueIdx < Count; ValueIdx++) - Stream << ((float*)&Key.Value)[ValueIdx] << ' '; + SaveFloatValue(Stream, Key.Value); Stream << QLatin1String("\r\n"); } @@ -182,3 +171,39 @@ void lcObjectKeyArray::RemoveTime(lcStep Start, lcStep Time) KeyIt++; } } + +template +void lcObject::SaveAttribute(QTextStream& Stream, const T& Variable, const lcObjectKeyArray& Keys, const char* ObjectName, const char* VariableName) const +{ + if (Keys.GetSize() == 1) + { + Stream << QLatin1String("0 !LEOCAD ") << ObjectName << ' ' << VariableName << ' '; + + SaveFloatValue(Stream, Variable); + + Stream << QLatin1String("\r\n"); + } + else + Keys.SaveKeysLDraw(Stream, ObjectName, VariableName); +} + +template +bool lcObject::LoadAttribute(QTextStream& Stream, const QString& Token, T& Variable, lcObjectKeyArray& Keys, const char* VariableName) +{ + if (Token == VariableName) + { + LoadFloatValue(Stream, Variable); + Keys.ChangeKey(Variable, 1, true); + + return true; + } + + if (Token.endsWith(QLatin1String("_KEY")) && Token.leftRef(Token.size() - 4) == VariableName) + { + Keys.LoadKeysLDraw(Stream); + + return true; + } + + return false; +} diff --git a/common/object.h b/common/object.h index d4694f10..92af6cd2 100644 --- a/common/object.h +++ b/common/object.h @@ -36,7 +36,7 @@ public: mKeys.clear(); } - void SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const; + void SaveKeysLDraw(QTextStream& Stream, const char* ObjectName, const char* VariableName) const; void LoadKeysLDraw(QTextStream& Stream); const T& CalculateKey(lcStep Step) const; void ChangeKey(const T& Value, lcStep Step, bool AddKey); @@ -141,7 +141,12 @@ public: virtual void RemoveKeyFrames() = 0; virtual QString GetName() const = 0; +protected: + template + void SaveAttribute(QTextStream& Stream, const T& Variable, const lcObjectKeyArray& Keys, const char* ObjectName, const char* VariableName) const; + template + bool LoadAttribute(QTextStream& Stream, const QString& Token, T& Variable, lcObjectKeyArray& Keys, const char* VariableName); + private: lcObjectType mObjectType; }; - diff --git a/common/piece.cpp b/common/piece.cpp index 6c392d72..89cee77e 100644 --- a/common/piece.cpp +++ b/common/piece.cpp @@ -125,10 +125,10 @@ void lcPiece::SaveLDraw(QTextStream& Stream) const } if (mPositionKeys.GetSize() > 1) - mPositionKeys.SaveKeysLDraw(Stream, "PIECE POSITION_KEY "); + mPositionKeys.SaveKeysLDraw(Stream, "PIECE", "POSITION"); if (mRotationKeys.GetSize() > 1) - mRotationKeys.SaveKeysLDraw(Stream, "PIECE ROTATION_KEY "); + mRotationKeys.SaveKeysLDraw(Stream, "PIECE", "ROTATION"); Stream << "1 " << mColorCode << ' ';