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.

importObjEle.hpp 6.59 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* 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.           *
*                                                                              *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

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

namespace CGoGN
{
Pierre Kraemer's avatar
Pierre Kraemer committed
30

Pierre Kraemer's avatar
Pierre Kraemer committed
31
32
namespace Algo
{
Pierre Kraemer's avatar
Pierre Kraemer committed
33
34

namespace Import
Pierre Kraemer's avatar
Pierre Kraemer committed
35
36
37
{

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
38
bool importOFFWithELERegions(typename PFP::MAP& map, char* filenameOFF, char * filenameELE, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
39
40
41
{
	typedef typename PFP::VEC3 VEC3;

Pierre Kraemer's avatar
Pierre Kraemer committed
42
43
44
45
	AttribContainer& container = map.getAttributeContainer(VERTEX_CELL) ;
	AttributeHandler<VEC3> position = map.template addAttribute<VEC3>(VERTEX_ORBIT, "position") ;
	attrNames.push_back(position.name()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
46
47
	unsigned int m_nbVertices=0, m_nbFaces=0, m_nbEdges=0, m_nbVolumes=0;

Pierre Kraemer's avatar
Pierre Kraemer committed
48
	AutoAttributeHandler<  NoMathIONameAttribute< std::vector<Dart> > > vecDartsPerVertex(map, VERTEX_ORBIT, "incidents");
Pierre Kraemer's avatar
Pierre Kraemer committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

	// open files
	std::ifstream foff(filenameOFF, std::ios::in);
	if (!foff.good())
	{
		std::cerr << "Unable to open OFF file " << std::endl;
		return false;
	}

	std::ifstream fele(filenameELE, std::ios::in);
	if (!fele.good())
	{
		std::cerr << "Unable to open ELE file " << std::endl;
		return false;
	}

	std::string line;

	//OFF reading
	std::getline(foff,line);
	if(line.rfind("OFF") == std::string::npos)
	{
		std::cerr << "Problem reading off file: not an off file"<<std::endl;
		std::cerr << line << std::endl;
		return false;
	}

	//Reading number of vertex/faces/edges in OFF file
	int nbe;
	{
		do
		{
			std::getline(foff,line);
		}while(line.size() == 0);

		std::stringstream oss(line);
		oss >> m_nbVertices;
		oss >> m_nbFaces;
		oss >> m_nbEdges;
		oss >> nbe;
	}

	//Reading number of tetrahedra in ELE file
	int nbv;
	{
		do
		{
			std::getline(fele,line);
		}while(line.size() == 0);

		std::stringstream oss(line);
		oss >> m_nbVolumes;
		oss >> nbv ; oss >> nbv;
	}

	std::cout << "nb points = " << m_nbVertices << " / nb faces = " << m_nbFaces << " / nb edges = " << m_nbEdges << " / nb tet = " << m_nbVolumes << std::endl;

	//Reading vertices
	std::vector<unsigned int> verticesID;
	verticesID.reserve(m_nbVertices);

Pierre Kraemer's avatar
Pierre Kraemer committed
110
	for(unsigned int i = 0 ; i < m_nbVertices ; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
	{
		do
		{
			std::getline(foff,line);
		}while(line.size() == 0);

		std::stringstream oss(line);

		float x,y,z;
		oss >> x;
		oss >> y;
		oss >> z;
		//we can read colors informations if exists
		VEC3 pos(x,y,z);

Pierre Kraemer's avatar
Pierre Kraemer committed
126
127
		unsigned int id = container.insertLine();
		position[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

//		std::cout << "emb : " << pos << " / id = " << id << std::endl;
		verticesID.push_back(id);
	}

	std::vector<std::vector<Dart> > vecDartPtrEmb;
	vecDartPtrEmb.reserve(m_nbVertices);

	//Read and embed tetrahedra TODO
	for(unsigned i=0; i < m_nbVolumes ; ++i)
	{
		do
		{
			std::getline(fele,line);
		} while(line.size() == 0);

		std::stringstream oss(line);
		oss >> nbe;
//		std::cout << "tetra number : " << nbe << std::endl;

Pierre Kraemer's avatar
Pierre Kraemer committed
148
149
		//Algo::Modelisation::Polyhedron<PFP>::createOrientedTetra(map);
		Dart d = Algo::Modelisation::Polyhedron<PFP>::createOrientedPolyhedron(map,4);
Pierre Kraemer's avatar
Pierre Kraemer committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
		Geom::Vec4ui pt;
		oss >> pt[0];
		oss >> pt[1];
		oss >> pt[2];
		oss >> pt[3];

		//regions ?
		oss >> nbe;

//		std::cout << "\t embedding number : " << pt[0] << " " << pt[1] << " " << pt[2] << " " << pt[3] << std::endl;

		// Embed three vertices
		for(unsigned int j=0 ; j<3 ; ++j)
		{
//			std::cout << "\t embedding number : " << pt[j];

Pierre Kraemer's avatar
Pierre Kraemer committed
166
			FunctorSetEmb<typename PFP::MAP> femb(map,VERTEX_ORBIT,verticesID[pt[j]]);
Pierre Kraemer's avatar
Pierre Kraemer committed
167
168
169
170
171
172

			Dart dd = d;
			do {
				femb(dd);
				//vecDartPtrEmb[pt[j]].push_back(dd);
				vecDartsPerVertex[pt[j]].push_back(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
173
				dd = map.phi1(map.phi2(dd));
Pierre Kraemer's avatar
Pierre Kraemer committed
174
175
			} while(dd!=d);

Pierre Kraemer's avatar
Pierre Kraemer committed
176
			d = map.phi1(d);
Pierre Kraemer's avatar
Pierre Kraemer committed
177
178
179
180
181
182

//			std::cout << " done" << std::endl;
		}

		//Embed the last vertex
//		std::cout << "\t embedding number : " << pt[3] << std::endl;
Pierre Kraemer's avatar
Pierre Kraemer committed
183
		d = map.phi_1(map.phi2(d));
Pierre Kraemer's avatar
Pierre Kraemer committed
184

Pierre Kraemer's avatar
Pierre Kraemer committed
185
		FunctorSetEmb<typename PFP::MAP> femb(map,VERTEX_ORBIT,verticesID[pt[3]]);
Pierre Kraemer's avatar
Pierre Kraemer committed
186
187
188
189
190
191
		Dart dd = d;
		do {
			femb(dd);
//			std::cout << "embed" << std::endl;
			//vecDartPtrEmb[pt[3]].push_back(dd);
			vecDartsPerVertex[pt[3]].push_back(dd);
Pierre Kraemer's avatar
Pierre Kraemer committed
192
			dd = map.phi1(map.phi2(dd));
Pierre Kraemer's avatar
Pierre Kraemer committed
193
194
195
196
197
198
199
200
201
202
203
		} while(dd != d);

//		std::cout << "end tetra" << std::endl;
	}

//	std::cout << "end 1/2" << std::endl;

	foff.close();
	fele.close();

	//Association des phi3
Pierre Kraemer's avatar
Pierre Kraemer committed
204
	for (Dart d = map.begin(); d != map.end(); map.next(d))
Pierre Kraemer's avatar
Pierre Kraemer committed
205
206
207
208
209
	{
		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
210
			if(map.phi3(*it)==*it)
Pierre Kraemer's avatar
Pierre Kraemer committed
211
212
213
214
			{
				bool sewn=false;
				for(typename std::vector<Dart>::iterator itnext=it+1; itnext!=vec.end() && !sewn; ++itnext)
				{
Pierre Kraemer's avatar
Pierre Kraemer committed
215
216
					if(map.getDartEmbedding(VERTEX_ORBIT,map.phi1(*it))==map.getDartEmbedding(VERTEX_ORBIT,map.phi_1(*itnext))
					&& map.getDartEmbedding(VERTEX_ORBIT,map.phi_1(*it))==map.getDartEmbedding(VERTEX_ORBIT,map.phi1(*itnext)))
Pierre Kraemer's avatar
Pierre Kraemer committed
217
					{
Pierre Kraemer's avatar
Pierre Kraemer committed
218
						map.sewVolumes(*it,map.phi_1(*itnext));
Pierre Kraemer's avatar
Pierre Kraemer committed
219
220
221
222
223
224
225
226
227
228
229
230
						sewn = true;
					}
				}
			}
		}
	}

//	std::cout << "end 2/2" << std::endl;

	return true;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
231
} // namespace Import
Pierre Kraemer's avatar
Pierre Kraemer committed
232

Pierre Kraemer's avatar
Pierre Kraemer committed
233
234
235
} // namespace Algo

} // namespace CGoGN