-------------------------------- PyUtilComputeNormalFromlPoints -------------------------------- Compute a normal from a list of points.
| 606 | // Compute a normal from a list of points. |
| 607 | // |
| 608 | std::array<double,3> |
| 609 | PyUtilComputeNormalFromlPoints(const std::vector<std::array<double,3>>& points) |
| 610 | { |
| 611 | std::array<double,3> normal; |
| 612 | |
| 613 | if (points.size() < 3) { |
| 614 | throw std::runtime_error("[PyUtilComputeNormalFromlPoints] The number of points is < 3."); |
| 615 | } |
| 616 | |
| 617 | int numPoints = points.size(); |
| 618 | double nx = 0.0, ny = 0.0, nz = 0.0, mag; |
| 619 | int j; |
| 620 | |
| 621 | for (int i = 0; i < numPoints; i++) { |
| 622 | if (i == (numPoints - 1)) { |
| 623 | j = 0; |
| 624 | } else { |
| 625 | j = i + 1; |
| 626 | } |
| 627 | |
| 628 | nx += points[j][2]*points[i][1] - points[j][1]*points[i][2]; |
| 629 | ny += points[j][0]*points[i][2] - points[j][2]*points[i][0]; |
| 630 | nz += points[j][1]*points[i][0] - points[j][0]*points[i][1]; |
| 631 | } |
| 632 | |
| 633 | mag = sqrt(nx*nx + ny*ny + nz*nz); |
| 634 | |
| 635 | if (mag == 0.0) { |
| 636 | throw std::runtime_error("[PyUtilComputeNormalFromlPoints] Points produced a magnitude zero normal."); |
| 637 | } |
| 638 | |
| 639 | normal[0] = nx / mag; |
| 640 | normal[1] = ny / mag; |
| 641 | normal[2] = nz / mag; |
| 642 | |
| 643 | return normal; |
| 644 | } |
| 645 | |
| 646 | //------------------------ |
| 647 | // PyUtilSetupApiFunction |
no test coverage detected