ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
copy.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 <Utils/copy.h>
9 
10 // CV_DB_LIB
11 #include <ecvPointCloud.h>
12 #include <ecvScalarField.h>
13 
14 void copyScalarFields(const ccPointCloud *inCloud,
15  ccPointCloud *outCloud,
16  pcl::PointIndicesPtr &in2outMapping,
17  bool overwrite) {
18  if (in2outMapping->indices.empty()) return;
19  assert(in2outMapping->indices.size() == outCloud->size());
20 
21  unsigned n_out = outCloud->size();
22 
23  unsigned sfCount = inCloud->getNumberOfScalarFields();
24  for (unsigned i = 0; i < sfCount; ++i) {
25  const cloudViewer::ScalarField *field =
26  inCloud->getScalarField(static_cast<int>(i));
27  const char *name = field->getName();
28 
29  ccScalarField *new_field = nullptr;
30 
31  // we need to verify no scalar field with the same name exists in the
32  // output cloud
33  int id = outCloud->getScalarFieldIndexByName(name);
34  if (id >= 0) // a scalar field with the same name exists
35  {
36  if (overwrite) {
37  new_field = static_cast<ccScalarField *>(
38  outCloud->getScalarField(id));
39  } else {
40  continue;
41  }
42  } else {
43  new_field = new ccScalarField(name);
44 
45  // resize the scalar field to the outcloud size
46  if (!new_field->resizeSafe(n_out)) {
47  // not enough memory!
48  new_field->release();
49  new_field = nullptr;
50  continue;
51  }
52  }
53 
54  // now perform point to point copy
55  for (std::size_t j = 0; j < n_out; ++j) {
56  new_field->setValue(j,
57  field->getValue(in2outMapping->indices.at(j)));
58  }
59 
60  // recompute stats
61  new_field->computeMinAndMax();
62 
63  // now put back the scalar field to the outCloud
64  if (id < 0) {
65  outCloud->addScalarField(new_field);
66  }
67  }
68 
69  outCloud->showSF(outCloud->sfShown() || inCloud->sfShown());
70 }
71 
72 void copyRGBColors(const ccPointCloud *inCloud,
73  ccPointCloud *outCloud,
74  pcl::PointIndicesPtr &in2outMapping,
75  bool overwrite) {
76  // if inCloud has no color there is nothing to do
77  if (!inCloud->hasColors()) return;
78 
79  if (in2outMapping->indices.empty()) return;
80  assert(in2outMapping->indices.size() == outCloud->size());
81 
82  if (outCloud->hasColors() && !overwrite) return;
83 
84  if (outCloud->reserveTheRGBTable()) {
85  // now perform point to point copy
86  unsigned n_out = outCloud->size();
87  for (unsigned j = 0; j < n_out; ++j) {
88  outCloud->addRGBColor(
89  inCloud->getPointColor(in2outMapping->indices.at(j)));
90  }
91  }
92 
93  outCloud->showColors(outCloud->colorsShown() || inCloud->colorsShown());
94 }
95 
96 // #endif // LP_PCL_PATCH_ENABLED
std::string name
virtual void release()
Decrease counter and deletes object when 0.
Definition: CVShareable.cpp:35
virtual bool colorsShown() const
Returns whether colors are shown or not.
virtual bool sfShown() const
Returns whether active scalar field is visible.
virtual void showColors(bool state)
Sets colors visibility.
virtual void showSF(bool state)
Sets active scalarfield visibility.
A 3D cloud and its associated features (color, normals, scalar fields, etc.)
int addScalarField(const char *uniqueName) override
Creates a new scalar field and registers it.
bool reserveTheRGBTable()
Reserves memory to store the RGB colors.
bool hasColors() const override
Returns whether colors are enabled or not.
void addRGBColor(const ecvColor::Rgb &C)
Pushes an RGB color on stack.
const ecvColor::Rgb & getPointColor(unsigned pointIndex) const override
Returns color corresponding to a given point.
A scalar field associated to display-related parameters.
void computeMinAndMax() override
Determines the min and max values.
int getScalarFieldIndexByName(const char *name) const
Returns the index of a scalar field represented by its name.
ScalarField * getScalarField(int index) const
Returns a pointer to a specific scalar field.
unsigned getNumberOfScalarFields() const
Returns the number of associated (and active) scalar fields.
unsigned size() const override
Definition: PointCloudTpl.h:38
A simple scalar field (to be associated to a point cloud)
Definition: ScalarField.h:25
ScalarType & getValue(std::size_t index)
Definition: ScalarField.h:92
void setValue(std::size_t index, ScalarType value)
Definition: ScalarField.h:96
const char * getName() const
Returns scalar field name.
Definition: ScalarField.h:43
bool resizeSafe(std::size_t count, bool initNewElements=false, ScalarType valueForNewElements=0)
Resizes memory (no exception thrown)
Definition: ScalarField.cpp:81
void copyRGBColors(const ccPointCloud *inCloud, ccPointCloud *outCloud, pcl::PointIndicesPtr &in2outMapping, bool overwrite)
Makes a copy of RGB colors from one cloud to another.
Definition: copy.cpp:72
void copyScalarFields(const ccPointCloud *inCloud, ccPointCloud *outCloud, pcl::PointIndicesPtr &in2outMapping, bool overwrite)
Makes a copy of all scalar fields from one cloud to another.
Definition: copy.cpp:14