PeriDyno 1.2.1
Loading...
Searching...
No Matches
Vessel.cpp
Go to the documentation of this file.
1#include "Vessel.h"
2
3#include "Quat.h"
4
7
8#include "GltfFunc.h"
9
10namespace dyno
11{
12 template<typename TDataType>
14 : RigidBody<TDataType>()
15 {
16 this->varDensity()->setRange(1.0f, 10000.0f);
17
18 auto callback = std::make_shared<FCallBackFunc>(std::bind(&Vessel<TDataType>::transform, this));
19 this->varLocation()->attach(callback);
20 this->varScale()->attach(callback);
21 this->varRotation()->attach(callback);
22
23 auto EnvelopeRender = std::make_shared<GLSurfaceVisualModule>();
24 EnvelopeRender->setColor(Color(0.8f, 0.8f, 0.8f));
25 this->stateEnvelope()->promoteOuput()->connect(EnvelopeRender->inTriangleSet());
26 this->graphicsPipeline()->pushModule(EnvelopeRender);
27 EnvelopeRender->setVisible(false);
28
29
30 auto texMeshRender = std::make_shared<GLPhotorealisticInstanceRender>();
31 this->stateTextureMesh()->connect(texMeshRender->inTextureMesh());
32 this->stateInstanceTransform()->connect(texMeshRender->inTransform());
33 this->graphicsPipeline()->pushModule(texMeshRender);
34
35
36 auto evenlopeLoader = std::make_shared<FCallBackFunc>(
37 [=]() {
38 std::string name = this->varEnvelopeName()->getValue().string();
39 bool succeed = mInitialEnvelope.loadObjFile(name);
40
41 if (succeed)
42 {
43 if (this->stateEnvelope()->isEmpty())
44 this->stateEnvelope()->allocate();
45
46 auto envelope = this->stateEnvelope()->getDataPtr();
47
48 envelope->copyFrom(mInitialEnvelope);
49
50 envelope->scale(this->varScale()->getValue());
51 envelope->rotate(this->varRotation()->getValue() * M_PI / 180);
52 envelope->translate(this->varLocation()->getValue());
53 }
54
55
56 }
57 );
58 evenlopeLoader->update();
59
60 this->varEnvelopeName()->attach(evenlopeLoader);
61
62 auto textureMeshLoader = std::make_shared<FCallBackFunc>(
63 [=]() {
64 std::string filepath = this->varTextureMeshName()->getValue().string();
65 if (this->stateTextureMesh()->isEmpty())
66 this->stateTextureMesh()->allocate();
67
68 std::shared_ptr<TextureMesh> texMesh = this->stateTextureMesh()->getDataPtr();
69 loadGLTFTextureMesh(texMesh, filepath);
70 }
71 );
72
73 this->varTextureMeshName()->attach(textureMeshLoader);
74
75 this->varDensity()->setValue(150.0f);
76 this->varBarycenterOffset()->setValue(Vec3f(0.0f, 0.0f, -0.5f));
77 }
78
79 template<typename TDataType>
84
85 template<typename TDataType>
87 {
88 NBoundingBox box;
89
90 mInitialEnvelope.requestBoundingBox(box.lower, box.upper);
91
92 return box;
93 }
94
95 template<typename TDataType>
97 {
98 if (this->stateEnvelope()->isEmpty()) this->stateEnvelope()->allocate();
99 if (this->stateTextureMesh()->isEmpty()) this->stateTextureMesh()->allocate();
100
101 this->transform();
102
103 auto texMesh = this->stateTextureMesh()->constDataPtr();
104
105 //Initialize states for the rigid body
106 {
107 Coord lo;
108 Coord hi;
109
110 if (mInitialEnvelope.isEmpty())
111 return;
112
113 mInitialEnvelope.requestBoundingBox(lo, hi);
114
115 Coord scale = this->varScale()->getValue();
116
117 mShapeCenter = 0.5f * (hi + lo);
118
119
120 Real lx = hi.x - lo.x;
121 Real ly = hi.y - lo.y;
122 Real lz = hi.z - lo.z;
123
124 Real rho = this->varDensity()->getData();
125 Real mass = rho * lx * ly * lz;
126
127 //Calculate mass using the bounding box
128 Matrix inertia = 1.0f / 12.0f * mass
129 * Mat3f(ly * ly + lz * lz, 0, 0,
130 0, lx * lx + lz * lz, 0,
131 0, 0, lx * lx + ly * ly);
132
133
134 Coord location = this->varLocation()->getValue();
135 Coord rot = this->varRotation()->getValue();
136
137 auto quat = this->computeQuaternion();
138 auto offset = this->varBarycenterOffset()->getValue();
139
140 this->stateMass()->setValue(mass);
141 this->stateCenter()->setValue(location + mShapeCenter);
142 this->stateBarycenter()->setValue(location + mShapeCenter + quat.rotate(offset));
143 this->stateVelocity()->setValue(Vec3f(0));
144 this->stateAngularVelocity()->setValue(Vec3f(0));
145 this->stateInertia()->setValue(inertia);
146 this->stateQuaternion()->setValue(quat);
147 this->stateInitialInertia()->setValue(inertia);
148 }
149
151 }
152
153 template<typename TDataType>
155 {
157
158 auto center = this->stateCenter()->getValue();
159 auto quat = this->stateQuaternion()->getValue();
160 auto scale = this->varScale()->getValue();
161
162 auto offset = this->varBarycenterOffset()->getValue();
163
164 this->stateBarycenter()->setValue(center + quat.rotate(offset));
165
166 auto buoy = this->stateEnvelope()->getDataPtr();
167 buoy->copyFrom(mInitialEnvelope);
168 buoy->rotate(quat);
169 buoy->scale(scale);
170 buoy->translate(center - mShapeCenter);
171
172 auto texMesh = this->stateTextureMesh()->getDataPtr();
173 {
174
175 uint N = texMesh->shapes().size();
176
178 tms.assign(this->stateInstanceTransform()->constData());
179
180 for (uint i = 0; i < tms.size(); i++)
181 {
182 auto& list = tms[i];
183 for (uint j = 0; j < list.size(); j++)
184 {
185 list[j].translation() = center + quat.rotate(texMesh->shapes()[i]->boundingTransform.translation() * scale) - mShapeCenter; //
186 list[j].rotation() = quat.toMatrix3x3();
187 list[j].scale() = scale;
188 }
189
190 }
191
192 auto instantanceTransform = this->stateInstanceTransform()->getDataPtr();
193 instantanceTransform->assign(tms);
194
195 tms.clear();
196
197 }
198 }
199
200 template<typename TDataType>
202 {
203
204 Coord location = this->varLocation()->getValue();
205 Coord rot = this->varRotation()->getValue();
206 Coord scale = this->varScale()->getValue();
207
208 auto quat = this->computeQuaternion();
209
210 auto envelope = this->stateEnvelope()->getDataPtr();
211 envelope->copyFrom(mInitialEnvelope);
212 envelope->scale(scale);
213 envelope->rotate(quat);
214 envelope->translate(location);
215
216 if (this->stateTextureMesh()->isEmpty())
217 return;
218
219 auto texMesh = this->stateTextureMesh()->constDataPtr();
220 {
221 uint N = texMesh->shapes().size();
222
224 tms.resize(N, 1);
225
226 for (uint i = 0; i < N; i++)
227 {
228 Transform3f t = texMesh->shapes()[i]->boundingTransform;
229
230 tms[i].insert(Transform3f(t.translation() * scale + location, quat.toMatrix3x3(), t.scale() * scale));
231 }
232
233 if (this->stateInstanceTransform()->isEmpty())
234 {
235 this->stateInstanceTransform()->allocate();
236 }
237
238 auto instantanceTransform = this->stateInstanceTransform()->getDataPtr();
239 instantanceTransform->assign(tms);
240
241 tms.clear();
242 }
243 }
244
246}
#define DEFINE_CLASS(name)
Definition Object.h:140
Implementation of quaternion.
#define M_PI
Definition Typedef.inl:36
std::shared_ptr< GraphicsPipeline > graphicsPipeline()
Definition Node.cpp:311
virtual void resetStates()
Definition Node.cpp:205
Quat< Real > computeQuaternion()
void updateStates() override
Definition RigidBody.cpp:22
TDataType::Matrix Matrix
Definition Vessel.h:33
NBoundingBox boundingBox() override
Definition Vessel.cpp:86
~Vessel() override
Definition Vessel.cpp:80
void resetStates() override
Definition Vessel.cpp:96
void transform()
Definition Vessel.cpp:201
void updateStates() override
Definition Vessel.cpp:154
TDataType::Coord Coord
Definition Vessel.h:32
TriangleSet< TDataType > mInitialEnvelope
Definition Vessel.h:67
TDataType::Real Real
Definition Vessel.h:31
Coord mShapeCenter
Definition Vessel.h:69
#define N(x, y, z)
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
SquareMatrix< float, 3 > Mat3f
Definition Matrix3x3.h:92
ArrayList< T, DeviceType::CPU > CArrayList
Definition ArrayList.h:207
void loadGLTFTextureMesh(std::shared_ptr< TextureMesh > texMesh, const std::string &filepath)
Transform< float, 3 > Transform3f
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
unsigned int uint
Definition VkProgram.h:14
vgm::Quat quat
Definition vgMath.h:633