<commit_before>/*
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2011, Willow Garage, Inc.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of Willow Garage, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *	
 * Author: <NAME> (<EMAIL>)
 *         <NAME> (<EMAIL>)
 *         <NAME> (<EMAIL>)
 *         <NAME> (<EMAIL>)
 */

#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/common/time.h> //fps calculations
#include <pcl/io/openni_grabber.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/io/openni_camera/openni_driver.h>
#include <pcl/console/parse.h>
#include <pcl/visualization/mouse_event.h>
#include <vector>
#include <string>

using namespace std;

#define SHOW_FPS 1
#if SHOW_FPS
#define FPS_CALC(_WHAT_) \
do \
{ \
    static unsigned count = 0;\
    static double last = pcl::getTime ();\
    double now = pcl::getTime (); \
    ++count; \
    if (now - last >= 1.0) \
    { \
      std::cout << "Average framerate("<< _WHAT_ << "): " << double(count)/double(now - last) << " Hz" <<  std::endl; \
      count = 0; \
      last = now; \
    } \
}while(false)
#else
#define FPS_CALC(_WHAT_) \
do \
{ \
}while(false)
#endif

void keyCallback (const pcl::visualization::KeyboardEvent& event, void* cookie)
{
      string* message = (string*)cookie;
    cout << (*message) << " :: ";
    if (event.getKeyCode())
      cout << "the key \'" << event.getKeyCode() << "\' (" << (int)event.getKeyCode() << ") was";
    else
      cout << "the special key \'" << event.getKeySym() << "\' was";
    if (event.keyDown())
      cout << " pressed" << endl;
    else
      cout << " released" << endl;
}

template <typename PointType>
class SimpleOpenNIViewer
{
public:
  typedef pcl::PointCloud<PointType> Cloud;
  typedef typename Cloud::ConstPtr CloudConstPtr;

  SimpleOpenNIViewer(pcl::OpenNIGrabber& grabber)
    : viewer("PCL OpenNI Viewer")
    , grabber_(grabber)
  {
    
  }

  /**
   * @brief Callback method for the grabber interface
   * @param cloud The new point cloud from Grabber
   */
  void
  cloud_cb_ (const CloudConstPtr& cloud)
  {
    //FPS_CALC ("callback");
    boost::mutex::scoped_lock lock (mtx_);
    cloud_ = cloud;
  }

  void keyboard_callback (const pcl::visualization::KeyboardEvent& event, void* cookie)
  {
    string* message = (string*)cookie;
    cout << (*message) << " :: ";
    if (event.getKeyCode())
      cout << "the key \'" << event.getKeyCode() << "\' (" << (int)event.getKeyCode() << ") was";
    else
      cout << "the special key \'" << event.getKeySym() << "\' was";
    if (event.keyDown())
      cout << " pressed" << endl;
    else
      cout << " released" << endl;
  }
  
  void mouse_callback (const pcl::visualization::MouseEvent& mouse_event, void* cookie)
  {
    string* message = (string*) cookie;
    if (mouse_event.getType() == pcl::visualization::MouseEvent::MouseButtonPress && mouse_event.getButton() == pcl::visualization::MouseEvent::LeftButton)
    {
      cout << (*message) << " :: " << mouse_event.getX () << " , " << mouse_event.getY () << endl;
    }
  }
  /**
   * @brief swaps the pointer to the point cloud with Null pointer and returns the cloud pointer
   * @return boost shared pointer to point cloud
   */
  CloudConstPtr
  getLatestCloud ()
  {
    //lock while we swap our cloud and reset it.
    boost::mutex::scoped_lock lock(mtx_);
    CloudConstPtr temp_cloud;
    temp_cloud.swap (cloud_); //here we set cloud_ to null, so that
    //it is safe to set it again from our
    //callback
    return (temp_cloud);
  }

  /**
   * @brief starts the main loop
   */
  void
  run()
  {
    //pcl::Grabber* interface = new pcl::OpenNIGrabber(device_id_, pcl::OpenNIGrabber::OpenNI_QQVGA_30Hz, pcl::OpenNIGrabber::OpenNI_VGA_30Hz);

    string msg("Mouse coordinates");
    string msg1("Hello");
    string msg2("World");
    string msg3("Member function");
    viewer.registerMouseCallback (&SimpleOpenNIViewer::mouse_callback, *this, (void*)(&msg));
    
    // registering same callback function twice but with different custom data (cookies)
    viewer.registerKeyboardCallback(keyCallback, (void*)(&msg1));
    viewer.registerKeyboardCallback(keyCallback, (void*)(&msg2));
    viewer.registerKeyboardCallback(&SimpleOpenNIViewer::keyboard_callback, *this, (void*)(&msg3));

    boost::function<void (const CloudConstPtr&) > f = boost::bind (&SimpleOpenNIViewer::cloud_cb_, this, _1);

    boost::signals2::connection c = grabber_.registerCallback (f);

    grabber_.start();

    while (!viewer.wasStopped ())
    {
      if (cloud_)
      {
        //FPS_CALC ("drawing");
        //the call to get() sets the cloud_ to null;
        viewer.showCloud (getLatestCloud ());
      }
    }

    grabber_.stop();
  }

  pcl::visualization::CloudViewer viewer;
  pcl::OpenNIGrabber& grabber_;
  boost::mutex mtx_;
  CloudConstPtr cloud_;
};

void
usage(char ** argv)
{
  cout << "usage: " << argv[0] << " [((<device_id> | <path-to-oni-file>) [-depthmode <mode>] [-imagemode <mode>] [-xyz] | -l [<device_id>]| -h | --help)]" << endl;
  cout << argv[0] << " -h | --help : shows this help" << endl;
  cout << argv[0] << " -l : list all available devices" << endl;
  cout << argv[0] << " -l <device-id> : list all available modes for specified device" << endl;

  cout << "                 device_id may be #1, #2, ... for the first, second etc device in the list"
#ifndef _WIN32
       << " or" << endl
       << "                 bus@address for the device connected to a specific usb-bus / address combination or" << endl
       << "                 <serial-number>"
#endif
       << endl;
  cout << endl;
  cout << "examples:" << endl;
  cout << argv[0] << " \"#1\"" << endl;
  cout << "    uses the first device." << endl;
  cout << argv[0] << " \"./temp/test.oni\"" << endl;
  cout << "    uses the oni-player device to play back oni file given by path." << endl;
  cout << argv[0] << " -l" << endl;<commit_msg>image viewer added<commit_after>/*
 * Software License Agreement (BSD License)
 *
 *  Copyright (c) 2011, <NAME>, Inc.
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above
 *     copyright notice, this list of conditions and the following
 *     disclaimer in the documentation and/or other materials provided
 *     with the distribution.
 *   * Neither the name of <NAME>, Inc. nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 *  POSSIBILITY OF SUCH DAMAGE.
 *	
 * Author: <NAME> (<EMAIL>)
 *         <NAME> (<EMAIL>)
 *         <NAME> (<EMAIL>)
 *         <NAME> (<EMAIL>)
 */

#include <boost/thread/thread.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/common/time.h> //fps calculations
#include <pcl/io/openni_grabber.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/visualization/image_viewer.h>
#include <pcl/io/openni_camera/openni_driver.h>
#include <pcl/console/parse.h>
#include <pcl/visualization/mouse_event.h>
#include <vtkImageViewer.h>
#include <vtkImageImport.h>
#include <vector>
#include <string>

using namespace std;

#define SHOW_FPS 1
#if SHOW_FPS
#define FPS_CALC(_WHAT_) \
do \
{ \
    static unsigned count = 0;\
    static double last = pcl::getTime ();\
    double now = pcl::getTime (); \
    ++count; \
    if (now - last >= 1.0) \
    { \
      std::cout << "Average framerate("<< _WHAT_ << "): " << double(count)/double(now - last) << " Hz" <<  std::endl; \
      count = 0; \
      last = now; \
    } \
}while(false)
#else
#define FPS_CALC(_WHAT_) \
do \
{ \
}while(false)
#endif

template <typename PointType>
class SimpleOpenNIViewer
{
public:
  typedef pcl::PointCloud<PointType> Cloud;
  typedef typename Cloud::ConstPtr CloudConstPtr;

  SimpleOpenNIViewer(pcl::OpenNIGrabber& grabber)
    : cloud_viewer_("PCL OpenNI Viewer")
    , grabber_(grabber)
    , image_viewer_ ("PCL image viewer")
  {
  }

  /**
   * @brief Callback method for the grabber interface
   * @param cloud The new point cloud from Grabber
   */
  void
  cloud_callback (const CloudConstPtr& cloud)
  {
    //FPS_CALC ("callback");
    boost::mutex::scoped_lock lock (cloud_mutex_);
    cloud_ = cloud;
  }

  void
  image_callback (const boost::shared_ptr<openni_wrapper::Image>& image)
  {
    boost::mutex::scoped_lock lock (image_mutex_);
    image_ = image;
  }
  
  void keyboard_callback (const pcl::visualization::KeyboardEvent& event, void* cookie)
  {
    string* message = (string*)cookie;
    cout << (*message) << " :: ";
    if (event.getKeyCode())
      cout << "the key \'" << event.getKeyCode() << "\' (" << (int)event.getKeyCode() << ") was";
    else
      cout << "the special key \'" << event.getKeySym() << "\' was";
    if (event.keyDown())
      cout << " pressed" << endl;
    else
      cout << " released" << endl;
  }
  
  void mouse_callback (const pcl::visualization::MouseEvent& mouse_event, void* cookie)
  {
    string* message = (string*) cookie;
    if (mouse_event.getType() == pcl::visualization::MouseEvent::MouseButtonPress && mouse_event.getButton() == pcl::visualization::MouseEvent::LeftButton)
    {
      cout << (*message) << " :: " << mouse_event.getX () << " , " << mouse_event.getY () << endl;
    }
  }
  /**
   * @brief swaps the pointer to the point cloud with Null pointer and returns the cloud pointer
   * @return boost shared pointer to point cloud
   */
  CloudConstPtr
  getLatestCloud ()
  {
    //lock while we swap our cloud and reset it.
    boost::mutex::scoped_lock lock(cloud_mutex_);
    CloudConstPtr temp_cloud;
    temp_cloud.swap (cloud_); //here we set cloud_ to null, so that
    //it is safe to set it again from our
    //callback
    return (temp_cloud);
  }

  boost::shared_ptr<openni_wrapper::Image>
  getLatestImage ()
  {
    boost::mutex::scoped_lock lock(image_mutex_);
    boost::shared_ptr<openni_wrapper::Image> temp_image;
    temp_image.swap (image_);
    return temp_image;
  }
  /**
   * @brief starts the main loop
   */
  void
  run()
  {
    //pcl::Grabber* interface = new pcl::OpenNIGrabber(device_id_, pcl::OpenNIGrabber::OpenNI_QQVGA_30Hz, pcl::OpenNIGrabber::OpenNI_VGA_30Hz);

    string mouseMsg3D("Mouse coordinates in PCL Visualizer");
    string mouseMsg2D("Mouse coordinates in image viewer");
    string keyMsg3D("Key event for PCL Visualizer");
    string keyMsg2D("Key event for image viewer");
    //cloud_viewer_.registerMouseCallback (&SimpleOpenNIViewer::mouse_callback, *this, (void*)(&mouseMsg3D));    
    //cloud_viewer_.registerKeyboardCallback(&SimpleOpenNIViewer::keyboard_callback, *this, (void*)(&keyMsg3D));

    image_viewer_.registerMouseCallback (&SimpleOpenNIViewer::mouse_callback, *this, (void*)(&mouseMsg2D));
    image_viewer_.registerKeyboardCallback(&SimpleOpenNIViewer::keyboard_callback, *this, (void*)(&keyMsg2D));
      
    boost::function<void (const CloudConstPtr&) > cloud_cb = boost::bind (&SimpleOpenNIViewer::cloud_callback, this, _1);
    boost::signals2::connection cloud_connection = grabber_.registerCallback (cloud_cb);
    
    boost::function<void (const boost::shared_ptr<openni_wrapper::Image>&) > image_cb = boost::bind (&SimpleOpenNIViewer::image_callback, this, _1);
    boost::signals2::connection image_connection = grabber_.registerCallback (image_cb);
    
    grabber_.start();
    
    unsigned char* rgb_data = 0;
    unsigned rgb_data_size = 0;
    
    //while (!cloud_viewer_.wasStopped ())
    while (!image_viewer_.wasStopped ())
    {
//      if (cloud_)
//      {
//        //FPS_CALC ("drawing");
//        //the call to get() sets the cloud_ to null;
//        cloud_viewer_.showCloud (getLatestCloud ());
//      }
      //*
      if (image_)
      {
        boost::shared_ptr<openni_wrapper::Image> image = getLatestImage();

        if (image->getEncoding() == openni_wrapper::Image::RGB)
        {
          image_viewer_.showRGBImage(image->getMetaData().Data(), image->getWidth(), image->getHeight());
        }
        else
        {
          if (rgb_data_size < image->getWidth() * image->getHeight())
          {
            rgb_data_size = image->getWidth() * image->getHeight();
            rgb_data = new unsigned char [rgb_data_size * 3];
          }
          image->fillRGB (image->getWidth(), image->getHeight(), rgb_data);
          image_viewer_.showRGBImage(rgb_data, image->getWidth(), image->getHeight());
        }
      }
      //image_viewer_.spinOnce(10);
      //*/
    }

    grabber_.stop();
    
    cloud_connection.disconnect();
    image_connection.disconnect();
    
    if (rgb_data)
      delete[] rgb_data;
  }

  pcl::visualization::CloudViewer cloud_viewer_;
  pcl::OpenNIGrabber& grabber_;
  boost::mutex cloud_mutex_;
  CloudConstPtr cloud_;
  
  boost::mutex image_mutex_;
  boost::shared_ptr<openni_wrapper::Image> image_;
  pcl::visualization::ImageViewer image_viewer_;
};

void
usage(char ** argv)
{
  cout << "usage: " << argv[0] << " [((<device_id> | <path-to-oni-file>) [-depthmode <mode>] [-imagemode <mode>] [-xyz] | -l [<device_id>]| -h | --help)]" << endl;
  cout << argv[0] << " -h | --help : shows this help" << endl;
  cout << argv[0] << " -l : list all available devices" << endl;
  cout << argv[0] << " -l <device-id> : list all available modes for specified device" << endl;

  cout << "                 device_id may be #1, #2, ... for the first, second etc device in the list"
#ifndef _WIN32
       << " or" << endl
       << "                 bus@address for the device connected to a specific usb-bus / address combination or" << endl
       << "                 <serial-number>"
#endif
       << endl;
  cout << endl;
  cout << "examples:" << endl;
  cout << argv[0] << " \"#1\"" << endl;
  cout << "    uses the first device." << endl;
  cout << argv[0] << " \"./temp/test.oni\"" << endl;
  cout << "    uses the oni-player device to play back oni file given by path." << endl;
  cout << argv[0] << " -l" << endl;
{'fraction_non_alphanumeric': 0.13867386738673868, 'fraction_numerical': 0.004500450045004501, 'mean_word_length': 3.2821171634121273, 'pattern_counts': {'":': 0, '<': 352, '<?xml version=': 0, '>': 90, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2357818', 'n_tokens_mistral': 5412, 'n_tokens_neox': 5251, 'n_words': 1485}