importTet.hpp 6.11 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

34
35
36
namespace Volume
{

Pierre Kraemer's avatar
Pierre Kraemer committed
37
38
39
40
namespace Import 
{

template <typename PFP>
41
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
42
43
44
{
	typedef typename PFP::VEC3 VEC3;

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

48
	AttributeContainer& container = map.template getAttributeContainer<VERTEX>() ;
49

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

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

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

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

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

		std::stringstream oss(ligne);

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

Pierre Kraemer's avatar
Pierre Kraemer committed
92
93
		unsigned int id = container.insertLine();
		position[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
94
95
96
97

		verticesID.push_back(id);
	}

98
	m_nbVertices = nbv;
Pierre Kraemer's avatar
Pierre Kraemer committed
99
100
	m_nbVolumes = nbt;

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

103
104
	DartMarkerNoUnmark m(map) ;

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

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

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

119
		Dart d = Surface::Modelisation::createTetrahedron<PFP>(map,false);
120

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

127
128
		//if regions are defined use this number
		oss >> nbe; //ignored here
Pierre Kraemer's avatar
Pierre Kraemer committed
129

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
145
			d = map.phi1(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
146
147
		}

148
		//Embed the last "top" vertex
Pierre Kraemer's avatar
Pierre Kraemer committed
149
150
		d = map.phi_1(map.phi2(d));

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

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

163
		//end of tetra
Pierre Kraemer's avatar
Pierre Kraemer committed
164
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
165

Pierre Kraemer's avatar
Pierre Kraemer committed
166
	//Association des phi3
167
	unsigned int nbBoundaryFaces = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
168
169
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
170
		if (m.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
171
		{
172
173
174
175
			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
176
			{
177
178
179
				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
180
				{
181
					good_dart = *it ;
Pierre Kraemer's avatar
Pierre Kraemer committed
182
183
				}
			}
184
185
186
187

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
205
206
207
208
	fp.close();
	return true;
}

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

211
212
}

Pierre Kraemer's avatar
Pierre Kraemer committed
213
214
215
} // namespace Algo

} // namespace CGoGN