Salome HOME
7d9af8d31613ea4cddcd3f77c1866ad1aacedf5d
[modules/geom.git] / src / OBJECT / GEOM_Annotation.hxx
1 // Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  GEOM OBJECT : interactive object for Geometry entities visualization
24 //  File   : GEOM_Annotation.hxx
25 //  Module : GEOM
26 //
27 #ifndef GEOM_Annotation_HeaderFile
28 #define GEOM_Annotation_HeaderFile
29
30 #include <AIS_InteractiveObject.hxx>
31 #include <Bnd_Box.hxx>
32 #include <Font_FontAspect.hxx>
33 #include <gp_Pnt.hxx>
34 #include <NCollection_Handle.hxx>
35 #include <NCollection_String.hxx>
36 #include <OpenGl_Element.hxx>
37 #include <Prs3d_Presentation.hxx>
38 #include <Prs3d_LineAspect.hxx>
39 #include <Prs3d_TextAspect.hxx>
40 #include <PrsMgr_PresentationManager3d.hxx>
41 #include <Select3D_SensitiveBox.hxx>
42 #include <SelectMgr_EntityOwner.hxx>
43 #include <StdSelect_Shape.hxx>
44 #include <TCollection_ExtendedString.hxx>
45
46 #include <Basics_OCCTVersion.hxx>
47 #if OCC_VERSION_LARGE >= 0x07040000
48 #include <Graphic3d_Text.hxx>
49 #else
50 #include <OpenGl_TextParam.hxx>
51 #endif
52
53 class OpenGl_GraphicDriver;
54 class OpenGl_PrimitiveArray;
55 class OpenGl_Text;
56 class V3d_View;
57
58 /*!
59  * \class GEOM_Annotation
60  * \brief Interactive object, representating annotation entity
61  */
62 class GEOM_Annotation : public AIS_InteractiveObject
63 {
64 public:
65
66   DEFINE_STANDARD_RTTIEXT( GEOM_Annotation, AIS_InteractiveObject )
67
68   //! Enumerates supported highlighting modes.
69   //! - HighlightAll   : all elements of the annotation are highlighted.
70   //! - HighlightLabel : only annotation label is highlighted.
71   enum HighlightMode
72   {
73     HighlightAll   = 0,
74     HighlightLabel = 1
75   };
76
77 public:
78
79   //! Constructor.
80   Standard_EXPORT GEOM_Annotation();
81
82   //! Destructor.
83   virtual ~GEOM_Annotation() {}
84
85   //! Sets annotation text string.
86   //! \param theText [in] the string displayed in annotation label.
87   Standard_EXPORT void SetText( const TCollection_ExtendedString& theText );
88
89   //! Returns annotation text string.
90   const TCollection_ExtendedString& GetText() const { return myText; }
91
92   //! Sets position of the annotation text label.
93   //! \param thePosition [in] the cartesian point defining the position of lower left
94   //!        corner of the text label. When displayed in fixed screen position mode
95   //!        (\sa SetScreenFixed) the position is defined as {x,y} pixel coordinate
96   //!        of window space, otherwise 3D point defined in world's coordinate system
97   //!        is used.
98   Standard_EXPORT void SetPosition( const gp_Pnt& thePosition );
99
100   //! Returns position of the annotation text label.
101   const gp_Pnt& GetPosition() const { return myPosition; }
102
103   //! Sets or disables "2D screen fixed" positioning mode. In this mode the annotation
104   //! is fixed at predefined pixel location in the window coordinate space. Other mode
105   //! is "3D screen aligned" positioning, when the label is aligned in plane of the
106   //! screen, while its position is a 3D point defined in world's coordinate system.
107   Standard_EXPORT void SetIsScreenFixed( const Standard_Boolean theIsFixed );
108
109   //! Returns value of "screen fixed" positioning mode.
110   Standard_Boolean GetIsScreenFixed() const { return myIsScreenFixed; }
111
112   //! Converts annotation position to screen fixed coordinates and
113   //! modifies its "2D screen fixed" flag correspondingly.
114   //! @param theView [in] the view to perform projection of coordinates.
115   Standard_EXPORT void Set2dPosition( const Handle(V3d_View)& theView );
116
117   //! Converts annotation position from screen fixed coordinates
118   //! to 3D position and modifies its "2D screen fixed" flag
119   //! correspondingly.
120   //! @param theView [in] the view to perform projection of coordinates.
121   Standard_EXPORT void Set3dPosition( const Handle(V3d_View)& theView );
122
123   //! Sets attachment point of extension line.
124   //! \param thePoint [in] the 3D cartesian point defined in world's coordinate system
125   //!        (a point on annotated geometry).
126   Standard_EXPORT void SetAttachPoint( const gp_Pnt& thePoint );
127
128   //! Returns attachment point of extension line.
129   const gp_Pnt& GetAttachPoint() const { return myAttach; }
130
131   //! Sets shape (annotated shape) that will be used for highlighting.
132   Standard_EXPORT void SetHilightShape( const TopoDS_Shape& theShape );
133
134   //! Returns the highlighting shape.
135   const TopoDS_Shape HilightShape() const { return myShape; }
136
137 public:
138
139   //! Sets color for the presentation.
140   Standard_EXPORT virtual void SetColor( const Quantity_Color& theColor ) Standard_OVERRIDE;
141
142   //! Sets text color.
143   Standard_EXPORT void SetTextColor( const Quantity_Color& theColor );
144
145   //! Returns color for the text's label.
146   Quantity_Color GetTextColor() const { return myDrawer->TextAspect()->Aspect()->Color(); }
147
148   //! Sets line's color.
149   Standard_EXPORT void SetLineColor( const Quantity_Color& theColor);
150
151    //! Returns color for the connection line.
152   Quantity_Color GetLineColor() const { return myDrawer->LineAspect()->Aspect()->Color(); }
153
154   //! Sets line width to be used for drawing the annotation's extension line and underline.
155   Standard_EXPORT void SetLineWidth( const Standard_Real theLineWidth );
156
157   //! Returns line width for drawing the annotation's extension line and underline.
158   Standard_Real GetLineWidth() const { return myDrawer->LineAspect()->Aspect()->Width(); }
159
160   //! Sets style of connection line.
161   Standard_EXPORT void SetLineStyle( const Aspect_TypeOfLine theStyle );
162
163   //! Retusn style of connection line.
164   Aspect_TypeOfLine GetLineStyle() const { return myDrawer->LineAspect()->Aspect()->Type(); }
165
166   //! Sets style of hidden connection line.
167   Standard_EXPORT void SetHiddenLineStyle( const Aspect_TypeOfLine theStyle );
168
169   //! Retusn style of hidden connection line.
170   Aspect_TypeOfLine GetHiddenLineStyle() const { return myDrawer->HiddenLineAspect()->Aspect()->Type(); }
171
172   //! Sets text height in pixels.
173   Standard_EXPORT void SetTextHeight( const Standard_Real theHeight );
174
175   //! Returns text's height in pixels.
176   Standard_Real GetTextHeight() const { return myDrawer->TextAspect()->Height(); }
177
178   //! Sets font aspect for label.
179   Standard_EXPORT void SetFontAspect( const Font_FontAspect theFontAspect );
180
181   //! Returns label's font aspect.
182   Font_FontAspect GetFontAspect() const { return myDrawer->TextAspect()->Aspect()->GetTextFontAspect(); }
183
184   //! Sets font used for drawing the label.
185   Standard_EXPORT void SetFont( const TCollection_AsciiString& theFont );
186
187   //! Returns font used for drawing the label.
188   TCollection_AsciiString GetFont() const { return myDrawer->TextAspect()->Aspect()->Font(); }
189
190   //! Sets annotation auto-hiding option.
191   //! \param theIsEnable [in] the option flag. If passed true, the annotation 
192   //!        will be automatically hidden in the view if the attachment point
193   //!        goes outside of the view.
194   void SetAutoHide( const Standard_Boolean theIsEnable ) { myIsAutoHide = theIsEnable; }
195
196   //! Returns current state of the auto-hiding option.
197   Standard_Boolean GetAutoHide() const { return myIsAutoHide; }
198
199   //! Sets highlight mode used for display of presentation.
200   //! \param theMode [in] the one of the supported highlight modes.
201   void SetHighlightMode( const HighlightMode theMode ) { myHilightMode = theMode; }
202
203   //! Returns highlight mode
204   HighlightMode GetHilightMode() const { return myHilightMode; }
205
206   //! Sets special flag that allows disabling depth testing when rendering
207   //! the graphical presentation. When disable the hidden parts such as
208   //! lines and text become visible and a rendered with another drawing
209   //! aspect. This mode should be explicitly used with setting top layer
210   //! for the presentation. Otherwise the behavior is undefined.
211   Standard_EXPORT void SetDepthCulling( const Standard_Boolean theToEnable );
212
213   //! Returns depth culling state.
214   Standard_Boolean GetDepthCulling() const { return myIsDepthCulling; }
215
216 // Helper tools:
217 public:
218
219   //! Restores default z-layer for this presentation.
220   Standard_EXPORT void SetDefaultZLayer();
221
222   //! Returns default position for placing annotation when its attachment point
223   //! has been determined.
224   //! @param theIsScreenFixed [in] the state of annotation's "2D screen fixed" mode.
225   //! @param theAttachPnt [in] the attachment point.
226   //! @param theOffset [in] the offset value for placing the position relative to attachment.s
227   //! @param theView [in] the view for projecting coordinates.
228   Standard_EXPORT static gp_Pnt GetDefaultPosition( const Standard_Boolean theIsScreenFixed,
229                                                     const gp_Pnt& theAttachPnt,
230                                                     const Standard_Real theOffset,
231                                                     const Handle(V3d_View)& theView );
232
233   //! Converts 3d position to 2d on screen point.
234   //! @param thePosition [in] the 3d position.
235   //! @param theAttach [in] the attachment point.
236   //! @param theView [in] the view for projecting coordinates.
237   Standard_EXPORT static gp_Pnt ConvertPosition2d( const gp_Pnt& thePosition,
238                                                    const gp_Pnt& theAttach,
239                                                    const Handle(V3d_View)& theView );
240
241   //! Converts 2d position to 3d point.
242   //! @param thePosition [in] the 2d position.
243   //! @param theAttach [in] the attachment point.
244   //! @param theView [in] the view for projecting coordinates.
245   Standard_EXPORT static gp_Pnt ConvertPosition3d( const gp_Pnt& thePosition,
246                                                    const gp_Pnt& theAttach,
247                                                    const Handle(V3d_View)& theView );
248
249 // Interactive dragging:
250 public:
251
252   //! Prepares necessary data to perform dragging.
253   Standard_EXPORT void BeginDrag();
254
255   //! Drags annotation presentation in the screen plane using the given pixel delta.
256   //! \param theDx, theDy [in] the drag offset in pixels (from beginning of dragging).
257   //! \param theView [in] the current view for evaluating drag with 3D position mode.
258   Standard_EXPORT void Drag( const Standard_Integer theDx,
259                              const Standard_Integer theDy,
260                              const Handle(V3d_View)& theView );
261
262   //! Perform necessary update when dragging is finished.
263   Standard_EXPORT void EndDrag();
264
265   //! Perform necessary update when dragging need undo.
266   Standard_EXPORT void UndoDrag();
267
268 private:
269
270   virtual void Compute( const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
271                         const Handle(Prs3d_Presentation)& thePresentation,
272                         const Standard_Integer theMode = 0 ) Standard_OVERRIDE;
273
274   virtual void ComputeSelection( const Handle(SelectMgr_Selection)& theSelection,
275                                  const Standard_Integer theMode ) Standard_OVERRIDE;
276
277 #if OCC_VERSION_LARGE >= 0x070400ff
278   virtual void setLocalTransformation( const Handle(TopLoc_Datum3D)& /*theTransformation*/ ) Standard_OVERRIDE {}
279 #else
280   virtual void setLocalTransformation( const Handle(Geom_Transformation)& /*theTransformation*/ ) Standard_OVERRIDE {}
281 #endif
282
283   virtual void SetTransformPersistence( const Handle(Graphic3d_TransformPers)& /*theTrsfPers*/ ) Standard_OVERRIDE {}
284
285   Bnd_Box TextBoundingBox() const;
286
287   void SetPosition( const gp_Pnt& thePosition, const Standard_Boolean theUpdateSelection );
288
289 private:
290
291   gp_Pnt myAttach; //!< Attachment point of extension line.
292   gp_Pnt myPosition; //!< Position of text label.
293   gp_Pnt myStartPosition; //!< Position before starting dragging operation.
294   Standard_Boolean myIsScreenFixed; //!< Flag indicating whether "screen fixed" positioning mode is turned on or off.
295   Standard_Boolean myIsAutoHide; //!< Flag indicating whether "auto-hiding" option is turned on.
296   Standard_Boolean myIsDepthCulling; //!< Flag indicating whether the "depth culling" is turned on.
297   HighlightMode myHilightMode; //!< Highlight mode for presentation.
298   TCollection_ExtendedString myText; //!< Text string of the label presentation.
299   TopoDS_Shape myShape; //!< Highlighting shape.
300
301 private:
302
303   //! Custom element implementing dynamic rendering of 3D annotation
304   //! and invoking dynamic callback in presentation class.
305   class OpenGl_Annotation : public OpenGl_Element
306   {
307   public:
308
309     //! Constructor. Some of the input properties are assigned by reference for dynamic draw
310     //! (it is not likely that GL element will ever outlive the interactive object).
311     //! \param theAnnotation [in] the instance of interactive presentation class.
312     //! \param theTextHeight [in] the height of the text label.
313     //! \param theDriver [in] the instance of graphical driver required for initialization.
314     OpenGl_Annotation( GEOM_Annotation* theAnnotation,
315                        const Standard_Integer theTextHeight,
316                        const OpenGl_GraphicDriver* theDriver );
317
318     //! Destructor. Releases GL resources with NULL context.
319     virtual ~OpenGl_Annotation();
320
321     //! Releases GL resources with the given GL context.
322     virtual void Release( OpenGl_Context* theCtx ) Standard_OVERRIDE;
323
324     //! Renders the annotation graphical elements.
325     virtual void Render( const Handle(OpenGl_Workspace)& theWorkspace ) const Standard_OVERRIDE;
326
327     void SetDepthMode( const int theMode ) { myDepthMode = theMode; }
328
329   private:
330
331     struct TextSize {
332       float x; // width
333       float y; // height
334       float a; // ascent
335       float d; // descent
336     };
337
338     GEOM_Annotation* myAISObject;           //!< Instance of presentation class.
339     NCollection_String myText;              //!< Text string of annotation label.
340 #if OCC_VERSION_LARGE >= 0x07040000
341     Handle(Graphic3d_Text) myTextParams;    //!< Text draw parameters.
342 #else
343     OpenGl_TextParam myTextParams;          //!< Text draw parameters.
344 #endif
345     OpenGl_Text* myTextDraw;                //!< Text draw element.
346     OpenGl_PrimitiveArray* myTextLineDraw;  //!< Text underline draw element.
347     OpenGl_PrimitiveArray* myExtLineDraw;   //!< Extension line draw element.
348     OpenGl_PrimitiveArray* myExtMarkerDraw; //!< Extension marker draw element.
349     int myDepthMode;                        //!< Depth mode for drawing hidden line presentation.
350     mutable float myTextLineY;              //!< Text's underlines relative position.
351     mutable TextSize myTextSize;            //!< Text's size parameters
352     mutable Graphic3d_Vec2 myTextUnderline; //!< Text's underline position.
353     mutable unsigned int myTextDPI;         //!< Text's DPI scale used for last rendering.
354   };
355
356   friend class OpenGl_Annotation; // allow opengl element to get private data and invoke callback methods
357
358 public:
359
360   //! Custom entity owner implementing correct highlight for topmost mode.
361   class GEOM_AnnotationOwner : public SelectMgr_EntityOwner
362   {
363   public:
364
365     //! Constructor.
366     GEOM_AnnotationOwner( const TopoDS_Shape& theShape,
367                           const Handle(SelectMgr_SelectableObject)& theSelectable,
368                           const Standard_Integer thePriority )
369       : SelectMgr_EntityOwner( theSelectable, thePriority ),
370         myShape( theShape ) {}
371
372     //! Perform highlighting of the presentation.
373     //! \param thePresentationMgr [in] the presentation manager.
374     //! \param theColor [in] the highlighting color.
375     //! \param theMode [in] the display mode.
376     virtual void
377       HilightWithColor( const Handle(PrsMgr_PresentationManager3d)& thePM,
378                         const Handle(Prs3d_Drawer)& theStyle,
379                         const Standard_Integer theMode = 0 ) Standard_OVERRIDE;
380
381     //! Removes highlighting from the type of shape.
382     virtual void
383       Unhilight ( const Handle(PrsMgr_PresentationManager)& thePM,
384                   const Standard_Integer theMode = 0 ) Standard_OVERRIDE;
385
386     //! Clears the presentation manager object aPM of all shapes
387     //! with the given selection mode.
388     virtual void
389       Clear ( const Handle(PrsMgr_PresentationManager)& thePM,
390               const Standard_Integer theMode = 0 ) Standard_OVERRIDE;
391
392   private:
393
394     TopoDS_Shape myShape;
395     Handle(StdSelect_Shape) myPrsSh;
396   };
397
398   //! Custom sensitive entity with implementing option to support selection
399   //! with depth culling flag turned off.
400   class GEOM_AnnotationSensEntity : public Select3D_SensitiveBox
401   {
402   public:
403
404     //! Constructor.
405     GEOM_AnnotationSensEntity( const Handle(SelectMgr_EntityOwner)& theOwner,
406                                const Bnd_Box& theBox,
407                                const Standard_Boolean theIsDepthCulling )
408       : Select3D_SensitiveBox( theOwner, theBox ),
409         myIsDepthCulling( theIsDepthCulling ) {}
410
411     //! Checks whether the box overlaps current selecting volume.
412     virtual Standard_Boolean Matches( SelectBasics_SelectingVolumeManager& theMgr,
413                                       SelectBasics_PickResult& thePickResult ) Standard_OVERRIDE
414     {
415       const Standard_Boolean isMatches = Select3D_SensitiveBox::Matches( theMgr, thePickResult );
416       if ( !myIsDepthCulling )
417       {
418         thePickResult = SelectBasics_PickResult( -DBL_MAX, thePickResult.DistToGeomCenter(), thePickResult.PickedPoint() );
419       }
420       return isMatches;
421     }
422
423   private:
424
425     Standard_Boolean myIsDepthCulling;
426   };
427 };
428
429 DEFINE_STANDARD_HANDLE( GEOM_Annotation, AIS_InteractiveObject )
430
431 #endif