diff --git a/src/meshlabplugins/io_expe/import_xyz.h b/src/meshlabplugins/io_expe/import_xyz.h new file mode 100644 index 000000000..fbc5b57c3 --- /dev/null +++ b/src/meshlabplugins/io_expe/import_xyz.h @@ -0,0 +1,195 @@ +/**************************************************************************** +* VCGLib o o * +* Visual and Computer Graphics Library o o * +* _ O _ * +* Copyright(C) 2004 \/)\/ * +* Visual Computing Lab /\/| * +* ISTI - Italian National Research Council | * +* \ * +* All rights reserved. * +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ + +/* This code has been adapted from the OFF importer and Gael's .pts loader from Expe */ + +#ifndef __VCGLIB_IMPORT_XYZ +#define __VCGLIB_IMPORT_XYZ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace vcg +{ +namespace tri +{ +namespace io +{ + +// /** \addtogroup */ +// /* @{ */ +/** +This class encapsulate a filter for importing Expes's .pts point sets. +*/ +template +class ImporterXYZ +{ + public: + + typedef typename MESH_TYPE::VertexType VertexType; + typedef typename MESH_TYPE::VertexIterator VertexIterator; + typedef typename MESH_TYPE::VertexPointer VertexPointer; + typedef typename MESH_TYPE::CoordType CoordType; + typedef typename MESH_TYPE::ScalarType ScalarType; + + enum ExpeCodes {NoError=0, CantOpen, InvalidFile, UnsupportedVersion}; + + struct Options + { + Options() + : onlyMaskFlag(false) + {} + bool onlyMaskFlag; + }; + + struct FileProperty + { + FileProperty(QByteArray _name, uint _size) + : name(_name), size(_size) + {} + QByteArray name; // name of the property + uint size; // size in byte (binary mode) + bool hasProperty; // true if the target mesh has the property + }; + typedef std::vector FileProperties; + + + /*! + * Standard call for knowing the meaning of an error code + * \param message_code The code returned by Open + * \return The string describing the error code + */ + static const char* ErrorMsg(int message_code) + { + static const char* error_msg[] = + { + "No errors", "Can't open file", "Invalid file", "Unsupported version" + }; + + if(message_code>4 || message_code<0) + return "Unknown error"; + else + return error_msg[message_code]; + }; + + /** + * Load only the properties of the 3D objects. + * + * \param filename the name of the file to read from + * \param loadmask the mask which encodes the properties + * \return the operation result + */ + static bool LoadMask(const char *filename, int &loadmask) + { + // To obtain the loading mask all the file must be parsed + // to distinguish between per-vertex and per-face color attribute. + loadmask=0; + MESH_TYPE dummyMesh; + return (Open(dummyMesh, filename, loadmask,0,true)==NoError); + } + + static int Open(MESH_TYPE &mesh, const char *filename, CallBackPos *cb=0) + { + int loadmask; + return Open(mesh,filename,loadmask,cb); + } + + /*! + * Standard call for reading a mesh. + * + * \param mesh the destination mesh + * \param filename the name of the file to read from + * \return the operation result + */ + static int Open(MESH_TYPE &mesh, const char *filename, int &loadmask, + CallBackPos *cb=0, bool onlyMaskFlag=false) + { + Options opt; + opt.onlyMaskFlag = onlyMaskFlag; + return Open(mesh, filename, loadmask, opt, cb); + } + + static int Open(MESH_TYPE &mesh, const char *filename, int &loadmask, + const Options& options, CallBackPos *cb=0) + { + QFile device(filename); + if ( (!device.open(QFile::ReadOnly)) ) + return CantOpen; + + QTextStream stream(&device); + + loadmask = 0; + + loadmask |= Mask::IOM_VERTCOORD; + loadmask |= Mask::IOM_VERTNORMAL; + + if (options.onlyMaskFlag) + return 0; + + std::vector pos,normals; + CoordType p,n; + QStringList line; + while(!stream.atEnd()) + { + line = stream.readLine(1024).split(" "); + if (line.size()==6) + { + for (int k=0; k<3; ++k) + { + p[k] = line[k].toDouble(); + n[k] = line[3+k].toDouble(); + } + pos.push_back(p); + normals.push_back(n); + } + } + VertexIterator v_iter = Allocator::AddVertices(mesh,pos.size()); + for (int i=0; iP() = pos[i]; + v_iter->N() = normals[i]; + ++v_iter; + } + + return 0; + } // end Open + + protected: + +}; +// /*! @} */ + +} //namespace io +} //namespace tri +} // namespace vcg + +#endif //__VCGLIB_IMPORT_EXPE diff --git a/src/meshlabplugins/io_expe/io_expe.cpp b/src/meshlabplugins/io_expe/io_expe.cpp index abd7d7011..996bd0818 100644 --- a/src/meshlabplugins/io_expe/io_expe.cpp +++ b/src/meshlabplugins/io_expe/io_expe.cpp @@ -32,6 +32,7 @@ #include #include "import_expe.h" +#include "import_xyz.h" // #include "export_expe.h" #include @@ -70,7 +71,7 @@ bool ExpeIOPlugin::open(const QString &formatName, const QString &fileName, Mesh return false; //std::cout << "loadMask = " << loadMask << "\n"; m.Enable(loadMask); - + int result = vcg::tri::io::ImporterExpePTS::Open(m.cm, filename.c_str(), mask, cb); if (result != 0) @@ -79,7 +80,24 @@ bool ExpeIOPlugin::open(const QString &formatName, const QString &fileName, Mesh errorMsgFormat.arg(fileName, vcg::tri::io::ImporterExpePTS::ErrorMsg(result))); return false; } - } + + } + else if (formatName.toLower() == tr("xyz")) + { + int loadMask; + if (!vcg::tri::io::ImporterXYZ::LoadMask(filename.c_str(),loadMask)) + return false; + m.Enable(loadMask); + + + int result = vcg::tri::io::ImporterXYZ::Open(m.cm, filename.c_str(), mask, cb); + if (result != 0) + { + QMessageBox::warning(parent, tr("XYZ Opening Error"), + errorMsgFormat.arg(fileName, vcg::tri::io::ImporterXYZ::ErrorMsg(result))); + return false; + } + } vcg::tri::UpdateBounding::Box(m.cm); // updates bounding box