3#include <GLSurfaceVisualModule.h>
7 template<typename TDataType>
8 RigidMesh<TDataType>::RigidMesh()
9 : RigidBody<TDataType>()
11 this->stateEnvelope()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
12 this->stateInitialEnvelope()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
14 this->stateMesh()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
15 this->stateInitialMesh()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
17 this->varDensity()->setRange(1.0f, 10000.0f);
19 auto callback = std::make_shared<FCallBackFunc>(std::bind(&RigidMesh<TDataType>::transform, this));
20 this->varLocation()->attach(callback);
21 this->varScale()->attach(callback);
22 this->varRotation()->attach(callback);
24 auto rigidMeshRender = std::make_shared<GLSurfaceVisualModule>();
25 rigidMeshRender->setColor(Color(0.8f, 0.8f, 0.8f));
26 this->stateMesh()->promoteOuput()->connect(rigidMeshRender->inTriangleSet());
27 this->graphicsPipeline()->pushModule(rigidMeshRender);
30 template<typename TDataType>
31 RigidMesh<TDataType>::~RigidMesh()
36 template<typename TDataType>
37 void RigidMesh<TDataType>::resetStates()
39 Coord location = this->varLocation()->getData();
40 Coord rot = this->varRotation()->getData();
41 Coord scale = this->varScale()->getData();
43 dyno::Quat<Real> quat = dyno::Quat<Real>(M_PI * rot[0] / 180, Coord(1, 0, 0))
44 * dyno::Quat<Real>(M_PI * rot[1] / 180, Coord(0, 1, 0))
45 * dyno::Quat<Real>(M_PI * rot[2] / 180, Coord(0, 0, 1));
49 if (this->varEnvelopeName()->getValue() != "") {
50 auto initEnvlope = this->stateInitialEnvelope()->getDataPtr();
52 initEnvlope->loadObjFile(this->varEnvelopeName()->getValue().string());
54 auto points = initEnvlope->getPoints();
56 Reduction<Coord> reduce;
57 Coord lo = reduce.minimum(points.begin(), points.size());
58 Coord hi = reduce.maximum(points.begin(), points.size());
60 center = 0.5f * (hi + lo);
62 initEnvlope->translate(-center);
64 auto curEnvlope = this->stateEnvelope()->getDataPtr();
65 curEnvlope->copyFrom(*initEnvlope);
66 curEnvlope->scale(scale);
67 curEnvlope->rotate(quat);
68 curEnvlope->translate(location);
70 Real lx = hi.x - lo.x;
71 Real ly = hi.y - lo.y;
72 Real lz = hi.z - lo.z;
74 Real rho = this->varDensity()->getData();
75 Real mass = rho * lx * ly * lz;
76 Matrix inertia = 1.0f / 12.0f * mass
77 * Mat3f(ly * ly + lz * lz, 0, 0,
78 0, lx * lx + lz * lz, 0,
79 0, 0, lx * lx + ly * ly);
81 this->stateMass()->setValue(mass);
82 this->stateCenter()->setValue(location);
83 this->stateVelocity()->setValue(Vec3f(0));
84 this->stateAngularVelocity()->setValue(Vec3f(0));
85 this->stateInertia()->setValue(inertia);
86 this->stateQuaternion()->setValue(quat);
87 this->stateInitialInertia()->setValue(inertia);
90 if (this->varMeshName()->getValue() != "") {
91 auto initMesh = this->stateInitialMesh()->getDataPtr();
93 initMesh->loadObjFile(this->varMeshName()->getValue().string());
94 initMesh->translate(-center);
96 auto curMesh = this->stateMesh()->getDataPtr();
97 curMesh->copyFrom(*initMesh);
98 curMesh->scale(scale);
99 curMesh->rotate(quat);
100 curMesh->translate(location);
103 RigidBody<TDataType>::resetStates();
106 template<typename TDataType>
107 void RigidMesh<TDataType>::updateStates()
109 RigidBody<TDataType>::updateStates();
111 auto center = this->stateCenter()->getData();
112 auto quat = this->stateQuaternion()->getData();
113 auto scale = this->varScale()->getData();
115 auto envlope = this->stateEnvelope()->getDataPtr();
116 envlope->copyFrom(this->stateInitialEnvelope()->getData());
117 envlope->rotate(quat);
118 envlope->scale(scale);
119 envlope->translate(center);
121 auto mesh = this->stateMesh()->getDataPtr();
122 mesh->copyFrom(this->stateInitialMesh()->getData());
125 mesh->translate(center);
128 template<typename TDataType>
129 void RigidMesh<TDataType>::transform()
131 Coord location = this->varLocation()->getData();
132 Coord rot = this->varRotation()->getData();
133 Coord scale = this->varScale()->getData();
135 dyno::Quat<Real> quat = dyno::Quat<Real>(M_PI * rot[0] / 180, Coord(1, 0, 0))
136 * dyno::Quat<Real>(M_PI * rot[1] / 180, Coord(0, 1, 0))
137 * dyno::Quat<Real>(M_PI * rot[2] / 180, Coord(0, 0, 1));
139 if (this->varEnvelopeName()->getValue().string() != "") {
140 auto initEnvlope = this->stateInitialEnvelope()->getDataPtr();
142 auto curEnvlope = this->stateEnvelope()->getDataPtr();
143 curEnvlope->copyFrom(*initEnvlope);
144 curEnvlope->scale(scale);
145 curEnvlope->rotate(quat);
146 curEnvlope->translate(location);
149 if (this->varMeshName()->getValue().string() != "") {
150 auto initMesh = this->stateInitialMesh()->getDataPtr();
152 auto curMesh = this->stateMesh()->getDataPtr();
153 curMesh->copyFrom(*initMesh);
154 curMesh->scale(scale);
155 curMesh->rotate(quat);
156 curMesh->translate(location);
161 DEFINE_CLASS(RigidMesh);