inclusion.hpp 9.68 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
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/

#include "Algo/Geometry/intersection.h"
#include "Algo/Geometry/orientation.h"
#include "Algo/Geometry/basic.h"
#include "Algo/Geometry/plane.h"

#include <limits>

namespace CGoGN
{

namespace Algo
{

namespace Geometry
{

template <typename PFP>
Sylvain Thery's avatar
Sylvain Thery committed
42
bool isConvex(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& positions, bool CCW, unsigned int thread)
Pierre Kraemer's avatar
Pierre Kraemer committed
43 44 45 46
{
	//get all the dart of the volume
	std::vector<Dart> vStore;
	FunctorStore fs(vStore);
Sylvain Thery's avatar
Sylvain Thery committed
47
	map.foreach_dart_of_volume(d, fs,thread);
Pierre Kraemer's avatar
Pierre Kraemer committed
48 49 50 51 52 53 54 55 56

	bool convex = true;

	DartMarkerStore m(map);
	for (std::vector<Dart>::iterator it = vStore.begin() ; it != vStore.end() && convex ; ++it)
	{
		Dart e = *it;
		if (!m.isMarked(e))
		{
57
			m.markOrbit(EDGE, e) ;
Pierre Kraemer's avatar
Pierre Kraemer committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
			convex = isTetrahedronWellOriented<PFP>(map, e, positions, CCW) ;
		}
	}

	return convex;
}

template <typename PFP>
bool isPointInVolume(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& positions, const typename PFP::VEC3& point)
{
	typedef typename PFP::VEC3 VEC3;

	//number of intersection between a ray and the volume must be odd
	int countInter = 0;

// 	if(isConvex<PFP>(map,d)) {
74
// 		CGoGNout << "optimize point in volume" << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
// 	}

	std::list<Dart> visitedFaces;			// Faces that are traversed
	visitedFaces.push_back(d);				// Start with the face of d
	std::list<Dart>::iterator face;
	VEC3 dir(0.5f,0.5f,0.5f);
	VEC3 inter;
	std::vector<VEC3> interPrec;

	DartMarkerStore mark(map);					// Lock a marker
	// For every face added to the list
	// search the number of faces intersected by a ray whose origin is the tested point
	for (face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
	{
		if (!mark.isMarked(*face))			// Face has not been visited yet
		{
			bool alreadyfound = false;
			bool interRes = Algo::Geometry::intersectionLineConvexFace<PFP>(map, *face, positions, point, dir, inter);
			if(interRes && (dir * (inter-point)) >= 0.0f)
			{
				if(interPrec.size() > 0)
				{
					for(typename std::vector<VEC3>::iterator it = interPrec.begin(); !alreadyfound && it != interPrec.end(); ++it)
					{
						if((*it)[0] == inter[0])
							alreadyfound = true;
					}
				}
				if(!alreadyfound)
				{
					++countInter;
					interPrec.push_back(inter);
				}

				// add non visited adjacent faces to the list of face
				Dart dNext = *face ;
				do
				{
					mark.mark(dNext);					// Mark
					Dart adj = map.phi2(dNext);			// Get adjacent face
					if (adj != dNext && !mark.isMarked(adj))
						visitedFaces.push_back(adj);	// Add it
					dNext = map.phi1(dNext) ;
				} while(dNext != *face) ;
			}
		}
	}

	//if the point is in the volume there is an odd number of intersection with all faces with any direction
	return (countInter % 2) == 1;
}

template <typename PFP>
bool isPointInConvexVolume(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& positions, const typename PFP::VEC3& point, bool CCW)
{
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL;

	bool inside = true;
	std::list<Dart> visitedFaces;			// Faces that are traversed
	visitedFaces.push_back(d);				// Start with the face of d
	std::list<Dart>::iterator face;
	VEC3 N;

	DartMarkerStore mark(map);					// Lock a marker
	for (face = visitedFaces.begin(); inside && face != visitedFaces.end(); ++face)
	{
		if (!mark.isMarked(*face))
		{
			Geom::Plane3D<REAL> p = Geometry::facePlane<PFP>(map, *face, positions);
			Geom::Orientation3D o3d = p.orient(point);
			if(CCW)
			{
				if(o3d == Geom::OVER)
					inside = false;
			}
			else if(o3d == Geom::UNDER)
				inside = false;

			Dart dNext = *face ;
			do
			{
				mark.mark(dNext);					// Mark
				Dart adj = map.phi2(dNext);			// Get adjacent face
				if (adj != dNext && !mark.isMarked(adj))
					visitedFaces.push_back(adj);	// Add it
				dNext = map.phi1(dNext) ;
			} while(dNext != *face) ;
		}
	}

	//if the point is in the volume there is an odd number of intersection with all faces with any direction
	return inside;
}

template <typename PFP>
bool isPointInConvexFace(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& positions, const typename PFP::VEC3& point, bool CCW )
{
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL;

	bool inside = true;

	Geom::Plane3D<REAL> pl = Geometry::facePlane<PFP>(map,d,positions);
	Geom::Orientation3D o3d = pl.orient(point);
	if(o3d == Geom::ON)
	{
		Dart dd =d;
		do
		{
			VEC3 N = pl.normal();
			VEC3 v2(positions[map.phi1(dd)]-positions[dd]);
			VEC3 norm2 = N^v2;
			Geom::Plane3D<REAL> pl2(norm2,positions[dd]);
			o3d = pl2.orient(point);
			if(CCW)
			{
				if(o3d == Geom::UNDER)
					inside = false;
			}
			else if(o3d == Geom::OVER)
				inside =false;

			dd = map.phi1(dd);
		} while(dd != d);

		return inside;
	}

	return false;
}

template <typename PFP>
bool isPointInConvexFace2D(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& positions, const typename PFP::VEC3& point, bool CCW )
{
	typedef typename PFP::VEC3 VEC3 ;
	typedef typename PFP::REAL REAL;

213
// 	CGoGNout << "point " << point << "d " << d << "faceDeg " << map.faceDegree(d) << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
214 215 216 217 218 219

	bool convex=true;
	Geom::Orientation2D o2d;
	Dart dd = d;
	do
	{
220
// 		CGoGNout << "dd " << dd << " ";
Pierre Kraemer's avatar
Pierre Kraemer committed
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
		o2d = Geom::testOrientation2D(point, positions[dd], positions[map.phi1(dd)]);
		if(CCW)
		{
			if(o2d == Geom::RIGHT)
				convex= false;
		}
		else if(o2d == Geom::LEFT)
			return false;

		dd = map.phi1(dd);
	} while(dd != d);

	return convex;
}

template <typename PFP>
bool isPointOnEdge(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& positions, const typename PFP::VEC3& point)
{
// 	typedef typename PFP::REAL REAL;
// 	typedef typename PFP::VEC3 VEC3 ;
// 
// 	VEC3 v1 = Algo::Geometry::vectorOutOfDart<PFP>(map, d, positions);
// 	VEC3 v2(point - positions[d]);
// 
// 	v1.normalize();
// 	v2.normalize();
// 
// 	return fabs(REAL(1) - (v1*v2)) < std::numeric_limits<REAL>::min();

	if((Algo::Geometry::isPointOnHalfEdge<PFP>(map,d,positions,point) && Algo::Geometry::isPointOnHalfEdge<PFP>(map,map.phi2(d),positions,point)) || Algo::Geometry::isPointOnVertex<PFP>(map,d,positions,point) || Algo::Geometry::isPointOnVertex<PFP>(map,map.phi1(d),positions,point))
		return true;
	else {
253 254
		CGoGNout << " point " << point << CGoGNendl;
		CGoGNout << " d1 " << positions[d] << " d2 " << positions[map.phi2(d)] << CGoGNendl;
Pierre Kraemer's avatar
Pierre Kraemer committed
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
		return false;
	}
}

template <typename PFP>
bool isPointOnHalfEdge(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& positions, const typename PFP::VEC3& point)
{
	typedef typename PFP::REAL REAL;
	typedef typename PFP::VEC3 VEC3;

	VEC3 v1 = Algo::Geometry::vectorOutOfDart<PFP>(map,d,positions);
	VEC3 v2(point - positions[d]);

	v1.normalize();
	v2.normalize();

	return (v1*v2) >= -REAL(0.00001);
}

template <typename PFP>
bool isPointOnVertex(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& positions, const typename PFP::VEC3& point)
{
// 	typedef typename PFP::REAL REAL;
// 	typedef typename PFP::VEC3 VEC3 ;
// 
// 	VEC3 v(point - positions[d]);
// 
// 	return v.norm2() < std::numeric_limits<REAL>::min()*std::numeric_limits<REAL>::min();

	return Geom::arePointsEquals(point,positions[d]);
}

template <typename PFP>
bool isConvexFaceInOrIntersectingTetrahedron(typename PFP::MAP& map, Dart d, const typename PFP::TVEC3& positions, const typename PFP::VEC3 points[4], bool CCW)
{
	typedef typename PFP::VEC3 VEC3 ;

	Dart it = d;
	do
	{
		if(Geom::isPointInTetrahedron(points, positions[d], CCW))
			return true;
		it = map.phi1(it);
	} while(it!=d);

	VEC3 inter;
	if( intersectionSegmentConvexFace(map,d,positions,points[0],points[1],inter)
	|| 	intersectionSegmentConvexFace(map,d,positions,points[1],points[2],inter)
	|| 	intersectionSegmentConvexFace(map,d,positions,points[2],points[0],inter)
	|| 	intersectionSegmentConvexFace(map,d,positions,points[0],points[3],inter)
	|| 	intersectionSegmentConvexFace(map,d,positions,points[1],points[3],inter)
	|| 	intersectionSegmentConvexFace(map,d,positions,points[2],points[3],inter)
	)
		return true;

	return false;
}

}

}

}