Commit 8259ea87 authored by Pierre Kraemer's avatar Pierre Kraemer

update several Algo/Geometry functions

parent f0e1b718
...@@ -25,7 +25,6 @@ ...@@ -25,7 +25,6 @@
#ifndef __ALGO_GEOMETRY_BOUNDINGBOX_H__ #ifndef __ALGO_GEOMETRY_BOUNDINGBOX_H__
#define __ALGO_GEOMETRY_BOUNDINGBOX_H__ #define __ALGO_GEOMETRY_BOUNDINGBOX_H__
#include "Geometry/basic.h"
#include "Geometry/bounding_box.h" #include "Geometry/bounding_box.h"
#include "Topology/generic/attributeHandler.h" #include "Topology/generic/attributeHandler.h"
#include "Topology/generic/traversor/traversorCell.h" #include "Topology/generic/traversor/traversorCell.h"
...@@ -43,9 +42,7 @@ template <typename PFP> ...@@ -43,9 +42,7 @@ template <typename PFP>
Geom::BoundingBox<typename PFP::VEC3> computeBoundingBox(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position) Geom::BoundingBox<typename PFP::VEC3> computeBoundingBox(typename PFP::MAP& map, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position)
{ {
Geom::BoundingBox<typename PFP::VEC3> bb ; Geom::BoundingBox<typename PFP::VEC3> bb ;
TraversorV<typename PFP::MAP> t(map) ; foreach_cell<VERTEX>(map, [&] (Vertex v) { bb.addPoint(position[v]) ; });
for(Dart d = t.begin(); d != t.end(); d = t.next())
bb.addPoint(position[d]) ;
return bb ; return bb ;
} }
......
...@@ -41,7 +41,7 @@ namespace Geometry ...@@ -41,7 +41,7 @@ namespace Geometry
* Test if an edge bounded by 2 faces is convex or concave * Test if an edge bounded by 2 faces is convex or concave
*/ */
template <typename PFP> template <typename PFP>
bool isEdgeConvexe(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position) ; bool isEdgeConvex(typename PFP::MAP& map, Edge e, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position) ;
} // namespace Geometry } // namespace Geometry
......
...@@ -40,14 +40,14 @@ namespace Geometry ...@@ -40,14 +40,14 @@ namespace Geometry
{ {
template <typename PFP> template <typename PFP>
bool isEdgeConvexe(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position) bool isEdgeConvex(typename PFP::MAP& map, Edge e, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position)
{ {
typedef typename PFP::VEC3 VEC3 ; typedef typename PFP::VEC3 VEC3 ;
const VEC3 n = faceNormal<PFP>(map, d, position); const VEC3 n = faceNormal<PFP>(map, e.dart, position);
const VEC3 e = vectorOutOfDart<PFP>(map, map.phi1(map.phi2(d)), position) ; const VEC3 ee = vectorOutOfDart<PFP>(map, map.phi1(map.phi2(e.dart)), position) ;
if((e * n) > 0) if((ee * n) < 0)
return true; return true;
else else
return false; return false;
......
...@@ -37,38 +37,36 @@ namespace Geometry ...@@ -37,38 +37,36 @@ namespace Geometry
/** /**
* compute squared distance from point to the plane of a planar face * compute squared distance from point to the plane of a planar face
* @param map the map * @param map the map
* @param d a dart of the face * @param f a face
* @param position the vertex attribute storing positions
* @param P the point * @param P the point
* @return the squared distance to tha plane * @return the squared distance to the plane
*/ */
template <typename PFP> template <typename PFP>
typename PFP::REAL squaredDistancePoint2FacePlane(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const VEC3& P) ; typename PFP::REAL squaredDistancePoint2FacePlane(typename PFP::MAP& map, Face f, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const VEC3& P) ;
/** /**
* compute squared distance from point to face (assuming face is convex) * compute squared distance from point to face (assuming face is convex)
* Algo: min distance of each subtriangle of face (not optimum ?) * Algo: min distance of each subtriangle of face (not optimum ?)
* @param map the map * @param map the map
* @param d a dart of the face * @param f a face
* @param position the vertex attribute storing positions
* @param P the point * @param P the point
* @return the squared distance * @return the squared distance
*/ */
template <typename PFP> template <typename PFP>
typename PFP::REAL squaredDistancePoint2Face(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const VEC3& P) ; typename PFP::REAL squaredDistancePoint2Face(typename PFP::MAP& map, Face f, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const VEC3& P) ;
/** /**
* compute squared distance from point to an edge * compute squared distance from point to an edge
* @param map the map * @param map the map
* @param d a dart of the edge * @param e an edge
* @param position the vertex attribute storing positions
* @param P the point * @param P the point
* @return the squared distance * @return the squared distance
*/ */
template <typename PFP> template <typename PFP>
typename PFP::REAL squaredDistancePoint2Edge(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const VEC3& P) ; typename PFP::REAL squaredDistancePoint2Edge(typename PFP::MAP& map, Edge e, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const VEC3& P) ;
template <typename PFP>
bool isPlanar(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position);
} // namespace Geometry } // namespace Geometry
......
...@@ -23,8 +23,6 @@ ...@@ -23,8 +23,6 @@
*******************************************************************************/ *******************************************************************************/
#include "Geometry/distances.h" #include "Geometry/distances.h"
#include "Algo/Geometry/normal.h"
#include <limits>
namespace CGoGN namespace CGoGN
{ {
...@@ -36,64 +34,50 @@ namespace Geometry ...@@ -36,64 +34,50 @@ namespace Geometry
{ {
template <typename PFP> template <typename PFP>
bool isPlanar(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position) typename PFP::REAL squaredDistancePoint2FacePlane(typename PFP::MAP& map, Face f, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const VEC3& P)
{ {
if (map.phi<111>(d)==d) Vertex v(f.dart);
return true; const typename PFP::VEC3& A = position[v];
v.dart = map.phi1(v.dart);
const typename PFP::VEC3& B = position[v];
v.dart = map.phi1(v.dart);
const typename PFP::VEC3& C = position[v];
typename PFP::VEC3 No = Algo::Surface::Geometry::triangleNormal(map, d, position) ; return Geom::squaredDistancePoint2TrianglePlane(P, A, B, C);
Dart e = map.phi<11>(d);
while (e != d)
{
typename PFP::VEC3 n = Algo::Surface::Geometry::triangleNormal(map, e, position) ;
e = map.phi1(e);
if (e!=d)
e = map.phi1(e);
}
} }
template <typename PFP> template <typename PFP>
typename PFP::REAL squaredDistancePoint2FacePlane(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const VEC3& P) typename PFP::REAL squaredDistancePoint2Face(typename PFP::MAP& map, Face f, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const VEC3& P)
{
const typename PFP::VEC3& A = position[d];
d = map.phi1(d);
const typename PFP::VEC3& B = position[d];
d = map.phi1(d);
const typename PFP::VEC3& C = position[d];
return Geom::squaredDistancePoint2TrianglePlane(P,A,B,C);
}
template <typename PFP>
typename PFP::REAL squaredDistancePoint2Face(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const VEC3& P)
{ {
typedef typename PFP::REAL REAL; typedef typename PFP::REAL REAL;
const typename PFP::VEC3& A = position[d];
const typename PFP::VEC3& A = position[f.dart];
Dart d = map.phi1(f.dart);
Dart e = map.phi1(d); Dart e = map.phi1(d);
Dart f = map.phi1(e); REAL dist2 = Geom::squaredDistancePoint2Triangle(P, A, position[d], position[e]);
REAL dist2 = Geom::squaredDistancePoint2Triangle(P,A,position[e],position[f]); d = e;
e = f; e = map.phi1(d);
f = map.phi1(e);
while (f != d) while (e != f.dart)
{ {
REAL d2 = Geom::squaredDistancePoint2Triangle(P,A,position[e],position[f]); REAL d2 = Geom::squaredDistancePoint2Triangle(P, A, position[d], position[e]);
if (d2 < dist2) if (d2 < dist2)
dist2 = d2; dist2 = d2;
e = f; d = e;
f = map.phi1(e); e = map.phi1(d);
} }
return dist2; return dist2;
} }
template <typename PFP> template <typename PFP>
typename PFP::REAL squaredDistancePoint2Edge(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const VEC3& P) typename PFP::REAL squaredDistancePoint2Edge(typename PFP::MAP& map, Edge e, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const VEC3& P)
{ {
const typename PFP::VEC3& A = position[d]; const typename PFP::VEC3& A = position[e.dart];
typename PFP::VEC3& AB = position[map.phi1(d)]-A; typename PFP::VEC3 AB = position[map.phi1(e.dart)] - A;
typename PFP::REAL AB2 = AB*AB; typename PFP::REAL AB2 = AB * AB;
return Geom::squaredDistanceSeg2Point(A,AB,AB2,P) ; return Geom::squaredDistanceSeg2Point(A, AB, AB2, P) ;
} }
} // namespace Geometry } // namespace Geometry
......
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
#include "Geometry/basic.h" #include "Geometry/basic.h"
#include "Algo/Geometry/normal.h" #include "Algo/Geometry/normal.h"
#include "Topology/generic/traversorCell.h" #include "Topology/generic/traversor/traversorCell.h"
namespace CGoGN namespace CGoGN
{ {
......
...@@ -45,61 +45,61 @@ namespace Geometry ...@@ -45,61 +45,61 @@ namespace Geometry
/** /**
* test if the volume is convex * test if the volume is convex
* @param the map * @param the map
* @param a dart of the volume * @param a volume
* @param true if the faces of the volume must be in CCW order (default=true) * @param true if the faces of the volume must be in CCW order (default=true)
*/ */
template <typename PFP> template <typename PFP>
bool isConvex(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, bool CCW, unsigned int thread = 0); bool isConvex(typename PFP::MAP& map, Vol v, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, bool CCW, unsigned int thread = 0);
/** /**
* test if a point is inside a volume * test if a point is inside a volume
* @param map the map * @param map the map
* @param d a dart defining the volume * @param a volume
* @param the point * @param the point
*/ */
template <typename PFP> template <typename PFP>
bool isPointInVolume(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3& point); bool isPointInVolume(typename PFP::MAP& map, Vol v, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3& point);
/** /**
* test if a point is inside a volume * test if a point is inside a volume
* @param map the map * @param map the map
* @param d a dart defining a convex volume * @param a convex volume
* @param the point * @param the point
*/ */
template <typename PFP> template <typename PFP>
bool isPointInConvexVolume(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3& point, bool CCW = true); bool isPointInConvexVolume(typename PFP::MAP& map, Vol v, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3& point, bool CCW = true);
/** /**
* test if a point is inside a face in a plane * test if a point is inside a face
* @param map the map * @param map the map
* @param d a dart defining the face * @param a face
* @param the point * @param the point
*/ */
template <typename PFP> template <typename PFP>
bool isPointInConvexFace2D(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3& point, bool CCW = true); bool isPointInConvexFace(typename PFP::MAP& map, Face f, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3& point, bool CCW);
/** /**
* test if a point is inside a face * test if a point is inside a face in a plane
* @param map the map * @param map the map
* @param d a dart defining the face * @param a face
* @param the point * @param the point
*/ */
template <typename PFP> template <typename PFP>
bool isPointInConvexFace(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3& point, bool CCW); bool isPointInConvexFace2D(typename PFP::MAP& map, Face f, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3& point, bool CCW = true);
/** /**
* test if a point is on an edge * test if a point is on an edge
* @param map the map * @param map the map
* @param d a dart defining the edge * @param an edge
* @param the point * @param the point
*/ */
template <typename PFP> template <typename PFP>
bool isPointOnEdge(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3& point); bool isPointOnEdge(typename PFP::MAP& map, Edge e, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3& point);
/** /**
* test if a point is on an half-edge defined by a dart * test if a point is on an half-edge defined by a dart
* @param map the map * @param map the map
* @param d a dart defining the edge * @param a Dart
* @param the point * @param the point
*/ */
template <typename PFP> template <typename PFP>
...@@ -108,23 +108,22 @@ bool isPointOnHalfEdge(typename PFP::MAP& map, Dart d, const VertexAttribute<typ ...@@ -108,23 +108,22 @@ bool isPointOnHalfEdge(typename PFP::MAP& map, Dart d, const VertexAttribute<typ
/** /**
* test if a point is on a vertex * test if a point is on a vertex
* @param map the map * @param map the map
* @param d a dart defining the vertex * @param a vertex
* @param the point * @param the point
*/ */
template <typename PFP> template <typename PFP>
bool isPointOnVertex(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3& point); bool isPointOnVertex(typename PFP::MAP& map, Vertex v, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3& point);
/** /**
* test if a face is intersecting or totally included in a tetrahedron * test if a face is intersecting or totally included in a tetrahedron
* TODO to test * TODO to test
* @param map the map * @param map the map
* @param d a dart defining the face * @param f a face
* @param the points of the tetra (0,1,2) the first face and (3) the last point of the tetra, in well oriented order * @param the points of the tetra (0,1,2) the first face and (3) the last point of the tetra, in well oriented order
* @param true if the faces of the tetra are in CCW order (default=true) * @param true if the faces of the tetra are in CCW order (default=true)
*/ */
template <typename PFP> template <typename PFP>
bool isConvexFaceInOrIntersectingTetrahedron(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3 points[4], bool CCW); bool isConvexFaceInOrIntersectingTetrahedron(typename PFP::MAP& map, Face f, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& positions, const typename PFP::VEC3 points[4], bool CCW);
} // namespace Geometry } // namespace Geometry
......
...@@ -66,7 +66,7 @@ bool isConvex(typename PFP::MAP& map, Vol v, const VertexAttribute<typename PFP: ...@@ -66,7 +66,7 @@ bool isConvex(typename PFP::MAP& map, Vol v, const VertexAttribute<typename PFP:
// TODO add thread Pameter // TODO add thread Pameter
template <typename PFP> template <typename PFP>
bool isPointInVolume(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const typename PFP::VEC3& point) bool isPointInVolume(typename PFP::MAP& map, Vol v, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const typename PFP::VEC3& point)
{ {
typedef typename PFP::VEC3 VEC3; typedef typename PFP::VEC3 VEC3;
...@@ -75,74 +75,79 @@ bool isPointInVolume(typename PFP::MAP& map, Dart d, const VertexAttribute<typen ...@@ -75,74 +75,79 @@ bool isPointInVolume(typename PFP::MAP& map, Dart d, const VertexAttribute<typen
int countInter2 = 0; int countInter2 = 0;
VEC3 dir(0.9f,1.1f,1.3f); VEC3 dir(0.9f,1.1f,1.3f);
std::vector<VEC3> interPrec; std::vector<VEC3> interPrec;
interPrec.reserve(16); interPrec.reserve(16);
std::vector<Dart> visitedFaces; // Faces that are traversed
std::vector<Face> visitedFaces; // Faces that are traversed
visitedFaces.reserve(64); visitedFaces.reserve(64);
visitedFaces.push_back(d); // Start with the face of d Face f(v.dart);
visitedFaces.push_back(f); // Start with the first face of v
DartMarkerStore<typename PFP::MAP> mark(map); DartMarkerStore<typename PFP::MAP> mark(map);
mark.markOrbit<FACE>(d) ; mark.markOrbit(f) ;
for(unsigned int iface = 0; iface != visitedFaces.size(); ++iface)
for(unsigned int iface = 0; iface != visitedFaces.size(); ++iface)
{ {
Dart e = visitedFaces[iface]; f = visitedFaces[iface];
VEC3 inter; VEC3 inter;
bool interRes = intersectionLineConvexFace<PFP>(map, e, position, point, dir, inter); bool interRes = intersectionLineConvexFace<PFP>(map, f, position, point, dir, inter);
if (interRes) if (interRes)
{ {
// check if already intersect on same point (a vertex certainly) // check if already intersect on same point (a vertex certainly)
bool alreadyfound = false; bool alreadyfound = false;
for(typename std::vector<VEC3>::iterator it = interPrec.begin(); !alreadyfound && it != interPrec.end(); ++it) for(typename std::vector<VEC3>::iterator it = interPrec.begin(); !alreadyfound && it != interPrec.end(); ++it)
{ {
if (Geom::arePointsEquals(*it,inter)) if (Geom::arePointsEquals(*it, inter))
alreadyfound = true; alreadyfound = true;
} }
if (!alreadyfound) if (!alreadyfound)
{ {
float v = dir * (inter-point); float v = dir * (inter - point);
if (v>0) if (v > 0)
++countInter; ++countInter;
if (v<0) if (v < 0)
++countInter2; ++countInter2;
interPrec.push_back(inter); interPrec.push_back(inter);
} }
} }
// add all face neighbours to the table // add all face neighbours to the table
Dart currentFace = e; foreach_adjacent2<EDGE>(map, f, [&] (Face ff)
do
{ {
Dart ee = map.phi2(e) ; if(!mark.isMarked(ff)) // not already marked
if(!mark.isMarked(ee)) // not already marked
{ {
visitedFaces.push_back(ee) ; visitedFaces.push_back(ff) ;
mark.markOrbit<FACE>(ee) ; mark.markOrbit(ff) ;
} }
e = map.phi1(e) ; });
} while(e != currentFace) ;
} }
//if the point is in the volume there is an odd number of intersection with all faces with any direction //if the point is in the volume there is an odd number of intersection with all faces with any direction
return ((countInter % 2) != 0) && ((countInter2 % 2) != 0); // return (countInter % 2) == 1; return ((countInter % 2) != 0) && ((countInter2 % 2) != 0); // return (countInter % 2) == 1;
} }
template <typename PFP> template <typename PFP>
bool isPointInConvexVolume(typename PFP::MAP& map, Dart d, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const typename PFP::VEC3& point, bool CCW) bool isPointInConvexVolume(typename PFP::MAP& map, Vol v, const VertexAttribute<typename PFP::VEC3, typename PFP::MAP::IMPL>& position, const typename PFP::VEC3& point, bool CCW)
{ {
typedef typename PFP::VEC3 VEC3 ; typedef typename PFP::VEC3 VEC3 ;
typedef typename PFP::REAL REAL; typedef typename PFP::REAL REAL;
std::list<Dart> visitedFaces; // Faces that are traversed std::vector<Face> visitedFaces; // Faces that are traversed
visitedFaces.push_back(d); // Start with the face of d visitedFaces.reserve(64);
std::list<Dart>::iterator face; Face f(v.dart);
VEC3 N; visitedFaces.push_back(f); // Start with the first face of v
DartMarkerStore<typename PFP::MAP> mark(map); // Lock a marker DartMarkerStore<typename PFP::MAP> mark(map); // Lock a marker
for (face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
for (std::vector<Face>::iterator face = visitedFaces.begin(); face != visitedFaces.end(); ++face)
{ {
if (!mark.isMarked(*face)) f = *face;
if (!mark.isMarked(f))
{ {
Geom::Plane3D<REAL> p = facePlane<PFP>(map, *face, position); mark.markOrbit(f);
Geom::Plane3D<REAL> p = facePlane<PFP>(map, f, position);
Geom::Orientation3D o3d = p.orient(point); Geom::Orientation3D o3d = p.orient(point);
if(CCW) if(CCW)
{ {
...@@ -152,15 +157,12 @@ bool isPointInConvexVolume(typename PFP::MAP& map, Dart d, const VertexAttribute ...@@ -152,15 +157,12 @@ bool isPointInConvexVolume(typename PFP::MAP& map, Dart d, const VertexAttribute
else if(o3d == Geom::UNDER) else if(o3d == Geom::UNDER)
return false; return false;
Dart dNext = *face ; // add all face neighbours to the table
do foreach_adjacent2<EDGE>(map, f, [&] (Face ff)
{ {
mark.mark(dNext); // Mark if(!mark.isMarked(ff)) // not already marked
Dart adj = map.phi2(dNext); // Get adjacent face visitedFaces.push_back(ff) ;
if (adj != dNext && !mark.isMarked(adj)) });
visitedFaces.push_back(adj); // Add it