0031458: Visualization - refine classes across Prs3d and StdPrs packages
[occt.git] / src / Graphic3d / Graphic3d_Structure.hxx
1 // Created on: 1991-06-12
2 // Created by: NW,JPB,CAL
3 // Copyright (c) 1991-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifndef _Graphic3d_Structure_HeaderFile
18 #define _Graphic3d_Structure_HeaderFile
19
20 #include <Graphic3d_BndBox4f.hxx>
21 #include <Graphic3d_BndBox4d.hxx>
22 #include <Graphic3d_CStructure.hxx>
23 #include <Graphic3d_MapOfStructure.hxx>
24 #include <Graphic3d_SequenceOfGroup.hxx>
25 #include <Graphic3d_SequenceOfHClipPlane.hxx>
26 #include <Graphic3d_TypeOfComposition.hxx>
27 #include <Graphic3d_TypeOfConnection.hxx>
28 #include <Graphic3d_TypeOfStructure.hxx>
29 #include <Graphic3d_TransformPers.hxx>
30 #include <Graphic3d_TransModeFlags.hxx>
31 #include <Graphic3d_Vertex.hxx>
32 #include <Graphic3d_ZLayerId.hxx>
33 #include <NCollection_IndexedMap.hxx>
34
35 class Graphic3d_StructureManager;
36 class Graphic3d_DataStructureManager;
37 class Bnd_Box;
38 class gp_Pnt;
39
40 DEFINE_STANDARD_HANDLE(Graphic3d_Structure, Standard_Transient)
41
42 //! This class allows the definition a graphic object.
43 //! This graphic structure can be displayed, erased, or highlighted.
44 //! This graphic structure can be connected with another graphic structure.
45 class Graphic3d_Structure : public Standard_Transient
46 {
47   DEFINE_STANDARD_RTTIEXT(Graphic3d_Structure, Standard_Transient)
48   friend class Graphic3d_Group;
49 public:
50
51   //! Creates a graphic object in the manager theManager.
52   //! It will appear in all the views of the visualiser.
53   //! The structure is not displayed when it is created.
54   //! @param theManager structure manager holding this structure
55   //! @param theLinkPrs another structure for creating a shadow (linked) structure
56   Standard_EXPORT Graphic3d_Structure (const Handle(Graphic3d_StructureManager)& theManager,
57                                        const Handle(Graphic3d_Structure)& theLinkPrs = Handle(Graphic3d_Structure)());
58   
59   //! if WithDestruction == Standard_True then
60   //! suppress all the groups of primitives in the structure.
61   //! and it is mandatory to create a new group in <me>.
62   //! if WithDestruction == Standard_False then
63   //! clears all the groups of primitives in the structure.
64   //! and all the groups are conserved and empty.
65   //! They will be erased at the next screen update.
66   //! The structure itself is conserved.
67   //! The transformation and the attributes of <me> are conserved.
68   //! The childs of <me> are conserved.
69   Standard_EXPORT virtual void Clear (const Standard_Boolean WithDestruction = Standard_True);
70   
71   //! Suppresses the structure <me>.
72   //! It will be erased at the next screen update.
73   Standard_EXPORT virtual ~Graphic3d_Structure();
74   
75   //! Displays the structure <me> in all the views of the visualiser.
76   Standard_EXPORT virtual void Display();
77
78   //! Returns the current display priority for this structure.
79   Standard_Integer DisplayPriority() const { return myCStructure->Priority; }
80   
81   //! Erases the structure <me> in all the views
82   //! of the visualiser.
83   Standard_EXPORT virtual void Erase();
84   
85   //! Highlights the structure in all the views with the given style
86   //! @param theStyle [in] the style (type of highlighting: box/color, color and opacity)
87   //! @param theToUpdateMgr [in] defines whether related computed structures will be
88   //! highlighted via structure manager or not
89   Standard_EXPORT void Highlight (const Handle(Graphic3d_PresentationAttributes)& theStyle, const Standard_Boolean theToUpdateMgr = Standard_True);
90   
91   //! Suppress the structure <me>.
92   //! It will be erased at the next screen update.
93   //! Warning: No more graphic operations in <me> after this call.
94   //! Category: Methods to modify the class definition
95   Standard_EXPORT void Remove();
96   
97   //! Computes axis-aligned bounding box of a structure.
98   Standard_EXPORT virtual void CalculateBoundBox();
99   
100   //! Sets infinite flag.
101   //! When TRUE, the MinMaxValues method returns:
102   //! theXMin = theYMin = theZMin = RealFirst().
103   //! theXMax = theYMax = theZMax = RealLast().
104   //! By default, structure is created not infinite but empty.
105   void SetInfiniteState (const Standard_Boolean theToSet) { myCStructure->IsInfinite = theToSet ? 1 : 0; }
106
107   //! Modifies the order of displaying the structure.
108   //! Values are between 0 and 10.
109   //! Structures are drawn according to their display priorities
110   //! in ascending order.
111   //! A structure of priority 10 is displayed the last and appears over the others.
112   //! The default value is 5.
113   //! Category: Methods to modify the class definition
114   //! Warning: If <me> is displayed then the SetDisplayPriority
115   //! method erase <me> and display <me> with the
116   //! new priority.
117   //! Raises PriorityDefinitionError if <Priority> is
118   //! greater than 10 or a negative value.
119   Standard_EXPORT void SetDisplayPriority (const Standard_Integer Priority);
120   
121   //! Reset the current priority of the structure to the
122   //! previous priority.
123   //! Category: Methods to modify the class definition
124   //! Warning: If <me> is displayed then the SetDisplayPriority
125   //! method erase <me> and display <me> with the
126   //! previous priority.
127   Standard_EXPORT void ResetDisplayPriority();
128   
129   //! Set Z layer ID for the structure. The Z layer mechanism
130   //! allows to display structures presented in higher layers in overlay
131   //! of structures in lower layers by switching off z buffer depth
132   //! test between layers
133   Standard_EXPORT void SetZLayer (const Graphic3d_ZLayerId theLayerId);
134   
135   //! Get Z layer ID of displayed structure.
136   //! The method returns -1 if the structure has no ID (deleted from graphic driver).
137   Graphic3d_ZLayerId GetZLayer() const { return myCStructure->ZLayer(); }
138   
139   //! Changes a sequence of clip planes slicing the structure on rendering.
140   //! @param thePlanes [in] the set of clip planes.
141   void SetClipPlanes (const Handle(Graphic3d_SequenceOfHClipPlane)& thePlanes) { myCStructure->SetClipPlanes (thePlanes); }
142   
143   //! Get clip planes slicing the structure on rendering.
144   //! @return set of clip planes.
145   const Handle(Graphic3d_SequenceOfHClipPlane)& ClipPlanes() const { return myCStructure->ClipPlanes(); }
146
147   //! Modifies the visibility indicator to Standard_True or
148   //! Standard_False for the structure <me>.
149   //! The default value at the definition of <me> is
150   //! Standard_True.
151   Standard_EXPORT void SetVisible (const Standard_Boolean AValue);
152   
153   //! Modifies the visualisation mode for the structure <me>.
154   Standard_EXPORT virtual void SetVisual (const Graphic3d_TypeOfStructure AVisual);
155   
156   //! Modifies the minimum and maximum zoom coefficients
157   //! for the structure <me>.
158   //! The default value at the definition of <me> is unlimited.
159   //! Category: Methods to modify the class definition
160   //! Warning: Raises StructureDefinitionError if <LimitInf> is
161   //! greater than <LimitSup> or if <LimitInf> or
162   //! <LimitSup> is a negative value.
163   Standard_EXPORT void SetZoomLimit (const Standard_Real LimitInf, const Standard_Real LimitSup);
164
165   //! Marks the structure <me> representing wired structure needed for highlight only so it won't be added to BVH tree.
166   void SetIsForHighlight (const Standard_Boolean isForHighlight) { myCStructure->IsForHighlight = isForHighlight; }
167   
168   //! Suppresses the highlight for the structure <me>
169   //! in all the views of the visualiser.
170   Standard_EXPORT void UnHighlight();
171   
172   virtual void Compute()
173   {
174     //
175   }
176
177   //! Returns the new Structure defined for the new visualization
178   virtual void computeHLR (const Handle(Graphic3d_Camera)& theProjector,
179                            Handle(Graphic3d_Structure)& theStructure)
180   {
181     (void )theProjector;
182     (void )theStructure;
183   }
184
185   //! Forces a new construction of the structure <me>
186   //! if <me> is displayed and TOS_COMPUTED.
187   Standard_EXPORT void ReCompute();
188   
189   //! Forces a new construction of the structure <me>
190   //! if <me> is displayed in <aProjetor> and TOS_COMPUTED.
191   Standard_EXPORT void ReCompute (const Handle(Graphic3d_DataStructureManager)& aProjector);
192   
193   //! Returns Standard_True if the structure <me> contains
194   //! Polygons, Triangles or Quadrangles.
195   Standard_EXPORT Standard_Boolean ContainsFacet() const;
196
197   //! Returns the groups sequence included in this structure.
198   const Graphic3d_SequenceOfGroup& Groups() const { return myCStructure->Groups(); }
199
200   //! Returns the current number of groups in this structure.
201   Standard_Integer NumberOfGroups() const { return myCStructure->Groups().Length(); }
202   
203   //! Append new group to this structure.
204   Standard_EXPORT Handle(Graphic3d_Group) NewGroup();
205
206   //! Returns the last created group or creates new one if list is empty.
207   Handle(Graphic3d_Group) CurrentGroup()
208   {
209     if (Groups().IsEmpty())
210     {
211       return NewGroup();
212     }
213     return Groups().Last();
214   }
215
216   //! Returns the highlight attributes.
217   const Handle(Graphic3d_PresentationAttributes)& HighlightStyle() const { return myCStructure->HighlightStyle(); }
218
219   //! Returns TRUE if this structure is deleted (after Remove() call).
220   Standard_Boolean IsDeleted() const { return myCStructure.IsNull(); }
221   
222   //! Returns the display indicator for this structure.
223   virtual Standard_Boolean IsDisplayed() const { return myCStructure->stick != 0; }
224   
225   //! Returns Standard_True if the structure <me> is empty.
226   //! Warning: A structure is empty if :
227   //! it do not have group or all the groups are empties
228   //! and it do not have descendant or all the descendants
229   //! are empties.
230   Standard_EXPORT Standard_Boolean IsEmpty() const;
231   
232   //! Returns Standard_True if the structure <me> is infinite.
233   Standard_Boolean IsInfinite() const
234   {
235     return IsDeleted()
236         || myCStructure->IsInfinite;
237   }
238   
239   //! Returns the highlight indicator for this structure.
240   virtual Standard_Boolean IsHighlighted() const { return myCStructure->highlight != 0; }
241   
242   //! Returns TRUE if the structure is transformed.
243   Standard_Boolean IsTransformed() const
244   {
245     return !myCStructure->Transformation().IsNull()
246          && myCStructure->Transformation()->Form() != gp_Identity;
247   }
248   
249   //! Returns the visibility indicator for this structure.
250   Standard_Boolean IsVisible() const { return myCStructure->visible != 0; }
251
252   //! Returns the coordinates of the boundary box of the structure <me>.
253   //! If <theToIgnoreInfiniteFlag> is TRUE, the method returns actual graphical
254   //! boundaries of the Graphic3d_Group components. Otherwise, the
255   //! method returns boundaries taking into account infinite state
256   //! of the structure. This approach generally used for application
257   //! specific fit operation (e.g. fitting the model into screen,
258   //! not taking into accout infinite helper elements).
259   //! Warning: If the structure <me> is empty then the empty box is returned,
260   //! If the structure <me> is infinite then the whole box is returned.
261   Standard_EXPORT Bnd_Box MinMaxValues (const Standard_Boolean theToIgnoreInfiniteFlag = Standard_False) const;
262
263   //! Returns the visualisation mode for the structure <me>.
264   Graphic3d_TypeOfStructure Visual() const { return myVisual; }
265   
266   //! Returns Standard_True if the connection is possible between
267   //! <AStructure1> and <AStructure2> without a creation
268   //! of a cycle.
269   //!
270   //! It's not possible to call the method
271   //! AStructure1->Connect (AStructure2, TypeOfConnection)
272   //! if
273   //! - the set of all ancestors of <AStructure1> contains
274   //! <AStructure1> and if the
275   //! TypeOfConnection == TOC_DESCENDANT
276   //! - the set of all descendants of <AStructure1> contains
277   //! <AStructure2> and if the
278   //! TypeOfConnection == TOC_ANCESTOR
279   Standard_EXPORT static Standard_Boolean AcceptConnection (Graphic3d_Structure* theStructure1,
280                                                             Graphic3d_Structure* theStructure2,
281                                                             Graphic3d_TypeOfConnection theType);
282   
283   //! Returns the group of structures to which <me> is connected.
284   Standard_EXPORT void Ancestors (Graphic3d_MapOfStructure& SG) const;
285   
286   //! If Atype is TOC_DESCENDANT then add <AStructure>
287   //! as a child structure of  <me>.
288   //! If Atype is TOC_ANCESTOR then add <AStructure>
289   //! as a parent structure of <me>.
290   //! The connection propagates Display, Highlight, Erase,
291   //! Remove, and stacks the transformations.
292   //! No connection if the graph of the structures
293   //! contains a cycle and <WithCheck> is Standard_True;
294   Standard_EXPORT void Connect (Graphic3d_Structure* theStructure,
295                                 Graphic3d_TypeOfConnection theType,
296                                 Standard_Boolean theWithCheck = Standard_False);
297
298   Standard_DEPRECATED("Deprecated short-cut")
299   void Connect (const Handle(Graphic3d_Structure)& thePrs)
300   {
301     Connect (thePrs.get(), Graphic3d_TOC_DESCENDANT);
302   }
303   
304   //! Returns the group of structures connected to <me>.
305   Standard_EXPORT void Descendants (Graphic3d_MapOfStructure& SG) const;
306   
307   //! Suppress the connection between <AStructure> and <me>.
308   Standard_EXPORT void Disconnect (Graphic3d_Structure* theStructure);
309
310   Standard_DEPRECATED("Deprecated alias for Disconnect()")
311   void Remove (const Handle(Graphic3d_Structure)& thePrs) { Disconnect (thePrs.get()); }
312   
313   //! If Atype is TOC_DESCENDANT then suppress all
314   //! the connections with the child structures of <me>.
315   //! If Atype is TOC_ANCESTOR then suppress all
316   //! the connections with the parent structures of <me>.
317   Standard_EXPORT void DisconnectAll (const Graphic3d_TypeOfConnection AType);
318
319   Standard_DEPRECATED("Deprecated alias for DisconnectAll()")
320   void RemoveAll() { DisconnectAll (Graphic3d_TOC_DESCENDANT); }
321   
322   //! Returns <ASet> the group of structures :
323   //! - directly or indirectly connected to <AStructure> if the
324   //! TypeOfConnection == TOC_DESCENDANT
325   //! - to which <AStructure> is directly or indirectly connected
326   //! if the TypeOfConnection == TOC_ANCESTOR
327   Standard_EXPORT static void Network (Graphic3d_Structure* theStructure,
328                                        const Graphic3d_TypeOfConnection theType,
329                                        NCollection_Map<Graphic3d_Structure*>& theSet);
330   
331   void SetOwner (const Standard_Address theOwner) { myOwner = theOwner; }
332   
333   Standard_Address Owner() const { return myOwner; }
334   
335   void SetHLRValidation (const Standard_Boolean theFlag) { myCStructure->HLRValidation = theFlag ? 1 : 0; }
336
337   //! Hidden parts stored in this structure are valid if:
338   //! 1) the owner is defined.
339   //! 2) they are not invalid.
340   Standard_Boolean HLRValidation() const
341   {
342     return myOwner != NULL
343         && myCStructure->HLRValidation != 0;
344   }
345
346   //! Return local transformation.
347   const Handle(TopLoc_Datum3D)& Transformation() const { return myCStructure->Transformation(); }
348
349   //! Modifies the current local transformation
350   Standard_EXPORT void SetTransformation (const Handle(TopLoc_Datum3D)& theTrsf);
351
352   Standard_DEPRECATED("This method is deprecated - SetTransformation() should be called instead")
353   void Transform (const Handle(TopLoc_Datum3D)& theTrsf) { SetTransformation (theTrsf); }
354
355   //! Modifies the current transform persistence (pan, zoom or rotate)
356   Standard_EXPORT void SetTransformPersistence (const Handle(Graphic3d_TransformPers)& theTrsfPers);
357
358   //! @return transform persistence of the presentable object.
359   const Handle(Graphic3d_TransformPers)& TransformPersistence() const { return myCStructure->TransformPersistence(); }
360
361   //! Sets if the structure location has mutable nature (content or location will be changed regularly).
362   void SetMutable (const Standard_Boolean theIsMutable) { myCStructure->IsMutable = theIsMutable; }
363   
364   //! Returns true if structure has mutable nature (content or location are be changed regularly).
365   //! Mutable structure will be managed in different way than static onces.
366   Standard_Boolean IsMutable() const { return myCStructure->IsMutable; }
367   
368   Graphic3d_TypeOfStructure ComputeVisual() const { return myComputeVisual; }
369   
370   //! Clears the structure <me>.
371   Standard_EXPORT void GraphicClear (const Standard_Boolean WithDestruction);
372   
373   void GraphicConnect (const Handle(Graphic3d_Structure)& theDaughter) { myCStructure->Connect (*theDaughter->myCStructure); }
374   
375   void GraphicDisconnect (const Handle(Graphic3d_Structure)& theDaughter) { myCStructure->Disconnect (*theDaughter->myCStructure); }
376
377   //! Internal method which sets new transformation without calling graphic manager callbacks.
378   void GraphicTransform (const Handle(TopLoc_Datum3D)& theTrsf) { myCStructure->SetTransformation (theTrsf); }
379
380   //! Returns the identification number of this structure.
381   Standard_Integer Identification() const { return myCStructure->Id; }
382   
383   //! Prints informations about the network associated
384   //! with the structure <AStructure>.
385   Standard_EXPORT static void PrintNetwork (const Handle(Graphic3d_Structure)& AStructure, const Graphic3d_TypeOfConnection AType);
386   
387   //! Suppress the structure in the list of descendants or in the list of ancestors.
388   Standard_EXPORT void Remove (Graphic3d_Structure* thePtr,
389                                const Graphic3d_TypeOfConnection theType);
390   
391   void SetComputeVisual (const Graphic3d_TypeOfStructure theVisual)
392   {
393     // The ComputeVisual is saved only if the structure is declared TOS_ALL, TOS_WIREFRAME or TOS_SHADING.
394     // This declaration permits to calculate proper representation of the structure calculated by Compute instead of passage to TOS_COMPUTED.
395     if (theVisual != Graphic3d_TOS_COMPUTED)
396     {
397       myComputeVisual = theVisual;
398     }
399   }
400   
401   //! Transforms theX, theY, theZ with the transformation theTrsf.
402   Standard_EXPORT static void Transforms (const gp_Trsf& theTrsf,
403                                           const Standard_Real theX, const Standard_Real theY, const Standard_Real theZ,
404                                           Standard_Real& theNewX, Standard_Real& theNewY, Standard_Real& theNewZ);
405
406   //! Returns the low-level structure
407   const Handle(Graphic3d_CStructure)& CStructure() const { return myCStructure; }
408
409   //! Dumps the content of me into the stream
410   Standard_EXPORT virtual void DumpJson (Standard_OStream& theOStream, Standard_Integer theDepth = -1) const;
411
412 protected:
413
414   //! Transforms boundaries with <theTrsf> transformation.
415   Standard_EXPORT static void TransformBoundaries (const gp_Trsf& theTrsf,
416                                                    Standard_Real& theXMin, Standard_Real& theYMin, Standard_Real& theZMin,
417                                                    Standard_Real& theXMax, Standard_Real& theYMax, Standard_Real& theZMax);
418
419   //! Appends new descendant structure.
420   Standard_EXPORT Standard_Boolean AppendDescendant (Graphic3d_Structure* theDescendant);
421   
422   //! Removes the given descendant structure.
423   Standard_EXPORT Standard_Boolean RemoveDescendant (Graphic3d_Structure* theDescendant);
424   
425   //! Appends new ancestor structure.
426   Standard_EXPORT Standard_Boolean AppendAncestor (Graphic3d_Structure* theAncestor);
427   
428   //! Removes the given ancestor structure.
429   Standard_EXPORT Standard_Boolean RemoveAncestor (Graphic3d_Structure* theAncestor);
430
431 private:
432
433   //! Suppress in the structure <me>, the group theGroup.
434   //! It will be erased at the next screen update.
435   Standard_EXPORT void Remove (const Handle(Graphic3d_Group)& theGroup);
436   
437   //! Manages the number of groups in the structure <me>
438   //! which contains facet.
439   //! Polygons, Triangles or Quadrangles.
440   //! <ADelta> = +1 or -1
441   Standard_EXPORT void GroupsWithFacet (const Standard_Integer ADelta);
442   
443   //! Returns the extreme coordinates found in the structure <me> without transformation applied.
444   Standard_EXPORT Graphic3d_BndBox4f minMaxCoord() const;
445   
446   //! Gets untransformed bounding box from structure.
447   Standard_EXPORT void getBox (Graphic3d_BndBox3d& theBox, const Standard_Boolean theToIgnoreInfiniteFlag = Standard_False) const;
448   
449   //! Adds transformed (with myCStructure->Transformation) bounding box of structure to theBox.
450   Standard_EXPORT void addTransformed (Graphic3d_BndBox3d& theBox, const Standard_Boolean theToIgnoreInfiniteFlag = Standard_False) const;
451   
452   //! Returns the manager to which <me> is associated.
453   Standard_EXPORT Handle(Graphic3d_StructureManager) StructureManager() const;
454   
455   //! Calls the Update method of the StructureManager which contains the Structure <me>.
456   //! If theUpdateLayer is true then invalidates bounding box of ZLayer.
457   Standard_EXPORT void Update (const bool theUpdateLayer = false) const;
458
459 protected:
460
461   Graphic3d_StructureManager*   myStructureManager;
462   Handle(Graphic3d_CStructure)  myCStructure;
463   NCollection_IndexedMap<Graphic3d_Structure*> myAncestors;
464   NCollection_IndexedMap<Graphic3d_Structure*> myDescendants;
465   Standard_Address              myOwner;
466   Graphic3d_TypeOfStructure     myVisual;
467   Graphic3d_TypeOfStructure     myComputeVisual;
468
469 };
470
471 #endif // _Graphic3d_Structure_HeaderFile