leocad/common/object.cpp
2023-11-19 18:05:43 -08:00

210 lines
5 KiB
C++

#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(lcVector2i);
LC_OBJECT_ATTRIBUTE(lcVector2);
LC_OBJECT_ATTRIBUTE(lcVector3);
LC_OBJECT_ATTRIBUTE(lcVector4);
LC_OBJECT_ATTRIBUTE(lcMatrix33);
lcObject::lcObject(lcObjectType ObjectType)
: mObjectType(ObjectType)
{
}
lcObject::~lcObject()
{
}
template<typename T>
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<typename T>
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<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");
}
}
template<typename T>
void lcObjectKeyArray<T>::LoadKeysLDraw(QTextStream& Stream)
{
QString Token;
Stream >> Token;
const int Step = Token.toInt();
T Value;
constexpr int Count = sizeof(T) / sizeof(float);
for (int ValueIdx = 0; ValueIdx < Count; ValueIdx++)
Stream >> ((float*)&Value)[ValueIdx];
ChangeKey(Value, Step, true);
}
template<typename T>
const T& lcObjectKeyArray<T>::CalculateKey(lcStep Step) const
{
const lcObjectKey<T>* PreviousKey = &mKeys[0];
for (const lcObjectKey<T>& Key : mKeys)
{
if (Key.Step > Step)
break;
PreviousKey = &Key;
}
return PreviousKey->Value;
}
template<typename T>
void lcObjectKeyArray<T>::ChangeKey(const T& Value, lcStep Step, bool AddKey)
{
for (typename std::vector<lcObjectKey<T>>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end(); KeyIt++)
{
if (KeyIt->Step < Step)
continue;
if (KeyIt->Step == Step)
KeyIt->Value = Value;
else if (AddKey)
mKeys.insert(KeyIt, lcObjectKey<T>{ Step, Value });
else if (KeyIt == mKeys.begin())
KeyIt->Value = Value;
else
{
KeyIt = KeyIt - 1;
KeyIt->Value = Value;
}
return;
}
if (AddKey || mKeys.empty())
mKeys.emplace_back(lcObjectKey<T>{ Step, Value });
else
mKeys.back().Value = Value;
}
template<typename T>
void lcObjectKeyArray<T>::InsertTime(lcStep Start, lcStep Time)
{
bool EndKey = false;
for (typename std::vector<lcObjectKey<T>>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end();)
{
if ((KeyIt->Step < Start) || (KeyIt->Step == 1))
{
KeyIt++;
continue;
}
if (EndKey)
{
KeyIt = mKeys.erase(KeyIt);
continue;
}
if (KeyIt->Step >= LC_STEP_MAX - Time)
{
KeyIt->Step = LC_STEP_MAX;
EndKey = true;
}
else
KeyIt->Step += Time;
KeyIt++;
}
}
template<typename T>
void lcObjectKeyArray<T>::RemoveTime(lcStep Start, lcStep Time)
{
for (typename std::vector<lcObjectKey<T>>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end();)
{
if ((KeyIt->Step < Start) || (KeyIt->Step == 1))
{
KeyIt++;
continue;
}
if (KeyIt->Step < Start + Time)
{
KeyIt = mKeys.erase(KeyIt);
continue;
}
KeyIt->Step -= 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;
}