1 // Created on: 2015-06-30
2 // Created by: Anton POLETAEV
3 // Copyright (c) 2015 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
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.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <OpenGl_BVHClipPrimitiveTrsfPersSet.hxx>
17 #include <BVH_LinearBuilder.hxx>
19 // =======================================================================
20 // function : OpenGl_BVHClipPrimitiveTrsfPersSet
22 // =======================================================================
23 OpenGl_BVHClipPrimitiveTrsfPersSet::OpenGl_BVHClipPrimitiveTrsfPersSet()
24 : myIsDirty (Standard_False),
25 myBVH (new BVH_Tree<Standard_Real, 3>())
27 myBuilder = new BVH_LinearBuilder<Standard_Real, 3> (1, 32);
30 // =======================================================================
33 // =======================================================================
34 Standard_Integer OpenGl_BVHClipPrimitiveTrsfPersSet::Size() const
36 return myStructs.Size();
39 // =======================================================================
42 // =======================================================================
43 Graphic3d_BndBox3d OpenGl_BVHClipPrimitiveTrsfPersSet::Box (const Standard_Integer theIdx) const
45 return *myStructBoxes (theIdx + 1);
48 // =======================================================================
51 // =======================================================================
52 Standard_Real OpenGl_BVHClipPrimitiveTrsfPersSet::Center (const Standard_Integer theIdx,
53 const Standard_Integer theAxis) const
55 const Graphic3d_BndBox3d& aBndBox = *myStructBoxes (theIdx + 1);
56 return (aBndBox.CornerMin()[theAxis] + aBndBox.CornerMax()[theAxis]) * 0.5;
59 // =======================================================================
62 // =======================================================================
63 void OpenGl_BVHClipPrimitiveTrsfPersSet::Swap (const Standard_Integer theIdx1,
64 const Standard_Integer theIdx2)
66 const Standard_Integer aStructIdx1 = theIdx1 + 1;
67 const Standard_Integer aStructIdx2 = theIdx2 + 1;
69 myStructs .Swap (aStructIdx1, aStructIdx2);
70 myStructBoxes.Swap (aStructIdx1, aStructIdx2);
73 // =======================================================================
76 // =======================================================================
77 Standard_Boolean OpenGl_BVHClipPrimitiveTrsfPersSet::Add (const OpenGl_Structure* theStruct)
79 const Standard_Integer aSize = myStructs.Size();
81 if (myStructs.Add (theStruct) > aSize) // new structure?
88 return Standard_False;
91 // =======================================================================
94 // =======================================================================
95 Standard_Boolean OpenGl_BVHClipPrimitiveTrsfPersSet::Remove (const OpenGl_Structure* theStruct)
97 const Standard_Integer anIndex = myStructs.FindIndex (theStruct);
101 myStructs.Swap (Size(), anIndex);
102 myStructs.RemoveLast();
105 return Standard_True;
108 return Standard_False;
111 // =======================================================================
114 // =======================================================================
115 void OpenGl_BVHClipPrimitiveTrsfPersSet::Clear()
121 // =======================================================================
122 // function : GetStructureById
124 // =======================================================================
125 const OpenGl_Structure* OpenGl_BVHClipPrimitiveTrsfPersSet::GetStructureById (Standard_Integer theId)
127 return myStructs.FindKey (theId + 1);
130 //=======================================================================
133 //=======================================================================
134 const NCollection_Handle<BVH_Tree<Standard_Real, 3> >&
135 OpenGl_BVHClipPrimitiveTrsfPersSet::BVH (const Handle(Graphic3d_Camera)& theCamera,
136 const OpenGl_Mat4d& theProjectionMatrix,
137 const OpenGl_Mat4d& theWorldViewMatrix,
138 const Standard_Integer theViewportWidth,
139 const Standard_Integer theViewportHeight,
140 const Graphic3d_WorldViewProjState& theWVPState)
143 && (myStructBoxesState.IsValid()
144 && !myStructBoxesState.IsChanged (theWVPState)))
149 myStructBoxes.ReSize (myStructs.Size());
151 for (Standard_Integer aStructIdx = 1; aStructIdx <= myStructs.Size(); ++aStructIdx)
153 const OpenGl_Structure* aStructure = myStructs (aStructIdx);
155 Handle(HBndBox3d) aBoundingBox = new HBndBox3d();
156 *aBoundingBox = aStructure->BoundingBox();
157 if (!aStructure->TransformPersistence().IsNull())
159 aStructure->TransformPersistence()->Apply (theCamera, theProjectionMatrix, theWorldViewMatrix, theViewportWidth, theViewportHeight, *aBoundingBox);
162 myStructBoxes.Add (aBoundingBox);
165 myBuilder->Build (this, myBVH.operator->(), BVH_Set<Standard_Real, 3>::Box());
167 myStructBoxesState = theWVPState;
168 myStructBoxes.Clear();
169 myIsDirty = Standard_False;