diff --git a/tests/dimension.scad b/tests/dimension.scad new file mode 100644 index 0000000..0c51ca4 --- /dev/null +++ b/tests/dimension.scad @@ -0,0 +1,81 @@ +// +// NopSCADlib Copyright Chris Palmer 2018 +// nop.head@gmail.com +// hydraraptor.blogspot.com +// +// This file is part of NopSCADlib. +// +// NopSCADlib is free software: you can redistribute it and/or modify it under the terms of the +// GNU General Public License as published by the Free Software Foundation, either version 3 of +// the License, or (at your option) any later version. +// +// NopSCADlib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; +// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with NopSCADlib. +// If not, see . +// +include <../utils/core/core.scad> +use <../utils/dimension.scad> + + +module dimensions_3d_xy() { + dimension([0,0,0], [10,10,0], "", 0.2); + dimension([0,0,0], [10,-10,0], "", 0.2); + dimension([0,0,0], [-10,10,0], "", 0.2); + dimension([0,0,0], [-10,-10,0], "", 0.2); + + + dimension([0,0,0], [0,10,0], "", 0.2); + dimension([0,0,0], [0,-10,0], "", 0.2); + dimension([0,0,0], [10,0,0], "", 0.2); + dimension([0,0,0], [-10,0,0], "", 0.2); +} + +module dimensions_3d_xyz() { + dimension([0,0,0], [10,10,10], "", 0.2); + dimension([0,0,0], [10,-10,10], "", 0.2); + dimension([0,0,0], [-10,10,10], "", 0.2); + dimension([0,0,0], [-10,-10,10], "", 0.2); + + dimension([0,0,0], [10,10,-10], "", 0.2); + dimension([0,0,0], [10,-10,-10], "", 0.2); + dimension([0,0,0], [-10,10,-10], "", 0.2); + dimension([0,0,0], [-10,-10,-10], "", 0.2); + + dimension([0,0,0], [-3,0,10], "", 0.2); + dimension([0,0,0], [0,0,-10], "", 0.2); + + dimension([0,0,0], [0,2,10], "", 0.2); + dimension([0,0,0], [0,2,-10], "", 0.2); + +} + +module dimension_1d_x() { + dimension_x([12,0,0], [18,0,0]); + dimension_x([12,5,0], [18,10,0]); + dimension_x([12,5,0], [18,10,5]); +} + +module dimension_1d_y() { + dimension_y([12,0,0], [12,-5,0]); + dimension_y([12,-8,0], [18,-10,0]); + dimension_y([12,-5,0], [18,-10,5]); +} + +module dimension_1d_z() { + dimension_z([20,0,0], [20,0,5]); + dimension_z([20,0,0], [20,0,10]); + dimension_z([20,0,0], [20,10,10]); +} + +if($preview) + dimensions_3d_xy(); + dimensions_3d_xyz(); + dimension_1d_x(); + dimension_1d_y(); + dimension_1d_z(); + + + \ No newline at end of file diff --git a/utils/dimension.scad b/utils/dimension.scad new file mode 100644 index 0000000..81ff73e --- /dev/null +++ b/utils/dimension.scad @@ -0,0 +1,157 @@ +// +// NopSCADlib Copyright Chris Palmer 2018 +// nop.head@gmail.com +// hydraraptor.blogspot.com +// +// This file is part of NopSCADlib. +// +// NopSCADlib is free software: you can redistribute it and/or modify it under the terms of the +// GNU General Public License as published by the Free Software Foundation, either version 3 of +// the License, or (at your option) any later version. +// +// NopSCADlib is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; +// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +// See the GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along with NopSCADlib. +// If not, see . +// + +// +//! Annotation used in this documentation +// + +include <../utils/core/core.scad> +include <../utils/maths.scad> + + + +//if text is empty, will display the number value +//text_plane is either "XY" or "XZ" +module dimension(startpoint, endpoint, text = "", thickness = 0.1) { + // Compute vector between points + direction = endpoint - startpoint; + length = norm(direction); + midpoint = (startpoint + endpoint) / 2; + + // Ensure nonzero values for calculations + dir_xy = norm([direction.x, direction.y]); + + // Compute rotation angles safely + //azimuth = (dir_xy == 0) ? 0 : atan2(direction.y, direction.x); + azimuth = atan2(direction.y, direction.x); + /*elevation = (direction.x == 0 && direction.y == 0) + ? ((direction.z > 0) ? -90 : 90) + : -atan2(direction.z, dir_xy);*/ + elevation = -atan2(direction.z, dir_xy); + + // Draw measurement line as a thin cylinder + translate(midpoint) + rotate([0, elevation, azimuth]) + rotate([0, 90, 0]) + cylinder(d = thickness, h = length - thickness * 2, center = true); + + // Draw endpoint markers + translate(startpoint) + rotate([0, elevation - 90, azimuth]) + translate([0, 0, -thickness * 4]) + cylinder(h = thickness * 4, r1 = thickness * 2, r2 = 0); + + translate(endpoint) + rotate([0, elevation + 90, azimuth]) + translate([0, 0, -thickness * 4]) + cylinder(h = thickness * 4, r1 = thickness * 2, r2 = 0); + + // Draw the text/distance + dir = (length > 0) ? (direction / length) * thickness * 4 : [1, 0, 0]; + up_dir = rotate_vector_3d([0,1,0], [0,0,1] ,azimuth); + + translate(midpoint + up_dir*0.66) + rotate([0, elevation, azimuth]) + linear_extrude(thickness) + text(text == "" ? str(length) : text, size = thickness * 5, valign = "center", halign = "center"); +} + +//offset will detirmine how much space is between the measured point and the dimension +//for x, this offset will be in the y direction +module dimension_x(startpoint, endpoint, offset = 1, text = "", thickness = 0.1) { + y = max(startpoint.y, endpoint.y) + offset; + z = max(startpoint.z, endpoint.z) ; + dimension([startpoint.x, y, z], [endpoint.x, y, z], text, thickness); + + v1= [startpoint.x, y, z]-startpoint; + h1 = norm(v1); + axis1 = cross([0,0,1], v1); + angle1 = atan2(norm(axis1), v1.z); + translate(startpoint) + rotate(angle1, axis1) + cylinder( h= h1+thickness*2, d=thickness); + + + v2= [endpoint.x, y, z]-endpoint; + h2 = norm(v2); + axis2 = cross([0,0,1], v2); + angle2 = atan2(norm(axis2), v2.z); + + translate(endpoint) + rotate(angle2, axis2) + cylinder( h= h2+thickness*2, d=thickness); +} + +//offset will detirmine how much space is between the measured point and the dimension +//for y, this offset will be in the x direction +module dimension_y(startpoint, endpoint, offset = 1, text = "", thickness = 0.1) { + x = max(startpoint.x, endpoint.x) + offset; + z = max(startpoint.z, endpoint.z) ; + dimension([x, startpoint.y, z], [x, endpoint.y, z], text, thickness); + + v1= [x, startpoint.y, z]-startpoint; + h1 = norm(v1); + axis1 = cross([0,0,1], v1); + angle1 = atan2(norm(axis1), v1.z); + + translate(startpoint) + rotate(angle1, axis1) + cylinder( h= h1+thickness*2, d=thickness); + + + v2= [x, endpoint.y, z]-endpoint; + h2 = norm(v2); + axis2 = cross([0,0,1], v2); + angle2 = atan2(norm(axis2), v2.z); + + translate(endpoint) + rotate(angle2, axis2) + cylinder( h= h2+thickness*2, d=thickness); +} + +//offset will detirmine how much space is between the measured point and the dimension +//for z, this offset will be in the x direction +module dimension_z(startpoint, endpoint, offset = 1, text = "", thickness = 0.1) { + x = max(startpoint.x, endpoint.x) + offset; + y = max(startpoint.y, endpoint.y) ; + dimension([x, y, startpoint.z], [x, y, endpoint.z], text, thickness); + + v1= [x, y, startpoint.z]-startpoint; + h1 = norm(v1); + axis1 = cross([0,0,1], v1); + angle1 = atan2(norm(axis1), v1.z); + + translate(startpoint) + rotate(angle1, axis1) + cylinder( h= h1+thickness*2, d=thickness); + + + v2= [x, y, endpoint.z]-endpoint; + h2 = norm(v2); + axis2 = cross([0,0,1], v2); + angle2 = atan2(norm(axis2), v2.z); + + translate(endpoint) + rotate(angle2, axis2) + cylinder( h= h2+thickness*2, d=thickness); +} + + + + diff --git a/utils/maths.scad b/utils/maths.scad index 3d03f47..4fea047 100644 --- a/utils/maths.scad +++ b/utils/maths.scad @@ -191,3 +191,32 @@ function cubic_real_roots(a, b, c, d) = //! Returns real roots of cubic equation function path_length(path, i = 0, length = 0) = //! Calculated the length along a path i >= len(path) - 1 ? length : path_length(path, i + 1, length + norm(path[i + 1] - path[i])); + + function rotate_vector_2d(v, angle) = [ + v[0] * cos(angle) - v[1] * sin(angle), + v[0] * sin(angle) + v[1] * cos(angle) +]; + +function rotate_vector_2d(v, angle) = [ + v[0] * cos(angle) - v[1] * sin(angle), + v[0] * sin(angle) + v[1] * cos(angle) +]; + +function rotation_matrix(axis, angle) = let( + u = axis / norm(axis), // Normalize axis + ux = u[0], uy = u[1], uz = u[2], + cosA = cos(angle), sinA = sin(angle), + one_minus_cosA = 1 - cosA +) [ + [cosA + ux*ux*one_minus_cosA, ux*uy*one_minus_cosA - uz*sinA, ux*uz*one_minus_cosA + uy*sinA], + [uy*ux*one_minus_cosA + uz*sinA, cosA + uy*uy*one_minus_cosA, uy*uz*one_minus_cosA - ux*sinA], + [uz*ux*one_minus_cosA - uy*sinA, uz*uy*one_minus_cosA + ux*sinA, cosA + uz*uz*one_minus_cosA] +]; + +function rotate_vector_3d(v, axis, angle) = let( + mat = rotation_matrix(axis, angle) +) [ + mat[0][0]*v[0] + mat[0][1]*v[1] + mat[0][2]*v[2], + mat[1][0]*v[0] + mat[1][1]*v[1] + mat[1][2]*v[2], + mat[2][0]*v[0] + mat[2][1]*v[1] + mat[2][2]*v[2] +]; \ No newline at end of file