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