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"
29 #include <OCCViewer_ViewWindow.h>
30 #include <OCCViewer_ViewManager.h>
32 #include <SOCC_ViewModel.h>
35 #include <GeometryGUI.h>
36 #include <EntityGUI.h>
38 #include <GEOM_Object.hxx>
39 #include <ShapeRec_FeatureDetector.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("OCCViewer", tr("ICON_OCCVIEWER_VIEW_DUMP")));
110 QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT")));
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 mySelectionGroup = new QGroupBox(tr("GEOM_DETECT"), centralWidget());
135 QGridLayout* mySelectGrpLayout = new QGridLayout(mySelectionGroup);
136 myPushButton = new QPushButton(mySelectionGroup);
137 myPushButton->setIcon(image0);
138 myPushButton->setCheckable(true);
140 mySelButton = new QPushButton(mySelectionGroup);
141 mySelButton->setIcon(image1);
142 myLineEdit = new QLineEdit(mySelectionGroup);
144 mySnapshotLabel = new QLabel(mySelectionGroup);
145 mySelectGrpLayout->addWidget(myLineEdit, 0, 1);
146 mySelectGrpLayout->addWidget(mySelButton, 0, 0);
147 mySelectGrpLayout->addWidget(mySnapshotLabel, 1, 1);
148 mySelectGrpLayout->addWidget(myPushButton, 1, 0);
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();
170 //=================================================================================
171 // function : ~EntityGUI_FeatureDetectorDlg()
172 // purpose : Destroys the object and frees any allocated resources
173 //=================================================================================
174 EntityGUI_FeatureDetectorDlg::~EntityGUI_FeatureDetectorDlg()
179 //=================================================================================
182 //=================================================================================
183 void EntityGUI_FeatureDetectorDlg::Init()
185 connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) );
186 connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) );
187 connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) );
189 connect( this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int)));
191 connect( myPushButton, SIGNAL( toggled( bool ) ), this, SLOT( onButtonToggled( bool ) ) );
192 connect( mySelButton, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) );
194 // connect( myViewButtonGroup, SIGNAL( buttonClicked( int ) ), this, SLOT( onViewClicked( int ) ) );
196 connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),this, SLOT( SelectionIntoArgument() ) );
200 mySelButton->click();
201 mySelButton->setDown(true);
203 // SetEditCurrentArgument();
204 SelectionIntoArgument();
206 initName(tr("GEOM_CONTOURS"));
209 // myViewGroup->RadioButton1->setChecked(true);
210 myOutputGroup->RadioButton1->setChecked(true);
212 gp_Pnt aOrigin = gp_Pnt(0, 0, 0);
213 gp_Dir aDirZ = gp_Dir(0, 0, 1);
214 gp_Dir aDirX = gp_Dir(1, 0, 0);
216 aGlobalCS = gp_Ax3(aOrigin, aDirZ, aDirX);
218 myStartPnt = gp_Pnt(0,0,0);
219 myEndPnt = myStartPnt;
221 myGeomGUI->SetWorkingPlane( aGlobalCS );
222 myGeomGUI->ActiveWorkingPlane();
224 ConstructorsClicked(myConstructorId);
227 //=================================================================================
228 // function : SetEditCurrentArgument()
230 //=================================================================================
231 void EntityGUI_FeatureDetectorDlg::SetEditCurrentArgument()
233 QPushButton* send = (QPushButton*)sender();
234 if ( send == mySelButton ) {
235 myEditCurrentArgument = myLineEdit;
236 myLineEdit->setEnabled(true);
239 myLineEdit->setEnabled(false);
243 //=================================================================================
244 // function : SelectionIntoArgument()
245 // purpose : Called when selection as changed or other case
246 //=================================================================================
247 void EntityGUI_FeatureDetectorDlg::SelectionIntoArgument()
249 if (!myEditCurrentArgument->isEnabled())
252 myEditCurrentArgument->setText( "" );
254 LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr();
255 SALOME_ListIO aSelList;
256 aSelMgr->selectedObjects(aSelList);
257 SALOME_ListIteratorOfListIO anIt( aSelList );
258 for( ; anIt.More(); anIt.Next() )
259 if( !anIt.Value().IsNull() )
261 myFaceEntry = anIt.Value()->getEntry();
264 if (aSelList.Extent() != 1) {
265 if (myEditCurrentArgument == myLineEdit)
270 TopAbs_ShapeEnum aNeedType = TopAbs_FACE ;
271 GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType );
273 if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) {
274 QString aName = GEOMBase::GetName( aSelectedObject.get() );
275 myEditCurrentArgument->setText( aName );
276 if ( myEditCurrentArgument == myLineEdit ) {
277 myFace = aSelectedObject;
282 //=================================================================================
283 // function : acceptMouseEvent()
285 //=================================================================================
286 bool EntityGUI_FeatureDetectorDlg::acceptMouseEvent() const
288 return myPushButton->isChecked();
291 //=======================================================================
292 // function : ClickOnOk()
294 //=======================================================================
295 void EntityGUI_FeatureDetectorDlg::ClickOnOk()
297 setIsApplyAndClose( true );
302 //=================================================================================
303 // function : ClickOnApply()
305 //=================================================================================
306 bool EntityGUI_FeatureDetectorDlg::ClickOnApply()
312 ConstructorsClicked(getConstructorId());
316 //=================================================================================
317 // function : onConstructorClicked(int)
319 //=================================================================================
320 void EntityGUI_FeatureDetectorDlg::ConstructorsClicked(int id)
322 MESSAGE("Constructor id ="<<id)
323 myConstructorId = id;
327 // myViewGroup->show();
328 // mySelectionGroup->show();
329 myOutputGroup->hide();
330 mySnapshotLabel->setText(tr("GEOM_DETECT_ZONE"));
331 initName(tr("GEOM_CORNERS"));
334 // myViewGroup->hide();
335 // mySelectionGroup->hide();
336 // mySelectionGroup->show();
337 myOutputGroup->show();
338 mySnapshotLabel->setText(tr("GEOM_COLOR_FILTER"));
339 initName(tr("GEOM_CONTOURS"));
342 // myViewGroup->hide();
343 // mySelectionGroup->hide();
344 // mySelectionGroup->show();
345 myOutputGroup->hide();
346 mySnapshotLabel->setText(tr(""));
347 initName(tr("GEOM_LINES"));
352 // //=================================================================================
353 // // function : onViewClicked()
355 // //=================================================================================
356 // void EntityGUI_FeatureDetectorDlg::onViewClicked(int id)
358 // gp_Pnt aOrigin = gp_Pnt(0, 0, 0);
365 // aDirZ = gp_Dir(0, 0, 1);
366 // aDirX = gp_Dir(1, 0, 0);
369 // aDirZ = gp_Dir(1, 0, 0);
370 // aDirX = gp_Dir(0, 1, 0);
373 // aDirZ = gp_Dir(0, -1, 0);
374 // aDirX = gp_Dir(1, 0 , 0);
378 // myWPlane = gp_Ax3(aOrigin, aDirZ, aDirX);
379 // myGeomGUI->SetWorkingPlane( myWPlane );
380 // myGeomGUI->ActiveWorkingPlane();
384 //=================================================================================
385 // function : onButtonToggled()
387 //=================================================================================
388 void EntityGUI_FeatureDetectorDlg::onButtonToggled( bool checked)
392 myStartPnt = gp_Pnt(0,0,0);
393 myEndPnt = myStartPnt;
394 myLineEdit->setEnabled(true);
398 myLineEdit->setEnabled(false);
402 //=================================================================================
403 // function : setStartPnt( const QPoint& )
405 //=================================================================================
406 void EntityGUI_FeatureDetectorDlg::setStartPnt(const gp_Pnt& theStartPnt)
408 myStartPnt = theStartPnt;
409 MESSAGE("myStartPnt = ("<<theStartPnt.X()<<", "<<theStartPnt.Y()<<")")
412 //=================================================================================
413 // function : setEndPnt( const QPoint& )
415 //=================================================================================
416 void EntityGUI_FeatureDetectorDlg::setEndPnt(const gp_Pnt& theEndPnt)
418 myEndPnt = theEndPnt;
419 MESSAGE("myEndPnt = ("<<theEndPnt.X()<<", "<<theEndPnt.Y()<<")")
422 //=================================================================================
423 // function : createOperation
425 //=================================================================================
426 GEOM::GEOM_IOperations_ptr EntityGUI_FeatureDetectorDlg::createOperation()
428 return myGeomGUI->GetGeomGen()->GetIShapesOperations( getStudyId() );
431 //=================================================================================
432 // function : execute()
434 //=================================================================================
435 bool EntityGUI_FeatureDetectorDlg::execute( ObjectList& objects )
439 SUIT_ViewWindow* theViewWindow = getDesktop()->activeWindow();
440 std::map< std::string , std::vector<Handle(AIS_InteractiveObject)> >::iterator AISit;
441 SOCC_Viewer* soccViewer = (SOCC_Viewer*)(theViewWindow->getViewManager()->getViewModel());
443 AISit = soccViewer->entry2aisobjects.find(myFaceEntry.toStdString());
444 if (AISit == soccViewer->entry2aisobjects.end())
447 Handle(AIS_InteractiveObject) myAIS = (*AISit).second[0];
448 Handle(GEOM_AISShape) myAISShape;
449 if( myAIS->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
450 myAISShape = Handle(GEOM_AISShape)::DownCast( myAIS );
455 std::string theImgFileName = myAISShape->TextureFile();
457 if ( theImgFileName == "" )
460 // Build an instance of detection used to perform image processing operations
461 ShapeRec_FeatureDetector* aDetector = new ShapeRec_FeatureDetector( theImgFileName );
463 int height = aDetector->GetImgHeight();
464 int width = aDetector->GetImgWidth();
466 // Operations to display the corners properly in the 3D scene
467 double pictureLeft = -0.5 * width; // X coordinate of the top left corner of the background image in the view
468 double pictureTop = 0.5 * height; // Y coordinate of both top corners
470 // Set detection rectangle in the background image coordinates system
471 QPoint topLeft = QPoint(myStartPnt.X() - pictureLeft, pictureTop - myStartPnt.Y());
472 QPoint bottomRight = QPoint(myEndPnt.X() - pictureLeft, pictureTop - myEndPnt.Y());
473 QRect aRect = QRect(topLeft, bottomRight);
475 GEOM::GEOM_IBasicOperations_var aBasicOperations = myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() );
476 GEOM::GEOM_IShapesOperations_var aShapesOperations = GEOM::GEOM_IShapesOperations::_narrow( getOperation() );
478 if (myConstructorId == CORNERS)
480 if( !aRect.isEmpty() )
482 aDetector->SetROI( aRect );
483 pictureLeft = myStartPnt.X();
484 pictureTop = myStartPnt.Y();
486 aDetector->ComputeCorners();
487 CvPoint2D32f* corners = aDetector->GetCorners();
488 int cornerCount = aDetector->GetCornerCount();
491 // Build the geom objects associated to the detected corners and returned by execute
492 if( !aBasicOperations->_is_nil() && !aShapesOperations->_is_nil() )
494 GEOM::GEOM_Object_var aGeomCorner;
495 GEOM::ListOfGO_var geomCorners = new GEOM::ListOfGO();
496 geomCorners->length( cornerCount );
497 for (i = 0; i < cornerCount; i++)
499 double x = pictureLeft + corners[i].x;
500 double y = pictureTop - corners[i].y;
503 aGeomCorner = aBasicOperations->MakePointXYZ( x,y,z );
505 geomCorners[i] = aGeomCorner;
507 GEOM::GEOM_Object_var aCompound = aShapesOperations->MakeCompound(geomCorners);
508 if ( !aCompound->_is_nil() )
511 SALOMEDS::Color aColor;
516 aCompound->SetColor(aColor);
517 aCompound->SetMarkerStd(GEOM::MT_POINT,GEOM::MS_30);
518 objects.push_back( aCompound._retn() );
523 else if (myConstructorId == CONTOURS)
525 int method = 0 ; //CANNY
526 if( !aRect.isEmpty() && aRect.width() > 1 )
528 aDetector->SetROI( aRect );
529 method = 1 ; //COLORFILTER
532 GEOM::GEOM_ICurvesOperations_var aCurveOperations = myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() );
534 aDetector->ComputeContours( method );
535 std::vector< std::vector<cv::Point> > contours = aDetector->GetContours();
536 std::vector<cv::Vec4i> hierarchy = aDetector->GetContoursHierarchy();
538 std::vector< cv::Point > contour;
541 GEOM::ListOfGO_var geomContours = new GEOM::ListOfGO();
542 int contourCount = 0;
546 MESSAGE("hierarchy.size() =" << hierarchy.size())
547 for( ; idx >= 0; idx = hierarchy[idx][0])
549 // for(int count=0, child=idx; child>=0, count<1; child=hierarchy[idx][2], count++)
551 // contour = contours[child];
552 contour = contours[idx];
553 std::vector< cv::Point >::iterator it;
554 std::vector< cv::Point >::iterator it_previous;
555 std::vector< cv::Point >::iterator it_next;
556 GEOM::GEOM_Object_var aGeomContourPnt;
557 GEOM::ListOfGO_var geomContourPnts = new GEOM::ListOfGO();
559 geomContourPnts->length( contour.size() );
562 std::set< std::vector<int> > existing_points;
563 std::pair< std::set< std::vector<int> >::iterator,bool > pnt_it;
564 for ( it=contour.begin() ; it < contour.end(); it++ )
566 int pnt_array[] = {it->x,it->y};
567 std::vector<int> pnt (pnt_array, pnt_array + sizeof(pnt_array) / sizeof(int) );
569 pnt_it=existing_points.insert(pnt);
570 if (pnt_it.second == true) // To avoid double points in the contours
573 if (it!=contour.begin()) // From the second point on perform some checking to avoid loops in the contours we build
575 it_previous = boost::prior(it);
576 it_next = boost::next(it);
578 double u_v_scalar_product = (it->x - it_previous->x) * (it_next->x - it->x) +
579 (it->y - it_previous->y) * (it_next->y - it->y);
580 if (u_v_scalar_product < 0)
582 double u_v_det = (it->x - it_previous->x) * (it_next->y - it->y) -
583 (it->y - it_previous->y) * (it_next->x - it->x);
585 double norme_u = sqrt ( (it->x - it_previous->x)*(it->x - it_previous->x) +
586 (it->y - it_previous->y)*(it->y - it_previous->y) );
588 double norme_v = sqrt ( (it->x - it_next->x)*(it->x - it_next->x) +
589 (it->y - it_next->y)*(it->y - it_next->y) );
591 double u_v_sinus = u_v_det / (norme_u * norme_v);
593 if (fabs(u_v_sinus) < Precision::Confusion())
599 double x = -0.5 *width + it->x;
600 double y = 0.5 *height - it->y;
604 aGeomContourPnt = aBasicOperations->MakePointXYZ( x,y,z );
605 geomContourPnts->length( j+1 );
606 geomContourPnts[j] = aGeomContourPnt;
612 GEOM::GEOM_Object_var aWire;
613 if(myOutputGroup->RadioButton2->isChecked())
615 aWire = aCurveOperations->MakePolyline(geomContourPnts.in(), false);
617 else if(myOutputGroup->RadioButton1->isChecked())
619 aWire = aCurveOperations->MakeSplineInterpolation(geomContourPnts.in(), /*closed =*/ false, /*reordering =*/ false);
624 if ( !aWire->_is_nil() )
626 geomContours->length(contourCount + 1);
627 geomContours[contourCount] = aWire;
632 GEOM::GEOM_Object_var aContoursCompound = aShapesOperations->MakeCompound(geomContours);
633 if ( !aContoursCompound->_is_nil() )
635 objects.push_back( aContoursCompound._retn() );
640 else if(myConstructorId ==LINES)
642 aDetector->ComputeLines();
643 std::vector<cv::Vec4i> lines = aDetector->GetLines();
644 GEOM::GEOM_Object_var Pnt1;
645 GEOM::GEOM_Object_var Pnt2;
646 GEOM::GEOM_Object_var aLine;
648 GEOM::ListOfGO_var geomLines = new GEOM::ListOfGO();
650 for( int i = 0; i < lines.size(); i++ )
652 Pnt1 = aBasicOperations->MakePointXYZ( -0.5 *width + lines[i][0], 0.5 *height - lines[i][1], 0 );
653 Pnt2 = aBasicOperations->MakePointXYZ( -0.5 *width + lines[i][2], 0.5 *height - lines[i][3], 0 );
654 aLine = aBasicOperations->MakeLineTwoPnt( Pnt1, Pnt2 );
655 if ( !aLine->_is_nil() )
657 geomLines->length(linesCount + 1);
658 geomLines[linesCount] = aLine;
662 GEOM::GEOM_Object_var aLinesCompound = aShapesOperations->MakeCompound(geomLines);
663 if ( !aLinesCompound->_is_nil() )
665 objects.push_back( aLinesCompound._retn() );