PeriDyno 1.0.0
Loading...
Searching...
No Matches
CopyModel.cu
Go to the documentation of this file.
1#include "CopyModel.h"
2#include "Topology/PointSet.h"
3#include "GLSurfaceVisualModule.h"
4#include "GLWireframeVisualModule.h"
5#include "GLPointVisualModule.h"
6
7
8
9namespace dyno
10{
11 template<typename TDataType>
12 CopyModel<TDataType>::CopyModel()
13 : ParametricModel<TDataType>()
14 {
15 this->stateTriangleSet()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
16
17 auto glModule = std::make_shared<GLSurfaceVisualModule>();
18 glModule->setColor(Color(0.8f, 0.52f, 0.25f));
19 glModule->setVisible(true);
20 this->stateTriangleSet()->connect(glModule->inTriangleSet());
21 this->graphicsPipeline()->pushModule(glModule);
22
23 auto glModule2 = std::make_shared<GLPointVisualModule>();
24 glModule2->setColor(Color(1.0f, 0.1f, 0.1f));
25 glModule2->setVisible(false);
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(0.0f, 0.0f, 0.0f));
32 glModule3->setVisible(false);
33 this->stateTriangleSet()->connect(glModule3->inEdgeSet());
34 this->graphicsPipeline()->pushModule(glModule3);
35 }
36
37 template<typename TDataType>
38 void CopyModel<TDataType>::resetStates()
39 {
40 auto center = this->varCopyTransform()->getData();
41 auto rot = this->varCopyRotation()->getData();
42 auto scale = this->varCopyScale()->getData();
43
44 auto triangleSet = this->stateTriangleSet()->getDataPtr();
45
46 auto VertexIn = this->inTriangleSetIn()->getData().getPoints();
47 auto TriangleIn = this->inTriangleSetIn()->getData().getTriangles();
48
49 std::vector<Coord> vertices;
50 std::vector<TopologyModule::Triangle> triangle;
51
52 CArray<TopologyModule::Triangle> c_triangle;
53 CArray<Coord> c_point;
54 c_point.assign(VertexIn);
55 c_triangle.assign(TriangleIn);
56
57 int lengthV = VertexIn.size();
58 int lengthT = TriangleIn.size();
59 unsigned CopyNumber = this->varTotalNumber()->getData() - 1 ;
60
61 Coord Location;
62 //copy data to vector
63 for (int i = 0; i < lengthV; i++)
64 {
65 Location = { c_point[i][0], c_point[i][1], c_point[i][2] };
66 vertices.push_back( Location );
67 }
68 for (int i = 0; i < lengthT; i++)
69 {
70 triangle.push_back(TopologyModule::Triangle(c_triangle[i][0], c_triangle[i][1], c_triangle[i][2]));
71 }
72
73
74
75 //build copy vertices
76 if (this->varTotalNumber()->getValue() > 1)
77 {
78
79 for(int i = 0;i < CopyNumber; i++)
80 {
81 //rotator
82 auto rot = this->varCopyRotation()->getValue() * (i + 1);
83
84 Quat<Real> q =
85 Quat<Real>(Real(M_PI) * rot[2] / 180, Coord(0, 0, 1))
86 * Quat<Real>(Real(M_PI) * rot[1] / 180, Coord(0, 1, 0))
87 * Quat<Real>(Real(M_PI) * rot[0] / 180, Coord(1, 0, 0));
88 q.normalize();
89
90 auto RV = [&](const Coord& v)->Coord {
91 return q.rotate(v );
92 };
93
94 Vec3f RealScale = scale;
95 if (this->varScaleMode()->getData() == 0)
96 {
97 for (int k = 0; k < i; k++)
98 {
99 RealScale *= scale;
100 }
101 }
102 if (this->varScaleMode()->getData() == 1)
103 {
104 RealScale = scale * 1 / (i + 1);
105 }
106
107 for (int j = 0; j < lengthV; j++)
108 {
109 Location = { c_point[j][0], c_point[j][1], c_point[j][2] };
110
111 Location = RV(Location * RealScale) + center * (i+1);
112 vertices.push_back(Location);
113 }
114 }
115
116 //Triangle
117 for (int i = 0; i < CopyNumber; i++)
118 {
119 for (int j = 0; j < lengthT; j++)
120 {
121 ;
122 triangle.push_back(TopologyModule::Triangle(c_triangle[j][0] + (i + 1) * lengthV, c_triangle[j][1] + (i + 1) * lengthV, c_triangle[j][2] + (i + 1) * lengthV));
123
124 }
125
126 }
127 }
128
129 //TransformModel
130
131 auto centert = this->varLocation()->getData();
132 auto rott = this->varRotation()->getData();
133 auto scalet= this->varScale()->getData();
134
135 Quat<Real> qt = this->computeQuaternion();
136
137 qt.normalize();
138
139 auto RVt = [&](const Coord& v)->Coord {
140 return centert + qt.rotate(v - centert);
141 };
142
143 int numptt = vertices.size();
144
145 for (int i = 0; i < numptt; i++)
146 {
147 vertices[i] = RVt(vertices[i] * scalet + RVt(centert));
148 }
149
150 triangleSet->setPoints(vertices);
151 triangleSet->setTriangles(triangle);
152
153
154 triangleSet->update();
155
156 vertices.clear();
157 triangle.clear();
158 }
159
160 DEFINE_CLASS(CopyModel);
161}