ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
ScalarField.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 <ScalarField.h>
9 
10 // System
11 #include <cassert>
12 #include <cstring>
13 
14 using namespace cloudViewer;
15 
16 ScalarField::ScalarField(const char* name /*=0*/) { setName(name); }
17 
18 ScalarField::ScalarField(const ScalarField& sf) : std::vector<ScalarType>(sf) {
19  setName(sf.m_name);
20 }
21 
22 void ScalarField::setName(const char* name) {
23  if (name)
24  strncpy(m_name, name, 255);
25  else
26  strcpy(m_name, "Undefined");
27 }
28 
29 std::size_t ScalarField::countValidValues() const {
30  std::size_t count = 0;
31 
32  for (std::size_t i = 0; i < size(); ++i) {
33  const ScalarType& val = at(i);
34  if (ValidValue(val)) {
35  ++count;
36  }
37  }
38 
39  return count;
40 }
42  ScalarType* variance) const {
43  double _mean = 0.0, _std2 = 0.0;
44  std::size_t count = 0;
45 
46  for (std::size_t i = 0; i < size(); ++i) {
47  const ScalarType& val = at(i);
48  if (ValidValue(val)) {
49  _mean += val;
50  _std2 += static_cast<double>(val) * val;
51  ++count;
52  }
53  }
54 
55  if (count) {
56  _mean /= count;
57  mean = static_cast<ScalarType>(_mean);
58 
59  if (variance) {
60  _std2 = std::abs(_std2 / count - _mean * _mean);
61  *variance = static_cast<ScalarType>(_std2);
62  }
63  } else {
64  mean = 0;
65  if (variance) {
66  *variance = 0;
67  }
68  }
69 }
70 
71 bool ScalarField::reserveSafe(std::size_t count) {
72  try {
73  reserve(count);
74  } catch (const std::bad_alloc&) {
75  // not enough memory
76  return false;
77  }
78  return true;
79 }
80 
81 bool ScalarField::resizeSafe(std::size_t count,
82  bool initNewElements /*=false*/,
83  ScalarType valueForNewElements /*=0*/) {
84  try {
85  if (initNewElements)
86  resize(count, valueForNewElements);
87  else
88  resize(count);
89  } catch (const std::bad_alloc&) {
90  // not enough memory
91  return false;
92  }
93  return true;
94 }
int size
std::string name
int count
A simple scalar field (to be associated to a point cloud)
Definition: ScalarField.h:25
std::size_t countValidValues() const
Returns the number of valid values in this scalar field.
Definition: ScalarField.cpp:29
void computeMeanAndVariance(ScalarType &mean, ScalarType *variance=nullptr) const
Definition: ScalarField.cpp:41
char m_name[256]
Scalar field name.
Definition: ScalarField.h:115
bool reserveSafe(std::size_t count)
Reserves memory (no exception thrown)
Definition: ScalarField.cpp:71
ScalarField(const char *name=nullptr)
Default constructor.
Definition: ScalarField.cpp:16
void setName(const char *name)
Sets scalar field name.
Definition: ScalarField.cpp:22
bool resizeSafe(std::size_t count, bool initNewElements=false, ScalarType valueForNewElements=0)
Resizes memory (no exception thrown)
Definition: ScalarField.cpp:81
static bool ValidValue(ScalarType value)
Returns whether a scalar value is valid or not.
Definition: ScalarField.h:61
__host__ __device__ int2 abs(int2 v)
Definition: cutil_math.h:1267
Generic file read and write utility for python interface.
Rgb at(size_t color_id)
Definition: Eigen.h:85