Path: blob/master/modules/viz/src/vtk/vtkOBJWriter.cpp
16358 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2013, OpenCV Foundation, all rights reserved.13// Third party copyrights are property of their respective owners.14//15// Redistribution and use in source and binary forms, with or without modification,16// are permitted provided that the following conditions are met:17//18// * Redistribution's of source code must retain the above copyright notice,19// this list of conditions and the following disclaimer.20//21// * Redistribution's in binary form must reproduce the above copyright notice,22// this list of conditions and the following disclaimer in the documentation23// and/or other materials provided with the distribution.24//25// * The name of the copyright holders may not be used to endorse or promote products26// derived from this software without specific prior written permission.27//28// This software is provided by the copyright holders and contributors "as is" and29// any express or implied warranties, including, but not limited to, the implied30// warranties of merchantability and fitness for a particular purpose are disclaimed.31// In no event shall the Intel Corporation or contributors be liable for any direct,32// indirect, incidental, special, exemplary, or consequential damages33// (including, but not limited to, procurement of substitute goods or services;34// loss of use, data, or profits; or business interruption) however caused35// and on any theory of liability, whether in contract, strict liability,36// or tort (including negligence or otherwise) arising in any way out of37// the use of this software, even if advised of the possibility of such damage.38//39// Authors:40// * Anatoly Baksheev, Itseez Inc. myname.mysurname <> mycompany.com41//42//M*/4344#include "../precomp.hpp"4546namespace cv { namespace viz47{48vtkStandardNewMacro(vtkOBJWriter);49}}5051cv::viz::vtkOBJWriter::vtkOBJWriter()52{53std::ofstream fout; // only used to extract the default precision54this->DecimalPrecision = fout.precision();55this->FileName = NULL;56}5758cv::viz::vtkOBJWriter::~vtkOBJWriter(){}5960void cv::viz::vtkOBJWriter::WriteData()61{62vtkPolyData *input = this->GetInput();63if (!input)64return;6566if (!this->FileName )67{68vtkErrorMacro(<< "No FileName specified! Can't write!");69this->SetErrorCode(vtkErrorCode::NoFileNameError);70return;71}7273vtkDebugMacro(<<"Opening vtk file for writing...");74ostream *outfilep = new ofstream(this->FileName, ios::out);75if (outfilep->fail())76{77vtkErrorMacro(<< "Unable to open file: "<< this->FileName);78this->SetErrorCode(vtkErrorCode::CannotOpenFileError);79delete outfilep;80return;81}8283std::ostream& outfile = *outfilep;8485//write header86outfile << "# wavefront obj file written by opencv viz module" << std::endl << std::endl;87outfile << "mtllib NONE" << std::endl << std::endl;8889// write out the points90for (int i = 0; i < input->GetNumberOfPoints(); i++)91{92Vec3d p;93input->GetPoint(i, p.val);94outfile << std::setprecision(this->DecimalPrecision) << "v " << p[0] << " " << p[1] << " " << p[2] << std::endl;95}9697const int idStart = 1;9899// write out the point data100vtkSmartPointer<vtkDataArray> normals = input->GetPointData()->GetNormals();101if(normals)102{103for (int i = 0; i < normals->GetNumberOfTuples(); i++)104{105Vec3d p;106normals->GetTuple(i, p.val);107outfile << std::setprecision(this->DecimalPrecision) << "vn " << p[0] << " " << p[1] << " " << p[2] << std::endl;108}109}110111vtkSmartPointer<vtkDataArray> tcoords = input->GetPointData()->GetTCoords();112if (tcoords)113{114for (int i = 0; i < tcoords->GetNumberOfTuples(); i++)115{116Vec2d p;117tcoords->GetTuple(i, p.val);118outfile << std::setprecision(this->DecimalPrecision) << "vt " << p[0] << " " << p[1] << std::endl;119}120}121122// write out a group name and material123outfile << std::endl << "g grp" << idStart << std::endl;124outfile << "usemtl mtlNONE" << std::endl;125126// write out verts if any127if (input->GetNumberOfVerts() > 0)128{129vtkIdType npts = 0, *index = 0;130vtkCellArray *cells = input->GetVerts();131for (cells->InitTraversal(); cells->GetNextCell(npts, index); )132{133outfile << "p ";134for (int i = 0; i < npts; i++)135outfile << index[i] + idStart << " ";136outfile << std::endl;137}138}139140// write out lines if any141if (input->GetNumberOfLines() > 0)142{143vtkIdType npts = 0, *index = 0;144vtkCellArray *cells = input->GetLines();145for (cells->InitTraversal(); cells->GetNextCell(npts, index); )146{147outfile << "l ";148if (tcoords)149{150for (int i = 0; i < npts; i++)151outfile << index[i] + idStart << "/" << index[i] + idStart << " ";152}153else154for (int i = 0; i < npts; i++)155outfile << index[i] + idStart << " ";156157outfile << std::endl;158}159}160161// write out polys if any162if (input->GetNumberOfPolys() > 0)163{164vtkIdType npts = 0, *index = 0;165vtkCellArray *cells = input->GetPolys();166for (cells->InitTraversal(); cells->GetNextCell(npts, index); )167{168outfile << "f ";169for (int i = 0; i < npts; i++)170{171if (normals)172{173if (tcoords)174outfile << index[i] + idStart << "/" << index[i] + idStart << "/" << index[i] + idStart << " ";175else176outfile << index[i] + idStart << "//" << index[i] + idStart << " ";177}178else179{180if (tcoords)181outfile << index[i] + idStart << " " << index[i] + idStart << " ";182else183outfile << index[i] + idStart << " ";184}185}186outfile << std::endl;187}188}189190// write out tstrips if any191if (input->GetNumberOfStrips() > 0)192{193vtkIdType npts = 0, *index = 0;194vtkCellArray *cells = input->GetStrips();195for (cells->InitTraversal(); cells->GetNextCell(npts, index); )196{197for (int i = 2, i1, i2; i < npts; ++i)198{199if (i % 2)200{201i1 = i - 1;202i2 = i - 2;203}204else205{206i1 = i - 1;207i2 = i - 2;208}209210if(normals)211{212if (tcoords)213{214outfile << "f " << index[i1] + idStart << "/" << index[i1] + idStart << "/" << index[i1] + idStart << " "215<< index[i2]+ idStart << "/" << index[i2] + idStart << "/" << index[i2] + idStart << " "216<< index[i] + idStart << "/" << index[i] + idStart << "/" << index[i] + idStart << std::endl;217}218else219{220outfile << "f " << index[i1] + idStart << "//" << index[i1] + idStart << " " << index[i2] + idStart221<< "//" << index[i2] + idStart << " " << index[i] + idStart << "//" << index[i] + idStart << std::endl;222}223}224else225{226if (tcoords)227{228outfile << "f " << index[i1] + idStart << "/" << index[i1] + idStart << " " << index[i2] + idStart229<< "/" << index[i2] + idStart << " " << index[i] + idStart << "/" << index[i] + idStart << std::endl;230}231else232outfile << "f " << index[i1] + idStart << " " << index[i2] + idStart << " " << index[i] + idStart << std::endl;233}234} /* for (int i = 2; i < npts; ++i) */235}236} /* if (input->GetNumberOfStrips() > 0) */237238vtkDebugMacro(<<"Closing vtk file\n");239delete outfilep;240241// Delete the file if an error occurred242if (this->ErrorCode == vtkErrorCode::OutOfDiskSpaceError)243{244vtkErrorMacro("Ran out of disk space; deleting file: " << this->FileName);245unlink(this->FileName);246}247}248249void cv::viz::vtkOBJWriter::PrintSelf(ostream& os, vtkIndent indent)250{251Superclass::PrintSelf(os, indent);252os << indent << "DecimalPrecision: " << DecimalPrecision << "\n";253}254255int cv::viz::vtkOBJWriter::FillInputPortInformation(int, vtkInformation *info)256{257info->Set(vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkPolyData");258return 1;259}260261vtkPolyData* cv::viz::vtkOBJWriter::GetInput()262{263return vtkPolyData::SafeDownCast(this->Superclass::GetInput());264}265266vtkPolyData* cv::viz::vtkOBJWriter::GetInput(int port)267{268return vtkPolyData::SafeDownCast(this->Superclass::GetInput(port));269}270271272