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