Back to index

salome-geom  6.5.0
EntityGUI_FeatureDetectorDlg.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 // GEOM GEOMGUI : GUI for Geometry component
00024 // File   : EntityGUI_SketcherDlg.cxx
00025 // Author : Renaud NEDELEC, Open CASCADE S.A.S.
00026 
00027 // SALOME includes
00028 #include "EntityGUI_FeatureDetectorDlg.h"
00029 #include <ShapeRec_FeatureDetector.hxx>
00030 
00031 #include <OCCViewer_ViewWindow.h>
00032 #include <OCCViewer_ViewManager.h>
00033 
00034 #include <SOCC_ViewModel.h>
00035 
00036 #include <DlgRef.h>
00037 #include <GeometryGUI.h>
00038 #include <EntityGUI.h>
00039 #include <GEOMBase.h>
00040 #include <GEOM_Object.hxx>
00041 
00042 #include <SUIT_Desktop.h>
00043 #include <SUIT_ResourceMgr.h>
00044 #include <SUIT_Session.h>
00045 #include <SUIT_ViewWindow.h>
00046 #include <SalomeApp_Application.h>
00047 #include <LightApp_Application.h>
00048 #include <LightApp_SelectionMgr.h>
00049 
00050 #include <SALOME_ListIteratorOfListIO.hxx>
00051 
00052 #include <SalomeApp_Study.h>
00053 
00054 #include <utilities.h>
00055 #include <Precision.hxx>
00056 
00057 // OCCT includes
00058 #include <gp_Pnt.hxx>
00059 #include <TopoDS_Shape.hxx>
00060 #include <TopoDS_Wire.hxx>
00061 
00062 #include <BRepBuilderAPI_MakeVertex.hxx>
00063 #include <BRepBuilderAPI_MakeFace.hxx>
00064 #include <BRepBuilderAPI_MakePolygon.hxx>
00065 #include <BRepBuilderAPI_Transform.hxx>
00066 
00067 #include <AIS_TexturedShape.hxx>
00068 #include <TCollection_AsciiString.hxx>
00069 #include <StdSelect_DisplayMode.hxx>
00070 
00071 #include <Graphic3d_MaterialAspect.hxx>
00072 
00073 // C++ includes
00074 #include <set>
00075 #include <utility>
00076 
00077 // boost includes
00078 #include <boost/utility.hpp>
00079 
00080 // Constructors
00081 enum{
00082   CONTOURS,
00083   CORNERS,
00084   LINES
00085 };
00086 
00087 enum {
00088   RADIO_BUTTONS,
00089   MSG,
00090   PUSH_BUTTON,
00091 };
00092 
00093 // // // View
00094 // // enum {
00095 // //   XY,
00096 // //   YZ,
00097 // //   XZ
00098 // // };
00099   
00100 
00101 //=================================================================================
00102 // class    : EntityGUI_FeatureDetectorDlg()
00103 // purpose  : Constructs a EntityGUI_FeatureDetectorDlg which is a child of 'parent', with the
00104 //            name 'name' and widget flags set to 'f'.
00105 //            The dialog will by default be modeless, unless you set 'modal' to
00106 //            TRUE to construct a modal dialog.
00107 //=================================================================================
00108 EntityGUI_FeatureDetectorDlg::EntityGUI_FeatureDetectorDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
00109                                               bool modal, Qt::WindowFlags fl)
00110   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl) 
00111 {
00112   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
00113   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_DUMP")));
00114 
00115   setWindowTitle(tr("GEOM_DETECT_TITLE"));
00116   
00117   /***************************************************************/
00118   
00119   mainFrame()->GroupConstructors->setTitle(tr("GEOM_FEATURES"));
00120   mainFrame()->RadioButton1->setText(tr("GEOM_CONTOURS"));
00121   mainFrame()->RadioButton2->setText(tr("GEOM_CORNERS"));
00122   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
00123   mainFrame()->RadioButton3->close();
00124   
00125 //   myViewGroup = new DlgRef_3Radio(centralWidget());
00126 //   myViewGroup->GroupBox1->setTitle(tr("GEOM_VIEW"));
00127 //   myViewGroup->RadioButton1->setText(tr( "GEOM_TOP"  ));
00128 //   myViewGroup->RadioButton2->setText(tr( "GEOM_FRONT"));
00129 //   myViewGroup->RadioButton3->setText(tr( "GEOM_LEFT" ));
00130 //   myViewButtonGroup = new QButtonGroup( this );
00131 //   myViewButtonGroup->addButton( myViewGroup->RadioButton1, XY ); // Top view
00132 //   myViewButtonGroup->addButton( myViewGroup->RadioButton2, YZ ); // Front View
00133 //   myViewButtonGroup->addButton( myViewGroup->RadioButton3, XZ ); // Left View
00134 //   
00135 //   myViewGroup->hide();
00136   
00137   // Widgets for the selection of the picture and the Region Of Interest 
00138   
00139   
00140   mySelectionGroup = new DlgRef_1Sel1Frame(centralWidget());
00141 
00142   mySelectionGroup->PushButton1->setIcon(image0);
00143   mySelectionGroup->PushButton1->setCheckable(true);
00144   mySelectionGroup->PushButton1->setAutoExclusive(true);
00145   
00146   mySelectionGroup->PushButton2->setIcon(image1);
00147   mySelectionGroup->PushButton2->setCheckable(true);
00148   mySelectionGroup->PushButton2->setAutoExclusive(true);
00149 
00150   mySelectionGroup->TextLabel1->setText(tr( "GEOM_PICTURE" ));
00151   mySelectionGroup->FrameLabel->setText("");
00152   
00153   myOutputGroup = new DlgRef_3Radio(centralWidget());
00154   myOutputGroup->GroupBox1->setTitle(tr("GEOM_DETECT_OUTPUT"));
00155   myOutputGroup->RadioButton2->setText(tr( "GEOM_POLYLINE"  ));
00156   myOutputGroup->RadioButton1->setText(tr( "GEOM_SPLINE"));
00157   myOutputGroup->RadioButton3->hide();
00158   
00159 //   myOutputGroup->hide(); //caché pour la demo
00160     
00161   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
00162   layout->setMargin(0); layout->setSpacing(6);
00163 //   layout->addWidget( myViewGroup);
00164   layout->addWidget( mySelectionGroup);
00165   layout->addWidget( myOutputGroup);
00166   
00167 //   mainFrame()->GroupBoxName->hide();
00168   
00169   // Build an instance of detection used to perform image processing operations
00170   aDetector = new ShapeRec_FeatureDetector();
00171   
00172   setHelpFileName( "shape_recognition_page.html" );
00173   
00174   Init();
00175   
00176 }
00177 
00178 //=================================================================================
00179 // function : ~EntityGUI_FeatureDetectorDlg()
00180 // purpose  : Destroys the object and frees any allocated resources
00181 //=================================================================================
00182 EntityGUI_FeatureDetectorDlg::~EntityGUI_FeatureDetectorDlg()
00183 {
00184   
00185 }
00186 
00187 //=================================================================================
00188 // function : Init()
00189 // purpose  :
00190 //=================================================================================
00191 void EntityGUI_FeatureDetectorDlg::Init()
00192 {
00193   connect( myGeomGUI,         SIGNAL( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) );
00194   connect( buttonOk(),        SIGNAL( clicked() ),               this, SLOT( ClickOnOk() ) );
00195   connect( buttonApply(),     SIGNAL( clicked() ),               this, SLOT( ClickOnApply() ) );
00196   connect( this,              SIGNAL(constructorsClicked(int)),  this, SLOT(ConstructorsClicked(int))); 
00197   connect( mySelectionGroup->PushButton2,      SIGNAL( clicked() ),               this, SLOT( onButtonClicked() ) );
00198   connect( mySelectionGroup->PushButton1,       SIGNAL( clicked() ),               this, SLOT( onButtonClicked() ) );  
00199 //   connect( myViewButtonGroup, SIGNAL( buttonClicked( int ) ),    this, SLOT( onViewClicked( int ) ) );
00200   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),this, SLOT( SelectionIntoArgument() ) );
00201   
00202   myConstructorId = 0;
00203   mySelectionGroup->PushButton1->click();
00204   
00205 //   SetEditCurrentArgument();
00206   SelectionIntoArgument();
00207   
00208   initName(tr("GEOM_CONTOURS")); 
00209   resize(100,100);
00210   
00211 //   myViewGroup->RadioButton1->setChecked(true);
00212   myOutputGroup->RadioButton1->setChecked(true);
00213   
00214   gp_Pnt aOrigin = gp_Pnt(0, 0, 0);
00215   gp_Dir aDirZ = gp_Dir(0, 0, 1);
00216   gp_Dir aDirX = gp_Dir(1, 0, 0);
00217   
00218   aGlobalCS = gp_Ax3(aOrigin, aDirZ, aDirX);
00219   
00220   myStartPnt = gp_Pnt(0,0,0);
00221   myEndPnt = myStartPnt;
00222   
00223   myGeomGUI->SetWorkingPlane( aGlobalCS );
00224   myGeomGUI->ActiveWorkingPlane();
00225   
00226   ConstructorsClicked(myConstructorId);
00227 }
00228 
00229 //=================================================================================
00230 // function : SelectionIntoArgument()
00231 // purpose  : Called when selection as changed or other case
00232 //=================================================================================
00233 void EntityGUI_FeatureDetectorDlg::SelectionIntoArgument()
00234 {
00235   
00236   // TODO supprimer les lignes qui ne servent à rien le cas échéant
00237   SUIT_ViewWindow*       theViewWindow  = getDesktop()->activeWindow();
00238   std::map< std::string , std::vector<Handle(AIS_InteractiveObject)> >::iterator AISit;
00239   SOCC_Viewer* soccViewer = (SOCC_Viewer*)(theViewWindow->getViewManager()->getViewModel());
00240 
00241   if (!myEditCurrentArgument->isEnabled())
00242     return;
00243   
00244   myEditCurrentArgument->setText( "" );
00245 
00246   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
00247   SALOME_ListIO aSelList;
00248   aSelMgr->selectedObjects(aSelList);
00249   SALOME_ListIteratorOfListIO anIt( aSelList );
00250   
00251   for( ; anIt.More(); anIt.Next() )
00252     if( !anIt.Value().IsNull() )
00253     {
00254       myFaceEntry = anIt.Value()->getEntry();
00255     }
00256 
00257   if (aSelList.Extent() != 1) {
00258     if (myEditCurrentArgument == mySelectionGroup->LineEdit1) 
00259       myFace.nullify();
00260     return;
00261   }
00262 
00263   TopAbs_ShapeEnum aNeedType = TopAbs_FACE ;
00264   GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
00265   TopoDS_Shape aShape;
00266   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
00267     QString aName = GEOMBase::GetName( aSelectedObject.get() );
00268     myEditCurrentArgument->setText( aName );
00269     
00270     if ( myEditCurrentArgument == mySelectionGroup->LineEdit1 ) {
00271       myFace = aSelectedObject;
00272       AISit = soccViewer->entry2aisobjects.find(myFaceEntry.toStdString());
00273       if (AISit == soccViewer->entry2aisobjects.end())
00274         return;
00275       
00276       Handle(AIS_InteractiveObject) myAIS = (*AISit).second[0];
00277       Handle(GEOM_AISShape) myAISShape;
00278       if( myAIS->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
00279         myAISShape = Handle(GEOM_AISShape)::DownCast( myAIS );
00280       }
00281       else
00282         return ;
00283       
00284       std::string theImgFileName = myAISShape->TextureFile();      
00285       if ( theImgFileName == "" )
00286         return ;
00287 
00288       // Setting the image caracteristics
00289       aDetector->SetPath( theImgFileName );
00290       height            =  aDetector->GetImgHeight();
00291       width             =  aDetector->GetImgWidth();
00292       pictureLeft       = -0.5 * width;              // X coordinate of the top left  corner of the background image in the view
00293       pictureTop        =  0.5 * height;             // Y coordinate of both top corners
00294       
00295     } 
00296   }
00297   
00298 }
00299 
00300 //=================================================================================
00301 // function : acceptMouseEvent()
00302 // purpose  :
00303 //=================================================================================
00304 bool EntityGUI_FeatureDetectorDlg::acceptMouseEvent() const
00305 { 
00306   return mySelectionGroup->PushButton2->isChecked();  
00307 }
00308 
00309 //=======================================================================
00310 // function : ClickOnOk()
00311 // purpose  :
00312 //=======================================================================
00313 void EntityGUI_FeatureDetectorDlg::ClickOnOk()
00314 {
00315   setIsApplyAndClose( true );
00316   if (onAccept())
00317     ClickOnCancel();
00318 }
00319 
00320 //=================================================================================
00321 // function : ClickOnApply()
00322 // purpose  :
00323 //=================================================================================
00324 bool EntityGUI_FeatureDetectorDlg::ClickOnApply()
00325 {
00326   if (!onAccept())
00327     return false;
00328 
00329 //   initName();
00330   ConstructorsClicked(getConstructorId());
00331   return true;
00332 }
00333 
00334 //=================================================================================
00335 // function : onConstructorClicked(int)
00336 // purpose  :
00337 //=================================================================================
00338 void EntityGUI_FeatureDetectorDlg::ConstructorsClicked(int id)
00339 {
00340   MESSAGE("Constructor id ="<<id)
00341   myConstructorId = id;
00342   switch (id)
00343   {
00344     case CORNERS:
00345 //       myViewGroup->show();
00346 //       mySelectionGroup->show();
00347       myOutputGroup->hide();
00348 //       mySelectionGroup->TextLabel2->setText(tr("GEOM_DETECT_ZONE"));
00349       mySelectionGroup->TextLabel2->hide();
00350       mySelectionGroup->Frame->hide();
00351       mySelectionGroup->PushButton2->hide();
00352       initName(tr("GEOM_CORNERS"));
00353       break;
00354     case CONTOURS:
00355 //       myViewGroup->hide();
00356 //       mySelectionGroup->hide();
00357 //       mySelectionGroup->show();
00358       myOutputGroup->show();
00359       mySelectionGroup->TextLabel2->show();
00360       mySelectionGroup->Frame->show();
00361       mySelectionGroup->PushButton2->show();
00362       mySelectionGroup->TextLabel2->setText(tr("GEOM_COLOR_FILTER"));
00363       initName(tr("GEOM_CONTOURS"));
00364       break;
00365     case LINES:
00366 //       myViewGroup->hide();
00367 //       mySelectionGroup->hide();
00368 //       mySelectionGroup->show();
00369       myOutputGroup->hide();
00370       mySelectionGroup->TextLabel2->setText(tr(""));
00371       initName(tr("GEOM_LINES"));
00372       break;
00373   }
00374 }
00375 
00376 // //=================================================================================
00377 // // function : onViewClicked()
00378 // // purpose  :
00379 // //=================================================================================
00380 // void EntityGUI_FeatureDetectorDlg::onViewClicked(int id)
00381 // {
00382 //   gp_Pnt aOrigin = gp_Pnt(0, 0, 0);
00383 //   gp_Dir aDirZ;
00384 //   gp_Dir aDirX;
00385 //   
00386 //   switch(id)
00387 //   {
00388 //     case XY:
00389 //         aDirZ = gp_Dir(0, 0, 1);
00390 //         aDirX = gp_Dir(1, 0, 0);
00391 //       break;
00392 //     case YZ:
00393 //         aDirZ = gp_Dir(1, 0, 0);
00394 //         aDirX = gp_Dir(0, 1, 0);
00395 //       break;
00396 //     case XZ:
00397 //         aDirZ = gp_Dir(0, -1, 0);
00398 //         aDirX = gp_Dir(1, 0 , 0);
00399 //       break;
00400 //   }
00401 //   
00402 //   myWPlane = gp_Ax3(aOrigin, aDirZ, aDirX);
00403 //   myGeomGUI->SetWorkingPlane( myWPlane );
00404 //   myGeomGUI->ActiveWorkingPlane();
00405 //   
00406 // }
00407 
00408 //=================================================================================
00409 // function : onButtonClicked()
00410 // purpose  :
00411 //=================================================================================
00412 void EntityGUI_FeatureDetectorDlg::onButtonClicked()
00413 {
00414   QPushButton* send = (QPushButton*)sender();
00415   if (send == mySelectionGroup->PushButton2)
00416   {
00417     mySelectionGroup->LineEdit1->setEnabled(false);
00418   }
00419   else if (send == mySelectionGroup->PushButton1)
00420   {
00421     myStartPnt = gp_Pnt(0,0,0);
00422     myEndPnt   = myStartPnt;
00423     myEditCurrentArgument = mySelectionGroup->LineEdit1;
00424     mySelectionGroup->LineEdit1->setEnabled(true);   
00425   }
00426 }
00427 
00428 //=================================================================================
00429 // function : setStartPnt( const QPoint& )
00430 // purpose  :
00431 //=================================================================================
00432 void EntityGUI_FeatureDetectorDlg::setStartPnt(const gp_Pnt& theStartPnt)
00433 {
00434   myStartPnt = theStartPnt;
00435   MESSAGE("myStartPnt = ("<<theStartPnt.X()<<", "<<theStartPnt.Y()<<")")
00436 }
00437 
00438 //=================================================================================
00439 // function : setEndPnt( const QPoint& )
00440 // purpose  :
00441 //=================================================================================
00442 void EntityGUI_FeatureDetectorDlg::setEndPnt(const gp_Pnt& theEndPnt)
00443 {
00444   myEndPnt = theEndPnt;
00445   MESSAGE("myEndPnt = ("<<theEndPnt.X()<<", "<<theEndPnt.Y()<<")")
00446   if (setSelectionRect() && aDetector->GetImgHeight() > 0)
00447     showImageSample();
00448 }
00449 
00450 //=================================================================================
00451 // function : setSelectionRect()
00452 // purpose  :
00453 //=================================================================================
00454 bool EntityGUI_FeatureDetectorDlg::setSelectionRect()
00455 { 
00456   // Set detection rectangle in the background image coordinates system
00457   double left    = std::min( myStartPnt.X(), myEndPnt.X() );
00458   double top     = std::max( myStartPnt.Y(), myEndPnt.Y() );
00459   double right   = std::max( myStartPnt.X(), myEndPnt.X() );
00460   double bottom  = std::min( myStartPnt.Y(), myEndPnt.Y() );
00461   
00462   QPoint topLeft     = QPoint(left  - pictureLeft, pictureTop - top   );
00463   QPoint bottomRight = QPoint(right - pictureLeft, pictureTop - bottom);
00464   
00465   myRect = QRect(topLeft, bottomRight);
00466   
00467   return (!myRect.isEmpty() && myRect.width() > 1);
00468 }
00469 
00470 //=================================================================================
00471 // function : showImageSample()
00472 // purpose  : Display a preview of the image sample selected by the user
00473 //=================================================================================
00474 void EntityGUI_FeatureDetectorDlg::showImageSample()
00475 { 
00476   // Cropp the image to the selection rectangle given by the user
00477   aDetector->SetROI( myRect ); 
00478   std::string samplePicturePath = aDetector->CroppImage();
00479   
00480   // Display the result
00481   QPixmap pixmap(QString(samplePicturePath.c_str()));
00482   mySelectionGroup->FrameLabel->setPixmap(pixmap);
00483   mySelectionGroup->FrameLabel->setMask(pixmap.mask());
00484 }
00485 
00486 //=================================================================================
00487 // function : createOperation
00488 // purpose  :
00489 //=================================================================================
00490 GEOM::GEOM_IOperations_ptr EntityGUI_FeatureDetectorDlg::createOperation()
00491 {
00492   return myGeomGUI->GetGeomGen()->GetIShapesOperations( getStudyId() );
00493 }
00494 
00495 //=================================================================================
00496 // function : execute()
00497 // purpose  :
00498 //=================================================================================
00499 bool EntityGUI_FeatureDetectorDlg::execute( ObjectList& objects )
00500 {
00501   bool res = false;
00502   
00503   GEOM::GEOM_IBasicOperations_var  aBasicOperations  = myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
00504   GEOM::GEOM_IShapesOperations_var aShapesOperations = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
00505   
00506   if (myConstructorId == CORNERS)
00507   {
00508     double subPictureLeft;
00509     double subPictureTop;
00510     if( !myRect.isEmpty() )
00511     {
00512       aDetector->SetROI( myRect );
00513       subPictureLeft    = myStartPnt.X();                
00514       subPictureTop     = myStartPnt.Y();
00515     }
00516     else
00517     {
00518       subPictureLeft    = pictureLeft;
00519       subPictureTop     = pictureTop;
00520     }
00521     aDetector->ComputeCorners();
00522     CvPoint2D32f* corners     = aDetector->GetCorners();
00523     int cornerCount           = aDetector->GetCornerCount();
00524     int i;
00525     
00526     // Build the geom objects associated to the detected corners and returned by execute   
00527     if( !aBasicOperations->_is_nil() && !aShapesOperations->_is_nil() ) 
00528     {
00529       GEOM::GEOM_Object_var  aGeomCorner;
00530       GEOM::ListOfGO_var     geomCorners = new GEOM::ListOfGO();
00531       geomCorners->length( cornerCount );
00532       for (i = 0; i < cornerCount; i++)
00533       {
00534         double x = subPictureLeft + corners[i].x;
00535         double y = subPictureTop  - corners[i].y;
00536         double z =  0;
00537         
00538         aGeomCorner = aBasicOperations->MakePointXYZ( x,y,z );
00539         
00540         geomCorners[i] = aGeomCorner;  
00541       } 
00542       GEOM::GEOM_Object_var aCompound = aShapesOperations->MakeCompound(geomCorners);    
00543       if ( !aCompound->_is_nil() )
00544       {
00545         // Dark blue color
00546         SALOMEDS::Color aColor;
00547         aColor.R = 0;
00548         aColor.G = 0;
00549         aColor.B = 0.8;
00550         
00551         aCompound->SetColor(aColor);
00552         aCompound->SetMarkerStd(GEOM::MT_POINT,GEOM::MS_30);
00553         objects.push_back( aCompound._retn() );
00554         res = true;
00555       }
00556     }
00557   }
00558   else if (myConstructorId == CONTOURS)
00559   {
00560     int method = 0 ; //CANNY
00561     if( !myRect.isEmpty() && myRect.width() > 1 )
00562     {
00563       aDetector->SetROI( myRect );
00564       method = 1 ; //COLORFILTER    
00565     }
00566     
00567     GEOM::GEOM_ICurvesOperations_var aCurveOperations = myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
00568     
00569     aDetector->ComputeContours( method );
00570     std::vector< std::vector<cv::Point> >   contours  = aDetector->GetContours();
00571     std::vector<cv::Vec4i>                  hierarchy = aDetector->GetContoursHierarchy();
00572     
00573     std::vector< cv::Point >                contour;
00574     int idx = 0;
00575     
00576     GEOM::ListOfGO_var                      geomContours = new GEOM::ListOfGO();
00577     int contourCount = 0;
00578     
00579     bool insert;
00580     
00581     MESSAGE("hierarchy.size() =" << hierarchy.size()) 
00582     for( ; idx >= 0; idx = hierarchy[idx][0])
00583     {
00584 //       for(int count=0, child=idx; child>=0, count<1; child=hierarchy[idx][2], count++)
00585 //       {     
00586 //         contour = contours[child];
00587         contour = contours[idx];
00588         std::vector< cv::Point >::iterator it;
00589         std::vector< cv::Point >::iterator it_previous;
00590         std::vector< cv::Point >::iterator it_next;
00591         GEOM::GEOM_Object_var  aGeomContourPnt;
00592         GEOM::ListOfGO_var     geomContourPnts = new GEOM::ListOfGO();
00593        
00594         geomContourPnts->length( contour.size() );
00595 
00596         int j = 0;
00597         std::set< std::vector<int> > existing_points;
00598         std::pair< std::set< std::vector<int> >::iterator,bool > pnt_it;
00599         for ( it=contour.begin() ; it < contour.end(); it++ )
00600         {
00601           int pnt_array[] = {it->x,it->y};     
00602           std::vector<int> pnt (pnt_array, pnt_array + sizeof(pnt_array) / sizeof(int) );
00603 
00604           pnt_it=existing_points.insert(pnt);
00605           if (pnt_it.second == true)         // To avoid double points in the contours
00606           {
00607             insert = true;
00608             if (it!=contour.begin())         // From the second point on perform some checking to avoid loops in the contours we build
00609             {
00610               it_previous = boost::prior(it);
00611               it_next = boost::next(it);
00612               
00613               double u_v_scalar_product = (it->x - it_previous->x) * (it_next->x - it->x) + 
00614                                           (it->y - it_previous->y) * (it_next->y - it->y);                                       
00615               if (u_v_scalar_product < 0)
00616               {
00617                 double u_v_det = (it->x - it_previous->x) * (it_next->y - it->y) - 
00618                                  (it->y - it_previous->y) * (it_next->x - it->x);
00619                                           
00620                 double norme_u = sqrt ( (it->x - it_previous->x)*(it->x - it_previous->x) +
00621                                         (it->y - it_previous->y)*(it->y - it_previous->y) );
00622                 
00623                 double norme_v = sqrt ( (it->x - it_next->x)*(it->x - it_next->x) +
00624                                         (it->y - it_next->y)*(it->y - it_next->y) );
00625                                                                                                 
00626                 double u_v_sinus = u_v_det / (norme_u * norme_v);
00627                 
00628                 if (fabs(u_v_sinus) < Precision::Confusion())
00629                 { 
00630                   insert = false;
00631                 }                         
00632               }
00633             }
00634             double x = -0.5 *width  + it->x;
00635             double y =  0.5 *height - it->y;
00636             double z =  0;
00637             if (insert)
00638             {
00639               aGeomContourPnt    = aBasicOperations->MakePointXYZ( x,y,z );
00640               geomContourPnts->length( j+1 );
00641               geomContourPnts[j] = aGeomContourPnt;
00642               j++;
00643             }
00644           }
00645         }
00646         
00647         GEOM::GEOM_Object_var aWire;
00648         if(myOutputGroup->RadioButton2->isChecked())
00649         {
00650           aWire = aCurveOperations->MakePolyline(geomContourPnts.in(), false);
00651         }
00652         else if(myOutputGroup->RadioButton1->isChecked())
00653         {
00654           aWire = aCurveOperations->MakeSplineInterpolation(geomContourPnts.in(), /*closed =*/ false, /*reordering =*/ false);
00655         }
00656         else
00657           return res;
00658         
00659         if ( !aWire->_is_nil() )
00660         {
00661           geomContours->length(contourCount + 1);
00662           geomContours[contourCount] = aWire;
00663           contourCount++;
00664         }
00665 //       }
00666     }
00667     GEOM::GEOM_Object_var aContoursCompound = aShapesOperations->MakeCompound(geomContours);
00668     if ( !aContoursCompound->_is_nil() )
00669     {
00670       objects.push_back( aContoursCompound._retn() );
00671     }
00672     res=true;
00673   }
00674   
00675   // TEST not very conclusive
00676   
00677 //   else if(myConstructorId ==LINES)
00678 //   {
00679 //     aDetector->ComputeLines();
00680 //     std::vector<cv::Vec4i>  lines = aDetector->GetLines();
00681 //     GEOM::GEOM_Object_var  Pnt1;
00682 //     GEOM::GEOM_Object_var  Pnt2;
00683 //     GEOM::GEOM_Object_var  aLine;
00684 //     
00685 //     GEOM::ListOfGO_var     geomLines = new GEOM::ListOfGO();
00686 //     int linesCount=0;
00687 //     for( int i = 0; i < lines.size(); i++ )
00688 //     {
00689 //       Pnt1 = aBasicOperations->MakePointXYZ( -0.5 *width + lines[i][0], 0.5 *height - lines[i][1], 0 );
00690 //       Pnt2 = aBasicOperations->MakePointXYZ( -0.5 *width + lines[i][2], 0.5 *height - lines[i][3], 0 );
00691 //       aLine = aBasicOperations->MakeLineTwoPnt( Pnt1, Pnt2 );
00692 //       if ( !aLine->_is_nil() )
00693 //       {
00694 //         geomLines->length(linesCount + 1);
00695 //         geomLines[linesCount] = aLine;
00696 //         linesCount++;
00697 //       }
00698 //     }
00699 //     GEOM::GEOM_Object_var aLinesCompound = aShapesOperations->MakeCompound(geomLines);
00700 //     if ( !aLinesCompound->_is_nil() )
00701 //     {
00702 //       objects.push_back( aLinesCompound._retn() );
00703 //     }
00704 // 
00705 //     res=true;
00706 //   }
00707   
00708   return res;
00709 }