-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTriangleHierarchy.cpp
More file actions
241 lines (221 loc) · 7.54 KB
/
Copy pathTriangleHierarchy.cpp
File metadata and controls
241 lines (221 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include "TriangleHierarchy.h"
#include <iostream>
#include <unordered_set>
#include <set>
using pool_set = std::set<unsigned int>;
using namespace PBD;
using namespace std;
BVHOnFaces::BVHOnFaces()
:super(0)
{
}
Vector3r const& BVHOnFaces::entity_position(unsigned int i)const
{
return m_faceCenter[i];
}
void BVHOnFaces::init(vector<vector<unsigned int >> faces, const unsigned int numFaces, const Vector3r* vertices, const unsigned int numVertices)
{
m_lst.resize(numFaces);
m_faceCenter.resize(numFaces);
m_faces = faces;
m_numFaces = numFaces;
m_vertices = vertices;
m_numVertices = numVertices;
//³õʼ»¯¸÷¸öÃæµÄÖÐÐÄ
for (unsigned int i = 0; i < numFaces; i++)
{
for (unsigned int j = 0; j < 3; j++)
{
m_faceCenter[i] += m_vertices[m_faces[i][j]];
m_faceIndex.push_back(faces[i][j]);
}
m_faceCenter[i] /= 3.0;
}
}
void BVHOnFaces::compute_hull_approx(unsigned int b, unsigned int n, BoundingSphere & hull) const
{
// compute center
Vector3r x;
x.setZero();
for (unsigned int i = b; i < b + n; i++)
for (unsigned int j = 0; j < 3; j++)
{
Vector3r y = m_vertices[m_faces[m_lst[i]][j]];
x += y;
}
x /= (float)(n * 3);
float radius2 = 0.0;
for (unsigned int i = b; i < b + n; i++)
{
for (unsigned int j = 0; j < 3; j++)
{
radius2 = std::max(radius2, (x - m_vertices[m_faces[m_lst[i]][j]]).squaredNorm());
}
}
// cout << hull.x()[0] << " " << hull.x()[1] << " " << hull.x()[2] << endl;
hull.x()[0] = x[0];
hull.x()[1] = x[1];
hull.x()[2] = x[2];
// cout << hull.x()[0] << " " << hull.x()[1] << " " << hull.x()[2] << endl;
// cout << hull.r();
hull.r() = sqrt(radius2);
// cout << " " << hull.r() << sqrt(radius2) << endl;
}
Vector3r PBD::BVHOnFaces::getFaceCenter(unsigned int node_index)
{
return m_faceCenter[m_lst[m_nodes[node_index].begin]];
}
vector<Vector3r> BVHOnFaces::getFace(unsigned int node_index)
{
unsigned int index = getFaceIndex(node_index);
vector<unsigned int> indice = m_faces[index];
vector<Vector3r> face;
for (int i = 0; i < 3; i++)
{
face.push_back(m_vertices[indice[i]]);
}
return face;
}
unsigned int PBD::BVHOnFaces::getIndice(unsigned int node_index, unsigned int i)
{
unsigned int index = getFaceIndex(node_index);
return m_faces[index][i];
}
void PBD::BVHOnFaces::copyHullsMemory()
{
cudaFree(d_hulls);
cudaError_t err = cudaMalloc((BoundingSphere * *)& d_hulls, sizeof(BoundingSphere) * m_hulls.size());
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate bvhtree.inl : d_hulls (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(d_hulls, m_hulls.data(), sizeof(BoundingSphere) * m_hulls.size(), cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy bvhtree.inl : d_hulls (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
//d_vertices
cudaFree(d_vertices);
err = cudaMalloc((Vector3r * *)& d_vertices, sizeof(Vector3r) * m_numVertices);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate bvhtree.inl : d_vertices (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(d_vertices, m_vertices, sizeof(Vector3r) * m_numVertices, cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy bvhtree.inl : d_vertices (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
}
void PBD::BVHOnFaces::copyMemory()
{
//d_lst
cudaError_t err = cudaMalloc((unsigned int**)& d_lst, sizeof(unsigned int) * m_lst.size());
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate bvhtree.inl : d_lst (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(d_lst, m_lst.data(), sizeof(unsigned int) * m_lst.size(), cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy bvhtree.inl : d_lst (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
//d_nodes
err = cudaMalloc((Node**)& d_nodes, sizeof(Node) * m_nodes.size());
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate bvhtree.inl : d_nodes (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(d_nodes, m_nodes.data(), sizeof(Node) * m_nodes.size(), cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy bvhtree.inl : d_nodes (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
//d_hulls
err = cudaMalloc((BoundingSphere * *)& d_hulls, sizeof(BoundingSphere) * m_hulls.size());
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate bvhtree.inl : d_hulls (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(d_hulls, m_hulls.data(), sizeof(BoundingSphere) * m_hulls.size(), cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy bvhtree.inl : d_hulls (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
//d_index32
err = cudaMalloc((unsigned int * *)& d_index32, sizeof(unsigned int) * m_index32.size());
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate bvhtree.inl : d_index32 (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(d_index32, m_index32.data(), sizeof(unsigned int) * m_index32.size(), cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy bvhtree.inl : d_index32 (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
//d_leaf
err = cudaMalloc((unsigned int**)& d_leaf, sizeof(unsigned int) * m_leaf.size());
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate bvhtree.inl : d_leaf (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(d_leaf, m_leaf.data(), sizeof(unsigned int) * m_leaf.size(), cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy bvhtree.inl : d_leaf (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
//d_faceIndex
err = cudaMalloc((unsigned int**)& d_faceIndex, sizeof(unsigned int) * m_faceIndex.size());
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate bvhtree.inl : d_faceIndex (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(d_faceIndex, m_faceIndex.data(), sizeof(unsigned int) * m_faceIndex.size(), cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy bvhtree.inl : d_faceIndex (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
//d_vertices
err = cudaMalloc((Vector3r **)& d_vertices, sizeof(Vector3r) * m_numVertices);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate bvhtree.inl : d_vertices (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(d_vertices, m_vertices, sizeof(Vector3r) * m_numVertices, cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy bvhtree.inl : d_vertices (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
//d_faceCenter
err = cudaMalloc((Vector3r * *)& d_faceCenter, sizeof(Vector3r) * m_faceCenter.size());
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to allocate bvhtree.inl : d_faceCenter (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
err = cudaMemcpy(d_faceCenter, m_faceCenter.data(), sizeof(Vector3r) * m_faceCenter.size(), cudaMemcpyHostToDevice);
if (err != cudaSuccess)
{
fprintf(stderr, "Failed to copy bvhtree.inl : d_faceCenter (error code:: %s)!\n", cudaGetErrorString(err));
exit(EXIT_FAILURE);
}
printf("success\n");
}