Integration of OCCT 6.5.0 from SVN
[occt.git] / src / BRepTopAdaptor / BRepTopAdaptor_TopolTool.cxx
CommitLineData
7fd59977 1
2#include <Standard_NotImplemented.hxx>
3
4#include <BRepTopAdaptor_TopolTool.ixx>
5
6#include <Standard_ConstructionError.hxx>
7#include <Standard_DomainError.hxx>
8#include <BRepAdaptor_HSurface.hxx>
9#include <BRepAdaptor_HCurve2d.hxx>
10#include <BRepTopAdaptor_HVertex.hxx>
11#include <BRepClass_FaceClassifier.hxx>
12#include <BRepClass_FaceExplorer.hxx>
13#include <TopoDS.hxx>
14#include <Precision.hxx>
15
16#include <BRepTopAdaptor_FClass2d.hxx>
17#include <BRep_Tool.hxx>
18
19#include <TColgp_Array2OfPnt.hxx>
20#include <Geom_BezierSurface.hxx>
21#include <Geom_BSplineSurface.hxx>
22
23static
24 void Analyse(const TColgp_Array2OfPnt& array2,
25 const Standard_Integer nbup,
26 const Standard_Integer nbvp,
27 Standard_Integer& myNbSamplesU,
28 Standard_Integer& myNbSamplesV);
29//=======================================================================
30//function : BRepTopAdaptor_TopolTool
31//purpose :
32//=======================================================================
33 BRepTopAdaptor_TopolTool::BRepTopAdaptor_TopolTool () : myFClass2d(NULL)
34{
35 myNbSamplesU=-1;
36}
37
38//=======================================================================
39//function : BRepTopAdaptor_TopolTool
40//purpose :
41//=======================================================================
42 BRepTopAdaptor_TopolTool::BRepTopAdaptor_TopolTool(const Handle(Adaptor3d_HSurface)& S)
43: myFClass2d(NULL)
44{
45 Initialize(S);
46 //myS = S;
47}
48//=======================================================================
49//function : Initialize
50//purpose :
51//=======================================================================
52 void BRepTopAdaptor_TopolTool::Initialize()
53{
54 Standard_NotImplemented::Raise("BRepTopAdaptor_TopolTool::Initialize()");
55}
56//=======================================================================
57//function : Initialize
58//purpose :
59//=======================================================================
60 void BRepTopAdaptor_TopolTool::Initialize(const Handle(Adaptor3d_HSurface)& S)
61{
62 Handle(BRepAdaptor_HSurface) brhs =
63 Handle(BRepAdaptor_HSurface)::DownCast(S);
64 if (brhs.IsNull()) {Standard_ConstructionError::Raise();}
65 TopoDS_Shape s_wnt = ((BRepAdaptor_Surface *)&(brhs->Surface()))->Face();
66 s_wnt.Orientation(TopAbs_FORWARD);
67 myFace = TopoDS::Face(s_wnt);
68 if(myFClass2d != NULL) {
69 delete (BRepTopAdaptor_FClass2d *)myFClass2d;
70 }
71 myFClass2d = NULL;
72 myNbSamplesU=-1;
73 myS = S;
74 myCurves.Clear();
75 TopExp_Explorer ex(myFace,TopAbs_EDGE);
76 for (; ex.More(); ex.Next()) {
77 Handle(BRepAdaptor_HCurve2d) aCurve = new BRepAdaptor_HCurve2d
78 (BRepAdaptor_Curve2d(TopoDS::Edge(ex.Current()),myFace));
79 myCurves.Append(aCurve);
80 }
81 myCIterator = TColStd_ListIteratorOfListOfTransient();
82}
83//=======================================================================
84//function : Initialize
85//purpose :
86//=======================================================================
87 void BRepTopAdaptor_TopolTool::Initialize(const Handle(Adaptor2d_HCurve2d)& C)
88{
89 myCurve = Handle(BRepAdaptor_HCurve2d)::DownCast(C);
90 if (myCurve.IsNull()) {Standard_ConstructionError::Raise();}
91}
92//=======================================================================
93//function : Init
94//purpose :
95//=======================================================================
96 void BRepTopAdaptor_TopolTool::Init ()
97{
98 myCIterator.Initialize(myCurves);
99}
100//=======================================================================
101//function : More
102//purpose :
103//=======================================================================
104 Standard_Boolean BRepTopAdaptor_TopolTool::More ()
105{
106 return myCIterator.More();
107}
108//=======================================================================
109//function : Next
110//purpose :
111//=======================================================================
112 void BRepTopAdaptor_TopolTool::Next()
113{
114 myCIterator.Next();
115}
116//=======================================================================
117//function : Value
118//purpose :
119//=======================================================================
120 Handle(Adaptor2d_HCurve2d) BRepTopAdaptor_TopolTool::Value ()
121{
122 return Handle(Adaptor2d_HCurve2d)::DownCast(myCIterator.Value());
123}
124//modified by NIZNHY-PKV Tue Mar 27 14:23:40 2001 f
125//=======================================================================
126//function : Edge
127//purpose :
128//=======================================================================
129 Standard_Address BRepTopAdaptor_TopolTool::Edge () const
130{
131 Handle(BRepAdaptor_HCurve2d) aHCurve =
132 Handle(BRepAdaptor_HCurve2d)::DownCast(myCIterator.Value());
133 const BRepAdaptor_Curve2d& aCurve = (const BRepAdaptor_Curve2d&)aHCurve->Curve2d();
134 return Standard_Address (& aCurve.Edge());
135}
136
137//modified by NIZNHY-PKV Tue Mar 27 14:23:43 2001 t
138//=======================================================================
139//function : InitVertexIterator
140//purpose :
141//=======================================================================
142 void BRepTopAdaptor_TopolTool::InitVertexIterator ()
143{
144 myVIterator.Init(((BRepAdaptor_Curve2d *)&(myCurve->Curve2d()))->Edge(),TopAbs_VERTEX);
145}
146//=======================================================================
147//function : NextVertex
148//purpose :
149//=======================================================================
150 Standard_Boolean BRepTopAdaptor_TopolTool::MoreVertex ()
151{
152 return myVIterator.More();
153}
154
155 void BRepTopAdaptor_TopolTool::NextVertex ()
156{
157 myVIterator.Next();
158}
159//=======================================================================
160//function : Vertex
161//purpose :
162//=======================================================================
163 Handle(Adaptor3d_HVertex) BRepTopAdaptor_TopolTool::Vertex ()
164{
165 return new
166 BRepTopAdaptor_HVertex(TopoDS::Vertex(myVIterator.Current()),myCurve);
167}
168
169//=======================================================================
170//function : Classify
171//purpose :
172//=======================================================================
173 TopAbs_State BRepTopAdaptor_TopolTool::Classify(const gp_Pnt2d& P,
174 const Standard_Real Tol,
175 const Standard_Boolean RecadreOnPeriodic)
176{
177 if(myFClass2d == NULL) {
178 myFClass2d = (void *) new BRepTopAdaptor_FClass2d(myFace,Tol);
179 }
180 return(((BRepTopAdaptor_FClass2d *)myFClass2d)->Perform(P,RecadreOnPeriodic));
181}
182
183//=======================================================================
184//function : IsThePointOn
185//purpose :
186//=======================================================================
187 Standard_Boolean BRepTopAdaptor_TopolTool::IsThePointOn(const gp_Pnt2d& P,
188 const Standard_Real Tol,
189 const Standard_Boolean RecadreOnPeriodic)
190{
191 if(myFClass2d == NULL) {
192 myFClass2d = (void *) new BRepTopAdaptor_FClass2d(myFace,Tol);
193 }
194 return(TopAbs_ON==((BRepTopAdaptor_FClass2d *)myFClass2d)->TestOnRestriction(P,Tol,RecadreOnPeriodic));
195}
196
197//=======================================================================
198//function : Destroy
199//purpose :
200//=======================================================================
201 void BRepTopAdaptor_TopolTool::Destroy()
202{
203 if(myFClass2d != NULL) {
204 delete (BRepTopAdaptor_FClass2d *)myFClass2d;
205 myFClass2d=NULL;
206 }
207}
208//=======================================================================
209//function : Orientation
210//purpose :
211//=======================================================================
212 TopAbs_Orientation BRepTopAdaptor_TopolTool::Orientation (const Handle(Adaptor2d_HCurve2d)& C)
213{
214 Handle(BRepAdaptor_HCurve2d) brhc =
215 Handle(BRepAdaptor_HCurve2d)::DownCast(C);
216 return ((BRepAdaptor_Curve2d *)&(brhc->Curve2d()))->Edge().Orientation();
217}
218//=======================================================================
219//function : Orientation
220//purpose :
221//=======================================================================
222 TopAbs_Orientation BRepTopAdaptor_TopolTool::Orientation (const Handle(Adaptor3d_HVertex)& C)
223{
224 return Adaptor3d_TopolTool::Orientation(C);
225}
226//-- ============================================================
227//-- m e t h o d e s u t i l i s e e s p o u r l e s
228//-- s a m p l e s
229//-- ============================================================
230
231//=======================================================================
232//function : Analyse
233//purpose :
234//=======================================================================
235void Analyse(const TColgp_Array2OfPnt& array2,
236 const Standard_Integer nbup,
237 const Standard_Integer nbvp,
238 Standard_Integer& myNbSamplesU,
239 Standard_Integer& myNbSamplesV)
240{
241 gp_Vec Vi,Vip1;
242 Standard_Integer sh,nbch,i,j;
243
244 sh = 1;
245 nbch = 0;
246 if(nbvp>2) {
247 for(i=2;i<nbup;i++) {
248 const gp_Pnt& A=array2.Value(i,1);
249 const gp_Pnt& B=array2.Value(i,2);
250 const gp_Pnt& C=array2.Value(i,3);
251 Vi.SetCoord(C.X()-B.X()-B.X()+A.X(),
252 C.Y()-B.Y()-B.Y()+A.Y(),
253 C.Z()-B.Z()-B.Z()+A.Z());
254 Standard_Integer locnbch=0;
255 for(j=3; j<nbvp;j++) { //-- essai
256 const gp_Pnt& Ax=array2.Value(i,j-1);
257 const gp_Pnt& Bx=array2.Value(i,j);
258 const gp_Pnt& Cx=array2.Value(i,j+1);
259 Vip1.SetCoord(Cx.X()-Bx.X()-Bx.X()+Ax.X(),
260 Cx.Y()-Bx.Y()-Bx.Y()+Ax.Y(),
261 Cx.Z()-Bx.Z()-Bx.Z()+Ax.Z());
262 Standard_Real pd = Vi.Dot(Vip1);
263 Vi=Vip1;
264 if(pd>1.0e-7 || pd<-1.0e-7) {
265 if(pd>0) { if(sh==-1) { sh=1; locnbch++; } }
266 else { if(sh==1) { sh=-1; locnbch++; } }
267 }
268 }
269 if(locnbch>nbch) {
270 nbch=locnbch;
271 }
272 }
273 }
274 myNbSamplesV = nbch+5;
275
276
277 nbch=0;
278 if(nbup>2) {
279 for(j=2;j<nbvp;j++) {
280 const gp_Pnt& A=array2.Value(1,j);
281 const gp_Pnt& B=array2.Value(2,j);
282 const gp_Pnt& C=array2.Value(3,j);
283 Vi.SetCoord(C.X()-B.X()-B.X()+A.X(),
284 C.Y()-B.Y()-B.Y()+A.Y(),
285 C.Z()-B.Z()-B.Z()+A.Z());
286 Standard_Integer locnbch=0;
287 for(i=3; i<nbup;i++) { //-- essai
288 const gp_Pnt& Ax=array2.Value(i-1,j);
289 const gp_Pnt& Bx=array2.Value(i,j);
290 const gp_Pnt& Cx=array2.Value(i+1,j);
291 Vip1.SetCoord(Cx.X()-Bx.X()-Bx.X()+Ax.X(),
292 Cx.Y()-Bx.Y()-Bx.Y()+Ax.Y(),
293 Cx.Z()-Bx.Z()-Bx.Z()+Ax.Z());
294 Standard_Real pd = Vi.Dot(Vip1);
295 Vi=Vip1;
296 if(pd>1.0e-7 || pd<-1.0e-7) {
297 if(pd>0) { if(sh==-1) { sh=1; locnbch++; } }
298 else { if(sh==1) { sh=-1; locnbch++; } }
299 }
300 }
301 if(locnbch>nbch) nbch=locnbch;
302 }
303 }
304 myNbSamplesU = nbch+5;
305}
306
307
308
309
310//=======================================================================
311//function : ComputeSamplePoints
312//purpose :
313//=======================================================================
314 void BRepTopAdaptor_TopolTool::ComputeSamplePoints()
315{
316 Standard_Real uinf,usup,vinf,vsup;
317 uinf = myS->FirstUParameter(); usup = myS->LastUParameter();
318 vinf = myS->FirstVParameter(); vsup = myS->LastVParameter();
319 if (usup < uinf) { Standard_Real temp=uinf; uinf=usup; usup=temp; }
320 if (vsup < vinf) { Standard_Real temp=vinf; vinf=vsup; vsup=temp; }
321 if (uinf == RealFirst() && usup == RealLast()) { uinf=-1.e5; usup=1.e5; }
322 else if (uinf == RealFirst()) { uinf=usup-2.e5; }
323 else if (usup == RealLast()) { usup=uinf+2.e5; }
324
325 if (vinf == RealFirst() && vsup == RealLast()) { vinf=-1.e5; vsup=1.e5; }
326 else if (vinf == RealFirst()) { vinf=vsup-2.e5; }
327 else if (vsup == RealLast()) { vsup=vinf+2.e5; }
328
329 Standard_Integer nbsu,nbsv;
330 GeomAbs_SurfaceType typS = myS->GetType();
331 switch(typS) {
332 case GeomAbs_Plane: { nbsv=2; nbsu=2; } break;
333 case GeomAbs_BezierSurface: { nbsv=3+myS->NbVPoles(); nbsu=3+myS->NbUPoles(); } break;
334 case GeomAbs_BSplineSurface: {
335 nbsv = myS->NbVKnots(); nbsv*= myS->VDegree(); if(nbsv < 4) nbsv=4;
336 nbsu = myS->NbUKnots(); nbsu*= myS->UDegree(); if(nbsu < 4) nbsu=4;
337 }
338 break;
339 case GeomAbs_Cylinder:
340 case GeomAbs_Cone:
341 case GeomAbs_Sphere:
342 case GeomAbs_Torus: {
343 //-- On place 15 echnt pour 2pi
344 //-- pas suffisant ->25 pour 2pi
345 nbsu = (Standard_Integer)(8*(usup-uinf));
346 nbsv = (Standard_Integer)(7*(vsup-vinf));
347 if(nbsu<5) nbsu=5;
348 if(nbsv<5) nbsv=5;
349 if(nbsu>30) nbsu=30; //modif HRT buc60462
350 if(nbsv>15) nbsv=15;
351 //-- printf("\n nbsu=%d nbsv=%d\n",nbsu,nbsv);
352 } break;
353 case GeomAbs_SurfaceOfRevolution:
354 case GeomAbs_SurfaceOfExtrusion: { nbsv = 15; nbsu=25; } break;
355 default: { nbsu = 10; nbsv=10; } break;
356 }
357
358 //-- Si le nb de points est trop grand on analyse
359 //--
360 //--
361
362 if(nbsu<10) nbsu=10;
363 if(nbsv<10) nbsv=10;
364
365 myNbSamplesU = nbsu;
366 myNbSamplesV = nbsv;
367
368 //-- printf("\n BRepTopAdaptor_TopolTool NbSu=%d NbSv=%d ",nbsu,nbsv);
369 if(nbsu>10 || nbsv>10) {
370 if(typS == GeomAbs_BSplineSurface) {
371 const Handle(Geom_BSplineSurface)& Bspl = myS->BSpline();
372 Standard_Integer nbup = Bspl->NbUPoles();
373 Standard_Integer nbvp = Bspl->NbVPoles();
374 TColgp_Array2OfPnt array2(1,nbup,1,nbvp);
375 Bspl->Poles(array2);
376 Analyse(array2,nbup,nbvp,myNbSamplesU,myNbSamplesV);
377 nbsu=myNbSamplesU;
378 nbsv=myNbSamplesV;
379 //-- printf("\n Apres analyse BSPline NbSu=%d NbSv=%d ",myNbSamplesU,myNbSamplesV);
380 }
381 else if(typS == GeomAbs_BezierSurface) {
382 const Handle(Geom_BezierSurface)& Bez = myS->Bezier();
383 Standard_Integer nbup = Bez->NbUPoles();
384 Standard_Integer nbvp = Bez->NbVPoles();
385 TColgp_Array2OfPnt array2(1,nbup,1,nbvp);
386 Bez->Poles(array2);
387 Analyse(array2,nbup,nbvp,myNbSamplesU,myNbSamplesV);
388 nbsu=myNbSamplesU;
389 nbsv=myNbSamplesV;
390 //-- printf("\n Apres analyse Bezier NbSu=%d NbSv=%d ",myNbSamplesU,myNbSamplesV);
391 }
392 }
393
394 if(nbsu<10) nbsu=10;
395 if(nbsv<10) nbsv=10;
396
397 myNbSamplesU = nbsu;
398 myNbSamplesV = nbsv;
399
400 myU0 = uinf;
401 myV0 = vinf;
402
403 myDU = (usup-uinf)/(myNbSamplesU+1);
404 myDV = (vsup-vinf)/(myNbSamplesV+1);
405}
406//=======================================================================
407//function : NbSamplesU
408//purpose :
409//=======================================================================
410 Standard_Integer BRepTopAdaptor_TopolTool::NbSamplesU()
411{
412 if(myNbSamplesU <0) {
413 ComputeSamplePoints();
414 }
415 return(myNbSamplesU);
416}
417//=======================================================================
418//function : NbSamplesV
419//purpose :
420//=======================================================================
421 Standard_Integer BRepTopAdaptor_TopolTool::NbSamplesV()
422{
423 if(myNbSamplesU <0) {
424 ComputeSamplePoints();
425 }
426 return(myNbSamplesV);
427}
428//=======================================================================
429//function : NbSamples
430//purpose :
431//=======================================================================
432 Standard_Integer BRepTopAdaptor_TopolTool::NbSamples()
433{
434 if(myNbSamplesU <0) {
435 ComputeSamplePoints();
436 }
437 return(myNbSamplesU*myNbSamplesV);
438}
439
440//=======================================================================
441//function : SamplePoint
442//purpose :
443//=======================================================================
444 void BRepTopAdaptor_TopolTool::SamplePoint(const Standard_Integer i,
445 gp_Pnt2d& P2d,
446 gp_Pnt& P3d)
447{
448 Standard_Integer iv = 1 + i/myNbSamplesU;
449 Standard_Integer iu = 1+ i-(iv-1)*myNbSamplesU;
450 Standard_Real u=myU0+iu*myDU;
451 Standard_Real v=myV0+iv*myDV;
452 P2d.SetCoord(u,v);
453 P3d=myS->Value(u,v);
454}
455//=======================================================================
456//function : DomainIsInfinite
457//purpose :
458//=======================================================================
459 Standard_Boolean BRepTopAdaptor_TopolTool::DomainIsInfinite()
460{
461 Standard_Real uinf,usup,vinf,vsup;
462 uinf = myS->FirstUParameter(); usup = myS->LastUParameter();
463 vinf = myS->FirstVParameter(); vsup = myS->LastVParameter();
464 if(Precision::IsNegativeInfinite(uinf)) return(Standard_True);
465 if(Precision::IsPositiveInfinite(usup)) return(Standard_True);
466 if(Precision::IsNegativeInfinite(vinf)) return(Standard_True);
467 if(Precision::IsPositiveInfinite(vsup)) return(Standard_True);
468 return(Standard_False);
469}
470
471//=======================================================================
472//function : Has3d
473//purpose :
474//=======================================================================
475
476Standard_Boolean BRepTopAdaptor_TopolTool::Has3d() const
477{
478 return Standard_True;
479}
480
481//=======================================================================
482//function : Tol3d
483//purpose :
484//=======================================================================
485
486Standard_Real BRepTopAdaptor_TopolTool::Tol3d(const Handle(Adaptor2d_HCurve2d)& C) const
487{
488 Handle(BRepAdaptor_HCurve2d) brhc = Handle(BRepAdaptor_HCurve2d)::DownCast(C);
489 if (brhc.IsNull())
490 Standard_DomainError::Raise("BRepTopAdaptor_TopolTool: arc has no 3d representation");
491 const BRepAdaptor_Curve2d& brc = (const BRepAdaptor_Curve2d &)brhc->Curve2d();
492 const TopoDS_Edge& edge = brc.Edge();
493 if (edge.IsNull())
494 Standard_DomainError::Raise("BRepTopAdaptor_TopolTool: arc has no 3d representation");
495 return BRep_Tool::Tolerance(edge);
496}
497
498//=======================================================================
499//function : Tol3d
500//purpose :
501//=======================================================================
502
503Standard_Real BRepTopAdaptor_TopolTool::Tol3d(const Handle(Adaptor3d_HVertex)& V) const
504{
505 Handle(BRepTopAdaptor_HVertex) brhv = Handle(BRepTopAdaptor_HVertex)::DownCast(V);
506 if (brhv.IsNull())
507 Standard_DomainError::Raise("BRepTopAdaptor_TopolTool: vertex has no 3d representation");
508 const TopoDS_Vertex& ver = brhv->Vertex();
509 if (ver.IsNull())
510 Standard_DomainError::Raise("BRepTopAdaptor_TopolTool: vertex has no 3d representation");
511 return BRep_Tool::Tolerance(ver);
512}
513
514//=======================================================================
515//function : Pnt
516//purpose :
517//=======================================================================
518
519gp_Pnt BRepTopAdaptor_TopolTool::Pnt(const Handle(Adaptor3d_HVertex)& V) const
520{
521 Handle(BRepTopAdaptor_HVertex) brhv = Handle(BRepTopAdaptor_HVertex)::DownCast(V);
522 if (brhv.IsNull())
523 Standard_DomainError::Raise("BRepTopAdaptor_TopolTool: vertex has no 3d representation");
524 const TopoDS_Vertex& ver = brhv->Vertex();
525 if (ver.IsNull())
526 Standard_DomainError::Raise("BRepTopAdaptor_TopolTool: vertex has no 3d representation");
527 return BRep_Tool::Pnt(ver);
528}