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 <Graphic3d_BvhCStructureSetTrsfPers.hxx>
18 #include <Graphic3d_CStructure.hxx>
20 // =======================================================================
21 // function : Graphic3d_BvhCStructureSetTrsfPers
23 // =======================================================================
24 Graphic3d_BvhCStructureSetTrsfPers::Graphic3d_BvhCStructureSetTrsfPers (const Handle(Select3D_BVHBuilder3d)& theBuilder)
25 : myIsDirty (Standard_False),
26 myBVH (new BVH_Tree<Standard_Real, 3>()),
27 myBuilder (theBuilder)
32 // =======================================================================
35 // =======================================================================
36 Standard_Integer Graphic3d_BvhCStructureSetTrsfPers::Size() const
38 return myStructs.Size();
41 // =======================================================================
44 // =======================================================================
45 Graphic3d_BndBox3d Graphic3d_BvhCStructureSetTrsfPers::Box (const Standard_Integer theIdx) const
47 return *myStructBoxes (theIdx + 1);
50 // =======================================================================
53 // =======================================================================
54 Standard_Real Graphic3d_BvhCStructureSetTrsfPers::Center (const Standard_Integer theIdx,
55 const Standard_Integer theAxis) const
57 const Graphic3d_BndBox3d& aBndBox = *myStructBoxes (theIdx + 1);
58 return (aBndBox.CornerMin()[theAxis] + aBndBox.CornerMax()[theAxis]) * 0.5;
61 // =======================================================================
64 // =======================================================================
65 void Graphic3d_BvhCStructureSetTrsfPers::Swap (const Standard_Integer theIdx1,
66 const Standard_Integer theIdx2)
68 const Standard_Integer aStructIdx1 = theIdx1 + 1;
69 const Standard_Integer aStructIdx2 = theIdx2 + 1;
71 myStructs .Swap (aStructIdx1, aStructIdx2);
72 myStructBoxes.Swap (aStructIdx1, aStructIdx2);
75 // =======================================================================
78 // =======================================================================
79 Standard_Boolean Graphic3d_BvhCStructureSetTrsfPers::Add (const Graphic3d_CStructure* theStruct)
81 const Standard_Integer aSize = myStructs.Size();
83 if (myStructs.Add (theStruct) > aSize) // new structure?
90 return Standard_False;
93 // =======================================================================
96 // =======================================================================
97 Standard_Boolean Graphic3d_BvhCStructureSetTrsfPers::Remove (const Graphic3d_CStructure* theStruct)
99 const Standard_Integer anIndex = myStructs.FindIndex (theStruct);
103 myStructs.Swap (Size(), anIndex);
104 myStructs.RemoveLast();
107 return Standard_True;
110 return Standard_False;
113 // =======================================================================
116 // =======================================================================
117 void Graphic3d_BvhCStructureSetTrsfPers::Clear()
123 // =======================================================================
124 // function : GetStructureById
126 // =======================================================================
127 const Graphic3d_CStructure* Graphic3d_BvhCStructureSetTrsfPers::GetStructureById (Standard_Integer theId)
129 return myStructs.FindKey (theId + 1);
132 //=======================================================================
135 //=======================================================================
136 const opencascade::handle<BVH_Tree<Standard_Real, 3> >& Graphic3d_BvhCStructureSetTrsfPers::BVH (const Handle(Graphic3d_Camera)& theCamera,
137 const Graphic3d_Mat4d& theProjectionMatrix,
138 const Graphic3d_Mat4d& theWorldViewMatrix,
139 const Standard_Integer theViewportWidth,
140 const Standard_Integer theViewportHeight,
141 const Graphic3d_WorldViewProjState& theWVPState)
144 && (myStructBoxesState.IsValid()
145 && !myStructBoxesState.IsChanged (theWVPState)))
150 myStructBoxes.ReSize (myStructs.Size());
152 for (Standard_Integer aStructIdx = 1; aStructIdx <= myStructs.Size(); ++aStructIdx)
154 const Graphic3d_CStructure* aStructure = myStructs (aStructIdx);
156 Handle(HBndBox3d) aBoundingBox = new HBndBox3d();
157 *aBoundingBox = aStructure->BoundingBox();
158 if (!aStructure->TransformPersistence().IsNull())
160 aStructure->TransformPersistence()->Apply (theCamera, theProjectionMatrix, theWorldViewMatrix, theViewportWidth, theViewportHeight, *aBoundingBox);
163 myStructBoxes.Add (aBoundingBox);
166 myBuilder->Build (this, myBVH.operator->(), BVH_Set<Standard_Real, 3>::Box());
168 myStructBoxesState = theWVPState;
169 myStructBoxes.Clear();
170 myIsDirty = Standard_False;