Compare commits

...

3 commits

Author SHA1 Message Date
Arthur Moore e7ef96bbcf Stop using as many hacks in sweep_rounded 2024-04-17 22:34:49 -04:00
Arthur Moore ff3a325b37 Update affine_translate to support all axes. 2024-04-17 22:34:30 -04:00
Arthur Moore 20492634d8 Update affine_rotate to support all axes. 2024-04-17 22:34:03 -04:00

View file

@ -71,28 +71,46 @@ function vector_as_unit(vector) = vector / vector_magnitude(vector);
*/ */
function atanv(vector) = atan2(vector.y, vector.x); function atanv(vector) = atan2(vector.y, vector.x);
/** function _affine_rotate_x(angle_x) = [
* @brief Affine transformation matrix for 2d rotation on the X,Y plane. [1, 0, 0, 0],
* @param angle The angle to rotate things by [0, cos(angle_x), -sin(angle_x), 0],
* @returns an Affine transformation matrix for use with `multmatrix()` [0, sin(angle_x), cos(angle_x), 0],
*/ [0, 0, 0, 1]
function affine_rotation(angle) = [ ];
[cos(angle), 0, sin(angle), 0],
function _affine_rotate_y(angle_y) = [
[cos(angle_y), 0, sin(angle_y), 0],
[0, 1, 0, 0], [0, 1, 0, 0],
[-sin(angle), 0, cos(angle), 0], [-sin(angle_y), 0, cos(angle_y), 0],
[0, 0, 0, 1]
];
function _affine_rotate_z(angle_z) = [
[cos(angle_z), -sin(angle_z), 0, 0],
[sin(angle_z), cos(angle_z), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1] [0, 0, 0, 1]
]; ];
/** /**
* @brief Affine transformation matrix for 2d translation on the X,Y plane. * @brief Affine transformation matrix equivalent of `rotate`
* @param vector 2d Vector to translate by. * @param angle_vector @see `rotate`
* @returns an Affine transformation matrix for use with `multmatrix()` * @details Equivalent to `rotate([0, angle, 0])`
* @returns An affine transformation matrix for use with `multmatrix()`
*/ */
function affine_translation(vector) = [ function affine_rotate(angle_vector) =
[1, 0, 0, vector.y], _affine_rotate_z(angle_vector.z) * _affine_rotate_y(angle_vector.y) * _affine_rotate_x(angle_vector.x);
[0, 1, 0, 0],
[0, 0, 1, vector.x], /**
* @brief Affine transformation matrix equivalent of `translate`
* @param vector @see `translate`
* @returns An affine transformation matrix for use with `multmatrix()`
*/
function affine_translate(vector) = [
[1, 0, 0, vector.x],
[0, 1, 0, vector.y],
[0, 0, 1, vector.z],
[0, 0, 0, 1] [0, 0, 0, 1]
]; ];
@ -118,39 +136,21 @@ module sweep_rounded(width=10, length=10) {
]; ];
// These contain the translations, but not the rotations // These contain the translations, but not the rotations
// OpenSCAD requires this hacky for loop to get accumulate to work! // OpenSCAD requires this hacky for loop to get accumulate to work!
first_translation = affine_translation(path_points[0]); first_translation = affine_translate([path_points[0].y, 0,path_points[0].x]);
affine_translations = concat([first_translation], [ affine_translations = concat([first_translation], [
for (i = 0, a = first_translation; for (i = 0, a = first_translation;
i < len(path_vectors); i < len(path_vectors);
a=a * affine_translation(path_vectors[i]), i=i+1) a=a * affine_translate([path_vectors[i].y, 0, path_vectors[i].x]), i=i+1)
a * affine_translation(path_vectors[i]) a * affine_translate([path_vectors[i].y, 0, path_vectors[i].x])
]); ]);
// Affine matrix to rotate around X axis
rot_x = 90;
x_matrix = [
[1, 0, 0, 0],
[0, cos(rot_x), -sin(rot_x), 0],
[0, sin(rot_x), cos(rot_x), 0],
[0, 0, 0, 1]
];
// Affine matrix to rotate around Z axis
z_rot = 90;
z_matrix = [
[cos(z_rot), -sin(z_rot), 0, 0],
[sin(z_rot), cos(z_rot), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
];
// Bring extrusion to the xy plane // Bring extrusion to the xy plane
affine_matrix = z_matrix * x_matrix; affine_matrix = affine_rotate([90, 0, 90]);
walls = [ walls = [
for (i = [0 : len(path_vectors) - 1]) for (i = [0 : len(path_vectors) - 1])
affine_matrix * affine_translations[i] affine_matrix * affine_translations[i]
* affine_rotation(atanv(path_vectors[i])) * affine_rotate([0, atanv(path_vectors[i]), 0])
]; ];
union() union()
@ -161,8 +161,7 @@ module sweep_rounded(width=10, length=10) {
children(); children();
// Rounded Corners // Rounded Corners
multmatrix(walls[i] multmatrix(walls[i] * affine_rotate([-90, 0, 0]))
*x_matrix*x_matrix*x_matrix *z_matrix*z_matrix*z_matrix*z_matrix)
rotate_extrude(angle = 90, convexity = 4) rotate_extrude(angle = 90, convexity = 4)
children(); children();
} }