collector.hpp 25.1 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* 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                                        *
*                                                                              *
*******************************************************************************/

25 26
#include "Topology/generic/traversor2.h"
#include "Algo/Geometry/intersection.h"
27
#include <queue>
28

Pierre Kraemer's avatar
Pierre Kraemer committed
29 30 31
namespace CGoGN
{

32 33 34
namespace Algo
{

Pierre Kraemer's avatar
Pierre Kraemer committed
35 36 37
namespace Selection
{

38 39 40 41
/*********************************************************
 * Generic Collector
 *********************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
42
template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
43
Collector<PFP>::Collector(typename PFP::MAP& m, unsigned int thread) : map(m), m_thread(thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
44 45
{}

46
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
47
inline bool Collector<PFP>::applyOnInsideVertices(FunctorType& f)
48 49 50 51 52 53 54
{
	for(std::vector<Dart>::iterator iv = insideVertices.begin(); iv != insideVertices.end(); ++iv)
		if(f(*iv))
			return true ;
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
template <typename PFP>
inline bool Collector<PFP>::applyOnInsideEdges(FunctorType& f)
{
	for(std::vector<Dart>::iterator iv = insideEdges.begin(); iv != insideEdges.end(); ++iv)
		if(f(*iv))
			return true ;
	return false ;
}

template <typename PFP>
inline bool Collector<PFP>::applyOnInsideFaces(FunctorType& f)
{
	for(std::vector<Dart>::iterator iv = insideFaces.begin(); iv != insideFaces.end(); ++iv)
		if(f(*iv))
			return true ;
	return false ;
}

template <typename PFP>
inline bool Collector<PFP>::applyOnBorder(FunctorType& f)
{
	for(std::vector<Dart>::iterator iv = border.begin(); iv != border.end(); ++iv)
		if(f(*iv))
			return true ;
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
82 83 84 85 86
template <typename PPFP>
std::ostream& operator<<(std::ostream &out, const Collector<PPFP>& c)
{
    out << "Collector around " << c.centerDart << std::endl;
    out << "insideVertices (" << c.insideVertices.size() << ") = {";
Pierre Kraemer's avatar
Pierre Kraemer committed
87
    for (unsigned int i = 0; i< c.insideVertices.size(); ++i) out << c.insideVertices[i] << " ";
Pierre Kraemer's avatar
Pierre Kraemer committed
88 89
    out << "}" << std::endl ;
    out << "insideEdges (" << c.insideEdges.size() << ") = {";
Pierre Kraemer's avatar
Pierre Kraemer committed
90
    for (unsigned int i = 0; i< c.insideEdges.size(); ++i) out << c.insideEdges[i] << " ";
Pierre Kraemer's avatar
Pierre Kraemer committed
91 92
    out << "}" << std::endl ;
    out << "insideFaces (" << c.insideFaces.size() << ") = {";
Pierre Kraemer's avatar
Pierre Kraemer committed
93
    for (unsigned int i = 0; i< c.insideFaces.size(); ++i) out << c.insideFaces[i] << " ";
Pierre Kraemer's avatar
Pierre Kraemer committed
94 95
    out << "}" << std::endl ;
    out << "border (" << c.border.size() << ") = {";
Pierre Kraemer's avatar
Pierre Kraemer committed
96
    for (unsigned int i = 0; i< c.border.size(); ++i) out << c.border[i] << " ";
Pierre Kraemer's avatar
Pierre Kraemer committed
97 98 99 100 101 102 103 104 105
    out << "}" << std::endl;
    return out;
}

/*********************************************************
 * Collector One Ring
 *********************************************************/

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
106
void Collector_OneRing<PFP>::collectAll(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
107
{
Pierre Kraemer's avatar
Pierre Kraemer committed
108
	this->init(d);
109 110 111
	this->insideEdges.reserve(16);
	this->insideFaces.reserve(16);
	this->border.reserve(16);
Pierre Kraemer's avatar
Pierre Kraemer committed
112

113 114 115 116 117 118 119 120
	this->insideVertices.push_back(this->centerDart);

	Traversor2VE<typename PFP::MAP> te(this->map, this->centerDart) ;
	for(Dart it = te.begin(); it != te.end(); it = te.next())
		this->insideEdges.push_back(it);

	Traversor2VF<typename PFP::MAP> tf(this->map, this->centerDart) ;
	for(Dart it = tf.begin(); it != tf.end(); it = tf.next())
Pierre Kraemer's avatar
Pierre Kraemer committed
121
	{
122 123 124
		this->insideFaces.push_back(it);
		this->border.push_back(this->map.phi1(it));
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
125 126 127
}

template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
128
void Collector_OneRing<PFP>::collectBorder(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
129
{
Pierre Kraemer's avatar
Pierre Kraemer committed
130 131 132
	this->init(d);
	this->border.reserve(12);

133 134 135
	Traversor2VF<typename PFP::MAP> t(this->map, this->centerDart) ;
	for(Dart it = t.begin(); it != t.end(); it = t.next())
		this->border.push_back(this->map.phi1(it));
Pierre Kraemer's avatar
Pierre Kraemer committed
136 137
}

Pierre Kraemer's avatar
Pierre Kraemer committed
138 139 140 141
/*********************************************************
 * Collector Within Sphere
 *********************************************************/

Pierre Kraemer's avatar
Pierre Kraemer committed
142
template <typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
143
void Collector_WithinSphere<PFP>::collectAll(Dart d)
Pierre Kraemer's avatar
Pierre Kraemer committed
144 145 146 147
{
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

Pierre Kraemer's avatar
Pierre Kraemer committed
148
	this->init(d);
149 150 151
	this->insideEdges.reserve(32);
	this->insideFaces.reserve(32);
	this->border.reserve(32);
Pierre Kraemer's avatar
Pierre Kraemer committed
152

153 154 155
	CellMarkerStore<VERTEX> vm(this->map, this->m_thread);	// mark the collected inside-vertices
	CellMarkerStore<EDGE> em(this->map, this->m_thread);	// mark the collected inside-edges + border-edges
	CellMarkerStore<FACE> fm(this->map, this->m_thread);	// mark the collected inside-faces + border-faces
Pierre Kraemer's avatar
Pierre Kraemer committed
156 157 158 159

	this->insideVertices.push_back(this->centerDart);
	vm.mark(this->centerDart);

Pierre Kraemer's avatar
Pierre Kraemer committed
160
	VEC3 centerPosition = this->position[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
161
	unsigned int i = 0;
162
//	for(std::vector<Dart>::iterator iv = this->insideVertices.begin(); iv != this->insideVertices.end(); ++iv)
Pierre Kraemer's avatar
Pierre Kraemer committed
163 164 165 166 167 168 169 170 171 172 173
	while (i < this->insideVertices.size())
	{
		Dart end = this->insideVertices[i];
		Dart e = end;
		do
		{
			if (! em.isMarked(e) || ! fm.isMarked(e)) // are both tests useful ?
			{
				const Dart f = this->map.phi1(e);
				const Dart g = this->map.phi1(f);

174
				if (! Geom::isPointInSphere(this->position[f], centerPosition, this->radius))
Pierre Kraemer's avatar
Pierre Kraemer committed
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
				{
					this->border.push_back(e); // add to border
					em.mark(e);
					fm.mark(e); // is it useful ?
				}
				else
				{
					if (! vm.isMarked(f))
					{
						this->insideVertices.push_back(f);
						vm.mark(f);
					}
					if (! em.isMarked(e))
					{
						this->insideEdges.push_back(e);
						em.mark(e);
					}
192
					if (! fm.isMarked(e) && Geom::isPointInSphere(this->position[g], centerPosition, this->radius))
Pierre Kraemer's avatar
Pierre Kraemer committed
193 194 195 196 197 198
					{
						this->insideFaces.push_back(e);
						fm.mark(e);
					}
				}
			}
199
			e = this->map.phi2_1(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
200 201 202 203 204
		} while (e != end);
		++i;
	}
}

Pierre Kraemer's avatar
Pierre Kraemer committed
205 206 207 208 209 210 211
template <typename PFP>
void Collector_WithinSphere<PFP>::collectBorder(Dart d)
{
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

	this->init(d);
212 213
	this->border.reserve(128);
	this->insideVertices.reserve(128);
Pierre Kraemer's avatar
Pierre Kraemer committed
214

215 216
	CellMarkerStore<VERTEX> vm(this->map, this->m_thread);	// mark the collected inside-vertices
	CellMarkerStore<EDGE> em(this->map, this->m_thread);	// mark the collected inside-edges + border-edges
217 218 219

	this->insideVertices.push_back(this->centerDart);
	vm.mark(this->centerDart);
Pierre Kraemer's avatar
Pierre Kraemer committed
220 221 222 223 224 225 226 227 228

	VEC3 centerPosition = this->position[d];
	unsigned int i = 0;
	while (i < this->insideVertices.size())
	{
		Dart end = this->insideVertices[i];
		Dart e = end;
		do
		{
229
			if ( ! em.isMarked(e) )
Pierre Kraemer's avatar
Pierre Kraemer committed
230 231 232 233 234 235 236
			{
				const Dart f = this->map.phi1(e);

				if (! Geom::isPointInSphere(this->position[f], centerPosition, this->radius))
				{
					this->border.push_back(e); // add to border
				}
237 238 239 240 241 242 243 244 245
				else
				{
					if (! vm.isMarked(f))
					{
						this->insideVertices.push_back(f);
						vm.mark(f);
					}
				}
				em.mark(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
246
			}
247
			e = this->map.phi2_1(e);
Pierre Kraemer's avatar
Pierre Kraemer committed
248 249 250
		} while (e != end);
		++i;
	}
251
	this->insideVertices.clear();
Pierre Kraemer's avatar
Pierre Kraemer committed
252 253
}

Pierre Kraemer's avatar
Pierre Kraemer committed
254 255 256
template <typename PFP>
void Collector_WithinSphere<PFP>::computeArea()
{
Pierre Kraemer's avatar
Pierre Kraemer committed
257 258 259
	area = 0;
	typename PFP::VEC3 centerPosition = this->position[this->centerDart];

Pierre Kraemer's avatar
Pierre Kraemer committed
260
	for (std::vector<Dart>::const_iterator it = this->insideFaces.begin(); it != this->insideFaces.end(); ++it)
261
		area += Algo::Geometry::triangleArea<PFP>(this->map, *it, this->position);
Pierre Kraemer's avatar
Pierre Kraemer committed
262 263 264 265 266

	for (std::vector<Dart>::const_iterator it = this->border.begin(); it != this->border.end(); ++it)
	{
		const Dart f = this->map.phi1(*it); // we know that f is outside
		const Dart g = this->map.phi1(f);
267
		if (Geom::isPointInSphere(this->position[g], centerPosition, this->radius))
Pierre Kraemer's avatar
Pierre Kraemer committed
268
		{ // only f is outside
269 270 271 272
			typename PFP::REAL alpha, beta;
			Algo::Geometry::intersectionSphereEdge<PFP>(this->map, centerPosition, this->radius, *it, this->position, alpha);
			Algo::Geometry::intersectionSphereEdge<PFP>(this->map, centerPosition, this->radius, this->map.phi2(f), this->position, beta);
			area += (alpha+beta - alpha*beta) * Algo::Geometry::triangleArea<PFP>(this->map, *it, this->position);
Pierre Kraemer's avatar
Pierre Kraemer committed
273 274 275
		}
		else
		{ // f and g are outside
276 277 278 279
			typename PFP::REAL alpha, beta;
			Algo::Geometry::intersectionSphereEdge<PFP>(this->map, centerPosition, this->radius, *it, this->position, alpha);
			Algo::Geometry::intersectionSphereEdge<PFP>(this->map, centerPosition, this->radius, this->map.phi2(g), this->position, beta);
			area += alpha * beta * Algo::Geometry::triangleArea<PFP>(this->map, *it, this->position);
Pierre Kraemer's avatar
Pierre Kraemer committed
280 281 282 283
		}
	}
}

284
/*********************************************************
285
 * Collector Normal Angle (Vertices)
286 287 288 289 290 291 292 293 294 295 296 297 298
 *********************************************************/

template <typename PFP>
void Collector_NormalAngle<PFP>::collectAll(Dart d)
{
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

	this->init(d);
	this->insideEdges.reserve(32);
	this->insideFaces.reserve(32);
	this->border.reserve(32);

299 300 301
	CellMarkerStore<VERTEX> vm(this->map, this->m_thread);	// mark the collected inside-vertices
	CellMarkerStore<EDGE> em(this->map, this->m_thread);	// mark the collected inside-edges + border-edges
	CellMarkerStore<FACE> fm(this->map, this->m_thread);	// mark the collected inside-faces + border-faces
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 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 351 352 353 354 355 356 357 358 359 360 361 362 363

	this->insideVertices.push_back(this->centerDart);
	vm.mark(this->centerDart);

	VEC3 centerNormal = this->normal[d];
	unsigned int i = 0;
	while (i < this->insideVertices.size())
	{
		Dart end = this->insideVertices[i];
		Dart e = end;
		do
		{
			if (! em.isMarked(e) || ! fm.isMarked(e)) // are both tests useful ?
			{
				const Dart f = this->map.phi1(e);
				const Dart g = this->map.phi1(f);

				REAL a = Geom::angle(centerNormal, this->normal[f]);

				if (a > angleThreshold)
				{
					this->border.push_back(e); // add to border
					em.mark(e);
					fm.mark(e); // is it useful ?
				}
				else
				{
					if (! vm.isMarked(f))
					{
						this->insideVertices.push_back(f);
						vm.mark(f);
					}
					if (! em.isMarked(e))
					{
						this->insideEdges.push_back(e);
						em.mark(e);
					}

					REAL b = Geom::angle(centerNormal, this->normal[g]);
					if (! fm.isMarked(e) && b < angleThreshold)
					{
						this->insideFaces.push_back(e);
						fm.mark(e);
					}
				}
			}
			e = this->map.phi2_1(e);
		} while (e != end);
		++i;
	}
}

template <typename PFP>
void Collector_NormalAngle<PFP>::collectBorder(Dart d)
{
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

	this->init(d);
	this->border.reserve(128);
	this->insideVertices.reserve(128);

364 365
	CellMarkerStore<VERTEX> vm(this->map, this->m_thread);	// mark the collected inside-vertices
	CellMarkerStore<EDGE> em(this->map, this->m_thread);	// mark the collected inside-edges + border-edges
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404

	this->insideVertices.push_back(this->centerDart);
	vm.mark(this->centerDart);

	VEC3 centerNormal = this->normal[d];
	unsigned int i = 0;
	while (i < this->insideVertices.size())
	{
		Dart end = this->insideVertices[i];
		Dart e = end;
		do
		{
			if ( ! em.isMarked(e) )
			{
				const Dart f = this->map.phi1(e);

				REAL a = Geom::angle(centerNormal, this->normal[f]);

				if (a > angleThreshold)
				{
					this->border.push_back(e); // add to border
				}
				else
				{
					if (! vm.isMarked(f))
					{
						this->insideVertices.push_back(f);
						vm.mark(f);
					}
				}
				em.mark(e);
			}
			e = this->map.phi2_1(e);
		} while (e != end);
		++i;
	}
	this->insideVertices.clear();
}

405 406 407 408 409 410 411 412 413 414 415 416

/*********************************************************
 * Collector Normal Angle (Triangles)
 *********************************************************/

template <typename PFP>
void Collector_NormalAngle_Triangles<PFP>::collectAll(Dart d)
{
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

	this->init(d);
Basile Sauvage's avatar
Basile Sauvage committed
417
	this->insideVertices.reserve(32);
418 419 420 421
	this->insideEdges.reserve(32);
	this->insideFaces.reserve(32);
	this->border.reserve(32);

422 423
	CellMarkerStore<FACE> fm(this->map, this->m_thread);	// mark the collected inside-faces + front-faces
	CellMarkerStore<FACE> fminside(this->map, this->m_thread);	// mark the collected inside-faces
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452

	std::queue<Dart> front;
	front.push(this->centerDart);
	fm.mark(this->centerDart);
	VEC3 centerNormal = this->normal[this->centerDart];

	while ( !front.empty() ) // collect inside faces
	{
		Dart f = front.front();
		front.pop();
		REAL a = Geom::angle(centerNormal, this->normal[f]);


		if (a < angleThreshold )
		{ // collect this face and add adjacent faces to the front
			this->insideFaces.push_back(f);
			fminside.mark(f);
			Traversor2FFaE<typename PFP::MAP> t (this->map, f) ;
			for (Dart it = t.begin(); it != t.end(); it=t.next())
			{
				if (!fm.isMarked(it))
				{
					front.push(it);
					fm.mark(it);
				}
			}
		}
	}

453 454
	CellMarkerStore<VERTEX> vm(this->map, this->m_thread);	// mark inside-vertices and border-vertices
	CellMarkerStore<EDGE> em(this->map, this->m_thread);	// mark inside-edges and border-edges
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
	std::vector<Dart>::iterator f_it;
	for (f_it = this->insideFaces.begin(); f_it != this->insideFaces.end(); f_it++)
	{ // collect insideVertices, insideEdges, and border
		Traversor2FE<typename PFP::MAP> te (this->map, *f_it) ;
		for (Dart it = te.begin(); it != te.end(); it=te.next())
		{ // collect insideEdges and border
			if (!em.isMarked(it))
			{
				em.mark(it);
				if (this->map.isBoundaryEdge(it))
					this->border.push_back(it);
				else if ( fminside.isMarked(it) && fminside.isMarked(this->map.phi2(it)) )
					this->insideEdges.push_back(it);
				else
					this->border.push_back(it);
			}
		}

		Traversor2FV<typename PFP::MAP> tv (this->map, *f_it) ;
		for (Dart it = tv.begin(); it != tv.end(); it=tv.next())
		{ // collect insideVertices
			if (!vm.isMarked(it))
			{
				vm.mark(it);
479 480 481 482 483 484 485 486 487 488
				this->insideVertices.push_back(it);
//				if ( !this->map.isBoundaryVertex(it))
//				{
//					Traversor2VF<typename PFP::MAP> tf (this->map, it);
//					Dart it2 = tf.begin();
//					while ( (it2 != tf.end()) && fminside.isMarked(it2))
//						it2=tf.next();
//					if (it2 == tf.end())
//						this->insideVertices.push_back(it);
//				}
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
			}
		}
	}

}

template <typename PFP>
void Collector_NormalAngle_Triangles<PFP>::collectBorder(Dart d)
{
	typedef typename PFP::VEC3 VEC3;
	typedef typename PFP::REAL REAL;

	this->init(d);
	this->insideFaces.reserve(32);
	this->border.reserve(32);

505 506
	CellMarkerStore<FACE> fm(this->map, this->m_thread);	// mark the collected inside-faces + front-faces
	CellMarkerStore<FACE> fminside(this->map, this->m_thread);	// mark the collected inside-faces
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

	std::queue<Dart> front;
	front.push(this->centerDart);
	fm.mark(this->centerDart);
	VEC3 centerNormal = this->normal[this->centerDart];

	while ( !front.empty() ) // collect inside faces
	{
		Dart f = front.front();
		front.pop();
		REAL a = Geom::angle(centerNormal, this->normal[f]);


		if (a < angleThreshold )
		{ // collect this face and add adjacent faces to the front
			this->insideFaces.push_back(f);
			fminside.mark(f);
			Traversor2FFaE<typename PFP::MAP> t (this->map, f) ;
			for (Dart it = t.begin(); it != t.end(); it=t.next())
			{
				if (!fm.isMarked(it))
				{
					front.push(it);
					fm.mark(it);
				}
			}
		}
	}

536
	CellMarkerStore<EDGE> em(this->map, this->m_thread);	// mark inside-edges and border-edges
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
	std::vector<Dart>::iterator f_it;
	for (f_it = this->insideFaces.begin(); f_it != this->insideFaces.end(); f_it++)
	{ // collect border (edges)
		Traversor2FE<typename PFP::MAP> te (this->map, *f_it) ;
		for (Dart it = te.begin(); it != te.end(); it=te.next())
		{
			if (!em.isMarked(it))
			{
				em.mark(it);
				if (this->map.isBoundaryEdge(it))
					this->border.push_back(it);
				else if ( !fminside.isMarked(it) || !fminside.isMarked(this->map.phi2(it)) )
					this->border.push_back(it);
			}
		}
	}
	this->insideFaces.clear();
}

Basile Sauvage's avatar
Basile Sauvage committed
556 557 558 559 560
/*********************************************************
 * Collector Dijkstra
 *********************************************************/

template <typename PFP>
561 562
void Collector_Dijkstra<PFP>::collectAll(Dart dinit)
{
Basile Sauvage's avatar
Basile Sauvage committed
563 564
	init(dinit);

565
	CellMarkerStore<VERTEX> vmReached (this->map, this->m_thread);
Basile Sauvage's avatar
Basile Sauvage committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
	vertexInfo[this->centerDart].it = front.insert(std::pair<float,Dart>(0.0, this->centerDart));
	vertexInfo[this->centerDart].valid = true;
	vmReached.mark(this->centerDart);

	while ( !front.empty() && front.begin()->first < this->maxDist)
	{
		Dart e = front.begin()->second;
		float d = front.begin()->first;
		front.erase(vertexInfo[e].it);
		vertexInfo[e].valid=false;
		this->insideVertices.push_back(e);

		Traversor2VVaE<typename PFP::MAP> tv (this->map, e);
		for (Dart f = tv.begin(); f != tv.end(); f=tv.next())
		{
			VertexInfo& vi (vertexInfo[f]);
			if (vmReached.isMarked(f))
			{
584
				if (vi.valid) // probably useless (because of distance test) but faster
Basile Sauvage's avatar
Basile Sauvage committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
				{
					float dist = d + edgeLength(f);
					if (dist < vi.it->first)
					{
						front.erase(vi.it);
						vi.it = front.insert(std::pair<float,Dart>(dist, f));
					}
				}
			}
			else
			{
				vi.it = front.insert(std::pair<float,Dart>(d + edgeLength(f), f));
				vi.valid=true;
				vmReached.mark(f);
			}

		}

	}

605 606 607 608 609 610 611
	while ( !front.empty())
	{
		vmReached.unmark(front.begin()->second);
		front.erase(front.begin());
	}


612 613
	CellMarkerStore<EDGE> em (this->map, this->m_thread);
	CellMarkerStore<FACE> fm (this->map, this->m_thread);
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
	for (std::vector<Dart>::iterator e_it = this->insideVertices.begin(); e_it != this->insideVertices.end() ; e_it++)
	{
		// collect insideEdges
		Traversor2VE<typename PFP::MAP> te (this->map, *e_it);
		for (Dart e = te.begin(); e != te.end(); e=te.next())
		{
			if ( !em.isMarked(e) && vmReached.isMarked(this->map.phi2(e)) )
			{ // opposite vertex is inside -> inside edge
				this->insideEdges.push_back(e);
				em.mark(e);
			}
		}

		// collect insideFaces and border
		Traversor2VF<typename PFP::MAP> tf (this->map, *e_it);
		for (Dart f = tf.begin(); f != tf.end(); f=tf.next())
		{
			if ( !fm.isMarked(f) )
			{
				fm.mark(f);
				Traversor2FV<typename PFP::MAP> tv (this->map, f);
				Dart v = tv.begin();
				while ( v != tv.end() && vmReached.isMarked(v) ) {v=tv.next();}
				if ( v == tv.end() )
					this->insideFaces.push_back(f);
				else
					this->border.push_back(f);
			}
		}
	}
Basile Sauvage's avatar
Basile Sauvage committed
644 645 646
}

template <typename PFP>
647 648
void Collector_Dijkstra<PFP>::collectBorder(Dart dinit)
{
649
	init(dinit);
Basile Sauvage's avatar
Basile Sauvage committed
650

651
	CellMarkerStore<VERTEX> vmReached (this->map, this->m_thread);
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
	vertexInfo[this->centerDart].it = front.insert(std::pair<float,Dart>(0.0, this->centerDart));
	vertexInfo[this->centerDart].valid = true;
	vmReached.mark(this->centerDart);

	while ( !front.empty() && front.begin()->first < this->maxDist)
	{
		Dart e = front.begin()->second;
		float d = front.begin()->first;
		front.erase(vertexInfo[e].it);
		vertexInfo[e].valid=false;
		this->insideVertices.push_back(e);

		Traversor2VVaE<typename PFP::MAP> tv (this->map, e);
		for (Dart f = tv.begin(); f != tv.end(); f=tv.next())
		{
			VertexInfo& vi (vertexInfo[f]);
			if (vmReached.isMarked(f))
			{
				if (vi.valid) // probably useless (because of distance test) but faster
				{
					float dist = d + edgeLength(f);
					if (dist < vi.it->first)
					{
						front.erase(vi.it);
						vi.it = front.insert(std::pair<float,Dart>(dist, f));
					}
				}
			}
			else
			{
				vi.it = front.insert(std::pair<float,Dart>(d + edgeLength(f), f));
				vi.valid=true;
				vmReached.mark(f);
			}

		}

	}

	while ( !front.empty())
	{
		vmReached.unmark(front.begin()->second);
		front.erase(front.begin());
	}


698
	CellMarkerStore<FACE> fm (this->map, this->m_thread);
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
	for (std::vector<Dart>::iterator e_it = this->insideVertices.begin(); e_it != this->insideVertices.end() ; e_it++)
	{
		// collect border
		Traversor2VF<typename PFP::MAP> tf (this->map, *e_it);
		for (Dart f = tf.begin(); f != tf.end(); f=tf.next())
		{
			if ( !fm.isMarked(f) )
			{
				fm.mark(f);
				Traversor2FV<typename PFP::MAP> tv (this->map, f);
				Dart v = tv.begin();
				while ( v != tv.end() && vmReached.isMarked(v) ) {v=tv.next();}
				if ( v != tv.end() )
					this->border.push_back(f);
			}
		}
	}
	this->insideVertices.clear();
Basile Sauvage's avatar
Basile Sauvage committed
717 718 719
}

template <typename PFP>
720 721
inline float Collector_Dijkstra<PFP>::edgeLength (Dart d)
{
Basile Sauvage's avatar
Basile Sauvage committed
722 723 724
	typename PFP::VEC3 v = Algo::Geometry::vectorOutOfDart<PFP>(this->map, d, this->position);
	return v.norm();
}
725

Basile Sauvage's avatar
Basile Sauvage committed
726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
/*********************************************************
 * Collector Dijkstra_Vertices
 *********************************************************/

template <typename PFP>
void Collector_Dijkstra_Vertices<PFP>::collectAll(Dart dinit)
{
	init(dinit);

	CellMarkerStore<VERTEX> vmReached (this->map, this->m_thread);
	vertexInfo[this->centerDart].it = front.insert(std::pair<float,Dart>(0.0, this->centerDart));
	vertexInfo[this->centerDart].valid = true;
	vmReached.mark(this->centerDart);

	while ( !front.empty() && front.begin()->first < this->maxDist)
	{
		Dart e = front.begin()->second;
		float d = front.begin()->first;
		front.erase(vertexInfo[e].it);
		vertexInfo[e].valid=false;
		this->insideVertices.push_back(e);

		Traversor2VVaE<typename PFP::MAP> tv (this->map, e);
		for (Dart f = tv.begin(); f != tv.end(); f=tv.next())
		{
			VertexInfo& vi (vertexInfo[f]);
			if (vmReached.isMarked(f))
			{
				if (vi.valid) // probably useless (because of distance test) but faster
				{
					float dist = d + edge_cost[f];
					if (dist < vi.it->first)
					{
						front.erase(vi.it);
						vi.it = front.insert(std::pair<float,Dart>(dist, f));
					}
				}
			}
			else
			{
				vi.it = front.insert(std::pair<float,Dart>(d + edge_cost[f], f));
				vi.valid=true;
				vmReached.mark(f);
			}

		}

	}

	while ( !front.empty())
	{
		vmReached.unmark(front.begin()->second);
		front.erase(front.begin());
	}


	CellMarkerStore<EDGE> em (this->map, this->m_thread);
	CellMarkerStore<FACE> fm (this->map, this->m_thread);
	for (std::vector<Dart>::iterator e_it = this->insideVertices.begin(); e_it != this->insideVertices.end() ; e_it++)
	{
		// collect insideEdges
		Traversor2VE<typename PFP::MAP> te (this->map, *e_it);
		for (Dart e = te.begin(); e != te.end(); e=te.next())
		{
			if ( !em.isMarked(e) && vmReached.isMarked(this->map.phi2(e)) )
			{ // opposite vertex is inside -> inside edge
				this->insideEdges.push_back(e);
				em.mark(e);
			}
		}

		// collect insideFaces and border
		Traversor2VF<typename PFP::MAP> tf (this->map, *e_it);
		for (Dart f = tf.begin(); f != tf.end(); f=tf.next())
		{
			if ( !fm.isMarked(f) )
			{
				fm.mark(f);
				Traversor2FV<typename PFP::MAP> tv (this->map, f);
				Dart v = tv.begin();
				while ( v != tv.end() && vmReached.isMarked(v) ) {v=tv.next();}
				if ( v == tv.end() )
					this->insideFaces.push_back(f);
				else
					this->border.push_back(f);
			}
		}
	}
}

template <typename PFP>
void Collector_Dijkstra_Vertices<PFP>::collectBorder(Dart dinit)
{
	init(dinit);

	CellMarkerStore<VERTEX> vmReached (this->map, this->m_thread);
	vertexInfo[this->centerDart].it = front.insert(std::pair<float,Dart>(0.0, this->centerDart));
	vertexInfo[this->centerDart].valid = true;
	vmReached.mark(this->centerDart);

	while ( !front.empty() && front.begin()->first < this->maxDist)
	{
		Dart e = front.begin()->second;
		float d = front.begin()->first;
		front.erase(vertexInfo[e].it);
		vertexInfo[e].valid=false;
		this->insideVertices.push_back(e);

		Traversor2VVaE<typename PFP::MAP> tv (this->map, e);
		for (Dart f = tv.begin(); f != tv.end(); f=tv.next())
		{
			VertexInfo& vi (vertexInfo[f]);
			if (vmReached.isMarked(f))
			{
				if (vi.valid) // probably useless (because of distance test) but faster
				{
					float dist = d + edge_cost[f];
					if (dist < vi.it->first)
					{
						front.erase(vi.it);
						vi.it = front.insert(std::pair<float,Dart>(dist, f));
					}
				}
			}
			else
			{
				vi.it = front.insert(std::pair<float,Dart>(d + edge_cost[f], f));
				vi.valid=true;
				vmReached.mark(f);
			}

		}

	}

	while ( !front.empty())
	{
		vmReached.unmark(front.begin()->second);
		front.erase(front.begin());
	}


	CellMarkerStore<FACE> fm (this->map, this->m_thread);
	for (std::vector<Dart>::iterator e_it = this->insideVertices.begin(); e_it != this->insideVertices.end() ; e_it++)
	{
		// collect border
		Traversor2VF<typename PFP::MAP> tf (this->map, *e_it);
		for (Dart f = tf.begin(); f != tf.end(); f=tf.next())
		{
			if ( !fm.isMarked(f) )
			{
				fm.mark(f);
				Traversor2FV<typename PFP::MAP> tv (this->map, f);
				Dart v = tv.begin();
				while ( v != tv.end() && vmReached.isMarked(v) ) {v=tv.next();}
				if ( v != tv.end() )
					this->border.push_back(f);
			}
		}
	}
	this->insideVertices.clear();
}
888

Pierre Kraemer's avatar
Pierre Kraemer committed
889 890
} // namespace Selection

891 892
} // namespace Algo

Pierre Kraemer's avatar
Pierre Kraemer committed
893
} // namespace CGoGN