From eb8728d72751d78342d2dbb9fb11110380cf6c50 Mon Sep 17 00:00:00 2001 From: Anatoly Baksheev Date: Sun, 2 Feb 2014 19:00:54 +0400 Subject: [PATCH] Updated cloud io functionality - removed dependency from VTK legacy --- modules/viz/src/precomp.hpp | 4 +- modules/viz/src/vizcore.cpp | 4 +- modules/viz/src/vtk/vtkCloudMatSink.cpp | 16 +++++ modules/viz/src/vtk/vtkCloudMatSink.h | 12 +++- modules/viz/src/vtk/vtkOBJWriter.cpp | 37 +++++++++-- modules/viz/src/vtk/vtkOBJWriter.h | 22 +++++-- modules/viz/src/vtk/vtkXYZReader.cpp | 107 ++++++++++++++++++++++++++++++++ modules/viz/src/vtk/vtkXYZReader.h | 80 ++++++++++++++++++++++++ modules/viz/src/vtk/vtkXYZWriter.cpp | 37 +++++++++-- modules/viz/src/vtk/vtkXYZWriter.h | 18 +++++- 10 files changed, 313 insertions(+), 24 deletions(-) create mode 100644 modules/viz/src/vtk/vtkXYZReader.cpp create mode 100644 modules/viz/src/vtk/vtkXYZReader.h diff --git a/modules/viz/src/precomp.hpp b/modules/viz/src/precomp.hpp index d9681ce..2b34f56 100644 --- a/modules/viz/src/precomp.hpp +++ b/modules/viz/src/precomp.hpp @@ -76,7 +76,6 @@ #include #include #include -#include #include #include #include @@ -115,11 +114,9 @@ #include #include #include -#include #include #include #include -#include #include #include #include @@ -142,6 +139,7 @@ #include #include +#include #include #include #include diff --git a/modules/viz/src/vizcore.cpp b/modules/viz/src/vizcore.cpp index b4ec83b..b8cad1c 100644 --- a/modules/viz/src/vizcore.cpp +++ b/modules/viz/src/vizcore.cpp @@ -173,8 +173,8 @@ cv::Mat cv::viz::readCloud(const String& file, OutputArray colors, OutputArray n vtkSmartPointer reader; if (extention == ".xyz") { - reader = vtkSmartPointer::New(); - vtkSimplePointsReader::SafeDownCast(reader)->SetFileName(file.c_str()); + reader = vtkSmartPointer::New(); + vtkXYZReader::SafeDownCast(reader)->SetFileName(file.c_str()); } else if (extention == ".ply") { diff --git a/modules/viz/src/vtk/vtkCloudMatSink.cpp b/modules/viz/src/vtk/vtkCloudMatSink.cpp index 09ef0cc..7f47022 100644 --- a/modules/viz/src/vtk/vtkCloudMatSink.cpp +++ b/modules/viz/src/vtk/vtkCloudMatSink.cpp @@ -156,3 +156,19 @@ void cv::viz::vtkCloudMatSink::PrintSelf(ostream& os, vtkIndent indent) os << indent << "Colors: " << colors.needed() << "\n"; os << indent << "Normals: " << normals.needed() << "\n"; } + +int cv::viz::vtkCloudMatSink::FillInputPortInformation(int, vtkInformation *info) +{ + info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData"); + return 1; +} + +vtkPolyData* cv::viz::vtkCloudMatSink::GetInput() +{ + return vtkPolyData::SafeDownCast(this->Superclass::GetInput()); +} + +vtkPolyData* cv::viz::vtkCloudMatSink::GetInput(int port) +{ + return vtkPolyData::SafeDownCast(this->Superclass::GetInput(port)); +} diff --git a/modules/viz/src/vtk/vtkCloudMatSink.h b/modules/viz/src/vtk/vtkCloudMatSink.h index 44d7e52..b3fd3eb 100644 --- a/modules/viz/src/vtk/vtkCloudMatSink.h +++ b/modules/viz/src/vtk/vtkCloudMatSink.h @@ -46,26 +46,32 @@ #define __vtkCloudMatSink_h #include -#include +#include namespace cv { namespace viz { - class vtkCloudMatSink : public vtkPolyDataWriter + class vtkCloudMatSink : public vtkWriter { public: static vtkCloudMatSink *New(); - vtkTypeMacro(vtkCloudMatSink,vtkPolyDataWriter) + vtkTypeMacro(vtkCloudMatSink,vtkWriter) void PrintSelf(ostream& os, vtkIndent indent); void SetOutput(OutputArray cloud, OutputArray colors = noArray(), OutputArray normals = noArray(), OutputArray tcoords = noArray()); + // Description: + // Get the input to this writer. + vtkPolyData* GetInput(); + vtkPolyData* GetInput(int port); + protected: vtkCloudMatSink(); ~vtkCloudMatSink(); void WriteData(); + int FillInputPortInformation(int port, vtkInformation *info); _OutputArray cloud, colors, normals, tcoords; diff --git a/modules/viz/src/vtk/vtkOBJWriter.cpp b/modules/viz/src/vtk/vtkOBJWriter.cpp index 452ad19..e92424c 100644 --- a/modules/viz/src/vtk/vtkOBJWriter.cpp +++ b/modules/viz/src/vtk/vtkOBJWriter.cpp @@ -54,7 +54,6 @@ cv::viz::vtkOBJWriter::vtkOBJWriter() std::ofstream fout; // only used to extract the default precision this->DecimalPrecision = fout.precision(); this->FileName = NULL; - this->FileType = VTK_ASCII; } cv::viz::vtkOBJWriter::~vtkOBJWriter(){} @@ -65,9 +64,22 @@ void cv::viz::vtkOBJWriter::WriteData() if (!input) return; - std::ostream *outfilep = this->OpenVTKFile(); - if (!outfilep) + if (!this->FileName ) + { + vtkErrorMacro(<< "No FileName specified! Can't write!"); + this->SetErrorCode(vtkErrorCode::NoFileNameError); + return; + } + + vtkDebugMacro(<<"Opening vtk file for writing..."); + ostream *outfilep = new ofstream(this->FileName, ios::out); + if (outfilep->fail()) + { + vtkErrorMacro(<< "Unable to open file: "<< this->FileName); + this->SetErrorCode(vtkErrorCode::CannotOpenFileError); + delete outfilep; return; + } std::ostream& outfile = *outfilep; @@ -224,7 +236,8 @@ void cv::viz::vtkOBJWriter::WriteData() } } /* if (input->GetNumberOfStrips() > 0) */ - this->CloseVTKFile(outfilep); + vtkDebugMacro(<<"Closing vtk file\n"); + delete outfilep; // Delete the file if an error occurred if (this->ErrorCode == vtkErrorCode::OutOfDiskSpaceError) @@ -239,3 +252,19 @@ void cv::viz::vtkOBJWriter::PrintSelf(ostream& os, vtkIndent indent) Superclass::PrintSelf(os, indent); os << indent << "DecimalPrecision: " << DecimalPrecision << "\n"; } + +int cv::viz::vtkOBJWriter::FillInputPortInformation(int, vtkInformation *info) +{ + info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData"); + return 1; +} + +vtkPolyData* cv::viz::vtkOBJWriter::GetInput() +{ + return vtkPolyData::SafeDownCast(this->Superclass::GetInput()); +} + +vtkPolyData* cv::viz::vtkOBJWriter::GetInput(int port) +{ + return vtkPolyData::SafeDownCast(this->Superclass::GetInput(port)); +} diff --git a/modules/viz/src/vtk/vtkOBJWriter.h b/modules/viz/src/vtk/vtkOBJWriter.h index f888988..7ad0f17 100644 --- a/modules/viz/src/vtk/vtkOBJWriter.h +++ b/modules/viz/src/vtk/vtkOBJWriter.h @@ -45,29 +45,41 @@ #ifndef __vtkOBJWriter_h #define __vtkOBJWriter_h -#include +#include namespace cv { namespace viz { - class vtkOBJWriter : public vtkPolyDataWriter + class vtkOBJWriter : public vtkWriter { public: static vtkOBJWriter *New(); - vtkTypeMacro(vtkOBJWriter,vtkPolyDataWriter) + vtkTypeMacro(vtkOBJWriter,vtkWriter) void PrintSelf(ostream& os, vtkIndent indent); - vtkGetMacro(DecimalPrecision, int); - vtkSetMacro(DecimalPrecision, int); + vtkGetMacro(DecimalPrecision, int) + vtkSetMacro(DecimalPrecision, int) + + // Description: + // Specify file name of data file to write. + vtkSetStringMacro(FileName) + vtkGetStringMacro(FileName) + + // Description: + // Get the input to this writer. + vtkPolyData* GetInput(); + vtkPolyData* GetInput(int port); protected: vtkOBJWriter(); ~vtkOBJWriter(); void WriteData(); + int FillInputPortInformation(int port, vtkInformation *info); int DecimalPrecision; + char *FileName; private: vtkOBJWriter(const vtkOBJWriter&); // Not implemented. diff --git a/modules/viz/src/vtk/vtkXYZReader.cpp b/modules/viz/src/vtk/vtkXYZReader.cpp new file mode 100644 index 0000000..283a592 --- /dev/null +++ b/modules/viz/src/vtk/vtkXYZReader.cpp @@ -0,0 +1,107 @@ +/*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: +// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com +// +//M*/ + +#include "precomp.hpp" + +namespace cv { namespace viz +{ + vtkStandardNewMacro(vtkXYZReader); +}} + + +cv::viz::vtkXYZReader::vtkXYZReader() +{ + this->FileName = 0; + this->SetNumberOfInputPorts(0); +} + +cv::viz::vtkXYZReader::~vtkXYZReader() +{ + this->SetFileName(0); +} + +void cv::viz::vtkXYZReader::PrintSelf(ostream& os, vtkIndent indent) +{ + this->Superclass::PrintSelf(os,indent); + os << indent << "FileName: " << (this->FileName ? this->FileName : "(none)") << "\n"; +} + +int cv::viz::vtkXYZReader::RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector* outputVector) +{ + // Make sure we have a file to read. + if(!this->FileName) + { + vtkErrorMacro("A FileName must be specified."); + return 0; + } + + // Open the input file. + ifstream fin(this->FileName); + if(!fin) + { + vtkErrorMacro("Error opening file " << this->FileName); + return 0; + } + + // Allocate objects to hold points and vertex cells. + vtkSmartPointer points = vtkSmartPointer::New(); + vtkSmartPointer verts = vtkSmartPointer::New(); + + // Read points from the file. + vtkDebugMacro("Reading points from file " << this->FileName); + double x[3]; + while(fin >> x[0] >> x[1] >> x[2]) + { + vtkIdType id = points->InsertNextPoint(x); + verts->InsertNextCell(1, &id); + } + vtkDebugMacro("Read " << points->GetNumberOfPoints() << " points."); + + // Store the points and cells in the output data object. + vtkPolyData* output = vtkPolyData::GetData(outputVector); + output->SetPoints(points); + output->SetVerts(verts); + + return 1; +} diff --git a/modules/viz/src/vtk/vtkXYZReader.h b/modules/viz/src/vtk/vtkXYZReader.h new file mode 100644 index 0000000..13ae048 --- /dev/null +++ b/modules/viz/src/vtk/vtkXYZReader.h @@ -0,0 +1,80 @@ +/*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: +// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com +// +//M*/ + +#ifndef __vtkXYZReader_h +#define __vtkXYZReader_h + +#include "vtkPolyDataAlgorithm.h" + +namespace cv +{ + namespace viz + { + class vtkXYZReader : public vtkPolyDataAlgorithm + { + public: + static vtkXYZReader* New(); + vtkTypeMacro(vtkXYZReader,vtkPolyDataAlgorithm) + void PrintSelf(ostream& os, vtkIndent indent); + + // Description: + // Set/Get the name of the file from which to read points. + vtkSetStringMacro(FileName) + vtkGetStringMacro(FileName) + + protected: + vtkXYZReader(); + ~vtkXYZReader(); + + char* FileName; + + int RequestData(vtkInformation*, vtkInformationVector**, vtkInformationVector*); + private: + vtkXYZReader(const vtkXYZReader&); // Not implemented. + void operator=(const vtkXYZReader&); // Not implemented. + }; + } +} + +#endif diff --git a/modules/viz/src/vtk/vtkXYZWriter.cpp b/modules/viz/src/vtk/vtkXYZWriter.cpp index 4518a01..5a3d7d5 100644 --- a/modules/viz/src/vtk/vtkXYZWriter.cpp +++ b/modules/viz/src/vtk/vtkXYZWriter.cpp @@ -61,10 +61,22 @@ void cv::viz::vtkXYZWriter::WriteData() if (!input) return; - // OpenVTKFile() will report any errors that happen - ostream *outfilep = this->OpenVTKFile(); - if (!outfilep) + if (!this->FileName ) + { + vtkErrorMacro(<< "No FileName specified! Can't write!"); + this->SetErrorCode(vtkErrorCode::NoFileNameError); + return; + } + + vtkDebugMacro(<<"Opening vtk file for writing..."); + ostream *outfilep = new ofstream(this->FileName, ios::out); + if (outfilep->fail()) + { + vtkErrorMacro(<< "Unable to open file: "<< this->FileName); + this->SetErrorCode(vtkErrorCode::CannotOpenFileError); + delete outfilep; return; + } ostream &outfile = *outfilep; @@ -76,7 +88,8 @@ void cv::viz::vtkXYZWriter::WriteData() } // Close the file - this->CloseVTKFile(outfilep); + vtkDebugMacro(<<"Closing vtk file\n"); + delete outfilep; // Delete the file if an error occurred if (this->ErrorCode == vtkErrorCode::OutOfDiskSpaceError) @@ -86,8 +99,24 @@ void cv::viz::vtkXYZWriter::WriteData() } } +int cv::viz::vtkXYZWriter::FillInputPortInformation(int, vtkInformation *info) +{ + info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData"); + return 1; +} + void cv::viz::vtkXYZWriter::PrintSelf(ostream& os, vtkIndent indent) { this->Superclass::PrintSelf(os,indent); os << indent << "DecimalPrecision: " << this->DecimalPrecision << "\n"; } + +vtkPolyData* cv::viz::vtkXYZWriter::GetInput() +{ + return vtkPolyData::SafeDownCast(this->Superclass::GetInput()); +} + +vtkPolyData* cv::viz::vtkXYZWriter::GetInput(int port) +{ + return vtkPolyData::SafeDownCast(this->Superclass::GetInput(port)); +} diff --git a/modules/viz/src/vtk/vtkXYZWriter.h b/modules/viz/src/vtk/vtkXYZWriter.h index 3db18b7..91d0c8f 100644 --- a/modules/viz/src/vtk/vtkXYZWriter.h +++ b/modules/viz/src/vtk/vtkXYZWriter.h @@ -45,29 +45,41 @@ #ifndef __vtkXYZWriter_h #define __vtkXYZWriter_h -#include "vtkPolyDataWriter.h" +#include "vtkWriter.h" namespace cv { namespace viz { - class vtkXYZWriter : public vtkPolyDataWriter + class vtkXYZWriter : public vtkWriter { public: static vtkXYZWriter *New(); - vtkTypeMacro(vtkXYZWriter,vtkPolyDataWriter) + vtkTypeMacro(vtkXYZWriter,vtkWriter) void PrintSelf(ostream& os, vtkIndent indent); vtkGetMacro(DecimalPrecision, int) vtkSetMacro(DecimalPrecision, int) + // Description: + // Specify file name of data file to write. + vtkSetStringMacro(FileName) + vtkGetStringMacro(FileName) + + // Description: + // Get the input to this writer. + vtkPolyData* GetInput(); + vtkPolyData* GetInput(int port); + protected: vtkXYZWriter(); ~vtkXYZWriter(){} void WriteData(); + int FillInputPortInformation(int port, vtkInformation *info); int DecimalPrecision; + char *FileName; private: vtkXYZWriter(const vtkXYZWriter&); // Not implemented. -- 2.7.4