PeriDyno 1.0.0
Loading...
Searching...
No Matches
PlaneModel.cpp
Go to the documentation of this file.
1#include "PlaneModel.h"
2
5
7#include "Mapping/Extract.h"
8
9namespace dyno
10{
11 template<typename TDataType>
13 : BasicShape<TDataType>()
14 {
15 this->varLengthX()->setRange(0.01, 100.0f);
16 this->varLengthZ()->setRange(1, 100.0f);
17
18 this->varSegmentX()->setRange(1, 100);
19 this->varSegmentZ()->setRange(1, 100);
20
21
22 this->statePolygonSet()->setDataPtr(std::make_shared<PolygonSet<TDataType>>());
23
24 this->stateTriangleSet()->setDataPtr(std::make_shared<TriangleSet<TDataType>>());
25
26 this->stateQuadSet()->setDataPtr(std::make_shared<QuadSet<TDataType>>());
27
28
29 auto callback = std::make_shared<FCallBackFunc>(std::bind(&PlaneModel<TDataType>::varChanged, this));
30
31 this->varLocation()->attach(callback);
32 this->varScale()->attach(callback);
33 this->varRotation()->attach(callback);
34
35 this->varSegmentX()->attach(callback);
36 this->varSegmentZ()->attach(callback);
37
38 this->varLengthX()->attach(callback);
39 this->varLengthZ()->attach(callback);
40
41
42 auto tsRender = std::make_shared<GLSurfaceVisualModule>();
43 this->stateTriangleSet()->connect(tsRender->inTriangleSet());
44 this->graphicsPipeline()->pushModule(tsRender);
45
46 auto exES = std::make_shared<ExtractEdgeSetFromPolygonSet<TDataType>>();
47 this->statePolygonSet()->connect(exES->inPolygonSet());
48 this->graphicsPipeline()->pushModule(exES);
49
50 auto esRender = std::make_shared<GLWireframeVisualModule>();
51 esRender->varBaseColor()->setValue(Color(0, 0, 0));
52 //exES->outEdgeSet()->connect(esRender->inEdgeSet());
53 this->stateTriangleSet()->connect(esRender->inEdgeSet());
54 this->graphicsPipeline()->pushModule(esRender);
55
56 //this->statePolygonSet()->promoteOuput();
57 this->stateTriangleSet()->promoteOuput();
58 this->stateQuadSet()->promoteOuput();
59
60 this->stateTriangleSet()->promoteOuput();
61 this->stateQuadSet()->promoteOuput();
62 this->statePolygonSet()->promoteOuput();
63 }
64
66 {
67 Index2DPlane() : x(), y() {}
68 Index2DPlane(int x, int y) : x(x), y(y) {}
69 int x, y;
70 };
71
72 bool operator<(const Index2DPlane& lhs, const Index2DPlane& rhs)
73 {
74 return lhs.x != rhs.x ? lhs.x < rhs.x : lhs.y < rhs.y;
75 }
76
77 template<typename TDataType>
79 {
80 auto center = this->varLocation()->getValue();
81 auto rot = this->varRotation()->getValue();
82 auto scale = this->varScale()->getValue();
83
84 Coord length;
85 length[0] *= scale[0];
86 length[1] = 1;
87 length[2] *= scale[2];
88
89 Quat<Real> q = this->computeQuaternion();
90
91 q.normalize();
92
94 box.center = center;
95 box.u = q.rotate(Coord(1, 0, 0));
96 box.v = q.rotate(Coord(0, 1, 0));
97 box.w = q.rotate(Coord(0, 0, 1));
98 box.extent = length;
99
100 auto AABB = box.aabb();
101 auto& v0 = AABB.v0;
102 auto& v1 = AABB.v1;
103
104
105 NBoundingBox ret;
106 ret.lower = Vec3f(v0.x, v0.y, v0.z);
107 ret.upper = Vec3f(v1.x, v1.y, v1.z);
108
109 return ret;
110 }
111
112 template<typename TDataType>
117
118 template<typename TDataType>
120 {
121 auto center = this->varLocation()->getData();
122 auto rot = this->varRotation()->getData();
123 auto scale = this->varScale()->getData();
124
125 auto segmentX = this->varSegmentX()->getData();
126 auto segmentZ = this->varSegmentZ()->getData();
127
128 auto lengthX = this->varLengthX()->getData();
129 auto lengthZ = this->varLengthZ()->getData();
130
131 Vec3f length = Vec3f(lengthX,1,lengthZ);
132 Vec3i segments = Vec3i(segmentX, 1, segmentZ);
133
134 lengthX *= scale[0];
135 lengthZ *= scale[2];
136
137 Quat<Real> q = this->computeQuaternion();
138
139 q.normalize();
140
141 std::vector<Coord> vertices;
142 std::vector<TopologyModule::Quad> quads;
143 std::vector<TopologyModule::Triangle> triangles;
144
145 Real dx = lengthX / segmentX;
146 Real dz = lengthZ / segmentZ;
147
148 //A lambda function to rotate a vertex
149 auto RV = [&](const Coord& v)->Coord {
150 return center + q.rotate(v);
151 };
152
153 uint numOfPolygon = segments[0] * segments[2];
154
155 CArray<uint> counter2(numOfPolygon);
156
157 uint incre = 0;
158
159 for (uint j = 0; j < numOfPolygon; j++)
160 {
161 counter2[incre] = 4;
162 incre++;
163 }
164
165 CArrayList<uint> polygonIndices;
166 polygonIndices.resize(counter2);
167
168 incre = 0;
169
170 Real x, y, z;
171 //Bottom
172 for (int nz = 0; nz <= segmentZ; nz++)
173 {
174 for (int nx = 0; nx <= segmentX; nx++)
175 {
176 x = nx * dx - lengthX / 2;
177 z = nz * dz - lengthZ / 2;
178 vertices.push_back(RV(Coord(x, Real(0), z)));
179 }
180 }
181
182 int v0;
183 int v1;
184 int v2;
185 int v3;
186
187 for (int nz = 0; nz < segmentZ; nz++)
188 {
189 for (int nx = 0; nx < segmentX; nx++)
190 {
191 v0 = nx + nz * (segmentX + 1);
192 v1 = nx + 1 + nz * (segmentX + 1);;
193 v2 = nx + 1 + (nz + 1) * (segmentX + 1);;
194 v3 = nx + (nz + 1) * (segmentX + 1);;
195
196 auto& quads = polygonIndices[incre];
197
198 if ((nx + nz) % 2 == 0) {
199 quads.insert(v3);
200 quads.insert(v2);
201 quads.insert(v1);
202 quads.insert(v0);
203 }
204 else {
205 quads.insert(v2);
206 quads.insert(v1);
207 quads.insert(v0);
208 quads.insert(v3);
209 }
210
211
212 incre++;
213 }
214 }
215
216 auto polySet = this->statePolygonSet()->getDataPtr();
217
218 polySet->setPoints(vertices);
219 polySet->setPolygons(polygonIndices);
220 polySet->update();
221
222 polygonIndices.clear();
223
224 auto& ts = this->stateTriangleSet()->getData();
225 polySet->turnIntoTriangleSet(ts);
226
227 auto& qs = this->stateQuadSet()->getData();
228 polySet->extractQuadSet(qs);
229
230 vertices.clear();
231
232 }
233
235}
#define DEFINE_CLASS(name)
Definition Object.h:140
double Real
Definition Typedef.inl:23
std::shared_ptr< GraphicsPipeline > graphicsPipeline()
Definition Node.cpp:320
Quat< Real > computeQuaternion()
TDataType::Coord Coord
Definition PlaneModel.h:33
TDataType::Real Real
Definition PlaneModel.h:32
NBoundingBox boundingBox() override
void resetStates() override
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
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
Vector< int, 3 > Vec3i
Definition Vector3D.h:95
ArrayList< T, DeviceType::CPU > CArrayList
Definition ArrayList.h:207
DYN_FUNC bool operator<(const priority_queue< T, Container, Compare > &a, const priority_queue< T, Container, Compare > &b)
::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
Index2DPlane(int x, int y)