summaryrefslogtreecommitdiff
path: root/src/SelectMgr/SelectMgr_ViewerSelector.cxx
blob: b2312c7059d4c05d62fb4307367cea32464a300c (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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
// Copyright:   Matra-Datavision 1995
// File:        SelectMgr_ViewerSelector.cxx
// Created:     Wed Feb 15 13:54:24 1995
// Author:      Roberc Coublanc
//              <rob>
// Modified by  ...
//              ROB JAN/07/98 : Improve Storage of detected entities
//              AGV OCT/23/03 : Optimize the method SortResult() (OCC4201)

#include <SelectMgr_ViewerSelector.ixx>
#include <SelectMgr_CompareResults.hxx>
#include <gp_Pnt2d.hxx>
#include <gp_Pnt.hxx>
#include <gp_Lin.hxx>
#include <Bnd_HArray1OfBox2d.hxx>
#include <Bnd_Array1OfBox2d.hxx>
#include <Precision.hxx>
#include <TColStd_Array1OfInteger.hxx>
#include <TCollection_AsciiString.hxx>
#include <SelectBasics_ListIteratorOfListOfBox2d.hxx>
#include <SelectBasics_SensitiveEntity.hxx>
#include <SelectBasics_EntityOwner.hxx>
#include <SelectBasics_ListOfBox2d.hxx>
#include <SelectMgr_DataMapIteratorOfDataMapOfIntegerSensitive.hxx>
#include <SelectMgr_DataMapIteratorOfDataMapOfSelectionActivation.hxx>
#include <SelectMgr_SortCriterion.hxx>
#include <Select3D_SensitiveEntity.hxx>
#include <SortTools_QuickSortOfInteger.hxx>
#include <OSD_Environment.hxx>

static Standard_Boolean SelectDebugModeOnVS()
{
  static Standard_Integer isDebugMode( -1 );
  if ( isDebugMode < 0 ) {
    isDebugMode = 1;
    OSD_Environment selectdb("SELDEBUGMODE");
    if ( selectdb.Value().IsEmpty() )
      isDebugMode = 0;
  }                       
  return ( isDebugMode != 0 );
}

//==================================================
// Function: Initialize
// Purpose :
//==================================================
SelectMgr_ViewerSelector::SelectMgr_ViewerSelector():
toupdate(Standard_True),
tosort(Standard_True),
preferclosest(Standard_True),
mytolerance(0.),
myCurRank(0),
lastx (Precision::Infinite()),
lasty (Precision::Infinite()),
myUpdateSortPossible( Standard_True )
{
}


//==================================================
// Function: Activate
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::
Activate (const Handle(SelectMgr_Selection)& aSelection,
          const Standard_Boolean AutomaticProj)
{
  tosort = Standard_True;

  if (!myselections.IsBound(aSelection))
  {
    myselections.Bind(aSelection,0);
  } 
  else if (myselections(aSelection)!=0)
  {
    myselections(aSelection)= 0;
  }
  if(AutomaticProj)
    Convert(aSelection);
}


//==================================================
// Function: Deactivate
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::
Deactivate (const Handle(SelectMgr_Selection)& aSel)
{
  if(myselections.IsBound(aSel))
  {myselections(aSel)=1;
  tosort = Standard_True;}
}





//==================================================
// Function: Sleep
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::Sleep()
{ SelectMgr_DataMapIteratorOfDataMapOfSelectionActivation It(myselections);
for (;It.More();It.Next()){
  if(It.Value()==0) myselections(It.Key())= 2;
}
UpdateSort();
}
//=======================================================================
//function : Sleep
//purpose  : 
//=======================================================================

void SelectMgr_ViewerSelector::Sleep(const Handle(SelectMgr_SelectableObject)& SO)
{ 

  for(SO->Init();SO->More();SO->Next()){
    if(myselections.IsBound(SO->CurrentSelection())){
      myselections(SO->CurrentSelection()) = 2;
    }
  }
  UpdateSort();
}


//==================================================
// Function: Awake
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::Awake(const Standard_Boolean AutomaticProj)
{
  SelectMgr_DataMapIteratorOfDataMapOfSelectionActivation It(myselections);
  for (;It.More();It.Next()){
    if(It.Value()==2)
      myselections(It.Key())=0;
    if(AutomaticProj)
      UpdateConversion();
    UpdateSort();
  }
}

void SelectMgr_ViewerSelector::Awake(const Handle(SelectMgr_SelectableObject)& SO,
                                     const Standard_Boolean AutomaticProj)
{
  for(SO->Init();SO->More();SO->Next()){
    if(myselections.IsBound(SO->CurrentSelection())){
      myselections(SO->CurrentSelection()) =0;
      if(AutomaticProj)
        Convert(SO->CurrentSelection());
    }
  }

}
//==================================================
// Function: Clear
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::Clear()
{
  myentities.Clear();
  myselections.Clear();
  toupdate = Standard_True;
  tosort = Standard_True;
  mystored.Clear();
  lastx = Precision::Infinite();
  lasty = Precision::Infinite();

}

//==================================================
// Function: UpdateConversion
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::UpdateConversion()
{
  if( SelectDebugModeOnVS() )
    cout<<"\t\t\t\t\t SelectMgr_VS::UpdateConversion"<<endl;

  SelectMgr_DataMapIteratorOfDataMapOfSelectionActivation It(myselections);
  for(;It.More();It.Next()){
    //Convert only if active...
    if(It.Value()==0)
      Convert(It.Key());
  }
  toupdate = Standard_False;
  tosort = Standard_True;
}


//==================================================
// Function: Convert
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::
Convert (const Handle(SelectMgr_Selection)& /*aSel*/) {tosort=Standard_True;}


//==================================================
// Function: UpdateSort
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::UpdateSort()
{
  if( !myUpdateSortPossible )
    return;

  if( SelectDebugModeOnVS() )
    cout<<"\t\t\t\t\t SelectMgr_ViewerSelector::UpdateSort()"<<endl;
  mystored.Clear();
  myentities.Clear();
  myactivenb = NbBoxes();

  if(myactivenb > 0) {
    Standard_Boolean NoClip = myclip.IsVoid();
    Handle(Bnd_HArray1OfBox2d) refToTab = new Bnd_HArray1OfBox2d(1,myactivenb);
    Bnd_Array1OfBox2d & tab = refToTab->ChangeArray1();
    Standard_Real xmin=Precision::Infinite(),ymin=Precision::Infinite(),xmax=-Precision::Infinite(),ymax=-Precision::Infinite();
    Standard_Real curxmin,curymin,curxmax,curymax;
    //    Standard_Integer boxindex=0,indexsel=0,indexprim=0;
    Standard_Integer boxindex=0;

    SelectMgr_DataMapIteratorOfDataMapOfSelectionActivation It;
    SelectBasics_ListIteratorOfListOfBox2d LIt;
    Handle(SelectMgr_Selection) curEntity;
    Standard_Real ScaleFactor;
    for(It.Initialize(myselections);It.More();It.Next()){
      if(It.Value()== 0)
      { curEntity = It.Key();
      for(curEntity->Init();curEntity->More();curEntity->Next())
      {     
        static SelectBasics_ListOfBox2d BoxList;
        BoxList.Clear();
        curEntity->Sensitive()->Areas(BoxList);
        ScaleFactor = curEntity->Sensitive()->SensitivityFactor();


        for(LIt.Initialize(BoxList);LIt.More();LIt.Next()){
          boxindex++;

          tab.SetValue(boxindex,LIt.Value());

          tab(boxindex).SetGap(mytolerance*ScaleFactor);
          myentities.Bind(boxindex,curEntity->Sensitive());
          if(NoClip){
            if (!tab(boxindex).IsVoid()) {
              tab(boxindex).Get(curxmin,curymin,curxmax,curymax);
              if(curxmin<xmin) xmin=curxmin;
              if(curxmax>xmax) xmax=curxmax;
              if(curymin<ymin) ymin=curymin;
              if(curymax>ymax) ymax=curymax;
            }
          }
        }
      }
      }
    }


    if(NoClip) {myclip.SetVoid();myclip.Update(xmin,ymin,xmax,ymax);}
    myselector.Initialize(myclip, mytolerance,refToTab);
    tosort = Standard_False;
    if(NoClip) myclip.SetVoid();
  }
}


//==================================================
// Function: Remove
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::
Remove(const Handle(SelectMgr_Selection)& aSel)
{
  if (myselections.IsBound(aSel))
  { myselections.UnBind(aSel);
  tosort = Standard_True;
  }
}

//==================================================
// Function: SetSensitivity
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::SetSensitivity(const Standard_Real aVal)
{mytolerance = aVal;
tosort=Standard_True;}

//==================================================
// Function: SetClipping
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::SetClipping(const Standard_Real Xc,
                                           const Standard_Real Yc,
                                           const Standard_Real Height,
                                           const Standard_Real Width)
{
  Bnd_Box2d aClip;
  aClip.Set(gp_Pnt2d(Xc-Width/2, Yc-Height/2));
  aClip.Add(gp_Pnt2d(Xc+Width/2, Yc+Height/2));
  myclip = aClip;
  tosort = Standard_True;
}


//==================================================
// Function: SetClipping
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::SetClipping (const Bnd_Box2d& abox)
{myclip = abox;
tosort = Standard_True;
}

//==================================================
// Function: InitSelect
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::InitSelect(const Standard_Real Xr,
                                          const Standard_Real Yr)
{
  Standard_OutOfRange_Raise_if(Abs(Xr-Precision::Infinite())<=Precision::Confusion() ||
    Abs(Yr-Precision::Infinite())<=Precision::Confusion(),
    " Infinite values in IniSelect");
  mystored.Clear();
  myprim.Clear();
  if (toupdate) UpdateConversion();
  if (tosort) UpdateSort();
  if(myactivenb!=0){
    myselector.InitSelect(Xr,Yr);
    if(myselector.More()) {lastx = Xr;lasty=Yr;}
    LoadResult();
  }
}

//==================================================
// Function: InitSelect
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::InitSelect(const Bnd_Box2d& aBox)
{
  mystored.Clear();
  if(toupdate) UpdateConversion();
  if (tosort) UpdateSort();
  if (myactivenb!=0){
    myselector.InitSelect(aBox);
    LoadResult(aBox);
  }
}

//==================================================
// Function: InitSelect
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::InitSelect(const Standard_Real Xmin,
                                          const Standard_Real Ymin,
                                          const Standard_Real Xmax,
                                          const Standard_Real Ymax)
{
  mystored.Clear();

  if (toupdate) UpdateConversion();
  if (tosort)   UpdateSort();
  if (myactivenb!=0){
    Bnd_Box2d aBox;
    aBox.Update(Xmin,Ymin,Xmax,Ymax);
    myselector.InitSelect(aBox);
    LoadResult(aBox);
  }
}

//==================================================
// Function: InitSelect
// Purpose : Polyline Selection
//==================================================
void SelectMgr_ViewerSelector::InitSelect(const TColgp_Array1OfPnt2d& aPoly)
{
  mystored.Clear();

  if (toupdate) UpdateConversion();
  if (tosort)   UpdateSort();
  if (myactivenb!=0){
    // the Bnd boxes are used for the first time  
    Bnd_Box2d aBox;
    Standard_Integer NbPnt = aPoly.Length();
    Standard_Integer i;
    for(i=1;i<=NbPnt;i++) {
      aBox.Update(aPoly(i).X(),aPoly(i).Y());
    }
    myselector.InitSelect(aBox);
    LoadResult(aPoly);
    //    LoadResult(aBox);
  }
}


//==================================================
// Function: LoadResult
// Purpose : for the moment the size of the primitive 
//           is not taken into account in the search criteriai...
//           The priority, the depth and the min. distance to CDG or Borders is taken...
//==================================================
void SelectMgr_ViewerSelector::
LoadResult()
{
  //  Handle(SelectMgr_EntityOwner)  OWNR;  
  if(myselector.More())
  {
    //      Standard_Boolean Found(Standard_False);
    Standard_Real DMin;
    Standard_Integer nument;
    for(;myselector.More();myselector.Next()){
      nument = myselector.Value();
      const Handle(SelectBasics_SensitiveEntity)& SE = myentities(nument);
      if (SE->Matches(lastx,lasty,mytolerance,DMin)) { 
        const Handle(SelectBasics_EntityOwner)& OWNR = SE->OwnerId();

        if(!OWNR.IsNull()){
          Standard_Real TheDepth = SE->Depth();
          Standard_Integer Prior = OWNR->Priority();

          SelectMgr_SortCriterion SC(Prior,TheDepth,DMin,mytolerance,preferclosest);
          if ( mystored.Contains(OWNR) )
          {
            SelectMgr_SortCriterion& Crit = mystored.ChangeFromKey(OWNR);
            if ( SC > Crit )
            {
              Crit = SC;

              // update previously recorded entity for this owner
              for (int i=1; i <= myprim.Length(); i++)
                if (myentities(myprim(i))->OwnerId() == OWNR) {
                  myprim.SetValue (i, nument);
                  break;
                }
            }
          }
          else
          {
            mystored.Add(OWNR,SC);

            // record entity
            myprim.Append(nument);
          }
        }
      }
    }
    SortResult();
  }
  if( SelectDebugModeOnVS() ){
    cout<<"\tSelectMgr_VS:: Resultat du move"<<endl;
    cout<<"\tNb Detectes :"<<mystored.Extent()<<endl;
    for(Standard_Integer i=1;i<=mystored.Extent();i++){
      const SelectMgr_SortCriterion& Crit = mystored(myIndexes->Value(i));
      cout<<"\t"<<i<<" - Prior"<<Crit.Priority()<<" - prof :"<<Crit.Depth()<<"  - Dist. :"<<Crit.MinDist()<<endl;
    }
  }
}
//==================================================
// Function: LoadResult
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::LoadResult(const Bnd_Box2d& abox)
{
  mystored.Clear();

  //  Handle(SelectMgr_EntityOwner)  OWNR;  
  if(myselector.More())
  { Standard_Real xmin,ymin,xmax,ymax;
  abox.Get(xmin,ymin,xmax,ymax);
  //      Standard_Boolean Found(Standard_False);
  //      Standard_Real DMin=0.;
  Standard_Integer nument;
  for(;myselector.More();myselector.Next()){
    nument = myselector.Value();
    const Handle(SelectBasics_SensitiveEntity)& SE = myentities(nument);
    if (SE->Matches(xmin,ymin,xmax,ymax,0.0)){
      const Handle(SelectBasics_EntityOwner)& OWNR = SE->OwnerId();
      if(!OWNR.IsNull()){
        if(!mystored.Contains(OWNR)){
          SelectMgr_SortCriterion SC(OWNR->Priority(),Precision::Infinite(),
            Precision::Infinite(),mytolerance,preferclosest);
          mystored.Add(OWNR,SC);
          myprim.Append(nument);
        }
      }
    }
  }

  // do not parse in case of selection by elastic rectangle (BUG ANALYST)
  if(mystored.IsEmpty()) return; 
  if(myIndexes.IsNull()) 
    myIndexes = new TColStd_HArray1OfInteger(1,mystored.Extent()); 
  else if(mystored.Extent() !=myIndexes->Length()) 
    myIndexes = new TColStd_HArray1OfInteger (1,mystored.Extent()); 

  // to work faster... 
  TColStd_Array1OfInteger& thearr = myIndexes->ChangeArray1(); 
  for(Standard_Integer I=1;I<=mystored.Extent();I++) 
    thearr(I)=I; 
  } 
}
//==================================================
// Function: LoadResult
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::LoadResult(const TColgp_Array1OfPnt2d& aPoly)
{
  mystored.Clear();
  Bnd_Box2d aBox;
  Standard_Integer NbPnt = aPoly.Length();
  Standard_Integer i;
  for(i=1;i<=NbPnt;i++) {
    aBox.Update(aPoly(i).X(),aPoly(i).Y());
  }
  Standard_Integer NB=0;
  //  Handle(SelectMgr_EntityOwner)  OWNR;  
  if(myselector.More())
  { 
    Standard_Integer nument;

    for(;myselector.More();myselector.Next()){
      NB++;
      nument = myselector.Value();
      const Handle(SelectBasics_SensitiveEntity)& SE = myentities(nument);
      if (SE->Matches(aPoly,aBox,0.0)){
        const Handle(SelectBasics_EntityOwner)& OWNR = SE->OwnerId();
        if(!OWNR.IsNull()){
          if(!mystored.Contains(OWNR)){
            SelectMgr_SortCriterion SC(OWNR->Priority(),Precision::Infinite(),
              Precision::Infinite(),mytolerance,preferclosest);
            mystored.Add(OWNR,SC);
            myprim.Append(nument);
          }
        }
      }
    }

    if(mystored.IsEmpty()) return; 
    if(myIndexes.IsNull()) 
      myIndexes = new TColStd_HArray1OfInteger(1,mystored.Extent()); 
    else if(mystored.Extent() !=myIndexes->Length()) 
      myIndexes = new TColStd_HArray1OfInteger (1,mystored.Extent()); 

    // to work faster... 
    TColStd_Array1OfInteger& thearr = myIndexes->ChangeArray1(); 
    for(Standard_Integer I=1;I<=mystored.Extent();I++) 
      thearr(I)=I; 
  }
}


//==================================================
// Function: HasStored
// Purpose :
//==================================================
Standard_Boolean SelectMgr_ViewerSelector::
HasStored ()
{
  if(Abs(lastx-Precision::Infinite())<=Precision::Confusion()) return Standard_False;
  if(Abs(lasty-Precision::Infinite())<=Precision::Confusion()) return Standard_False;
  InitSelect(lastx,lasty);
  Init();
  return More();
}




//==================================================
// Function: Picked
// Purpose :
//==================================================
Handle(SelectMgr_EntityOwner) SelectMgr_ViewerSelector
::Picked() const
{
  Standard_Integer RankInMap = myIndexes->Value(myCurRank);
  const Handle(SelectBasics_EntityOwner)& toto = mystored.FindKey(RankInMap);
  Handle(SelectMgr_EntityOwner) Ownr = *((Handle(SelectMgr_EntityOwner)*) &toto);
  return Ownr;
}



//=======================================================================
//function : More
//purpose  : 
//=======================================================================
Standard_Boolean SelectMgr_ViewerSelector::More() 
{
  if(mystored.Extent()==0) return Standard_False;
  if(myCurRank==0) return Standard_False;
  return myCurRank <= myIndexes->Length();
}

//==================================================
// Function: OnePicked
// Purpose : only the best one is chosen
//           depend on priority and mindist...
//==================================================

Handle(SelectMgr_EntityOwner) SelectMgr_ViewerSelector
::OnePicked()
{

  Init();
  if(More()){
    Standard_Integer RankInMap = myIndexes->Value(1);
    const Handle(SelectBasics_EntityOwner)& toto = mystored.FindKey(RankInMap);
    Handle(SelectMgr_EntityOwner) Ownr = *((Handle(SelectMgr_EntityOwner)*) &toto);
    return Ownr;
  }

  Handle (SelectMgr_EntityOwner) NullObj; //returns a null Handle if there was not successfull pick...
  return NullObj;
}


//=======================================================================
//function : NbPicked
//purpose  : 
//=======================================================================

Standard_Integer SelectMgr_ViewerSelector::NbPicked() const
{
  return mystored.Extent();
}
//=======================================================================
//function : Picked
//purpose  : 
//=======================================================================
Handle(SelectMgr_EntityOwner) SelectMgr_ViewerSelector::Picked(const Standard_Integer aRank) const
{

  Handle(SelectMgr_EntityOwner) Own; 
  if (aRank<1 || aRank>NbPicked())
    return Own;
  Standard_Integer Indx = myIndexes->Value(aRank);


  const Handle(SelectBasics_EntityOwner)& toto = mystored.FindKey(Indx);
  Own = *((Handle(SelectMgr_EntityOwner)*) &toto);
  return Own;
}
//=======================================================================
//function : Primitive
//purpose  : 
//=======================================================================
Handle(SelectBasics_SensitiveEntity) SelectMgr_ViewerSelector::Primitive
(const Standard_Integer /*Index*/) const
{
  return myentities(myprim(myCurRank));
}


//==================================================
// Function: LastPosition
// Purpose :
//==================================================
void SelectMgr_ViewerSelector::LastPosition(Standard_Real& Xlast,
                                            Standard_Real& YLast) const
{   Xlast = lastx;YLast = lasty;} 



//===================================================
//
//       INTERNAL METHODS ....
//
//==================================================




//==================================================
// Function: NbBoxes
// Purpose :
//==================================================
Standard_Integer SelectMgr_ViewerSelector::NbBoxes()
{
  SelectMgr_DataMapIteratorOfDataMapOfSelectionActivation It(myselections);
  //  Standard_Integer Nbb=0, first,last;
  Standard_Integer Nbb=0;

  for(;It.More();It.Next()){
    if(It.Value()==0){
      for(It.Key()->Init();It.Key()->More();It.Key()->Next())
      {Nbb+= It.Key()->Sensitive()->MaxBoxes();}
    }
  }
  return Nbb;
}




//==================================================
// Function: Contains
// Purpose : 
//==================================================
Standard_Boolean SelectMgr_ViewerSelector::
Contains(const Handle(SelectMgr_SelectableObject)& anObject) const
{
  for (anObject->Init();anObject->More();anObject->Next()){
    if(myselections.IsBound(anObject->CurrentSelection()))
      return Standard_True;
  }
  return Standard_False;
}



//==================================================
// Function: ActiveModes
// Purpose : return all the  modes with a given state for an object
//==================================================


Standard_Boolean SelectMgr_ViewerSelector::
Modes(const Handle(SelectMgr_SelectableObject)& SO,
      TColStd_ListOfInteger& TheActiveList,
      const SelectMgr_StateOfSelection WantedState) const 
{
  Standard_Boolean Found= Standard_False;
  for(SO->Init();SO->More();SO->Next()){
    if(myselections.IsBound(SO->CurrentSelection())){
      if(WantedState==SelectMgr_SOS_Any)
        TheActiveList.Append(SO->CurrentSelection()->Mode());
      else if( myselections(SO->CurrentSelection())==WantedState) 
        TheActiveList.Append(SO->CurrentSelection()->Mode());

      if(!Found) Found=Standard_True;
    }
  }
  return Found;
}


Standard_Boolean SelectMgr_ViewerSelector::
IsActive(const Handle(SelectMgr_SelectableObject)& SO,
         const Standard_Integer aMode) const
{
  for(SO->Init();SO->More();SO->Next()){
    if(aMode==SO->CurrentSelection()->Mode()){
      if(myselections.IsBound(SO->CurrentSelection()) && 
        myselections(SO->CurrentSelection())==SelectMgr_SOS_Activated) 
        return Standard_True;
      else return Standard_False;
    }
  }
  return Standard_False;
}


Standard_Boolean SelectMgr_ViewerSelector::
IsInside(const Handle(SelectMgr_SelectableObject)& SO,
         const Standard_Integer aMode) const
{
  for(SO->Init();SO->More();SO->Next()){
    if(aMode==SO->CurrentSelection()->Mode()){
      if(myselections.IsBound(SO->CurrentSelection())) return Standard_True;
      else return Standard_False;

    }
  }
  return Standard_False;
}


//=======================================================================
//function : Status
//purpose  : 
//=======================================================================

SelectMgr_StateOfSelection SelectMgr_ViewerSelector::Status(const Handle(SelectMgr_Selection)& aSel) const 
{
  if(!myselections.IsBound(aSel)) return SelectMgr_SOS_Unknown;
  //JR/Hp
  Standard_Integer ie = myselections(aSel) ;
  return SelectMgr_StateOfSelection( ie );
  //  return SelectMgr_StateOfSelection(myselections(aSel));

}



//=======================================================================
//function : Dump
//purpose  : 
//=======================================================================

void SelectMgr_ViewerSelector::Dump(Standard_OStream& S) const
{
  S<<"=========================="<<endl;
  S<<" SelectMgr_ViewerSelector "<<endl;
  S<<"=========================="<<endl;
  S<<" "<<endl;
}



//==================================================
// Function: Status
// Purpose : gives Information about selectors
//==================================================

TCollection_AsciiString SelectMgr_ViewerSelector::
Status(const Handle(SelectMgr_SelectableObject)& SO) const
{
  TCollection_AsciiString Status("Status Object :\n\t");
  Standard_Boolean Found= Standard_False;
  for(SO->Init();SO->More();SO->Next()){
    if(myselections.IsBound(SO->CurrentSelection()))
    {
      Found = Standard_True;
      Status = Status + "Mode " + 
        TCollection_AsciiString(SO->CurrentSelection()->Mode()) +
        " present - " ;
      if(myselections(SO->CurrentSelection())) 
        Status = Status + " Active \n\t";
      else
        Status = Status + " Inactive \n\t";
    }
  }

  if(!Found) Status = Status + "Not Present in the selector\n\n";
  return Status;
}


TCollection_AsciiString SelectMgr_ViewerSelector::
Status () const 
{
  // sevsitive primitives present 
  //-----------------------------
  TCollection_AsciiString Status("\t\tSelector Status :\n\t");
  // selections
  //-----------
  Standard_Integer NbActive =0,NbPrim=0;
  Status = Status + "Number of already computed selections : " + 
    TCollection_AsciiString(myselections.Extent());

  SelectMgr_DataMapIteratorOfDataMapOfSelectionActivation It(myselections);
  for(;It.More();It.Next())
  {
    if(It.Value()==0) {NbActive++;
    for(It.Key()->Init();It.Key()->More();It.Key()->Next()){NbPrim++;}
    }
  }
  Status = Status + " - " + TCollection_AsciiString(NbActive) + " activated ones\n\t";
  Status = Status + "Number of active sensitive primitives : " + 
    TCollection_AsciiString(NbPrim)+"\n\t";
  Status = Status + "Real stored Pick Tolerance : " + TCollection_AsciiString(mytolerance) +"\n\t";
  if(toupdate) {
    Status = Status + "\nWARNING : those informations will be obsolete for the next Pick\n"
      +"to get the real status of the selector - make One pick and call Status again\n";
  }
  return Status;
}

//=======================================================================
//function : SortResult
//purpose  :  there is a certain number of entities ranged by criteria 
//            (depth, size, priority, mouse distance from borders or
//            CDG of the detected primitive. Parsing :
//             maximum priorities .
//             then a reasonable compromise between depth and distance...
// finally the ranges are stored in myindexes depending on the parsing.
// so, it is possible to only read 
//=======================================================================
void SelectMgr_ViewerSelector::SortResult()
{
  if(mystored.IsEmpty()) return;

  const Standard_Integer anExtent = mystored.Extent();
  if(myIndexes.IsNull() || anExtent != myIndexes->Length())
    myIndexes = new TColStd_HArray1OfInteger (1, anExtent);

  // to work faster...
  TColStd_Array1OfInteger& thearr = myIndexes->ChangeArray1();

  // indices from 1 to N are loaded
  Standard_Integer I ;
  for (I=1; I <= anExtent; I++)
    thearr(I)=I;

  // OCC4201 (AGV): This loop is inefficient on large arrays, so I replace it
  //                with a standard sort algo
  //  // on trie suivant les criteres  (i) (Owner) (SortCriterion)
  //  Standard_Boolean OKSort;
  //  Standard_Integer temp,indx,indx1;
  //  Standard_Integer tmprim;
  //  // merci lbr...
  //  do{
  //    OKSort =Standard_True;
  //    for(I=1;I<thearr.Length();I++){
  //      indx = thearr(I);
  //      indx1 = thearr(I+1);
  //      if(mystored(indx) < mystored(indx1)){
  //      OKSort = Standard_False;
  //
  //      temp = thearr(I+1);
  //      thearr(I+1) = thearr (I);
  //      thearr(I) = temp;
  //
  //      tmprim = myprim(I+1);
  //      myprim(I+1) = myprim(I);
  //      myprim(I) = tmprim;
  //
  //      }
  //    }
  //  } while (OKSort==Standard_False);
  //
  // OCC4201 (AGV): debut

  SortTools_QuickSortOfInteger::Sort (thearr,
    SelectMgr_CompareResults(mystored));
  TColStd_Array1OfInteger myPrimArr (1, myprim.Length());
  for (I = 1; I <= myPrimArr.Length(); I++)
    myPrimArr (I) = myprim (I);
  for (I = 1; I <= thearr.Length(); I++) {
    const Standard_Integer ind = thearr(I);
    if (ind > 0 && ind <= myPrimArr.Upper())
      myprim (I) = myPrimArr (ind);
  }
  // OCC4201 (AGV): fin
  // it is enough to return owners corresponding to parced indices...

}


//=======================================================================
//function :
//purpose  : 
//=======================================================================
Standard_Boolean SelectMgr_ViewerSelector::IsUpdateSortPossible() const
{
  return myUpdateSortPossible;
}

//=======================================================================
//function :
//purpose  : 
//=======================================================================
void SelectMgr_ViewerSelector::SetUpdateSortPossible( const Standard_Boolean possible )
{
  myUpdateSortPossible = possible;
}