0028060: Samples - Objects in Geometry/Viewer2d have incorrect color
[occt.git] / samples / mfc / standard / 01_Geometry / src / ISession2D / ISession2D_Curve.cpp
1 #include "stdafx.h"
2
3 #include <ISession2D_Curve.h>
4 #include "..\\GeometryApp.h"
5 #include <Graphic3d_ArrayOfPolylines.hxx>
6 #include <Geom2d_BezierCurve.hxx>
7 #include <Geom2dLProp_CLProps2d.hxx>
8 #include <TColStd_Array1OfReal.hxx>
9 #include <Geom2d_BSplineCurve.hxx>
10
11 IMPLEMENT_STANDARD_RTTIEXT(ISession2D_Curve,AIS_InteractiveObject)
12
13 ISession2D_Curve::ISession2D_Curve(const Handle(Geom2d_Curve) aGeom2dCurve,
14                                    const Aspect_TypeOfLine aTypeOfLine,
15                                    const Aspect_WidthOfLine aWidthOfLine,
16                                    const Standard_Integer aColorIndex)
17 {
18   myGeom2dCurve = aGeom2dCurve;
19   myTypeOfLine  = aTypeOfLine ;
20   myWidthOfLine = aWidthOfLine;
21   myColorIndex  = aColorIndex ;
22   myDisplayPole = Standard_True;
23   myDisplayCurbure = Standard_False;
24   myDiscretisation = 20;
25   myradiusmax = 10;
26   myradiusratio = 1;
27 }
28
29 void ISession2D_Curve::Compute(const Handle(PrsMgr_PresentationManager3d)& ,
30                                const Handle(Prs3d_Presentation)& thePrs,
31                                const Standard_Integer )
32 {
33   Handle(Graphic3d_Group) aPrsGroup = Prs3d_Root::CurrentGroup (thePrs);
34   aPrsGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
35   aPrsGroup->SetGroupPrimitivesAspect (myDrawer->PointAspect()->Aspect());
36
37   Geom2dAdaptor_Curve anAdaptor(myGeom2dCurve);
38   GCPnts_QuasiUniformDeflection anEdgeDistrib(anAdaptor,1.e-2);
39   if(anEdgeDistrib.IsDone())
40   {
41     Handle(Graphic3d_ArrayOfPolylines) aCurve =
42       new Graphic3d_ArrayOfPolylines(anEdgeDistrib.NbPoints()); 
43     for(Standard_Integer i=1;i<=anEdgeDistrib.NbPoints();++i)
44       aCurve->AddVertex(anEdgeDistrib.Value(i));
45
46     aPrsGroup->AddPrimitiveArray (aCurve);
47   }
48
49   if (myDisplayPole) 
50   {
51     if (anAdaptor.GetType() == GeomAbs_BezierCurve  )
52     {
53       Handle(Geom2d_BezierCurve) aBezier = anAdaptor.Bezier();
54       Handle(Graphic3d_ArrayOfPolylines) anArrayOfVertex = new Graphic3d_ArrayOfPolylines(aBezier->NbPoles());
55       for(int i=1;i<=aBezier->NbPoles();i++)
56       {
57         gp_Pnt2d CurrentPoint = aBezier->Pole(i);
58         anArrayOfVertex->AddVertex(CurrentPoint.X(),CurrentPoint.Y(),0.);
59       }
60       aPrsGroup->AddPrimitiveArray (anArrayOfVertex);
61     }
62
63     if (anAdaptor.GetType() == GeomAbs_BSplineCurve  )
64     {
65       Handle(Geom2d_BSplineCurve) aBSpline = anAdaptor.BSpline();
66
67       Handle(Graphic3d_ArrayOfPolylines) anArrayOfVertex = 
68         new Graphic3d_ArrayOfPolylines(aBSpline->NbPoles());
69
70       for(int i=1;i<=aBSpline->NbPoles();i++)
71       {
72         gp_Pnt2d CurrentPoint = aBSpline->Pole(i);
73         anArrayOfVertex->AddVertex(CurrentPoint.X(),CurrentPoint.Y(),0.);
74       }
75       aPrsGroup->AddPrimitiveArray (anArrayOfVertex);
76     }
77   }
78
79   if (myDisplayCurbure && (anAdaptor.GetType() != GeomAbs_Line))
80   {
81     Standard_Integer ii;
82     Standard_Integer intrv, nbintv = anAdaptor.NbIntervals(GeomAbs_CN);
83     TColStd_Array1OfReal TI(1,nbintv+1);
84     anAdaptor.Intervals(TI,GeomAbs_CN);
85     Standard_Real Resolution = 1.0e-9, Curvature;
86     Geom2dLProp_CLProps2d LProp(myGeom2dCurve, 2, Resolution);
87     gp_Pnt2d P1, P2;    
88
89     for (intrv = 1; intrv <= nbintv; intrv++) 
90     {
91       Standard_Real t = TI(intrv);
92       Standard_Real step = (TI(intrv+1) - t) / GetDiscretisation();
93       Standard_Real LRad, ratio;
94       for (ii = 1; ii <= myDiscretisation; ii++) 
95       {  
96         LProp.SetParameter(t);
97         if (LProp.IsTangentDefined()) 
98         {
99           Curvature = Abs(LProp.Curvature());
100           if ( Curvature >  Resolution) 
101           {
102             myGeom2dCurve->D0(t, P1);
103             LRad = 1./Curvature;
104             ratio = ( (  LRad > myradiusmax) ? myradiusmax/LRad : 1 );
105             ratio *= myradiusratio;
106             LProp.CentreOfCurvature(P2);
107             gp_Vec2d V(P1, P2);
108             gp_Pnt2d P3 = P1.Translated(ratio*V);
109             Handle(Graphic3d_ArrayOfPolylines) aSegment = new Graphic3d_ArrayOfPolylines(2);
110             aSegment->AddVertex(P1.X(),P1.Y(),0.);
111             aSegment->AddVertex(P3.X(),P3.Y(),0.);
112             aPrsGroup->AddPrimitiveArray (aSegment);
113           }
114         }
115         t += step;
116       }
117     }
118   }
119 }