Coupure prévue mardi 3 Août au matin pour maintenance du serveur. Nous faisons au mieux pour que celle-ci soit la plus brève possible.

importTet.hpp 6.22 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-2011, 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.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

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

namespace CGoGN
{
namespace Algo
{
namespace Import 
{

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

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

43
	AttributeContainer& container = map.getAttributeContainer(VERTEX) ;
44
45

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

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

	std::string ligne;
Pierre Kraemer's avatar
Pierre Kraemer committed
57
	unsigned int nbv, nbt;
Pierre Kraemer's avatar
Pierre Kraemer committed
58
59
60
61
62
	// lecture des nombres de sommets/tetra
	std::getline (fp, ligne);
	std::stringstream oss(ligne);
	oss >> nbv;

63
	//CGoGNout << "nbV = " << nbv << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
64
65
66
67
68

	std::getline (fp, ligne);
	std::stringstream oss2(ligne);
	oss2 >> nbt;

69
	//CGoGNout << "nbT = " << nbt << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
70
71
72
73

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

		std::stringstream oss(ligne);

		float x,y,z;
		oss >> x;
		oss >> y;
		oss >> z;
		// on peut ajouter ici la lecture de couleur si elle existe
		VEC3 pos(x*scaleFactor,y*scaleFactor,z*scaleFactor);

90
		//CGoGNout << "VEC3 = " << pos << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
91

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
98

		verticesID.push_back(id);
	}
	m_nbVertices = verticesID.size();

99
	//CGoGNout << "nbVertices = " << m_nbVertices << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
100
	m_nbVolumes = nbt;
101
	//CGoGNout << "nbVolumes = " << m_nbVolumes << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
102
103
104
105

	// lecture tetra
	// normalement m_nbVolumes*12 (car on ne charge que des tetra)

106
	m_nbFaces = nbt*4;
Pierre Kraemer's avatar
Pierre Kraemer committed
107

108
	CGoGNout << "nb points = " << m_nbVertices << " / nb faces = " << m_nbFaces << " / nb edges = " << m_nbEdges << " / nb tet = " << m_nbVolumes << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
109
110

	//Read and embed tetrahedra TODO
Pierre Kraemer's avatar
Pierre Kraemer committed
111
112
113
114
	for(unsigned int i = 0; i < m_nbVolumes ; ++i)
	{
		int nbe;
		do
Pierre Kraemer's avatar
Pierre Kraemer committed
115
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
116
117
			std::getline(fp,ligne);
		} while(ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
118

Pierre Kraemer's avatar
Pierre Kraemer committed
119
120
		std::stringstream oss(ligne);
		oss >> nbe;
121
//		CGoGNout << "tetra number : " << nbe << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
122

Pierre Kraemer's avatar
Pierre Kraemer committed
123
124
125
126
127
128
129
		//Algo::Modelisation::Polyhedron<PFP>::createOrientedTetra(map);
		Dart d = Algo::Modelisation::Polyhedron<PFP>::createOrientedPolyhedron(map,4);
		Geom::Vec4ui pt;
		oss >> pt[0];
		oss >> pt[1];
		oss >> pt[2];
		oss >> pt[3];
Pierre Kraemer's avatar
Pierre Kraemer committed
130

Pierre Kraemer's avatar
Pierre Kraemer committed
131
132
		//regions ?
		oss >> nbe;
Pierre Kraemer's avatar
Pierre Kraemer committed
133

134
//		CGoGNout << "\t embedding number : " << pt[0] << " " << pt[1] << " " << pt[2] << " " << pt[3] << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
135

Pierre Kraemer's avatar
Pierre Kraemer committed
136
		// Embed three vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
137
		for(unsigned int j = 0 ; j < 3 ; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
138
		{
139
//			CGoGNout << "\t embedding number : " << pt[j];
Pierre Kraemer's avatar
Pierre Kraemer committed
140

141
			FunctorSetEmb<typename PFP::MAP> femb(map, VERTEX, verticesID[pt[j]]);
Pierre Kraemer's avatar
Pierre Kraemer committed
142
143

			Dart dd = d;
Pierre Kraemer's avatar
Pierre Kraemer committed
144
145
			do
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
146
				femb(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
147
148
149
				//vecDartPtrEmb[pt[j]].push_back(dd);
				vecDartsPerVertex[pt[j]].push_back(dd);
				dd = map.phi1(map.phi2(dd));
150
			} while(dd != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
151

Pierre Kraemer's avatar
Pierre Kraemer committed
152
153
			d = map.phi1(d);

154
//			CGoGNout << " done" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
155
156
		}

Pierre Kraemer's avatar
Pierre Kraemer committed
157
		//Embed the last vertex
158
//		CGoGNout << "\t embedding number : " << pt[3] << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
159
160
		d = map.phi_1(map.phi2(d));

161
		FunctorSetEmb<typename PFP::MAP> femb(map, VERTEX, verticesID[pt[3]]);
Pierre Kraemer's avatar
Pierre Kraemer committed
162
		Dart dd = d;
Pierre Kraemer's avatar
Pierre Kraemer committed
163
164
		do
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
165
			femb(dd);
166
//			CGoGNout << "embed" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
167
168
169
170
171
			//vecDartPtrEmb[pt[3]].push_back(dd);
			vecDartsPerVertex[pt[3]].push_back(dd);
			dd = map.phi1(map.phi2(dd));
		} while(dd != d);

172
//		CGoGNout << "end tetra" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
173
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
174

175
//	CGoGNout << "end 1/2" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
176

Pierre Kraemer's avatar
Pierre Kraemer committed
177
178
179
180
181
182
183
	//Association des phi3
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
		std::vector<Dart>& vec = vecDartsPerVertex[d];

		for(typename std::vector<Dart>::iterator it = vec.begin(); it!=vec.end(); ++it)
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
184
			if(map.phi3(*it) == *it)
Pierre Kraemer's avatar
Pierre Kraemer committed
185
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
186
187
				bool sewn = false;
				for(typename std::vector<Dart>::iterator itnext = it+1; itnext != vec.end() && !sewn; ++itnext)
Pierre Kraemer's avatar
Pierre Kraemer committed
188
				{
189
190
					if(map.getEmbedding(VERTEX,map.phi1(*it))==map.getEmbedding(VERTEX,map.phi_1(*itnext))
					&& map.getEmbedding(VERTEX,map.phi_1(*it))==map.getEmbedding(VERTEX,map.phi1(*itnext)))
Pierre Kraemer's avatar
Pierre Kraemer committed
191
					{
Pierre Kraemer's avatar
Pierre Kraemer committed
192
						map.sewVolumes(*it, map.phi_1(*itnext));
Pierre Kraemer's avatar
Pierre Kraemer committed
193
						sewn = true;
Pierre Kraemer's avatar
Pierre Kraemer committed
194
195
196
197
					}
				}
			}
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
198
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
199
200
201
202
203

	fp.close();
	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
204
205
206
207
208
} // namespace Import

} // namespace Algo

} // namespace CGoGN