Maintaining Image Aspect ratio

Discussion in 'ShapeJS' started by niquegeek, May 24, 2016.

  1. niquegeek
    niquegeek Active Member
    I'm just trying to create what I think is a pretty simple script to make a model from a 2D image, but I'm having a difficult time maintaining the image's aspect ratio with Image3D

    I have tried to set the Height (imageHeight) to 0.0 and it just flattens the 3D model. I must be missing something, can anyone see what it is? Thanks!


    var params = [
    {
    name: "image",
    label: "Pattern Image",
    desc: "Image",
    type: "uri",
    defaultVal: " http://www.shapeways.com/rrstatic/img/shapejs/creator/sw_log o.png",
    group: "Main"
    },
    {
    name: "useGrayscale",
    label: "Use Grayscale",
    desc: "Use Grayscale",
    type: "boolean",
    defaultVal: false,
    group: "Main"

    },
    {
    name: "imageSize",
    desc: "Image Size",
    type: "double",
    rangeMin: 20,
    rangeMax: 150,
    step: 10,
    defaultVal: 26,
    unit: "MM",
    group: "Main"
    },
    {
    name: "imageDepth",
    desc: "Image Depth",
    type: "double",
    rangeMin: 0.2,
    rangeMax: 2.0,
    step: 0.1,
    defaultVal: 0.75,
    unit: "MM",
    group: "Main"
    }
    ];



    function main(args){

    var voxelSize = 0.05*MM;

    var imageThickness = args.imageDepth;
    var imagePath = args.image;

    var imageWidth = args.imageSize;
    var imageHeight = imageWidth;

    var margin = 0.2*MM;// empty space around object

    var gridWidth = (imageWidth+margin);
    var gridHeight = (imageHeight+margin);

    var image = new Image3D(imagePath,imageWidth,imageHeight,imageThickness);
    image.setBaseThickness(0.0);
    image.setVoxelSize(1*voxelSize);
    image.setBlurWidth(1*voxelSize);

    var imageTransform = new CompositeTransform();
    imageTransform.add(new Rotation(new Vector3d(0,1,0), -Math.PI));
    //imageTransform.add(new Translation(0,2, 0));
    image.setTransform(imageTransform);


    var bounds = new Bounds(-gridWidth, gridWidth, -gridHeight, gridHeight, -gridWidth, gridWidth);
    var scene = new Scene(image,bounds,voxelSize*2);
    scene.setName("Image");

    return scene;
    }
     
  2. AlanHudson
    AlanHudson Shapeways Employee Dev Team
    We used to have some auto aspect ratio stuff in there but it looks like it didn't get moved over to 2.0. Here is a way to to handle the logic yourself using loadImage. This logic takes a physical size and accounts for the aspect ratio of the incoming image.


    var image = loadImage(path);

    var grid = image.getGrid();

    imageW = grid.getWidth();
    imageH = grid.getHeight();

    var bx = args.size * MM;
    var by = args.size * MM;
    var bz = args.depth * MM;

    if(imageH <= imageW){
    by = bx * imageH/imageW;
    } else {
    bx = by * imageW/imageH;
    }

    var img = new Image3D(grid,bx,by,bz,vs);
     
  3. niquegeek
    niquegeek Active Member
    Thanks Alan, that helps a lot! My temporary workaround was to just crop the images I'm using to a square, and leave a lot of white space that doesn't create geometry, this will remove the need for that!