opencv/modules/viz/src/viz3d_impl.hpp

396 lines
15 KiB
C++
Raw Normal View History

2013-09-08 21:20:02 +08:00
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's 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.
//
// * The name of the copyright holders may not 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 Intel Corporation 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.
//
// Authors:
// * Ozan Tonkal, ozantonkal@gmail.com
// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com
//
// OpenCV Viz module is complete rewrite of
// PCL visualization module (www.pointclouds.org)
//
//M*/
#ifndef __OPENCV_VIZ_VIZ3D_IMPL_HPP__
#define __OPENCV_VIZ_VIZ3D_IMPL_HPP__
2013-03-18 23:52:46 +08:00
2013-07-13 23:08:25 +08:00
#include <opencv2/viz.hpp>
2013-07-13 21:45:46 +08:00
#include "interactor_style.h"
2013-03-18 23:52:46 +08:00
2013-07-13 22:01:56 +08:00
struct cv::viz::Viz3d::VizImpl
2013-03-18 23:52:46 +08:00
{
public:
typedef cv::Ptr<VizImpl> Ptr;
2013-07-13 23:08:25 +08:00
typedef Viz3d::KeyboardCallback KeyboardCallback;
typedef Viz3d::MouseCallback MouseCallback;
int ref_counter;
2013-03-18 23:52:46 +08:00
2013-09-08 21:20:02 +08:00
VizImpl(const String &name);
virtual ~VizImpl();
void showWidget(const String &id, const Widget &widget, const Affine3f &pose = Affine3f::Identity());
void removeWidget(const String &id);
Widget getWidget(const String &id) const;
void removeAllWidgets();
void setWidgetPose(const String &id, const Affine3f &pose);
void updateWidgetPose(const String &id, const Affine3f &pose);
Affine3f getWidgetPose(const String &id) const;
2013-03-18 23:52:46 +08:00
void setDesiredUpdateRate(double rate);
double getDesiredUpdateRate();
2013-03-18 23:52:46 +08:00
/** \brief Returns true when the user tried to close the window */
2013-09-08 21:20:02 +08:00
bool wasStopped() const { if (interactor_ != NULL) return (stopped_); else return true; }
2013-03-18 23:52:46 +08:00
/** \brief Set the stopped flag back to false */
2013-09-08 21:20:02 +08:00
void resetStoppedFlag() { if (interactor_ != NULL) stopped_ = false; }
2013-03-18 23:52:46 +08:00
/** \brief Stop the interaction and close the visualizaton window. */
2013-09-08 21:20:02 +08:00
void close()
2013-03-18 23:52:46 +08:00
{
stopped_ = true;
if (interactor_)
{
interactor_->GetRenderWindow()->Finalize();
2013-09-08 21:20:02 +08:00
interactor_->TerminateApp(); // This tends to close the window...
}
2013-03-18 23:52:46 +08:00
}
void setRepresentation(int representation);
2013-08-03 22:33:11 +08:00
2013-08-05 21:37:08 +08:00
void setCamera(const Camera &camera);
Camera getCamera() const;
2013-09-08 21:20:02 +08:00
/** \brief Reset the camera to a given widget */
2013-08-31 18:16:47 +08:00
void resetCameraViewpoint(const String& id);
void resetCamera();
2013-08-01 21:22:35 +08:00
void setViewerPose(const Affine3f &pose);
Affine3f getViewerPose();
void convertToWindowCoordinates(const Point3d &pt, Point3d &window_coord);
void converTo3DRay(const Point3d &window_coord, Point3d &origin, Vec3d &direction);
2013-09-08 21:20:02 +08:00
void saveScreenshot(const String &file);
void setWindowPosition(int x, int y);
2013-08-07 15:28:39 +08:00
Size getWindowSize() const;
2013-09-08 21:20:02 +08:00
void setWindowSize(int xw, int yw);
void setFullScreen(bool mode);
String getWindowName() const;
2013-09-08 21:20:02 +08:00
void setBackgroundColor(const Color& color);
2013-03-18 23:52:46 +08:00
2013-09-08 21:20:02 +08:00
void spin();
void spinOnce(int time = 1, bool force_redraw = false);
2013-03-18 23:52:46 +08:00
void registerKeyboardCallback(KeyboardCallback callback, void* cookie = 0);
void registerMouseCallback(MouseCallback callback, void* cookie = 0);
2013-03-18 23:52:46 +08:00
private:
vtkSmartPointer<vtkRenderWindowInteractor> interactor_;
struct ExitMainLoopTimerCallback : public vtkCommand
{
static ExitMainLoopTimerCallback* New()
{
return new ExitMainLoopTimerCallback;
}
virtual void Execute(vtkObject* vtkNotUsed(caller), unsigned long event_id, void* call_data)
{
if (event_id != vtkCommand::TimerEvent)
return;
2013-09-08 21:20:02 +08:00
int timer_id = *reinterpret_cast<int*>(call_data);
2013-03-18 23:52:46 +08:00
if (timer_id != right_timer_id)
return;
// Stop vtk loop and send notification to app to wake it up
2013-09-08 21:20:02 +08:00
viz_->interactor_->TerminateApp();
2013-03-18 23:52:46 +08:00
}
int right_timer_id;
VizImpl* viz_;
};
struct ExitCallback : public vtkCommand
{
2013-09-08 21:20:02 +08:00
static ExitCallback* New()
2013-03-18 23:52:46 +08:00
{
return new ExitCallback;
}
2013-09-08 21:20:02 +08:00
virtual void Execute(vtkObject*, unsigned long event_id, void*)
2013-03-18 23:52:46 +08:00
{
if (event_id == vtkCommand::ExitEvent)
{
viz_->stopped_ = true;
viz_->interactor_->GetRenderWindow()->Finalize();
2013-09-08 21:20:02 +08:00
viz_->interactor_->TerminateApp();
2013-03-18 23:52:46 +08:00
}
}
VizImpl* viz_;
};
/** \brief Set to false if the interaction loop is running. */
bool stopped_;
double s_lastDone_;
/** \brief Global timer ID. Used in destructor only. */
int timer_id_;
/** \brief Callback object enabling us to leave the main loop, when a timer fires. */
vtkSmartPointer<ExitMainLoopTimerCallback> exit_main_loop_timer_callback_;
vtkSmartPointer<ExitCallback> exit_callback_;
vtkSmartPointer<vtkRenderer> renderer_;
vtkSmartPointer<vtkRenderWindow> window_;
/** \brief The render window interactor style. */
vtkSmartPointer<InteractorStyle> style_;
/** \brief Internal list with actor pointers and name IDs for all widget actors */
cv::Ptr<WidgetActorMap> widget_actor_map_;
2013-03-18 23:52:46 +08:00
/** \brief Boolean that holds whether or not the camera parameters were manually initialized*/
bool camera_set_;
2013-09-08 21:20:02 +08:00
bool removeActorFromRenderer(const vtkSmartPointer<vtkProp> &actor);
2013-03-18 23:52:46 +08:00
/** \brief Internal method. Creates a vtk actor from a vtk polydata object.
* \param[in] data the vtk polydata object to create an actor for
* \param[out] actor the resultant vtk actor object
* \param[in] use_scalars set scalar properties to the mapper if it exists in the data. Default: true.
*/
2013-09-08 21:20:02 +08:00
void createActorFromVTKDataSet(const vtkSmartPointer<vtkDataSet> &data, vtkSmartPointer<vtkLODActor> &actor, bool use_scalars = true);
2013-03-18 23:52:46 +08:00
/** \brief Updates a set of cells (vtkIdTypeArray) if the number of points in a cloud changes
* \param[out] cells the vtkIdTypeArray object (set of cells) to update
* \param[out] initcells a previously saved set of cells. If the number of points in the current cloud is
* higher than the number of cells in \a cells, and initcells contains enough data, then a copy from it
* will be made instead of regenerating the entire array.
* \param[in] nr_points the number of points in the new cloud. This dictates how many cells we need to
* generate
*/
2013-09-08 21:20:02 +08:00
void updateCells(vtkSmartPointer<vtkIdTypeArray> &cells, vtkSmartPointer<vtkIdTypeArray> &initcells, vtkIdType nr_points);
2013-03-18 23:52:46 +08:00
};
2013-07-13 21:45:46 +08:00
2013-07-13 22:01:56 +08:00
namespace cv
2013-07-13 21:45:46 +08:00
{
2013-07-13 22:01:56 +08:00
namespace viz
{
2013-09-08 21:20:02 +08:00
vtkSmartPointer<vtkMatrix4x4> convertToVtkMatrix(const cv::Matx44f &m);
2013-07-13 22:01:56 +08:00
cv::Matx44f convertToMatx(const vtkSmartPointer<vtkMatrix4x4>& vtk_matrix);
2013-03-18 23:52:46 +08:00
2013-07-13 22:01:56 +08:00
struct NanFilter
2013-06-11 22:38:20 +08:00
{
2013-07-13 22:01:56 +08:00
template<typename _Tp, typename _Msk>
struct Impl
2013-06-11 22:38:20 +08:00
{
2013-07-13 22:01:56 +08:00
typedef Vec<_Tp, 3> _Out;
static _Out* copy(const Mat& source, _Out* output, const Mat& nan_mask)
{
CV_Assert(DataDepth<_Tp>::value == source.depth() && source.size() == nan_mask.size());
CV_Assert(nan_mask.channels() == 3 || nan_mask.channels() == 4);
CV_DbgAssert(DataDepth<_Msk>::value == nan_mask.depth());
int s_chs = source.channels();
int m_chs = nan_mask.channels();
2013-09-08 21:20:02 +08:00
for (int y = 0; y < source.rows; ++y)
2013-07-13 22:01:56 +08:00
{
const _Tp* srow = source.ptr<_Tp>(y);
const _Msk* mrow = nan_mask.ptr<_Msk>(y);
2013-09-08 21:20:02 +08:00
for (int x = 0; x < source.cols; ++x, srow += s_chs, mrow += m_chs)
2013-07-13 22:01:56 +08:00
if (!isNan(mrow[0]) && !isNan(mrow[1]) && !isNan(mrow[2]))
*output++ = _Out(srow);
}
return output;
}
2013-07-15 22:13:33 +08:00
static _Out* copyColor(const Mat& source, _Out* output, const Mat& nan_mask)
{
CV_Assert(DataDepth<_Tp>::value == source.depth() && source.size() == nan_mask.size());
CV_Assert(nan_mask.channels() == 3 || nan_mask.channels() == 4);
CV_DbgAssert(DataDepth<_Msk>::value == nan_mask.depth());
int s_chs = source.channels();
int m_chs = nan_mask.channels();
2013-09-08 21:20:02 +08:00
for (int y = 0; y < source.rows; ++y)
2013-07-15 22:13:33 +08:00
{
const _Tp* srow = source.ptr<_Tp>(y);
const _Msk* mrow = nan_mask.ptr<_Msk>(y);
2013-09-08 21:20:02 +08:00
for (int x = 0; x < source.cols; ++x, srow += s_chs, mrow += m_chs)
2013-07-15 22:13:33 +08:00
if (!isNan(mrow[0]) && !isNan(mrow[1]) && !isNan(mrow[2]))
{
*output = _Out(srow);
std::swap((*output)[0], (*output)[2]); // BGR -> RGB
++output;
}
}
return output;
}
2013-07-13 22:01:56 +08:00
};
template<typename _Tp>
static inline Vec<_Tp, 3>* copy(const Mat& source, Vec<_Tp, 3>* output, const Mat& nan_mask)
{
CV_Assert(nan_mask.depth() == CV_32F || nan_mask.depth() == CV_64F);
2013-07-13 22:01:56 +08:00
typedef Vec<_Tp, 3>* (*copy_func)(const Mat&, Vec<_Tp, 3>*, const Mat&);
const static copy_func table[2] = { &NanFilter::Impl<_Tp, float>::copy, &NanFilter::Impl<_Tp, double>::copy };
2013-07-13 22:01:56 +08:00
return table[nan_mask.depth() - 5](source, output, nan_mask);
}
2013-07-15 22:13:33 +08:00
template<typename _Tp>
static inline Vec<_Tp, 3>* copyColor(const Mat& source, Vec<_Tp, 3>* output, const Mat& nan_mask)
{
CV_Assert(nan_mask.depth() == CV_32F || nan_mask.depth() == CV_64F);
typedef Vec<_Tp, 3>* (*copy_func)(const Mat&, Vec<_Tp, 3>*, const Mat&);
const static copy_func table[2] = { &NanFilter::Impl<_Tp, float>::copyColor, &NanFilter::Impl<_Tp, double>::copyColor };
return table[nan_mask.depth() - 5](source, output, nan_mask);
}
2013-07-13 22:01:56 +08:00
};
2013-07-13 22:01:56 +08:00
struct ApplyAffine
{
const Affine3f& affine_;
ApplyAffine(const Affine3f& affine) : affine_(affine) {}
2013-06-11 16:48:48 +08:00
2013-07-13 22:01:56 +08:00
template<typename _Tp> Point3_<_Tp> operator()(const Point3_<_Tp>& p) const { return affine_ * p; }
2013-07-13 22:01:56 +08:00
template<typename _Tp> Vec<_Tp, 3> operator()(const Vec<_Tp, 3>& v) const
{
const float* m = affine_.matrix.val;
2013-07-13 22:01:56 +08:00
Vec<_Tp, 3> result;
result[0] = (_Tp)(m[0] * v[0] + m[1] * v[1] + m[ 2] * v[2] + m[ 3]);
result[1] = (_Tp)(m[4] * v[0] + m[5] * v[1] + m[ 6] * v[2] + m[ 7]);
result[2] = (_Tp)(m[8] * v[0] + m[9] * v[1] + m[10] * v[2] + m[11]);
return result;
}
2013-07-13 22:01:56 +08:00
private:
ApplyAffine(const ApplyAffine&);
ApplyAffine& operator=(const ApplyAffine&);
};
2013-07-13 23:08:25 +08:00
inline Color vtkcolor(const Color& color)
{
Color scaled_color = color * (1.0/255.0);
std::swap(scaled_color[0], scaled_color[2]);
return scaled_color;
}
inline Vec3d vtkpoint(const Point3f& point) { return Vec3d(point.x, point.y, point.z); }
template<typename _Tp> inline _Tp normalized(const _Tp& v) { return v * 1/cv::norm(v); }
struct ConvertToVtkImage
{
struct Impl
{
static void copyImageMultiChannel(const Mat &image, vtkSmartPointer<vtkImageData> output)
{
int i_chs = image.channels();
for (int i = 0; i < image.rows; ++i)
{
const unsigned char * irows = image.ptr<unsigned char>(i);
for (int j = 0; j < image.cols; ++j, irows += i_chs)
{
unsigned char * vrows = static_cast<unsigned char *>(output->GetScalarPointer(j,i,0));
memcpy(vrows, irows, i_chs);
std::swap(vrows[0], vrows[2]); // BGR -> RGB
}
}
output->Modified();
}
static void copyImageSingleChannel(const Mat &image, vtkSmartPointer<vtkImageData> output)
{
for (int i = 0; i < image.rows; ++i)
{
const unsigned char * irows = image.ptr<unsigned char>(i);
for (int j = 0; j < image.cols; ++j, ++irows)
{
unsigned char * vrows = static_cast<unsigned char *>(output->GetScalarPointer(j,i,0));
*vrows = *irows;
}
}
output->Modified();
}
};
static void convert(const Mat &image, vtkSmartPointer<vtkImageData> output)
{
// Create the vtk image
output->SetDimensions(image.cols, image.rows, 1);
#if VTK_MAJOR_VERSION <= 5
output->SetNumberOfScalarComponents(image.channels());
output->SetScalarTypeToUnsignedChar();
output->AllocateScalars();
#else
output->AllocateScalars(VTK_UNSIGNED_CHAR, image.channels());
#endif
int i_chs = image.channels();
if (i_chs > 1)
{
// Multi channel images are handled differently because of BGR <-> RGB
Impl::copyImageMultiChannel(image, output);
}
else
{
Impl::copyImageSingleChannel(image, output);
}
}
};
2013-06-11 16:48:48 +08:00
}
2013-03-18 23:52:46 +08:00
}
2013-09-08 21:20:02 +08:00
#endif