PeriDyno 1.0.0
Loading...
Searching...
No Matches
ExtractShape.cu
Go to the documentation of this file.
1#include "ExtractShape.h"
2#include "GLPhotorealisticRender.h"
3#include "Mapping/TextureMeshToTriangleSet.h"
4#include "GLWireframeVisualModule.h"
5#include "GLSurfaceVisualModule.h"
6
7namespace dyno
8{
9
10 template< typename uint >
11 __global__ void GetShapeVerticesRange(
12 DArray<uint> shapeIDs,
13 int* min,
14 int* max,
15 int i
16 )
17 {
18 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
19 if (pId >= shapeIDs.size()) return;
20
21 if (shapeIDs[pId] == i)
22 {
23 atomicMax(max, pId);
24 atomicMin(min, pId);
25 }
26 }
27
28 template< typename Vec3f , typename Vec2i>
29 __global__ void extractShapeVertices(
30 DArray<Vec3f> inVertices,
31 DArray<Vec3f> vertices,
32 DArray<Vec2i> idRange,
33 int id,
34 int offset
35 )
36 {
37 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
38 if (pId >= inVertices.size()) return;
39
40
41 Vec2i range = idRange[id];
42
43 if (pId <= range[1] && pId >= range[0])
44 {
45 vertices[pId - range[0] + offset] = inVertices[pId];
46 }
47 }
48
49 template< typename Vec2f, typename Vec2i>
50 __global__ void extractShapeVec2f(
51 DArray<Vec2f> inTexcoords,
52 DArray<Vec2f> texcoords,
53 DArray<Vec2i> idRange,
54 int id,
55 int offset
56 )
57 {
58 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
59 if (pId >= inTexcoords.size()) return;
60
61
62 Vec2i range = idRange[id];
63
64 if (pId <= range[1] && pId >= range[0])
65 {
66 texcoords[pId - range[0] + offset] = inTexcoords[pId];
67 }
68 }
69
70 template< typename uint, typename Vec2i>
71 __global__ void extractShapeIds(
72 DArray<uint> inShapeIds,
73 DArray<uint> shapeIds,
74 DArray<Vec2i> idRange,
75 int id,
76 int offset,
77 int newId
78 )
79 {
80 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
81 if (pId >= inShapeIds.size()) return;
82
83
84 Vec2i range = idRange[id];
85
86 if (pId <= range[1] && pId >= range[0])
87 {
88 shapeIds[pId - range[0] + offset] = newId;
89 }
90 }
91
92 template< typename Triangle >
93 __global__ void extractShapeTriangles(
94 DArray<Triangle> inTriangle,
95 DArray<Triangle> triangle,
96 int offset,
97 int verticesOffset
98 )
99 {
100 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
101 if (pId >= inTriangle.size()) return;
102
103
104 triangle[pId][0] = inTriangle[pId][0] - offset + verticesOffset;
105 triangle[pId][1] = inTriangle[pId][1] - offset + verticesOffset;
106 triangle[pId][2] = inTriangle[pId][2] - offset + verticesOffset;
107
108
109 }
110
111
112 template<typename TDataType>
113 ExtractShape<TDataType>::ExtractShape()
114 {
115 auto inTexMesh = this->inInTextureMesh();
116 auto convertModule = std::make_shared<TextureMeshToTriangleSet<DataType3f>>();
117 inTexMesh->connect(convertModule->inTextureMesh());
118
119 //auto wireRender = std::make_shared<GLWireframeVisualModule>();
120 //convertModule->outTriangleSet()->connect(wireRender->inEdgeSet());
121 //wireRender->setColor(Color::Black());
122 //wireRender->setVisible(false);
123
124 auto surfaceRender = std::make_shared<GLSurfaceVisualModule>();
125 convertModule->outTriangleSet()->connect(surfaceRender->inTriangleSet());
126 surfaceRender->setAlpha(0.1);
127 surfaceRender->setColor(Color::LightGray());
128
129 //this->graphicsPipeline()->pushModule(wireRender);
130
131 this->graphicsPipeline()->pushModule(convertModule);
132 this->graphicsPipeline()->pushModule(surfaceRender);
133
134 this->stateResult()->setDataPtr(std::make_shared<TextureMesh>());
135 this->stateResult()->promoteOuput();
136
137 auto render = std::make_shared<GLPhotorealisticRender>();
138 this->stateResult()->connect(render->inTextureMesh());
139
140 this->graphicsPipeline()->pushModule(render);
141 this->setForceUpdate(false);
142 }
143
144 template<typename TDataType>
145 ExtractShape<TDataType>::~ExtractShape()
146 {
147 }
148
149
150 template<typename TDataType>
151 void ExtractShape<TDataType>::resetStates()
152 {
153 auto extractId = this->varShapeId()->getValue();
154 //std::sort(extractId.begin(), extractId.end());
155 //extractId.erase(std::unique(extractId.begin(), extractId.end()), extractId.end());
156
157 auto inTexMesh = this->inInTextureMesh()->constDataPtr();
158
159 for (size_t i = 0; i < extractId.size(); i++)
160 {
161 if (extractId[i] >= inTexMesh->shapes().size())
162 return;
163 }
164
165 auto out = this->stateResult()->getDataPtr();
166
167 std::vector<Vec2i> shapeVerticesRange;
168 std::vector<int> shapeVerticesSize;
169 shapeVerticesRange.resize(inTexMesh->shapes().size());
170 shapeVerticesSize.resize(inTexMesh->shapes().size());
171
172 //get Shape_VerticesRange
173 for (int i = 0; i < inTexMesh->shapes().size(); i++)
174 {
175 int max = INT_MIN;
176 int min = INT_MAX;
177
178 int* d_max;
179 int* d_min;
180
181 cudaMalloc(&d_max, sizeof(int));
182 cudaMalloc(&d_min, sizeof(int));
183
184 cudaMemcpy(d_max, &max, sizeof(int), cudaMemcpyHostToDevice);
185 cudaMemcpy(d_min, &min, sizeof(int), cudaMemcpyHostToDevice);
186
187 cuExecute(inTexMesh->shapeIds().size(),
188 GetShapeVerticesRange,
189 inTexMesh->shapeIds(),
190 d_min,
191 d_max,
192 i
193 );
194
195 cudaMemcpy(&max, d_max, sizeof(int), cudaMemcpyDeviceToHost);
196 cudaMemcpy(&min, d_min, sizeof(int), cudaMemcpyDeviceToHost);
197
198 shapeVerticesRange[i] = Vec2i(min,max);
199
200 }
201
202 //get Shape_VerticesSize
203 for (size_t i = 0; i < shapeVerticesRange.size(); i++)
204 {
205 shapeVerticesSize[i] = shapeVerticesRange[i][1] - shapeVerticesRange[i][0] + 1;
206 }
207
208 uint count = 0;
209
210 for (auto it : extractId)
211 count += shapeVerticesSize[it];
212
213 DArray<Vec3f> d_extractVertices;
214 d_extractVertices.resize(count);
215
216 DArray<Vec3f> d_extractNormals;
217 d_extractNormals.resize(count);
218
219 DArray<Vec2f> d_extractTexCoords;
220 d_extractTexCoords.resize(count);
221
222 DArray<uint>d_extractShapeIds;
223 d_extractShapeIds.resize(count);
224
225 DArray<Vec2i> d_range;
226 d_range.assign(shapeVerticesRange);
227
228 DArray<int> d_size;
229 d_size.assign(shapeVerticesSize);
230
231 int verticesOffset = 0;
232
233 auto inMaterial = inTexMesh->materials();
234 std::vector<std::shared_ptr<Material>>outMaterials;
235
236 for (auto it : inMaterial)
237 outMaterials.push_back(it);
238
239 auto inShapes = inTexMesh->shapes();
240
241 std::vector<std::shared_ptr<Shape>> outShapes;
242
243
244 auto userTransform = this->varShapeTransform()->getValue();
245
246 for (size_t i = 0; i < extractId.size(); i++)
247 {
248 auto id = extractId[i];
249
250 cuExecute(inTexMesh->vertices().size(),
251 extractShapeVertices,
252 inTexMesh->vertices(),
253 d_extractVertices,
254 d_range,
255 id,
256 verticesOffset
257 );
258
259 cuExecute(inTexMesh->vertices().size(),
260 extractShapeVertices,
261 inTexMesh->normals(),
262 d_extractNormals,
263 d_range,
264 id,
265 verticesOffset
266 );
267
268 cuExecute(inTexMesh->vertices().size(),
269 extractShapeVec2f,
270 inTexMesh->texCoords(),
271 d_extractTexCoords,
272 d_range,
273 id,
274 verticesOffset
275 );
276
277 cuExecute(inTexMesh->vertices().size(),
278 extractShapeIds,
279 inTexMesh->shapeIds(),
280 d_extractShapeIds,
281 d_range,
282 id,
283 verticesOffset,
284 i
285 );
286
287 int triangleIndexOffset = shapeVerticesRange[id][0];
288
289 DArray<Triangle> d_triangle;
290 d_triangle.resize(inTexMesh->shapes()[id]->vertexIndex.size());
291
292 cuExecute(inTexMesh->shapes()[id]->vertexIndex.size(),
293 extractShapeTriangles,
294 inTexMesh->shapes()[id]->vertexIndex,
295 d_triangle,
296 triangleIndexOffset,
297 verticesOffset
298 );
299
300 verticesOffset += shapeVerticesSize[id];
301
302 if (id < inShapes.size())
303 {
304 auto element = std::make_shared<Shape>();
305 auto currentShape = inShapes[id];
306 element->vertexIndex.assign(d_triangle);
307 element->normalIndex.assign(d_triangle);
308 element->texCoordIndex.assign(d_triangle);
309 element->boundingBox = currentShape->boundingBox;
310
311 if (i < userTransform.size())
312 {
313 auto currentT = currentShape->boundingTransform;
314 auto userT = userTransform[i];
315 if (this->varOffset()->getValue())
316 {
317 element->boundingTransform.translation() = currentT.translation() + userT.translation();
318 element->boundingTransform.scale() = currentT.scale() * userT.scale();
319 element->boundingTransform.rotation() = userT.rotation() * currentT.rotation();
320 }
321 else
322 {
323 element->boundingTransform.translation() = userT.translation();
324 element->boundingTransform.scale() = userT.scale();
325 element->boundingTransform.rotation() = currentT.rotation();
326 }
327 }
328 else
329 element->boundingTransform = currentShape->boundingTransform;
330
331 element->material = currentShape->material;
332
333 outShapes.push_back(element);
334
335 d_triangle.clear();
336 }
337
338 }
339
340 out->vertices().assign(d_extractVertices);
341 out->normals().assign(d_extractNormals);
342 out->texCoords().assign(d_extractTexCoords);
343 out->shapeIds().assign(d_extractShapeIds);
344
345 out->materials() = outMaterials;
346 out->shapes() = outShapes;
347
348 d_extractVertices.clear();
349 d_extractNormals.clear();
350 d_extractTexCoords.clear();
351 d_extractShapeIds.clear();
352 d_range.clear();
353 d_size.clear();
354
355 }
356
357 DEFINE_CLASS(ExtractShape);
358
359}