Skip to content

Commit

Permalink
Bring logic into JSCAD output, keep logic in JavaScript for OpenSCAD
Browse files Browse the repository at this point in the history
  • Loading branch information
Hermann-SW committed Aug 19, 2022
1 parent f12a234 commit 160d73c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 41 deletions.
35 changes: 22 additions & 13 deletions jscad.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ if (true) {

wlog("function getParameterDefinitions() {");
wlog(" return [");
wlog(" { name: 'look_inside', type: 'checkbox', caption: 'look_inside:', initial: '20', checked: true }");
wlog(" { name: 'faces', type: 'choice', values: ['Pentagons', '6coloring', 'None'], initial: 'Pentagons', caption: 'face coloring:' },");
wlog(" ,{ name: 'white', type: 'checkbox', checked: true, initial: '20', caption: 'surface of sphere:' },");
wlog(" ,{ name: 'half', type: 'checkbox', checked: true, initial: '20', caption: 'half vertex:' },");
wlog(" ,{ name: 'vtxt', type: 'choice', values: ['Id', 'Type', 'None'], initial: 'Type', caption: 'vtxt:' },");
wlog(" ,{ name: 'look_inside', type: 'choice', values: ['no', 'yes'], initial: 'no', caption: 'look_inside:' }");
wlog(" ];");
wlog("}");

Expand All @@ -82,25 +86,30 @@ if (true) {
wlog("function vertex(_v, half=false) {");
wlog(" p = coords[_v] ");
wlog(" v = map_3D(p,sc)");
wlog(" la1 = degToRad(p[0])");
wlog(" ph1 = degToRad(90 - p[1])");
wlog(" return colorize([0, 0.7, 0],");
wlog(" subtract(sphere({radius: 0.5, center: v}),");
wlog(" translate([0, 0, 0],");
wlog(" rotate([0, 0, la1],");
wlog(" rotate([0, -ph1, 0],");
wlog(" translate([sc+0.5, 0],");
wlog(" rotate([degToRad(90), 0, degToRad(90)],");
wlog(" translate([-0, -0, -1],");
wlog(" cuboid({size: [1, 1, 0.8]})");
wlog(" s = sphere({radius: 0.5, center: v})");
wlog(" if (half) {");
wlog(" la1 = degToRad(p[0])");
wlog(" ph1 = degToRad(90 - p[1])");
wlog(" return colorize([0, 0.7, 0],");
wlog(" subtract(s,");
wlog(" translate([0, 0, 0],");
wlog(" rotate([0, 0, la1],");
wlog(" rotate([0, -ph1, 0],");
wlog(" translate([sc+0.5, 0],");
wlog(" rotate([degToRad(90), 0, degToRad(90)],");
wlog(" translate([-0, -0, -1],");
wlog(" cuboid({size: [1, 1, 0.8]})");
wlog(" )");
wlog(" )");
wlog(" )");
wlog(" )");
wlog(" )");
wlog(" )");
wlog(" )");
wlog(" )");
wlog(" )");
wlog(" } else {");
wlog(" return colorize([0, 0.7, 0], s)");
wlog(" }");
wlog("}");

wlog("function txt(mesg, w) {");
Expand Down
72 changes: 44 additions & 28 deletions node.tetra.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,46 @@ function jtetra(G, M, sc = 1, visited, pent) {
scad.header2();
scad.wlog("function main(params) {");
scad.wlog(" sub = [cube({size: params.look_inside?sc+0.1:0.01, center: [sc/2,-sc/2,sc/2]})]");
scad.wlog(" sub = [cube({size: (params.look_inside === 'yes')?sc+0.1:0.01, center: [sc/2,-sc/2,sc/2]})]");
scad.wlog("pentagons = (params.faces !== 'Pentagons') ? [] : [[]");
pent.forEach(function(face) {
console.log(face);
scad.wlog(",sp_tria(", face[0], ",", face[1], ",", face[2], ", sub)");
scad.wlog(",sp_tria(", face[0], ",", face[2], ",", face[3], ", sub)");
scad.wlog(",sp_tria(", face[0], ",", face[3], ",", face[4], ", sub)");
});
scad.wlog("]");
scad.wlog("white = (!params.white) ? [] : [[]");
scad.wlog(", colorize([1,1,1],");
scad.wlog(" subtract(");
scad.wlog(" sphere({radius: sc, segments: 30})");
scad.wlog(" ,sphere({radius: sc-0.1, segments: 30})");
scad.wlog(" ,sub ");
scad.wlog(" )");
scad.wlog(" )");
scad.wlog("]");

scad.wlog_("vtype = [");
forall_vertices(G, function(v) {
scad.wlog_(v>0 ? "," : " ");
scad.wlog("[", v, ",", vtype[v].toString(), "]");
});
scad.wlog("]");

scad.wlog("tvtxt = (params.vtxt === 'Type') ? 1 : 0");

if (vtype.length > 0) {
scad.wlog("vtxts = (params.vtxt === 'None') ? [] : [");
forall_vertices(G, function(v) {
scad.wlog_(v>0 ? "," : " ");
scad.wlog("vtxt(", v, ", vtype[", v, "][tvtxt])");
});
scad.wlog("]");
}

scad.wlog(" return[");


Expand All @@ -118,15 +157,12 @@ function jtetra(G, M, sc = 1, visited, pent) {
if (Ms.includes(v)) {
scad.wlog_("colorize([0.7, 0, 0], ");
}
scad.wlog_("vertex(", v, ",", vhalf && ((vtype[v] !== 0) || half0), ")");
scad.wlog_("vertex(", v, ", params.half && ((tvtxt !== 1) || (vtype[", v, "][1] !== 0)))");
if (Ms.includes(v)) {
scad.wlog(")");
} else {
scad.wlog("");
}
if (dotxt) {
scad.wlog(", vtxt(", v, ",", v, ")");
}
});

forall_edges(G, function(e) {
Expand All @@ -138,29 +174,9 @@ function jtetra(G, M, sc = 1, visited, pent) {
}
});

if (vtype.length > 0) {
forall_vertices(G, function(v) {
scad.wlog(",vtxt(", v, ",", vtype[v], ")");
});
}
pent.forEach(function(face) {
console.log(face);
scad.wlog(",sp_tria(", face[0], ",", face[1], ",", face[2], ", sub)");
scad.wlog(",sp_tria(", face[0], ",", face[2], ",", face[3], ", sub)");
scad.wlog(",sp_tria(", face[0], ",", face[3], ",", face[4], ", sub)");
});
if (white) {
scad.wlog(", colorize([1,1,1],");
scad.wlog(" subtract(");
scad.wlog(" sphere({radius: sc, segments: 30})");
scad.wlog(" ,sphere({radius: sc-0.1, segments: 30})");
scad.wlog(" ,sub ");
scad.wlog(" )");
scad.wlog(" )");
}
scad.wlog(",pentagons");
scad.wlog(",white");
scad.wlog(",vtxts");

scad.wlog("] }");
scad.wlog("module.exports = { main, getParameterDefinitions }");
Expand Down

0 comments on commit 160d73c

Please sign in to comment.