PeriDyno 1.2.1
Loading...
Searching...
No Matches
CylinderModel.cpp
Go to the documentation of this file.
1#include "CylinderModel.h"
2
5
6#include "Mapping/Extract.h"
7
8
9namespace dyno
10{
11 template<typename TDataType>
13 : BasicShape<TDataType>()
14 {
15 this->varRow()->setRange(1, 500);
16 this->varColumns()->setRange(3, 500);
17 this->varRadius()->setRange(0.001f, 20.0f);
18 this->varHeight()->setRange(0.001f, 20.0f);
19 this->varEndSegment()->setRange(0, 500);
20
21 this->stateTriangleSet()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
22 this->statePolygonSet()->setDataPtr(std::make_shared<PolygonSet<TDataType>>());
23
24 auto callback = std::make_shared<FCallBackFunc>(std::bind(&CylinderModel<TDataType>::varChanged, this));
25
26 this->varLocation()->attach(callback);
27 this->varScale()->attach(callback);
28 this->varRotation()->attach(callback);
29
30 this->varColumns()->attach(callback);
31 this->varEndSegment()->attach(callback);
32 this->varRow()->attach(callback);
33 this->varRadius()->attach(callback);
34 this->varHeight()->attach(callback);
35
36 auto tsRender = std::make_shared<GLSurfaceVisualModule>();
37 this->stateTriangleSet()->connect(tsRender->inTriangleSet());
38 this->graphicsPipeline()->pushModule(tsRender);
39
40 auto exES = std::make_shared<ExtractEdgeSetFromPolygonSet<TDataType>>();
41 this->statePolygonSet()->connect(exES->inPolygonSet());
42 this->graphicsPipeline()->pushModule(exES);
43
44 auto esRender = std::make_shared<GLWireframeVisualModule>();
45 esRender->varBaseColor()->setValue(Color(0, 0, 0));
46 exES->outEdgeSet()->connect(esRender->inEdgeSet());
47 this->graphicsPipeline()->pushModule(esRender);
48
49 this->statePolygonSet()->promoteOuput();
50 this->stateTriangleSet()->promoteOuput();
51
52 }
53
54 template<typename TDataType>
59
60 template<typename TDataType>
62 {
63 auto center = this->varLocation()->getValue();
64 auto rot = this->varRotation()->getValue();
65 auto scale = this->varScale()->getValue();
66
67 auto radius = this->varRadius()->getValue();
68 auto row = this->varRow()->getValue();
69 auto columns = this->varColumns()->getValue();
70 auto height = this->varHeight()->getValue();
71 auto end_segment = this->varEndSegment()->getValue();
72
73 auto triangleSet = this->stateTriangleSet()->getDataPtr();
74 auto polySet = this->statePolygonSet()->getDataPtr();
75
76 std::vector<Coord> vertices;
77 std::vector<TopologyModule::Triangle> triangle;
78
79 this->varScale()->setValue(Coord(scale.x, scale.y, scale.x), false);
80
81 scale = this->varScale()->getValue();
82
83
84 Coord Location;
85 Real angle = M_PI / 180 * 360 / columns;
86 Real temp_angle = angle;
87
88 //Side Point
89 for (int k = 0; k <= row; k++)
90 {
91 Real tempy = height / row * k;
92
93 for (int j = 0; j < columns; j++) {
94
95 temp_angle = j * angle;
96
97 Location = { sin(temp_angle) * radius , tempy ,cos(temp_angle) * radius };
98
99 vertices.push_back(Location);
100 }
101 }
102
103 //Top_Buttom Point
104
105 int pt_side_len = vertices.size();
106
107 for (int i = 1; i < end_segment; i++)
108 {
109 float offset = i / (float(end_segment) - i);
110
111 for (int p = 0; p < columns; p++)
112 {
113 int top_start = pt_side_len - columns + p;
114
115 Coord toppt = { (vertices[top_start][0] + offset * 0) / (1 + offset), (vertices[top_start][1] + offset * height) / (1 + offset), (vertices[top_start][2] + offset * 0) / (1 + offset) };
116
117 vertices.push_back(toppt);
118 }
119
120 }
121
122 for (int i = 1; i < end_segment; i++)
123 {
124 float offset = i / (float(end_segment) - i);
125
126 for (int p = 0; p < columns; p++)
127 {
128 Coord buttompt = { (vertices[p][0] + offset * 0) / (1 + offset), (vertices[p][1] + offset * 0) / (1 + offset), (vertices[p][2] + offset * 0) / (1 + offset) };
129
130 vertices.push_back(buttompt);
131 }
132
133 }
134
135 vertices.push_back(Coord(0, 0, 0));
136 uint buttomCenter = vertices.size() - 1;
137 vertices.push_back(Coord(0, height, 0));
138 uint topCenter = vertices.size() - 1;
139
140
141 uint numOfPolygon = columns * row + 2 * columns * end_segment;
142 CArray<uint> counter(numOfPolygon);
143
144 uint incre = 0;
145 uint endQuadNum = ((int(end_segment) - 1) * int(columns) < 0 ? 0 : (int(end_segment) - 1) * int(columns));
146
147 uint QuadNum = columns * row + 2 * endQuadNum;
148 for (uint j = 0; j < QuadNum; j++)
149 {
150 counter[incre] = 4;
151 incre++;
152 }
153
154 uint TriangleNum = (int(end_segment) - 1) < 0 ? 0 : columns * 2;
155 for (uint j = 0; j < TriangleNum; j++)
156 {
157 counter[incre] = 3;
158 incre++;
159 }
160
161 CArrayList<uint> polygonIndices;
162 polygonIndices.resize(counter);
163
164
165 //side;
166 incre = 0;
167 for (uint i = 0; i < columns ; i++)
168 {
169 for (uint j = 0; j < row ; j++)
170 {
171 auto& index = polygonIndices[incre];
172
173 uint p1 = i + j * columns;
174 uint p2 = (i + 1) % columns + j * columns;
175 uint p3 = (i + 1) % columns + j * columns + columns;
176 uint p4 = i + j * columns + columns;
177
178
179 index.insert(p1);
180 index.insert(p2);
181 index.insert(p3);
182 index.insert(p4);
183
184 incre++;
185 }
186 }
187
188 //Top
189 if (end_segment > 0)
190 {
191 uint sidePtNum = columns * row;
192 for (uint i = 0; i < columns; i++)
193 {
194 for (uint j = 0; j < end_segment - 1; j++)
195 {
196 auto& index = polygonIndices[incre];
197
198 uint p1 = i + j * columns + (sidePtNum);
199 uint p2 = (i + 1) % columns + j * columns + (sidePtNum);
200 uint p3 = (i + 1) % columns + j * columns + columns + (sidePtNum);
201 uint p4 = i + j * columns + columns + (sidePtNum);
202
203
204 index.insert(p1);
205 index.insert(p2);
206 index.insert(p3);
207 index.insert(p4);
208
209 incre++;
210 }
211 }
212
213 //Buttom
214 uint sideTopPtNum = incre;
215 for (uint i = 0; i < columns; i++)
216 {
217 for (uint j = 0; j < end_segment - 1; j++)
218 {
219 auto& index = polygonIndices[incre];
220
221 uint temp = sideTopPtNum;
222 if (j == 0)
223 {
224 temp = 0;
225 }
226
227 uint p1 = i + j * columns + (temp);
228 uint p2 = (i + 1) % columns + j * columns + (temp);
229 uint p3 = (i + 1) % columns + j * columns + columns + (sideTopPtNum);
230 uint p4 = i + j * columns + columns + (sideTopPtNum);
231
232
233 index.insert(p4);
234 index.insert(p3);
235 index.insert(p2);
236 index.insert(p1);
237
238 incre++;
239 }
240 }
241 uint buttomPtNum = incre;
242
243 //TriangleTop
244 for (uint i = 0; i < columns; i++)
245 {
246 auto& index = polygonIndices[incre];
247 uint p1 = sideTopPtNum + i;
248 uint p2 = sideTopPtNum + (i + 1) % columns;
249 uint p3 = topCenter;
250
251 index.insert(p1);
252 index.insert(p2);
253 index.insert(p3);
254
255 incre++;
256 }
257
258 //TriangleButtom
259 if (end_segment == 1)
260 {
261 buttomPtNum = 0;
262 }
263 for (uint i = 0; i < columns; i++)
264 {
265 auto& index = polygonIndices[incre];
266 uint p1 = buttomPtNum + i;
267 uint p2 = buttomPtNum + (i + 1) % columns;
268 uint p3 = buttomCenter;
269
270 index.insert(p3);
271 index.insert(p2);
272 index.insert(p1);
273
274 incre++;
275 }
276 }
277
278
279
280 //TransformModel
281
282 Quat<Real> q = this->computeQuaternion();
283
284 q.normalize();
285
286 auto RV = [&](const Coord& v)->Coord {
287 return center + q.rotate(v - center);
288 };
289
290 int numpt = vertices.size();
291
292 for (int i = 0; i < numpt; i++)
293 {
294 vertices[i][1] -= height / 2;
295 vertices[i] = RV(vertices[i] * scale + RV(center));
296 }
297
298 TCylinder3D<Real> cylinder;
299 cylinder.center = center;
300 cylinder.height = height;
301 cylinder.radius = radius;
302 cylinder.scale = scale;
303 cylinder.rotation = q;
304 this->outCylinder()->setValue(cylinder);
305
306
307 polySet->setPoints(vertices);
308 polySet->setPolygons(polygonIndices);
309 polySet->update();
310
311 polygonIndices.clear();
312
313 auto& ts = this->stateTriangleSet()->getData();
314 polySet->turnIntoTriangleSet(ts);
315
316 vertices.clear();
317
318
319
320
321 }
322
323 template<typename TDataType>
325 {
326 auto center = this->varLocation()->getData();
327 auto rot = this->varRotation()->getData();
328 auto scale = this->varScale()->getData();
329
330 auto radius = this->varRadius()->getData();
331 auto height = this->varHeight()->getData();
332
333 Coord length(Coord(radius, height, radius));
334 length[0] *= scale[0];
335 length[1] *= scale[1];
336 length[2] *= scale[2];
337
338 Quat<Real> q = this->computeQuaternion();
339
340 q.normalize();
341
343 box.center = center;
344 box.u = q.rotate(Coord(1, 0, 0));
345 box.v = q.rotate(Coord(0, 1, 0));
346 box.w = q.rotate(Coord(0, 0, 1));
347 box.extent = length;
348
349 auto AABB = box.aabb();
350 auto& v0 = AABB.v0;
351 auto& v1 = AABB.v1;
352
353
354 NBoundingBox ret;
355 ret.lower = Vec3f(v0.x, v0.y, v0.z);
356 ret.upper = Vec3f(v1.x, v1.y, v1.z);
357
358 return ret;
359 }
360
362}
#define DEFINE_CLASS(name)
Definition Object.h:140
#define M_PI
Definition Typedef.inl:36
TDataType::Coord Coord
TDataType::Real Real
NBoundingBox boundingBox() override
void resetStates() override
std::shared_ptr< GraphicsPipeline > graphicsPipeline()
Definition Node.cpp:311
Quat< Real > computeQuaternion()
a class to store polygon whose vertex number is greater or equal than 3
Definition PolygonSet.h:29
DYN_FUNC Quat< Real > & normalize()
Definition Quat.inl:183
DYN_FUNC Vector< Real, 3 > rotate(const Vector< Real, 3 > &v) const
Rotate a vector by the quaternion, guarantee the quaternion is normalized before rotating the vector.
Definition Quat.inl:259
Quat< Real > rotation
Coord3D u
three unit vectors u, v and w forming a right-handed orthornormal basis
Coord3D extent
half the dimension in each of the u, v, and w directions
Coord3D center
centerpoint
DYN_FUNC TAlignedBox3D< Real > aabb()
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
DYN_FUNC Complex< Real > sin(const Complex< Real > &)
Definition Complex.inl:564
ArrayList< T, DeviceType::CPU > CArrayList
Definition ArrayList.h:207
DYN_FUNC Complex< Real > cos(const Complex< Real > &)
Definition Complex.inl:573
::dyno::TAlignedBox3D< Real > AABB
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
Array< T, DeviceType::CPU > CArray
Definition Array.h:151
unsigned int uint
Definition VkProgram.h:14