Salome HOME
Mantis issue 0021772: EDF 2336 GEOM: Non valid face created from two circles
[modules/geom.git] / src / GEOMGUI / GEOM_Displayer.cxx
index 197b32029f155232f431f123684ad9896aa70e35..7edbae5b250419101084af6f93bee54c5481c7d6 100644 (file)
 #include <Graphic3d_HArray1OfBytes.hxx>
 #endif
 
+// If the next macro is defined, autocolor feature works for all sub-shapes;
+// if it is undefined, autocolor feature works for groups only
+//#define GENERAL_AUTOCOLOR
+// Below macro, when uncommented, switches on simplified (more performant) algorithm
+// of auto-color picking up
+//#define SIMPLE_AUTOCOLOR
+
 //================================================================
 // Function : getActiveStudy
 // Purpose  : Get active study, returns 0 if no open study frame
@@ -683,7 +690,6 @@ void GEOM_Displayer::Update( SALOME_OCCPrs* prs )
           
           AISShape->SetDisplayVectors(aPropMap.value(VECTOR_MODE_PROP).toInt()); 
         }else {
-          MESSAGE("myDisplayMode = "<< myDisplayMode)
           AISShape->SetDisplayMode( myDisplayMode );
           AISShape->SetShadingColor( myShadingColor );
         }
@@ -1824,6 +1830,40 @@ int GEOM_Displayer::UnsetDisplayMode()
   return aPrevMode;
 }
 
+SALOMEDS::Color GEOM_Displayer::getPredefinedUniqueColor()
+{
+  static QList<QColor> colors;
+
+  if ( colors.isEmpty() ) {
+    const int rfactor = 4; // red   color component split factor, must be > 0
+    const int gfactor = 4; // green color component split factor, must be > 0
+    const int bfactor = 3; // blue  color component split factor, must be > 0
+                           // -
+                           // total number of pre-defined colors is defined as
+                           // nbcolors = rfactor * gfactor * bfactor
+                           // NB: all three factors should not have same values
+                           // otherwise all colors will be greyish
+    
+    for ( int g = 0; g < gfactor; g++ ) {
+      for ( int r = 0; r < rfactor; r++ ) {
+       for ( int b = 0; b < bfactor; b++ )
+         colors.append( QColor( qRgb( r * 255 / (rfactor-1), g * 255 / (gfactor-1), b * 255 / (bfactor-1) ) ) );
+      }
+    }
+  }
+
+  static int currentColor = 0;
+
+  SALOMEDS::Color color;
+  color.R = (double)colors[currentColor].red()   / 255.0;
+  color.G = (double)colors[currentColor].green() / 255.0;
+  color.B = (double)colors[currentColor].blue()  / 255.0;
+
+  currentColor = (currentColor+1) % colors.count();
+
+  return color;
+}
+
 SALOMEDS::Color GEOM_Displayer::getUniqueColor( const QList<SALOMEDS::Color>& theReservedColors )
 {
   int aHue = -1;
@@ -2004,48 +2044,50 @@ SALOMEDS::Color GEOM_Displayer::getColor(GEOM::GEOM_Object_var theGeomObject, bo
   hasColor = false;
 
   SUIT_Session* session = SUIT_Session::session();
-  SUIT_Application* app = session->activeApplication();
+  SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( session->activeApplication() );
 
   if ( app && !theGeomObject->_is_nil()) {
     SalomeApp_Study* study = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
 
     if ( study ) {
+      _PTR(Study) aStudy = study->studyDS();
       aSColor = theGeomObject->GetColor();
       hasColor = aSColor.R >= 0 && aSColor.G >= 0 && aSColor.B >= 0;
-      if( !hasColor && theGeomObject->GetType() == GEOM_GROUP ) { // auto color for group
-        GEOM::GEOM_Gen_var theGeomGen = GeometryGUI::GetGeomGen();
-        GEOM::GEOM_IGroupOperations_var anOperations = theGeomGen->GetIGroupOperations( study->id() );
-        GEOM::GEOM_Object_var aMainObject = anOperations->GetMainShape( theGeomObject );
-        if ( !aMainObject->_is_nil() && aMainObject->GetAutoColor() )
-          {
-            QList<SALOMEDS::Color> aReservedColors;
-
-            SalomeApp_Application* app = dynamic_cast<SalomeApp_Application*>( app );
-            CORBA::String_var IOR = app->orb()->object_to_string( aMainObject );
-            if ( strcmp(IOR.in(), "") != 0 )
-              {
-                _PTR(Study) aStudy = study->studyDS();
-                _PTR(SObject) aMainSObject( aStudy->FindObjectIOR( std::string(IOR) ) );
-                _PTR(ChildIterator) it( aStudy->NewChildIterator( aMainSObject ) );
-                for( ; it->More(); it->Next() )
-                  {
-                    _PTR(SObject) aChildSObject( it->Value() );
-                    GEOM::GEOM_Object_var aChildObject =
-                      GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aChildSObject));
-                    if( CORBA::is_nil( aChildObject ) )
-                      continue;
-
-                    if( aChildObject->GetType() != GEOM_GROUP )
-                      continue;
-
-                    SALOMEDS::Color aReservedColor = aChildObject->GetColor();
-                    aReservedColors.append( aReservedColor );
-                  }
-              }
-
-            aSColor = getUniqueColor( aReservedColors );
-            hasColor = true;
-          }
+      if ( !hasColor ) {
+#ifdef GENERAL_AUTOCOLOR // auto-color for all sub-shapes
+       bool general_autocolor = true;
+#else                    // auto-color for groups only
+       bool general_autocolor = false;
+#endif                   // GENERAL_AUTOCOLOR
+       if ( general_autocolor || theGeomObject->GetType() == GEOM_GROUP ) {
+         GEOM::GEOM_Object_var aMainObject = theGeomObject->GetMainShape();
+         if ( !CORBA::is_nil( aMainObject ) && aMainObject->GetAutoColor() ) {
+#ifdef SIMPLE_AUTOCOLOR  // simplified algorithm for auto-colors
+           aSColor = getPredefinedUniqueColor();
+           hasColor = true;
+#else                    // old algorithm  for auto-colors
+           QList<SALOMEDS::Color> aReservedColors;
+           CORBA::String_var IOR = app->orb()->object_to_string( aMainObject );
+           _PTR(SObject) aMainSObject( aStudy->FindObjectIOR( IOR.in() ) );
+           if ( aMainSObject ) {
+             _PTR(ChildIterator) it( aStudy->NewChildIterator( aMainSObject ) );
+             for ( ; it->More(); it->Next() ) {
+               _PTR(SObject) aChildSObject( it->Value() );
+               GEOM::GEOM_Object_var aChildObject =
+                 GEOM::GEOM_Object::_narrow(GeometryGUI::ClientSObjectToObject(aChildSObject));
+               if ( CORBA::is_nil( aChildObject ) )
+                 continue;
+               
+               SALOMEDS::Color aReservedColor = aChildObject->GetColor();
+               if ( aReservedColor.R >= 0 && aReservedColor.G >= 0 && aReservedColor.B >= 0 )
+                 aReservedColors.append( aReservedColor );
+             }
+           }
+           aSColor = getUniqueColor( aReservedColors );
+           hasColor = true;
+#endif                   // SIMPLE_AUTOCOLOR
+         }
+       }
       }
     }
   }