micycle1 / PGS

Processing Geometry Suite
https://micycle1.github.io/PGS/
170 stars 13 forks source link

Shape loses its fill styling with successive subtracts #99

Open gverger opened 1 year ago

gverger commented 1 year ago

Hello,

Your library is (almost) a life savior for me, thanks!

I have an issue with a shape that I want to create. I want so subtract several rectangles from a circle, like so:

import micycle.pgs.*;

void setup() {
  background(255);
  size(800, 600);
  smooth();
}

void draw() {
  fill(#505050);
  noStroke();
  translate(400, 300);

  // create a donut
  var outer = createShape(ELLIPSE, 0, 0, 200, 200);
  var inner = createShape(ELLIPSE, 0, 0, 100, 100);
  outer = PGS_ShapeBoolean.subtract(outer, inner);

  // display on left side
  shape(outer, -250, 0);

  // make a first hole
  var hole1 = createShape(RECT, 0, -20, 200, 40);
  outer = PGS_ShapeBoolean.subtract(outer,hole1);

  // display in the center
  shape(outer);

  // make a second hole
  var hole2 = createShape(RECT, 0, -20, 200, 40);
  hole2.rotate(PI/3);
  outer = PGS_ShapeBoolean.subtract(outer,hole2);

  // display on right side
  shape(outer, 250, 0);
}

image

I would expect the result to be the same as the second shape + one more hole, but it loses its color. Is there a limitation in subtractions, or did I discover a bug?

Thank you!

micycle1 commented 1 year ago

Making the second cut changes the nature of the shape from a single shape to a GROUP shape (now having 2 child shapes).

During shape boolean operations, PGS creates a new, non-stylised shape. To preserve styling (color, fill, etc.) it then copies the style of the input shape to the new shape.

In your case the new shape is a GROUP shape and the apply styling operation is happening to the group shape, but that doesn't automatically apply styling to child shapes -- I suppose this is an oversight in the code.

For now you can use this to get the styling as intended:

// display on right side
PGS_Conversion.disableAllStroke(outer); // applies to all child shapes
PGS_Conversion.setAllFillColor(outer, #505050); // applies to all child shapes
shape(outer, 250, 0);

image

gverger commented 1 year ago

Thanks, that makes sense. It seems surprising when you are not familiar with the internals of it, especially when I see that PRESERVE_STYLE is true.

From my novice point of view I would have expected that these 2 lines you provided are somehow automatically applied.

Anyway, for my current usage, I'll manage with this workaround, feel free to close this issue if you consider you won't touch the current behavior.

Thanks again!

micycle1 commented 1 year ago

From my novice point of view I would have expected that these 2 lines you provided are somehow automatically applied.

Yes, I agree. It's oversight in the code and I'll fix it.