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