1 // File: BRepBuilderAPI_FindPlane.cxx
2 // Created: Thu Nov 2 11:47:55 1995
3 // Author: Jing Cheng MEI
6 #include <BRepBuilderAPI_FindPlane.ixx>
8 #include <Precision.hxx>
12 #include <TColgp_SequenceOfPnt.hxx>
14 #include <TopLoc_Location.hxx>
15 #include <TopExp_Explorer.hxx>
16 #include <BRep_Tool.hxx>
18 #include <Geom_Line.hxx>
19 #include <Geom_Conic.hxx>
20 #include <Geom_Circle.hxx>
21 #include <Geom_Ellipse.hxx>
22 #include <Geom_Parabola.hxx>
23 #include <Geom_Hyperbola.hxx>
24 #include <Geom_BezierCurve.hxx>
25 #include <Geom_BSplineCurve.hxx>
29 //=======================================================================
30 //function : BRepBuilderAPI_FindPlane
32 //=======================================================================
34 BRepBuilderAPI_FindPlane::BRepBuilderAPI_FindPlane()
38 //=======================================================================
39 //function : BRepBuilderAPI_FindPlane
41 //=======================================================================
43 BRepBuilderAPI_FindPlane::BRepBuilderAPI_FindPlane(const TopoDS_Shape& S,
44 const Standard_Real Tol)
49 //=======================================================================
52 //=======================================================================
54 void BRepBuilderAPI_FindPlane::Init(const TopoDS_Shape& S,
55 const Standard_Real Tol)
57 Standard_Real tolerance = Tol;
60 // compute the tolerance
62 for (ex.Init(S,TopAbs_EDGE); ex.More(); ex.Next()) {
63 Standard_Real t = BRep_Tool::Tolerance(TopoDS::Edge(ex.Current()));
64 if (t > tolerance) tolerance = t;
67 Standard_Real tol2 = tolerance*tolerance;
68 // try to find an analytical curve and calculate points
70 Standard_Real first, last;
71 Standard_Boolean found = Standard_False;
73 TColgp_SequenceOfPnt points;
74 Standard_Integer nbPnts;
76 for (ex.Init(S,TopAbs_EDGE); ex.More(); ex.Next()) {
77 Handle(Geom_Curve) c3d =
78 BRep_Tool::Curve(TopoDS::Edge(ex.Current()), loc, first, last);
81 Handle(Geom_Curve) c3dptr =
82 Handle(Geom_Curve)::DownCast(c3d->Transformed(loc.Transformation()));
84 Handle(Standard_Type) cType = c3dptr->DynamicType();
86 if (cType == STANDARD_TYPE(Geom_Line)) {
89 else if ((cType == STANDARD_TYPE(Geom_Circle)) ||
90 (cType == STANDARD_TYPE(Geom_Ellipse)) ||
91 (cType == STANDARD_TYPE(Geom_Parabola)) ||
92 (cType == STANDARD_TYPE(Geom_Hyperbola))) {
95 found = Standard_True;
96 Handle(Geom_Conic) Co = Handle(Geom_Conic)::DownCast(c3dptr);
97 P = new Geom_Plane(gp_Ax3(Co->Position()));
100 else if (cType == STANDARD_TYPE(Geom_BezierCurve)) {
101 Handle(Geom_BezierCurve) Co =
102 Handle(Geom_BezierCurve)::DownCast(c3dptr);
103 nbPnts = Co->NbPoles();
105 else if (cType == STANDARD_TYPE(Geom_BSplineCurve)) {
106 Handle(Geom_BSplineCurve) Co =
107 Handle(Geom_BSplineCurve)::DownCast(c3dptr);
108 nbPnts = Co->NbPoles();
115 for (Standard_Integer i=1; i<=nbPnts; i++) {
117 c3dptr->D0(first, p0);
119 else if (i == nbPnts) {
120 c3dptr->D0(last, p0);
123 c3dptr->D0(first+(last-first)/(nbPnts-1)*(i-1), p0);
131 // try to find a plane with the points
132 if (points.Length() > 2) {
134 Standard_Real disMax = 0.0;
135 gp_Pnt p0 = points(1);
137 for (Standard_Integer i=2; i<=points.Length(); i++) {
138 Standard_Real dist = p0.SquareDistance(points(i));
141 p1 = points(i); // it will be faster to store the point, otherwise it is necessary to find a value in a sequence
146 gp_Vec V1(p0, p1), V3;
147 Standard_Real proMax = 0.0;
149 for (Standard_Integer j=2; j<=points.Length(); j++) {
150 V3 = V1^gp_Vec(p0, points(j));
151 Standard_Real pro = V3.SquareMagnitude();
158 if (p0.SquareDistance(p2) > tol2) {
159 gp_Dir D1(V1), D2(gp_Vec(p0, p2));
160 if (!D1.IsParallel(D2, Precision::Angular())) {
161 P = new Geom_Plane(gp_Ax3(p0, D1.Crossed(D2), D1));
162 found = Standard_True;
170 // test if all points are on the plane
171 const gp_Pln& pln = P->Pln();
172 for (Standard_Integer i=1; i<=points.Length(); i++) {
173 if (pln.SquareDistance(points(i)) > tol2) {
174 found = Standard_False;
185 //=======================================================================
188 //=======================================================================
190 Standard_Boolean BRepBuilderAPI_FindPlane::Found() const
192 return !myPlane.IsNull();
195 //=======================================================================
198 //=======================================================================
200 Handle(Geom_Plane) BRepBuilderAPI_FindPlane::Plane() const