0031682: Visualization - Prs3d_ShadingAspect::SetTransparency() has no effect with...
[occt.git] / src / SelectMgr / SelectMgr_SortCriterion.hxx
1 // Created on: 1998-03-26
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1998-1999 Matra Datavision
4 // Copyright (c) 1999-2014 OPEN CASCADE SAS
5 //
6 // This file is part of Open CASCADE Technology software library.
7 //
8 // This library is free software; you can redistribute it and/or modify it under
9 // the terms of the GNU Lesser General Public License version 2.1 as published
10 // by the Free Software Foundation, with special exception defined in the file
11 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12 // distribution for complete text of the license and disclaimer of any warranty.
13 //
14 // Alternatively, this file may be used under the terms of Open CASCADE
15 // commercial license or contractual agreement.
16
17 #ifndef _SelectMgr_SortCriterion_HeaderFile
18 #define _SelectMgr_SortCriterion_HeaderFile
19
20 #include <Graphic3d_Vec3.hxx>
21 #include <Graphic3d_ZLayerId.hxx>
22 #include <Precision.hxx>
23 #include <Select3D_SensitiveEntity.hxx>
24
25 //! This class provides data and criterion for sorting candidate
26 //! entities in the process of interactive selection by mouse click
27 class SelectMgr_SortCriterion 
28 {
29 public:
30
31   Handle(Select3D_SensitiveEntity) Entity; //!< detected entity
32   gp_Pnt             Point;           //!< 3D point
33   Graphic3d_Vec3     Normal;          //!< surface normal or 0 vector if undefined
34   Standard_Real      Depth;           //!< distance from the view plane to the entity
35   Standard_Real      MinDist;         //!< distance from the clicked point to the entity on the view plane
36   Standard_Real      Tolerance;       //!< tolerance used for selecting candidates
37   Standard_Integer   Priority;        //!< selection priority
38   Standard_Integer   ZLayerPosition;  //!< ZLayer rendering order index, stronger than a depth
39   Standard_Integer   NbOwnerMatches;  //!< overall number of entities collected for the same owner
40   Standard_Boolean   ToPreferClosest; //!< whether closest object is preferred even if has less priority
41
42 public:
43   DEFINE_STANDARD_ALLOC
44
45   //! Empty constructor.
46   SelectMgr_SortCriterion()
47   : Depth    (0.0),
48     MinDist  (0.0),
49     Tolerance(0.0),
50     Priority (0),
51     ZLayerPosition  (0),
52     NbOwnerMatches  (0),
53     ToPreferClosest (Standard_True) {}
54
55   //! Comparison operator.
56   bool operator> (const SelectMgr_SortCriterion& theOther) const { return IsGreater (theOther); }
57
58   //! Comparison operator.
59   bool operator< (const SelectMgr_SortCriterion& theOther) const { return IsLower   (theOther); }
60
61   //! Compare with another item.
62   bool IsGreater (const SelectMgr_SortCriterion& theOther) const
63   {
64     // the object within different ZLayer groups can not be compared by depth
65     if (ZLayerPosition != theOther.ZLayerPosition)
66     {
67       return ZLayerPosition > theOther.ZLayerPosition;
68     }
69
70     if (ToPreferClosest)
71     {
72       // closest object is selected unless difference is within tolerance
73       if (Abs (Depth - theOther.Depth) > (Tolerance + theOther.Tolerance))
74       {
75         return Depth < theOther.Depth;
76       }
77
78       // if two objects have similar depth, select the one with higher priority
79       if (Priority > theOther.Priority)
80       {
81         return true;
82       }
83
84       // if priorities are equal, one closest to the mouse
85       return Priority == theOther.Priority
86           && MinDist  <  theOther.MinDist;
87     }
88
89     // old logic (OCCT version <= 6.3.1)
90     if (Priority > theOther.Priority)
91     {
92       return true;
93     }
94     else if (Priority != theOther.Priority)
95     {
96       return false;
97     }
98
99     if (Abs (Depth - theOther.Depth) <= Precision::Confusion())
100     {
101       return MinDist < theOther.MinDist;
102     }
103
104     return Depth < theOther.Depth;
105   }
106
107   //! Compare with another item.
108   bool IsLower (const SelectMgr_SortCriterion& theOther) const
109   {
110     // the object within different ZLayer groups can not be compared by depth
111     if (ZLayerPosition != theOther.ZLayerPosition)
112     {
113       return ZLayerPosition < theOther.ZLayerPosition;
114     }
115
116     if (ToPreferClosest)
117     {
118       // closest object is selected unless difference is within tolerance
119       if (ToPreferClosest
120        && Abs (Depth - theOther.Depth) > (Tolerance + theOther.Tolerance))
121       {
122         return Depth > theOther.Depth;
123       }
124
125       // if two objects have similar depth, select the one with higher priority
126       if (Priority < theOther.Priority)
127       {
128         return true;
129       }
130
131       // if priorities are equal, one closest to the mouse
132       return Priority == theOther.Priority
133           && MinDist  >  theOther.MinDist;
134     }
135
136     // old logic (OCCT version <= 6.3.1)
137     if (Priority > theOther.Priority)
138     {
139       return false;
140     }
141     else if (Priority != theOther.Priority)
142     {
143       return true;
144     }
145
146     if (Abs (Depth - theOther.Depth) <= Precision::Confusion())
147     {
148       return MinDist > theOther.MinDist;
149     }
150
151     return Depth > theOther.Depth;
152   }
153
154 };
155
156 #endif // _SelectMgr_SortCriterion_HeaderFile