Salome HOME
Merge branch 'V7_dev'
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PointDriver.cxx
1 // Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
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, or (at your option) any later version.
10 //
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.
15 //
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
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include <Standard_Stream.hxx>
24
25 #include <GEOMImpl_PointDriver.hxx>
26 #include <GEOMImpl_IPoint.hxx>
27 #include <GEOMImpl_Types.hxx>
28 #include <GEOM_Function.hxx>
29 #include <GEOMAlgo_AlgoTools.hxx>
30
31 #include <ShapeAnalysis.hxx>
32
33 #include <BRep_Builder.hxx>
34 #include <BRep_Tool.hxx>
35 #include <BRepAdaptor_Curve.hxx>
36 #include <BRepBuilderAPI_MakeVertex.hxx>
37 #include <BRepExtrema_DistShapeShape.hxx>
38
39 #include <TopAbs.hxx>
40 #include <TopExp.hxx>
41 #include <TopoDS.hxx>
42 #include <TopoDS_Edge.hxx>
43 #include <TopoDS_Face.hxx>
44 #include <TopoDS_Shape.hxx>
45 #include <TopoDS_Vertex.hxx>
46 #include <TopoDS_Compound.hxx>
47 #include <TopoDS_Iterator.hxx>
48
49 #include <GCPnts_AbscissaPoint.hxx>
50 #include <IntTools.hxx>
51
52 #include <Geom_Curve.hxx>
53 #include <Geom_Surface.hxx>
54
55 #include <gp_Pnt.hxx>
56
57 #include <Precision.hxx>
58
59 #include <Standard_NullObject.hxx>
60
61 //=======================================================================
62 //function : GetID
63 //purpose  :
64 //=======================================================================
65 const Standard_GUID& GEOMImpl_PointDriver::GetID()
66 {
67   static Standard_GUID aPointDriver("FF1BBB02-5D14-4df2-980B-3A668264EA16");
68   return aPointDriver;
69 }
70
71
72 //=======================================================================
73 //function : GEOMImpl_PointDriver
74 //purpose  :
75 //=======================================================================
76 GEOMImpl_PointDriver::GEOMImpl_PointDriver()
77 {
78 }
79
80 //=======================================================================
81 //function : getExtremaSolution
82 //purpose  : local function
83 //=======================================================================
84 static Standard_Boolean getExtremaSolution
85 (const gp_Pnt&       theInitPnt,
86  const TopoDS_Shape& theRefShape,
87  gp_Pnt& thePnt)
88 {
89   BRepBuilderAPI_MakeVertex mkVertex (theInitPnt);
90   TopoDS_Vertex anInitV = TopoDS::Vertex(mkVertex.Shape());
91   
92   BRepExtrema_DistShapeShape anExt (anInitV, theRefShape);
93   if ( !anExt.IsDone() || anExt.NbSolution() < 1 )
94     return Standard_False;
95   thePnt = anExt.PointOnShape2(1);
96   Standard_Real aMinDist2 = theInitPnt.SquareDistance( thePnt );
97   for ( Standard_Integer j = 2, jn = anExt.NbSolution(); j <= jn; j++ )
98   {
99     gp_Pnt aPnt = anExt.PointOnShape2(j);
100     Standard_Real aDist2 = theInitPnt.SquareDistance( aPnt );
101     if ( aDist2 > aMinDist2)
102       continue;
103     aMinDist2 = aDist2;
104     thePnt = aPnt;
105   }
106   return Standard_True;
107 }
108
109 //=======================================================================
110 //function : Execute
111 //purpose  :
112 //=======================================================================
113 Standard_Integer GEOMImpl_PointDriver::Execute(TFunction_Logbook& log) const
114 {
115   if (Label().IsNull())  return 0;
116   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
117
118   GEOMImpl_IPoint aPI (aFunction);
119   Standard_Integer aType = aFunction->GetType();
120
121   gp_Pnt aPnt;
122   TopoDS_Compound aCompound;
123   bool retCompound = false;
124
125   if (aType == POINT_XYZ) {
126     aPnt = gp_Pnt(aPI.GetX(), aPI.GetY(), aPI.GetZ());
127   }
128   else if (aType == POINT_XYZ_REF) {
129     Handle(GEOM_Function) aRefFunc = aPI.GetRef();
130     TopoDS_Shape aRefShape = aRefFunc->GetValue();
131     if (aRefShape.ShapeType() != TopAbs_VERTEX) {
132       Standard_TypeMismatch::Raise
133         ("Point creation aborted : referenced shape is not a vertex");
134     }
135     gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(aRefShape));
136     aPnt = gp_Pnt(P.X() + aPI.GetX(), P.Y() + aPI.GetY(), P.Z() + aPI.GetZ());
137   }
138   else if (aType == POINT_CURVE_PAR) {
139     Handle(GEOM_Function) aRefFunc = aPI.GetCurve();
140     TopoDS_Shape aRefShape = aRefFunc->GetValue();
141     if (aRefShape.ShapeType() != TopAbs_EDGE) {
142       Standard_TypeMismatch::Raise
143         ("Point On Curve creation aborted : curve shape is not an edge");
144     }
145     Standard_Real aFP, aLP, aP;
146     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFP, aLP);
147     if ( !aCurve.IsNull() ) {
148       if (aPI.GetTakeOrientationIntoAccount() &&
149           aRefShape.Orientation() == TopAbs_REVERSED) {
150         aP = 1. - aPI.GetParameter();
151       } else {
152         aP = aPI.GetParameter();
153       }
154
155       aP = aFP + (aLP - aFP) * aP;
156       aPnt = aCurve->Value(aP);
157     }
158     else {
159       // null curve, e.g. degenerated edge
160       TopoDS_Iterator It(aRefShape, Standard_False, Standard_False);
161       TopoDS_Vertex aVertex;
162       if ( It.More() ) {
163         TopoDS_Shape aShape = It.Value();
164         if ( !aShape.IsNull() )
165           aVertex = TopoDS::Vertex( aShape );
166       }
167       if ( !aVertex.IsNull() ) {
168         aPnt = BRep_Tool::Pnt( aVertex );
169       }
170       else {
171         Standard_TypeMismatch::Raise
172           ("Point On Curve creation aborted : null curve");
173       }
174     }
175   }
176   else if (aType == POINT_CURVE_COORD) {
177     Handle(GEOM_Function) aRefFunc = aPI.GetCurve();
178     TopoDS_Shape aRefShape = aRefFunc->GetValue();
179     if (aRefShape.ShapeType() != TopAbs_EDGE) {
180       Standard_TypeMismatch::Raise
181         ("Point On Curve creation aborted : curve shape is not an edge");
182     }
183     gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
184     if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
185       Standard_ConstructionError::Raise
186         ("Point On Curve creation aborted : cannot project point");
187     }
188   }
189   else if (aType == POINT_CURVE_LENGTH) {
190     // RefCurve
191     Handle(GEOM_Function) aRefFunc = aPI.GetCurve();
192     if (aRefFunc.IsNull()) {
193       Standard_NullObject::Raise
194         ("Point On Curve creation aborted : curve object is null");
195     }
196     TopoDS_Shape aRefShape1 = aRefFunc->GetValue();
197     if (aRefShape1.ShapeType() != TopAbs_EDGE) {
198       Standard_TypeMismatch::Raise
199         ("Point On Curve creation aborted : curve shape is not an edge");
200     }
201     TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
202     TopoDS_Vertex V1, V2;
203     TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
204
205     // RefPoint
206     TopoDS_Vertex aRefVertex;
207     Handle(GEOM_Function) aRefPoint = aPI.GetRef();
208     if (aRefPoint.IsNull()) {
209       aRefVertex = V1;
210     }
211     else {
212       TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
213       if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
214         Standard_TypeMismatch::Raise
215           ("Point On Curve creation aborted : start point shape is not a vertex");
216       }
217       aRefVertex = TopoDS::Vertex(aRefShape2);
218     }
219     gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
220
221     // Length
222     Standard_Real aLength = aPI.GetLength();
223     //Standard_Real theCurveLength = IntTools::Length(aRefEdge);
224     //if (aLength > theCurveLength) {
225     //  Standard_ConstructionError::Raise
226     //    ("Point On Curve creation aborted : given length is greater than edges length");
227     //}
228
229     // Check orientation
230     Standard_Real UFirst, ULast;
231     Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
232
233     if ( !EdgeCurve.IsNull() ) {
234       Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
235
236       Standard_Real dU = ULast - UFirst;
237       Standard_Real par1 = UFirst + 0.1 * dU;
238       Standard_Real par2 = ULast  - 0.1 * dU;
239       
240       gp_Pnt P1 = EdgeCurve->Value(par1);
241       gp_Pnt P2 = EdgeCurve->Value(par2);
242       
243       if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
244         ReOrientedCurve = EdgeCurve->Reversed();
245         UFirst = EdgeCurve->ReversedParameter(ULast);
246       }
247       
248       // Get the point by length
249       GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
250       GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst); 
251       Standard_Real aParam = anAbsPnt.Parameter();
252       aPnt = AdapCurve.Value(aParam);
253     }
254     else {
255       // null curve, e.g. degenerated edge
256       TopoDS_Iterator It(aRefEdge, Standard_False, Standard_False);
257       TopoDS_Vertex aVertex;
258       if ( It.More() ) {
259         TopoDS_Shape aShape = It.Value();
260         if ( !aShape.IsNull() )
261           aVertex = TopoDS::Vertex( aShape );
262       }
263       if ( !aVertex.IsNull() ) {
264         aPnt = BRep_Tool::Pnt( aVertex );
265       }
266       else {
267         Standard_TypeMismatch::Raise
268           ("Point On Curve creation aborted : null curve");
269       }
270     }
271   }
272   else if (aType == POINT_SURFACE_PAR) {
273     Handle(GEOM_Function) aRefFunc = aPI.GetSurface();
274     TopoDS_Shape aRefShape = aRefFunc->GetValue();
275     if (aRefShape.ShapeType() != TopAbs_FACE) {
276       Standard_TypeMismatch::Raise
277         ("Point On Surface creation aborted : surface shape is not a face");
278     }
279     TopoDS_Face F = TopoDS::Face(aRefShape);
280     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
281     Standard_Real U1,U2,V1,V2;
282     //aSurf->Bounds(U1,U2,V1,V2);
283     ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
284     Standard_Real U = U1 + (U2-U1) * aPI.GetParameter();
285     Standard_Real V = V1 + (V2-V1) * aPI.GetParameter2();
286     aPnt = aSurf->Value(U,V);
287   }
288   else if (aType == POINT_SURFACE_COORD) {
289     Handle(GEOM_Function) aRefFunc = aPI.GetSurface();
290     TopoDS_Shape aRefShape = aRefFunc->GetValue();
291     if (aRefShape.ShapeType() != TopAbs_FACE) {
292       Standard_TypeMismatch::Raise
293         ("Point On Surface creation aborted : surface shape is not a face");
294     }
295     gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
296     if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
297       Standard_ConstructionError::Raise
298         ("Point On Surface creation aborted : cannot project point");
299     }
300   }
301   else if (aType == POINT_FACE_ANY) {
302     Handle(GEOM_Function) aRefFunc = aPI.GetSurface();
303     TopoDS_Shape aRefShape = aRefFunc->GetValue();
304     if (aRefShape.ShapeType() != TopAbs_FACE) {
305       Standard_TypeMismatch::Raise
306         ("Point On Surface creation aborted : surface shape is not a face");
307     }
308     TopoDS_Face F = TopoDS::Face(aRefShape);
309     gp_Pnt2d aP2d;
310     GEOMAlgo_AlgoTools::PntInFace(F, aPnt, aP2d);
311   }
312   else if (aType == POINT_LINES_INTERSECTION) {
313     Handle(GEOM_Function) aRef1 = aPI.GetLine1();
314     Handle(GEOM_Function) aRef2 = aPI.GetLine2();
315
316     TopoDS_Shape aRefShape1 = aRef1->GetValue();
317     TopoDS_Shape aRefShape2 = aRef2->GetValue();
318
319     if ( (aRefShape1.ShapeType() != TopAbs_EDGE && aRefShape1.ShapeType() != TopAbs_WIRE)
320       || (aRefShape2.ShapeType() != TopAbs_EDGE && aRefShape2.ShapeType() != TopAbs_WIRE) ) {
321       Standard_TypeMismatch::Raise
322         ("Creation Point On Lines Intersection Aborted : Line shape is not an edge or wire");
323     }
324
325     if (aRefShape1.IsSame(aRefShape2))
326       Standard_ConstructionError::Raise("The lines to make intersection must be different");
327
328     //Calculate Lines Intersection Point
329     BRepExtrema_DistShapeShape dst (aRefShape1, aRefShape2);
330     if (dst.IsDone()) {
331       gp_Pnt P1, P2;
332       BRep_Builder B;
333       B.MakeCompound( aCompound );
334       for (int i = 1; i <= dst.NbSolution(); i++) {
335         P1 = dst.PointOnShape1(i);
336         P2 = dst.PointOnShape2(i);
337         Standard_Real Dist = P1.Distance(P2);
338         if ( Dist <= Precision::Confusion() && dst.NbSolution() > 1) {
339           BRepBuilderAPI_MakeVertex mkVertex (P1);
340           B.Add(aCompound, mkVertex.Shape());
341           retCompound = true;
342         } else if ( Dist <= Precision::Confusion() ) {
343           aPnt = P1;
344         } else {
345           Standard_TypeMismatch::Raise ("Shapes have not an Intersection Point");
346         }
347       }
348     }
349   }
350   else {
351     return 0;
352   }
353
354   TopoDS_Shape aShape;
355   if ( retCompound ) {
356     aShape = aCompound;
357   } else {
358     BRepBuilderAPI_MakeVertex mkVertex (aPnt);
359     aShape = mkVertex.Shape();
360   }
361
362   //aShape.Infinite(Standard_True); // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
363   aFunction->SetValue(aShape);
364
365   log.SetTouched(Label());
366
367   return 1;
368 }
369
370 //================================================================================
371 /*!
372  * \brief Returns a name of creation operation and names and values of creation parameters
373  */
374 //================================================================================
375
376 bool GEOMImpl_PointDriver::
377 GetCreationInformation(std::string&             theOperationName,
378                        std::vector<GEOM_Param>& theParams)
379 {
380   if (Label().IsNull()) return 0;
381   Handle(GEOM_Function) function = GEOM_Function::GetFunction(Label());
382
383   GEOMImpl_IPoint aCI( function );
384   Standard_Integer aType = function->GetType();
385
386   theOperationName = "POINT";
387
388   switch ( aType ) {
389   case POINT_XYZ:
390     AddParam( theParams, "X", aCI.GetX() );
391     AddParam( theParams, "Y", aCI.GetY() );
392     AddParam( theParams, "Z", aCI.GetZ() );
393     break;
394   case POINT_XYZ_REF:
395     AddParam( theParams, "Point", aCI.GetRef() );
396     AddParam( theParams, "Dx", aCI.GetX() );
397     AddParam( theParams, "Dy", aCI.GetY() );
398     AddParam( theParams, "Dz", aCI.GetZ() );
399     break;
400   case POINT_CURVE_PAR:
401     AddParam( theParams, "Edge", aCI.GetCurve() );
402     AddParam( theParams, "Parameter", aCI.GetParameter() );
403     AddParam( theParams, "Use Orientation", aCI.GetTakeOrientationIntoAccount() );
404     break;
405   case POINT_CURVE_COORD:
406     AddParam( theParams, "X", aCI.GetX() );
407     AddParam( theParams, "Y", aCI.GetY() );
408     AddParam( theParams, "Z", aCI.GetZ() );
409     AddParam( theParams, "Edge", aCI.GetCurve() );
410     break;
411   case POINT_CURVE_LENGTH:
412     AddParam( theParams, "Edge", aCI.GetCurve() );
413     AddParam( theParams, "Start Point", aCI.GetRef(), "First Vertex" );
414     AddParam( theParams, "Length", aCI.GetLength() );
415     break;
416   case POINT_SURFACE_PAR:
417     AddParam( theParams, "Face", aCI.GetSurface() );
418     AddParam( theParams, "U-Parameter", aCI.GetParameter() );
419     AddParam( theParams, "V-Parameter", aCI.GetParameter2() );
420     break;
421   case POINT_SURFACE_COORD:
422     AddParam( theParams, "X", aCI.GetX() );
423     AddParam( theParams, "Y", aCI.GetY() );
424     AddParam( theParams, "Z", aCI.GetZ() );
425     AddParam( theParams, "Face", aCI.GetSurface() );
426     break;
427   case POINT_FACE_ANY:
428     AddParam( theParams, "Face", aCI.GetSurface() );
429     break;
430   case POINT_LINES_INTERSECTION:
431     AddParam( theParams, "Line 1", aCI.GetLine1() );
432     AddParam( theParams, "Line 2", aCI.GetLine2() );
433     break;
434   default:
435     return false;
436   }
437
438   return true;
439 }
440
441 IMPLEMENT_STANDARD_HANDLE (GEOMImpl_PointDriver,GEOM_BaseDriver);
442 IMPLEMENT_STANDARD_RTTIEXT (GEOMImpl_PointDriver,GEOM_BaseDriver);