Visualizing Triangulations

Hi,

Is there any way to visualize a Poly_Triangulation directly? Alternatively, how does one _add_ a (self-made) Triangulation to a TopoDS_Shape?

[Background: I am trying to import STL files.]

Any help would be appreciated. Thanks, Chris Clason

Michael Gandyra's picture

What do you mean with "directly" ? There are several possibillities.

Regards, Michael

chris_clason's picture

Well, I have a Poly_Triangulation object, which I would like to display in an existing AIS Context. I know I can loop over the Array of Triangles it contains and create an Array of Vertices, which can be drawn. I was hoping there would be a way to draw the Poly_Triangulation object itself (or convert it to a TopoDS_Shape) so I could later (after I've figured out the whole selection concept :) ) go back and work it through some )user specified) routines.

Anyway, I'd be grateful for any of those possibilities you mentioned.

Thanks for your time, Chris Clason

Michael Gandyra's picture

OK. OK. Then its very easy, do it like this:

Create your own InteractiveObject, that inherits from AIS_InteractiveObject for example "MyPack_Triangulation". Redefine the Compute method and get the structure manager from the given PresentationManager. Define as a member a Graphic3d_Group (here: myGroup). Fill the group with your triangulation and stick it into the given Presentation. Assuming that you have the following members:

myTriangles : HArray1OfTriangle from Poly;

myGroup : Group from Graphic3d;

myNodes : HArray1OfPnt from TColgp;

myD1u : HArray1OfXYZ from TColgp;

myD1v : HArray1OfXYZ from TColgp;

Then it could look like:

void MyPack_Triangulation::Compute(const Handle_PrsMgr_PresentationManager3d& aPresentationManager,const Handle_Prs3d_Presentation& aPresentation,const Standard_Integer aMode)

{

Handle_Graphic3d_StructureManager aViewer = aPresentationManager->StructureManager();

// first remove all groups out of this presentation

Prs3d_Root::CurrentGroup(aPresentation)->Remove();

if (!myGroup.IsNull())

myGroup->Remove();

// set the aspect for the presentation of the triangulation depending on the mode

switch (aMode)

{

case 1: myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle(Aspect_IS_SOLID); break;

case 2: myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle(Aspect_IS_HOLLOW); break;

case 3: myDrawer->ShadingAspect()->Aspect()->SetInteriorStyle(Aspect_IS_EMPTY); break;

}

///// Normal shaded, triangulated or empty presentation

if (aMode<4)

{

myCurvat.Nullify(); // delete curvature values, if we changed to another mode

myInterp.Nullify(); // delete interpolator, if we changed to another mode

aPresentation->SetShadingAspect(myDrawer->ShadingAspect());

// clear the groups in the presentation ...

aPresentation->Clear(Standard_False);

myGroup = new Graphic3d_Group(aPresentation);

// dMode=3 is an empty presentation --> we need not to compute the visualization

if (dMode==3)

return;

}

else

return;

// Set the triangles first

Standard_Integer i, lower=myTriangles->Lower(), upper=myTriangles->Upper()+1;

Aspect_Array1OfEdge arrayEdgS (1, 3*myTriangles->Length());

Aspect_TypeOfEdge theEType = Aspect_TOE_INVISIBLE;

for (i=lower; iValue(i).Get(I,J,K);

arrayEdgS(3*i-2).SetValues(I,J,theEType);

arrayEdgS(3*i-1).SetValues(J,K,theEType);

arrayEdgS(3*i).SetValues(K,I,theEType);

}

// Set the Nodes and the normals if we have some and finally set the group

lower=myNodes->Lower(), upper=myNodes->Upper()+1;

// Check if we have normals. If not, we have to use Graphic3d_Array1OfVertex

// instead of an array of Graphic3d_Array1OfVertexN.

if (myD1u.IsNull() || myD1v.IsNull())

{

Graphic3d_Array1OfVertex arrayVertX (1, myNodes->Length());

for (i=lower; iValue(i).X(), myNodes->Value(i).Y(), myNodes->Value(i).Z());

myGroup->TriangleSet(arrayVertX,arrayEdgS);

}

else

{

Graphic3d_Array1OfVertexN arrayVertXN (1, myNodes->Length());

for (i=lower; iValue(i) ^ myD1v->Value(i);

arrayVertXN(i).SetCoord(myNodes->Value(i).X(), myNodes->Value(i).Y(), myNodes->Value(i).Z());

arrayVertXN(i).SetNormal(nrm.X(), nrm.Y(), nrm.Z());

}

myGroup->TriangleSet(arrayVertXN,arrayEdgS);

}

}

Best regards,

Michael

------------------------------------------------------------------

University of Kaiserslautern

Research Group for Computer Application in Engineering Design

http://rkk.mv.uni-kl.de

------------------------------------------------------------------

Michael Gandyra's picture

I forgot something:

Display it in the Context !

Handle_MyPack_Triangulation aTri = new ........

aisContext->Display(aTri, aMode, aSelectionMode);

If you like to have a selectable trinagulation then you have to redefine the ComputeSelection method too.

Bye !

Michael

PS: the lines with "myInterp" and "myCurvat" are not relevant.

chris_clason's picture

Thanks! That was a great help. On a slightly different angle, I am still a bit confused about the relationship between TopoDS_Shapes and Poly_Triangulations. If I remember correctly, there is a way to generate a Triangulation for a Shape, and a method to extract it. Can I access the Triangulation directly, maybe replacing it with my own (the FAQ hinted in that direction)

Many thanks, Chris Clason

Michael Gandyra's picture

You can get and set the triangulation of a Face with the following commands:

/// To get a triangulation :

/// Its a DownCast on the underlying TFace (look in BRep_Tool.cxx)

Handle_Poly_Triangulation aTri = (*((Handle(BRep_TFace)*)&yourFace.TShape()))->Triangulation();

/// To set a triangulation :

(*((Handle(BRep_TFace)*)&yourFace.TShape()))->Triangulation(yourTri);

Try it ! I hope it works for what you intend to do.

Best regards, Michael

Joe's picture

I am developing application to deform shape (Faces).Before the caculation for shape(Faces) I holp can dynamic visualizing (like rubber bending). First :I get triangulation from TopoDS_Face. Second:I make the deformation to the triangulation. then I check the underlay triangulation for the Face had modifyed correctly. Third :I want to redisplay the face with the modifyed triangulation. But how to do it? I try may method in AIS but not success. Is the only way is to create a new InteractiveObject class to direct display the triangulation?

Best Regards, Joe

Michael Gandyra's picture

What do you mean with "redisplay the face with the modified triangulation"? - If you just want to display the triangulation either in shaded or wireframe mode for visualization purposes, you can do it in the mentioned way, like I described it before (with an self derived Interactive Object)! To display the FACE (topology meaning) would presume to approximate a new base surface from your deformed triangulation (vertices/normals).

Best regards,

Michael

------------------------------------------------------------------

University of Kaiserslautern

Research Group for Computer Application in Engineering Design

------------------------------------------------------------------

Joe's picture

Hi! In my application, I want to display the triangulation. And it's a loop , base on mouse move event to deform the triangulation,display the new triangulation in shaded mode.

Is that I need to reconstruct the Graphic3d_Group in each mouse event or just modify Graphic3d_Arry1OfVertex? How can have good performance, I am deforming a lot of faces.

Best Regards. Joe

Jean-Claude Demosthenidy's picture

Hi, I have been following your discussion - but I could not see the answer to this message (bad page..)

Could you please explain/show to a beginner (me) how/where to capture mouse events to deform your faces?

I would be very grateful for some help with this. I have tried posting questions on both forums for about a month with no answers, and therefore no success...

Please let me know if you could share this formation.

Thank you very much

Jean-Claude

Joe's picture

Hi The answer in previous response message are "yes",and later I don't known why it became bad page.

My question is because I want to deform a lot of Face , and it need much cpu time. If I holp the deformation can dynamically visualize, the way that I can do is to get triangulation from each face with bigger deflection,and make deformation on the triangulation in each mouse move event (or use slider). After the function was validated by the user, do real deformation to each face, and create AIS object for result.

Best Regards Joe Lee

Jean-Claude Demosthenidy's picture

Yes, but how/where do you capture mouse input, and how/where do you manage the coordinate change and re-create the objects?

Thanks! Jean-Claude

Shawn Yang's picture

Hi: There are several samples that are provided by Open CASCADE. You can look inside about InputEvent function. When you click you left mouse. It will activate OnLButtonUp event. Try to set break point on InputEvent(). To see what happen. Wish it helps.

shawn

Shawn Yang's picture

P.S. If you want to catch moving mouse. You can use CXXXXView:OnMouseMove(..., CPoint point). The coordinates of X and Y are point.x and point.y. When you move the mouse, it will activate OnMouseMove event.

Jean-Claude Demosthenidy's picture

Great, Thanks! If you've been able to create a line (in 2D for example) interactively by placing a point and dragging the end point with the mouse (rubberbanding), please send me the code at jcd@ids-soft.com

I appreciate any help.

Jean-Claude