]> SALOME platform Git repositories - modules/geom.git/blob - src/EntityGUI/EntityGUI_FeatureDetectorDlg.cxx
Salome HOME
rnc: Import Picture and Feature Detection dialogs small modifications
[modules/geom.git] / src / EntityGUI / EntityGUI_FeatureDetectorDlg.cxx
1 // Copyright (C) 2007-2011  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : EntityGUI_SketcherDlg.cxx
25 // Author : Renaud NEDELEC, Open CASCADE S.A.S.
26
27 #include "EntityGUI_FeatureDetectorDlg.h"
28
29 #include <OCCViewer_ViewWindow.h>
30 #include <OCCViewer_ViewManager.h>
31
32 #include <SOCC_ViewModel.h>
33
34 #include <DlgRef.h>
35 #include <GeometryGUI.h>
36 #include <EntityGUI.h>
37 #include <GEOMBase.h>
38 #include <GEOM_Object.hxx>
39
40 #include <SUIT_Desktop.h>
41 #include <SUIT_ResourceMgr.h>
42 #include <SUIT_Session.h>
43 #include <SUIT_ViewWindow.h>
44 #include <SalomeApp_Application.h>
45 #include <LightApp_Application.h>
46 #include <LightApp_SelectionMgr.h>
47
48 #include <SALOME_ListIteratorOfListIO.hxx>
49
50 #include <SalomeApp_Study.h>
51
52 #include <utilities.h>
53
54 #include <gp_Pnt.hxx>
55 #include <TopoDS_Shape.hxx>
56 #include <TopoDS_Wire.hxx>
57
58 #include <BRepBuilderAPI_MakeVertex.hxx>
59 #include <BRepBuilderAPI_MakeFace.hxx>
60 #include <BRepBuilderAPI_MakePolygon.hxx>
61 #include <BRepBuilderAPI_Transform.hxx>
62
63 #include <AIS_TexturedShape.hxx>
64 #include <TCollection_AsciiString.hxx>
65 #include <StdSelect_DisplayMode.hxx>
66
67 #include <Graphic3d_MaterialAspect.hxx>
68
69 #include <Precision.hxx>
70
71 #include <set>
72 #include <utility>
73
74 #include <boost/utility.hpp>
75
76 // Constructors
77 enum{
78   CONTOURS,
79   CORNERS,
80   LINES
81 };
82
83 enum {
84   RADIO_BUTTONS,
85   MSG,
86   PUSH_BUTTON,
87 };
88
89 // // // View
90 // // enum {
91 // //   XY,
92 // //   YZ,
93 // //   XZ
94 // // };
95   
96
97 //=================================================================================
98 // class    : EntityGUI_FeatureDetectorDlg()
99 // purpose  : Constructs a EntityGUI_FeatureDetectorDlg which is a child of 'parent', with the
100 //            name 'name' and widget flags set to 'f'.
101 //            The dialog will by default be modeless, unless you set 'modal' to
102 //            TRUE to construct a modal dialog.
103 //=================================================================================
104 EntityGUI_FeatureDetectorDlg::EntityGUI_FeatureDetectorDlg( GeometryGUI* theGeometryGUI, QWidget* parent,
105                                               bool modal, Qt::WindowFlags fl)
106   : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl) 
107 {
108   QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_DUMP")));
109   QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
110   
111   setWindowTitle(tr("GEOM_DETECT_TITLE"));
112   
113   /***************************************************************/
114   
115   mainFrame()->GroupConstructors->setTitle(tr("GEOM_FEATURES"));
116   mainFrame()->RadioButton1->setText(tr("GEOM_CONTOURS"));
117   mainFrame()->RadioButton2->setText(tr("GEOM_CORNERS"));
118   mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose);
119   mainFrame()->RadioButton3->close();
120   
121 //   myViewGroup = new DlgRef_3Radio(centralWidget());
122 //   myViewGroup->GroupBox1->setTitle(tr("GEOM_VIEW"));
123 //   myViewGroup->RadioButton1->setText(tr( "GEOM_TOP"  ));
124 //   myViewGroup->RadioButton2->setText(tr( "GEOM_FRONT"));
125 //   myViewGroup->RadioButton3->setText(tr( "GEOM_LEFT" ));
126 //   myViewButtonGroup = new QButtonGroup( this );
127 //   myViewButtonGroup->addButton( myViewGroup->RadioButton1, XY ); // Top view
128 //   myViewButtonGroup->addButton( myViewGroup->RadioButton2, YZ ); // Front View
129 //   myViewButtonGroup->addButton( myViewGroup->RadioButton3, XZ ); // Left View
130 //   
131 //   myViewGroup->hide();
132   
133   mySelectionGroup = new QGroupBox(tr("GEOM_DETECT"), centralWidget());
134   QGridLayout* mySelectGrpLayout = new QGridLayout(mySelectionGroup);
135   myPushButton = new QPushButton(mySelectionGroup);
136   myPushButton->setIcon(image0);
137   myPushButton->setCheckable(true);
138   
139   mySelButton = new QPushButton(mySelectionGroup);
140   mySelButton->setIcon(image1);
141   myLineEdit = new QLineEdit(mySelectionGroup);
142   
143   mySnapshotLabel          = new QLabel(mySelectionGroup);
144   QFrame* myImgSampleFrame = new QFrame(mySelectionGroup);
145 //   myImgSampleFrame->setFrameRect(QRect(0,0,10,10));
146   myImgSampleFrame->setFrameStyle(QFrame::Box);
147   QLabel* myPictureLabel   = new QLabel(tr( "GEOM_PICTURE" ),mySelectionGroup);
148   mySelectGrpLayout->addWidget(myPictureLabel,   0, 0);
149   mySelectGrpLayout->addWidget(mySelButton,      0, 1);
150   mySelectGrpLayout->addWidget(myLineEdit,       0, 2);// 1, 2);
151   
152   mySelectGrpLayout->addWidget(mySnapshotLabel,  1, 0);
153   mySelectGrpLayout->addWidget(myPushButton,     1, 1);
154   mySelectGrpLayout->addWidget(myImgSampleFrame, 1, 2);
155 //   mySelectGrpLayout->setColumnStretch(3, 1);
156   
157   QGridLayout* myFrameLayout = new QGridLayout(myImgSampleFrame);
158   myImgSampleLabel           = new QLabel(myImgSampleFrame);
159   myFrameLayout->addWidget(myImgSampleLabel,     0, 0);
160   
161   myOutputGroup = new DlgRef_3Radio(centralWidget());
162   myOutputGroup->GroupBox1->setTitle(tr("GEOM_DETECT_OUTPUT"));
163   myOutputGroup->RadioButton2->setText(tr( "GEOM_POLYLINE"  ));
164   myOutputGroup->RadioButton1->setText(tr( "GEOM_SPLINE"));
165   myOutputGroup->RadioButton3->hide();
166   
167 //   myOutputGroup->hide(); //caché pour la demo
168     
169   QVBoxLayout* layout = new QVBoxLayout(centralWidget());
170   layout->setMargin(0); layout->setSpacing(6);
171 //   layout->addWidget( myViewGroup);
172   layout->addWidget( mySelectionGroup);
173   layout->addWidget( myOutputGroup);
174   
175 //   mainFrame()->GroupBoxName->hide();
176   
177   // Build an instance of detection used to perform image processing operations
178   aDetector = new ShapeRec_FeatureDetector();
179   
180   Init();
181   
182 }
183
184 //=================================================================================
185 // function : ~EntityGUI_FeatureDetectorDlg()
186 // purpose  : Destroys the object and frees any allocated resources
187 //=================================================================================
188 EntityGUI_FeatureDetectorDlg::~EntityGUI_FeatureDetectorDlg()
189 {
190   
191 }
192
193 //=================================================================================
194 // function : Init()
195 // purpose  :
196 //=================================================================================
197 void EntityGUI_FeatureDetectorDlg::Init()
198 {
199   connect( myGeomGUI,         SIGNAL( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) );
200   connect( buttonOk(),        SIGNAL( clicked() ),               this, SLOT( ClickOnOk() ) );
201   connect( buttonApply(),     SIGNAL( clicked() ),               this, SLOT( ClickOnApply() ) );
202  
203   connect( this,              SIGNAL(constructorsClicked(int)),  this, SLOT(ConstructorsClicked(int)));
204   
205   connect( myPushButton,      SIGNAL( toggled( bool ) ),         this, SLOT( onButtonToggled( bool ) ) );
206   connect( mySelButton,       SIGNAL( clicked() ),               this, SLOT( SetEditCurrentArgument() ) );
207   
208 //   connect( myViewButtonGroup, SIGNAL( buttonClicked( int ) ),    this, SLOT( onViewClicked( int ) ) );
209   
210   connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),this, SLOT( SelectionIntoArgument() ) );
211   
212   myConstructorId = 0;
213   
214   mySelButton->click();
215   mySelButton->setDown(true);
216   
217 //   SetEditCurrentArgument();
218   SelectionIntoArgument();
219   
220   initName(tr("GEOM_CONTOURS")); 
221   resize(100,100);
222   
223 //   myViewGroup->RadioButton1->setChecked(true);
224   myOutputGroup->RadioButton1->setChecked(true);
225   
226   gp_Pnt aOrigin = gp_Pnt(0, 0, 0);
227   gp_Dir aDirZ = gp_Dir(0, 0, 1);
228   gp_Dir aDirX = gp_Dir(1, 0, 0);
229   
230   aGlobalCS = gp_Ax3(aOrigin, aDirZ, aDirX);
231   
232   myStartPnt = gp_Pnt(0,0,0);
233   myEndPnt = myStartPnt;
234   
235   myGeomGUI->SetWorkingPlane( aGlobalCS );
236   myGeomGUI->ActiveWorkingPlane();
237   
238   ConstructorsClicked(myConstructorId);
239 }
240
241 //=================================================================================
242 // function : SetEditCurrentArgument()
243 // purpose  :
244 //=================================================================================
245 void EntityGUI_FeatureDetectorDlg::SetEditCurrentArgument()
246 {
247   QPushButton* send = (QPushButton*)sender();
248   if ( send == mySelButton ) { 
249     myEditCurrentArgument = myLineEdit;
250     myLineEdit->setEnabled(true);
251   }
252   else 
253     myLineEdit->setEnabled(false);
254   send->setDown(true);
255 }
256
257 //=================================================================================
258 // function : SelectionIntoArgument()
259 // purpose  : Called when selection as changed or other case
260 //=================================================================================
261 void EntityGUI_FeatureDetectorDlg::SelectionIntoArgument()
262 {
263   
264   SUIT_ViewWindow*       theViewWindow  = getDesktop()->activeWindow();
265   std::map< std::string , std::vector<Handle(AIS_InteractiveObject)> >::iterator AISit;
266   SOCC_Viewer* soccViewer = (SOCC_Viewer*)(theViewWindow->getViewManager()->getViewModel());
267
268   if (!myEditCurrentArgument->isEnabled())
269     return;
270   
271   myEditCurrentArgument->setText( "" );
272
273   LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
274   SALOME_ListIO aSelList;
275   aSelMgr->selectedObjects(aSelList);
276   SALOME_ListIteratorOfListIO anIt( aSelList );
277   
278   for( ; anIt.More(); anIt.Next() )
279     if( !anIt.Value().IsNull() )
280     {
281       myFaceEntry = anIt.Value()->getEntry();
282     }
283
284   if (aSelList.Extent() != 1) {
285     if (myEditCurrentArgument == myLineEdit) 
286       myFace.nullify();
287     return;
288   }
289
290   TopAbs_ShapeEnum aNeedType = TopAbs_FACE ;
291   GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
292   TopoDS_Shape aShape;
293   if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
294     QString aName = GEOMBase::GetName( aSelectedObject.get() );
295     myEditCurrentArgument->setText( aName );
296     
297     if ( myEditCurrentArgument == myLineEdit ) {
298       myFace = aSelectedObject;
299       AISit = soccViewer->entry2aisobjects.find(myFaceEntry.toStdString());
300       if (AISit == soccViewer->entry2aisobjects.end())
301         return;
302       
303       Handle(AIS_InteractiveObject) myAIS = (*AISit).second[0];
304       Handle(GEOM_AISShape) myAISShape;
305       if( myAIS->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
306         myAISShape = Handle(GEOM_AISShape)::DownCast( myAIS );
307       }
308       else
309         return ;
310       
311       std::string theImgFileName = myAISShape->TextureFile();      
312       if ( theImgFileName == "" )
313         return ;
314
315       aDetector->SetPath( theImgFileName );   
316     } 
317   }
318   
319 }
320
321 //=================================================================================
322 // function : acceptMouseEvent()
323 // purpose  :
324 //=================================================================================
325 bool EntityGUI_FeatureDetectorDlg::acceptMouseEvent() const
326
327   return myPushButton->isChecked();  
328 }
329
330 //=======================================================================
331 // function : ClickOnOk()
332 // purpose  :
333 //=======================================================================
334 void EntityGUI_FeatureDetectorDlg::ClickOnOk()
335 {
336   setIsApplyAndClose( true );
337   if (onAccept())
338     ClickOnCancel();
339 }
340
341 //=================================================================================
342 // function : ClickOnApply()
343 // purpose  :
344 //=================================================================================
345 bool EntityGUI_FeatureDetectorDlg::ClickOnApply()
346 {
347   if (!onAccept())
348     return false;
349
350 //   initName();
351   ConstructorsClicked(getConstructorId());
352   return true;
353 }
354
355 //=================================================================================
356 // function : onConstructorClicked(int)
357 // purpose  :
358 //=================================================================================
359 void EntityGUI_FeatureDetectorDlg::ConstructorsClicked(int id)
360 {
361   MESSAGE("Constructor id ="<<id)
362   myConstructorId = id;
363   switch (id)
364   {
365     case CORNERS:
366 //       myViewGroup->show();
367 //       mySelectionGroup->show();
368       myOutputGroup->hide();
369       mySnapshotLabel->setText(tr("GEOM_DETECT_ZONE"));
370       initName(tr("GEOM_CORNERS"));
371       break;
372     case CONTOURS:
373 //       myViewGroup->hide();
374 //       mySelectionGroup->hide();
375 //       mySelectionGroup->show();
376       myOutputGroup->show();
377       mySnapshotLabel->setText(tr("GEOM_COLOR_FILTER"));
378       initName(tr("GEOM_CONTOURS"));
379       break;
380     case LINES:
381 //       myViewGroup->hide();
382 //       mySelectionGroup->hide();
383 //       mySelectionGroup->show();
384       myOutputGroup->hide();
385       mySnapshotLabel->setText(tr(""));
386       initName(tr("GEOM_LINES"));
387       break;
388   }
389 }
390
391 // //=================================================================================
392 // // function : onViewClicked()
393 // // purpose  :
394 // //=================================================================================
395 // void EntityGUI_FeatureDetectorDlg::onViewClicked(int id)
396 // {
397 //   gp_Pnt aOrigin = gp_Pnt(0, 0, 0);
398 //   gp_Dir aDirZ;
399 //   gp_Dir aDirX;
400 //   
401 //   switch(id)
402 //   {
403 //     case XY:
404 //         aDirZ = gp_Dir(0, 0, 1);
405 //         aDirX = gp_Dir(1, 0, 0);
406 //       break;
407 //     case YZ:
408 //         aDirZ = gp_Dir(1, 0, 0);
409 //         aDirX = gp_Dir(0, 1, 0);
410 //       break;
411 //     case XZ:
412 //         aDirZ = gp_Dir(0, -1, 0);
413 //         aDirX = gp_Dir(1, 0 , 0);
414 //       break;
415 //   }
416 //   
417 //   myWPlane = gp_Ax3(aOrigin, aDirZ, aDirX);
418 //   myGeomGUI->SetWorkingPlane( myWPlane );
419 //   myGeomGUI->ActiveWorkingPlane();
420 //   
421 // }
422
423 //=================================================================================
424 // function : onButtonToggled()
425 // purpose  :
426 //=================================================================================
427 void EntityGUI_FeatureDetectorDlg::onButtonToggled( bool checked)
428 {
429   if (!checked)
430   {
431     myStartPnt = gp_Pnt(0,0,0);
432     myEndPnt = myStartPnt;
433     myLineEdit->setEnabled(true);
434   }
435   else
436   {
437     myLineEdit->setEnabled(false);
438   }
439 }
440
441 //=================================================================================
442 // function : setStartPnt( const QPoint& )
443 // purpose  :
444 //=================================================================================
445 void EntityGUI_FeatureDetectorDlg::setStartPnt(const gp_Pnt& theStartPnt)
446 {
447   myStartPnt = theStartPnt;
448   MESSAGE("myStartPnt = ("<<theStartPnt.X()<<", "<<theStartPnt.Y()<<")")
449 }
450
451 //=================================================================================
452 // function : setEndPnt( const QPoint& )
453 // purpose  :
454 //=================================================================================
455 void EntityGUI_FeatureDetectorDlg::setEndPnt(const gp_Pnt& theEndPnt)
456 {
457   myEndPnt = theEndPnt;
458   MESSAGE("myEndPnt = ("<<theEndPnt.X()<<", "<<theEndPnt.Y()<<")")
459   showImageSample();
460 }
461
462 //=================================================================================
463 // function : showImageSample()
464 // purpose  :
465 //=================================================================================
466 void EntityGUI_FeatureDetectorDlg::showImageSample()
467 {  
468   int height            =  aDetector->GetImgHeight();
469   int width             =  aDetector->GetImgWidth();
470   
471   // Operations to display the corners properly in the 3D scene
472   double pictureLeft  = -0.5 * width;   // X coordinate of the top left  corner of the background image in the view
473   double pictureTop   =  0.5 * height;  // Y coordinate of both top corners
474   
475   // Set detection rectangle in the background image coordinates system
476   QPoint topLeft     = QPoint(myStartPnt.X() - pictureLeft, pictureTop - myStartPnt.Y());
477   QPoint bottomRight = QPoint(myEndPnt.X()   - pictureLeft, pictureTop - myEndPnt.Y());
478   QRect aRect = QRect(topLeft, bottomRight);
479   
480   if( !aRect.isEmpty() && aRect.width() > 1 )
481   {
482     aDetector->SetROI( aRect );
483     std::string samplePicturePath = aDetector->CroppImage();
484     MESSAGE("samplePicturePath = "<<samplePicturePath)
485     QPixmap pixmap(QString(samplePicturePath.c_str()));
486     myImgSampleLabel->setPixmap(pixmap);
487     myImgSampleLabel->setMask(pixmap.mask());
488   }
489 }
490
491
492
493 //=================================================================================
494 // function : createOperation
495 // purpose  :
496 //=================================================================================
497 GEOM::GEOM_IOperations_ptr EntityGUI_FeatureDetectorDlg::createOperation()
498 {
499   return myGeomGUI->GetGeomGen()->GetIShapesOperations( getStudyId() );
500 }
501
502 //=================================================================================
503 // function : execute()
504 // purpose  :
505 //=================================================================================
506 bool EntityGUI_FeatureDetectorDlg::execute( ObjectList& objects )
507 {
508   bool res = false;
509   
510 //   SUIT_ViewWindow*       theViewWindow  = getDesktop()->activeWindow();
511 //   std::map< std::string , std::vector<Handle(AIS_InteractiveObject)> >::iterator AISit;
512 //   SOCC_Viewer* soccViewer = (SOCC_Viewer*)(theViewWindow->getViewManager()->getViewModel());
513   
514 //   AISit = soccViewer->entry2aisobjects.find(myFaceEntry.toStdString());
515 //   if (AISit == soccViewer->entry2aisobjects.end())
516 //     return res;
517 //   
518 //   Handle(AIS_InteractiveObject) myAIS = (*AISit).second[0];
519 //   Handle(GEOM_AISShape) myAISShape;
520 //   if( myAIS->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
521 //     myAISShape = Handle(GEOM_AISShape)::DownCast( myAIS );
522 //   }
523 //   else
524 //     return res;
525 //   
526 //   std::string theImgFileName = myAISShape->TextureFile();
527 //     
528 //   if ( theImgFileName == "" )
529 //     return res;
530 //   
531 //   aDetector->SetPath( theImgFileName );
532     
533   int height            =  aDetector->GetImgHeight();
534   int width             =  aDetector->GetImgWidth();
535   
536   // Operations to display the corners properly in the 3D scene
537   double pictureLeft  = -0.5 * width;   // X coordinate of the top left  corner of the background image in the view
538   double pictureTop   =  0.5 * height;  // Y coordinate of both top corners
539   
540   // Set detection rectangle in the background image coordinates system
541   QPoint topLeft     = QPoint(myStartPnt.X() - pictureLeft, pictureTop - myStartPnt.Y());
542   QPoint bottomRight = QPoint(myEndPnt.X()   - pictureLeft, pictureTop - myEndPnt.Y());
543   QRect aRect = QRect(topLeft, bottomRight);
544   
545   GEOM::GEOM_IBasicOperations_var  aBasicOperations  = myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
546   GEOM::GEOM_IShapesOperations_var aShapesOperations = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
547   
548   if (myConstructorId == CORNERS)
549   {
550     if( !aRect.isEmpty() )
551     {
552       aDetector->SetROI( aRect );
553       pictureLeft  =  myStartPnt.X();                
554       pictureTop   =  myStartPnt.Y();
555     }
556     aDetector->ComputeCorners();
557     CvPoint2D32f* corners     = aDetector->GetCorners();
558     int cornerCount           = aDetector->GetCornerCount();
559     int i;
560     
561     // Build the geom objects associated to the detected corners and returned by execute   
562     if( !aBasicOperations->_is_nil() && !aShapesOperations->_is_nil() ) 
563     {
564       GEOM::GEOM_Object_var  aGeomCorner;
565       GEOM::ListOfGO_var     geomCorners = new GEOM::ListOfGO();
566       geomCorners->length( cornerCount );
567       for (i = 0; i < cornerCount; i++)
568       {
569         double x = pictureLeft + corners[i].x;
570         double y = pictureTop  - corners[i].y;
571         double z =  0;
572         
573         aGeomCorner = aBasicOperations->MakePointXYZ( x,y,z );
574         
575         geomCorners[i] = aGeomCorner;  
576       } 
577       GEOM::GEOM_Object_var aCompound = aShapesOperations->MakeCompound(geomCorners);    
578       if ( !aCompound->_is_nil() )
579       {
580         // Dark blue color
581         SALOMEDS::Color aColor;
582         aColor.R = 0;
583         aColor.G = 0;
584         aColor.B = 0.8;
585         
586         aCompound->SetColor(aColor);
587         aCompound->SetMarkerStd(GEOM::MT_POINT,GEOM::MS_30);
588         objects.push_back( aCompound._retn() );
589         res = true;
590       }
591     }
592   }
593   else if (myConstructorId == CONTOURS)
594   {
595     int method = 0 ; //CANNY
596     if( !aRect.isEmpty() && aRect.width() > 1 )
597     {
598       aDetector->SetROI( aRect );
599       method = 1 ; //COLORFILTER    
600     }
601     
602     GEOM::GEOM_ICurvesOperations_var aCurveOperations = myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
603     
604     aDetector->ComputeContours( method );
605     std::vector< std::vector<cv::Point> >   contours  = aDetector->GetContours();
606     std::vector<cv::Vec4i>                  hierarchy = aDetector->GetContoursHierarchy();
607     
608     std::vector< cv::Point >                contour;
609     int idx = 0;
610     
611     GEOM::ListOfGO_var                      geomContours = new GEOM::ListOfGO();
612     int contourCount = 0;
613     
614     bool insert;
615     
616     MESSAGE("hierarchy.size() =" << hierarchy.size()) 
617     for( ; idx >= 0; idx = hierarchy[idx][0])
618     {
619 //       for(int count=0, child=idx; child>=0, count<1; child=hierarchy[idx][2], count++)
620 //       {     
621 //         contour = contours[child];
622         contour = contours[idx];
623         std::vector< cv::Point >::iterator it;
624         std::vector< cv::Point >::iterator it_previous;
625         std::vector< cv::Point >::iterator it_next;
626         GEOM::GEOM_Object_var  aGeomContourPnt;
627         GEOM::ListOfGO_var     geomContourPnts = new GEOM::ListOfGO();
628        
629         geomContourPnts->length( contour.size() );
630
631         int j = 0;
632         std::set< std::vector<int> > existing_points;
633         std::pair< std::set< std::vector<int> >::iterator,bool > pnt_it;
634         for ( it=contour.begin() ; it < contour.end(); it++ )
635         {
636           int pnt_array[] = {it->x,it->y};     
637           std::vector<int> pnt (pnt_array, pnt_array + sizeof(pnt_array) / sizeof(int) );
638
639           pnt_it=existing_points.insert(pnt);
640           if (pnt_it.second == true)         // To avoid double points in the contours
641           {
642             insert = true;
643             if (it!=contour.begin())         // From the second point on perform some checking to avoid loops in the contours we build
644             {
645               it_previous = boost::prior(it);
646               it_next = boost::next(it);
647               
648               double u_v_scalar_product = (it->x - it_previous->x) * (it_next->x - it->x) + 
649                                           (it->y - it_previous->y) * (it_next->y - it->y);                                       
650               if (u_v_scalar_product < 0)
651               {
652                 double u_v_det = (it->x - it_previous->x) * (it_next->y - it->y) - 
653                                  (it->y - it_previous->y) * (it_next->x - it->x);
654                                           
655                 double norme_u = sqrt ( (it->x - it_previous->x)*(it->x - it_previous->x) +
656                                         (it->y - it_previous->y)*(it->y - it_previous->y) );
657                 
658                 double norme_v = sqrt ( (it->x - it_next->x)*(it->x - it_next->x) +
659                                         (it->y - it_next->y)*(it->y - it_next->y) );
660                                                                                                 
661                 double u_v_sinus = u_v_det / (norme_u * norme_v);
662                 
663                 if (fabs(u_v_sinus) < Precision::Confusion())
664                 { 
665                   insert = false;
666                 }                         
667               }
668             }
669             double x = -0.5 *width  + it->x;
670             double y =  0.5 *height - it->y;
671             double z =  0;
672             if (insert)
673             {
674               aGeomContourPnt    = aBasicOperations->MakePointXYZ( x,y,z );
675               geomContourPnts->length( j+1 );
676               geomContourPnts[j] = aGeomContourPnt;
677               j++;
678             }
679           }
680         }
681         
682         GEOM::GEOM_Object_var aWire;
683         if(myOutputGroup->RadioButton2->isChecked())
684         {
685           aWire = aCurveOperations->MakePolyline(geomContourPnts.in(), false);
686         }
687         else if(myOutputGroup->RadioButton1->isChecked())
688         {
689           aWire = aCurveOperations->MakeSplineInterpolation(geomContourPnts.in(), /*closed =*/ false, /*reordering =*/ false);
690         }
691         else
692           return res;
693         
694         if ( !aWire->_is_nil() )
695         {
696           geomContours->length(contourCount + 1);
697           geomContours[contourCount] = aWire;
698           contourCount++;
699         }
700 //       }
701     }
702     GEOM::GEOM_Object_var aContoursCompound = aShapesOperations->MakeCompound(geomContours);
703     if ( !aContoursCompound->_is_nil() )
704     {
705       objects.push_back( aContoursCompound._retn() );
706     }
707     res=true;
708   }
709   
710   else if(myConstructorId ==LINES)
711   {
712     aDetector->ComputeLines();
713     std::vector<cv::Vec4i>  lines = aDetector->GetLines();
714     GEOM::GEOM_Object_var  Pnt1;
715     GEOM::GEOM_Object_var  Pnt2;
716     GEOM::GEOM_Object_var  aLine;
717     
718     GEOM::ListOfGO_var     geomLines = new GEOM::ListOfGO();
719     int linesCount=0;
720     for( int i = 0; i < lines.size(); i++ )
721     {
722       Pnt1 = aBasicOperations->MakePointXYZ( -0.5 *width + lines[i][0], 0.5 *height - lines[i][1], 0 );
723       Pnt2 = aBasicOperations->MakePointXYZ( -0.5 *width + lines[i][2], 0.5 *height - lines[i][3], 0 );
724       aLine = aBasicOperations->MakeLineTwoPnt( Pnt1, Pnt2 );
725       if ( !aLine->_is_nil() )
726       {
727         geomLines->length(linesCount + 1);
728         geomLines[linesCount] = aLine;
729         linesCount++;
730       }
731     }
732     GEOM::GEOM_Object_var aLinesCompound = aShapesOperations->MakeCompound(geomLines);
733     if ( !aLinesCompound->_is_nil() )
734     {
735       objects.push_back( aLinesCompound._retn() );
736     }
737
738     res=true;
739   }
740   
741   return res;
742 }