MCPcopy Create free account
hub / github.com/CesiumGS/cesium-native / closestPointPolygon

Function closestPointPolygon

CesiumGeospatial/src/S2CellBoundingVolume.cpp:260–289  ·  view source on GitHub ↗

* Finds closes point on the polygon, created by the given vertices, from * a point. The test point and the polygon are all on the same plane. * @private */

Source from the content-addressed store, hash-verified

258 * @private
259 */
260glm::dvec3 closestPointPolygon(
261 const glm::dvec3& p,
262 const std::array<glm::dvec3, 4>& vertices,
263 const std::array<glm::dvec3, 4>& edgeNormals) {
264 double minDistance = std::numeric_limits<double>::max();
265 glm::dvec3 closestPoint = p;
266
267 for (size_t i = 0; i < vertices.size(); ++i) {
268 Plane edgePlane(vertices[i], edgeNormals[i]);
269 double edgePlaneDistance = edgePlane.getPointDistance(p);
270
271 // Skip checking against the edge if the point is not in the half-space that
272 // the edgePlane's normal points towards i.e. if the edgePlane is facing
273 // away from the point.
274 if (edgePlaneDistance < 0.0) {
275 continue;
276 }
277
278 glm::dvec3 closestPointOnEdge =
279 closestPointLineSegment(p, vertices[i], vertices[(i + 1) % 4]);
280
281 double distance = glm::distance(p, closestPointOnEdge);
282 if (distance < minDistance) {
283 minDistance = distance;
284 closestPoint = closestPointOnEdge;
285 }
286 }
287
288 return closestPoint;
289}
290
291} // namespace
292

Callers 1

Calls 3

closestPointLineSegmentFunction · 0.85
getPointDistanceMethod · 0.80
sizeMethod · 0.45

Tested by

no test coverage detected