0026937: Eliminate NO_CXX_EXCEPTION macro support
[occt.git] / src / Contap / Contap_HContTool.cxx
CommitLineData
b311480e 1// Created on: 1995-07-02
2// Created by: Laurent BUCHARD
3// Copyright (c) 1995-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 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
973c2be1 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.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
e2065c2f 17
42cf5bc1 18#include <Adaptor2d_HCurve2d.hxx>
19#include <Adaptor3d_HSurface.hxx>
20#include <Adaptor3d_HVertex.hxx>
21#include <Contap_HContTool.hxx>
7fd59977 22#include <Extrema_EPCOfExtPC2d.hxx>
23#include <Extrema_POnCurv2d.hxx>
42cf5bc1 24#include <gp_Pnt.hxx>
25#include <gp_Pnt2d.hxx>
7fd59977 26
7fd59977 27static Standard_Real uinf,vinf,usup,vsup;
28
e2065c2f 29Standard_Integer Contap_HContTool::NbSamplesV
30(const Handle(Adaptor3d_HSurface)& S,
31 const Standard_Real ,
32 const Standard_Real )
7fd59977 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 {
857ffd5e 49 //-- Handle(Geom_BSplineSurface)& HBS=S->BSpline();
7fd59977 50 nbs = S->NbVKnots();
51 nbs*= S->VDegree();
52 if(nbs < 2) nbs=2;
e2065c2f 53
7fd59977 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;
e2065c2f 66
7fd59977 67 default:
68 {
69 nbs = 10;
70 }
71 break;
72 }
73 return(nbs);
74}
75
e2065c2f 76Standard_Integer Contap_HContTool::NbSamplesU
77(const Handle(Adaptor3d_HSurface)& S,
78 const Standard_Real ,
79 const Standard_Real )
7fd59977 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 {
857ffd5e 96 //-- Handle(Geom_BSplineSurface)& HBS=S->BSpline();
7fd59977 97 nbs = S->NbUKnots();
98 nbs*= S->UDegree();
99 if(nbs < 2) nbs=2;
e2065c2f 100
7fd59977 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;
e2065c2f 117
7fd59977 118 default:
119 {
120 nbs = 10;
121 }
122 break;
123 }
124 return(nbs);
125}
126
e2065c2f 127Standard_Integer Contap_HContTool::NbSamplePoints
128(const Handle(Adaptor3d_HSurface)& S)
7fd59977 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;
e2065c2f 139 }
140 if (vsup < vinf) {
7fd59977 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 }
e2065c2f 155
7fd59977 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
e2065c2f 175void Contap_HContTool::SamplePoint (const Handle(Adaptor3d_HSurface)& S,
176 const Standard_Integer Index,
177 Standard_Real& U,
178 Standard_Real& V )
7fd59977 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
e2065c2f 186
7fd59977 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);
e2065c2f 189
7fd59977 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 }
e2065c2f 196
7fd59977 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
e2065c2f 221Standard_Integer Contap_HContTool::NbSamplesOnArc
222(const Handle(Adaptor2d_HCurve2d)& A) {
7fd59977 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: {
857ffd5e 241 //-- Handle(Geom2d_BSplineCurve)& BSC=A->BSpline();
7fd59977 242 nbsOnC = 2 + A->NbKnots() * A->Degree();
243 break;
e2065c2f 244 }
7fd59977 245 default:
246 nbsOnC = 10;
247 }
7fd59977 248 return (Standard_Integer)(nbsOnC);
7fd59977 249}
250
251
e2065c2f 252void Contap_HContTool::Bounds(const Handle(Adaptor2d_HCurve2d)& A,
253 Standard_Real& Ufirst,
254 Standard_Real& Ulast)
7fd59977 255{
256 Ufirst = A->FirstParameter();
257 Ulast = A->LastParameter();
258}
259
260
e2065c2f 261Standard_Boolean Contap_HContTool::Project (const Handle(Adaptor2d_HCurve2d)& C,
262 const gp_Pnt2d& P,
263 Standard_Real& Paramproj,
264 gp_Pnt2d& Ptproj)
7fd59977 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
e2065c2f 295Standard_Real Contap_HContTool::Tolerance (const Handle(Adaptor3d_HVertex)& V,
296 const Handle(Adaptor2d_HCurve2d)& C)
7fd59977 297{
e2065c2f 298 // return BRepAdaptor2d_Curve2dTool::Resolution(C,BRep_Tool::Tolerance(V));
7fd59977 299 return V->Resolution(C);
300}
301
e2065c2f 302Standard_Real Contap_HContTool::Parameter (const Handle(Adaptor3d_HVertex)& V,
303 const Handle(Adaptor2d_HCurve2d)& C)
7fd59977 304{
e2065c2f 305 // return BRep_Tool::Parameter(V,C.Edge());
7fd59977 306 return V->Parameter(C);
307}
308
309
310
e2065c2f 311Standard_Boolean Contap_HContTool::HasBeenSeen
312(const Handle(Adaptor2d_HCurve2d)&)
7fd59977 313{
314 return Standard_False;
315}
316
e2065c2f 317Standard_Integer Contap_HContTool::NbPoints(const Handle(Adaptor2d_HCurve2d)&)
7fd59977 318{
319 return 0;
320}
321
e2065c2f 322void Contap_HContTool::Value(const Handle(Adaptor2d_HCurve2d)&,
323 const Standard_Integer,
324 gp_Pnt&,
325 Standard_Real&,
326 Standard_Real&)
7fd59977 327{
9775fa61 328 throw Standard_OutOfRange();
7fd59977 329}
330
e2065c2f 331Standard_Boolean Contap_HContTool::IsVertex(const Handle(Adaptor2d_HCurve2d)&,
332 const Standard_Integer)
7fd59977 333{
334 return Standard_False;
335}
336
e2065c2f 337void Contap_HContTool::Vertex(const Handle(Adaptor2d_HCurve2d)&,
338 const Standard_Integer,
339 Handle(Adaptor3d_HVertex)&)
7fd59977 340{
9775fa61 341 throw Standard_OutOfRange();
7fd59977 342}
343
e2065c2f 344Standard_Integer Contap_HContTool::NbSegments(const Handle(Adaptor2d_HCurve2d)&)
7fd59977 345{
346 return 0;
347}
348
e2065c2f 349Standard_Boolean Contap_HContTool::HasFirstPoint
350(const Handle(Adaptor2d_HCurve2d)&,
351 const Standard_Integer,
352 Standard_Integer&)
7fd59977 353{
9775fa61 354 throw Standard_OutOfRange();
7fd59977 355}
356
e2065c2f 357Standard_Boolean Contap_HContTool::HasLastPoint
358(const Handle(Adaptor2d_HCurve2d)&,
359 const Standard_Integer,
360 Standard_Integer&)
7fd59977 361{
9775fa61 362 throw Standard_OutOfRange();
7fd59977 363}
364
e2065c2f 365Standard_Boolean Contap_HContTool::IsAllSolution
366(const Handle(Adaptor2d_HCurve2d)&)
7fd59977 367
368{
369 return Standard_False;
370}
371