importObjTex.hpp 13.1 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
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

Sylvain Thery's avatar
Sylvain Thery committed
25 26 27
#include "Topology/generic/attributeHandler.h"
#include "Container/fakeAttribute.h"
#include <fstream>
thery's avatar
thery committed
28

Pierre Kraemer's avatar
Pierre Kraemer committed
29 30 31 32 33

namespace CGoGN
{
namespace Algo
{
34 35
namespace Surface
{
Pierre Kraemer's avatar
Pierre Kraemer committed
36 37 38
namespace Import 
{

Sylvain Thery's avatar
Sylvain Thery committed
39 40 41 42 43 44 45 46 47 48 49
template <typename PFP>
OBJModel<PFP>::OBJModel(typename PFP::MAP& map):
	m_map(map),m_specialVertices(map),m_dirtyEdges(map)
{
}

template <typename PFP>
inline typename PFP::VEC3 OBJModel<PFP>::getPosition(Dart d)
{
	return m_positions[d];
}
thery's avatar
thery committed
50

Sylvain Thery's avatar
Sylvain Thery committed
51 52 53 54 55 56 57 58 59 60 61 62

template <typename PFP>
inline typename PFP::VEC3 OBJModel<PFP>::getNormal(Dart d)
{
	if (m_specialVertices.isMarked(d))
		return m_normalsF[d];
	return m_normals[d];
}


template <typename PFP>
inline Geom::Vec2f OBJModel<PFP>::getTexCoord(Dart d)
thery's avatar
thery committed
63
{
Sylvain Thery's avatar
Sylvain Thery committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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
	if (m_specialVertices.isMarked(d))
		return m_texCoordsF[d];
	return m_texCoords[d];
}


template <typename PFP>
std::vector<std::string>& OBJModel<PFP>::getMaterialNames()
{
	return m_materialNames;
}



template <typename PFP>
void OBJModel<PFP>::readMaterials(const std::string& filename)
{
	m_materials.resize(m_materialNames.size());
		
	// open file
	std::ifstream fp(filename.c_str());
	if (!fp.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
		return false;
	}
	
	std::vector<MaterialOBJ>::iterator mit;
	std::string ligne;
	std::string tag;
	fp >> tag;
	do
	{
		std::getline (fp, ligne);
		if (tag == "newmtl")
		{
			std::vector<std::string>::iterator it = std::find(m_materialNames.begin(), m_materialNames.end(), ligne);
			if (it ==  m_materialNames.end())
			{
				CGoGNerr << "Skipping material "<< ligne << CGoGNendl;
				do
				{
					fp >> tag;
					
				}while (!fp.eof() && (tag == "new mtl")); 
			}
			else
			{
				CGoGNerr << "Reading material "<< ligne << CGoGNendl;
				mit =  m_materials.begin() + (it - m_materialNames.begin());
			}
		}
		else
		{
			std::stringstream oss(ligne);
			if (tag == "Ka")
			{
				oss >> mit->ambiantColor[0];
				oss >> mit->ambiantColor[1];
				oss >> mit->ambiantColor[2];
			}
			if (tag == "Kd")
			{
				oss >> mit->diffuseColor[0];
				oss >> mit->diffuseColor[1];
				oss >> mit->diffuseColor[2];
				
			}
			if (tag == "Ks")
			{
				oss >> mit->specularColor[0];
				oss >> mit->specularColor[1];
				oss >> mit->specularColor[2];
			}
			if (tag == "Ns")
			{
				oss >> mit->shininess;
			}
			
			if (tag == "map_Ka") // ambiant texture
			{
				CGoGNerr << tag << " not yet supported in OBJ material reading" << CGoGNendl;
			}
			if (tag == "map_Kd") // diffuse texture
			{
				CGoGNerr << tag << " not yet supported in OBJ material reading" << CGoGNendl;
			}
			if (tag == "map_d") // opacity texture
			{
				CGoGNerr << tag << " not yet supported in OBJ material reading" << CGoGNendl;
			}
			if ((tag == "map_bump") || (tag == "bump"))
			{
				CGoGNerr << tag << " not yet supported in OBJ material reading" << CGoGNendl;
			}
			
			fp >> tag;

		}
	}while (!fp.eof());
}





template <typename PFP>
bool OBJModel<PFP>::generateBrowsers(std::vector<MapBrowser*>& browsers)
{
	browsers.clear();
	
	if (m_groupNames.empty())
		return false;
	
Sylvain Thery's avatar
Sylvain Thery committed
178
	MapBrowserLinked* MBLptr = new MapBrowserLinked(m_map);
Sylvain Thery's avatar
Sylvain Thery committed
179 180 181 182 183
	DartAttribute<Dart>& links = MBLptr->getLinkAttr();
	browsers.push_back(MBLptr);
	
	for (unsigned int i = 1; i<m_groupNames.size(); ++i)
	{
Sylvain Thery's avatar
Sylvain Thery committed
184
		MapBrowser* MBptr = new MapBrowserLinked(m_map,links);
Sylvain Thery's avatar
Sylvain Thery committed
185 186 187 188 189 190
		browsers.push_back(MBptr);
	}
	
	for (Dart d=m_map.begin(); d!=m_map.end(); m_map.next(d))
	{
		unsigned int g = m_groups[d] -1 ; // groups are name from 1
Sylvain Thery's avatar
Sylvain Thery committed
191 192
		MapBrowserLinked* mb = static_cast<MapBrowserLinked*>(browsers[g]);
		mb->pushBack(d);
Sylvain Thery's avatar
Sylvain Thery committed
193 194 195 196 197 198 199 200 201 202 203 204 205
	}
	return true;
}




	
template <typename PFP>
short OBJModel<PFP>::readObjLine(std::stringstream& oss, std::vector<unsigned int>& indices)
{
	indices.clear();
	
thery's avatar
thery committed
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
	unsigned int nb=0;
	while (!oss.eof())  // lecture de tous les indices
	{
		int index;
		oss >> index;

		indices.push_back(index);

		int slash = 0;
		char sep='_';
		do
		{
			oss >> sep;
			if (sep =='/')
				++slash;
		} while ( ((sep=='/') || (sep ==' ')) && !oss.eof() ) ;

		if ((sep>='0') && (sep<='9'))
			oss.seekg(-1,std::ios_base::cur);

		if (slash == 0)
		{
			if (indices.size()%3 == 1)
			{
				indices.push_back(0);
				indices.push_back(0);
			}
			if (indices.size()%3 == 2)
			{
				indices.push_back(0);
			}
			nb++;
		}


		if (slash == 2)
		{
			indices.push_back(0);
		}
	}
	return nb;
}


Sylvain Thery's avatar
Sylvain Thery committed
250 251 252 253 254
// template <typename PFP>
// bool importObjTex(typename PFP::MAP& map, const std::string& filename,
// 				  std::vector<std::string>& attrNames,/* std::vector<MapBrowserLinked*>& browsers,*/
// 				  CellMarker<VERTEX>& specialVertices, DartMarker& dirtyEdges)

thery's avatar
thery committed
255
template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
256
bool OBJModel<PFP>::import( const std::string& filename, std::vector<std::string>& attrNames)
thery's avatar
thery committed
257 258 259 260 261 262
{
	typedef  typename PFP::VEC3 VEC3;
	typedef Geom::Vec2f VEC2;

	attrNames.clear();
	// open file
Sylvain Thery's avatar
Sylvain Thery committed
263
	std::ifstream fp(filename.c_str()/*, std::ios::binary*/);
thery's avatar
thery committed
264 265 266 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
	if (!fp.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
		return false;
	}

	unsigned int tagV = 0;
	unsigned int tagVT = 0;
	unsigned int tagVN = 0;
	unsigned int tagG = 0;
	unsigned int tagF = 0;

	std::string ligne;
	std::string tag;
	do
	{
		fp >> tag;
		std::getline (fp, ligne);
		if (tag == "v")
			tagV++;
		if (tag == "vn")
			tagVN++;
		if (tag == "vt")
			tagVT++;
		if (tag == "g")
			tagG++;
		if (tag == "f")
			tagF++;
	}while (!fp.eof());

	std::cout << "Parsing OBJ"<< tagV<< std::endl;
	std::cout << "Vertices:"<< tagV<< std::endl;
	std::cout << "Normals:"<< tagVN<< std::endl;
	std::cout << "TexCoords:"<< tagVT<< std::endl;
	std::cout << "Groups:"<< tagG<< std::endl;
	std::cout << "Faces:"<< tagF<< std::endl;


Sylvain Thery's avatar
Sylvain Thery committed
302 303 304 305
	m_positions =  m_map.template getAttribute<typename PFP::VEC3, VERTEX>("position") ;
	if (!m_positions.isValid())
		m_positions = m_map.template addAttribute<VEC3, VERTEX>("position") ;
	attrNames.push_back(m_positions.name()) ;
thery's avatar
thery committed
306

Sylvain Thery's avatar
Sylvain Thery committed
307
	if (tagVT != 0)
thery's avatar
thery committed
308
	{
Sylvain Thery's avatar
Sylvain Thery committed
309 310 311 312 313 314 315 316
		m_texCoords =  m_map.template getAttribute<VEC2, VERTEX>("texCoord") ;
		if (!m_texCoords.isValid())
			m_texCoords = m_map.template addAttribute<VEC2, VERTEX>("texCoord") ;
		attrNames.push_back(m_texCoords.name()) ;

		m_texCoordsF =  m_map.template getAttribute<VEC2, VERTEX1>("texCoordF") ;
		if (!m_texCoordsF.isValid())
			m_texCoordsF = m_map.template addAttribute<VEC2, VERTEX1>("texCoordF") ;
thery's avatar
thery committed
317 318
	}

Sylvain Thery's avatar
Sylvain Thery committed
319
	if (tagVN != 0)
thery's avatar
thery committed
320
	{
Sylvain Thery's avatar
Sylvain Thery committed
321 322 323 324 325 326 327 328
		m_normals =  m_map.template getAttribute<typename PFP::VEC3, VERTEX>("normal") ;
		if (!m_normals.isValid())
			m_normals = m_map.template addAttribute<VEC3, VERTEX>("normal") ;
		attrNames.push_back(m_normals.name()) ;

		m_normalsF =  m_map.template getAttribute<VEC3, VERTEX1>("normalF") ;
		if (!m_normalsF.isValid())
			m_normalsF = m_map.template addAttribute<VEC3, VERTEX1>("normalF") ;
thery's avatar
thery committed
329 330
	}

Sylvain Thery's avatar
Sylvain Thery committed
331
	
thery's avatar
thery committed
332
	if (tagG != 0)
Sylvain Thery's avatar
Sylvain Thery committed
333 334 335 336 337 338 339
	{
		m_groups =  m_map.template getAttribute<unsigned int, FACE>("groups") ;
		if (!m_groups.isValid())
			m_groups = m_map.template addAttribute<unsigned int, FACE>("groups") ;
// ??	attrNames.push_back(m_groups.name()) ;
	}
	
thery's avatar
thery committed
340 341 342 343 344 345 346 347

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

	fp.close();
	fp.clear();
	fp.open(filename.c_str());

	std::vector<VEC3> normalsBuffer;
Sylvain Thery's avatar
Sylvain Thery committed
348 349
	normalsBuffer.reserve(tagVN);
	
thery's avatar
thery committed
350
	std::vector<VEC2> texCoordsBuffer;
Sylvain Thery's avatar
Sylvain Thery committed
351 352
	texCoordsBuffer.reserve(tagVT);
	
thery's avatar
thery committed
353
	std::vector<unsigned int> verticesID;
Sylvain Thery's avatar
Sylvain Thery committed
354 355 356 357
	verticesID.reserve(tagV);
	
	std::vector<unsigned int> normalsID;
	normalsID.reserve(tagV);
thery's avatar
thery committed
358 359
	std::vector<unsigned int> texCoordsID;
	texCoordsID.reserve(tagV);
Sylvain Thery's avatar
Sylvain Thery committed
360 361
	
	
thery's avatar
thery committed
362 363 364

	std::vector<unsigned int> localIndices;
	localIndices.reserve(64*3);
Sylvain Thery's avatar
Sylvain Thery committed
365
	FunctorInitEmb<typename PFP::MAP, VERTEX> fsetemb(m_map);
thery's avatar
thery committed
366

Sylvain Thery's avatar
Sylvain Thery committed
367 368 369
	VertexAutoAttribute< NoMathIONameAttribute< std::vector<Dart> > > vecDartsPerVertex(m_map, "incidents");
	VertexAutoAttribute< NoMathIONameAttribute< std::vector<unsigned int> > > vecNormIndPerVertex(m_map, "incidentsN");
	VertexAutoAttribute< NoMathIONameAttribute< std::vector<unsigned int> > > vecTCIndPerVertex(m_map, "incidentsTC");
thery's avatar
thery committed
370

Sylvain Thery's avatar
Sylvain Thery committed
371
	unsigned int currentGroup = 0;
thery's avatar
thery committed
372

Sylvain Thery's avatar
Sylvain Thery committed
373
	DartMarkerNoUnmark mk(m_map) ;
thery's avatar
thery committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
	unsigned int i = 0;
	fp >> tag;
	std::getline(fp, ligne);
	do
	{
		if (tag == std::string("v"))
		{
			std::stringstream oss(ligne);

			float x,y,z;
			oss >> x;
			oss >> y;
			oss >> z;

			VEC3 pos(x,y,z);

			unsigned int id = container.insertLine();
Sylvain Thery's avatar
Sylvain Thery committed
391
			m_positions[id] = pos;
thery's avatar
thery committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413

			verticesID.push_back(id);
			i++;
		}

		if (tag == std::string("vn"))
		{
			std::stringstream oss(ligne);

			VEC3 norm;
			oss >> norm[0];
			oss >> norm[1];
			oss >> norm[2];
			normalsBuffer.push_back(norm);
		}

		if (tag == std::string("vt"))
		{
			std::stringstream oss(ligne);
			VEC2 tc;
			oss >> tc[0];
			oss >> tc[1];
Sylvain Thery's avatar
Sylvain Thery committed
414
			texCoordsBuffer.push_back(tc);
thery's avatar
thery committed
415 416 417 418
		}

		if (tag == std::string("g"))
		{
Sylvain Thery's avatar
Sylvain Thery committed
419 420 421 422 423 424 425 426 427 428
			m_groupNames.push_back(ligne);
			std::string buf;
			fp >> buf;
			if (buf != "usemtl")
			{
				CGoGNerr << "problem reading OBJ, waiting for usemtl get "<< buf << CGoGNendl;
			}
			fp >> buf;
			m_materialNames.push_back(buf);
			currentGroup++;
thery's avatar
thery committed
429 430 431 432 433 434 435 436
		}

		if (tag == std::string("f"))
		{
			std::stringstream oss(ligne);

			short nbe = readObjLine(oss,localIndices);

Sylvain Thery's avatar
Sylvain Thery committed
437 438 439
			Dart d = m_map.newFace(nbe, false);
			m_groups[d] = currentGroup;
			
thery's avatar
thery committed
440 441 442 443
			for (short j = 0; j < nbe; ++j)
			{
				unsigned int em = localIndices[3*j]-1;		// get embedding
				fsetemb.changeEmb(em) ;
Sylvain Thery's avatar
Sylvain Thery committed
444 445
				m_map.template foreach_dart_of_orbit<PFP::MAP::VERTEX_OF_PARENT>(d, fsetemb);
				mk.mark(d) ;								// mark on the fly to unmark on second loop
thery's avatar
thery committed
446 447 448
				vecDartsPerVertex[em].push_back(d);		// store incident darts for fast adjacency reconstruction
				vecTCIndPerVertex[em].push_back(localIndices[3*j+1]-1);
				vecNormIndPerVertex[em].push_back(localIndices[3*j+2]-1);
Sylvain Thery's avatar
Sylvain Thery committed
449
				d = m_map.phi1(d);
thery's avatar
thery committed
450 451 452 453 454
			}
		}
		fp >> tag;
		std::getline(fp, ligne);
	} while (!fp.eof());
Sylvain Thery's avatar
Sylvain Thery committed
455
	fp.close ();
thery's avatar
thery committed
456 457 458

	// reconstruct neighbourhood
	unsigned int nbBoundaryEdges = 0;
Sylvain Thery's avatar
Sylvain Thery committed
459
	for (Dart d = m_map.begin(); d != m_map.end(); m_map.next(d))
thery's avatar
thery committed
460
	{
Sylvain Thery's avatar
Sylvain Thery committed
461
		if (mk.isMarked(d))
thery's avatar
thery committed
462 463
		{
			// darts incident to end vertex of edge
Sylvain Thery's avatar
Sylvain Thery committed
464
			std::vector<Dart>& vec = vecDartsPerVertex[m_map.phi1(d)];
thery's avatar
thery committed
465

Sylvain Thery's avatar
Sylvain Thery committed
466
			unsigned int embd = m_map.template getEmbedding<VERTEX>(d);
thery's avatar
thery committed
467 468 469
			Dart good_dart = NIL;
			for (typename std::vector<Dart>::iterator it = vec.begin(); it != vec.end() && good_dart == NIL; ++it)
			{
Sylvain Thery's avatar
Sylvain Thery committed
470
				if (m_map.template getEmbedding<VERTEX>(m_map.phi1(*it)) == embd)
thery's avatar
thery committed
471 472 473 474 475
					good_dart = *it;
			}

			if (good_dart != NIL)
			{
Sylvain Thery's avatar
Sylvain Thery committed
476 477 478 479 480
				if (good_dart == m_map.phi2(good_dart) && (d == m_map.phi2(d)))
					m_map.sewFaces(d, good_dart, false);
				else
					m_dirtyEdges.mark(d);
				mk.unmarkOrbit<EDGE>(d);
thery's avatar
thery committed
481 482 483
			}
			else
			{
Sylvain Thery's avatar
Sylvain Thery committed
484 485
				mk.unmark(d);
				m_dirtyEdges.mark(d);
thery's avatar
thery committed
486 487 488 489 490
				++nbBoundaryEdges;
			}
		}
	}

Sylvain Thery's avatar
Sylvain Thery committed
491
	TraversorV<typename PFP::MAP> tra(m_map);
thery's avatar
thery committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
	for (Dart d = tra.begin(); d != tra.end(); d = tra.next())
	{
		std::vector<Dart>& vec			= vecDartsPerVertex[d];
		std::vector<unsigned int>& vtc	= vecTCIndPerVertex[d];
		std::vector<unsigned int>& vn	= vecNormIndPerVertex[d];

		// test if normal vertex or multi-attrib vertex
		unsigned int nb = vtc.size();
		bool same=true;
		for (unsigned int j=1; (j<nb)&&(same); ++j)
		{
			if ( (vtc[j] != vtc[0]) || (vn[j] != vn[0]) )
				same = false;
		}

		// if not all faces the same embedding
		if (!same)
		{
			for (unsigned int j=0; j<nb; ++j)
			{
Sylvain Thery's avatar
Sylvain Thery committed
512 513 514
				Dart e = vec[j];
				m_texCoordsF[e] = texCoordsBuffer[ vecTCIndPerVertex[e][j] ];
				m_normalsF[e] = normalsBuffer[ vecNormIndPerVertex[e][j] ];
thery's avatar
thery committed
515
			}
Sylvain Thery's avatar
Sylvain Thery committed
516
			m_specialVertices.mark(d);
thery's avatar
thery committed
517 518 519
		}
		else
		{
Sylvain Thery's avatar
Sylvain Thery committed
520 521 522
			m_texCoords[d] = texCoordsBuffer[ vecTCIndPerVertex[d][0] ];
			m_normals[d] = normalsBuffer[ vecNormIndPerVertex[d][0] ];
			m_specialVertices.unmark(d);
thery's avatar
thery committed
523 524 525
		}
	}

Sylvain Thery's avatar
Sylvain Thery committed
526
	return true;
thery's avatar
thery committed
527 528 529 530 531

}



532
}
Pierre Kraemer's avatar
Pierre Kraemer committed
533 534 535
}
} // end namespaces
}