importTet.hpp 6.08 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* 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.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Modelisation/polyhedron.h"
#include <vector>

namespace CGoGN
{
30

Pierre Kraemer's avatar
Pierre Kraemer committed
31 32
namespace Algo
{
33

Pierre Kraemer's avatar
Pierre Kraemer committed
34 35 36 37
namespace Import 
{

template <typename PFP>
38
bool importTet(typename PFP::MAP& map, const std::string& filename, std::vector<std::string>& attrNames, float scaleFactor, bool invertTetra)
Pierre Kraemer's avatar
Pierre Kraemer committed
39 40 41
{
	typedef typename PFP::VEC3 VEC3;

42
	VertexAttribute<VEC3> position = map.template addAttribute<VEC3, VERTEX>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
43 44
	attrNames.push_back(position.name()) ;

45
	AttributeContainer& container = map.template getAttributeContainer<VERTEX>() ;
46

47
	unsigned int m_nbVertices = 0, m_nbVolumes = 0;
48
	VertexAutoAttribute< NoMathIONameAttribute< std::vector<Dart> > > vecDartsPerVertex(map, "incidents");
Pierre Kraemer's avatar
Pierre Kraemer committed
49

50
	//open file
Pierre Kraemer's avatar
Pierre Kraemer committed
51
	std::ifstream fp(filename.c_str(), std::ios::in);
Pierre Kraemer's avatar
Pierre Kraemer committed
52 53
	if (!fp.good())
	{
54
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
55 56 57 58
		return false;
	}

	std::string ligne;
Pierre Kraemer's avatar
Pierre Kraemer committed
59
	unsigned int nbv, nbt;
60
	// reading number of vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
61 62 63 64
	std::getline (fp, ligne);
	std::stringstream oss(ligne);
	oss >> nbv;

65
	// reading number of tetrahedra
Pierre Kraemer's avatar
Pierre Kraemer committed
66 67 68 69
	std::getline (fp, ligne);
	std::stringstream oss2(ligne);
	oss2 >> nbt;

70
	//reading vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
71 72
	std::vector<unsigned int> verticesID;
	verticesID.reserve(nbv);
Pierre Kraemer's avatar
Pierre Kraemer committed
73
	for(unsigned int i = 0; i < nbv;++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
74 75 76 77
	{
		do
		{
			std::getline (fp, ligne);
78
		} while (ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
79 80 81 82 83 84 85

		std::stringstream oss(ligne);

		float x,y,z;
		oss >> x;
		oss >> y;
		oss >> z;
86
		// TODO : if required read other vertices attributes here
Pierre Kraemer's avatar
Pierre Kraemer committed
87 88
		VEC3 pos(x*scaleFactor,y*scaleFactor,z*scaleFactor);

Pierre Kraemer's avatar
Pierre Kraemer committed
89 90
		unsigned int id = container.insertLine();
		position[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
91 92 93 94

		verticesID.push_back(id);
	}

95
	m_nbVertices = nbv;
Pierre Kraemer's avatar
Pierre Kraemer committed
96 97
	m_nbVolumes = nbt;

98
	CGoGNout << "nb points = " << m_nbVertices  << " / nb tet = " << m_nbVolumes << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
99

100 101
	DartMarkerNoUnmark m(map) ;

102
	//Read and embed all tetrahedrons
Pierre Kraemer's avatar
Pierre Kraemer committed
103 104
	for(unsigned int i = 0; i < m_nbVolumes ; ++i)
	{
105 106
		//start one tetra

Pierre Kraemer's avatar
Pierre Kraemer committed
107 108
		int nbe;
		do
Pierre Kraemer's avatar
Pierre Kraemer committed
109
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
110 111
			std::getline(fp,ligne);
		} while(ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
112

Pierre Kraemer's avatar
Pierre Kraemer committed
113
		std::stringstream oss(ligne);
114
		oss >> nbe; //number of vertices = 4 or used for region mark
Pierre Kraemer's avatar
Pierre Kraemer committed
115

116
		Dart d = Algo::Modelisation::createTetrahedron<PFP>(map,false);
117

Pierre Kraemer's avatar
Pierre Kraemer committed
118 119
		Geom::Vec4ui pt;
		oss >> pt[0];
120 121
		oss >> pt[1+invertTetra];
		oss >> pt[2-invertTetra];
Pierre Kraemer's avatar
Pierre Kraemer committed
122
		oss >> pt[3];
Pierre Kraemer's avatar
Pierre Kraemer committed
123

124 125
		//if regions are defined use this number
		oss >> nbe; //ignored here
Pierre Kraemer's avatar
Pierre Kraemer committed
126

127
		// Embed three "base" vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
128
		for(unsigned int j = 0 ; j < 3 ; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
129
		{
130
			FunctorSetEmb<typename PFP::MAP, VERTEX> fsetemb(map, verticesID[pt[2-j]]);
Pierre Kraemer's avatar
Pierre Kraemer committed
131
			map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
Pierre Kraemer's avatar
Pierre Kraemer committed
132

133
			//store darts per vertices to optimize reconstruction
Pierre Kraemer's avatar
Pierre Kraemer committed
134
			Dart dd = d;
Pierre Kraemer's avatar
Pierre Kraemer committed
135 136
			do
			{
137
				m.mark(dd) ;
138
				vecDartsPerVertex[pt[2-j]].push_back(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
139
				dd = map.phi1(map.phi2(dd));
140
			} while(dd != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
141

Pierre Kraemer's avatar
Pierre Kraemer committed
142
			d = map.phi1(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
143 144
		}

145
		//Embed the last "top" vertex
Pierre Kraemer's avatar
Pierre Kraemer committed
146 147
		d = map.phi_1(map.phi2(d));

148
		FunctorSetEmb<typename PFP::MAP, VERTEX> fsetemb(map, verticesID[pt[3]]);
Pierre Kraemer's avatar
Pierre Kraemer committed
149
		map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
150 151

		//store darts per vertices to optimize reconstruction
Pierre Kraemer's avatar
Pierre Kraemer committed
152
		Dart dd = d;
Pierre Kraemer's avatar
Pierre Kraemer committed
153 154
		do
		{
155
			m.mark(dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
156 157 158 159
			vecDartsPerVertex[pt[3]].push_back(dd);
			dd = map.phi1(map.phi2(dd));
		} while(dd != d);

160
		//end of tetra
Pierre Kraemer's avatar
Pierre Kraemer committed
161
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
162

Pierre Kraemer's avatar
Pierre Kraemer committed
163
	//Association des phi3
164
	unsigned int nbBoundaryFaces = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
165 166
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
167
		if (m.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
168
		{
169 170 171 172
			std::vector<Dart>& vec = vecDartsPerVertex[map.phi1(d)];

			Dart good_dart = NIL;
			for(typename std::vector<Dart>::iterator it = vec.begin(); it != vec.end() && good_dart == NIL; ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
173
			{
174 175 176
				if(map.template getEmbedding<VERTEX>(map.phi1(*it)) == map.template getEmbedding<VERTEX>(d) &&
				   map.template getEmbedding<VERTEX>(map.phi_1(*it)) == map.template getEmbedding<VERTEX>(map.phi_1(d)) /*&&
				   map.template getEmbedding<VERTEX>(*it) == map.template getEmbedding<VERTEX>(map.phi1(d)) */)
Pierre Kraemer's avatar
Pierre Kraemer committed
177
				{
178
					good_dart = *it ;
Pierre Kraemer's avatar
Pierre Kraemer committed
179 180
				}
			}
181 182 183 184

			if (good_dart != NIL)
			{
				map.sewVolumes(d, good_dart, false);
185
				m.template unmarkOrbit<FACE>(d);
186 187 188
			}
			else
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
189
				m.unmarkOrbit<PFP::MAP::FACE_OF_PARENT>(d);
190 191
				++nbBoundaryFaces;
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
192
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
193
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
194

195 196 197 198 199 200 201
	if (nbBoundaryFaces > 0)
	{
		std::cout << "closing" << std::endl ;
		map.closeMap();
		CGoGNout << "Map closed (" << nbBoundaryFaces << " boundary faces)" << CGoGNendl;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
202 203 204 205
	fp.close();
	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
206 207 208 209 210
} // namespace Import

} // namespace Algo

} // namespace CGoGN