Math behind logical operators on 3D boxes / cuboids

Cheers.
Recently I’ve been coding a lot 3D isometric projections of complex structures rendered as cuboids (like this render of 2D k-d tree into 3D space: Imgur: The magic of the Internet). Standard Processing libraries do work but the render has its quirks: for example, as you might have noticed, the intersections are not outlined with color(0) edges – this is because cuboids are drawn on each in a, say, straightforward way, without any logic.

I’ve been wondering about implementing a simple – would it be actually simple? – library / procedure which would compute logical sums, intersections, results of subtraction and so on between two cubes or, if I allow myself to generalize, cuboids.

I first assumed it should be simple: in 2D this task is more or less trivial but when I sat down I started to tackle the math, I realized assumptions and simplifications will not work in 3D.

Would anyone, please, either guide me to a library that could be easily ported to Processing or some papers in which I could read on the subject and implement my own?

Most kind regards,
Łukasz

1 Like

what about trying ray marching? if you check this page it includes a short piece on csg it sounds like it might meet your needs. there is also a great page here just search the page for “Primitive combinations”.

1 Like

Cheers. I did some research. What I asked about in its generalized form is CSG – Constructive Solid Geometry and, as one may expect, is not a trifle. This requires some deeper research, even when constraining to operations on cubes only.

have you checked the following threads

@Lukasz – there is a post by @villares on making the JCSG Constructive Solid Geometry library work with Processing in both Java and Python modes.

You should also look at the discussion on his Stackoverflow post with the original question and an amazing detailed answer by @George. @Chrisir also made a demo – I’m not sure, but he may also use JCSG in some of his movies?

1 Like

That’s right

for example Some Movies I made - #14 by Chrisir

In one corner of the stage there is a light gray cube that’s hollowed out. CSG

Code for the moebius strip that also features that CUBE: 3DSketches/Moebius4 at master · Kango/3DSketches · GitHub

You need the 2 jars and then init



//https://stackoverflow.com/questions/56999816/is-it-possible-to-use-jcsg-library-with-processing
//https://discourse.processing.org/t/csg-constructive-solid-geometry/12693


// the PShape reference which will contain the converted 
PShape csgResult;

void initCSG() {

  // JCSG sample code:
  // we use cube and sphere as base geometries
  CSG cube = new Cube(2).toCSG();
  CSG sphere = new Sphere(1.25).toCSG();

  // perform difference 
  CSG cubeMinusSphere = cube.difference(sphere);  

  // Convert CSG to PShape -> Note: CSG units are small so we scale them up so the shapes are visible in Processing
  csgResult = CSGToPShape(cubeMinusSphere, 45);

  println("use mouse to  rotate");
}

//-----------------------------------------------------------------------------

// re-usable function to convert a CSG mesh to a Processing PShape
PShape CSGToPShape(CSG mesh, float scale) {
  // allocate a PShape group
  PShape csgResult = createShape(GROUP);
  // for each CSG polygon (Note: these can have 3,4 or more vertices)
  for (Polygon p : mesh.getPolygons()) {
    // make a child PShape
    PShape polyShape = createShape();
    // begin setting vertices to it
    polyShape.beginShape();
    // for each vertex in the polygon
    for (Vertex v : p.vertices) {
      // add each (scaled) polygon vertex 
      polyShape.vertex((float)v.pos.getX() * scale, (float)v.pos.getY() * scale, (float)v.pos.getZ() * scale);
    }
    // finish this polygon
    polyShape.endShape();
    //append the child PShape to the parent
    csgResult.addChild(polyShape);
  }
  return csgResult;
}
//

AND display


  void showCSG() {
    pushMatrix(); 
    csgResult.setStroke(false); 
    translate (410, GENERAL_Y_HEIGHT-47, 404);
    shape(csgResult);
    popMatrix();
  }

4 Likes