raySelectFunctor.hpp 11.4 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1 2 3
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
4
* Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#ifndef RAYSELECTFUNCTOR_H_
#define RAYSELECTFUNCTOR_H_

#include "Geometry/distances.h"
#include "Geometry/intersection.h"

#include "Algo/Geometry/centroid.h"

namespace CGoGN
{

namespace Algo
{

namespace Selection
{

template <typename PFP>
class FuncFaceInter: public FunctorMap<typename PFP::MAP>
{
	typedef typename PFP::MAP MAP;
	
protected:
	std::vector<Dart>& m_faces;
	std::vector<typename PFP::VEC3>& m_Ipoints ;
	const typename PFP::VEC3& m_A;
	const typename PFP::VEC3& m_AB;
	const typename PFP::TVEC3& m_positions;
53

Pierre Kraemer's avatar
Pierre Kraemer committed
54 55 56 57 58 59 60 61 62 63 64 65 66
public:
	/**
 	* @param map the map
	* @param f vector of selected darts
	* @param A first point of ray
	* @param AB direction of ray
	*/
	FuncFaceInter(MAP& map, const typename PFP::TVEC3& position, std::vector<Dart>& f, std::vector<typename PFP::VEC3>& ip, const typename PFP::VEC3& A, const typename PFP::VEC3& AB):
		FunctorMap<typename PFP::MAP>(map), m_faces(f), m_Ipoints(ip), m_A(A), m_AB(AB), m_positions(position)
	{}

	bool operator()(Dart d)
	{
67
		const typename PFP::VEC3& Ta = m_positions[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
68 69 70 71 72 73 74

		Dart dd  = this->m_map.phi1(d);
		Dart ddd = this->m_map.phi1(dd);
		bool notfound = true;
		do 
		{
			// get back position of triangle Ta,Tb,Tc
75 76
			const typename PFP::VEC3& Tb = m_positions[dd];
			const typename PFP::VEC3& Tc = m_positions[ddd];
Pierre Kraemer's avatar
Pierre Kraemer committed
77 78 79 80
			typename PFP::VEC3 I;
			if (Geom::intersectionLineTriangle<typename PFP::VEC3>(m_A, m_AB, Ta, Tb, Tc, I))
			{
				m_faces.push_back(d);
81
				m_Ipoints.push_back(I);
Pierre Kraemer's avatar
Pierre Kraemer committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
				notfound = false;
			}
			// next triangle if we are in polygon
			dd = ddd;
			ddd = this->m_map.phi1(dd);
		} while ((ddd != d) && notfound);
		return false;
	}
};


template <typename PFP>
class FuncEdgeInter: public FunctorMap<typename PFP::MAP>
{
	typedef typename PFP::MAP MAP;
	
protected:
	std::vector<Dart>& m_edges;
	const typename PFP::VEC3& m_A;
	const typename PFP::VEC3& m_AB;
	float m_AB2;
	float m_distMax;
	const typename PFP::TVEC3& m_positions;
105

Pierre Kraemer's avatar
Pierre Kraemer committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
public:
	/**
 	* @param map the map
	* @param e vector of selected darts
	* @param A first point of ray
	* @param AB direction of ray
	* @param AB2 squared length of direction
	* @param dm2 max distance from ray squared
	*/
	FuncEdgeInter(MAP& map, const typename PFP::TVEC3& position, std::vector<Dart>& e, const typename PFP::VEC3& A, const typename PFP::VEC3& AB, typename PFP::REAL AB2, typename PFP::REAL dm2):
		FunctorMap<typename PFP::MAP>(map), m_edges(e), m_A(A), m_AB(AB), m_AB2(AB2), m_distMax(dm2), m_positions(position)
	{}

	bool operator()(Dart d)
	{
		// get back position of segment PQ
122
		const typename PFP::VEC3& P = m_positions[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
123
		Dart dd = this->m_map.phi1(d);
124
		const typename PFP::VEC3& Q = m_positions[dd];
Pierre Kraemer's avatar
Pierre Kraemer committed
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
		// the three distance to P, Q and (PQ) not used here
		float dist = Geom::squaredDistanceLine2Seg(m_A, m_AB, m_AB2, P, Q);

		if (dist < m_distMax)
		{
			m_edges.push_back(d);
		}
		return false;
	}
};


template <typename PFP>
class FuncVertexInter: public FunctorMap<typename PFP::MAP>
{
	typedef typename PFP::MAP MAP;

protected:
	std::vector<Dart>& m_vertices;
	const typename PFP::VEC3& m_A;
	const typename PFP::VEC3& m_AB;
	float m_AB2;
	float m_distMax;
	const typename PFP::TVEC3& m_positions;
public:
	/**
 	* @param map the map
	* @param v vector of selected darts
	* @param A first point of ray
	* @param AB direction of ray
	* @param AB2 squared length of direction
	* @param dm2 max distance from ray squared
	*/
	FuncVertexInter(MAP& map, const typename PFP::TVEC3& position, std::vector<Dart>& v, const typename PFP::VEC3& A, const typename PFP::VEC3& AB, typename PFP::REAL AB2, typename PFP::REAL dm2):
		FunctorMap<typename PFP::MAP>(map), m_vertices(v), m_A(A), m_AB(AB), m_AB2(AB2), m_distMax(dm2), m_positions(position)
	{}

	bool operator()(Dart d)
	{
164
		const typename PFP::VEC3& P = m_positions[d];
Pierre Kraemer's avatar
Pierre Kraemer committed
165 166 167 168 169 170 171 172 173 174
		float dist = Geom::squaredDistanceLine2Point(m_A, m_AB, m_AB2, P);
		if (dist < m_distMax)
		{
			m_vertices.push_back(d);
		}
		return false;
	}
};

/**
175
 * Functor which store the dart that correspond to the subpart of face
Pierre Kraemer's avatar
Pierre Kraemer committed
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
 * that is intersected
 * Must be called in foreachface
 */
template <typename PFP>
class FuncDartMapD2Inter: public FunctorMap<typename PFP::MAP>
{
	typedef typename PFP::MAP MAP ;

protected:
	std::vector<Dart>& m_darts ;
	const typename PFP::VEC3& m_A ;
	const typename PFP::VEC3& m_AB ;
	const typename PFP::TVEC3& m_positions;
public:
	/**
 	* @param map the map
	* @param f vector of selected darts
	* @param A first point of ray
	* @param AB direction of ray
	*/
	FuncDartMapD2Inter(MAP& map, const typename PFP::TVEC3& position, std::vector<Dart>& f, const typename PFP::VEC3& A, const typename PFP::VEC3& AB):
		FunctorMap<typename PFP::MAP>(map), m_darts(f), m_A(A), m_AB(AB), m_positions(position)
	{}

	bool operator()(Dart d)
	{
		typename PFP::VEC3 center = Geometry::faceCentroid<PFP>(this->m_map, d, m_positions) ;
		bool notfound = true ;
		Dart face = d ;
		do
		{
			// get back position of triangle
			const typename PFP::VEC3& Tb = m_positions[face]; //this->m_map.getVertexEmb(face)->getPosition() ;
			const typename PFP::VEC3& Tc = m_positions[this->m_map.phi1(face)]; //this->m_map.getVertexEmb(this->m_map.phi1(face))->getPosition() ;
			typename PFP::VEC3 I;
			if (Geom::intersectionLineTriangle(m_A, m_AB, center, Tb, Tc, I))
			{
				m_darts.push_back(face) ;
				notfound = false ;
			}
			face = this->m_map.phi1(face) ;
		} while((face != d) && notfound) ;
		return false;
	}
};

/**
 * Order relation between Darts with respect to the depth of
 * their 0-embedding
 *
 * Used in a std::sort algorithm
 */
//template <typename PFP>
//class DartDepthOrdering
//{
//	typedef typename PFP::MAP MAP;
////	typedef typename PFP::EMB EMB;
//	
//
//protected:
//	MAP& m_map;
//	const typename PFP::VEC3& m_A;
//	typename PFP::TVEC3 m_positions;
//public:
//	DartDepthOrdering(MAP& map, unsigned int idPos, const typename PFP::VEC3& A):
//			m_map(map), m_A(A), m_positions(idPos,map) {}
//
//	bool operator()(const Dart& d, const Dart& e)
//	{
//		typename PFP::VEC3 P = m_positions[d]; //m_map.getVertexEmb(d)->getPosition();
//		P -= m_A;
//		typename PFP::REAL ld = P*P;// l^2
//
//		P = m_positions[e]; //m_map.getVertexEmb(e)->getPosition();
//		P -= m_A;
//		typename PFP::REAL le = P*P;// l^2
//
//		if (ld < le)
//			return true;
//		return false;
//	}
//};

/**
 * Order relation between Darts with respect to the distance of
 * their 0-embedding to the line given in parameter in the constructor
 *
 * Used in a std::sort algorithm
 */
//template <typename PFP>
//class DartRayDistanceOrdering
//{
//	typedef typename PFP::MAP MAP ;
////	typedef typename PFP::EMB EMB ;
//	
//
//protected:
//	MAP& m_map ;
//	const typename PFP::VEC3& m_A ;
//	const typename PFP::VEC3& m_AB ;
//	float m_AB2 ;
//	typename PFP::TVEC3 m_positions;
//
//public:
//	DartRayDistanceOrdering(MAP& map, unsigned int idPos, const typename PFP::VEC3& A, const typename PFP::VEC3& AB) :
//	m_map(map), m_A(A), m_AB(AB), m_positions(idPos,map)
//	{
//		m_AB2 = m_AB*m_AB ; // l^2
//	}
//
//	bool operator()(const Dart& d, const Dart& e)
//	{
//		typename PFP::VEC3 P = m_positions[d]; //m_map.getVertexEmb(d)->getPosition() ;
//		float ld = Geom::squaredDistanceLine2Point(m_A, m_AB, m_AB2, P) ;
//		P =  m_positions[e]; //m_map.getVertexEmb(e)->getPosition() ;
//		float le = Geom::squaredDistanceLine2Point(m_A, m_AB, m_AB2, P) ;
//
//		if (ld < le)
//			return true;
//		return false;
//	}
//};

/**
 * Order relation between Darts with respect to the depth
 * of their faces :
 * f1 < f2 if f1 contains the vertex whose 0-embedding is the closest
 *
 * Used in a std::sort algorithm
 */
//template <typename PFP>
//class FaceDepthOrdering
//{
//	typedef typename PFP::MAP MAP ;
//	
//
//protected:
//	MAP& m_map ;
//	const typename PFP::VEC3& m_A ;
//	typename PFP::TVEC3 m_positions;
//	std::vector<Dart>& m_faces;
//
//public:
//	FaceDepthOrdering(MAP& map, unsigned int idPos, const typename PFP::VEC3& A, std::vector<Dart>& faces) : m_map(map), m_A(A), m_positions(idPos,map), m_faces(faces) {}
//
////	bool operator()(const Dart& d, const Dart& e)
//	bool operator()(Dart d, Dart e)
//	{
//		for (typename std::vector<Dart>::iterator it = m_faces.begin(); it != m_faces.end(); ++it)
//		{
326
//			CGoGNout << "Dart: "<< (it - m_faces.begin())  <<" = " << (*it)->getLabel()<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
327 328
//		}
//
329 330
//		CGoGNout << "beg sort op:"<<CGoGNendl;
//		CGoGNout <<"  darts "<<d->getLabel() <<" ?? "<< e->getLabel()<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
//		typename PFP::VEC3 P = m_positions[d]; //m_map.getVertexEmb(d)->getPosition() ;
//		P -= m_A ;
//		typename PFP::REAL minDist = P*P ;
//		Dart closestD = d ;
//		Dart it = d ;
//		do
//		{
//			P = m_positions[it]; //m_map.getVertexEmb(it)->getPosition() ; 	// compute the minimum
//			P -= m_A ;										// distance among the vertices
//			typename PFP::REAL dist = P*P ;			// of the face of d
//			if(dist < minDist)
//				minDist = dist ;
//			it = m_map.phi1(it) ;
//		} while(it != d) ;
//		it = e ;
//		do
//		{
//			P = m_positions[it]; //m_map.getVertexEmb(it)->getPosition() ; 	// if one vertex of the face
//			P -= m_A ;										// of e is closer than the previous
//			typename PFP::REAL dist = P*P ;			// minimum, then e < d
//			if(dist < minDist)								// else, d < e
//				return false ;
//			it = m_map.phi1(it) ;
//		} while(it != e) ;
//
356
//		CGoGNout << "end sort op"<<CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
//
////		return rand()&1;
//
//		return true ;
//	}
//} ;

template <typename PFP>
bool distndartOrdering(const std::pair<typename PFP::REAL, Dart>& e1, const std::pair<typename PFP::REAL, Dart>& e2)
{
	return (e1.first < e2.first);
}

template <typename PFP>
bool distnintOrdering(const std::pair<typename PFP::REAL, unsigned int>& e1, const std::pair<typename PFP::REAL, unsigned int>& e2)
{
	return (e1.first < e2.first);
}

} //namespace Selection

} //namespace Algo

} //namespace CGoGN

#endif