PeriDyno 1.0.0
Loading...
Searching...
No Matches
RigidMesh.cu
Go to the documentation of this file.
1#include "RigidMesh.h"
2
3#include <GLSurfaceVisualModule.h>
4
5namespace dyno
6{
7 template<typename TDataType>
8 RigidMesh<TDataType>::RigidMesh()
9 : RigidBody<TDataType>()
10 {
11 this->stateEnvelope()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
12 this->stateInitialEnvelope()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
13
14 this->stateMesh()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
15 this->stateInitialMesh()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
16
17 this->varDensity()->setRange(1.0f, 10000.0f);
18
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);
23
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);
28 }
29
30 template<typename TDataType>
31 RigidMesh<TDataType>::~RigidMesh()
32 {
33
34 }
35
36 template<typename TDataType>
37 void RigidMesh<TDataType>::resetStates()
38 {
39 Coord location = this->varLocation()->getData();
40 Coord rot = this->varRotation()->getData();
41 Coord scale = this->varScale()->getData();
42
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));
46
47 Coord center(0);
48
49 if (this->varEnvelopeName()->getValue() != "") {
50 auto initEnvlope = this->stateInitialEnvelope()->getDataPtr();
51
52 initEnvlope->loadObjFile(this->varEnvelopeName()->getValue().string());
53
54 auto points = initEnvlope->getPoints();
55
56 Reduction<Coord> reduce;
57 Coord lo = reduce.minimum(points.begin(), points.size());
58 Coord hi = reduce.maximum(points.begin(), points.size());
59
60 center = 0.5f * (hi + lo);
61
62 initEnvlope->translate(-center);
63
64 auto curEnvlope = this->stateEnvelope()->getDataPtr();
65 curEnvlope->copyFrom(*initEnvlope);
66 curEnvlope->scale(scale);
67 curEnvlope->rotate(quat);
68 curEnvlope->translate(location);
69
70 Real lx = hi.x - lo.x;
71 Real ly = hi.y - lo.y;
72 Real lz = hi.z - lo.z;
73
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);
80
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);
88 }
89
90 if (this->varMeshName()->getValue() != "") {
91 auto initMesh = this->stateInitialMesh()->getDataPtr();
92
93 initMesh->loadObjFile(this->varMeshName()->getValue().string());
94 initMesh->translate(-center);
95
96 auto curMesh = this->stateMesh()->getDataPtr();
97 curMesh->copyFrom(*initMesh);
98 curMesh->scale(scale);
99 curMesh->rotate(quat);
100 curMesh->translate(location);
101 }
102
103 RigidBody<TDataType>::resetStates();
104 }
105
106 template<typename TDataType>
107 void RigidMesh<TDataType>::updateStates()
108 {
109 RigidBody<TDataType>::updateStates();
110
111 auto center = this->stateCenter()->getData();
112 auto quat = this->stateQuaternion()->getData();
113 auto scale = this->varScale()->getData();
114
115 auto envlope = this->stateEnvelope()->getDataPtr();
116 envlope->copyFrom(this->stateInitialEnvelope()->getData());
117 envlope->rotate(quat);
118 envlope->scale(scale);
119 envlope->translate(center);
120
121 auto mesh = this->stateMesh()->getDataPtr();
122 mesh->copyFrom(this->stateInitialMesh()->getData());
123 mesh->rotate(quat);
124 mesh->scale(scale);
125 mesh->translate(center);
126 }
127
128 template<typename TDataType>
129 void RigidMesh<TDataType>::transform()
130 {
131 Coord location = this->varLocation()->getData();
132 Coord rot = this->varRotation()->getData();
133 Coord scale = this->varScale()->getData();
134
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));
138
139 if (this->varEnvelopeName()->getValue().string() != "") {
140 auto initEnvlope = this->stateInitialEnvelope()->getDataPtr();
141
142 auto curEnvlope = this->stateEnvelope()->getDataPtr();
143 curEnvlope->copyFrom(*initEnvlope);
144 curEnvlope->scale(scale);
145 curEnvlope->rotate(quat);
146 curEnvlope->translate(location);
147 }
148
149 if (this->varMeshName()->getValue().string() != "") {
150 auto initMesh = this->stateInitialMesh()->getDataPtr();
151
152 auto curMesh = this->stateMesh()->getDataPtr();
153 curMesh->copyFrom(*initMesh);
154 curMesh->scale(scale);
155 curMesh->rotate(quat);
156 curMesh->translate(location);
157 }
158 }
159
160
161 DEFINE_CLASS(RigidMesh);
162}