Skip to content

Commit

Permalink
Scalar fields now rely on an internal offset to properly manage doubl…
Browse files Browse the repository at this point in the history
…e values (as long as the min and max values are not too far from each other!)
  • Loading branch information
dgirardeau committed Nov 10, 2024
1 parent 411d11b commit aaf1fda
Show file tree
Hide file tree
Showing 13 changed files with 366 additions and 152 deletions.
16 changes: 0 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ option( CCCORELIB_SHARED
"Compile CCCoreLib as a shared library"
ON
)
option( CCCORELIB_SCALAR_DOUBLE
"Define ScalarType as double (instead of float)"
OFF
)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)

Expand Down Expand Up @@ -119,18 +115,6 @@ target_compile_definitions( CCCoreLib
"$<$<CONFIG:DEBUG>:CC_DEBUG>"
)

if ( CCCORELIB_SCALAR_DOUBLE )
target_compile_definitions( CCCoreLib
PUBLIC
CC_CORE_LIB_USES_DOUBLE
)
else()
target_compile_definitions( CCCoreLib
PUBLIC
CC_CORE_LIB_USES_FLOAT
)
endif()

# Nanoflann
option(NANOFLANN_BUILD_BENCHMARKS "" OFF)
option(NANOFLANN_BUILD_EXAMPLES "" OFF)
Expand Down
2 changes: 1 addition & 1 deletion include/CCConst.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ namespace CCCoreLib
};

//! Min number of points to compute local models (see CC_LOCAL_MODEL_TYPES)
constexpr unsigned CC_LOCAL_MODEL_MIN_SIZE[] = {
constexpr unsigned CC_LOCAL_MODEL_MIN_SIZE[] {
1, //!< for single point model (i.e. no model ;)
3, //!< for least Square best fitting plane
3, //!< for Delaunay triangulation (2.5D)
Expand Down
6 changes: 0 additions & 6 deletions include/CCTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,4 @@
using PointCoordinateType = float;

//! Type of a single scalar field value
#if defined CC_CORE_LIB_USES_DOUBLE
using ScalarType = double;
#elif defined CC_CORE_LIB_USES_FLOAT
using ScalarType = float;
#else
static_assert(false, "type for ScalarType has not been declared");
#endif
42 changes: 35 additions & 7 deletions include/GenericDistribution.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,7 @@
#pragma once

//Local
#include "CCCoreLib.h"
#include "CCTypes.h"

//system
#include <vector>
#include "ScalarField.h"

namespace CCCoreLib
{
Expand Down Expand Up @@ -39,8 +35,40 @@ namespace CCCoreLib
**/
virtual bool isValid() const { return m_isValid; }

//! Scalar values container
using ScalarContainer = std::vector<ScalarType>;
//! Scalar values container interface
struct ScalarContainer
{
virtual size_t size() const = 0;
virtual ScalarType getValue(size_t index) const = 0;
};

//! Wrapper of a CCCoreLib's scalar field as a Scalar values container
struct SFAsScalarContainer : ScalarContainer
{
SFAsScalarContainer(const ScalarField& sf)
: ScalarContainer()
, m_sf(sf)
{}

inline size_t size() const override { return m_sf.size(); }
inline ScalarType getValue(size_t index) const override { return m_sf.getValue(index); }

const CCCoreLib::ScalarField& m_sf;
};

//! Wrapper of a std::vector as a Scalar values container
struct VectorAsScalarContainer : ScalarContainer
{
VectorAsScalarContainer(const std::vector<ScalarType>& vector)
: ScalarContainer()
, m_vector(vector)
{}

inline size_t size() const override { return m_vector.size(); }
inline ScalarType getValue(size_t index) const override { return m_vector[index]; }

const std::vector<ScalarType>& m_vector;
};

//! Computes the distribution parameters from a set of values
/** \param values a set of scalar values
Expand Down
38 changes: 29 additions & 9 deletions include/PointCloudTpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,27 @@ namespace CCCoreLib
}

unsigned n = size();
for (unsigned i = 0; i < n; ++i)

if (0 != n)
{
action(m_points[i], (*currentOutScalarFieldArray)[i]);
double previousOffset = currentOutScalarFieldArray->getOffset();
if (n == currentOutScalarFieldArray->size())
{
// if we are going to change ALL the values, we can also apply the functor on the offset
double firstValue = currentOutScalarFieldArray->getValue(0);
action(m_points.front(), firstValue);
if (ScalarField::ValidValue(firstValue))
{
currentOutScalarFieldArray->setOffset(firstValue);
}
}

for (unsigned i = 0; i < n; ++i)
{
ScalarType value = previousOffset + currentOutScalarFieldArray->getLocalValue(i); // warning, the offset has been changed, we can't use getValue anymore
action(m_points[i], value);
currentOutScalarFieldArray->setValue(i, value);
}
}
}

Expand Down Expand Up @@ -244,7 +262,7 @@ namespace CCCoreLib
//if something fails, we restore the previous size for already processed SFs!
for (std::size_t j = 0; j < i; ++j)
{
m_scalarFields[j]->resize(oldCount);
m_scalarFields[j]->resizeSafe(oldCount);
m_scalarFields[j]->computeMinAndMax();
}
//we can assume that newNumberOfPoints > oldCount, so it should always be ok
Expand Down Expand Up @@ -348,23 +366,25 @@ namespace CCCoreLib
/** \param index a scalar field index
\return a pointer to a string structure (null-terminated array of characters), or 0 if the index is invalid.
**/
const char* getScalarFieldName(int index) const
const std::string getScalarFieldName(int index) const
{
return (index >= 0 && index < static_cast<int>(m_scalarFields.size()) ? m_scalarFields[index]->getName() : 0);
return (index >= 0 && index < static_cast<int>(m_scalarFields.size()) ? m_scalarFields[index]->getName() : std::string{});
}

//! Returns the index of a scalar field represented by its name
/** \param name a scalar field name
\return an index (-1 if the scalar field couldn't be found)
**/
int getScalarFieldIndexByName(const char* name) const
int getScalarFieldIndexByName(const std::string& name) const
{
std::size_t sfCount = m_scalarFields.size();
for (std::size_t i = 0; i < sfCount; ++i)
{
//we don't accept two SF with the same name!
if (strcmp(m_scalarFields[i]->getName(), name) == 0)
if (0 == m_scalarFields[i]->getName().compare(name))
{
return static_cast<int>(i);
}
}

return -1;
Expand Down Expand Up @@ -414,7 +434,7 @@ namespace CCCoreLib
\param uniqueName scalar field name (must be unique)
\return index of this new scalar field (or -1 if an error occurred)
**/
virtual int addScalarField(const char* uniqueName)
virtual int addScalarField(const std::string& uniqueName)
{
//we don't accept two SF with the same name!
if (getScalarFieldIndexByName(uniqueName) >= 0)
Expand Down Expand Up @@ -455,7 +475,7 @@ namespace CCCoreLib
\param newName new name
\return success
**/
bool renameScalarField(int index, const char* newName)
bool renameScalarField(int index, const std::string& newName)
{
if (getScalarFieldIndexByName(newName) < 0)
{
Expand Down
Loading

0 comments on commit aaf1fda

Please sign in to comment.