RenderKit / ospray

An Open, Scalable, Portable, Ray Tracing Based Rendering Engine for High-Fidelity Visualization
http://ospray.org
Apache License 2.0
982 stars 178 forks source link

C++ getBounds() template error #497

Closed paulmelis closed 2 years ago

paulmelis commented 2 years ago

What's the correct way of using a custom box3f type together with getBounds()? As far as I understand it defining your own type is the only option with the C++ wrappers if you don't want to use rkcommon (which only has source releases, hence is less attractive). I've tried this:

#include <ospray/ospray_cpp.h>

struct vec3f { 
    float x, y, z; 

    vec3f(): x(0), y(0), z(0) {}
    vec3f(float xx, float yy, float zz): x(xx), y(yy), z(zz) {}
};

struct box3f {
    vec3f   upper, lower;
};

namespace ospray {
OSPTYPEFOR_SPECIALIZATION(vec3f, OSP_VEC3F);
OSPTYPEFOR_SPECIALIZATION(box3f, OSP_BOX3F);
}

int main()
{
    ospray::cpp::Camera c;

    const box3f bounds = c.getBounds();
}

Compiling this snippet results in an error, saying that the return type template parameter of getBounds() can't be determined:

melis@juggle 18:58:~/concepts/ospray-python$ g++ -I ~/software/ospray-2.7.0.x86_64.linux/include/ t_bounds2.cpp 
t_bounds2.cpp: In function ‘int main()’:
t_bounds2.cpp:23:37: error: no matching function for call to ‘ospray::cpp::Camera::getBounds()’
   23 |     const box3f bounds = c.getBounds();
      |                          ~~~~~~~~~~~^~
In file included from /home/melis/software/ospray-2.7.0.x86_64.linux/include/ospray/ospray_cpp/Camera.h:6,
                 from /home/melis/software/ospray-2.7.0.x86_64.linux/include/ospray/ospray_cpp.h:8,
                 from t_bounds2.cpp:1:
/home/melis/software/ospray-2.7.0.x86_64.linux/include/ospray/ospray_cpp/ManagedObject.h:44:11: note: candidate: ‘template<class BOX3F_T> BOX3F_T ospray::cpp::ManagedObject<HANDLE_T, TYPE>::getBounds() const [with BOX3F_T = BOX3F_T; HANDLE_T = osp::Camera*; OSPDataType TYPE = OSP_CAMERA]’
   44 |   BOX3F_T getBounds() const;
      |           ^~~~~~~~~
/home/melis/software/ospray-2.7.0.x86_64.linux/include/ospray/ospray_cpp/ManagedObject.h:44:11: note:   template argument deduction/substitution failed:
t_bounds2.cpp:23:37: note:   couldn’t deduce template parameter ‘BOX3F_T’
   23 |     const box3f bounds = c.getBounds();
      |                          ~~~~~~~~~~~^~

My C++ template-fu isn't strong enough to figure out how to fix this...

paulmelis commented 2 years ago

Hmmm, looking at https://github.com/ospray/ospray/blob/a783bf987cff6a2d70a2dfce6732d7f2572703bd/ospray/common/Instance.cpp#L37-L40 it seems that what I'm trying isn't too unreasonable

Strike that, as that's internal code, not something that uses the C++ wrappers

paulmelis commented 2 years ago

Ah, looking at apps/ospTestSuite/test_geometry.cpp I think I got it:

const box3f bounds = c.getBounds<box3f>();
johguenther commented 2 years ago

Yes, that should do the trick (using OSPTYPEFOR_SPECIALIZATION for own types, as done for example in ospray_cpp/ext/glm.h and then forcing the template specialization by explicitly passing the own type as template argument) .