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