importTet.hpp 5.86 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_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;
59
	// reading number of vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
60 61 62 63
	std::getline (fp, ligne);
	std::stringstream oss(ligne);
	oss >> nbv;

64
	// reading number of tetrahedra
Pierre Kraemer's avatar
Pierre Kraemer committed
65 66 67 68
	std::getline (fp, ligne);
	std::stringstream oss2(ligne);
	oss2 >> nbt;

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

		std::stringstream oss(ligne);

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

88
//		CGoGNout << "VEC3 = " << pos << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
89

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

		verticesID.push_back(id);
	}
95
	m_nbVertices = nbv;
Pierre Kraemer's avatar
Pierre Kraemer committed
96 97 98

	m_nbVolumes = nbt;

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

101
	//Read and embed all tetrahedrons
Pierre Kraemer's avatar
Pierre Kraemer committed
102 103
	for(unsigned int i = 0; i < m_nbVolumes ; ++i)
	{
104 105
		//start one tetra

Pierre Kraemer's avatar
Pierre Kraemer committed
106 107
		int nbe;
		do
Pierre Kraemer's avatar
Pierre Kraemer committed
108
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
109 110
			std::getline(fp,ligne);
		} while(ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
111

Pierre Kraemer's avatar
Pierre Kraemer committed
112
		std::stringstream oss(ligne);
113 114
		oss >> nbe; //number of vertices =4
		assert(nbe == 4);
Pierre Kraemer's avatar
Pierre Kraemer committed
115

116
		Dart d = Algo::Modelisation::Polyhedron<PFP>::createPolyhedron(map,4);
Pierre Kraemer's avatar
Pierre Kraemer committed
117 118 119 120 121
		Geom::Vec4ui pt;
		oss >> pt[0];
		oss >> pt[1];
		oss >> pt[2];
		oss >> pt[3];
Pierre Kraemer's avatar
Pierre Kraemer committed
122

123 124
		//if regions are defined use this number
		oss >> nbe; //ignored here
Pierre Kraemer's avatar
Pierre Kraemer committed
125

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

128
		// Embed three "base" vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
129
		for(unsigned int j = 0 ; j < 3 ; ++j)
Pierre Kraemer's avatar
Pierre Kraemer committed
130
		{
131
			FunctorSetEmb<typename PFP::MAP> femb(map, VERTEX, verticesID[pt[j]]);
132
			map.foreach_dart_of_vertex(d,femb); //apply foreach for gmaps : cannot be included in the following loop
Pierre Kraemer's avatar
Pierre Kraemer committed
133

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

Pierre Kraemer's avatar
Pierre Kraemer committed
142
			d = map.phi1(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
143 144
		}

145
		//Embed the last "top" vertex
Pierre Kraemer's avatar
Pierre Kraemer committed
146 147
		d = map.phi_1(map.phi2(d));

148
		FunctorSetEmb<typename PFP::MAP> femb(map, VERTEX, verticesID[pt[3]]);
149 150 151
		map.foreach_dart_of_vertex(d,femb); //apply foreach for gmaps : cannot be included in the following loop

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

159
		//end of tetra
Pierre Kraemer's avatar
Pierre Kraemer committed
160
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
161

162
//	CGoGNout << "end 1/2" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
163

Pierre Kraemer's avatar
Pierre Kraemer committed
164 165 166
	//Association des phi3
	for (Dart d = map.begin(); d != map.end(); map.next(d))
	{
167 168
		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
169
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
170
			if(map.phi3(*it) == *it)
Pierre Kraemer's avatar
Pierre Kraemer committed
171
			{
Pierre Kraemer's avatar
Pierre Kraemer committed
172
				bool sewn = false;
173
				for(typename std::vector<Dart>::const_iterator itnext = it+1; itnext != vec.end() && !sewn; ++itnext)
Pierre Kraemer's avatar
Pierre Kraemer committed
174
				{
175 176
					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
177
					{
178
						map.PFP::MAP::TOPO_MAP::sewVolumes(*it, map.phi_1(*itnext));
Pierre Kraemer's avatar
Pierre Kraemer committed
179
						sewn = true;
Pierre Kraemer's avatar
Pierre Kraemer committed
180 181 182 183
					}
				}
			}
		}
Pierre Kraemer's avatar
Pierre Kraemer committed
184
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
185 186 187 188 189

	fp.close();
	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
190 191 192 193 194
} // namespace Import

} // namespace Algo

} // namespace CGoGN