Integration of OCCT 6.5.0 from SVN
[occt.git] / samples / java / src / ISession2D / ISession2D_Curve.cxx
1 #include <ISession2D_Curve.ixx>
2 #include <Geom2d_Curve.hxx>
3 #include <Geom2dAdaptor_Curve.hxx>
4 #include <Geom2d_BezierCurve.hxx>
5 #include <Geom2d_BSplineCurve.hxx>
6 #include <Geom2dLProp_CLProps2d.hxx>
7 #include <GGraphic2d_SetOfCurves.hxx>
8 #include <Graphic2d_Array1OfVertex.hxx>
9 #include <Graphic2d_Vertex.hxx>
10 #include <Graphic2d_Polyline.hxx>
11 #include <Graphic2d_Segment.hxx>
12 #include <gp_Pnt2d.hxx>
13 #include <TColStd_Array1OfReal.hxx>
14
15
16 ISession2D_Curve::ISession2D_Curve(const Handle(Geom2d_Curve)& aGeom2dCurve,const Aspect_TypeOfLine aTypeOfLine,const Aspect_WidthOfLine aWidthOfLine,const Standard_Integer aColorIndex)
17      :AIS_InteractiveObject()
18 {
19   myGeom2dCurve = aGeom2dCurve;
20   myTypeOfLine = aTypeOfLine;
21   myWidthOfLine = aWidthOfLine;
22   myColorIndex = aColorIndex;
23   myDisplayPole = Standard_True;
24   myDisplayCurbure = Standard_False;
25   myDiscretisation = 20;
26   myradiusmax = 10;
27   myradiusratio = 1;
28 }
29
30  void ISession2D_Curve::Compute(const Handle(PrsMgr_PresentationManager2d)& ,const Handle(Graphic2d_GraphicObject)& aGrObj,const Standard_Integer ) 
31 {
32   Handle(GGraphic2d_SetOfCurves) segment;
33   segment = new GGraphic2d_SetOfCurves(aGrObj);
34   
35   segment->Add(myGeom2dCurve);
36   
37   segment->SetColorIndex (myColorIndex);
38   segment->SetWidthIndex (myWidthOfLine + 1);
39   segment->SetTypeIndex  (myTypeOfLine + 1);
40
41   Geom2dAdaptor_Curve anAdaptor(myGeom2dCurve);
42   if (myDisplayPole)
43     {
44       if (anAdaptor.GetType() == GeomAbs_BezierCurve)
45         {
46           Handle(Geom2d_BezierCurve) aBezier = anAdaptor.Bezier();
47           Graphic2d_Array1OfVertex anArrayOfVertex(1,aBezier->NbPoles());
48           for (int i=1;i<=aBezier->NbPoles();i++)
49             {
50               gp_Pnt2d CurrentPoint = aBezier->Pole(i);
51               Graphic2d_Vertex aVertex(CurrentPoint.X(),CurrentPoint.Y());
52               anArrayOfVertex(i) = aVertex;
53             }
54           Handle(Graphic2d_Polyline) aPolyline = new Graphic2d_Polyline(aGrObj, anArrayOfVertex);
55         }
56
57       if (anAdaptor.GetType() == GeomAbs_BSplineCurve)
58         {
59           Handle(Geom2d_BSplineCurve) aBSpline = anAdaptor.BSpline();
60           Graphic2d_Array1OfVertex anArrayOfVertex(1,aBSpline->NbPoles());
61           for (int i=1;i<=aBSpline->NbPoles();i++)
62             {
63               gp_Pnt2d CurrentPoint = aBSpline->Pole(i);
64               Graphic2d_Vertex aVertex(CurrentPoint.X(),CurrentPoint.Y());
65               anArrayOfVertex(i) = aVertex;
66             }
67           Handle(Graphic2d_Polyline) aPolyline = new Graphic2d_Polyline(aGrObj,anArrayOfVertex);
68         }
69     }
70
71   if (myDisplayCurbure && (anAdaptor.GetType() != GeomAbs_Line))
72     {
73       Standard_Integer ii;
74       Standard_Integer intrv, nbintv = anAdaptor.NbIntervals(GeomAbs_CN);
75       TColStd_Array1OfReal TI(1,nbintv+1);
76       anAdaptor.Intervals(TI,GeomAbs_CN);
77       Standard_Real Resolution = 1.0e-9, Curvature;
78       Geom2dLProp_CLProps2d LProp(myGeom2dCurve, 2, Resolution);
79       gp_Pnt2d P1, P2;
80
81       for (intrv=1; intrv<=nbintv; intrv++)
82         {
83           Standard_Real t = TI(intrv);
84           Standard_Real step = (TI(intrv+1) - t) / GetDiscretisation();
85           Standard_Real LRad, ratio;
86           for (ii = 1; ii <= myDiscretisation; ii++)
87             {
88               LProp.SetParameter(t);
89               if (LProp.IsTangentDefined())
90                 {
91                   Curvature = Abs(LProp.Curvature());
92                   if (Curvature > Resolution)
93                     {
94                       myGeom2dCurve->D0(t, P1);
95                       LRad = 1./Curvature;
96                       ratio = ((LRad>myradiusmax)?myradiusmax/LRad:1);
97                       ratio *= myradiusratio;
98                       LProp.CentreOfCurvature(P2);
99                       gp_Vec2d V(P1,P2);
100                       gp_Pnt2d P3 = P1.Translated(ratio*V);
101                       Handle(Graphic2d_Segment) aSegment = new Graphic2d_Segment(aGrObj,P1.X(),P1.Y(),P3.X(),P3.Y());
102                     }
103                 }
104               t += step;
105             }
106         }
107     }
108 }
109
110  void ISession2D_Curve::ComputeSelection(const Handle(SelectMgr_Selection)& ,const Standard_Integer ) 
111 {
112 }
113