c2cf79a9fb8a1f3555a6b0ac5dbf9b5594940b01
[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-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22
23
24 #include <Select3D_SensitiveBox.ixx>
25 #include <gp_Pnt2d.hxx>
26 #include <gp_Pnt.hxx>
27 #include <Bnd_Box.hxx>
28 #include <ElCLib.hxx>
29
30
31
32 //==================================================
33 // Function: Constructor
34 // Purpose :
35 //==================================================
36
37 Select3D_SensitiveBox::Select3D_SensitiveBox(const Handle(SelectBasics_EntityOwner)& OwnerId,
38                                              const Bnd_Box& BBox):
39 Select3D_SensitiveEntity(OwnerId),
40 mybox3d(BBox){}
41
42 //==================================================
43 // Function: Constructor
44 // Purpose :
45 //==================================================
46
47 Select3D_SensitiveBox::
48 Select3D_SensitiveBox(const Handle(SelectBasics_EntityOwner)& OwnerId,
49                       const Standard_Real XMin,
50                       const Standard_Real YMin,
51                       const Standard_Real ZMin,
52                       const Standard_Real XMax,
53                       const Standard_Real YMax,
54                       const Standard_Real ZMax):
55 Select3D_SensitiveEntity(OwnerId)
56 {
57   mybox3d.Update(XMin,YMin,ZMin,XMax,YMax,ZMax);
58 }
59
60 //==================================================
61 // Function: Project
62 // Purpose :
63 //==================================================
64
65 void Select3D_SensitiveBox::
66 Project(const Handle(Select3D_Projector)& aProj)
67 {
68   Select3D_SensitiveEntity::Project(aProj); // to set the field last proj...
69
70   if(HasLocation())
71   {
72     Bnd_Box B = mybox3d.Transformed(Location().Transformation());
73     ProjectBox(aProj,B);
74   }
75   else
76     ProjectBox(aProj,mybox3d);
77 }
78
79 //==================================================
80 // Function: Areas
81 // Purpose :
82 //==================================================
83
84 void Select3D_SensitiveBox::
85 Areas(SelectBasics_ListOfBox2d& aSeq)
86 {  aSeq.Append(mybox2d);}
87
88 //=======================================================================
89 //function : GetConnected
90 //purpose  : 
91 //=======================================================================
92
93 Handle(Select3D_SensitiveEntity) Select3D_SensitiveBox::GetConnected(const TopLoc_Location& aLoc) 
94 {
95   Handle(Select3D_SensitiveBox) NiouEnt =  new Select3D_SensitiveBox(myOwnerId,mybox3d);
96   
97   if(HasLocation()) NiouEnt->SetLocation(Location());
98   NiouEnt->UpdateLocation(aLoc);
99   return NiouEnt;
100 }
101
102 //==================================================
103 // Function: Matches
104 // Purpose :
105 //==================================================
106 Standard_Boolean Select3D_SensitiveBox::
107 Matches(const Standard_Real X, 
108         const Standard_Real Y, 
109         const Standard_Real aTol, 
110         Standard_Real& DMin)
111 {
112   Select3D_SensitiveEntity::Matches(X,Y,aTol,DMin);
113   DMin=0.;
114   
115   return Standard_True;
116 }
117
118 //==================================================
119 // Function: Matches
120 // Purpose :
121 //==================================================
122
123 Standard_Boolean Select3D_SensitiveBox::
124 Matches (const Standard_Real XMin,
125          const Standard_Real YMin,
126          const Standard_Real XMax,
127          const Standard_Real YMax,
128          const Standard_Real aTol)
129 {
130   Bnd_Box2d BoundBox;
131   BoundBox.Update(XMin-aTol,YMin-aTol,XMax+aTol,YMax+aTol);
132   return(!BoundBox.IsOut(mybox2d));
133 }
134
135 //=======================================================================
136 //function : Matches
137 //purpose  : 
138 //=======================================================================
139
140 Standard_Boolean Select3D_SensitiveBox::
141 Matches (const TColgp_Array1OfPnt2d& /*aPoly*/,
142          const Bnd_Box2d& aBox,
143          const Standard_Real /*aTol*/)
144 {
145   return(!aBox.IsOut(mybox2d));
146 }
147
148 //=======================================================================
149 //function : Dump
150 //purpose  : 
151 //=======================================================================
152
153 void Select3D_SensitiveBox::Dump(Standard_OStream& S,const Standard_Boolean FullDump) const
154 {
155   S<<"\tSensitiveBox 3D :\n";
156   if(HasLocation())
157     S<<"\t\tExisting Location"<<endl;
158   
159   Standard_Real XMin,YMin,ZMin,XMax,YMax,ZMax;
160   mybox3d.Get(XMin,YMin,ZMin,XMax,YMax,ZMax);
161   
162   S<<"\t\t PMin [ "<<XMin<<" , "<<YMin<<" , "<<ZMin<<" ]";
163   S<<"\t\t PMax [ "<<XMax<<" , "<<YMax<<" , "<<ZMax<<" ]"<<endl;
164
165   if(FullDump)
166   {
167 //    S<<"\t\t\tOwner:"<<myOwnerId<<endl;
168     Select3D_SensitiveEntity::DumpBox(S,mybox2d);
169   }
170 }
171
172
173 //=======================================================================
174 //function : ProjectBox
175 //purpose  : 
176 //=======================================================================
177
178 void Select3D_SensitiveBox::ProjectBox(const Handle(Select3D_Projector)& aPrj,
179                                        const Bnd_Box& aBox) 
180 {
181   mybox2d.SetVoid();
182   gp_Pnt2d curp2d;
183   Standard_Real XMin,YMin,ZMin,XMax,YMax,ZMax;
184   aBox.Get(XMin,YMin,ZMin,XMax,YMax,ZMax);
185
186   aPrj->Project(gp_Pnt(XMin,YMin,ZMin),curp2d);
187   mybox2d.Update(curp2d.X(),curp2d.Y());
188   aPrj->Project(gp_Pnt(XMax,YMin,ZMin),curp2d);
189   mybox2d.Update(curp2d.X(),curp2d.Y());
190   aPrj->Project(gp_Pnt(XMax,YMax,ZMin),curp2d);
191   mybox2d.Update(curp2d.X(),curp2d.Y());
192   aPrj->Project(gp_Pnt(XMin,YMax,ZMin),curp2d);
193   mybox2d.Update(curp2d.X(),curp2d.Y());
194   aPrj->Project(gp_Pnt(XMin,YMin,ZMax),curp2d);
195   mybox2d.Update(curp2d.X(),curp2d.Y());
196   aPrj->Project(gp_Pnt(XMax,YMin,ZMax),curp2d);
197   mybox2d.Update(curp2d.X(),curp2d.Y());
198   aPrj->Project(gp_Pnt(XMax,YMax,ZMax),curp2d);
199   mybox2d.Update(curp2d.X(),curp2d.Y());
200   aPrj->Project(gp_Pnt(XMin,YMax,ZMax),curp2d);
201   mybox2d.Update(curp2d.X(),curp2d.Y());
202 }
203
204 //=======================================================================
205 //function : ComputeDepth
206 //purpose  : 
207 //=======================================================================
208
209 Standard_Real Select3D_SensitiveBox::ComputeDepth(const gp_Lin& EyeLine) const
210 {
211   Standard_Real XMin,YMin,ZMin,XMax,YMax,ZMax;
212   mybox3d.Get(XMin,YMin,ZMin,XMax,YMax,ZMax);
213   gp_Pnt PMid((XMin+XMax)/2.,(YMin+YMax)/2.,(ZMin+ZMax)/2.);
214   return ElCLib::Parameter(EyeLine,PMid);
215 }