575d376f4974d32f15e78a883ebf15bfff1ce6c9
[occt.git] / src / SelectMgr / SelectMgr_ViewClipRange.hxx
1 // Created on: 2015-12-15
2 // Created by: Varvara POSKONINA
3 // Copyright (c) 2005-2014 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
16 #ifndef _SelectMgr_ViewClipRange_HeaderFile
17 #define _SelectMgr_ViewClipRange_HeaderFile
18
19 #include <Bnd_Range.hxx>
20 #include <Standard_TypeDef.hxx>
21
22 //! Class for handling depth clipping range.
23 //! It is used to perform checks in case if global (for the whole view)
24 //! clipping planes are defined inside of SelectMgr_RectangularFrustum class methods.
25 class SelectMgr_ViewClipRange
26 {
27 public:
28   //! Creates an empty clip range.
29   SelectMgr_ViewClipRange()
30   {
31     SetVoid();
32   }
33
34   //! Check if the given depth is not within clipping range(s),
35   //! e.g. TRUE means depth is clipped.
36   Standard_Boolean IsClipped (const Standard_Real theDepth) const
37   {
38     if (myUnclipRange.IsOut (theDepth))
39     {
40       return Standard_True;
41     }
42     for (size_t aRangeIter = 0; aRangeIter < myClipRanges.size(); ++aRangeIter)
43     {
44       if (!myClipRanges[aRangeIter].IsOut (theDepth))
45       {
46         return Standard_True;
47       }
48     }
49     return Standard_False;
50   }
51   
52   //! Calculates the min not clipped value from the range.
53   //! Returns FALSE if the whole range is clipped.
54   Standard_Boolean GetNearestDepth (const Bnd_Range& theRange, Standard_Real& theDepth) const
55   {
56     if (!myUnclipRange.IsVoid() && myUnclipRange.IsOut (theRange))
57     {
58       return false;
59     }
60
61     Bnd_Range aCommonClipRange;
62     theRange.GetMin (theDepth);
63
64     if (!myUnclipRange.IsVoid() && myUnclipRange.IsOut (theDepth))
65     {
66       myUnclipRange.GetMin (theDepth);
67     }
68
69     for (size_t aRangeIter = 0; aRangeIter < myClipRanges.size(); ++aRangeIter)
70     {
71       if (!myClipRanges[aRangeIter].IsOut (theDepth))
72       {
73         aCommonClipRange = myClipRanges[aRangeIter];
74         break;
75       }
76     }
77
78     if (aCommonClipRange.IsVoid())
79     {
80       return true;
81     }
82
83     for (size_t aRangeIter = 0; aRangeIter < myClipRanges.size(); ++aRangeIter)
84     {
85       if (!aCommonClipRange.IsOut (myClipRanges[aRangeIter]))
86       {
87         aCommonClipRange.Add (myClipRanges[aRangeIter]);
88       }
89     }
90
91     aCommonClipRange.GetMax (theDepth);
92
93     return !theRange.IsOut (theDepth);
94   }
95
96   //! Clears clipping range.
97   void SetVoid()
98   {
99     myClipRanges.resize (0);
100     myUnclipRange = Bnd_Range (RealFirst(), RealLast());
101   }
102
103   //! Returns the main unclipped range; [-inf, inf] by default.
104   Bnd_Range& ChangeUnclipRange() { return myUnclipRange; }
105
106   //! Adds a clipping sub-range (for clipping chains).
107   void AddClipSubRange (const Bnd_Range& theRange) { myClipRanges.push_back (theRange); }
108
109 private:
110
111   std::vector<Bnd_Range> myClipRanges;
112   Bnd_Range myUnclipRange;
113
114 };
115
116 #endif // _SelectMgr_ViewClipRange_HeaderFile