openvdb icon indicating copy to clipboard operation
openvdb copied to clipboard

[BUG] component-wise math ops break for non-POD types

Open hiokazaki opened this issue 1 year ago • 1 comments

Describe the bug

The functions cwiseAdd, cwiseLessThan and cwiseGreaterThan in openvdb/math/Math.h should only be enabled for POD types, but are not guarded, so they break when grids with non-POD types are used.

A simple fix is something like:

template<typename Type1, typename Type2,
  typename = std::enable_if_t<
    std::is_trivial<Type1>::value && std::is_trivial<Type2>::value
  >
>
inline bool cwiseGreaterThan(const Type1& a, const Type2& b)

This should result in no functional change for OpenVDB as shipped, but permit third parties to extend support for custom non-POD types.

hiokazaki avatar Nov 18 '24 00:11 hiokazaki

Hi @hiokazaki,

Thanks for this, however I believe that simply disabling these function from being instantiated won't help you as you'll still need to provide your own specialization to support your custom grid types?

So I believe the solution is for you to provide this specialization before you instantiate your custom types in your own code i.e:

namespace openvdb {
OPENVDB_USE_VERSION_NAMESPACE
namespace OPENVDB_VERSION_NAME {
namespace math {

// specialize vdb math grid ops for my types
template<>
inline auto cwiseAdd(const MyTypeA& v, const MyTypeB s)
{
    // impl what to do
}

}
}
}

Does that make sense?

Idclip avatar Sep 08 '25 10:09 Idclip