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 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 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
|
// Geometric Tools, LLC
// Copyright (c) 1998-2014
// Distributed under the Boost Software License, Version 1.0.
// http://www.boost.org/LICENSE_1_0.txt
// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
//
// File Version: 5.0.1 (2010/10/01)
#include "Wm5MathematicsPCH.h"
#include "Wm5ContSeparatePoints2.h"
#include "Wm5ConvexHull2.h"
namespace Wm5
{
//----------------------------------------------------------------------------
template <typename Real>
SeparatePoints2<Real>::SeparatePoints2 (int numPoints0,
const Vector2<Real>* points0, int numPoints1,
const Vector2<Real>* points1, Line2<Real>& separatingLine)
{
// Construct convex hull of point set 0.
ConvexHull2<Real> hull0(numPoints0, (Vector2<Real>*)points0, 0.001f,
false, Query::QT_INT64);
assertion(hull0.GetDimension() == 2,
"Code currently supports only noncollinear points\n");
int numEdges0 = hull0.GetNumSimplices();
const int* edges0 = hull0.GetIndices();
// Construct convex hull of point set 1.
ConvexHull2<Real> hull1(numPoints1,(Vector2<Real>*)points1,0.001f,
false,Query::QT_INT64);
assertion(hull1.GetDimension() == 2,
"Code currently supports only noncollinear points\n");
int numEdges1 = hull1.GetNumSimplices();
const int* edges1 = hull1.GetIndices();
// Test edges of hull 0 for possible separation of points.
int j0, j1, i0, i1, side0, side1;
Vector2<Real> lineNormal;
Real lineConstant;
for (j1 = 0, j0 = numEdges0-1; j1 < numEdges0; j0 = j1++)
{
// Look up edge (assert: i0 != i1 ).
i0 = edges0[j0];
i1 = edges0[j1];
// Compute potential separating line (assert: (xNor,yNor) != (0,0)).
separatingLine.Origin = points0[i0];
separatingLine.Direction = points0[i1] - points0[i0];
separatingLine.Direction.Normalize();
lineNormal = separatingLine.Direction.Perp();
lineConstant = lineNormal.Dot(separatingLine.Origin);
// Determine if hull 1 is on same side of line.
side1 = OnSameSide(lineNormal, lineConstant, numEdges1, edges1,
points1);
if (side1)
{
// Determine which side of line hull 0 lies.
side0 = WhichSide(lineNormal, lineConstant, numEdges0,
edges0, points0);
if (side0*side1 <= 0) // Line separates hulls.
{
mSeparated = true;
return;
}
}
}
// Test edges of hull 1 for possible separation of points.
for (j1 = 0, j0 = numEdges1-1; j1 < numEdges1; j0 = j1++)
{
// Look up edge (assert: i0 != i1 ).
i0 = edges1[j0];
i1 = edges1[j1];
// Compute perpendicular to edge (assert: (xNor,yNor) != (0,0)).
separatingLine.Origin = points1[i0];
separatingLine.Direction = points1[i1] - points1[i0];
separatingLine.Direction.Normalize();
lineNormal = separatingLine.Direction.Perp();
lineConstant = lineNormal.Dot(separatingLine.Origin);
// Determine if hull 0 is on same side of line.
side0 = OnSameSide(lineNormal, lineConstant, numEdges0, edges0,
points0);
if (side0)
{
// Determine which side of line hull 1 lies.
side1 = WhichSide(lineNormal, lineConstant, numEdges1,
edges1, points1);
if (side0*side1 <= 0) // Line separates hulls.
{
mSeparated = true;
return;
}
}
}
mSeparated = false;
}
//----------------------------------------------------------------------------
template <typename Real>
SeparatePoints2<Real>::operator bool ()
{
return mSeparated;
}
//----------------------------------------------------------------------------
template <typename Real>
int SeparatePoints2<Real>::OnSameSide (const Vector2<Real>& lineNormal,
Real lineConstant, int numEdges, const int* edges,
const Vector2<Real>* points)
{
// Test whether all points on same side of line Dot(N,X) = c.
Real c0;
int posSide = 0, negSide = 0;
for (int i1 = 0, i0 = numEdges-1; i1 < numEdges; i0 = i1++)
{
c0 = lineNormal.Dot(points[edges[i0]]);
if (c0 > lineConstant + Math<Real>::ZERO_TOLERANCE)
{
++posSide;
}
else if (c0 < lineConstant - Math<Real>::ZERO_TOLERANCE)
{
++negSide;
}
if (posSide && negSide)
{
// Line splits point set.
return 0;
}
c0 = lineNormal.Dot(points[edges[i1]]);
if (c0 > lineConstant + Math<Real>::ZERO_TOLERANCE)
{
++posSide;
}
else if (c0 < lineConstant - Math<Real>::ZERO_TOLERANCE)
{
++negSide;
}
if (posSide && negSide)
{
// Line splits point set.
return 0;
}
}
return (posSide ? +1 : -1);
}
//----------------------------------------------------------------------------
template <typename Real>
int SeparatePoints2<Real>::WhichSide (const Vector2<Real>& lineNormal,
Real lineConstant, int numEdges, const int* edges,
const Vector2<Real>* points)
{
// Establish which side of line hull is on.
Real c0;
for (int i1 = 0, i0 = numEdges-1; i1 < numEdges; i0 = i1++)
{
c0 = lineNormal.Dot(points[edges[i0]]);
if (c0 > lineConstant + Math<Real>::ZERO_TOLERANCE)
{
// Hull on positive side.
return +1;
}
if (c0 < lineConstant - Math<Real>::ZERO_TOLERANCE)
{
// Hull on negative side.
return -1;
}
c0 = lineNormal.Dot(points[edges[i1]]);
if (c0 > lineConstant + Math<Real>::ZERO_TOLERANCE)
{
// Hull on positive side.
return +1;
}
if (c0 < lineConstant - Math<Real>::ZERO_TOLERANCE)
{
// Hull on negative side.
return -1;
}
}
// Hull is effectively collinear.
return 0;
}
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// Explicit instantiation.
//----------------------------------------------------------------------------
template WM5_MATHEMATICS_ITEM
class SeparatePoints2<float>;
template WM5_MATHEMATICS_ITEM
class SeparatePoints2<double>;
//----------------------------------------------------------------------------
}
|