0029516: Visualization - eliminate redundant property Graphic3d_MaterialAspect::Refle...
[occt.git] / src / Graphic3d / Graphic3d_BvhCStructureSetTrsfPers.cxx
CommitLineData
39fb68ad 1// Created on: 2015-06-30
825aa485 2// Created by: Anton POLETAEV
3// Copyright (c) 2015 OPEN CASCADE SAS
4//
5// This file is part of Open CASCADE Technology software library.
6//
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.
12//
13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
15
d325cb7f 16#include <Graphic3d_BvhCStructureSetTrsfPers.hxx>
17
18#include <Graphic3d_CStructure.hxx>
825aa485 19
20// =======================================================================
d325cb7f 21// function : Graphic3d_BvhCStructureSetTrsfPers
825aa485 22// purpose :
23// =======================================================================
d325cb7f 24Graphic3d_BvhCStructureSetTrsfPers::Graphic3d_BvhCStructureSetTrsfPers (const Handle(Select3D_BVHBuilder3d)& theBuilder)
825aa485 25: myIsDirty (Standard_False),
f5b72419 26 myBVH (new BVH_Tree<Standard_Real, 3>()),
27 myBuilder (theBuilder)
825aa485 28{
f5b72419 29 //
825aa485 30}
31
32// =======================================================================
33// function : Size
34// purpose :
35// =======================================================================
d325cb7f 36Standard_Integer Graphic3d_BvhCStructureSetTrsfPers::Size() const
825aa485 37{
38 return myStructs.Size();
39}
40
41// =======================================================================
42// function : Box
43// purpose :
44// =======================================================================
d325cb7f 45Graphic3d_BndBox3d Graphic3d_BvhCStructureSetTrsfPers::Box (const Standard_Integer theIdx) const
825aa485 46{
47 return *myStructBoxes (theIdx + 1);
48}
49
50// =======================================================================
51// function : Center
52// purpose :
53// =======================================================================
d325cb7f 54Standard_Real Graphic3d_BvhCStructureSetTrsfPers::Center (const Standard_Integer theIdx,
7c3ef2f7 55 const Standard_Integer theAxis) const
825aa485 56{
7c3ef2f7 57 const Graphic3d_BndBox3d& aBndBox = *myStructBoxes (theIdx + 1);
58 return (aBndBox.CornerMin()[theAxis] + aBndBox.CornerMax()[theAxis]) * 0.5;
825aa485 59}
60
61// =======================================================================
62// function : Swap
63// purpose :
64// =======================================================================
d325cb7f 65void Graphic3d_BvhCStructureSetTrsfPers::Swap (const Standard_Integer theIdx1,
825aa485 66 const Standard_Integer theIdx2)
67{
68 const Standard_Integer aStructIdx1 = theIdx1 + 1;
69 const Standard_Integer aStructIdx2 = theIdx2 + 1;
70
97f937cc 71 myStructs .Swap (aStructIdx1, aStructIdx2);
825aa485 72 myStructBoxes.Swap (aStructIdx1, aStructIdx2);
73}
74
75// =======================================================================
76// function : Add
77// purpose :
78// =======================================================================
d325cb7f 79Standard_Boolean Graphic3d_BvhCStructureSetTrsfPers::Add (const Graphic3d_CStructure* theStruct)
825aa485 80{
81 const Standard_Integer aSize = myStructs.Size();
82
83 if (myStructs.Add (theStruct) > aSize) // new structure?
84 {
85 MarkDirty();
86
87 return Standard_True;
88 }
89
90 return Standard_False;
91}
92
93// =======================================================================
94// function : Remove
95// purpose :
96// =======================================================================
d325cb7f 97Standard_Boolean Graphic3d_BvhCStructureSetTrsfPers::Remove (const Graphic3d_CStructure* theStruct)
825aa485 98{
99 const Standard_Integer anIndex = myStructs.FindIndex (theStruct);
100
101 if (anIndex != 0)
102 {
103 myStructs.Swap (Size(), anIndex);
104 myStructs.RemoveLast();
105 MarkDirty();
106
107 return Standard_True;
108 }
109
110 return Standard_False;
111}
112
113// =======================================================================
114// function : Clear
115// purpose :
116// =======================================================================
d325cb7f 117void Graphic3d_BvhCStructureSetTrsfPers::Clear()
825aa485 118{
119 myStructs.Clear();
120 MarkDirty();
121}
122
123// =======================================================================
124// function : GetStructureById
125// purpose :
126// =======================================================================
d325cb7f 127const Graphic3d_CStructure* Graphic3d_BvhCStructureSetTrsfPers::GetStructureById (Standard_Integer theId)
825aa485 128{
129 return myStructs.FindKey (theId + 1);
130}
131
132//=======================================================================
133// function : BVH
134// purpose :
135//=======================================================================
d325cb7f 136const 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)
825aa485 142{
97f937cc 143 if (!myIsDirty
144 && (myStructBoxesState.IsValid()
145 && !myStructBoxesState.IsChanged (theWVPState)))
825aa485 146 {
147 return myBVH;
148 }
149
150 myStructBoxes.ReSize (myStructs.Size());
151
152 for (Standard_Integer aStructIdx = 1; aStructIdx <= myStructs.Size(); ++aStructIdx)
153 {
d325cb7f 154 const Graphic3d_CStructure* aStructure = myStructs (aStructIdx);
825aa485 155
7c3ef2f7 156 Handle(HBndBox3d) aBoundingBox = new HBndBox3d();
97f937cc 157 *aBoundingBox = aStructure->BoundingBox();
778cd667 158 if (!aStructure->TransformPersistence().IsNull())
159 {
160 aStructure->TransformPersistence()->Apply (theCamera, theProjectionMatrix, theWorldViewMatrix, theViewportWidth, theViewportHeight, *aBoundingBox);
161 }
825aa485 162
163 myStructBoxes.Add (aBoundingBox);
164 }
165
7c3ef2f7 166 myBuilder->Build (this, myBVH.operator->(), BVH_Set<Standard_Real, 3>::Box());
825aa485 167
168 myStructBoxesState = theWVPState;
169 myStructBoxes.Clear();
170 myIsDirty = Standard_False;
171
172 return myBVH;
173}