0026922: Huge performance issue writing data to the output stream
[occt.git] / src / GeometryTest / GeometryTest_SurfaceCommands.cxx
1 // Created on: 1993-08-12
2 // Created by: Joelle CHAUVET
3 // Copyright (c) 1993-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 // 09/06/97 : JPI : suppression des commandes redondantes suite a la creation de GeomliteTest
18 // Modified:    Mon Jan 18 11:06:46 1999
19 //              option -NS dans la commande tuyau pour distinguer les tuyaux
20 //              a section evolutive des tuyaux a N sections avec N = 2
21
22 #include <GeometryTest.hxx>
23 #include <DrawTrSurf.hxx>
24 #include <Draw.hxx>
25 #include <Draw_Interpretor.hxx>
26 #include <Draw_Appli.hxx>
27 #include <Draw_Display.hxx>
28
29 #include <GeomAbs_SurfaceType.hxx>
30 #include <GeomAbs_IsoType.hxx>
31 #include <GeomAbs_Shape.hxx>
32
33 #include <Geom_BSplineSurface.hxx>
34 #include <Geom_BSplineCurve.hxx>
35 #include <GeomConvert.hxx>
36
37
38 #include <GeomAdaptor_Surface.hxx>
39 #include <GeomAdaptor_Curve.hxx>
40 #include <Geom2dAdaptor_Curve.hxx>
41
42 #include <TColgp_Array1OfPnt.hxx>
43 #include <TColgp_Array2OfPnt.hxx>
44 #include <TColStd_Array1OfReal.hxx>
45 #include <TColStd_Array2OfReal.hxx>
46 #include <TColStd_Array1OfInteger.hxx>
47 #include <TColStd_HArray1OfReal.hxx>
48 #include <TColStd_HArray2OfReal.hxx>
49 #include <TColGeom_SequenceOfCurve.hxx>
50
51 #include <Precision.hxx>
52
53 #include <GeomFill.hxx>
54 #include <GeomFill_BSplineCurves.hxx>
55 #include <GeomFill_Pipe.hxx>
56 #include <GeomFill_SectionGenerator.hxx>
57 #include <GeomFill_Line.hxx>
58 #include <GeomFill_AppSurf.hxx>
59
60
61 #include <DrawTrSurf_BezierSurface.hxx>
62 #include <DrawTrSurf_BSplineSurface.hxx>
63
64 #ifdef _MSC_VER
65 #include <stdio.h>
66 //#define strcasecmp strcmp Already defined
67 #endif
68
69 #ifdef _WIN32
70 Standard_IMPORT Draw_Viewer dout;
71 #endif
72
73
74
75
76 //=======================================================================
77 //function : sweep
78 //purpose  : 
79 //=======================================================================
80
81 static Standard_Integer sweep (Draw_Interpretor&,
82                                Standard_Integer n, const char** a)
83 {
84   GeomFill_Trihedron Option = GeomFill_IsCorrectedFrenet;
85   Standard_Integer ipath =2, isection =4 , NbSeg=30, MaxDegree = 10;
86   Standard_Real Tol = 1.e-4;
87
88   if (n < 4 ) return 1;
89   
90   if (! strcmp(a[2],"-FX")) {
91     ipath = 3;
92     Option = GeomFill_IsFixed;
93   }
94   if (! strcmp(a[2],"-FR")) {
95     ipath = 3;
96     Option = GeomFill_IsFrenet;
97   }
98   if (! strcmp(a[2],"-CF")) {
99     ipath = 3;
100     Option = GeomFill_IsCorrectedFrenet;
101   }
102   if (! strcmp(a[2],"-CN")) {
103     ipath = 3;
104     isection = 7;
105     Option = GeomFill_IsConstantNormal;
106   }
107   if (! strcmp(a[2],"-DX")) {
108     ipath = 3;
109     isection = 5;
110     Option = GeomFill_IsDarboux;
111   }
112
113   GeomFill_Pipe Pipe;
114   Pipe.GenerateParticularCase(Standard_True);
115   
116   if (Option == GeomFill_IsDarboux) {
117     Handle(Geom2d_Curve) path = DrawTrSurf::GetCurve2d(a[ipath]);
118     if ( path.IsNull()) return 1;
119     
120     Handle(Geom_Surface) Support = DrawTrSurf::GetSurface(a[ipath+1]);
121     if ( Support.IsNull()) return 1;
122
123     Handle(Geom_Curve) firstS = DrawTrSurf::GetCurve(a[ipath+2]);
124     if ( firstS.IsNull()) return 1;
125     
126     Pipe.Init(path, Support, firstS);
127   }
128   else if (Option == GeomFill_IsConstantNormal) {
129     gp_Dir D ( Draw::Atof(a[3]), Draw::Atof(a[4]), Draw::Atof(a[5]) );
130     Handle(Geom_Curve) path = DrawTrSurf::GetCurve(a[6]);
131     Handle(Geom_Curve) firstS = DrawTrSurf::GetCurve(a[7]);
132     Pipe.Init(path, firstS, D);
133   }
134   else  {
135     Handle(Geom_Curve) path = DrawTrSurf::GetCurve(a[ipath]);
136     if ( path.IsNull()) return 1;
137     
138     Handle(Geom_Curve) firstS = DrawTrSurf::GetCurve(a[ipath+1]);
139     if ( firstS.IsNull()) return 1;
140
141     Pipe.Init(path, firstS, Option);
142   }     
143
144   if (n >=isection+2) { 
145     MaxDegree = Draw::Atoi(a[isection+1]);
146     if (n >isection+2) NbSeg = Draw::Atoi(a[isection+2]);
147   }
148     
149   Pipe.Perform(Tol, Standard_False,  GeomAbs_C2, MaxDegree, NbSeg);
150
151   DrawTrSurf::Set(a[1], Pipe.Surface());
152   return 0;
153   
154 }   
155 //=======================================================================
156 //function : tuyau
157 //purpose  : 
158 //=======================================================================
159
160 static Standard_Integer tuyau (Draw_Interpretor&,
161                                Standard_Integer n, const char** a)
162 {
163   if ( n < 4) return 1;
164
165   GeomAbs_Shape Cont = GeomAbs_C2;
166   GeomFill_Pipe Pipe;
167   Pipe.GenerateParticularCase(Standard_True);
168
169   Standard_Boolean Option_NS = Standard_False;
170   Standard_Integer indice_path = 2, narg = n;
171   if (! strcmp(a[1],"-NS")) {
172     Option_NS = Standard_True;
173     indice_path++;
174     narg--;
175   }
176   Handle(Geom_Curve) path = DrawTrSurf::GetCurve(a[indice_path]);
177   if ( path.IsNull()) return 1;
178
179   Standard_Integer isect = indice_path+1;
180   Handle(Geom_Curve) firstS = DrawTrSurf::GetCurve(a[isect]);
181   if ( firstS.IsNull()) {
182     if ( narg == 4) { 
183       // tuyau a rayon constant.
184       Pipe.Init(path, Draw::Atof(a[isect]));
185     }
186     else 
187       return 1;
188   }
189   else {
190     if ( narg == 4) {
191       // tuyau a section constante.
192       Pipe.Init(path,firstS, GeomFill_IsCorrectedFrenet);
193     }
194     else {
195       if ( narg == 5 && !Option_NS) {
196         // tuyau a section evolutive
197         Handle(Geom_Curve) lastS = DrawTrSurf::GetCurve(a[isect+1]);
198         Cont = GeomAbs_C2;
199         Pipe.Init(path, firstS, lastS); 
200       }
201       else { 
202         // tuyau a N sections, N>=2
203         TColGeom_SequenceOfCurve Seq;
204         Seq.Clear();
205         Seq.Append(firstS);
206         for (Standard_Integer i=isect+1;i<n;i++) {
207           Handle(Geom_Curve) nextS = DrawTrSurf::GetCurve(a[i]);
208           Seq.Append(nextS);
209         }
210         Cont = GeomAbs_C2;
211         Pipe.Init(path, Seq); 
212       }
213     }
214   }
215
216   Pipe.Perform(1.e-4, Standard_False,  Cont);
217   DrawTrSurf::Set(a[indice_path-1], Pipe.Surface());
218
219   return 0;
220 }
221
222 //=======================================================================
223 //function : ruled
224 //purpose  : 
225 //=======================================================================
226
227 static Standard_Integer ruled(Draw_Interpretor& di,
228                               Standard_Integer n, const char** a)
229 {
230   if ( n < 4) return 1;
231
232   Handle(Geom_Curve) C1 = DrawTrSurf::GetCurve(a[2]);
233   if ( C1.IsNull()) {
234     di << " C1 is not a Curve ==> Command failed\n";
235     return 1;
236   }
237   Handle(Geom_Curve) C2 = DrawTrSurf::GetCurve(a[3]);
238   if ( C2.IsNull()) {
239     di << " C2 is not a Curve ==> Command failed\n";
240     return 1;
241   }
242   
243   Handle(Geom_Surface) S = GeomFill::Surface(C1,C2);
244   if ( S.IsNull()) return 1;
245
246   DrawTrSurf::Set(a[1],S);
247   return 0;
248 }
249
250 //=======================================================================
251 //function : appsurf
252 //purpose  : 
253 //=======================================================================
254
255 static Standard_Integer appsurf(Draw_Interpretor& di,
256                                 Standard_Integer n, const char** a)
257 {
258   if ( n < 4) return 1;
259   
260   GeomFill_SectionGenerator Section;
261   Standard_Integer i;
262   Handle(Geom_Curve) C;
263   for ( i = 2; i < n; i++) {
264     C = DrawTrSurf::GetCurve(a[i]);
265     if (C.IsNull()) 
266       return 1;
267     Section.AddCurve(C);
268   }
269   
270   Section.Perform(Precision::PConfusion());
271
272   Handle(GeomFill_Line) Line = new GeomFill_Line(n-2);
273   Standard_Integer NbIt = 0;
274   GeomFill_AppSurf App( 3, 8, Precision::Confusion(), Precision::PConfusion(),
275                         NbIt);
276
277   App.Perform( Line, Section);
278
279   if ( !App.IsDone()) {
280     di << "    Approximation aux fraises \n";
281   }
282
283   Standard_Integer UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots;
284   App.SurfShape(UDegree, VDegree, NbUPoles, NbVPoles, NbUKnots, NbVKnots);
285
286   Handle(Geom_BSplineSurface) GBS = new 
287     Geom_BSplineSurface(App.SurfPoles(),
288                         App.SurfWeights(),
289                         App.SurfUKnots(),
290                         App.SurfVKnots(),
291                         App.SurfUMults(),
292                         App.SurfVMults(),
293                         App.UDegree(),
294                         App.VDegree());
295
296   DrawTrSurf::Set(a[1],GBS);
297   return 0;
298 }
299
300 static Standard_Integer fillcurves(Draw_Interpretor& /*di*/,
301                                    Standard_Integer n, const char** a)
302 {
303   if ( n < 6) return 1;
304   
305   Standard_Integer i;
306   Handle(Geom_Curve) aC;
307   Handle(Geom_BSplineCurve) C[4];
308   for ( i = 2; i < 6; i++) {
309     aC = DrawTrSurf::GetCurve(a[i]);
310     if (aC.IsNull()) 
311       return 1;
312     C[i-2] = GeomConvert::CurveToBSplineCurve(aC, Convert_RationalC1);
313   }
314
315   Standard_Integer ist = 2;
316   GeomFill_FillingStyle Style = GeomFill_CoonsStyle;
317   if(n > 6) ist = Draw::Atoi(a[6]);
318
319   if(ist == 1) Style =  GeomFill_StretchStyle;
320   if(ist == 2) Style =  GeomFill_CoonsStyle;
321   if(ist == 3) Style =  GeomFill_CurvedStyle;
322
323   GeomFill_BSplineCurves aFilling(C[0], C[1], C[2], C[3], Style);
324    
325   const Handle(Geom_BSplineSurface)& GBS = aFilling.Surface();
326   DrawTrSurf::Set(a[1],GBS);
327   return 0;
328 }
329
330
331 //=======================================================================
332 //function : GetSurfaceContinuity
333 //purpose  : Returns the continuity of the given surface
334 //=======================================================================
335 static Standard_Integer GetSurfaceContinuity( Draw_Interpretor& theDI,
336                                               Standard_Integer theNArg,
337                                               const char** theArgv)
338 {
339   if(theNArg != 2)
340   {
341     theDI << "Use: getsurfcontinuity surface\n";
342     return 1;
343   }
344
345   Handle(Geom_Surface) GS1 = DrawTrSurf::GetSurface(theArgv[1]);
346   if(GS1.IsNull())
347   {
348     theDI << "Argument is not a surface!\n";
349     return 1;
350   }
351
352   char aContName[7][3] = {"C0",   //0
353                           "G1",   //1
354                           "C1",   //2
355                           "G2",   //3
356                           "C2",   //4
357                           "C3",   //5
358                           "CN"};  //6
359
360   theDI << theArgv[1] << " has " << aContName[GS1->Continuity()] << " continuity.\n";
361
362   return 0;
363 }
364
365
366 //=======================================================================
367 //function : SurfaceCommands
368 //purpose  : 
369 //=======================================================================
370
371
372 void  GeometryTest::SurfaceCommands(Draw_Interpretor& theCommands)
373 {
374   static Standard_Boolean loaded = Standard_False;
375   if (loaded) return;
376   loaded = Standard_True;
377
378   DrawTrSurf::BasicCommands(theCommands);
379
380   const char* g;
381
382   g = "GEOMETRY surfaces creation";
383
384
385   theCommands.Add("tuyau",
386                   "tuyau [-NS] result Path Curve/Radius [Curve2] [Curve3] ... \n the option -NS is used only with 2 sections.\n With it, <result> is going from the first section to the last section \n Without, <result> is a pipe by evolutive section ",
387                   __FILE__,
388                   tuyau,g);
389
390   theCommands.Add("partuyau",
391                   "tuyau result Path Curve/Radius [Curve2]\n the parametrization of the surface in the V direction will be as the Path",
392                   __FILE__,
393                   tuyau,g);
394
395   theCommands.Add("sweep", "sweep result [options] path [Surf] curve [Tol [nbsegment]]\n sweep the the curve along the path, options are \n -FX : Tangent and Normal are fixed\n -FR : Tangent and Normal are given by Frenet trihedron \n -CF : Tangente is given by Frenet, \n     the Normal is computed to minimize the torsion \n -DX : Tangent and Normal are given by Darboux trihedron \n     <path> have to be a 2d curve,\n     <Surf> have to be defined\n -CN dx dy dz : Normal is given by dx dy dz" ,
396                   __FILE__,
397                   sweep, g);
398
399   theCommands.Add("ruled",
400                   "ruled result C1 C2",
401                   __FILE__,
402                   ruled,g);
403
404   theCommands.Add("appsurf",
405                   "appsurf result C1 C2 C3 .....: \n\tCreate a surface passing through the curves",
406                   __FILE__,
407                   appsurf,g);
408  
409  theCommands.Add("fillcurves",
410                   "fillcurves result C1 C2 C3 C4 [style 1/2/3]: \n\tCreate a surface filling frame of 4 curves",
411                   __FILE__,
412                   fillcurves,g);
413
414  theCommands.Add("getsurfcontinuity",
415                   "getsurfcontinuity surface: \n\tReturns the continuity of the given surface",
416                   __FILE__,
417                   GetSurfaceContinuity,g);
418
419
420 }
421
422
423
424