Merge pull request #1394 from adrianVmariano/master

path sweep end anchors
This commit is contained in:
Revar Desmera
2024-02-25 11:49:01 -08:00
committed by GitHub
4 changed files with 147 additions and 50 deletions

View File

@@ -2764,16 +2764,46 @@ function reorient(
// See Also: reorient(), attachable()
// Usage:
// a = named_anchor(name, pos, [orient], [spin]);
// a = named_anchor(name, [pos], rot=, [flip=]);
// Description:
// Creates an anchor data structure. For a step-by-step explanation of attachments,
// Creates an anchor data structure. You can specify the position, orient direction and spin directly.
// Alternatively for the 3D case you can give a 4x4 rotation matrix which can specify the orient and spin, and optionally
// the position, using a translation component of the matrix. If you specify `pos` along with `rot` then the position you
// give overrides any translation included in `rot`. For a step-by-step explanation of attachments,
// see the [Attachments Tutorial](Tutorial-Attachments).
// Arguments:
// name = The string name of the anchor. Lowercase. Words separated by single dashes. No spaces.
// pos = The [X,Y,Z] position of the anchor.
// orient = A vector pointing in the direction parts should project from the anchor position. Default: UP
// spin = If needed, the angle to rotate the part around the direction vector. Default: 0
function named_anchor(name, pos, orient=UP, spin=0) = [name, pos, orient, spin];
// ---
// rot = A 4x4 rotations matrix, which may include a translation
// flip = If true, flip the anchor the opposite direction. Default: false
function named_anchor(name, pos, orient, spin, rot, flip) =
assert(num_defined([orient,spin])==0 || num_defined([rot,flip])==0, "Cannot mix orient or spin with rot or flip")
assert(num_defined([pos,rot])>0, "Must give pos or rot")
is_undef(rot) ? [name, pos, default(orient,UP), default(spin,0)]
:
let(
flip = default(flip,false),
pos = default(pos,apply(rot,CTR)),
rotpart = _force_rot(rot),
dummy = assert(approx(det4(rotpart),1), "Input rotation is not a rotation matrix"),
dir = flip ? apply(rotpart,DOWN)
: apply(rotpart,UP),
rot = flip? affine3d_rot_by_axis(apply(rotpart,BACK),180)*rot
: rot,
decode=rot_decode(rot(to=UP,from=dir)*_force_rot(rot)),
spin = decode[0]*sign(decode[1].z)
)
[name, pos, dir, spin];
function _force_rot(T) =
[for(i=[0:3])
[for(j=[0:3]) j<3 ? T[i][j] :
i==3 ? 1
: 0]];
// Function: attach_geom()
// Synopsis: Returns the internal geometry description of an attachable object.
@@ -3202,7 +3232,6 @@ function _attach_transform(anchor, spin, orient, geom, p) =
assert(is_undef(orient) || is_vector(orient,3), str("Got: ",orient))
let(
anchor = default(anchor, CENTER),
spin = default(spin, 0),
orient = default(orient, UP),
two_d = _attach_geom_2d(geom),
@@ -3210,12 +3239,13 @@ function _attach_transform(anchor, spin, orient, geom, p) =
let(
anch = _find_anchor($attach_to, geom),
pos = anch[1]
) two_d? (
assert(two_d && is_num(spin))
affine3d_zrot(spin) *
rot(to=FWD, from=point3d(anch[2])) *
affine3d_translate(point3d(-pos))
) : (
)
two_d?
assert(is_num(spin))
affine3d_zrot(spin)
* rot(to=FWD, from=point3d(anch[2]))
* affine3d_translate(point3d(-pos))
:
assert(is_num(spin) || is_vector(spin,3))
let(
ang = vector_angle(anch[2], DOWN),
@@ -3223,40 +3253,33 @@ function _attach_transform(anchor, spin, orient, geom, p) =
ang2 = (anch[2]==UP || anch[2]==DOWN)? 0 : 180-anch[3],
axis2 = rot(p=axis,[0,0,ang2])
)
affine3d_rot_by_axis(axis2,ang) * (
is_num(spin)? affine3d_zrot(ang2+spin) : (
affine3d_zrot(spin.z) *
affine3d_yrot(spin.y) *
affine3d_xrot(spin.x) *
affine3d_zrot(ang2)
)
) * affine3d_translate(point3d(-pos))
)
affine3d_rot_by_axis(axis2,ang)
* (is_num(spin)? affine3d_zrot(ang2+spin)
: affine3d_zrot(spin.z) * affine3d_yrot(spin.y) * affine3d_xrot(spin.x)
* affine3d_zrot(ang2))
* affine3d_translate(point3d(-pos))
) : (
let(
pos = _find_anchor(anchor, geom)[1]
) two_d? (
assert(two_d && is_num(spin))
affine3d_zrot(spin) *
affine3d_translate(point3d(-pos))
) : (
)
two_d?
assert(is_num(spin))
affine3d_zrot(spin) * affine3d_translate(point3d(-pos))
:
assert(is_num(spin) || is_vector(spin,3))
let(
axis = vector_axis(UP,orient),
ang = vector_angle(UP,orient)
)
affine3d_rot_by_axis(axis,ang) * (
is_num(spin)? affine3d_zrot(spin) : (
affine3d_zrot(spin.z) *
affine3d_yrot(spin.y) *
affine3d_xrot(spin.x)
)
) * affine3d_translate(point3d(-pos))
)
affine3d_rot_by_axis(axis,ang)
* ( is_num(spin)? affine3d_zrot(spin)
: affine3d_zrot(spin.z) * affine3d_yrot(spin.y) * affine3d_xrot(spin.x))
* affine3d_translate(point3d(-pos))
)
) is_undef(p)? m :
is_vnf(p)? [(p==EMPTY_VNF? p : apply(m, p[0])), p[1]] :
apply(m, p);
)
is_undef(p)? m
: is_vnf(p) && p==EMPTY_VNF? p
: apply(m, p);
function _get_cp(geom) =