Integration of OCCT 6.5.0 from SVN
[occt.git] / src / Contap / Contap_HContToolGen.gxx
1 //-- File:      Contap_HContToolGen.cdl
2 //-- Created:   Fri Jul  2 13:19:22 1995
3 //-- Author:    Laurent BUCHARD
4 //--            <lbr@mastox>
5 //---Copyright:  Matra Datavision 1995
6
7
8
9 #include <Extrema_EPCOfExtPC2d.hxx>
10 #include <Extrema_POnCurv2d.hxx>
11
12 #include <GeomAbs_SurfaceType.hxx>
13 #include <Standard_OutOfRange.hxx>
14
15 #include <Handle_Geom_BezierSurface.hxx>
16 #include <Handle_Geom_BSplineSurface.hxx>
17 #include <Handle_Geom2d_BezierCurve.hxx>
18 #include <Handle_Geom2d_BSplineCurve.hxx>
19
20 #include <Geom_BezierSurface.hxx>
21 #include <Geom_BSplineSurface.hxx>
22 #include <Geom2d_BezierCurve.hxx>
23 #include <Geom2d_BSplineCurve.hxx>
24
25 static Standard_Real uinf,vinf,usup,vsup;
26
27 #include TheHSurface_hxx
28 #include TheHCurve2d_hxx
29 #include TheHVertex_hxx
30
31
32 Standard_Integer Contap_HContToolGen::NbSamplesV
33   (const TheHSurface& S,
34    const Standard_Real ,
35    const Standard_Real )
36 {
37   Standard_Integer nbs;
38   GeomAbs_SurfaceType typS = S->GetType();
39   switch(typS) { 
40   case GeomAbs_Plane:
41     {
42       nbs = 2;
43     }
44     break;
45   case GeomAbs_BezierSurface: 
46     {
47       nbs =  3 + S->NbVPoles();
48     }
49     break;
50   case GeomAbs_BSplineSurface: 
51     {
52       //-- Handle_Geom_BSplineSurface& HBS=S->BSpline();
53       nbs = S->NbVKnots();
54       nbs*= S->VDegree();
55       if(nbs < 2) nbs=2;
56       
57     }
58     break;
59   case GeomAbs_Cylinder:
60   case GeomAbs_Cone:
61   case GeomAbs_Sphere:
62   case GeomAbs_Torus:
63   case GeomAbs_SurfaceOfRevolution:
64   case GeomAbs_SurfaceOfExtrusion:
65     {
66       nbs = 15;
67     }
68     break;
69     
70   default: 
71     {
72       nbs = 10;
73     }
74     break;
75   }
76   return(nbs);
77 }
78
79 Standard_Integer Contap_HContToolGen::NbSamplesU
80   (const TheHSurface& S,
81    const Standard_Real ,
82    const Standard_Real )
83 {
84   Standard_Integer nbs;
85   GeomAbs_SurfaceType typS = S->GetType();
86   switch(typS) { 
87   case GeomAbs_Plane:
88     {
89       nbs = 2;
90     }
91     break;
92   case GeomAbs_BezierSurface: 
93     {
94       nbs =  3 + S->NbUPoles();
95     }
96     break;
97   case GeomAbs_BSplineSurface: 
98     {
99       //-- Handle_Geom_BSplineSurface& HBS=S->BSpline();
100       nbs = S->NbUKnots();
101       nbs*= S->UDegree();
102       if(nbs < 2) nbs=2;
103       
104     }
105     break;
106   case GeomAbs_Torus: 
107     {
108       nbs = 20;
109     }
110     break;
111   case GeomAbs_Cylinder:
112   case GeomAbs_Cone:
113   case GeomAbs_Sphere:
114   case GeomAbs_SurfaceOfRevolution:
115   case GeomAbs_SurfaceOfExtrusion:
116     {
117       nbs = 10;
118     }
119     break;
120     
121   default: 
122     {
123       nbs = 10;
124     }
125     break;
126   }
127   return(nbs);
128 }
129
130 Standard_Integer Contap_HContToolGen::NbSamplePoints
131   (const TheHSurface& S)
132 {
133   uinf = S->FirstUParameter();
134   usup = S->LastUParameter();
135   vinf = S->FirstVParameter();
136   vsup = S->LastVParameter();
137
138   if (usup < uinf) {
139     Standard_Real temp = uinf;
140     uinf = usup;
141     usup = temp;
142     }
143     if (vsup < vinf) {
144     Standard_Real temp = vinf;
145     vinf = vsup;
146     vsup = temp;
147   }
148   if (uinf == RealFirst() && usup == RealLast()) {
149     uinf = -1.e5;
150     usup =  1.e5;
151   }
152   else if (uinf == RealFirst()) {
153     uinf = usup - 2.e5;
154   }
155   else if (usup == RealLast()) {
156     usup = uinf + 2.e5;
157   }
158   
159   if (vinf == RealFirst() && vsup == RealLast()) {
160     vinf = -1.e5;
161     vsup =  1.e5;
162   }
163   else if (vinf == RealFirst()) {
164     vinf = vsup - 2.e5;
165   }
166   else if (vsup == RealLast()) {
167     vsup = vinf + 2.e5;
168   }
169   if(S->GetType() ==   GeomAbs_BSplineSurface) { 
170     Standard_Integer m = (NbSamplesU(S,uinf,usup)/3) * (NbSamplesV(S,vinf,vsup)/3);
171     if(m>5) return(m); 
172     else return(5);
173   }
174   else 
175     return 5;
176 }
177
178 void Contap_HContToolGen::SamplePoint (const TheHSurface& S,
179                                      const Standard_Integer Index,
180                                      Standard_Real& U,
181                                      Standard_Real& V )
182 {
183   if(S->GetType() ==   GeomAbs_BSplineSurface) {
184     Standard_Integer nbIntU = NbSamplesU(S,uinf,usup)/3;
185     Standard_Integer nbIntV = NbSamplesV(S,vinf,vsup)/3;
186     if(nbIntU * nbIntV >5) { 
187       Standard_Integer indU = (Index-1)/nbIntU;                  //----   0 --> nbIntV
188       Standard_Integer indV = (Index-1) - indU*nbIntU;           //----   0 --> nbIntU
189       
190       U = uinf + ((usup-uinf)/((Standard_Real)(nbIntU+1)))*(Standard_Real)(indU+1);
191       V = vinf + ((vsup-vinf)/((Standard_Real)(nbIntV+2)))*(Standard_Real)(indV+1);
192       
193       //-- cout<<"Index :"<<Index<<"  uinf:"<<uinf<<"  usup:"<<usup<<"  vinf:"<<vinf<<" vsup:"<<vsup<<"  ";
194       //-- cout<<"  ("<<indU<<"/"<<nbIntU<<" ->U:"<<U<<"  ";
195       //-- cout<<"  ("<<indV<<"/"<<nbIntV<<" ->V:"<<V<<endl;
196       return;
197     }
198   }
199   
200   switch (Index) {
201   case 1:
202     U = 0.75*uinf + 0.25*usup; //0.25;
203     V = 0.75*vinf + 0.25*vsup; //0.25;
204     break;
205   case 2:
206     U = 0.75*uinf + 0.25*usup; //0.25;
207     V = 0.25*vinf + 0.75*vsup; //0.75;
208     break;
209   case 3:
210     U = 0.25*uinf + 0.75*usup; //0.75;
211     V = 0.75*vinf + 0.25*vsup; //0.25;
212     break;
213   case 4:
214     U = 0.25*uinf + 0.75*usup; //0.75;
215     V = 0.25*vinf + 0.75*vsup; //0.75;
216     break;
217   default:
218     U = 0.5*(uinf+usup); //0.5;
219     V = 0.5*(vinf+vsup); //0.5;
220   }
221 }
222
223
224 Standard_Integer Contap_HContToolGen::NbSamplesOnArc
225   (const TheHCurve2d& A) { 
226
227   GeomAbs_CurveType CurveType  = A->GetType();
228
229   Standard_Real nbsOnC = 5;
230   switch(CurveType) { 
231   case GeomAbs_Line:
232     nbsOnC = 2;
233     break;
234   case GeomAbs_Circle:
235   case GeomAbs_Ellipse:
236   case GeomAbs_Hyperbola:
237   case GeomAbs_Parabola:
238     nbsOnC = 10;
239     break;
240   case GeomAbs_BezierCurve:
241     nbsOnC = A->NbPoles();
242     break;
243   case GeomAbs_BSplineCurve: { 
244     //-- Handle_Geom2d_BSplineCurve& BSC=A->BSpline();
245     nbsOnC = 2 + A->NbKnots() * A->Degree();
246     break;
247   }
248   default:
249     nbsOnC = 10;
250   } 
251 //#ifndef DEB
252   return (Standard_Integer)(nbsOnC);
253 //#else
254 //  return(nbsOnC);
255 //#endif
256 }
257
258
259 void Contap_HContToolGen::Bounds(const TheHCurve2d& A,
260                                Standard_Real& Ufirst,
261                                Standard_Real& Ulast)
262 {
263   Ufirst = A->FirstParameter();
264   Ulast  = A->LastParameter();
265 }
266
267
268 Standard_Boolean Contap_HContToolGen::Project (const TheHCurve2d& C,
269                                                  const gp_Pnt2d& P,
270                                                  Standard_Real& Paramproj,
271                                                  gp_Pnt2d& Ptproj)
272
273 {
274
275   Standard_Real epsX = 1.e-8;
276   Standard_Integer Nbu = 20;
277   Standard_Real Tol = 1.e-5;
278   Standard_Real Dist2;
279
280   Extrema_EPCOfExtPC2d extrema(P,C->Curve2d(),Nbu,epsX,Tol);
281   if (!extrema.IsDone()) {
282     return Standard_False;
283   }
284   Standard_Integer Nbext = extrema.NbExt();
285   if (Nbext == 0) {
286     return Standard_False;
287   }
288   Standard_Integer indexmin = 1;
289   Dist2 = extrema.SquareDistance(1);
290   for (Standard_Integer i=2; i<=Nbext; i++) {
291     if (extrema.SquareDistance(i) < Dist2) {
292       indexmin = i;
293       Dist2 = extrema.SquareDistance(i);
294     }
295   }
296   Paramproj = extrema.Point(indexmin).Parameter();
297   Ptproj = extrema.Point(indexmin).Value();
298   return Standard_True;
299 }
300
301
302 Standard_Real Contap_HContToolGen::Tolerance (const TheHVertex& V,
303                                             const TheHCurve2d& C)
304 {
305 //  return BRepAdaptor2d_Curve2dTool::Resolution(C,BRep_Tool::Tolerance(V));
306   return V->Resolution(C);
307 }
308
309 Standard_Real Contap_HContToolGen::Parameter (const TheHVertex& V,
310                                             const TheHCurve2d& C)
311 {
312 //  return BRep_Tool::Parameter(V,C.Edge());
313   return V->Parameter(C);
314 }
315
316
317
318 Standard_Boolean Contap_HContToolGen::HasBeenSeen
319   (const TheHCurve2d&)
320 {
321   return Standard_False;
322 }
323
324 Standard_Integer Contap_HContToolGen::NbPoints(const TheHCurve2d&)
325 {
326   return 0;
327 }
328
329 void Contap_HContToolGen::Value(const TheHCurve2d&,
330                               const Standard_Integer,
331                               gp_Pnt&,
332                               Standard_Real&,
333                               Standard_Real&)
334 {
335   Standard_OutOfRange::Raise();
336 }
337
338 Standard_Boolean Contap_HContToolGen::IsVertex(const TheHCurve2d&,
339                                              const Standard_Integer)
340 {
341   return Standard_False;
342 }
343
344 void Contap_HContToolGen::Vertex(const TheHCurve2d&,
345                                const Standard_Integer,
346                                TheHVertex&)
347 {
348   Standard_OutOfRange::Raise();
349 }
350
351 Standard_Integer Contap_HContToolGen::NbSegments(const TheHCurve2d&)
352 {
353   return 0;
354 }
355
356 Standard_Boolean Contap_HContToolGen::HasFirstPoint
357   (const TheHCurve2d&,
358    const Standard_Integer,
359    Standard_Integer&)
360 {
361   Standard_OutOfRange::Raise();
362     return Standard_False;
363 }
364
365 Standard_Boolean Contap_HContToolGen::HasLastPoint
366   (const TheHCurve2d&,
367    const Standard_Integer,
368    Standard_Integer&)
369 {
370   Standard_OutOfRange::Raise();
371     return Standard_False;
372 }
373
374 Standard_Boolean Contap_HContToolGen::IsAllSolution
375   (const TheHCurve2d&)
376
377 {
378   return Standard_False;
379 }
380