importTet.hpp 6.26 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

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

namespace CGoGN
{
namespace Algo
{
namespace Import 
{

35

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

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

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

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

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

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

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

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

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

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

		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);

91
		//CGoGNout << "VEC3 = " << pos << CGoGNendl;
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

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

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

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

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

109
	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
110 111

	//Read and embed tetrahedra TODO
Pierre Kraemer's avatar
Pierre Kraemer committed
112 113 114 115
	for(unsigned int i = 0; i < m_nbVolumes ; ++i)
	{
		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 121
		std::stringstream oss(ligne);
		oss >> nbe;
122
//		CGoGNout << "tetra number : " << nbe << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
123

Pierre Kraemer's avatar
Pierre Kraemer committed
124 125 126 127 128 129 130
		//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
131

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

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

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

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

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

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

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

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

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

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

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

Pierre Kraemer's avatar
Pierre Kraemer committed
178 179 180
	//Association des phi3
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
181 182
		const std::vector<Dart>& vec = vecDartsPerVertex[d];
		for(typename std::vector<Dart>::const_iterator it = vec.begin(); it!=vec.end(); ++it)
Pierre Kraemer's avatar
Pierre Kraemer committed
183
		{
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
				bool sewn = false;
187
				for(typename std::vector<Dart>::const_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
					{
192
						map.PFP::MAP::TOPO_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