// Copyright (C) 2007-2011 CEA/DEN, EDF R&D, OPEN CASCADE // // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // // GEOM GEOMGUI : GUI for Geometry component // File : EntityGUI_SketcherDlg.cxx // Author : Renaud NEDELEC, Open CASCADE S.A.S. #include "EntityGUI_FeatureDetectorDlg.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Constructors enum{ CONTOURS, CORNERS, LINES }; enum { RADIO_BUTTONS, MSG, PUSH_BUTTON, }; // // // View // // enum { // // XY, // // YZ, // // XZ // // }; //================================================================================= // class : EntityGUI_FeatureDetectorDlg() // purpose : Constructs a EntityGUI_FeatureDetectorDlg which is a child of 'parent', with the // name 'name' and widget flags set to 'f'. // The dialog will by default be modeless, unless you set 'modal' to // TRUE to construct a modal dialog. //================================================================================= EntityGUI_FeatureDetectorDlg::EntityGUI_FeatureDetectorDlg( GeometryGUI* theGeometryGUI, QWidget* parent, bool modal, Qt::WindowFlags fl) : GEOMBase_Skeleton(theGeometryGUI, parent, modal, fl) { QPixmap image0(SUIT_Session::session()->resourceMgr()->loadPixmap("OCCViewer", tr("ICON_OCCVIEWER_VIEW_DUMP"))); QPixmap image1(SUIT_Session::session()->resourceMgr()->loadPixmap("GEOM", tr("ICON_SELECT"))); setWindowTitle(tr("GEOM_DETECT_TITLE")); /***************************************************************/ mainFrame()->GroupConstructors->setTitle(tr("GEOM_FEATURES")); mainFrame()->RadioButton1->setText(tr("GEOM_CONTOURS")); mainFrame()->RadioButton2->setText(tr("GEOM_CORNERS")); mainFrame()->RadioButton3->setAttribute(Qt::WA_DeleteOnClose); mainFrame()->RadioButton3->close(); // myViewGroup = new DlgRef_3Radio(centralWidget()); // myViewGroup->GroupBox1->setTitle(tr("GEOM_VIEW")); // myViewGroup->RadioButton1->setText(tr( "GEOM_TOP" )); // myViewGroup->RadioButton2->setText(tr( "GEOM_FRONT")); // myViewGroup->RadioButton3->setText(tr( "GEOM_LEFT" )); // myViewButtonGroup = new QButtonGroup( this ); // myViewButtonGroup->addButton( myViewGroup->RadioButton1, XY ); // Top view // myViewButtonGroup->addButton( myViewGroup->RadioButton2, YZ ); // Front View // myViewButtonGroup->addButton( myViewGroup->RadioButton3, XZ ); // Left View // // myViewGroup->hide(); mySelectionGroup = new QGroupBox(tr("GEOM_DETECT"), centralWidget()); QGridLayout* mySelectGrpLayout = new QGridLayout(mySelectionGroup); myPushButton = new QPushButton(mySelectionGroup); myPushButton->setIcon(image0); myPushButton->setCheckable(true); mySelButton = new QPushButton(mySelectionGroup); mySelButton->setIcon(image1); myLineEdit = new QLineEdit(mySelectionGroup); mySnapshotLabel = new QLabel(mySelectionGroup); mySelectGrpLayout->addWidget(myLineEdit, 0, 1); mySelectGrpLayout->addWidget(mySelButton, 0, 0); mySelectGrpLayout->addWidget(mySnapshotLabel, 1, 1); mySelectGrpLayout->addWidget(myPushButton, 1, 0); myOutputGroup = new DlgRef_3Radio(centralWidget()); myOutputGroup->GroupBox1->setTitle(tr("GEOM_DETECT_OUTPUT")); myOutputGroup->RadioButton2->setText(tr( "GEOM_POLYLINE" )); myOutputGroup->RadioButton1->setText(tr( "GEOM_SPLINE")); myOutputGroup->RadioButton3->hide(); // myOutputGroup->hide(); //caché pour la demo QVBoxLayout* layout = new QVBoxLayout(centralWidget()); layout->setMargin(0); layout->setSpacing(6); // layout->addWidget( myViewGroup); layout->addWidget( mySelectionGroup); layout->addWidget( myOutputGroup); // mainFrame()->GroupBoxName->hide(); Init(); } //================================================================================= // function : ~EntityGUI_FeatureDetectorDlg() // purpose : Destroys the object and frees any allocated resources //================================================================================= EntityGUI_FeatureDetectorDlg::~EntityGUI_FeatureDetectorDlg() { } //================================================================================= // function : Init() // purpose : //================================================================================= void EntityGUI_FeatureDetectorDlg::Init() { connect( myGeomGUI, SIGNAL( SignalCloseAllDialogs() ), this, SLOT( ClickOnCancel() ) ); connect( buttonOk(), SIGNAL( clicked() ), this, SLOT( ClickOnOk() ) ); connect( buttonApply(), SIGNAL( clicked() ), this, SLOT( ClickOnApply() ) ); connect( this, SIGNAL(constructorsClicked(int)), this, SLOT(ConstructorsClicked(int))); connect( myPushButton, SIGNAL( toggled( bool ) ), this, SLOT( onButtonToggled( bool ) ) ); connect( mySelButton, SIGNAL( clicked() ), this, SLOT( SetEditCurrentArgument() ) ); // connect( myViewButtonGroup, SIGNAL( buttonClicked( int ) ), this, SLOT( onViewClicked( int ) ) ); connect( myGeomGUI->getApp()->selectionMgr(), SIGNAL( currentSelectionChanged() ),this, SLOT( SelectionIntoArgument() ) ); myConstructorId = 0; mySelButton->click(); mySelButton->setDown(true); // SetEditCurrentArgument(); SelectionIntoArgument(); initName(tr("GEOM_CONTOURS")); resize(100,100); // myViewGroup->RadioButton1->setChecked(true); myOutputGroup->RadioButton1->setChecked(true); gp_Pnt aOrigin = gp_Pnt(0, 0, 0); gp_Dir aDirZ = gp_Dir(0, 0, 1); gp_Dir aDirX = gp_Dir(1, 0, 0); aGlobalCS = gp_Ax3(aOrigin, aDirZ, aDirX); myStartPnt = gp_Pnt(0,0,0); myEndPnt = myStartPnt; myGeomGUI->SetWorkingPlane( aGlobalCS ); myGeomGUI->ActiveWorkingPlane(); ConstructorsClicked(myConstructorId); } //================================================================================= // function : SetEditCurrentArgument() // purpose : //================================================================================= void EntityGUI_FeatureDetectorDlg::SetEditCurrentArgument() { QPushButton* send = (QPushButton*)sender(); if ( send == mySelButton ) { myEditCurrentArgument = myLineEdit; myLineEdit->setEnabled(true); } else myLineEdit->setEnabled(false); send->setDown(true); } //================================================================================= // function : SelectionIntoArgument() // purpose : Called when selection as changed or other case //================================================================================= void EntityGUI_FeatureDetectorDlg::SelectionIntoArgument() { if (!myEditCurrentArgument->isEnabled()) return; myEditCurrentArgument->setText( "" ); LightApp_SelectionMgr* aSelMgr = myGeomGUI->getApp()->selectionMgr(); SALOME_ListIO aSelList; aSelMgr->selectedObjects(aSelList); SALOME_ListIteratorOfListIO anIt( aSelList ); for( ; anIt.More(); anIt.Next() ) if( !anIt.Value().IsNull() ) { myFaceEntry = anIt.Value()->getEntry(); } if (aSelList.Extent() != 1) { if (myEditCurrentArgument == myLineEdit) myFace.nullify(); return; } TopAbs_ShapeEnum aNeedType = TopAbs_FACE ; GEOM::GeomObjPtr aSelectedObject = getSelected( aNeedType ); TopoDS_Shape aShape; if ( aSelectedObject && GEOMBase::GetShape( aSelectedObject.get(), aShape ) && !aShape.IsNull() ) { QString aName = GEOMBase::GetName( aSelectedObject.get() ); myEditCurrentArgument->setText( aName ); if ( myEditCurrentArgument == myLineEdit ) { myFace = aSelectedObject; } } } //================================================================================= // function : acceptMouseEvent() // purpose : //================================================================================= bool EntityGUI_FeatureDetectorDlg::acceptMouseEvent() const { return myPushButton->isChecked(); } //======================================================================= // function : ClickOnOk() // purpose : //======================================================================= void EntityGUI_FeatureDetectorDlg::ClickOnOk() { setIsApplyAndClose( true ); if (onAccept()) ClickOnCancel(); } //================================================================================= // function : ClickOnApply() // purpose : //================================================================================= bool EntityGUI_FeatureDetectorDlg::ClickOnApply() { if (!onAccept()) return false; // initName(); ConstructorsClicked(getConstructorId()); return true; } //================================================================================= // function : onConstructorClicked(int) // purpose : //================================================================================= void EntityGUI_FeatureDetectorDlg::ConstructorsClicked(int id) { MESSAGE("Constructor id ="<show(); // mySelectionGroup->show(); myOutputGroup->hide(); mySnapshotLabel->setText(tr("GEOM_DETECT_ZONE")); initName(tr("GEOM_CORNERS")); break; case CONTOURS: // myViewGroup->hide(); // mySelectionGroup->hide(); // mySelectionGroup->show(); myOutputGroup->show(); mySnapshotLabel->setText(tr("GEOM_COLOR_FILTER")); initName(tr("GEOM_CONTOURS")); break; case LINES: // myViewGroup->hide(); // mySelectionGroup->hide(); // mySelectionGroup->show(); myOutputGroup->hide(); mySnapshotLabel->setText(tr("")); initName(tr("GEOM_LINES")); break; } } // //================================================================================= // // function : onViewClicked() // // purpose : // //================================================================================= // void EntityGUI_FeatureDetectorDlg::onViewClicked(int id) // { // gp_Pnt aOrigin = gp_Pnt(0, 0, 0); // gp_Dir aDirZ; // gp_Dir aDirX; // // switch(id) // { // case XY: // aDirZ = gp_Dir(0, 0, 1); // aDirX = gp_Dir(1, 0, 0); // break; // case YZ: // aDirZ = gp_Dir(1, 0, 0); // aDirX = gp_Dir(0, 1, 0); // break; // case XZ: // aDirZ = gp_Dir(0, -1, 0); // aDirX = gp_Dir(1, 0 , 0); // break; // } // // myWPlane = gp_Ax3(aOrigin, aDirZ, aDirX); // myGeomGUI->SetWorkingPlane( myWPlane ); // myGeomGUI->ActiveWorkingPlane(); // // } //================================================================================= // function : onButtonToggled() // purpose : //================================================================================= void EntityGUI_FeatureDetectorDlg::onButtonToggled( bool checked) { if (!checked) { myStartPnt = gp_Pnt(0,0,0); myEndPnt = myStartPnt; myLineEdit->setEnabled(true); } else { myLineEdit->setEnabled(false); } } //================================================================================= // function : setStartPnt( const QPoint& ) // purpose : //================================================================================= void EntityGUI_FeatureDetectorDlg::setStartPnt(const gp_Pnt& theStartPnt) { myStartPnt = theStartPnt; MESSAGE("myStartPnt = ("<GetGeomGen()->GetIShapesOperations( getStudyId() ); } //================================================================================= // function : execute() // purpose : //================================================================================= bool EntityGUI_FeatureDetectorDlg::execute( ObjectList& objects ) { MESSAGE("EntityGUI_FeatureDetectorDlg::execute( ObjectList& objects )") bool res = false; SUIT_ViewWindow* theViewWindow = getDesktop()->activeWindow(); MESSAGE("myFaceEntry = "<< myFaceEntry.toStdString()); std::map< std::string , std::vector >::iterator AISit; SOCC_Viewer* soccViewer = (SOCC_Viewer*)(theViewWindow->getViewManager()->getViewModel()); MESSAGE("repere1") AISit = soccViewer->entry2aisobjects.find(myFaceEntry.toStdString()); if (AISit == soccViewer->entry2aisobjects.end()) return res; Handle(AIS_InteractiveObject) myAIS = (*AISit).second[0]; Handle(GEOM_AISShape) myAISShape; if( myAIS->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) { myAISShape = Handle(GEOM_AISShape)::DownCast( myAIS ); } else return res; std::string theImgFileName = myAISShape->TextureFile(); if ( theImgFileName == "" ) return res; // Build an instance of detection used to perform image processing operations ShapeRec_FeatureDetector* aDetector = new ShapeRec_FeatureDetector( theImgFileName ); int height = aDetector->GetImgHeight(); int width = aDetector->GetImgWidth(); // Operations to display the corners properly in the 3D scene double pictureLeft = -0.5 * width; // X coordinate of the top left corner of the background image in the view double pictureTop = 0.5 * height; // Y coordinate of both top corners // Set detection rectangle in the background image coordinates system QPoint topLeft = QPoint(myStartPnt.X() - pictureLeft, pictureTop - myStartPnt.Y()); QPoint bottomRight = QPoint(myEndPnt.X() - pictureLeft, pictureTop - myEndPnt.Y()); QRect aRect = QRect(topLeft, bottomRight); GEOM::GEOM_IBasicOperations_var aBasicOperations = myGeomGUI->GetGeomGen()->GetIBasicOperations( getStudyId() ); GEOM::GEOM_IShapesOperations_var aShapesOperations = GEOM::GEOM_IShapesOperations::_narrow( getOperation() ); if (myConstructorId == CORNERS) { if( !aRect.isEmpty() ) { aDetector->SetROI( aRect ); pictureLeft = myStartPnt.X(); pictureTop = myStartPnt.Y(); } aDetector->ComputeCorners(); CvPoint2D32f* corners = aDetector->GetCorners(); int cornerCount = aDetector->GetCornerCount(); int i; // Build the geom objects associated to the detected corners and returned by execute if( !aBasicOperations->_is_nil() && !aShapesOperations->_is_nil() ) { GEOM::GEOM_Object_var aGeomCorner; GEOM::ListOfGO_var geomCorners = new GEOM::ListOfGO(); geomCorners->length( cornerCount ); for (i = 0; i < cornerCount; i++) { double x = pictureLeft + corners[i].x; double y = pictureTop - corners[i].y; double z = 0; aGeomCorner = aBasicOperations->MakePointXYZ( x,y,z ); geomCorners[i] = aGeomCorner; } GEOM::GEOM_Object_var aCompound = aShapesOperations->MakeCompound(geomCorners); if ( !aCompound->_is_nil() ) { // Dark blue color SALOMEDS::Color aColor; aColor.R = 0; aColor.G = 0; aColor.B = 0.8; aCompound->SetColor(aColor); aCompound->SetMarkerStd(GEOM::MT_POINT,GEOM::MS_30); objects.push_back( aCompound._retn() ); res = true; } } } else if (myConstructorId == CONTOURS) { int method = 0 ; //CANNY if( !aRect.isEmpty() && aRect.width() > 1 ) { aDetector->SetROI( aRect ); method = 1 ; //COLORFILTER } GEOM::GEOM_ICurvesOperations_var aCurveOperations = myGeomGUI->GetGeomGen()->GetICurvesOperations( getStudyId() ); aDetector->ComputeContours( method ); std::vector< std::vector > contours = aDetector->GetContours(); std::vector hierarchy = aDetector->GetContoursHierarchy(); std::vector< cv::Point > contour; int idx = 0; GEOM::ListOfGO_var geomContours = new GEOM::ListOfGO(); int contourCount = 0; bool insert; MESSAGE("hierarchy.size() =" << hierarchy.size()) for( ; idx >= 0; idx = hierarchy[idx][0]) { // for(int count=0, child=idx; child>=0, count<1; child=hierarchy[idx][2], count++) // { // contour = contours[child]; contour = contours[idx]; std::vector< cv::Point >::iterator it; std::vector< cv::Point >::iterator it_previous; std::vector< cv::Point >::iterator it_next; GEOM::GEOM_Object_var aGeomContourPnt; GEOM::ListOfGO_var geomContourPnts = new GEOM::ListOfGO(); geomContourPnts->length( contour.size() ); int j = 0; std::set< std::vector > existing_points; std::pair< std::set< std::vector >::iterator,bool > pnt_it; for ( it=contour.begin() ; it < contour.end(); it++ ) { int pnt_array[] = {it->x,it->y}; std::vector pnt (pnt_array, pnt_array + sizeof(pnt_array) / sizeof(int) ); pnt_it=existing_points.insert(pnt); if (pnt_it.second == true) // To avoid double points in the contours { insert = true; if (it!=contour.begin()) // From the second point on perform some checking to avoid loops in the contours we build { it_previous = boost::prior(it); it_next = boost::next(it); double u_v_scalar_product = (it->x - it_previous->x) * (it_next->x - it->x) + (it->y - it_previous->y) * (it_next->y - it->y); if (u_v_scalar_product < 0) { double u_v_det = (it->x - it_previous->x) * (it_next->y - it->y) - (it->y - it_previous->y) * (it_next->x - it->x); double norme_u = sqrt ( (it->x - it_previous->x)*(it->x - it_previous->x) + (it->y - it_previous->y)*(it->y - it_previous->y) ); double norme_v = sqrt ( (it->x - it_next->x)*(it->x - it_next->x) + (it->y - it_next->y)*(it->y - it_next->y) ); double u_v_sinus = u_v_det / (norme_u * norme_v); if (fabs(u_v_sinus) < Precision::Confusion()) { insert = false; } } } double x = -0.5 *width + it->x; double y = 0.5 *height - it->y; double z = 0; if (insert) { aGeomContourPnt = aBasicOperations->MakePointXYZ( x,y,z ); geomContourPnts->length( j+1 ); geomContourPnts[j] = aGeomContourPnt; j++; } } } GEOM::GEOM_Object_var aWire; if(myOutputGroup->RadioButton2->isChecked()) { aWire = aCurveOperations->MakePolyline(geomContourPnts.in(), false); } else if(myOutputGroup->RadioButton1->isChecked()) { aWire = aCurveOperations->MakeSplineInterpolation(geomContourPnts.in(), /*closed =*/ false, /*reordering =*/ false); } else return res; if ( !aWire->_is_nil() ) { geomContours->length(contourCount + 1); geomContours[contourCount] = aWire; contourCount++; } // } } GEOM::GEOM_Object_var aContoursCompound = aShapesOperations->MakeCompound(geomContours); if ( !aContoursCompound->_is_nil() ) { objects.push_back( aContoursCompound._retn() ); } res=true; } else if(myConstructorId ==LINES) { aDetector->ComputeLines(); std::vector lines = aDetector->GetLines(); GEOM::GEOM_Object_var Pnt1; GEOM::GEOM_Object_var Pnt2; GEOM::GEOM_Object_var aLine; GEOM::ListOfGO_var geomLines = new GEOM::ListOfGO(); int linesCount=0; for( int i = 0; i < lines.size(); i++ ) { Pnt1 = aBasicOperations->MakePointXYZ( -0.5 *width + lines[i][0], 0.5 *height - lines[i][1], 0 ); Pnt2 = aBasicOperations->MakePointXYZ( -0.5 *width + lines[i][2], 0.5 *height - lines[i][3], 0 ); aLine = aBasicOperations->MakeLineTwoPnt( Pnt1, Pnt2 ); if ( !aLine->_is_nil() ) { geomLines->length(linesCount + 1); geomLines[linesCount] = aLine; linesCount++; } } GEOM::GEOM_Object_var aLinesCompound = aShapesOperations->MakeCompound(geomLines); if ( !aLinesCompound->_is_nil() ) { objects.push_back( aLinesCompound._retn() ); } res=true; } return res; }