2017-01-03 15:18:06 +01:00
|
|
|
#include "data/Clump.hpp"
|
2017-10-30 23:44:40 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <limits>
|
|
|
|
#include <memory>
|
2017-01-05 02:26:37 +01:00
|
|
|
#include <queue>
|
2014-02-09 04:14:43 +01:00
|
|
|
|
|
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
|
|
|
2018-02-10 15:23:04 +01:00
|
|
|
Geometry::Geometry() : EBO(0), flags(0) {
|
2013-09-25 10:05:18 +02:00
|
|
|
}
|
|
|
|
|
2017-01-03 17:21:21 +01:00
|
|
|
Geometry::~Geometry() {
|
2018-02-10 15:23:04 +01:00
|
|
|
if (EBO) {
|
|
|
|
glDeleteBuffers(1, &EBO);
|
|
|
|
}
|
2013-09-25 10:05:18 +02:00
|
|
|
}
|
2014-02-09 04:14:43 +01:00
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
ModelFrame::ModelFrame(unsigned int index, glm::mat3 dR, glm::vec3 dT)
|
2016-09-09 22:13:21 +02:00
|
|
|
: index(index)
|
|
|
|
, defaultRotation(dR)
|
|
|
|
, defaultTranslation(dT)
|
2017-01-04 22:08:21 +01:00
|
|
|
, parent_(nullptr) {
|
2016-09-09 22:13:21 +02:00
|
|
|
reset();
|
2014-02-09 04:14:43 +01:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:21 +02:00
|
|
|
void ModelFrame::reset() {
|
2018-02-06 20:47:31 +01:00
|
|
|
matrix = glm::translate(glm::mat4(1.0f), defaultTranslation) *
|
2016-09-09 22:13:21 +02:00
|
|
|
glm::mat4(defaultRotation);
|
2017-01-04 22:08:21 +01:00
|
|
|
updateHierarchyTransform();
|
2014-02-09 04:14:43 +01:00
|
|
|
}
|
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
void ModelFrame::updateHierarchyTransform() {
|
|
|
|
// Update our own transformation
|
|
|
|
if (parent_) {
|
|
|
|
worldtransform_ = parent_->getWorldTransform() * matrix;
|
|
|
|
} else {
|
|
|
|
worldtransform_ = matrix;
|
|
|
|
}
|
|
|
|
for (const auto& child : children_) {
|
|
|
|
child->updateHierarchyTransform();
|
|
|
|
}
|
2014-06-08 23:40:46 +02:00
|
|
|
}
|
2014-08-04 23:21:01 +02:00
|
|
|
|
2018-05-16 19:13:11 +02:00
|
|
|
void ModelFrame::addChild(const ModelFramePtr& child) {
|
2017-01-04 22:08:21 +01:00
|
|
|
// Make sure the child is an orphan
|
|
|
|
if (child->getParent()) {
|
|
|
|
auto& other_children = child->getParent()->children_;
|
|
|
|
other_children.erase(
|
|
|
|
std::remove(other_children.begin(), other_children.end(), child),
|
|
|
|
other_children.end());
|
|
|
|
}
|
|
|
|
child->parent_ = this;
|
|
|
|
children_.push_back(child);
|
2018-01-27 19:09:33 +01:00
|
|
|
child->updateHierarchyTransform();
|
2017-01-04 22:08:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ModelFrame* ModelFrame::findDescendant(const std::string& name) const {
|
|
|
|
for (const auto& frame : children_) {
|
|
|
|
if (frame->getName() == name) {
|
|
|
|
return frame.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
auto result = frame->findDescendant(name);
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
2016-09-09 22:13:21 +02:00
|
|
|
}
|
2017-01-04 22:08:21 +01:00
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-01-05 02:26:37 +01:00
|
|
|
ModelFramePtr ModelFrame::cloneHierarchy() const {
|
|
|
|
auto self = std::make_shared<ModelFrame>(getIndex(), getDefaultRotation(),
|
|
|
|
getDefaultTranslation());
|
|
|
|
self->setName(getName());
|
|
|
|
for (const auto& child : getChildren()) {
|
|
|
|
auto childclone = child->cloneHierarchy();
|
|
|
|
self->addChild(childclone);
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2019-05-16 00:28:09 +02:00
|
|
|
AtomicPtr Atomic::clone(const ModelFramePtr& newFrame) const {
|
2017-01-05 02:26:37 +01:00
|
|
|
auto newatomic = std::make_shared<Atomic>();
|
|
|
|
newatomic->setGeometry(getGeometry());
|
2019-05-16 00:28:09 +02:00
|
|
|
newatomic->setFrame(newFrame ? newFrame : getFrame());
|
2017-01-05 02:26:37 +01:00
|
|
|
newatomic->setFlags(getFlags());
|
|
|
|
return newatomic;
|
|
|
|
}
|
|
|
|
|
2017-01-04 22:08:21 +01:00
|
|
|
ModelFrame* Clump::findFrame(const std::string& name) const {
|
|
|
|
if (rootframe_->getName() == name) {
|
|
|
|
return rootframe_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
return rootframe_->findDescendant(name);
|
|
|
|
}
|
|
|
|
|
2018-02-18 01:06:39 +01:00
|
|
|
Clump::~Clump() = default;
|
2015-04-06 05:06:35 +02:00
|
|
|
|
2017-01-03 15:18:06 +01:00
|
|
|
void Clump::recalculateMetrics() {
|
2016-09-09 22:13:21 +02:00
|
|
|
boundingRadius = std::numeric_limits<float>::min();
|
2017-01-04 22:08:21 +01:00
|
|
|
for (const auto& atomic : atomics_) {
|
|
|
|
const auto& geometry = atomic->getGeometry();
|
|
|
|
if (!geometry) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
const auto& bounds = geometry->geometryBounds;
|
2016-09-09 22:13:21 +02:00
|
|
|
boundingRadius = std::max(boundingRadius,
|
|
|
|
glm::length(bounds.center) + bounds.radius);
|
|
|
|
}
|
2015-04-06 05:06:35 +02:00
|
|
|
}
|
2017-01-05 02:26:37 +01:00
|
|
|
|
2018-08-25 17:31:22 +02:00
|
|
|
ClumpPtr Clump::clone() const {
|
2017-01-05 02:26:37 +01:00
|
|
|
// Clone frame hierarchy
|
|
|
|
auto newroot = rootframe_->cloneHierarchy();
|
2018-08-25 17:31:22 +02:00
|
|
|
auto clump = std::make_shared<Clump>();
|
2017-01-05 02:26:37 +01:00
|
|
|
clump->setFrame(newroot);
|
2019-05-11 21:33:23 +02:00
|
|
|
clump->boundingRadius = boundingRadius;
|
2017-01-05 02:26:37 +01:00
|
|
|
|
|
|
|
// This isn't the most optimal implementation, but this code is likely
|
|
|
|
// to be replaced soon.
|
|
|
|
auto find_new_frame = [&](const ModelFramePtr& old) -> ModelFramePtr {
|
|
|
|
std::queue<ModelFramePtr> open;
|
|
|
|
open.push(newroot);
|
|
|
|
while (!open.empty()) {
|
|
|
|
auto frame = open.front();
|
|
|
|
open.pop();
|
|
|
|
if (frame->getIndex() == old->getIndex()) {
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
for (const auto& child : frame->getChildren()) {
|
|
|
|
open.push(child);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Generate new atomics
|
|
|
|
for (const auto& atomic : getAtomics()) {
|
|
|
|
auto newatomic = atomic->clone();
|
|
|
|
// Replace the original frame with the cloned frame
|
|
|
|
if (atomic->getFrame()) {
|
|
|
|
newatomic->setFrame(find_new_frame(atomic->getFrame()));
|
|
|
|
}
|
|
|
|
clump->addAtomic(newatomic);
|
|
|
|
}
|
|
|
|
|
|
|
|
return clump;
|
|
|
|
}
|