PeriDyno 1.0.0
Loading...
Searching...
No Matches
VoxelOctree.h
Go to the documentation of this file.
1
16#pragma once
17
18#include "Topology/TriangleSet.h"
21#include "Vector.h"
22
23namespace dyno
24{
25 typedef unsigned short OcIndex;
26 typedef unsigned long long int OcKey;
27 typedef unsigned short Level;
28
29#define MAX_LEVEL 15
30#define DIM 3
31
33 {
34 OcKey key = 0;
35 OcKey mask = 1;
36
37 for (int i = 0; i < MAX_LEVEL; i++)
38 {
39 key |= (x & mask << i) << 2 * i | (y & mask << i) << (2 * i + 1) | (z & mask << i) << (2 * i + 2);
40 }
41 return key;
42 }
43 DYN_FUNC static void RecoverFromMortonCode(OcKey key, OcIndex& x, OcIndex& y, OcIndex& z)
44 {
45 x = 0;
46 y = 0;
47 z = 0;
48
49 for (int i = 0; i < MAX_LEVEL; i++)
50 {
51 OcKey x_buf = ((key >> 3 * i) & 1U) << i;
52 OcKey y_buf = ((key >> (3 * i + 1)) & 1U) << i;
53 OcKey z_buf = ((key >> (3 * i + 2)) & 1U) << i;
54
55 x |= x_buf;
56 y |= y_buf;
57 z |= z_buf;
58 }
59 }
60
61 template<typename TCoord>
63 {
64 public:
65 DYN_FUNC VoxelOctreeNode();
66 DYN_FUNC VoxelOctreeNode(Level l, OcKey key);
67 DYN_FUNC VoxelOctreeNode(Level l, OcIndex x, OcIndex y, OcIndex z);
68 DYN_FUNC VoxelOctreeNode(Level l, OcIndex x, OcIndex y, OcIndex z, TCoord point_pos);
69
70 DYN_FUNC bool operator> (const VoxelOctreeNode<TCoord>&) const;
71 DYN_FUNC inline bool isContainedStrictlyIn(const VoxelOctreeNode<TCoord>&) const;
72
73 DYN_FUNC inline OcKey key() const { return m_key; }
74 DYN_FUNC inline Level level() const { return m_level; }
75 DYN_FUNC inline TCoord position() const { return m_position; }
76 DYN_FUNC inline bool midside() const { return midside_node; }
77 DYN_FUNC inline int child() const { return m_first_child_loc; }
78 DYN_FUNC inline int value() const { return m_value_loc; }
79
80 DYN_FUNC inline void setKey(OcKey key) { m_key=key; }
81 DYN_FUNC inline void setLevel(Level lev) { m_level = lev; }
82 DYN_FUNC inline void setMidsideNode() { midside_node = true; }
83 DYN_FUNC inline void setChildIndex(int id) { m_first_child_loc = id; }
84 DYN_FUNC inline void setValueLocation(int id) { m_value_loc = id; }
85 DYN_FUNC inline void setPosition(TCoord pos) { m_position = pos; }
86
87 DYN_FUNC void plusChildIndex(int id);
88
89 int m_neighbor[6]; //x-1.x+1,y-1,y+1,z-1,z+1
90
91 protected:
94
96
97 bool midside_node = false;
98
101 };
102
103 template<typename TCoord>
105 : m_key(0)
106 , m_level(0)
107 {
108 m_position = TCoord(0, 0, 0);
109
110 for (int i = 0; i < 6; i++)
111 {
112 m_neighbor[i] = EMPTY;
113 }
114 }
115
116 template<typename TCoord>
118 : m_key(key)
119 , m_level(l)
120 {
121 m_position = TCoord(0, 0, 0);
122
123 for (int i = 0; i < 6; i++)
124 {
125 m_neighbor[i] = EMPTY;
126 }
127 }
128
129 template<typename TCoord>
131 : m_key(0)
132 , m_level(l)
133 {
134 m_key = CalculateMortonCode(x, y, z);
135
136 m_position = TCoord(0, 0, 0);
137
138 for (int i = 0; i < 6; i++)
139 {
140 m_neighbor[i] = EMPTY;
141 }
142 }
143
144 template<typename TCoord>
146 : m_key(0)
147 , m_level(l)
148 {
149 m_key = CalculateMortonCode(x, y, z);
150
151 m_position = point_pos;
152
153 for (int i = 0; i < 6; i++)
154 {
155 m_neighbor[i] = EMPTY;
156 }
157 }
158
159 template<typename TCoord>
161 {
162 if (m_level >= mc2.m_level)
163 {
164 return false;
165 }
166
167 auto k1 = m_key >> 3 * (mc2.m_level - m_level);
168 auto k2 = mc2.key();
169
170 return k1 == k2;
171 }
172
173 template<typename TCoord>
175 {
176 if (isContainedStrictlyIn(mc2))
177 {
178 return true;
179 }
180
181 auto k1 = m_key;
182 auto k2 = mc2.m_key;
183
184 m_level < mc2.m_level ? (k1 = k1 >> 3 * (mc2.m_level - m_level)) : (k2 = k2 >> 3 * (m_level - mc2.m_level));
185
186 return k1 > k2;
187 }
188
189 template<typename TCoord>
191 {
194 else
195 m_first_child_loc += id;
196 }
197
198 template<typename TDataType>
200 {
201 DECLARE_TCLASS(VoxelOctree, TDataType)
202 public:
203 typedef typename TDataType::Real Real;
204 typedef typename TDataType::Coord Coord;
205
207 ~VoxelOctree() override;
208
209
210 DYN_FUNC inline uint size() const { return m_octree.size(); }
211
212 DYN_FUNC inline void setLevelNum(int num) { level_num = num; }
213 DYN_FUNC inline void setGrid(int nx, int ny, int nz) { m_nx = nx; m_ny = ny; m_nz = nz; }
214 DYN_FUNC inline void setDx(Real dx) { m_dx = dx; }
215 DYN_FUNC inline void setOrigin(Coord origin) { m_origin = origin; }
216 DYN_FUNC inline void setLevel0(int level0) { m_level0 = level0; }
217
218 DYN_FUNC inline int getLevelNum() { return level_num; }
219 DYN_FUNC inline Real getDx() { return m_dx; }
220 DYN_FUNC inline Coord getOrigin() { return m_origin; }
221 DYN_FUNC inline Coord getTopOrigin() { return (m_origin + m_dx * Coord(m_nx, m_ny, m_nz)); }
222 DYN_FUNC inline int getLevel0() { return m_level0; }
223 DYN_FUNC inline void getGrid(int& nx, int& ny, int& nz) { nx = m_nx; ny = m_ny; nz = m_nz; }
224
225 GPU_FUNC inline VoxelOctreeNode<Coord>& operator [] (unsigned int id) {
226 return m_octree[id];
227 }
228
230
231 GPU_FUNC void getNode(int point_i, int point_j, int point_k, VoxelOctreeNode<Coord>& node_index,int& id)
232 {
233 Real top_index = pow(Real(2), int(level_num - 1));
234 int top_nx = m_nx / top_index;
235 int top_ny = m_ny / top_index;
236 int top_nz = m_nz / top_index;
237 int top_level_start = m_octree.size() - top_nx * top_ny*top_nz;
238
239 int i_top = std::floor(point_i / top_index);
240 int j_top = std::floor(point_j / top_index);
241 int k_top = std::floor(point_k / top_index);
242
243 int point_index = i_top + j_top * top_nx + k_top * top_nx*top_ny;
244
245 id = top_level_start + point_index;
246 VoxelOctreeNode<Coord> node = m_octree[top_level_start + point_index];
247
248 while (node.midside())
249 {
250 int l_this = node.level();
251 int i_this = std::floor(point_i / pow(Real(2), int(l_this - 1)));
252 int j_this = std::floor(point_j / pow(Real(2), int(l_this - 1)));
253 int k_this = std::floor(point_k / pow(Real(2), int(l_this - 1)));
254 OcKey key_this = CalculateMortonCode(i_this, j_this, k_this);
255 int gIndex = key_this & 7U;
256
257 int child_index = node.child();
258 id = child_index + (7 - gIndex);
259 node = m_octree[child_index + (7 - gIndex)];
260 //std::printf("the_first_node: gIndex: %d; the level is: %d; the key is: %lld; the distance is %f; the first child loc is: %d \n", gIndex, node.level(), node.key(), node.value(), node.child());
261 }
262 node_index = node;
263 }
264
265 void getLeafs(DArray<Coord>& pos, DArray<int>& pos_pos);
266
267 //返回所有的叶节点的顶点坐标
269 //返回最精细一层的叶节点坐标
271 //返回均匀网格(dx)的坐标
273 //返回最精细一层内部的叶节点坐标
275
277
279 return sdfValues;
280 };
281
283
284 //void getOctreeBW(DArray<int>& nodes_bw);
285
287
290
291
293 DArray<Coord> point_pos,
294 DArray<Real>& point_sdf,
295 DArray<Coord>& point_normal,
296 bool inverted = false);
297
299 DArray<Coord> point_pos,
300 DArray<Real>& point_sdf);
301
303 DArray<Coord> point_pos,
304 DArray<Real>& point_sdf,
305 DArray<Coord>& point_normal,
306 bool inverted = false);
307
308 private:
309 int m_nx;
310 int m_ny;
311 int m_nz;
312
313 int level_num = 3;
315
318
322 };
323
324}
#define DECLARE_TCLASS(name, T1)
Definition Object.h:87
#define MAX_LEVEL
Definition VoxelOctree.h:29
ObjectId id
Definition Object.h:131
void getSignDistance(DArray< Coord > point_pos, DArray< Real > &point_sdf, DArray< Coord > &point_normal, bool inverted=false)
void getCellVertices(DArray< Coord > &pos)
DYN_FUNC Coord getOrigin()
TDataType::Coord Coord
void getLeafsValue(DArray< Coord > &pos, DArray< Real > &val)
DYN_FUNC void getGrid(int &nx, int &ny, int &nz)
DYN_FUNC Real getDx()
DArrayList< int > & getNeighbors()
void getLeafs(DArray< Coord > &pos, DArray< int > &pos_pos)
DArray< VoxelOctreeNode< Coord > > & getVoxelOctree()
DYN_FUNC int getLevel0()
DArray< Real > & getSdfValues()
DYN_FUNC void setLevel0(int level0)
GPU_FUNC void getNode(int point_i, int point_j, int point_k, VoxelOctreeNode< Coord > &node_index, int &id)
DYN_FUNC void setGrid(int nx, int ny, int nz)
void getCellVertices0(DArray< Coord > &pos)
DYN_FUNC int getLevelNum()
DYN_FUNC uint size() const
DArray< VoxelOctreeNode< Coord > > m_octree
DYN_FUNC void setDx(Real dx)
void getSignDistanceMLS(DArray< Coord > point_pos, DArray< Real > &point_sdf, DArray< Coord > &point_normal, bool inverted=false)
DArrayList< int > m_neighbors
GPU_FUNC VoxelOctreeNode< Coord > & operator[](unsigned int id)
~VoxelOctree() override
void setSdfValues(DArray< Real > &vals)
TDataType::Real Real
DArray< Real > sdfValues
DYN_FUNC Coord getTopOrigin()
void getCellVertices1(DArray< Coord > &pos)
void getCellVertices2(DArray< Coord > &pos)
void setVoxelOctree(DArray< VoxelOctreeNode< Coord > > &oct)
DYN_FUNC void setLevelNum(int num)
void getSignDistanceKernel(DArray< Coord > point_pos, DArray< Real > &point_sdf)
DYN_FUNC void setOrigin(Coord origin)
DYN_FUNC VoxelOctreeNode()
DYN_FUNC bool midside() const
Definition VoxelOctree.h:76
DYN_FUNC void setLevel(Level lev)
Definition VoxelOctree.h:81
DYN_FUNC TCoord position() const
Definition VoxelOctree.h:75
DYN_FUNC OcKey key() const
Definition VoxelOctree.h:73
DYN_FUNC bool isContainedStrictlyIn(const VoxelOctreeNode< TCoord > &) const
DYN_FUNC void plusChildIndex(int id)
DYN_FUNC void setKey(OcKey key)
Definition VoxelOctree.h:80
DYN_FUNC int child() const
Definition VoxelOctree.h:77
DYN_FUNC void setChildIndex(int id)
Definition VoxelOctree.h:83
DYN_FUNC void setValueLocation(int id)
Definition VoxelOctree.h:84
DYN_FUNC void setMidsideNode()
Definition VoxelOctree.h:82
DYN_FUNC void setPosition(TCoord pos)
Definition VoxelOctree.h:85
DYN_FUNC int value() const
Definition VoxelOctree.h:78
DYN_FUNC bool operator>(const VoxelOctreeNode< TCoord > &) const
DYN_FUNC Level level() const
Definition VoxelOctree.h:74
This is an implementation of AdditiveCCD based on peridyno.
Definition Array.h:25
Array< T, DeviceType::GPU > DArray
Definition Array.inl:89
DYN_FUNC Complex< Real > pow(const Complex< Real > &, const Real &)
Definition Complex.inl:370
static DYN_FUNC void RecoverFromMortonCode(OcKey key, OcIndex &x, OcIndex &y, OcIndex &z)
Definition VoxelOctree.h:43
constexpr int EMPTY
static DYN_FUNC OcKey CalculateMortonCode(OcIndex x, OcIndex y, OcIndex z)
Definition VoxelOctree.h:32
ArrayList< ElementType, DeviceType::GPU > DArrayList
Definition ArrayList.inl:83
unsigned long long int OcKey
unsigned int uint
Definition VkProgram.h:14