ofBaseApp member function as callback for PCL lib, mutex question?

Hi,

I want to use PCL as my interface for the Kinect as it offers frame syncing. I am passing a member function of my OF baseApp to the grabber as a callback using boost, which gets called when a new frame is available. I want to copy this frame into a member var of my baseApp and use it in draw() to render it to the screen. As the grabber that executes the callback is running as a thread i wanted to know if I have to lock the variable in the baseApp, when the callback copies the new image into it? Is the baseApp its own thread or is there no chance that draw() is called while the callback writes to the variabel?

thanks

To answer my question:
Yes one needs a mutex. The app does not crash but there is visible tearing in the video streams as the callback function is now part of the PCL thread and writes into the pixel data arrays while he OF thread reads them.
i encapsulated OF in its own class to beable to use the boost mutex functions:

  
  
threadMainOF::threadMainOF(void)  
{  
	 m_stoprequested = false;  
}  
  
  
threadMainOF::~threadMainOF(void)  
{  
	threadMainOF::stop();  
}  
  
void threadMainOF::go()  
{  
	assert(!m_thread);  
	m_thread = boost::shared_ptr<boost::thread>(  
		new boost::thread(boost::bind(&threadMainOF::do_work, this)));  
}  
  
void threadMainOF::stop()  
{  
	assert(m_thread);  
	m_stoprequested = true;  
	m_thread->join();  
}  
  
void threadMainOF::do_work()  
{  
	ofAppGlutWindow window;  
	ofSetupOpenGL(&window, 1280,720, OF_WINDOW);			// <-------- setup the GL context  
  
	// this kicks off the running of my app  
	// can be OF_WINDOW or OF_FULLSCREEN  
	// pass in width and height too:  
	testApp* app = new testApp();  
	app->thisThread = this;  
	ofRunApp(app);  
}  
  

i am now using triple buffering to reduce the durations the variables are locked from being accessed from another thread. i copy the new kinect data in the PCL callback into a temp buffer and then copy it from there into my work buffer in the OF update() call. anyone wanting to load the pixels into an OF texture in the callback like me beware: its an OpenGL call, that only works in the OpenGL conext in other words in OF update(); and results in a garbage white image with some random colored pixels.

If anyone has some comments about threading ofBaseApp or ideas for improvement please let me hear them.

thanks

Hi Christoph.

Gruss nach Wien :slight_smile:

I am trying to use PCL or ofxPCL but had no luck so far.
Would you be able to give me a quick how to and maybe a sample code?

thanks,
stephan.

Hi Gruss nach Montreal!

This depends on where youre stuck? I did not use ofxPCL and until now only tried to use PCL to capture and record kinect streams while rendering in OF. I switched back to ofxOpenNI because its bugs were fixed and PCL turned out to be too slow for recording, at least for me under Windows. I plan on using PCL for segmentation and depending on how things turn out on feature extraction and tracking. But thats in the future so no experience there.

best
Chris

i was hoping to do some point cloud matching / model based tracking.

similar to this but with a simple rigid model.
http://www.dailymotion.com/video/xhvlyk-3d-human-motion-tracking-using-kinect-tech
http://www.dailymotion.com/video/xdj60z-augmented-reality-for-video-games-p-tech

this uses the ICP Algorithm which PCL also uses.

tschüß,
stephan.

and where is it you are stuck?

if you look into the svn trunk there is a folder called tracking with some beta code doing rigid model tracking. there is also a blog entry on the PCL website about this code with youtube videos showing results. it seems to run in realtime when the cloud is downsampled

4 years later and i got it to work.