summaryrefslogtreecommitdiff
path: root/src/Bnd/Bnd_BoundSortBox.cxx
blob: 9b83f840e361a707344edb2134d4213fa6272281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
// File:	Bnd_BoundSortBox.cxx
// Created:	Tue Nov 24 16:06:09 1992
// Author:	Didier PIFFAULT
//		<dpf@phylox>

#include <Bnd_BoundSortBox.ixx>
#include <Standard_NullValue.hxx>
#include <Standard_MultiplyDefined.hxx>
#include <Bnd_Array1OfBox.hxx>
#include <TColStd_ListIteratorOfListOfInteger.hxx>
#include <TColStd_DataMapIteratorOfDataMapOfIntegerInteger.hxx>

//#if defined(WNT) || defined(LIN)
# include <stdio.h>
//#endif  // WNT

//-- ================================================================================
//--  lbr le 27 fev 97
//--  
//-- 
//--      Initialisation:  Une boite englobante BE   
//--                       Une liste de boites Bi (Bi dans BE)
//-- 
//--      Compare(b) renvoie la liste des Boites Bi touchees par b
//-- 
//--      
//--   Principe General: 
//-- 
//--       1) On discretise la boite BE en N*N*N  voxels
//--          Chaque boite Bi touche un certain nombre de voxels
//--          Bi touche { Vijk  avec i0<=i<=i1 ...  k0<=k<=k1 } 
//--       2) On projete sur chaque axe X,Y,Z les boites Bi
//--          pour obtenir les structures suivantes : 
//-- 
//--          Exemple :  
//--
//--             la boite i1 touche les voxels { Vijk  avec 1<=i<=N-1  ... }
//--             la boite i2 touche les voxels { Vijk  avec 2<=i<=3  ... }
//--             la boite i3 touche les voxels { Vijk  avec 1<=i<=2  ... }
//--
//-- 
//--         X[.] |  1    2    3    4   ....   N-1   N     
//--         -----|-------------------------------------
//--              |  i3   i1   i1   i1          i1
//--              |       i2   i2
//--              |       i3 
//-- 
//-- 
//--         On realise la meme chose pour les axes Y et Z 
//--         
//--         On obtient donc 3 tableaux (X,Y,Z) de listes d entiers (les indices des boites)
//-- 
//--       3) Pour rechercher les boites en contact avec une boite bt
//--             a) On cherche les voxels touches par bt -> i0bt,i1bt ... k0bt,k1bt
//--             b) On cherche dans la liste Z les boites presentes dans les cases Z[k0bt ... k1bt]
//--             c) On cherche parmi ces boites celles presentes dans les cases Y[j0bt ... j1bt]
//--             d) et le resultat est l intersection du result. precedent avec X[i0bt ... i1bt]
//-- 
//--
//--  Rejection de plus haut niveau. 
//-- 
//--       *) On garde une representation a l aide d un tableau de bit des voxels de l espace BE 
//--          qui contiennenrt au moins une boite Bi.
//--       *) Lorsque on teste une boite bt, on regarde tout d abord si cette boite comprend dans
//--          le tableau de bit au moins un voxel occuppe. 
//--          Si un voxel occuppe est touche : pas de rejection
//--          Sinon return
//--
//--      **) Une autre rejection a ete adoptee. Elle consiste a ne pas checher a placer dans les
//--          structures ci-dessus (tableaux X,Y,Z et tableau de Bits) une boite Bi qui est grande
//--          devant la boite englobante BE. 
//--
//--          Les indices de ces boites sont placees dans un tableau ToTest, et on comparera
//--          systematiquement ces boites avec bt. 
//--         
//--         
//--         
//--         
//--   Note : des tableaux C remplacent ici avantageusement les HArray1OfListOfInteger et autres      
//--          structures agreables lorsqu il faut ajouter des donnees mais lentes a la lecture.
//--          
//--          Ici, on ajoute les donnees au debut (ds les fcts Initialize et SortBoxes) et ensuite,
//--          on passe beaucoup de temps a parcourir les tableaux. Des structures lentes a l ecriture
//--          mais rapides a la lecture sont donc meilleures.
//--         
//--         
//--         
//--         
//--         




//=======================================================================
#define VERIFICATION 0
#define DEBUG 0
#define DIMAXIS 20

#if DEBUG
static long unsigned   APPELREJECTION=0L;
static long unsigned   REJECTNIV0=0L;
static long unsigned   REJECTNIV1=0L;
static long unsigned   NBCOMPARE=0L;
static long unsigned   NBBOITES=0L;
static long unsigned   NBBOITESATESTER=0L;
#endif
//=======================================================================
static Standard_Integer  ComputeSize(const Standard_Integer n) { 
  if(n>40000) return(128);
  if(n>10000)  return(64);
  if(n>1000)   return(32);
  if(n>100)    return(16);
  return(8);
}
//=======================================================================
static long unsigned _P2[32] = { 1,2,4,8,  16,32,64,128,  256,512,1024,2048,
				 4096,8192,16384,32768,
				 65536,131072,262144,524288,
				 1048576,2097152,4194304,8388608,
				 16777216,33554432,67108864,134217728,
				 268435456,536870912,1073741824,2147483648U};
class BSB_T3Bits { //-- size est une puissance de 2 > 4
public:

  Standard_Integer _DECAL;
  Standard_Integer _DECAL2;
  Standard_Integer _BASE;
  Standard_Integer _BASEM1;
  
  long unsigned ind;
  long unsigned Isize;
  Standard_Integer ssize;
  Standard_Real Xmin,Xmax,Ymin,Ymax,Zmin,Zmax;
  
  long unsigned *p;
  Standard_Integer **axisX;
  Standard_Integer **axisY;
  Standard_Integer **axisZ;
  
  Standard_Integer *ToTest;

public:
  BSB_T3Bits(int size);
  ~BSB_T3Bits();

  //-- Partie HArray1OfListOfInteger

  void AppendAxisX(const Standard_Integer i,const Standard_Integer v);
  void AppendAxisY(const Standard_Integer i,const Standard_Integer v);
  void AppendAxisZ(const Standard_Integer i,const Standard_Integer v);

  void Add(long unsigned t) { int o=t&31;    int k=t>>5;    p[k]|=_P2[o];          }
  int Val(long unsigned t)  { int o=t&31;    int k=t>>5;    return(p[k]&_P2[o]);   }
  void Raz(long unsigned t) { int o=t&31;    int k=t>>5;    p[k]&= ~(_P2[o]);      }
  
  Standard_Integer NbAxisX(const Standard_Integer i) {   return(axisX[0][i]);   }
  Standard_Integer NbAxisY(const Standard_Integer i) {   return(axisY[0][i]);   }
  Standard_Integer NbAxisZ(const Standard_Integer i) {   return(axisZ[0][i]);   }
  
  inline Standard_Integer GrilleInteger(Standard_Integer ix,
					Standard_Integer iy,
					Standard_Integer iz) { 
    Standard_Integer tz = iz<<_DECAL2;
    Standard_Integer ty = iy<<_DECAL;
    Standard_Integer t  = ix;
    t|=ty; t|=tz;
    return(t);
  }
  
  inline void IntegerGrille(Standard_Integer t,
			    Standard_Integer &ix,
			    Standard_Integer &iy,
			    Standard_Integer &iz) { 
    ix = t & _BASEM1; t>>=_DECAL;
    iy = t & _BASEM1; t>>=_DECAL;
    iz = t;
  }
};
//=======================================================================
BSB_T3Bits::~BSB_T3Bits() { 
  if(p) { delete [] p; p=0; } 
#if DEBUG
  printf("\n BASE:%d\n",_BASE);
#endif
  for(Standard_Integer i=0; i<=ssize; i++) { 
    if(axisX[i]) { delete [] axisX[i]; axisX[i]=0; } 
    if(axisY[i]) { delete [] axisY[i]; axisY[i]=0; }
    if(axisZ[i]) { delete [] axisZ[i]; axisZ[i]=0; }
  }
  free(axisX); axisX=0;
  free(axisY); axisY=0;
  free(axisZ); axisZ=0;
  if(ToTest)  { delete [] ToTest; ToTest=0; } 
}
//=======================================================================
BSB_T3Bits::BSB_T3Bits(int size) {
  switch (size) { 
  case 128: {  _DECAL=7;   _DECAL2=14;   _BASE=128;  _BASEM1=127;  break; } 
  case  64: {  _DECAL=6;   _DECAL2=12;   _BASE= 64;  _BASEM1= 63;  break; } 
  case  32: {  _DECAL=5;   _DECAL2=10;   _BASE= 32;  _BASEM1= 31;  break; } 
  case  16: {  _DECAL=4;   _DECAL2= 8;   _BASE= 16;  _BASEM1= 15;  break; } 
  default : {  _DECAL=3;   _DECAL2= 6;   _BASE=  8;  _BASEM1=  7;  break; } 
  }
  Standard_Integer i ;    
  long unsigned nb = (size*size*size)>>5;
  Isize = nb;
  ssize = size;
  p = new long unsigned [nb];
  do { p[--nb]=0; } while(nb);

  axisX = (Standard_Integer **) malloc((size+1)*sizeof(Standard_Integer *));
  axisY = (Standard_Integer **) malloc((size+1)*sizeof(Standard_Integer *));
  axisZ = (Standard_Integer **) malloc((size+1)*sizeof(Standard_Integer *));

  axisX[0]=new Standard_Integer [_BASE+1];
  axisY[0]=new Standard_Integer [_BASE+1];
  axisZ[0]=new Standard_Integer [_BASE+1];

  for( i=0; i<(_BASE+1); i++) {
    axisX[0][i]=0;
    axisY[0][i]=0;
    axisZ[0][i]=0;
  }

  for(i=1; i<=size; i++) {  
    axisX[i] =  new Standard_Integer[DIMAXIS];
    axisY[i] =  new Standard_Integer[DIMAXIS];
    axisZ[i] =  new Standard_Integer[DIMAXIS];
    axisX[i][0]=DIMAXIS;
    axisY[i][0]=DIMAXIS;
    axisZ[i][0]=DIMAXIS;
    axisX[i][1]=axisY[i][1]=axisZ[i][1]=-1;
  }
  ToTest=0;
}
//=======================================================================
void BSB_T3Bits::AppendAxisZ(const Standard_Integer i,
			     const Standard_Integer v) { 
  Standard_Integer n = axisZ[0][i];
  n++;
  if(n<axisZ[i][0]) { axisZ[i][n]=v; }
  else { 
    Standard_Integer s=axisZ[i][0];
    Standard_Integer *nt = new Standard_Integer [s+s];
    nt[0]=s+s;
    for(Standard_Integer j=1;j<s;j++) { 
      nt[j]=axisZ[i][j];
    }
    nt[n]=v;
    delete [] axisZ[i];
    axisZ[i]=nt;
  }
  axisZ[0][i]=n;
}
//=======================================================================
void BSB_T3Bits::AppendAxisY(const Standard_Integer i,
			     const Standard_Integer v) { 
  Standard_Integer n = axisY[0][i];
  n++;
  if(n<axisY[i][0]) { axisY[i][n]=v;   }
  else { 
    Standard_Integer s=axisY[i][0];
    Standard_Integer *nt = new Standard_Integer [s+s];
    nt[0]=s+s;
    for(Standard_Integer j=1;j<s;j++) { 
      nt[j]=axisY[i][j];
    }
    nt[n]=v;
    delete [] axisY[i];
    axisY[i]=nt;
  }
  axisY[0][i]=n;
}
//=======================================================================
void BSB_T3Bits::AppendAxisX(const Standard_Integer i,
			     const Standard_Integer v) { 
  Standard_Integer n = axisX[0][i];

  n++;
  if(n<axisX[i][0]) {   axisX[i][n]=v;   }
  else { 
    //-- il faut etendre
    Standard_Integer s=axisX[i][0];
    Standard_Integer *nt = new Standard_Integer [s+s];
    nt[0]=s+s;
    for(Standard_Integer j=1;j<s;j++) { 
      nt[j]=axisX[i][j];
    }
    nt[n]=v;
    delete [] axisX[i];
    axisX[i]=nt;
  }
  axisX[0][i]=n;
}
//=======================================================================
//=======================================================================
//function : Bnd_BoundSortBox
//purpose  : 
//=======================================================================
Bnd_BoundSortBox::Bnd_BoundSortBox()
     : discrX(0), discrY(0), discrZ(0)
{
  TabBits=0;
#if DEBUG
  NBCOMPARE=0L;   NBBOITES=0L;   NBBOITESATESTER=0L;
  APPELREJECTION=0L;  REJECTNIV0=0L;  REJECTNIV1=0L;
#endif
}
//=======================================================================
//function : Initialize
//purpose  : 
//=======================================================================

void Bnd_BoundSortBox::Initialize(const Bnd_Box& CompleteBox,
				  const Handle(Bnd_HArray1OfBox)& SetOfBox)
{
  myBox=CompleteBox;
  myBndComponents=SetOfBox;
  const Bnd_Array1OfBox & taBox=myBndComponents->Array1();
  discrX=discrY=discrZ=ComputeSize(taBox.Upper()-taBox.Lower());
  Standard_Real Xmax, Ymax, Zmax;
  if(CompleteBox.IsVoid()) 
    return;
  CompleteBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
  deltaX = (Xmax-Xmin == 0. ? 0. : discrX/(Xmax-Xmin));
  deltaY = (Ymax-Ymin == 0. ? 0. : discrY/(Ymax-Ymin));
  deltaZ = (Zmax-Zmin == 0. ? 0. : discrZ/(Zmax-Zmin));
  SortBoxes();
}
//=======================================================================
//function : Initialize
//purpose  : 
//=======================================================================
void Bnd_BoundSortBox::Initialize(const Handle(Bnd_HArray1OfBox)& SetOfBox)
{
  myBndComponents=SetOfBox;
  const Bnd_Array1OfBox & taBox=myBndComponents->Array1();
  Standard_Integer i0,i1;
  i0=taBox.Lower();
  i1=taBox.Upper();
  discrX=discrY=discrZ=ComputeSize(i1-i0);
  Standard_Integer labox;
  for (labox=i0; labox<=i1; labox++) {
    if (!taBox(labox).IsVoid()) {
      myBox.Add(taBox(labox));
    }
  }
  Standard_Real Xmax, Ymax, Zmax;
  if(myBox.IsVoid()) 
    return;
  myBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
  deltaX = (Xmax-Xmin == 0. ? 0. : discrX/(Xmax-Xmin));
  deltaY = (Ymax-Ymin == 0. ? 0. : discrY/(Ymax-Ymin));
  deltaZ = (Zmax-Zmin == 0. ? 0. : discrZ/(Zmax-Zmin));
  SortBoxes();
}
//=======================================================================
//function : SortBoxes
//purpose  : 
//=======================================================================
void Bnd_BoundSortBox::SortBoxes() 
{
  Standard_Integer labox;
  Standard_Integer lacaseX, firstcaseX, lastcaseX;
  Standard_Integer lacaseY, firstcaseY, lastcaseY;
  Standard_Integer lacaseZ, firstcaseZ, lastcaseZ;
  Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
  const Bnd_Array1OfBox & taBox=myBndComponents->Array1();
  Standard_Integer i0=taBox.Lower();
  Standard_Integer i1=taBox.Upper();
  BSB_T3Bits* Map=0;
  if(TabBits) { 
    BSB_T3Bits* _Map = (BSB_T3Bits *)TabBits;
    delete _Map;
  }
  Map = new BSB_T3Bits(discrX);
  TabBits = (void *)Map;
  if(Map->ToTest==0) { 
    Standard_Integer s=i1-i0;
    if(s<2) s=2;
    Map->ToTest = new Standard_Integer [s];
    for(Standard_Integer i=0; i<s; i++) { 
      Map->ToTest[i]=i0-1;
    }
  }
  Standard_Real _Xmax,_Xmin,_Ymax,_Ymin,_Zmin,_Zmax;
  myBox.Get(_Xmin,_Ymin,_Zmin,_Xmax,_Ymax,_Zmax);
  Map->Xmax=_Xmax; Map->Ymax=_Ymax; Map->Zmax=_Zmax;
  Map->Xmin=_Xmin; Map->Ymin=_Ymin; Map->Zmin=_Zmin;
  for (labox=i0; labox<=i1; labox++) {
    if (!taBox(labox).IsVoid()) {
      taBox(labox).Get(xmin, ymin, zmin, xmax, ymax, zmax);
      if(xmin>Xmin) firstcaseX=(Standard_Integer )((xmin-Xmin)*deltaX)-1; else  firstcaseX=1;
      if(ymin>Ymin) firstcaseY=(Standard_Integer )((ymin-Ymin)*deltaY)-1; else  firstcaseY=1;
      if(zmin>Zmin) firstcaseZ=(Standard_Integer )((zmin-Zmin)*deltaZ)-1; else  firstcaseZ=1;
      if(xmax<_Xmax) lastcaseX=(Standard_Integer )((xmax-Xmin)*deltaX)+1; else  lastcaseX=discrX;
      if(ymax<_Ymax) lastcaseY=(Standard_Integer )((ymax-Ymin)*deltaY)+1; else  lastcaseY=discrY;
      if(zmax<_Zmax) lastcaseZ=(Standard_Integer )((zmax-Zmin)*deltaZ)+1; else  lastcaseZ=discrZ;
      if(firstcaseX<1) firstcaseX=1; else if(firstcaseX>discrX) firstcaseX=discrX;
      if(firstcaseY<1) firstcaseY=1; else if(firstcaseY>discrY) firstcaseY=discrY;
      if(firstcaseZ<1) firstcaseZ=1; else if(firstcaseZ>discrZ) firstcaseZ=discrZ;

      if(lastcaseX<1) lastcaseX=1; else if(lastcaseX>discrX) lastcaseX=discrX;
      if(lastcaseY<1) lastcaseY=1; else if(lastcaseY>discrY) lastcaseY=discrY;
      if(lastcaseZ<1) lastcaseZ=1; else if(lastcaseZ>discrZ) lastcaseZ=discrZ;

      Standard_Integer n = (lastcaseX-firstcaseX);
      if(n>(lastcaseY-firstcaseY)) n=lastcaseY-firstcaseY;
      if(n>(lastcaseZ-firstcaseZ)) n=lastcaseZ-firstcaseZ;
#if DEBUG
      NBBOITES++;
#endif
      n<<=2;
      if(n>discrX) { 
#if DEBUG
      NBBOITESATESTER++;
#endif
	for(Standard_Integer i=0; i<(i1-i0); i++) { 
	  if(Map->ToTest[i]<i0) { 
	    Map->ToTest[i]=labox;
	    break;
	  }
	}
      }
      else { 
	for (lacaseX=firstcaseX; lacaseX<=lastcaseX; lacaseX++) {
	  Map->AppendAxisX(lacaseX,labox);
	}
	for (lacaseY=firstcaseY; lacaseY<=lastcaseY; lacaseY++) {
	  Map->AppendAxisY(lacaseY,labox);
	}
	for (lacaseZ=firstcaseZ; lacaseZ<=lastcaseZ; lacaseZ++) {
	  Map->AppendAxisZ(lacaseZ,labox);
	}
	//------------------------------------------------------------
	//-- remplissage du tableau de bits
	//--
	if(Map) { 
	  for (lacaseX=firstcaseX; lacaseX<=lastcaseX; lacaseX++) {
	    for (lacaseY=firstcaseY; lacaseY<=lastcaseY; lacaseY++) {
	      for (lacaseZ=firstcaseZ; lacaseZ<=lastcaseZ; lacaseZ++) {
		long unsigned t=Map->GrilleInteger(lacaseX-1,lacaseY-1,lacaseZ-1);
		Map->Add(t);
	      }
	    }
	  }
	}
      }     
    }
  }
}
//=======================================================================
//function : Initialize
//purpose  : 
//=======================================================================
void Bnd_BoundSortBox::Initialize(const Bnd_Box& CompleteBox,
				  const Standard_Integer nbComponents)
{
  Standard_NullValue_Raise_if (nbComponents <=0, "BoundSortBox nul!");
  myBox=CompleteBox;
  myBndComponents=new Bnd_HArray1OfBox(1,nbComponents);

  //***>>> JCD - 04.08.2000 - Array initialization is missing... 
  Bnd_Box emptyBox; 
  myBndComponents->Init( emptyBox ); 
  //***<<< JCD - End  

  discrX=discrY=discrZ=ComputeSize(nbComponents);
  Standard_Real Xmax, Ymax, Zmax;

  if(CompleteBox.IsVoid())
    return;
  CompleteBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
  myBox.Get(Xmin, Ymin, Zmin, Xmax, Ymax, Zmax);
  deltaX = (Xmax-Xmin == 0. ? 0. : discrX/(Xmax-Xmin));
  deltaY = (Ymax-Ymin == 0. ? 0. : discrY/(Ymax-Ymin));
  deltaZ = (Zmax-Zmin == 0. ? 0. : discrZ/(Zmax-Zmin));
  if(TabBits) { 
    BSB_T3Bits* _Map = (BSB_T3Bits *)TabBits;
    delete _Map;
    TabBits=0;
  }
  BSB_T3Bits* Map=0;
  Map = new BSB_T3Bits(discrX);
  TabBits = (void *)Map;
}
//=======================================================================
//function : Add
//purpose  : 
//=======================================================================
void Bnd_BoundSortBox::Add(const Bnd_Box& theBox, 
			   const Standard_Integer boxIndex)
{
  Standard_MultiplyDefined_Raise_if (!(myBndComponents->Value(boxIndex).IsVoid()), " This box is already defined !");
  if (!theBox.IsVoid()) {
    Standard_Integer i0=myBndComponents->Lower();
    Standard_Integer i1=myBndComponents->Upper();
    Standard_Integer theGapX, firstGapX , lastGapX;
    Standard_Integer theGapY, firstGapY , lastGapY;
    Standard_Integer theGapZ, firstGapZ , lastGapZ;
    Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
    myBndComponents->SetValue(boxIndex, theBox);
    theBox.Get(xmin, ymin, zmin, xmax, ymax, zmax);
    BSB_T3Bits* Map = (BSB_T3Bits *)TabBits;
    if(Map->ToTest==0) { 
      Standard_Integer s=i1-i0;
      if(s<2) s=2;
      Map->ToTest = new Standard_Integer [s];
      for(Standard_Integer i=0; i<s; i++) { 
	Map->ToTest[i]=i0-1;
      }
    }
    Standard_Real _Xmax,_Ymax,_Zmax;
    _Xmax=Map->Xmax; _Ymax=Map->Ymax; _Zmax=Map->Zmax;
    if(xmin>Xmin) firstGapX=(Standard_Integer )((xmin-Xmin)*deltaX)-1; else  firstGapX=1;
    if(ymin>Ymin) firstGapY=(Standard_Integer )((ymin-Ymin)*deltaY)-1; else  firstGapY=1;
    if(zmin>Zmin) firstGapZ=(Standard_Integer ) ((zmin-Zmin)*deltaZ)-1; else  firstGapZ=1;
    if(xmax<_Xmax) lastGapX=(Standard_Integer )((xmax-Xmin)*deltaX)+1; else  lastGapX=discrX;
    if(ymax<_Ymax) lastGapY=(Standard_Integer )((ymax-Ymin)*deltaY)+1; else  lastGapY=discrY;
    if(zmax<_Zmax) lastGapZ=(Standard_Integer )((zmax-Zmin)*deltaZ)+1; else  lastGapZ=discrZ;
    if(firstGapX<1) firstGapX=1; else if(firstGapX>discrX) firstGapX=discrX;
    if(firstGapY<1) firstGapY=1; else if(firstGapY>discrY) firstGapY=discrY;
    if(firstGapZ<1) firstGapZ=1; else if(firstGapZ>discrZ) firstGapZ=discrZ;

    if(lastGapX<1) lastGapX=1; else if(lastGapX>discrX) lastGapX=discrX;
    if(lastGapY<1) lastGapY=1; else if(lastGapY>discrY) lastGapY=discrY;
    if(lastGapZ<1) lastGapZ=1; else if(lastGapZ>discrZ) lastGapZ=discrZ;
    Standard_Integer n = (lastGapX-firstGapX);
    if(n>(lastGapY-firstGapY)) n=lastGapY-firstGapY;
    if(n>(lastGapZ-firstGapZ)) n=lastGapZ-firstGapZ;
    n<<=2;
#if DEBUG
      NBBOITES++;
#endif
    if(n>discrX) { 
#if DEBUG
      NBBOITESATESTER++;
#endif
      for(Standard_Integer i=0; i<(i1-i0); i++) { 
	if(Map->ToTest[i]<i0) { 
	  Map->ToTest[i]=boxIndex;
	  break;
	}
      }
    }
    for (theGapY=firstGapY; theGapY<=lastGapY; theGapY++) {
      Map->AppendAxisY(theGapY,boxIndex);
    }
    for (theGapX=firstGapX; theGapX<=lastGapX; theGapX++) {
      Map->AppendAxisX(theGapX,boxIndex);
    }
    for (theGapZ=firstGapZ; theGapZ<=lastGapZ; theGapZ++) {
      Map->AppendAxisZ(theGapZ,boxIndex);
    }      
    //------------------------------------------------------------
    //-- remplissage du tableau de bits
    //--
    if(TabBits) { 
      Map=(BSB_T3Bits *)TabBits; 
      for (theGapX=firstGapX; theGapX<=lastGapX; theGapX++) {
	for (theGapY=firstGapY; theGapY<=lastGapY; theGapY++) {
	  for (theGapZ=firstGapZ; theGapZ<=lastGapZ; theGapZ++) {
	    long unsigned t=Map->GrilleInteger(theGapX-1,theGapY-1,theGapZ-1);
	    Map->Add(t);
	  }
	}
      }
    }
  }
}
//=======================================================================
#if VERIFICATION
static void VerifCompare(const TColStd_ListOfInteger& lastResult,
			 const Bnd_Box& theBox,
			 const Bnd_Array1OfBox&  taBox) { 
  static int Verif = 1;
  Standard_Integer i ;

  if(Verif) { 
    Standard_Integer i0,i1;
    i0=taBox.Lower();
    i1=taBox.Upper();
    char * qwe=new char [i1+1];  //-- $$$$$$$ ATTENTION SI I0 < 0
    for( i=i0; i<=i1; i++) qwe[i]='\0';
    TColStd_ListIteratorOfListOfInteger  theList(lastResult);
    for (; theList.More(); theList.Next()) {
      qwe[theList.Value()]=(char)1;
    }
    Standard_Integer labox;
    for (labox=i0; labox<=i1; labox++) {
      if (!taBox(labox).IsOut(theBox)) {
	qwe[labox]+=2;
      }
    }
    for(i=i0;i<=i1;i++) { 
      if(qwe[i]==2) { 
	printf("\nPb avec boite: %d ",i);
      }
      else if(qwe[i]==1) { 
	printf("\n fausse rejection en %d \n",i);
      }
    }
    delete [] qwe;
  }
}
#endif
//=======================================================================
//function : Compare
//purpose  : 
//=======================================================================
const TColStd_ListOfInteger& Bnd_BoundSortBox::Compare (const Bnd_Box& theBox)

{
 Standard_Integer lacase ;
#if DEBUG
  NBCOMPARE++;
#endif
  lastResult.Clear();
  if (theBox.IsVoid()) return lastResult;
  if (theBox.IsOut(myBox)) { 
#if DEBUG
    REJECTNIV0++;
#endif
    return lastResult;
  }
  const Bnd_Array1OfBox& taBox=myBndComponents->Array1();
  //-- Rejection avec le tableau de bits
  Standard_Boolean touch = Standard_True;
  touch = Standard_False;
  Standard_Real _Xmax,_Ymax,_Zmax;
  BSB_T3Bits* Map = (BSB_T3Bits *)TabBits;
  Standard_Real xmin, ymin, zmin, xmax, ymax, zmax;
  _Xmax=Map->Xmax; _Ymax=Map->Ymax; _Zmax=Map->Zmax;
  theBox.Get(xmin, ymin, zmin, xmax, ymax, zmax);
  Standard_Integer i0,i1,j0,j1,k0,k1;
  if(xmin>Xmin) i0=(Standard_Integer )((xmin-Xmin)*deltaX)-1; else  i0=1;
  if(ymin>Ymin) j0=(Standard_Integer )((ymin-Ymin)*deltaY)-1; else  j0=1;
  if(zmin>Zmin) k0=(Standard_Integer )((zmin-Zmin)*deltaZ)-1; else  k0=1;
  if(xmax<_Xmax) i1=(Standard_Integer )((xmax-Xmin)*deltaX)+1; else  i1=discrX;
  if(ymax<_Ymax) j1=(Standard_Integer )((ymax-Ymin)*deltaY)+1; else  j1=discrY;
  if(zmax<_Zmax) k1=(Standard_Integer )((zmax-Zmin)*deltaZ)+1; else  k1=discrZ;
  if(i0<1) i0=1; else if(i0>discrX) i0=discrX;
  if(j0<1) j0=1; else if(j0>discrY) j0=discrY;
  if(k0<1) k0=1; else if(k0>discrZ) k0=discrZ;

  if(i1<1) i1=1; else if(i1>discrX) i1=discrX;
  if(j1<1) j1=1; else if(j1>discrY) j1=discrY;
  if(k1<1) k1=1; else if(k1>discrZ) k1=discrZ;
  i0--; j0--; k0--; i1--; j1--; k1--;
  for(Standard_Integer i=i0; touch==Standard_False && i<=i1;i++) { 
    for(Standard_Integer j=j0; touch==Standard_False && j<=j1;j++) { 
      for(Standard_Integer k=k0;  touch==Standard_False && k<=k1;k++) {
	long unsigned t=Map->GrilleInteger(i,j,k);
	if(Map->Val(t)) { 
	  touch = Standard_True;
	}
      }
    }
  }
  //-- traitement des boites a tester systematiquement
  if(Map->ToTest) { 
    Standard_Integer l0 = taBox.Lower();
    Standard_Integer l1 = taBox.Upper();
    l1-=l0;
    for(Standard_Integer l=0; Map->ToTest[l]>=l0 && l<(l1-l0); l++) { 
      if(Map->ToTest[l]>=l0) { 
	if (!taBox(Map->ToTest[l]).IsOut(theBox)){
	  lastResult.Append(Map->ToTest[l]);
	}
      }
    }
  }
  if(touch == Standard_False) { 
#if DEBUG
    REJECTNIV1++;
#endif
#if VERIFICATION
    VerifCompare(lastResult,theBox,taBox);
#endif
    return(lastResult);
  }
  //-------------------------
  //-- traitement classique 
  //-------------------------
  i0++; i1++; j0++; j1++; k0++; k1++;
  Crible.Clear();
  theFound=6;
  Standard_Integer cardY=0;
  for (lacase=j0; lacase<=j1; lacase++) {
    Standard_Integer nby=Map->NbAxisY(lacase);
    while(nby>0) {
      cardY++;
      Crible.Bind(Map->axisY[lacase][nby], 4);
      nby--;
    }
  }
  if (cardY==0) {
#if VERIFICATION
    VerifCompare(lastResult,theBox,taBox);
#endif
    return lastResult;
  }
  Standard_Integer cardZ=0;  
  for (lacase=k0; lacase<=k1; lacase++) {
    Standard_Integer nbz=Map->NbAxisZ(lacase);  
    while(nbz>0) { 
      cardZ++;
      if (Crible.IsBound(Map->axisZ[lacase][nbz])) {
	Crible.Bind(Map->axisZ[lacase][nbz], 6);
      }
      nbz--;
    }
  }
  if (cardZ==0) {
#if VERIFICATION 
    VerifCompare(lastResult,theBox,taBox);
#endif
    return lastResult;
  }  
  for (lacase=i0; lacase<=i1; lacase++) {
    Standard_Integer nbx = Map->NbAxisX(lacase); 
    while(nbx>0) { 
      Standard_Integer x=Map->axisX[lacase][nbx];
      if (Crible.IsBound(x)) {
	if (Crible(x)==theFound) {
	  Crible.UnBind(x);
	  if (!taBox(x).IsOut(theBox)){
	    lastResult.Append(x);
	  }
	}
      }
      nbx--;
    }
  }
#if VERIFICATION
  VerifCompare(lastResult,theBox,taBox);
#endif
  return lastResult; 
}
//=======================================================================
//function : Dump
//purpose  : 
//=======================================================================

void Bnd_BoundSortBox::Dump() const
{}
//=======================================================================
//function : Compare
//purpose  : 
//=======================================================================
const TColStd_ListOfInteger& Bnd_BoundSortBox::Compare(const gp_Pln& thePlane)

{
  lastResult.Clear();
  Standard_Integer i;
  const Bnd_Array1OfBox& boxes = myBndComponents->Array1();
  for (i = boxes.Lower(); i <= boxes.Upper(); i++) {
    if (!boxes(i).IsOut(thePlane))
      lastResult.Append(i);
  }
  return lastResult;
}
//=======================================================================
void Bnd_BoundSortBox::Destroy() { 
#if DEBUG
  printf("\nDESTROY NBCOMPARE:%lu  REJECTNIV0:%lu  REJECTIONSOK=%lu  NBBOITES:%lu NBBOITESATESTER:%lu\n",
	 NBCOMPARE,REJECTNIV0,REJECTNIV1,NBBOITES,NBBOITESATESTER);
#endif
  BSB_T3Bits* Map = (BSB_T3Bits *)TabBits;
  if(Map) { 
    delete Map;
    Map=0;
  }
}
//=======================================================================