importTs.hpp 6.15 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
	VertexAttribute<VEC3> position = map.template addAttribute<VEC3, VERTEX>("position") ;
Pierre Kraemer's avatar
Pierre Kraemer committed
47
	attrNames.push_back(position.name()) ;
48

49
	VertexAttribute<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.template getAttributeContainer<VERTEX>() ;
53

54
	unsigned int m_nbVertices = 0, m_nbVolumes = 0;
55
	VertexAutoAttribute< NoMathIONameAttribute< std::vector<Dart> > > vecDartsPerVertex(map, "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
	{
		do
		{
			std::getline (fp, ligne);
Pierre Kraemer's avatar
Pierre Kraemer committed
81
		} while (ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
82 83 84 85 86 87 88 89

		std::stringstream oss(ligne);

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

90
		VEC3 pos(x*scaleFactor,y*scaleFactor,z*scaleFactor);
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 99 100 101

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

		verticesID.push_back(id);
	}

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

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

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

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

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

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

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

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

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

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

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

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

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

156
		FunctorSetEmb<typename PFP::MAP, VERTEX> fsetemb(map, verticesID[pt[3]]);
Pierre Kraemer's avatar
Pierre Kraemer committed
157
		map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
158 159

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

168
		//end of tetra
Pierre Kraemer's avatar
Pierre Kraemer committed
169
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
170

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

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

203 204 205 206 207 208 209 210

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
215 216 217 218 219
} // namespace Import

} // namespace Algo

} // namespace CGoGN