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