From 2f1266e8c3116ce63a601ebf0f3136528a8c2666 Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Mon, 27 Mar 2023 02:21:26 -0700 Subject: [PATCH] Corrected prismoid geometry corner anchor vectors. --- attachments.scad | 25 +++++++++++++++++++------ vectors.scad | 20 ++++++++++++++++++++ 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/attachments.scad b/attachments.scad index af62c38..5361964 100644 --- a/attachments.scad +++ b/attachments.scad @@ -2542,12 +2542,25 @@ function _find_anchor(anchor, geom) = bot = point3d(v_mul(point2d(size )/2, axy), -h/2), top = point3d(v_mul(point2d(size2)/2, axy) + shift, h/2), pos = point3d(cp) + lerp(bot,top,u) + offset, - vecs = [ - if (anch.x!=0) unit(rot(from=UP, to=[(top-bot).x,0,h], p=[axy.x,0,0]), UP), - if (anch.y!=0) unit(rot(from=UP, to=[0,(top-bot).y,h], p=[0,axy.y,0]), UP), - if (anch.z!=0) anch==CENTER? UP : unit([0,0,anch.z],UP) - ], - vec = anchor==CENTER? UP : rot(from=UP, to=axis, p=unit(sum(vecs) / len(vecs))), + vecs = anchor==CENTER? [UP] + : [ + if (anch.x!=0) unit(rot(from=UP, to=[(top-bot).x,0,h], p=[axy.x,0,0]), UP), + if (anch.y!=0) unit(rot(from=UP, to=[0,(top-bot).y,h], p=[0,axy.y,0]), UP), + if (anch.z!=0) unit([0,0,anch.z],UP) + ], + vec2 = anchor==CENTER? UP + : len(vecs)==1? unit(vecs[0],UP) + : len(vecs)==2? vector_bisect(vecs[0],vecs[1]) + : let( + v1 = vector_bisect(vecs[0],vecs[2]), + v2 = vector_bisect(vecs[1],vecs[2]), + p1 = plane_from_normal(yrot(90,p=v1)), + p2 = plane_from_normal(xrot(-90,p=v2)), + line = plane_intersection(p1,p2), + v3 = unit(line[1]-line[0],UP) * anch.z + ) + unit(v3,UP), + vec = rot(from=UP, to=axis, p=vec2), pos2 = rot(from=UP, to=axis, p=pos) ) [anchor, pos2, vec, oang] ) : type == "conoid"? ( //r1, r2, l, shift diff --git a/vectors.scad b/vectors.scad index a654716..f588b82 100644 --- a/vectors.scad +++ b/vectors.scad @@ -284,6 +284,26 @@ function vector_axis(v1,v2=undef,v3=undef) = ) unit(cross(w1,w3)); +// Function: vector_bisect() +// Usage: +// newv = vector_bisect(v1,v2); +// Description: +// Returns a unit vector that exactly bisects the minor angle between two given vectors. +// If given two vectors that are directly opposed, returns `undef`. +function vector_bisect(v1,v2) = + assert(is_vector(v1)) + assert(is_vector(v2)) + assert(!approx(norm(v1),0), "Zero length vector.") + assert(!approx(norm(v2),0), "Zero length vector.") + assert(len(v1)==len(v2), "Vectors are of different sizes.") + let( v1 = unit(v1), v2 = unit(v2) ) + approx(v1,-v2)? undef : + let( + axis = vector_axis(v1,v2), + ang = vector_angle(v1,v2), + v3 = rot(ang/2, v=axis, p=v1) + ) v3; + // Section: Vector Searching