* 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 */
| 258 | * @private |
| 259 | */ |
| 260 | glm::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 |
no test coverage detected