1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File : EntityGUI_SketcherDlg.cxx
25 // Author : Renaud NEDELEC, Open CASCADE S.A.S.
28 #include "EntityGUI_FeatureDetectorDlg.h"
29 #include <ShapeRec_FeatureDetector.hxx>
31 #include <OCCViewer_ViewWindow.h>
32 #include <OCCViewer_ViewManager.h>
34 #include <SOCC_ViewModel.h>
37 #include <GeometryGUI.h>
38 #include <EntityGUI.h>
40 #include <GEOM_Object.hxx>
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>
50 #include <SALOME_ListIteratorOfListIO.hxx>
52 #include <SalomeApp_Study.h>
54 #include <utilities.h>
55 #include <Precision.hxx>
59 #include <TopoDS_Shape.hxx>
60 #include <TopoDS_Wire.hxx>
62 #include <BRepBuilderAPI_MakeVertex.hxx>
63 #include <BRepBuilderAPI_MakeFace.hxx>
64 #include <BRepBuilderAPI_MakePolygon.hxx>
65 #include <BRepBuilderAPI_Transform.hxx>
67 #include <AIS_TexturedShape.hxx>
68 #include <TCollection_AsciiString.hxx>
69 #include <StdSelect_DisplayMode.hxx>
71 #include <Graphic3d_MaterialAspect.hxx>
78 #include <boost/utility.hpp>
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)
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")));
115 setWindowTitle(tr("GEOM_DETECT_TITLE"));
117 /***************************************************************/
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();
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
135 // myViewGroup->hide();
137 // Widgets for the selection of the picture and the Region Of Interest
140 mySelectionGroup = new DlgRef_1Sel1Frame(centralWidget());
142 mySelectionGroup->PushButton1->setIcon(image0);
143 mySelectionGroup->PushButton1->setCheckable(true);
144 mySelectionGroup->PushButton1->setAutoExclusive(true);
146 mySelectionGroup->PushButton2->setIcon(image1);
147 mySelectionGroup->PushButton2->setCheckable(true);
148 mySelectionGroup->PushButton2->setAutoExclusive(true);
150 mySelectionGroup->TextLabel1->setText(tr( "GEOM_PICTURE" ));
151 mySelectionGroup->FrameLabel->setText("");
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();
159 // myOutputGroup->hide(); //caché pour la demo
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);
167 // mainFrame()->GroupBoxName->hide();
169 // Build an instance of detection used to perform image processing operations
170 aDetector = new ShapeRec_FeatureDetector();
172 setHelpFileName( "shape_recognition_page.html" );
178 //=================================================================================
179 // function : ~EntityGUI_FeatureDetectorDlg()
180 // purpose : Destroys the object and frees any allocated resources
181 //=================================================================================
182 EntityGUI_FeatureDetectorDlg::~EntityGUI_FeatureDetectorDlg()
187 //=================================================================================
190 //=================================================================================
191 void EntityGUI_FeatureDetectorDlg::Init()
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() ) );
203 mySelectionGroup->PushButton1->click();
205 // SetEditCurrentArgument();
206 SelectionIntoArgument();
208 initName(tr("GEOM_CONTOURS"));
211 // myViewGroup->RadioButton1->setChecked(true);
212 myOutputGroup->RadioButton1->setChecked(true);
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);
218 aGlobalCS = gp_Ax3(aOrigin, aDirZ, aDirX);
220 myStartPnt = gp_Pnt(0,0,0);
221 myEndPnt = myStartPnt;
223 myGeomGUI->SetWorkingPlane( aGlobalCS );
224 myGeomGUI->ActiveWorkingPlane();
226 ConstructorsClicked(myConstructorId);
229 //=================================================================================
230 // function : SelectionIntoArgument()
231 // purpose : Called when selection as changed or other case
232 //=================================================================================
233 void EntityGUI_FeatureDetectorDlg::SelectionIntoArgument()
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());
241 if (!myEditCurrentArgument->isEnabled())
244 myEditCurrentArgument->setText( "" );
246 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
247 SALOME_ListIO aSelList;
248 aSelMgr->selectedObjects(aSelList);
249 SALOME_ListIteratorOfListIO anIt( aSelList );
251 for( ; anIt.More(); anIt.Next() )
252 if( !anIt.Value().IsNull() )
254 myFaceEntry = anIt.Value()->getEntry();
257 if (aSelList.Extent() != 1) {
258 if (myEditCurrentArgument == mySelectionGroup->LineEdit1)
263 TopAbs_ShapeEnum aNeedType = TopAbs_FACE ;
264 GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
266 if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
267 QString aName = GEOMBase::GetName( aSelectedObject.get() );
268 myEditCurrentArgument->setText( aName );
270 if ( myEditCurrentArgument == mySelectionGroup->LineEdit1 ) {
271 myFace = aSelectedObject;
272 AISit = soccViewer->entry2aisobjects.find(myFaceEntry.toStdString());
273 if (AISit == soccViewer->entry2aisobjects.end())
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 );
284 std::string theImgFileName = myAISShape->TextureFile();
285 if ( theImgFileName == "" )
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
300 //=================================================================================
301 // function : acceptMouseEvent()
303 //=================================================================================
304 bool EntityGUI_FeatureDetectorDlg::acceptMouseEvent() const
306 return mySelectionGroup->PushButton2->isChecked();
309 //=======================================================================
310 // function : ClickOnOk()
312 //=======================================================================
313 void EntityGUI_FeatureDetectorDlg::ClickOnOk()
315 setIsApplyAndClose( true );
320 //=================================================================================
321 // function : ClickOnApply()
323 //=================================================================================
324 bool EntityGUI_FeatureDetectorDlg::ClickOnApply()
330 ConstructorsClicked(getConstructorId());
334 //=================================================================================
335 // function : onConstructorClicked(int)
337 //=================================================================================
338 void EntityGUI_FeatureDetectorDlg::ConstructorsClicked(int id)
340 MESSAGE("Constructor id ="<<id)
341 myConstructorId = id;
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"));
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"));
366 // myViewGroup->hide();
367 // mySelectionGroup->hide();
368 // mySelectionGroup->show();
369 myOutputGroup->hide();
370 mySelectionGroup->TextLabel2->setText(tr(""));
371 initName(tr("GEOM_LINES"));
376 // //=================================================================================
377 // // function : onViewClicked()
379 // //=================================================================================
380 // void EntityGUI_FeatureDetectorDlg::onViewClicked(int id)
382 // gp_Pnt aOrigin = gp_Pnt(0, 0, 0);
389 // aDirZ = gp_Dir(0, 0, 1);
390 // aDirX = gp_Dir(1, 0, 0);
393 // aDirZ = gp_Dir(1, 0, 0);
394 // aDirX = gp_Dir(0, 1, 0);
397 // aDirZ = gp_Dir(0, -1, 0);
398 // aDirX = gp_Dir(1, 0 , 0);
402 // myWPlane = gp_Ax3(aOrigin, aDirZ, aDirX);
403 // myGeomGUI->SetWorkingPlane( myWPlane );
404 // myGeomGUI->ActiveWorkingPlane();
408 //=================================================================================
409 // function : onButtonClicked()
411 //=================================================================================
412 void EntityGUI_FeatureDetectorDlg::onButtonClicked()
414 QPushButton* send = (QPushButton*)sender();
415 if (send == mySelectionGroup->PushButton2)
417 mySelectionGroup->LineEdit1->setEnabled(false);
419 else if (send == mySelectionGroup->PushButton1)
421 myStartPnt = gp_Pnt(0,0,0);
422 myEndPnt = myStartPnt;
423 myEditCurrentArgument = mySelectionGroup->LineEdit1;
424 mySelectionGroup->LineEdit1->setEnabled(true);
428 //=================================================================================
429 // function : setStartPnt( const QPoint& )
431 //=================================================================================
432 void EntityGUI_FeatureDetectorDlg::setStartPnt(const gp_Pnt& theStartPnt)
434 myStartPnt = theStartPnt;
435 MESSAGE("myStartPnt = ("<<theStartPnt.X()<<", "<<theStartPnt.Y()<<")")
438 //=================================================================================
439 // function : setEndPnt( const QPoint& )
441 //=================================================================================
442 void EntityGUI_FeatureDetectorDlg::setEndPnt(const gp_Pnt& theEndPnt)
444 myEndPnt = theEndPnt;
445 MESSAGE("myEndPnt = ("<<theEndPnt.X()<<", "<<theEndPnt.Y()<<")")
446 if (setSelectionRect() && aDetector->GetImgHeight() > 0)
450 //=================================================================================
451 // function : setSelectionRect()
453 //=================================================================================
454 bool EntityGUI_FeatureDetectorDlg::setSelectionRect()
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() );
462 QPoint topLeft = QPoint(left - pictureLeft, pictureTop - top );
463 QPoint bottomRight = QPoint(right - pictureLeft, pictureTop - bottom);
465 myRect = QRect(topLeft, bottomRight);
467 return (!myRect.isEmpty() && myRect.width() > 1);
470 //=================================================================================
471 // function : showImageSample()
472 // purpose : Display a preview of the image sample selected by the user
473 //=================================================================================
474 void EntityGUI_FeatureDetectorDlg::showImageSample()
476 // Cropp the image to the selection rectangle given by the user
477 aDetector->SetROI( myRect );
478 std::string samplePicturePath = aDetector->CroppImage();
480 // Display the result
481 QPixmap pixmap(QString(samplePicturePath.c_str()));
482 mySelectionGroup->FrameLabel->setPixmap(pixmap);
483 mySelectionGroup->FrameLabel->setMask(pixmap.mask());
486 //=================================================================================
487 // function : createOperation
489 //=================================================================================
490 GEOM::GEOM_IOperations_ptr EntityGUI_FeatureDetectorDlg::createOperation()
492 return myGeomGUI->GetGeomGen()->GetIShapesOperations( getStudyId() );
495 //=================================================================================
496 // function : execute()
498 //=================================================================================
499 bool EntityGUI_FeatureDetectorDlg::execute( ObjectList& objects )
503 GEOM::GEOM_IBasicOperations_var aBasicOperations = myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
504 GEOM::GEOM_IShapesOperations_var aShapesOperations = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
506 if (myConstructorId == CORNERS)
508 double subPictureLeft;
509 double subPictureTop;
510 if( !myRect.isEmpty() )
512 aDetector->SetROI( myRect );
513 subPictureLeft = myStartPnt.X();
514 subPictureTop = myStartPnt.Y();
518 subPictureLeft = pictureLeft;
519 subPictureTop = pictureTop;
521 aDetector->ComputeCorners();
522 CvPoint2D32f* corners = aDetector->GetCorners();
523 int cornerCount = aDetector->GetCornerCount();
526 // Build the geom objects associated to the detected corners and returned by execute
527 if( !aBasicOperations->_is_nil() && !aShapesOperations->_is_nil() )
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++)
534 double x = subPictureLeft + corners[i].x;
535 double y = subPictureTop - corners[i].y;
538 aGeomCorner = aBasicOperations->MakePointXYZ( x,y,z );
540 geomCorners[i] = aGeomCorner;
542 GEOM::GEOM_Object_var aCompound = aShapesOperations->MakeCompound(geomCorners);
543 if ( !aCompound->_is_nil() )
546 SALOMEDS::Color aColor;
551 aCompound->SetColor(aColor);
552 aCompound->SetMarkerStd(GEOM::MT_POINT,GEOM::MS_30);
553 objects.push_back( aCompound._retn() );
558 else if (myConstructorId == CONTOURS)
560 int method = 0 ; //CANNY
561 if( !myRect.isEmpty() && myRect.width() > 1 )
563 aDetector->SetROI( myRect );
564 method = 1 ; //COLORFILTER
567 GEOM::GEOM_ICurvesOperations_var aCurveOperations = myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
569 aDetector->ComputeContours( method );
570 std::vector< std::vector<cv::Point> > contours = aDetector->GetContours();
571 std::vector<cv::Vec4i> hierarchy = aDetector->GetContoursHierarchy();
573 std::vector< cv::Point > contour;
576 GEOM::ListOfGO_var geomContours = new GEOM::ListOfGO();
577 int contourCount = 0;
581 MESSAGE("hierarchy.size() =" << hierarchy.size())
582 for( ; idx >= 0; idx = hierarchy[idx][0])
584 // for(int count=0, child=idx; child>=0, count<1; child=hierarchy[idx][2], count++)
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();
594 geomContourPnts->length( contour.size() );
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++ )
601 int pnt_array[] = {it->x,it->y};
602 std::vector<int> pnt (pnt_array, pnt_array + sizeof(pnt_array) / sizeof(int) );
604 pnt_it=existing_points.insert(pnt);
605 if (pnt_it.second == true) // To avoid double points in the contours
608 if (it!=contour.begin()) // From the second point on perform some checking to avoid loops in the contours we build
610 it_previous = boost::prior(it);
611 it_next = boost::next(it);
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)
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);
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) );
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) );
626 double u_v_sinus = u_v_det / (norme_u * norme_v);
628 if (fabs(u_v_sinus) < Precision::Confusion())
634 double x = -0.5 *width + it->x;
635 double y = 0.5 *height - it->y;
639 aGeomContourPnt = aBasicOperations->MakePointXYZ( x,y,z );
640 geomContourPnts->length( j+1 );
641 geomContourPnts[j] = aGeomContourPnt;
647 GEOM::GEOM_Object_var aWire;
648 if(myOutputGroup->RadioButton2->isChecked())
650 aWire = aCurveOperations->MakePolyline(geomContourPnts.in(), false);
652 else if(myOutputGroup->RadioButton1->isChecked())
654 aWire = aCurveOperations->MakeSplineInterpolation(geomContourPnts.in(), /*closed =*/ false, /*reordering =*/ false);
659 if ( !aWire->_is_nil() )
661 geomContours->length(contourCount + 1);
662 geomContours[contourCount] = aWire;
667 GEOM::GEOM_Object_var aContoursCompound = aShapesOperations->MakeCompound(geomContours);
668 if ( !aContoursCompound->_is_nil() )
670 objects.push_back( aContoursCompound._retn() );
675 // TEST not very conclusive
677 // else if(myConstructorId ==LINES)
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;
685 // GEOM::ListOfGO_var geomLines = new GEOM::ListOfGO();
687 // for( int i = 0; i < lines.size(); i++ )
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() )
694 // geomLines->length(linesCount + 1);
695 // geomLines[linesCount] = aLine;
699 // GEOM::GEOM_Object_var aLinesCompound = aShapesOperations->MakeCompound(geomLines);
700 // if ( !aLinesCompound->_is_nil() )
702 // objects.push_back( aLinesCompound._retn() );