# Boolean difference and extrusion example

jokoon

22-01-2013 10:37:20

Is there an example of 2D boolean difference and then extrusion somewhere in the tests.h in the src archive ?

I don't really understand why it crashes for me.

I'm just trying to make a carved, thick-walled open box, just like a house without a roof, from a difference of 2 rectangles.

mikachu

22-01-2013 12:16:48

In Tests.h, line 546 :
```MeshPtr mp; Shape s2 = RectangleShape().setHeight(.5).realizeShape().switchSide(); Shape s3 = s2; s3.scale(1.5).switchSide(); MultiShape ms = MultiShape(2, &s2, &s3); Path p3 = CatmullRomSpline3().addPoint(0,5,-5).addPoint(0,0,0).addPoint(0,0,5).realizePath(); mp = Extruder().setMultiShapeToExtrude(&ms).setExtrusionPath(&p3).realizeMesh(); ```
It doesn't use boolean difference, but the result should be the same...

Could you show me what code makes it crash?

jokoon

22-01-2013 13:00:34

``` void house(float th, float w, float h, float d) { using namespace Procedural; //Procedural::Shape sh; //sh.addPoint(0,0).addPoint(w,0).addPoint(w,d).addPoint(0,d).realizeMesh(; //Procedural::Shape sh_in; //sh_in.addPoint(0+th,0+th).addPoint(w-th,0+th).addPoint(w-th,d-th).addPoint(0+th,d-th); //Vec2 points[4] = {Vec2(0,0),Vec2(0,w),Vec2(d,w),Vec2(d,0)}; Procedural::RectangleShape rect_out, rect_in; rect_out.setHeight(h).setWidth(w); rect_in.setHeight(h-th*2).setWidth(w-th*2); Shape s1 = rect_in.realizeShape(); Shape s2 = rect_out.realizeShape(); s2.translate(th,th); Procedural::MultiShape mult = s2.booleanDifference(s1); //mult.close(); if(mult.isClosed()) LOGMSG("YEAAAH"); else LOGMSG("Oooooh"); Procedural::Path p = Procedural::LinePath().betweenPoints(Vector3(0,0,0), Vector3(0,0,h)).realizePath(); Procedural::Extruder().setExtrusionPath(&p).setMultiShapeToExtrude(&mult).realizeMesh("house"); ```

When I uncomment mult.close() it hangs.

call stack:

``` OgreProcedural_d.dll!std::vector<Ogre::Vector2,std::allocator<Ogre::Vector2> >::operator[](unsigned int _Pos=0) Line 916 + 0x17 bytes C++ OgreProcedural_d.dll!Procedural::Shape::getTotalLength() Line 452 + 0xc bytes C++ > OgreProcedural_d.dll!Procedural::Extruder::_extrudeBodyImpl(Procedural::TriangleBuffer & buffer={...}, const Procedural::Shape * shapeToExtrude=0x01673308) Line 46 + 0x8 bytes C++ OgreProcedural_d.dll!Procedural::Extruder::addToTriangleBuffer(Procedural::TriangleBuffer & buffer={...}) Line 240 + 0x1c bytes C++ ogredural.exe!Procedural::MeshGenerator<Procedural::Extruder>::realizeMesh(const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & name="house", const std::basic_string<char,std::char_traits<char>,std::allocator<char> > & group="General") Line 116 + 0x12 bytes C++ ogredural.exe!bilder::house(float th=2.0000000, float w=100.00000, float h=100.00000, float d=100.00000) Line 70 + 0x70 bytes C++ ```

I'm learning to use this awesome lib, I guess I made a mistake at some point. Why didn't you make a boolean difference ?

mikachu

22-01-2013 13:31:26

RectangleShapes are centered on the origin (0,0), which means that in your setup, since you're translating s2 by (th,th), some segments of s2 and s1 are overlapping.
Is it what you're trying to achieve?
If yes, then some OgreProcedural components (namely triangulator, extruder with caps, boolean operations) are not very comfortable with overlapping segments..

Why didn't you make a boolean difference ?
In the example I showed you, it was easier creating multishape by hand (boolean difference is only needed when some segments are intersecting), even though boolean difference would had been fine.

jokoon

23-01-2013 20:31:53

Thanks I thought the top left corner was 0,0

So if I understand, here switchSide allowed you to define the inner and outer shape.

mikachu

23-01-2013 20:45:49

o if I understand, here switchSide allowed you to define the inner and outer shape.
Shapes have a knowledge of what is outside and what is inside, a bit like normals with meshes.
It's just a flag to say whether the outside is on the right or left side of the shape.
switchSide() will just flip that flag's state.