16e5949778d9d0926b31fecc7932366cc8019a3d
[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 #include <Standard_OStream.hxx>
22 #include <Standard_Dump.hxx>
23
24 #include <vector>
25
26 class gp_Ax1;
27 class Graphic3d_SequenceOfHClipPlane;
28
29 //! Class for handling depth clipping range.
30 //! It is used to perform checks in case if global (for the whole view)
31 //! clipping planes are defined inside of SelectMgr_RectangularFrustum class methods.
32 class SelectMgr_ViewClipRange
33 {
34 public:
35   //! Creates an empty clip range.
36   SelectMgr_ViewClipRange()
37   {
38     SetVoid();
39   }
40
41   //! Check if the given depth is not within clipping range(s),
42   //! e.g. TRUE means depth is clipped.
43   Standard_Boolean IsClipped (const Standard_Real theDepth) const
44   {
45     if (myUnclipRange.IsOut (theDepth))
46     {
47       return Standard_True;
48     }
49     for (size_t aRangeIter = 0; aRangeIter < myClipRanges.size(); ++aRangeIter)
50     {
51       if (!myClipRanges[aRangeIter].IsOut (theDepth))
52       {
53         return Standard_True;
54       }
55     }
56     return Standard_False;
57   }
58   
59   //! Calculates the min not clipped value from the range.
60   //! Returns FALSE if the whole range is clipped.
61   Standard_Boolean GetNearestDepth (const Bnd_Range& theRange, Standard_Real& theDepth) const
62   {
63     if (!myUnclipRange.IsVoid() && myUnclipRange.IsOut (theRange))
64     {
65       return false;
66     }
67
68     Bnd_Range aCommonClipRange;
69     theRange.GetMin (theDepth);
70
71     if (!myUnclipRange.IsVoid() && myUnclipRange.IsOut (theDepth))
72     {
73       myUnclipRange.GetMin (theDepth);
74     }
75
76     for (size_t aRangeIter = 0; aRangeIter < myClipRanges.size(); ++aRangeIter)
77     {
78       if (!myClipRanges[aRangeIter].IsOut (theDepth))
79       {
80         aCommonClipRange = myClipRanges[aRangeIter];
81         break;
82       }
83     }
84
85     if (aCommonClipRange.IsVoid())
86     {
87       return true;
88     }
89
90     for (size_t aRangeIter = 0; aRangeIter < myClipRanges.size(); ++aRangeIter)
91     {
92       if (!aCommonClipRange.IsOut (myClipRanges[aRangeIter]))
93       {
94         aCommonClipRange.Add (myClipRanges[aRangeIter]);
95       }
96     }
97
98     aCommonClipRange.GetMax (theDepth);
99
100     return !theRange.IsOut (theDepth);
101   }
102
103 public:
104
105   //! Clears clipping range.
106   void SetVoid()
107   {
108     myClipRanges.resize (0);
109     myUnclipRange = Bnd_Range (RealFirst(), RealLast());
110   }
111
112   //! Add clipping planes. Planes and picking ray should be defined in the same coordinate system.
113   Standard_EXPORT void AddClippingPlanes (const Graphic3d_SequenceOfHClipPlane& thePlanes,
114                                           const gp_Ax1& thePickRay);
115
116   //! Returns the main unclipped range; [-inf, inf] by default.
117   Bnd_Range& ChangeUnclipRange() { return myUnclipRange; }
118
119   //! Adds a clipping sub-range (for clipping chains).
120   void AddClipSubRange (const Bnd_Range& theRange) { myClipRanges.push_back (theRange); }
121
122   //! Dumps the content of me into the stream
123   Standard_EXPORT void DumpJson (Standard_OStream& theOStream, const Standard_Integer theDepth = -1) const;
124
125 private:
126
127   std::vector<Bnd_Range> myClipRanges;
128   Bnd_Range myUnclipRange;
129
130 };
131
132 #endif // _SelectMgr_ViewClipRange_HeaderFile