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