import2tablesVolume.hpp 5.49 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(".ele")!=std::string::npos) || (filename.rfind(".ELE")!=std::string::npos))
		return ImportVolumique::ELE;
42

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

46 47 48
	if ((filename.rfind(".moka")!=std::string::npos) || (filename.rfind(".MOKA")!=std::string::npos))
		return ImportVolumique::MOKA;

Pierre Kraemer's avatar
Pierre Kraemer committed
49 50 51 52
	return ImportVolumique::UNKNOWNVOLUME;
}

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

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

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

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

84 85
	attrNames.push_back(positions.name()) ;

86 87 88
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;

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

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

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

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

		std::stringstream oss(ligne);

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

127 128
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
129 130 131 132

		verticesID.push_back(id);
	}

133
	m_nbVertices = nbv;
Pierre Kraemer's avatar
Pierre Kraemer committed
134
	m_nbVolumes = nbt;
135

Pierre Kraemer's avatar
Pierre Kraemer committed
136 137 138 139 140 141 142

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

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

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