42cf5bc1 |
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 | |
1593b4ee |
20 | #include <Graphic3d_ZLayerId.hxx> |
21 | #include <Precision.hxx> |
aa75c0cf |
22 | #include <SelectBasics_SensitiveEntity.hxx> |
42cf5bc1 |
23 | |
24 | //! This class provides data and criterion for sorting candidate |
25 | //! entities in the process of interactive selection by mouse click |
26 | class SelectMgr_SortCriterion |
27 | { |
28 | public: |
29 | |
aa75c0cf |
30 | Handle(SelectBasics_SensitiveEntity) Entity; //!< detected entity |
31 | gp_Pnt Point; //!< 3D point |
1593b4ee |
32 | Standard_Real Depth; //!< distance from the view plane to the entity |
33 | Standard_Real MinDist; //!< distance from the clicked point to the entity on the view plane |
34 | Standard_Real Tolerance; //!< tolerance used for selecting candidates |
35 | Standard_Integer Priority; //!< selection priority |
36 | Standard_Integer ZLayerPosition; //!< ZLayer rendering order index, stronger than a depth |
37 | Standard_Boolean ToPreferClosest; //!< whether closest object is preferred even if has less priority |
42cf5bc1 |
38 | |
1593b4ee |
39 | public: |
40 | DEFINE_STANDARD_ALLOC |
42cf5bc1 |
41 | |
1593b4ee |
42 | //! Empty constructor. |
43 | SelectMgr_SortCriterion() |
44 | : Depth (0.0), |
45 | MinDist (0.0), |
46 | Tolerance(0.0), |
47 | Priority (0), |
48 | ZLayerPosition (0), |
49 | ToPreferClosest (Standard_True) {} |
50 | |
51 | //! Comparison operator. |
52 | bool operator> (const SelectMgr_SortCriterion& theOther) const { return IsGreater (theOther); } |
53 | |
54 | //! Comparison operator. |
55 | bool operator< (const SelectMgr_SortCriterion& theOther) const { return IsLower (theOther); } |
56 | |
57 | //! Compare with another item. |
58 | bool IsGreater (const SelectMgr_SortCriterion& theOther) const |
59 | { |
60 | // the object within different ZLayer groups can not be compared by depth |
61 | if (ZLayerPosition != theOther.ZLayerPosition) |
62 | { |
63 | return ZLayerPosition > theOther.ZLayerPosition; |
64 | } |
65 | |
66 | if (ToPreferClosest) |
67 | { |
68 | // closest object is selected unless difference is within tolerance |
69 | if (Abs (Depth - theOther.Depth) > (Tolerance + theOther.Tolerance)) |
70 | { |
71 | return Depth < theOther.Depth; |
72 | } |
73 | |
74 | // if two objects have similar depth, select the one with higher priority |
75 | if (Priority > theOther.Priority) |
76 | { |
77 | return true; |
78 | } |
79 | |
80 | // if priorities are equal, one closest to the mouse |
81 | return Priority == theOther.Priority |
82 | && MinDist < theOther.MinDist; |
83 | } |
84 | |
85 | // old logic (OCCT version <= 6.3.1) |
86 | if (Priority > theOther.Priority) |
87 | { |
88 | return true; |
89 | } |
90 | else if (Priority != theOther.Priority) |
91 | { |
92 | return false; |
93 | } |
94 | |
95 | if (Abs (Depth - theOther.Depth) <= Precision::Confusion()) |
96 | { |
97 | return MinDist < theOther.MinDist; |
98 | } |
99 | |
100 | return Depth < theOther.Depth; |
101 | } |
102 | |
103 | //! Compare with another item. |
104 | bool IsLower (const SelectMgr_SortCriterion& theOther) const |
105 | { |
106 | // the object within different ZLayer groups can not be compared by depth |
107 | if (ZLayerPosition != theOther.ZLayerPosition) |
108 | { |
109 | return ZLayerPosition < theOther.ZLayerPosition; |
110 | } |
111 | |
112 | if (ToPreferClosest) |
113 | { |
114 | // closest object is selected unless difference is within tolerance |
115 | if (ToPreferClosest |
116 | && Abs (Depth - theOther.Depth) > (Tolerance + theOther.Tolerance)) |
117 | { |
118 | return Depth > theOther.Depth; |
119 | } |
120 | |
121 | // if two objects have similar depth, select the one with higher priority |
122 | if (Priority < theOther.Priority) |
123 | { |
124 | return true; |
125 | } |
126 | |
127 | // if priorities are equal, one closest to the mouse |
128 | return Priority == theOther.Priority |
129 | && MinDist > theOther.MinDist; |
130 | } |
131 | |
132 | // old logic (OCCT version <= 6.3.1) |
133 | if (Priority > theOther.Priority) |
134 | { |
135 | return false; |
136 | } |
137 | else if (Priority != theOther.Priority) |
138 | { |
139 | return true; |
140 | } |
141 | |
142 | if (Abs (Depth - theOther.Depth) <= Precision::Confusion()) |
143 | { |
144 | return MinDist > theOther.MinDist; |
145 | } |
146 | |
147 | return Depth > theOther.Depth; |
148 | } |
42cf5bc1 |
149 | |
150 | }; |
151 | |
42cf5bc1 |
152 | #endif // _SelectMgr_SortCriterion_HeaderFile |