PeriDyno 1.0.0
Loading...
Searching...
No Matches
TextureMeshMerge.cu
Go to the documentation of this file.
1#include "TextureMeshMerge.h"
2#include "GLPhotorealisticRender.h"
3
4namespace dyno
5{
6 template<typename Vec3f>
7 __global__ void mergeVec3f(
8 DArray<Vec3f> v0,
9 DArray<Vec3f> v1,
10 DArray<Vec3f> target,
11 int sizeV0
12 )
13 {
14 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
15 if (pId >= target.size()) return;
16
17 if (pId < sizeV0)
18 target[pId] = v0[pId];
19 else
20 target[pId] = v1[pId - sizeV0];
21 }
22
23 template<typename Vec2f>
24 __global__ void mergeVec2f(
25 DArray<Vec2f> v0,
26 DArray<Vec2f> v1,
27 DArray<Vec2f> target,
28 int sizeV0
29 )
30 {
31 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
32 if (pId >= target.size()) return;
33
34 if (pId < sizeV0)
35 target[pId] = v0[pId];
36 else
37 target[pId] = v1[pId - sizeV0];
38 }
39
40 __global__ void mergeUint(
41 DArray<uint> v0,
42 DArray<uint> v1,
43 DArray<uint> target,
44 int sizeV0
45 )
46 {
47 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
48 if (pId >= target.size()) return;
49
50 if (pId < sizeV0)
51 target[pId] = v0[pId];
52 else
53 target[pId] = v1[pId - sizeV0];
54 }
55
56 __global__ void mergeShapeId(
57 DArray<uint> v0,
58 DArray<uint> v1,
59 DArray<uint> target,
60 int sizeV0,
61 int shapeSize
62 )
63 {
64 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
65 if (pId >= target.size()) return;
66
67 if (pId < sizeV0)
68 target[pId] = v0[pId];
69 else
70 target[pId] = v1[pId - sizeV0] + shapeSize;
71 }
72
73 template<typename Triangle>
74 __global__ void updateVertexIndex(
75 DArray<Triangle> v,
76 DArray<Triangle> target,
77 int offset
78 )
79 {
80 int pId = threadIdx.x + (blockIdx.x * blockDim.x);
81 if (pId >= target.size()) return;
82
83 target[pId] = Triangle(v[pId][0] + offset, v[pId][1] + offset, v[pId][2] + offset);
84 }
85
86 template<typename TDataType>
87 TextureMeshMerge<TDataType>::TextureMeshMerge()
88 {
89 this->stateTextureMesh()->setDataPtr(std::make_shared<TextureMesh>());
90
91 auto render = std::make_shared<GLPhotorealisticRender>();
92 this->stateTextureMesh()->connect(render->inTextureMesh());
93 this->stateTextureMesh()->promoteOuput();
94
95 this->graphicsPipeline()->pushModule(render);
96 this->setForceUpdate(false);
97 }
98
99 template<typename TDataType>
100 TextureMeshMerge<TDataType>::~TextureMeshMerge()
101 {
102 }
103
104 template<typename TDataType>
105 void TextureMeshMerge<TDataType>::merge(const std::shared_ptr<TextureMesh>& texMesh01, const std::shared_ptr<TextureMesh>& texMesh02, std::shared_ptr<TextureMesh>& out)
106 {
107 auto vertices01 = texMesh01->vertices();
108 auto vertices02 = texMesh02->vertices();
109
110 out->vertices().resize(vertices01.size() + vertices02.size());
111
112 cuExecute(out->vertices().size(),
113 mergeVec3f,
114 vertices01,
115 vertices02,
116 out->vertices(),
117 vertices01.size()
118 );
119
120 auto normals01 = texMesh01->normals();
121 auto normals02 = texMesh02->normals();
122 out->normals().resize(normals01.size() + normals02.size());
123
124 cuExecute(out->normals().size(),
125 mergeVec3f,
126 normals01,
127 normals02,
128 out->normals(),
129 normals01.size()
130 );
131
132 auto texCoords01 = texMesh01->texCoords();
133 auto texCoords02 = texMesh02->texCoords();
134 out->texCoords().resize(texCoords01.size() + texCoords02.size());
135
136 cuExecute(out->texCoords().size(),
137 mergeVec2f,
138 texCoords01,
139 texCoords02,
140 out->texCoords(),
141 texCoords01.size()
142 );
143
144 auto shapeIds01 = texMesh01->shapeIds();
145 auto shapeIds02 = texMesh02->shapeIds();
146 out->shapeIds().resize(shapeIds01.size() + shapeIds02.size());
147
148 cuExecute(out->texCoords().size(),
149 mergeShapeId,
150 shapeIds01,
151 shapeIds02,
152 out->shapeIds(),
153 shapeIds01.size(),
154 texMesh01->shapes().size()
155 );
156
157
158 auto material01 = texMesh01->materials();
159 auto material02 = texMesh02->materials();
160
161 auto outMaterials = out->materials();
162 outMaterials.clear();
163
164 for (auto it : material01)
165 outMaterials.push_back(it);
166
167 for (auto it : material02)
168 outMaterials.push_back(it);
169
170
171 auto shapes01 = texMesh01->shapes();
172 auto shapes02 = texMesh02->shapes();
173
174 std::vector<std::shared_ptr<Shape>> outShapes;
175
176 for (auto it : shapes01)
177 {
178 auto element = std::make_shared<Shape>();
179 element->vertexIndex.assign(it->vertexIndex);
180 element->normalIndex.assign(it->normalIndex);
181 element->texCoordIndex.assign(it->texCoordIndex);
182 element->boundingBox = it->boundingBox;
183 element->boundingTransform = it->boundingTransform;
184 element->material = it->material;
185
186 outShapes.push_back(element);
187 }
188
189
190 for (auto it : shapes02)
191 {
192 auto element = std::make_shared<Shape>();
193 element->vertexIndex.assign(it->vertexIndex);
194 element->normalIndex.assign(it->normalIndex);
195 element->texCoordIndex.assign(it->texCoordIndex);
196 element->boundingBox = it->boundingBox;
197 element->boundingTransform = it->boundingTransform;
198 element->material = it->material;
199
200 outShapes.push_back(element);
201
202 cuExecute(it->vertexIndex.size(),
203 updateVertexIndex,
204 it->vertexIndex,
205 element->vertexIndex,
206 vertices01.size()
207 );
208
209 cuExecute(it->normalIndex.size(),
210 updateVertexIndex,
211 it->normalIndex,
212 element->normalIndex,
213 normals01.size()
214 );
215
216 cuExecute(it->texCoordIndex.size(),
217 updateVertexIndex,
218 it->texCoordIndex,
219 element->texCoordIndex,
220 texCoords01.size()
221 );
222
223 }
224
225 out->shapes() = outShapes;
226 out->materials() = outMaterials;
227
228
229 out->vertices();
230 }
231
232 template<typename TDataType>
233 void TextureMeshMerge<TDataType>::resetStates()
234 {
235 auto texMesh01 = this->inFirst()->constDataPtr();
236 auto texMesh02 = this->inSecond()->constDataPtr();
237
238 auto out = this->stateTextureMesh()->getDataPtr();
239
240 this->merge(texMesh01,texMesh02,out);
241 }
242
243 DEFINE_CLASS(TextureMeshMerge);
244
245}