add .xyz importer (ascii, point + normal)

This commit is contained in:
Gael Guennebaud guennebaud 2009-03-02 14:09:49 +00:00
parent 7a28176652
commit ef059de75f
2 changed files with 215 additions and 2 deletions

View File

@ -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 <fstream>
#include <string>
#include <vector>
#include <cassert>
#include <QString>
#include <QFile>
#include <QTextStream>
#include <vcg/space/color4.h>
#include<vcg/complex/trimesh/allocate.h>
#include <wrap/callback.h>
#include <wrap/io_trimesh/io_mask.h>
namespace vcg
{
namespace tri
{
namespace io
{
// /** \addtogroup */
// /* @{ */
/**
This class encapsulate a filter for importing Expes's .pts point sets.
*/
template<class MESH_TYPE>
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<FileProperty> FileProperties;
/*!
* Standard call for knowing the meaning of an error code
* \param message_code The code returned by <CODE>Open</CODE>
* \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<CoordType> 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<MESH_TYPE>::AddVertices(mesh,pos.size());
for (int i=0; i<pos.size(); ++i)
{
v_iter->P() = pos[i];
v_iter->N() = normals[i];
++v_iter;
}
return 0;
} // end Open
protected:
};
// /*! @} */
} //namespace io
} //namespace tri
} // namespace vcg
#endif //__VCGLIB_IMPORT_EXPE

View File

@ -32,6 +32,7 @@
#include <vcg/complex/trimesh/update/bounding.h>
#include "import_expe.h"
#include "import_xyz.h"
// #include "export_expe.h"
#include <QMessageBox>
@ -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<CMeshO>::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<CMeshO>::ErrorMsg(result)));
return false;
}
}
}
else if (formatName.toLower() == tr("xyz"))
{
int loadMask;
if (!vcg::tri::io::ImporterXYZ<CMeshO>::LoadMask(filename.c_str(),loadMask))
return false;
m.Enable(loadMask);
int result = vcg::tri::io::ImporterXYZ<CMeshO>::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<CMeshO>::ErrorMsg(result)));
return false;
}
}
vcg::tri::UpdateBounding<CMeshO>::Box(m.cm); // updates bounding box