0

Mesh not Filling with Solid.make()

Hello!

I am new to Tinkercad and I'm trying to learn how to make shape generators, but I'm stuck on meshes.

I have a program that generates a mesh from any given function of two variables. When I call Solid.make() on one of these meshes, the result is filled with ugly blocks.

Here is the code:

[CODE]

// Convenience Declarations For Dependencies.
// 'Core' Is Configured In Libraries Section.
// Some of these may not be used by this example.
var Conversions = Core.Conversions;
var Debug = Core.Debug;
var Path2D = Core.Path2D;
var Point2D = Core.Point2D;
var Point3D = Core.Point3D;
var Matrix2D = Core.Matrix2D;
var Matrix3D = Core.Matrix3D;
var Mesh3D = Core.Mesh3D;
var Plugin = Core.Plugin;
var Tess = Core.Tess;
var Sketch2D = Core.Sketch2D;
var Solid = Core.Solid;
var Vector2D = Core.Vector2D;
var Vector3D = Core.Vector3D;

function abs(num) {
  if (num < 0) {
    return -1 * num;
  }
  if (num > 0) {
    return num;
  }
  return 0;
}

function pointMap(funct, domain, range, step, scale) {
  var points = [];
  var index = 0;
  var xInc = domain[0];
  var yInc;
  while (xInc <= domain[1]) {
    yInc = range[0];
    points.push([]);
    while (yInc >= range[1]) {
      points[index].push([xInc * scale, yInc * scale, funct(xInc * scale, yInc * scale)]);
      yInc -= step;
    }
    xInc += step;
    index += 1;
  }
  return points;
}

function meshFromMap(map) {
  var mesh = new Mesh3D();
  var zVals = [];
  for (var a = 0; a < map.length; a++) {
    for (var b = 0; b < map[a].length; b++) {
      zVals.push(map[a][b][2]);
    }
  }
  var zMin = Math.min.apply(null, zVals);
  var base1 = [map[0][0][0],map[0][0][1],zMin];
  var base2 = [map[0][map[0].length-1][0],map[0][map[0].length-1][1],zMin];
  var base3 = [map[map.length-1][0][0],map[map.length-1][0][1],zMin];
  var base4 = [map[map.length-1][map[0].length-1][0],map[map.length-1][map[0].length-1][1],zMin];
  for (var i = 0; i < map.length; i++) {
    for (var j = map[i].length - 1; j >= 0; j--) {
      // Connect points to points
      if (i - 1 >= 0 && j - 1 >= 0) {
        mesh.triangle(map[i - 1][j], map[i][j - 1], map[i][j]);
      }
      if (i + 1 < map.length && j - 1 >= 0) {
        mesh.triangle(map[i + 1][j], map[i][j - 1], map[i][j]);
      }
      if (i + 1 < map.length && j + 1 < map[i].length) {
        mesh.triangle(map[i + 1][j], map[i][j + 1], map[i][j]);
      }
      if (i - 1 >= 0 && j + 1 < map[i].length) {
        mesh.triangle(map[i - 1][j], map[i][j + 1], map[i][j]);
      }
      // Connect points to base
      if ((i === 0 || i === map.length - 1) && j - 1 >= 0) {
        mesh.triangle(map[i][j], [map[i][j - 1][0], map[i][j - 1][1], zMin], [map[i][j][0], map[i][j][1], zMin]);
      }
      if ((i === 0 || i === map.length - 1) && j + 1 < map[i].length) {
        mesh.triangle(map[i][j], [map[i][j + 1][0], map[i][j + 1][1], zMin], [map[i][j][0], map[i][j][1], zMin]);
      }
      if ((j === 0 || j === map.length - 1) && i - 1 >= 0) {
        mesh.triangle(map[i][j], [map[i - 1][j][0], map[i - 1][j][1], zMin], [map[i][j][0], map[i][j][1], zMin]);
      }
      if ((j === 0 || j === map.length - 1) && i + 1 < map.length) {
        mesh.triangle(map[i][j], [map[i + 1][j][0], map[i + 1][j][1], zMin], [map[i][j][0], map[i][j][1], zMin]);
      }
      
    }
  }

  // Create base
  mesh.triangle(base4, base1, base2);
  mesh.triangle(base1, base2, base3);
  mesh.triangle(base2, base3, base4);
  mesh.triangle(base3, base4, base1);
  return mesh;
}

function process(params) {
 
  function paraboloid(x,y) {
    return -0.5*(x*x + y*y);
  }
 
  var p = meshFromMap(pointMap(paraboloid,[-10,10], [10,-10], 0.5, 1));
  return Solid.make(p);
}

[\CODE]

pointMap takes in a function, domain, range, step, scale, and returns an array of arrays of points in the form [x, y, z]

meshFromMap takes in an array of arrays of points in the form [x, y, z] and returns a mesh connecting each of the points

 

I have no idea how to fix this, so any help is greatly appreciated!

0 comments

Please sign in to leave a comment.