raySelector.hpp 27 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009-2012, 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.           *
*                                                                              *
* Web site: http://cgogn.unistra.fr/                                           *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include <algorithm>
#include <set>
27 28 29
#include "Geometry/distances.h"
#include "Geometry/intersection.h"
#include "Algo/Geometry/centroid.h"
30 31 32 33 34 35 36 37 38 39

namespace CGoGN
{

namespace Algo
{

namespace Selection
{

Pierre Kraemer's avatar
Pierre Kraemer committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
template <typename R, typename T>
bool distOrdering(const std::pair<R, T>& e1, const std::pair<R, T>& e2)
{
	return (e1.first < e2.first);
}

template <typename PFP>
struct FaceInter
{
	Dart f;
	typename PFP::VEC3 i;
	FaceInter(Dart d, const typename PFP::VEC3& v) : f(d), i(v) {}
	FaceInter() {}
};

55
/**
Pierre Kraemer's avatar
Pierre Kraemer committed
56
 * Function that does the selection of faces, returned darts and intersection points are sorted from closest to farthest
57
 * @param map the map we want to test
Pierre Kraemer's avatar
Pierre Kraemer committed
58
 * @param position the vertex attribute storing positions
59 60 61
 * @param rayA first point of ray (user side)
 * @param rayAB direction of ray (directed to the scene)
 * @param vecFaces (out) vector to store the darts of intersected faces
Pierre Kraemer's avatar
Pierre Kraemer committed
62
 * @param iPoints (out) vector to store the intersection points
63 64
 */
template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
65 66 67 68 69 70 71
void facesRaySelection(
		typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position,
		const typename PFP::VEC3& rayA,
		const typename PFP::VEC3& rayAB,
		std::vector<Dart>& vecFaces,
		std::vector<typename PFP::VEC3>& iPoints)
72 73 74 75
{
	vecFaces.reserve(256);
	iPoints.reserve(256);
	vecFaces.clear();
Pierre Kraemer's avatar
Pierre Kraemer committed
76 77
	iPoints.clear();

78 79 80 81 82 83 84 85 86
	TraversorF<typename PFP::MAP> trav(map);
	for(Dart d = trav.begin(); d!=trav.end(); d = trav.next())
	{
		const typename PFP::VEC3& Ta = position[d];
		Dart dd  = map.phi1(d);
		Dart ddd = map.phi1(dd);
		bool notfound = true;
		do
		{
Pierre Kraemer's avatar
Pierre Kraemer committed
87
			// get position of triangle Ta,Tb,Tc
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
			const typename PFP::VEC3& Tb = position[dd];
			const typename PFP::VEC3& Tc = position[ddd];
			typename PFP::VEC3 I;
			if (Geom::intersectionRayTriangleOpt<typename PFP::VEC3>(rayA, rayAB, Ta, Tb, Tc, I))
			{
				vecFaces.push_back(d);
				iPoints.push_back(I);
				notfound = false;
			}
			// next triangle if we are in polygon
			dd = ddd;
			ddd = map.phi1(dd);
		} while ((ddd != d) && notfound);
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
103
	if(vecFaces.size() > 0)
104
	{
Pierre Kraemer's avatar
Pierre Kraemer committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		// compute all distances to observer for each intersected face
		// and put them in a vector for sorting
		typedef std::pair<typename PFP::REAL, FaceInter<PFP> > faceInterDist;
		std::vector<faceInterDist> dist;

		unsigned int nbi = vecFaces.size();
		dist.resize(nbi);
		for (unsigned int i = 0; i < nbi; ++i)
		{
			dist[i].first = (iPoints[i] - rayA).norm2();
			dist[i].second = FaceInter<PFP>(vecFaces[i], iPoints[i]);
		}

		// sort the vector of pair dist/dart
		std::sort(dist.begin(), dist.end(), distOrdering<typename PFP::REAL, FaceInter<PFP> >);

		// store result in returned vectors
		for (unsigned int i = 0; i < nbi; ++i)
		{
			vecFaces[i] = dist[i].second.f;
			iPoints[i] = dist[i].second.i;
		}
127
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
128
}
129

Pierre Kraemer's avatar
Pierre Kraemer committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
/**
 * Function that does the selection of faces, returned darts are sorted from closest to farthest
 * @param map the map we want to test
 * @param position the vertex attribute storing positions
 * @param rayA first point of ray (user side)
 * @param rayAB direction of ray (directed to the scene)
 * @param vecFaces (out) vector to store the darts of intersected faces
 */
template<typename PFP>
void facesRaySelection(
		typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position,
		const typename PFP::VEC3& rayA,
		const typename PFP::VEC3& rayAB,
		std::vector<Dart>& vecFaces)
{
	std::vector<typename PFP::VEC3> iPoints;
	facesRaySelection<PFP>(map, position, rayA, rayAB, vecFaces, iPoints);
}
149

Pierre Kraemer's avatar
Pierre Kraemer committed
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
/**
 * Function that does the selection of one face
 * @param map the map we want to test
 * @param rayA first point of  ray (user side)
 * @param rayAB vector of ray (directed ot the scene)
 * @param face (out) dart of intersected edge (set to NIL if no face selected)
 */
template<typename PFP>
void faceRaySelection(
		typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position,
		const typename PFP::VEC3& rayA,
		const typename PFP::VEC3& rayAB,
		Dart& face)
{
	if (map.dimension() > 2)
		CGoGNerr << "faceRaySelection only on map of dimension 2" << CGoGNendl;

	std::vector<Dart> vecFaces;
	std::vector<typename PFP::VEC3> iPoints;

	facesRaySelection<PFP>(map, position, rayA, rayAB, vecFaces, iPoints);

	if(vecFaces.size() > 0)
		face = vecFaces[0];
	else
		face = NIL;
177 178 179 180 181 182 183 184 185 186 187
}

/**
 * Function that does the selection of edges, returned darts are sorted from closest to farthest
 * @param map the map we want to test
 * @param rayA first point of  ray (user side)
 * @param rayAB vector of ray (directed ot the scene)
 * @param vecEdges (out) vector to store dart of intersected edges
 * @param dist radius of the cylinder of selection
 */
template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
188 189 190 191 192 193 194
void edgesRaySelection(
		typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position,
		const typename PFP::VEC3& rayA,
		const typename PFP::VEC3& rayAB,
		std::vector<Dart>& vecEdges,
		float distMax)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
{
	typename PFP::REAL dist2 = distMax * distMax;
	typename PFP::REAL AB2 = rayAB * rayAB;

	// recuperation des aretes intersectees
	vecEdges.reserve(256);
	vecEdges.clear();

	TraversorE<typename PFP::MAP> trav(map);
	for(Dart d = trav.begin(); d!=trav.end(); d = trav.next())
	{
		// get back position of segment PQ
		const typename PFP::VEC3& P = position[d];
		const typename PFP::VEC3& Q = position[map.phi1(d)];
		// the three distance to P, Q and (PQ) not used here
		float ld2 = Geom::squaredDistanceLine2Seg(rayA, rayAB, AB2, P, Q);
		if (ld2 < dist2)
			vecEdges.push_back(d);
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
215 216 217 218
	if(vecEdges.size() > 0)
	{
		typedef std::pair<typename PFP::REAL, Dart> DartDist;
		std::vector<DartDist> distndart;
219

Pierre Kraemer's avatar
Pierre Kraemer committed
220 221
		unsigned int nbi = vecEdges.size();
		distndart.resize(nbi);
222

Pierre Kraemer's avatar
Pierre Kraemer committed
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
		// compute all distances to observer for each middle of intersected edge
		// and put them in a vector for sorting
		for (unsigned int i = 0; i < nbi; ++i)
		{
			Dart d = vecEdges[i];
			distndart[i].second = d;
			typename PFP::VEC3 V = (position[d] + position[map.phi1(d)]) / typename PFP::REAL(2);
			V -= rayA;
			distndart[i].first = V.norm2();
		}

		// sort the vector of pair dist/dart
		std::sort(distndart.begin(), distndart.end(), distOrdering<typename PFP::REAL, Dart>);

		// store sorted darts in returned vector
		for (unsigned int i = 0; i < nbi; ++i)
			vecEdges[i] = distndart[i].second;
240
	}
Pierre Kraemer's avatar
Pierre Kraemer committed
241
}
242

Pierre Kraemer's avatar
Pierre Kraemer committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
/**
 * Function that does the selection of one edge
 * @param map the map we want to test
 * @param rayA first point of  ray (user side)
 * @param rayAB vector of ray (directed ot the scene)
 * @param edge (out) dart of intersected edge (set to NIL if no edge selected)
 */
template<typename PFP>
void edgeRaySelection(
		typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position,
		const typename PFP::VEC3& rayA,
		const typename PFP::VEC3& rayAB,
		Dart& edge)
{
	if (map.dimension() > 2)
		CGoGNerr << "edgeRaySelection only on map of dimension 2" << CGoGNendl;
260

Pierre Kraemer's avatar
Pierre Kraemer committed
261 262 263 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
	std::vector<Dart> vecFaces;
	std::vector<typename PFP::VEC3> iPoints;

	facesRaySelection<PFP>(map, position, rayA, rayAB, vecFaces, iPoints);

	if(vecFaces.size() > 0)
	{
		// recuperation du point d'intersection sur la face la plus proche
		typename PFP::VEC3 ip = iPoints[0];

		// recuperation de l'arete la plus proche du point d'intersection
		Dart d = vecFaces[0];
		Dart it = d;
		typename PFP::REAL minDist = squaredDistanceLine2Point(position[it], position[map.phi1(it)], ip);
		edge = it;
		it = map.phi1(it);
		while(it != d)
		{
			typename PFP::REAL dist = squaredDistanceLine2Point(position[it], position[map.phi1(it)], ip);
			if(dist < minDist)
			{
				minDist = dist;
				edge = it;
			}
			it = map.phi1(it);
		}
	}
	else
		edge = NIL;
290 291 292 293 294 295 296 297 298 299 300
}

/**
 * Function that does the selection of vertices, returned darts are sorted from closest to farthest
 * @param map the map we want to test
 * @param rayA first point of  ray (user side)
 * @param rayAB vector of ray (directed ot the scene)
 * @param vecVertices (out) vector to store dart of intersected vertices
 * @param dist radius of the cylinder of selection
 */
template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
301 302 303 304 305 306 307
void verticesRaySelection(
		typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position,
		const typename PFP::VEC3& rayA,
		const typename PFP::VEC3& rayAB,
		std::vector<Dart>& vecVertices,
		float dist)
308 309 310 311 312 313 314 315
{
	typename PFP::REAL dist2 = dist * dist;
	typename PFP::REAL AB2 = rayAB * rayAB;

	// recuperation des sommets intersectes
	vecVertices.reserve(256);
	vecVertices.clear();

316
	TraversorV<typename PFP::MAP> trav(map);
317 318 319 320 321 322 323 324
	for(Dart d = trav.begin(); d!=trav.end(); d = trav.next())
	{
		const typename PFP::VEC3& P = position[d];
		float ld2 = Geom::squaredDistanceLine2Point(rayA, rayAB, AB2, P);
		if (ld2 < dist2)
			vecVertices.push_back(d);
	}

Pierre Kraemer's avatar
Pierre Kraemer committed
325 326 327 328
	if(vecVertices.size() > 0)
	{
		typedef std::pair<typename PFP::REAL, Dart> DartDist;
		std::vector<DartDist> distndart;
329

Pierre Kraemer's avatar
Pierre Kraemer committed
330 331
		unsigned int nbi = vecVertices.size();
		distndart.resize(nbi);
332

Pierre Kraemer's avatar
Pierre Kraemer committed
333 334 335 336 337 338 339 340 341
		// compute all distances to observer for each intersected vertex
		// and put them in a vector for sorting
		for (unsigned int i = 0; i < nbi; ++i)
		{
			Dart d = vecVertices[i];
			distndart[i].second = d;
			typename PFP::VEC3 V = position[d] - rayA;
			distndart[i].first = V.norm2();
		}
342

Pierre Kraemer's avatar
Pierre Kraemer committed
343 344
		// sort the vector of pair dist/dart
		std::sort(distndart.begin(), distndart.end(), distOrdering<typename PFP::REAL, Dart>);
345

Pierre Kraemer's avatar
Pierre Kraemer committed
346 347 348 349
		// store sorted darts in returned vector
		for (unsigned int i = 0; i < nbi; ++i)
			vecVertices[i] = distndart[i].second;
	}
350 351
}

Pierre Kraemer's avatar
Pierre Kraemer committed
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
/**
 * Function that does the selection of one vertex
 * @param map the map we want to test
 * @param rayA first point of  ray (user side)
 * @param rayAB vector of ray (directed ot the scene)
 * @param vertex (out) dart of selected vertex (set to NIL if no vertex selected)
 */
template<typename PFP>
void vertexRaySelection(
		typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position,
		const typename PFP::VEC3& rayA,
		const typename PFP::VEC3& rayAB,
		Dart& vertex)
{
	if (map.dimension() > 2)
		CGoGNerr << "vertexRaySelection only on map of dimension 2" << CGoGNendl;

	std::vector<Dart> vecFaces;
	std::vector<typename PFP::VEC3> iPoints;

	facesRaySelection<PFP>(map, position, rayA, rayAB, vecFaces, iPoints);
374

Pierre Kraemer's avatar
Pierre Kraemer committed
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
	if(vecFaces.size() > 0)
	{
		// recuperation du point d'intersection sur la face la plus proche
		typename PFP::VEC3 ip = iPoints[0];

		// recuperation du sommet le plus proche du point d'intersection
		Dart d = vecFaces[0];
		Dart it = d;
		typename PFP::REAL minDist = (ip - position[it]).norm2();
		vertex = it;
		it = map.phi1(it);
		while(it != d)
		{
			typename PFP::REAL dist = (ip - position[it]).norm2();
			if(dist < minDist)
			{
				minDist = dist;
				vertex = it;
			}
			it = map.phi1(it);
		}
	}
	else
		vertex = NIL;
}
400 401

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
402 403 404 405 406 407
void volumesRaySelection(
		typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position,
		const typename PFP::VEC3& rayA,
		const typename PFP::VEC3& rayAB,
		std::vector<Dart>& vecVolumes)
408
{
Sylvain Thery's avatar
bug  
Sylvain Thery committed
409
	std::vector<Dart> vecFaces;
410 411 412 413 414 415 416
	std::vector<typename PFP::VEC3> iPoints;

	// get back intersected faces
	vecFaces.reserve(256);
	iPoints.reserve(256);

	TraversorF<typename PFP::MAP> trav(map);
Pierre Kraemer's avatar
Pierre Kraemer committed
417
	for(Dart d = trav.begin(); d != trav.end(); d = trav.next())
418 419 420 421 422 423 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 453 454 455
	{
		const typename PFP::VEC3& Ta = position[d];
		Dart dd  = map.phi1(d);
		Dart ddd = map.phi1(dd);
		bool notfound = true;
		do
		{
			// get back position of triangle Ta,Tb,Tc
			const typename PFP::VEC3& Tb = position[dd];
			const typename PFP::VEC3& Tc = position[ddd];
			typename PFP::VEC3 I;
			if (Geom::intersectionRayTriangleOpt<typename PFP::VEC3>(rayA, rayAB, Ta, Tb, Tc, I))
			{
				vecFaces.push_back(d);
				iPoints.push_back(I);
				notfound = false;
			}
			// next triangle if we are in polygon
			dd = ddd;
			ddd = map.phi1(dd);
		} while ((ddd != d) && notfound);
	}

	// compute all distances to observer for each intersected face
	// and put them in a vector for sorting
	typedef std::pair<typename PFP::REAL, Dart> DartDist;
	std::vector<DartDist> distndart;

	unsigned int nbi = vecFaces.size();
	distndart.resize(nbi);
	for (unsigned int i = 0; i < nbi; ++i)
	{
		distndart[i].second = vecFaces[i];
		typename PFP::VEC3 V = iPoints[i] - rayA;
		distndart[i].first = V.norm2();
	}

	// sort the vector of pair dist/dart
Pierre Kraemer's avatar
Pierre Kraemer committed
456
	std::sort(distndart.begin(), distndart.end(), distOrdering<typename PFP::REAL, Dart>);
457 458 459 460 461 462 463 464

//TODO
//	think of how sort volumes from faces order
//
}


/**
Pierre Kraemer's avatar
Pierre Kraemer committed
465
 * Fonction that does the selection of darts, returned darts are sorted from closest to farthest
466 467 468 469 470 471 472
 * Dart is here considered as a triangle formed by the 2 end vertices of the edge and the face centroid
 * @param map the map we want to test
 * @param rayA first point of  ray (user side)
 * @param rayAB vector of ray (directed ot the scene)
 * @param vecDarts (out) vector to store dart of intersected darts
 */
//template<typename PFP>
473
//void dartsRaySelection(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& rayA, const typename PFP::VEC3& rayAB, std::vector<Dart>& vecDarts)
474 475 476 477
//{
//	// recuperation des brins intersectes
//	vecDarts.clear();
//	Algo::Selection::FuncDartMapD2Inter<PFP> ffi(map, position, vecDarts, rayA, rayAB);
478
//	map.template foreach_orbit<FACE>(ffi);
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
//
//	typedef std::pair<typename PFP::REAL, Dart> DartDist;
//	std::vector<DartDist> distndart;
//
//	unsigned int nbi = vecDarts.size();
//	distndart.resize(nbi);
//
//	// compute all distances to observer for each dart of middle of edge
//	// and put them in a vector for sorting
//	for (unsigned int i = 0; i < nbi; ++i)
//	{
//		Dart d = vecDarts[i];
//		distndart[i].second = d;
//		typename PFP::VEC3 V = (position[d] + position[map.phi1(d)]) / typename PFP::REAL(2);
//		V -= rayA;
//		distndart[i].first = V.norm2();
//	}
//
//	// sort the vector of pair dist/dart
//	std::sort(distndart.begin(), distndart.end(), distndartOrdering<PFP>);
//
//	// store sorted darts in returned vector
//	for (unsigned int i=0; i< nbi; ++i)
//		vecDarts[i] = distndart[i].second;
//}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
506 507 508 509 510
void facesPlanSelection(
		typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position,
		const typename Geom::Plane3D<typename PFP::VEC3::DATA_TYPE>& plan,
		std::vector<Dart>& vecDarts)
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
{
	TraversorF<typename PFP::MAP> travF(map);

	for(Dart dit = travF.begin() ; dit != travF.end() ; dit = travF.next() )
	{
		if(Geom::intersectionTrianglePlan<typename PFP::VEC3>(position[dit], position[map.phi1(dit)], position[map.phi_1(dit)],plan.d(), plan.normal()) == Geom::FACE_INTERSECTION)
		{
			vecDarts.push_back(dit);
		}
	}

	std::cout << "nb faces = " << vecDarts.size() << std::endl;
}



/**
 * Function that does the selection of vertices in a cone, returned darts are sorted from closest to farthest
 * @param map the map we want to test
 * @param rayA first point of  ray (user side)
 * @param rayAB vector of ray (directed ot the scene)
 * @param vecVertices (out) vector to store dart of intersected vertices
 * @param angle angle of the code in degree.
 */
template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
536 537 538 539 540 541 542
void verticesConeSelection(
		typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position,
		const typename PFP::VEC3& rayA,
		const typename PFP::VEC3& rayAB,
		float angle,
		std::vector<Dart>& vecVertices)
543 544 545 546 547 548 549 550 551 552
{
	typename PFP::REAL AB2 = rayAB * rayAB;

	double sin2 = sin(M_PI/180.0 * angle);
	sin2 = sin2*sin2;

	// recuperation des sommets intersectes
	vecVertices.reserve(256);
	vecVertices.clear();

553
	TraversorV<typename PFP::MAP> trav(map);
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
	for(Dart d = trav.begin(); d!=trav.end(); d = trav.next())
	{
		const typename PFP::VEC3& P = position[d];
		float ld2 = Geom::squaredDistanceLine2Point(rayA, rayAB, AB2, P);
		typename PFP::VEC3 V = P - rayA;
		double s2 = double(ld2) / double(V*V);
		if (s2 < sin2)
			vecVertices.push_back(d);
	}

	typedef std::pair<typename PFP::REAL, Dart> DartDist;
	std::vector<DartDist> distndart;

	unsigned int nbi = vecVertices.size();
	distndart.resize(nbi);

	// compute all distances to observer for each intersected vertex
	// and put them in a vector for sorting
	for (unsigned int i = 0; i < nbi; ++i)
	{
		Dart d = vecVertices[i];
		distndart[i].second = d;
		typename PFP::VEC3 V = position[d] - rayA;
		distndart[i].first = V.norm2();
	}

	// sort the vector of pair dist/dart
Pierre Kraemer's avatar
Pierre Kraemer committed
581
	std::sort(distndart.begin(), distndart.end(), distOrdering<typename PFP::REAL, Dart>);
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596

	// store sorted darts in returned vector
	for (unsigned int i = 0; i < nbi; ++i)
		vecVertices[i] = distndart[i].second;
}

/**
 * Function that does the selection of edges, returned darts are sorted from closest to farthest
 * @param map the map we want to test
 * @param rayA first point of  ray (user side)
 * @param rayAB vector of ray (directed ot the scene)
 * @param vecEdges (out) vector to store dart of intersected edges
 * @param angle radius of the cylinder of selection
 */
template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
597 598 599 600 601 602 603
void edgesConeSelection(
		typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position,
		const typename PFP::VEC3& rayA,
		const typename PFP::VEC3& rayAB,
		float angle,
		std::vector<Dart>& vecEdges)
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
{
	typename PFP::REAL AB2 = rayAB * rayAB;

	double sin2 = sin(M_PI/180.0 * angle);
	sin2 = sin2*sin2;

	// recuperation des aretes intersectees
	vecEdges.reserve(256);
	vecEdges.clear();

	TraversorE<typename PFP::MAP> trav(map);
	for(Dart d = trav.begin(); d!=trav.end(); d = trav.next())
	{
		// get back position of segment PQ
		const typename PFP::VEC3& P = position[d];
		const typename PFP::VEC3& Q = position[map.phi1(d)];
		// the three distance to P, Q and (PQ) not used here
		float ld2 = Geom::squaredDistanceLine2Seg(rayA, rayAB, AB2, P, Q);
622
		typename PFP::VEC3 V = (P+Q)/2.0f - rayA;
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645
		double s2 = double(ld2) / double(V*V);
		if (s2 < sin2)
			vecEdges.push_back(d);
	}

	typedef std::pair<typename PFP::REAL, Dart> DartDist;
	std::vector<DartDist> distndart;

	unsigned int nbi = vecEdges.size();
	distndart.resize(nbi);

	// compute all distances to observer for each middle of intersected edge
	// and put them in a vector for sorting
	for (unsigned int i = 0; i < nbi; ++i)
	{
		Dart d = vecEdges[i];
		distndart[i].second = d;
		typename PFP::VEC3 V = (position[d] + position[map.phi1(d)]) / typename PFP::REAL(2);
		V -= rayA;
		distndart[i].first = V.norm2();
	}

	// sort the vector of pair dist/dart
Pierre Kraemer's avatar
Pierre Kraemer committed
646
	std::sort(distndart.begin(), distndart.end(), distOrdering<typename PFP::REAL, Dart>);
647 648 649 650 651 652 653 654 655

	// store sorted darts in returned vector
	for (unsigned int i = 0; i < nbi; ++i)
		vecEdges[i] = distndart[i].second;
}



template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
656 657 658 659 660
Dart verticesBubbleSelection(
		typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position,
		const typename PFP::VEC3& cursor,
		typename PFP::REAL radiusMax)
661 662 663 664 665
{
	typename PFP::REAL l2max = radiusMax*radiusMax;
	typename PFP::REAL l2min(std::numeric_limits<float>::max());
	Dart d_min=NIL;

666
	TraversorV<typename PFP::MAP> trav(map);
667 668 669 670 671 672 673 674 675 676 677 678 679 680
	for(Dart d = trav.begin(); d!=trav.end(); d = trav.next())
	{
		const typename PFP::VEC3& P = position[d];
		typename PFP::VEC3 V = P - cursor;
		typename PFP::REAL l2 = V*V;
		if ((l2<l2max) && (l2<l2min))
		{
			d_min=d;
		}
	}
	return d_min;
}

template<typename PFP>
Pierre Kraemer's avatar
Pierre Kraemer committed
681 682 683 684 685
Dart edgesBubbleSelection(
		typename PFP::MAP& map,
		const VertexAttribute<typename PFP::VEC3>& position,
		const typename PFP::VEC3& cursor,
		typename PFP::REAL radiusMax)
686 687 688 689 690
{
	typename PFP::REAL l2max = radiusMax*radiusMax;
	typename PFP::REAL l2min(std::numeric_limits<float>::max());
	Dart d_min=NIL;

691
	TraversorE<typename PFP::MAP> trav(map);
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
	for(Dart d = trav.begin(); d!=trav.end(); d = trav.next())
	{
		const typename PFP::VEC3& A = position[d];
		typename PFP::VEC3 AB =  position[map.phi1(d)] - A;
		typename PFP::REAL l2 = Geom::squaredDistanceSeg2Point(A,AB,AB*AB,cursor);
		if ((l2<l2max) && (l2<l2min))
		{
			d_min=d;
		}
	}
	return d_min;
}





//
//namespace Parallel
//{
//
//template<typename PFP>
714
//void facesRaySelection(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& rayA, const typename PFP::VEC3& rayAB, std::vector<Dart>& vecFaces, unsigned int nbth=0, unsigned int current_thread=0)
715 716 717 718 719 720 721 722 723
//{
//	if (nbth==0)
////		nbth = Algo::Parallel::optimalNbThreads();
//		nbth =2;	// seems to be optimal ?
//
//	std::vector<FunctorMapThreaded<typename PFP::MAP>*> functs;
//	for (unsigned int i=0; i < nbth; ++i)
//		functs.push_back(new Parallel::FuncFaceInter<PFP>(map,position,rayA, rayAB));
//
724
//	Algo::Parallel::foreach_cell<typename PFP::MAP,FACE>(map, functs, false, current_thread);
725 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
//
//
//	// compute total nb of intersection
//	unsigned int nbtot=0;
//	for (unsigned int i=0; i < nbth; ++i)
//		nbtot += static_cast<Parallel::FuncFaceInter<PFP>*>(functs[i])->getFaceDistances().size();
//
//	std::vector<std::pair<typename PFP::REAL, Dart> > distndart;
//	distndart.reserve(nbtot);
//	for (unsigned int i=0; i < nbth; ++i)
//	{
//		distndart.insert(distndart.end(),static_cast<Parallel::FuncFaceInter<PFP>*>(functs[i])->getFaceDistances().begin(), static_cast<Parallel::FuncFaceInter<PFP>*>(functs[i])->getFaceDistances().end() );
//		delete functs[i];
//	}
//
//	// sort the vector of pair dist/dart
//	std::sort(distndart.begin(), distndart.end(), distndartOrdering<PFP>);
//
//	vecFaces.clear();
//	vecFaces.reserve(nbtot);
//	// store sorted darts in returned vector
//	for (unsigned int i = 0; i < nbtot; ++i)
//		vecFaces.push_back(distndart[i].second);
//}
//
//template<typename PFP>
751
//void edgesRaySelection(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& rayA, const typename PFP::VEC3& rayAB, std::vector<Dart>& vecEdges, float dist, unsigned int nbth=0, unsigned int current_thread=0)
752 753 754 755 756 757 758 759 760 761 762 763
//{
//	typename PFP::REAL dist2 = dist * dist;
//	typename PFP::REAL AB2 = rayAB * rayAB;
//
//	if (nbth==0)
////		nbth = Algo::Parallel::optimalNbThreads();
//		nbth =2;	// seems to be optimal ?
//
//	std::vector<FunctorMapThreaded<typename PFP::MAP>*> functs;
//	for (unsigned int i=0; i < nbth; ++i)
//		functs.push_back(new Parallel::FuncEdgeInter<PFP>(map,position,rayA, rayAB, AB2, dist2));
//
764
//	Algo::Parallel::foreach_cell<typename PFP::MAP,EDGE>(map, functs, false, current_thread);
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
//
//	// compute total nb of intersection
//	unsigned int nbtot=0;
//	for (unsigned int i=0; i < nbth; ++i)
//		nbtot += static_cast<Parallel::FuncEdgeInter<PFP>*>(functs[i])->getEdgeDistances().size();
//
//	std::vector<std::pair<typename PFP::REAL, Dart> > distndart;
//	distndart.reserve(nbtot);
//	for (unsigned int i=0; i < nbth; ++i)
//	{
//		distndart.insert(distndart.end(),static_cast<Parallel::FuncEdgeInter<PFP>*>(functs[i])->getEdgeDistances().begin(), static_cast<Parallel::FuncEdgeInter<PFP>*>(functs[i])->getEdgeDistances().end() );
//		delete functs[i];
//	}
//
//	// sort the vector of pair dist/dart
//	std::sort(distndart.begin(), distndart.end(), distndartOrdering<PFP>);
//
//	// store sorted darts in returned vector
//	vecEdges.clear();
//	vecEdges.reserve(nbtot);
//	for (unsigned int i = 0; i < nbtot; ++i)
//		vecEdges.push_back(distndart[i].second);
//}
//
//
//template<typename PFP>
791
//void verticesRaySelection(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& rayA, const typename PFP::VEC3& rayAB, std::vector<Dart>& vecVertices, float dist , unsigned int nbth=0, unsigned int current_thread=0)
792 793 794 795 796 797 798 799 800 801 802 803
//{
//	typename PFP::REAL dist2 = dist * dist;
//	typename PFP::REAL AB2 = rayAB * rayAB;
//
//	if (nbth==0)
////		nbth = Algo::Parallel::optimalNbThreads();
//		nbth =2;	// seems to be optimal ?
//
//	std::vector<FunctorMapThreaded<typename PFP::MAP>*> functs;
//	for (unsigned int i=0; i < nbth; ++i)
//		functs.push_back(new Parallel::FuncVertexInter<PFP>(map,position,rayA, rayAB, AB2, dist2));
//
804
//	Algo::Parallel::foreach_cell<typename PFP::MAP,VERTEX>(map, functs, false, current_thread);
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
//
//	// compute total nb of intersection
//	unsigned int nbtot=0;
//	for (unsigned int i=0; i < nbth; ++i)
//		nbtot += static_cast<Parallel::FuncVertexInter<PFP>*>(functs[i])->getVertexDistances().size();
//
//	std::vector<std::pair<typename PFP::REAL, Dart> > distndart;
//	distndart.reserve(nbtot);
//	for (unsigned int i=0; i < nbth; ++i)
//	{
//		distndart.insert(distndart.end(),static_cast<Parallel::FuncVertexInter<PFP>*>(functs[i])->getVertexDistances().begin(), static_cast<Parallel::FuncVertexInter<PFP>*>(functs[i])->getVertexDistances().end() );
//		delete functs[i];
//	}
//
//	// sort the vector of pair dist/dart
//	std::sort(distndart.begin(), distndart.end(), distndartOrdering<PFP>);
//
//	// store sorted darts in returned vector
//	vecVertices.clear();
//	vecVertices.reserve(nbtot);
//	for (unsigned int i = 0; i < nbtot; ++i)
//		vecVertices.push_back(distndart[i].second);
//
//
//}
//
//template<typename PFP>
832
//void vertexRaySelection(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& rayA, const typename PFP::VEC3& rayAB, Dart& vertex, unsigned int nbth=0, unsigned int current_thread=0)
833 834 835
//{
//	std::vector<Dart> vecFaces;
//	vecFaces.reserve(100);
836
//	Parallel::facesRaySelection<PFP>(map, position, rayA, rayAB, vecFaces, nbth, current_thread);
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
//
//	if(vecFaces.size() > 0)
//	{
//		// recuperation du sommet le plus proche
//		Dart d = vecFaces.front();
//		Dart it = d;
//		typename PFP::REAL minDist = (rayA - position[it]).norm2();
//		vertex = it;
//		it = map.phi1(it);
//		while(it != d)
//		{
//			typename PFP::REAL dist = (rayA - position[it]).norm2();
//			if(dist < minDist)
//			{
//				minDist = dist;
//				vertex = it;
//			}
//			it = map.phi1(it);
//		}
//	}
//	else
//		vertex = NIL;
//}
//
//}

} //namespace Selection

} //namespace Algo

} //namespace CGoGN