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