1
0
mirror of https://github.com/JustinSDK/dotSCAD.git synced 2025-01-17 22:28:16 +01:00

performance improved when using "FACES"

This commit is contained in:
Justin Lin 2017-05-15 14:13:50 +08:00
parent fd2acd3a36
commit d479c57893

View File

@ -16,44 +16,100 @@
**/ **/
module function_grapher(points, thickness, style = "FACES", slicing = "SLASH") { module function_grapher(points, thickness, style = "FACES", slicing = "SLASH") {
rows = len(points);
columns = len(points[0]);
// Increasing $fn will be slow when you use "LINES" or "HULL_FACES". // Increasing $fn will be slow when you use "LINES" or "HULL_FACES".
function tri_shell_points(top) = module faces() {
let( top_pts = [
z_offset = [0, 0, -thickness], for(row_pts = points)
bottom = [ for(pt = row_pts)
top[0] + z_offset, pt
top[1] + z_offset, ];
top[2] + z_offset
], base_pts = [
faces = [ for(pt = top_pts)
[0, 1, 2], [pt[0], pt[1], pt[2] - thickness]
[3, 4, 5],
[0, 1, 4, 3],
[1, 2, 5, 4],
[2, 0, 3, 5]
]
)
[
concat(top, bottom),
faces
]; ];
leng_pts = len(top_pts);
top_faces = [
for(yi = [0:rows - 2])
for(xi = [0:columns - 2])
[
xy_to_index(xi, yi, columns),
xy_to_index(xi + 1, yi, columns),
xy_to_index(xi + 1, yi + 1, columns),
xy_to_index(xi, yi + 1, columns)
]
];
module tri_to_faces(top_tri1, top_tri2) { base_faces = [
pts_faces1 = tri_shell_points(top_tri1); for(face = top_faces)
pts_faces2 = tri_shell_points(top_tri2); face + [leng_pts, leng_pts, leng_pts, leng_pts]
];
// hull is for preventing from WARNING: Object may not be a valid 2-manifold side_faces1 = [
hull() polyhedron( for(xi = [0:columns - 2])
points = pts_faces1[0], [xi, xi + 1, xi + 1 + leng_pts, xi + leng_pts]
faces = pts_faces1[1] ];
);
hull() polyhedron( side_faces2 = [
points = pts_faces2[0], for(yi = [0:rows - 2])
faces = pts_faces2[1] let(
); xi = columns - 1,
idx1 = xy_to_index(xi, yi, columns),
idx2 = xy_to_index(xi, yi + 1, columns)
)
[
idx1,
idx1 + leng_pts,
idx2 + leng_pts,
idx2
]
];
side_faces3 = [
for(xi = [0:columns - 2])
let(
idx1 = xy_to_index(xi, rows - 1, columns),
idx2 = xy_to_index(xi + 1, rows - 1, columns)
)
[
idx1, idx2,
idx2 + leng_pts,
idx1 + leng_pts
]
];
side_faces4 = [
for(yi = [0:rows - 2])
let(
idx1 = xy_to_index(0, yi, columns),
idx2 = xy_to_index(0, yi + 1, columns)
)
[
idx1,
idx1 + leng_pts,
idx2 + leng_pts,
idx2
]
];
polyhedron(
points = concat(top_pts, base_pts),
faces = concat(
top_faces,
base_faces,
side_faces1,
side_faces2,
side_faces3,
side_faces4
)
);
} }
module tri_to_lines(tri1, tri2) { module tri_to_lines(tri1, tri2) {
@ -76,38 +132,44 @@ module function_grapher(points, thickness, style = "FACES", slicing = "SLASH") {
} }
module tri_to_graph(tri1, tri2) { module tri_to_graph(tri1, tri2) {
if(style == "FACES") { if(style == "LINES") {
tri_to_faces(tri1, tri2);
} else if(style == "LINES") {
tri_to_lines(tri1, tri2); tri_to_lines(tri1, tri2);
} else { // Warning: May be very slow!! } else { // Warning: May be very slow!!
tri_to_hull_faces(tri1, tri2); tri_to_hull_faces(tri1, tri2);
} }
} }
function xy_to_index(x, y, columns) = y * columns + x;
for(yi = [0:len(points) - 2]) {
for(xi = [0:len(points[yi]) - 2]) { if(style == "FACES") {
if(slicing == "SLASH") { faces();
tri_to_graph([ } else {
points[yi][xi], for(yi = [0:rows - 2]) {
points[yi][xi + 1], for(xi = [0:columns - 2]) {
points[yi + 1][xi + 1] if(slicing == "SLASH") {
], [ tri_to_graph([
points[yi][xi], points[yi][xi],
points[yi + 1][xi + 1], points[yi][xi + 1],
points[yi + 1][xi] points[yi + 1][xi + 1]
]); ], [
} else { points[yi][xi],
tri_to_graph([ points[yi + 1][xi + 1],
points[yi][xi], points[yi + 1][xi]
points[yi][xi + 1], ]);
points[yi + 1][xi] } else {
], [ tri_to_graph([
points[yi + 1][xi], points[yi][xi],
points[yi][xi + 1], points[yi][xi + 1],
points[yi + 1][xi + 1] points[yi + 1][xi]
]); ], [
} points[yi + 1][xi],
points[yi][xi + 1],
points[yi + 1][xi + 1]
]);
}
}
} }
} }
} }