mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-28 23:39:36 +00:00
Renamed matrix[34]_* -> affine[23]d_*.
This commit is contained in:
parent
9182319994
commit
9645aef2a9
9 changed files with 339 additions and 330 deletions
|
@ -99,7 +99,7 @@ The library files are as follows:
|
|||
- [`math.scad`](https://github.com/revarbat/BOSL2/wiki/math.scad): Useful helper functions.
|
||||
- [`arrays.scad`](https://github.com/revarbat/BOSL2/wiki/arrays.scad): List and Array helper functions.
|
||||
- [`vectors.scad`](https://github.com/revarbat/BOSL2/wiki/vectors.scad): Vector math functions.
|
||||
- [`matrices.scad`](https://github.com/revarbat/BOSL2/wiki/matrices.scad): Matrix math and affine transformation functions.
|
||||
- [`affine.scad`](https://github.com/revarbat/BOSL2/wiki/affine.scad): Affine transformation matrix functions.
|
||||
- [`coords.scad`](https://github.com/revarbat/BOSL2/wiki/coords.scad): Coordinate system conversions and point transformations.
|
||||
- [`geometry.scad`](https://github.com/revarbat/BOSL2/wiki/geometry.scad): Functions to calculate various geometry.
|
||||
- [`quaternions.scad`](https://github.com/revarbat/BOSL2/wiki/quaternions.scad): Functions to work with quaternion rotations.
|
||||
|
|
307
affine.scad
Normal file
307
affine.scad
Normal file
|
@ -0,0 +1,307 @@
|
|||
//////////////////////////////////////////////////////////////////////
|
||||
// LibFile: matrices.scad
|
||||
// Matrix math and affine transformation matrices.
|
||||
// To use, add the following lines to the beginning of your file:
|
||||
// ```
|
||||
// use <BOSL2/std.scad>
|
||||
// ```
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
// Section: Matrix Manipulation
|
||||
|
||||
// Function: ident()
|
||||
// Description: Create an `n` by `n` identity matrix.
|
||||
// Arguments:
|
||||
// n = The size of the identity matrix square, `n` by `n`.
|
||||
function ident(n) = [for (i = [0:n-1]) [for (j = [0:n-1]) (i==j)?1:0]];
|
||||
|
||||
|
||||
// Function: affine2d_to_affine3d()
|
||||
// Description: Takes a 3x3 affine2d matrix and returns its 4x4 affine3d equivalent.
|
||||
function mat3_to_mat4(m) = concat(
|
||||
[for (r = [0:2])
|
||||
concat(
|
||||
[for (c = [0:2]) m[r][c]],
|
||||
[0]
|
||||
)
|
||||
],
|
||||
[[0, 0, 0, 1]]
|
||||
);
|
||||
|
||||
|
||||
|
||||
// Section: Affine2d 3x3 Transformation Matrices
|
||||
|
||||
|
||||
// Function: affine2d_identity()
|
||||
// Description: Create a 3x3 affine2d identity matrix.
|
||||
function affine2d_identity() = ident(3);
|
||||
|
||||
|
||||
// Function: affine2d_translate()
|
||||
// Description:
|
||||
// Returns the 3x3 affine2d matrix to perform a 2D translation.
|
||||
// Arguments:
|
||||
// v = 2D Offset to translate by. [X,Y]
|
||||
function affine2d_translate(v) = [
|
||||
[1, 0, v.x],
|
||||
[0, 1, v.y],
|
||||
[0 ,0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: affine2d_scale()
|
||||
// Description:
|
||||
// Returns the 3x3 affine2d matrix to perform a 2D scaling transformation.
|
||||
// Arguments:
|
||||
// v = 2D vector of scaling factors. [X,Y]
|
||||
function affine2d_scale(v) = [
|
||||
[v.x, 0, 0],
|
||||
[ 0, v.y, 0],
|
||||
[ 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: affine2d_zrot()
|
||||
// Description:
|
||||
// Returns the 3x3 affine2d matrix to perform a rotation of a 2D vector around the Z axis.
|
||||
// Arguments:
|
||||
// ang = Number of degrees to rotate.
|
||||
function affine2d_zrot(ang) = [
|
||||
[cos(ang), -sin(ang), 0],
|
||||
[sin(ang), cos(ang), 0],
|
||||
[ 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: affine2d_skew()
|
||||
// Usage:
|
||||
// affine2d_skew(xa, ya)
|
||||
// Description:
|
||||
// Returns the 3x3 affine2d matrix to skew a 2D vector along the XY plane.
|
||||
// Arguments:
|
||||
// xa = Skew angle, in degrees, in the direction of the X axis.
|
||||
// ya = Skew angle, in degrees, in the direction of the Y axis.
|
||||
function affine2d_skew(xa, ya) = [
|
||||
[1, tan(xa), 0],
|
||||
[tan(ya), 1, 0],
|
||||
[0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: affine2d_mult()
|
||||
// Usage:
|
||||
// affine2d_mult(affines)
|
||||
// Description:
|
||||
// Returns a 3x3 affine2d transformation matrix which results from applying each matrix in `affines` in order.
|
||||
// Arguments:
|
||||
// affines = A list of 3x3 affine2d matrices.
|
||||
function affine2d_mult(affines, _m=undef, _i=0) =
|
||||
(_i>=len(affines))? (is_undef(_m)? ident(3) : _m) :
|
||||
affine2d_mult(affines, _m=(is_undef(_m)? affines[_i] : affines[_i] * _m), _i=_i+1);
|
||||
|
||||
|
||||
// Function: affine2d_apply()
|
||||
// Usage:
|
||||
// affine2d_apply(pts, affines)
|
||||
// Description:
|
||||
// Given a list of 3x3 affine2d transformation matrices, applies them in order to the points in the point list.
|
||||
// Arguments:
|
||||
// pts = A list of 2D points to transform.
|
||||
// affines = A list of 3x3 affine2d matrices to apply, in order.
|
||||
// Example:
|
||||
// npts = affine2d_apply(
|
||||
// pts = [for (x=[0:3]) [5*x,0]],
|
||||
// affines =[
|
||||
// affine2d_scale([3,1]),
|
||||
// affine2d_rot(90),
|
||||
// affine2d_translate([5,5])
|
||||
// ]
|
||||
// ); // Returns [[5,5], [5,20], [5,35], [5,50]]
|
||||
function affine2d_apply(pts, affines) =
|
||||
let(m = affine2d_mult(affines))
|
||||
[for (p = pts) point2d(m * concat(point2d(p),[1]))];
|
||||
|
||||
|
||||
|
||||
// Section: Affine3d 4x4 Transformation Matrices
|
||||
|
||||
|
||||
// Function: affine3d_identity()
|
||||
// Description: Create a 4x4 affine3d identity matrix.
|
||||
function affine3d_identity() = ident(4);
|
||||
|
||||
|
||||
// Function: affine3d_translate()
|
||||
// Description:
|
||||
// Returns the 4x4 affine3d matrix to perform a 3D translation.
|
||||
// Arguments:
|
||||
// v = 3D offset to translate by. [X,Y,Z]
|
||||
function affine3d_translate(v) = [
|
||||
[1, 0, 0, v.x],
|
||||
[0, 1, 0, v.y],
|
||||
[0, 0, 1, v.z],
|
||||
[0 ,0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: affine3d_scale()
|
||||
// Description:
|
||||
// Returns the 4x4 affine3d matrix to perform a 3D scaling transformation.
|
||||
// Arguments:
|
||||
// v = 3D vector of scaling factors. [X,Y,Z]
|
||||
function affine3d_scale(v) = [
|
||||
[v.x, 0, 0, 0],
|
||||
[ 0, v.y, 0, 0],
|
||||
[ 0, 0, v.z, 0],
|
||||
[ 0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: affine3d_xrot()
|
||||
// Description:
|
||||
// Returns the 4x4 affine3d matrix to perform a rotation of a 3D vector around the X axis.
|
||||
// Arguments:
|
||||
// ang = number of degrees to rotate.
|
||||
function affine3d_xrot(ang) = [
|
||||
[1, 0, 0, 0],
|
||||
[0, cos(ang), -sin(ang), 0],
|
||||
[0, sin(ang), cos(ang), 0],
|
||||
[0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: affine3d_yrot()
|
||||
// Description:
|
||||
// Returns the 4x4 affine3d matrix to perform a rotation of a 3D vector around the Y axis.
|
||||
// Arguments:
|
||||
// ang = Number of degrees to rotate.
|
||||
function affine3d_yrot(ang) = [
|
||||
[ cos(ang), 0, sin(ang), 0],
|
||||
[ 0, 1, 0, 0],
|
||||
[-sin(ang), 0, cos(ang), 0],
|
||||
[ 0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: affine3d_zrot()
|
||||
// Usage:
|
||||
// affine3d_zrot(ang)
|
||||
// Description:
|
||||
// Returns the 4x4 affine3d matrix to perform a rotation of a 3D vector around the Z axis.
|
||||
// Arguments:
|
||||
// ang = number of degrees to rotate.
|
||||
function affine3d_zrot(ang) = [
|
||||
[cos(ang), -sin(ang), 0, 0],
|
||||
[sin(ang), cos(ang), 0, 0],
|
||||
[ 0, 0, 1, 0],
|
||||
[ 0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: affine3d_rot_by_axis()
|
||||
// Usage:
|
||||
// affine3d_rot_by_axis(u, ang);
|
||||
// Description:
|
||||
// Returns the 4x4 affine3d matrix to perform a rotation of a 3D vector around an axis.
|
||||
// Arguments:
|
||||
// u = 3D axis vector to rotate around.
|
||||
// ang = number of degrees to rotate.
|
||||
function affine3d_rot_by_axis(u, ang) = let(
|
||||
u = normalize(u),
|
||||
c = cos(ang),
|
||||
c2 = 1-c,
|
||||
s = sin(ang)
|
||||
) [
|
||||
[u[0]*u[0]*c2+c , u[0]*u[1]*c2-u[2]*s, u[0]*u[2]*c2+u[1]*s, 0],
|
||||
[u[1]*u[0]*c2+u[2]*s, u[1]*u[1]*c2+c , u[1]*u[2]*c2-u[0]*s, 0],
|
||||
[u[2]*u[0]*c2-u[1]*s, u[2]*u[1]*c2+u[0]*s, u[2]*u[2]*c2+c , 0],
|
||||
[ 0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: affine3d_skew_xy()
|
||||
// Usage:
|
||||
// affine3d_skew_xy(xa, ya)
|
||||
// Description:
|
||||
// Returns the 4x4 affine3d matrix to perform a skew transformation along the XY plane..
|
||||
// Arguments:
|
||||
// xa = Skew angle, in degrees, in the direction of the X axis.
|
||||
// ya = Skew angle, in degrees, in the direction of the Y axis.
|
||||
function affine3d_skew_xy(xa, ya) = [
|
||||
[1, 0, tan(xa), 0],
|
||||
[0, 1, tan(ya), 0],
|
||||
[0, 0, 1, 0],
|
||||
[0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: affine3d_skew_xz()
|
||||
// Usage:
|
||||
// affine3d_skew_xz(xa, za)
|
||||
// Description:
|
||||
// Returns the 4x4 affine3d matrix to perform a skew transformation along the XZ plane.
|
||||
// Arguments:
|
||||
// xa = Skew angle, in degrees, in the direction of the X axis.
|
||||
// za = Skew angle, in degrees, in the direction of the Z axis.
|
||||
function affine3d_skew_xz(xa, za) = [
|
||||
[1, tan(xa), 0, 0],
|
||||
[0, 1, 0, 0],
|
||||
[0, tan(za), 1, 0],
|
||||
[0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: affine3d_skew_yz()
|
||||
// Usage:
|
||||
// affine3d_skew_yz(ya, za)
|
||||
// Description:
|
||||
// Returns the 4x4 affine3d matrix to perform a skew transformation along the YZ plane.
|
||||
// Arguments:
|
||||
// ya = Skew angle, in degrees, in the direction of the Y axis.
|
||||
// za = Skew angle, in degrees, in the direction of the Z axis.
|
||||
function affine3d_skew_yz(ya, za) = [
|
||||
[ 1, 0, 0, 0],
|
||||
[tan(ya), 1, 0, 0],
|
||||
[tan(za), 0, 1, 0],
|
||||
[ 0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: affine3d_mult()
|
||||
// Usage:
|
||||
// affine3d_mult(affines)
|
||||
// Description:
|
||||
// Returns a 4x4 affine3d transformation matrix which results from applying each matrix in `affines` in order.
|
||||
// Arguments:
|
||||
// affines = A list of 4x4 affine3d matrices.
|
||||
function affine3d_mult(affines, _m=undef, _i=0) =
|
||||
(_i>=len(affines))? (is_undef(_m)? ident(4) : _m) :
|
||||
affine3d_mult(affines, _m=(is_undef(_m)? affines[_i] : affines[_i] * _m), _i=_i+1);
|
||||
|
||||
|
||||
// Function: affine3d_apply()
|
||||
// Usage:
|
||||
// affine3d_apply(pts, affines)
|
||||
// Description:
|
||||
// Given a list of affine3d transformation matrices, applies them in order to the points in the point list.
|
||||
// Arguments:
|
||||
// pts = A list of 3D points to transform.
|
||||
// affines = A list of 4x4 matrices to apply, in order.
|
||||
// Example:
|
||||
// npts = affine3d_apply(
|
||||
// pts = [for (x=[0:3]) [5*x,0,0]],
|
||||
// affines =[
|
||||
// affine3d_scale([2,1,1]),
|
||||
// affine3d_zrot(90),
|
||||
// affine3d_translate([5,5,10])
|
||||
// ]
|
||||
// ); // Returns [[5,5,10], [5,15,10], [5,25,10], [5,35,10]]
|
||||
function affine3d_apply(pts, affines) =
|
||||
let(m = affine3d_mult(affines))
|
||||
[for (p = pts) point3d(m * concat(point3d(p),[1]))];
|
||||
|
||||
|
||||
|
||||
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
|
@ -167,17 +167,17 @@ module orient_and_anchor(
|
|||
size2 = point2d(default(size2, size));
|
||||
shift = point2d(shift);
|
||||
anchor = !is_undef(center)? (center? CENTER : noncentered) : anchor;
|
||||
m = matrix4_mult(concat(
|
||||
m = affine3d_mult(concat(
|
||||
(orig_anchor==CENTER)? [] : [
|
||||
// If original anchor is not centered, center it.
|
||||
let(anch = find_anchor(orig_anchor, size.z, size, size2=size2, shift=shift, geometry=geometry, two_d=two_d))
|
||||
matrix4_translate(anch[1])
|
||||
affine3d_translate(anch[1])
|
||||
],
|
||||
(orig_orient==ORIENT_Z)? [] : [
|
||||
// If original orientation is not upright, rotate it upright.
|
||||
matrix4_zrot(-orig_orient.z),
|
||||
matrix4_yrot(-orig_orient.y),
|
||||
matrix4_xrot(-orig_orient.x)
|
||||
affine3d_zrot(-orig_orient.z),
|
||||
affine3d_yrot(-orig_orient.y),
|
||||
affine3d_xrot(-orig_orient.x)
|
||||
],
|
||||
($attach_to!=undef)? (
|
||||
let(
|
||||
|
@ -187,21 +187,21 @@ module orient_and_anchor(
|
|||
ang2 = (anch[2]==UP || anch[2]==DOWN)? 0 : 180-anch[3],
|
||||
axis2 = rotate_points3d([axis],[0,0,ang2])[0]
|
||||
) concat(
|
||||
[matrix4_translate(-anch[1])],
|
||||
[affine3d_translate(-anch[1])],
|
||||
$attach_norot? [] : [
|
||||
matrix4_zrot(ang2),
|
||||
matrix4_rot_by_axis(axis2, ang)
|
||||
affine3d_zrot(ang2),
|
||||
affine3d_rot_by_axis(axis2, ang)
|
||||
]
|
||||
)
|
||||
) : concat(
|
||||
(anchor==CENTER)? [] : [
|
||||
let(anch = find_anchor(anchor, size.z, size, size2=size2, shift=shift, extra_anchors=anchors, geometry=geometry, two_d=two_d))
|
||||
matrix4_translate(-anch[1])
|
||||
affine3d_translate(-anch[1])
|
||||
],
|
||||
(orient==ORIENT_Z)? [] : [
|
||||
matrix4_xrot(orient.x),
|
||||
matrix4_yrot(orient.y),
|
||||
matrix4_zrot(orient.z)
|
||||
affine3d_xrot(orient.x),
|
||||
affine3d_yrot(orient.y),
|
||||
affine3d_zrot(orient.z)
|
||||
]
|
||||
)
|
||||
));
|
||||
|
|
20
coords.scad
20
coords.scad
|
@ -77,7 +77,7 @@ function scale_points(pts, v=[0,0,0], cp=[0,0,0]) = [for (pt = pts) [for (i = [0
|
|||
// ang = Angle to rotate by.
|
||||
// cp = 2D Centerpoint to rotate around. Default: `[0,0]`
|
||||
function rotate_points2d(pts, ang, cp=[0,0]) = let(
|
||||
m = matrix3_zrot(ang)
|
||||
m = affine2d_zrot(ang)
|
||||
) [for (pt = pts) m*point3d(pt-cp)+cp];
|
||||
|
||||
|
||||
|
@ -107,13 +107,13 @@ function rotate_points3d(pts, a=0, v=undef, cp=[0,0,0], from=undef, to=undef, re
|
|||
ang = vector_angle(from, to),
|
||||
v = vector_axis(from, to)
|
||||
)
|
||||
matrix4_rot_by_axis(from, -a) * matrix4_rot_by_axis(v, -ang)
|
||||
affine3d_rot_by_axis(from, -a) * affine3d_rot_by_axis(v, -ang)
|
||||
) : !is_undef(v)? (
|
||||
matrix4_rot_by_axis(v, -a)
|
||||
affine3d_rot_by_axis(v, -a)
|
||||
) : is_num(a)? (
|
||||
matrix4_zrot(-a)
|
||||
affine3d_zrot(-a)
|
||||
) : (
|
||||
matrix4_xrot(-a.x) * matrix4_yrot(-a.y) * matrix4_zrot(-a.z)
|
||||
affine3d_xrot(-a.x) * affine3d_yrot(-a.y) * affine3d_zrot(-a.z)
|
||||
)
|
||||
) : (
|
||||
!is_undef(from)? (
|
||||
|
@ -123,16 +123,16 @@ function rotate_points3d(pts, a=0, v=undef, cp=[0,0,0], from=undef, to=undef, re
|
|||
ang = vector_angle(from, to),
|
||||
v = vector_axis(from, to)
|
||||
)
|
||||
matrix4_rot_by_axis(v, ang) * matrix4_rot_by_axis(from, a)
|
||||
affine3d_rot_by_axis(v, ang) * affine3d_rot_by_axis(from, a)
|
||||
) : !is_undef(v)? (
|
||||
matrix4_rot_by_axis(v, a)
|
||||
affine3d_rot_by_axis(v, a)
|
||||
) : is_num(a)? (
|
||||
matrix4_zrot(a)
|
||||
affine3d_zrot(a)
|
||||
) : (
|
||||
matrix4_zrot(a.z) * matrix4_yrot(a.y) * matrix4_xrot(a.x)
|
||||
affine3d_zrot(a.z) * affine3d_yrot(a.y) * affine3d_xrot(a.x)
|
||||
)
|
||||
),
|
||||
m = matrix4_translate(cp) * mrot * matrix4_translate(-cp)
|
||||
m = affine3d_translate(cp) * mrot * affine3d_translate(-cp)
|
||||
) [for (pt = pts) point3d(m*concat(point3d(pt),[1]))];
|
||||
|
||||
|
||||
|
|
298
matrices.scad
298
matrices.scad
|
@ -1,298 +0,0 @@
|
|||
//////////////////////////////////////////////////////////////////////
|
||||
// LibFile: matrices.scad
|
||||
// Matrix math and affine transformation matrices.
|
||||
// To use, add the following lines to the beginning of your file:
|
||||
// ```
|
||||
// use <BOSL2/std.scad>
|
||||
// ```
|
||||
//////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
// Section: Matrix Manipulation
|
||||
|
||||
// Function: ident()
|
||||
// Description: Create an `n` by `n` identity matrix.
|
||||
// Arguments:
|
||||
// n = The size of the identity matrix square, `n` by `n`.
|
||||
function ident(n) = [for (i = [0:n-1]) [for (j = [0:n-1]) (i==j)?1:0]];
|
||||
|
||||
|
||||
// Function: mat3_to_mat4()
|
||||
// Description: Takes a 3x3 matrix and returns its 4x4 affine equivalent.
|
||||
function mat3_to_mat4(m) = concat(
|
||||
[for (r = [0:2])
|
||||
concat(
|
||||
[for (c = [0:2]) m[r][c]],
|
||||
[0]
|
||||
)
|
||||
],
|
||||
[[0, 0, 0, 1]]
|
||||
);
|
||||
|
||||
|
||||
|
||||
// Section: Affine Transformation 3x3 Matrices
|
||||
|
||||
|
||||
// Function: matrix3_translate()
|
||||
// Description:
|
||||
// Returns the 3x3 matrix to perform a 2D translation.
|
||||
// Arguments:
|
||||
// v = 2D Offset to translate by. [X,Y]
|
||||
function matrix3_translate(v) = [
|
||||
[1, 0, v.x],
|
||||
[0, 1, v.y],
|
||||
[0 ,0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: matrix3_scale()
|
||||
// Description:
|
||||
// Returns the 3x3 matrix to perform a 2D scaling transformation.
|
||||
// Arguments:
|
||||
// v = 2D vector of scaling factors. [X,Y]
|
||||
function matrix3_scale(v) = [
|
||||
[v.x, 0, 0],
|
||||
[ 0, v.y, 0],
|
||||
[ 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: matrix3_zrot()
|
||||
// Description:
|
||||
// Returns the 3x3 matrix to perform a rotation of a 2D vector around the Z axis.
|
||||
// Arguments:
|
||||
// ang = Number of degrees to rotate.
|
||||
function matrix3_zrot(ang) = [
|
||||
[cos(ang), -sin(ang), 0],
|
||||
[sin(ang), cos(ang), 0],
|
||||
[ 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: matrix3_skew()
|
||||
// Usage:
|
||||
// matrix3_skew(xa, ya)
|
||||
// Description:
|
||||
// Returns the 3x3 matrix to skew a 2D vector along the XY plane.
|
||||
// Arguments:
|
||||
// xa = Skew angle, in degrees, in the direction of the X axis.
|
||||
// ya = Skew angle, in degrees, in the direction of the Y axis.
|
||||
function matrix3_skew(xa, ya) = [
|
||||
[1, tan(xa), 0],
|
||||
[tan(ya), 1, 0],
|
||||
[0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: matrix3_mult()
|
||||
// Usage:
|
||||
// matrix3_mult(matrices)
|
||||
// Description:
|
||||
// Returns a 3x3 transformation matrix which results from applying each matrix in `matrices` in order.
|
||||
// Arguments:
|
||||
// matrices = A list of 3x3 matrices.
|
||||
function matrix3_mult(matrices, _m=undef, _i=0) =
|
||||
(_i>=len(matrices))? (is_undef(_m)? ident(3) : _m) :
|
||||
matrix3_mult(matrices, _m=(is_undef(_m)? matrices[_i] : matrices[_i] * _m), _i=_i+1);
|
||||
|
||||
|
||||
// Function: matrix3_apply()
|
||||
// Usage:
|
||||
// matrix3_apply(pts, matrices)
|
||||
// Description:
|
||||
// Given a list of transformation matrices, applies them in order to the points in the point list.
|
||||
// Arguments:
|
||||
// pts = A list of 2D points to transform.
|
||||
// matrices = A list of 3x3 matrices to apply, in order.
|
||||
// Example:
|
||||
// npts = matrix3_apply(
|
||||
// pts = [for (x=[0:3]) [5*x,0]],
|
||||
// matrices =[
|
||||
// matrix3_scale([3,1]),
|
||||
// matrix3_rot(90),
|
||||
// matrix3_translate([5,5])
|
||||
// ]
|
||||
// ); // Returns [[5,5], [5,20], [5,35], [5,50]]
|
||||
function matrix3_apply(pts, matrices) =
|
||||
let(m = matrix3_mult(matrices))
|
||||
[for (p = pts) point2d(m * concat(point2d(p),[1]))];
|
||||
|
||||
|
||||
|
||||
// Section: Affine Transformation 4x4 Matrices
|
||||
|
||||
|
||||
// Function: matrix4_translate()
|
||||
// Description:
|
||||
// Returns the 4x4 matrix to perform a 3D translation.
|
||||
// Arguments:
|
||||
// v = 3D offset to translate by. [X,Y,Z]
|
||||
function matrix4_translate(v) = [
|
||||
[1, 0, 0, v.x],
|
||||
[0, 1, 0, v.y],
|
||||
[0, 0, 1, v.z],
|
||||
[0 ,0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: matrix4_scale()
|
||||
// Description:
|
||||
// Returns the 4x4 matrix to perform a 3D scaling transformation.
|
||||
// Arguments:
|
||||
// v = 3D vector of scaling factors. [X,Y,Z]
|
||||
function matrix4_scale(v) = [
|
||||
[v.x, 0, 0, 0],
|
||||
[ 0, v.y, 0, 0],
|
||||
[ 0, 0, v.z, 0],
|
||||
[ 0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: matrix4_xrot()
|
||||
// Description:
|
||||
// Returns the 4x4 matrix to perform a rotation of a 3D vector around the X axis.
|
||||
// Arguments:
|
||||
// ang = number of degrees to rotate.
|
||||
function matrix4_xrot(ang) = [
|
||||
[1, 0, 0, 0],
|
||||
[0, cos(ang), -sin(ang), 0],
|
||||
[0, sin(ang), cos(ang), 0],
|
||||
[0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: matrix4_yrot()
|
||||
// Description:
|
||||
// Returns the 4x4 matrix to perform a rotation of a 3D vector around the Y axis.
|
||||
// Arguments:
|
||||
// ang = Number of degrees to rotate.
|
||||
function matrix4_yrot(ang) = [
|
||||
[ cos(ang), 0, sin(ang), 0],
|
||||
[ 0, 1, 0, 0],
|
||||
[-sin(ang), 0, cos(ang), 0],
|
||||
[ 0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: matrix4_zrot()
|
||||
// Usage:
|
||||
// matrix4_zrot(ang)
|
||||
// Description:
|
||||
// Returns the 4x4 matrix to perform a rotation of a 3D vector around the Z axis.
|
||||
// Arguments:
|
||||
// ang = number of degrees to rotate.
|
||||
function matrix4_zrot(ang) = [
|
||||
[cos(ang), -sin(ang), 0, 0],
|
||||
[sin(ang), cos(ang), 0, 0],
|
||||
[ 0, 0, 1, 0],
|
||||
[ 0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: matrix4_rot_by_axis()
|
||||
// Usage:
|
||||
// matrix4_rot_by_axis(u, ang);
|
||||
// Description:
|
||||
// Returns the 4x4 matrix to perform a rotation of a 3D vector around an axis.
|
||||
// Arguments:
|
||||
// u = 3D axis vector to rotate around.
|
||||
// ang = number of degrees to rotate.
|
||||
function matrix4_rot_by_axis(u, ang) = let(
|
||||
u = normalize(u),
|
||||
c = cos(ang),
|
||||
c2 = 1-c,
|
||||
s = sin(ang)
|
||||
) [
|
||||
[u[0]*u[0]*c2+c , u[0]*u[1]*c2-u[2]*s, u[0]*u[2]*c2+u[1]*s, 0],
|
||||
[u[1]*u[0]*c2+u[2]*s, u[1]*u[1]*c2+c , u[1]*u[2]*c2-u[0]*s, 0],
|
||||
[u[2]*u[0]*c2-u[1]*s, u[2]*u[1]*c2+u[0]*s, u[2]*u[2]*c2+c , 0],
|
||||
[ 0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: matrix4_skew_xy()
|
||||
// Usage:
|
||||
// matrix4_skew_xy(xa, ya)
|
||||
// Description:
|
||||
// Returns the 4x4 matrix to perform a skew transformation along the XY plane..
|
||||
// Arguments:
|
||||
// xa = Skew angle, in degrees, in the direction of the X axis.
|
||||
// ya = Skew angle, in degrees, in the direction of the Y axis.
|
||||
function matrix4_skew_xy(xa, ya) = [
|
||||
[1, 0, tan(xa), 0],
|
||||
[0, 1, tan(ya), 0],
|
||||
[0, 0, 1, 0],
|
||||
[0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: matrix4_skew_xz()
|
||||
// Usage:
|
||||
// matrix4_skew_xz(xa, za)
|
||||
// Description:
|
||||
// Returns the 4x4 matrix to perform a skew transformation along the XZ plane.
|
||||
// Arguments:
|
||||
// xa = Skew angle, in degrees, in the direction of the X axis.
|
||||
// za = Skew angle, in degrees, in the direction of the Z axis.
|
||||
function matrix4_skew_xz(xa, za) = [
|
||||
[1, tan(xa), 0, 0],
|
||||
[0, 1, 0, 0],
|
||||
[0, tan(za), 1, 0],
|
||||
[0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: matrix4_skew_yz()
|
||||
// Usage:
|
||||
// matrix4_skew_yz(ya, za)
|
||||
// Description:
|
||||
// Returns the 4x4 matrix to perform a skew transformation along the YZ plane.
|
||||
// Arguments:
|
||||
// ya = Skew angle, in degrees, in the direction of the Y axis.
|
||||
// za = Skew angle, in degrees, in the direction of the Z axis.
|
||||
function matrix4_skew_yz(ya, za) = [
|
||||
[ 1, 0, 0, 0],
|
||||
[tan(ya), 1, 0, 0],
|
||||
[tan(za), 0, 1, 0],
|
||||
[ 0, 0, 0, 1]
|
||||
];
|
||||
|
||||
|
||||
// Function: matrix4_mult()
|
||||
// Usage:
|
||||
// matrix4_mult(matrices)
|
||||
// Description:
|
||||
// Returns a 4x4 transformation matrix which results from applying each matrix in `matrices` in order.
|
||||
// Arguments:
|
||||
// matrices = A list of 4x4 matrices.
|
||||
function matrix4_mult(matrices, _m=undef, _i=0) =
|
||||
(_i>=len(matrices))? (is_undef(_m)? ident(4) : _m) :
|
||||
matrix4_mult(matrices, _m=(is_undef(_m)? matrices[_i] : matrices[_i] * _m), _i=_i+1);
|
||||
|
||||
|
||||
// Function: matrix4_apply()
|
||||
// Usage:
|
||||
// matrix4_apply(pts, matrices)
|
||||
// Description:
|
||||
// Given a list of transformation matrices, applies them in order to the points in the point list.
|
||||
// Arguments:
|
||||
// pts = A list of 3D points to transform.
|
||||
// matrices = A list of 4x4 matrices to apply, in order.
|
||||
// Example:
|
||||
// npts = matrix4_apply(
|
||||
// pts = [for (x=[0:3]) [5*x,0,0]],
|
||||
// matrices =[
|
||||
// matrix4_scale([2,1,1]),
|
||||
// matrix4_zrot(90),
|
||||
// matrix4_translate([5,5,10])
|
||||
// ]
|
||||
// ); // Returns [[5,5,10], [5,15,10], [5,25,10], [5,35,10]]
|
||||
|
||||
function matrix4_apply(pts, matrices) =
|
||||
let(m = matrix4_mult(matrices))
|
||||
[for (p = pts) point3d(m * concat(point3d(p),[1]))];
|
||||
|
||||
|
||||
|
||||
// vim: noexpandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap
|
|
@ -248,11 +248,11 @@ module extrude_2dpath_along_spiral(polyline, h, r, twist=360, center=undef, orie
|
|||
dx = r*cos(a),
|
||||
dy = r*sin(a),
|
||||
dz = h * (p/steps),
|
||||
pts = matrix4_apply(
|
||||
pts = affine3d_apply(
|
||||
polyline, [
|
||||
matrix4_xrot(90),
|
||||
matrix4_zrot(a),
|
||||
matrix4_translate([dx, dy, dz-h/2])
|
||||
affine3d_xrot(90),
|
||||
affine3d_zrot(a),
|
||||
affine3d_translate([dx, dy, dz-h/2])
|
||||
]
|
||||
)
|
||||
) for (pt = pts) pt
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
if [[ $# > 0 ]]; then
|
||||
PREVIEW_LIBS="$@"
|
||||
else
|
||||
PREVIEW_LIBS="compat attachments math arrays vectors matrices coords geometry triangulation quaternions hull constants edges transforms primitives shapes masks shapes2d paths beziers walls metric_screws threading involute_gears sliders joiners linear_bearings nema_steppers wiring phillips_drive torx_drive polyhedra debug"
|
||||
PREVIEW_LIBS="compat attachments math arrays vectors affine coords geometry triangulation quaternions hull constants edges transforms primitives shapes masks shapes2d paths beziers walls metric_screws threading involute_gears sliders joiners linear_bearings nema_steppers wiring phillips_drive torx_drive polyhedra debug"
|
||||
fi
|
||||
|
||||
dir="$(basename $PWD)"
|
||||
|
|
2
std.scad
2
std.scad
|
@ -14,7 +14,7 @@ include <compat.scad>
|
|||
include <math.scad>
|
||||
include <arrays.scad>
|
||||
include <vectors.scad>
|
||||
include <matrices.scad>
|
||||
include <affine.scad>
|
||||
include <quaternions.scad>
|
||||
include <coords.scad>
|
||||
include <geometry.scad>
|
||||
|
|
|
@ -444,7 +444,7 @@ module zflip(cp=[0,0,0]) translate(cp) mirror([0,0,1]) translate(-cp) children()
|
|||
// skew_xy(xa=30, ya=15) cube(size=10);
|
||||
// Example(2D):
|
||||
// skew_xy(xa=15,ya=30,planar=true) square(30);
|
||||
module skew_xy(xa=0, ya=0, planar=false) multmatrix(m = planar? matrix3_skew(xa, ya) : matrix4_skew_xy(xa, ya)) children();
|
||||
module skew_xy(xa=0, ya=0, planar=false) multmatrix(m = planar? affine2d_skew(xa, ya) : affine3d_skew_xy(xa, ya)) children();
|
||||
|
||||
|
||||
// Module: skew_yz()
|
||||
|
@ -462,7 +462,7 @@ module skew_xy(xa=0, ya=0, planar=false) multmatrix(m = planar? matrix3_skew(xa,
|
|||
// Example(FlatSpin):
|
||||
// #cube(size=10);
|
||||
// skew_yz(ya=30, za=15) cube(size=10);
|
||||
module skew_yz(ya=0, za=0) multmatrix(m = matrix4_skew_yz(ya, za)) children();
|
||||
module skew_yz(ya=0, za=0) multmatrix(m = affine3d_skew_yz(ya, za)) children();
|
||||
|
||||
|
||||
// Module: skew_xz()
|
||||
|
@ -480,7 +480,7 @@ module skew_yz(ya=0, za=0) multmatrix(m = matrix4_skew_yz(ya, za)) children();
|
|||
// Example(FlatSpin):
|
||||
// #cube(size=10);
|
||||
// skew_xz(xa=15, za=-10) cube(size=10);
|
||||
module skew_xz(xa=0, za=0) multmatrix(m = matrix4_skew_xz(xa, za)) children();
|
||||
module skew_xz(xa=0, za=0) multmatrix(m = affine3d_skew_xz(xa, za)) children();
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue