1#include "HexahedronSet.h"
7#include <thrust/sort.h>
11 template<typename TDataType>
12 HexahedronSet<TDataType>::HexahedronSet()
13 : QuadSet<TDataType>()
18 template<typename TDataType>
19 HexahedronSet<TDataType>::~HexahedronSet()
23 template<typename TDataType>
24 void HexahedronSet<TDataType>::setHexahedrons(std::vector<Hexahedron>& hexahedrons)
26 std::vector<Quad> quads;
28 m_hexahedrons.resize(hexahedrons.size());
29 m_hexahedrons.assign(hexahedrons);
34 template<typename TDataType>
35 void HexahedronSet<TDataType>::setHexahedrons(DArray<Hexahedron>& hexahedrons)
37 if (hexahedrons.size() != m_hexahedrons.size())
39 m_hexahedrons.resize(hexahedrons.size());
42 m_hexahedrons.assign(hexahedrons);
47 template<typename Hexahedron>
48 __global__ void HS_CountHexs(
50 DArray<Hexahedron> hexs)
52 int tId = threadIdx.x + (blockIdx.x * blockDim.x);
53 if (tId >= hexs.size()) return;
55 Hexahedron t = hexs[tId];
57 atomicAdd(&counter[t[0]], 1);
58 atomicAdd(&counter[t[1]], 1);
59 atomicAdd(&counter[t[2]], 1);
60 atomicAdd(&counter[t[3]], 1);
61 atomicAdd(&counter[t[4]], 1);
62 atomicAdd(&counter[t[5]], 1);
63 atomicAdd(&counter[t[6]], 1);
64 atomicAdd(&counter[t[7]], 1);
67 template<typename Hexahedron>
68 __global__ void HS_SetupHexIds(
69 DArrayList<int> hexIds,
70 DArray<Hexahedron> hexs)
72 int tId = threadIdx.x + (blockIdx.x * blockDim.x);
73 if (tId >= hexs.size()) return;
75 Hexahedron t = hexs[tId];
77 hexIds[t[0]].atomicInsert(tId);
78 hexIds[t[1]].atomicInsert(tId);
79 hexIds[t[2]].atomicInsert(tId);
80 hexIds[t[3]].atomicInsert(tId);
81 hexIds[t[4]].atomicInsert(tId);
82 hexIds[t[5]].atomicInsert(tId);
83 hexIds[t[6]].atomicInsert(tId);
84 hexIds[t[7]].atomicInsert(tId);
87 template<typename TDataType>
88 DArrayList<int>& HexahedronSet<TDataType>::getVer2Hex()
91 counter.resize(this->mCoords.size());
94 cuExecute(m_hexahedrons.size(),
99 m_ver2Hex.resize(counter);
102 cuExecute(m_hexahedrons.size(),
112 template<typename TDataType>
113 void HexahedronSet<TDataType>::getVolume(DArray<Real>& volume)
118 template<typename QKey, typename Hexahedron>
119 __global__ void HS_SetupKeys(
122 DArray<Hexahedron> hexs)
124 int tId = threadIdx.x + (blockIdx.x * blockDim.x);
125 if (tId >= hexs.size()) return;
127 Hexahedron hex = hexs[tId];
128 keys[6 * tId] = QKey(hex[0], hex[1], hex[2], hex[3]);
129 keys[6 * tId + 1] = QKey(hex[4], hex[5], hex[6], hex[7]);
130 keys[6 * tId + 2] = QKey(hex[0], hex[1], hex[5], hex[4]);
131 keys[6 * tId + 3] = QKey(hex[1], hex[2], hex[6], hex[5]);
132 keys[6 * tId + 4] = QKey(hex[3], hex[2], hex[6], hex[7]);
133 keys[6 * tId + 5] = QKey(hex[0], hex[3], hex[7], hex[4]);
136 ids[6 * tId + 1] = tId;
137 ids[6 * tId + 2] = tId;
138 ids[6 * tId + 3] = tId;
139 ids[6 * tId + 4] = tId;
140 ids[6 * tId + 5] = tId;
143 template<typename QKey>
144 __global__ void HS_CountQuadNumber(
148 int tId = threadIdx.x + (blockIdx.x * blockDim.x);
149 if (tId >= keys.size()) return;
151 if (tId == 0 || keys[tId] != keys[tId - 1])
157 template<typename Quad, typename Quad2Hex, typename QKey>
158 __global__ void HS_SetupQuads(
160 DArray<Quad2Hex> quad2Hex,
165 int tId = threadIdx.x + (blockIdx.x * blockDim.x);
166 if (tId >= keys.size()) return;
168 int shift = counter[tId];
169 if (tId == 0 || keys[tId] != keys[tId - 1])
171 QKey key = keys[tId];
172 quads[shift] = Quad(key[0], key[1], key[2], key[3]);
174 Quad2Hex q2H(EMPTY, EMPTY);
175 q2H[0] = hexIds[tId];
177 if (tId + 1 < keys.size() && keys[tId + 1] == key)
178 q2H[1] = hexIds[tId + 1];
180 quad2Hex[shift] = q2H;
185 template<typename QKey>
186 void printTKey(DArray<QKey> keys, int maxLength) {
188 h_keys.resize(keys.size());
191 int psize = min((int)h_keys.size(), maxLength);
192 for (int i = 0; i < psize; i++)
194 printf("%d: %d %d %d %d \n", i, h_keys[i][0], h_keys[i][1], h_keys[i][2], h_keys[i][3]);
200 /*void printCount(DArray<int> keys, int maxLength) {
202 h_keys.resize(keys.size());
205 int psize = minimum((int)h_keys.size(), maxLength);
206 for (int i = 0; i < psize; i++)
208 printf("%d: %d \n", i, h_keys[i]);
214 template<typename TDataType>
215 void HexahedronSet<TDataType>::updateQuads()
217 uint hexSize = m_hexahedrons.size();
222 keys.resize(6 * hexSize);
223 hexIds.resize(6 * hexSize);
231 thrust::sort_by_key(thrust::device, keys.begin(), keys.begin() + keys.size(), hexIds.begin());
234 counter.resize(6 * hexSize);
236 cuExecute(keys.size(),
241 int quadNum = thrust::reduce(thrust::device, counter.begin(), counter.begin() + counter.size());
242 thrust::exclusive_scan(thrust::device, counter.begin(), counter.begin() + counter.size(), counter.begin());
244 quad2Hex.resize(quadNum);
246 auto& pQuad = this->getQuads();
247 pQuad.resize(quadNum);
248 cuExecute(keys.size(),
261// this->updateTriangles();
262// this->updateEdges();
266 template<typename TDataType>
267 void HexahedronSet<TDataType>::copyFrom(HexahedronSet<TDataType> hexSet)
269 m_hexahedrons.resize(hexSet.m_hexahedrons.size());
270 m_hexahedrons.assign(hexSet.m_hexahedrons);
272 quad2Hex.resize(hexSet.quad2Hex.size());
273 quad2Hex.assign(hexSet.quad2Hex);
275 m_ver2Hex.assign(hexSet.m_ver2Hex);
277 QuadSet<TDataType>::copyFrom(hexSet);
280 DEFINE_CLASS(HexahedronSet);