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