import2tablesVolume.hpp 8.49 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
30
31
32
33
34
35
36
37
38
/*******************************************************************************
* 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                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Algo
{

namespace Import
{

template <typename PFP>
ImportVolumique::ImportType MeshTablesVolume<PFP>::getFileType(const std::string& filename)
{
	if ((filename.rfind(".tet")!=std::string::npos) || (filename.rfind(".TET")!=std::string::npos))
		return ImportVolumique::TET;
39

Pierre Kraemer's avatar
Pierre Kraemer committed
40
41
	if ((filename.rfind(".trianbgz")!=std::string::npos) || (filename.rfind(".TRIANBGZ")!=std::string::npos))
		return ImportVolumique::TRIANBGZ;
42

Pierre Kraemer's avatar
Pierre Kraemer committed
43
44
45
46
47
48
49
	if ((filename.rfind(".ply")!=std::string::npos) || (filename.rfind(".PLY")!=std::string::npos))
		return ImportVolumique::PLY;

	return ImportVolumique::UNKNOWNVOLUME;
}

template <typename PFP>
50
bool MeshTablesVolume<PFP>::importMesh(const std::string& filename, std::vector<std::string>& attrNames, ImportVolumique::ImportType kind, float scaleFactor=1.0f)
Pierre Kraemer's avatar
Pierre Kraemer committed
51
52
53
54
55
56
57
{
	if (kind == ImportVolumique::UNKNOWNVOLUME)
		kind = getFileType(filename);

	switch (kind)
	{
	case ImportVolumique::PLY:
58
		return importPly(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
59
60
		break;
	case ImportVolumique::TET:
61
		return importTet(filename, attrNames, scaleFactor);
Pierre Kraemer's avatar
Pierre Kraemer committed
62
63
		break;
	case ImportVolumique::TRIANBGZ:
64
		return importTrianBinGz(filename, attrNames);
Pierre Kraemer's avatar
Pierre Kraemer committed
65
66
67
68
69
70
71
72
73
		break;
	default:
		std::cerr << "Not yet supported" << std::endl;
		break;
	}
	return false;
}

template <typename PFP>
74
bool MeshTablesVolume<PFP>::importTet(const std::string& filename, std::vector<std::string>& attrNames, float scaleFactor=1.0f)
Pierre Kraemer's avatar
Pierre Kraemer committed
75
{
76
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX_CELL) ;
77
78
79
	AttributeHandler<typename PFP::VEC3> positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX_ORBIT, "position") ;
	attrNames.push_back(positions.name()) ;

Pierre Kraemer's avatar
Pierre Kraemer committed
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
	// open file
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
		std::cerr << "Unable to open file " << filename<< std::endl;
		return false;
	}

    std::string ligne;
    int nbv,nbt;
    // lecture des nombres de sommets/tetra
	std::getline (fp, ligne);
	std::stringstream oss(ligne);
	oss >> nbv;

	//std::cout << "nbV = " << nbv << std::endl;

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

	//std::cout << "nbT = " << nbt << std::endl;

	//lecture sommets
	std::vector<unsigned int> verticesID;
	verticesID.reserve(nbv);
	for(int i=0; i<nbv;++i)
	{
    	do
    	{
    		std::getline (fp, ligne);
    	} while (ligne.size()==0);

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

		//std::cout << "VEC3 = " << pos << std::endl;

124
125
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

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

	//std::cout << "nbVertices = " << m_nbVertices << std::endl;
	m_nbVolumes = nbt;
	//std::cout << "nbVolumes = " << m_nbVolumes << std::endl;

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

	m_nbFaces=nbt*4;
	m_emb.reserve(nbt*12);

141
	for (unsigned int i = 0; i < m_nbVolumes ; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
	{
    	do
    	{
    		std::getline (fp, ligne);
    	} while (ligne.size()==0);

		std::stringstream oss(ligne);
		int n;
		oss >> n; // nb de faces d'un volume ?
//		m_nbVerticesPerFace.push_back(3);
//		m_nbVerticesPerFace.push_back(3);
//		m_nbVerticesPerFace.push_back(3);
//		m_nbVerticesPerFace.push_back(3);

//		m_nbFacesPerVolume.push_back(4);
		m_nbEdges.push_back(3);
		m_nbEdges.push_back(3);
		m_nbEdges.push_back(3);
		m_nbEdges.push_back(3);

		int s0,s1,s2,s3;

		oss >> s0;
		oss >> s1;
		oss >> s2;
		oss >> s3;

		m_emb.push_back(verticesID[s0]);
		m_emb.push_back(verticesID[s1]);
		m_emb.push_back(verticesID[s2]);

		m_emb.push_back(verticesID[s1]);
		m_emb.push_back(verticesID[s0]);
		m_emb.push_back(verticesID[s3]);

		m_emb.push_back(verticesID[s2]);
		m_emb.push_back(verticesID[s1]);
		m_emb.push_back(verticesID[s3]);

		m_emb.push_back(verticesID[s0]);
		m_emb.push_back(verticesID[s2]);
		m_emb.push_back(verticesID[s3]);
	}

	fp.close();
	return true;
}

template <typename PFP>
191
bool MeshTablesVolume<PFP>::importTrianBinGz(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
{
//	// open file
//	igzstream fs(filename.c_str(), std::ios::in|std::ios::binary);
//
//	if (!fs.good())
//	{
//		std::cerr << "Unable to open file " << filename << std::endl;
//		return false;
//	}
//	// read nb of points
//	int nbp;
//	fs.read(reinterpret_cast<char*>(&nbp), sizeof(int));
//
//	// read points
//	std::vector<EMB*> vertices;
//	{// juste pour limiter la portee des variables
//		vertices.reserve(nbp);
//		int labEmb=0;
//		float* buffer = new float[nbp*3];
//		fs.read(reinterpret_cast<char*>(buffer),3*nbp*sizeof(float));
//		float *ptr = buffer;
//		for (int i=0; i<nbp; ++i)
//		{
//			gmtl::Vec3f pos;
//			pos[0]= *ptr++;
//			pos[1]= *ptr++;
//			pos[2]= *ptr++;
//			EMB* em = EMB::create(pos);
//			em->setLabel(labEmb++);
//			vertices.push_back(em);
//		}
//		delete[] buffer;
//	}
//	m_nbVertices = vertices.size();
//
//	// read nb of faces
//	fs.read(reinterpret_cast<char*>(&m_nbFaces), sizeof(int));
//	m_nbVolumes=1;
//	m_nbFacesPerVolume.push_back(m_nbFaces);
//	m_nbVerticesPerFace.reserve(m_nbFaces);
//	m_emb.reserve(3*m_nbFaces);
//
//	// read indices of faces
//	{// juste pour limiter la portee des variables
//		int* buffer = new int[m_nbFaces*6];
//		fs.read(reinterpret_cast<char*>(buffer),6*m_nbFaces*sizeof(float));
//		int *ptr = buffer;
//
//		for (unsigned i=0; i<m_nbFaces; i++)
//		{
//			m_nbVerticesPerFace.push_back(3);
//			m_emb.push_back(vertices[*ptr++]);
//			m_emb.push_back(vertices[*ptr++]);
//			m_emb.push_back(vertices[*ptr++]);
//		}
//	}
//
//	fs.close();
	return true;
}

template<typename PFP>
254
bool MeshTablesVolume<PFP>::importPly(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
{
//	PlyImportData pid;
//
//	if (! pid.read_file(filename) )
//	{
//		std::cerr << "Unable to open file " << filename<< std::endl;
//		return false;
//	}
//
//    // lecture des nombres de sommets/faces/ar�tes
//	m_nbVertices = pid.nbVertices();
//	m_nbFaces = pid.nbFaces();
//	m_nbVolumes=1;
//
//
//	//lecture sommets
//	std::vector<EMB*> vertices;
//	vertices.reserve(m_nbVertices);
//	for (unsigned i=0; i<m_nbVertices;++i)
//	{
//		EMB* em =EMB::create(pid.vertexPosition(i));
//		em->setLabel(i);
//		vertices.push_back(em);
//	}
//
//	m_nbVerticesPerFace.reserve(m_nbFaces);
//	m_emb.reserve(m_nbVertices*8);
//	m_nbFacesPerVolume.push_back(m_nbFaces);
//
//	for (unsigned i=0; i<m_nbFaces;++i)
//	{
//		int n = pid.getFaceValence(i);
//		m_nbVerticesPerFace.push_back(n);
//		int* indices = pid.getFaceIndices(i);
//		for (int j=0;j<n; ++j)
//		{
//			m_emb.push_back(vertices[ indices[j] ]);
//		}
//	}
	return true;
}

} // namespace Import

} // namespace Algo

} // namespace CGoGN