Made polygon_area() work with 3D polygons as well.

This commit is contained in:
Revar Desmera 2020-03-19 14:04:40 -07:00
parent cb11ac558a
commit 1df6dcff2b
3 changed files with 19 additions and 12 deletions

View file

@ -452,7 +452,7 @@ function find_anchor(anchor, geom) =
sum([ sum([
for (i = nfaces) let( for (i = nfaces) let(
faceverts = select(vnf[0],vnf[1][i]), faceverts = select(vnf[0],vnf[1][i]),
faceplane = plane_from_pointslist(faceverts), faceplane = plane_from_points(faceverts),
nrm = plane_normal(faceplane) nrm = plane_normal(faceplane)
) nrm ) nrm
]) / len(nfaces) ]) / len(nfaces)

View file

@ -618,9 +618,9 @@ function plane_from_normal(normal, pt) =
concat(normal, [normal*pt]); concat(normal, [normal*pt]);
// Function: plane_from_pointslist() // Function: plane_from_points()
// Usage: // Usage:
// plane_from_pointslist(points, [fast], [eps]); // plane_from_points(points, [fast], [eps]);
// Description: // Description:
// Given a list of 3 or more coplanar points, returns the cartesian equation of a plane. // Given a list of 3 or more coplanar points, returns the cartesian equation of a plane.
// Returns [A,B,C,D] where Ax+By+Cz=D is the equation of the plane. // Returns [A,B,C,D] where Ax+By+Cz=D is the equation of the plane.
@ -631,7 +631,7 @@ function plane_from_normal(normal, pt) =
// points = The list of points to find the plane of. // points = The list of points to find the plane of.
// fast = If true, don't verify that all points in the list are coplanar. Default: false // fast = If true, don't verify that all points in the list are coplanar. Default: false
// eps = How much variance is allowed in testing that each point is on the same plane. Default: `EPSILON` (1e-9) // eps = How much variance is allowed in testing that each point is on the same plane. Default: `EPSILON` (1e-9)
function plane_from_pointslist(points, fast=false, eps=EPSILON) = function plane_from_points(points, fast=false, eps=EPSILON) =
let( let(
points = deduplicate(points), points = deduplicate(points),
indices = sort(find_noncollinear_points(points)), indices = sort(find_noncollinear_points(points)),
@ -839,7 +839,7 @@ function coplanar(plane, point, eps=EPSILON) =
// Function: points_are_coplanar() // Function: points_are_coplanar()
// Usage: // Usage:
// points_are_coplanar(points); // points_are_coplanar(points, [eps]);
// Description: // Description:
// Given a list of points, returns true if all points in the list are coplanar. // Given a list of points, returns true if all points in the list are coplanar.
// Arguments: // Arguments:
@ -847,7 +847,7 @@ function coplanar(plane, point, eps=EPSILON) =
// eps = How much variance is allowed in testing that each point is on the same plane. Default: `EPSILON` (1e-9) // eps = How much variance is allowed in testing that each point is on the same plane. Default: `EPSILON` (1e-9)
function points_are_coplanar(points, eps=EPSILON) = function points_are_coplanar(points, eps=EPSILON) =
let( let(
plane = plane_from_pointslist(points, fast=true, eps=eps) plane = plane_from_points(points, fast=true, eps=eps)
) all([for (pt = points) coplanar(plane, pt, eps=eps)]); ) all([for (pt = points) coplanar(plane, pt, eps=eps)]);
@ -1080,11 +1080,18 @@ function furthest_point(pt, points) =
// Function: polygon_area() // Function: polygon_area()
// Usage: // Usage:
// area = polygon_area(vertices); // area = polygon_area(poly);
// Description: // Description:
// Given a polygon, returns the area of that polygon. If the polygon is self-crossing, the results are undefined. // Given a 2D or 3D planar polygon, returns the area of that polygon. If the polygon is self-crossing, the results are undefined.
function polygon_area(vertices) = function polygon_area(poly) =
0.5*sum([for(i=[0:len(vertices)-1]) det2(select(vertices,i,i+1))]); len(poly)<3? 0 :
len(poly[0])==2? 0.5*sum([for(i=[0:1:len(poly)-1]) det2(select(poly,i,i+1))]) :
let(
plane = plane_from_points(poly),
n = unit(plane_normal(plane)),
total = sum([for (i=[0:1:len(poly)-1]) cross(poly[i], select(poly,i+1))]),
res = abs(total * n) / 2
) res;
// Function: polygon_is_convex() // Function: polygon_is_convex()
@ -1240,7 +1247,7 @@ function centroid(poly) =
]) / 6 / polygon_area(poly) ]) / 6 / polygon_area(poly)
) : ( ) : (
let( let(
n = plane_normal(plane_from_pointslist(poly)), n = plane_normal(plane_from_points(poly)),
p1 = vector_angle(n,UP)>15? vector_axis(n,UP) : vector_axis(n,RIGHT), p1 = vector_angle(n,UP)>15? vector_axis(n,UP) : vector_axis(n,RIGHT),
p2 = vector_axis(n,p1), p2 = vector_axis(n,p1),
cp = mean(poly), cp = mean(poly),

View file

@ -8,7 +8,7 @@
////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////
BOSL_VERSION = [2,0,202]; BOSL_VERSION = [2,0,203];
// Section: BOSL Library Version Functions // Section: BOSL Library Version Functions