importObjTex.hpp 21.3 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
#include "Topology/generic/attributeHandler.h"
26
#include "Topology/generic/autoAttributeHandler.h"
Sylvain Thery's avatar
Sylvain Thery committed
27 28
#include "Container/fakeAttribute.h"
#include <fstream>
thery's avatar
thery committed
29

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

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

Sylvain Thery's avatar
Sylvain Thery committed
40

Sylvain Thery's avatar
Sylvain Thery committed
41 42
template <typename PFP>
OBJModel<PFP>::OBJModel(typename PFP::MAP& map):
43 44 45
	m_map(map),
	m_tagV(0),m_tagVT(0),m_tagVN(0),m_tagG(0),m_tagF(0),
	m_specialVertices(map),m_dirtyEdges(map)
Sylvain Thery's avatar
Sylvain Thery committed
46 47 48 49 50 51 52 53
{
}

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

Sylvain Thery's avatar
Sylvain Thery committed
55 56 57 58 59 60 61 62 63 64 65 66

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
67
{
Sylvain Thery's avatar
Sylvain Thery committed
68 69 70 71 72 73
	if (m_specialVertices.isMarked(d))
		return m_texCoordsF[d];
	return m_texCoords[d];
}


74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
template <typename PFP>
void OBJModel<PFP>::setPositionAttribute(VertexAttribute<Geom::Vec3f> position)
{
	m_positions = position;
}

template <typename PFP>
void OBJModel<PFP>::setNormalAttribute(VertexAttribute<Geom::Vec3f> normal)
{
	m_normals = normal;
}

template <typename PFP>
void OBJModel<PFP>::setTexCoordAttribute(VertexAttribute<Geom::Vec2f>texcoord)
{
	m_texCoords = texcoord;
}


Sylvain Thery's avatar
Sylvain Thery committed
93 94 95


template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
96
void OBJModel<PFP>::readMaterials(const std::string& filename, std::vector<MaterialOBJ>& materials)
Sylvain Thery's avatar
Sylvain Thery committed
97
{
Sylvain Thery's avatar
Sylvain Thery committed
98
	materials.reserve(m_materialNames.size());
Sylvain Thery's avatar
Sylvain Thery committed
99 100 101 102 103 104
		
	// open file
	std::ifstream fp(filename.c_str());
	if (!fp.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
Thery Sylvain's avatar
Thery Sylvain committed
105
		return ;
Sylvain Thery's avatar
Sylvain Thery committed
106 107 108
	}
	
	std::vector<MaterialOBJ>::iterator mit;
Sylvain Thery's avatar
Sylvain Thery committed
109

Sylvain Thery's avatar
Sylvain Thery committed
110 111 112 113 114 115 116 117
	std::string ligne;
	std::string tag;
	fp >> tag;
	do
	{
		std::getline (fp, ligne);
		if (tag == "newmtl")
		{
Sylvain Thery's avatar
Sylvain Thery committed
118 119

			std::map<std::string,int>::iterator it = m_materialNames.find(ligne);
Sylvain Thery's avatar
Sylvain Thery committed
120 121 122 123 124 125 126 127 128 129 130
			if (it ==  m_materialNames.end())
			{
				CGoGNerr << "Skipping material "<< ligne << CGoGNendl;
				do
				{
					fp >> tag;
					
				}while (!fp.eof() && (tag == "new mtl")); 
			}
			else
			{
Sylvain Thery's avatar
Sylvain Thery committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144
				CGoGNout << "Reading material "<< ligne << CGoGNendl;

				if (materials.empty())
				{
					materials.resize(1);
					mit = materials.begin();
				}
				else
				{
					materials.resize(materials.size()+1);
					++mit;
				}
				it->second = (mit-materials.begin());
				mit->name = ligne;
Sylvain Thery's avatar
Sylvain Thery committed
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
			}
		}
		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;
			}
Sylvain Thery's avatar
Sylvain Thery committed
173 174 175 176 177 178 179 180 181 182 183
			if (tag == "Tf")
			{
				oss >> mit->transparentFilter[0];
				oss >> mit->transparentFilter[1];
				oss >> mit->transparentFilter[2];
			}

			if ((tag == "Tr") || (tag == "d"))
			{
				oss >> mit->transparency;
			}
Sylvain Thery's avatar
Sylvain Thery committed
184 185 186 187 188 189 190
			
			if (tag == "map_Ka") // ambiant texture
			{
				CGoGNerr << tag << " not yet supported in OBJ material reading" << CGoGNendl;
			}
			if (tag == "map_Kd") // diffuse texture
			{
Sylvain Thery's avatar
Sylvain Thery committed
191 192 193 194 195 196
				mit->textureDiffuse = new Utils::Texture<2,Geom::Vec3uc>(GL_UNSIGNED_BYTE);
				std::string tname;
				oss >> tname;
				mit->textureDiffuse->load(tname);
				CGoGNout << "Loading texture "<< tname << CGoGNendl;

Sylvain Thery's avatar
Sylvain Thery committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
			}
			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>
218
bool OBJModel<PFP>::generateBrowsers(std::vector<ContainerBrowser*>& browsers)
Sylvain Thery's avatar
Sylvain Thery committed
219
{
220 221 222 223 224 225 226 227
//	browsers.clear();
	
//	if (m_groupNames.empty())
//		return false;
	
//	MapBrowserLinked* MBLptr = new MapBrowserLinked(m_map);
//	DartAttribute<Dart>& links = MBLptr->getLinkAttr();
//	browsers.push_back(MBLptr);
Sylvain Thery's avatar
Sylvain Thery committed
228
	
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
//	for (unsigned int i = 1; i<m_groupNames.size(); ++i)
//	{
//		MapBrowser* MBptr = new MapBrowserLinked(m_map,links);
//		browsers.push_back(MBptr);
//		m_groupMaterialID[i]= m_materialNames[m_groupMaterialNames[i]];
//	}
	
//	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
//		MapBrowserLinked* mb = static_cast<MapBrowserLinked*>(browsers[g]);
//		mb->pushBack(d);
//	}
//	return true;

	browsers.clear();
Sylvain Thery's avatar
Sylvain Thery committed
245 246
	if (m_groupNames.empty())
		return false;
247 248

	ContainerBrowserLinked* MBLptr = new ContainerBrowserLinked(m_map,DART);
Sylvain Thery's avatar
Sylvain Thery committed
249
	browsers.push_back(MBLptr);
250

Sylvain Thery's avatar
Sylvain Thery committed
251 252
	for (unsigned int i = 1; i<m_groupNames.size(); ++i)
	{
253
		ContainerBrowser* MBptr = new ContainerBrowserLinked(*MBLptr);
Sylvain Thery's avatar
Sylvain Thery committed
254
		browsers.push_back(MBptr);
Sylvain Thery's avatar
Sylvain Thery committed
255
		m_groupMaterialID[i]= m_materialNames[m_groupMaterialNames[i]];
Sylvain Thery's avatar
Sylvain Thery committed
256
	}
257

Sylvain Thery's avatar
Sylvain Thery committed
258 259 260
	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
261 262
		ContainerBrowserLinked* mb = static_cast<ContainerBrowserLinked*>(browsers[g]);
		mb->pushBack(d.index);
Sylvain Thery's avatar
Sylvain Thery committed
263 264 265 266 267 268 269 270 271 272 273 274 275
	}
	return true;
}




	
template <typename PFP>
short OBJModel<PFP>::readObjLine(std::stringstream& oss, std::vector<unsigned int>& indices)
{
	indices.clear();
	
thery's avatar
thery committed
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 308 309 310 311 312 313 314 315 316 317 318 319
	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;
}


320 321 322 323 324 325
template <typename PFP>
unsigned int OBJModel<PFP>::createSimpleVBO_P(Utils::VBO* positionVBO)
{
	TraversorF<typename PFP::MAP> traf(m_map);
	std::vector<Geom::Vec3f> posBuff;
	posBuff.reserve(16384);
Pierre Kraemer's avatar
Pierre Kraemer committed
326

327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	unsigned int nbtris = 0;
	for (Dart d=traf.begin(); d!= traf.end(); d = traf.next())
	{
		Dart e = m_map.phi1(d);
		Dart f = m_map.phi1(e);
		do
		{
			posBuff.push_back(m_positions[d]);
			posBuff.push_back(m_positions[e]);
			posBuff.push_back(m_positions[f]);
			e=f;
			f = m_map.phi1(e);
			nbtris++;
		}while (f!=d);
	}

	positionVBO->setDataSize(3);
	positionVBO->allocate(posBuff.size());
	Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
	memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
	positionVBO->releasePtr();

	return 3*nbtris;
}
Pierre Kraemer's avatar
Pierre Kraemer committed
351

352 353 354 355 356 357 358 359 360 361 362 363

template <typename PFP>
unsigned int OBJModel<PFP>::createSimpleVBO_PT(Utils::VBO* positionVBO, Utils::VBO* texcoordVBO)
{
	TraversorF<typename PFP::MAP> traf(m_map);
	std::vector<Geom::Vec3f> posBuff;
	std::vector<Geom::Vec2f> TCBuff;
	posBuff.reserve(16384);
	TCBuff.reserve(16384);

	unsigned int nbtris = 0;
	for (Dart d=traf.begin(); d!= traf.end(); d = traf.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
364
	{
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
		Dart e = m_map.phi1(d);
		Dart f = m_map.phi1(e);
		do
		{
			posBuff.push_back(m_positions[d]);
			if (m_specialVertices.isMarked(d))
				TCBuff.push_back(m_texCoordsF[d]);
			else
				TCBuff.push_back(m_texCoords[d]);

			posBuff.push_back(m_positions[e]);
			if (m_specialVertices.isMarked(e))
				TCBuff.push_back(m_texCoordsF[e]);
			else
				TCBuff.push_back(m_texCoords[e]);

			posBuff.push_back(m_positions[f]);
			if (m_specialVertices.isMarked(f))
				TCBuff.push_back(m_texCoordsF[f]);
			else
				TCBuff.push_back(m_texCoords[f]);
			e=f;
			f = m_map.phi1(e);
			nbtris++;
		}while (f!=d);
Pierre Kraemer's avatar
Pierre Kraemer committed
390 391
	}

392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
	positionVBO->setDataSize(3);
	positionVBO->allocate(posBuff.size());
	Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
	memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
	positionVBO->releasePtr();

	texcoordVBO->setDataSize(2);
	texcoordVBO->allocate(TCBuff.size());
	Geom::Vec2f* ptrTC = reinterpret_cast<Geom::Vec2f*>(texcoordVBO->lockPtr());
	memcpy(ptrTC,&TCBuff[0],TCBuff.size()*sizeof(Geom::Vec2f));
	texcoordVBO->releasePtr();

	return 3*nbtris;
}

template <typename PFP>
unsigned int OBJModel<PFP>::createSimpleVBO_PTN(Utils::VBO* positionVBO, Utils::VBO* texcoordVBO, Utils::VBO* normalVBO )
{
410 411 412 413 414 415 416 417 418 419 420 421
	if (!m_normals.isValid())
	{
		CGoGNerr << "no normal attribute "<< CGoGNendl;
		return 0;
	}
	if (!m_texCoords.isValid())
	{
		CGoGNerr << "no tex coords attribute "<< CGoGNendl;
		return 0;
	}


422 423 424
	TraversorF<typename PFP::MAP> traf(m_map);
	std::vector<Geom::Vec3f> posBuff;
	std::vector<Geom::Vec2f> TCBuff;
425
	std::vector<Geom::Vec3f> normalBuff;
426 427 428 429 430 431 432 433 434 435 436 437 438 439
	posBuff.reserve(16384);
	TCBuff.reserve(16384);
	normalBuff.reserve(16384);

	unsigned int nbtris = 0;
	for (Dart d=traf.begin(); d!= traf.end(); d = traf.next())
	{
		Dart e = m_map.phi1(d);
		Dart f = m_map.phi1(e);
		do
		{
			posBuff.push_back(m_positions[d]);
			if (m_specialVertices.isMarked(d))
			{
440 441 442 443 444 445 446 447
				if (hasTexCoords())
					TCBuff.push_back(m_texCoordsF[d]);
				else
					TCBuff.push_back(m_texCoords[d]);
				if (hasNormals())
					normalBuff.push_back(m_normalsF[d]);
				else
					normalBuff.push_back(m_normals[d]);
448 449 450 451 452 453 454 455 456 457
			}
			else
			{
				TCBuff.push_back(m_texCoords[d]);
				normalBuff.push_back(m_normals[d]);
			}

			posBuff.push_back(m_positions[e]);
			if (m_specialVertices.isMarked(e))
			{
458 459 460 461 462 463 464 465
				if (hasTexCoords())
					TCBuff.push_back(m_texCoordsF[e]);
				else
					TCBuff.push_back(m_texCoords[e]);
				if (hasNormals())
					normalBuff.push_back(m_normalsF[e]);
				else
					normalBuff.push_back(m_normals[e]);
466 467 468 469 470 471 472 473 474 475
			}
			else
			{
				TCBuff.push_back(m_texCoords[e]);
				normalBuff.push_back(m_normals[e]);
			}

			posBuff.push_back(m_positions[f]);
			if (m_specialVertices.isMarked(f))
			{
476 477 478 479 480 481 482 483
				if (hasTexCoords())
					TCBuff.push_back(m_texCoordsF[f]);
				else
					TCBuff.push_back(m_texCoords[f]);
				if (hasNormals())
					normalBuff.push_back(m_normalsF[f]);
				else
					normalBuff.push_back(m_normals[f]);
484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
			}
			else
			{
				TCBuff.push_back(m_texCoords[f]);
				normalBuff.push_back(m_normals[f]);
			}
			e=f;
			f = m_map.phi1(e);
			nbtris++;
		}while (f!=d);
	}

	positionVBO->setDataSize(3);
	positionVBO->allocate(posBuff.size());
	Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
	memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
	positionVBO->releasePtr();

	texcoordVBO->setDataSize(2);
	texcoordVBO->allocate(TCBuff.size());
	Geom::Vec2f* ptrTC = reinterpret_cast<Geom::Vec2f*>(texcoordVBO->lockPtr());
	memcpy(ptrTC,&TCBuff[0],TCBuff.size()*sizeof(Geom::Vec2f));
	texcoordVBO->releasePtr();

	normalVBO->setDataSize(3);
	normalVBO->allocate(normalBuff.size());
	Geom::Vec3f* ptrNormal = reinterpret_cast<Geom::Vec3f*>(normalVBO->lockPtr());
	memcpy(ptrNormal, &normalBuff[0], normalBuff.size()*sizeof(Geom::Vec3f));
	normalVBO->releasePtr();


	return 3*nbtris;
}


template <typename PFP>
unsigned int OBJModel<PFP>::createSimpleVBO_PN(Utils::VBO* positionVBO, Utils::VBO* normalVBO )
{
	TraversorF<typename PFP::MAP> traf(m_map);
	std::vector<Geom::Vec3f> posBuff;
	std::vector<Geom::Vec2f> normalBuff;
	posBuff.reserve(16384);
	normalBuff.reserve(16384);

	unsigned int nbtris = 0;
	for (Dart d=traf.begin(); d!= traf.end(); d = traf.next())
	{
		Dart e = m_map.phi1(d);
		Dart f = m_map.phi1(e);
		do
		{
			posBuff.push_back(m_positions[d]);
			if (m_specialVertices.isMarked(d))
			{
				normalBuff.push_back(m_normalsF[d]);
			}
			else
			{
				normalBuff.push_back(m_normals[d]);
			}

			posBuff.push_back(m_positions[e]);
			if (m_specialVertices.isMarked(e))
			{
				normalBuff.push_back(m_normalsF[e]);
			}
			else
			{
				normalBuff.push_back(m_normals[e]);
			}

			posBuff.push_back(m_positions[f]);
			if (m_specialVertices.isMarked(f))
			{
				normalBuff.push_back(m_normalsF[f]);
			}
			else
			{
				normalBuff.push_back(m_normals[f]);
			}
			e=f;
			f = m_map.phi1(e);
			nbtris++;
		}while (f!=d);
	}

	positionVBO->setDataSize(3);
	positionVBO->allocate(posBuff.size());
	Geom::Vec3f* ptrPos = reinterpret_cast<Geom::Vec3f*>(positionVBO->lockPtr());
	memcpy(ptrPos,&posBuff[0],posBuff.size()*sizeof(Geom::Vec3f));
	positionVBO->releasePtr();

	normalVBO->setDataSize(3);
	normalVBO->allocate(normalBuff.size());
	Geom::Vec3f* ptrNormal = reinterpret_cast<Geom::Vec3f*>(normalVBO->lockPtr());
	memcpy(ptrNormal, &normalBuff[0], normalBuff.size()*sizeof(Geom::Vec3f));
	normalVBO->releasePtr();


	return 3*nbtris;
}


Sylvain Thery's avatar
Sylvain Thery committed
587 588 589 590 591
// 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
592
template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
593
bool OBJModel<PFP>::import( const std::string& filename, std::vector<std::string>& attrNames)
thery's avatar
thery committed
594 595 596 597 598 599
{
	typedef  typename PFP::VEC3 VEC3;
	typedef Geom::Vec2f VEC2;

	attrNames.clear();
	// open file
Sylvain Thery's avatar
Sylvain Thery committed
600
	std::ifstream fp(filename.c_str()/*, std::ios::binary*/);
thery's avatar
thery committed
601 602 603 604 605 606 607 608 609 610 611 612 613
	if (!fp.good())
	{
		CGoGNerr << "Unable to open file " << filename << CGoGNendl;
		return false;
	}

	std::string ligne;
	std::string tag;
	do
	{
		fp >> tag;
		std::getline (fp, ligne);
		if (tag == "v")
614
			m_tagV++;
thery's avatar
thery committed
615
		if (tag == "vn")
616
			m_tagVN++;
thery's avatar
thery committed
617
		if (tag == "vt")
618
			m_tagVT++;
thery's avatar
thery committed
619
		if (tag == "g")
620
			m_tagG++;
thery's avatar
thery committed
621
		if (tag == "f")
622
			m_tagF++;
thery's avatar
thery committed
623 624
	}while (!fp.eof());

625 626 627 628 629 630
	std::cout << "Parsing OBJ"<< m_tagV<< std::endl;
	std::cout << "Vertices:"<< m_tagV<< std::endl;
	std::cout << "Normals:"<< m_tagVN<< std::endl;
	std::cout << "TexCoords:"<< m_tagVT<< std::endl;
	std::cout << "Groups:"<< m_tagG<< std::endl;
	std::cout << "Faces:"<< m_tagF<< std::endl;
thery's avatar
thery committed
631 632


Sylvain Thery's avatar
Sylvain Thery committed
633 634 635 636
	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
637

638
	if (m_tagVT != 0)
thery's avatar
thery committed
639
	{
Sylvain Thery's avatar
Sylvain Thery committed
640 641 642 643 644 645 646 647
		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
648 649
	}

650
	if (m_tagVN != 0)
thery's avatar
thery committed
651
	{
Sylvain Thery's avatar
Sylvain Thery committed
652 653 654 655 656 657 658 659
		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
660 661
	}

Sylvain Thery's avatar
Sylvain Thery committed
662
	
663
	if (m_tagG != 0)
Sylvain Thery's avatar
Sylvain Thery committed
664 665 666 667
	{
		m_groups =  m_map.template getAttribute<unsigned int, FACE>("groups") ;
		if (!m_groups.isValid())
			m_groups = m_map.template addAttribute<unsigned int, FACE>("groups") ;
668
		attrNames.push_back(m_groups.name()) ;
Sylvain Thery's avatar
Sylvain Thery committed
669 670
	}
	
thery's avatar
thery committed
671 672 673 674 675 676 677 678

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

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

	std::vector<VEC3> normalsBuffer;
679
	normalsBuffer.reserve(m_tagVN);
Sylvain Thery's avatar
Sylvain Thery committed
680
	
thery's avatar
thery committed
681
	std::vector<VEC2> texCoordsBuffer;
682
	texCoordsBuffer.reserve(m_tagVT);
Sylvain Thery's avatar
Sylvain Thery committed
683
	
thery's avatar
thery committed
684
	std::vector<unsigned int> verticesID;
685
	verticesID.reserve(m_tagV);
Sylvain Thery's avatar
Sylvain Thery committed
686 687
	
	std::vector<unsigned int> normalsID;
688
	normalsID.reserve(m_tagV);
thery's avatar
thery committed
689
	std::vector<unsigned int> texCoordsID;
690
	texCoordsID.reserve(m_tagV);
Sylvain Thery's avatar
Sylvain Thery committed
691 692
	
	
thery's avatar
thery committed
693 694 695

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

698 699 700
	VertexAutoAttribute< NoTypeNameAttribute< std::vector<Dart> > > vecDartsPerVertex(m_map, "incidents");
	VertexAutoAttribute< NoTypeNameAttribute< std::vector<unsigned int> > > vecNormIndPerVertex(m_map, "incidentsN");
	VertexAutoAttribute< NoTypeNameAttribute< std::vector<unsigned int> > > vecTCIndPerVertex(m_map, "incidentsTC");
thery's avatar
thery committed
701

Sylvain Thery's avatar
Sylvain Thery committed
702
	unsigned int currentGroup = 0;
thery's avatar
thery committed
703

Sylvain Thery's avatar
Sylvain Thery committed
704
	DartMarkerNoUnmark mk(m_map) ;
thery's avatar
thery committed
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
	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
722
			m_positions[id] = pos;
thery's avatar
thery committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

			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
745
			texCoordsBuffer.push_back(tc);
thery's avatar
thery committed
746 747 748 749
		}

		if (tag == std::string("g"))
		{
Sylvain Thery's avatar
Sylvain Thery committed
750 751 752 753 754 755 756 757
			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;
Sylvain Thery's avatar
Sylvain Thery committed
758 759
			m_materialNames.insert(std::pair<std::string,int>(buf,-1));
			m_groupMaterialNames.push_back(buf);
Sylvain Thery's avatar
Sylvain Thery committed
760
			currentGroup++;
thery's avatar
thery committed
761 762 763 764 765 766 767 768
		}

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

			short nbe = readObjLine(oss,localIndices);

Sylvain Thery's avatar
Sylvain Thery committed
769
			Dart d = m_map.newFace(nbe, false);
770
			if (m_tagG!=0)
771
				m_groups[d] = currentGroup;
Sylvain Thery's avatar
Sylvain Thery committed
772
			
thery's avatar
thery committed
773 774 775 776
			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
777 778
				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
779 780 781
				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
782
				d = m_map.phi1(d);
thery's avatar
thery committed
783 784 785 786 787
			}
		}
		fp >> tag;
		std::getline(fp, ligne);
	} while (!fp.eof());
Sylvain Thery's avatar
Sylvain Thery committed
788
	fp.close ();
thery's avatar
thery committed
789 790 791

	// reconstruct neighbourhood
	unsigned int nbBoundaryEdges = 0;
Sylvain Thery's avatar
Sylvain Thery committed
792
	for (Dart d = m_map.begin(); d != m_map.end(); m_map.next(d))
thery's avatar
thery committed
793
	{
Sylvain Thery's avatar
Sylvain Thery committed
794
		if (mk.isMarked(d))
thery's avatar
thery committed
795 796
		{
			// darts incident to end vertex of edge
Sylvain Thery's avatar
Sylvain Thery committed
797
			std::vector<Dart>& vec = vecDartsPerVertex[m_map.phi1(d)];
thery's avatar
thery committed
798

Sylvain Thery's avatar
Sylvain Thery committed
799
			unsigned int embd = m_map.template getEmbedding<VERTEX>(d);
thery's avatar
thery committed
800 801 802
			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
803
				if (m_map.template getEmbedding<VERTEX>(m_map.phi1(*it)) == embd)
thery's avatar
thery committed
804 805 806 807 808
					good_dart = *it;
			}

			if (good_dart != NIL)
			{
Sylvain Thery's avatar
Sylvain Thery committed
809 810 811 812 813
				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
814 815 816
			}
			else
			{
Sylvain Thery's avatar
Sylvain Thery committed
817 818
				mk.unmark(d);
				m_dirtyEdges.mark(d);
thery's avatar
thery committed
819 820 821 822 823
				++nbBoundaryEdges;
			}
		}
	}

Sylvain Thery's avatar
Sylvain Thery committed
824
	TraversorV<typename PFP::MAP> tra(m_map);
thery's avatar
thery committed
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
	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
841 842
//		if (!same)
//		{
thery's avatar
thery committed
843 844
			for (unsigned int j=0; j<nb; ++j)
			{
Sylvain Thery's avatar
Sylvain Thery committed
845
				Dart e = vec[j];
846
				if (m_tagVT)
847
					m_texCoordsF[e] = texCoordsBuffer[ vecTCIndPerVertex[e][j] ];
848
				if (m_tagVN)
849
					m_normalsF[e] = normalsBuffer[ vecNormIndPerVertex[e][j] ];
thery's avatar
thery committed
850
			}
Sylvain Thery's avatar
Sylvain Thery committed
851
			m_specialVertices.mark(d);
852 853 854 855 856 857 858 859 860
//		}
//		else
//		{
//			if (tagVT)
//				m_texCoords[d] = texCoordsBuffer[ vecTCIndPerVertex[d][0] ];
//			if (tagVN)
//				m_normals[d] = normalsBuffer[ vecNormIndPerVertex[d][0] ];
//			m_specialVertices.unmark(d);
//		}
thery's avatar
thery committed
861 862
	}

Sylvain Thery's avatar
Sylvain Thery committed
863
	return true;
thery's avatar
thery committed
864 865 866 867 868

}



869
}
Pierre Kraemer's avatar
Pierre Kraemer committed
870 871 872
}
} // end namespaces
}