import2tablesVolume.hpp 8.7 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

Pierre Kraemer's avatar
Pierre Kraemer committed
40 41
	if ((filename.rfind(".trianbgz")!=std::string::npos) || (filename.rfind(".TRIANBGZ")!=std::string::npos))
		return ImportVolumique::TRIANBGZ;
42

Pierre Kraemer's avatar
Pierre Kraemer committed
43 44 45 46 47 48 49
	if ((filename.rfind(".ply")!=std::string::npos) || (filename.rfind(".PLY")!=std::string::npos))
		return ImportVolumique::PLY;

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

template <typename PFP>
74
bool MeshTablesVolume<PFP>::importTet(const std::string& filename, std::vector<std::string>& attrNames, float scaleFactor=1.0f)
Pierre Kraemer's avatar
Pierre Kraemer committed
75
{
76
	AttributeContainer& container = m_map.getAttributeContainer(VERTEX) ;
77

78 79
//	AttributeHandler<typename PFP::VEC3> positions = m_map.template addAttribute<typename PFP::VEC3>(VERTEX, "position") ;
	AttributeHandler<typename PFP::VEC3> positions =  m_map.template getAttribute<typename PFP::VEC3>(VERTEX, "position") ;
80 81

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

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

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

    std::string ligne;
    int nbv,nbt;
    // lecture des nombres de sommets/tetra
	std::getline (fp, ligne);
	std::stringstream oss(ligne);
	oss >> nbv;

101
	//CGoGNout << "nbV = " << nbv << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
102 103 104 105 106

	std::getline (fp, ligne);
	std::stringstream oss2(ligne);
	oss2 >> nbt;

107
	//CGoGNout << "nbT = " << nbt << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

	//lecture sommets
	std::vector<unsigned int> verticesID;
	verticesID.reserve(nbv);
	for(int i=0; i<nbv;++i)
	{
    	do
    	{
    		std::getline (fp, ligne);
    	} while (ligne.size()==0);

		std::stringstream oss(ligne);

		float x,y,z;
		oss >> x;
		oss >> y;
		oss >> z;
		// on peut ajouter ici la lecture de couleur si elle existe
		VEC3 pos(x*scaleFactor,y*scaleFactor,z*scaleFactor);

128
		//CGoGNout << "VEC3 = " << pos << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
129

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

		verticesID.push_back(id);
	}
	m_nbVertices = verticesID.size();

137
	//CGoGNout << "nbVertices = " << m_nbVertices << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
138
	m_nbVolumes = nbt;
139
	//CGoGNout << "nbVolumes = " << m_nbVolumes << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
140 141 142 143 144 145 146

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

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

147
	for (unsigned int i = 0; i < m_nbVolumes ; ++i)
Pierre Kraemer's avatar
Pierre Kraemer committed
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 195 196
	{
    	do
    	{
    		std::getline (fp, ligne);
    	} while (ligne.size()==0);

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

//		m_nbFacesPerVolume.push_back(4);
		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;
}

template <typename PFP>
197
bool MeshTablesVolume<PFP>::importTrianBinGz(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
198 199 200 201 202 203
{
//	// open file
//	igzstream fs(filename.c_str(), std::ios::in|std::ios::binary);
//
//	if (!fs.good())
//	{
204
//		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
//		return false;
//	}
//	// read nb of points
//	int nbp;
//	fs.read(reinterpret_cast<char*>(&nbp), sizeof(int));
//
//	// read points
//	std::vector<EMB*> vertices;
//	{// juste pour limiter la portee des variables
//		vertices.reserve(nbp);
//		int labEmb=0;
//		float* buffer = new float[nbp*3];
//		fs.read(reinterpret_cast<char*>(buffer),3*nbp*sizeof(float));
//		float *ptr = buffer;
//		for (int i=0; i<nbp; ++i)
//		{
//			gmtl::Vec3f pos;
//			pos[0]= *ptr++;
//			pos[1]= *ptr++;
//			pos[2]= *ptr++;
//			EMB* em = EMB::create(pos);
//			em->setLabel(labEmb++);
//			vertices.push_back(em);
//		}
//		delete[] buffer;
//	}
//	m_nbVertices = vertices.size();
//
//	// read nb of faces
//	fs.read(reinterpret_cast<char*>(&m_nbFaces), sizeof(int));
//	m_nbVolumes=1;
//	m_nbFacesPerVolume.push_back(m_nbFaces);
//	m_nbVerticesPerFace.reserve(m_nbFaces);
//	m_emb.reserve(3*m_nbFaces);
//
//	// read indices of faces
//	{// juste pour limiter la portee des variables
//		int* buffer = new int[m_nbFaces*6];
//		fs.read(reinterpret_cast<char*>(buffer),6*m_nbFaces*sizeof(float));
//		int *ptr = buffer;
//
//		for (unsigned i=0; i<m_nbFaces; i++)
//		{
//			m_nbVerticesPerFace.push_back(3);
//			m_emb.push_back(vertices[*ptr++]);
//			m_emb.push_back(vertices[*ptr++]);
//			m_emb.push_back(vertices[*ptr++]);
//		}
//	}
//
//	fs.close();
	return true;
}

template<typename PFP>
260
bool MeshTablesVolume<PFP>::importPly(const std::string& filename, std::vector<std::string>& attrNames)
Pierre Kraemer's avatar
Pierre Kraemer committed
261 262 263 264 265
{
//	PlyImportData pid;
//
//	if (! pid.read_file(filename) )
//	{
266
//		CGoGNerr << "Unable to open file " << filename<< CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
//		return false;
//	}
//
//    // lecture des nombres de sommets/faces/ar�tes
//	m_nbVertices = pid.nbVertices();
//	m_nbFaces = pid.nbFaces();
//	m_nbVolumes=1;
//
//
//	//lecture sommets
//	std::vector<EMB*> vertices;
//	vertices.reserve(m_nbVertices);
//	for (unsigned i=0; i<m_nbVertices;++i)
//	{
//		EMB* em =EMB::create(pid.vertexPosition(i));
//		em->setLabel(i);
//		vertices.push_back(em);
//	}
//
//	m_nbVerticesPerFace.reserve(m_nbFaces);
//	m_emb.reserve(m_nbVertices*8);
//	m_nbFacesPerVolume.push_back(m_nbFaces);
//
//	for (unsigned i=0; i<m_nbFaces;++i)
//	{
//		int n = pid.getFaceValence(i);
//		m_nbVerticesPerFace.push_back(n);
//		int* indices = pid.getFaceIndices(i);
//		for (int j=0;j<n; ++j)
//		{
//			m_emb.push_back(vertices[ indices[j] ]);
//		}
//	}
	return true;
}

} // namespace Import

} // namespace Algo

} // namespace CGoGN