Skip to content

Commit

Permalink
A scalar field can be filled with only NaN/-inf/+inf values
Browse files Browse the repository at this point in the history
  • Loading branch information
dgirardeau committed Nov 21, 2024
1 parent f33abba commit 06936c8
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 11 deletions.
61 changes: 55 additions & 6 deletions include/ScalarField.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,19 @@ namespace CCCoreLib
**/
inline void setOffset(double offset)
{
assert(std::isfinite(offset));

m_offsetHasBeenSet = true;
m_offset = offset;
}

//! Resets the offset
inline void resetOffset()
{
m_offsetHasBeenSet = false;
m_offset = 0.0;
}

//! Clears the scalar field
inline void clear()
{
Expand Down Expand Up @@ -105,13 +114,38 @@ namespace CCCoreLib
inline void fill(ScalarType fillValue = 0)
{
float fillValueF = 0.0f;
if (m_offsetHasBeenSet)
if (std::isfinite(fillValue))
{
fillValueF = static_cast<float>(fillValue - m_offset);
if (fillValue == 0)
{
// special case: filling with zeros
// (it doesn't really give an idea of what the optimal offset is)
resetOffset();

//fillValueF = 0.0f; // already set
}
else
{
if (m_offsetHasBeenSet)
{
fillValueF = static_cast<float>(fillValue - m_offset);
}
else
{
// if the offset has not been set yet, we use the first finite value by default
setOffset(fillValue);

//fillValueF = 0.0f; // already set
}
}
}
else
{
setOffset(fillValue);
// special case: filling with NaN or +/-inf values
// (it doesn't really give an idea of what the optimal offset is)
resetOffset();

fillValueF = static_cast<float>(fillValue); // NaN/-inf/+inf should be maintained
}

if (empty())
Expand Down Expand Up @@ -140,27 +174,42 @@ namespace CCCoreLib
{
if (m_offsetHasBeenSet)
{
// use the already set offset
(*this)[index] = static_cast<float>(value - m_offset);
}
else
else if (std::isfinite(value))
{
// if the offset has not been set yet, we use the
// first finite value as offset by default
setOffset(value);
(*this)[index] = 0.0f;
}
else
{
// we can't set an offset
(*this)[index] = static_cast<float>(value); // NaN/-inf/+inf should be maintained
}
}

inline void addElement(ScalarType value)
{
if (m_offsetHasBeenSet)
{
// use the already set offset
push_back(static_cast<float>(value - m_offset));
}
else
else if (std::isfinite(value))
{
// if the offset has not been set yet, we use the first value by default
// if the offset has not been set yet, we use the
// first finite value as offset by default
setOffset(value);
push_back(0.0f);
}
else
{
// we can't set an offset
push_back(static_cast<float>(value)); // NaN/-inf/+inf should be maintained
}
}

inline unsigned currentSize() const { return static_cast<unsigned>(size()); }
Expand Down
28 changes: 23 additions & 5 deletions src/ScalarField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,35 @@ bool ScalarField::resizeSafe(std::size_t count, bool initNewElements/*=false*/,
{
try
{
if (initNewElements)
if (initNewElements && count > size())
{
float fillValueF = 0.0f;
if (m_offsetHasBeenSet)
if (std::isfinite(valueForNewElements))
{
fillValueF = static_cast<float>(valueForNewElements - m_offset);
if (m_offsetHasBeenSet)
{
// use the already set offset
fillValueF = static_cast<float>(valueForNewElements - m_offset);
}
else // if the offset has not been set yet...
{
if (valueForNewElements == 0)
{
// special case: filling with zeros
// (it doesn't really give an idea of what the optimal offset is)
m_offset = 0.0;
}
else
{
// we use the first finite value as offset by default
setOffset(valueForNewElements);
}
}
}
else
{
// if the offset has not been set yet, we use the first value by default
setOffset(valueForNewElements);
// special case: filling with NaN or +/-inf values
fillValueF = static_cast<float>(valueForNewElements); // NaN/-inf/+inf should be maintained
}

resize(count, fillValueF);
Expand Down

0 comments on commit 06936c8

Please sign in to comment.