raySelector.hpp 25.1 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 40 41 42 43 44 45 46 47 48

namespace CGoGN
{

namespace Algo
{

namespace Selection
{

/**
 * Function that does the selection of faces, returned darts are sorted from closest to farthest
 * @param map the map we want to test
 * @param good a dart selector
 * @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>
49
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, const FunctorSelect& good)
50 51 52 53 54 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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 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 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 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 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 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 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 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
{
	std::vector<typename PFP::VEC3> iPoints;

	// get back intersected faces
	vecFaces.reserve(256);
	iPoints.reserve(256);
	vecFaces.clear();
	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
		{
			// 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
	std::sort(distndart.begin(), distndart.end(), distndartOrdering<PFP>);

	// store sorted darts in returned vector
	for (unsigned int i = 0; i < nbi; ++i)
		vecFaces[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 dist radius of the cylinder of selection
 */
template<typename PFP>
void edgesRaySelection(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const FunctorSelect& good, const typename PFP::VEC3& rayA, const typename PFP::VEC3& rayAB, std::vector<Dart>& vecEdges, float distMax)
{
	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);
	}

	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
	std::sort(distndart.begin(), distndart.end(), distndartOrdering<PFP>);

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

/**
 * 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>
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, const FunctorSelect& good= allDarts)
{
	typename PFP::REAL dist2 = dist * dist;
	typename PFP::REAL AB2 = rayAB * rayAB;

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

	TraversorV<typename PFP::MAP> trav(map,good);
	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);
	}

	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
	std::sort(distndart.begin(), distndart.end(), distndartOrdering<PFP>);

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



template<typename PFP>
void volumesRaySelection(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const FunctorSelect& good, const typename PFP::VEC3& rayA, const typename PFP::VEC3& rayAB, std::vector<Dart>& vecVolumes)
{
	std::vector<Dart>& vecFaces;
	std::vector<typename PFP::VEC3> iPoints;

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

	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
		{
			// 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
	std::sort(distndart.begin(), distndart.end(), distndartOrdering<PFP>);

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





/**
 * 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 vertexOfSurfaceRaySelection(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& rayA, const typename PFP::VEC3& rayAB, Dart& vertex, const FunctorSelect& good = allDarts)
{
	if (map.dimension() >2)
	{
		CGoGNerr << "vertexOfSurfaceRaySelection only on map of dimension 2" << CGoGNendl;
	}

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

	// recuperation des faces intersectes
	TraversorF<typename PFP::MAP> trav(map,good);
	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
		{
			// 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);
	}

	if(vecFaces.size() > 0)
	{
		typedef std::pair<typename PFP::REAL, unsigned int> IndexedDist;
		std::vector<IndexedDist> distnint;

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

		// sort the vector of pair dist/dart
		std::sort(distnint.begin(), distnint.end(), distnintOrdering<PFP>);

		// recuperation du point d'intersection sur la face la plus proche
		unsigned int first = distnint[0].second;
		typename PFP::VEC3 ip = iPoints[first];

		// recuperation du sommet le plus proche du point d'intersection
		Dart d = vecFaces[first];
		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;
}

/**
 * Fonction that do the selection of darts, returned darts are sorted from closest to farthest
 * 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>
//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, const FunctorSelect& good = allDarts)
//{
//	// recuperation des brins intersectes
//	vecDarts.clear();
//	Algo::Selection::FuncDartMapD2Inter<PFP> ffi(map, position, vecDarts, rayA, rayAB);
//	map.template foreach_orbit<FACE>(ffi, good);
//
//	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>
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,
		const FunctorSelect& good = allDarts)
{
	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>
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, const FunctorSelect& good= allDarts)
{
	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();

	TraversorV<typename PFP::MAP> trav(map,good);
	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
	std::sort(distndart.begin(), distndart.end(), distndartOrdering<PFP>);

	// 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>
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, const FunctorSelect& good)
{
	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);
502
		typename PFP::VEC3 V = (P+Q)/2.0f - rayA;
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
		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
	std::sort(distndart.begin(), distndart.end(), distndartOrdering<PFP>);

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



template<typename PFP>
536
Dart verticesBubbleSelection(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& cursor, typename PFP::REAL radiusMax, const FunctorSelect& good=allDarts)
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
{
	typename PFP::REAL l2max = radiusMax*radiusMax;
	typename PFP::REAL l2min(std::numeric_limits<float>::max());
	Dart d_min=NIL;

	TraversorV<typename PFP::MAP> trav(map,good);
	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>
558
Dart edgesBubbleSelection(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& cursor, typename PFP::REAL radiusMax, const FunctorSelect& good=allDarts)
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 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 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 644 645 646 647 648 649 650 651 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 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 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 751 752 753 754 755
{
	typename PFP::REAL l2max = radiusMax*radiusMax;
	typename PFP::REAL l2min(std::numeric_limits<float>::max());
	Dart d_min=NIL;

	TraversorE<typename PFP::MAP> trav(map,good);
	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>
//void facesRaySelection(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const FunctorSelect& good, const typename PFP::VEC3& rayA, const typename PFP::VEC3& rayAB, std::vector<Dart>& vecFaces, unsigned int nbth=0, unsigned int current_thread=0)
//{
//	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));
//
//	Algo::Parallel::foreach_cell<typename PFP::MAP,FACE>(map, functs, false, good, current_thread);
//
//
//	// 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>
//void edgesRaySelection(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const FunctorSelect& good, 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)
//{
//	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));
//
//	Algo::Parallel::foreach_cell<typename PFP::MAP,EDGE>(map, functs, false, good, current_thread);
//
//	// 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>
//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, const FunctorSelect& good= allDarts, unsigned int nbth=0, unsigned int current_thread=0)
//{
//	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));
//
//	Algo::Parallel::foreach_cell<typename PFP::MAP,VERTEX>(map, functs, false, good, current_thread);
//
//	// 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>
//void vertexRaySelection(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& rayA, const typename PFP::VEC3& rayAB, Dart& vertex, const FunctorSelect& good = allDarts, unsigned int nbth=0, unsigned int current_thread=0)
//{
//	std::vector<Dart> vecFaces;
//	vecFaces.reserve(100);
//	Parallel::facesRaySelection<PFP>(map, position, good, rayA, rayAB, vecFaces, nbth, current_thread);
//
//	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