1

I'm working with Open3D library using the following function:

bool open3d::visualization::DrawGeometriesWithAnimationCallback 
(   
const std::vector< std::shared_ptr< const geometry::Geometry >> &   geometry_ptrs,
std::function< bool(Visualizer *)>  callback_func,
const std::string &     window_name = "Open3D",
int     width = 640,
int     height = 480,
int     left = 50,
int     top = 50 
)

I have managed to get it working, if I call this function from my main and having the function in the same main.cpp file. However, I would like to point to a class member function instead. This is what I got so far:

#include "WorkDispatcher.h"

int main(int argc, char* argv[]) 
{
    // setup of the needed classes I want to point to
    WorkDispatcher dispatcher;
    dispatcher.Initialize();
    dispatcher.m_MeshHandler.m_Mesh = open3d::geometry::TriangleMesh::CreateBox();
    dispatcher.Work();

    // here is the described issue
    std::function<bool(open3d::visualization::Visualizer*)> f = std::bind(&MeshHandler::UpdateMesh, dispatcher.m_MeshHandler);
    open3d::visualization::DrawGeometriesWithAnimationCallback({ dispatcher.m_MeshHandler.m_Mesh }, f, "Edit Mesh", 1600, 900);
    
    dispatcher.Stop();
    return 0;

}

This is derived from this post, but gives me the following error:

no suitable user-defined conversion from "std::_Binder<std::_Unforced, bool (MeshHandler::*)(open3d::visualization::Visualizer *vis), MeshHandler &>" to "std::function<bool (open3d::visualization::Visualizer *)>" exists

I'm not quite sure how to resolve this issue. Within the MeshHandler::UpdateMesh() function I would like to access other members of the class instance.

Roland Deschain
  • 2,211
  • 19
  • 50
  • note this is not a function pointer, it's a `std::function` – user253751 Jul 02 '21 at 11:09
  • @user253751 oh tbh, I thought those are basically the same? I'm still getting the hang of c++ stuff coming from c#... – Roland Deschain Jul 02 '21 at 11:11
  • Function pointers are something inherited from C, and they can point to functions. Not member functions, not binders, not anything else. std::function is a C++ class that tries to do the same thing but instead of only pointing to functions, it can point to anything that acts like a function. – user253751 Jul 02 '21 at 11:14
  • OK I see, thanks for the clarification! – Roland Deschain Jul 02 '21 at 11:15

1 Answers1

1

Turns out you need to use std::placeholders in your std::bind function. The following code works:

std::function<bool(open3d::visualization::Visualizer*)> f = std::bind(&MeshHandler::UpdateMesh, dispatcher.m_MeshHandler, std::placeholders::_1);
open3d::visualization::DrawGeometriesWithAnimationCallback({ dispatcher.m_MeshHandler.m_Mesh }, f, "Edit Mesh", 1600, 900);

See std::bind documentation for further information.

Roland Deschain
  • 2,211
  • 19
  • 50
  • 1
    It might be useful to know where the [documentation](https://en.cppreference.com/w/cpp/utility/functional/bind) for this is. The reason for needing the placeholders is that this isn't a simple partial application - as a benefit, you can pass placeholders to the bound function in a different order, or even multiple times. – Useless Jul 02 '21 at 11:50
  • 1
    Note that you could also just use a lambda instead. Some people flat-out prefer lambdas over `std::bind`. E.g., `auto f = [&m=dispatcher.m_MeshHandler] (open3d::visualization::Visualizer* v) { return m.UpdateMesh(v); };` – ypnos Jul 02 '21 at 11:51
  • @Useless thanks, added the link to the answer. :) – Roland Deschain Jul 02 '21 at 11:52