8ccf511c53f99c834c1316528bcb154d71dc1d6b
[occt.git] / src / Select3D / Select3D_SensitiveBox.cxx
1 // Created on: 1995-04-13
2 // Created by: Robert COUBLANC
3 // Copyright (c) 1995-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 #include <Select3D_SensitiveBox.ixx>
18 #include <gp_Pnt2d.hxx>
19 #include <gp_Pnt.hxx>
20 #include <Bnd_Box.hxx>
21 #include <ElCLib.hxx>
22
23
24
25 //==================================================
26 // Function: Constructor
27 // Purpose :
28 //==================================================
29
30 Select3D_SensitiveBox::Select3D_SensitiveBox(const Handle(SelectBasics_EntityOwner)& OwnerId,
31                                              const Bnd_Box& BBox):
32 Select3D_SensitiveEntity(OwnerId),
33 mybox3d(BBox){}
34
35 //==================================================
36 // Function: Constructor
37 // Purpose :
38 //==================================================
39
40 Select3D_SensitiveBox::
41 Select3D_SensitiveBox(const Handle(SelectBasics_EntityOwner)& OwnerId,
42                       const Standard_Real XMin,
43                       const Standard_Real YMin,
44                       const Standard_Real ZMin,
45                       const Standard_Real XMax,
46                       const Standard_Real YMax,
47                       const Standard_Real ZMax):
48 Select3D_SensitiveEntity(OwnerId)
49 {
50   mybox3d.Update(XMin,YMin,ZMin,XMax,YMax,ZMax);
51 }
52
53 //==================================================
54 // Function: Project
55 // Purpose :
56 //==================================================
57
58 void Select3D_SensitiveBox::
59 Project(const Handle(Select3D_Projector)& aProj)
60 {
61   if(HasLocation())
62   {
63     Bnd_Box B = mybox3d.Transformed(Location().Transformation());
64     ProjectBox(aProj,B);
65   }
66   else
67     ProjectBox(aProj,mybox3d);
68 }
69
70 //==================================================
71 // Function: Areas
72 // Purpose :
73 //==================================================
74
75 void Select3D_SensitiveBox::
76 Areas(SelectBasics_ListOfBox2d& aSeq)
77 {  aSeq.Append(mybox2d);}
78
79 //=======================================================================
80 //function : GetConnected
81 //purpose  : 
82 //=======================================================================
83
84 Handle(Select3D_SensitiveEntity) Select3D_SensitiveBox::GetConnected(const TopLoc_Location& aLoc) 
85 {
86   Handle(Select3D_SensitiveBox) NiouEnt =  new Select3D_SensitiveBox(myOwnerId,mybox3d);
87   
88   if(HasLocation()) NiouEnt->SetLocation(Location());
89   NiouEnt->UpdateLocation(aLoc);
90   return NiouEnt;
91 }
92
93 //==================================================
94 // Function: Matches
95 // Purpose :
96 //==================================================
97
98 Standard_Boolean Select3D_SensitiveBox::Matches (const SelectBasics_PickArgs& thePickArgs,
99                                                  Standard_Real& theMatchDMin,
100                                                  Standard_Real& theMatchDepth)
101 {
102   // check that sensitive box passes by depth
103   Standard_Real aDepth = ComputeDepth (thePickArgs.PickLine());
104   if (thePickArgs.IsClipped (aDepth))
105   {
106     return Standard_False;
107   }
108
109   theMatchDMin = 0.0;
110   theMatchDepth = aDepth;
111   return Standard_True;
112 }
113
114 //==================================================
115 // Function: Matches
116 // Purpose :
117 //==================================================
118
119 Standard_Boolean Select3D_SensitiveBox::
120 Matches (const Standard_Real XMin,
121          const Standard_Real YMin,
122          const Standard_Real XMax,
123          const Standard_Real YMax,
124          const Standard_Real aTol)
125 {
126   Bnd_Box2d BoundBox;
127   BoundBox.Update(XMin-aTol,YMin-aTol,XMax+aTol,YMax+aTol);
128   return(!BoundBox.IsOut(mybox2d));
129 }
130
131 //=======================================================================
132 //function : Matches
133 //purpose  : 
134 //=======================================================================
135
136 Standard_Boolean Select3D_SensitiveBox::
137 Matches (const TColgp_Array1OfPnt2d& /*aPoly*/,
138          const Bnd_Box2d& aBox,
139          const Standard_Real /*aTol*/)
140 {
141   return(!aBox.IsOut(mybox2d));
142 }
143
144 //=======================================================================
145 //function : Dump
146 //purpose  : 
147 //=======================================================================
148
149 void Select3D_SensitiveBox::Dump(Standard_OStream& S,const Standard_Boolean FullDump) const
150 {
151   S<<"\tSensitiveBox 3D :\n";
152   if(HasLocation())
153     S<<"\t\tExisting Location"<<endl;
154   
155   Standard_Real XMin,YMin,ZMin,XMax,YMax,ZMax;
156   mybox3d.Get(XMin,YMin,ZMin,XMax,YMax,ZMax);
157   
158   S<<"\t\t PMin [ "<<XMin<<" , "<<YMin<<" , "<<ZMin<<" ]";
159   S<<"\t\t PMax [ "<<XMax<<" , "<<YMax<<" , "<<ZMax<<" ]"<<endl;
160
161   if(FullDump)
162   {
163 //    S<<"\t\t\tOwner:"<<myOwnerId<<endl;
164     Select3D_SensitiveEntity::DumpBox(S,mybox2d);
165   }
166 }
167
168
169 //=======================================================================
170 //function : ProjectBox
171 //purpose  : 
172 //=======================================================================
173
174 void Select3D_SensitiveBox::ProjectBox(const Handle(Select3D_Projector)& aPrj,
175                                        const Bnd_Box& aBox) 
176 {
177   mybox2d.SetVoid();
178   gp_Pnt2d curp2d;
179   Standard_Real XMin,YMin,ZMin,XMax,YMax,ZMax;
180   aBox.Get(XMin,YMin,ZMin,XMax,YMax,ZMax);
181
182   aPrj->Project(gp_Pnt(XMin,YMin,ZMin),curp2d);
183   mybox2d.Update(curp2d.X(),curp2d.Y());
184   aPrj->Project(gp_Pnt(XMax,YMin,ZMin),curp2d);
185   mybox2d.Update(curp2d.X(),curp2d.Y());
186   aPrj->Project(gp_Pnt(XMax,YMax,ZMin),curp2d);
187   mybox2d.Update(curp2d.X(),curp2d.Y());
188   aPrj->Project(gp_Pnt(XMin,YMax,ZMin),curp2d);
189   mybox2d.Update(curp2d.X(),curp2d.Y());
190   aPrj->Project(gp_Pnt(XMin,YMin,ZMax),curp2d);
191   mybox2d.Update(curp2d.X(),curp2d.Y());
192   aPrj->Project(gp_Pnt(XMax,YMin,ZMax),curp2d);
193   mybox2d.Update(curp2d.X(),curp2d.Y());
194   aPrj->Project(gp_Pnt(XMax,YMax,ZMax),curp2d);
195   mybox2d.Update(curp2d.X(),curp2d.Y());
196   aPrj->Project(gp_Pnt(XMin,YMax,ZMax),curp2d);
197   mybox2d.Update(curp2d.X(),curp2d.Y());
198 }
199
200 //=======================================================================
201 //function : ComputeDepth
202 //purpose  : 
203 //=======================================================================
204
205 Standard_Real Select3D_SensitiveBox::ComputeDepth(const gp_Lin& EyeLine) const
206 {
207   Standard_Real XMin,YMin,ZMin,XMax,YMax,ZMax;
208   mybox3d.Get(XMin,YMin,ZMin,XMax,YMax,ZMax);
209   gp_Pnt PMid((XMin+XMax)/2.,(YMin+YMax)/2.,(ZMin+ZMax)/2.);
210   return ElCLib::Parameter(EyeLine,PMid);
211 }