#include "gpuCacheGeometry.h"
#include <algorithm>
#include <cassert>
#include <limits>
#include <vector>
namespace {
using namespace GPUCache;
class NodeDataValidator : public SubNodeVisitor
{
public:
NodeDataValidator() : fIsValid(false) {}
bool isValid() const { return fIsValid; }
void visit(const XformData& xform,
const SubNode& ) override
{
fIsValid = !xform.getSamples().empty();
}
void visit(const ShapeData& shape,
const SubNode& ) override
{
fIsValid = !shape.getSamples().empty();
}
private:
bool fIsValid;
};
}
namespace GPUCache {
SubNodeData::~SubNodeData()
{}
struct ShapeData::MakeSharedEnabler: public ShapeData {
MakeSharedEnabler() = default;
};
ShapeData::ShapeData()
{}
ShapeData::~ShapeData()
{}
const std::shared_ptr<const ShapeSample>&
ShapeData::getSample(double seconds) const
{
assert(!fSamples.empty());
if (fSamples.empty()) {
static const std::shared_ptr<const ShapeSample> null;
return null;
}
SampleMap::const_iterator it = fSamples.upper_bound(seconds);
if (it != fSamples.begin()) {
--it;
}
return it->second;
}
void ShapeData::addSample(
const std::shared_ptr<const ShapeSample>& sample)
{
fSamples[sample->timeInSeconds()] = sample;
}
void ShapeData::accept(SubNodeVisitor& visitor,
const SubNode& subNode) const
{
return visitor.visit(*this, subNode);
}
void ShapeData::setMaterial(
const MString& material)
{
assert(fMaterials.empty());
fMaterials.push_back(material);
}
void ShapeData::setMaterials(const std::vector<MString>& materials)
{
assert(fMaterials.empty());
fMaterials = materials;
}
const std::vector<MString>& ShapeData::getMaterials() const
{
return fMaterials;
}
ShapeData::MPtr ShapeData::create() {
return std::make_shared<MakeSharedEnabler>();
}
struct XformData::MakeSharedEnabler: public XformData {
MakeSharedEnabler() = default;
};
XformData::~XformData()
{}
const std::shared_ptr<const XformSample>&
XformData::getSample(double seconds) const
{
assert(!fSamples.empty());
if (fSamples.empty()) {
static const std::shared_ptr<const XformSample> null;
return null;
}
SampleMap::const_iterator it = fSamples.upper_bound(seconds);
if (it != fSamples.begin()) {
--it;
}
return it->second;
}
void XformData::accept(SubNodeVisitor& visitor,
const SubNode& subNode) const
{
return visitor.visit(*this, subNode);
}
XformData::MPtr XformData::create() {
return std::make_shared<MakeSharedEnabler>();
}
SubNodeVisitor::~SubNodeVisitor()
{}
struct SubNode::MakeSharedEnabler: public SubNode {
MakeSharedEnabler(
const SubNodeData::Ptr& nodeData
): SubNode(name, nodeData){}
};
SubNode::SubNode(
const SubNodeData::Ptr& nodeData)
: fName(name), fNodeData(nodeData), fTransparentType(kOpaque)
{
NodeDataValidator validator;
nodeData->accept(validator, *this);
assert(validator.isValid());
}
SubNode::~SubNode()
{}
SubNode::MPtr SubNode::create(
const MString& name,
const SubNodeData::Ptr& nodeData) {
return std::make_shared<MakeSharedEnabler>(name, nodeData);
}
}