0029367: Visualization - simplify interface of V3d_View and V3d_Viewer
[occt.git] / src / AIS / AIS_ParallelRelation.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_ParallelRelation.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRepAdaptor_Curve.hxx>
22 #include <BRepAdaptor_Surface.hxx>
23 #include <DsgPrs_LengthPresentation.hxx>
24 #include <ElCLib.hxx>
25 #include <gce_MakeLin.hxx>
26 #include <Geom_Ellipse.hxx>
27 #include <Geom_Line.hxx>
28 #include <Geom_Plane.hxx>
29 #include <Geom_Transformation.hxx>
30 #include <gp_Ax1.hxx>
31 #include <gp_Ax2.hxx>
32 #include <gp_Dir.hxx>
33 #include <gp_Lin.hxx>
34 #include <gp_Pln.hxx>
35 #include <gp_Pnt.hxx>
36 #include <Precision.hxx>
37 #include <Prs3d_ArrowAspect.hxx>
38 #include <Prs3d_DimensionAspect.hxx>
39 #include <Prs3d_Drawer.hxx>
40 #include <Prs3d_Presentation.hxx>
41 #include <Prs3d_Projector.hxx>
42 #include <Select3D_SensitiveBox.hxx>
43 #include <Select3D_SensitiveSegment.hxx>
44 #include <SelectMgr_EntityOwner.hxx>
45 #include <SelectMgr_Selection.hxx>
46 #include <Standard_DomainError.hxx>
47 #include <Standard_NotImplemented.hxx>
48 #include <Standard_Type.hxx>
49 #include <TCollection_AsciiString.hxx>
50 #include <TCollection_ExtendedString.hxx>
51 #include <TopoDS.hxx>
52 #include <TopoDS_Shape.hxx>
53
54 IMPLEMENT_STANDARD_RTTIEXT(AIS_ParallelRelation,AIS_Relation)
55
56 //=======================================================================
57 //function : Constructor
58 //purpose  : 
59 //=======================================================================
60 AIS_ParallelRelation::AIS_ParallelRelation(const TopoDS_Shape& aFShape, 
61                                            const TopoDS_Shape& aSShape, 
62                                            const Handle(Geom_Plane)& aPlane)
63 {
64   myFShape = aFShape;
65   mySShape = aSShape;
66   myPlane = aPlane;
67   myAutomaticPosition = Standard_True;
68   myArrowSize = 0.01;
69   mySymbolPrs = DsgPrs_AS_BOTHAR;
70 }
71
72 //=======================================================================
73 //function : Constructor
74 //purpose  : 
75 //=======================================================================
76 AIS_ParallelRelation::AIS_ParallelRelation(const TopoDS_Shape& aFShape, 
77                                            const TopoDS_Shape& aSShape, 
78                                            const Handle(Geom_Plane)& aPlane, 
79                                            const gp_Pnt& aPosition,
80                                            const DsgPrs_ArrowSide aSymbolPrs, 
81                                            const Standard_Real anArrowSize)
82 {
83   myFShape = aFShape;
84   mySShape = aSShape;
85   myPlane = aPlane;
86   myAutomaticPosition = Standard_False;
87   SetArrowSize( anArrowSize );
88   myPosition = aPosition;
89   mySymbolPrs = aSymbolPrs;
90 }
91
92 //=======================================================================
93 //function : Compute
94 //purpose  : 
95 //=======================================================================
96 void AIS_ParallelRelation::Compute(const Handle(PrsMgr_PresentationManager3d)&, 
97                                    const Handle(Prs3d_Presentation)& aPresentation, 
98                                    const Standard_Integer)
99 {
100   switch (myFShape.ShapeType())
101     {
102     case TopAbs_FACE :
103       {
104         // cas longueur entre deux faces
105         ComputeTwoFacesParallel(aPresentation);
106       }
107       break;
108     case TopAbs_EDGE :
109       {
110         // cas longueur entre deux edges
111         ComputeTwoEdgesParallel(aPresentation);
112       }
113       break;
114     default:
115       break;
116     }
117 }
118
119 //=======================================================================
120 //function : Compute
121 //purpose  : to avoid warning
122 //=======================================================================
123 void AIS_ParallelRelation::Compute(const Handle(Prs3d_Projector)& aProjector,
124                                    const Handle(Prs3d_Presentation)& aPresentation)
125 {
126 // throw Standard_NotImplemented("AIS_ParallelRelation::Compute(const Handle(Prs3d_Projector)&,const Handle(Prs3d_Presentation)&)");
127   PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
128 }
129
130 void AIS_ParallelRelation::Compute(const Handle(Prs3d_Projector)& aProjector, const Handle(Geom_Transformation)& aTransformation, const Handle(Prs3d_Presentation)& aPresentation)
131 {
132 // throw Standard_NotImplemented("AIS_ParallelRelation::Compute(const Handle(Prs3d_Projector)&, const Handle(Geom_Transformation)&, const Handle(Prs3d_Presentation)&)");
133   PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
134 }
135
136 //=======================================================================
137 //function : ComputeSelection
138 //purpose  : 
139 //=======================================================================
140 void AIS_ParallelRelation::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection, 
141                                             const Standard_Integer)
142 {
143   gp_Lin L1 (myFAttach,myDirAttach);
144   gp_Lin L2 (mySAttach,myDirAttach);
145   gp_Pnt Proj1 = ElCLib::Value(ElCLib::Parameter(L1,myPosition),L1);
146   gp_Pnt Proj2 = ElCLib::Value(ElCLib::Parameter(L2,myPosition),L2);
147   
148   gp_Lin L3;
149   Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
150
151   if (!Proj1.IsEqual(Proj2,Precision::Confusion()))
152     {
153       L3 = gce_MakeLin(Proj1,Proj2);
154     }
155   else
156     {
157       L3 = gce_MakeLin(Proj1,myDirAttach);
158       Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
159       Handle(Select3D_SensitiveBox) box =
160         new Select3D_SensitiveBox(own,
161                                   myPosition.X(),
162                                   myPosition.Y(),
163                                   myPosition.Z(),
164                                   myPosition.X()+size,
165                                   myPosition.Y()+size,
166                                   myPosition.Z()+size);
167       aSelection->Add(box);
168     }
169   Standard_Real parmin,parmax,parcur;
170   parmin = ElCLib::Parameter(L3,Proj1);
171   parmax = parmin;
172   
173   parcur = ElCLib::Parameter(L3,Proj2);
174   parmin = Min(parmin,parcur);
175   parmax = Max(parmax,parcur);
176   
177   parcur = ElCLib::Parameter(L3,myPosition);
178   parmin = Min(parmin,parcur);
179   parmax = Max(parmax,parcur);
180   
181   gp_Pnt PointMin = ElCLib::Value(parmin,L3);
182   gp_Pnt PointMax = ElCLib::Value(parmax,L3);
183
184   Handle(Select3D_SensitiveSegment) seg;
185   
186   if (!PointMin.IsEqual(PointMax,Precision::Confusion()))
187     {
188       seg = new Select3D_SensitiveSegment(own,
189                                           PointMin,
190                                           PointMax);
191       aSelection->Add(seg);
192     }
193   if (!myFAttach.IsEqual(Proj1,Precision::Confusion()))
194     {
195       seg = new Select3D_SensitiveSegment(own, myFAttach, Proj1);
196       aSelection->Add(seg);
197     }
198   if (!mySAttach.IsEqual(Proj2,Precision::Confusion()))
199     {
200       seg = new Select3D_SensitiveSegment(own, mySAttach, Proj2);
201       aSelection->Add(seg);
202     }
203 }
204
205 //=======================================================================
206 //function : ComputeTwoFacesParallel
207 //purpose  : 
208 //=======================================================================
209 void AIS_ParallelRelation::ComputeTwoFacesParallel(const Handle(Prs3d_Presentation)&)
210 {
211   throw Standard_NotImplemented("AIS_ParallelRelation::ComputeTwoFacesParallel not implemented");
212 }
213
214 //=======================================================================
215 //function : ComputeTwoEdgesParallel
216 //purpose  : 
217 //=======================================================================
218 void AIS_ParallelRelation::ComputeTwoEdgesParallel(const Handle(Prs3d_Presentation)& aPresentation)
219 {
220   TopoDS_Edge E1 = TopoDS::Edge(myFShape);
221   TopoDS_Edge E2 = TopoDS::Edge(mySShape);
222
223   gp_Pnt ptat11,ptat12,ptat21,ptat22;//,pint3d;
224   Handle(Geom_Curve) geom1,geom2;
225   Standard_Boolean isInfinite1,isInfinite2;
226   Handle(Geom_Curve) extCurv;
227   if (!AIS::ComputeGeometry(E1,E2,myExtShape,
228                             geom1,geom2,
229                             ptat11,ptat12,ptat21,ptat22,
230                             extCurv,
231                             isInfinite1,isInfinite2,
232                             myPlane))
233     {
234       return;
235     }
236
237   aPresentation->SetInfiniteState((isInfinite1 || isInfinite2) && (myExtShape != 0));
238
239   gp_Lin l1;
240   gp_Lin l2;
241   Standard_Boolean isEl1 = Standard_False, isEl2 = Standard_False;
242
243   if (geom1->IsInstance(STANDARD_TYPE(Geom_Ellipse)))
244     {
245       Handle(Geom_Ellipse) geom_el1 (Handle(Geom_Ellipse)::DownCast (geom1));
246       // construct lines through focuses
247       gp_Ax1 elAx = geom_el1->XAxis();
248       l1 = gp_Lin(elAx);
249       Standard_Real focex = geom_el1->MajorRadius() - geom_el1->Focal()/2.0;
250       gp_Vec transvec = gp_Vec(elAx.Direction())*focex;
251       ptat11 = geom_el1->Focus1().Translated(transvec);
252       ptat12 = geom_el1->Focus2().Translated(-transvec);
253       isEl1 = Standard_True;
254     }
255   else if (geom1->IsInstance(STANDARD_TYPE(Geom_Line)))
256     {
257       Handle(Geom_Line) geom_lin1 (Handle(Geom_Line)::DownCast (geom1));
258       l1 = geom_lin1->Lin();
259     }
260   else return;
261
262   if (geom2->IsInstance(STANDARD_TYPE(Geom_Ellipse)))
263     {
264       Handle(Geom_Ellipse) geom_el2 (Handle(Geom_Ellipse)::DownCast (geom2));
265       // construct lines through focuses
266       gp_Ax1 elAx = geom_el2->XAxis();
267       l2 = gp_Lin(elAx);
268       Standard_Real focex = geom_el2->MajorRadius() - geom_el2->Focal()/2.0;
269       gp_Vec transvec = gp_Vec(elAx.Direction())*focex;
270       ptat21 = geom_el2->Focus1().Translated(transvec);
271       ptat22 = geom_el2->Focus2().Translated(-transvec);
272       isEl2 = Standard_True;
273     }
274   else if (geom2->IsInstance(STANDARD_TYPE(Geom_Line)))
275     {
276       Handle(Geom_Line) geom_lin2 (Handle(Geom_Line)::DownCast (geom2));
277       l2 = geom_lin2->Lin();
278     }
279   else return;
280
281   const Handle(Geom_Line)& geom_lin1 = new Geom_Line(l1);
282   const Handle(Geom_Line)& geom_lin2 = new Geom_Line(l2);
283
284   myDirAttach = l1.Direction();
285   // size
286   if( !myArrowSizeIsDefined ) {
287     Standard_Real arrSize1 (myArrowSize), arrSize2 (myArrowSize);
288     if (!isInfinite1) arrSize1 = ptat11.Distance(ptat12)/50.;
289     if (!isInfinite2) arrSize2 = ptat21.Distance(ptat22)/50.;
290     myArrowSize = Max(myArrowSize,Max(arrSize1,arrSize2));
291 //  myArrowSize = Min(myArrowSize,Min(arrSize1,arrSize2));
292   }
293
294   if ( myAutomaticPosition )
295     {    
296       gp_Pnt curpos;
297       if ( !isInfinite1 )
298         {
299           gp_Pnt p2 = ElCLib::Value(ElCLib::Parameter(l2,ptat11),l2);
300           curpos.SetXYZ((ptat11.XYZ() + p2.XYZ())/2.);
301         }
302       else if ( !isInfinite2 )
303         {
304           gp_Pnt p2 = ElCLib::Value(ElCLib::Parameter(l1,ptat21),l1);
305           curpos.SetXYZ((ptat21.XYZ()+p2.XYZ())/2.);
306         }
307       else
308         {
309           curpos.SetXYZ((l1.Location().XYZ()+l2.Location().XYZ())/2.);
310         }
311       // offset pour eviter confusion Edge et Dimension
312       gp_Vec offset (myDirAttach);
313       offset = offset*myArrowSize*(-10.);
314       curpos.Translate(offset);
315       myPosition = curpos;
316     }
317
318   // recherche points attache
319   if (!isInfinite1)
320     {
321       if ( isEl1 )
322         {
323           if (myPosition.Distance(ptat11) < myPosition.Distance(ptat12)) myFAttach = ptat12;
324           else myFAttach = ptat11;
325         }
326       else
327         {
328           if (myPosition.Distance(ptat11) > myPosition.Distance(ptat12)) myFAttach = ptat12;
329           else myFAttach = ptat11;
330         }
331     }
332   else
333     {
334       myFAttach = ElCLib::Value(ElCLib::Parameter(l1,myPosition),l1);
335     }
336   
337   if (!isInfinite2)
338     {
339       if ( isEl2 )
340         {
341           if (myPosition.Distance(ptat21) < myPosition.Distance(ptat22)) mySAttach = ptat22;
342           else mySAttach = ptat21;
343         }
344       else
345         {
346           if (myPosition.Distance(ptat21) > myPosition.Distance(ptat22)) mySAttach = ptat22;
347           else mySAttach = ptat21;
348         }
349     }
350   else
351     {
352       mySAttach = ElCLib::Value(ElCLib::Parameter(l2,myPosition),l2);
353     }
354   TCollection_ExtendedString aText (" //");
355   
356   if (l1.Distance(l2) <= Precision::Confusion())
357     {
358       myArrowSize = 0.;
359     }
360   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
361   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
362   arr->SetLength(myArrowSize);
363   arr = la->ArrowAspect();
364   arr->SetLength(myArrowSize);
365   if ( myExtShape == 1)
366     mySymbolPrs = DsgPrs_AS_FIRSTPT_LASTAR;
367   else if ( myExtShape == 2)
368     mySymbolPrs = DsgPrs_AS_FIRSTAR_LASTPT;
369
370   DsgPrs_LengthPresentation::Add(aPresentation,
371                                  myDrawer,
372                                  aText,
373                                  myFAttach,
374                                  mySAttach,
375                                  myDirAttach,
376                                  myPosition,
377                                  mySymbolPrs);
378   if ( (myExtShape != 0) &&  !extCurv.IsNull())
379     {
380       gp_Pnt pf, pl;
381       if ( myExtShape == 1 )
382         {
383           if (!isInfinite1)
384             {
385               pf = ptat11; 
386               pl = ptat12;
387             }
388           ComputeProjEdgePresentation(aPresentation,E1,geom_lin1,pf,pl);
389         }
390       else
391         {
392           if (!isInfinite2)
393             {
394               pf = ptat21; 
395               pl = ptat22;
396             }
397           ComputeProjEdgePresentation(aPresentation,E2,geom_lin2,pf,pl);
398         }
399     }
400 }