-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClump.cpp
153 lines (132 loc) · 4.25 KB
/
Clump.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// https://github.com/rwengine/openrw/blob/master/rwcore/data/Clump.cpp
#include "Clump.hpp"
#include <algorithm>
#include <limits>
#include <queue>
#include <glm/gtc/matrix_transform.hpp>
ModelFrame::ModelFrame(unsigned int index, glm::mat3 dR, glm::vec3 dT)
: index(index)
, defaultRotation(dR)
, defaultTranslation(dT)
, parent_(nullptr) {
reset();
}
void ModelFrame::reset() {
matrix = glm::translate(glm::mat4(1.0f), defaultTranslation) *
glm::mat4(defaultRotation);
updateHierarchyTransform();
}
void ModelFrame::updateHierarchyTransform() {
// Update our own transformation
if (parent_) {
worldtransform_ = parent_->getWorldTransform() * matrix;
} else {
worldtransform_ = matrix;
}
for (const auto& child : children_) {
child->updateHierarchyTransform();
}
}
void ModelFrame::addChild(const ModelFramePtr& child) {
// 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);
child->updateHierarchyTransform();
}
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;
}
}
return nullptr;
}
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;
}
AtomicPtr Atomic::clone(const ModelFramePtr& newFrame) const {
auto newatomic = std::make_shared<Atomic>();
newatomic->setGeometry(getGeometry());
newatomic->setFrame(newFrame ? newFrame : getFrame());
newatomic->setFlags(getFlags());
return newatomic;
}
ModelFrame* Clump::findFrame(const std::string& name) const {
if (rootframe_->getName() == name) {
return rootframe_.get();
}
return rootframe_->findDescendant(name);
}
Clump::~Clump() = default;
void Clump::recalculateMetrics() {
boundingRadius = std::numeric_limits<float>::min();
for (const auto& atomic : atomics_) {
const auto& geometry = atomic->getGeometry();
if (!geometry) {
continue;
}
const auto& bounds = geometry->geometryBounds;
boundingRadius = std::max(boundingRadius,
glm::length(bounds.center) + bounds.radius);
}
}
ClumpPtr Clump::clone() const {
// Clone frame hierarchy
auto newroot = rootframe_->cloneHierarchy();
auto clump = std::make_shared<Clump>();
clump->setFrame(newroot);
clump->boundingRadius = boundingRadius;
// 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;
}
#ifdef __EMSCRIPTEN__
#include <emscripten/bind.h>
using namespace emscripten;
EMSCRIPTEN_BINDINGS(Clump) {
class_<Clump>("Clump")
.smart_ptr_constructor("Clump", &std::make_shared<Clump>)
;
}
#endif