Correction of polygon_area and a better polygon_normal

This commit is contained in:
RonaldoCMP 2021-04-11 11:35:38 +01:00
parent 5c6181d4d4
commit 160e3f3edd

View file

@ -1784,7 +1784,7 @@ function polygon_area(poly, signed=false) =
len(poly[0])==2 len(poly[0])==2
? let( total = sum([for(i=[1:1:len(poly)-2]) cross(poly[i]-poly[0],poly[i+1]-poly[0]) ])/2 ) ? let( total = sum([for(i=[1:1:len(poly)-2]) cross(poly[i]-poly[0],poly[i+1]-poly[0]) ])/2 )
signed ? total : abs(total) signed ? total : abs(total)
: let( plane = plane_from_points(poly) ) : let( plane = plane_from_polygon(poly) )
plane==undef? undef : plane==undef? undef :
let( let(
n = plane_normal(plane), n = plane_normal(plane),
@ -2101,18 +2101,15 @@ function reverse_polygon(poly) =
// Description: // Description:
// Given a 3D planar polygon, returns a unit-length normal vector for the // Given a 3D planar polygon, returns a unit-length normal vector for the
// clockwise orientation of the polygon. If the polygon points are collinear, returns `undef`. // clockwise orientation of the polygon. If the polygon points are collinear, returns `undef`.
// It doesn't check for coplanarity.
// Arguments: // Arguments:
// poly = The list of 3D path points for the perimeter of the polygon. // poly = The list of 3D path points for the perimeter of the polygon.
function polygon_normal(poly) = function polygon_normal(poly) =
assert(is_path(poly,dim=3), "Invalid 3D polygon." ) assert(is_path(poly,dim=3), "Invalid 3D polygon." )
let( len(poly)==3 ? point3d(plane3pt(poly[0],poly[1],poly[2])) :
poly = cleanup_path(poly), let( triple = sort(noncollinear_triple(poly,error=false)) )
p0 = poly[0], triple==[] ? undef :
n = sum([ point3d(plane3pt(poly[triple[0]],poly[triple[1]],poly[triple[2]])) ;
for (i=[1:1:len(poly)-2])
cross(poly[i+1]-p0, poly[i]-p0)
])
) unit(n,undef);
function _split_polygon_at_x(poly, x) = function _split_polygon_at_x(poly, x) =