1#include "ExtractShape.h"
2#include "GLPhotorealisticRender.h"
3#include "Mapping/TextureMeshToTriangleSet.h"
4#include "GLWireframeVisualModule.h"
5#include "GLSurfaceVisualModule.h"
10 template< typename uint >
11 __global__ void GetShapeVerticesRange(
12 DArray<uint> shapeIDs,
18 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
19 if (pId >= shapeIDs.size()) return;
21 if (shapeIDs[pId] == i)
28 template< typename Vec3f , typename Vec2i>
29 __global__ void extractShapeVertices(
30 DArray<Vec3f> inVertices,
31 DArray<Vec3f> vertices,
32 DArray<Vec2i> idRange,
37 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
38 if (pId >= inVertices.size()) return;
41 Vec2i range = idRange[id];
43 if (pId <= range[1] && pId >= range[0])
45 vertices[pId - range[0] + offset] = inVertices[pId];
49 template< typename Vec2f, typename Vec2i>
50 __global__ void extractShapeVec2f(
51 DArray<Vec2f> inTexcoords,
52 DArray<Vec2f> texcoords,
53 DArray<Vec2i> idRange,
58 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
59 if (pId >= inTexcoords.size()) return;
62 Vec2i range = idRange[id];
64 if (pId <= range[1] && pId >= range[0])
66 texcoords[pId - range[0] + offset] = inTexcoords[pId];
70 template< typename uint, typename Vec2i>
71 __global__ void extractShapeIds(
72 DArray<uint> inShapeIds,
73 DArray<uint> shapeIds,
74 DArray<Vec2i> idRange,
80 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
81 if (pId >= inShapeIds.size()) return;
84 Vec2i range = idRange[id];
86 if (pId <= range[1] && pId >= range[0])
88 shapeIds[pId - range[0] + offset] = newId;
92 template< typename Triangle >
93 __global__ void extractShapeTriangles(
94 DArray<Triangle> inTriangle,
95 DArray<Triangle> triangle,
100 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
101 if (pId >= inTriangle.size()) return;
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;
112 template<typename TDataType>
113 ExtractShape<TDataType>::ExtractShape()
115 auto inTexMesh = this->inInTextureMesh();
116 auto convertModule = std::make_shared<TextureMeshToTriangleSet<DataType3f>>();
117 inTexMesh->connect(convertModule->inTextureMesh());
119 //auto wireRender = std::make_shared<GLWireframeVisualModule>();
120 //convertModule->outTriangleSet()->connect(wireRender->inEdgeSet());
121 //wireRender->setColor(Color::Black());
122 //wireRender->setVisible(false);
124 auto surfaceRender = std::make_shared<GLSurfaceVisualModule>();
125 convertModule->outTriangleSet()->connect(surfaceRender->inTriangleSet());
126 surfaceRender->setAlpha(0.1);
127 surfaceRender->setColor(Color::LightGray());
129 //this->graphicsPipeline()->pushModule(wireRender);
131 this->graphicsPipeline()->pushModule(convertModule);
132 this->graphicsPipeline()->pushModule(surfaceRender);
134 this->stateResult()->setDataPtr(std::make_shared<TextureMesh>());
135 this->stateResult()->promoteOuput();
137 auto render = std::make_shared<GLPhotorealisticRender>();
138 this->stateResult()->connect(render->inTextureMesh());
140 this->graphicsPipeline()->pushModule(render);
141 this->setForceUpdate(false);
144 template<typename TDataType>
145 ExtractShape<TDataType>::~ExtractShape()
150 template<typename TDataType>
151 void ExtractShape<TDataType>::resetStates()
153 auto extractId = this->varShapeId()->getValue();
154 //std::sort(extractId.begin(), extractId.end());
155 //extractId.erase(std::unique(extractId.begin(), extractId.end()), extractId.end());
157 auto inTexMesh = this->inInTextureMesh()->constDataPtr();
159 for (size_t i = 0; i < extractId.size(); i++)
161 if (extractId[i] >= inTexMesh->shapes().size())
165 auto out = this->stateResult()->getDataPtr();
167 std::vector<Vec2i> shapeVerticesRange;
168 std::vector<int> shapeVerticesSize;
169 shapeVerticesRange.resize(inTexMesh->shapes().size());
170 shapeVerticesSize.resize(inTexMesh->shapes().size());
172 //get Shape_VerticesRange
173 for (int i = 0; i < inTexMesh->shapes().size(); i++)
181 cudaMalloc(&d_max, sizeof(int));
182 cudaMalloc(&d_min, sizeof(int));
184 cudaMemcpy(d_max, &max, sizeof(int), cudaMemcpyHostToDevice);
185 cudaMemcpy(d_min, &min, sizeof(int), cudaMemcpyHostToDevice);
187 cuExecute(inTexMesh->shapeIds().size(),
188 GetShapeVerticesRange,
189 inTexMesh->shapeIds(),
195 cudaMemcpy(&max, d_max, sizeof(int), cudaMemcpyDeviceToHost);
196 cudaMemcpy(&min, d_min, sizeof(int), cudaMemcpyDeviceToHost);
198 shapeVerticesRange[i] = Vec2i(min,max);
202 //get Shape_VerticesSize
203 for (size_t i = 0; i < shapeVerticesRange.size(); i++)
205 shapeVerticesSize[i] = shapeVerticesRange[i][1] - shapeVerticesRange[i][0] + 1;
210 for (auto it : extractId)
211 count += shapeVerticesSize[it];
213 DArray<Vec3f> d_extractVertices;
214 d_extractVertices.resize(count);
216 DArray<Vec3f> d_extractNormals;
217 d_extractNormals.resize(count);
219 DArray<Vec2f> d_extractTexCoords;
220 d_extractTexCoords.resize(count);
222 DArray<uint>d_extractShapeIds;
223 d_extractShapeIds.resize(count);
225 DArray<Vec2i> d_range;
226 d_range.assign(shapeVerticesRange);
229 d_size.assign(shapeVerticesSize);
231 int verticesOffset = 0;
233 auto inMaterial = inTexMesh->materials();
234 std::vector<std::shared_ptr<Material>>outMaterials;
236 for (auto it : inMaterial)
237 outMaterials.push_back(it);
239 auto inShapes = inTexMesh->shapes();
241 std::vector<std::shared_ptr<Shape>> outShapes;
244 auto userTransform = this->varShapeTransform()->getValue();
246 for (size_t i = 0; i < extractId.size(); i++)
248 auto id = extractId[i];
250 cuExecute(inTexMesh->vertices().size(),
251 extractShapeVertices,
252 inTexMesh->vertices(),
259 cuExecute(inTexMesh->vertices().size(),
260 extractShapeVertices,
261 inTexMesh->normals(),
268 cuExecute(inTexMesh->vertices().size(),
270 inTexMesh->texCoords(),
277 cuExecute(inTexMesh->vertices().size(),
279 inTexMesh->shapeIds(),
287 int triangleIndexOffset = shapeVerticesRange[id][0];
289 DArray<Triangle> d_triangle;
290 d_triangle.resize(inTexMesh->shapes()[id]->vertexIndex.size());
292 cuExecute(inTexMesh->shapes()[id]->vertexIndex.size(),
293 extractShapeTriangles,
294 inTexMesh->shapes()[id]->vertexIndex,
300 verticesOffset += shapeVerticesSize[id];
302 if (id < inShapes.size())
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;
311 if (i < userTransform.size())
313 auto currentT = currentShape->boundingTransform;
314 auto userT = userTransform[i];
315 if (this->varOffset()->getValue())
317 element->boundingTransform.translation() = currentT.translation() + userT.translation();
318 element->boundingTransform.scale() = currentT.scale() * userT.scale();
319 element->boundingTransform.rotation() = userT.rotation() * currentT.rotation();
323 element->boundingTransform.translation() = userT.translation();
324 element->boundingTransform.scale() = userT.scale();
325 element->boundingTransform.rotation() = currentT.rotation();
329 element->boundingTransform = currentShape->boundingTransform;
331 element->material = currentShape->material;
333 outShapes.push_back(element);
340 out->vertices().assign(d_extractVertices);
341 out->normals().assign(d_extractNormals);
342 out->texCoords().assign(d_extractTexCoords);
343 out->shapeIds().assign(d_extractShapeIds);
345 out->materials() = outMaterials;
346 out->shapes() = outShapes;
348 d_extractVertices.clear();
349 d_extractNormals.clear();
350 d_extractTexCoords.clear();
351 d_extractShapeIds.clear();
357 DEFINE_CLASS(ExtractShape);