0031570: Samples - add Qt samples similar to standard MFC samples
[occt.git] / samples / OCCTOverview / code / AdaptorCurve2d_AIS.cxx
1 // Copyright (c) 2020 OPEN CASCADE SAS
2 //
3 // This file is part of the examples of the Open CASCADE Technology software library.
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
21
22 #include "AdaptorCurve2d_AIS.h"
23
24 #include <Prs3d_LineAspect.hxx>
25 #include <Prs3d_PointAspect.hxx>
26 #include <StdPrs_PoleCurve.hxx>
27 #include <Geom2dAdaptor_Curve.hxx>
28 #include <GCPnts_QuasiUniformDeflection.hxx>
29 #include <Graphic3d_ArrayOfPolylines.hxx>
30 #include <Geom2d_BezierCurve.hxx>
31 #include <Geom2d_BSplineCurve.hxx>
32 #include <Geom2dLProp_CLProps2d.hxx>
33
34 AdaptorCurve2d_AIS::AdaptorCurve2d_AIS (const Handle(Geom2d_Curve)& theGeom2dCurve,
35                                         const Aspect_TypeOfLine theTypeOfLine,
36                                         const Aspect_WidthOfLine theWidthOfLine)
37 : myGeom2dCurve (theGeom2dCurve),
38   myTypeOfLine (theTypeOfLine),
39   myWidthOfLine (theWidthOfLine),
40   myDisplayPole (Standard_True),
41   myDisplayCurbure (Standard_False),
42   myDiscretisation (20),
43   myradiusmax (10),
44   myradiusratio (1)
45 {
46   //
47 }
48
49 void AdaptorCurve2d_AIS::Compute (const Handle(PrsMgr_PresentationManager3d)&,
50                                   const Handle(Prs3d_Presentation)& thePrs,
51                                   const Standard_Integer theMode)
52 {
53   if (theMode != 0)
54   {
55     return;
56   }
57
58   Geom2dAdaptor_Curve anAdaptor(myGeom2dCurve);
59   GCPnts_QuasiUniformDeflection anEdgeDistrib(anAdaptor, 1.e-2);
60   if (anEdgeDistrib.IsDone())
61   {
62     Handle(Graphic3d_ArrayOfPolylines) aCurve = new Graphic3d_ArrayOfPolylines(anEdgeDistrib.NbPoints());
63     for (Standard_Integer i = 1; i <= anEdgeDistrib.NbPoints(); ++i)
64     {
65       aCurve->AddVertex(anEdgeDistrib.Value(i));
66     }
67
68     Handle(Graphic3d_Group) aPrsGroup = thePrs->NewGroup();
69     aPrsGroup->SetGroupPrimitivesAspect(myDrawer->LineAspect()->Aspect());
70     aPrsGroup->AddPrimitiveArray(aCurve);
71   }
72
73   if (myDisplayPole)
74   {
75     if (anAdaptor.GetType() == GeomAbs_BezierCurve)
76     {
77       Handle(Geom2d_BezierCurve) aBezier = anAdaptor.Bezier();
78       Handle(Graphic3d_ArrayOfPolylines) anArrayOfVertex = new Graphic3d_ArrayOfPolylines(aBezier->NbPoles());
79       for (int i = 1; i <= aBezier->NbPoles(); i++)
80       {
81         gp_Pnt2d CurrentPoint = aBezier->Pole(i);
82         anArrayOfVertex->AddVertex(CurrentPoint.X(), CurrentPoint.Y(), 0.);
83       }
84
85       Handle(Graphic3d_Group) aPrsGroup = thePrs->NewGroup();
86       aPrsGroup->SetGroupPrimitivesAspect(myDrawer->LineAspect()->Aspect());
87       aPrsGroup->AddPrimitiveArray(anArrayOfVertex);
88     }
89
90     if (anAdaptor.GetType() == GeomAbs_BSplineCurve)
91     {
92       Handle(Geom2d_BSplineCurve) aBSpline = anAdaptor.BSpline();
93       Handle(Graphic3d_ArrayOfPolylines) anArrayOfVertex = new Graphic3d_ArrayOfPolylines(aBSpline->NbPoles());
94       for (int i = 1; i <= aBSpline->NbPoles(); i++)
95       {
96         gp_Pnt2d CurrentPoint = aBSpline->Pole(i);
97         anArrayOfVertex->AddVertex(CurrentPoint.X(), CurrentPoint.Y(), 0.);
98       }
99
100       Handle(Graphic3d_Group) aPrsGroup = thePrs->NewGroup();
101       aPrsGroup->SetGroupPrimitivesAspect(myDrawer->LineAspect()->Aspect());
102       aPrsGroup->AddPrimitiveArray(anArrayOfVertex);
103     }
104   }
105
106   if (myDisplayCurbure && (anAdaptor.GetType() != GeomAbs_Line))
107   {
108     const Standard_Integer nbintv = anAdaptor.NbIntervals(GeomAbs_CN);
109     TColStd_Array1OfReal TI(1, nbintv + 1);
110     anAdaptor.Intervals(TI, GeomAbs_CN);
111     Standard_Real Resolution = 1.0e-9, Curvature;
112     Geom2dLProp_CLProps2d LProp(myGeom2dCurve, 2, Resolution);
113     gp_Pnt2d P1, P2;
114
115     Handle(Graphic3d_Group) aPrsGroup = thePrs->NewGroup();
116     aPrsGroup->SetGroupPrimitivesAspect (myDrawer->LineAspect()->Aspect());
117     for (Standard_Integer intrv = 1; intrv <= nbintv; intrv++)
118     {
119       Standard_Real t = TI(intrv);
120       Standard_Real step = (TI(intrv + 1) - t) / GetDiscretisation();
121       Standard_Real LRad, ratio;
122       for (Standard_Integer ii = 1; ii <= myDiscretisation; ii++)
123       {
124         LProp.SetParameter(t);
125         if (LProp.IsTangentDefined())
126         {
127           Curvature = Abs(LProp.Curvature());
128           if (Curvature > Resolution)
129           {
130             myGeom2dCurve->D0(t, P1);
131             LRad = 1. / Curvature;
132             ratio = ((LRad > myradiusmax) ? myradiusmax / LRad : 1);
133             ratio *= myradiusratio;
134             LProp.CentreOfCurvature(P2);
135             gp_Vec2d V(P1, P2);
136             gp_Pnt2d P3 = P1.Translated(ratio*V);
137             Handle(Graphic3d_ArrayOfPolylines) aSegment = new Graphic3d_ArrayOfPolylines(2);
138             aSegment->AddVertex(P1.X(), P1.Y(), 0.);
139             aSegment->AddVertex(P3.X(), P3.Y(), 0.);
140             aPrsGroup->AddPrimitiveArray(aSegment);
141           }
142         }
143         t += step;
144       }
145     }
146   }
147 }