1 // Created on: 2007-07-06
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #include <NIS_View.hxx>
17 #include <NIS_InteractiveContext.hxx>
18 #include <NIS_InteractiveObject.hxx>
20 #include <Visual3d_View.hxx>
21 #include <Bnd_B2f.hxx>
22 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
24 #include <OpenGl_GlCore11.hxx>
26 IMPLEMENT_STANDARD_HANDLE (NIS_View, V3d_View)
27 IMPLEMENT_STANDARD_RTTIEXT (NIS_View, V3d_View)
29 //=======================================================================
30 //function : NIS_View()
31 //purpose : Constructor
32 //=======================================================================
34 NIS_View::NIS_View (const Handle(V3d_Viewer)& theViewer,
35 const Handle(Aspect_Window)& theWindow)
36 : V3d_View (theViewer),
37 myIsTopHilight(Standard_False),
38 myDoHilightSelected(Standard_True)
40 if (!theWindow.IsNull())
41 V3d_View::SetWindow (theWindow, NULL, &MyCallback, this);
44 //=======================================================================
45 //function : SetWindow
47 //=======================================================================
49 void NIS_View::SetWindow(const Handle(Aspect_Window) &theWindow)
51 V3d_View::SetWindow (theWindow, NULL, &MyCallback, this);
54 // //=======================================================================
55 // //function : ~NIS_View
56 // //purpose : Destructor
57 // //=======================================================================
59 // NIS_View::~NIS_View()
63 //=======================================================================
64 //function : AddContext
66 //=======================================================================
68 void NIS_View::AddContext (NIS_InteractiveContext * theCtx)
70 // Check that the given context is not among already attached contexts
71 NCollection_List<NIS_InteractiveContext *>::Iterator anIter (myContexts);
72 for (; anIter.More(); anIter.Next())
73 if (anIter.Value() == theCtx)
75 if (anIter.More() == Standard_False)
76 myContexts.Append (theCtx);
79 //=======================================================================
80 //function : RemoveContext
82 //=======================================================================
84 void NIS_View::RemoveContext (NIS_InteractiveContext * theCtx)
86 NCollection_List<NIS_InteractiveContext *>::Iterator anIter (myContexts);
87 for (; anIter.More(); anIter.Next())
88 if (anIter.Value() == theCtx) {
89 myContexts.Remove (anIter);
93 NCollection_Map<Handle(NIS_Drawer)>::Iterator anIterD (theCtx->GetDrawers ());
94 for (; anIterD.More(); anIterD.Next()) {
95 const Handle(NIS_Drawer)& aDrawer = anIterD.Value();
96 if (aDrawer.IsNull() == Standard_False) {
97 aDrawer->UpdateExListId(this);
102 //=======================================================================
103 //function : FitAll3d
105 //=======================================================================
107 Standard_Boolean NIS_View::FitAll3d (const Quantity_Coefficient theCoef)
109 Bnd_B3f aB3fBox = GetBndBox();
111 if (aB3fBox.IsVoid() || MyView->IsDefined() == Standard_False)
113 return Standard_False;
117 aBox.Add (gp_Pnt (aB3fBox.CornerMin()));
118 aBox.Add (gp_Pnt (aB3fBox.CornerMax()));
120 if (!FitMinMax (myCamera, aBox, theCoef, 0.0, Standard_False))
122 return Standard_False;
129 return Standard_True;
132 //=======================================================================
133 //function : GetBndBox
135 //=======================================================================
137 Bnd_B3f NIS_View::GetBndBox() const
139 // Calculate the 3D bounding box of visible objects
140 // in all interactive contexts
142 NCollection_List<NIS_InteractiveContext *>::Iterator anIterC (myContexts);
143 for (; anIterC.More(); anIterC.Next()) {
144 NCollection_Map<Handle(NIS_Drawer)>::Iterator anIterD
145 (anIterC.Value()->myDrawers);
146 for (; anIterD.More(); anIterD.Next()) {
147 const Handle(NIS_Drawer)& aDrawer = anIterD.Value();
148 Bnd_B3f aBoxD = aDrawer->GetBox (this);
153 // Take the bounding box of AIS objects displayed in the view
154 Bnd_Box aVal = View()->MinMaxValues();
157 aBox.Add (aVal.CornerMin());
158 aBox.Add (aVal.CornerMax());
164 //=======================================================================
165 //function : GetBndBox
167 //=======================================================================
169 void NIS_View::GetBndBox( Standard_Integer& theXMin, Standard_Integer& theXMax,
170 Standard_Integer& theYMin, Standard_Integer& theYMax ) const
172 theXMin = theYMin = 0;
173 theXMax = theYMax = -1;
175 Bnd_B3f aBox = GetBndBox();
177 // Check that the box is not empty
178 if (aBox.IsVoid() == Standard_False) {
179 // Convert the 3D box to 2D representation in pixel coordinates
181 Standard_Integer anXp, anYp;
182 const gp_XYZ aCorner[2] = { aBox.CornerMin(), aBox.CornerMax() };
183 Standard_Integer aLimp[4] = { 1000000, -1000000, 1000000, -1000000 };
184 for (Standard_Integer i = 0; i < 8; i++) {
185 if (i & 0x1) aCoord.SetX (aCorner[0].X());
186 else aCoord.SetX (aCorner[1].X());
187 if (i & 0x2) aCoord.SetY (aCorner[0].Y());
188 else aCoord.SetY (aCorner[1].Y());
189 if (i & 0x4) aCoord.SetZ (aCorner[0].Z());
190 else aCoord.SetZ (aCorner[1].Z());
191 Convert( aCoord.X(), aCoord.Y(), aCoord.Z(), anXp, anYp );
192 if (aLimp[0] > anXp) aLimp[0] = anXp;
193 if (aLimp[1] < anXp) aLimp[1] = anXp;
194 if (aLimp[2] > anYp) aLimp[2] = anYp;
195 if (aLimp[3] < anYp) aLimp[3] = anYp;
197 if (aLimp[0] < aLimp[1] && aLimp[2] < aLimp[3])
200 // WindowFit (aLimp[0], aLimp[2], aLimp[1], aLimp[3]);
210 //=======================================================================
211 //function : MyCallback
213 //=======================================================================
215 int NIS_View::MyCallback (Aspect_Drawable /* Window ID */,
217 Aspect_GraphicCallbackStruct* callData /* call data */)
219 // Avoid multiple rendering of the scene ( accordingly with update of
220 // callback mechanism, that invokes additional callbacks before
221 // underlay and overlay redrawing with OCC_PRE_REDRAW and OCC_PRE_OVERLAY
222 // bits added to the "reason" value of the callback data structure;
223 // see comments to OCC_REDRAW_ADDITIONAL_CALLBACKS definition )
224 if (callData->reason & OCC_REDRAW_ADDITIONAL_CALLBACKS)
227 if (callData->IsCoreProfile)
229 return 0; // not supported
232 const Handle(NIS_View) thisView (static_cast<NIS_View *> (ptrData));
233 NCollection_List<NIS_InteractiveContext *>::Iterator anIter;
235 // Find the bounding box of all displayed objects by summing the boxes stored
236 // in the relevant DrawList instances.
238 for (anIter = thisView->myContexts; anIter.More(); anIter.Next())
239 anIter.Value()->GetBox (aBndBox, pView);
241 if (aBndBox.IsVoid() == Standard_False) {
242 const gp_XYZ aBoxSize = 0.5 * (aBndBox.CornerMax() - aBndBox.CornerMin());
243 const gp_XYZ aBoxCenter = 0.5 * (aBndBox.CornerMax() + aBndBox.CornerMin());
245 // Find the ray passing through the clicked point in the view window.
246 Standard_Real anX, anY, aZ;
247 thisView->Convert(0, 0, anX, anY, aZ); // 3D point for the 3D coordinates
248 const gp_Pnt anEye (anX, anY, aZ);
249 thisView->Proj (anX, anY, aZ); // vector orthogonal to the view plane
250 const gp_Dir aProj (anX, anY, aZ);
251 const gp_Ax1 anAxis (anEye, aProj);
253 const Standard_Real aCenterDist = (anEye.XYZ() - aBoxCenter) * aProj.XYZ();
254 const Standard_Real aBoxExtent /*(fabs(aBoxSize.X() * anX) +
255 fabs(aBoxSize.Y() * anY) +
256 fabs(aBoxSize.Z() * aZ))*/(100.);
258 #define FRONT_CLIPPING_PLANE (GL_CLIP_PLANE0 + 0)
259 #define BACK_CLIPPING_PLANE (GL_CLIP_PLANE0 + 1)
260 Standard_Real arr[4] = {
266 arr[3] = aBoxExtent + 1.;
267 glClipPlane (BACK_CLIPPING_PLANE, arr);
268 glEnable (BACK_CLIPPING_PLANE);
270 arr[3] = aBoxExtent + 1.;
271 glClipPlane (FRONT_CLIPPING_PLANE, arr);
272 glEnable (FRONT_CLIPPING_PLANE);
276 GLboolean isDepthWriteMask, isDepthTest;
277 glGetBooleanv(GL_DEPTH_WRITEMASK,&isDepthWriteMask);
278 glGetBooleanv(GL_DEPTH_TEST,&isDepthTest);
279 // printf ("GlDepthMask=%d; GlDepthTest=%d\n", depthwritemask, depthtest);
280 glDisableClientState(GL_COLOR_ARRAY);
281 glDisableClientState(GL_EDGE_FLAG_ARRAY);
282 glDisableClientState(GL_INDEX_ARRAY);
283 glDisableClientState(GL_NORMAL_ARRAY);
284 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
286 glEnable(GL_DEPTH_TEST);
287 glDepthFunc(GL_LESS);
289 glClear(GL_DEPTH_BUFFER_BIT);
292 TColStd_MapIteratorOfPackedMapOfInteger anIterM(thisView->myExListId);
293 for (; anIterM.More(); anIterM.Next())
294 if (anIterM.Key() != 0) {
296 glDeleteLists (anIterM.Key(), 5);
298 glDeleteLists (anIterM.Key(), 1);
301 thisView->myExListId.Clear();
303 for (anIter = thisView->myContexts; anIter.More(); anIter.Next())
304 anIter.Value()->redraw (thisView, NIS_Drawer::Draw_Normal);
306 // #818151 - selected object is hidden by covered unselected one
307 // display hilighted objects always above the rest ones
308 if (thisView->myIsTopHilight == Standard_True) {
309 glDepthFunc(GL_ALWAYS);
312 for (anIter = thisView->myContexts; anIter.More(); anIter.Next())
313 anIter.Value()->redraw (thisView, NIS_Drawer::Draw_Hilighted);
314 for (anIter = thisView->myContexts; anIter.More(); anIter.Next())
315 anIter.Value()->redraw (thisView, NIS_Drawer::Draw_DynHilighted);
316 for (anIter = thisView->myContexts; anIter.More(); anIter.Next())
317 anIter.Value()->redraw (thisView, NIS_Drawer::Draw_Transparent);
319 // draw top objects always above
320 if (thisView->myIsTopHilight == Standard_False) {
321 glDepthFunc(GL_ALWAYS);
324 for (anIter = thisView->myContexts; anIter.More(); anIter.Next())
325 anIter.Value()->redraw (thisView, NIS_Drawer::Draw_Top);
330 //=======================================================================
331 //function : DynamicHilight
333 //=======================================================================
335 void NIS_View::DynamicHilight (const Standard_Integer theX,
336 const Standard_Integer theY)
339 const Handle(NIS_InteractiveObject) aSelected = Pick (theX, theY);
341 // ASV: if at least one Context returns IsSelectable()==False,
342 // hilight is canceled, this method returns
343 if (aSelected.IsNull() == Standard_False) {
344 if (aSelected->IsSelectable() == Standard_False)
347 if (aSelected != myDynHilighted) {
348 const Handle(NIS_View) aView (this);
349 if (myDynHilighted.IsNull() == Standard_False)
350 if (myDynHilighted->GetDrawer().IsNull() == Standard_False)
351 myDynHilighted->GetDrawer()->SetDynamicHilighted(Standard_False,
352 myDynHilighted, aView);
354 // 30.07.10 - NKV - synchronize behaviour with AIS interactive context (if need)
355 if (aSelected.IsNull() ||
356 (myDoHilightSelected == Standard_False &&
357 aSelected->GetDrawer()->GetContext()->IsSelected(aSelected)))
359 myDynHilighted.Nullify();
362 aSelected->GetDrawer()->SetDynamicHilighted (Standard_True,
364 myDynHilighted = aSelected;
370 //=======================================================================
371 //function : DynamicUnhilight
373 //=======================================================================
375 void NIS_View::DynamicUnhilight(const Handle(NIS_InteractiveObject)& theObj)
377 if (theObj == myDynHilighted && theObj.IsNull() == Standard_False) {
378 const Handle(NIS_View) aView (this);
379 if (myDynHilighted->GetDrawer().IsNull() == Standard_False)
380 myDynHilighted->GetDrawer()->SetDynamicHilighted (Standard_False,
381 myDynHilighted, aView);
382 myDynHilighted.Nullify();
387 //=======================================================================
389 //purpose : selection by single click
390 //=======================================================================
392 void NIS_View::Select (const Standard_Integer theX,
393 const Standard_Integer theY,
394 const Standard_Boolean isForceMultiple,
395 const Standard_Boolean theRedraw)
398 const Handle(NIS_InteractiveObject) aSelected = Pick (theX, theY);
399 NCollection_List<NIS_InteractiveContext *>::Iterator anIter (myContexts);
400 for (; anIter.More(); anIter.Next())
401 anIter.Value()->ProcessSelection (aSelected, isForceMultiple);
402 if (aSelected == myDynHilighted && aSelected.IsNull() == Standard_False)
404 myDynHilighted.Nullify();
405 const Handle(NIS_Drawer)& aDrawer = aSelected->GetDrawer();
406 aDrawer->SetDynamicHilighted (Standard_False, aSelected, this);
408 if (theRedraw) Redraw();
411 //=======================================================================
413 //purpose : selection by rectange
414 //=======================================================================
416 void NIS_View::Select (const Standard_Integer theXmin,
417 const Standard_Integer theYmin,
418 const Standard_Integer theXmax,
419 const Standard_Integer theYmax,
420 const Standard_Boolean isForceMult,
421 const Standard_Boolean isFullyIncluded,
422 const Standard_Boolean theRedraw)
425 Standard_Real anX, anY, aZ;
426 if (theXmin == theXmax || theYmin == theYmax)
429 //Transformed box corresponding to the selected rectangle
430 Proj (anX, anY, aZ); // vector orthogonal to the view plane
431 const gp_Dir aProj (anX, anY, aZ);
433 Convert(theXmin, theYmin, anX, anY, aZ); // 3D point for the 3D coordinates
434 const gp_Pnt anEye (anX, anY, aZ);
436 Convert(theXmax, theYmin, anX, anY, aZ); // 3D point for the 3D coordinates
437 const gp_XYZ anXdir (gp_XYZ(anX, anY, aZ) - anEye.XYZ());
438 const gp_Ax3 anAx3 (anEye, aProj, anXdir);
440 aTrf.SetTransformation (anAx3);
441 const gp_Trsf aTrfInv = aTrf.Inverted();
443 Convert(theXmax, theYmax, anX, anY, aZ); // 3D point for the 3D coordinates
444 gp_XYZ anUpperCorner (anX, anY, aZ);
445 aTrf.Transforms(anUpperCorner);
449 aBoxSel.Add (gp_XYZ(0., 0., -10000.));
450 aBoxSel.Add (anUpperCorner);
452 TColStd_PackedMapOfInteger mapSelected;
453 NCollection_List<NIS_InteractiveContext *>::Iterator anIterC (myContexts);
454 for (; anIterC.More(); anIterC.Next()) {
455 NIS_InteractiveContext * pCtx = anIterC.Value();
457 pCtx->selectObjects (mapSelected, aBoxSel, aTrfInv, aTrf, isFullyIncluded);
458 pCtx->ProcessSelection (mapSelected, isForceMult);
460 if (theRedraw) Redraw();
463 //=======================================================================
465 //purpose : Selection by polygon
466 //=======================================================================
468 void NIS_View::Select (const NCollection_List<gp_XY> &thePolygon,
469 const Standard_Boolean isForceMult,
470 const Standard_Boolean isFullyIncluded,
471 const Standard_Boolean theRedraw)
474 if (thePolygon.IsEmpty())
477 Standard_Real anX, anY, aZ;
479 //Transformed box corresponding to the selected rectangle
480 Proj (anX, anY, aZ); // vector orthogonal to the view plane
481 const gp_Dir aProj (anX, anY, aZ);
483 const gp_XY &aPf = thePolygon.First();
484 // 3D point for the 3D coordinates
485 Convert((Standard_Integer) aPf.X(), (Standard_Integer) aPf.Y(), anX, anY, aZ);
486 const gp_Pnt anEye (anX, anY, aZ);
488 // 3D point for the 3D coordinates
489 const gp_XY &aPl = thePolygon.Last();
491 Convert((Standard_Integer) aPl.X(), (Standard_Integer) aPl.Y(), anX, anY, aZ);
493 // Compute transformation.
494 const gp_XYZ anXdir (gp_XYZ(anX, anY, aZ) - anEye.XYZ());
495 if (anXdir.Modulus() <= gp::Resolution())
500 const gp_Ax3 anAx3 (anEye, aProj, anXdir);
502 aTrf.SetTransformation (anAx3);
504 // Prepare list of 2d points of selection polygon.
505 NCollection_List<gp_XY> aPoints;
506 NCollection_List<gp_XY>::Iterator anIter(thePolygon);
509 for (; anIter.More(); anIter.Next()) {
510 const gp_XY &aP = anIter.Value();
512 Convert((Standard_Integer) aP.X(), (Standard_Integer) aP.Y(), anX, anY, aZ);
513 gp_XYZ aP3d(anX, anY, aZ);
515 aTrf.Transforms(aP3d);
517 gp_XY aP2d(aP3d.X(), aP3d.Y());
519 aPoints.Append(aP2d);
523 TColStd_PackedMapOfInteger mapSelected;
524 NCollection_List<NIS_InteractiveContext *>::Iterator anIterC(myContexts);
526 for (; anIterC.More(); anIterC.Next()) {
527 NIS_InteractiveContext * pCtx = anIterC.Value();
529 pCtx->selectObjects (mapSelected, aPoints, aPolyBox, aTrf, isFullyIncluded);
530 pCtx->ProcessSelection (mapSelected, isForceMult);
533 if (theRedraw) Redraw();
536 //=======================================================================
539 //=======================================================================
541 Handle(NIS_InteractiveObject) NIS_View::Pick (const Standard_Integer theX,
542 const Standard_Integer theY)
544 // Find the ray passing through the clicked point in the view window.
545 Standard_Real anX, anY, aZ, anOver;
546 Convert(theX, theY, anX, anY, aZ); // 3D point for the 3D coordinates
547 const gp_Pnt anEye (anX, anY, aZ);
548 Proj (anX, anY, aZ); // vector orthogonal to the view plane
549 const gp_Dir aProj (-anX, -anY, -aZ);
550 const gp_Ax1 anAxis (anEye, aProj);
552 Convert (theX+1, theY+1, anX, anY, aZ);
553 anOver = ((gp_XYZ(anX, anY, aZ) - anEye.XYZ()) ^ aProj.XYZ()).Modulus() * 1.5;
555 return Pick(anAxis, anOver, Standard_True);
558 //=======================================================================
561 //=======================================================================
563 Handle(NIS_InteractiveObject) NIS_View::Pick
564 (const gp_Ax1& theAxis,
565 const Standard_Real theOver,
566 const Standard_Boolean isOnlySelectable)
568 typedef NCollection_List<NIS_InteractiveContext::DetectedEnt> LstDetected;
569 Standard_Real aDistance (0.1 * RealLast());
570 Handle(NIS_InteractiveObject) aSelected, aTmpSel;
571 LstDetected aDetected;
573 NCollection_List<NIS_InteractiveContext *>::Iterator anIterC (myContexts);
574 for (; anIterC.More(); anIterC.Next()) {
575 const Standard_Real aDist =
576 anIterC.Value()->selectObject (aTmpSel, aDetected, theAxis, theOver,
578 if (aDist < aDistance) {
584 // simple iterating is enough to create list of detected objects
585 // in the order of increasing distance
587 for (LstDetected::Iterator anIt(aDetected); anIt.More(); anIt.Next())
588 myDetected.Append(anIt.Value().PObj);