Salome HOME
Issue #1649: Added options to create plane by three points;
[modules/shaper.git] / src / GeomAPI / GeomAPI_Face.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAPI_Face.cpp
4 // Created:     2 Dec 2014
5 // Author:      Artem ZHIDKOV
6
7 #include "GeomAPI_Face.h"
8
9 #include "GeomAPI_Dir.h"
10 #include "GeomAPI_Pln.h"
11 #include "GeomAPI_Pnt.h"
12
13 #include <BRep_Tool.hxx>
14 #include <BRepAdaptor_Surface.hxx>
15 #include <Geom_Surface.hxx>
16 #include <Geom_CylindricalSurface.hxx>
17 #include <Geom_RectangularTrimmedSurface.hxx>
18 #include <TopoDS.hxx>
19 #include <TopoDS_Face.hxx>
20
21 GeomAPI_Face::GeomAPI_Face()
22   : GeomAPI_Shape()
23 {
24 }
25
26 GeomAPI_Face::GeomAPI_Face(const std::shared_ptr<GeomAPI_Shape>& theShape)
27 {
28   if (!theShape->isNull() && theShape->isFace()) {
29     setImpl(new TopoDS_Shape(theShape->impl<TopoDS_Shape>()));
30   }
31 }
32
33 bool GeomAPI_Face::isEqual(std::shared_ptr<GeomAPI_Shape> theFace) const
34 {
35   if (!theFace.get())
36     return false;
37
38   if (!theFace->isFace())
39     return false;
40
41   const TopoDS_Shape& aMyShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
42   const TopoDS_Shape& aInShape = theFace->impl<TopoDS_Shape>();
43
44   Handle(Geom_Surface) aMySurf = BRep_Tool::Surface(TopoDS::Face(aMyShape));
45   Handle(Geom_Surface) aInSurf = BRep_Tool::Surface(TopoDS::Face(aInShape));
46
47   // Check that surfaces a the same type
48   if (aMySurf->DynamicType() != aInSurf->DynamicType())
49     return false;
50
51   // Get parameters of surfaces
52   double aMyUMin, aMyUMax, aMyVMin, aMyVMax;
53   aMySurf->Bounds(aMyUMin, aMyUMax, aMyVMin, aMyVMax);
54   double aInUMin, aInUMax, aInVMin, aInVMax;
55   aInSurf->Bounds(aInUMin, aInUMax, aInVMin, aInVMax);
56
57   // Check that parameters are the same
58   if (fabs(aMyUMin - aInUMin) > Precision::PConfusion() ||
59       fabs(aMyUMax - aInUMax) > Precision::PConfusion() ||
60       fabs(aMyVMin - aInVMin) > Precision::PConfusion() ||
61       fabs(aMyVMax - aInVMax) > Precision::PConfusion())
62     return false;
63
64   return true;
65 }
66
67 bool GeomAPI_Face::isCylindrical() const
68 {
69   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
70   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
71   Handle(Geom_RectangularTrimmedSurface) aTrimmed = 
72     Handle(Geom_RectangularTrimmedSurface)::DownCast(aSurf);
73   if (!aTrimmed.IsNull())
74     aSurf = aTrimmed->BasisSurface();
75   return aSurf->IsKind(STANDARD_TYPE(Geom_CylindricalSurface)) == Standard_True;
76 }
77
78 std::shared_ptr<GeomAPI_Pln> GeomAPI_Face::getPlane() const
79 {
80   const TopoDS_Shape& aShape = const_cast<GeomAPI_Face*>(this)->impl<TopoDS_Shape>();
81   BRepAdaptor_Surface aSurfAdapt(TopoDS::Face(aShape));
82
83   if (aSurfAdapt.GetType() != GeomAbs_Plane)
84     return std::shared_ptr<GeomAPI_Pln>();
85
86   // Obtain central point
87   double aUMin, aUMax, aVMin, aVMax;
88   aUMin = aSurfAdapt.FirstUParameter();
89   aUMax = aSurfAdapt.LastUParameter();
90   aVMin = aSurfAdapt.FirstVParameter();
91   aVMax = aSurfAdapt.LastVParameter();
92   gp_Pnt aCentralPnt;
93   gp_Vec aDU, aDV;
94   aSurfAdapt.D1((aUMin+aUMax)*0.5, (aVMin+aVMax)*0.5, aCentralPnt, aDU, aDV);
95   std::shared_ptr<GeomAPI_Pnt> aCenter(
96       new GeomAPI_Pnt(aCentralPnt.X(), aCentralPnt.Y(), aCentralPnt.Z()));
97
98   // Obtain plane direction
99   gp_XYZ aNormalVec = aDU.XYZ().Crossed(aDV.XYZ());
100   if (aNormalVec.SquareModulus() < Precision::Confusion() * Precision::Confusion())
101     return std::shared_ptr<GeomAPI_Pln>();
102   std::shared_ptr<GeomAPI_Dir> aNormal(
103       new GeomAPI_Dir(aNormalVec.X(), aNormalVec.Y(), aNormalVec.Z()));
104
105   std::shared_ptr<GeomAPI_Pln> aResult(new GeomAPI_Pln(aCenter, aNormal));
106   return aResult;
107 }