136 template <
typename Real>
149 template <
typename Real>
175 std::vector<Real>
const&
height,
int maxPosIndex, Result&
result);
179 template <
typename Real>
182 std::vector<Vector3<Real>>
const& polygon,
Plane3<Real> const& plane)
186 int const numVertices =
static_cast<int>(polygon.size());
190 result.intersect =
false;
196 int numPositive = 0, numNegative = 0, numZero = 0;
197 for (
int i = 0; i < numVertices; ++i)
199 Real
height =
Dot(plane.normal, polygon[i]) - plane.constant;
204 else if (height < (Real)0)
219 result.intersect =
true;
222 else if (numZero > 0)
225 result.intersect =
true;
226 result.configuration =
232 result.intersect =
false;
236 else if (numNegative > 0)
241 result.intersect =
true;
242 result.configuration =
248 result.intersect =
false;
255 result.intersect =
true;
262 template <
typename Real>
265 std::vector<Vector3<Real>>
const& polygon,
Plane3<Real> const& plane)
269 int const numVertices =
static_cast<int>(polygon.size());
284 std::vector<Real>
height(polygon.size());
285 std::vector<int> zeroHeightIndices;
286 int numPositive = 0, numNegative = 0;
287 Real maxPosHeight = -std::numeric_limits<Real>::max();
288 Real maxNegHeight = std::numeric_limits<Real>::max();
289 int maxPosIndex = -1, maxNegIndex = -1;
290 for (
int i = 0; i < numVertices; ++i)
292 height[i] =
Dot(plane.normal, polygon[i]) - plane.constant;
296 if (
height[i] > maxPosHeight)
302 else if (
height[i] < (Real)0)
305 if (
height[i] < maxNegHeight)
313 zeroHeightIndices.push_back(i);
327 Real cmax = fabs(plane.normal[0]);
328 Real cvalue = fabs(plane.normal[1]);
334 cvalue = fabs(plane.normal[2]);
341 bool doSwap = (plane.normal[imax] < (Real)0);
348 std::swap(maxPosIndex, maxNegIndex);
351 SplitPolygon(polygon,
height, maxPosIndex, result);
355 std::swap(result.positivePolygon, result.negativePolygon);
360 if (zeroHeightIndices.size() > 0)
363 if (zeroHeightIndices.size() == 1)
366 result.intersection.push_back(polygon[zeroHeightIndices[0]]);
371 result.intersection.push_back(polygon[zeroHeightIndices[0]]);
372 result.intersection.push_back(polygon[zeroHeightIndices[1]]);
380 result.positivePolygon = polygon;
383 else if (numNegative > 0)
385 if (zeroHeightIndices.size() > 0)
388 if (zeroHeightIndices.size() == 1)
391 result.intersection.push_back(polygon[zeroHeightIndices[0]]);
396 result.intersection.push_back(polygon[zeroHeightIndices[0]]);
397 result.intersection.push_back(polygon[zeroHeightIndices[1]]);
405 result.negativePolygon = polygon;
411 result.intersection = polygon;
417 template <
typename Real>
420 int maxPosIndex, Result&
result)
423 int const numVertices =
static_cast<int>(polygon.size());
424 std::list<Vector3<Real>> positiveList;
425 positiveList.push_back(polygon[maxPosIndex]);
426 int end0 = maxPosIndex, end0prev = -1;
427 for (
int i = 0; i < numVertices; ++i)
429 end0prev = (end0 + numVertices - 1) % numVertices;
430 if (height[end0prev] >= (Real)0)
432 positiveList.push_front(polygon[end0prev]);
441 int end1 = maxPosIndex, end1next = -1;
442 for (
int i = 0; i < numVertices; ++i)
444 end1next = (end1 + 1) % numVertices;
445 if (height[end1next] >= (Real)0)
447 positiveList.push_back(polygon[end1next]);
456 int index = end1next;
457 std::list<Vector3<Real>> negativeList;
458 for (
int i = 0; i < numVertices; ++i)
460 negativeList.push_back(polygon[index]);
461 index = (index + 1) % numVertices;
469 if (height[end0] > 0)
471 Real
t = -height[end0prev] / (height[end0] - height[end0prev]);
472 Real omt = (Real)1 - t;
473 Vector3<Real> V = omt * polygon[end0prev] + t * polygon[end0];
474 positiveList.push_front(V);
475 negativeList.push_back(V);
476 result.intersection.push_back(V);
480 negativeList.push_back(polygon[end0]);
481 result.intersection.push_back(polygon[end0]);
484 if (height[end1] > 0)
486 Real
t = -height[end1next] / (height[end1] - height[end1next]);
487 Real omt = (Real)1 - t;
488 Vector3<Real> V = omt * polygon[end1next] + t * polygon[end1];
489 positiveList.push_back(V);
490 negativeList.push_front(V);
491 result.intersection.push_back(V);
495 negativeList.push_front(polygon[end1]);
496 result.intersection.push_back(polygon[end1]);
499 result.positivePolygon.reserve(positiveList.size());
500 for (
auto const&
p : positiveList)
502 result.positivePolygon.push_back(
p);
505 result.negativePolygon.reserve(negativeList.size());
506 for (
auto const&
p : negativeList)
508 result.negativePolygon.push_back(
p);
std::vector< Vector3< Real > > negativePolygon
ConvexPolygonPlaneConfiguration configuration
DualQuaternion< Real > Dot(DualQuaternion< Real > const &d0, DualQuaternion< Real > const &d1)
ConvexPolygonPlaneConfiguration configuration
GLint GLsizei GLsizei height
std::vector< Vector3< Real > > positivePolygon
ConvexPolygonPlaneConfiguration
std::vector< Vector3< Real > > intersection
GLfloat GLfloat GLfloat GLfloat h
Result operator()(Type0 const &primitive0, Type1 const &primitive1)