0026936: Drawbacks of inlining in new type system in OCCT 7.0 -- automatic
[occt.git] / src / AIS / AIS_OffsetDimension.cxx
1 // Created on: 1997-03-04
2 // Created by: Jean-Pierre COMBE
3 // Copyright (c) 1997-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_OffsetDimension.hxx>
20 #include <BRep_Tool.hxx>
21 #include <BRepAdaptor_Surface.hxx>
22 #include <BRepBuilderAPI_Transform.hxx>
23 #include <DsgPrs_OffsetPresentation.hxx>
24 #include <ElCLib.hxx>
25 #include <ElSLib.hxx>
26 #include <gce_MakeLin.hxx>
27 #include <Geom_Transformation.hxx>
28 #include <GeomAbs_SurfaceType.hxx>
29 #include <gp_Ax1.hxx>
30 #include <gp_Ax2.hxx>
31 #include <gp_Ax3.hxx>
32 #include <gp_Lin.hxx>
33 #include <gp_Pln.hxx>
34 #include <gp_Pnt.hxx>
35 #include <gp_Trsf.hxx>
36 #include <gp_Vec.hxx>
37 #include <Graphic3d_Structure.hxx>
38 #include <Precision.hxx>
39 #include <Prs3d_ArrowAspect.hxx>
40 #include <Prs3d_DimensionAspect.hxx>
41 #include <Prs3d_Drawer.hxx>
42 #include <Prs3d_LineAspect.hxx>
43 #include <Prs3d_Presentation.hxx>
44 #include <Prs3d_Projector.hxx>
45 #include <Select3D_SensitiveBox.hxx>
46 #include <Select3D_SensitiveSegment.hxx>
47 #include <SelectMgr_EntityOwner.hxx>
48 #include <SelectMgr_Selection.hxx>
49 #include <Standard_NotImplemented.hxx>
50 #include <Standard_Type.hxx>
51 #include <StdPrs_WFShape.hxx>
52 #include <TCollection_ExtendedString.hxx>
53 #include <TColStd_Array2OfReal.hxx>
54 #include <TopExp_Explorer.hxx>
55 #include <TopoDS.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <TopoDS_Vertex.hxx>
58
59 IMPLEMENT_STANDARD_RTTIEXT(AIS_OffsetDimension,AIS_Relation)
60
61 //=======================================================================
62 //function : AIS_OffsetDimension
63 //purpose  : 
64 //=======================================================================
65 AIS_OffsetDimension::AIS_OffsetDimension(const TopoDS_Shape& FistShape, 
66                                          const TopoDS_Shape& SecondShape,
67                                          const Standard_Real aVal,
68                                          const TCollection_ExtendedString& aText)
69 :AIS_Relation(),
70 myFAttach(0.,0.,0.),
71 mySAttach(0.,0.,0.)
72 {
73   myFShape = FistShape;
74   mySShape = SecondShape;
75   mySymbolPrs = DsgPrs_AS_BOTHAR;
76   myVal = aVal;
77   myText = aText;
78   //myArrowSize = fabs (myVal/5.);
79   myArrowSize = fabs (myVal/10.0);
80   if (myArrowSize > 30.) myArrowSize = 30.;
81   if (myArrowSize < 15.) myArrowSize = 15.;
82   //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
83 }
84
85 //=======================================================================
86 //function : Compute
87 //purpose  : 
88 //=======================================================================
89 void AIS_OffsetDimension::Compute(const Handle(PrsMgr_PresentationManager3d)&,
90                                   const Handle(Prs3d_Presentation)& aprs,
91                                   const Standard_Integer)
92 {
93   aprs->Clear();
94
95   //cout << endl << "This is strange Offset Dimension!" << endl;
96
97   gp_Trsf aInvertTrsf = myRelativePos;
98   //myArrowSize = fabs (myVal/5.);
99   myArrowSize = fabs (myVal/10.0);
100   if (myArrowSize > 30.) myArrowSize = 30.;
101   if (myArrowSize < 15.) myArrowSize = 15.;
102   //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
103   
104   BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
105   BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));
106   
107   if (surf1.GetType() == GeomAbs_Cylinder || 
108       surf1.GetType() == GeomAbs_Cone     || 
109       surf1.GetType() == GeomAbs_Torus) {
110     if (surf2.GetType() == GeomAbs_Cylinder ||
111         surf2.GetType() == GeomAbs_Cone     ||
112         surf2.GetType() == GeomAbs_Torus) {
113       ComputeTwoAxesOffset(aprs, aInvertTrsf);
114     } else {
115       ComputeAxeFaceOffset(aprs, aInvertTrsf);
116     }
117   }
118   else {
119     //myDirAttach : oriente de myFShape vers mySShape
120     gp_Pln aPln = surf1.Plane();
121     gp_Pnt aPnt = aPln.Location();
122     
123     gp_Pln bPln = surf2.Plane();
124     
125     Standard_Real uPnt, vPnt;
126     ElSLib::Parameters (bPln , aPnt , uPnt, vPnt);
127     gp_Pnt bPnt = ElSLib::Value (uPnt, vPnt, bPln);
128     if (aPnt.IsEqual(bPnt,Precision::Confusion())) {
129       gp_Ax1 aAx1 = aPln.Axis();
130       myDirAttach = aAx1.Direction();
131     } else {
132       gp_Vec aVec (aPnt,bPnt);
133       myDirAttach.SetCoord(aVec.X(),aVec.Y(),aVec.Z());
134     }
135     ComputeTwoFacesOffset(aprs, aInvertTrsf);
136   }  
137 }
138
139
140 //=======================================================================
141 //function : Compute
142 //purpose  : to avoid warning at compilation (SUN)
143 //=======================================================================
144 void AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& /*aProjector*/,
145                                   const Handle(Prs3d_Presentation)& /*aPresentation*/)
146 {
147 // Standard_NotImplemented::Raise("AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& aProjector,const Handle(Prs3d_Presentation)& aPresentation)");
148 // PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
149 }
150
151 void AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& aProjector, 
152                                   const Handle(Geom_Transformation)& aTransformation,
153                                   const Handle(Prs3d_Presentation)& aPresentation)
154 {
155 // Standard_NotImplemented::Raise("AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)&,const Handle(Geom_Transformation)&,const Handle(Prs3d_Presentation)&)");
156  PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
157 }
158
159 //=======================================================================
160 //function : ComputeSelection
161 //purpose  : 
162 //=======================================================================
163 void AIS_OffsetDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSel, 
164                                            const Standard_Integer)
165 {
166   //myArrowSize = fabs (myVal/5.);
167   myArrowSize = fabs (myVal/10.0);
168   if (myArrowSize > 30.) myArrowSize = 30.;
169   if (myArrowSize < 15.) myArrowSize = 15.;
170   //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
171   gp_Pnt myTFAttach = myFAttach.Transformed (myRelativePos);
172   gp_Pnt myTSAttach = mySAttach.Transformed (myRelativePos);
173   gp_Dir myTDirAttach = myDirAttach.Transformed (myRelativePos);
174   gp_Dir myTDirAttach2 = myDirAttach2.Transformed (myRelativePos);
175   gp_Pnt Tcurpos = myPosition.Transformed (myRelativePos);;
176
177   gp_Lin L1 (myTFAttach,myTDirAttach);
178   gp_Lin L2 (myTSAttach,myTDirAttach2);
179   gp_Pnt Proj1 = ElCLib::Value(ElCLib::Parameter(L1,Tcurpos),L1);
180   gp_Pnt Proj2 = ElCLib::Value(ElCLib::Parameter(L2,Tcurpos),L2);
181   gp_Lin L3;
182
183   Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
184
185   if (!Proj1.IsEqual(Proj2,Precision::Confusion())) {
186     L3 = gce_MakeLin(Proj1,Proj2);
187   }
188   else {    // cas ou la dimension est nulle
189     if (!Proj1.IsEqual(Tcurpos,Precision::Confusion())) {
190       gp_Vec v3 (Proj1,Tcurpos);
191       gp_Dir d3 (v3);
192       L3 = gce_MakeLin(Proj1,d3);
193     } else {
194       L3 = gce_MakeLin(Proj1,myTDirAttach);
195     }
196
197   // Text
198     Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
199     Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
200                                                                      Tcurpos.X(),
201                                                                      Tcurpos.Y(),
202                                                                      Tcurpos.Z(),
203                                                                      Tcurpos.X() + size,
204                                                                      Tcurpos.Y() + size,
205                                                                      Tcurpos.Z() + size);
206     aSel->Add(box);
207   }
208
209   Standard_Real parmin,parmax,parcur;
210   parmin = ElCLib::Parameter(L3,Proj1);
211   parmax = parmin;
212
213   parcur = ElCLib::Parameter(L3,Proj2);
214   parmin = Min(parmin,parcur);
215   parmax = Max(parmax,parcur);
216
217   parcur = ElCLib::Parameter(L3,Tcurpos);
218   parmin = Min(parmin,parcur);
219   parmax = Max(parmax,parcur);
220
221   gp_Pnt PointMin = ElCLib::Value(parmin,L3);
222   gp_Pnt PointMax = ElCLib::Value(parmax,L3);
223
224   Handle(Select3D_SensitiveSegment) seg;
225   if (!PointMin.IsEqual(PointMax,Precision::Confusion())) {
226     seg = new Select3D_SensitiveSegment(own,
227                                         PointMin,
228                                         PointMax);
229     aSel->Add(seg);
230   }
231   
232   if (!myTFAttach.IsEqual(Proj1,Precision::Confusion())) {
233     seg = new Select3D_SensitiveSegment(own,
234                                         myTFAttach,
235                                         Proj1);
236     aSel->Add(seg);
237   }
238   if (!myTSAttach.IsEqual(Proj2,Precision::Confusion())) {
239     seg = new Select3D_SensitiveSegment(own,
240                                         myTSAttach,
241                                         Proj2);
242     aSel->Add(seg);
243   }
244 }
245
246 //=======================================================================
247 //function : ComputeTwoAxesOffset
248 //purpose  : 
249 //=======================================================================
250 void AIS_OffsetDimension::ComputeTwoAxesOffset(const Handle(Prs3d_Presentation)& aprs, 
251                                                 const gp_Trsf& aTrsf)
252 {
253   BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
254   BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));
255
256   gp_Ax1 Ax1Surf1, Ax1Surf2;
257
258   if (surf1.GetType() == GeomAbs_Cylinder) {
259     gp_Cylinder aCyl= surf1.Cylinder();
260     Ax1Surf1 = aCyl.Axis();    
261   } else if (surf1.GetType() == GeomAbs_Cone) {
262     gp_Cone aCone= surf1.Cone();
263     Ax1Surf1 = aCone.Axis();    
264   } else if (surf1.GetType() == GeomAbs_Torus) {
265     gp_Torus aTore= surf1.Torus();
266     Ax1Surf1 = aTore.Axis();    
267   }
268   Standard_Real FirstUParam = surf1.FirstUParameter();
269   Standard_Real FirstVParam = surf1.FirstVParameter();
270   Standard_Real LastVParam  = surf1.LastVParameter();
271   gp_Pnt P1First = surf1.Value(FirstUParam,FirstVParam);
272   gp_Pnt P1Last  = surf1.Value(FirstUParam,LastVParam);
273
274
275   if (surf2.GetType() == GeomAbs_Cylinder) {
276     gp_Cylinder aCyl= surf2.Cylinder();
277     Ax1Surf2 = aCyl.Axis();    
278   } else if (surf2.GetType() == GeomAbs_Cone) {
279     gp_Cone aCone= surf2.Cone();
280     Ax1Surf2 = aCone.Axis();    
281   } else if (surf2.GetType() == GeomAbs_Torus) {
282     gp_Torus aTore= surf2.Torus();
283     Ax1Surf2 = aTore.Axis();    
284   }
285   FirstUParam = surf2.FirstUParameter();
286   FirstVParam = surf2.FirstVParameter();
287   LastVParam  = surf2.LastVParameter();
288   gp_Pnt P2First = surf2.Value(FirstUParam,FirstVParam);
289   gp_Pnt P2Last  = surf2.Value(FirstUParam,LastVParam);
290
291   
292   
293   myFAttach = Ax1Surf1.Location();
294   mySAttach = Ax1Surf2.Location();
295   myDirAttach = Ax1Surf1.Direction();
296   myDirAttach2 = myDirAttach;
297   gp_Pnt curpos;
298   gp_Lin aProjLine  = gce_MakeLin(myFAttach,myDirAttach);
299
300   if (myAutomaticPosition) {
301     curpos.SetX ( (myFAttach.X() +  mySAttach.X()) /2. + 0.01);
302     curpos.SetY ( (myFAttach.Y() +  mySAttach.Y()) /2. + 0.01);
303     curpos.SetZ ( (myFAttach.Z() +  mySAttach.Z()) /2. + 0.01);
304     // + 0.01 pour eviter un raise de ComputeSelection...
305
306     myPosition = curpos;
307   } 
308   else {
309     curpos = myPosition;
310   }
311   
312   curpos = ElCLib::Value(ElCLib::Parameter(aProjLine,curpos),aProjLine);
313   // on projette pour la presentation
314
315   gp_Pnt P1FirstProj  = ElCLib::Value(ElCLib::Parameter(aProjLine,P1First),aProjLine);
316   gp_Pnt P1LastProj   = ElCLib::Value(ElCLib::Parameter(aProjLine,P1Last),aProjLine);
317   if (P1FirstProj.Distance(curpos) > P1LastProj.Distance(curpos))
318     myFAttach = P1FirstProj;
319   else
320     myFAttach = P1LastProj;
321   
322   gp_Pnt P2FirstProj  = ElCLib::Value(ElCLib::Parameter(aProjLine,P2First),aProjLine);
323   gp_Pnt P2LastProj   = ElCLib::Value(ElCLib::Parameter(aProjLine,P2Last),aProjLine);
324   if (P2FirstProj.Distance(curpos) > P2LastProj.Distance(curpos))
325     mySAttach = P2FirstProj;
326   else
327     mySAttach = P2LastProj;
328
329
330   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
331   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
332   //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
333   arr->SetLength(myArrowSize);
334   arr = la->ArrowAspect();
335   arr->SetLength(myArrowSize);
336
337   gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
338   gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
339   gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
340   gp_Dir myTDirAttach2 = myTDirAttach;
341   gp_Pnt Tcurpos = curpos.Transformed (aTrsf);
342  
343   if (myIsSetBndBox)
344     Tcurpos = AIS::TranslatePointToBound( Tcurpos, myDirAttach, myBndBox );
345
346   DsgPrs_OffsetPresentation::AddAxes(aprs,
347                                      myDrawer,
348                                      myText,
349                                      myTFAttach,
350                                      myTSAttach,
351                                      myTDirAttach,
352                                      myTDirAttach2,
353                                      Tcurpos);
354   
355   BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
356   TopoDS_Shape myTFShape = transform1.Shape();
357   BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
358   TopoDS_Shape myTSShape = transform2.Shape();
359
360   StdPrs_WFShape::Add (aprs, myTFShape, myDrawer);
361   StdPrs_WFShape::Add (aprs, myTSShape, myDrawer);
362 }
363
364 //=======================================================================
365 //function : ComputeTwoFacesOffset
366 //purpose  : 
367 //=======================================================================
368 void AIS_OffsetDimension::ComputeTwoFacesOffset(const Handle(Prs3d_Presentation)& aprs, 
369                                                 const gp_Trsf& aTrsf)
370 {
371   gp_Dir norm1 = myDirAttach;
372   gp_Pnt curpos;
373   gp_Ax2 myax2;
374   if (myAutomaticPosition && ! myIsSetBndBox) {
375     TopExp_Explorer explo(myFShape,TopAbs_VERTEX);
376     if (explo.More()) {
377       TopoDS_Vertex vertref = TopoDS::Vertex(explo.Current());
378       myFAttach = BRep_Tool::Pnt(vertref);
379       gp_Vec trans = norm1.XYZ()*fabs(myVal/2);
380       gp_Ax2 ax2(myFAttach,norm1);
381       myDirAttach = ax2.XDirection();
382       curpos = myFAttach.Translated(trans);
383       if (myVal <= Precision::Confusion()) {
384         gp_Vec vecnorm1 = norm1.XYZ()*.001;
385         curpos.Translate(vecnorm1);
386       }
387       myPosition = curpos;
388       myax2 = ax2;
389     }
390   }
391   else {
392     if (myAutomaticPosition && myIsSetBndBox)
393       {
394         Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
395         myBndBox.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
396         myPosition.SetCoord( aXmax, aYmax, aZmax );
397       }
398      
399     curpos = myPosition;
400     myFAttach = AIS::Nearest(myFShape,curpos);
401     if (myFAttach.Distance(curpos) <= Precision::Confusion()) {
402       gp_Ax2 ax2(myFAttach,norm1);
403       myDirAttach = ax2.XDirection();
404       myax2 = ax2;
405     }
406     else {
407       gp_Dir orient(myFAttach.XYZ()-curpos.XYZ());
408       gp_Ax2 ax2(myFAttach,norm1);
409       if (orient.Angle(norm1) <= Precision::Angular()) {
410         myDirAttach = ax2.XDirection();
411       }
412       else {
413         gp_Dir adir = norm1 ^ orient;
414         myDirAttach = adir ^ norm1;
415       }
416       myax2 = ax2;
417     }
418   }
419   // en attendant mieux
420   mySAttach = AIS::Nearest(mySShape,curpos);
421   gp_Ax3 anax3 (myax2);
422   gp_Pln apln (anax3);
423   
424   //gp_Pnt proj2;
425   Standard_Real u2,v2, uatt, vatt;
426   ElSLib::Parameters (apln , mySAttach, uatt,vatt);
427   ElSLib::Parameters (apln , curpos   , u2,v2);
428   
429   if (uatt== u2 && vatt == v2) {
430     myDirAttach2 = myDirAttach;
431   } else {
432     gp_Vec avec (ElSLib::Value (uatt,vatt, apln) , ElSLib::Value (u2,v2, apln));
433     myDirAttach2.SetCoord (avec.X(),avec.Y(),avec.Z());
434   }
435   
436   Handle(Prs3d_DimensionAspect) la = myDrawer->DimensionAspect();
437   Handle(Prs3d_ArrowAspect) arr = la->ArrowAspect();
438   //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
439   arr->SetLength(myArrowSize);
440   arr = la->ArrowAspect();
441   arr->SetLength(myArrowSize);
442
443   gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
444   gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
445   gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
446   gp_Dir myTDirAttach2 = myDirAttach2.Transformed (aTrsf);
447   gp_Pnt Tcurpos = curpos.Transformed (aTrsf);
448
449 /*
450   if (myIsSetBndBox)
451     {
452       BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
453       Tcurpos = AIS::TranslatePointToBound( Tcurpos, surf1.Plane().XAxis().Direction(), myBndBox );
454     }
455 */
456   DsgPrs_OffsetPresentation::Add(aprs,
457                                  myDrawer,
458                                  myText,
459                                  myTFAttach,
460                                  myTSAttach,
461                                  myTDirAttach,
462                                  myTDirAttach2,
463                                  Tcurpos);
464   
465
466   BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
467   TopoDS_Shape myTFShape = transform1.Shape();
468   BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
469   TopoDS_Shape myTSShape = transform2.Shape();
470
471   StdPrs_WFShape::Add (aprs, myTFShape, myDrawer);
472   StdPrs_WFShape::Add (aprs, myTSShape, myDrawer);
473 }
474
475 //=======================================================================
476 //function : ComputeAxeFaceOffset
477 //purpose  : 
478 //=======================================================================
479 void AIS_OffsetDimension::ComputeAxeFaceOffset(const Handle(Prs3d_Presentation)& aprs, 
480                                                const gp_Trsf& aTrsf)
481 {
482   BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
483   TopoDS_Shape myTFShape = transform1.Shape();
484   BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
485   TopoDS_Shape myTSShape = transform2.Shape();
486
487   StdPrs_WFShape::Add (aprs, myTFShape, myDrawer);
488   StdPrs_WFShape::Add (aprs, myTSShape, myDrawer);
489 }
490