1 // Created on: 2003-09-16
2 // Created by: Alexander SOLOVYOV
3 // Copyright (c) 2003-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <MeshVS_MeshPrsBuilder.ixx>
18 #include <Prs3d_Root.hxx>
19 #include <Prs3d_ShadingAspect.hxx>
20 #include <Prs3d_LineAspect.hxx>
21 #include <Prs3d_PointAspect.hxx>
23 #include <Graphic3d_AspectLine3d.hxx>
24 #include <Graphic3d_AspectFillArea3d.hxx>
25 #include <Graphic3d_AspectMarker3d.hxx>
26 #include <Graphic3d_ArrayOfPolygons.hxx>
27 #include <Graphic3d_ArrayOfTriangles.hxx>
28 #include <Graphic3d_ArrayOfSegments.hxx>
29 #include <Graphic3d_ArrayOfPoints.hxx>
30 #include <Graphic3d_ArrayOfPolylines.hxx>
31 #include <Graphic3d_Group.hxx>
33 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
34 #include <TColStd_ListIteratorOfListOfReal.hxx>
35 #include <TColStd_MapIntegerHasher.hxx>
36 #include <TColStd_Array1OfReal.hxx>
37 #include <TColStd_Array1OfInteger.hxx>
38 #include <TColStd_SequenceOfInteger.hxx>
39 #include <TColStd_PackedMapOfInteger.hxx>
40 #include <TColStd_HPackedMapOfInteger.hxx>
41 #include <TColStd_MapOfInteger.hxx>
43 #include <Quantity_NameOfColor.hxx>
44 #include <Aspect_InteriorStyle.hxx>
45 #include <Select3D_SensitivePoint.hxx>
47 #include <MeshVS_DataSource.hxx>
48 #include <MeshVS_Drawer.hxx>
49 #include <MeshVS_Mesh.hxx>
50 #include <MeshVS_Tool.hxx>
51 #include <MeshVS_DrawerAttribute.hxx>
52 #include <MeshVS_MapOfTwoNodes.hxx>
53 #include <MeshVS_Buffer.hxx>
54 #include <MeshVS_SymmetricPairHasher.hxx>
56 #include <NCollection_Map.hxx>
57 #include <NCollection_Vector.hxx>
59 //================================================================
60 // Function : Constructor MeshVS_MeshPrsBuilder
62 //================================================================
63 MeshVS_MeshPrsBuilder::MeshVS_MeshPrsBuilder ( const Handle(MeshVS_Mesh)& Parent,
64 const Standard_Integer& DisplayModeMask,
65 const Handle (MeshVS_DataSource)& DS,
66 const Standard_Integer Id,
67 const MeshVS_BuilderPriority& Priority )
68 : MeshVS_PrsBuilder ( Parent, DisplayModeMask, DS, Id, Priority )
72 //================================================================
75 //================================================================
76 void MeshVS_MeshPrsBuilder::Build ( const Handle(Prs3d_Presentation)& Prs,
77 const TColStd_PackedMapOfInteger& IDs,
78 TColStd_PackedMapOfInteger& IDsToExclude,
79 const Standard_Boolean IsElement,
80 const Standard_Integer DisplayMode ) const
82 if ( DisplayMode <= 0 )
85 Standard_Boolean HasHilightFlag = ( ( DisplayMode & MeshVS_DMF_HilightPrs ) != 0 );
86 Standard_Integer Extent = IDs.Extent();
88 if ( HasHilightFlag && Extent == 1)
89 BuildHilightPrs ( Prs, IDs, IsElement );
91 BuildElements ( Prs, IDs, IDsToExclude, DisplayMode );
93 BuildNodes ( Prs, IDs, IDsToExclude, DisplayMode );
96 //================================================================
97 // Function : BuildNodes
99 //================================================================
100 void MeshVS_MeshPrsBuilder::BuildNodes ( const Handle(Prs3d_Presentation)& Prs,
101 const TColStd_PackedMapOfInteger& IDs,
102 TColStd_PackedMapOfInteger& IDsToExclude,
103 const Standard_Integer DisplayMode ) const
105 Handle( MeshVS_DataSource ) aSource = GetDataSource();
106 Handle( MeshVS_Drawer ) aDrawer = GetDrawer();
107 Handle (Graphic3d_AspectMarker3d) aNodeMark =
108 MeshVS_Tool::CreateAspectMarker3d( GetDrawer() );
109 if ( aSource.IsNull() || aDrawer.IsNull() || aNodeMark.IsNull() )
112 Standard_Boolean DisplayFreeNodes = Standard_True;
113 aDrawer->GetBoolean( MeshVS_DA_DisplayNodes, DisplayFreeNodes );
114 Standard_Boolean HasSelectFlag = ( ( DisplayMode & MeshVS_DMF_SelectionPrs ) != 0 );
115 Standard_Boolean HasHilightFlag = ( ( DisplayMode & MeshVS_DMF_HilightPrs ) != 0 );
117 Standard_Real aCoordsBuf[ 3 ];
118 TColStd_Array1OfReal aCoords( *aCoordsBuf, 1, 3 );
119 Standard_Integer NbNodes;
120 MeshVS_EntityType aType;
122 if ( !DisplayFreeNodes )
125 TColStd_PackedMapOfInteger anIDs;
127 if ( !HasSelectFlag && !HasHilightFlag )
129 // subtract the hidden nodes and ids to exclude (to minimize allocated memory)
130 Handle(TColStd_HPackedMapOfInteger) aHiddenNodes = myParentMesh->GetHiddenNodes();
131 if ( !aHiddenNodes.IsNull() )
132 anIDs.Subtract( aHiddenNodes->Map() );
134 anIDs.Subtract( IDsToExclude );
136 Standard_Integer upper = anIDs.Extent();
140 Handle(Graphic3d_ArrayOfPoints) aNodePoints = new Graphic3d_ArrayOfPoints (upper);
141 Standard_Integer k=0;
142 TColStd_MapIteratorOfPackedMapOfInteger it (anIDs);
143 for( ; it.More(); it.Next() )
145 Standard_Integer aKey = it.Key();
146 if ( aSource->GetGeom ( aKey, Standard_False, aCoords, NbNodes, aType ) )
148 if ( IsExcludingOn() )
149 IDsToExclude.Add (aKey);
151 aNodePoints->AddVertex (aCoords(1), aCoords(2), aCoords(3));
157 Prs3d_Root::NewGroup ( Prs );
158 Handle (Graphic3d_Group) aNodeGroup = Prs3d_Root::CurrentGroup ( Prs );
159 aNodeGroup->SetPrimitivesAspect ( aNodeMark );
160 aNodeGroup->AddPrimitiveArray (aNodePoints);
164 //================================================================
165 // Function : BuildElements
167 //================================================================
168 void MeshVS_MeshPrsBuilder::BuildElements( const Handle(Prs3d_Presentation)& Prs,
169 const TColStd_PackedMapOfInteger& IDs,
170 TColStd_PackedMapOfInteger& IDsToExclude,
171 const Standard_Integer DisplayMode ) const
173 Standard_Integer maxnodes;
175 Handle(MeshVS_DataSource) aSource = GetDataSource();
176 if ( aSource.IsNull() )
179 Handle( MeshVS_Drawer ) aDrawer = GetDrawer();
180 if ( aDrawer.IsNull() || !aDrawer->GetInteger ( MeshVS_DA_MaxFaceNodes, maxnodes ) ||
184 //----------- extract useful display mode flags ----------
185 Standard_Integer aDispMode = ( DisplayMode & GetFlags() );
186 if ( ( aDispMode & MeshVS_DMF_DeformedMask) != 0 )
188 aDispMode /= MeshVS_DMF_DeformedPrsWireFrame;
189 // This transformation turns deformed mesh flags to real display modes
191 aDispMode &= MeshVS_DMF_OCCMask;
192 //--------------------------------------------------------
194 Standard_Real aShrinkCoef;
195 aDrawer->GetDouble ( MeshVS_DA_ShrinkCoeff, aShrinkCoef );
197 Standard_Boolean IsWireFrame = ( aDispMode==MeshVS_DMF_WireFrame ),
198 IsShading = ( aDispMode==MeshVS_DMF_Shading ),
199 IsShrink = ( aDispMode==MeshVS_DMF_Shrink ),
200 HasHilightFlag = ( ( DisplayMode & MeshVS_DMF_HilightPrs ) != 0 ),
201 HasSelectFlag = ( ( DisplayMode & MeshVS_DMF_SelectionPrs ) != 0 ),
202 IsMeshReflect, IsMeshAllowOverlap, IsReflect, IsMeshSmoothShading = Standard_False;
204 aDrawer->GetBoolean ( MeshVS_DA_Reflection, IsMeshReflect );
205 aDrawer->GetBoolean ( MeshVS_DA_IsAllowOverlapped, IsMeshAllowOverlap );
206 IsReflect = ( IsMeshReflect && !HasHilightFlag );
207 aDrawer->GetBoolean ( MeshVS_DA_SmoothShading, IsMeshSmoothShading );
209 // display mode for highlighted prs of groups
210 IsShrink = ( IsShrink && !HasHilightFlag );
211 IsShading = ( IsShading || HasHilightFlag );
213 //---------- Creating AspectFillArea3d and AspectLine3d -------------
214 Graphic3d_MaterialAspect AMat;
215 aDrawer->GetMaterial ( MeshVS_DA_FrontMaterial, AMat );
218 AMat.SetReflectionModeOff(Graphic3d_TOR_AMBIENT);
219 AMat.SetReflectionModeOff(Graphic3d_TOR_DIFFUSE);
220 AMat.SetReflectionModeOff(Graphic3d_TOR_SPECULAR);
221 AMat.SetReflectionModeOff(Graphic3d_TOR_EMISSION);
223 Handle( Graphic3d_AspectFillArea3d ) aFill = MeshVS_Tool::CreateAspectFillArea3d( GetDrawer(), AMat );
224 Handle( Graphic3d_AspectLine3d ) aBeam = MeshVS_Tool::CreateAspectLine3d ( GetDrawer() );
225 //-------------------------------------------------------------------
227 Standard_Boolean IsOverlapControl =
228 !IsMeshAllowOverlap && ( IsWireFrame || IsShading ) && !HasSelectFlag;
230 // subtract the hidden elements and ids to exclude (to minimize allocated memory)
231 TColStd_PackedMapOfInteger anIDs;
233 Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
234 if ( !aHiddenElems.IsNull() )
235 anIDs.Subtract( aHiddenElems->Map() );
236 anIDs.Subtract( IDsToExclude );
238 Handle( MeshVS_HArray1OfSequenceOfInteger ) aTopo;
239 TColStd_MapIteratorOfPackedMapOfInteger it (anIDs);
241 Standard_Boolean showEdges = Standard_True;
242 aDrawer->GetBoolean( MeshVS_DA_ShowEdges, showEdges );
244 showEdges = IsWireFrame || showEdges;
246 Standard_Integer* aNodesBuf = (Standard_Integer*) alloca (maxnodes * sizeof (Standard_Integer));
247 Standard_Real* aCoordsBuf = (Standard_Real*) alloca (3 * maxnodes * sizeof (Standard_Real));
249 TColStd_Array1OfInteger aNodes (*aNodesBuf, 1, maxnodes);
250 TColStd_Array1OfReal aCoords (*aCoordsBuf, 1, 3 * maxnodes);
252 Standard_Integer aNbFacePrimitives = 0;
253 Standard_Integer aNbVolmPrimitives = 0;
254 Standard_Integer aNbEdgePrimitives = 0;
255 Standard_Integer aNbLinkPrimitives = 0;
257 MeshVS_EntityType aType;
259 for (it.Reset(); it.More(); it.Next())
261 Standard_Integer aNbNodes = 0;
263 if (!aSource->GetGeom (it.Key(), Standard_True, aCoords, aNbNodes, aType))
266 if (aType == MeshVS_ET_Volume)
268 if (aSource->Get3DGeom (it.Key(), aNbNodes, aTopo))
270 for (Standard_Integer aFaceIdx = aTopo->Lower(); aFaceIdx <= aTopo->Upper(); ++aFaceIdx)
272 const TColStd_SequenceOfInteger& aFaceNodes = aTopo->Value (aFaceIdx);
274 if (showEdges) // add edge segments
276 aNbEdgePrimitives += aFaceNodes.Length();
279 if (IsShading || IsShrink) // add volumetric cell triangles
282 aNbVolmPrimitives += aFaceNodes.Length() - 2;
287 else if (aType == MeshVS_ET_Link)
291 aNbLinkPrimitives += 1; // add link segment
294 else if (aType == MeshVS_ET_Face)
298 aNbEdgePrimitives += aNbNodes; // add edge segments
301 if (!IsOverlapControl || IsShading)
303 if ((IsShading || IsShrink) && !HasSelectFlag)
305 aNbFacePrimitives += aNbNodes - 2; // add face triangles
311 // Here we do not use indices arrays because they are not effective for some mesh
312 // drawing modes: shrinking mode (displaces the vertices inside the polygon), 3D
313 // cell rendering (normal interpolation is not always applicable - flat shading),
314 // elemental coloring (color interpolation is impossible)
315 Handle (Graphic3d_ArrayOfTriangles) aVolmTriangles =
316 new Graphic3d_ArrayOfTriangles (aNbVolmPrimitives * 3, 0, IsReflect);
317 Handle (Graphic3d_ArrayOfTriangles) aFaceTriangles =
318 new Graphic3d_ArrayOfTriangles (aNbFacePrimitives * 3, 0, IsReflect);
320 Handle (Graphic3d_ArrayOfSegments) aLinkSegments;
321 Handle (Graphic3d_ArrayOfSegments) aEdgeSegments;
325 aLinkSegments = new Graphic3d_ArrayOfSegments (aNbLinkPrimitives * 2);
326 aEdgeSegments = new Graphic3d_ArrayOfSegments (aNbEdgePrimitives * 2);
329 TColStd_PackedMapOfInteger aCustomElements;
331 Quantity_Color anOldEdgeColor;
332 Quantity_Color anEdgeColor;
333 Quantity_Color anIntColor;
334 Aspect_InteriorStyle anIntType;
335 Aspect_TypeOfLine aLine;
336 Standard_Real aWidth;
338 aFill->Values (anIntType, anIntColor, anEdgeColor, aLine, aWidth);
340 MeshVS_MapOfTwoNodes aLinkNodes;
342 // Forbid drawings of edges which overlap with some links
343 if (showEdges && IsOverlapControl)
345 for (it.Reset(); it.More(); it.Next())
347 if (aSource->GetGeomType (it.Key(), Standard_True, aType) && aType == MeshVS_ET_Link)
349 Standard_Integer aNbNodes;
351 if (aSource->GetNodesByElement (it.Key(), aNodes, aNbNodes) && aNbNodes == 2)
353 aLinkNodes.Add (MeshVS_TwoNodes (aNodes(1), aNodes(2)));
359 NCollection_Map<MeshVS_NodePair, MeshVS_SymmetricPairHasher> aSegmentMap;
361 for (it.Reset(); it.More(); it.Next())
363 const Standard_Integer aKey = it.Key();
365 Standard_Integer NbNodes;
366 if (!aSource->GetGeom (aKey, Standard_True, aCoords, NbNodes, aType))
369 if (!aSource->GetNodesByElement (aKey, aNodes, NbNodes))
374 case MeshVS_ET_Volume:
377 IDsToExclude.Add (aKey);
379 if (aSource->Get3DGeom (aKey, NbNodes, aTopo))
381 // Add wire-frame presentation (draw edges for shading mode as well)
384 AddVolumePrs (aTopo, aCoords, NbNodes,
385 aEdgeSegments, IsReflect, IsShrink, HasSelectFlag, aShrinkCoef);
388 // Add shading presentation
389 if ((IsShading || IsShrink) && !HasSelectFlag)
391 AddVolumePrs (aTopo, aCoords, NbNodes,
392 aVolmTriangles, IsReflect, IsShrink, HasSelectFlag, aShrinkCoef);
401 IDsToExclude.Add (aKey);
405 AddLinkPrs (aCoords, aLinkSegments, IsShrink || HasSelectFlag, aShrinkCoef);
413 IDsToExclude.Add (aKey);
415 if (showEdges && IsOverlapControl)
417 Standard_Integer Last = 0;
419 MeshVS_TwoNodes aTwoNodes (aNodes (1));
421 for (Standard_Integer i = 1; i <= NbNodes; ++i)
424 aTwoNodes.First = aTwoNodes.Second;
426 aTwoNodes.Second = (i < NbNodes) ? aNodes (i+1) : aNodes (1);
428 if (aLinkNodes.Contains (aTwoNodes))
430 for (Standard_Integer aNodeIdx = Last + 1; aNodeIdx < i; ++aNodeIdx)
432 const Standard_Integer aNextIdx = aNodeIdx + 1;
434 aEdgeSegments->AddVertex (
435 aCoords (3 * aNodeIdx - 2), aCoords (3 * aNodeIdx - 1), aCoords (3 * aNodeIdx));
436 aEdgeSegments->AddVertex (
437 aCoords (3 * aNextIdx - 2), aCoords (3 * aNextIdx - 1), aCoords (3 * aNextIdx));
444 if (NbNodes - Last > 0)
446 for (Standard_Integer aNodeIdx = Last; aNodeIdx < NbNodes; ++aNodeIdx)
448 const Standard_Integer aNextIdx = (aNodeIdx + 1) % NbNodes;
450 const MeshVS_NodePair aSegment (aNodes (aNodeIdx + 1),
451 aNodes (aNextIdx + 1));
453 if (!aSegmentMap.Contains (aSegment))
455 aEdgeSegments->AddVertex (aCoords (3 * aNodeIdx + 1),
456 aCoords (3 * aNodeIdx + 2),
457 aCoords (3 * aNodeIdx + 3));
459 aEdgeSegments->AddVertex (aCoords (3 * aNextIdx + 1),
460 aCoords (3 * aNextIdx + 2),
461 aCoords (3 * aNextIdx + 3));
463 aSegmentMap.Add (aSegment);
469 if (!IsOverlapControl || IsShading)
471 if (!IsOverlapControl && showEdges)
473 AddFaceWirePrs (aCoords, NbNodes,
474 aEdgeSegments, IsShrink || HasSelectFlag, aShrinkCoef);
477 if ((IsShading || IsShrink) && !HasSelectFlag)
479 AddFaceSolidPrs (aKey, aCoords, NbNodes, maxnodes, aFaceTriangles, IsReflect,
480 IsShrink || HasSelectFlag, aShrinkCoef, IsMeshSmoothShading);
488 aCustomElements.Add (aKey);
495 anOldEdgeColor = anEdgeColor;
496 aFill->SetEdgeColor (Quantity_NOC_BLACK);
499 //std::cout << "Actual extents: " << std::endl
500 // << "Face tris: " << aFaceTriangles->ItemNumber() << " from " << aNbFacePrimitives << std::endl
501 // << "Volm tris: " << aVolmTriangles->ItemNumber() << " from " << aNbVolmPrimitives << std::endl
502 // << "Face segs: " << aEdgeSegments->ItemNumber() << " from " << aNbEdgePrimitives << std::endl
503 // << "Link segs: " << aLinkSegments->ItemNumber() << " from " << aNbLinkPrimitives << std::endl;
505 DrawArrays ( Prs, aFaceTriangles, aEdgeSegments, aLinkSegments, aVolmTriangles,
506 !showEdges, HasSelectFlag, aFill, aBeam );
508 if ( !aCustomElements.IsEmpty() )
509 CustomBuild ( Prs, aCustomElements, IDsToExclude, DisplayMode );
512 aFill->SetEdgeColor( anOldEdgeColor );
515 //================================================================
516 // Function : BuildHilightPrs
518 //================================================================
519 void MeshVS_MeshPrsBuilder::BuildHilightPrs ( const Handle(Prs3d_Presentation)& Prs,
520 const TColStd_PackedMapOfInteger& IDs,
521 const Standard_Boolean IsElement ) const
523 Standard_Integer maxnodes;
525 Handle (MeshVS_DataSource) aSource = GetDataSource();
526 if ( aSource.IsNull() || IDs.IsEmpty() )
529 Handle( MeshVS_Drawer ) aDrawer = GetDrawer();
530 if ( aDrawer.IsNull() || !aDrawer->GetInteger ( MeshVS_DA_MaxFaceNodes, maxnodes ) ||
534 MeshVS_Buffer aCoordsBuf (3*maxnodes*sizeof(Standard_Real));
535 TColStd_Array1OfReal aCoords (aCoordsBuf, 1, 3*maxnodes);
537 Graphic3d_MaterialAspect AMat;
538 aDrawer->GetMaterial ( MeshVS_DA_FrontMaterial, AMat );
539 AMat.SetReflectionModeOff(Graphic3d_TOR_AMBIENT);
540 AMat.SetReflectionModeOff(Graphic3d_TOR_DIFFUSE);
541 AMat.SetReflectionModeOff(Graphic3d_TOR_SPECULAR);
542 AMat.SetReflectionModeOff(Graphic3d_TOR_EMISSION);
544 Handle( Graphic3d_AspectFillArea3d ) aFill = MeshVS_Tool::CreateAspectFillArea3d( GetDrawer(), AMat );
545 Handle( Graphic3d_AspectLine3d ) aBeam = MeshVS_Tool::CreateAspectLine3d( GetDrawer() );
546 Handle( Graphic3d_AspectMarker3d ) aNodeMark = MeshVS_Tool::CreateAspectMarker3d( GetDrawer() );
548 // Hilight one element or node
549 TColStd_MapIteratorOfPackedMapOfInteger it (IDs);
550 Standard_Integer ID = it.Key(), NbNodes;
551 MeshVS_EntityType aType;
553 if ( !aSource->GetGeom ( ID, IsElement, aCoords, NbNodes, aType ) )
556 Prs3d_Root::NewGroup ( Prs );
557 Handle (Graphic3d_Group) aHilightGroup = Prs3d_Root::CurrentGroup ( Prs );
561 case MeshVS_ET_Node :
563 aHilightGroup->SetPrimitivesAspect (aNodeMark);
564 Handle(Graphic3d_ArrayOfPoints) anArrayOfPoints = new Graphic3d_ArrayOfPoints (1);
565 anArrayOfPoints->AddVertex (aCoords(1), aCoords(2), aCoords(3));
566 aHilightGroup->AddPrimitiveArray (anArrayOfPoints);
572 aHilightGroup->SetPrimitivesAspect ( aBeam );
573 Handle(Graphic3d_ArrayOfSegments) aPrims = new Graphic3d_ArrayOfSegments(2);
574 aPrims->AddVertex(aCoords(1),aCoords(2),aCoords(3));
575 aPrims->AddVertex(aCoords(4),aCoords(5),aCoords(6));
576 aHilightGroup->AddPrimitiveArray(aPrims);
583 aHilightGroup->SetPrimitivesAspect ( aFill );
584 Handle(Graphic3d_ArrayOfPolygons) aPrims = new Graphic3d_ArrayOfPolygons(NbNodes);
585 for ( Standard_Integer k=1; k<=NbNodes; k++)
586 aPrims->AddVertex(aCoords(3*k-2),aCoords(3*k-1),aCoords(3*k));
587 aHilightGroup->AddPrimitiveArray(aPrims);
591 case MeshVS_ET_Volume:
594 Handle( MeshVS_HArray1OfSequenceOfInteger ) aTopo;
596 aHilightGroup->SetPrimitivesAspect ( aFill );
598 if( aSource->Get3DGeom( ID, NbNodes, aTopo ) )
600 const Standard_Integer up = aTopo->Upper();
601 const Standard_Integer lo = aTopo->Lower();
602 Standard_Integer nbnodes = 0, i, j;
603 for( i=lo; i<=up; i++ )
604 nbnodes += aTopo->Value( i ).Length();
606 Handle(Graphic3d_ArrayOfPolygons) aPrims = new Graphic3d_ArrayOfPolygons(nbnodes,aTopo->Length());
607 for( i=lo; i<=up; i++ )
609 const TColStd_SequenceOfInteger& aSeq = aTopo->Value( i );
610 const Standard_Integer m = aSeq.Length();
612 for( j=1; j<=m; j++ )
614 const Standard_Integer ind = 3*aSeq.Value( j );
615 aPrims->AddVertex(aCoords(ind+1),aCoords(ind+2),aCoords(ind+3));
618 aHilightGroup->AddPrimitiveArray(aPrims);
625 TColStd_PackedMapOfInteger tmp;
626 CustomBuild ( Prs, IDs, tmp, MeshVS_DMF_HilightPrs );
632 //================================================================
633 // Function : AddLinkPrs
635 //================================================================
636 void MeshVS_MeshPrsBuilder::AddLinkPrs (const TColStd_Array1OfReal& theCoords,
637 const Handle(Graphic3d_ArrayOfSegments)& theSegments,
638 const Standard_Boolean theIsShrinked,
639 const Standard_Real theShrinkCoef) const
641 Standard_Real aX1 = theCoords (1);
642 Standard_Real aY1 = theCoords (2);
643 Standard_Real aZ1 = theCoords (3);
644 Standard_Real aX2 = theCoords (4);
645 Standard_Real aY2 = theCoords (5);
646 Standard_Real aZ2 = theCoords (6);
650 const Standard_Real xG = (aX1 + aX2) * 0.5;
651 const Standard_Real yG = (aY1 + aY2) * 0.5;
652 const Standard_Real zG = (aZ1 + aZ2) * 0.5;
654 aX1 = (aX1 - xG) * theShrinkCoef + xG;
655 aY1 = (aY1 - yG) * theShrinkCoef + yG;
656 aZ1 = (aZ1 - zG) * theShrinkCoef + zG;
658 aX2 = 2.0 * xG - aX1;
659 aY2 = 2.0 * yG - aY1;
660 aZ2 = 2.0 * zG - aZ1;
663 theSegments->AddVertex (aX1, aY1, aZ1);
664 theSegments->AddVertex (aX2, aY2, aZ2);
667 //================================================================
668 // Function : AddFaceWirePrs
670 //================================================================
671 void MeshVS_MeshPrsBuilder::AddFaceWirePrs (const TColStd_Array1OfReal& theCoords,
672 const Standard_Integer theNbNodes,
673 const Handle(Graphic3d_ArrayOfSegments)& theSegments,
674 const Standard_Boolean theIsShrinked,
675 const Standard_Real theShrinkingCoef) const
677 Standard_Real aCenterX = 0.0;
678 Standard_Real aCenterY = 0.0;
679 Standard_Real aCenterZ = 0.0;
683 CalculateCenter (theCoords, theNbNodes, aCenterX, aCenterY, aCenterZ);
686 NCollection_Vector<gp_XYZ> aNodes (theNbNodes);
688 for (Standard_Integer aNodeIdx = 0; aNodeIdx < theNbNodes; ++aNodeIdx)
690 gp_XYZ aPnt (theCoords (3 * aNodeIdx + 1),
691 theCoords (3 * aNodeIdx + 2),
692 theCoords (3 * aNodeIdx + 3));
696 aPnt.SetX ((aPnt.X() - aCenterX) * theShrinkingCoef + aCenterX);
697 aPnt.SetY ((aPnt.Y() - aCenterY) * theShrinkingCoef + aCenterY);
698 aPnt.SetZ ((aPnt.Z() - aCenterZ) * theShrinkingCoef + aCenterZ);
701 aNodes.Append (aPnt);
704 for (Standard_Integer aNodeIdx = 0; aNodeIdx < theNbNodes; ++aNodeIdx)
706 theSegments->AddVertex (aNodes.Value (aNodeIdx).X(),
707 aNodes.Value (aNodeIdx).Y(),
708 aNodes.Value (aNodeIdx).Z());
710 const Standard_Integer aNextIdx = (aNodeIdx + 1) % theNbNodes;
712 theSegments->AddVertex (aNodes.Value (aNextIdx).X(),
713 aNodes.Value (aNextIdx).Y(),
714 aNodes.Value (aNextIdx).Z());
718 //================================================================
719 // Function : AddFaceSolidPrs
721 //================================================================
722 void MeshVS_MeshPrsBuilder::AddFaceSolidPrs (const Standard_Integer theID,
723 const TColStd_Array1OfReal& theCoords,
724 const Standard_Integer theNbNodes,
725 const Standard_Integer theMaxNodes,
726 const Handle(Graphic3d_ArrayOfTriangles)& theTriangles,
727 const Standard_Boolean theIsShaded,
728 const Standard_Boolean theIsShrinked,
729 const Standard_Real theShrinkingCoef,
730 const Standard_Boolean theIsSmoothShading) const
732 Handle(MeshVS_DataSource) aDataSource = myParentMesh->GetDataSource();
734 if (aDataSource.IsNull())
737 Standard_Real aCenterX = 0.0;
738 Standard_Real aCenterY = 0.0;
739 Standard_Real aCenterZ = 0.0;
740 Standard_Real aNormalX = 0.0;
741 Standard_Real aNormalY = 0.0;
742 Standard_Real aNormalZ = 0.0;
746 CalculateCenter (theCoords, theNbNodes, aCenterX, aCenterY, aCenterZ);
749 NCollection_Vector<gp_XYZ> aVertexNormals (theMaxNodes);
753 if (theIsSmoothShading)
755 for (Standard_Integer aNodeIdx = 1; aNodeIdx <= theNbNodes; ++aNodeIdx)
757 if (!aDataSource->GetNodeNormal (aNodeIdx, theID, aNormalX, aNormalY, aNormalZ))
760 aVertexNormals.Append (gp_XYZ (aNormalX, aNormalY, aNormalZ));
764 if (!theIsSmoothShading || aVertexNormals.Size() != theNbNodes)
766 aDataSource->GetNormal (theID, theMaxNodes, aNormalX, aNormalY, aNormalZ);
770 NCollection_Vector<gp_XYZ> aNodes (theMaxNodes);
772 for (Standard_Integer aNodeIdx = 0; aNodeIdx < theNbNodes; ++aNodeIdx)
774 gp_XYZ aPnt (theCoords (3 * aNodeIdx + 1),
775 theCoords (3 * aNodeIdx + 2),
776 theCoords (3 * aNodeIdx + 3));
780 aPnt.SetX ((aPnt.X() - aCenterX) * theShrinkingCoef + aCenterX);
781 aPnt.SetY ((aPnt.Y() - aCenterY) * theShrinkingCoef + aCenterY);
782 aPnt.SetZ ((aPnt.Z() - aCenterZ) * theShrinkingCoef + aCenterZ);
785 aNodes.Append (aPnt);
788 // Triangulate polygon
789 for (Standard_Integer aNodeIdx = 0; aNodeIdx < theNbNodes - 2; ++aNodeIdx)
791 for (Standard_Integer aSubIdx = 0; aSubIdx < 3; ++aSubIdx)
795 if (theIsSmoothShading && aVertexNormals.Size() == theNbNodes)
797 aNormalX = aVertexNormals.Value (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)).X();
798 aNormalY = aVertexNormals.Value (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)).Y();
799 aNormalZ = aVertexNormals.Value (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)).Z();
802 theTriangles->AddVertex (aNodes.Value (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)).X(),
803 aNodes.Value (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)).Y(),
804 aNodes.Value (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)).Z(),
811 theTriangles->AddVertex (aNodes.Value (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)).X(),
812 aNodes.Value (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)).Y(),
813 aNodes.Value (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)).Z());
819 //================================================================
820 // Function : AddVolumePrs
822 //================================================================
823 void MeshVS_MeshPrsBuilder::AddVolumePrs (const Handle(MeshVS_HArray1OfSequenceOfInteger)& theTopo,
824 const TColStd_Array1OfReal& theNodes,
825 const Standard_Integer theNbNodes,
826 const Handle(Graphic3d_ArrayOfPrimitives)& theArray,
827 const Standard_Boolean theIsShaded,
828 const Standard_Boolean theIsShrinked,
829 const Standard_Boolean theIsSelected,
830 const Standard_Real theShrinkCoef)
832 Standard_Real aCenter[] = { 0.0, 0.0, 0.0 };
834 Standard_Integer aLow = theNodes.Lower();
836 if (theTopo.IsNull() || theArray.IsNull())
841 for (Standard_Integer aNodeIdx = 0; aNodeIdx < 3 * theNbNodes; ++aNodeIdx)
843 aCenter[aNodeIdx % 3] += theNodes.Value (aLow + aNodeIdx);
846 aCenter[0] /= theNbNodes;
847 aCenter[1] /= theNbNodes;
848 aCenter[2] /= theNbNodes;
851 Standard_Boolean aIsPolygons = theArray->IsKind (STANDARD_TYPE (Graphic3d_ArrayOfTriangles));
855 for (Standard_Integer aFaceIdx = theTopo->Lower(), topoup = theTopo->Upper(); aFaceIdx <= topoup; ++aFaceIdx)
857 const TColStd_SequenceOfInteger& aFaceNodes = theTopo->Value (aFaceIdx);
858 const Standard_Integer aNbPolyNodes = aFaceNodes.Length();
860 Standard_Real* aPolyNodesBuf = (Standard_Real*) alloca ((3 * aNbPolyNodes + 1) * sizeof (Standard_Real));
861 TColStd_Array1OfReal aPolyNodes (*aPolyNodesBuf, 0, 3 * aNbPolyNodes);
863 for (Standard_Integer aNodeIdx = 0; aNodeIdx < aNbPolyNodes; ++aNodeIdx)
865 Standard_Integer anIdx = aFaceNodes.Value (aNodeIdx + 1);
867 Standard_Real aX = theNodes.Value (aLow + 3 * anIdx + 0);
868 Standard_Real aY = theNodes.Value (aLow + 3 * anIdx + 1);
869 Standard_Real aZ = theNodes.Value (aLow + 3 * anIdx + 2);
873 aX = aCenter[0] + theShrinkCoef * (aX - aCenter[0]);
874 aY = aCenter[1] + theShrinkCoef * (aY - aCenter[1]);
875 aZ = aCenter[2] + theShrinkCoef * (aZ - aCenter[2]);
878 aPolyNodes.SetValue (3 * aNodeIdx + 1, aX);
879 aPolyNodes.SetValue (3 * aNodeIdx + 2, aY);
880 aPolyNodes.SetValue (3 * aNodeIdx + 3, aZ);
887 aPolyNodes.SetValue (0, aNbPolyNodes);
889 if (!MeshVS_Tool::GetAverageNormal (aPolyNodes, aNorm))
891 aNorm.SetCoord (0.0, 0.0, 1.0);
895 for (Standard_Integer aNodeIdx = 0; aNodeIdx < aNbPolyNodes - 2; ++aNodeIdx) // triangulate polygon
897 for (Standard_Integer aSubIdx = 0; aSubIdx < 3; ++aSubIdx) // generate sub-triangle
901 theArray->AddVertex (aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 1),
902 aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 2),
903 aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 3),
910 theArray->AddVertex (aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 1),
911 aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 2),
912 aPolyNodes.Value (3 * (aSubIdx == 0 ? 0 : (aNodeIdx + aSubIdx)) + 3));
918 else if (theIsSelected)
920 for (Standard_Integer aFaceIdx = theTopo->Lower(), topoup = theTopo->Upper(); aFaceIdx <= topoup; ++aFaceIdx)
922 const TColStd_SequenceOfInteger& aFaceNodes = theTopo->Value (aFaceIdx);
924 Standard_Real aFaceCenter[] = { 0.0, 0.0, 0.0 };
926 for (Standard_Integer aNodeIdx = 1; aNodeIdx <= aFaceNodes.Length(); ++aNodeIdx)
928 for (Standard_Integer aAxisIdx = 0; aAxisIdx < 3; ++aAxisIdx)
930 aFaceCenter[aAxisIdx] += theNodes.Value (aLow + 3 * aFaceNodes.Value (aNodeIdx) + aAxisIdx);
934 aFaceCenter[0] /= aFaceNodes.Length();
935 aFaceCenter[1] /= aFaceNodes.Length();
936 aFaceCenter[2] /= aFaceNodes.Length();
938 for (Standard_Integer aNodeIdx = 0, aNbNodes = aFaceNodes.Length(); aNodeIdx < aNbNodes; ++aNodeIdx)
940 for (Standard_Integer aSubIdx = 0; aSubIdx < 2; ++aSubIdx) // add segment
942 Standard_Integer anIdx = aFaceNodes.Value ((aNodeIdx + aSubIdx) % aNbNodes + 1);
944 Standard_Real aX = theNodes.Value (aLow + 3 * anIdx + 0);
945 Standard_Real aY = theNodes.Value (aLow + 3 * anIdx + 1);
946 Standard_Real aZ = theNodes.Value (aLow + 3 * anIdx + 2);
948 theArray->AddVertex (aFaceCenter[0] + theShrinkCoef * (aX - aFaceCenter[0]),
949 aFaceCenter[1] + theShrinkCoef * (aY - aFaceCenter[1]),
950 aFaceCenter[2] + theShrinkCoef * (aZ - aFaceCenter[2]));
957 // Find all pairs of nodes (edges) to draw (will be drawn only once)
958 NCollection_Map<MeshVS_NodePair, MeshVS_SymmetricPairHasher> aEdgeMap;
960 for (Standard_Integer aFaceIdx = theTopo->Lower(), topoup = theTopo->Upper(); aFaceIdx <= topoup; ++aFaceIdx)
962 const TColStd_SequenceOfInteger& aFaceNodes = theTopo->Value (aFaceIdx);
964 for (Standard_Integer aNodeIdx = 0, aNbNodes = aFaceNodes.Length(); aNodeIdx < aNbNodes; ++aNodeIdx)
966 const Standard_Integer aNextIdx = (aNodeIdx + 1) % aNbNodes;
968 aEdgeMap.Add (MeshVS_NodePair (aFaceNodes.Value (aNodeIdx + 1),
969 aFaceNodes.Value (aNextIdx + 1)));
974 for(NCollection_Map<MeshVS_NodePair, MeshVS_SymmetricPairHasher>::Iterator anIt (aEdgeMap); anIt.More(); anIt.Next())
976 const Standard_Integer anIdx1 = aLow + 3 * anIt.Key().first;
977 const Standard_Integer anIdx2 = aLow + 3 * anIt.Key().second;
979 Standard_Real aX[] = { theNodes.Value (anIdx1 + 0), theNodes.Value (anIdx2 + 0) };
980 Standard_Real aY[] = { theNodes.Value (anIdx1 + 1), theNodes.Value (anIdx2 + 1) };
981 Standard_Real aZ[] = { theNodes.Value (anIdx1 + 2), theNodes.Value (anIdx2 + 2) };
985 for (Standard_Integer anAxisIdx = 0; anAxisIdx < 2; ++anAxisIdx)
987 aX[anAxisIdx] = aCenter[0] + theShrinkCoef * (aX[anAxisIdx] - aCenter[0]);
988 aY[anAxisIdx] = aCenter[1] + theShrinkCoef * (aY[anAxisIdx] - aCenter[1]);
989 aZ[anAxisIdx] = aCenter[2] + theShrinkCoef * (aZ[anAxisIdx] - aCenter[2]);
993 theArray->AddVertex (aX[0], aY[0], aZ[0]);
994 theArray->AddVertex (aX[1], aY[1], aZ[1]);
999 //================================================================
1000 // Function : HowManyPrimitives
1002 //================================================================
1003 void MeshVS_MeshPrsBuilder::HowManyPrimitives (const Handle(MeshVS_HArray1OfSequenceOfInteger)& Topo,
1004 const Standard_Boolean AsPolygons,
1005 const Standard_Boolean IsSelect,
1006 const Standard_Integer NbNodes,
1007 Standard_Integer& Vertices,
1008 Standard_Integer& Bounds)
1010 if( !Topo.IsNull() ) {
1011 if( AsPolygons || IsSelect )
1013 Standard_Integer B = Topo->Upper()-Topo->Lower()+1;
1015 for( Standard_Integer i=Topo->Lower(), n=Topo->Upper(); i<=n; i++ )
1016 Vertices += Topo->Value( i ).Length();
1023 Standard_Integer F = Topo->Upper()-Topo->Lower()+1,
1024 E = NbNodes + F - 2;
1025 // number of edges is obtained by Euler's expression for polyhedrons
1033 //================================================================
1034 // Function : DrawArrays
1036 //================================================================
1037 void MeshVS_MeshPrsBuilder::DrawArrays( const Handle(Prs3d_Presentation)& Prs,
1038 const Handle(Graphic3d_ArrayOfPrimitives)& thePolygons,
1039 const Handle(Graphic3d_ArrayOfPrimitives)& theLines,
1040 const Handle(Graphic3d_ArrayOfPrimitives)& theLinkLines,
1041 const Handle(Graphic3d_ArrayOfPrimitives)& theVolumesInShad,
1042 const Standard_Boolean IsPolygonsEdgesOff,
1043 const Standard_Boolean IsSelected,
1044 const Handle(Graphic3d_AspectFillArea3d)& theFillAsp,
1045 const Handle(Graphic3d_AspectLine3d)& theLineAsp ) const
1047 if ( theFillAsp.IsNull() )
1050 Standard_Boolean IsFacePolygons = ( !thePolygons.IsNull() && thePolygons->ItemNumber() > 0 ),
1051 IsVolumePolygons = ( !theVolumesInShad.IsNull() && theVolumesInShad->ItemNumber() > 0 ),
1052 IsPolygons = IsFacePolygons || IsVolumePolygons,
1053 IsPolylines = ( !theLines.IsNull() && theLines->ItemNumber() > 0 ),
1054 IsLinkPolylines = ( !theLinkLines.IsNull() && theLinkLines->ItemNumber() > 0 );
1056 Aspect_InteriorStyle aStyle;
1057 Quantity_Color anIntColor, aBackColor, anEdgeColor;
1058 Aspect_TypeOfLine aType;
1059 Standard_Real aWidth;
1061 theFillAsp->Values( aStyle, anIntColor, aBackColor, anEdgeColor, aType, aWidth );
1063 if ( IsPolygons && theFillAsp->FrontMaterial().Transparency()<0.01 )
1065 Prs3d_Root::NewGroup ( Prs );
1066 Handle (Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup ( Prs );
1068 //if ( IsPolygonsEdgesOff )
1069 theFillAsp->SetEdgeOff ();
1071 // theFillAsp->SetEdgeOn ();
1073 if( anIntColor!=aBackColor )
1074 theFillAsp->SetDistinguishOn();
1076 theFillAsp->SetDistinguishOff();
1078 if( IsFacePolygons )
1080 aGroup->SetPrimitivesAspect ( theFillAsp );
1081 aGroup->AddPrimitiveArray ( thePolygons );
1084 if( IsVolumePolygons )
1086 Handle( Graphic3d_AspectFillArea3d ) aCullFillAsp =
1087 new Graphic3d_AspectFillArea3d( *( theFillAsp.operator->() ) );
1089 Standard_Boolean isSupressBackFaces = Standard_False;
1090 Handle( MeshVS_Drawer ) aDrawer = GetDrawer();
1091 if (!aDrawer.IsNull())
1092 aDrawer->GetBoolean ( MeshVS_DA_SupressBackFaces, isSupressBackFaces );
1094 if (isSupressBackFaces)
1095 aCullFillAsp->SuppressBackFace();
1097 aGroup->SetPrimitivesAspect ( aCullFillAsp );
1098 aGroup->AddPrimitiveArray ( theVolumesInShad );
1102 if ( IsPolylines && !IsPolygonsEdgesOff )
1104 Prs3d_Root::NewGroup ( Prs );
1105 Handle (Graphic3d_Group) aLGroup = Prs3d_Root::CurrentGroup ( Prs );
1107 theFillAsp->SetEdgeOff();
1109 aLGroup->SetPrimitivesAspect ( theLineAsp );
1112 aLGroup->SetPrimitivesAspect ( theFillAsp );
1113 aLGroup->SetPrimitivesAspect ( new Graphic3d_AspectLine3d
1114 ( anEdgeColor, Aspect_TOL_SOLID, aWidth ) );
1116 aLGroup->AddPrimitiveArray ( theLines );
1117 theFillAsp->SetEdgeOn();
1120 if ( IsLinkPolylines )
1122 Prs3d_Root::NewGroup ( Prs );
1123 Handle (Graphic3d_Group) aBeamGroup = Prs3d_Root::CurrentGroup ( Prs );
1125 theFillAsp->SetEdgeOff();
1127 aBeamGroup->SetPrimitivesAspect ( theFillAsp );
1128 aBeamGroup->SetPrimitivesAspect ( theLineAsp );
1129 aBeamGroup->AddPrimitiveArray ( theLinkLines );
1130 theFillAsp->SetEdgeOn();
1133 if ( IsPolygons && theFillAsp->FrontMaterial().Transparency()>=0.01 )
1135 Prs3d_Root::NewGroup ( Prs );
1136 Handle (Graphic3d_Group) aGroup = Prs3d_Root::CurrentGroup ( Prs );
1138 //if ( IsPolygonsEdgesOff )
1139 theFillAsp->SetEdgeOff ();
1141 // theFillAsp->SetEdgeOn ();
1143 if( anIntColor!=aBackColor )
1144 theFillAsp->SetDistinguishOn();
1146 theFillAsp->SetDistinguishOff();
1148 if( IsFacePolygons )
1150 aGroup->SetPrimitivesAspect ( theFillAsp );
1151 aGroup->AddPrimitiveArray ( thePolygons );
1154 if( IsVolumePolygons )
1156 Handle( Graphic3d_AspectFillArea3d ) aCullFillAsp =
1157 new Graphic3d_AspectFillArea3d( *( theFillAsp.operator->() ) );
1159 Standard_Boolean isSupressBackFaces = Standard_False;
1160 Handle( MeshVS_Drawer ) aDrawer = GetDrawer();
1161 if (!aDrawer.IsNull())
1162 aDrawer->GetBoolean ( MeshVS_DA_SupressBackFaces, isSupressBackFaces );
1164 if (isSupressBackFaces)
1165 aCullFillAsp->SuppressBackFace();
1167 aGroup->SetPrimitivesAspect ( aCullFillAsp );
1168 aGroup->AddPrimitiveArray ( theVolumesInShad );
1173 //================================================================
1174 // Function : CalculateCenter
1176 //================================================================
1177 void MeshVS_MeshPrsBuilder::CalculateCenter (const TColStd_Array1OfReal& theCoords,
1178 const Standard_Integer NbNodes,
1186 for ( Standard_Integer k=1; k<=NbNodes; k++)
1188 xG += theCoords(3*k-2);
1189 yG += theCoords(3*k-1);
1190 zG += theCoords(3*k);
1192 xG /= Standard_Real(NbNodes);
1193 yG /= Standard_Real(NbNodes);
1194 zG /= Standard_Real(NbNodes);
1198 Standard_Integer a = 1, b = 3;
1199 xG = ( theCoords( 3*a-2 ) + theCoords( 3*b-2 ) ) / 2.0;
1200 yG = ( theCoords( 3*a-1 ) + theCoords( 3*b-1 ) ) / 2.0;
1201 zG = ( theCoords( 3*a ) + theCoords( 3*b ) ) / 2.0;