0022627: Change OCCT memory management defaults
[occt.git] / src / LocOpe / LocOpe_CSIntersector.cxx
CommitLineData
7fd59977 1// File: LocOpe_CSIntersector.lxx
2// Created: Tue Jun 11 09:11:57 1996
3// Author: Jacques GOUSSARD
4// <jag@bravox>
5
6
7#include <LocOpe_CSIntersector.ixx>
8
9#include <LocOpe_PntFace.hxx>
10#include <LocOpe_SequenceOfPntFace.hxx>
11#include <TopExp_Explorer.hxx>
12
13#include <IntCurvesFace_Intersector.hxx>
14#include <GeomAdaptor_HCurve.hxx>
15#include <Geom_Circle.hxx>
16
17
18#include <Standard_ConstructionError.hxx>
19
20#include <TopoDS.hxx>
21#include <Precision.hxx>
22
23static Standard_Boolean LocAfter (const LocOpe_SequenceOfPntFace&,
24 const Standard_Real,
25 const Standard_Real,
26 TopAbs_Orientation&,
27 Standard_Integer&,
28 Standard_Integer&);
29
30static Standard_Boolean LocBefore (const LocOpe_SequenceOfPntFace&,
31 const Standard_Real,
32 const Standard_Real,
33 TopAbs_Orientation&,
34 Standard_Integer&,
35 Standard_Integer&);
36
37static Standard_Boolean LocAfter (const LocOpe_SequenceOfPntFace&,
38 const Standard_Integer,
39 const Standard_Real,
40 TopAbs_Orientation&,
41 Standard_Integer&,
42 Standard_Integer&);
43
44#ifdef DEB
45static Standard_Boolean LocBefore (const LocOpe_SequenceOfPntFace&,
46 const Standard_Integer,
47 Standard_Integer&,
48 Standard_Integer&);
49#endif
50
51
52static void AddPoints(IntCurvesFace_Intersector&,
53 LocOpe_SequenceOfPntFace&,
54 const TopoDS_Face&);
55
56
57
58
59//=======================================================================
60//function : Init
61//purpose :
62//=======================================================================
63
64void LocOpe_CSIntersector::Init(const TopoDS_Shape& S)
65{
66 myDone = Standard_False;
67 myShape = S;
68 if (myPoints != NULL) {
69 delete [] (LocOpe_SequenceOfPntFace *)myPoints;
70 myPoints = NULL;
71 }
72 myNbelem = 0;
73}
74
75
76//=======================================================================
77//function : Perform
78//purpose :
79//=======================================================================
80
81void LocOpe_CSIntersector::Perform(const LocOpe_SequenceOfLin& Slin)
82{
83 if (myShape.IsNull() || Slin.Length() <= 0) {
84 Standard_ConstructionError::Raise();
85 }
86 myDone = Standard_False;
87
88 myNbelem = Slin.Length();
89 if (myPoints != NULL) {
90 delete [] (LocOpe_SequenceOfPntFace *)myPoints;
91 }
92 myPoints =
93 (LocOpe_SequenceOfPntFace *) new LocOpe_SequenceOfPntFace[myNbelem];
94
95 Standard_Real binf = RealFirst();
96 Standard_Real bsup = RealLast();
97 TopExp_Explorer exp(myShape,TopAbs_FACE);
98 for (; exp.More(); exp.Next()) {
99 const TopoDS_Face& theface = TopoDS::Face(exp.Current());
100 IntCurvesFace_Intersector theInt(theface,Precision::PConfusion());
101 for (Standard_Integer i = 1; i<=myNbelem; i++) {
102 theInt.Perform(Slin(i),binf,bsup);
103 if (theInt.IsDone()) {
104 AddPoints(theInt,(((LocOpe_SequenceOfPntFace*)myPoints)[i-1]),theface);
105 }
106 }
107 }
108 myDone = Standard_True;
109}
110
111
112//=======================================================================
113//function : Perform
114//purpose :
115//=======================================================================
116
117void LocOpe_CSIntersector::Perform(const LocOpe_SequenceOfCirc& Scir)
118{
119 if (myShape.IsNull() || Scir.Length() <= 0) {
120 Standard_ConstructionError::Raise();
121 }
122 myDone = Standard_False;
123
124 myNbelem = Scir.Length();
125 if (myPoints != NULL) {
126 delete [] (LocOpe_SequenceOfPntFace *)myPoints;
127 }
128 myPoints =
129 (LocOpe_SequenceOfPntFace *) new LocOpe_SequenceOfPntFace[myNbelem];
130
131 TopExp_Explorer exp(myShape,TopAbs_FACE);
132 Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve ();
133 Standard_Real binf = 0.;
c6541a0c 134 Standard_Real bsup = 2.*M_PI;
7fd59977 135
136
137 for (; exp.More(); exp.Next()) {
138 const TopoDS_Face& theface = TopoDS::Face(exp.Current());
139 IntCurvesFace_Intersector theInt(theface,0.);
7fd59977 140 for (Standard_Integer i = 1; i<=myNbelem; i++) {
141
142 HC->ChangeCurve().Load(new Geom_Circle(Scir(i)));
143 theInt.Perform(HC,binf,bsup);
144 if (theInt.IsDone()) {
145 AddPoints(theInt,(((LocOpe_SequenceOfPntFace*)myPoints)[i-1]),theface);
146 }
147 }
148 }
149 myDone = Standard_True;
150}
151
152
153
154//=======================================================================
155//function : Perform
156//purpose :
157//=======================================================================
158
159void LocOpe_CSIntersector::Perform(const TColGeom_SequenceOfCurve& Scur)
160{
161 if (myShape.IsNull() || Scur.Length() <= 0) {
162 Standard_ConstructionError::Raise();
163 }
164 myDone = Standard_False;
165
166 myNbelem = Scur.Length();
167 if (myPoints != NULL) {
168 delete [] (LocOpe_SequenceOfPntFace *)myPoints;
169 }
170 myPoints =
171 (LocOpe_SequenceOfPntFace *) new LocOpe_SequenceOfPntFace[myNbelem];
172
173 TopExp_Explorer exp(myShape,TopAbs_FACE);
174 Handle(GeomAdaptor_HCurve) HC = new GeomAdaptor_HCurve ();
175 for (; exp.More(); exp.Next()) {
176 const TopoDS_Face& theface = TopoDS::Face(exp.Current());
177 IntCurvesFace_Intersector theInt(theface,0.);
7fd59977 178 for (Standard_Integer i = 1; i<=myNbelem; i++) {
179 if (Scur(i).IsNull()) {
180 continue;
181 }
182 HC->ChangeCurve().Load(Scur(i));
183 Standard_Real binf = HC->FirstParameter();
184 Standard_Real bsup = HC->LastParameter();
185 theInt.Perform(HC,binf,bsup);
186 if (theInt.IsDone()) {
187 AddPoints(theInt,(((LocOpe_SequenceOfPntFace*)myPoints)[i-1]),theface);
188 }
189 }
190 }
191 myDone = Standard_True;
192}
193
194
195
196//=======================================================================
197//function : NbPoints
198//purpose :
199//=======================================================================
200
201Standard_Integer LocOpe_CSIntersector::NbPoints
202 (const Standard_Integer I) const
203{
204 if (!myDone) {StdFail_NotDone::Raise();}
205 if (I <= 0 || I > myNbelem) {
206 Standard_OutOfRange::Raise();
207 }
208 return ((LocOpe_SequenceOfPntFace *)myPoints)[I-1].Length();
209}
210
211//=======================================================================
212//function : Point
213//purpose :
214//=======================================================================
215
216const LocOpe_PntFace& LocOpe_CSIntersector::
217 Point(const Standard_Integer I,
218 const Standard_Integer Index) const
219{
220 if (!myDone) {StdFail_NotDone::Raise();}
221 if (I <= 0 || I > myNbelem) {
222 Standard_OutOfRange::Raise();
223 }
224 return ((LocOpe_SequenceOfPntFace *)myPoints)[I-1](Index);
225}
226
227//=======================================================================
228//function : Destroy
229//purpose :
230//=======================================================================
231
232void LocOpe_CSIntersector::Destroy()
233{
234 if (myPoints != NULL) {
235 delete [] (LocOpe_SequenceOfPntFace *)myPoints;
236 myPoints = NULL;
237 }
238}
239
240
241//=======================================================================
242//function : LocalizeAfter
243//purpose :
244//=======================================================================
245
246Standard_Boolean LocOpe_CSIntersector::LocalizeAfter
247 (const Standard_Integer I,
248 const Standard_Real From,
249 const Standard_Real Tol,
250 TopAbs_Orientation& Or,
251 Standard_Integer& IndFrom,
252 Standard_Integer& IndTo) const
253{
254 if (!myDone) {
255 StdFail_NotDone::Raise();
256 }
257 if (I <= 0 || I > myNbelem) {
258 Standard_OutOfRange::Raise();
259 }
260 return LocAfter((((LocOpe_SequenceOfPntFace*)myPoints)[I-1]),
261 From,Tol,Or,IndFrom,IndTo);
262}
263
264
265//=======================================================================
266//function : LocalizeBefore
267//purpose :
268//=======================================================================
269
270Standard_Boolean LocOpe_CSIntersector::LocalizeBefore
271 (const Standard_Integer I,
272 const Standard_Real From,
273 const Standard_Real Tol,
274 TopAbs_Orientation& Or,
275 Standard_Integer& IndFrom,
276 Standard_Integer& IndTo) const
277{
278 if (!myDone) {
279 StdFail_NotDone::Raise();
280 }
281 if (I <= 0 || I > myNbelem) {
282 Standard_OutOfRange::Raise();
283 }
284 return LocBefore(((LocOpe_SequenceOfPntFace*)myPoints)[I-1],
285 From,Tol,Or,IndFrom,IndTo);
286}
287
288//=======================================================================
289//function : LocalizeAfter
290//purpose :
291//=======================================================================
292
293Standard_Boolean LocOpe_CSIntersector::LocalizeAfter
294 (const Standard_Integer I,
295 const Standard_Integer FromInd,
296 const Standard_Real Tol,
297 TopAbs_Orientation& Or,
298 Standard_Integer& IndFrom,
299 Standard_Integer& IndTo) const
300{
301 if (!myDone) {
302 StdFail_NotDone::Raise();
303 }
304 if (I <= 0 || I > myNbelem) {
305 Standard_OutOfRange::Raise();
306 }
307 return LocAfter(((LocOpe_SequenceOfPntFace*)myPoints)[I-1],
308 FromInd,Tol,Or,IndFrom,IndTo);
309
310}
311
312//=======================================================================
313//function : LocalizeBefore
314//purpose :
315//=======================================================================
316
317Standard_Boolean LocOpe_CSIntersector::LocalizeBefore
318 (const Standard_Integer I,
319 const Standard_Integer FromInd,
320 const Standard_Real Tol,
321 TopAbs_Orientation& Or,
322 Standard_Integer& IndFrom,
323 Standard_Integer& IndTo) const
324{
325 if (!myDone) {
326 StdFail_NotDone::Raise();
327 }
328 if (I <= 0 || I > myNbelem) {
329 Standard_OutOfRange::Raise();
330 }
331 return LocBefore(((LocOpe_SequenceOfPntFace*)myPoints)[I-1],
332 FromInd,Tol,Or,IndFrom,IndTo);
333
334}
335
336
337
338
339//=======================================================================
340//function : LocAfter
341//purpose :
342//=======================================================================
343
344static Standard_Boolean LocAfter (const LocOpe_SequenceOfPntFace& Spt,
345 const Standard_Real From,
346 const Standard_Real Tol,
347 TopAbs_Orientation& Or,
348 Standard_Integer& IndFrom,
349 Standard_Integer& IndTo)
350{
351
352 Standard_Real param,FMEPS = From - Tol;
353 Standard_Integer i,ifirst,nbpoints = Spt.Length();
354 for (ifirst=1; ifirst<=nbpoints; ifirst++) {
355 if (Spt(ifirst).Parameter() >= FMEPS) {
356 break;
357 }
358 }
359 Standard_Boolean RetVal = Standard_False;
360 if (ifirst <= nbpoints) {
361 i = ifirst;
362 IndFrom = ifirst;
363 Standard_Boolean found = Standard_False;
364 while (!found) {
365 Or = Spt(i).Orientation();
366 param = Spt(i).Parameter();
367 i = i+1;
368 while (i<=nbpoints) {
369 if (Spt(i).Parameter()-param <= Tol) {
370 if (Or != TopAbs_EXTERNAL && Or != Spt(i).Orientation()) {
371 Or = TopAbs_EXTERNAL;
372 }
373 i++;
374 }
375 else {
376 break;
377 }
378 }
379 if (Or == TopAbs_EXTERNAL) {
380 found = (i > nbpoints);
381 IndFrom = i;
382 }
383 else { // on a une intersection franche
384 IndTo = i-1;
385 found = Standard_True;
386 RetVal = Standard_True;
387 }
388 }
389 }
390
391 return RetVal;
392}
393
394//=======================================================================
395//function : LocBefore
396//purpose :
397//=======================================================================
398
399static Standard_Boolean LocBefore (const LocOpe_SequenceOfPntFace& Spt,
400 const Standard_Real From,
401 const Standard_Real Tol,
402 TopAbs_Orientation& Or,
403 Standard_Integer& IndFrom,
404 Standard_Integer& IndTo)
405{
406 Standard_Real param,FPEPS = From + Tol;
407 Standard_Integer i,ifirst,nbpoints = Spt.Length();
408 for (ifirst=nbpoints; ifirst>=1; ifirst--) {
409 if (Spt(ifirst).Parameter() <= FPEPS) {
410 break;
411 }
412 }
413 Standard_Boolean RetVal = Standard_False;
414 if (ifirst >= 1) {
415 i = ifirst;
416 IndTo = ifirst;
417 Standard_Boolean found = Standard_False;
418 while (!found) {
419 Or = Spt(i).Orientation();
420 param = Spt(i).Parameter();
421 i = i-1;
422 while (i>=1) {
423 if (param - Spt(i).Parameter() <= Tol) {
424 if (Or != TopAbs_EXTERNAL && Or != Spt(i).Orientation()) {
425 Or = TopAbs_EXTERNAL;
426 }
427 i--;
428 }
429 else {
430 break;
431 }
432 }
433 if (Or == TopAbs_EXTERNAL) {
434 found = (i < 1);
435 IndTo = i;
436 }
437 else { // on a une intersection franche
438 IndFrom = i+1;
439 found = Standard_True;
440 RetVal = Standard_True;
441 }
442 }
443 }
444
445 return RetVal;
446}
447
448//=======================================================================
449//function : LocAfter
450//purpose :
451//=======================================================================
452
453static Standard_Boolean LocAfter (const LocOpe_SequenceOfPntFace& Spt,
454 const Standard_Integer FromInd,
455 const Standard_Real Tol,
456 TopAbs_Orientation& Or,
457 Standard_Integer& IndFrom,
458 Standard_Integer& IndTo)
459{
460 Standard_Integer nbpoints = Spt.Length();
461 if (FromInd >= nbpoints) {
462 return Standard_False;
463 }
464
465 Standard_Real param,FMEPS;
466 Standard_Integer i,ifirst;
467 if (FromInd >= 1) {
468 FMEPS = Spt(FromInd).Parameter() - Tol;
469 for (ifirst=FromInd+1; ifirst<=nbpoints; ifirst++) {
470 if (Spt(ifirst).Parameter() >= FMEPS) {
471 break;
472 }
473 }
474 }
475 else {
476 ifirst = 1;
477 }
478
479 Standard_Boolean RetVal = Standard_False;
480 if (ifirst <= nbpoints) {
481 i = ifirst;
482 IndFrom = ifirst;
483 Standard_Boolean found = Standard_False;
484 while (!found) {
485 Or = Spt(i).Orientation();
486 param = Spt(i).Parameter();
487 i = i+1;
488 while (i<=nbpoints) {
489 if (Spt(i).Parameter()-param <= Tol) {
490 if (Or != TopAbs_EXTERNAL && Or != Spt(i).Orientation()) {
491 Or = TopAbs_EXTERNAL;
492 }
493 i++;
494 }
495 else {
496 break;
497 }
498 }
499 if (Or == TopAbs_EXTERNAL) {
500 found = (i > nbpoints);
501 IndFrom = i;
502 }
503 else { // on a une intersection franche
504 IndTo = i-1;
505 found = Standard_True;
506 RetVal = Standard_True;
507 }
508 }
509 }
510 return RetVal;
511}
512
513
514//=======================================================================
515//function : LocBefore
516//purpose :
517//=======================================================================
518
519#ifdef DEB
520static Standard_Boolean LocBefore (const LocOpe_SequenceOfPntFace& Spt,
521 const Standard_Integer FromInd,
522 const Standard_Real Tol,
523 TopAbs_Orientation& Or,
524 Standard_Integer& IndFrom,
525 Standard_Integer& IndTo)
526{
527 Standard_Integer nbpoints = Spt.Length();
528 if (FromInd <= 1) {
529 return Standard_False;
530 }
531
532 Standard_Real param,FPEPS;
533 Standard_Integer i,ifirst;
534 if (FromInd <= nbpoints) {
535 FPEPS = Spt(FromInd).Parameter() + Tol;
536 for (ifirst=FromInd-1; ifirst>=1; ifirst--) {
537 if (Spt(ifirst).Parameter() <= FPEPS) {
538 break;
539 }
540 }
541 }
542 else {
543 ifirst = nbpoints;
544 }
545
546 Standard_Boolean RetVal = Standard_False;
547 if (ifirst >= 1) {
548 i = ifirst;
549 IndTo = ifirst;
550 Standard_Boolean found = Standard_False;
551 while (!found) {
552 Or = Spt(i).Orientation();
553 param = Spt(i).Parameter();
554 i = i-1;
555 while (i>=1) {
556 if (param - Spt(i).Parameter() <= Tol) {
557 if (Or != TopAbs_EXTERNAL && Or != Spt(i).Orientation()) {
558 Or = TopAbs_EXTERNAL;
559 }
560 i--;
561 }
562 else {
563 break;
564 }
565 }
566 if (Or == TopAbs_EXTERNAL) {
567 found = (i < 1);
568 IndTo = i;
569 }
570 else { // on a une intersection franche
571 IndFrom = i+1;
572 found = Standard_True;
573 RetVal = Standard_True;
574 }
575 }
576 }
577 return RetVal;
578}
579#endif
580
581
582//=======================================================================
583//function : AddPoints
584//purpose :
585//=======================================================================
586
587static void AddPoints(IntCurvesFace_Intersector& theInt,
588 LocOpe_SequenceOfPntFace& theSeq,
589 const TopoDS_Face& theface)
590{
591 Standard_Integer nbpoints = theSeq.Length();
592 Standard_Integer newpnt = theInt.NbPnt();
593 Standard_Real param,paramu,paramv;
7fd59977 594 for (Standard_Integer j = 1; j<=newpnt; j++) {
595 const gp_Pnt& thept = theInt.Pnt(j);
596 param = theInt.WParameter(j);
597 paramu = theInt.UParameter(j);
598 paramv = theInt.VParameter(j);
599#ifdef DEB
600 TopAbs_Orientation theor;
601#else
602 TopAbs_Orientation theor=TopAbs_FORWARD;
603#endif
604 switch (theInt.Transition(j)) {
605 case IntCurveSurface_In:
606/* JAG 13.09.96
607 if ( orface == TopAbs_FORWARD) {
608 theor = TopAbs_FORWARD;
609 }
610 else if (orface == TopAbs_REVERSED) {
611 theor = TopAbs_REVERSED;
612 }
613 else {
614 theor = TopAbs_EXTERNAL;
615 }
616*/
617 theor = TopAbs_FORWARD;
618
619 break;
620 case IntCurveSurface_Out:
621/* JAG 13.09.96
622 if ( orface == TopAbs_FORWARD) {
623 theor = TopAbs_REVERSED;
624 }
625 else if (orface == TopAbs_REVERSED) {
626 theor = TopAbs_FORWARD;
627 }
628 else {
629 theor = TopAbs_EXTERNAL;
630 }
631*/
632 theor = TopAbs_REVERSED;
633 break;
634 case IntCurveSurface_Tangent:
635 theor = TopAbs_EXTERNAL;
636 break;
637
638 }
639 LocOpe_PntFace newpt(thept,theface,theor,param,paramu,paramv);
640// for (Standard_Integer k=1; k <= nbpoints; k++) {
641 Standard_Integer k;
642 for ( k=1; k <= nbpoints; k++) {
643 if (theSeq(k).Parameter() - param > 0.) {
644 break;
645 }
646 }
647 if (k <= nbpoints) {
648 theSeq.InsertBefore(k,newpt);
649 }
650 else {
651 theSeq.Append(newpt);
652 }
653 nbpoints++;
654 }
655}