How to create slices of a HE_Mesh mesh in Processing

This sketch illustrates the "most" efficient way of generating planar slices of a static mesh.

  1. Generate the HE_Mesh mesh
  2. Create an efficient intersection structure from the mesh: a WB_AABBTree  tree (axis-aligned box tree)
  3. Specify a slicing plane P of type WB_Plane
  4. Use HE_Intersection.getIntersection(tree,P) to get all intersections as a list of line segments of type List<WB_ExplicitSegment>
  5. Reorient the 3D segments back to the XY plane (by expressing them in the local coordinate system of the slicing plane P)
  6. Do whatever you want with the segments

In the following example the segments are cast into a float[] array: 3 coordinates of start point of first segment, 3 coordinates of end point of first segment, 3 coordinates of start point of second segment, etc...

Have fun!

import wblut.core.processing.*;
import wblut.hemesh.subdividors.*;
import wblut.hemesh.creators.*;
import wblut.geom.tree.*;
import wblut.hemesh.core.*;
import wblut.geom.core.*;

float[][] data;//Fake data
HE_Mesh mesh;
WB_Render render;
WB_AABBTree tree;
float[] segments;

void setup() {
  size(800, 800, P3D);
  createData();
  HEC_DataCylinder dc=new HEC_DataCylinder().setRadius(20, 50).setHeight(780);
  dc.setDataFromFloat(data).setChamfer(0.8);
  mesh=new HE_Mesh(dc);
  tree=new WB_AABBTree(mesh, 4);
  render=new WB_Render(this);
}

void draw() {
  background(255);
  translate(height/2, height/2, 0);
 
 
  //Dynamic slice plane
  WB_Plane P=new WB_Plane(0, 0, mouseX-width/2, 0, 0, 1);
  stroke(0, 0, 255);
  strokeWeight(3);
  
  //Efficient slicing 
  List<WB_ExplicitSegment> cuts=HE_Intersection.getIntersection(tree, P);
  segments=new float[cuts.size()*6];
  for (int i=0;i<cuts.size();i++) {
    WB_Segment seg=cuts.get(i);
    WB_Point3d start=seg.getOrigin();
    WB_Point3d end=seg.getEnd();

    //Projecting all points in XY Plane
    start=P.localPoint(start);
    end=P.localPoint(end);   
    start.z=0;//Z should be 0 but just to be clean
    end.z=0;  
    seg=new WB_ExplicitSegment(start, end);
    segments[i*6]=start.xf();
    segments[i*6+1]=start.yf();
    segments[i*6+2]=start.zf();
    segments[i*6+3]=end.xf();
    segments[i*6+4]=end.yf();
    segments[i*6+5]=end.zf();
    
      render.drawSegment(seg);
  }

  directionalLight(255, 255, 255, 1, 1, -1);
  directionalLight(127, 127, 127, -1, -1, 1);
  translate(0, 0, -200);
  rotateX(mouseY*TWO_PI/height-PI);
  rotateY(HALF_PI);
  fill(255);
  noStroke();
  render.drawFaces(mesh);
  noFill();
  strokeWeight(0.5);
  stroke(0);
  render.drawEdges(mesh);
  fill(255, 0, 0, 50);
  noStroke();
  render.draw(P, 500);
}


void createData() {
  data=new float[20][20];
  for (int i=0; i < 20; i++) {
    for (int j=0; j < 20; j++) {
      data[i][j]=200*noise(0.2*i, 0.2*j);
    }
  }
}

Info

Difficulty: ●●●○○

Contributors:

Last updated: 7 years ago

Admin