Commit 5d7f0623 authored by Sylvain Thery's avatar Sylvain Thery
Browse files

add volume exporting (NAS only)

parent 02729b30
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library 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 Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: http://cgogn.unistra.fr/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#ifndef __EXPORT_VOL_H__
#define __EXPORT_VOL_H__
#include "Topology/generic/attributeHandler.h"
#include <stdint.h>
namespace CGoGN
{
namespace Algo
{
namespace Volume
{
namespace Export
{
/**
* export the map into a .nas (nastran file)
* @param the_map map to be exported
* @param position the position container
* @param filename filename of ply file
* @param binary write in binary mode
* @return true
*/
template <typename PFP>
bool exportNAS(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const char* filename) ;
/**
* export the map into a .msh (gmesh file)
* @param the_map map to be exported
* @param position the position container
* @param filename filename of ply file
* @param binary write in binary mode
* @return true
*/
template <typename PFP>
bool exportMSH(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const char* filename) ;
/**
* export the map into a .tet file
* @param the_map map to be exported
* @param position the position container
* @param filename filename of ply file
* @param binary write in binary mode
* @return true
*/
template <typename PFP>
bool exportTet(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const char* filename) ;
/**
* export the map into a .node/.ele file pair
* @param the_map map to be exported
* @param position the position container
* @param filename filename of ply file
* @param binary write in binary mode
* @return true
*/
template <typename PFP>
bool exportNodeEle(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const char* filename) ;
} // namespace Export
}
} // namespace Algo
} // namespace CGoGN
#include "Algo/Export/export.hpp"
#endif
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* version 0.1 *
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg *
* *
* This library is free software; you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library 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 Lesser General Public License *
* for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: http://cgogn.unistra.fr/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/
#include "Topology/generic/attributeHandler.h"
#include "Topology/generic/autoAttributeHandler.h"
#include "Topology/generic/traversorCell.h"
#include "Topology/generic/traversor2.h"
#include "Topology/generic/cellmarker.h"
namespace CGoGN
{
namespace Algo
{
namespace Volume
{
namespace Export
{
template <typename PFP>
bool exportNAS(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const char* filename)
{
typedef typename PFP::MAP MAP;
typedef typename PFP::VEC3 VEC3;
// open file
std::ofstream fout ;
fout.open(filename, std::ios::out) ;
if (!fout.good())
{
CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
return false ;
}
VertexAutoAttribute<unsigned int> indices(map,"indices_vert");
fout << "CEND" << std::endl;;
fout << "BEGIN BULK" << std::endl;
unsigned int count=1;
fout .width(8);
for (unsigned int i = position.begin(); i != position.end(); position.next(i))
{
const VEC3& P = position[i];
fout << "GRID ";
fout << std::right;
fout << count;
fout << " ";
fout << std::left;
fout << P[0] << P[1] << P[2] << std::endl;
indices[i] = count++;
}
std::vector<unsigned int> hexa;
std::vector<unsigned int> tetra;
hexa.reserve(2048);
tetra.reserve(2048);
TraversorW<MAP> trav(map) ;
for(Dart d = trav.begin(); d != trav.end(); d = trav.next())
{
unsigned int degree = 0 ;
Traversor3WV<typename PFP::MAP> twv(map, d) ;
for(Dart it = twv.begin(); it != twv.end(); it = twv.next())
{
degree++;
}
if (degree == 8)
{
//CAS HEXAEDRIQUE (ordre 2 quad superposes, le premier en CCW)
Dart e = d;
Dart f = map.template phi<21121>(d);
hexa.push_back(indices[e]);
e = map.phi1(e);
hexa.push_back(indices[e]);
e = map.phi1(e);
hexa.push_back(indices[e]);
e = map.phi1(e);
hexa.push_back(indices[e]);
hexa.push_back(indices[f]);
e = map.phi_1(f);
hexa.push_back(indices[f]);
e = map.phi_1(f);
hexa.push_back(indices[f]);
e = map.phi_1(f);
hexa.push_back(indices[f]);
}
if (degree == 4)
{
//CAS HEXAEDRIQUE (ordre 2 quad superposes, le premier en CCW)
Dart e = d;
tetra.push_back(indices[e]);
e = map.phi1(e);
tetra.push_back(indices[e]);
e = map.phi1(e);
tetra.push_back(indices[e]);
e = map.template phi<211>(e);
tetra.push_back(indices[e]);
}
}
unsigned int countCell=1;
unsigned int nbhexa = hexa.size()/8;
unsigned int nbtetra = tetra.size()/8;
for (unsigned int i=0; i<nbhexa; ++i)
{
fout << "CHEXA ";
fout << std::right;
fout << countCell++;
fout << hexa[8*i] << hexa[8*i+1] << hexa[8*i+2] << hexa[8*i+3] << hexa[8*i+4] << hexa[8*i+6] << "+";
fout << "+ " << hexa[8*i+6] << hexa[8*i+7] << std::endl;
}
for (unsigned int i=0; i<nbtetra; ++i)
{
fout << "CTETRA ";
fout << std::right;
fout << countCell++;
fout << tetra[4*i] << tetra[4*i+1] << tetra[4*i+2] << tetra[4*i+3] << std::endl;
}
fout << "ENDDATA" << std::endl;
}
template <typename PFP>
bool exportMSH(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const char* filename)
{
typedef typename PFP::MAP MAP;
typedef typename PFP::VEC3 VEC3;
// open file
std::ofstream fout ;
fout.open(filename, std::ios::out) ;
if (!fout.good())
{
CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
return false ;
}
}
template <typename PFP>
bool exportTet(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const char* filename)
{
typedef typename PFP::MAP MAP;
typedef typename PFP::VEC3 VEC3;
// open file
std::ofstream fout ;
fout.open(filename, std::ios::out) ;
if (!fout.good())
{
CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
return false ;
}
}
template <typename PFP>
bool exportNodeEle(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const char* filename)
{
typedef typename PFP::MAP MAP;
typedef typename PFP::VEC3 VEC3;
// open file
std::ofstream fout ;
fout.open(filename, std::ios::out) ;
if (!fout.good())
{
CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
return false ;
}
std::ofstream foutEle ;
foutEle.open(filename, std::ios::out) ;
if (!fout.good())
{
CGoGNerr << "Unable to open file " << filename << CGoGNendl ;
return false ;
}
}
template <typename PFP>
bool exportPLY(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const char* filename, bool binary)
{
typedef typename PFP::MAP MAP;
typedef typename PFP::VEC3 VEC3;
// open file
std::ofstream out ;
if (!binary)
out.open(filename, std::ios::out) ;
else
out.open(filename, std::ios::out | std::ios::binary) ;
if (!out.good())
{
CGoGNerr << "Unable to open file " << CGoGNendl ;
return false ;
}
unsigned int nbDarts = map.getNbDarts() ;
std::vector<unsigned int> facesSize ;
std::vector<std::vector<unsigned int> > facesIdx ;
facesSize.reserve(nbDarts/3) ;
facesIdx.reserve(nbDarts/3) ;
std::map<unsigned int, unsigned int> vIndex ;
unsigned int vCpt = 0 ;
std::vector<unsigned int> vertices ;
vertices.reserve(nbDarts/6) ;
// Go over all faces
CellMarker<VERTEX> markV(map) ;
TraversorF<MAP> t(map) ;
for(Dart d = t.begin(); d != t.end(); d = t.next())
{
std::vector<unsigned int> fidx ;
fidx.reserve(8) ;
unsigned int degree = 0 ;
Traversor2FV<typename PFP::MAP> tfv(map, d) ;
for(Dart it = tfv.begin(); it != tfv.end(); it = tfv.next())
{
++degree ;
unsigned int vNum = map.template getEmbedding<VERTEX>(it) ;
if(!markV.isMarked(it))
{
markV.mark(it) ;
vIndex[vNum] = vCpt++ ;
vertices.push_back(vNum) ;
}
fidx.push_back(vIndex[vNum]) ;
}
facesSize.push_back(degree) ;
facesIdx.push_back(fidx) ;
}
// Start writing the file
out << "ply" << std::endl ;
// ascii or binary
if (!binary)
out << "format ascii 1.0" << std::endl ;
else
{ // test endianness
union
{
uint32_t i ;
char c[4] ;
} bint = {0x01020304} ;
if (bint.c[0] == 1) // big endian
out << "format binary_big_endian 1.0" << std::endl ;
else
out << "format binary_little_endian 1.0" << std::endl ;
}
out << "comment File generated by the CGoGN library" << std::endl ;
out << "comment See : http://cgogn.unistra.fr/" << std::endl ;
out << "comment or contact : cgogn@unistra.fr" << std::endl ;
// Vertex elements
out << "element vertex " << vertices.size() << std::endl ;
// Position property
if (position.isValid())
{
out << "property " << nameOfTypePly(position[0][0]) << " x" << std::endl ;
out << "property " << nameOfTypePly(position[0][1]) << " y" << std::endl ;
out << "property " << nameOfTypePly(position[0][2]) << " z" << std::endl ;
}
// Face element
out << "element face " << facesSize.size() << std::endl ;
out << "property list uint8 uint" << 8 * sizeof(facesIdx[0][0]) << " vertex_indices" << std::endl ;
out << "end_header" << std::endl ;
if (!binary) // ascii
{
// ascii vertices
for(unsigned int i = 0; i < vertices.size(); ++i)
out << position[vertices[i]] << std::endl ;
// ascii faces
for(unsigned int i = 0; i < facesSize.size(); ++i)
{
out << facesSize[i] ;
for(unsigned int j = 0; j < facesIdx[i].size(); ++j)
out << " " << facesIdx[i][j] ;
out << std::endl ;
}
}
else // binary
{
// binary vertices
for(unsigned int i = 0; i < vertices.size(); ++i)
{
Geom::Vec3f v = position[vertices[i]] ;
out.write((char*)(&(v[0])), sizeof(v)) ;
}
// binary faces
for(unsigned int i = 0; i < facesSize.size(); ++i)
{
unsigned char nbe = facesSize[i] ;
out.write((char*)(&nbe), sizeof(unsigned char)) ;
out.write((char*)(&(facesIdx[i][0])), facesSize[i] * sizeof(facesIdx[i][0])) ;
}
}
out.close() ;
return true ;
}
template <typename PFP>
bool exportPLYnew(typename PFP::MAP& map, const std::vector<VertexAttribute<typename PFP::VEC3>*>& attributeHandlers, const char* filename, bool binary)
{
typedef typename PFP::MAP MAP;
typedef typename PFP::VEC3 VEC3;
// open file
std::ofstream out ;
if (!binary)
out.open(filename, std::ios::out) ;
else
out.open(filename, std::ios::out | std::ios::binary) ;
if (!out.good())
{
CGoGNerr << "Unable to open file " << CGoGNendl ;
return false ;
}
unsigned int nbDarts = map.getNbDarts() ;
std::vector<unsigned int> facesSize ;
std::vector<std::vector<unsigned int> > facesIdx ;
facesSize.reserve(nbDarts/3) ;
facesIdx.reserve(nbDarts/3) ;
std::map<unsigned int, unsigned int> vIndex ;
unsigned int vCpt = 0 ;
std::vector<unsigned int> vertices ;
vertices.reserve(nbDarts/6) ;
// Go over all faces
CellMarker<VERTEX> markV(map) ;
TraversorF<MAP> t(map) ;
for(Dart d = t.begin(); d != t.end(); d = t.next())
{
std::vector<unsigned int> fidx ;
fidx.reserve(8) ;
unsigned int degree = 0 ;
Traversor2FV<typename PFP::MAP> tfv(map, d) ;
for(Dart it = tfv.begin(); it != tfv.end(); it = tfv.next())
{
++degree ;
unsigned int vNum = map.template getEmbedding<VERTEX>(it) ;
if(!markV.isMarked(it))
{
markV.mark(it) ;
vIndex[vNum] = vCpt++ ;
vertices.push_back(vNum) ;
}
fidx.push_back(vIndex[vNum]) ;
}
facesSize.push_back(degree) ;
facesIdx.push_back(fidx) ;
}
// Start writing the file
out << "ply" << std::endl ;
// ascii or binary
if (!binary)
out << "format ascii 1.0" << std::endl ;
else
{ // test endianness
union
{
uint32_t i ;
char c[4] ;
} bint = {0x01020304} ;
if (bint.c[0] == 1) // big endian
out << "format binary_big_endian 1.0" << std::endl ;
else
out << "format binary_little_endian 1.0" << std::endl ;
}
out << "comment File generated by the CGoGN library" << std::endl ;
out << "comment See : http://cgogn.unistra.fr/" << std::endl ;
out << "comment or contact : cgogn@unistra.fr" << std::endl ;
// Vertex elements
out << "element vertex " << vertices.size() << std::endl ;
for (typename std::vector<VertexAttribute<typename PFP::VEC3>* >::const_iterator attrHandler = attributeHandlers.begin() ; attrHandler != attributeHandlers.end() ; ++attrHandler)
{
if ((*attrHandler)->isValid() && ((*attrHandler)->getOrbit() == VERTEX) )
{
if ((*attrHandler)->name().compare("position") == 0) // Vertex position property
{
out << "property " << nameOfTypePly((*(*attrHandler))[0][0]) << " x" << std::endl ;
out << "property " << nameOfTypePly((*(*attrHandler))[0][1]) << " y" << std::endl ;
out << "property " << nameOfTypePly((*(*attrHandler))[0][2]) << " z" << std::endl ;
}
else if ((*attrHandler)->name().compare("normal") == 0) // normal property
{
out << "property " << nameOfTypePly((*(*attrHandler))[0][0]) << " nx" << std::endl ;
out << "property " << nameOfTypePly((*(*attrHandler))[0][1]) << " ny" << std::endl ;
out << "property " << nameOfTypePly((*(*attrHandler))[0][2]) << " nz" << std::endl ;
}
else if ((*attrHandler)->name().compare("color") == 0) // vertex color property
{
out << "property " << nameOfTypePly((*(*attrHandler))[0][0]) << " r" << std::endl ;
out << "property " << nameOfTypePly((*(*attrHandler))[0][1]) << " g" << std::endl ;
out << "property " << nameOfTypePly((*(*attrHandler))[0][2]) << " b" << std::endl ;
}
else // other vertex properties
{
out << "property " << nameOfTypePly((*(*attrHandler))[0][0]) << " " << (*attrHandler)->name() << "_0" << std::endl ;
out << "property " << nameOfTypePly((*(*attrHandler))[0][1]) << " " << (*attrHandler)->name() << "_1" << std::endl ;
out << "property " << nameOfTypePly((*(*attrHandler))[0][2]) << " " << (*attrHandler)->name() << "_2" << std::endl ;
}
}
}
// Face element
out << "element face " << facesSize.size() << std::endl ;
out << "property list uint8 " << nameOfTypePly(facesIdx[0][0]) << " vertex_indices" << std::endl ;
out << "end_header" << std::endl ;
if (!binary) // ascii
{
// ascii vertices
for(unsigned int i = 0; i < vertices.size(); ++i)
{
for (typename std::vector<VertexAttribute<typename PFP::VEC3>* >::const_iterator attrHandler = attributeHandlers.begin() ; attrHandler != attributeHandlers.end() ; ++attrHandler)
if ((*attrHandler)->isValid() && (*attrHandler)->getOrbit() == VERTEX)
out << (*(*attrHandler))[vertices[i]] ;
out << std::endl ;
}
// ascii faces
for(unsigned int i = 0; i < facesSize.size(); ++i)
{
out << facesSize[i] ;
for(unsigned int j = 0; j < facesIdx[i].size(); ++j)
out << " " << facesIdx[i][j] ;
out << std::endl ;
}
}
else // binary
{
// binary vertices
for(unsigned int i = 0; i < vertices.size(); ++i)
for (typename std::vector<VertexAttribute<typename PFP::VEC3>*>::const_iterator attrHandler = attributeHandlers.begin() ; attrHandler != attributeHandlers.end() ; ++attrHandler)
if ((*attrHandler)->isValid() && (*attrHandler)->getOrbit() == VERTEX)
{
const typename PFP::VEC3& v = (*(*attrHandler))[vertices[i]] ;
out.write((char*)(&(v[0])), sizeof(v)) ;
}
// binary faces
for(unsigned int i = 0; i < facesSize.size(); ++i)
{
uint8_t nbe = facesSize[i] ;
out.write((char*)(&nbe), sizeof(uint8_t)) ;
out.write((char*)(&(facesIdx[i][0])), facesSize[i] * sizeof(facesIdx[i][0])) ;
}
}
out.close() ;