PeriDyno 1.0.0
Loading...
Searching...
No Matches
Sweep.cpp
Go to the documentation of this file.
1#include "Sweep.h"
2
6#include "EarClipper.h"
7
8
9
10namespace dyno
11{
12 template<typename TDataType>
14 : ParametricModel<TDataType>()
15 {
16 this->varRadius()->setRange(0.001f, 10.0f);
17
18 this->varRadius()->setValue(1);
19
20
21 this->stateTriangleSet()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
22
23
24 auto glModule = std::make_shared<GLSurfaceVisualModule>();
25 glModule->setColor(Color(0.8f, 0.52f, 0.25f));
26 glModule->setVisible(true);
27 this->stateTriangleSet()->connect(glModule->inTriangleSet());
28 this->graphicsPipeline()->pushModule(glModule);
29
30
31 auto wireframe = std::make_shared<GLWireframeVisualModule>();
32 this->stateTriangleSet()->connect(wireframe->inEdgeSet());
33 this->graphicsPipeline()->pushModule(wireframe);
34
35 this->stateTriangleSet()->promoteOuput();
36
37 auto pointGlModule = std::make_shared<GLPointVisualModule>();
38 this->stateTriangleSet()->connect(pointGlModule->inPointSet());
39 this->graphicsPipeline()->pushModule(pointGlModule);
40 pointGlModule->varPointSize()->setValue(0.005);
41
42 this->stateTriangleSet()->promoteOuput();
43
44 auto callback = std::make_shared<FCallBackFunc>(std::bind(&SweepModel<TDataType>::varChanged, this));
45
46 this->varLocation()->attach(callback);
47 this->varScale()->attach(callback);
48 this->varRotation()->attach(callback);
49
50 this->varRadius()->attach(callback);
51 this->varCurveRamp()->attach(callback);
52 this->varReverseNormal()->attach(callback);
53
54 auto displayCallback = std::make_shared<FCallBackFunc>(std::bind(&SweepModel<TDataType>::displayChanged, this));
55
56 this->varDisplayPoints()->attach(displayCallback);
57 this->varDisplaySurface()->attach(displayCallback);
58 this->varDisplayWireframe()->attach(displayCallback);
59
60 }
61
62 template<typename TDataType>
64 {
65 varChanged();
67 printf("resetStates sweep \n");
68 }
69
70 template<typename TDataType>
72 {
73 auto center = this->varLocation()->getData();
74 auto rot = this->varRotation()->getData();
75 auto scale = this->varScale()->getData();
76
77 auto radius = this->varRadius()->getData();
78
79 if (this->inSpline()->isEmpty())
80 {
81 printf("inSpline empty \n");
82 return;
83 }
84 if (this->inCurve()->isEmpty())
85 {
86 printf("inCurve empty \n");
87 return;
88 }
89
90 auto SplineIn = this->inSpline()->getData().getPoints();
91
92 auto triangleSet = this->stateTriangleSet()->getDataPtr();
93
94 auto VertexIn2 = this->inCurve()->getData().getPoints();
95
96 if (SplineIn.size() == 0) { return; }
97 if (VertexIn2.size() == 0) { return; }
98
99 std::vector<Coord> vertices;
100 std::vector<TopologyModule::Triangle> triangle;
101
102 //Spline
103 CArray<Coord> c_point1;
104
105 c_point1.assign(SplineIn);
106
107 int lengthV = SplineIn.size();
108 totalIndex = lengthV;
109 //Curve2
110
111 CArray<Coord> c_point2;
112 c_point2.assign(VertexIn2);
113
114 int lengthV2 = VertexIn2.size();
115
116 unsigned CopyNumber = lengthV;
117
118 Real PI = 3.1415926535;
119
120 uint counter = 0;
121
122 Coord LocationCurrent;
123 Coord LocationNext = {0,1,0};
124 Coord LocationTemp2 = {0,1,0};
125
126
127 //Transform
128
129
130 for (size_t i = 0; i < lengthV; i++)
131 {
132 currentIndex = i;
133
134 int current = i;
135 int next = i + 1;
136
137 if (i == lengthV - int(1))
138 {
139 next = i ;
140 current = i - 1;
141 }
142
143 LocationCurrent = { c_point1[current][0], c_point1[current][1], c_point1[current][2] };
144 LocationNext = { c_point1[next][0], c_point1[next][1], c_point1[next][2] };
145
146 Vec3f vb = Vec3f(0, 1, 0);
147 Vec3f va =LocationNext - LocationCurrent;
148 va.normalize();
149 vb.normalize();
150
151 Vec3f Axis = va.cross(vb);
152 if (Axis == Vec3f(0, 0, 0))
153 {
154 Axis = Vec3f(0,1,0);
155
156 }
157 Axis.normalize();
158
159 Real angle =-1 * acos(va.dot(vb));
160
161 //DYN_FUNC Quat(Real rot, const Vector<Real, 3> &axis); //init from the rotation axis and angle(in radian)
162
163 Quat<Real> q = Quat<Real>(angle ,Axis);
164
165
166 auto RV = [&](const Coord& v)->Coord
167 {
168 return q.rotate(v);//
169 };
170
171 Coord LocationCurvePoint;
172 Coord Offest;
173 for (size_t k = 0; k < lengthV2; k++ )
174 {
175 LocationCurvePoint = { c_point2[k][0], c_point2[k][1], c_point2[k][2] };
176 Offest = c_point1[i];
177 LocationCurvePoint = RV(LocationCurvePoint * RealScale()) + Offest;//
178 vertices.push_back(LocationCurvePoint);
179 }
180
181 }
182
183 unsigned ptnum = vertices.size();
184
185 for (int rowl = 0; rowl < lengthV - 1; rowl++)
186 {
187 for (int faceid = 0; faceid < lengthV2; faceid++)
188 {
189 if (faceid != lengthV2 - 1)
190 {
191
192 triangle.push_back(TopologyModule::Triangle(lengthV2 + faceid + rowl * lengthV2, 0 + faceid + rowl * lengthV2, 1 + faceid + rowl * lengthV2));
193 triangle.push_back(TopologyModule::Triangle(lengthV2 + 1 + faceid + rowl * lengthV2, lengthV2 + faceid + rowl * lengthV2, 1 + faceid + rowl * lengthV2));
194 }
195 else
196 {
197 triangle.push_back(TopologyModule::Triangle(1 + 2 * faceid + rowl * lengthV2, 0 + faceid + rowl * lengthV2, 0 + rowl * lengthV2));
198 triangle.push_back(TopologyModule::Triangle(1 + faceid + rowl * lengthV2, 1 + 2 * faceid + rowl * lengthV2, 0 + rowl * lengthV2));
199 }
200 }
201
202 }
203
204 //fill cap
205 // get triangleCap by EarClipper
207 std::vector<TopologyModule::Triangle> triangleCap;
208
209 sab.polyClip(VertexIn2, triangleCap);
210 int addnum2 = vertices.size() - VertexIn2.size();
211
212
213
214 for (int i = 0; i < triangleCap.size(); i++)
215 {
216 triangle.push_back(TopologyModule::Triangle(triangleCap[i][2], triangleCap[i][1], triangleCap[i][0]));
217 triangle.push_back(TopologyModule::Triangle(triangleCap[i][0] + addnum2, triangleCap[i][1] + addnum2, triangleCap[i][2] + addnum2));
218 }
219
220 //ReverseNormal
221 if (this->varReverseNormal()->getData() == true)
222 {
223 int trinum = triangle.size();
224 for (int i = 0; i < trinum; i++)
225 {
226 int temp;
227 temp = triangle[i][0];
228 triangle[i][0] = triangle[i][2];
229 triangle[i][2] = temp;
230 }
231 }
232
233 //Transform
234
235 Quat<Real> q2 = this->computeQuaternion();
236
237 q2.normalize();
238
239 auto RVt = [&](const Coord& v)->Coord {
240 return center + q2.rotate(v - center);
241 };
242
243 int numpt = vertices.size();
244
245 for (int i = 0; i < numpt; i++)
246 {
247
248 vertices[i] =RVt( vertices[i] * scale + RVt( center ));
249 }
250
251 int s = vertices.size();
252
253 triangleSet->setPoints(vertices);
254 triangleSet->setTriangles(triangle);
255
256 triangleSet->update();
257
258 vertices.clear();
259 triangle.clear();
260
261 }
262
263
264
265 template<typename TDataType>
267 {
268 auto radius = this->varRadius()->getData();
269 Vec3f s = Vec3f(1*radius ,1* radius ,1 *radius);
270
271 float pr = this->varCurveRamp()->getValue().getCurveValueByX(currentIndex / (totalIndex - 1));
272 //std::cout << "current : "<< currentIndex <<" totalIndex : " << totalIndex << "sampler" << pr << std::endl;
273 if (pr != -1) { s = s * pr;}
274
275 return s;
276 }
277
278 template<typename TDataType>
280 {
281 auto SurfaceModule = this->graphicsPipeline()->template findFirstModule<GLSurfaceVisualModule>();
282 SurfaceModule->setVisible(this->varDisplaySurface()->getValue());
283
284 auto wireModule = this->graphicsPipeline()->template findFirstModule<GLWireframeVisualModule>();
285 wireModule->setVisible(this->varDisplayWireframe()->getValue());
286
287 auto pointModule = this->graphicsPipeline()->template findFirstModule<GLPointVisualModule>();
288 pointModule->setVisible(this->varDisplayPoints()->getValue());
289 }
290
292}
#define DEFINE_CLASS(name)
Definition Object.h:140
void polyClip(std::vector< DataType3f::Coord > vts, std::vector< TopologyModule::Triangle > &outTriangles)
std::shared_ptr< GraphicsPipeline > graphicsPipeline()
Definition Node.cpp:320
Quat< Real > computeQuaternion()
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
void displayChanged()
Definition Sweep.cpp:279
Vec3f RealScale()
Definition Sweep.cpp:266
float totalIndex
Definition Sweep.h:71
void resetStates() override
Definition Sweep.cpp:63
TDataType::Real Real
Definition Sweep.h:35
void varChanged()
Definition Sweep.cpp:71
float currentIndex
Definition Sweep.h:70
TDataType::Coord Coord
Definition Sweep.h:36
Vector< PointType, 3 > Triangle
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
DYN_FUNC Complex< Real > acos(const Complex< Real > &)
Definition Complex.inl:519
Vector< float, 3 > Vec3f
Definition Vector3D.h:93
Array< T, DeviceType::CPU > CArray
Definition Array.h:151
unsigned int uint
Definition VkProgram.h:14