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

40 41
	if ((filename.rfind(".node")!=std::string::npos) || (filename.rfind(".NODE")!=std::string::npos))
		return ImportVolumique::NODE;
42

43 44
	if ((filename.rfind(".off")!=std::string::npos) || (filename.rfind(".OFF")!=std::string::npos))
		return ImportVolumique::OFF;
45

46 47
	if ((filename.rfind(".ts")!=std::string::npos) || (filename.rfind(".TS")!=std::string::npos))
		return ImportVolumique::TS;
Pierre Kraemer's avatar
Pierre Kraemer committed
48

49 50 51
	if ((filename.rfind(".moka")!=std::string::npos) || (filename.rfind(".MOKA")!=std::string::npos))
		return ImportVolumique::MOKA;

Pierre Kraemer's avatar
Pierre Kraemer committed
52 53 54 55
	return ImportVolumique::UNKNOWNVOLUME;
}

template <typename PFP>
56
bool MeshTablesVolume<PFP>::importMesh(const std::string& filename, std::vector<std::string>& attrNames, float scaleFactor)
Pierre Kraemer's avatar
Pierre Kraemer committed
57
{
Pierre Kraemer's avatar
Pierre Kraemer committed
58
	ImportVolumique::ImportType kind = getFileType(filename);
Pierre Kraemer's avatar
Pierre Kraemer committed
59 60 61 62

	switch (kind)
	{
	case ImportVolumique::TET:
63
		return importTet(filename, attrNames, scaleFactor);
Pierre Kraemer's avatar
Pierre Kraemer committed
64
		break;
65
	case ImportVolumique::NODE:
66 67
		break;
	case ImportVolumique::TS:
Pierre Kraemer's avatar
Pierre Kraemer committed
68
		break;
69 70 71
//	case ImportVolumique::MOKA:
//		return importMoka(filename,attrNames);
//		break;
Pierre Kraemer's avatar
Pierre Kraemer committed
72
	default:
73
		CGoGNerr << "Not yet supported" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
74 75 76 77 78 79
		break;
	}
	return false;
}

template <typename PFP>
80
bool MeshTablesVolume<PFP>::importTet(const std::string& filename, std::vector<std::string>& attrNames, float scaleFactor)
Pierre Kraemer's avatar
Pierre Kraemer committed
81
{
82
	VertexAttribute<VEC3> positions =  m_map.template getAttribute<VEC3, VERTEX>("position") ;
83 84

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

87 88
	attrNames.push_back(positions.name()) ;

89
	AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;
90 91

	//open file
Pierre Kraemer's avatar
Pierre Kraemer committed
92 93 94
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
95
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
96 97 98
		return false;
	}

99 100 101
	std::string ligne;
	unsigned int nbv, nbt;
	// reading number of vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
102 103 104 105
	std::getline (fp, ligne);
	std::stringstream oss(ligne);
	oss >> nbv;

106
	// reading number of tetrahedra
Pierre Kraemer's avatar
Pierre Kraemer committed
107 108 109 110
	std::getline (fp, ligne);
	std::stringstream oss2(ligne);
	oss2 >> nbt;

111
	//reading vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
112 113
	std::vector<unsigned int> verticesID;
	verticesID.reserve(nbv);
114
	for(unsigned int i = 0; i < nbv;++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
115
	{
116 117 118 119
		do
		{
			std::getline (fp, ligne);
		} while (ligne.size() == 0);
Pierre Kraemer's avatar
Pierre Kraemer committed
120 121 122 123 124 125 126

		std::stringstream oss(ligne);

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

130 131
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
132 133 134 135

		verticesID.push_back(id);
	}

136
	m_nbVertices = nbv;
Pierre Kraemer's avatar
Pierre Kraemer committed
137
	m_nbVolumes = nbt;
138

Pierre Kraemer's avatar
Pierre Kraemer committed
139 140 141 142 143 144 145

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

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

146
	for (unsigned int i = 0; i < m_nbVolumes ; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
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 191 192 193 194
	{
    	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

} // namespace Algo

} // namespace CGoGN