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-2012, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
20
* Web site: http://cgogn.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
* 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
{
41 42 43 44 45 46 47 48 49 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
//
//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 VertexAttribute<typename PFP::VEC3>& m_positions;
//
//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 VertexAttribute<typename PFP::VEC3>& 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)
//	{
//		const typename PFP::VEC3& Ta = m_positions[d];
//
//		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
//			const typename PFP::VEC3& Tb = m_positions[dd];
//			const typename PFP::VEC3& Tc = m_positions[ddd];
//			typename PFP::VEC3 I;
////			if (Geom::intersectionLineTriangle<typename PFP::VEC3>(m_A, m_AB, Ta, Tb, Tc, I))
//			if (Geom::intersectionRayTriangleOpt<typename PFP::VEC3>(m_A, m_AB, Ta, Tb, Tc, I))
//			{
//				m_faces.push_back(d);
//				m_Ipoints.push_back(I);
//				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 VertexAttribute<typename PFP::VEC3>& m_positions;
//
//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 VertexAttribute<typename PFP::VEC3>& 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
//		const typename PFP::VEC3& P = m_positions[d];
//		Dart dd = this->m_map.phi1(d);
//		const typename PFP::VEC3& Q = m_positions[dd];
//		// 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 VertexAttribute<typename PFP::VEC3>& 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 VertexAttribute<typename PFP::VEC3>& 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)
//	{
//		const typename PFP::VEC3& P = m_positions[d];
//		float dist = Geom::squaredDistanceLine2Point(m_A, m_AB, m_AB2, P);
//		if (dist < m_distMax)
//		{
//			m_vertices.push_back(d);
//		}
//		return false;
//	}
//};
Pierre Kraemer's avatar
Pierre Kraemer committed
174 175

/**
176
 * Functor which store the dart that correspond to the subpart of face
Pierre Kraemer's avatar
Pierre Kraemer committed
177 178 179 180 181 182 183 184 185 186 187 188
 * 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 ;
189
	const VertexAttribute<typename PFP::VEC3>& m_positions;
Pierre Kraemer's avatar
Pierre Kraemer committed
190 191 192 193 194 195 196
public:
	/**
 	* @param map the map
	* @param f vector of selected darts
	* @param A first point of ray
	* @param AB direction of ray
	*/
197
	FuncDartMapD2Inter(MAP& map, const VertexAttribute<typename PFP::VEC3>& position, std::vector<Dart>& f, const typename PFP::VEC3& A, const typename PFP::VEC3& AB):
Pierre Kraemer's avatar
Pierre Kraemer committed
198 199 200 201 202
		FunctorMap<typename PFP::MAP>(map), m_darts(f), m_A(A), m_AB(AB), m_positions(position)
	{}

	bool operator()(Dart d)
	{
203
		typename PFP::VEC3 center = Surface::Geometry::faceCentroid<PFP>(this->m_map, d, m_positions) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
204 205 206 207 208 209 210
		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() ;
Sylvain Thery's avatar
MAJ MC  
Sylvain Thery committed
211 212 213
//			typename PFP::VEC3 I;
//			if (Geom::intersectionLineTriangle(m_A, m_AB, center, Tb, Tc, I))
			if (Geom::intersectionRayTriangleOpt(m_A, m_AB, center, Tb, Tc))
Pierre Kraemer's avatar
Pierre Kraemer committed
214 215 216 217 218 219 220 221 222 223 224 225 226
			{
				m_darts.push_back(face) ;
				notfound = false ;
			}
			face = this->m_map.phi1(face) ;
		} while((face != d) && notfound) ;
		return false;
	}
};




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
namespace Parallel
{

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

protected:
	const VertexAttribute<typename PFP::VEC3>& m_positions;
	const typename PFP::VEC3& m_A;
	const typename PFP::VEC3& m_AB;
	float m_AB2;
	float m_distMax;
	std::vector<std::pair<typename PFP::REAL, Dart> > m_vd;

public:
	FuncVertexInter(MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& A, const typename PFP::VEC3& AB, typename PFP::REAL AB2, typename PFP::REAL dm2):
		FunctorMapThreaded<typename PFP::MAP>(map),  m_positions(position), m_A(A), m_AB(AB), m_AB2(AB2), m_distMax(dm2)
	{}

	void run(Dart d, unsigned int thread)
	{
		const typename PFP::VEC3& P = m_positions[d];
		float dist = Geom::squaredDistanceLine2Point(m_A, m_AB, m_AB2, P);
		if (dist < m_distMax)
		{
			typename PFP::REAL distA = (P-m_A).norm2();
			m_vd.push_back(std::pair<typename PFP::REAL, Dart>(distA,d));
		}

	}

	const std::vector<std::pair<typename PFP::REAL, Dart> >& getVertexDistances() { return m_vd;}
};


template <typename PFP>
class FuncEdgeInter: public FunctorMapThreaded<typename PFP::MAP>
{
	typedef typename PFP::MAP MAP;

protected:
	const VertexAttribute<typename PFP::VEC3>& m_positions;
	const typename PFP::VEC3& m_A;
	const typename PFP::VEC3& m_AB;
	float m_AB2;
	float m_distMax;
	std::vector<std::pair<typename PFP::REAL, Dart> > m_ed;

public:
	FuncEdgeInter(MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& A, const typename PFP::VEC3& AB, typename PFP::REAL AB2, typename PFP::REAL dm2):
		FunctorMapThreaded<typename PFP::MAP>(map), m_positions(position), m_A(A), m_AB(AB), m_AB2(AB2), m_distMax(dm2)
	{}

	void run(Dart d, unsigned int thread)
	{
		// get back position of segment PQ
		const typename PFP::VEC3& P = m_positions[d];
		Dart dd = this->m_map.phi1(d);
		const typename PFP::VEC3& Q = m_positions[dd];
		// 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)
		{
			typename PFP::VEC3 M = (P+Q)/2.0;
			typename PFP::REAL distA = (M-m_A).norm2();
			m_ed.push_back(std::pair<typename PFP::REAL, Dart>(distA,d));
		}
	}

	const std::vector<std::pair<typename PFP::REAL, Dart> >& getEdgeDistances() { return m_ed;}
};



template <typename PFP>
class FuncFaceInter: public FunctorMapThreaded<typename PFP::MAP>
{
	typedef typename PFP::MAP MAP;

protected:
	const VertexAttribute<typename PFP::VEC3>& m_positions;
	const typename PFP::VEC3& m_A;
	const typename PFP::VEC3& m_AB;
	std::vector<std::pair<typename PFP::REAL, Dart> > m_fd;

public:
	FuncFaceInter(MAP& map, const VertexAttribute<typename PFP::VEC3>& position, const typename PFP::VEC3& A, const typename PFP::VEC3& AB):
		FunctorMapThreaded<typename PFP::MAP>(map), m_positions(position), m_A(A), m_AB(AB)
	{}

	void run(Dart d, unsigned int thread)
	{
		const typename PFP::VEC3& Ta = m_positions[d];

		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
			const typename PFP::VEC3& Tb = m_positions[dd];
			const typename PFP::VEC3& Tc = m_positions[ddd];
			typename PFP::VEC3 I;
			if (Geom::intersectionRayTriangleOpt<typename PFP::VEC3>(m_A, m_AB, Ta, Tb, Tc, I))
			{
				typename PFP::REAL dist = (I-m_A).norm2();
				m_fd.push_back(std::pair<typename PFP::REAL, Dart>(dist,d));
				notfound = false;
			}
			// next triangle if we are in polygon
			dd = ddd;
			ddd = this->m_map.phi1(dd);
		} while ((ddd != d) && notfound);
	}

	const std::vector<std::pair<typename PFP::REAL, Dart> >& getFaceDistances() { return m_fd;}
};


}

Pierre Kraemer's avatar
Pierre Kraemer committed
350 351 352 353 354 355 356
} //namespace Selection

} //namespace Algo

} //namespace CGoGN

#endif