0030480: Visualization - Clear of Select3D_SensitiveGroup does not update internal...
[occt.git] / src / DrawTrSurf / DrawTrSurf_Drawable.cxx
1 // Created on: 1991-07-16
2 // Created by: Christophe MARION
3 // Copyright (c) 1991-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 <Adaptor2d_Curve2d.hxx>
19 #include <Adaptor3d_Curve.hxx>
20 #include <Adaptor3d_IsoCurve.hxx>
21 #include <Draw_Display.hxx>
22 #include <DrawTrSurf_Drawable.hxx>
23 #include <GCPnts_UniformDeflection.hxx>
24 #include <Geom_BezierCurve.hxx>
25 #include <Geom_BSplineCurve.hxx>
26 #include <GeomAdaptor_Curve.hxx>
27 #include <gp_Pnt.hxx>
28 #include <gp_Pnt2d.hxx>
29 #include <Precision.hxx>
30 #include <Standard_Type.hxx>
31 #include <TColStd_Array1OfReal.hxx>
32
33 IMPLEMENT_STANDARD_RTTIEXT(DrawTrSurf_Drawable,Draw_Drawable3D)
34
35 //=======================================================================
36 //function : DrawTrSurf_Drawable
37 //purpose  : initialise the discretisation
38 //=======================================================================
39 DrawTrSurf_Drawable::DrawTrSurf_Drawable (
40
41 const Standard_Integer discret,
42 const Standard_Real    deflection,
43 const Standard_Integer DrawMode ) :
44        myDrawMode (DrawMode),
45        myDiscret(discret),
46        myDeflection(deflection)
47 {
48 }
49
50 //=======================================================================
51 //function : DrawCurve2dOn
52 //purpose  : draw a 2D curve
53 //=======================================================================
54
55 void DrawTrSurf_Drawable::DrawCurve2dOn (Adaptor2d_Curve2d&   C, 
56                                          Draw_Display& aDisplay) const
57 {
58   gp_Pnt P;
59   
60   gp_Pnt2d aPoint2d,
61   *aPoint2dPtr ;
62   if (myDrawMode == 1) {
63     Standard_Real Fleche = myDeflection/aDisplay.Zoom();
64     GCPnts_UniformDeflection LineVu(C,Fleche);
65     if (LineVu.IsDone()) {
66       P = LineVu.Value(1) ;
67       aPoint2dPtr = (gp_Pnt2d *) &P ;
68       aDisplay.MoveTo(*aPoint2dPtr);
69       for (Standard_Integer i = 2; i <= LineVu.NbPoints(); i++) {
70         P = LineVu.Value(i) ;
71         aPoint2dPtr = (gp_Pnt2d *) &P ;
72         aDisplay.DrawTo(*aPoint2dPtr);
73       }
74     }  
75   }
76   else {
77     Standard_Integer intrv, nbintv = C.NbIntervals(GeomAbs_CN);
78     TColStd_Array1OfReal TI(1,nbintv+1);
79     C.Intervals(TI,GeomAbs_CN);
80     C.D0(C.FirstParameter(),aPoint2d);
81     aDisplay.MoveTo(aPoint2d);
82     for (intrv = 1; intrv <= nbintv; intrv++) {
83       if (C.GetType() != GeomAbs_Line) {
84         Standard_Real t = TI(intrv);
85         Standard_Real step = (TI(intrv+1) - t) / myDiscret;
86         for (Standard_Integer i = 1; i < myDiscret; i++) {
87           t += step;
88           C.D0(t,aPoint2d);
89           aDisplay.DrawTo(aPoint2d);
90         }
91       }
92       C.D0(TI(intrv+1),aPoint2d);
93       aDisplay.DrawTo(aPoint2d);
94     }
95   }
96 }
97
98 //=======================================================================
99 //static function : PlotCurve
100 //purpose  : draw a 3D curve
101 //=======================================================================
102 static void PlotCurve (Draw_Display& aDisplay,
103                        const Adaptor3d_Curve& C, 
104                        Standard_Real& theFirstParam, 
105                        Standard_Real  theHalfStep,
106                        const gp_Pnt&  theFirstPnt,
107                        const gp_Pnt&  theLastPnt)
108 {
109   Standard_Real IsoRatio = 1.001;
110   gp_Pnt Pm;
111
112   C.D0 (theFirstParam + theHalfStep, Pm);
113   
114   Standard_Real dfLength = theFirstPnt.Distance(theLastPnt);
115   if (dfLength < Precision::Confusion() || 
116       Pm.Distance(theFirstPnt) + Pm.Distance(theLastPnt) <= IsoRatio*dfLength) {
117     aDisplay.DrawTo (theLastPnt);
118   } else {
119     PlotCurve (aDisplay, C, theFirstParam, theHalfStep/2., theFirstPnt, Pm);
120     Standard_Real aLocalF = theFirstParam + theHalfStep;
121     PlotCurve (aDisplay, C, aLocalF, theHalfStep/2., Pm, theLastPnt);
122   }
123 }
124 //=======================================================================
125 //function : DrawCurveOn
126 //purpose  : draw a 3D curve
127 //=======================================================================
128
129 void DrawTrSurf_Drawable::DrawCurveOn (Adaptor3d_Curve&   C, 
130                                        Draw_Display& aDisplay) const
131 {
132   gp_Pnt P;
133   if (myDrawMode == 1)
134   {
135     Standard_Real Fleche = myDeflection/aDisplay.Zoom();
136     GCPnts_UniformDeflection LineVu(C,Fleche);
137     if (LineVu.IsDone())
138     {
139       aDisplay.MoveTo(LineVu.Value(1));
140       for (Standard_Integer i = 2; i <= LineVu.NbPoints(); i++)
141       {
142         aDisplay.DrawTo(LineVu.Value(i));
143       }
144     }
145   }
146   else
147   {
148     Standard_Integer j;
149     Standard_Integer intrv, nbintv = C.NbIntervals(GeomAbs_CN);
150     TColStd_Array1OfReal TI(1,nbintv+1);
151     C.Intervals(TI,GeomAbs_CN);
152     C.D0(C.FirstParameter(),P);
153     aDisplay.MoveTo(P);
154     GeomAbs_CurveType CurvType = C.GetType();
155     gp_Pnt aPPnt=P, aNPnt;
156
157     for (intrv = 1; intrv <= nbintv; intrv++)
158     {
159       Standard_Real t = TI(intrv);
160       Standard_Real step = (TI(intrv+1) - t) / myDiscret;
161
162       switch (CurvType)
163       {
164       case GeomAbs_Line:
165         break;
166       case GeomAbs_Circle:
167       case GeomAbs_Ellipse:
168         for (j = 1; j < myDiscret; j++)
169         {
170           t += step;
171           C.D0(t,P);
172           aDisplay.DrawTo(P);
173         }
174         break;
175       case GeomAbs_Parabola:
176       case GeomAbs_Hyperbola:
177       case GeomAbs_BezierCurve:
178       case GeomAbs_BSplineCurve:
179       case GeomAbs_OffsetCurve:
180       case GeomAbs_OtherCurve:
181         const Standard_Integer nIter = myDiscret/2;
182         for (j = 1; j < nIter; j++)
183         {
184           const Standard_Real t1 = t+step*2.;
185           C.D0 (t1, aNPnt);
186           PlotCurve (aDisplay, C, t, step, aPPnt, aNPnt);
187           aPPnt = aNPnt;
188           t = t1;
189         }
190
191         break;
192       }
193
194       C.D0(TI(intrv+1),P);
195       PlotCurve (aDisplay, C, t, step, aPPnt, P);
196       aDisplay.DrawTo(P);
197     }
198   }
199 }
200
201
202 //=======================================================================
203 //function : DrawIsoCurveOn
204 //purpose  : 
205 //=======================================================================
206
207 void DrawTrSurf_Drawable::DrawIsoCurveOn(Adaptor3d_IsoCurve& C,
208                                          const GeomAbs_IsoType T,
209                                          const Standard_Real P,
210                                          const Standard_Real F,
211                                          const Standard_Real L,
212                                          Draw_Display& dis) const 
213 {
214   C.Load(T,P,F,L);
215   if ((C.GetType() == GeomAbs_BezierCurve) || 
216       (C.GetType() == GeomAbs_BSplineCurve)) {
217     GeomAdaptor_Curve GC;
218     if (C.GetType() == GeomAbs_BezierCurve) 
219       GC.Load(C.Bezier(),F,L);
220     else
221       GC.Load(C.BSpline(),F,L);
222     
223     DrawCurveOn(GC,dis);
224   }
225   else
226     DrawCurveOn(C,dis);
227   
228 }