leocad/common/object.cpp

171 lines
4.8 KiB
C++
Raw Normal View History

#include "lc_global.h"
2011-09-07 23:06:51 +02:00
#include "object.h"
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
{
}
2021-01-06 00:43:23 +01:00
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<typename T>
void lcObjectKeyArray<T>::SaveKeysLDraw(QTextStream& Stream, const char* KeyName) const
{
2021-11-15 03:34:24 +01:00
constexpr int Count = sizeof(T) / sizeof(float);
for (const lcObjectKey<T>& Key : mKeys)
{
Stream << QLatin1String("0 !LEOCAD ") << KeyName << Key.Step << ' ';
for (int ValueIdx = 0; ValueIdx < Count; ValueIdx++)
Stream << ((float*)&Key.Value)[ValueIdx] << ' ';
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++;
}
}