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