AcademySoftwareFoundation / openvdb

OpenVDB - Sparse volume data structure and tools
http://www.openvdb.org/
Apache License 2.0
2.71k stars 660 forks source link

BaseStencil::max() compiler error #1610

Closed smauch closed 1 year ago

smauch commented 1 year ago

Environment

Operating System: (Ubuntu 20.04) Version / Commit SHA: (OpenVDB 10.0.1) Other: (GCC 9.4.0)

Describe the bug

The BaseStencil::max() function in Stencils.h causes a compiler error when used.

/usr/local/include/openvdb/math/Stencils.h:155:41: error: conversion from ‘__normal_iterator<const float*,[...]>’ to non-scalar type ‘__normal_iterator<float*,[...]>’ requested
  155 |         IterType iter = std::max_element(mValues.begin(), mValues.end());

To Reproduce

#include <iostream>
#include <openvdb/math/Stencils.h>
#include <openvdb/openvdb.h>

int main()
{
  openvdb::FloatGrid::Ptr grid = openvdb::FloatGrid::create();
  openvdb::math::BoxStencil<openvdb::FloatGrid> stencil(*grid);
  stencil.moveTo(openvdb::Coord(0, 0, 0));
  float max = stencil.max();
  return 0;
}

Reason

The reason for this issue is that, within the std::max_element() function call, mValues.begin() returns a const_iterator, as the member variable mValues is considered const due to BaseStencil::max() being a const member function.

Solution

The typedef of IterType should be typename BufferType::const_iterator instead of typename BufferType::iterator