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_ShortReal, 4>())
27 myBuilder = new BVH_LinearBuilder<Standard_ShortReal, 4> (1, 32);
30 // =======================================================================
33 // =======================================================================
34 Standard_Integer OpenGl_BVHClipPrimitiveTrsfPersSet::Size() const
36 return myStructs.Size();
39 // =======================================================================
42 // =======================================================================
43 Graphic3d_BndBox4f OpenGl_BVHClipPrimitiveTrsfPersSet::Box (const Standard_Integer theIdx) const
45 return *myStructBoxes (theIdx + 1);
48 // =======================================================================
51 // =======================================================================
52 Standard_ShortReal OpenGl_BVHClipPrimitiveTrsfPersSet::Center (const Standard_Integer theIdx,
53 const Standard_Integer theAxis) const
55 const Graphic3d_BndBox4f& aBndBox = *myStructBoxes (theIdx + 1);
57 return (aBndBox.CornerMin()[theAxis] + aBndBox.CornerMax()[theAxis]) * 0.5f;
60 // =======================================================================
63 // =======================================================================
64 void OpenGl_BVHClipPrimitiveTrsfPersSet::Swap (const Standard_Integer theIdx1,
65 const Standard_Integer theIdx2)
67 const Standard_Integer aStructIdx1 = theIdx1 + 1;
68 const Standard_Integer aStructIdx2 = theIdx2 + 1;
70 myStructs .Swap (aStructIdx1, aStructIdx2);
71 myStructBoxes.Swap (aStructIdx1, aStructIdx2);
74 // =======================================================================
77 // =======================================================================
78 Standard_Boolean OpenGl_BVHClipPrimitiveTrsfPersSet::Add (const OpenGl_Structure* theStruct)
80 const Standard_Integer aSize = myStructs.Size();
82 if (myStructs.Add (theStruct) > aSize) // new structure?
89 return Standard_False;
92 // =======================================================================
95 // =======================================================================
96 Standard_Boolean OpenGl_BVHClipPrimitiveTrsfPersSet::Remove (const OpenGl_Structure* theStruct)
98 const Standard_Integer anIndex = myStructs.FindIndex (theStruct);
102 myStructs.Swap (Size(), anIndex);
103 myStructs.RemoveLast();
106 return Standard_True;
109 return Standard_False;
112 // =======================================================================
115 // =======================================================================
116 void OpenGl_BVHClipPrimitiveTrsfPersSet::Clear()
122 // =======================================================================
123 // function : GetStructureById
125 // =======================================================================
126 const OpenGl_Structure* OpenGl_BVHClipPrimitiveTrsfPersSet::GetStructureById (Standard_Integer theId)
128 return myStructs.FindKey (theId + 1);
131 //=======================================================================
134 //=======================================================================
135 const NCollection_Handle<BVH_Tree<Standard_ShortReal, 4> >&
136 OpenGl_BVHClipPrimitiveTrsfPersSet::BVH (const OpenGl_Mat4& theProjectionMatrix,
137 const OpenGl_Mat4& 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 HBndBox4f aBoundingBox = new Graphic3d_BndBox4f;
156 *aBoundingBox = aStructure->BoundingBox();
157 aStructure->TransformPersistence.Apply (theProjectionMatrix, theWorldViewMatrix, theViewportWidth, theViewportHeight, *aBoundingBox);
159 myStructBoxes.Add (aBoundingBox);
162 myBuilder->Build (this, myBVH.operator->(), BVH_Set<Standard_ShortReal, 4>::Box());
164 myStructBoxesState = theWVPState;
165 myStructBoxes.Clear();
166 myIsDirty = Standard_False;