PeriDyno 1.0.0
Loading...
Searching...
No Matches
Turning.cpp
Go to the documentation of this file.
1#include "Turning.h"
2#include "Topology/PointSet.h"
5
7#include "Mapping/Extract.h"
8
9namespace dyno
10{
11 template<typename TDataType>
13 : ParametricModel<TDataType>()
14 {
15 this->varColumns()->setRange(3, 50);
16 this->varRadius()->setRange(-10.0f, 10.0f);
17 this->varEndSegment()->setRange(0, 39);
18
19 this->statePolygonSet()->setDataPtr(std::make_shared<PolygonSet<TDataType>>());
20 this->stateTriangleSet()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
21
22 this->inPointSet()->tagOptional(true);
23
24 auto callback = std::make_shared<FCallBackFunc>(std::bind(&TurningModel<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->varRadius()->attach(callback);
33 this->varReverseNormal()->attach(callback);
34 this->varUseRamp()->attach(callback);
35 this->varCurve()->attach(callback);
36
37 auto tsRender = std::make_shared<GLSurfaceVisualModule>();
38 tsRender->setColor(Color(0.8f, 0.52f, 0.25f));
39 tsRender->setVisible(true);
40 this->stateTriangleSet()->connect(tsRender->inTriangleSet());
41 this->graphicsPipeline()->pushModule(tsRender);
42
43 auto exES = std::make_shared<ExtractEdgeSetFromPolygonSet<TDataType>>();
44 this->statePolygonSet()->connect(exES->inPolygonSet());
45 this->graphicsPipeline()->pushModule(exES);
46
47 auto esRender = std::make_shared<GLWireframeVisualModule>();
48 esRender->varBaseColor()->setValue(Color(0, 0, 0));
49 exES->outEdgeSet()->connect(esRender->inEdgeSet());
50 this->graphicsPipeline()->pushModule(esRender);
51
52 //this->statePolygonSet()->promoteOuput();
53 this->stateTriangleSet()->promoteOuput();
54 this->statePolygonSet()->promoteOuput();
55
56 this->statePolygonSet()->promoteOuput();
57 this->stateTriangleSet()->promoteOuput();
58 }
59
60 template<typename TDataType>
65
66
67 template<typename TDataType>
69 {
70 int pointsize = 0;;
71 if (!this->inPointSet()->isEmpty())
72 {
73 pointsize = this->inPointSet()->getData().getPointSize();
74 }
75
76 auto useRamp = this->varUseRamp()->getValue();
77 auto Ramp = this->varCurve()->getValue();
78 auto floatCoordArray = Ramp.getPoints();
79
80
82 if (!this->inPointSet()->isEmpty())
83 {
84 s.copyFrom(this->inPointSet()->getData());
85 }
86 DArray<Coord> sa = s.getPoints();
87 CArray<Coord> c_sa;
88 PointSet<TDataType> pointSet;
89
90 if (useRamp)
91 {
92 std::vector<Coord> vertices;
93 std::vector<TopologyModule::Triangle> triangle;
94 pointsize = floatCoordArray.size();
95
96 Coord Location;
97 if (pointsize != 0)
98 {
99 for (size_t i = 0; i < pointsize; i++)
100 {
101 Location = Coord(floatCoordArray[i].x, floatCoordArray[i].y, 0);
102
103 vertices.push_back(Location);
104 }
105 }
106 pointSet.setPoints(vertices);
107 c_sa.assign(pointSet.getPoints());
108 }
109 else
110 {
111 c_sa.assign(sa);
112 }
113
114
115 auto center = this->varLocation()->getValue();
116 auto rot = this->varRotation()->getValue();
117 auto scale = this->varScale()->getValue();
118
119 auto radius = this->varRadius()->getValue();
120 auto row = pointsize - 1;
121 auto columns = this->varColumns()->getValue();
122 auto end_segment = this->varEndSegment()->getValue();
123
124 auto triangleSet = this->stateTriangleSet()->getDataPtr();
125 auto polySet = this->statePolygonSet()->getDataPtr();
126
127 std::vector<Coord> vertices;
128 std::vector<TopologyModule::Triangle> triangle;
129
130
131 Coord Location;
132 Real angle = M_PI / 180 * 360 / columns;
133 Real temp_angle = angle;
134
135 //Side Point
136 if (!this->inPointSet()->isEmpty() | (useRamp && pointsize > 0))
137 {
138 for (int k = 0; k <= row; k++)
139 {
140 Real tempy = c_sa[k][1];
141 Real radius = c_sa[k][0] + this->varRadius()->getData();
142
143 for (int j = 0; j < columns; j++) {
144
145 temp_angle = j * angle;
146
147 Location = { sin(temp_angle) * radius , tempy ,cos(temp_angle) * radius };
148
149 vertices.push_back(Location);
150 }
151 }
152
153 //Top_Buttom Point
154
155 int pt_side_len = vertices.size();
156
157 for (int i = 1; i < end_segment; i++)
158 {
159 float offset = i / (float(end_segment) - i);
160
161 for (int p = 0; p < columns; p++)
162 {
163 int top_start = pt_side_len - columns + p;
164
165 Coord toppt = { (vertices[top_start][0] + offset * 0) / (1 + offset), c_sa[c_sa.size() - 1][1], (vertices[top_start][2] + offset * 0) / (1 + offset) };
166
167 vertices.push_back(toppt);
168 }
169
170 }
171
172 for (int i = 1; i < end_segment; i++)
173 {
174 float offset = i / (float(end_segment) - i);
175
176 for (int p = 0; p < columns; p++)
177 {
178 Coord buttompt = { (vertices[p][0] + offset * 0) / (1 + offset), c_sa[0][1], (vertices[p][2] + offset * 0) / (1 + offset) };
179
180 vertices.push_back(buttompt);
181 }
182
183 }
184
185 vertices.push_back(Coord(0, c_sa[0][1], 0));
186 uint buttomCenter = vertices.size() - 1;
187 vertices.push_back(Coord(0, c_sa[c_sa.size() - 1][1], 0));
188 uint topCenter = vertices.size() - 1;
189
190
191 uint numOfPolygon = columns * row + 2 * columns * end_segment;
192 CArray<uint> counter(numOfPolygon);
193
194 uint incre = 0;
195 uint endQuadNum = ((int(end_segment) - 1) * int(columns) < 0 ? 0 : (int(end_segment) - 1) * int(columns));
196
197 uint QuadNum = columns * row + 2 * endQuadNum;
198 for (uint j = 0; j < QuadNum; j++)
199 {
200 counter[incre] = 4;
201 incre++;
202 }
203
204 uint TriangleNum = (int(end_segment) - 1) < 0 ? 0 : columns * 2;
205 for (uint j = 0; j < TriangleNum; j++)
206 {
207 counter[incre] = 3;
208 incre++;
209 }
210
211 CArrayList<uint> polygonIndices;
212 polygonIndices.resize(counter);
213
214
215 //side;
216 incre = 0;
217 for (uint i = 0; i < columns; i++)
218 {
219 for (uint j = 0; j < row; j++)
220 {
221 auto& index = polygonIndices[incre];
222
223 uint p1 = i + j * columns;
224 uint p2 = (i + 1) % columns + j * columns;
225 uint p3 = (i + 1) % columns + j * columns + columns;
226 uint p4 = i + j * columns + columns;
227
228
229 index.insert(p1);
230 index.insert(p2);
231 index.insert(p3);
232 index.insert(p4);
233
234 incre++;
235 }
236 }
237
238 //Top
239 if (end_segment > 0)
240 {
241 uint sidePtNum = columns * row;
242 for (uint i = 0; i < columns; i++)
243 {
244 for (uint j = 0; j < end_segment - 1; j++)
245 {
246 auto& index = polygonIndices[incre];
247
248 uint p1 = i + j * columns + (sidePtNum);
249 uint p2 = (i + 1) % columns + j * columns + (sidePtNum);
250 uint p3 = (i + 1) % columns + j * columns + columns + (sidePtNum);
251 uint p4 = i + j * columns + columns + (sidePtNum);
252
253
254 index.insert(p1);
255 index.insert(p2);
256 index.insert(p3);
257 index.insert(p4);
258
259 incre++;
260 }
261 }
262
263 //Buttom
264 uint sideTopPtNum = incre;
265 for (uint i = 0; i < columns; i++)
266 {
267 for (uint j = 0; j < end_segment - 1; j++)
268 {
269 auto& index = polygonIndices[incre];
270
271 uint temp = sideTopPtNum;
272 if (j == 0)
273 {
274 temp = 0;
275 }
276
277 uint p1 = i + j * columns + (temp);
278 uint p2 = (i + 1) % columns + j * columns + (temp);
279 uint p3 = (i + 1) % columns + j * columns + columns + (sideTopPtNum);
280 uint p4 = i + j * columns + columns + (sideTopPtNum);
281
282
283 index.insert(p1);
284 index.insert(p2);
285 index.insert(p3);
286 index.insert(p4);
287
288 incre++;
289 }
290 }
291 uint buttomPtNum = incre;
292
293 //TriangleTop
294 for (uint i = 0; i < columns; i++)
295 {
296 auto& index = polygonIndices[incre];
297 uint p1 = sideTopPtNum + i;
298 uint p2 = sideTopPtNum + (i + 1) % columns;
299 uint p3 = topCenter;
300
301 index.insert(p1);
302 index.insert(p2);
303 index.insert(p3);
304
305 incre++;
306 }
307
308 //TriangleButtom
309 if (end_segment == 1)
310 {
311 buttomPtNum = 0;
312 }
313 for (uint i = 0; i < columns; i++)
314 {
315 auto& index = polygonIndices[incre];
316 uint p1 = buttomPtNum + i;
317 uint p2 = buttomPtNum + (i + 1) % columns;
318 uint p3 = buttomCenter;
319
320 index.insert(p1);
321 index.insert(p2);
322 index.insert(p3);
323
324 incre++;
325 }
326 }
327
328
329
330 //TransformModel
331
332 Quat<Real> q = this->computeQuaternion();
333
334 q.normalize();
335
336 auto RV = [&](const Coord& v)->Coord {
337 return center + q.rotate(v - center);
338 };
339
340 int numpt = vertices.size();
341
342 for (int i = 0; i < numpt; i++)
343 {
344 vertices[i] = RV(vertices[i] * scale + RV(center));
345 }
346
347
348
349 polySet->setPoints(vertices);
350 polySet->setPolygons(polygonIndices);
351 polySet->update();
352
353 polygonIndices.clear();
354
355 auto& ts = this->stateTriangleSet()->getData();
356 polySet->turnIntoTriangleSet(ts);
357
358 vertices.clear();
359 }
360
361 }
362
363
365}
#define DEFINE_CLASS(name)
Definition Object.h:140
#define M_PI
Definition Typedef.inl:36
std::vector< Coord2D > getPoints()
Definition Canvas.h:139
std::shared_ptr< GraphicsPipeline > graphicsPipeline()
Definition Node.cpp:320
Quat< Real > computeQuaternion()
A PointSet stores the coordinates for a set of independent points.
Definition PointSet.h:8
DArray< Coord > & getPoints()
Return the array of points.
Definition PointSet.h:70
void setPoints(const std::vector< Coord > &pos)
void copyFrom(PointSet< TDataType > &pointSet)
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
TDataType::Coord Coord
Definition Turning.h:37
void resetStates() override
Definition Turning.cpp:61
TDataType::Real Real
Definition Turning.h:36
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Array< T, DeviceType::GPU > DArray
Definition Array.inl:89
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
Array< T, DeviceType::CPU > CArray
Definition Array.h:151
unsigned int uint
Definition VkProgram.h:14