Hollow Out Example

We have a new version of ShapeJS deployed that makes it easy to write a hollow out routine. This allows you to take a solid object and turn it into a shell of specified thickness. Pull up the attached script in ShapeJS, add two params. One text param for the desired thickness(0.001 is 1mm), and a file param for the STL file you want to hollow out.

This script uses two new classes. DistanceTransformMultiStep which calculates the distance from the surface to a voxel. The other class is DensityGridExtractor which makes a density grid from the distance transform, ie it turns those distances into geometry. In combination you can use these two operations to hollow out a model. We calculate the distance of each voxel, then keep all the voxels which are further inside then the thickness we want. We then subtract that geometry from the original model to hollow it out.

I’ve set the default to .2mm for the voxelSize. If your using small objects you may want to set this to .1mm or smaller.

Load the following script into shapejs.shapeways.com, add the params to the interface:

function main(args){
  var thickness = parseFloat(args[0]);
  var baseFile = args[1];
  var voxelSize = 0.2 * MM;
  var maxAttribute = 255;

  var grid = load(baseFile,voxelSize);

  var bounds = java.lang.reflect.Array.newInstance(java.lang.Double.TYPE, 6);
  grid.getGridBounds(bounds);
  var xDist = Math.abs(bounds[1] - bounds[0]);
  var yDist = Math.abs(bounds[3] - bounds[2]);
  var zDist = Math.abs(bounds[5] - bounds[4]);

  var maxDepth = Math.min(Math.min(xDist,yDist),zDist) / 2;
  var maxInDistance = maxDepth + voxelSize;
  var maxOutDistance = voxelSize;

  var distanceGrid = new DistanceTransformMultiStep(maxAttribute, maxInDistance, maxOutDistance);
  var dg = distanceGrid.execute(grid);

  var dge = new DensityGridExtractor(maxDepth, -thickness,dg,maxInDistance,maxOutDistance, maxAttribute);
  var subsurface = createGrid(grid);

  subsurface = dge.execute(subsurface);

  dest = createGrid(grid);

  var maker = new GridMaker();
  maker.setMaxAttributeValue(maxAttribute);
  var dsg1 = new DataSourceGrid(grid,maxAttribute);
  var dsg2 = new DataSourceGrid(subsurface,maxAttribute);

  var result = new Subtraction(dsg1,dsg2);

  maker.setSource(result);
  maker.makeGrid(dest);
  return dest;
}
Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Hollow Out Example

  1. Christian Brunschen says:

    Hi,

    Distance transforms are very useful! I wonder whether any of the algorithms in these papers might be useful?

    Wim H. Hesselink, Menno Visser and Jos B.T.M. Roerdink, Eucleidean skeletons of 3D data sets in linear time by the Integer Medial Axis Transform [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.76.3936]

    which expands on the algorithm presented for the 2D case in

    A. Meijster, J. B. T. M. Roerdink and W. H. Hesselink, A general algorithm for computing distance transforms in linear time [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.25.1461]

    and a similar algorithm from

    Pedro F. Felzenszwalb and Daniel P. Huttenlocher, Distance transforms of sampled functions [http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.357.8656]

    Both of these (fundamentally very similar) algorithms can calculate the (squared euclidean) distance transform in linear time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s