Remove start/end angle, do substantial cleanup
This commit is contained in:
110
fcad.scad
110
fcad.scad
@@ -1,8 +1,8 @@
|
||||
fDraw(fSphere(5));
|
||||
fDraw(fSphere(10));
|
||||
|
||||
///// Drawing modules
|
||||
module fDraw(model) {
|
||||
polyhedron(points=fPoints(model), faces=fFaces(model));
|
||||
polyhedron(points=fPoints(model), faces=fFaces(model));
|
||||
}
|
||||
|
||||
module fDrawN(model, scales) {
|
||||
@@ -11,8 +11,8 @@ module fDrawN(model, scales) {
|
||||
model_max = fMaxPoint(model);
|
||||
translation =
|
||||
[for (axis = [0 : 2])
|
||||
scales[axis] * model_max[axis]];
|
||||
translate(translation) children();
|
||||
scales[axis] * model_max[axis]];
|
||||
translate(translation) children();
|
||||
}
|
||||
|
||||
module fDrawX(model) {
|
||||
@@ -45,64 +45,45 @@ function fCube(dims) = (
|
||||
]
|
||||
);
|
||||
|
||||
function fCylinder(r, h, start_angle=0, end_angle=360, sides=36) = (
|
||||
let(degrees_per_face = (end_angle - start_angle) / sides,
|
||||
face_range = [0 : sides - 1],
|
||||
vertical_range = [0 : sides],
|
||||
complete = (start_angle == 0 && end_angle == 360),
|
||||
bottom_index = (sides + 1) * 2)
|
||||
function fCylinder(h, r, sides=36) = (
|
||||
let(degrees_per_side = 360 / sides,
|
||||
side_range = [0 : sides - 1])
|
||||
[
|
||||
[fKeyPoints, concat(
|
||||
[fKeyPoints,
|
||||
fCartesianProduct([
|
||||
[for (i = vertical_range)
|
||||
let (angle = start_angle + (i * degrees_per_face))
|
||||
[for (side = side_range)
|
||||
let (angle = side * degrees_per_side)
|
||||
[r * sin(angle), r * cos(angle)]],
|
||||
[0, h],
|
||||
]),
|
||||
complete ? [] : [
|
||||
[0, 0, 0], // bottom center
|
||||
[0, 0, h], // top center
|
||||
]
|
||||
)],
|
||||
],
|
||||
[fKeyFaces, concat(
|
||||
// bottom
|
||||
[concat(
|
||||
[for (i = [sides : -1 : 0]) i * 2],
|
||||
complete ? [] : [bottom_index]
|
||||
)],
|
||||
[[for (side = [sides : -1 : 0]) side * 2]],
|
||||
// top
|
||||
[concat(
|
||||
[for (i = [0 : sides]) i * 2 + 1],
|
||||
complete ? [] : [bottom_index + 1]
|
||||
)],
|
||||
[[for (side = [0 : sides]) side * 2 + 1]],
|
||||
// sides
|
||||
[for (i = face_range)
|
||||
[for (j = [2, 3, 1, 0]) i * 2 + j]],
|
||||
// cut ins
|
||||
complete ? [] : [
|
||||
[0, 1, bottom_index + 1, bottom_index],
|
||||
[bottom_index - 1, bottom_index - 2, bottom_index, bottom_index + 1],
|
||||
]
|
||||
[for (side = side_range)
|
||||
[for (vertex = [2, 3, 1, 0]) (side * 2 + vertex) % (sides * 2)]]
|
||||
)],
|
||||
]
|
||||
);
|
||||
|
||||
function fSphere(r, sides=36, stripes=18) = (
|
||||
let(stripe_slice = 180 / stripes,
|
||||
side_slice = 360 / sides,
|
||||
top_index = (stripes - 1) * sides,
|
||||
bottom_index = top_index + 1)
|
||||
let(degrees_per_slice = 180 / stripes,
|
||||
degrees_per_side = 360 / sides,
|
||||
top_index = (stripes - 1) * sides)
|
||||
[
|
||||
[fKeyPoints, concat(
|
||||
[for (stripe_index = [1 : stripes - 1])
|
||||
for (side_index = [0 : sides - 1])
|
||||
let (stripe_angle = stripe_index * stripe_slice,
|
||||
side_angle = side_index * side_slice,
|
||||
let (stripe_angle = stripe_index * degrees_per_slice,
|
||||
side_angle = side_index * degrees_per_side,
|
||||
stripe_radius = r * sin(stripe_angle),
|
||||
stripe_z = r * cos(stripe_angle),
|
||||
side_x = stripe_radius * sin(side_angle),
|
||||
side_y = stripe_radius * cos(side_angle))
|
||||
[side_x, side_y, stripe_z]
|
||||
[side_x, side_y, stripe_z]
|
||||
],
|
||||
[
|
||||
[0, 0, r], // top
|
||||
@@ -110,28 +91,39 @@ function fSphere(r, sides=36, stripes=18) = (
|
||||
]
|
||||
)],
|
||||
[fKeyFaces, concat(
|
||||
// side squares
|
||||
[for (stripe_index = [2 : stripes - 1])
|
||||
for (side_index = [0 : sides - 1])
|
||||
let (prev_side_base = (stripe_index - 2) * sides,
|
||||
side_base = (stripe_index - 1) * sides)
|
||||
[prev_side_base + (side_index + 1) % sides,
|
||||
prev_side_base + side_index,
|
||||
side_base + side_index,
|
||||
side_base + (side_index + 1) % sides]
|
||||
[
|
||||
prev_side_base + (side_index + 1) % sides,
|
||||
prev_side_base + side_index,
|
||||
side_base + side_index,
|
||||
side_base + (side_index + 1) % sides,
|
||||
]
|
||||
],
|
||||
// top triangles
|
||||
[for (side_index = [0 : sides - 1])
|
||||
[top_index, side_index, (side_index + 1) % sides]
|
||||
[
|
||||
top_index,
|
||||
side_index,
|
||||
(side_index + 1) % sides,
|
||||
]
|
||||
],
|
||||
// bottom triangles
|
||||
[for (side_index = [0 : sides - 1])
|
||||
let (side_base = (stripes - 2) * sides)
|
||||
[bottom_index,
|
||||
side_base + (side_index + 1) % sides,
|
||||
side_base + side_index]
|
||||
[
|
||||
top_index + 1,
|
||||
side_base + (side_index + 1) % sides,
|
||||
side_base + side_index,
|
||||
]
|
||||
]
|
||||
)],
|
||||
]
|
||||
);
|
||||
|
||||
|
||||
///// 3D object information functions
|
||||
function fMinPoint(model) = (
|
||||
[for (axis = [0 : 2])
|
||||
@@ -141,7 +133,7 @@ function fMinPoint(model) = (
|
||||
function fMaxPoint(model) = (
|
||||
[for (axis = [0 : 2])
|
||||
max([for (point = fPoints(model)) point[axis]])]
|
||||
);
|
||||
);
|
||||
|
||||
function fCenterPoint(model) = (
|
||||
let(model_min = fMinPoint(model),
|
||||
@@ -152,7 +144,7 @@ function fCenterPoint(model) = (
|
||||
[for (axis = [0 : 2])
|
||||
model_min[axis] + (model_size[axis] / 2)]
|
||||
);
|
||||
|
||||
|
||||
///// 3D object translation functions
|
||||
function fTranslate(model, translation) = (
|
||||
[
|
||||
@@ -173,15 +165,15 @@ function fZeroN(model, scales) = (
|
||||
scales[axis] * model_min[axis]])
|
||||
fTranslate(model, translation)
|
||||
);
|
||||
|
||||
|
||||
function fZeroX(model) = (
|
||||
fZeroN(model, [-1, 0, 0])
|
||||
);
|
||||
|
||||
|
||||
function fZeroY(model) = (
|
||||
fZeroN(model, [0, -1, 0])
|
||||
);
|
||||
|
||||
|
||||
function fZeroZ(model) = (
|
||||
fZeroN(model, [0, 0, -1])
|
||||
);
|
||||
@@ -193,7 +185,7 @@ function fCenterN(model, scales) = (
|
||||
scales[axis] * model_center[axis]])
|
||||
fTranslate(model, translation)
|
||||
);
|
||||
|
||||
|
||||
function fCenterX(model) = (
|
||||
fCenterN(model, [-1, 0, 0])
|
||||
);
|
||||
@@ -201,7 +193,7 @@ function fCenterX(model) = (
|
||||
function fCenterY(model) = (
|
||||
fCenterN(model, [0, -1, 0])
|
||||
);
|
||||
|
||||
|
||||
function fCenterZ(model) = (
|
||||
fCenterN(model, [0, 0, -1])
|
||||
);
|
||||
@@ -222,7 +214,7 @@ fTypeUnknown = "unknown";
|
||||
function fType(x) = (
|
||||
x == undef ? fTypeUndef
|
||||
: floor(x) == x ? fTypeInt
|
||||
: abs(x) + 1 > abs(x) ? fTypeFloat
|
||||
: abs(x) + 1 > abs(x) ? fTypeFloat
|
||||
: str(x) == x ? fTypeString
|
||||
: str(x) == "false" || str(x) == "true" ? fTypeBoolean
|
||||
: (x[0] == x[0]) && len(x) != undef ? fTypeVector
|
||||
@@ -269,7 +261,7 @@ function fVectorSlice(vec, start=0, step=undef, end=undef) = (
|
||||
[for (i = [start : real_step : real_end])
|
||||
vec[i]]
|
||||
);
|
||||
|
||||
|
||||
function fCartesianProduct(vecs) = (
|
||||
len(vecs) == 1 ?
|
||||
vecs[0]
|
||||
@@ -282,7 +274,7 @@ function fCartesianProduct(vecs) = (
|
||||
function fPoints(model) = (
|
||||
fMapLookup(fKeyPoints, model)
|
||||
);
|
||||
|
||||
|
||||
function fFaces(model) = (
|
||||
fMapLookup(fKeyFaces, model)
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user