0024420: Add methods to switch the type of sensitivity in AIS_Circle and AIS_Plane
[occt.git] / src / AIS / AIS_PerpendicularRelation.cxx
CommitLineData
b311480e 1// Created on: 1996-12-05
2// Created by: Jean-Pierre COMBE/Odile Olivier
3// Copyright (c) 1996-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
7fd59977 21
22#include <Standard_NotImplemented.hxx>
23
24#include <AIS_PerpendicularRelation.ixx>
25
26#include <AIS.hxx>
27
28#include <BRepBuilderAPI_MakeFace.hxx>
29#include <BRepAdaptor_Surface.hxx>
30#include <BRep_Tool.hxx>
31
32#include <DsgPrs_PerpenPresentation.hxx>
33
34#include <ElCLib.hxx>
35
36#include <Geom2d_Line.hxx>
37#include <GeomAPI.hxx>
38#include <Geom_Line.hxx>
39#include <Geom_Line.hxx>
40#include <Geom_Plane.hxx>
41
42#include <IntAna2d_AnaIntersection.hxx>
43#include <IntAna2d_IntPoint.hxx>
44
45#include <Precision.hxx>
46
47#include <Select3D_SensitiveSegment.hxx>
48#include <SelectMgr_EntityOwner.hxx>
49
50#include <TopoDS.hxx>
51#include <TopoDS_Edge.hxx>
52#include <TopoDS_Face.hxx>
53#include <TopoDS_Vertex.hxx>
54
55#include <gce_MakeDir.hxx>
56
57#include <gp_Pln.hxx>
58#include <gp_Pnt.hxx>
59#include <gp_Pnt2d.hxx>
60#include <gp_Trsf.hxx>
61#include <gp_Vec.hxx>
62#include <Geom_Ellipse.hxx>
63
64//=======================================================================
65//function : Constructor
66//purpose : TwoEdgesPerpendicular
67//=======================================================================
68AIS_PerpendicularRelation::AIS_PerpendicularRelation(const TopoDS_Shape& aFShape,
69 const TopoDS_Shape& aSShape,
70 const Handle(Geom_Plane)& aPlane)
71:AIS_Relation()
72{
73 myFShape = aFShape;
74 mySShape = aSShape;
75 myPlane = aPlane;
76}
77
78//=======================================================================
79//function : Constructor
80//purpose : TwoFacesPerpendicular
81//=======================================================================
82AIS_PerpendicularRelation::AIS_PerpendicularRelation(const TopoDS_Shape& aFShape,
83 const TopoDS_Shape& aSShape)
84:AIS_Relation()
85{
86 myFShape = aFShape;
87 mySShape = aSShape;
88}
89
90//=======================================================================
91//function : Compute
92//purpose :
93//=======================================================================
94void AIS_PerpendicularRelation::Compute(const Handle(PrsMgr_PresentationManager3d)&,
95 const Handle(Prs3d_Presentation)& aPresentation,
96 const Standard_Integer)
97{
98 aPresentation->Clear();
99
100 if (myFShape.ShapeType() == mySShape.ShapeType()) {
101 switch (myFShape.ShapeType()) {
102 case TopAbs_FACE :
103 {
104 // cas perpendiculaire entre deux faces
105 ComputeTwoFacesPerpendicular(aPresentation);
106 }
107 break;
108 case TopAbs_EDGE :
109 {
110 // cas perpendiculaire entre deux edges
111 ComputeTwoEdgesPerpendicular(aPresentation);
112 }
113 break;
114 default:
115 break;
116 }
117 }
118 // Cas pas traite - Edge/Face
119}
120
121//=======================================================================
122//function : Compute
123//purpose : to avoid warning
124//=======================================================================
125void AIS_PerpendicularRelation::Compute(const Handle(Prs3d_Projector)& aProjector,
126 const Handle(Prs3d_Presentation)& aPresentation)
127{
128// Standard_NotImplemented::Raise("AIS_PerpendicularRelation::Compute(const Handle(Prs3d_Projector)&,const Handle(Prs3d_Presentation)&)");
129 PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
130}
131
7fd59977 132void AIS_PerpendicularRelation::Compute(const Handle_Prs3d_Projector& aProjector, const Handle_Geom_Transformation& aTransformation, const Handle_Prs3d_Presentation& aPresentation)
133{
134// Standard_NotImplemented::Raise("AIS_PerpendicularRelation::Compute(const Handle_Prs3d_Projector&, const Handle_Geom_Transformation&, const Handle_Prs3d_Presentation&)");
135 PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
136}
137
138//=======================================================================
139//function : ComputeSelection
140//purpose :
141//=======================================================================
142void AIS_PerpendicularRelation::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
143 const Standard_Integer)
144{
145 Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
146 const gp_Pnt& pos = myPosition;
147 Handle(Select3D_SensitiveSegment) seg;
148 Standard_Boolean ok1(Standard_False),ok2(Standard_False);
149
150 if (!myFAttach.IsEqual(pos,Precision::Confusion())) {
151 seg = new Select3D_SensitiveSegment(own,
152 myFAttach,
153 pos);
154 aSelection->Add(seg);
155 ok1 = Standard_True;
156 }
157 if (!mySAttach.IsEqual(myPosition,Precision::Confusion())) {
158 seg = new Select3D_SensitiveSegment(own,
159 mySAttach,
160 pos);
161 aSelection->Add(seg);
162 ok2 = Standard_True;
163 }
164
165 if (ok1 && ok2) {
166 gp_Vec vec1(gce_MakeDir(pos,myFAttach));
167 gp_Vec vec2(gce_MakeDir(pos,mySAttach));
168 Standard_Real dist1(pos.Distance(myFAttach));
169 Standard_Real dist2(pos.Distance(mySAttach));
170 vec1 *= dist1;
171 vec1 *= .2;
172 vec2 *= dist2;
173 vec2 *= .2;
174
175 gp_Pnt pAx11 = pos.Translated(vec1);
176 gp_Pnt pAx22 = pos.Translated(vec2);
177 gp_Pnt p_symb = pAx22.Translated(vec1);
178 seg = new Select3D_SensitiveSegment(own,pAx11,p_symb);
179 aSelection->Add(seg);
180 seg = new Select3D_SensitiveSegment(own,p_symb,pAx22);
181 aSelection->Add(seg);
182 }
183}
184
185//=======================================================================
186//function : ComputeTwoFacesPerpendicular
187//purpose :
188//=======================================================================
189void AIS_PerpendicularRelation::ComputeTwoFacesPerpendicular
190 (const Handle(Prs3d_Presentation)& /*aPresentation*/)
191{
192}
193
194//=======================================================================
195//function : ComputeTwoEdgesPerpendicular
196//purpose :
197//=======================================================================
198void AIS_PerpendicularRelation::ComputeTwoEdgesPerpendicular(const Handle(Prs3d_Presentation)& aPresentation)
199{
200 // 3d lines
201 Handle(Geom_Curve) geom1,geom2;
202 gp_Pnt pint3d,p1,p2,pAx1,pAx2,ptat11,ptat12,ptat21,ptat22;
203 Standard_Boolean isInfinite1,isInfinite2;
204 Handle(Geom_Curve) extCurv;
205 if ( !AIS::ComputeGeometry(TopoDS::Edge(myFShape),TopoDS::Edge(mySShape),
206 myExtShape,
207 geom1,geom2,
208 ptat11,ptat12,ptat21,ptat22,
209 extCurv,
210 isInfinite1,isInfinite2,
211 myPlane) ) return;
212
213 Standard_Boolean interOut1(Standard_False),interOut2(Standard_False);
214
215 Handle(Geom_Line) geom_lin1;
216 Handle(Geom_Line) geom_lin2;
217 if ( geom1->IsInstance(STANDARD_TYPE(Geom_Ellipse)) )
218 {
219 const Handle(Geom_Ellipse)& geom_el = (Handle(Geom_Ellipse)&) geom1;
220 // construct lines through focuses
221 gp_Ax1 elAx = geom_el->XAxis();
222 gp_Lin ll (elAx);
223 geom_lin1 = new Geom_Line(ll);
224 Standard_Real focex = geom_el->MajorRadius() - geom_el->Focal()/2.0;
225 gp_Vec transvec = gp_Vec(elAx.Direction())*focex;
226 ptat11 = geom_el->Focus1().Translated(transvec);
227 ptat12 = geom_el->Focus2().Translated(-transvec);
228 interOut1 = Standard_True;
229 }
230 else if ( geom1->IsInstance(STANDARD_TYPE(Geom_Line)) )
231 {
232 geom_lin1 = (Handle(Geom_Line)&) geom1;
233 }
234 else return;
235
236 if (geom2->IsInstance(STANDARD_TYPE(Geom_Ellipse)))
237 {
238 const Handle(Geom_Ellipse)& geom_el = (Handle(Geom_Ellipse)&) geom2;
239 // construct lines through focuses
240 gp_Ax1 elAx = geom_el->XAxis();
241 gp_Lin ll (elAx);
242 geom_lin2 = new Geom_Line(ll);
243 Standard_Real focex = geom_el->MajorRadius() - geom_el->Focal()/2.0;
244 gp_Vec transvec = gp_Vec(elAx.Direction())*focex;
245 ptat21 = geom_el->Focus1().Translated(transvec);
246 ptat22 = geom_el->Focus2().Translated(-transvec);
247 interOut2 = Standard_True;
248 }
249 else if ( geom2->IsInstance(STANDARD_TYPE(Geom_Line)) )
250 {
251 geom_lin2 = (Handle(Geom_Line)&) geom2;
252 }
253 else return;
254
255 // current face
256 BRepBuilderAPI_MakeFace makeface (myPlane->Pln());
257 TopoDS_Face face (makeface.Face());
258 BRepAdaptor_Surface adp (makeface.Face());
259
260 // 2d lines => projection of 3d on current plane
261 Handle(Geom2d_Curve) aGeom2dCurve = GeomAPI::To2d(geom_lin1,myPlane->Pln());
262 Handle(Geom2d_Line) lin1_2d = (Handle(Geom2d_Line)&) aGeom2dCurve ;
263 aGeom2dCurve = GeomAPI::To2d(geom_lin2,myPlane->Pln());
264 Handle(Geom2d_Line) lin2_2d = (Handle(Geom2d_Line)&) aGeom2dCurve ;
265 IntAna2d_AnaIntersection inter(lin1_2d->Lin2d(),lin2_2d->Lin2d());
266 if (!inter.IsDone()) return;
267 if (!inter.NbPoints()) return;
268
269 gp_Pnt2d pint(inter.Point(1).Value());
270 pint3d = adp.Value(pint.X(),pint.Y());
271
272 myPosition = pint3d;
273 // recherche points attache
274 Standard_Real par1,par2,curpar,pmin,pmax;//,dist,sign;
275 Standard_Real length(0.);
276
277 if ( isInfinite1 && isInfinite2 )
278 {
279 Standard_Real curpar1 = ElCLib::Parameter(geom_lin1->Lin(),pint3d);
280 Standard_Real curpar2 = ElCLib::Parameter(geom_lin2->Lin(),pint3d);
281 par1 = par2 = 50.;
282 p1 = p2 = pint3d;
283 myFAttach = ElCLib::Value(curpar1+par1,geom_lin1->Lin());
284 mySAttach = ElCLib::Value(curpar2+par2,geom_lin2->Lin());
285 }
286 else
287 {
288 Standard_Boolean lengthComputed (Standard_False);
289 if ( !isInfinite1 )
290 {
291 curpar = ElCLib::Parameter(geom_lin1->Lin(),pint3d);
292 par1 = ElCLib::Parameter(geom_lin1->Lin(),ptat11);
293 par2 = ElCLib::Parameter(geom_lin1->Lin(),ptat12);
294 pmin = Min(par1,par2);
295 pmax = Max(par1,par2);
296
297 if ( myPosition.SquareDistance(ptat11) > myPosition.SquareDistance(ptat12) )
298 p1 = ptat11;
299 else
300 p1 = ptat12;
301 if ( (curpar < pmin) || (curpar > pmax) )
302 {
303 interOut1 = Standard_True;
304 }
305 if ( !isInfinite2 ) length = 2.*Min(ptat11.Distance(ptat12),ptat21.Distance(ptat22))/5.;
306 else length = 2.*ptat11.Distance(ptat12)/5.;
307 lengthComputed = Standard_True;
308 gp_Vec vec1 (gce_MakeDir(myPosition,p1));
309 vec1.Multiply(length);
310 pAx1 = myPosition.Translated(vec1);
311 myFAttach = pAx1;
312 }
313 if ( !isInfinite2 )
314 {
315 curpar = ElCLib::Parameter(geom_lin2->Lin(),pint3d);
316 par1 = ElCLib::Parameter(geom_lin2->Lin(),ptat21);
317 par2 = ElCLib::Parameter(geom_lin2->Lin(),ptat22);
318 pmin = Min(par1,par2);
319 pmax = Max(par1,par2);
320
321 if ( myPosition.SquareDistance(ptat21) > myPosition.SquareDistance(ptat22) ) p2 = ptat21;
322 else p2 = ptat22;
323 if ( (curpar < pmin) || (curpar > pmax) )
324 {
325 interOut2 = Standard_True;
326 }
327 gp_Vec vec2 (gce_MakeDir(myPosition,p2));
328 if ( !lengthComputed )
329 {
330 if ( !isInfinite1 ) length = 2.*Min(ptat11.Distance(ptat12),ptat21.Distance(ptat22))/5.;
331 else length = 2.*ptat21.Distance(ptat22)/5.;
332 }
333 vec2.Multiply(length);
334 pAx2 = myPosition.Translated(vec2);
335 mySAttach = pAx2;
336 }
337 if ( isInfinite1 )
338 {
339 p1 = myPosition;
340 gp_Vec vec1(geom_lin1->Lin().Direction());
341 vec1.Multiply(length);
342 myFAttach = myPosition.Translated(vec1);
343 }
344 if ( isInfinite2 )
345 {
346 p2 = myPosition;
347 gp_Vec vec2(geom_lin2->Lin().Direction());
348 vec2.Multiply(length);
349 mySAttach = myPosition.Translated(vec2);
350 }
351 }
352 DsgPrs_PerpenPresentation::Add(aPresentation,myDrawer,
353 myFAttach,mySAttach,
354 p1,p2,
355 myPosition,
356 interOut1,interOut2);
357
358 if ( (myExtShape != 0) && !extCurv.IsNull()) {
359 gp_Pnt pf,pl;
360 if ( myExtShape == 1 ) {
361 if (!isInfinite1) {
362 pf = ptat11;
363 pl = ptat12;
364 }
365 aPresentation->SetInfiniteState(isInfinite1);
366 ComputeProjEdgePresentation(aPresentation,TopoDS::Edge(myFShape),geom_lin1,pf,pl);
367 }
368 else {
369 if (!isInfinite2) {
370 pf = ptat21;
371 pl = ptat22;
372 }
373 aPresentation->SetInfiniteState(isInfinite2);
374 ComputeProjEdgePresentation(aPresentation,TopoDS::Edge(mySShape),geom_lin2,pf,pl);
375 }
376 }
377}