1 // Copyright (C) 2013 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "CurveCreator_Utils.h"
22 #include <GEOMUtils.hxx>
27 #include <TopoDS_Vertex.hxx>
28 #include <TopoDS_Wire.hxx>
29 #include <TopoDS_Edge.hxx>
31 #include <AIS_ListOfInteractive.hxx>
32 #include <AIS_ListIteratorOfListOfInteractive.hxx>
33 #include <AIS_Shape.hxx>
34 #include <AIS_Point.hxx>
35 #include <AIS_Line.hxx>
36 #include <AIS_Trihedron.hxx>
38 #include <Geom_Point.hxx>
39 #include <Geom_BSplineCurve.hxx>
40 #include <Geom_Line.hxx>
42 #include <TopExp_Explorer.hxx>
43 #include <BRep_Tool.hxx>
44 #include <GeomAPI_ProjectPointOnCurve.hxx>
46 #include <ProjLib.hxx>
49 const double LOCAL_SELECTION_TOLERANCE = 0.0001;
50 const int SCENE_PIXEL_TOLERANCE = 10;
52 //=======================================================================
53 // function : ConvertClickToPoint()
54 // purpose : Returns the point clicked in 3D view
55 //=======================================================================
56 void CurveCreator_Utils::ConvertPointToClick( const gp_Pnt& thePoint,
57 Handle(V3d_View) theView,
60 theView->Convert(thePoint.X(), thePoint.Y(), thePoint.Z(), x, y );
64 //=======================================================================
65 // function : ConvertClickToPoint()
66 // purpose : Returns the point clicked in 3D view
67 //=======================================================================
68 gp_Pnt CurveCreator_Utils::ConvertClickToPoint( int x, int y, Handle(V3d_View) aView )
70 return GEOMUtils::ConvertClickToPoint( x, y, aView );
72 V3d_Coordinate XEye, YEye, ZEye, XAt, YAt, ZAt;
73 aView->Eye( XEye, YEye, ZEye );
75 aView->At( XAt, YAt, ZAt );
76 gp_Pnt EyePoint( XEye, YEye, ZEye );
77 gp_Pnt AtPoint( XAt, YAt, ZAt );
79 gp_Vec EyeVector( EyePoint, AtPoint );
80 gp_Dir EyeDir( EyeVector );
82 gp_Pln PlaneOfTheView = gp_Pln( AtPoint, EyeDir );
83 Standard_Real X, Y, Z;
84 aView->Convert( x, y, X, Y, Z );
85 gp_Pnt ConvertedPoint( X, Y, Z );
87 gp_Pnt2d ConvertedPointOnPlane = ProjLib::Project( PlaneOfTheView, ConvertedPoint );
88 gp_Pnt ResultPoint = ElSLib::Value( ConvertedPointOnPlane.X(), ConvertedPointOnPlane.Y(), PlaneOfTheView );
93 //=======================================================================
94 // function : setLocalPointContext
95 // purpose : Open/close the viewer local context
96 //=======================================================================
97 void CurveCreator_Utils::setLocalPointContext(
98 Handle(AIS_InteractiveContext) theContext,
105 // Open local context if there is no one
106 if ( !theContext->HasOpenedContext() ) {
107 theContext->ClearCurrents( false );
108 theContext->OpenLocalContext( false/*use displayed objects*/, true/*allow shape decomposition*/ );
110 AIS_ListOfInteractive aList;
111 theContext->DisplayedObjects( aList );
113 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
116 for ( AIS_ListIteratorOfListOfInteractive it( aList ); it.More(); it.Next() )
118 Handle(AIS_InteractiveObject) anAIS = it.Value();
119 if ( !anAIS.IsNull() )
121 if ( anAIS->IsKind( STANDARD_TYPE( AIS_Shape ) ) )
123 theContext->Load( anAIS, -1/*selection mode*/, true/*allow decomposition*/ );
124 theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_WIRE ) );
125 //theContext->Activate( anAIS, AIS_Shape::SelectionMode( (TopAbs_ShapeEnum)TopAbs_VERTEX ) );
127 else if ( anAIS->DynamicType() != STANDARD_TYPE(AIS_Trihedron) )
129 theContext->Load( anAIS, -1/*selection mode*/, false/*allow decomposition*/ );
130 theContext->Activate( anAIS, TopAbs_VERTEX );
137 if ( theContext->HasOpenedContext() )
138 theContext->CloseAllContexts();
142 bool CurveCreator_Utils::pointOnObject( Handle(V3d_View) theView,
143 Handle(AIS_InteractiveObject) theObject,
144 const int theX, const int theY,
146 gp_Pnt& thePoint1, gp_Pnt& thePoint2 )
148 bool isFound = false;
150 if ( theObject.IsNull() || theView.IsNull() )
154 Standard_Real aParameter;
156 Handle(AIS_Line) aLine = Handle(AIS_Line)::DownCast( theObject );
157 if ( !aLine.IsNull() ) {
158 const Handle(Geom_Line) aGLine = aLine->Line();
159 isFound = hasProjectPointOnCurve( theView, theX, theY, aGLine, aParameter );
161 aPoint = aGLine->Value( aParameter );
163 Handle(Geom_Point) aPStart;
164 Handle(Geom_Point) aPEnd;
165 aLine->Points( aPStart, aPEnd );
166 aPnt1 = aPStart->Pnt();
167 aPnt2 = aPEnd->Pnt();
169 // in case of Geom line a projection is performed to the infinite line,
170 // so it is necessary to bound it by the line size
172 aLineBox.Set( aPnt1, gp_Vec( aPnt1, aPnt2 ) );
173 isFound = !aLineBox.IsOut( aPoint );
177 Handle(AIS_Shape) aShape = Handle(AIS_Shape)::DownCast( theObject );
178 if ( !aShape.IsNull() ) {
179 const TopoDS_Wire& aWire = TopoDS::Wire( aShape->Shape() );
180 if ( !aWire.IsNull() ) {
181 TopExp_Explorer anExp( aWire, TopAbs_EDGE );
182 for ( ; anExp.More(); anExp.Next())
184 const TopoDS_Edge& anEdge = TopoDS::Edge(anExp.Current());
185 if ( !anEdge.IsNull() ) {
186 Standard_Real aFirst, aLast;
187 Handle(Geom_Curve) aCurve = BRep_Tool::Curve( anEdge, aFirst, aLast );
189 if ( aCurve->IsKind( STANDARD_TYPE(Geom_BSplineCurve) ) ) {
190 Handle(Geom_BSplineCurve) aBSplineCurve = Handle(Geom_BSplineCurve)::DownCast( aCurve );
191 if ( !aBSplineCurve.IsNull() ) {
192 isFound = hasProjectPointOnCurve( theView, theX, theY, aCurve, aParameter );
194 aPoint = aBSplineCurve->Value( aParameter );
195 Standard_Integer anI1, anI2;
196 aBSplineCurve->LocateU( aParameter, LOCAL_SELECTION_TOLERANCE, anI1, anI2 );
198 aPnt1 = aBSplineCurve->Value( aBSplineCurve->Knot( anI1 ) );
199 aPnt2 = aBSplineCurve->Value( aBSplineCurve->Knot( anI2 ) );
216 bool CurveCreator_Utils::hasProjectPointOnCurve( Handle(V3d_View) theView,
217 const int theX, const int theY,
218 const Handle(Geom_Curve)& theCurve,
219 Standard_Real& theParameter )
221 bool isFound = false;
222 if ( theView.IsNull() )
225 gp_Pnt aPoint = CurveCreator_Utils::ConvertClickToPoint( theX, theY, theView );
227 GeomAPI_ProjectPointOnCurve aProj( aPoint, theCurve );
228 Standard_Integer aNbPoint = aProj.NbPoints();
230 for (Standard_Integer j = 1; j <= aNbPoint && !isFound; j++) {
231 gp_Pnt aNewPoint = aProj.Point( j );
232 theParameter = aProj.Parameter( j );
235 CurveCreator_Utils::ConvertPointToClick( aNewPoint, theView, aX, anY );
237 int aXDelta = abs( aX - theX );
238 int anYDelta = abs( anY - theY );
239 isFound = aXDelta < SCENE_PIXEL_TOLERANCE && anYDelta < SCENE_PIXEL_TOLERANCE;