0022922: Clean up warnings on uninitialized / unused variables
[occt.git] / src / MeshVS / MeshVS_TextPrsBuilder.cxx
1 // File:  MeshVS_TextPrsBuilder.cxx
2 // Created: Mon Sep 22 2003
3 // Author:  Alexander SOLOVYOV
4 // Copyright:  Open CASCADE 2003
5
6 #include <MeshVS_TextPrsBuilder.ixx>
7
8 #include <MeshVS_DisplayModeFlags.hxx>
9
10 #include <Graphic3d_Group.hxx>
11 #include <Prs3d_Root.hxx>
12 #include <Prs3d_TextAspect.hxx>
13 #include <Graphic3d_AspectText3d.hxx>
14 #include <TColStd_ListIteratorOfListOfReal.hxx>
15 #include <Graphic3d_Vertex.hxx>
16 #include <Graphic3d_AspectMarker3d.hxx>
17 #include <TColStd_Array1OfReal.hxx>
18 #include <TCollection_AsciiString.hxx>
19 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
20 #include <TColStd_HPackedMapOfInteger.hxx>
21
22 #include <MeshVS_DataSource.hxx>
23 #include <MeshVS_Drawer.hxx>
24 #include <MeshVS_Mesh.hxx>
25 #include <MeshVS_DrawerAttribute.hxx>
26 #include <MeshVS_Buffer.hxx>
27
28 #include <Graphic3d_NameOfFont.hxx>
29
30 //================================================================
31 // Function : Constructor MeshVS_TextPrsBuilder
32 // Purpose  :
33 //================================================================
34 MeshVS_TextPrsBuilder::MeshVS_TextPrsBuilder (  const Handle(MeshVS_Mesh)& Parent,
35                                                 const Standard_Real Height,
36                                                 const Quantity_Color& Color,
37                                                 const MeshVS_DisplayModeFlags& Flags,
38                                                 const Handle (MeshVS_DataSource)& DS,
39                                                 const Standard_Integer Id,
40                                                 const MeshVS_BuilderPriority& Priority )
41                                               : MeshVS_PrsBuilder ( Parent, Flags, DS, Id, Priority )
42 {
43   Handle ( MeshVS_Drawer ) aDrawer = GetDrawer();
44   if ( !aDrawer.IsNull() )
45   {
46     aDrawer->SetDouble ( MeshVS_DA_TextHeight, Height );
47     aDrawer->SetColor  ( MeshVS_DA_TextColor, Color );
48   }
49 }
50
51 //================================================================
52 // Function : GetTexts
53 // Purpose  :
54 //================================================================
55 const MeshVS_DataMapOfIntegerAsciiString& MeshVS_TextPrsBuilder::GetTexts ( const Standard_Boolean IsElements ) const
56 {
57   if ( IsElements )
58     return myElemTextMap;
59   else
60     return myNodeTextMap;
61 }
62
63 //================================================================
64 // Function : SetTexts
65 // Purpose  :
66 //================================================================
67 void MeshVS_TextPrsBuilder::SetTexts ( const Standard_Boolean IsElements,
68                                        const MeshVS_DataMapOfIntegerAsciiString& Map )
69 {
70   if ( IsElements )
71     myElemTextMap = Map;
72   else
73     myNodeTextMap = Map;
74 }
75
76 //================================================================
77 // Function : HasTexts
78 // Purpose  :
79 //================================================================
80 Standard_Boolean MeshVS_TextPrsBuilder::HasTexts ( const Standard_Boolean IsElement ) const
81 {
82   Standard_Boolean aRes = (myNodeTextMap.Extent()>0);
83   if ( IsElement )
84     aRes = (myElemTextMap.Extent()>0);
85   return aRes;
86 }
87
88 //================================================================
89 // Function : GetText
90 // Purpose  :
91 //================================================================
92 Standard_Boolean MeshVS_TextPrsBuilder::GetText ( const Standard_Boolean IsElement,
93                                                   const Standard_Integer theID,
94                                                   TCollection_AsciiString& theStr ) const
95 {
96   const MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
97   if ( IsElement )
98     aMap = &myElemTextMap;
99
100   Standard_Boolean aRes = aMap->IsBound ( theID );
101   if ( aRes )
102     theStr = aMap->Find ( theID );
103
104   return aRes;
105 }
106
107 //================================================================
108 // Function : SetText
109 // Purpose  :
110 //================================================================
111 void MeshVS_TextPrsBuilder::SetText ( const Standard_Boolean IsElement,
112                                       const Standard_Integer ID,
113                                       const TCollection_AsciiString& Text )
114 {
115   MeshVS_DataMapOfIntegerAsciiString* aMap = &myNodeTextMap;
116   if ( IsElement )
117     aMap = &myElemTextMap;
118
119   Standard_Boolean aRes = aMap->IsBound ( ID );
120   if ( aRes )
121     aMap->ChangeFind ( ID ) = Text;
122   else
123     aMap->Bind ( ID, Text );
124 }
125
126 //================================================================
127 // Function : Build
128 // Purpose  :
129 //================================================================
130 void MeshVS_TextPrsBuilder::Build ( const Handle(Prs3d_Presentation)& Prs,
131                                     const TColStd_PackedMapOfInteger& IDs,
132                                     TColStd_PackedMapOfInteger& IDsToExclude,
133                                     const Standard_Boolean IsElement,
134                                     const Standard_Integer theDisplayMode ) const
135 {
136   Handle (MeshVS_DataSource) aSource = GetDataSource();
137   Handle (MeshVS_Drawer) aDrawer = GetDrawer();
138   if ( aSource.IsNull() || aDrawer.IsNull() || !HasTexts( IsElement ) ||
139     ( theDisplayMode & GetFlags() ) == 0 )
140     return;
141
142   Standard_Integer aMaxFaceNodes;
143   Standard_Real aHeight;
144   if ( !aDrawer->GetInteger ( MeshVS_DA_MaxFaceNodes, aMaxFaceNodes ) ||
145     aMaxFaceNodes <= 0 ||
146     !aDrawer->GetDouble  ( MeshVS_DA_TextHeight, aHeight )    )
147     return;
148
149   Prs3d_Root::NewGroup ( Prs );
150   Handle (Graphic3d_Group) aTextGroup = Prs3d_Root::CurrentGroup ( Prs );
151
152   Quantity_Color           AColor           = Quantity_NOC_YELLOW;
153 #ifdef WNT  
154   Standard_CString         AFont            = "Courier New";
155 #else
156   Standard_CString         AFont            = "Courier";
157 #endif
158   Standard_Real            AExpansionFactor = 1.0;
159   Standard_Real            ASpace           = 0.0;
160   Aspect_TypeOfStyleText   ATextStyle       = Aspect_TOST_ANNOTATION;
161   Standard_Integer         AStyleInt;
162   Aspect_TypeOfDisplayText ADisplayType     = Aspect_TODT_NORMAL;
163   TCollection_AsciiString  AFontString;
164   Standard_Integer         ADispInt;
165   // Bold font is used by default for better text readability
166   OSD_FontAspect           AFontAspectType  = OSD_FA_Bold;
167   Standard_Integer         AAspect; 
168   
169
170   aDrawer->GetColor  ( MeshVS_DA_TextColor, AColor );
171   aDrawer->GetDouble ( MeshVS_DA_TextExpansionFactor, AExpansionFactor );
172   aDrawer->GetDouble ( MeshVS_DA_TextSpace, ASpace );
173
174   if ( aDrawer->GetAsciiString ( MeshVS_DA_TextFont, AFontString ) )
175     AFont = AFontString.ToCString();
176   if ( aDrawer->GetInteger ( MeshVS_DA_TextStyle, AStyleInt ) )
177     ATextStyle = (Aspect_TypeOfStyleText) AStyleInt;
178   if ( aDrawer->GetInteger ( MeshVS_DA_TextDisplayType, ADispInt ) )
179     ADisplayType = (Aspect_TypeOfDisplayText) ADispInt;
180   if ( aDrawer->GetInteger ( MeshVS_DA_TextFontAspect, AAspect ) )
181     AFontAspectType = (OSD_FontAspect)AAspect;         
182
183   Handle (Graphic3d_AspectText3d) aTextAspect = new Graphic3d_AspectText3d ( AColor, AFont, AExpansionFactor, ASpace,
184     ATextStyle, ADisplayType );
185   aTextAspect->SetTextFontAspect( AFontAspectType );
186   Handle (Graphic3d_AspectMarker3d) anAspectMarker3d =
187     new Graphic3d_AspectMarker3d( Aspect_TOM_POINT, Quantity_NOC_GRAY, 1. );
188   aTextGroup->SetPrimitivesAspect( aTextAspect );
189   aTextGroup->SetPrimitivesAspect( anAspectMarker3d );
190
191   aTextGroup->BeginPrimitives();
192
193   MeshVS_Buffer aCoordsBuf (3*aMaxFaceNodes*sizeof(Standard_Real));
194   TColStd_Array1OfReal aCoords (aCoordsBuf, 1, 3*aMaxFaceNodes);
195   Standard_Integer NbNodes;
196   TCollection_AsciiString aStr;
197   MeshVS_EntityType aType;
198   TColStd_PackedMapOfInteger aCustomElements;
199
200   Standard_Real X, Y, Z;
201
202   // subtract the hidden elements and ids to exclude (to minimise allocated memory)
203   TColStd_PackedMapOfInteger anIDs;
204   anIDs.Assign( IDs );
205   if ( IsElement )
206   {
207     Handle(TColStd_HPackedMapOfInteger) aHiddenElems = myParentMesh->GetHiddenElems();
208     if ( !aHiddenElems.IsNull() )
209       anIDs.Subtract( aHiddenElems->Map() );
210   }
211   anIDs.Subtract( IDsToExclude );
212
213   TColStd_MapIteratorOfPackedMapOfInteger it (anIDs);
214   for( ; it.More(); it.Next() )
215   {
216     Standard_Integer aKey = it.Key();
217     if( GetText ( IsElement, aKey, aStr ) )
218     {
219       if( aSource->GetGeom ( aKey, IsElement, aCoords, NbNodes, aType ) )
220       {
221         if( aType == MeshVS_ET_Node )
222         {
223           X = aCoords(1);
224           Y = aCoords(2);
225           Z = aCoords(3);
226         }
227         else if( aType == MeshVS_ET_Link || 
228           aType == MeshVS_ET_Face || 
229           aType == MeshVS_ET_Volume )
230         {
231           if( IsElement && IsExcludingOn() )
232             IDsToExclude.Add( aKey );
233           X = Y = Z = 0;
234           for ( Standard_Integer i=1; i<=NbNodes; i++ )
235           {
236             X += aCoords (3*i-2);
237             Y += aCoords (3*i-1);
238             Z += aCoords (3*i);
239           }
240           X /= Standard_Real ( NbNodes );
241           Y /= Standard_Real ( NbNodes );
242           Z /= Standard_Real ( NbNodes );
243         }
244         else 
245         {
246           aCustomElements.Add( aKey );
247           continue;
248         }
249
250         Graphic3d_Vertex aPoint( X, Y, Z );
251
252         aTextGroup->Marker ( aPoint );
253         aTextGroup->Text ( aStr.ToCString(), aPoint, aHeight );
254       }
255     }
256   }
257
258   aTextGroup->EndPrimitives();
259
260   if (!aCustomElements.IsEmpty())
261     CustomBuild ( Prs, aCustomElements, IDsToExclude, theDisplayMode );
262 }