ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ReferenceCloud.cpp
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 #include "ReferenceCloud.h"
9 
10 // system
11 #include <algorithm>
12 
13 using namespace cloudViewer;
14 
16  : m_globalIterator(0), m_theAssociatedCloud(associatedCloud) {}
17 
19  : m_globalIterator(0),
20  m_theAssociatedCloud(refCloud.m_theAssociatedCloud),
21  m_theIndexes(refCloud.m_theIndexes) // we don't catch any exception so
22  // that the caller of the constructor
23  // can do it!
24 {}
25 
26 void ReferenceCloud::clear(bool releaseMemory /*=false*/) {
27  m_mutex.lock();
28  if (releaseMemory)
29  m_theIndexes.resize(0);
30  else
31  m_theIndexes.clear();
32 
34  m_mutex.unlock();
35 }
36 
38  m_mutex.lock();
39  if (!m_bbox.isValid()) {
40  m_bbox.clear();
41  for (unsigned index : m_theIndexes) {
43  }
44  }
45 
46  bbMin = m_bbox.minCorner();
47  bbMax = m_bbox.maxCorner();
48  m_mutex.unlock();
49 }
50 
51 bool ReferenceCloud::reserve(unsigned n) {
52  m_mutex.lock();
53  try {
54  m_theIndexes.reserve(n);
55  } catch (const std::bad_alloc&) {
56  m_mutex.unlock();
57  return false;
58  }
59 
60  m_mutex.unlock();
61  return true;
62 }
63 
64 bool ReferenceCloud::resize(unsigned n) {
65  m_mutex.lock();
66  try {
67  m_theIndexes.resize(n);
68  } catch (const std::bad_alloc&) {
69  m_mutex.unlock();
70  return false;
71  }
72 
73  m_mutex.unlock();
74  return true;
75 }
76 
82 }
83 
84 bool ReferenceCloud::addPointIndex(unsigned globalIndex) {
85  m_mutex.lock();
86  try {
87  m_theIndexes.push_back(globalIndex);
88  } catch (const std::bad_alloc&) {
89  m_mutex.unlock();
90  return false;
91  }
93 
94  m_mutex.unlock();
95  return true;
96 }
97 
98 bool ReferenceCloud::addPointIndex(unsigned firstIndex, unsigned lastIndex) {
99  if (firstIndex >= lastIndex) {
100  assert(false);
101  return false;
102  }
103 
104  unsigned range = lastIndex - firstIndex; // lastIndex is excluded
105 
106  m_mutex.lock();
107  unsigned pos = size();
108  if (size() < pos + range) {
109  try {
110  m_theIndexes.resize(pos + range);
111  } catch (const std::bad_alloc&) {
112  m_mutex.unlock();
113  return false;
114  }
115  }
116 
117  for (unsigned i = 0; i < range; ++i, ++firstIndex) {
118  m_theIndexes[pos++] = firstIndex;
119  }
120 
122  m_mutex.unlock();
123 
124  return true;
125 }
126 
127 void ReferenceCloud::setPointIndex(unsigned localIndex, unsigned globalIndex) {
128  assert(localIndex < size());
129  m_theIndexes[localIndex] = globalIndex;
131 }
132 
134  assert(m_theAssociatedCloud);
135 
136  unsigned count = size();
137  for (unsigned i = 0; i < count; ++i) {
138  const unsigned& index = m_theIndexes[i];
139  ScalarType d = m_theAssociatedCloud->getPointScalarValue(index);
140  ScalarType d2 = d;
141  action(*m_theAssociatedCloud->getPointPersistentPtr(index), d2);
142  if (d != d2) m_theAssociatedCloud->setPointScalarValue(index, d2);
143  }
144 }
145 
146 void ReferenceCloud::removePointGlobalIndex(unsigned localIndex) {
147  m_mutex.lock();
148 
149  if (localIndex < size()) {
150  unsigned lastIndex = size() - 1;
151  // swap the value to be removed with the last one
152  m_theIndexes[localIndex] = m_theIndexes[lastIndex];
153  m_theIndexes.resize(lastIndex);
154  } else {
155  assert(false);
156  }
157 
158  m_mutex.unlock();
159 }
160 
162  m_theAssociatedCloud = cloud;
164 }
165 
167  if (!cloud.m_theAssociatedCloud ||
169  return false;
170  }
171 
172  std::size_t newCount = cloud.m_theIndexes.size();
173  if (newCount == 0) return true;
174 
175  m_mutex.lock();
176 
177  // reserve memory
178  std::size_t currentSize = size();
179  try {
180  m_theIndexes.resize(currentSize + newCount);
181  } catch (const std::bad_alloc&) {
182  m_mutex.unlock();
183  return false;
184  }
185 
186  // copy new indexes (warning: no duplicate check!)
187  for (unsigned i = 0; i < newCount; ++i) {
188  m_theIndexes[currentSize + i] = cloud.m_theIndexes[i];
189  }
190 
192 
193  m_mutex.unlock();
194  return true;
195 }
int count
const Vector3Tpl< T > & maxCorner() const
Returns max corner (const)
Definition: BoundingBox.h:156
void clear()
Resets the bounding box.
Definition: BoundingBox.h:125
const Vector3Tpl< T > & minCorner() const
Returns min corner (const)
Definition: BoundingBox.h:154
bool isValid() const
Returns whether bounding box is valid or not.
Definition: BoundingBox.h:203
void add(const Vector3Tpl< T > &P)
'Enlarges' the bounding box with a point
Definition: BoundingBox.h:131
std::function< void(const CCVector3 &, ScalarType &)> genericPointAction
Generic function applied to a point (used by foreach)
Definition: GenericCloud.h:30
virtual unsigned size() const =0
Returns the number of points.
virtual ScalarType getPointScalarValue(unsigned pointIndex) const =0
Returns the ith point associated scalar value.
virtual void setPointScalarValue(unsigned pointIndex, ScalarType value)=0
Sets the ith point associated scalar value.
A generic 3D point cloud with index-based and presistent access to points.
virtual const CCVector3 * getPointPersistentPtr(unsigned index)=0
Returns the ith point as a persistent pointer.
virtual const CCVector3 * getPoint(unsigned index) const =0
Returns the ith point.
A very simple point cloud (no point duplication)
BoundingBox m_bbox
Bounding-box.
virtual bool addPointIndex(unsigned globalIndex)
Point global index insertion mechanism.
std::atomic< unsigned > m_globalIterator
Iterator on the point references container.
void getBoundingBox(CCVector3 &bbMin, CCVector3 &bbMax) override
Returns the cloud bounding box.
std::mutex m_mutex
For concurrent access.
unsigned size() const override
Returns the number of points.
virtual void setPointIndex(unsigned localIndex, unsigned globalIndex)
Sets global index for a given element.
void invalidateBoundingBox()
Invalidates the bounding-box.
ReferencesContainer m_theIndexes
Indexes of (some of) the associated cloud points.
GenericIndexedCloudPersist * m_theAssociatedCloud
Associated cloud.
virtual bool resize(unsigned n)
Presets the size of the vector used to store point references.
ReferenceCloud(GenericIndexedCloudPersist *associatedCloud)
Default constructor.
virtual void clear(bool releaseMemory=false)
Clears the cloud.
void forEach(genericPointAction action) override
Fast iteration mechanism.
bool add(const ReferenceCloud &cloud)
Add another reference cloud.
virtual const CCVector3 * getCurrentPointCoordinates() const
Returns the coordinates of the point pointed by the current element.
virtual void setAssociatedCloud(GenericIndexedCloudPersist *cloud)
Sets the associated (source) cloud.
virtual bool reserve(unsigned n)
Reserves some memory for hosting the point references.
virtual void removePointGlobalIndex(unsigned localIndex)
Removes a given element.
Generic file read and write utility for python interface.