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