import2tablesVolume.hpp 5.34 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 49

	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::TET:
58
		return importTet(filename, attrNames, scaleFactor);
Pierre Kraemer's avatar
Pierre Kraemer committed
59
		break;
60 61 62
	case ImportVolumique::ELE:
		break;
	case ImportVolumique::TS:
Pierre Kraemer's avatar
Pierre Kraemer committed
63 64
		break;
	default:
65
		CGoGNerr << "Not yet supported" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
66 67 68 69 70 71
		break;
	}
	return false;
}

template <typename PFP>
72
bool MeshTablesVolume<PFP>::importTet(const std::string& filename, std::vector<std::string>& attrNames, float scaleFactor=1.0f)
Pierre Kraemer's avatar
Pierre Kraemer committed
73
{
74
	AttributeHandler<VEC3> positions =  m_map.template getAttribute<VEC3>(VERTEX, "position") ;
75 76

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

79 80
	attrNames.push_back(positions.name()) ;

81 82 83
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;

	//open file
Pierre Kraemer's avatar
Pierre Kraemer committed
84 85 86
	std::ifstream fp(filename.c_str(), std::ios::in);
	if (!fp.good())
	{
87
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
88 89 90
		return false;
	}

91 92 93
	std::string ligne;
	unsigned int nbv, nbt;
	// reading number of vertices
Pierre Kraemer's avatar
Pierre Kraemer committed
94 95 96 97
	std::getline (fp, ligne);
	std::stringstream oss(ligne);
	oss >> nbv;

98
	// reading number of tetrahedra
Pierre Kraemer's avatar
Pierre Kraemer committed
99 100 101 102
	std::getline (fp, ligne);
	std::stringstream oss2(ligne);
	oss2 >> nbt;

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

		std::stringstream oss(ligne);

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

122 123
		unsigned int id = container.insertLine();
		positions[id] = pos;
Pierre Kraemer's avatar
Pierre Kraemer committed
124 125 126 127

		verticesID.push_back(id);
	}

128
	m_nbVertices = nbv;
Pierre Kraemer's avatar
Pierre Kraemer committed
129
	m_nbVolumes = nbt;
130

Pierre Kraemer's avatar
Pierre Kraemer committed
131 132 133 134 135 136 137

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

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

138
	for (unsigned int i = 0; i < m_nbVolumes ; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
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 175 176 177 178 179 180 181 182 183 184 185 186 187
	{
    	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