# Math behind logical operators on 3D boxes / cuboids

**URL:** <https://discourse.processing.org/t/math-behind-logical-operators-on-3d-boxes-cuboids/32318>\
**Category:** Project Guidance\
**Created:** [September 19, 2021, 5:15pm UTC](https://discourse.processing.org/t/math-behind-logical-operators-on-3d-boxes-cuboids/32318 "2021-09-19T17:15:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lukasz](https://avatars.discourse-cdn.com/v4/letter/l/0ea827/32.png) [@Lukasz](https://discourse.processing.org/u/Lukasz)\
**Post date:** [September 19, 2021, 5:15pm UTC](https://discourse.processing.org/t/math-behind-logical-operators-on-3d-boxes-cuboids/32318/1 "2021-09-19T17:15:51Z")

</div>

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](https://imgur.com/a/AYAeuDv)). 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

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [September 19, 2021, 11:16pm UTC](https://discourse.processing.org/t/math-behind-logical-operators-on-3d-boxes-cuboids/32318/2 "2021-09-19T23:16:44Z")

</div>

what about trying ray marching? if you check [this page](http://jamie-wong.com/2016/07/15/ray-marching-signed-distance-functions/) it includes a short piece on csg it sounds like it might meet your needs. there is also a great page [here](https://iquilezles.org/www/articles/distfunctions/distfunctions.htm) just search the page for “Primitive combinations”.

---

<div class="post-metadata">

**Author:** ![Lukasz](https://avatars.discourse-cdn.com/v4/letter/l/0ea827/32.png) [@Lukasz](https://discourse.processing.org/u/Lukasz)\
**Post date:** [September 20, 2021, 5:51pm UTC](https://discourse.processing.org/t/math-behind-logical-operators-on-3d-boxes-cuboids/32318/3 "2021-09-20T17:51:27Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [September 20, 2021, 10:55pm UTC](https://discourse.processing.org/t/math-behind-logical-operators-on-3d-boxes-cuboids/32318/4 "2021-09-20T22:55:46Z")

</div>

have you checked the following threads

> [@3d collision environment (Player versus three point Plane)](https://discourse.processing.org/t/3d-collision-environment-player-versus-three-point-plane/6937):
>
> Hey forum, first time poster. Here I have made a environment where a player that is shaped like a cylinder can interact with planes defined by 3 points with a few extra player behaviors. All instructions and sources and in the comments. The code could use more tidying up but I cleaned and commented as best I could so without further adieu here’s the stuff : /\*use WASD to move space to jump pivot camera with mouse KEY COMMANDS: 1 : raise a lot of land 2 : raise some land 3 : raise a little…

> [@3D collision environment for Android](https://discourse.processing.org/t/3d-collision-environment-for-android/7527):
>
> Hey yall Not too long ago I made a sketch showcasing a player that can collide with 3-point planes in 3D space. I recently adapted the code for Android and it seems to work pretty nicely so I thought’d I’d post it The code isn’t perfect and its actually runs kinda sluggish to be honest. This is my first time making something for android, not to mention my first time posting something on github (so I’m really hoping I did it right sweat) …

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [September 22, 2021, 12:56am UTC](https://discourse.processing.org/t/math-behind-logical-operators-on-3d-boxes-cuboids/32318/5 "2021-09-22T00:56:23Z")

</div>

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

> [@CSG (Constructive Solid Geometry)](https://discourse.processing.org/t/csg-constructive-solid-geometry/12693):
>
> For years I have looked for CSG in Processing… then I found a nice Java CSG candidate library and George Profenza in minutes made it work with Processing: I’m very happy smiley And it even works with my beloved Processing Python Mode: [https://github.com/villares/py.processing-play/tree/master/3D/demo\_JCSG\_py](https://github.com/villares/py.processing-play/tree/master/3D/demo_JCSG_py) Now the next step, I guess, would be someone skilled in the ancient Processing Library template & repository maintenance arts to wrap it and make it…

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?

> <https://stackoverflow.com/questions/56999816/is-it-possible-to-use-jcsg-library-with-processing>

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [September 22, 2021, 10:41am UTC](https://discourse.processing.org/t/math-behind-logical-operators-on-3d-boxes-cuboids/32318/6 "2021-09-22T10:41:05Z")

</div>

> [@jeremydouglass](#):
>
> @Chrisir also made a demo – I’m not sure, but he may also use JCSG in some of his movies?

That’s right

for example [Some Movies I made - #14 by Chrisir](https://discourse.processing.org/t/some-movies-i-made/13306/14)

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: [https://github.com/Kango/3DSketches/tree/master/Moebius4](https://github.com/Kango/3DSketches/tree/master/Moebius4)

You need the 2 jars and then init

```auto

//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

```auto

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

```
