1 // Copyright (C) 2007-2019 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM OBJECT : interactive object for Geometry entities visualization
24 // File : GEOM_Annotation.hxx
27 #ifndef GEOM_Annotation_HeaderFile
28 #define GEOM_Annotation_HeaderFile
30 #include <AIS_InteractiveObject.hxx>
31 #include <Bnd_Box.hxx>
32 #include <Font_FontAspect.hxx>
34 #include <NCollection_Handle.hxx>
35 #include <NCollection_String.hxx>
36 #include <OpenGl_Element.hxx>
37 #include <OpenGl_TextParam.hxx>
38 #include <Prs3d_Presentation.hxx>
39 #include <Prs3d_LineAspect.hxx>
40 #include <Prs3d_TextAspect.hxx>
41 #include <PrsMgr_PresentationManager3d.hxx>
42 #include <Select3D_SensitiveBox.hxx>
43 #include <SelectMgr_EntityOwner.hxx>
44 #include <StdSelect_Shape.hxx>
45 #include <TCollection_ExtendedString.hxx>
47 #include <Basics_OCCTVersion.hxx>
49 class OpenGl_GraphicDriver;
50 class OpenGl_PrimitiveArray;
55 * \class GEOM_Annotation
56 * \brief Interactive object, representating annotation entity
58 class GEOM_Annotation : public AIS_InteractiveObject
62 DEFINE_STANDARD_RTTIEXT( GEOM_Annotation, AIS_InteractiveObject )
64 //! Enumerates supported highlighting modes.
65 //! - HighlightAll : all elements of the annotation are highlighted.
66 //! - HighlightLabel : only annotation label is highlighted.
76 Standard_EXPORT GEOM_Annotation();
79 virtual ~GEOM_Annotation() {}
81 //! Sets annotation text string.
82 //! \param theText [in] the string displayed in annotation label.
83 Standard_EXPORT void SetText( const TCollection_ExtendedString& theText );
85 //! Returns annotation text string.
86 const TCollection_ExtendedString& GetText() const { return myText; }
88 //! Sets position of the annotation text label.
89 //! \param thePosition [in] the cartesian point defining the position of lower left
90 //! corner of the text label. When displayed in fixed screen position mode
91 //! (\sa SetScreenFixed) the position is defined as {x,y} pixel coordinate
92 //! of window space, otherwise 3D point defined in world's coordinate system
94 Standard_EXPORT void SetPosition( const gp_Pnt& thePosition );
96 //! Returns position of the annotation text label.
97 const gp_Pnt& GetPosition() const { return myPosition; }
99 //! Sets or disables "2D screen fixed" positioning mode. In this mode the annotation
100 //! is fixed at predefined pixel location in the window coordinate space. Other mode
101 //! is "3D screen aligned" positioning, when the label is aligned in plane of the
102 //! screen, while its position is a 3D point defined in world's coordinate system.
103 Standard_EXPORT void SetIsScreenFixed( const Standard_Boolean theIsFixed );
105 //! Returns value of "screen fixed" positioning mode.
106 Standard_Boolean GetIsScreenFixed() const { return myIsScreenFixed; }
108 //! Converts annotation position to screen fixed coordinates and
109 //! modifies its "2D screen fixed" flag correspondingly.
110 //! @param theView [in] the view to perform projection of coordinates.
111 Standard_EXPORT void Set2dPosition( const Handle(V3d_View)& theView );
113 //! Converts annotation position from screen fixed coordinates
114 //! to 3D position and modifies its "2D screen fixed" flag
116 //! @param theView [in] the view to perform projection of coordinates.
117 Standard_EXPORT void Set3dPosition( const Handle(V3d_View)& theView );
119 //! Sets attachment point of extension line.
120 //! \param thePoint [in] the 3D cartesian point defined in world's coordinate system
121 //! (a point on annotated geometry).
122 Standard_EXPORT void SetAttachPoint( const gp_Pnt& thePoint );
124 //! Returns attachment point of extension line.
125 const gp_Pnt& GetAttachPoint() const { return myAttach; }
127 //! Sets shape (annotated shape) that will be used for highlighting.
128 Standard_EXPORT void SetHilightShape( const TopoDS_Shape& theShape );
130 //! Returns the highlighting shape.
131 const TopoDS_Shape HilightShape() const { return myShape; }
135 //! Sets color for the presentation.
136 Standard_EXPORT virtual void SetColor( const Quantity_Color& theColor ) Standard_OVERRIDE;
139 Standard_EXPORT void SetTextColor( const Quantity_Color& theColor );
141 //! Returns color for the text's label.
142 Quantity_Color GetTextColor() const { return myDrawer->TextAspect()->Aspect()->Color(); }
144 //! Sets line's color.
145 Standard_EXPORT void SetLineColor( const Quantity_Color& theColor);
147 //! Returns color for the connection line.
148 Quantity_Color GetLineColor() const { return myDrawer->LineAspect()->Aspect()->Color(); }
150 //! Sets line width to be used for drawing the annotation's extension line and underline.
151 Standard_EXPORT void SetLineWidth( const Standard_Real theLineWidth );
153 //! Returns line width for drawing the annotation's extension line and underline.
154 Standard_Real GetLineWidth() const { return myDrawer->LineAspect()->Aspect()->Width(); }
156 //! Sets style of connection line.
157 Standard_EXPORT void SetLineStyle( const Aspect_TypeOfLine theStyle );
159 //! Retusn style of connection line.
160 Aspect_TypeOfLine GetLineStyle() const { return myDrawer->LineAspect()->Aspect()->Type(); }
162 //! Sets style of hidden connection line.
163 Standard_EXPORT void SetHiddenLineStyle( const Aspect_TypeOfLine theStyle );
165 //! Retusn style of hidden connection line.
166 Aspect_TypeOfLine GetHiddenLineStyle() const { return myDrawer->HiddenLineAspect()->Aspect()->Type(); }
168 //! Sets text height in pixels.
169 Standard_EXPORT void SetTextHeight( const Standard_Real theHeight );
171 //! Returns text's height in pixels.
172 Standard_Real GetTextHeight() const { return myDrawer->TextAspect()->Height(); }
174 //! Sets font aspect for label.
175 Standard_EXPORT void SetFontAspect( const Font_FontAspect theFontAspect );
177 //! Returns label's font aspect.
178 Font_FontAspect GetFontAspect() const { return myDrawer->TextAspect()->Aspect()->GetTextFontAspect(); }
180 //! Sets font used for drawing the label.
181 Standard_EXPORT void SetFont( const TCollection_AsciiString& theFont );
183 //! Returns font used for drawing the label.
184 TCollection_AsciiString GetFont() const { return myDrawer->TextAspect()->Aspect()->Font(); }
186 //! Sets annotation auto-hiding option.
187 //! \param theIsEnable [in] the option flag. If passed true, the annotation
188 //! will be automatically hidden in the view if the attachment point
189 //! goes outside of the view.
190 void SetAutoHide( const Standard_Boolean theIsEnable ) { myIsAutoHide = theIsEnable; }
192 //! Returns current state of the auto-hiding option.
193 Standard_Boolean GetAutoHide() const { return myIsAutoHide; }
195 //! Sets highlight mode used for display of presentation.
196 //! \param theMode [in] the one of the supported highlight modes.
197 void SetHighlightMode( const HighlightMode theMode ) { myHilightMode = theMode; }
199 //! Returns highlight mode
200 HighlightMode GetHilightMode() const { return myHilightMode; }
202 //! Sets special flag that allows disabling depth testing when rendering
203 //! the graphical presentation. When disable the hidden parts such as
204 //! lines and text become visible and a rendered with another drawing
205 //! aspect. This mode should be explicitly used with setting top layer
206 //! for the presentation. Otherwise the behavior is undefined.
207 Standard_EXPORT void SetDepthCulling( const Standard_Boolean theToEnable );
209 //! Returns depth culling state.
210 Standard_Boolean GetDepthCulling() const { return myIsDepthCulling; }
215 //! Restores default z-layer for this presentation.
216 Standard_EXPORT void SetDefaultZLayer();
218 //! Returns default position for placing annotation when its attachment point
219 //! has been determined.
220 //! @param theIsScreenFixed [in] the state of annotation's "2D screen fixed" mode.
221 //! @param theAttachPnt [in] the attachment point.
222 //! @param theOffset [in] the offset value for placing the position relative to attachment.s
223 //! @param theView [in] the view for projecting coordinates.
224 Standard_EXPORT static gp_Pnt GetDefaultPosition( const Standard_Boolean theIsScreenFixed,
225 const gp_Pnt& theAttachPnt,
226 const Standard_Real theOffset,
227 const Handle(V3d_View)& theView );
229 //! Converts 3d position to 2d on screen point.
230 //! @param thePosition [in] the 3d position.
231 //! @param theAttach [in] the attachment point.
232 //! @param theView [in] the view for projecting coordinates.
233 Standard_EXPORT static gp_Pnt ConvertPosition2d( const gp_Pnt& thePosition,
234 const gp_Pnt& theAttach,
235 const Handle(V3d_View)& theView );
237 //! Converts 2d position to 3d point.
238 //! @param thePosition [in] the 2d position.
239 //! @param theAttach [in] the attachment point.
240 //! @param theView [in] the view for projecting coordinates.
241 Standard_EXPORT static gp_Pnt ConvertPosition3d( const gp_Pnt& thePosition,
242 const gp_Pnt& theAttach,
243 const Handle(V3d_View)& theView );
245 // Interactive dragging:
248 //! Prepares necessary data to perform dragging.
249 Standard_EXPORT void BeginDrag();
251 //! Drags annotation presentation in the screen plane using the given pixel delta.
252 //! \param theDx, theDy [in] the drag offset in pixels (from beginning of dragging).
253 //! \param theView [in] the current view for evaluating drag with 3D position mode.
254 Standard_EXPORT void Drag( const Standard_Integer theDx,
255 const Standard_Integer theDy,
256 const Handle(V3d_View)& theView );
258 //! Perform necessary update when dragging is finished.
259 Standard_EXPORT void EndDrag();
261 //! Perform necessary update when dragging need undo.
262 Standard_EXPORT void UndoDrag();
266 virtual void Compute( const Handle(PrsMgr_PresentationManager3d)& thePresentationManager,
267 const Handle(Prs3d_Presentation)& thePresentation,
268 const Standard_Integer theMode = 0 ) Standard_OVERRIDE;
270 virtual void ComputeSelection( const Handle(SelectMgr_Selection)& theSelection,
271 const Standard_Integer theMode ) Standard_OVERRIDE;
273 virtual void setLocalTransformation( const Handle(Geom_Transformation)& /*theTransformation*/ ) Standard_OVERRIDE {}
275 virtual void SetTransformPersistence( const Handle(Graphic3d_TransformPers)& /*theTrsfPers*/ ) Standard_OVERRIDE {}
277 Bnd_Box TextBoundingBox() const;
279 void SetPosition( const gp_Pnt& thePosition, const Standard_Boolean theUpdateSelection );
283 gp_Pnt myAttach; //!< Attachment point of extension line.
284 gp_Pnt myPosition; //!< Position of text label.
285 gp_Pnt myStartPosition; //!< Position before starting dragging operation.
286 Standard_Boolean myIsScreenFixed; //!< Flag indicating whether "screen fixed" positioning mode is turned on or off.
287 Standard_Boolean myIsAutoHide; //!< Flag indicating whether "auto-hiding" option is turned on.
288 Standard_Boolean myIsDepthCulling; //!< Flag indicating whether the "depth culling" is turned on.
289 HighlightMode myHilightMode; //!< Highlight mode for presentation.
290 TCollection_ExtendedString myText; //!< Text string of the label presentation.
291 TopoDS_Shape myShape; //!< Highlighting shape.
295 //! Custom element implementing dynamic rendering of 3D annotation
296 //! and invoking dynamic callback in presentation class.
297 class OpenGl_Annotation : public OpenGl_Element
301 //! Constructor. Some of the input properties are assigned by reference for dynamic draw
302 //! (it is not likely that GL element will ever outlive the interactive object).
303 //! \param theAnnotation [in] the instance of interactive presentation class.
304 //! \param theTextHeight [in] the height of the text label.
305 //! \param theDriver [in] the instance of graphical driver required for initialization.
306 OpenGl_Annotation( GEOM_Annotation* theAnnotation,
307 const Standard_Integer theTextHeight,
308 const OpenGl_GraphicDriver* theDriver );
310 //! Destructor. Releases GL resources with NULL context.
311 virtual ~OpenGl_Annotation();
313 //! Releases GL resources with the given GL context.
314 virtual void Release( OpenGl_Context* theCtx ) Standard_OVERRIDE;
316 //! Renders the annotation graphical elements.
317 virtual void Render( const Handle(OpenGl_Workspace)& theWorkspace ) const Standard_OVERRIDE;
319 void SetDepthMode( const int theMode ) { myDepthMode = theMode; }
330 GEOM_Annotation* myAISObject; //!< Instance of presentation class.
331 NCollection_String myText; //!< Text string of annotation label.
332 OpenGl_TextParam myTextParams; //!< Text draw parameters.
333 OpenGl_Text* myTextDraw; //!< Text draw element.
334 OpenGl_PrimitiveArray* myTextLineDraw; //!< Text underline draw element.
335 OpenGl_PrimitiveArray* myExtLineDraw; //!< Extension line draw element.
336 OpenGl_PrimitiveArray* myExtMarkerDraw; //!< Extension marker draw element.
337 int myDepthMode; //!< Depth mode for drawing hidden line presentation.
338 mutable float myTextLineY; //!< Text's underlines relative position.
339 mutable TextSize myTextSize; //!< Text's size parameters
340 mutable Graphic3d_Vec2 myTextUnderline; //!< Text's underline position.
341 mutable unsigned int myTextDPI; //!< Text's DPI scale used for last rendering.
344 friend class OpenGl_Annotation; // allow opengl element to get private data and invoke callback methods
348 //! Custom entity owner implementing correct highlight for topmost mode.
349 class GEOM_AnnotationOwner : public SelectMgr_EntityOwner
354 GEOM_AnnotationOwner( const TopoDS_Shape& theShape,
355 const Handle(SelectMgr_SelectableObject)& theSelectable,
356 const Standard_Integer thePriority )
357 : SelectMgr_EntityOwner( theSelectable, thePriority ),
358 myShape( theShape ) {}
360 //! Perform highlighting of the presentation.
361 //! \param thePresentationMgr [in] the presentation manager.
362 //! \param theColor [in] the highlighting color.
363 //! \param theMode [in] the display mode.
365 HilightWithColor( const Handle(PrsMgr_PresentationManager3d)& thePM,
366 const Handle(Prs3d_Drawer)& theStyle,
367 const Standard_Integer theMode = 0 ) Standard_OVERRIDE;
369 //! Removes highlighting from the type of shape.
371 Unhilight ( const Handle(PrsMgr_PresentationManager)& thePM,
372 const Standard_Integer theMode = 0 ) Standard_OVERRIDE;
374 //! Clears the presentation manager object aPM of all shapes
375 //! with the given selection mode.
377 Clear ( const Handle(PrsMgr_PresentationManager)& thePM,
378 const Standard_Integer theMode = 0 ) Standard_OVERRIDE;
382 TopoDS_Shape myShape;
383 Handle(StdSelect_Shape) myPrsSh;
386 //! Custom sensitive entity with implementing option to support selection
387 //! with depth culling flag turned off.
388 class GEOM_AnnotationSensEntity : public Select3D_SensitiveBox
393 GEOM_AnnotationSensEntity( const Handle(SelectMgr_EntityOwner)& theOwner,
394 const Bnd_Box& theBox,
395 const Standard_Boolean theIsDepthCulling )
396 : Select3D_SensitiveBox( theOwner, theBox ),
397 myIsDepthCulling( theIsDepthCulling ) {}
399 //! Checks whether the box overlaps current selecting volume.
400 virtual Standard_Boolean Matches( SelectBasics_SelectingVolumeManager& theMgr,
401 SelectBasics_PickResult& thePickResult ) Standard_OVERRIDE
403 const Standard_Boolean isMatches = Select3D_SensitiveBox::Matches( theMgr, thePickResult );
404 if ( !myIsDepthCulling )
406 #if OCC_VERSION_LARGE <= 0x07030000
407 thePickResult = SelectBasics_PickResult( -DBL_MAX, thePickResult.DistToGeomCenter() );
409 thePickResult = SelectBasics_PickResult( -DBL_MAX, thePickResult.DistToGeomCenter(), thePickResult.PickedPoint() );
417 Standard_Boolean myIsDepthCulling;
421 DEFINE_STANDARD_HANDLE( GEOM_Annotation, AIS_InteractiveObject )