mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-01 09:49:45 +00:00
correction of centroid
This commit is contained in:
parent
d92bb933f0
commit
d4c7fab7ed
2 changed files with 23 additions and 26 deletions
|
@ -959,7 +959,7 @@ function plane_from_polygon(poly, fast=false, eps=EPSILON) =
|
||||||
// plane_normal(plane);
|
// plane_normal(plane);
|
||||||
// Description:
|
// Description:
|
||||||
// Returns the unit length normal vector for the given plane.
|
// Returns the unit length normal vector for the given plane.
|
||||||
// Argument:
|
// Arguments:
|
||||||
// plane = The `[A,B,C,D]` plane definition where `Ax+By+Cz=D` is the formula of the plane.
|
// plane = The `[A,B,C,D]` plane definition where `Ax+By+Cz=D` is the formula of the plane.
|
||||||
function plane_normal(plane) =
|
function plane_normal(plane) =
|
||||||
assert( _valid_plane(plane), "Invalid input plane." )
|
assert( _valid_plane(plane), "Invalid input plane." )
|
||||||
|
@ -973,7 +973,7 @@ function plane_normal(plane) =
|
||||||
// Returns coeficient D of the normalized plane equation `Ax+By+Cz=D`, or the scalar offset of the plane from the origin.
|
// Returns coeficient D of the normalized plane equation `Ax+By+Cz=D`, or the scalar offset of the plane from the origin.
|
||||||
// This value may be negative.
|
// This value may be negative.
|
||||||
// The absolute value of this coefficient is the distance of the plane from the origin.
|
// The absolute value of this coefficient is the distance of the plane from the origin.
|
||||||
// Argument:
|
// Arguments:
|
||||||
// plane = The `[A,B,C,D]` plane definition where `Ax+By+Cz=D` is the formula of the plane.
|
// plane = The `[A,B,C,D]` plane definition where `Ax+By+Cz=D` is the formula of the plane.
|
||||||
function plane_offset(plane) =
|
function plane_offset(plane) =
|
||||||
assert( _valid_plane(plane), "Invalid input plane." )
|
assert( _valid_plane(plane), "Invalid input plane." )
|
||||||
|
@ -1046,7 +1046,7 @@ function projection_on_plane(plane, points) =
|
||||||
// pt = plane_point_nearest_origin(plane);
|
// pt = plane_point_nearest_origin(plane);
|
||||||
// Description:
|
// Description:
|
||||||
// Returns the point on the plane that is closest to the origin.
|
// Returns the point on the plane that is closest to the origin.
|
||||||
// Argument:
|
// Arguments:
|
||||||
// plane = The `[A,B,C,D]` plane definition where `Ax+By+Cz=D` is the formula of the plane.
|
// plane = The `[A,B,C,D]` plane definition where `Ax+By+Cz=D` is the formula of the plane.
|
||||||
function plane_point_nearest_origin(plane) =
|
function plane_point_nearest_origin(plane) =
|
||||||
let( plane = normalize_plane(plane) )
|
let( plane = normalize_plane(plane) )
|
||||||
|
@ -1072,6 +1072,7 @@ function distance_from_plane(plane, point) =
|
||||||
let( plane = normalize_plane(plane) )
|
let( plane = normalize_plane(plane) )
|
||||||
point3d(plane)* point - plane[3];
|
point3d(plane)* point - plane[3];
|
||||||
|
|
||||||
|
|
||||||
// Returns [POINT, U] if line intersects plane at one point.
|
// Returns [POINT, U] if line intersects plane at one point.
|
||||||
// Returns [LINE, undef] if the line is on the plane.
|
// Returns [LINE, undef] if the line is on the plane.
|
||||||
// Returns undef if line is parallel to, but not on the given plane.
|
// Returns undef if line is parallel to, but not on the given plane.
|
||||||
|
@ -1594,7 +1595,6 @@ function circle_circle_tangents(c1,r1,c2,r2,d1,d2) =
|
||||||
];
|
];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Function: circle_line_intersection()
|
// Function: circle_line_intersection()
|
||||||
// Usage:
|
// Usage:
|
||||||
// isect = circle_line_intersection(c,r,line,<bounded>,<eps>);
|
// isect = circle_line_intersection(c,r,line,<bounded>,<eps>);
|
||||||
|
@ -1627,7 +1627,6 @@ function circle_line_intersection(c,r,line,d,bounded=false,eps=EPSILON) =
|
||||||
let( offset = sqrt(r*r-d*d),
|
let( offset = sqrt(r*r-d*d),
|
||||||
uvec=unit(line[1]-line[0])
|
uvec=unit(line[1]-line[0])
|
||||||
) [closest-offset*uvec, closest+offset*uvec]
|
) [closest-offset*uvec, closest+offset*uvec]
|
||||||
|
|
||||||
)
|
)
|
||||||
[for(p=isect)
|
[for(p=isect)
|
||||||
if ((!bounded[0] || (p-line[0])*(line[1]-line[0])>=0)
|
if ((!bounded[0] || (p-line[0])*(line[1]-line[0])>=0)
|
||||||
|
@ -1635,7 +1634,6 @@ function circle_line_intersection(c,r,line,d,bounded=false,eps=EPSILON) =
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Section: Pointlists
|
// Section: Pointlists
|
||||||
|
|
||||||
|
|
||||||
|
@ -1913,24 +1911,23 @@ function align_polygon(reference, poly, angles, cp) =
|
||||||
function centroid(poly, eps=EPSILON) =
|
function centroid(poly, eps=EPSILON) =
|
||||||
assert( is_path(poly,dim=[2,3]), "The input must be a 2D or 3D polygon." )
|
assert( is_path(poly,dim=[2,3]), "The input must be a 2D or 3D polygon." )
|
||||||
assert( is_finite(eps) && (eps>=0), "The tolerance should be a non-negative value." )
|
assert( is_finite(eps) && (eps>=0), "The tolerance should be a non-negative value." )
|
||||||
let(
|
let(
|
||||||
n = len(poly[0])==2 ? 1 :
|
n = len(poly[0])==2 ? 1 :
|
||||||
let(
|
let( plane = plane_from_points(poly, fast=true) )
|
||||||
plane = plane_from_points(poly, fast=true) )
|
assert( !is_undef(plane), "The polygon must be planar." )
|
||||||
assert( !is_undef(plane), "The polygon must be planar." )
|
plane_normal(plane),
|
||||||
plane_normal(plane),
|
v0 = poly[0] ,
|
||||||
v0 = poly[0] ,
|
val = sum([for(i=[1:len(poly)-2])
|
||||||
val = sum([for(i=[1:len(poly)-2])
|
let(
|
||||||
let(
|
v1 = poly[i],
|
||||||
v1 = poly[i],
|
v2 = poly[i+1],
|
||||||
v2 = poly[i+1],
|
area = cross(v2-v0,v1-v0)*n
|
||||||
area = cross(v2-v0,v1-v0)*n
|
)
|
||||||
)
|
[ area, (v0+v1+v2)*area ]
|
||||||
[ area, (v0+v1+v2)*area ]
|
] )
|
||||||
] )
|
)
|
||||||
)
|
assert(!approx(val[0],0, eps), "The polygon is self-intersecting or its points are collinear.")
|
||||||
assert(!approx(val[0],0, eps), "The polygon is self-intersecting or its points are collinear.")
|
val[1]/val[0]/3;
|
||||||
val[1]/val[0]/3;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -904,11 +904,11 @@ module test_noncollinear_triple() {
|
||||||
}
|
}
|
||||||
*test_noncollinear_triple();
|
*test_noncollinear_triple();
|
||||||
|
|
||||||
|
|
||||||
module test_centroid() {
|
module test_centroid() {
|
||||||
$fn = 24;
|
$fn = 24;
|
||||||
assert_approx(centroid(circle(d=100)), [0,0]);
|
assert_approx(centroid(circle(d=100)), [0,0]);
|
||||||
assert_approx(centroid(rect([40,60],rounding=10,anchor=LEFT)), [-20,0]);
|
assert_approx(centroid(rect([40,60],rounding=10,anchor=LEFT)), [-20,0]);
|
||||||
assert_approx(centroid(rect([40,60],rounding=10,anchor=FWD)), [0,30]);
|
assert_approx(centroid(rect([40,60],rounding=10,anchor=FWD)), [0,30]);
|
||||||
poly = move([1,2.5,3.1],p=rot([12,49,24], p=path3d(circle(10,$fn=33))));
|
poly = move([1,2.5,3.1],p=rot([12,49,24], p=path3d(circle(10,$fn=33))));
|
||||||
assert_approx(centroid(poly), [1,2.5,3.1]);
|
assert_approx(centroid(poly), [1,2.5,3.1]);
|
||||||
|
|
Loading…
Reference in a new issue