import2tablesVolume.hpp 5.29 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(".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, float scaleFactor)
Pierre Kraemer's avatar
Pierre Kraemer committed
51
{
Pierre Kraemer's avatar
Pierre Kraemer committed
52
	ImportVolumique::ImportType kind = getFileType(filename);
Pierre Kraemer's avatar
Pierre Kraemer committed
53 54 55 56

	switch (kind)
	{
	case ImportVolumique::TET:
57
		return importTet(filename, attrNames, scaleFactor);
Pierre Kraemer's avatar
Pierre Kraemer committed
58
		break;
59
	case ImportVolumique::NODE:
60 61
		break;
	case ImportVolumique::TS:
Pierre Kraemer's avatar
Pierre Kraemer committed
62 63
		break;
	default:
64
		CGoGNerr << "Not yet supported" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
65 66 67 68 69 70
		break;
	}
	return false;
}

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

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

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

80
	AttributeContainer& container = m_map.template getAttributeContainer<VERTEX>() ;
81 82

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

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

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

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

		std::stringstream oss(ligne);

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

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

		verticesID.push_back(id);
	}

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

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

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

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

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