Boolean intersection.

 

Let's create a number of objects being an intersection of 3D primitives.

 

Two first objects will be built by intersection of two toruses.

 

As sgCore is a solid modeling library the resulting Boolean operations objects are in no way connected with each other. They are grouped to return from the function. For the further work with each object separately you should take this group to component parts.

 

Let's give its own color to each object obtained as a result of toruses intersection, and raise it to 1.5 for a better view.

 

The code of creating toruses and constructing objects from their intersection looks as follows:

  sgCTorus* tor1 = sgCreateTorus(2,1 ,24,24);

 sgCTorus* tor2 = sgCreateTorus(2,0.3 ,24,24);

 SG_VECTOR transV1 = {1,1,0};

 tor2->InitTempMatrix()->Translate(transV1);

 tor2->ApplyTempMatrix();

 tor2->DestroyTempMatrix();

 sgGetScene()->AttachObject(tor1);

 tor1->SetAttribute(SG_OA_COLOR,5);

 sgGetScene()->AttachObject(tor2);

 tor2->SetAttribute(SG_OA_COLOR,45);

 

 SG_VECTOR transV2 = {0,0,1.5};

 

 sgCGroup* bool1 = sgBoolean::Intersection(*tor1, *tor2);

 

int ChCnt = bool1->GetChildrenList()->GetCount();

 

 sgCObject**  allChilds = (sgCObject**)malloc(ChCnt*sizeof(sgCObject*));

if (!bool1->BreakGroup(allChilds))

 {

   assert(0);

 }

 sgCObject::DeleteObject(bool1);

for (int i=0;i<ChCnt;i++)

 {

   allChilds[i]->InitTempMatrix()->Translate(transV2);

   allChilds[i]->ApplyTempMatrix();

   allChilds[i]->DestroyTempMatrix();

   sgGetScene()->AttachObject(allChilds[i]);

   allChilds[i]->SetAttribute(SG_OA_COLOR,10+i);

 }

 

 free(allChilds);

 

Even if only one solid is constructed as a result of objects intersection the function returns a group of objects (with one child object) all the same. It is illustrated on an example of a sphere and a box intersection:

 sgCBox* bx1 = sgCreateBox(2,2 ,1);

 sgCSphere* sp1 = sgCreateSphere(1 ,24,24);

 SG_VECTOR transV4 = {3,3,0};

 bx1->InitTempMatrix()->Translate(transV4);

 bx1->ApplyTempMatrix();

 bx1->DestroyTempMatrix();

 sgGetScene()->AttachObject(bx1);

 bx1->SetAttribute(SG_OA_COLOR,55);

 SG_VECTOR transV5 = {3,4,0};

 sp1->InitTempMatrix()->Translate(transV5);

 sp1->ApplyTempMatrix();

 sp1->DestroyTempMatrix();

 sgGetScene()->AttachObject(sp1);

 sp1->SetAttribute(SG_OA_COLOR,75);

 sgCGroup* bool2 = sgBoolean::Intersection(*sp1, *bx1);

 

 ChCnt = bool2->GetChildrenList()->GetCount();

 

 allChilds = (sgCObject**)malloc(ChCnt*sizeof(sgCObject*));

if (!bool2->BreakGroup(allChilds))

 {

   assert(0);

 }

 sgCObject::DeleteObject(bool2);

for (int i=0;i<ChCnt;i++)

 {

   allChilds[i]->InitTempMatrix()->Translate(transV2);

   allChilds[i]->ApplyTempMatrix();

   allChilds[i]->DestroyTempMatrix();

   sgGetScene()->AttachObject(allChilds[i]);

   allChilds[i]->SetAttribute(SG_OA_COLOR,10+i);

 }

 

 free(allChilds);

 

See also:

sgBoolean   sgBoolean::Intersection

sgCGroup     sgCGroup::BreakGroup   sgCGroup::GetChildrenList

sgCObject::InitTempMatrix sgCMatrix::Translate   sgCObject::ApplyTempMatrix   SgCObject::DestroyTempMatrix   sgGetScene sgCScene::AttachObject   sgCObject::SetAttribute

sgCObject::DeleteObject

 

Illustration:

bool_inters