summaryrefslogtreecommitdiff
path: root/src/NIS/NIS_View.cxx
blob: 47be974eb777599a76577204e6c4dad45d0d8a50 (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
// File:      NIS_View.cxx
// Created:   06.07.07 19:51
// Author:    Alexander GRIGORIEV
// Copyright: Open Cascade 2007

#include <NIS_View.hxx>
#include <NIS_InteractiveContext.hxx>
#include <NIS_InteractiveObject.hxx>
#include <gp_Ax1.hxx>
#include <Visual3d_View.hxx>
#include <Bnd_B2f.hxx>
#include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
#ifdef WNT
#include <windows.h>
#endif
#include <GL/gl.h>

IMPLEMENT_STANDARD_HANDLE  (NIS_View, V3d_OrthographicView)
IMPLEMENT_STANDARD_RTTIEXT (NIS_View, V3d_OrthographicView)

//=======================================================================
//function : NIS_View()
//purpose  : Constructor
//=======================================================================

NIS_View::NIS_View (const Handle(V3d_Viewer)&    theViewer,
                    const Handle(Aspect_Window)& theWindow)
  : V3d_OrthographicView (theViewer), 
    myIsTopHilight(Standard_False),
    myDoHilightSelected(Standard_True)
{
  if (!theWindow.IsNull())
    V3d_View::SetWindow (theWindow, NULL, &MyCallback, this);
}

//=======================================================================
//function : SetWindow
//purpose  : 
//=======================================================================

void NIS_View::SetWindow(const Handle(Aspect_Window) &theWindow)
{
  V3d_View::SetWindow (theWindow, NULL, &MyCallback, this);
}

// //=======================================================================
// //function : ~NIS_View
// //purpose  : Destructor
// //=======================================================================

// NIS_View::~NIS_View()
// {}


//=======================================================================
//function : AddContext
//purpose  : 
//=======================================================================

void  NIS_View::AddContext (NIS_InteractiveContext * theCtx)
{
  // Check that the given context is not among already attached contexts
  NCollection_List<NIS_InteractiveContext *>::Iterator anIter (myContexts);
  for (; anIter.More(); anIter.Next())
    if (anIter.Value() == theCtx)
      break;
  if (anIter.More() == Standard_False)
    myContexts.Append (theCtx);
}

//=======================================================================
//function : RemoveContext
//purpose  : 
//=======================================================================

void NIS_View::RemoveContext (NIS_InteractiveContext * theCtx)
{
  NCollection_List<NIS_InteractiveContext *>::Iterator anIter (myContexts);
  for (; anIter.More(); anIter.Next())
    if (anIter.Value() == theCtx) {
      myContexts.Remove (anIter);
      break;
    }

  NCollection_Map<Handle_NIS_Drawer>::Iterator anIterD (theCtx->GetDrawers ());
  for (; anIterD.More(); anIterD.Next()) {
    const Handle(NIS_Drawer)& aDrawer = anIterD.Value();
    if (aDrawer.IsNull() == Standard_False) {
      aDrawer->UpdateExListId(this);
    }
  }
}

//=======================================================================
//function : FitAll3d
//purpose  : 
//=======================================================================

Standard_Boolean NIS_View::FitAll3d (const Quantity_Coefficient theCoef)
{
  Standard_Boolean aResult(Standard_False);
  /*
  Standard_Integer aLimp[4] = { 1000000, -1000000, 1000000, -1000000 };
  GetBndBox( aLimp[0], aLimp[1], aLimp[2], aLimp[3] );
  if (aLimp[1] > -1000000 && aLimp[3] > -1000000 &&
      aLimp[0] < aLimp[1] && aLimp[2] < aLimp[3])
  {
    // Scale the view
    WindowFit (aLimp[0], aLimp[2], aLimp[1], aLimp[3]);
    aResult = Standard_True;
  }
  */

  Bnd_B3f aBox = GetBndBox();

  // Check that the box is not empty
  if (aBox.IsVoid() == Standard_False && MyView->IsDefined() == Standard_True) {
    // Convert the 3D box to 2D representation in view coordinates
    Standard_Real Umin=0.0,Umax=0.0,Vmin=0.0,Vmax=0.0,U,V,W;
    gp_XYZ aCoord;

    const gp_XYZ aCorner[2] = { aBox.CornerMin(), aBox.CornerMax() };

    Standard_Boolean doFit = Standard_True;
    while (doFit) {

    for (Standard_Integer i = 0; i < 8; i++) {
      if (i & 0x1) aCoord.SetX (aCorner[0].X());
      else         aCoord.SetX (aCorner[1].X());
      if (i & 0x2) aCoord.SetY (aCorner[0].Y());
      else         aCoord.SetY (aCorner[1].Y());
      if (i & 0x4) aCoord.SetZ (aCorner[0].Z());
      else         aCoord.SetZ (aCorner[1].Z());

      MyView->Projects(aCoord.X(), aCoord.Y(), aCoord.Z(), U, V, W);
      if (i) {
        Umin = Min(Umin, U); Umax = Max(Umax, U);
        Vmin = Min(Vmin, V); Vmax = Max(Vmax, V);
      }
      else {
        Umin = Umax = U;
        Vmin = Vmax = V;
      }
    }

    if ( (Umax > Umin) && (Vmax > Vmin) ) {
      Standard_Real OldUmin,OldUmax,OldVmin,OldVmax;
      MyViewMapping.WindowLimit(OldUmin, OldVmin, OldUmax, OldVmax);
      Standard_Real DxvOld = Abs(OldUmax - OldUmin);

      // make a margin
      Standard_Real Xrp, Yrp, DxvNew, DyvNew;

      DxvNew = Abs(Umax - Umin); DyvNew = Abs(Vmax - Vmin);
      DxvNew *= (1. + theCoef);
      DyvNew *= (1. + theCoef);

      Standard_Real aRatio = DxvNew / DxvOld;

      Xrp = (Umin + Umax)/2. ; Yrp = (Vmin + Vmax)/2. ;
      Umin = Xrp - DxvNew/2. ; Umax = Xrp + DxvNew/2. ;
      Vmin = Yrp - DyvNew/2. ; Vmax = Yrp + DyvNew/2. ;

      // fit view
      FitAll(Umin, Vmin, Umax, Vmax);

      // ratio 1e+6 often gives calculation error(s), reduce it
      // if (aRatio < 1e+6) doFit = Standard_False;
      if (aRatio < 100) doFit = Standard_False;
      aResult = Standard_True;
    }
    else doFit = Standard_False;

    }
  }

  return aResult;
}

//=======================================================================
//function : GetBndBox
//purpose  : 
//=======================================================================

Bnd_B3f NIS_View::GetBndBox() const
{
  // Calculate the 3D bounding box of visible objects
  // in all interactive contexts
  Bnd_B3f aBox;
  NCollection_List<NIS_InteractiveContext *>::Iterator anIterC (myContexts);
  for (; anIterC.More(); anIterC.Next()) {
    NCollection_Map<Handle_NIS_Drawer>::Iterator anIterD
      (anIterC.Value()->myDrawers);
    for (; anIterD.More(); anIterD.Next()) {
      const Handle(NIS_Drawer)& aDrawer = anIterD.Value();
      Bnd_B3f aBoxD = aDrawer->GetBox (this);
      aBox.Add (aBoxD);
    }
  }

  // Take the bounding box of AIS objects displayed in the view
  Standard_Real aVal[6];
  View()->MinMaxValues(aVal[0], aVal[1], aVal[2], aVal[3], aVal[4], aVal[5]);
  if (aVal[3] < 0.5 * RealLast()) {
    aBox.Add (gp_XYZ (aVal[0], aVal[1], aVal[2]));
    aBox.Add (gp_XYZ (aVal[3], aVal[4], aVal[5]));
  }

  return aBox;
}

//=======================================================================
//function : GetBndBox
//purpose  : 
//=======================================================================

void NIS_View::GetBndBox( Standard_Integer& theXMin, Standard_Integer& theXMax, 
                          Standard_Integer& theYMin, Standard_Integer& theYMax ) const
{
  theXMin = theYMin = 0; 
  theXMax = theYMax = -1;

  Bnd_B3f aBox = GetBndBox();

  // Check that the box is not empty
  if (aBox.IsVoid() == Standard_False) {
    // Convert the 3D box to 2D representation in pixel coordinates
    gp_XYZ aCoord;
    Standard_Integer anXp, anYp;
    const gp_XYZ aCorner[2] = { aBox.CornerMin(), aBox.CornerMax() };
    Standard_Integer aLimp[4] = { 1000000, -1000000, 1000000, -1000000 };
    for (Standard_Integer i = 0; i < 8; i++) {
      if (i & 0x1) aCoord.SetX (aCorner[0].X());
      else         aCoord.SetX (aCorner[1].X());
      if (i & 0x2) aCoord.SetY (aCorner[0].Y());
      else         aCoord.SetY (aCorner[1].Y());
      if (i & 0x4) aCoord.SetZ (aCorner[0].Z());
      else         aCoord.SetZ (aCorner[1].Z());
      Convert( aCoord.X(), aCoord.Y(), aCoord.Z(), anXp, anYp );
      if (aLimp[0] > anXp) aLimp[0] = anXp;
      if (aLimp[1] < anXp) aLimp[1] = anXp;
      if (aLimp[2] > anYp) aLimp[2] = anYp;
      if (aLimp[3] < anYp) aLimp[3] = anYp;
    }
    if (aLimp[0] < aLimp[1] && aLimp[2] < aLimp[3])
    {
      // Scale the view
      // WindowFit (aLimp[0], aLimp[2], aLimp[1], aLimp[3]);  
      theXMin = aLimp[0];
      theXMax = aLimp[1];
      theYMin = aLimp[2];
      theYMax = aLimp[3];
    } 
  }
}


//=======================================================================
//function : MyCallback
//purpose  : 
//=======================================================================

int NIS_View::MyCallback (Aspect_Drawable                /* Window ID */,
                          void*                          ptrData, 
                          Aspect_GraphicCallbackStruct*  callData /* call data */)
{
  // Avoid multiple rendering of the scene ( accordingly with update of
  // callback mechanism, that invokes additional callbacks before
  // underlay and overlay redrawing with OCC_PRE_REDRAW and OCC_PRE_OVERLAY
  // bits added to the "reason" value of the callback data structure;
  // see comments to OCC_REDRAW_ADDITIONAL_CALLBACKS definition )
  if (callData->reason & OCC_REDRAW_ADDITIONAL_CALLBACKS)
    return 0;
  
  const Handle(NIS_View) thisView (static_cast<NIS_View *> (ptrData));
  NCollection_List<NIS_InteractiveContext *>::Iterator anIter;
#ifdef CLIP
  // Find the bounding box of all displayed objects by summing the boxes stored
  // in the relevant DrawList instances.
  Bnd_B3f aBndBox;
  for (anIter = thisView->myContexts; anIter.More(); anIter.Next())
    anIter.Value()->GetBox (aBndBox, pView);

  if (aBndBox.IsVoid() == Standard_False) {
    const gp_XYZ aBoxSize   = 0.5 * (aBndBox.CornerMax() - aBndBox.CornerMin());
    const gp_XYZ aBoxCenter = 0.5 * (aBndBox.CornerMax() + aBndBox.CornerMin());

    // Find the ray passing through the clicked point in the view window.
    Standard_Real anX, anY, aZ;
    thisView->Convert(0, 0, anX, anY, aZ);  // 3D point for the 3D coordinates
    const gp_Pnt anEye (anX, anY, aZ);
    thisView->Proj (anX, anY, aZ);  // vector orthogonal to the view plane
    const gp_Dir aProj (anX, anY, aZ);
    const gp_Ax1 anAxis (anEye, aProj);

    const Standard_Real aCenterDist = (anEye.XYZ() - aBoxCenter) * aProj.XYZ();
    const Standard_Real aBoxExtent /*(fabs(aBoxSize.X() * anX) +
                                    fabs(aBoxSize.Y() * anY) +
                                    fabs(aBoxSize.Z() * aZ))*/(100.);

#define FRONT_CLIPPING_PLANE (GL_CLIP_PLANE0 + 0)
#define BACK_CLIPPING_PLANE  (GL_CLIP_PLANE0 + 1)
    Standard_Real arr[4] = {
      0.0,  /* Nx */
      0.0,  /* Ny */
      1.0,  /* Nz */
      0.
    };
    arr[3] = aBoxExtent + 1.;
    glClipPlane (BACK_CLIPPING_PLANE, arr);
    glEnable (BACK_CLIPPING_PLANE);
    arr[2] = -1.0;
    arr[3] = aBoxExtent + 1.;
    glClipPlane (FRONT_CLIPPING_PLANE, arr);
    glEnable (FRONT_CLIPPING_PLANE);
  }
#endif //IS_DISABLED

  GLboolean isDepthWriteMask, isDepthTest;
  glGetBooleanv(GL_DEPTH_WRITEMASK,&isDepthWriteMask);
  glGetBooleanv(GL_DEPTH_TEST,&isDepthTest);
//   printf ("GlDepthMask=%d; GlDepthTest=%d\n", depthwritemask, depthtest);
  glDisableClientState(GL_COLOR_ARRAY);
  glDisableClientState(GL_EDGE_FLAG_ARRAY);
  glDisableClientState(GL_INDEX_ARRAY);
  glDisableClientState(GL_NORMAL_ARRAY);
  glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  if (!isDepthTest) {
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glClearDepth(1.);
    glClear(GL_DEPTH_BUFFER_BIT);
  }

  TColStd_MapIteratorOfPackedMapOfInteger anIterM(thisView->myExListId);
  for (; anIterM.More(); anIterM.Next())
    if (anIterM.Key() != 0) {
#ifdef ARRAY_LISTS
      glDeleteLists (anIterM.Key(), 5);
#else
      glDeleteLists (anIterM.Key(), 1);
    }
#endif
  thisView->myExListId.Clear();

  for (anIter = thisView->myContexts; anIter.More(); anIter.Next())
    anIter.Value()->redraw (thisView, NIS_Drawer::Draw_Normal);

  // #818151 - selected object is hidden by covered unselected one
  // display hilighted objects always above the rest ones
  if (thisView->myIsTopHilight == Standard_True) {
    glDepthFunc(GL_ALWAYS);
  }

  for (anIter = thisView->myContexts; anIter.More(); anIter.Next())
    anIter.Value()->redraw (thisView, NIS_Drawer::Draw_Hilighted);
  for (anIter = thisView->myContexts; anIter.More(); anIter.Next())
    anIter.Value()->redraw (thisView, NIS_Drawer::Draw_DynHilighted);
  for (anIter = thisView->myContexts; anIter.More(); anIter.Next())
    anIter.Value()->redraw (thisView, NIS_Drawer::Draw_Transparent);

  // draw top objects always above
  if (thisView->myIsTopHilight == Standard_False) {
    glDepthFunc(GL_ALWAYS);
  }

  for (anIter = thisView->myContexts; anIter.More(); anIter.Next())
    anIter.Value()->redraw (thisView, NIS_Drawer::Draw_Top);

  return 0;
}

//=======================================================================
//function : DynamicHilight
//purpose  : 
//=======================================================================

void NIS_View::DynamicHilight  (const Standard_Integer theX,
                                const Standard_Integer theY)
{
  myDetected.Clear();
  const Handle(NIS_InteractiveObject) aSelected = Pick (theX, theY);

  // ASV: if at least one Context returns IsSelectable()==False, 
  // hilight is canceled, this method returns
  if (aSelected.IsNull() == Standard_False) {
    if (aSelected->IsSelectable() == Standard_False)
      return;
  }
  if (aSelected != myDynHilighted) {
    const Handle(NIS_View) aView (this);
    if (myDynHilighted.IsNull() == Standard_False)
      if (myDynHilighted->GetDrawer().IsNull() == Standard_False)
        myDynHilighted->GetDrawer()->SetDynamicHilighted(Standard_False,
                                                         myDynHilighted, aView);

    // 30.07.10 - NKV - synchronize behaviour with AIS interactive context (if need)
    if (aSelected.IsNull() ||
        (myDoHilightSelected == Standard_False && 
         aSelected->GetDrawer()->GetContext()->IsSelected(aSelected)))
    {
      myDynHilighted.Nullify();
    }
    else {
      aSelected->GetDrawer()->SetDynamicHilighted (Standard_True,
                                                   aSelected, aView);
      myDynHilighted = aSelected;
    }
    Redraw();
  }
}

//=======================================================================
//function : DynamicUnhilight
//purpose  : 
//=======================================================================

void NIS_View::DynamicUnhilight(const Handle_NIS_InteractiveObject& theObj)
{
  if (theObj == myDynHilighted && theObj.IsNull() == Standard_False) {
    const Handle(NIS_View) aView (this);
    if (myDynHilighted->GetDrawer().IsNull() == Standard_False)
      myDynHilighted->GetDrawer()->SetDynamicHilighted (Standard_False,
                                                        myDynHilighted, aView);
    myDynHilighted.Nullify();
    Redraw();
  }
}

//=======================================================================
//function : Select
//purpose  : selection by single click
//=======================================================================

void NIS_View::Select (const Standard_Integer theX,
                       const Standard_Integer theY,
                       const Standard_Boolean isForceMultiple,
                       const Standard_Boolean theRedraw)
{
  myDetected.Clear();
  const Handle(NIS_InteractiveObject) aSelected = Pick (theX, theY);
  NCollection_List<NIS_InteractiveContext *>::Iterator anIter (myContexts);
  for (; anIter.More(); anIter.Next())
    anIter.Value()->ProcessSelection (aSelected, isForceMultiple);
  if (aSelected == myDynHilighted && aSelected.IsNull() == Standard_False)
  {
    myDynHilighted.Nullify();
    const Handle(NIS_Drawer)& aDrawer = aSelected->GetDrawer();
    aDrawer->SetDynamicHilighted (Standard_False, aSelected, this);
  }
  if (theRedraw) Redraw();
}

//=======================================================================
//function : Select
//purpose  : selection by rectange
//=======================================================================

void NIS_View::Select (const Standard_Integer  theXmin,
                       const Standard_Integer  theYmin,
                       const Standard_Integer  theXmax,
                       const Standard_Integer  theYmax,
                       const Standard_Boolean  isForceMult,
                       const Standard_Boolean  isFullyIncluded,
                       const Standard_Boolean  theRedraw)
{
  myDetected.Clear();
  Standard_Real anX, anY, aZ;
  if (theXmin == theXmax || theYmin == theYmax)
    return;

  //Transformed box corresponding to the selected rectangle
  Proj (anX, anY, aZ);                  // vector orthogonal to the view plane
  const gp_Dir aProj (anX, anY, aZ);

  Convert(theXmin, theYmin, anX, anY, aZ); // 3D point for the 3D coordinates
  const gp_Pnt anEye (anX, anY, aZ);

  Convert(theXmax, theYmin, anX, anY, aZ); // 3D point for the 3D coordinates
  const gp_XYZ anXdir (gp_XYZ(anX, anY, aZ) - anEye.XYZ());
  const gp_Ax3 anAx3 (anEye, aProj, anXdir);
  gp_Trsf aTrf;
  aTrf.SetTransformation (anAx3);
  const gp_Trsf aTrfInv = aTrf.Inverted();

  Convert(theXmax, theYmax, anX, anY, aZ); // 3D point for the 3D coordinates
  gp_XYZ anUpperCorner (anX, anY, aZ);
  aTrf.Transforms(anUpperCorner);

  // Selecting box
  Bnd_B3f aBoxSel;
  aBoxSel.Add (gp_XYZ(0., 0., -10000.));
  aBoxSel.Add (anUpperCorner);

  TColStd_PackedMapOfInteger mapSelected;
  NCollection_List<NIS_InteractiveContext *>::Iterator anIterC (myContexts);
  for (; anIterC.More(); anIterC.Next()) {
    NIS_InteractiveContext * pCtx = anIterC.Value();
    mapSelected.Clear();
    pCtx->selectObjects (mapSelected, aBoxSel, aTrfInv, aTrf, isFullyIncluded);
    pCtx->ProcessSelection (mapSelected, isForceMult);
  }
  if (theRedraw) Redraw();
}

//=======================================================================
//function : Select
//purpose  : Selection by polygon
//=======================================================================

void  NIS_View::Select (const NCollection_List<gp_XY> &thePolygon,
                        const Standard_Boolean         isForceMult,
                        const Standard_Boolean         isFullyIncluded,
                        const Standard_Boolean         theRedraw)
{
  myDetected.Clear();
  if (thePolygon.IsEmpty())
    return;

  Standard_Real anX, anY, aZ;

  //Transformed box corresponding to the selected rectangle
  Proj (anX, anY, aZ);                  // vector orthogonal to the view plane
  const gp_Dir aProj (anX, anY, aZ);

  const gp_XY &aPf = thePolygon.First();
  // 3D point for the 3D coordinates
  Convert((Standard_Integer) aPf.X(), (Standard_Integer) aPf.Y(), anX, anY, aZ);
  const gp_Pnt anEye (anX, anY, aZ);

  // 3D point for the 3D coordinates
  const gp_XY &aPl = thePolygon.Last();

  Convert((Standard_Integer) aPl.X(), (Standard_Integer) aPl.Y(), anX, anY, aZ);

  // Compute transformation.
  const gp_XYZ anXdir (gp_XYZ(anX, anY, aZ) - anEye.XYZ());
  const gp_Ax3 anAx3 (anEye, aProj, anXdir);
  gp_Trsf aTrf;
  aTrf.SetTransformation (anAx3);

  // Prepare list of 2d points of selection polygon.
  NCollection_List<gp_XY>           aPoints;
  NCollection_List<gp_XY>::Iterator anIter(thePolygon);
  Bnd_B2f                           aPolyBox;

  for (; anIter.More(); anIter.Next()) {
    const gp_XY &aP = anIter.Value();

    Convert((Standard_Integer) aP.X(), (Standard_Integer) aP.Y(), anX, anY, aZ);
    gp_XYZ aP3d(anX, anY, aZ);

    aTrf.Transforms(aP3d);

    gp_XY aP2d(aP3d.X(), aP3d.Y());

    aPoints.Append(aP2d);
    aPolyBox.Add(aP2d);
  }

  TColStd_PackedMapOfInteger                           mapSelected;
  NCollection_List<NIS_InteractiveContext *>::Iterator anIterC(myContexts);

  for (; anIterC.More(); anIterC.Next()) {
    NIS_InteractiveContext * pCtx = anIterC.Value();
    mapSelected.Clear();
    pCtx->selectObjects (mapSelected, aPoints, aPolyBox, aTrf, isFullyIncluded);
    pCtx->ProcessSelection (mapSelected, isForceMult);
  }

  if (theRedraw) Redraw();
}

//=======================================================================
//function : Pick
//purpose  : 
//=======================================================================

Handle_NIS_InteractiveObject NIS_View::Pick (const Standard_Integer theX,
                                             const Standard_Integer theY)
{
  // Find the ray passing through the clicked point in the view window.
  Standard_Real anX, anY, aZ, anOver;
  Convert(theX, theY, anX, anY, aZ);  // 3D point for the 3D coordinates
  const gp_Pnt anEye (anX, anY, aZ);
  Proj (anX, anY, aZ);                // vector orthogonal to the view plane
  const gp_Dir aProj (-anX, -anY, -aZ);
  const gp_Ax1 anAxis (anEye, aProj);

  Convert (theX+1, theY+1, anX, anY, aZ);
  anOver = ((gp_XYZ(anX, anY, aZ) - anEye.XYZ()) ^ aProj.XYZ()).Modulus() * 1.5;

  return Pick(anAxis, anOver, Standard_True);
}

//=======================================================================
//function : Pick
//purpose  : 
//=======================================================================

Handle_NIS_InteractiveObject NIS_View::Pick
                                (const gp_Ax1&          theAxis,
                                 const Standard_Real    theOver,
                                 const Standard_Boolean isOnlySelectable)
{
  typedef NCollection_List<NIS_InteractiveContext::DetectedEnt> LstDetected;
  Standard_Real                 aDistance (0.1 * RealLast());
  Handle(NIS_InteractiveObject) aSelected, aTmpSel;
  LstDetected aDetected;

  NCollection_List<NIS_InteractiveContext *>::Iterator anIterC (myContexts);
  for (; anIterC.More(); anIterC.Next()) {
    const Standard_Real aDist =
      anIterC.Value()->selectObject (aTmpSel, aDetected, theAxis, theOver,
                                     isOnlySelectable);
    if (aDist < aDistance) {
      aDistance = aDist;
      aSelected = aTmpSel;
    }
  }

  // simple iterating is enough to create list of detected objects
  // in the order of increasing distance
  myDetected.Clear();
  for (LstDetected::Iterator anIt(aDetected); anIt.More(); anIt.Next())
    myDetected.Append(anIt.Value().PObj);

  return aSelected;
}