1 // Copyright (c) 2011-2019 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13 
14 #ifndef _Graphic3d_Layer_HeaderFile
15 #define _Graphic3d_Layer_HeaderFile
16 
17 #include <Graphic3d_BvhCStructureSet.hxx>
18 #include <Graphic3d_BvhCStructureSetTrsfPers.hxx>
19 #include <Graphic3d_Camera.hxx>
20 #include <Graphic3d_ZLayerId.hxx>
21 #include <Graphic3d_ZLayerSettings.hxx>
22 #include <Graphic3d_RenderingParams.hxx>
23 #include <NCollection_Array1.hxx>
24 #include <NCollection_IndexedMap.hxx>
25 #include <NCollection_Sequence.hxx>
26 
27 //! Defines index map of structures.
28 typedef NCollection_IndexedMap<const Graphic3d_CStructure*> Graphic3d_IndexedMapOfStructure;
29 
30 //! Defines array of indexed maps of structures.
31 typedef NCollection_Array1<Graphic3d_IndexedMapOfStructure> Graphic3d_ArrayOfIndexedMapOfStructure;
32 
33 class Graphic3d_CullingTool;
34 
35 //! Presentations list sorted within priorities.
36 class Graphic3d_Layer : public Standard_Transient
37 {
38   DEFINE_STANDARD_RTTIEXT(Graphic3d_Layer, Standard_Transient)
39 public:
40 
41   //! Initializes associated priority list and layer properties
42   Standard_EXPORT Graphic3d_Layer (Graphic3d_ZLayerId theId,
43                                    Standard_Integer theNbPriorities,
44                                    const Handle(Select3D_BVHBuilder3d)& theBuilder);
45 
46   //! Destructor.
47   Standard_EXPORT virtual ~Graphic3d_Layer();
48 
49   //! Return layer id.
LayerId() const50   Graphic3d_ZLayerId LayerId() const { return myLayerId; }
51 
52   //! Returns BVH tree builder for frustom culling.
Handle(Select3D_BVHBuilder3d)53   const Handle(Select3D_BVHBuilder3d)& FrustumCullingBVHBuilder() const { return myBVHPrimitivesTrsfPers.Builder(); }
54 
55   //! Assigns BVH tree builder for frustom culling.
SetFrustumCullingBVHBuilder(const Handle (Select3D_BVHBuilder3d)& theBuilder)56   void SetFrustumCullingBVHBuilder (const Handle(Select3D_BVHBuilder3d)& theBuilder) { myBVHPrimitivesTrsfPers.SetBuilder (theBuilder); }
57 
58   //! Return true if layer was marked with immediate flag.
IsImmediate() const59   Standard_Boolean IsImmediate() const  { return myLayerSettings.IsImmediate(); }
60 
61   //! Returns settings of the layer object.
LayerSettings() const62   const Graphic3d_ZLayerSettings& LayerSettings() const { return myLayerSettings; };
63 
64   //! Sets settings of the layer object.
65   Standard_EXPORT void SetLayerSettings (const Graphic3d_ZLayerSettings& theSettings);
66 
67   Standard_EXPORT void Add (const Graphic3d_CStructure* theStruct,
68                             Standard_Integer thePriority,
69                             Standard_Boolean isForChangePriority = Standard_False);
70 
71   //! Remove structure and returns its priority, if the structure is not found, method returns negative value
72   Standard_EXPORT bool Remove (const Graphic3d_CStructure* theStruct,
73                                Standard_Integer& thePriority,
74                                Standard_Boolean isForChangePriority = Standard_False);
75 
76   //! @return the number of structures
NbStructures() const77   Standard_Integer NbStructures() const { return myNbStructures; }
78 
79   //! Number of NOT culled structures in the layer.
NbStructuresNotCulled() const80   Standard_Integer NbStructuresNotCulled() const { return myNbStructuresNotCulled; }
81 
82   //! Returns the number of available priority levels
NbPriorities() const83   Standard_Integer NbPriorities() const { return myArray.Length(); }
84 
85   //! Append layer of acceptable type (with similar number of priorities or less).
86   //! Returns Standard_False if the list can not be accepted.
87   Standard_EXPORT Standard_Boolean Append (const Graphic3d_Layer& theOther);
88 
89   //! Returns array of structures.
ArrayOfStructures() const90   const Graphic3d_ArrayOfIndexedMapOfStructure& ArrayOfStructures() const { return myArray; }
91 
92   //! Marks BVH tree for given priority list as dirty and
93   //! marks primitive set for rebuild.
94   Standard_EXPORT void InvalidateBVHData();
95 
96   //! Marks cached bounding box as obsolete.
InvalidateBoundingBox() const97   void InvalidateBoundingBox() const
98   {
99     myIsBoundingBoxNeedsReset[0] = myIsBoundingBoxNeedsReset[1] = true;
100   }
101 
102   //! Returns layer bounding box.
103   //! @param theViewId             view index to consider View Affinity in structure
104   //! @param theCamera             camera definition
105   //! @param theWindowWidth        viewport width  (for applying transformation-persistence)
106   //! @param theWindowHeight       viewport height (for applying transformation-persistence)
107   //! @param theToIncludeAuxiliary consider also auxiliary presentations (with infinite flag or with trihedron transformation persistence)
108   //! @return computed bounding box
109   Standard_EXPORT Bnd_Box BoundingBox (Standard_Integer theViewId,
110                                        const Handle(Graphic3d_Camera)& theCamera,
111                                        Standard_Integer theWindowWidth,
112                                        Standard_Integer theWindowHeight,
113                                        Standard_Boolean theToIncludeAuxiliary) const;
114 
115   //! Returns zoom-scale factor.
116   Standard_EXPORT Standard_Real considerZoomPersistenceObjects (Standard_Integer theViewId,
117                                                                 const Handle(Graphic3d_Camera)& theCamera,
118                                                                 Standard_Integer theWindowWidth,
119                                                                 Standard_Integer theWindowHeight) const;
120 
121   //! Update culling state - should be called before rendering.
122   //! Traverses through BVH tree to determine which structures are in view volume.
123   Standard_EXPORT void UpdateCulling (Standard_Integer theViewId,
124                                       const Graphic3d_CullingTool& theSelector,
125                                       const Graphic3d_RenderingParams::FrustumCulling theFrustumCullingState);
126 
127   //! Returns TRUE if layer is empty or has been discarded entirely by culling test.
IsCulled() const128   bool IsCulled() const { return myNbStructuresNotCulled == 0; }
129 
130   //! Returns number of transform persistence objects.
NbOfTransformPersistenceObjects() const131   Standard_Integer NbOfTransformPersistenceObjects() const
132   {
133     return myBVHPrimitivesTrsfPers.Size();
134   }
135 
136 public:
137 
138   //! Returns set of Graphic3d_CStructures structures for building BVH tree.
CullableStructuresBVH() const139   const Graphic3d_BvhCStructureSet& CullableStructuresBVH() const { return myBVHPrimitives; }
140 
141   //! Returns set of transform persistent Graphic3d_CStructures for building BVH tree.
CullableTrsfPersStructuresBVH() const142   const Graphic3d_BvhCStructureSetTrsfPers& CullableTrsfPersStructuresBVH() const { return myBVHPrimitivesTrsfPers; }
143 
144   //! Returns indexed map of always rendered structures.
NonCullableStructures() const145   const NCollection_IndexedMap<const Graphic3d_CStructure*>& NonCullableStructures() const { return myAlwaysRenderedMap; }
146 
147 protected:
148 
149   //! Updates BVH trees if their state has been invalidated.
150   Standard_EXPORT void updateBVH() const;
151 
152 private:
153 
154   //! Array of Graphic3d_CStructures by priority rendered in layer.
155   Graphic3d_ArrayOfIndexedMapOfStructure myArray;
156 
157   //! Overall number of structures rendered in the layer.
158   Standard_Integer myNbStructures;
159 
160   //! Number of NOT culled structures in the layer.
161   Standard_Integer myNbStructuresNotCulled;
162 
163   //! Layer setting flags.
164   Graphic3d_ZLayerSettings myLayerSettings;
165 
166   //! Layer id.
167   Graphic3d_ZLayerId myLayerId;
168 
169   //! Set of Graphic3d_CStructures structures for building BVH tree.
170   mutable Graphic3d_BvhCStructureSet myBVHPrimitives;
171 
172   //! Set of transform persistent Graphic3d_CStructures for building BVH tree.
173   mutable Graphic3d_BvhCStructureSetTrsfPers myBVHPrimitivesTrsfPers;
174 
175   //! Indexed map of always rendered structures.
176   mutable NCollection_IndexedMap<const Graphic3d_CStructure*> myAlwaysRenderedMap;
177 
178   //! Is needed for implementation of stochastic order of BVH traverse.
179   Standard_Boolean myBVHIsLeftChildQueuedFirst;
180 
181   //! Defines if the primitive set for BVH is outdated.
182   mutable Standard_Boolean myIsBVHPrimitivesNeedsReset;
183 
184   //! Defines if the cached bounding box is outdated.
185   mutable bool myIsBoundingBoxNeedsReset[2];
186 
187   //! Cached layer bounding box.
188   mutable Bnd_Box myBoundingBox[2];
189 
190 };
191 
192 #endif // _Graphic3d_Layer_HeaderFile
193