PeriDyno 1.0.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
ConeModel.cpp
Go to the documentation of this file.
1#include "ConeModel.h"
2
6#include "Mapping/Extract.h"
7
8
9namespace dyno
10{
11 template<typename TDataType>
13 : BasicShape<TDataType>()
14 {
15 this->varRow()->setRange(1, 50);
16 this->varColumns()->setRange(3, 50);
17 this->varRadius()->setRange(0.001f, 10.0f);
18 this->varHeight()->setRange(0.001f, 10.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(&ConeModel<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->varRow()->attach(callback);
32 this->varRadius()->attach(callback);
33 this->varHeight()->attach(callback);
34
35 auto tsRender = std::make_shared<GLSurfaceVisualModule>();
36 tsRender->setVisible(true);
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 template<typename TDataType>
58
59 template<typename TDataType>
61 {
62 auto center = this->varLocation()->getValue();
63 auto rot = this->varRotation()->getValue();
64 auto scale = this->varScale()->getValue();
65
66 auto radius = this->varRadius()->getValue();
67 auto row = this->varRow()->getValue();
68 auto columns = this->varColumns()->getValue();
69 auto height = this->varHeight()->getValue();
70 auto end_segment = this->varEndSegment()->getValue();
71
72
73 std::vector<Coord> vertices;
74 std::vector<TopologyModule::Triangle> triangle;
75
76 Coord Location;
77 Real angle = M_PI / 180 * 360 / columns;
78 Real temp_angle = angle;
79 Real x, y, z;
80
81
82 //Side
83 for (int k = row - 1; k >= 0; k--)
84 {
85 Real ratio = float(k) / float(row);
86 Real tempy = height * ratio;
87 Real tempRadius = radius * (1 - ratio);
88
89 for (int j = 0; j < columns; j++) {
90
91 temp_angle = j * angle;
92
93 Location = { sin(temp_angle) * tempRadius , tempy ,cos(temp_angle) * tempRadius };
94
95 vertices.push_back(Location);
96 }
97 }
98
99 for (int i = 1; i < end_segment; i++)
100 {
101 Real ratio = float(i) / float(end_segment);
102 Real tempy = 0;
103 Real tempRadius = radius * (1 - ratio);
104
105 for (int p = 0; p < columns; p++)
106 {
107 temp_angle = p * angle;
108
109 Coord buttompt = { sin(temp_angle) * tempRadius , tempy ,cos(temp_angle) * tempRadius };
110
111 vertices.push_back(buttompt);
112 }
113 }
114
115
116 vertices.push_back(Coord(0, height, 0));
117 uint topCenter = vertices.size() - 1;
118 uint buttomCenter = vertices.size();
119
120 //if (end_segment > 0)
121 vertices.push_back(Coord(0, 0, 0));
122
123 uint realRow = (int(row) - 1 > 0) ? int(row) - 1 : 0;
124 uint realEndsegment = (int(end_segment) - 1 > 0) ? int(end_segment) - 1 : 0;
125
126 uint numOfPolygon = columns * (row + end_segment);
127 CArray<uint> counter(numOfPolygon);
128 uint incre = 0;
129
130 uint QuadNum = columns * realRow + columns * realEndsegment;
131 uint TriangleNum;
132 if(end_segment>0)
133 TriangleNum= uint(columns * 2);
134 else
135 TriangleNum = uint(columns);
136
137
138
139
140 for (uint j = 0; j < QuadNum; j++)
141 {
142 counter[incre] = 4;
143 incre++;
144 }
145
146 for (uint j = 0; j < TriangleNum; j++)
147 {
148 counter[incre] = 3;
149 incre++;
150 }
151 CArrayList<uint> polygonIndices;
152 polygonIndices.resize(counter);
153
154
155
156 //Quad
157 incre = 0;
158
159
160
161 for (uint i = 0; i < columns; i++)
162 {
163 for (uint j = 0; j < realRow + realEndsegment; j++)
164 {
165 auto& index = polygonIndices[incre];
166
167 uint p1 = i + j * columns;
168 uint p2 = (i + 1) % columns + j * columns;
169 uint p3 = (i + 1) % columns + j * columns + columns;
170 uint p4 = i + j * columns + columns;
171
172 index.insert(p4);
173 index.insert(p3);
174 index.insert(p2);
175 index.insert(p1);
176
177
178 incre++;
179 }
180 }
181
182 uint sidePtNum = incre;
183
184 if (end_segment > 0)
185 {
186 //TriangleButtom
187 for (uint i = 0; i < columns; i++)
188 {
189 auto& index = polygonIndices[incre];
190 uint p1 = sidePtNum + i;
191 uint p2 = sidePtNum + (i + 1) % columns;
192 uint p3 = buttomCenter;
193
194 index.insert(p3);
195 index.insert(p2);
196 index.insert(p1);
197
198
199 incre++;
200 }
201 }
202 //TriangleTop
203 for (uint i = 0; i < columns; i++)
204 {
205 auto& index = polygonIndices[incre];
206 uint p1 = i;
207 uint p2 = (i + 1) % columns;
208 uint p3 = topCenter;
209 index.insert(p1);
210 index.insert(p2);
211 index.insert(p3);
212
213 incre++;
214 }
215
216 //Transform
217 Quat<Real> q = this->computeQuaternion();
218
219 q.normalize();
220
221 auto RV = [&](const Coord& v)->Coord {
222 return center + q.rotate(v - center);
223 };
224
225 int numpt = vertices.size();
226
227 for (int i = 0; i < numpt; i++)
228 {
229 vertices[i][1] -= 1 * height / 2;
230 vertices[i] = RV(vertices[i] * scale + RV(center));
231 }
232
233 auto polySet = this->statePolygonSet()->getDataPtr();
234
235 polySet->setPoints(vertices);
236 polySet->setPolygons(polygonIndices);
237 polySet->update();
238
239 auto& ts = this->stateTriangleSet()->getData();
240 polySet->turnIntoTriangleSet(ts);
241
242 auto triangleSet = this->stateTriangleSet()->getDataPtr();
243
244
245 polygonIndices.clear();
246 vertices.clear();
247
248 //Setup the geometric primitive
249 TCone3D<Real> cone;
250 cone.center = center;
251 cone.height = height;
252 cone.radius = radius;
253 cone.scale = scale;
254 cone.rotation = q;
255 this->outCone()->setValue(cone);
256 }
257
258 template<typename TDataType>
260 {
261 auto center = this->varLocation()->getData();
262 auto rot = this->varRotation()->getData();
263 auto scale = this->varScale()->getData();
264
265 auto radius = this->varRadius()->getData();
266 auto height = this->varHeight()->getData();
267
268 Coord length(Coord(radius, height, radius));
269 length[0] *= scale[0];
270 length[1] *= scale[1];
271 length[2] *= scale[2];
272
273 Quat<Real> q = this->computeQuaternion();
274
275 q.normalize();
276
278 box.center = center;
279 box.u = q.rotate(Coord(1, 0, 0));
280 box.v = q.rotate(Coord(0, 1, 0));
281 box.w = q.rotate(Coord(0, 0, 1));
282 box.extent = length;
283
284 auto AABB = box.aabb();
285 auto& v0 = AABB.v0;
286 auto& v1 = AABB.v1;
287
288
289 NBoundingBox ret;
290 ret.lower = Vec3f(v0.x, v0.y, v0.z);
291 ret.upper = Vec3f(v1.x, v1.y, v1.z);
292
293 return ret;
294 }
295
296
297
299}
#define DEFINE_CLASS(name)
Definition Object.h:140
#define M_PI
Definition Typedef.inl:36
void resetStates() override
Definition ConeModel.cpp:54
TDataType::Real Real
Definition ConeModel.h:31
TDataType::Coord Coord
Definition ConeModel.h:32
NBoundingBox boundingBox() 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