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

namespace CGoGN
{

namespace Algo
{

31
32
33
namespace Volume
{

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


template <typename PFP>
39
bool MeshTablesVolume<PFP>::importMesh(const std::string& filename, std::vector<std::string>& attrNames, float scaleFactor)
Pierre Kraemer's avatar
Pierre Kraemer committed
40
{
41
	ImportType kind = getFileType(filename);
Pierre Kraemer's avatar
Pierre Kraemer committed
42
43
44

	switch (kind)
	{
45
	case TET:
46
		return importTet(filename, attrNames, scaleFactor);
Pierre Kraemer's avatar
Pierre Kraemer committed
47
		break;
48
	case NODE:
49
		break;
50
	case TS:
Pierre Kraemer's avatar
Pierre Kraemer committed
51
		break;
52
53
54
//	case ImportVolumique::MOKA:
//		return importMoka(filename,attrNames);
//		break;
Pierre Kraemer's avatar
Pierre Kraemer committed
55
	default:
56
		CGoGNerr << "Not yet supported" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
57
58
59
60
61
62
		break;
	}
	return false;
}

template <typename PFP>
63
bool MeshTablesVolume<PFP>::importTet(const std::string& filename, std::vector<std::string>& attrNames, float scaleFactor)
Pierre Kraemer's avatar
Pierre Kraemer committed
64
{
65
	VertexAttribute<VEC3> positions =  m_map.template getAttribute<VEC3, VERTEX>("position") ;
66
67

	if (!positions.isValid())
68
		positions = m_map.template addAttribute<VEC3, VERTEX>("position") ;
69

70
71
	attrNames.push_back(positions.name()) ;

72
	AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;
73
74

	//open file
Pierre Kraemer's avatar
Pierre Kraemer committed
75
76
77
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
78
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
79
80
81
		return false;
	}

82
83
84
	std::string ligne;
	unsigned int nbv, nbt;
	// reading number of vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
85
86
87
88
	std::getline (fp, ligne);
	std::stringstream oss(ligne);
	oss >> nbv;

89
	// reading number of tetrahedra
Pierre Kraemer's avatar
Pierre Kraemer committed
90
91
92
93
	std::getline (fp, ligne);
	std::stringstream oss2(ligne);
	oss2 >> nbt;

94
	//reading vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
95
96
	std::vector<unsigned int> verticesID;
	verticesID.reserve(nbv);
97
	for(unsigned int i = 0; i < nbv;++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
98
	{
99
100
101
102
		do
		{
			std::getline (fp, ligne);
		} while (ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
103
104
105
106
107
108
109

		std::stringstream oss(ligne);

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

113
114
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
115
116
117
118

		verticesID.push_back(id);
	}

119
	m_nbVertices = nbv;
Pierre Kraemer's avatar
Pierre Kraemer committed
120
	m_nbVolumes = nbt;
121

Pierre Kraemer's avatar
Pierre Kraemer committed
122
123
124
125
126
127
128

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

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

129
	for (unsigned int i = 0; i < m_nbVolumes ; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
130
131
132
133
134
135
136
137
138
139
140
141
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
	{
    	do
    	{
    		std::getline (fp, ligne);
    	} while (ligne.size()==0);

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

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

} // namespace Import

175
176
}

Pierre Kraemer's avatar
Pierre Kraemer committed
177
178
179
} // namespace Algo

} // namespace CGoGN