From d0bd344a42e90f07856b22aa2626dd5b827fc71c Mon Sep 17 00:00:00 2001 From: ozantonkal Date: Mon, 22 Jul 2013 16:48:17 +0200 Subject: [PATCH] image 3d widget use texture instead of imageactor for more flexibility (such as resizing) --- modules/viz/include/opencv2/viz/widgets.hpp | 2 +- modules/viz/src/precomp.hpp | 1 + modules/viz/src/shape_widgets.cpp | 42 ++++++++++++++++++--- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/modules/viz/include/opencv2/viz/widgets.hpp b/modules/viz/include/opencv2/viz/widgets.hpp index 7b7d09725..3559a093d 100644 --- a/modules/viz/include/opencv2/viz/widgets.hpp +++ b/modules/viz/include/opencv2/viz/widgets.hpp @@ -158,7 +158,7 @@ namespace cv class CV_EXPORTS Image3DWidget : public Widget3D { public: - Image3DWidget(const Mat &image); + Image3DWidget(const Mat &image, const Size &size); void setImage(const Mat &image); diff --git a/modules/viz/src/precomp.hpp b/modules/viz/src/precomp.hpp index 148fbb54d..12a042c15 100644 --- a/modules/viz/src/precomp.hpp +++ b/modules/viz/src/precomp.hpp @@ -135,6 +135,7 @@ #include #include #include +#include #include #include diff --git a/modules/viz/src/shape_widgets.cpp b/modules/viz/src/shape_widgets.cpp index 4b999a20b..18e232f97 100644 --- a/modules/viz/src/shape_widgets.cpp +++ b/modules/viz/src/shape_widgets.cpp @@ -752,7 +752,7 @@ struct cv::viz::Image3DWidget::CopyImpl } }; -cv::viz::Image3DWidget::Image3DWidget(const Mat &image) +cv::viz::Image3DWidget::Image3DWidget(const Mat &image, const Size &size) { CV_Assert(!image.empty() && image.depth() == CV_8U); @@ -771,9 +771,37 @@ cv::viz::Image3DWidget::Image3DWidget(const Mat &image) flipFilter->SetInputConnection(vtk_image->GetProducerPort()); flipFilter->Update(); - vtkSmartPointer actor = vtkSmartPointer::New(); - actor->SetInput(flipFilter->GetOutput()); + Vec3d plane_center(size.width * 0.5, size.height * 0.5, 0.0); + vtkSmartPointer plane = vtkSmartPointer::New(); + plane->SetCenter(plane_center[0], plane_center[1], plane_center[2]); + plane->SetNormal(0.0, 0.0, 1.0); + + vtkSmartPointer transform = vtkSmartPointer::New(); + transform->PreMultiply(); + transform->Translate(plane_center[0], plane_center[1], plane_center[2]); + transform->Scale(size.width, size.height, 1.0); + transform->Translate(-plane_center[0], -plane_center[1], -plane_center[2]); + + vtkSmartPointer transform_filter = vtkSmartPointer::New(); + transform_filter->SetTransform(transform); + transform_filter->SetInputConnection(plane->GetOutputPort()); + transform_filter->Update(); + + // Apply the texture + vtkSmartPointer texture = vtkSmartPointer::New(); + texture->SetInputConnection(flipFilter->GetOutputPort()); + + vtkSmartPointer texturePlane = vtkSmartPointer::New(); + texturePlane->SetInputConnection(transform_filter->GetOutputPort()); + + vtkSmartPointer planeMapper = vtkSmartPointer::New(); + planeMapper->SetInputConnection(texturePlane->GetOutputPort()); + + vtkSmartPointer actor = vtkSmartPointer::New(); + actor->SetMapper(planeMapper); + actor->SetTexture(texture); + WidgetAccessor::setProp(*this, actor); } @@ -781,7 +809,7 @@ void cv::viz::Image3DWidget::setImage(const Mat &image) { CV_Assert(!image.empty() && image.depth() == CV_8U); - vtkImageActor *actor = vtkImageActor::SafeDownCast(WidgetAccessor::getProp(*this)); + vtkActor *actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this)); CV_Assert(actor); // Create the vtk image and set its parameters based on input image @@ -799,7 +827,11 @@ void cv::viz::Image3DWidget::setImage(const Mat &image) flipFilter->SetInputConnection(vtk_image->GetProducerPort()); flipFilter->Update(); - actor->SetInput(flipFilter->GetOutput()); + // Apply the texture + vtkSmartPointer texture = vtkSmartPointer::New(); + texture->SetInputConnection(flipFilter->GetOutputPort()); + + actor->SetTexture(texture); } template<> cv::viz::Image3DWidget cv::viz::Widget::cast()