From 77f74c36ecd0258ef5d100742fd7cb5a32825c2d Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Fri, 29 Apr 2022 09:12:10 +0800 Subject: [PATCH] optimization --- src/surface/sf_solidify.scad | 251 +++++++++++++++++------------------ 1 file changed, 120 insertions(+), 131 deletions(-) diff --git a/src/surface/sf_solidify.scad b/src/surface/sf_solidify.scad index 103a9cdd..fdea126b 100644 --- a/src/surface/sf_solidify.scad +++ b/src/surface/sf_solidify.scad @@ -15,142 +15,131 @@ module sf_solidify(surface1, surface2, slicing = "SLASH", convexity = 1) { rows = len(surface1); columns = len(surface1[0]); + // dimensionality reduction + indices = [ + for(y = [0:rows - 1]) + [for(x = [0:columns - 1]) y * columns + x] + ]; + + flatted_sf1 = flat(surface1); + flatted_sf2 = flat(surface2); + + leng_pts = len(flatted_sf1); + yi_range = [0:rows - 2]; - xi_range = [0:columns - 2]; + xi_range = [0:columns - 2]; - module faces() { - function xy_to_index(x, y, columns) = y * columns + x; + sf1_tri_faces1 = slicing == "SLASH" ? [ + for(yi = yi_range, xi = xi_range) + [ + indices[yi][xi], + indices[yi + 1][xi + 1], + indices[yi][xi + 1] + ] + ] : [ + for(yi = yi_range, xi = xi_range) + [ + indices[yi][xi], + dim_reduce(xi, yi + 1, columns), + dim_reduce(xi + 1, yi, columns) + ] + ]; - flatted_sf1 = flat(surface1); - flatted_sf2 = flat(surface2); - - leng_pts = len(flatted_sf1); - - sf1_tri_faces1 = slicing == "SLASH" ? [ - for(yi = yi_range, xi = xi_range) + sf1_tri_faces2 = slicing == "SLASH" ? [ + for(yi = yi_range, xi = xi_range) + [ + indices[yi][xi], + indices[yi + 1][xi], + indices[yi + 1][xi + 1] + ] + ] : [ + for(yi = yi_range, xi = xi_range) + [ + indices[yi + 1][xi], + indices[yi + 1][xi + 1], + indices[yi][xi + 1] + ] + ]; + + offset_v = [leng_pts, leng_pts, leng_pts]; + sf2_tri_faces1 = [ + for(face = sf1_tri_faces1) + reverse(face) + offset_v + ]; + + sf2_tri_faces2 = [ + for(face = sf1_tri_faces2) + reverse(face) + offset_v + ]; + + side_faces1 = [ + for(xi = xi_range) + let( + idx1 = indices[0][xi], + idx2 = indices[0][xi + 1] + ) [ - xy_to_index(xi, yi, columns), - xy_to_index(xi + 1, yi + 1, columns), - xy_to_index(xi + 1, yi, columns) - ] - ] : [ - for(yi = yi_range, xi = xi_range) + idx1, + idx2, + idx2 + leng_pts, + idx1 + leng_pts + ] + ]; + + side_faces2 = [ + for(yi = yi_range) + let( + xi = columns - 1, + idx1 = indices[yi][xi], + idx2 = indices[yi + 1][xi] + ) + [ + idx1, + idx2, + idx2 + leng_pts, + idx1 + leng_pts + ] + ]; + + side_faces3 = [ + for(xi = xi_range) + let( + yi = rows - 1, + idx1 = indices[yi][xi], + idx2 = indices[yi][xi + 1] + ) [ - xy_to_index(xi, yi, columns), - xy_to_index(xi, yi + 1, columns), - xy_to_index(xi + 1, yi, columns) - ] - ]; - - sf1_tri_faces2 = slicing == "SLASH" ? [ - for(yi = yi_range, xi = xi_range) + idx2, + idx1, + idx1 + leng_pts, + idx2 + leng_pts + ] + ]; + + side_faces4 = [ + for(yi = yi_range) + let( + idx1 = indices[yi][0], + idx2 = indices[yi + 1][0] + ) [ - xy_to_index(xi, yi, columns), - xy_to_index(xi, yi + 1, columns), - xy_to_index(xi + 1, yi + 1, columns) - ] - ] : [ - for(yi = yi_range, xi = xi_range) - [ - xy_to_index(xi, yi + 1, columns), - xy_to_index(xi + 1, yi + 1, columns), - xy_to_index(xi + 1, yi, columns) - ] - ]; - - offset_v = [leng_pts, leng_pts, leng_pts]; - sf2_tri_faces1 = [ - for(face = sf1_tri_faces1) - reverse(face) + offset_v - ]; - - sf2_tri_faces2 = [ - for(face = sf1_tri_faces2) - reverse(face) + offset_v - ]; - - side_faces1 = [ - for(xi = xi_range) - let( - idx1 = xy_to_index(xi, 0, columns), - idx2 = xy_to_index(xi + 1, 0, columns) - ) - [ - idx1, - idx2, - idx2 + leng_pts, - idx1 + leng_pts - ] - ]; - - side_faces2 = [ - for(yi = yi_range) - let( - xi = columns - 1, - idx1 = xy_to_index(xi, yi, columns), - idx2 = xy_to_index(xi, yi + 1, columns) - ) - [ - idx1, - idx2, - idx2 + leng_pts, - idx1 + leng_pts - ] - ]; - - side_faces3 = [ - for(xi = xi_range) - let( - yi = rows - 1, - idx1 = xy_to_index(xi, yi, columns), - idx2 = xy_to_index(xi + 1, yi, columns) - ) - [ - idx2, - idx1, - idx1 + leng_pts, - idx2 + leng_pts - ] - ]; - - side_faces4 = [ - for(yi = yi_range) - let( - idx1 = xy_to_index(0, yi, columns), - idx2 = xy_to_index(0, yi + 1, columns) - ) - [ - idx2, - idx1, - idx1 + leng_pts, - idx2 + leng_pts - ] - ]; - - pts = concat(flatted_sf1, flatted_sf2); - face_idxs = concat( - sf1_tri_faces1, sf1_tri_faces2, - sf2_tri_faces1, sf2_tri_faces2, - side_faces1, - side_faces2, - side_faces3, - side_faces4 - ); - - polyhedron( - points = pts, - faces = face_idxs, - convexity = convexity - ); - - // hook for testing - test_surface_grapher_faces(pts, face_idxs); - } - - faces(); -} - -// override it to test -module test_surface_grapher_faces(surface1, faces) { + idx2, + idx1, + idx1 + leng_pts, + idx2 + leng_pts + ] + ]; + polyhedron( + points = concat(flatted_sf1, flatted_sf2), + faces = concat( + sf1_tri_faces1, sf1_tri_faces2, + sf2_tri_faces1, sf2_tri_faces2, + side_faces1, + side_faces2, + side_faces3, + side_faces4 + ), + convexity = convexity + ); } \ No newline at end of file