0031990: Coding, Draw Harness - Replace C-like pointers to function in Draw_SaveAndRe...
[occt.git] / src / DrawTrSurf / DrawTrSurf_BSplineSurface.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 #include <DrawTrSurf_BSplineSurface.hxx>
16
17 #include <Adaptor3d_IsoCurve.hxx>
18 #include <Draw_Color.hxx>
19 #include <Draw_Display.hxx>
20 #include <Draw_MarkerShape.hxx>
21 #include <DrawTrSurf.hxx>
22 #include <DrawTrSurf_BSplineCurve.hxx>
23 #include <DrawTrSurf_Params.hxx>
24 #include <Geom_BSplineSurface.hxx>
25 #include <GeomAdaptor_Surface.hxx>
26 #include <GeomTools_SurfaceSet.hxx>
27 #include <gp_Pnt2d.hxx>
28 #include <TColgp_Array2OfPnt.hxx>
29 #include <TColStd_Array1OfReal.hxx>
30
31 IMPLEMENT_STANDARD_RTTIEXT(DrawTrSurf_BSplineSurface, DrawTrSurf_Surface)
32
33 DrawTrSurf_BSplineSurface::DrawTrSurf_BSplineSurface (const Handle(Geom_BSplineSurface)& S)
34 : DrawTrSurf_Surface (S, S->NbUKnots()-2, S->NbVKnots()-2,
35                       Draw_jaune, Draw_bleu, 30, 0.05, 0)
36 {
37   drawPoles = Standard_True;
38   drawKnots = Standard_True;
39   knotsIsos  = Standard_True;
40   knotsForm = Draw_Losange;
41   knotsLook = Draw_violet;
42   knotsDim  = 5;
43   polesLook = Draw_rouge;
44 }
45
46 DrawTrSurf_BSplineSurface::DrawTrSurf_BSplineSurface (const Handle(Geom_BSplineSurface)& S, const Draw_Color& BoundsColor,
47                                                       const Draw_Color& IsosColor, const Draw_Color& PolesColor,
48                                                       const Draw_Color& KnotsColor, const Draw_MarkerShape KnotsShape,
49                                                       const Standard_Integer KnotsSize, const Standard_Boolean ShowPoles, const Standard_Boolean ShowKnots,
50                                                       const Standard_Integer Discret, const Standard_Real Deflection, const Standard_Integer DrawMode)
51 : DrawTrSurf_Surface (S, S->NbUKnots()-2, S->NbVKnots()-2, BoundsColor,
52                       IsosColor, Discret, Deflection, DrawMode)
53 {
54   knotsIsos  = Standard_True;
55   drawPoles = ShowPoles;
56   drawKnots = ShowKnots;
57   knotsForm = KnotsShape;
58   knotsLook = KnotsColor;
59   knotsDim  = KnotsSize;
60   polesLook = PolesColor;
61 }
62
63 DrawTrSurf_BSplineSurface::DrawTrSurf_BSplineSurface (const Handle(Geom_BSplineSurface)& S, const Standard_Integer NbUIsos,
64                                                       const Standard_Integer NbVIsos, const Draw_Color& BoundsColor,
65                                                       const Draw_Color& IsosColor, const Draw_Color& PolesColor,
66                                                       const Draw_Color& KnotsColor, const Draw_MarkerShape KnotsShape,
67                                                       const Standard_Integer KnotsSize, const Standard_Boolean ShowPoles, const Standard_Boolean ShowKnots,
68                                                       const Standard_Integer Discret, const Standard_Real Deflection, const Standard_Integer DrawMode) 
69 : DrawTrSurf_Surface (S, Abs(NbUIsos), Abs(NbVIsos), BoundsColor,
70                       IsosColor, Discret, Deflection, DrawMode)
71 {
72   knotsIsos  = Standard_False;
73   drawPoles  = ShowPoles;
74   drawKnots  = ShowKnots;
75   knotsForm  = KnotsShape;
76   knotsLook  = KnotsColor;
77   knotsDim   = KnotsSize;
78   polesLook  = PolesColor;
79 }
80
81 void DrawTrSurf_BSplineSurface::DrawOn (Draw_Display& dis) const
82 {
83   Handle(Geom_BSplineSurface) S = Handle(Geom_BSplineSurface)::DownCast(surf);
84   Standard_Integer i, j;
85
86   Standard_Real Ua,Ub,Va,Vb;
87   S->Bounds(Ua,Ub,Va,Vb);
88
89   if (drawPoles)
90   {
91     Standard_Integer NbUPoles = S->NbUPoles();
92     Standard_Integer NbVPoles = S->NbVPoles();
93     dis.SetColor(polesLook);
94     TColgp_Array2OfPnt SPoles (1, NbUPoles, 1, NbVPoles);
95     S->Poles (SPoles);
96     for (j = 1; j <= NbVPoles; j++)
97     {
98       dis.MoveTo(SPoles(1, j));
99       for (i = 2; i <= NbUPoles; i++)
100       {
101         dis.DrawTo(SPoles(i, j));
102       }
103       if (S->IsUPeriodic())
104       {
105         dis.DrawTo(SPoles(1,j));
106       }
107     }
108     for (i = 1; i <= NbUPoles; i++)
109     {
110       dis.MoveTo(SPoles(i, 1));
111       for (j = 2; j <= NbVPoles; j++)
112       {
113         dis.DrawTo(SPoles(i, j));
114       }
115       if (S->IsVPeriodic())
116       {
117         dis.DrawTo(SPoles(i,1));
118       }
119     }
120   }
121
122   if (knotsIsos)
123   {
124     Standard_Integer first, last;
125     Handle(GeomAdaptor_Surface) HS = new GeomAdaptor_Surface();
126     HS->Load(surf);
127     
128     Adaptor3d_IsoCurve C(HS);
129
130     dis.SetColor(isosLook);
131     first = S->FirstUKnotIndex() + 1;
132     last  = S->LastUKnotIndex() - 1;
133     for (i = first; i <= last; i++)
134     {
135       DrawIsoCurveOn(C,GeomAbs_IsoU,S->UKnot(i),Va,Vb,dis);
136     }
137     first = S->FirstVKnotIndex() + 1;
138     last  = S->LastVKnotIndex() - 1;
139     for (j = first; j <= last; j++)
140     {
141       DrawIsoCurveOn(C,GeomAbs_IsoV,S->VKnot(j),Ua,Ub,dis);
142     }
143   }
144
145   DrawTrSurf_Surface::DrawOn(dis,!knotsIsos);
146
147   if (drawKnots)
148   {
149     Standard_Integer first, last;
150     Standard_Integer NbUKnots = S->NbUKnots();
151     TColStd_Array1OfReal SUKnots (1, NbUKnots);
152     S->UKnots (SUKnots); 
153     dis.SetColor(knotsLook);
154     first = S->FirstUKnotIndex();
155     last  = S->LastUKnotIndex();
156     for (i = first; i <= last; i++)
157     {
158       dis.DrawMarker (S->Value (SUKnots(i), Va), knotsForm, knotsDim);
159     }
160     Standard_Integer NbVKnots = S->NbVKnots();
161     TColStd_Array1OfReal SVKnots (1, NbVKnots);
162     S->VKnots (SVKnots); 
163     dis.SetColor(knotsLook);
164     first = S->FirstVKnotIndex();
165     last  = S->LastVKnotIndex();
166     for (j = first; j <= last; j++)
167     {
168       dis.DrawMarker (S->Value (Ua, SVKnots(j)), knotsForm, knotsDim);
169     }
170   }
171 }
172
173 void DrawTrSurf_BSplineSurface::ShowKnotsIsos()
174 {
175   knotsIsos = Standard_True;
176   Handle(Geom_BSplineSurface) S = Handle(Geom_BSplineSurface)::DownCast(surf);
177   nbUIsos = S->NbUKnots()-2;
178   nbVIsos = S->NbVKnots()-2;
179 }
180
181 void DrawTrSurf_BSplineSurface::ClearIsos()
182 {
183   knotsIsos = Standard_False;
184   nbUIsos = 0;
185   nbVIsos = 0;
186 }
187
188 void DrawTrSurf_BSplineSurface::ShowIsos (const Standard_Integer Nu, const Standard_Integer Nv)
189 {
190   knotsIsos = Standard_False;
191   nbUIsos = Abs(Nu);
192   nbVIsos = Abs(Nv);
193 }
194
195 void DrawTrSurf_BSplineSurface::FindPole (const Standard_Real X, const Standard_Real Y, const Draw_Display& D, const Standard_Real XPrec,
196                                           Standard_Integer& UIndex, Standard_Integer& VIndex) const
197 {
198   Handle(Geom_BSplineSurface) bs = Handle(Geom_BSplineSurface)::DownCast(surf);
199   gp_Pnt2d p1(X/D.Zoom(),Y/D.Zoom());
200   Standard_Real Prec = XPrec / D.Zoom();
201   UIndex++;
202   VIndex++;
203   Standard_Integer NbUPoles = bs->NbUPoles();
204   Standard_Integer NbVPoles = bs->NbVPoles();
205   while (VIndex <= NbVPoles)
206   {
207     while (UIndex <= NbUPoles)
208     {
209       if (D.Project(bs->Pole(UIndex, VIndex)).Distance(p1) <= Prec)
210       {
211         return;
212       }
213       UIndex++;
214     }
215     UIndex = 1;
216     VIndex++;
217   }
218   UIndex = VIndex = 0;
219 }
220
221 void DrawTrSurf_BSplineSurface::FindUKnot (const Standard_Real X, const Standard_Real Y, const Draw_Display& D, const Standard_Real Prec,
222                                            Standard_Integer& UIndex) const
223 {
224   Handle(Geom_BSplineSurface) bs = Handle(Geom_BSplineSurface)::DownCast(surf);
225   gp_Pnt2d p1(X,Y);
226   UIndex++;
227   Standard_Integer NbUKnots = bs->NbUKnots();
228   Standard_Real U1, U2, V1, V2;
229   surf->Bounds (U1, U2, V1, V2);
230   while (UIndex <= NbUKnots)
231   {
232     if (D.Project(bs->Value(bs->UKnot(UIndex), V1)).Distance(p1) <= Prec)
233     {
234       return;
235     }
236     UIndex++;
237   }
238   UIndex = 0;
239 }
240
241 void DrawTrSurf_BSplineSurface::FindVKnot (const Standard_Real X, const Standard_Real Y, const Draw_Display& D, const Standard_Real Prec,
242                                            Standard_Integer& VIndex) const
243 {
244   Handle(Geom_BSplineSurface) bs = Handle(Geom_BSplineSurface)::DownCast(surf);
245   gp_Pnt2d p1(X,Y);
246   VIndex++;
247   Standard_Integer NbVKnots = bs->NbVKnots();
248   Standard_Real U1, U2, V1, V2;
249   surf->Bounds (U1, U2, V1, V2);
250   while (VIndex <= NbVKnots)
251   {
252     if (D.Project(bs->Value(U1, bs->VKnot(VIndex))).Distance(p1) <= Prec)
253     {
254       return;
255     }
256     VIndex++;
257   }
258   VIndex = 0;
259 }
260
261 //=======================================================================
262 //function : Copy
263 //purpose  :
264 //=======================================================================
265 Handle(Draw_Drawable3D) DrawTrSurf_BSplineSurface::Copy() const
266 {
267   Handle(DrawTrSurf_BSplineSurface) DS;
268   if (!knotsIsos)
269   {
270     DS = new DrawTrSurf_BSplineSurface
271       (Handle(Geom_BSplineSurface)::DownCast(surf->Copy()),
272        nbUIsos,nbVIsos,
273        boundsLook,isosLook,polesLook,knotsLook,
274        knotsForm,knotsDim,drawPoles,drawKnots,
275        GetDiscretisation(),GetDeflection(),GetDrawMode());
276   }
277   else
278   {
279     DS = new DrawTrSurf_BSplineSurface
280       (Handle(Geom_BSplineSurface)::DownCast(surf->Copy()),
281        boundsLook,isosLook,polesLook,knotsLook,
282        knotsForm,knotsDim,drawPoles,drawKnots,
283        GetDiscretisation(),GetDeflection(),GetDrawMode());
284   }
285   return DS;
286 }
287
288 //=======================================================================
289 //function : Restore
290 //purpose  :
291 //=======================================================================
292 Handle(Draw_Drawable3D) DrawTrSurf_BSplineSurface::Restore (Standard_IStream& theStream)
293 {
294   const DrawTrSurf_Params& aParams = DrawTrSurf::Parameters();
295   Handle(Geom_BSplineSurface) aGeomSurface = Handle(Geom_BSplineSurface)::DownCast (GeomTools_SurfaceSet::ReadSurface (theStream));
296   Handle(DrawTrSurf_BSplineSurface) aDrawSurface;
297   if (!aParams.NeedKnotsIsos)
298   {
299     aDrawSurface = new DrawTrSurf_BSplineSurface (aGeomSurface,
300                                                   aParams.NbUIsos, aParams.NbVIsos,
301                                                   aParams.BoundsColor, aParams.IsosColor,
302                                                   aParams.PolesColor, aParams.KnotsColor,
303                                                   aParams.KnotsMarker, aParams.KnotsSize,
304                                                   aParams.IsShowPoles, aParams.IsShowKnots,
305                                                   aParams.Discret, aParams.Deflection, aParams.DrawMode);
306   }
307   else
308   {
309     aDrawSurface = new DrawTrSurf_BSplineSurface (aGeomSurface,
310                                                   aParams.BoundsColor, aParams.IsosColor,
311                                                   aParams.PolesColor, aParams.KnotsColor,
312                                                   aParams.KnotsMarker, aParams.KnotsSize,
313                                                   aParams.IsShowPoles, aParams.IsShowKnots,
314                                                   aParams.Discret, aParams.Deflection, aParams.DrawMode);
315   }
316   return aDrawSurface;
317 }