leocad/common/object.cpp

210 lines
5 KiB
C++
Raw Normal View History

#include "lc_global.h"
2011-09-07 23:06:51 +02:00
#include "object.h"
2023-10-02 01:24:42 +02:00
#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);
2014-05-01 20:42:11 +02:00
lcObject::lcObject(lcObjectType ObjectType)
2014-08-31 02:53:12 +02:00
: mObjectType(ObjectType)
2011-09-07 23:06:51 +02:00
{
}
2014-05-01 20:42:11 +02:00
lcObject::~lcObject()
2011-09-07 23:06:51 +02:00
{
}
2023-10-02 01:24:42 +02:00
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] << ' ';
}
2023-08-07 11:31:33 +02:00
template<typename T>
2023-10-02 01:24:42 +02:00
static void LoadFloatValue(QTextStream& Stream, T& Value)
{
2021-11-15 03:34:24 +01:00
constexpr int Count = sizeof(T) / sizeof(float);
2023-10-02 01:24:42 +02:00
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)
{
2023-10-02 01:24:42 +02:00
Stream << QLatin1String("0 !LEOCAD ") << ObjectName << ' ' << VariableName << "_KEY " << Key.Step << ' ';
2023-10-02 01:24:42 +02:00
SaveFloatValue(Stream, Key.Value);
Stream << QLatin1String("\r\n");
}
}
template<typename T>
void lcObjectKeyArray<T>::LoadKeysLDraw(QTextStream& Stream)
{
QString Token;
Stream >> Token;
2021-11-15 03:34:24 +01:00
const int Step = Token.toInt();
T Value;
2021-11-15 03:34:24 +01:00
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)
{
2021-01-06 00:19:53 +01:00
for (typename std::vector<lcObjectKey<T>>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end(); KeyIt++)
{
2021-01-05 21:27:45 +01:00
if (KeyIt->Step < Step)
continue;
2021-01-05 21:27:45 +01:00
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
{
2021-01-05 21:27:45 +01:00
KeyIt = KeyIt - 1;
KeyIt->Value = Value;
}
2021-01-05 21:27:45 +01:00
return;
}
2021-01-05 21:27:45 +01:00
if (AddKey || mKeys.empty())
mKeys.emplace_back(lcObjectKey<T>{ Step, Value });
else
2021-01-05 21:27:45 +01:00
mKeys.back().Value = Value;
}
template<typename T>
void lcObjectKeyArray<T>::InsertTime(lcStep Start, lcStep Time)
{
bool EndKey = false;
2021-01-06 00:19:53 +01:00
for (typename std::vector<lcObjectKey<T>>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end();)
{
2021-01-05 21:27:45 +01:00
if ((KeyIt->Step < Start) || (KeyIt->Step == 1))
{
KeyIt++;
continue;
2021-01-05 21:27:45 +01:00
}
if (EndKey)
{
2021-01-05 21:27:45 +01:00
KeyIt = mKeys.erase(KeyIt);
continue;
}
2021-01-05 21:27:45 +01:00
if (KeyIt->Step >= LC_STEP_MAX - Time)
{
2021-01-05 21:27:45 +01:00
KeyIt->Step = LC_STEP_MAX;
EndKey = true;
}
else
2021-01-05 21:27:45 +01:00
KeyIt->Step += Time;
KeyIt++;
}
}
template<typename T>
void lcObjectKeyArray<T>::RemoveTime(lcStep Start, lcStep Time)
{
2021-01-06 00:19:53 +01:00
for (typename std::vector<lcObjectKey<T>>::iterator KeyIt = mKeys.begin(); KeyIt != mKeys.end();)
{
2021-01-05 21:27:45 +01:00
if ((KeyIt->Step < Start) || (KeyIt->Step == 1))
{
KeyIt++;
continue;
2021-01-05 21:27:45 +01:00
}
2021-01-05 21:27:45 +01:00
if (KeyIt->Step < Start + Time)
{
2021-01-05 21:27:45 +01:00
KeyIt = mKeys.erase(KeyIt);
continue;
}
2021-01-05 21:27:45 +01:00
KeyIt->Step -= Time;
KeyIt++;
}
}
2023-10-02 01:24:42 +02:00
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;
}