V2 Feedback : converting V1 examples



  • Hello, I'm slowly converting V1 examples to V2 syntax.
    It went ok for the first example, logo :
    Capture d’écran de 2020-08-29 14-42-25.png

    // title      : OpenJSCAD.org Logo
    // author     : Rene K. Mueller
    // license    : MIT License
    // revision   : 0.003
    // tags       : Logo,Intersection,Sphere,Cube
    // file       : logo.jscad
    
    const jscad = require('@jscad/modeling')
    const { primitives, transforms, booleans } = jscad
    const { cube, sphere } = primitives
    const { intersect, subtract, union } = booleans
    const { scale, translate } = transforms
    
    function main () {
      return translate([0, 0, 1.5], 
    		scale([10, 10, 10], 
    			union(
    				subtract( 
    					cube({size: 3}),
    					sphere({radius: 2})
    				),
    				intersect(
    					sphere({radius: 1.3}),
    					cube({size: 2.1})
    				)
    			)
    		)
    	);
    }
    
    module.exports = { main }
    


  • Eighth example, lookup :
    Capture d’écran de 2020-08-30 16-16-04.png
    It looks ok, but my lookup function is maybe buggy, I needed to stretch return value (x2) to have same lengths as V1, and to add a center to each cylinders to make it look the same.

    // title      : Lookup
    // author     : OpenSCAD.org, adapted by Rene K. Mueller
    // description: testing lookup() function
    // file       : lookup.jscad
    
    // from http://en.wikibooks.org/wiki/OpenSCAD_User_Manual/Mathematical_Functions
    
    const jscad = require('@jscad/modeling')
    const { primitives, transforms, colors, maths } = jscad
    const { cylinder } = primitives
    const { translate } = transforms
    const { colorize, hslToRgb } = colors
    const { vec2 } = maths
    const { lerp } = vec2
    
    function getCylinderH (p) {
      return lookup(p, 
    		[ [ -200, 5 ], [ -50, 20 ], [ -20, 18 ], [ +80, 25 ], [ +150, 2 ] ]);
    }
    
    function main () {
      var w = [];
      for (var i = -100; i <= 100; i += 5) {
        w.push(colorize(hslToRgb([((i + 100) / 200) * 0.3 + 0.6, 1, 0.5]), 
          translate([i, 0, -30],
            cylinder({radius: 4, 
    				  height: getCylinderH(i) * 3 *2, segments: 16
    				  , center : [i,0,getCylinderH(i) * 3]
    				  }))));
      }
      return w;
    }
    
    function lookup(ix, L){ // L = [ [ i0, v0], ..., [iN, vN] ]
    	if (ix <= L[0][0])
    		return L[0][0];
    		
    	var i = L.length-1;
    	if (ix >= L[i][0])
    		return L[i][0];
    		
    	i = L.findIndex( x => x[0] >= ix);
    	v1 = L[i-1][1];
    	v2 = L[i][1];
    	
    	if (ix === L[i-1][0])
    		return v1;
    		
    	if (ix === L[i][0])
    		return v2;
    	
    	var i1 = L[i-1][0], i2 = L[i][0];
    	var r = v1 + (v2-v1) / (i2-i1) * (ix-i1);
    	
    	return r;
    }
    
    module.exports = { main }
    
    


  • Seventh example, expand :
    Capture d’écran de 2020-08-30 14-47-35.png
    It looks ok.

    // title      : Expand
    // author     : OpenSCAD.org, adapted by Rene K. Mueller
    // license    : MIT License
    // description: testing expand() function
    // file       : expand.jscad
    
    const jscad = require('@jscad/modeling')
    const { primitives, transforms, booleans, expansions } = jscad
    const { cube, cylinder } = primitives
    const { subtract, union } = booleans
    const { rotate, scale, translate } = transforms
    const { expand } = expansions
    
    function main () {
      return scale([10, 10, 10], union(
        expand({delta:0.2, segments:8}, subtract(
    			cube({size:2}),
    			translate([0.3, -0.3, 0.3], cube({size:2})))),
    
        translate([-4, 0, 0], subtract(
          cube({size:2}), 
          translate([0.3, -0.3, 0.3], cube({size:2})))),
    
    
        expand({delta:0.3, segments:8}, 
          translate([0, -3, 0], cube({size:2}))),
    
        translate([-4, -3, 0], cube({size:2})),
    
        expand({delta:0.3, segments:4}, 
    			translate([0, 4, 0], 
    				cylinder({radius: 1, height: 2, segments: 16}))),
    
        translate([-4, 4, 0], cylinder({radius: 1, height: 2}))
      ));
    }
    
    module.exports = { main }
    
    


  • Sixth example, transformations :
    Capture d’écran de 2020-08-30 10-22-07.png
    It looks ok, except for transform (so, I removed it) that does not seem to work with same parameters as V1.

    // title      : transformations
    // author     : Mark Moissette
    // license    : MIT License
    // description: all the different transforms operations
    
    const jscad = require('@jscad/modeling')
    const { primitives, transforms } = jscad
    const { cube } = primitives
    const { scale, translate, rotate, transform } = transforms
    
    function main () {
      const testCube = cube();
    
      return [
        translate([0, 10, 0], testCube), // simple translation
        translate([10, 0, 0], rotate([10, 5, 0], testCube)), // translate + rotate
        translate([-10, 0, 0], scale([0.5, 0.5, 5], testCube)), // translate + scale
        /*transform([ // matrix transform
          Math.cos(15), -Math.sin(15), 0, 0,
          Math.sin(15), Math.cos(15), 0, 0,
          0, 0, 1, 1,
          0, 0, 0, 1
        ], testCube)*/
      ]
    }
    module.exports = { main }
    


  • Fifth example, pavillon :
    Capture d’écran de 2020-08-29 16-14-28.png
    I needed to move cylinders (columns) and cube (enter) to make them cope with their v1 conterparts

    // title      : Example 005
    // author     : OpenSCAD.org, adapted by Rene K. Mueller
    // license    : MIT License
    // description: example005.scad ported to OpenJSCAD
    // file       : example005.jscad
    
    
    const jscad = require('@jscad/modeling')
    const { primitives, transforms, booleans } = jscad
    const { cube, cylinder, cylinderElliptic } = primitives
    const { subtract, union } = booleans
    const { translate, scale } = transforms
    
    function example005 () {
      var cy = [];
      for (var i = 0; i <= 5; i++) {
        cy[i] = translate([Math.sin(360 * i / 6) * 80, Math.cos(360 * i / 6) * 80, 80],
    							cylinder({height: 200, radius: 10}));
      }
      return translate([0, 0, -120],
        union(
          subtract(
            cylinder({height: 50, radius: 100}),
            translate([0, 0, 10], cylinder({height: 50, radius: 80})),
            translate([100, 0, 35-25], cube({size: 50}))
          ),
          cy,
          translate([0, 0, 200], 
            cylinderElliptic({height: 80, startRadius: [120,120], endRadius: [1,1]}))
        )
      );
    }
    
    function main () {
      return scale([1 / 3, 1 / 3, 1 / 3], example005());
    }
    
    module.exports = { main }
    
    


  • Fourth example, box with cutouts :
    Capture d’écran de 2020-08-29 15-53-38.png

    // title      : Example 003
    // author     : OpenSCAD.org, adapted by Rene K. Mueller
    // license    : MIT License
    // description: example003.scad ported to OpenJSCAD.org
    // file       : example003.jscad
    
    
    const jscad = require('@jscad/modeling')
    const { primitives, transforms, booleans } = jscad
    const { cube, cuboid } = primitives
    const { subtract, union } = booleans
    
    function example003 () {
      return subtract(
        union(
          cube({size: 30}),
          cuboid({size: [40, 15, 15]}),
          cuboid({size: [15, 40, 15]}),
          cuboid({size: [15, 15, 40]})
        ),
        union(
          cuboid({size: [50, 10, 10]}),
          cuboid({size: [10, 50, 10]}),
          cuboid({size: [10, 10, 50]})
        )
      );
    }
    
    function main () {
      return example003();
    }
    
    module.exports = { main }
    


  • Third example, cone with cutouts, looks ok :
    Capture d’écran de 2020-08-29 15-38-54.png

    // title      : Example 002
    // author     : OpenSCAD.org, adapted by Rene K. Mueller
    // license    : MIT License
    // description: example002.scad ported to OpenJSCAD.org
    // file       : example002.jscad
    
    const jscad = require('@jscad/modeling')
    const { primitives, transforms, booleans } = jscad
    const { cube, cuboid, cylinderElliptic } = primitives
    const { intersect, subtract, union } = booleans
    const { translate } = transforms
    
    function example002 () {
      return intersect(
        subtract(
          union(
            cube({size: 30}),
            translate([0, 0, -25], cuboid({size: [15, 15, 50]}))
          ),
          union(
            cuboid({size: [50, 10, 10]}),
            cuboid({size: [10, 50, 10]}),
            cuboid({size: [10, 10, 50]})
          )
        ),
        translate([0, 0, 5], 
    			cylinderElliptic({height: 50, startRadius: [20,20], endRadius: [5,5]})
        ));
    }
    
    function main () {
      return example002();
    }
    
    module.exports = { main }
    


  • This post is deleted!


  • But the second example, sphere with cutouts doesn't look the same as on V1:
    Capture d’écran de 2020-08-29 14-51-45.png
    It may be default centering that is different

    // title      : Example 001
    // author     : OpenSCAD.org, adapted by Rene K. Mueller
    // license    : MIT License
    // description: example001.scad ported to OpenJSCAD.org
    // file       : example001.jscad
    
    const jscad = require('@jscad/modeling')
    const { primitives, transforms, booleans } = jscad
    const { cylinder, sphere } = primitives
    const { subtract } = booleans
    const { rotate } = transforms
    
    function radiusFromDiameter (d) {
     return d / 2;
    }
    
    function rotcy (rot, r, h) {
     return rotate(rot, cylinder({radius: r, height: h}));
    }
    
    function example001 () {
     var size = 50;
     var hole = 25;
     var radius = radiusFromDiameter(hole);
     var height = radiusFromDiameter(size * 2.5);
    
     return subtract(
       sphere({radius: radiusFromDiameter(size)}),
       rotcy([0, 0, 0], radius, height),
       rotcy([90, 0, 0], radius, height),
       rotcy([0, 90, 0], radius, height)
     );
    }
    
    function main () {
     return example001();
    }
    
    module.exports = { main }
    

Log in to reply