Added serialization helper functions.

This commit is contained in:
Leonardo Zide 2023-10-01 16:24:42 -07:00
parent ff313d4675
commit c2199eb6b0
5 changed files with 129 additions and 189 deletions

View file

@ -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 ");
@ -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();

View file

@ -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<int>(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;

View file

@ -1,6 +1,22 @@
#include "lc_global.h"
#include "object.h"
#define LC_OBJECT_ATTRIBUTE(T) \
template void lcObjectKeyArray<T>::SaveKeysLDraw(QTextStream& Stream, const char* ObjectName, const char* VariableName) const; \
template void lcObjectKeyArray<T>::LoadKeysLDraw(QTextStream& Stream); \
template const T& lcObjectKeyArray<T>::CalculateKey(lcStep Step) const; \
template void lcObjectKeyArray<T>::ChangeKey(const T& Value, lcStep Step, bool AddKey); \
template void lcObjectKeyArray<T>::InsertTime(lcStep Start, lcStep Time); \
template void lcObjectKeyArray<T>::RemoveTime(lcStep Start, lcStep Time); \
template void lcObject::SaveAttribute<T>(QTextStream& Stream, const T& Variable, const lcObjectKeyArray<T>& Keys, const char* ObjectName, const char* VariableName) const; \
template bool lcObject::LoadAttribute<T>(QTextStream& Stream, const QString& Token, T& Variable, lcObjectKeyArray<T>& 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<float>::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const;
template void lcObjectKeyArray<float>::LoadKeysLDraw(QTextStream& Stream);
template const float& lcObjectKeyArray<float>::CalculateKey(lcStep Step) const;
template void lcObjectKeyArray<float>::ChangeKey(const float& Value, lcStep Step, bool AddKey);
template void lcObjectKeyArray<float>::InsertTime(lcStep Start, lcStep Time);
template void lcObjectKeyArray<float>::RemoveTime(lcStep Start, lcStep Time);
template void lcObjectKeyArray<lcVector3>::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const;
template void lcObjectKeyArray<lcVector3>::LoadKeysLDraw(QTextStream& Stream);
template const lcVector3& lcObjectKeyArray<lcVector3>::CalculateKey(lcStep Step) const;
template void lcObjectKeyArray<lcVector3>::ChangeKey(const lcVector3& Value, lcStep Step, bool AddKey);
template void lcObjectKeyArray<lcVector3>::InsertTime(lcStep Start, lcStep Time);
template void lcObjectKeyArray<lcVector3>::RemoveTime(lcStep Start, lcStep Time);
template void lcObjectKeyArray<lcVector4>::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const;
template void lcObjectKeyArray<lcVector4>::LoadKeysLDraw(QTextStream& Stream);
template const lcVector4& lcObjectKeyArray<lcVector4>::CalculateKey(lcStep Step) const;
template void lcObjectKeyArray<lcVector4>::ChangeKey(const lcVector4& Value, lcStep Step, bool AddKey);
template void lcObjectKeyArray<lcVector4>::InsertTime(lcStep Start, lcStep Time);
template void lcObjectKeyArray<lcVector4>::RemoveTime(lcStep Start, lcStep Time);
template void lcObjectKeyArray<lcMatrix33>::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const;
template void lcObjectKeyArray<lcMatrix33>::LoadKeysLDraw(QTextStream& Stream);
template const lcMatrix33& lcObjectKeyArray<lcMatrix33>::CalculateKey(lcStep Step) const;
template void lcObjectKeyArray<lcMatrix33>::ChangeKey(const lcMatrix33& Value, lcStep Step, bool AddKey);
template void lcObjectKeyArray<lcMatrix33>::InsertTime(lcStep Start, lcStep Time);
template void lcObjectKeyArray<lcMatrix33>::RemoveTime(lcStep Start, lcStep Time);
template void lcObjectKeyArray<int>::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const;
template void lcObjectKeyArray<int>::LoadKeysLDraw(QTextStream& Stream);
template const int& lcObjectKeyArray<int>::CalculateKey(lcStep Step) const;
template void lcObjectKeyArray<int>::ChangeKey(const int& Value, lcStep Step, bool AddKey);
template void lcObjectKeyArray<int>::InsertTime(lcStep Start, lcStep Time);
template void lcObjectKeyArray<int>::RemoveTime(lcStep Start, lcStep Time);
template void lcObjectKeyArray<lcVector2>::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const;
template void lcObjectKeyArray<lcVector2>::LoadKeysLDraw(QTextStream& Stream);
template const lcVector2& lcObjectKeyArray<lcVector2>::CalculateKey(lcStep Step) const;
template void lcObjectKeyArray<lcVector2>::ChangeKey(const lcVector2& Value, lcStep Step, bool AddKey);
template void lcObjectKeyArray<lcVector2>::InsertTime(lcStep Start, lcStep Time);
template void lcObjectKeyArray<lcVector2>::RemoveTime(lcStep Start, lcStep Time);
template<typename T>
void lcObjectKeyArray<T>::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const
static void SaveFloatValue(QTextStream& Stream, const T& Value)
{
constexpr int Count = sizeof(T) / sizeof(float);
for (const lcObjectKey<T>& Key : mKeys)
for (int ValueIndex = 0; ValueIndex < Count; ValueIndex++)
Stream << ((const float*)&Value)[ValueIndex] << ' ';
}
template<typename T>
static void LoadFloatValue(QTextStream& Stream, T& Value)
{
Stream << QLatin1String("0 !LEOCAD ") << KeyName << Key.Step << ' ';
constexpr int Count = sizeof(T) / sizeof(float);
for (int ValueIdx = 0; ValueIdx < Count; ValueIdx++)
Stream << ((float*)&Key.Value)[ValueIdx] << ' ';
Stream >> ((float*)&Value)[ValueIdx];
}
template<typename T>
void lcObjectKeyArray<T>::SaveKeysLDraw(QTextStream& Stream, const char* ObjectName, const char* VariableName) const
{
for (const lcObjectKey<T>& Key : mKeys)
{
Stream << QLatin1String("0 !LEOCAD ") << ObjectName << ' ' << VariableName << "_KEY " << Key.Step << ' ';
SaveFloatValue(Stream, Key.Value);
Stream << QLatin1String("\r\n");
}
@ -182,3 +171,39 @@ void lcObjectKeyArray<T>::RemoveTime(lcStep Start, lcStep Time)
KeyIt++;
}
}
template<typename T>
void lcObject::SaveAttribute(QTextStream& Stream, const T& Variable, const lcObjectKeyArray<T>& 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<typename T>
bool lcObject::LoadAttribute(QTextStream& Stream, const QString& Token, T& Variable, lcObjectKeyArray<T>& 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;
}

View file

@ -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<typename T>
void SaveAttribute(QTextStream& Stream, const T& Variable, const lcObjectKeyArray<T>& Keys, const char* ObjectName, const char* VariableName) const;
template<typename T>
bool LoadAttribute(QTextStream& Stream, const QString& Token, T& Variable, lcObjectKeyArray<T>& Keys, const char* VariableName);
private:
lcObjectType mObjectType;
};

View file

@ -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 << ' ';