vcglib/apps/nexus/ply2crude.cpp

168 lines
5.4 KiB
C++
Raw Normal View History

2004-06-23 02:10:38 +02:00
/****************************************************************************
* 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. *
* *
****************************************************************************/
/****************************************************************************
History
$Log: not supported by cvs2svn $
Revision 1.3 2004/07/15 14:32:49 ponchio
Debug.
2004-07-15 16:32:49 +02:00
Revision 1.2 2004/07/05 15:49:39 ponchio
Windows (DevCpp, mingw) port.
2004-07-05 17:49:39 +02:00
Revision 1.1 2004/07/04 15:30:00 ponchio
Changed directory structure.
2004-07-04 17:30:22 +02:00
Revision 1.2 2004/07/02 13:09:57 ponchio
*** empty log message ***
2004-07-02 15:09:57 +02:00
Revision 1.1 2004/06/23 00:08:05 ponchio
Created
2004-06-23 02:10:38 +02:00
****************************************************************************/
///TODO: allow other kinds of ply to be readed.
#include <string>
#include <iostream>
#include <wrap/ply/plylib.h>
2004-07-05 17:49:39 +02:00
#include "crude.h"
2004-07-02 15:09:57 +02:00
2004-06-23 02:10:38 +02:00
using namespace std;
using namespace vcg;
using namespace vcg::ply;
using namespace nxs;
struct PlyVertex {
float v[3];
};
struct PlyFace {
unsigned int f[3];
unsigned char flags;
};
PropDescriptor plyprop1[3]= {
{"vertex","x",T_FLOAT,T_FLOAT,offsetof(PlyVertex,v[0]),0,0,0,0,0},
{"vertex","y",T_FLOAT,T_FLOAT,offsetof(PlyVertex,v[1]),0,0,0,0,0},
{"vertex","z",T_FLOAT,T_FLOAT,offsetof(PlyVertex,v[2]),0,0,0,0,0}
};
PropDescriptor plyprop2[1]= {
{"face", "vertex_indices",T_INT,T_UINT,offsetof(PlyFace,f[0]),
1,0,T_UCHAR,T_UCHAR,offsetof(PlyFace,flags) }
};
2004-07-05 17:49:39 +02:00
int main(int argc, char *argv[]) {
2004-06-23 02:10:38 +02:00
if(argc <= 2) {
cerr << "Usage: " << argv[0] << " <input1.ply> <...> <inputN.ply> <output>\n";
return 0;
}
string output = argv[argc-1];
//test last one is not a ply
if(output.size() > 4 &&
output.substr(output.size()-4, output.size()) == ".ply") {
2004-06-23 02:10:38 +02:00
cerr << "Last argument is output (so not a .ply)\n";
return -1;
}
Crude crude;
if(!crude.Create(output, 0, 0)) {
cerr << "Could not create crude output\n";
return -1;
}
Box3f box;
box.SetNull();
for(int k = 1; k < argc-1; k++) {
PlyFile pf;
//Opening ply file
int val = pf.Open(argv[k], PlyFile::MODE_READ);
if(val == -1) {
cerr << "Could not open file '" << argv[k] << "'\n";
return false;
}
//testing for required vertex fields.
if( pf.AddToRead(plyprop1[0])==-1 ||
pf.AddToRead(plyprop1[1])==-1 ||
pf.AddToRead(plyprop1[2])==-1) {
cerr << "Error Ply file has not one of the required elements :"
<< "xyz coords\n";
return false;
}
//testing for required face fields.
if( pf.AddToRead(plyprop2[0])==-1 ) {
cerr << "Error Ply file has not one of the required elements:"
<< "faces\n";
return false;
}
2004-07-15 16:32:49 +02:00
unsigned int vertex_offset = crude.Vertices();
2004-06-23 02:10:38 +02:00
for(unsigned int i = 0; i < pf.elements.size(); i++) {
if(!strcmp( pf.ElemName(i),"vertex")) {
unsigned int n_vertices = pf.ElemNumber(i);
unsigned int offset = crude.Vertices();
crude.Resize(offset + n_vertices, crude.Faces());
cerr << "Adding " << n_vertices << " n_vertices" << endl;
pf.SetCurElement(i);
PlyVertex vertex;
Point3f p;
for(unsigned v = offset; v < offset + n_vertices; v++) {
pf.Read((void *) &vertex);
p[0] = vertex.v[0];
p[1] = vertex.v[1];
p[2] = vertex.v[2];
box.Add(p);
crude.SetVertex(v, vertex.v);
}
} else if( !strcmp( pf.ElemName(i),"face") ) {
unsigned int n_faces = pf.ElemNumber(i);
unsigned int offset = crude.Faces();
crude.Resize(crude.Vertices(), offset + n_faces);
cerr << "Adding " << n_faces << " n_faces" << endl;
pf.SetCurElement(i);
PlyFace face;
for(unsigned v = offset; v < offset + n_faces; v++) {
pf.Read((void *) &face);
2004-07-15 16:32:49 +02:00
face.f[0] += vertex_offset;
face.f[1] += vertex_offset;
face.f[2] += vertex_offset;
2004-06-23 02:10:38 +02:00
assert(face.f[0] < crude.Vertices() &&
face.f[1] < crude.Vertices() &&
face.f[2] < crude.Vertices());
crude.SetFace(v, face.f);
}
}
}
pf.Destroy();
}
crude.GetBox() = box;
return 0;
}