ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ecvPointCloudLOD.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 // CV_DB_LIB
11 #include "ecvFrustum.h"
12 #include "ecvOctree.h"
13 
14 // Qt
15 #include <QMutex>
16 
17 // system
18 #include <stdint.h>
19 
20 #include <array>
21 #include <functional>
22 
23 class ccPointCloud;
25 
27 struct LODLevelDesc {
31  LODLevelDesc(unsigned _startIndex, unsigned _count)
32  : startIndex(_startIndex), count(_count) {}
34  unsigned startIndex;
36  unsigned count;
37 };
38 
40 typedef std::vector<unsigned> LODIndexSet;
41 
44 public:
47 
51  virtual ~ccPointCloudLOD();
52 
54  bool init(ccPointCloud* cloud);
55 
57  inline void lock() { m_mutex.lock(); }
59  inline void unlock() { m_mutex.unlock(); }
60 
62  inline State getState() {
63  lock();
64  State state = m_state;
65  unlock();
66  return state;
67  }
68 
70  void clear();
71 
73  const ccOctree::Shared& octree() const { return m_octree; }
74 
77  inline bool isNull() { return getState() == NOT_INITIALIZED; }
78 
80  inline bool isInitialized() { return getState() == INITIALIZED; }
81 
83  inline bool isUnderConstruction() {
84  return getState() == UNDER_CONSTRUCTION;
85  }
86 
88  inline bool isBroken() { return getState() == BROKEN; }
89 
91  inline unsigned char maxLevel() {
92  QMutexLocker locker(&m_mutex);
93  return (m_state == INITIALIZED
94  ? static_cast<unsigned char>(
95  std::max<size_t>(1, m_levels.size())) -
96  1
97  : 0);
98  }
99 
101  static const unsigned char UNDEFINED = 255;
102 
104  struct Node {
105  // Warning: put the non aligned members (< 4 bytes) at the end to avoid
106  // too much alignment padding!
107  uint32_t pointCount; // 4 bytes
108  float radius; // 4 bytes
109  CCVector3f center; // 12 bytes
110  std::array<int32_t, 8> childIndexes; // 32 bytes
111  uint32_t firstCodeIndex; // 4 bytes
112  uint32_t displayedPointCount; // 4 bytes
113  uint8_t level; // 1 byte
114  uint8_t childCount; // 1 byte
115  uint8_t intersection; // 1 byte
116 
117  // Total
118  // // 63 bytes (64 with alignment)
119 
121  Node(uint8_t _level = 0)
122  : pointCount(0),
123  radius(0),
124  center(0, 0, 0),
125  firstCodeIndex(0),
127  level(_level),
128  childCount(0),
130  childIndexes.fill(-1);
131  }
132  };
133 
134  inline Node& node(int32_t index, unsigned char level) {
135  assert(level < m_levels.size() && index >= 0 &&
136  index < m_levels[level].data.size());
137  return m_levels[level].data[index];
138  }
139 
140  inline const Node& node(int32_t index, unsigned char level) const {
141  assert(level < m_levels.size() && index >= 0 &&
142  index < m_levels[level].data.size());
143  return m_levels[level].data[index];
144  }
145 
146  inline Node& root() { return node(0, 0); }
147 
148  inline const Node& root() const { return node(0, 0); }
149 
150  // inline float maxRadius(unsigned char level) const
151  //{
152  // assert(level < m_levels.size());
153  // return (level < m_levels.size() ? m_levels[level].maxRadius : 0);
154  // }
155 
157 
159  uint32_t flagVisibility(const Frustum& frustum,
160  ccClipPlaneSet* clipPlanes = 0);
161 
163  LODIndexSet& getIndexMap(unsigned char level,
164  unsigned& maxCount,
165  unsigned& remainingPointsAtThisLevel);
166 
168  inline const LODIndexSet& getLasIndexMap() const { return m_lastIndexMap; }
169 
171  inline bool allDisplayed() const {
173  }
174 
176  size_t memory() const;
177 
178 protected: // methods
180 
182  bool initInternal(ccOctree::Shared octree);
183 
185  inline void setState(State state) {
186  lock();
187  m_state = state;
188  unlock();
189  }
190 
192  void clearExtended(bool autoStopThread, State newState);
193 
195  void clearData();
196 
198 
201  int32_t newCell(unsigned char level);
202 
204  void shrink_to_fit();
205 
207  // void updateMaxRadii();
208 
210 
213  void resetVisibility();
214 
217  uint32_t addNPointsToIndexMap(Node& node, uint32_t count);
218 
219 protected: // members
220  struct Level {
222  //: maxRadius(0)
223  {}
224 
225  std::vector<Node> data;
226  // float maxRadius;
227  };
228 
230  std::vector<Level> m_levels;
231 
233  struct RenderParams {
235  : visiblePoints(0),
236  displayedPoints(0),
237  unfinishedLevel(-1),
238  unfinishedPoints(0) {}
239 
241  uint32_t visiblePoints;
243  uint32_t displayedPoints;
248  };
249 
252 
255 
258 
261 
264 
266  QMutex m_mutex;
267 
270 };
271 
273 public:
274  typedef std::function<void(const ccPointCloudLOD::Node&)> RenderFunc;
275 
277  RenderFunc& func,
278  const Frustum& frustum,
279  unsigned char maxLevel)
280  : m_func(func), m_frustum(frustum), m_lod(lod), m_maxLevel(maxLevel) {}
281 
282  void render(const ccPointCloudLOD::Node& node, bool testVisibility) {
283  if (testVisibility) {
284  switch (m_frustum.sphereInFrustum(node.center, node.radius)) {
285  case Frustum::INSIDE:
286  testVisibility = false;
287  break;
288 
289  case Frustum::INTERSECT:
290  testVisibility = true;
291  break;
292 
293  case Frustum::OUTSIDE:
294  return;
295  }
296  }
297 
298  // go on with the display of the children first
299  if (node.childCount && node.level < m_maxLevel) {
300  for (int i = 0; i < 8; ++i) {
301  if (node.childIndexes[i] >= 0) {
302  const ccPointCloudLOD::Node& childNode =
303  m_lod.node(node.childIndexes[i], node.level + 1);
304  render(childNode, testVisibility);
305  }
306  }
307  } else {
308  m_func(node);
309  }
310  }
311 
315  unsigned char m_maxLevel;
316 };
PointCloudLODRenderer::m_frustum
const Frustum & m_frustum
Definition: ecvPointCloudLOD.h:313
ccPointCloudLOD::root
const Node & root() const
Definition: ecvPointCloudLOD.h:148
ccPointCloudLOD::getState
State getState()
Returns the current state.
Definition: ecvPointCloudLOD.h:62
ccPointCloudLOD::m_state
State m_state
State.
Definition: ecvPointCloudLOD.h:269
Frustum
Definition: ecvFrustum.h:85
ccPointCloudLODThread
Thread for background computation.
Definition: ecvPointCloudLOD.cpp:19
ccPointCloudLOD::clearExtended
void clearExtended(bool autoStopThread, State newState)
Clears the structure (with more options)
Frustum::INTERSECT
@ INTERSECT
Definition: ecvFrustum.h:98
Frustum::sphereInFrustum
Intersection sphereInFrustum(const CCVector3f &c, float r) const
Definition: ecvFrustum.h:113
PointCloudLODRenderer::RenderFunc
std::function< void(const ccPointCloudLOD::Node &)> RenderFunc
Definition: ecvPointCloudLOD.h:274
ccPointCloudLOD::Node::firstCodeIndex
uint32_t firstCodeIndex
Definition: ecvPointCloudLOD.h:111
LODIndexSet
std::vector< unsigned > LODIndexSet
L.O.D. indexes set.
Definition: ecvPointCloudLOD.h:40
ccPointCloudLOD::m_mutex
QMutex m_mutex
For concurrent access.
Definition: ecvPointCloudLOD.h:266
ccPointCloudLOD::isBroken
bool isBroken()
Returns whether the structure is broken or not.
Definition: ecvPointCloudLOD.h:88
ccPointCloudLOD::ccPointCloudLODThread
friend ccPointCloudLODThread
Definition: ecvPointCloudLOD.h:179
ccPointCloudLOD::Node::pointCount
uint32_t pointCount
Definition: ecvPointCloudLOD.h:107
ccClipPlaneSet
std::vector< ccClipPlane > ccClipPlaneSet
Definition: ecvDrawableObject.h:21
ccPointCloudLOD::~ccPointCloudLOD
virtual ~ccPointCloudLOD()
Destructor.
Definition: ecvPointCloudLOD.cpp:390
ccPointCloudLOD::RenderParams::unfinishedPoints
unsigned unfinishedPoints
Previously unfinished level.
Definition: ecvPointCloudLOD.h:247
ccPointCloudLOD::UNDEFINED
static const unsigned char UNDEFINED
Undefined visibility flag.
Definition: ecvPointCloudLOD.h:101
ccPointCloud
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
Definition: ecvPointCloud.h:146
ccPointCloudLOD::memory
size_t memory() const
Returns the memory used by the structure (in bytes)
Definition: ecvPointCloudLOD.cpp:392
ccPointCloudLOD::isNull
bool isNull()
Definition: ecvPointCloudLOD.h:77
ccPointCloudLOD::maxLevel
unsigned char maxLevel()
Returns the maximum accessible level.
Definition: ecvPointCloudLOD.h:91
ccPointCloudLOD::RenderParams::unfinishedLevel
int unfinishedLevel
Previously unfinished level.
Definition: ecvPointCloudLOD.h:245
ccPointCloudLOD::setState
void setState(State state)
Sets the current state.
Definition: ecvPointCloudLOD.h:185
ccOctree::Shared
QSharedPointer< ccOctree > Shared
Shared pointer.
Definition: ecvOctree.h:32
LODLevelDesc::LODLevelDesc
LODLevelDesc(unsigned _startIndex, unsigned _count)
Constructor from a start index and a count value.
Definition: ecvPointCloudLOD.h:31
ccPointCloudLOD::RenderParams
Parameters of the current render state.
Definition: ecvPointCloudLOD.h:233
LODLevelDesc::startIndex
unsigned startIndex
Start index (refers to the 'indexes' table)
Definition: ecvPointCloudLOD.h:34
ccPointCloudLOD::m_octree
ccOctree::Shared m_octree
Associated octree.
Definition: ecvPointCloudLOD.h:260
PointCloudLODRenderer::render
void render(const ccPointCloudLOD::Node &node, bool testVisibility)
Definition: ecvPointCloudLOD.h:282
ccPointCloudLOD::State
State
Structure initialization state.
Definition: ecvPointCloudLOD.h:46
ccPointCloudLOD::Node::radius
float radius
Definition: ecvPointCloudLOD.h:108
ecvFrustum.h
PointCloudLODRenderer::m_maxLevel
unsigned char m_maxLevel
Definition: ecvPointCloudLOD.h:315
ccPointCloudLOD::unlock
void unlock()
Unlocks the structure.
Definition: ecvPointCloudLOD.h:59
PointCloudLODRenderer::m_lod
ccPointCloudLOD & m_lod
Definition: ecvPointCloudLOD.h:314
Frustum::OUTSIDE
@ OUTSIDE
Definition: ecvFrustum.h:97
ccPointCloudLOD::RenderParams::displayedPoints
uint32_t displayedPoints
Number of already displayed points.
Definition: ecvPointCloudLOD.h:243
ccPointCloudLOD::root
Node & root()
Definition: ecvPointCloudLOD.h:146
ccPointCloudLOD::m_thread
ccPointCloudLODThread * m_thread
Computing thread.
Definition: ecvPointCloudLOD.h:263
Vector3Tpl< float >
LODLevelDesc::LODLevelDesc
LODLevelDesc()
Default constructor.
Definition: ecvPointCloudLOD.h:29
ccPointCloudLOD::UNDER_CONSTRUCTION
@ UNDER_CONSTRUCTION
Definition: ecvPointCloudLOD.h:46
ccPointCloudLOD::RenderParams::visiblePoints
uint32_t visiblePoints
Number of visible points (for the last visibility test)
Definition: ecvPointCloudLOD.h:241
ccPointCloudLOD::node
const Node & node(int32_t index, unsigned char level) const
Definition: ecvPointCloudLOD.h:140
ccPointCloudLOD::isInitialized
bool isInitialized()
Returns whether the structure is initialized or not.
Definition: ecvPointCloudLOD.h:80
ccPointCloudLOD::Node
Octree 'tree' node.
Definition: ecvPointCloudLOD.h:104
ccPointCloudLOD::clearData
void clearData()
Clears the internal (nodes) data.
Definition: ecvPointCloudLOD.cpp:427
ccPointCloudLOD::init
bool init(ccPointCloud *cloud)
Initializes the construction process (asynchronous)
Definition: ecvPointCloudLOD.cpp:405
ccPointCloudLOD::getIndexMap
LODIndexSet & getIndexMap(unsigned char level, unsigned &maxCount, unsigned &remainingPointsAtThisLevel)
Builds an index map with the remaining visible points.
Definition: ecvPointCloudLOD.cpp:738
ccPointCloudLOD::getLasIndexMap
const LODIndexSet & getLasIndexMap() const
Returns the last index map.
Definition: ecvPointCloudLOD.h:168
ccPointCloudLOD::resetVisibility
void resetVisibility()
Updates the max radius per level FOR ALL CELLS.
Definition: ecvPointCloudLOD.cpp:526
ccPointCloudLOD::Node::level
uint8_t level
Definition: ecvPointCloudLOD.h:113
ccPointCloudLOD
L.O.D. (Level of Detail) structure.
Definition: ecvPointCloudLOD.h:43
ccPointCloudLOD::Level::Level
Level()
Definition: ecvPointCloudLOD.h:221
ccPointCloudLOD::Node::Node
Node(uint8_t _level=0)
Default constructor.
Definition: ecvPointCloudLOD.h:121
ccPointCloudLOD::m_levels
std::vector< Level > m_levels
Per-level cells data.
Definition: ecvPointCloudLOD.h:230
ccPointCloudLOD::allDisplayed
bool allDisplayed() const
Returns whether all points have been displayed or not.
Definition: ecvPointCloudLOD.h:171
PointCloudLODRenderer::PointCloudLODRenderer
PointCloudLODRenderer(ccPointCloudLOD &lod, RenderFunc &func, const Frustum &frustum, unsigned char maxLevel)
Definition: ecvPointCloudLOD.h:276
ccPointCloudLOD::octree
const ccOctree::Shared & octree() const
Returns the associated octree.
Definition: ecvPointCloudLOD.h:73
PointCloudLODRenderer::m_func
RenderFunc m_func
Definition: ecvPointCloudLOD.h:312
ccPointCloudLOD::m_indexMap
LODIndexSet m_indexMap
Index map.
Definition: ecvPointCloudLOD.h:254
ccPointCloudLOD::INITIALIZED
@ INITIALIZED
Definition: ecvPointCloudLOD.h:46
ccPointCloudLOD::BROKEN
@ BROKEN
Definition: ecvPointCloudLOD.h:46
ccPointCloudLOD::Node::childCount
uint8_t childCount
Definition: ecvPointCloudLOD.h:114
ccPointCloudLOD::ccPointCloudLOD
ccPointCloudLOD()
Default constructor.
Definition: ecvPointCloudLOD.cpp:381
count
int count
Definition: FileIOFactory.cpp:132
ccPointCloudLOD::Node::center
CCVector3f center
Definition: ecvPointCloudLOD.h:109
ccPointCloudLOD::flagVisibility
uint32_t flagVisibility(const Frustum &frustum, ccClipPlaneSet *clipPlanes=0)
Test all cells visibility with a given frustum.
Definition: ecvPointCloudLOD.cpp:646
ccPointCloudLOD::shrink_to_fit
void shrink_to_fit()
Shrinks the internal data to its minimum size.
Definition: ecvPointCloudLOD.cpp:489
LODLevelDesc::count
unsigned count
Index count for this level.
Definition: ecvPointCloudLOD.h:36
LODLevelDesc
Level descriptor.
Definition: ecvPointCloudLOD.h:27
ccPointCloudLOD::clear
void clear()
Clears the structure.
Definition: ecvPointCloudLOD.cpp:507
ccPointCloudLOD::Node::childIndexes
std::array< int32_t, 8 > childIndexes
Definition: ecvPointCloudLOD.h:110
ccPointCloudLOD::newCell
int32_t newCell(unsigned char level)
Reserves a new cell at a given level.
Definition: ecvPointCloudLOD.cpp:457
ccPointCloudLOD::Node::displayedPointCount
uint32_t displayedPointCount
Definition: ecvPointCloudLOD.h:112
PointCloudLODRenderer
Definition: ecvPointCloudLOD.h:272
ccPointCloudLOD::Node::intersection
uint8_t intersection
Definition: ecvPointCloudLOD.h:115
ccPointCloudLOD::Level
Definition: ecvPointCloudLOD.h:220
ccPointCloudLOD::Level::data
std::vector< Node > data
Definition: ecvPointCloudLOD.h:225
ccPointCloudLOD::node
Node & node(int32_t index, unsigned char level)
Definition: ecvPointCloudLOD.h:134
ccPointCloudLOD::m_currentState
RenderParams m_currentState
Current rendering state.
Definition: ecvPointCloudLOD.h:251
Frustum::INSIDE
@ INSIDE
Definition: ecvFrustum.h:99
ccPointCloudLOD::lock
void lock()
Locks the structure.
Definition: ecvPointCloudLOD.h:57
ccPointCloudLOD::NOT_INITIALIZED
@ NOT_INITIALIZED
Definition: ecvPointCloudLOD.h:46
ccPointCloudLOD::addNPointsToIndexMap
uint32_t addNPointsToIndexMap(Node &node, uint32_t count)
Definition: ecvPointCloudLOD.cpp:667
ecvOctree.h
ccPointCloudLOD::m_lastIndexMap
LODIndexSet m_lastIndexMap
Last index map (pointer on)
Definition: ecvPointCloudLOD.h:257
ccPointCloudLOD::RenderParams::RenderParams
RenderParams()
Definition: ecvPointCloudLOD.h:234
ccPointCloudLOD::isUnderConstruction
bool isUnderConstruction()
Returns whether the structure is initialized or not.
Definition: ecvPointCloudLOD.h:83