Salome HOME
IMP 0021064: correct implementation
[modules/geom.git] / src / GEOMImpl / GEOMImpl_PointDriver.cxx
1 //  Copyright (C) 2007-2010  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.
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 #include <Standard_Stream.hxx>
23
24 #include <GEOMImpl_PointDriver.hxx>
25 #include <GEOMImpl_IPoint.hxx>
26 #include <GEOMImpl_Types.hxx>
27 #include <GEOM_Function.hxx>
28
29 #include <ShapeAnalysis.hxx>
30
31 #include <BRep_Builder.hxx>
32 #include <BRep_Tool.hxx>
33 #include <BRepAdaptor_Curve.hxx>
34 #include <BRepBuilderAPI_MakeVertex.hxx>
35 #include <BRepExtrema_DistShapeShape.hxx>
36
37 #include <TopAbs.hxx>
38 #include <TopExp.hxx>
39 #include <TopoDS.hxx>
40 #include <TopoDS_Edge.hxx>
41 #include <TopoDS_Face.hxx>
42 #include <TopoDS_Shape.hxx>
43 #include <TopoDS_Vertex.hxx>
44 #include <TopoDS_Compound.hxx>
45
46 #include <GCPnts_AbscissaPoint.hxx>
47 #include <IntTools.hxx>
48
49 #include <Geom_Curve.hxx>
50 #include <Geom_Surface.hxx>
51
52 #include <gp_Pnt.hxx>
53
54 #include <Precision.hxx>
55
56 #include <Standard_NullObject.hxx>
57
58 //=======================================================================
59 //function : GetID
60 //purpose  :
61 //=======================================================================
62 const Standard_GUID& GEOMImpl_PointDriver::GetID()
63 {
64   static Standard_GUID aPointDriver("FF1BBB02-5D14-4df2-980B-3A668264EA16");
65   return aPointDriver;
66 }
67
68
69 //=======================================================================
70 //function : GEOMImpl_PointDriver
71 //purpose  :
72 //=======================================================================
73 GEOMImpl_PointDriver::GEOMImpl_PointDriver()
74 {
75 }
76
77 //=======================================================================
78 //function : getExtremaSolution
79 //purpose  : local function
80 //=======================================================================
81 static Standard_Boolean getExtremaSolution
82 (const gp_Pnt&       theInitPnt,
83  const TopoDS_Shape& theRefShape,
84  gp_Pnt& thePnt)
85 {
86   BRepBuilderAPI_MakeVertex mkVertex (theInitPnt);
87   TopoDS_Vertex anInitV = TopoDS::Vertex(mkVertex.Shape());
88   
89   BRepExtrema_DistShapeShape anExt (anInitV, theRefShape);
90   if ( !anExt.IsDone() || anExt.NbSolution() < 1 )
91     return Standard_False;
92   thePnt = anExt.PointOnShape2(1);
93   Standard_Real aMinDist2 = theInitPnt.SquareDistance( thePnt );
94   for ( Standard_Integer j = 2, jn = anExt.NbSolution(); j <= jn; j++ )
95   {
96     gp_Pnt aPnt = anExt.PointOnShape2(j);
97     Standard_Real aDist2 = theInitPnt.SquareDistance( aPnt );
98     if ( aDist2 > aMinDist2)
99       continue;
100     aMinDist2 = aDist2;
101     thePnt = aPnt;
102   }
103   return Standard_True;
104 }
105
106 //=======================================================================
107 //function : Execute
108 //purpose  :
109 //=======================================================================
110 Standard_Integer GEOMImpl_PointDriver::Execute(TFunction_Logbook& log) const
111 {
112   if (Label().IsNull())  return 0;
113   Handle(GEOM_Function) aFunction = GEOM_Function::GetFunction(Label());
114
115   GEOMImpl_IPoint aPI (aFunction);
116   Standard_Integer aType = aFunction->GetType();
117
118   gp_Pnt aPnt;
119   TopoDS_Compound aCompound;
120   bool retCompound = false;
121
122   if (aType == POINT_XYZ) {
123     aPnt = gp_Pnt(aPI.GetX(), aPI.GetY(), aPI.GetZ());
124   }
125   else if (aType == POINT_XYZ_REF) {
126     Handle(GEOM_Function) aRefPoint = aPI.GetRef();
127     TopoDS_Shape aRefShape = aRefPoint->GetValue();
128     if (aRefShape.ShapeType() != TopAbs_VERTEX) {
129       Standard_TypeMismatch::Raise
130         ("Point creation aborted : referenced shape is not a vertex");
131     }
132     gp_Pnt P = BRep_Tool::Pnt(TopoDS::Vertex(aRefShape));
133     aPnt = gp_Pnt(P.X() + aPI.GetX(), P.Y() + aPI.GetY(), P.Z() + aPI.GetZ());
134   }
135   else if (aType == POINT_CURVE_PAR) {
136     Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
137     TopoDS_Shape aRefShape = aRefCurve->GetValue();
138     if (aRefShape.ShapeType() != TopAbs_EDGE) {
139       Standard_TypeMismatch::Raise
140         ("Point On Curve creation aborted : curve shape is not an edge");
141     }
142     Standard_Real aFP, aLP, aP;
143     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(TopoDS::Edge(aRefShape), aFP, aLP);
144     aP = aFP + (aLP - aFP) * aPI.GetParameter();
145     aPnt = aCurve->Value(aP);
146   }
147   else if (aType == POINT_CURVE_COORD) {
148     Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
149     TopoDS_Shape aRefShape = aRefCurve->GetValue();
150     if (aRefShape.ShapeType() != TopAbs_EDGE) {
151       Standard_TypeMismatch::Raise
152         ("Point On Curve creation aborted : curve shape is not an edge");
153     }
154     gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
155     if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
156       Standard_ConstructionError::Raise
157         ("Point On Curve creation aborted : cannot project point");
158     }
159   }
160   else if (aType == POINT_CURVE_LENGTH) {
161     // RefCurve
162     Handle(GEOM_Function) aRefCurve = aPI.GetCurve();
163     if (aRefCurve.IsNull()) {
164       Standard_NullObject::Raise
165         ("Point On Curve creation aborted : curve object is null");
166     }
167     TopoDS_Shape aRefShape1 = aRefCurve->GetValue();
168     if (aRefShape1.ShapeType() != TopAbs_EDGE) {
169       Standard_TypeMismatch::Raise
170         ("Point On Curve creation aborted : curve shape is not an edge");
171     }
172     TopoDS_Edge aRefEdge = TopoDS::Edge(aRefShape1);
173     TopoDS_Vertex V1, V2;
174     TopExp::Vertices(aRefEdge, V1, V2, Standard_True);
175
176     // RefPoint
177     TopoDS_Vertex aRefVertex;
178     Handle(GEOM_Function) aRefPoint = aPI.GetRef();
179     if (aRefPoint.IsNull()) {
180       aRefVertex = V1;
181     }
182     else {
183       TopoDS_Shape aRefShape2 = aRefPoint->GetValue();
184       if (aRefShape2.ShapeType() != TopAbs_VERTEX) {
185         Standard_TypeMismatch::Raise
186           ("Point On Curve creation aborted : start point shape is not a vertex");
187       }
188       aRefVertex = TopoDS::Vertex(aRefShape2);
189     }
190     gp_Pnt aRefPnt = BRep_Tool::Pnt(aRefVertex);
191
192     // Length
193     Standard_Real aLength = aPI.GetLength();
194     //Standard_Real theCurveLength = IntTools::Length(aRefEdge);
195     //if (aLength > theCurveLength) {
196     //  Standard_ConstructionError::Raise
197     //    ("Point On Curve creation aborted : given length is greater than edges length");
198     //}
199
200     // Check orientation
201     Standard_Real UFirst, ULast;
202     Handle(Geom_Curve) EdgeCurve = BRep_Tool::Curve(aRefEdge, UFirst, ULast);
203     Handle(Geom_Curve) ReOrientedCurve = EdgeCurve;
204
205     Standard_Real dU = ULast - UFirst;
206     Standard_Real par1 = UFirst + 0.1 * dU;
207     Standard_Real par2 = ULast  - 0.1 * dU;
208
209     gp_Pnt P1 = EdgeCurve->Value(par1);
210     gp_Pnt P2 = EdgeCurve->Value(par2);
211
212     if (aRefPnt.SquareDistance(P2) < aRefPnt.SquareDistance(P1)) {
213       ReOrientedCurve = EdgeCurve->Reversed();
214       UFirst = EdgeCurve->ReversedParameter(ULast);
215     }
216
217     // Get the point by length
218     GeomAdaptor_Curve AdapCurve = GeomAdaptor_Curve(ReOrientedCurve);
219     GCPnts_AbscissaPoint anAbsPnt (AdapCurve, aLength, UFirst); 
220     Standard_Real aParam = anAbsPnt.Parameter();
221     aPnt = AdapCurve.Value(aParam);
222   }
223   else if (aType == POINT_SURFACE_PAR) {
224     Handle(GEOM_Function) aRefCurve = aPI.GetSurface();
225     TopoDS_Shape aRefShape = aRefCurve->GetValue();
226     if (aRefShape.ShapeType() != TopAbs_FACE) {
227       Standard_TypeMismatch::Raise
228         ("Point On Surface creation aborted : surface shape is not a face");
229     }
230     TopoDS_Face F = TopoDS::Face(aRefShape);
231     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(F);
232     Standard_Real U1,U2,V1,V2;
233     //aSurf->Bounds(U1,U2,V1,V2);
234     ShapeAnalysis::GetFaceUVBounds(F,U1,U2,V1,V2);
235     Standard_Real U = U1 + (U2-U1) * aPI.GetParameter();
236     Standard_Real V = V1 + (V2-V1) * aPI.GetParameter2();
237     aPnt = aSurf->Value(U,V);
238   }
239   else if (aType == POINT_SURFACE_COORD) {
240     Handle(GEOM_Function) aRefCurve = aPI.GetSurface();
241     TopoDS_Shape aRefShape = aRefCurve->GetValue();
242     if (aRefShape.ShapeType() != TopAbs_FACE) {
243       Standard_TypeMismatch::Raise
244         ("Point On Surface creation aborted : surface shape is not a face");
245     }
246     gp_Pnt anInitPnt (aPI.GetX(), aPI.GetY(), aPI.GetZ());
247     if (!getExtremaSolution(anInitPnt, aRefShape, aPnt)) {
248       Standard_ConstructionError::Raise
249         ("Point On Surface creation aborted : cannot project point");
250     }
251   }
252   else if (aType == POINT_LINES_INTERSECTION) {
253     Handle(GEOM_Function) aRef1 = aPI.GetLine1();
254     Handle(GEOM_Function) aRef2 = aPI.GetLine2();
255
256     TopoDS_Shape aRefShape1 = aRef1->GetValue();
257     TopoDS_Shape aRefShape2 = aRef2->GetValue();
258
259     if ( (aRefShape1.ShapeType() != TopAbs_EDGE && aRefShape1.ShapeType() != TopAbs_WIRE)
260       || (aRefShape2.ShapeType() != TopAbs_EDGE && aRefShape2.ShapeType() != TopAbs_WIRE) ) {
261       Standard_TypeMismatch::Raise
262         ("Creation Point On Lines Intersection Aborted : Line shape is not an edge or wire");
263     }
264
265     if (aRefShape1.IsSame(aRefShape2))
266       Standard_ConstructionError::Raise("The lines to make intersection must be different");
267
268     //Calculate Lines Intersection Point
269     BRepExtrema_DistShapeShape dst (aRefShape1, aRefShape2);
270     if (dst.IsDone()) {
271       gp_Pnt P1, P2;
272       BRep_Builder B;
273       B.MakeCompound( aCompound );
274       for (int i = 1; i <= dst.NbSolution(); i++) {
275         P1 = dst.PointOnShape1(i);
276         P2 = dst.PointOnShape2(i);
277         Standard_Real Dist = P1.Distance(P2);
278         if ( Dist <= Precision::Confusion() && dst.NbSolution() > 1) {
279           BRepBuilderAPI_MakeVertex mkVertex (P1);
280           B.Add(aCompound, mkVertex.Shape());
281           retCompound = true;
282         } else if ( Dist <= Precision::Confusion() ) {
283           aPnt = P1;
284         } else {
285           Standard_TypeMismatch::Raise ("Shapes have not an Intersection Point");
286         }
287       }
288     }
289   }
290   else {
291     return 0;
292   }
293
294   TopoDS_Shape aShape;
295   if ( retCompound ) {
296     aShape = aCompound;
297   } else {
298     BRepBuilderAPI_MakeVertex mkVertex (aPnt);
299     aShape = mkVertex.Shape();
300   }
301
302   //aShape.Infinite(Standard_True); // VSR: 05/04/2010: Fix 20668 (Fit All for points & lines)
303   aFunction->SetValue(aShape);
304
305   log.SetTouched(Label());
306
307   return 1;
308 }
309
310
311 //=======================================================================
312 //function :  GEOMImpl_PointDriver_Type_
313 //purpose  :
314 //=======================================================================
315 Standard_EXPORT Handle_Standard_Type& GEOMImpl_PointDriver_Type_()
316 {
317
318   static Handle_Standard_Type aType1 = STANDARD_TYPE(TFunction_Driver);
319   if ( aType1.IsNull()) aType1 = STANDARD_TYPE(TFunction_Driver);
320   static Handle_Standard_Type aType2 = STANDARD_TYPE(MMgt_TShared);
321   if ( aType2.IsNull()) aType2 = STANDARD_TYPE(MMgt_TShared);
322   static Handle_Standard_Type aType3 = STANDARD_TYPE(Standard_Transient);
323   if ( aType3.IsNull()) aType3 = STANDARD_TYPE(Standard_Transient);
324
325
326   static Handle_Standard_Transient _Ancestors[]= {aType1,aType2,aType3,NULL};
327   static Handle_Standard_Type _aType = new Standard_Type("GEOMImpl_PointDriver",
328                                                          sizeof(GEOMImpl_PointDriver),
329                                                          1,
330                                                          (Standard_Address)_Ancestors,
331                                                          (Standard_Address)NULL);
332
333   return _aType;
334 }
335
336 //=======================================================================
337 //function : DownCast
338 //purpose  :
339 //=======================================================================
340
341 const Handle(GEOMImpl_PointDriver) Handle(GEOMImpl_PointDriver)::DownCast(const Handle(Standard_Transient)& AnObject)
342 {
343   Handle(GEOMImpl_PointDriver) _anOtherObject;
344
345   if (!AnObject.IsNull()) {
346      if (AnObject->IsKind(STANDARD_TYPE(GEOMImpl_PointDriver))) {
347        _anOtherObject = Handle(GEOMImpl_PointDriver)((Handle(GEOMImpl_PointDriver)&)AnObject);
348      }
349   }
350
351   return _anOtherObject ;
352 }