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