mirror of
https://github.com/opencv/opencv.git
synced 2026-07-31 00:03:03 +04:00
Added new functionalities to viz module
- load OBJ file - set offscreen rendering - set roll angle of the camera - get Mat screenshot of the current scene - remove all lights from the scene - add a custom light in the scene - modify the size of the WImage3D widget - added ambient property for the widget Changed Vec3d in cv::viz::Color Renamed method getMatScreenshotin getScreenshot Modified showWidget Fixed on viz::Color and reverted fix on vtkProp3D Removed cameraRoll method Merged load mesh method (for ply and obj file) Fixed doc Fixed cv::viz::WImage3D::setSize for vtk5.8 Fixed enum for cv::viz::Mesh::load
This commit is contained in:
committed by
antonella
parent
424c2bddb3
commit
079ceea616
@@ -130,6 +130,7 @@
|
||||
#include <vtkElevationFilter.h>
|
||||
#include <vtkColorTransferFunction.h>
|
||||
#include <vtkStreamingDemandDrivenPipeline.h>
|
||||
#include <vtkLight.h>
|
||||
#include "vtkCallbackCommand.h"
|
||||
|
||||
#if !defined(_WIN32) || defined(__CYGWIN__)
|
||||
|
||||
@@ -728,6 +728,24 @@ void cv::viz::WImage3D::setImage(InputArray image)
|
||||
actor->SetTexture(texture);
|
||||
}
|
||||
|
||||
void cv::viz::WImage3D::setSize(const cv::Size& size)
|
||||
{
|
||||
vtkSmartPointer<vtkActor> actor = vtkActor::SafeDownCast(WidgetAccessor::getProp(*this));
|
||||
vtkSmartPointer<vtkPolyDataMapper> mapper = vtkPolyDataMapper::SafeDownCast(actor->GetMapper());
|
||||
vtkSmartPointer<vtkTextureMapToPlane> textured_plane;
|
||||
vtkSmartPointer<vtkPlaneSource> plane;
|
||||
#if VTK_MAJOR_VERSION <= 5
|
||||
textured_plane = vtkTextureMapToPlane::SafeDownCast(mapper->GetInputConnection(0,0)->GetProducer());
|
||||
plane = vtkPlaneSource::SafeDownCast(textured_plane->GetInputConnection(0,0)->GetProducer());
|
||||
#else
|
||||
textured_plane = vtkTextureMapToPlane::SafeDownCast(mapper->GetInputAlgorithm());
|
||||
plane = vtkPlaneSource::SafeDownCast(textured_plane->GetInputAlgorithm());
|
||||
#endif
|
||||
plane->SetOrigin(-0.5 * size.width, -0.5 * size.height, 0.0);
|
||||
plane->SetPoint1( 0.5 * size.width, -0.5 * size.height, 0.0);
|
||||
plane->SetPoint2(-0.5 * size.width, 0.5 * size.height, 0.0);
|
||||
}
|
||||
|
||||
template<> cv::viz::WImage3D cv::viz::Widget::cast<cv::viz::WImage3D>()
|
||||
{
|
||||
Widget3D widget = this->cast<Widget3D>();
|
||||
|
||||
@@ -57,11 +57,35 @@ cv::viz::MouseEvent::MouseEvent(const Type& _type, const MouseButton& _button, c
|
||||
////////////////////////////////////////////////////////////////////
|
||||
/// cv::viz::Mesh3d
|
||||
|
||||
cv::viz::Mesh cv::viz::Mesh::load(const String& file)
|
||||
cv::viz::Mesh cv::viz::Mesh::load(const String& file, int type)
|
||||
{
|
||||
vtkSmartPointer<vtkPLYReader> reader = vtkSmartPointer<vtkPLYReader>::New();
|
||||
reader->SetFileName(file.c_str());
|
||||
reader->Update();
|
||||
vtkSmartPointer<vtkPolyDataAlgorithm> reader = vtkSmartPointer<vtkPolyDataAlgorithm>::New();
|
||||
switch (type) {
|
||||
case LOAD_AUTO:
|
||||
{
|
||||
CV_Assert(!"cv::viz::Mesh::LOAD_AUTO: Not implemented yet");
|
||||
break;
|
||||
}
|
||||
case LOAD_PLY:
|
||||
{
|
||||
vtkSmartPointer<vtkPLYReader> ply_reader = vtkSmartPointer<vtkPLYReader>::New();
|
||||
ply_reader->SetFileName(file.c_str());
|
||||
ply_reader->Update();
|
||||
reader = ply_reader;
|
||||
break;
|
||||
}
|
||||
case LOAD_OBJ:
|
||||
{
|
||||
vtkSmartPointer<vtkOBJReader> obj_reader = vtkSmartPointer<vtkOBJReader>::New();
|
||||
obj_reader->SetFileName(file.c_str());
|
||||
obj_reader->Update();
|
||||
reader = obj_reader;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
CV_Assert(!"cv::viz::Mesh::load: Unknown file type");
|
||||
break;
|
||||
}
|
||||
|
||||
vtkSmartPointer<vtkPolyData> polydata = reader->GetOutput();
|
||||
CV_Assert("File does not exist or file format is not supported." && polydata);
|
||||
|
||||
@@ -100,6 +100,10 @@ void cv::viz::Viz3d::release()
|
||||
|
||||
void cv::viz::Viz3d::spin() { impl_->spin(); }
|
||||
void cv::viz::Viz3d::spinOnce(int time, bool force_redraw) { impl_->spinOnce(time, force_redraw); }
|
||||
void cv::viz::Viz3d::setOffScreenRendering() { impl_->setOffScreenRendering(); }
|
||||
void cv::viz::Viz3d::removeAllLights() { impl_->removeAllLights(); }
|
||||
void cv::viz::Viz3d::addLight(Vec3d position, Vec3d focalPoint, Color color, Color diffuseColor, Color ambientColor, Color specularColor)
|
||||
{ impl_->addLight(position, focalPoint, color, diffuseColor, ambientColor, specularColor); }
|
||||
bool cv::viz::Viz3d::wasStopped() const { return impl_->wasStopped(); }
|
||||
void cv::viz::Viz3d::close() { impl_->close(); }
|
||||
|
||||
@@ -134,6 +138,7 @@ void cv::viz::Viz3d::converTo3DRay(const Point3d &window_coord, Point3d &origin,
|
||||
cv::Size cv::viz::Viz3d::getWindowSize() const { return impl_->getWindowSize(); }
|
||||
void cv::viz::Viz3d::setWindowSize(const Size &window_size) { impl_->setWindowSize(window_size); }
|
||||
cv::String cv::viz::Viz3d::getWindowName() const { return impl_->getWindowName(); }
|
||||
cv::Mat cv::viz::Viz3d::getScreenshot() const { return impl_->getScreenshot(); }
|
||||
void cv::viz::Viz3d::saveScreenshot(const String &file) { impl_->saveScreenshot(file); }
|
||||
void cv::viz::Viz3d::setWindowPosition(const Point& window_position) { impl_->setWindowPosition(window_position); }
|
||||
void cv::viz::Viz3d::setFullScreen(bool mode) { impl_->setFullScreen(mode); }
|
||||
|
||||
@@ -68,6 +68,8 @@ cv::viz::Viz3d::VizImpl::VizImpl(const String &name) : spin_once_state_(false),
|
||||
exit_callback_ = vtkSmartPointer<ExitCallback>::New();
|
||||
exit_callback_->viz = this;
|
||||
|
||||
offScreenMode_ = false;
|
||||
|
||||
setBackgroundMeshLab();
|
||||
}
|
||||
|
||||
@@ -187,6 +189,38 @@ void cv::viz::Viz3d::VizImpl::spinOnce(int time, bool force_redraw)
|
||||
local->DestroyTimer(timer_callback_->timer_id);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void cv::viz::Viz3d::VizImpl::setOffScreenRendering()
|
||||
{
|
||||
window_->SetOffScreenRendering(1);
|
||||
offScreenMode_ = true;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void cv::viz::Viz3d::VizImpl::removeAllLights()
|
||||
{
|
||||
renderer_->RemoveAllLights();
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void cv::viz::Viz3d::VizImpl::addLight(Vec3d position, Vec3d focalPoint, Color color, Color diffuseColor, Color ambientColor, Color specularColor)
|
||||
{
|
||||
Color color_ = vtkcolor(color);
|
||||
Color diffuseColor_ = vtkcolor(diffuseColor);
|
||||
Color ambientColor_ = vtkcolor(ambientColor);
|
||||
Color specularColor_ = vtkcolor(specularColor);
|
||||
|
||||
vtkSmartPointer<vtkLight> light = vtkSmartPointer<vtkLight>::New();
|
||||
light->SetPosition(position.val);
|
||||
light->SetFocalPoint(focalPoint.val);
|
||||
light->SetColor(color_.val);
|
||||
light->SetDiffuseColor(diffuseColor_.val);
|
||||
light->SetAmbientColor(ambientColor_.val);
|
||||
light->SetSpecularColor(specularColor_.val);
|
||||
|
||||
renderer_->AddLight(light);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void cv::viz::Viz3d::VizImpl::showWidget(const String &id, const Widget &widget, const Affine3d &pose)
|
||||
{
|
||||
@@ -293,6 +327,39 @@ cv::Affine3d cv::viz::Viz3d::VizImpl::getWidgetPose(const String &id) const
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void cv::viz::Viz3d::VizImpl::saveScreenshot(const String &file) { style_->saveScreenshot(file.c_str()); }
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||
cv::Mat cv::viz::Viz3d::VizImpl::getScreenshot() const
|
||||
{
|
||||
vtkSmartPointer<vtkWindowToImageFilter> windowToImageFilter =
|
||||
vtkSmartPointer<vtkWindowToImageFilter>::New();
|
||||
windowToImageFilter->SetInput(window_);
|
||||
windowToImageFilter->ReadFrontBufferOff(); // read from the back buffer
|
||||
windowToImageFilter->Update();
|
||||
|
||||
vtkImageData *resultImage = windowToImageFilter->GetOutput();
|
||||
int * dim = resultImage->GetDimensions();
|
||||
cv::Mat image(dim[1], dim[0], CV_8UC3);
|
||||
|
||||
Vec3b* dptr = reinterpret_cast<Vec3b*>(resultImage->GetScalarPointer());
|
||||
size_t elem_step = resultImage->GetIncrements()[1]/sizeof(Vec3b);
|
||||
|
||||
for (int y = 0; y < image.rows; ++y)
|
||||
{
|
||||
const Vec3b* drow = dptr + elem_step * y;
|
||||
unsigned char *srow = image.ptr<unsigned char>(image.rows - y - 1);
|
||||
for (int x = 0; x < image.cols; ++x, srow += image.channels())
|
||||
{
|
||||
srow[0] = drow[x][2];
|
||||
srow[1] = drow[x][1];
|
||||
srow[2] = drow[x][0];
|
||||
}
|
||||
}
|
||||
|
||||
resultImage = 0;
|
||||
|
||||
return image;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////////////////////
|
||||
void cv::viz::Viz3d::VizImpl::registerMouseCallback(MouseCallback callback, void* cookie)
|
||||
{ style_->registerMouseCallback(callback, cookie); }
|
||||
|
||||
@@ -62,6 +62,10 @@ public:
|
||||
|
||||
void spin();
|
||||
void spinOnce(int time = 1, bool force_redraw = false);
|
||||
void setOffScreenRendering();
|
||||
|
||||
void removeAllLights();
|
||||
void addLight(Vec3d position, Vec3d focalPoint, Color color, Color diffuseColor, Color ambientColor, Color specularColor);
|
||||
|
||||
void showWidget(const String &id, const Widget &widget, const Affine3d &pose = Affine3d::Identity());
|
||||
void removeWidget(const String &id);
|
||||
@@ -89,6 +93,7 @@ public:
|
||||
void convertToWindowCoordinates(const Point3d &pt, Point3d &window_coord);
|
||||
void converTo3DRay(const Point3d &window_coord, Point3d &origin, Vec3d &direction);
|
||||
|
||||
Mat getScreenshot() const;
|
||||
void saveScreenshot(const String &file);
|
||||
void setWindowPosition(const Point& position);
|
||||
Size getWindowSize() const;
|
||||
@@ -131,6 +136,8 @@ private:
|
||||
vtkSmartPointer<vtkVizInteractorStyle> style_;
|
||||
Ptr<WidgetActorMap> widget_actor_map_;
|
||||
|
||||
bool offScreenMode_;
|
||||
|
||||
bool removeActorFromRenderer(vtkSmartPointer<vtkProp> actor);
|
||||
void recreateRenderWindow();
|
||||
};
|
||||
|
||||
@@ -114,6 +114,7 @@ void cv::viz::Widget::setRenderingProperty(int property, double value)
|
||||
case OPACITY: actor->GetProperty()->SetOpacity(value); break;
|
||||
case LINE_WIDTH: actor->GetProperty()->SetLineWidth(float(value)); break;
|
||||
case IMMEDIATE_RENDERING: actor->GetMapper()->SetImmediateModeRendering(int(value)); break;
|
||||
case AMBIENT: actor->GetProperty()->SetAmbient(float(value)); break;
|
||||
case FONT_SIZE:
|
||||
{
|
||||
vtkTextActor* text_actor = vtkTextActor::SafeDownCast(actor);
|
||||
|
||||
Reference in New Issue
Block a user