PeriDyno 1.0.0
Loading...
Searching...
No Matches
TextureMeshToTriangleSet.cu
Go to the documentation of this file.
1#include "TextureMeshToTriangleSet.h"
2
3namespace dyno
4{
5 IMPLEMENT_TCLASS(TextureMeshToTriangleSet, TDataType)
6
7 template<typename TDataType>
8 TextureMeshToTriangleSet<TDataType>::TextureMeshToTriangleSet()
9 : TopologyMapping()
10 {
11 this->inTransform()->tagOptional(true);
12 }
13
14 template<typename TDataType>
15 TextureMeshToTriangleSet<TDataType>::~TextureMeshToTriangleSet()
16 {
17
18 }
19
20 template <typename Coord, typename Transform>
21 __global__ void TM2TS_TransformVertices(
22 DArray<Coord> vertices,
23 DArray<uint> shapeIds,
24 DArray<Transform> localTransform,
25 DArrayList<Transform> globalTransform)
26 {
27 int tId = threadIdx.x + (blockIdx.x * blockDim.x);
28 if (tId >= vertices.size()) return;
29
30 uint shapeId = shapeIds[tId];
31 Coord v = vertices[tId];
32
33 Transform locT = localTransform[shapeId];
34 //TODO: This is a temporary code
35 Transform globalT = globalTransform[shapeId][0];
36
37 Coord s = globalT.scale();
38
39 vertices[tId] = globalT.rotation() * Coord(v.x * s.x, v.y * s.y, v.z * s.z) + globalT.translation();
40 }
41
42 template <typename Coord, typename Transform>
43 __global__ void TransformVertices(
44 DArray<Coord> vertices,
45 DArray<uint> shapeIds,
46 DArray<Transform> localTransform
47 )
48 {
49 int tId = threadIdx.x + (blockIdx.x * blockDim.x);
50 if (tId >= vertices.size()) return;
51
52 uint shapeId = shapeIds[tId];
53 Coord v = vertices[tId];
54
55 Transform locT = localTransform[shapeId];
56 Coord s = locT.scale();
57
58 vertices[tId] = locT.rotation() * Coord(v.x * s.x, v.y * s.y, v.z * s.z) + locT.translation();
59
60 }
61
62
63 template<typename TDataType>
64 bool TextureMeshToTriangleSet<TDataType>::apply()
65 {
66 if (this->outTriangleSet()->isEmpty()) {
67 this->outTriangleSet()->allocate();
68 }
69
70 auto mesh = this->inTextureMesh()->constDataPtr();
71
72 uint indexNum = 0;
73 for (uint i = 0; i < mesh->shapes().size(); i++) {
74 indexNum += mesh->shapes()[i]->vertexIndex.size();
75 }
76
77 auto ts = this->outTriangleSet()->getDataPtr();
78
79 auto& vertices = ts->getPoints();
80 auto& indices = ts->getTriangles();
81
82 vertices.assign(mesh->vertices());
83
84 if (indices.size() != indexNum)
85 {
86 indices.resize(indexNum);
87 }
88
89 uint offset = 0;
90 for (uint i = 0; i < mesh->shapes().size(); i++) {
91 uint num = mesh->shapes()[i]->vertexIndex.size();
92 indices.assign(mesh->shapes()[i]->vertexIndex, num, offset, 0);
93
94 offset += num;
95 }
96
97 if (!this->inTransform()->isEmpty())
98 {
99 uint N = mesh->shapes().size();
100 CArray<Transform> hostT(N);
101 DArray<Transform> devT(N);
102
103 for (uint i = 0; i < N; i++)
104 {
105 hostT[i] = mesh->shapes()[i]->boundingTransform;
106 }
107
108 devT.assign(hostT);
109
110 cuExecute(vertices.size(),
111 TM2TS_TransformVertices,
112 vertices,
113 mesh->shapeIds(),
114 devT,
115 this->inTransform()->constData());
116
117 hostT.clear();
118 devT.clear();
119 }
120 else
121 {
122 uint N = mesh->shapes().size();
123 CArray<Transform> hostT(N);
124 DArray<Transform> devT(N);
125
126 for (uint i = 0; i < N; i++)
127 {
128 hostT[i] = mesh->shapes()[i]->boundingTransform;
129 }
130
131 devT.assign(hostT);
132
133 cuExecute(vertices.size(),
134 TransformVertices,
135 vertices,
136 mesh->shapeIds(),
137 devT
138 );
139
140 hostT.clear();
141 devT.clear();
142 }
143
144 ts->update();
145
146 return true;
147 }
148
149 DEFINE_CLASS(TextureMeshToTriangleSet);
150
151
152 IMPLEMENT_TCLASS(TextureMeshToTriangleSetNode, TDataType);
153
154 template<typename TDataType>
155 TextureMeshToTriangleSetNode<TDataType>::TextureMeshToTriangleSetNode()
156 : Node()
157 {
158 this->setForceUpdate(false);
159
160 mTM2TS = std::make_shared<TextureMeshToTriangleSet<TDataType>>();
161
162 this->inTextureMesh()->connect(mTM2TS->inTextureMesh());
163 mTM2TS->outTriangleSet()->connect(this->outTriangleSet());
164 }
165
166 template<typename TDataType>
167 void TextureMeshToTriangleSetNode<TDataType>::resetStates()
168 {
169 auto mesh = this->inTextureMesh()->constDataPtr();
170
171 uint N = mesh->shapes().size();
172
173 CArrayList<Transform3f> hostT;
174 hostT.resize(N, 1);
175
176 for (uint i = 0; i < N; i++)
177 {
178 hostT[i].insert(mesh->shapes()[i]->boundingTransform);
179 }
180
181 mTM2TS->inTransform()->assign(hostT);
182
183 hostT.clear();
184
185 mTM2TS->update();
186 }
187
188 template<typename TDataType>
189 void TextureMeshToTriangleSetNode<TDataType>::updateStates()
190 {
191 auto mesh = this->inTextureMesh()->constDataPtr();
192
193 uint N = mesh->shapes().size();
194
195 CArrayList<Transform3f> hostT;
196 hostT.resize(N, 1);
197
198 for (uint i = 0; i < N; i++)
199 {
200 hostT[i].insert(mesh->shapes()[i]->boundingTransform);
201 }
202
203 mTM2TS->inTransform()->assign(hostT);
204
205 hostT.clear();
206
207 mTM2TS->update();
208 }
209
210 DEFINE_CLASS(TextureMeshToTriangleSetNode);
211}