importTs.hpp 6.09 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
30
31
32
33
34
35
36
37
38
39
40
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Algo/Modelisation/polyhedron.h"

#include <vector>

namespace CGoGN
{
namespace Algo
{
namespace Import 
{

/*
 * TODO Quoi faire des champs scalaires
 *
 */
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
41
bool importTs(typename PFP::MAP& map, const std::string& filename, std::vector<std::string>& attrNames, float scaleFactor)
Pierre Kraemer's avatar
Pierre Kraemer committed
42
43
44
45
{
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

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

49
	AttributeHandler<REAL> scalaire = map.template addAttribute<REAL>(VERTEX, "scalar");
Pierre Kraemer's avatar
Pierre Kraemer committed
50
	attrNames.push_back(scalaire.name()) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
51

52
	AttributeContainer& container = map.getAttributeContainer(VERTEX) ;
53

54
	unsigned int m_nbVertices = 0, m_nbVolumes = 0;
55
	AutoAttributeHandler< NoMathIONameAttribute< std::vector<Dart> > > vecDartsPerVertex(map, VERTEX, "incidents");
Pierre Kraemer's avatar
Pierre Kraemer committed
56
57

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

	std::string ligne;
Pierre Kraemer's avatar
Pierre Kraemer committed
66
	unsigned int nbv, nbt;
67
	// reading number of vertices/tetrahedra
Pierre Kraemer's avatar
Pierre Kraemer committed
68
69
70
71
72
	std::getline (fp, ligne);
	std::stringstream oss(ligne);
	oss >> nbv;
	oss >> 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
81
	{

		do
		{
			std::getline (fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
82
		} while (ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
83
84
85
86
87
88
89
90

		std::stringstream oss(ligne);

		float x,y,z;
		oss >> x;
		oss >> y;
		oss >> z;

91
		VEC3 pos(x*scaleFactor,y*scaleFactor,z*scaleFactor);
Pierre Kraemer's avatar
Pierre Kraemer committed
92

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

		float scal;
		oss >> scal;
		scalaire[id] = scal;

		verticesID.push_back(id);
	}

103
	m_nbVertices = nbv;
Pierre Kraemer's avatar
Pierre Kraemer committed
104
105
	m_nbVolumes = nbt;

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

108
	DartMarkerNoUnmark m(map) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
109

110
	//Read and embed all tetrahedrons
Pierre Kraemer's avatar
Pierre Kraemer committed
111
112
	for(unsigned int i = 0; i < m_nbVolumes ; ++i)
	{
113
114
		//start one tetra

Pierre Kraemer's avatar
Pierre Kraemer committed
115
116
		int nbe;
		do
Pierre Kraemer's avatar
Pierre Kraemer committed
117
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
118
119
			std::getline(fp,ligne);
		} while(ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
120

Pierre Kraemer's avatar
Pierre Kraemer committed
121
		std::stringstream oss(ligne);
122
123
		oss >> nbe; //number of vertices = 4
		assert(nbe == 4);
Pierre Kraemer's avatar
Pierre Kraemer committed
124

125
		Dart d = Algo::Modelisation::createTetrahedron<PFP>(map);
126

Pierre Kraemer's avatar
Pierre Kraemer committed
127
128
129
130
131
		Geom::Vec4ui pt;
		oss >> pt[0];
		oss >> pt[1];
		oss >> pt[2];
		oss >> pt[3];
Pierre Kraemer's avatar
Pierre Kraemer committed
132

133
134
		//if regions are defined use this number
		oss >> nbe; //ignored here
Pierre Kraemer's avatar
Pierre Kraemer committed
135

136
		// Embed three "base" 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
140
			FunctorSetEmb<typename PFP::MAP> fsetemb(map, VERTEX, verticesID[pt[2-j]]);
			map.foreach_dart_of_orbit( PFP::MAP::ORBIT_IN_PARENT(VERTEX), d, fsetemb);
Pierre Kraemer's avatar
Pierre Kraemer committed
141
142


143
			//store darts per vertices to optimize reconstruction
Pierre Kraemer's avatar
Pierre Kraemer committed
144
			Dart dd = d;
Pierre Kraemer's avatar
Pierre Kraemer committed
145
146
			do
			{
147
148
				m.mark(dd) ;
				vecDartsPerVertex[pt[2-j]].push_back(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
149
				dd = map.phi1(map.phi2(dd));
Pierre Kraemer's avatar
Pierre Kraemer committed
150
			} while(dd != d);
Pierre Kraemer's avatar
Pierre Kraemer committed
151

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

155
		//Embed the last "top" vertex
Pierre Kraemer's avatar
Pierre Kraemer committed
156
157
		d = map.phi_1(map.phi2(d));

158
159
160
161
		FunctorSetEmb<typename PFP::MAP> fsetemb(map, VERTEX, verticesID[pt[3]]);
		map.foreach_dart_of_orbit( PFP::MAP::ORBIT_IN_PARENT(VERTEX), d, fsetemb);

		//store darts per vertices to optimize reconstruction
Pierre Kraemer's avatar
Pierre Kraemer committed
162
		Dart dd = d;
Pierre Kraemer's avatar
Pierre Kraemer committed
163
164
		do
		{
165
			m.mark(dd) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
166
167
168
169
			vecDartsPerVertex[pt[3]].push_back(dd);
			dd = map.phi1(map.phi2(dd));
		} while(dd != d);

170
		//end of tetra
Pierre Kraemer's avatar
Pierre Kraemer committed
171
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
172

Pierre Kraemer's avatar
Pierre Kraemer committed
173
	//Association des phi3
174
	unsigned int nbBoundaryFaces = 0 ;
Pierre Kraemer's avatar
Pierre Kraemer committed
175
176
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
177
		if (m.isMarked(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
178
		{
179
180
181
182
			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
183
			{
184
185
186
				if(map.getEmbedding<VERTEX>(map.phi1(*it)) == map.getEmbedding<VERTEX>(d) &&
				   map.getEmbedding<VERTEX>(map.phi_1(*it)) == map.getEmbedding<VERTEX>(map.phi_1(d)) /*&&
				   map.getEmbedding<VERTEX>(*it) == map.getEmbedding<VERTEX>(map.phi1(d)) */)
Pierre Kraemer's avatar
Pierre Kraemer committed
187
				{
188
					good_dart = *it ;
Pierre Kraemer's avatar
Pierre Kraemer committed
189
190
				}
			}
191
192
193
194
195
196
197
198
199
200
201

			if (good_dart != NIL)
			{
				map.sewVolumes(d, good_dart, false);
				m.unmarkOrbit(FACE, d);
			}
			else
			{
				m.unmarkOrbit(PFP::MAP::ORBIT_IN_PARENT(FACE), d);
				++nbBoundaryFaces;
			}
Pierre Kraemer's avatar
Pierre Kraemer committed
202
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
203
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
204

205
206
207
208
209
210
211
212

	if (nbBoundaryFaces > 0)
	{
		std::cout << "closing" << std::endl ;
		map.closeMap();
		CGoGNout << "Map closed (" << nbBoundaryFaces << " boundary faces)" << CGoGNendl;
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
213
214
215
216
	fp.close();
	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
217
218
219
220
221
} // namespace Import

} // namespace Algo

} // namespace CGoGN