1 // Copyright (C) 2007-2011 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.
27 #include "EntityGUI_FeatureDetectorDlg.h"
28 #include <ShapeRec_FeatureDetector.hxx>
30 #include <OCCViewer_ViewWindow.h>
31 #include <OCCViewer_ViewManager.h>
33 #include <SOCC_ViewModel.h>
36 #include <GeometryGUI.h>
37 #include <EntityGUI.h>
39 #include <GEOM_Object.hxx>
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>
49 #include <SALOME_ListIteratorOfListIO.hxx>
51 #include <SalomeApp_Study.h>
53 #include <utilities.h>
56 #include <TopoDS_Shape.hxx>
57 #include <TopoDS_Wire.hxx>
59 #include <BRepBuilderAPI_MakeVertex.hxx>
60 #include <BRepBuilderAPI_MakeFace.hxx>
61 #include <BRepBuilderAPI_MakePolygon.hxx>
62 #include <BRepBuilderAPI_Transform.hxx>
64 #include <AIS_TexturedShape.hxx>
65 #include <TCollection_AsciiString.hxx>
66 #include <StdSelect_DisplayMode.hxx>
68 #include <Graphic3d_MaterialAspect.hxx>
70 #include <Precision.hxx>
75 #include <boost/utility.hpp>
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)
109 QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
110 QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_DUMP")));
112 setWindowTitle(tr("GEOM_DETECT_TITLE"));
114 /***************************************************************/
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();
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
132 // myViewGroup->hide();
134 // Widgets for the selection of the picture and the Region Of Interest
137 mySelectionGroup = new DlgRef_1Sel1Frame(centralWidget());
139 mySelectionGroup->PushButton1->setIcon(image0);
140 mySelectionGroup->PushButton1->setCheckable(true);
141 mySelectionGroup->PushButton1->setAutoExclusive(true);
143 mySelectionGroup->PushButton2->setIcon(image1);
144 mySelectionGroup->PushButton2->setCheckable(true);
145 mySelectionGroup->PushButton2->setAutoExclusive(true);
147 mySelectionGroup->TextLabel1->setText(tr( "GEOM_PICTURE" ));
148 mySelectionGroup->FrameLabel->setText("");
150 myOutputGroup = new DlgRef_3Radio(centralWidget());
151 myOutputGroup->GroupBox1->setTitle(tr("GEOM_DETECT_OUTPUT"));
152 myOutputGroup->RadioButton2->setText(tr( "GEOM_POLYLINE" ));
153 myOutputGroup->RadioButton1->setText(tr( "GEOM_SPLINE"));
154 myOutputGroup->RadioButton3->hide();
156 // myOutputGroup->hide(); //caché pour la demo
158 QVBoxLayout* layout = new QVBoxLayout(centralWidget());
159 layout->setMargin(0); layout->setSpacing(6);
160 // layout->addWidget( myViewGroup);
161 layout->addWidget( mySelectionGroup);
162 layout->addWidget( myOutputGroup);
164 // mainFrame()->GroupBoxName->hide();
166 // Build an instance of detection used to perform image processing operations
167 aDetector = new ShapeRec_FeatureDetector();
173 //=================================================================================
174 // function : ~EntityGUI_FeatureDetectorDlg()
175 // purpose : Destroys the object and frees any allocated resources
176 //=================================================================================
177 EntityGUI_FeatureDetectorDlg::~EntityGUI_FeatureDetectorDlg()
182 //=================================================================================
185 //=================================================================================
186 void EntityGUI_FeatureDetectorDlg::Init()
188 connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) );
189 connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
190 connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
191 connect( this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
192 connect( mySelectionGroup->PushButton2, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
193 connect( mySelectionGroup->PushButton1, SIGNAL( clicked() ), this, SLOT( onButtonClicked() ) );
194 // connect( myViewButtonGroup, SIGNAL( buttonClicked( int ) ), this, SLOT( onViewClicked( int ) ) );
195 connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),this, SLOT( SelectionIntoArgument() ) );
198 mySelectionGroup->PushButton1->click();
200 // SetEditCurrentArgument();
201 SelectionIntoArgument();
203 initName(tr("GEOM_CONTOURS"));
206 // myViewGroup->RadioButton1->setChecked(true);
207 myOutputGroup->RadioButton1->setChecked(true);
209 gp_Pnt aOrigin = gp_Pnt(0, 0, 0);
210 gp_Dir aDirZ = gp_Dir(0, 0, 1);
211 gp_Dir aDirX = gp_Dir(1, 0, 0);
213 aGlobalCS = gp_Ax3(aOrigin, aDirZ, aDirX);
215 myStartPnt = gp_Pnt(0,0,0);
216 myEndPnt = myStartPnt;
218 myGeomGUI->SetWorkingPlane( aGlobalCS );
219 myGeomGUI->ActiveWorkingPlane();
221 ConstructorsClicked(myConstructorId);
224 //=================================================================================
225 // function : SelectionIntoArgument()
226 // purpose : Called when selection as changed or other case
227 //=================================================================================
228 void EntityGUI_FeatureDetectorDlg::SelectionIntoArgument()
231 // TODO supprimer les lignes qui ne servent à rien le cas échéant
232 SUIT_ViewWindow* theViewWindow = getDesktop()->activeWindow();
233 std::map< std::string , std::vector<Handle(AIS_InteractiveObject)> >::iterator AISit;
234 SOCC_Viewer* soccViewer = (SOCC_Viewer*)(theViewWindow->getViewManager()->getViewModel());
236 if (!myEditCurrentArgument->isEnabled())
239 myEditCurrentArgument->setText( "" );
241 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
242 SALOME_ListIO aSelList;
243 aSelMgr->selectedObjects(aSelList);
244 SALOME_ListIteratorOfListIO anIt( aSelList );
246 for( ; anIt.More(); anIt.Next() )
247 if( !anIt.Value().IsNull() )
249 myFaceEntry = anIt.Value()->getEntry();
252 if (aSelList.Extent() != 1) {
253 if (myEditCurrentArgument == mySelectionGroup->LineEdit1)
258 TopAbs_ShapeEnum aNeedType = TopAbs_FACE ;
259 GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
261 if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
262 QString aName = GEOMBase::GetName( aSelectedObject.get() );
263 myEditCurrentArgument->setText( aName );
265 if ( myEditCurrentArgument == mySelectionGroup->LineEdit1 ) {
266 myFace = aSelectedObject;
267 AISit = soccViewer->entry2aisobjects.find(myFaceEntry.toStdString());
268 if (AISit == soccViewer->entry2aisobjects.end())
271 Handle(AIS_InteractiveObject) myAIS = (*AISit).second[0];
272 Handle(GEOM_AISShape) myAISShape;
273 if( myAIS->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
274 myAISShape = Handle(GEOM_AISShape)::DownCast( myAIS );
279 std::string theImgFileName = myAISShape->TextureFile();
280 if ( theImgFileName == "" )
283 // Setting the image caracteristics
284 aDetector->SetPath( theImgFileName );
285 height = aDetector->GetImgHeight();
286 width = aDetector->GetImgWidth();
287 pictureLeft = -0.5 * width; // X coordinate of the top left corner of the background image in the view
288 pictureTop = 0.5 * height; // Y coordinate of both top corners
295 //=================================================================================
296 // function : acceptMouseEvent()
298 //=================================================================================
299 bool EntityGUI_FeatureDetectorDlg::acceptMouseEvent() const
301 return mySelectionGroup->PushButton2->isChecked();
304 //=======================================================================
305 // function : ClickOnOk()
307 //=======================================================================
308 void EntityGUI_FeatureDetectorDlg::ClickOnOk()
310 setIsApplyAndClose( true );
315 //=================================================================================
316 // function : ClickOnApply()
318 //=================================================================================
319 bool EntityGUI_FeatureDetectorDlg::ClickOnApply()
325 ConstructorsClicked(getConstructorId());
329 //=================================================================================
330 // function : onConstructorClicked(int)
332 //=================================================================================
333 void EntityGUI_FeatureDetectorDlg::ConstructorsClicked(int id)
335 MESSAGE("Constructor id ="<<id)
336 myConstructorId = id;
340 // myViewGroup->show();
341 // mySelectionGroup->show();
342 myOutputGroup->hide();
343 // mySelectionGroup->TextLabel2->setText(tr("GEOM_DETECT_ZONE"));
344 mySelectionGroup->TextLabel2->hide();
345 mySelectionGroup->Frame->hide();
346 mySelectionGroup->PushButton2->hide();
347 initName(tr("GEOM_CORNERS"));
350 // myViewGroup->hide();
351 // mySelectionGroup->hide();
352 // mySelectionGroup->show();
353 myOutputGroup->show();
354 mySelectionGroup->TextLabel2->show();
355 mySelectionGroup->Frame->show();
356 mySelectionGroup->PushButton2->show();
357 mySelectionGroup->TextLabel2->setText(tr("GEOM_COLOR_FILTER"));
358 initName(tr("GEOM_CONTOURS"));
361 // myViewGroup->hide();
362 // mySelectionGroup->hide();
363 // mySelectionGroup->show();
364 myOutputGroup->hide();
365 mySelectionGroup->TextLabel2->setText(tr(""));
366 initName(tr("GEOM_LINES"));
371 // //=================================================================================
372 // // function : onViewClicked()
374 // //=================================================================================
375 // void EntityGUI_FeatureDetectorDlg::onViewClicked(int id)
377 // gp_Pnt aOrigin = gp_Pnt(0, 0, 0);
384 // aDirZ = gp_Dir(0, 0, 1);
385 // aDirX = gp_Dir(1, 0, 0);
388 // aDirZ = gp_Dir(1, 0, 0);
389 // aDirX = gp_Dir(0, 1, 0);
392 // aDirZ = gp_Dir(0, -1, 0);
393 // aDirX = gp_Dir(1, 0 , 0);
397 // myWPlane = gp_Ax3(aOrigin, aDirZ, aDirX);
398 // myGeomGUI->SetWorkingPlane( myWPlane );
399 // myGeomGUI->ActiveWorkingPlane();
403 //=================================================================================
404 // function : onButtonClicked()
406 //=================================================================================
407 void EntityGUI_FeatureDetectorDlg::onButtonClicked()
409 QPushButton* send = (QPushButton*)sender();
410 if (send == mySelectionGroup->PushButton2)
412 mySelectionGroup->LineEdit1->setEnabled(false);
414 else if (send == mySelectionGroup->PushButton1)
416 myStartPnt = gp_Pnt(0,0,0);
417 myEndPnt = myStartPnt;
418 myEditCurrentArgument = mySelectionGroup->LineEdit1;
419 mySelectionGroup->LineEdit1->setEnabled(true);
423 //=================================================================================
424 // function : setStartPnt( const QPoint& )
426 //=================================================================================
427 void EntityGUI_FeatureDetectorDlg::setStartPnt(const gp_Pnt& theStartPnt)
429 myStartPnt = theStartPnt;
430 MESSAGE("myStartPnt = ("<<theStartPnt.X()<<", "<<theStartPnt.Y()<<")")
433 //=================================================================================
434 // function : setEndPnt( const QPoint& )
436 //=================================================================================
437 void EntityGUI_FeatureDetectorDlg::setEndPnt(const gp_Pnt& theEndPnt)
439 myEndPnt = theEndPnt;
440 MESSAGE("myEndPnt = ("<<theEndPnt.X()<<", "<<theEndPnt.Y()<<")")
441 if (setSelectionRect())
445 //=================================================================================
446 // function : setSelectionRect()
448 //=================================================================================
449 bool EntityGUI_FeatureDetectorDlg::setSelectionRect()
451 // Set detection rectangle in the background image coordinates system
452 QPoint topLeft = QPoint(myStartPnt.X() - pictureLeft, pictureTop - myStartPnt.Y());
453 QPoint bottomRight = QPoint(myEndPnt.X() - pictureLeft, pictureTop - myEndPnt.Y());
454 myRect = QRect(topLeft, bottomRight);
456 return (!myRect.isEmpty() && myRect.width() > 1);
459 //=================================================================================
460 // function : showImageSample()
461 // purpose : Display a preview of the image sample selected by the user
462 //=================================================================================
463 void EntityGUI_FeatureDetectorDlg::showImageSample()
465 // Cropp the image to the selection rectangle given by the user
466 aDetector->SetROI( myRect );
467 std::string samplePicturePath = aDetector->CroppImage();
469 // Display the result
470 QPixmap pixmap(QString(samplePicturePath.c_str()));
471 mySelectionGroup->FrameLabel->setPixmap(pixmap);
472 mySelectionGroup->FrameLabel->setMask(pixmap.mask());
475 //=================================================================================
476 // function : createOperation
478 //=================================================================================
479 GEOM::GEOM_IOperations_ptr EntityGUI_FeatureDetectorDlg::createOperation()
481 return myGeomGUI->GetGeomGen()->GetIShapesOperations( getStudyId() );
484 //=================================================================================
485 // function : execute()
487 //=================================================================================
488 bool EntityGUI_FeatureDetectorDlg::execute( ObjectList& objects )
492 GEOM::GEOM_IBasicOperations_var aBasicOperations = myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
493 GEOM::GEOM_IShapesOperations_var aShapesOperations = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
495 if (myConstructorId == CORNERS)
497 double subPictureLeft;
498 double subPictureTop;
499 if( !myRect.isEmpty() )
501 aDetector->SetROI( myRect );
502 subPictureLeft = myStartPnt.X();
503 subPictureTop = myStartPnt.Y();
507 subPictureLeft = pictureLeft;
508 subPictureTop = pictureTop;
510 aDetector->ComputeCorners();
511 CvPoint2D32f* corners = aDetector->GetCorners();
512 int cornerCount = aDetector->GetCornerCount();
515 // Build the geom objects associated to the detected corners and returned by execute
516 if( !aBasicOperations->_is_nil() && !aShapesOperations->_is_nil() )
518 GEOM::GEOM_Object_var aGeomCorner;
519 GEOM::ListOfGO_var geomCorners = new GEOM::ListOfGO();
520 geomCorners->length( cornerCount );
521 for (i = 0; i < cornerCount; i++)
523 double x = subPictureLeft + corners[i].x;
524 double y = subPictureTop - corners[i].y;
527 aGeomCorner = aBasicOperations->MakePointXYZ( x,y,z );
529 geomCorners[i] = aGeomCorner;
531 GEOM::GEOM_Object_var aCompound = aShapesOperations->MakeCompound(geomCorners);
532 if ( !aCompound->_is_nil() )
535 SALOMEDS::Color aColor;
540 aCompound->SetColor(aColor);
541 aCompound->SetMarkerStd(GEOM::MT_POINT,GEOM::MS_30);
542 objects.push_back( aCompound._retn() );
547 else if (myConstructorId == CONTOURS)
549 int method = 0 ; //CANNY
550 if( !myRect.isEmpty() && myRect.width() > 1 )
552 aDetector->SetROI( myRect );
553 method = 1 ; //COLORFILTER
556 GEOM::GEOM_ICurvesOperations_var aCurveOperations = myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
558 aDetector->ComputeContours( method );
559 std::vector< std::vector<cv::Point> > contours = aDetector->GetContours();
560 std::vector<cv::Vec4i> hierarchy = aDetector->GetContoursHierarchy();
562 std::vector< cv::Point > contour;
565 GEOM::ListOfGO_var geomContours = new GEOM::ListOfGO();
566 int contourCount = 0;
570 MESSAGE("hierarchy.size() =" << hierarchy.size())
571 for( ; idx >= 0; idx = hierarchy[idx][0])
573 // for(int count=0, child=idx; child>=0, count<1; child=hierarchy[idx][2], count++)
575 // contour = contours[child];
576 contour = contours[idx];
577 std::vector< cv::Point >::iterator it;
578 std::vector< cv::Point >::iterator it_previous;
579 std::vector< cv::Point >::iterator it_next;
580 GEOM::GEOM_Object_var aGeomContourPnt;
581 GEOM::ListOfGO_var geomContourPnts = new GEOM::ListOfGO();
583 geomContourPnts->length( contour.size() );
586 std::set< std::vector<int> > existing_points;
587 std::pair< std::set< std::vector<int> >::iterator,bool > pnt_it;
588 for ( it=contour.begin() ; it < contour.end(); it++ )
590 int pnt_array[] = {it->x,it->y};
591 std::vector<int> pnt (pnt_array, pnt_array + sizeof(pnt_array) / sizeof(int) );
593 pnt_it=existing_points.insert(pnt);
594 if (pnt_it.second == true) // To avoid double points in the contours
597 if (it!=contour.begin()) // From the second point on perform some checking to avoid loops in the contours we build
599 it_previous = boost::prior(it);
600 it_next = boost::next(it);
602 double u_v_scalar_product = (it->x - it_previous->x) * (it_next->x - it->x) +
603 (it->y - it_previous->y) * (it_next->y - it->y);
604 if (u_v_scalar_product < 0)
606 double u_v_det = (it->x - it_previous->x) * (it_next->y - it->y) -
607 (it->y - it_previous->y) * (it_next->x - it->x);
609 double norme_u = sqrt ( (it->x - it_previous->x)*(it->x - it_previous->x) +
610 (it->y - it_previous->y)*(it->y - it_previous->y) );
612 double norme_v = sqrt ( (it->x - it_next->x)*(it->x - it_next->x) +
613 (it->y - it_next->y)*(it->y - it_next->y) );
615 double u_v_sinus = u_v_det / (norme_u * norme_v);
617 if (fabs(u_v_sinus) < Precision::Confusion())
623 double x = -0.5 *width + it->x;
624 double y = 0.5 *height - it->y;
628 aGeomContourPnt = aBasicOperations->MakePointXYZ( x,y,z );
629 geomContourPnts->length( j+1 );
630 geomContourPnts[j] = aGeomContourPnt;
636 GEOM::GEOM_Object_var aWire;
637 if(myOutputGroup->RadioButton2->isChecked())
639 aWire = aCurveOperations->MakePolyline(geomContourPnts.in(), false);
641 else if(myOutputGroup->RadioButton1->isChecked())
643 aWire = aCurveOperations->MakeSplineInterpolation(geomContourPnts.in(), /*closed =*/ false, /*reordering =*/ false);
648 if ( !aWire->_is_nil() )
650 geomContours->length(contourCount + 1);
651 geomContours[contourCount] = aWire;
656 GEOM::GEOM_Object_var aContoursCompound = aShapesOperations->MakeCompound(geomContours);
657 if ( !aContoursCompound->_is_nil() )
659 objects.push_back( aContoursCompound._retn() );
664 // TEST not very conclusive
666 // else if(myConstructorId ==LINES)
668 // aDetector->ComputeLines();
669 // std::vector<cv::Vec4i> lines = aDetector->GetLines();
670 // GEOM::GEOM_Object_var Pnt1;
671 // GEOM::GEOM_Object_var Pnt2;
672 // GEOM::GEOM_Object_var aLine;
674 // GEOM::ListOfGO_var geomLines = new GEOM::ListOfGO();
676 // for( int i = 0; i < lines.size(); i++ )
678 // Pnt1 = aBasicOperations->MakePointXYZ( -0.5 *width + lines[i][0], 0.5 *height - lines[i][1], 0 );
679 // Pnt2 = aBasicOperations->MakePointXYZ( -0.5 *width + lines[i][2], 0.5 *height - lines[i][3], 0 );
680 // aLine = aBasicOperations->MakeLineTwoPnt( Pnt1, Pnt2 );
681 // if ( !aLine->_is_nil() )
683 // geomLines->length(linesCount + 1);
684 // geomLines[linesCount] = aLine;
688 // GEOM::GEOM_Object_var aLinesCompound = aShapesOperations->MakeCompound(geomLines);
689 // if ( !aLinesCompound->_is_nil() )
691 // objects.push_back( aLinesCompound._retn() );