0026254: Inject GeomAbs_OffsetCurve into GeomAbs_CurveType enumeration
[occt.git] / src / DrawTrSurf / DrawTrSurf_BSplineCurve.cxx
1 // Copyright (c) 1995-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
3 //
4 // This file is part of Open CASCADE Technology software library.
5 //
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
11 //
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
14
15
16 #include <Draw_Color.hxx>
17 #include <Draw_Display.hxx>
18 #include <Draw_Drawable3D.hxx>
19 #include <Draw_MarkerShape.hxx>
20 #include <DrawTrSurf_BSplineCurve.hxx>
21 #include <Geom_BSplineCurve.hxx>
22 #include <gp_Pnt2d.hxx>
23 #include <Standard_Type.hxx>
24 #include <TColgp_Array1OfPnt.hxx>
25 #include <TColStd_Array1OfReal.hxx>
26
27 DrawTrSurf_BSplineCurve::DrawTrSurf_BSplineCurve (
28    const Handle(Geom_BSplineCurve)& C) :
29    DrawTrSurf_Curve (C, Draw_vert, 16, 0.05, 1) {
30
31       drawKnots = Standard_True;
32       knotsForm = Draw_Losange;
33       knotsLook = Draw_violet;
34       knotsDim  = 5;
35       drawPoles = Standard_True;
36       polesLook = Draw_rouge;
37    }
38
39
40
41
42    DrawTrSurf_BSplineCurve::DrawTrSurf_BSplineCurve (
43    const Handle(Geom_BSplineCurve)& C, const Draw_Color& CurvColor,
44    const Draw_Color& PolesColor, const Draw_Color& KnotsColor,
45    const Draw_MarkerShape KnotsShape, const Standard_Integer KnotsSize,
46    const Standard_Boolean ShowPoles, const Standard_Boolean ShowKnots, 
47    const Standard_Integer Discret, const Standard_Real Deflection,
48    const Standard_Integer DrawMode)
49    : DrawTrSurf_Curve (C , CurvColor, Discret, Deflection, DrawMode) {   
50
51       drawKnots = ShowKnots;
52       knotsForm = KnotsShape;
53       knotsLook = KnotsColor;
54       knotsDim  = KnotsSize;
55       drawPoles = ShowPoles;
56       polesLook = PolesColor;
57    }
58
59
60
61   void DrawTrSurf_BSplineCurve::DrawOn (Draw_Display& dis) const {
62
63
64     Handle(Geom_BSplineCurve) C = Handle(Geom_BSplineCurve)::DownCast(curv);
65     if (drawPoles) {
66       Standard_Integer NbPoles = C->NbPoles();
67       dis.SetColor(polesLook);
68       TColgp_Array1OfPnt CPoles (1, NbPoles);
69       C->Poles (CPoles);
70       dis.MoveTo(CPoles(1));
71       for (Standard_Integer i = 2; i <= NbPoles; i++) {
72         dis.DrawTo(CPoles(i));
73       }
74       if (C->IsPeriodic())
75         dis.DrawTo(CPoles(1));
76     }
77     
78     DrawTrSurf_Curve::DrawOn(dis);
79     
80     if (drawKnots) {
81       Standard_Integer NbKnots = C->NbKnots();
82       TColStd_Array1OfReal CKnots (1, NbKnots);
83       C->Knots (CKnots); 
84       dis.SetColor(knotsLook);
85       Standard_Integer first = C->FirstUKnotIndex();
86       Standard_Integer last  = C->LastUKnotIndex();
87       for (Standard_Integer i = first; i <= last; i++) {
88         dis.DrawMarker (C->Value (CKnots (i)), knotsForm, knotsDim);
89       }
90     }
91   }
92
93
94   void DrawTrSurf_BSplineCurve::DrawOn (
95  
96   Draw_Display& dis,
97   const Standard_Boolean ShowPoles,
98   const Standard_Boolean ShowKnots
99   ) const {
100
101
102     Handle(Geom_BSplineCurve) C = Handle(Geom_BSplineCurve)::DownCast(curv);
103     if (drawPoles && ShowPoles) {
104       Standard_Integer NbPoles = C->NbPoles();
105       dis.SetColor(polesLook);
106       TColgp_Array1OfPnt CPoles (1, NbPoles);
107       C->Poles (CPoles);
108       dis.MoveTo(CPoles(1));
109       for (Standard_Integer i = 2; i <= NbPoles; i++) {
110         dis.DrawTo(CPoles(i));
111       }
112     }
113
114     DrawTrSurf_Curve::DrawOn(dis);
115
116     if (drawKnots && ShowKnots) {
117       Standard_Integer NbKnots = C->NbKnots();
118       TColStd_Array1OfReal CKnots (1, NbKnots);
119       C->Knots (CKnots); 
120       dis.SetColor(knotsLook);
121       for (Standard_Integer i = 1; i <= NbKnots; i++) {
122         dis.DrawMarker (C->Value (CKnots (i)), knotsForm, knotsDim);
123       }
124     }
125   }
126
127
128
129
130   void DrawTrSurf_BSplineCurve::DrawOn (
131
132    Draw_Display& dis, 
133    const Standard_Real    U1,
134    const Standard_Real    U2,
135    const Standard_Integer Pindex,
136    const Standard_Boolean ShowPoles,
137    const Standard_Boolean ShowKnots
138    ) const {
139     
140
141     Handle(Geom_BSplineCurve) C = Handle(Geom_BSplineCurve)::DownCast(curv);
142     Standard_Real Eps1 = Abs(Epsilon (U1));
143     Standard_Real Eps2 = Abs(Epsilon (U2));
144     Standard_Integer I1, J1, I2, J2;
145     C->LocateU (U1, Eps1, I1, J1);
146     C->LocateU (U2, Eps2, I2, J2);
147     Standard_Integer ka  = C->FirstUKnotIndex ();
148     Standard_Integer kb  = C->LastUKnotIndex ();
149
150
151     if (drawPoles && ShowPoles) {
152       Standard_Integer NbPoles = C->NbPoles();
153       dis.SetColor(polesLook);
154       TColgp_Array1OfPnt CPoles (1, NbPoles);
155       C->Poles (CPoles);
156       if (Pindex == 0) {
157         dis.MoveTo(CPoles(1));
158         for (Standard_Integer i = 2; i <= NbPoles; i++) {
159           dis.DrawTo(CPoles(i));
160         }
161       }
162       else if (Pindex == 1) {
163         dis.MoveTo(CPoles(1));
164         dis.DrawTo(CPoles(2));        
165       }
166       else if (Pindex == NbPoles) {
167         dis.MoveTo(CPoles(NbPoles-1));
168         dis.DrawTo(CPoles(NbPoles));
169       }
170       else {
171         dis.MoveTo(CPoles(Pindex-1));
172         dis.DrawTo(CPoles(Pindex));
173         dis.DrawTo(CPoles(Pindex+1));
174       }
175     }
176
177
178
179     dis.SetColor(look);
180     Standard_Integer Degree = C->Degree();
181
182     if (Degree == 1) {    
183       dis.MoveTo(C->Value(U1));
184       dis.DrawTo(C->Value(U2));
185     }
186     else {
187       Standard_Integer NbPoints;
188       Standard_Integer Discret = GetDiscretisation();
189       Standard_Real Ustart = C->Knot (ka);
190       Standard_Real Uend   = C->Knot (kb);
191       Standard_Real Du, U, Ua, Ub, Uk1, Uk2;
192
193       if (I1 > ka)  { ka = I1;  Uk1 = U1; }
194       else {
195         U = U1;
196         NbPoints = (Standard_Integer) Abs (Discret * (U1 - Ustart) / (Ustart - Uend));
197         NbPoints = Max (NbPoints, 30);
198         Du = (Ustart - U1) / NbPoints;
199         dis.MoveTo(C->Value (U));
200         for (Standard_Integer i = 1; i <= NbPoints - 2; i++) {
201            U+= Du;
202            dis.DrawTo(C->Value (U));
203         }
204         dis.DrawTo(C->Value (Ustart));
205         Uk1 = Ustart;
206       }
207
208       if (J2 < kb)  { kb = J2;  Uk2 = U2; }
209       else {
210         Uk2 = Uend;
211         U   = Uend;
212         NbPoints = (Standard_Integer) Abs (Discret * (U2 - Uend) / (Ustart - Uend));
213         NbPoints = Max (NbPoints, 30);
214         Du = (U2 - Uend) / NbPoints;
215         dis.MoveTo(C->Value (U));
216         for (Standard_Integer i = 1; i <= NbPoints - 2; i++) {
217            U+= Du;
218            dis.DrawTo(C->Value (U));
219         }
220         dis.DrawTo(C->Value (U2));
221       }
222
223
224       for (Standard_Integer k = ka; k < kb; k++) {
225         if (k == ka) { 
226           Ua = Uk1;
227           Ub = C->Knot (k+1);
228         }
229         else if (k == kb-1) {
230           Ua = C->Knot (k);
231           Ub = Uk2;
232         }
233         else {
234           Ua = C->Knot (k);
235           Ub = C->Knot (k+1);
236         }
237         U  = Ua;
238         NbPoints = (Standard_Integer) Abs (Discret * (Ua - Ub) / (Ustart - Uend));
239         NbPoints = Max (NbPoints, 30);
240         Du = (Ub - Ua) / NbPoints;
241         dis.MoveTo(C->Value (U));
242         for (Standard_Integer i = 1; i <= NbPoints - 2; i++) {
243            U+= Du;
244            dis.DrawTo(C->Value (U));
245         }
246         dis.DrawTo(C->Value (Ub));
247       }
248     }
249
250
251     if (drawKnots && ShowKnots) {
252       Standard_Integer NbKnots = C->NbKnots();
253       TColStd_Array1OfReal CKnots (1, NbKnots);
254       C->Knots (CKnots); 
255       dis.SetColor(knotsLook);
256       for (Standard_Integer i = J1; i <= I2; i++) {
257         dis.DrawMarker (C->Value (CKnots (i)), knotsForm, knotsDim);
258       }
259     }
260
261    }
262
263
264
265    void DrawTrSurf_BSplineCurve::ShowPoles () { drawPoles = Standard_True;  }
266
267
268    void DrawTrSurf_BSplineCurve::ShowKnots () { drawKnots = Standard_True;  }
269
270
271    void DrawTrSurf_BSplineCurve::ClearPoles () { drawPoles = Standard_False; }
272
273
274    void DrawTrSurf_BSplineCurve::ClearKnots () { drawKnots = Standard_False; }
275
276
277    void DrawTrSurf_BSplineCurve::FindPole (
278    const Standard_Real X, const Standard_Real Y, const Draw_Display& D, const Standard_Real XPrec,
279    Standard_Integer& Index) const {
280
281      Handle(Geom_BSplineCurve) bc = Handle(Geom_BSplineCurve)::DownCast(curv);
282      Standard_Real Prec = XPrec / D.Zoom();
283      gp_Pnt2d p1(X/D.Zoom(),Y/D.Zoom());
284      Index++;
285      Standard_Integer NbPoles = bc->NbPoles();
286      while (Index <= NbPoles) {
287        if (D.Project(bc->Pole(Index)).Distance(p1) <= Prec)
288          return;
289        Index++;
290      }
291      Index = 0;
292    }
293
294
295    void DrawTrSurf_BSplineCurve::FindKnot (
296    const Standard_Real X, const Standard_Real Y, const Draw_Display& D, const Standard_Real Prec,
297    Standard_Integer& Index) const {
298
299      Handle(Geom_BSplineCurve) bc = Handle(Geom_BSplineCurve)::DownCast(curv);
300      gp_Pnt2d p1(X,Y);
301      Index++;
302      Standard_Integer NbKnots = bc->NbKnots();
303      while (Index <= NbKnots) {
304        if (D.Project(bc->Value(bc->Knot(Index))).Distance(p1) <= Prec)
305          return;
306        Index++;
307      }
308      Index = 0;
309    }
310
311
312 //=======================================================================
313 //function : Copy
314 //purpose  : 
315 //=======================================================================
316
317 Handle(Draw_Drawable3D)  DrawTrSurf_BSplineCurve::Copy()const 
318 {
319   Handle(DrawTrSurf_BSplineCurve) DC = new DrawTrSurf_BSplineCurve
320     (Handle(Geom_BSplineCurve)::DownCast(curv->Copy()),
321      look,polesLook,knotsLook,knotsForm,knotsDim,
322      drawPoles,drawKnots,
323      GetDiscretisation(),GetDeflection(),GetDrawMode());
324      
325   return DC;
326 }
327
328