Files
CDAG/Research/scene/Octree/EdgeMaterialNode.h

95 lines
3.1 KiB
C++

#pragma once
#include <fstream>
#include "Node.h"
#include "BaseTree.h"
#include "../../core/Util/SmallDynamicArray.h"
#include "../../inc/glm/glm.hpp"
#include "../../core/Defines.h"
template<typename T, typename Comparer = std::less<T>>
class EdgeMaterialNode : public Node
{
private:
SmallDynamicArray<T> mEdgeMaterials;
public:
EdgeMaterialNode(BaseTree* root, unsigned8 level = 0) : Node(root, level), mEdgeMaterials(SmallDynamicArray<T>()) {}
//EdgeMaterialNode(const EdgeMaterialNode<T>& node) : Node(node) { SetEdgeMaterials(node->GetEdgeMaterials()); }
EdgeMaterialNode(EdgeMaterialNode&& node) : Node(std::move(node))
{
mEdgeMaterials = std::move(node.mEdgeMaterials);
}
~EdgeMaterialNode() {}
EdgeMaterialNode& operator=(EdgeMaterialNode&& node)
{
mEdgeMaterials = std::move(node.mEdgeMaterials);
Node::operator=(std::move(node));
return *this;
}
// Takes a pointer to an array in memory containing at least as many entries as there are children for this node.
// Copies that array as the edge materials for this node
void SetEdgeMaterials(T* edgeMaterials)
{
mEdgeMaterials.Clear();
unsigned8 childCount = GetChildCount();
mEdgeMaterials.Resize(0, childCount);
mEdgeMaterials.SetRange(edgeMaterials, 0, childCount);
}
T* GetEdgeMaterials() const { return &mEdgeMaterials[0]; }
T GetEdgeMaterial(ChildIndex child) const { return mEdgeMaterials[GetChildmask().GetSetBefore(child)]; }
void SetEdgeMaterial(ChildIndex child) const { assert(GetChildmask().Get(child)); mEdgeMaterials[GetChildmask().GetSetBefore(child)]; }
bool Compare(const EdgeMaterialNode& node) const
{
if (!mEdgeMaterials.IsEmpty() && !node.mEdgeMaterials.IsEmpty())
{
// DEBUG: Compare the shifts.
// This should lead to the same results as comparing the pointers but you never know
for (unsigned8 i = 0; i < GetChildCount(); i++)
{
if (node.mEdgeMaterials[i] != this->mEdgeMaterials[i])
return node.mEdgeMaterials[i] < this->mEdgeMaterials[i];
}
}
return Node::Compare(node);
}
bool Equals(const EdgeMaterialNode& node) const
{
if (!Node::Equals(node))
return false;
if (this->mEdgeMaterials.IsEmpty() || node.mEdgeMaterials.IsEmpty())
return this->mEdgeMaterials.IsEmpty() && node.mEdgeMaterials.IsEmpty();
else
{
// DEBUG: Check if the shifts are equal
// This should lead to the same results as comparing the pointers but you never know
for (unsigned8 i = 0; i < GetChildmask().GetSet(); i++)
{
if (node.mEdgeMaterials[i] != this->mEdgeMaterials[i])
return false;
}
}
return true;
}
void WriteProperties(std::ostream& file)
{
assert(!mEdgeMaterials.IsEmpty() || GetChildCount() == 0);
Serializer<T*>::Serialize(&mEdgeMaterials[0], GetChildCount(), file);
Node::WriteProperties(file);
}
void ReadProperties(std::istream& file)
{
mEdgeMaterials.Clear();
mEdgeMaterials.Resize(0, GetChildCount());
Serializer<T*>::Deserialize(&mEdgeMaterials[0], GetChildCount(), file);
Node::ReadProperties(file);
}
void CopyProperties(EdgeMaterialNode* source)
{
SetEdgeMaterials(source->GetEdgeMaterials());
}
};