PeriDyno 1.0.0
Loading...
Searching...
No Matches
Extrude.cu
Go to the documentation of this file.
1#include "Extrude.h"
2#include "Topology/PointSet.h"
3#include "GLSurfaceVisualModule.h"
4#include "GLWireframeVisualModule.h"
5#include "GLPointVisualModule.h"
6#include "EarClipper.h"
7
8namespace dyno
9{
10 template<typename TDataType>
11 ExtrudeModel<TDataType>::ExtrudeModel()
12 : ParametricModel<TDataType>()
13 {
14 this->varHeight()->setRange(0.001f, 10.0f);
15
16 this->stateTriangleSet()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
17
18 auto glModule = std::make_shared<GLSurfaceVisualModule>();
19 glModule->setColor(Color(0.8f, 0.52f, 0.25f));
20 glModule->setVisible(true);
21 this->stateTriangleSet()->connect(glModule->inTriangleSet());
22 this->graphicsPipeline()->pushModule(glModule);
23
24 auto glModule2 = std::make_shared<GLPointVisualModule>();
25 glModule2->setColor(Color(1.0f, 1.0f, 1.0f));
26 glModule2->varPointSize()->setValue(0.01);
27 this->stateTriangleSet()->connect(glModule2->inPointSet());
28 this->graphicsPipeline()->pushModule(glModule2);
29
30 auto glModule3 = std::make_shared<GLWireframeVisualModule>();
31 glModule3->setColor(Color(1.0f, 1.0f, 1.0f));
32 this->stateTriangleSet()->connect(glModule3->inEdgeSet());
33 this->graphicsPipeline()->pushModule(glModule3);
34
35 auto callback = std::make_shared<FCallBackFunc>(std::bind(&ExtrudeModel<TDataType>::varChanged, this));
36
37 this->varLocation()->attach(callback);
38 this->varScale()->attach(callback);
39 this->varRotation()->attach(callback);
40
41 this->varRow()->attach(callback);
42 this->varHeight()->attach(callback);
43 this->varReverseNormal()->attach(callback);
44 this->varCurve()->attach(callback);
45
46
47 }
48 template<typename TDataType>
49 void ExtrudeModel<TDataType>::resetStates()
50 {
51 varChanged();
52 }
53
54 template<typename TDataType>
55 void ExtrudeModel<TDataType>::varChanged()
56 {
57 auto center = this->varLocation()->getData();
58 auto rot = this->varRotation()->getData();
59 auto scale = this->varScale()->getData();
60
61 enum PointMode
62 {
63 UseInput = 0,
64 UseCurve = 1,
65 };
66
67 PointMode mPointMode = PointMode::UseCurve;
68
69 int columns_i = 0;
70 if (!this->inPointSet()->isEmpty())
71 {
72 columns_i = this->inPointSet()->getData().getPointSize();
73 mPointMode = PointMode::UseInput;
74 }
75 else if (this->varCurve()->getValue().getPointSize())
76 {
77 columns_i = this->varCurve()->getValue().getPointSize();
78 mPointMode = PointMode::UseCurve;
79 }
80 else
81 {
82 columns_i = 0;
83 }
84
85 if (columns_i >= 3)
86 {
87 //Side vertices
88
89 Real HeightValue = this->varHeight()->getData();
90 Real RowValue = this->varRow()->getData();
91 Real tempRow = 0;
92
93 std::vector<Coord> vertices;
94 CArray<Coord> capPoint;
95
96 for (int i = 0; i <= RowValue; i++)
97 {
98 Real tempy = HeightValue * i / RowValue;
99 Vec3f position;
100
101 if (mPointMode == PointMode::UseInput)
102 {
103 if (i == 0)
104 {
105 DArray<Coord> sa = this->inPointSet()->getData().getPoints();
106 capPoint.assign(sa);
107 }
108
109 for (int k = 0; k < columns_i; k++)
110 {
111 position = { capPoint[k][0] , capPoint[k][1] + tempy ,capPoint[k][2] };
112
113 vertices.push_back(position);
114
115 }
116 }
117 else if (mPointMode == PointMode::UseCurve)
118 {
119 for (int k = 0; k < columns_i; k++)
120 {
121 auto curvePoint = this->varCurve()->getValue().getPoints();
122
123 position = { float(curvePoint[k].x) , float(tempy) ,float(curvePoint[k].y) };
124 vertices.push_back(position);
125
126 if (i == 0 )
127 capPoint.pushBack(position);
128
129 }
130
131 }
132 }
133
134
135
136
137 //top and bottom
138 std::vector<TopologyModule::Triangle> triangle;
139
140 int pt_side_len = vertices.size();
141
142 //side
143 for (int rowl = 0; rowl <= RowValue - 1; rowl++)
144 {
145
146 for (int faceid = 0; faceid < columns_i; faceid++)
147 {
148 if (faceid != columns_i - 1)
149 {
150 triangle.push_back(TopologyModule::Triangle(columns_i + faceid + rowl * columns_i, 0 + faceid + rowl * columns_i, 1 + faceid + rowl * columns_i));
151 triangle.push_back(TopologyModule::Triangle(columns_i + 1 + faceid + rowl * columns_i, columns_i + faceid + rowl * columns_i, 1 + faceid + rowl * columns_i));
152 }
153 else
154 {
155 triangle.push_back(TopologyModule::Triangle(1 + 2 * faceid + rowl * columns_i, 0 + faceid + rowl * columns_i, 0 + rowl * columns_i));
156 triangle.push_back(TopologyModule::Triangle(1 + faceid + rowl * columns_i, 1 + 2 * faceid + rowl * columns_i, 0 + rowl * columns_i));
157 }
158
159 }
160 }
161
162 int pt_len = vertices.size() - 2;
163 int top_pt_len = vertices.size() - 2 - pt_side_len;
164 int addnum = 0;
165
166
167
168 //transform
169
170
171 Quat<Real> q = this->computeQuaternion();
172
173 q.normalize();
174
175 auto RV = [&](const Coord& v)->Coord {
176 return center + q.rotate(v - center);
177 };
178
179 int numpt = vertices.size();
180
181 for (int i = 0; i < numpt; i++)
182 {
183 //vertices[i][1] -= height / 2;
184 vertices[i] = RV(vertices[i] * scale + RV(center));
185 }
186
187
188
189 if (this->varReverseNormal()->getData() == true)
190 {
191 int trinum = triangle.size();
192 for (int i = 0; i < trinum; i++)
193 {
194 int temp;
195 temp = triangle[i][0];
196 triangle[i][0] = triangle[i][2];
197 triangle[i][2] = temp;
198 }
199 }
200
201
202 EarClipper<DataType3f> sab;
203 std::vector<TopologyModule::Triangle> triangleCap;
204
205 sab.polyClip(capPoint, triangleCap);
206 int addnum2 = vertices.size() - capPoint.size();
207
208
209 for (int i = 0; i < triangleCap.size(); i++)
210 {
211 triangle.push_back(triangleCap[i]);
212 triangle.push_back(TopologyModule::Triangle(triangleCap[i][0] + addnum2, triangleCap[i][1] + addnum2, triangleCap[i][2] + addnum2));
213 }
214
215 auto triangleSet = this->stateTriangleSet()->getDataPtr();
216
217 triangleSet->setPoints(vertices);
218 triangleSet->setTriangles(triangle);
219
220 triangleSet->update();
221
222 vertices.clear();
223 triangle.clear();
224 }
225
226 }
227
228
229 DEFINE_CLASS(ExtrudeModel);
230}