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