0023634: Eliminate Polyline and Polygon usage in drawers
[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-2012 OPEN CASCADE SAS
5 //
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
10 //
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 //
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
20
21
22
23 #include <Standard_NotImplemented.hxx>
24
25 #include <AIS_OffsetDimension.ixx>
26
27 #include <AIS.hxx>
28 #include <AIS_Drawer.hxx>
29 #include <BRepBuilderAPI_Transform.hxx>
30 #include <BRepAdaptor_Surface.hxx>
31 #include <BRep_Tool.hxx>
32 #include <DsgPrs_OffsetPresentation.hxx>
33 #include <ElCLib.hxx>
34 #include <ElSLib.hxx>
35 #include <GeomAbs_SurfaceType.hxx>
36 #include <Graphic3d_Structure.hxx>
37 #include <Precision.hxx>
38 #include <Prs3d_ArrowAspect.hxx>
39 #include <Prs3d_Drawer.hxx>
40 #include <Prs3d_LengthAspect.hxx>
41 #include <Prs3d_LineAspect.hxx>
42 #include <Select3D_SensitiveBox.hxx>
43 #include <Select3D_SensitiveSegment.hxx>
44 #include <SelectMgr_EntityOwner.hxx>
45 #include <StdPrs_WFDeflectionShape.hxx>
46 #include <TColStd_Array2OfReal.hxx>
47 #include <TCollection_ExtendedString.hxx>
48 #include <TopExp_Explorer.hxx>
49 #include <TopoDS.hxx>
50 #include <TopoDS_Vertex.hxx>
51 #include <gce_MakeLin.hxx>
52 #include <gp_Ax1.hxx>
53 #include <gp_Ax2.hxx>
54 #include <gp_Ax3.hxx>
55 #include <gp_Lin.hxx>
56 #include <gp_Pln.hxx>
57 #include <gp_Pnt.hxx>
58 #include <gp_Trsf.hxx>
59 #include <gp_Vec.hxx>
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 //=======================================================================
152 //function : Compute
153 //purpose  : to avoid warning at compilation (SUN)
154 //=======================================================================
155 void AIS_OffsetDimension::Compute(const Handle(PrsMgr_PresentationManager2d)& /*aPresentationManager2d*/,
156                                   const Handle(Graphic2d_GraphicObject)& /*aGraphicObject*/,
157                                   const Standard_Integer /*anInteger*/)
158 {
159 // Standard_NotImplemented::Raise("AIS_OffsetDimension::Compute(const Handle(PrsMgr_PresentationManager2d)&,const Handle(Graphic2d_GraphicObject)&,const Standard_Integer)");
160 // PrsMgr_PresentableObject::Compute( aPresentationManager2d ,aGraphicObject,anInteger) ;
161 }
162
163 void AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)& aProjector, 
164                                   const Handle(Geom_Transformation)& aTransformation,
165                                   const Handle(Prs3d_Presentation)& aPresentation)
166 {
167 // Standard_NotImplemented::Raise("AIS_OffsetDimension::Compute(const Handle(Prs3d_Projector)&,const Handle(Geom_Transformation)&,const Handle(Prs3d_Presentation)&)");
168  PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
169 }
170
171 //=======================================================================
172 //function : ComputeSelection
173 //purpose  : 
174 //=======================================================================
175 void AIS_OffsetDimension::ComputeSelection(const Handle(SelectMgr_Selection)& aSel, 
176                                            const Standard_Integer)
177 {
178   //myArrowSize = fabs (myVal/5.);
179   myArrowSize = fabs (myVal/10.0);
180   if (myArrowSize > 30.) myArrowSize = 30.;
181   if (myArrowSize < 15.) myArrowSize = 15.;
182   //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
183   gp_Pnt myTFAttach = myFAttach.Transformed (myRelativePos);
184   gp_Pnt myTSAttach = mySAttach.Transformed (myRelativePos);
185   gp_Dir myTDirAttach = myDirAttach.Transformed (myRelativePos);
186   gp_Dir myTDirAttach2 = myDirAttach2.Transformed (myRelativePos);
187   gp_Pnt Tcurpos = myPosition.Transformed (myRelativePos);;
188
189   gp_Lin L1 (myTFAttach,myTDirAttach);
190   gp_Lin L2 (myTSAttach,myTDirAttach2);
191   gp_Pnt Proj1 = ElCLib::Value(ElCLib::Parameter(L1,Tcurpos),L1);
192   gp_Pnt Proj2 = ElCLib::Value(ElCLib::Parameter(L2,Tcurpos),L2);
193   gp_Lin L3;
194
195   Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
196
197   if (!Proj1.IsEqual(Proj2,Precision::Confusion())) {
198     L3 = gce_MakeLin(Proj1,Proj2);
199   }
200   else {    // cas ou la dimension est nulle
201     if (!Proj1.IsEqual(Tcurpos,Precision::Confusion())) {
202       gp_Vec v3 (Proj1,Tcurpos);
203       gp_Dir d3 (v3);
204       L3 = gce_MakeLin(Proj1,d3);
205     } else {
206       L3 = gce_MakeLin(Proj1,myTDirAttach);
207     }
208
209   // Text
210     Standard_Real size(Min(myVal/100.+1.e-6,myArrowSize+1.e-6));
211     Handle( Select3D_SensitiveBox ) box = new Select3D_SensitiveBox( own,
212                                                                      Tcurpos.X(),
213                                                                      Tcurpos.Y(),
214                                                                      Tcurpos.Z(),
215                                                                      Tcurpos.X() + size,
216                                                                      Tcurpos.Y() + size,
217                                                                      Tcurpos.Z() + size);
218     aSel->Add(box);
219   }
220
221   Standard_Real parmin,parmax,parcur;
222   parmin = ElCLib::Parameter(L3,Proj1);
223   parmax = parmin;
224
225   parcur = ElCLib::Parameter(L3,Proj2);
226   parmin = Min(parmin,parcur);
227   parmax = Max(parmax,parcur);
228
229   parcur = ElCLib::Parameter(L3,Tcurpos);
230   parmin = Min(parmin,parcur);
231   parmax = Max(parmax,parcur);
232
233   gp_Pnt PointMin = ElCLib::Value(parmin,L3);
234   gp_Pnt PointMax = ElCLib::Value(parmax,L3);
235
236   Handle(Select3D_SensitiveSegment) seg;
237   if (!PointMin.IsEqual(PointMax,Precision::Confusion())) {
238     seg = new Select3D_SensitiveSegment(own,
239                                         PointMin,
240                                         PointMax);
241     aSel->Add(seg);
242   }
243   
244   if (!myTFAttach.IsEqual(Proj1,Precision::Confusion())) {
245     seg = new Select3D_SensitiveSegment(own,
246                                         myTFAttach,
247                                         Proj1);
248     aSel->Add(seg);
249   }
250   if (!myTSAttach.IsEqual(Proj2,Precision::Confusion())) {
251     seg = new Select3D_SensitiveSegment(own,
252                                         myTSAttach,
253                                         Proj2);
254     aSel->Add(seg);
255   }
256 }
257
258 //=======================================================================
259 //function : ComputeTwoAxesOffset
260 //purpose  : 
261 //=======================================================================
262 void AIS_OffsetDimension::ComputeTwoAxesOffset(const Handle(Prs3d_Presentation)& aprs, 
263                                                 const gp_Trsf& aTrsf)
264 {
265   BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
266   BRepAdaptor_Surface surf2(TopoDS::Face(mySShape));
267
268   gp_Ax1 Ax1Surf1, Ax1Surf2;
269
270   if (surf1.GetType() == GeomAbs_Cylinder) {
271     gp_Cylinder aCyl= surf1.Cylinder();
272     Ax1Surf1 = aCyl.Axis();    
273   } else if (surf1.GetType() == GeomAbs_Cone) {
274     gp_Cone aCone= surf1.Cone();
275     Ax1Surf1 = aCone.Axis();    
276   } else if (surf1.GetType() == GeomAbs_Torus) {
277     gp_Torus aTore= surf1.Torus();
278     Ax1Surf1 = aTore.Axis();    
279   }
280   Standard_Real FirstUParam = surf1.FirstUParameter();
281   Standard_Real FirstVParam = surf1.FirstVParameter();
282   Standard_Real LastVParam  = surf1.LastVParameter();
283   gp_Pnt P1First = surf1.Value(FirstUParam,FirstVParam);
284   gp_Pnt P1Last  = surf1.Value(FirstUParam,LastVParam);
285
286
287   if (surf2.GetType() == GeomAbs_Cylinder) {
288     gp_Cylinder aCyl= surf2.Cylinder();
289     Ax1Surf2 = aCyl.Axis();    
290   } else if (surf2.GetType() == GeomAbs_Cone) {
291     gp_Cone aCone= surf2.Cone();
292     Ax1Surf2 = aCone.Axis();    
293   } else if (surf2.GetType() == GeomAbs_Torus) {
294     gp_Torus aTore= surf2.Torus();
295     Ax1Surf2 = aTore.Axis();    
296   }
297   FirstUParam = surf2.FirstUParameter();
298   FirstVParam = surf2.FirstVParameter();
299   LastVParam  = surf2.LastVParameter();
300   gp_Pnt P2First = surf2.Value(FirstUParam,FirstVParam);
301   gp_Pnt P2Last  = surf2.Value(FirstUParam,LastVParam);
302
303   
304   
305   myFAttach = Ax1Surf1.Location();
306   mySAttach = Ax1Surf2.Location();
307   myDirAttach = Ax1Surf1.Direction();
308   myDirAttach2 = myDirAttach;
309   gp_Pnt curpos;
310   gp_Lin aProjLine  = gce_MakeLin(myFAttach,myDirAttach);
311
312   if (myAutomaticPosition) {
313     curpos.SetX ( (myFAttach.X() +  mySAttach.X()) /2. + 0.01);
314     curpos.SetY ( (myFAttach.Y() +  mySAttach.Y()) /2. + 0.01);
315     curpos.SetZ ( (myFAttach.Z() +  mySAttach.Z()) /2. + 0.01);
316     // + 0.01 pour eviter un raise de ComputeSelection...
317
318     myPosition = curpos;
319   } 
320   else {
321     curpos = myPosition;
322   }
323   
324   curpos = ElCLib::Value(ElCLib::Parameter(aProjLine,curpos),aProjLine);
325   // on projette pour la presentation
326
327   gp_Pnt P1FirstProj  = ElCLib::Value(ElCLib::Parameter(aProjLine,P1First),aProjLine);
328   gp_Pnt P1LastProj   = ElCLib::Value(ElCLib::Parameter(aProjLine,P1Last),aProjLine);
329   if (P1FirstProj.Distance(curpos) > P1LastProj.Distance(curpos))
330     myFAttach = P1FirstProj;
331   else
332     myFAttach = P1LastProj;
333   
334   gp_Pnt P2FirstProj  = ElCLib::Value(ElCLib::Parameter(aProjLine,P2First),aProjLine);
335   gp_Pnt P2LastProj   = ElCLib::Value(ElCLib::Parameter(aProjLine,P2Last),aProjLine);
336   if (P2FirstProj.Distance(curpos) > P2LastProj.Distance(curpos))
337     mySAttach = P2FirstProj;
338   else
339     mySAttach = P2LastProj;
340
341
342   Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
343   Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
344   //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
345   arr->SetLength(myArrowSize);
346   arr = la->Arrow2Aspect();
347   arr->SetLength(myArrowSize);
348
349   gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
350   gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
351   gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
352   gp_Dir myTDirAttach2 = myTDirAttach;
353   gp_Pnt Tcurpos = curpos.Transformed (aTrsf);
354  
355   if (myIsSetBndBox)
356     Tcurpos = AIS::TranslatePointToBound( Tcurpos, myDirAttach, myBndBox );
357
358   DsgPrs_OffsetPresentation::AddAxes(aprs,
359                                      myDrawer,
360                                      myText,
361                                      myTFAttach,
362                                      myTSAttach,
363                                      myTDirAttach,
364                                      myTDirAttach2,
365                                      Tcurpos);
366   
367   BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
368   TopoDS_Shape myTFShape = transform1.Shape();
369   BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
370   TopoDS_Shape myTSShape = transform2.Shape();
371
372   StdPrs_WFDeflectionShape::Add(aprs, myTFShape, myDrawer);
373   StdPrs_WFDeflectionShape::Add(aprs, myTSShape, myDrawer);
374 }
375
376 //=======================================================================
377 //function : ComputeTwoFacesOffset
378 //purpose  : 
379 //=======================================================================
380 void AIS_OffsetDimension::ComputeTwoFacesOffset(const Handle(Prs3d_Presentation)& aprs, 
381                                                 const gp_Trsf& aTrsf)
382 {
383   gp_Dir norm1 = myDirAttach;
384   gp_Pnt curpos;
385   gp_Ax2 myax2;
386   if (myAutomaticPosition && ! myIsSetBndBox) {
387     TopExp_Explorer explo(myFShape,TopAbs_VERTEX);
388     if (explo.More()) {
389       TopoDS_Vertex vertref = TopoDS::Vertex(explo.Current());
390       myFAttach = BRep_Tool::Pnt(vertref);
391       gp_Vec trans = norm1.XYZ()*fabs(myVal/2);
392       gp_Ax2 ax2(myFAttach,norm1);
393       myDirAttach = ax2.XDirection();
394       curpos = myFAttach.Translated(trans);
395       if (myVal <= Precision::Confusion()) {
396         gp_Vec vecnorm1 = norm1.XYZ()*.001;
397         curpos.Translate(vecnorm1);
398       }
399       myPosition = curpos;
400       myax2 = ax2;
401     }
402   }
403   else {
404     if (myAutomaticPosition && myIsSetBndBox)
405       {
406         Standard_Real aXmin, aYmin, aZmin, aXmax, aYmax, aZmax;
407         myBndBox.Get( aXmin, aYmin, aZmin, aXmax, aYmax, aZmax );
408         myPosition.SetCoord( aXmax, aYmax, aZmax );
409       }
410      
411     curpos = myPosition;
412     myFAttach = AIS::Nearest(myFShape,curpos);
413     if (myFAttach.Distance(curpos) <= Precision::Confusion()) {
414       gp_Ax2 ax2(myFAttach,norm1);
415       myDirAttach = ax2.XDirection();
416       myax2 = ax2;
417     }
418     else {
419       gp_Dir orient(myFAttach.XYZ()-curpos.XYZ());
420       gp_Ax2 ax2(myFAttach,norm1);
421       if (orient.Angle(norm1) <= Precision::Angular()) {
422         myDirAttach = ax2.XDirection();
423       }
424       else {
425         gp_Dir adir = norm1 ^ orient;
426         myDirAttach = adir ^ norm1;
427       }
428       myax2 = ax2;
429     }
430   }
431   // en attendant mieux
432   mySAttach = AIS::Nearest(mySShape,curpos);
433   gp_Ax3 anax3 (myax2);
434   gp_Pln apln (anax3);
435   
436   //gp_Pnt proj2;
437   Standard_Real u2,v2, uatt, vatt;
438   ElSLib::Parameters (apln , mySAttach, uatt,vatt);
439   ElSLib::Parameters (apln , curpos   , u2,v2);
440   
441   if (uatt== u2 && vatt == v2) {
442     myDirAttach2 = myDirAttach;
443   } else {
444     gp_Vec avec (ElSLib::Value (uatt,vatt, apln) , ElSLib::Value (u2,v2, apln));
445     myDirAttach2.SetCoord (avec.X(),avec.Y(),avec.Z());
446   }
447   
448   Handle(Prs3d_LengthAspect) la = myDrawer->LengthAspect();
449   Handle(Prs3d_ArrowAspect) arr = la->Arrow1Aspect();
450   //cout<<"AIS_OffsetDimension::AIS_OffsetDimension " <<  myArrowSize << " myArrowSize"<<endl;
451   arr->SetLength(myArrowSize);
452   arr = la->Arrow2Aspect();
453   arr->SetLength(myArrowSize);
454
455   gp_Pnt myTFAttach = myFAttach.Transformed (aTrsf);
456   gp_Pnt myTSAttach = mySAttach.Transformed (aTrsf);
457   gp_Dir myTDirAttach = myDirAttach.Transformed (aTrsf);
458   gp_Dir myTDirAttach2 = myDirAttach2.Transformed (aTrsf);
459   gp_Pnt Tcurpos = curpos.Transformed (aTrsf);
460
461 /*
462   if (myIsSetBndBox)
463     {
464       BRepAdaptor_Surface surf1(TopoDS::Face(myFShape));
465       Tcurpos = AIS::TranslatePointToBound( Tcurpos, surf1.Plane().XAxis().Direction(), myBndBox );
466     }
467 */
468   DsgPrs_OffsetPresentation::Add(aprs,
469                                  myDrawer,
470                                  myText,
471                                  myTFAttach,
472                                  myTSAttach,
473                                  myTDirAttach,
474                                  myTDirAttach2,
475                                  Tcurpos);
476   
477
478   BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
479   TopoDS_Shape myTFShape = transform1.Shape();
480   BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
481   TopoDS_Shape myTSShape = transform2.Shape();
482
483   StdPrs_WFDeflectionShape::Add(aprs, myTFShape, myDrawer);
484   StdPrs_WFDeflectionShape::Add(aprs, myTSShape, myDrawer);
485
486 }
487
488 //=======================================================================
489 //function : ComputeAxeFaceOffset
490 //purpose  : 
491 //=======================================================================
492 void AIS_OffsetDimension::ComputeAxeFaceOffset(const Handle(Prs3d_Presentation)& aprs, 
493                                                const gp_Trsf& aTrsf)
494 {
495   BRepBuilderAPI_Transform transform1 (myFShape, aTrsf, Standard_True);
496   TopoDS_Shape myTFShape = transform1.Shape();
497   BRepBuilderAPI_Transform transform2 (mySShape, aTrsf, Standard_True);
498   TopoDS_Shape myTSShape = transform2.Shape();
499   
500   StdPrs_WFDeflectionShape::Add(aprs, myTFShape, myDrawer);
501   StdPrs_WFDeflectionShape::Add(aprs, myTSShape, myDrawer);
502 }
503