coastline 0.0.4
Loading...
Searching...
No Matches
clipplane.h
Go to the documentation of this file.
1#ifndef COASTLINE_CLIP_PLANE_H
2#define COASTLINE_CLIP_PLANE_H
3
4#include <concepts>
5#include <type_traits>
6
14template <typename Polygon, typename Distance, typename Interpolate, typename Recycle>
15concept LinkedPolygonClipping = requires(Polygon *polygon,
16 decltype(polygon->pHead) vertex,
17 Distance distance,
18 Interpolate interpolate,
19 Recycle recycle) {
20 requires std::is_pointer_v<decltype(polygon->pHead)>;
21 { polygon->nCount } -> std::same_as<int &>;
22 { vertex->pNext } -> std::same_as<decltype(polygon->pHead) &>;
23 { distance(vertex) } -> std::convertible_to<float>;
24 { interpolate(vertex, vertex, 0.0f) } -> std::same_as<decltype(polygon->pHead)>;
25 { recycle(vertex) } -> std::same_as<void>;
26};
27
40template <typename Polygon, typename Distance, typename Interpolate, typename Recycle>
42static inline void ClipLinkedPolygonPlane(Polygon *pPolygon,
43 Distance distance,
44 Interpolate interpolate,
45 Recycle recycle) {
46#ifdef ENABLE_PATCHES
47 if (pPolygon->pHead == nullptr || pPolygon->nCount <= 0) {
48 pPolygon->nCount = 0;
49 return;
50 }
51#endif
52 auto *pCurrent = pPolygon->pHead;
53 auto *pNext = pCurrent->pNext;
54 auto *pPrevious = pCurrent;
55 decltype(pCurrent) pRemoved = nullptr;
56 int nRemaining = pPolygon->nCount;
57#ifdef ENABLE_PATCHES
58 auto discard = [&] {
59 auto *head = pPrevious->pNext;
60 auto *vertex = head;
61 do {
62 auto *next = vertex->pNext;
63 recycle(vertex);
64 vertex = next;
65 } while (vertex != head);
66 while (pRemoved) {
67 auto *removed = pRemoved;
68 pRemoved = removed->pNext;
69 recycle(removed);
70 }
71 pPolygon->pHead = nullptr;
72 pPolygon->nCount = 0;
73 };
74#endif
75 while (nRemaining > 0) {
76 float fNextDistance = distance(pNext);
77 float fCurrentDistance = distance(pCurrent);
78 auto *pFollowing = pNext->pNext;
79 if (fNextDistance >= 0.0f) {
80 if (fCurrentDistance >= 0.0f) {
81 pPrevious = pNext;
82 } else {
83 float fFraction = static_cast<float>(
84 fCurrentDistance /
85 (static_cast<long double>(fCurrentDistance) - fNextDistance));
86 auto *pIntersection = interpolate(pCurrent, pNext, fFraction);
87#ifdef ENABLE_PATCHES
88 if (pIntersection == nullptr) {
89 discard();
90 return;
91 }
92#endif
93 pIntersection->pNext = pPrevious->pNext;
94 pPrevious->pNext = pIntersection;
95 pPrevious = pNext;
96 ++pPolygon->nCount;
97 }
98 } else {
99 if (pPrevious->pNext) {
100 pPrevious->pNext = pPrevious->pNext->pNext;
101 }
102 pNext->pNext = pRemoved;
103 pRemoved = pNext;
104 if (fCurrentDistance >= 0.0f) {
105 float fFraction = static_cast<float>(
106 fNextDistance / (static_cast<long double>(fNextDistance) - fCurrentDistance));
107 auto *pIntersection = interpolate(pNext, pCurrent, fFraction);
108#ifdef ENABLE_PATCHES
109 if (pIntersection == nullptr) {
110 discard();
111 return;
112 }
113#endif
114 pIntersection->pNext = pPrevious->pNext;
115 pPrevious->pNext = pIntersection;
116 pPrevious = pIntersection;
117 } else {
118 --pPolygon->nCount;
119 }
120 }
121 pCurrent = pNext;
122 pNext = pFollowing;
123 if (pPolygon->nCount < 2) {
124 break;
125 }
126 --nRemaining;
127 }
128 pPolygon->pHead = pNext;
129 while (pRemoved) {
130 auto *pVertex = pRemoved;
131 pRemoved = pRemoved->pNext;
132 recycle(pVertex);
133 }
134}
135
136#endif /* COASTLINE_CLIP_PLANE_H */
static void ClipLinkedPolygonPlane(Polygon *pPolygon, Distance distance, Interpolate interpolate, Recycle recycle)
Clip a circular vertex list to the non-negative side of a plane.
Definition clipplane.h:42
Require a circular vertex list, signed distances, interpolation, and recycling operations.
Definition clipplane.h:15
T is_pointer_v