Salome HOME
Debug and optimization of selection of elements of the sketch.
[modules/shaper.git] / src / XGUI / XGUI_Displayer.cpp
index 6c2f216e38dc4f4b9f61017dab69fbb0c9b61295..202afd6b2c7d6253e1a0075e0ec7f1825162bd37 100644 (file)
@@ -37,7 +37,7 @@
 #include <ModelAPI_Object.h>
 #include <ModelAPI_Tools.h>
 #include <ModelAPI_AttributeIntArray.h>
-#include <ModelAPI_ResultCompSolid.h>
+#include <ModelAPI_ResultBody.h>
 
 #include <ModuleBase_BRepOwner.h>
 #include <ModuleBase_IModule.h>
@@ -50,6 +50,8 @@
 #include <GeomAPI_Shape.h>
 #include <GeomAPI_IPresentable.h>
 #include <GeomAPI_ICustomPrs.h>
+#include <GeomAPI_Pnt.h>
+#include <GeomAPI_IScreenParams.h>
 
 #include <SUIT_ResourceMgr.h>
 
@@ -163,8 +165,13 @@ bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
     GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
     bool isShading = false;
     if (aPrs.get() != NULL) {
+      GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
+      if (aScreen.get()) {
+        aScreen->setScreenPlane(getScreenPlane());
+        aScreen->setViewScale(getViewScale());
+      }
       anAIS = aPrs->getAISObject(anAIS);
-      if (anAIS.get()) {
+      //if (anAIS.get()) {
         // correct deviation coefficient for
         /*Handle(AIS_InteractiveObject) anAISPrs = anAIS->impl<Handle(AIS_InteractiveObject)>();
         if (!anAISPrs.IsNull()) {
@@ -175,7 +182,7 @@ bool XGUI_Displayer::display(ObjectPtr theObject, bool theUpdateViewer)
               //ModuleBase_Tools::setDefaultDeviationCoefficient(aShape, anAISPrs->Attributes());
           }
         }*/
-      }
+      //}
     } else {
       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(theObject);
       if (aResult.get() != NULL) {
@@ -285,7 +292,7 @@ bool XGUI_Displayer::erase(ObjectPtr theObject, const bool theUpdateViewer)
   if (aContext.IsNull())
     return aErased;
 
-  AISObjectPtr anObject = myResult2AISObjectMap[theObject];
+  AISObjectPtr anObject = myResult2AISObjectMap.value(theObject);
   if (anObject) {
     Handle(AIS_InteractiveObject) anAIS = anObject->impl<Handle(AIS_InteractiveObject)>();
     if (!anAIS.IsNull()) {
@@ -324,12 +331,18 @@ bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
 
   GeomPresentablePtr aPrs = std::dynamic_pointer_cast<GeomAPI_IPresentable>(theObject);
   if (aPrs) {
+    GeomScreenParamsPtr aScreen = std::dynamic_pointer_cast<GeomAPI_IScreenParams>(theObject);
+    if (aScreen.get()) {
+      aScreen->setScreenPlane(getScreenPlane());
+      aScreen->setViewScale(getViewScale());
+    }
     AISObjectPtr aAIS_Obj = aPrs->getAISObject(aAISObj);
     if (!aAIS_Obj) {
       aRedisplayed = erase(theObject, theUpdateViewer);
       return aRedisplayed;
     }
     if (aAIS_Obj != aAISObj) {
+      erase(theObject, theUpdateViewer);
       appendResultObject(theObject, aAIS_Obj);
     }
     aAISIO = aAIS_Obj->impl<Handle(AIS_InteractiveObject)>();
@@ -373,7 +386,10 @@ bool XGUI_Displayer::redisplay(ObjectPtr theObject, bool theUpdateViewer)
 #ifdef CLEAR_OUTDATED_SELECTION_BEFORE_REDISPLAY
       myWorkshop->selector()->deselectPresentation(aAISIO);
 #endif
-      aContext->Redisplay(aAISIO, false);
+      if (aContext->IsDisplayed(aAISIO))
+        aContext->Redisplay(aAISIO, false);
+      else
+        aContext->Display(aAISIO, false);
 
       #ifdef TINSPECTOR
       if (getCallBack()) getCallBack()->Redisplay(aAISIO);
@@ -435,12 +451,12 @@ bool XGUI_Displayer::isVisible(XGUI_Displayer* theDisplayer, const ObjectPtr& th
     // compsolid is not visualized in the viewer,
     // but should have presentation when all sub solids are
     // visible. It is useful for highlight presentation where compsolid shape is selectable
-    if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultCompSolid::group()) {
-      ResultCompSolidPtr aCompsolidResult =
-        std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
+    if (!aVisible && aResult.get() && aResult->groupName() == ModelAPI_ResultBody::group()) {
+      ResultBodyPtr aCompsolidResult = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
       if (aCompsolidResult.get() != NULL) { // change colors for all sub-solids
-        bool anAllSubsVisible = aCompsolidResult->numberOfSubs() > 0;
-        for(int i = 0; i < aCompsolidResult->numberOfSubs() && anAllSubsVisible; i++) {
+        int aNumberOfSubs = aCompsolidResult->numberOfSubs();
+        bool anAllSubsVisible = aNumberOfSubs > 0;
+        for(int i = 0; i < aNumberOfSubs && anAllSubsVisible; i++) {
           anAllSubsVisible = theDisplayer->isVisible(aCompsolidResult->subResult(i));
         }
         aVisible = anAllSubsVisible;
@@ -503,7 +519,7 @@ void XGUI_Displayer::setSelected(const  QList<ModuleBase_ViewerPrsPtr>& theValue
       ObjectPtr anObject = aPrs->object();
       ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
       if (aResult.get() && isVisible(aResult)) {
-        AISObjectPtr anObj = myResult2AISObjectMap[aResult];
+        AISObjectPtr anObj = myResult2AISObjectMap.value(aResult);
         Handle(AIS_InteractiveObject) anAIS = anObj->impl<Handle(AIS_InteractiveObject)>();
         if (!anAIS.IsNull()) {
           // The methods are replaced in order to provide multi-selection, e.g. restore selection
@@ -545,8 +561,13 @@ bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
   bool aErased = false;
   Handle(AIS_InteractiveContext) aContext = AISContext();
   if (!aContext.IsNull()) {
-    foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
+#ifdef OPTIMIZE_PRS
+    foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
+      AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
+#else
+    foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
       AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
+#endif
       // erase an object
       Handle(AIS_InteractiveObject) anIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
       if (!anIO.IsNull()) {
@@ -573,10 +594,14 @@ bool XGUI_Displayer::eraseAll(const bool theUpdateViewer)
 //**************************************************************
 AISObjectPtr XGUI_Displayer::getAISObject(ObjectPtr theObject) const
 {
+#ifdef OPTIMIZE_PRS
+  return myResult2AISObjectMap.value(theObject);
+#else
   AISObjectPtr anIO;
   if (myResult2AISObjectMap.contains(theObject))
     anIO = myResult2AISObjectMap[theObject];
   return anIO;
+#endif
 }
 
 //**************************************************************
@@ -589,15 +614,20 @@ ObjectPtr XGUI_Displayer::getObject(const AISObjectPtr& theIO) const
 //**************************************************************
 ObjectPtr XGUI_Displayer::getObject(const Handle(AIS_InteractiveObject)& theIO) const
 {
+#ifdef OPTIMIZE_PRS
+  ObjectPtr anObject = myResult2AISObjectMap.value(theIO);
+#else
   ObjectPtr anObject;
-  foreach (ObjectPtr anObj, myResult2AISObjectMap.keys()) {
-    AISObjectPtr aAIS = myResult2AISObjectMap[anObj];
+  ResultToAISMap::const_iterator aMapIter = myResult2AISObjectMap.cbegin();
+  for (; aMapIter != myResult2AISObjectMap.cend(); aMapIter++) {
+    const AISObjectPtr& aAIS = aMapIter.value();
     Handle(AIS_InteractiveObject) anAIS = aAIS->impl<Handle(AIS_InteractiveObject)>();
     if (anAIS == theIO)
-      anObject = anObj;
+      anObject = aMapIter.key();
     if (anObject.get())
       break;
   }
+#endif
   if (!anObject.get()) {
     std::shared_ptr<GeomAPI_AISObject> anAISObj = AISObjectPtr(new GeomAPI_AISObject());
     if (!theIO.IsNull()) {
@@ -860,7 +890,11 @@ void XGUI_Displayer::removeFilters()
 //**************************************************************
 void XGUI_Displayer::showOnly(const QObjectPtrList& theList)
 {
+#ifdef OPTIMIZE_PRS
+  QObjectPtrList aDispList = myResult2AISObjectMap.objects();
+#else
   QObjectPtrList aDispList = myResult2AISObjectMap.keys();
+#endif
   foreach(ObjectPtr aObj, aDispList) {
     if (!theList.contains(aObj))
       erase(aObj, false);
@@ -939,7 +973,11 @@ QColor XGUI_Displayer::setObjectColor(ObjectPtr theObject,
 //**************************************************************
 void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS)
 {
+#ifdef OPTIMIZE_PRS
+  myResult2AISObjectMap.add(theObject, theAIS);
+#else
   myResult2AISObjectMap[theObject] = theAIS;
+#endif
 
 #ifdef DEBUG_DISPLAY
   std::ostringstream aPtrStr;
@@ -954,8 +992,13 @@ void XGUI_Displayer::appendResultObject(ObjectPtr theObject, AISObjectPtr theAIS
 std::string XGUI_Displayer::getResult2AISObjectMapInfo() const
 {
   QStringList aContent;
-  foreach (ObjectPtr aObj, myResult2AISObjectMap.keys()) {
+#ifdef OPTIMIZE_PRS
+  foreach(ObjectPtr aObj, myResult2AISObjectMap.objects()) {
+    AISObjectPtr aAISObj = myResult2AISObjectMap.value(aObj);
+#else
+  foreach(ObjectPtr aObj, myResult2AISObjectMap.keys()) {
     AISObjectPtr aAISObj = myResult2AISObjectMap[aObj];
+#endif
     std::ostringstream aPtrStr;
     aPtrStr << "aObj = " << aObj.get() << ":";
     aPtrStr << "anAIS = " << aAISObj.get() << ":";
@@ -978,7 +1021,7 @@ void XGUI_Displayer::getPresentations(const ObjectPtr& theObject,
     if (aAISObj.get() == NULL) {
       // if result is a result of a composite feature, it is visualized by visualization of
       // composite children, so we should get one of this presentations
-      ResultCompSolidPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultCompSolid>(aResult);
+      ResultBodyPtr aCompSolid = std::dynamic_pointer_cast<ModelAPI_ResultBody>(aResult);
       if (aCompSolid.get() && aCompSolid->numberOfSubs() > 0) {
         aAISObj = getAISObject(aCompSolid->subResult(0));
       }
@@ -1069,12 +1112,12 @@ void XGUI_Displayer::AddOrRemoveSelectedShapes(Handle(AIS_InteractiveContext) th
   /// If count of calls setSelectec is even, the object stays in the previous state
   /// (selected, deselected)
   /// OCCT: to write about the problem that active owners method returns one owner several times
-  QList<long> aSelectedIds; // Remember of selected address in order to avoid duplicates
+  QList<size_t> aSelectedIds; // Remember of selected address in order to avoid duplicates
   for (; anOwnersIt.More(); anOwnersIt.Next()) {
     anOwner = Handle(SelectMgr_EntityOwner)::DownCast (anOwnersIt.Value());
-    if (aSelectedIds.contains((long)anOwner.get()))
+    if (aSelectedIds.contains((size_t)anOwner.get()))
       continue;
-    aSelectedIds.append((long)anOwner.get());
+    aSelectedIds.append((size_t)anOwner.get());
 
     Handle(StdSelect_BRepOwner) BROwnr = Handle(StdSelect_BRepOwner)::DownCast(anOwner);
     if (!BROwnr.IsNull() && BROwnr->HasShape()) {
@@ -1131,3 +1174,46 @@ XGUI_SelectionActivate* XGUI_Displayer::selectionActivate() const
 {
   return myWorkshop->selectionActivate();
 }
+
+//**************************************************************
+GeomPlanePtr XGUI_Displayer::getScreenPlane() const
+{
+  GeomPlanePtr aResult;
+  Handle(AIS_InteractiveContext) aContext = AISContext();
+  if (!aContext.IsNull()) {
+    Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
+    Handle(V3d_View) aView;
+    for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
+      aView = aViewer->ActiveView();
+      break;
+    }
+    if (!aView.IsNull()) {
+      double aEyeX, aEyeY, aEyeZ;
+      aView->Eye(aEyeX, aEyeY, aEyeZ);
+
+      double aProjX, aProjY, aProjZ;
+      aView->Proj(aProjX, aProjY, aProjZ);
+
+      GeomPointPtr aPnt = GeomPointPtr(new GeomAPI_Pnt(aEyeX, aEyeY, aEyeZ));
+      GeomDirPtr aDir = GeomDirPtr(new GeomAPI_Dir(aProjX, aProjY, aProjZ));
+
+      aResult = GeomPlanePtr(new GeomAPI_Pln(aPnt, aDir));
+    }
+  }
+  return aResult;
+}
+
+double XGUI_Displayer::getViewScale() const
+{
+  Handle(AIS_InteractiveContext) aContext = AISContext();
+  if (!aContext.IsNull()) {
+    Handle(V3d_Viewer) aViewer = aContext->CurrentViewer();
+    Handle(V3d_View) aView;
+    for (aViewer->InitActiveViews(); aViewer->MoreActiveViews(); aViewer->NextActiveViews()) {
+      aView = aViewer->ActiveView();
+      break;
+    }
+    return aView->Camera()->Scale();
+  }
+  return 1;
+}