1 // Created on: 1995-11-02
2 // Created by: Jing Cheng MEI
3 // Copyright (c) 1995-1999 Matra Datavision
4 // Copyright (c) 1999-2012 OPEN CASCADE SAS
6 // The content of this file is subject to the Open CASCADE Technology Public
7 // License Version 6.5 (the "License"). You may not use the content of this file
8 // except in compliance with the License. Please obtain a copy of the License
9 // at http://www.opencascade.org and read it completely before using this file.
11 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
12 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
14 // The Original Code and all software distributed under the License is
15 // distributed on an "AS IS" basis, without warranty of any kind, and the
16 // Initial Developer hereby disclaims all such warranties, including without
17 // limitation, any warranties of merchantability, fitness for a particular
18 // purpose or non-infringement. Please see the License for the specific terms
19 // and conditions governing the rights and limitations under the License.
22 #include <BRepBuilderAPI_FindPlane.ixx>
24 #include <Precision.hxx>
28 #include <TColgp_SequenceOfPnt.hxx>
30 #include <TopLoc_Location.hxx>
31 #include <TopExp_Explorer.hxx>
32 #include <BRep_Tool.hxx>
34 #include <Geom_Line.hxx>
35 #include <Geom_Conic.hxx>
36 #include <Geom_Circle.hxx>
37 #include <Geom_Ellipse.hxx>
38 #include <Geom_Parabola.hxx>
39 #include <Geom_Hyperbola.hxx>
40 #include <Geom_BezierCurve.hxx>
41 #include <Geom_BSplineCurve.hxx>
45 //=======================================================================
46 //function : BRepBuilderAPI_FindPlane
48 //=======================================================================
50 BRepBuilderAPI_FindPlane::BRepBuilderAPI_FindPlane()
54 //=======================================================================
55 //function : BRepBuilderAPI_FindPlane
57 //=======================================================================
59 BRepBuilderAPI_FindPlane::BRepBuilderAPI_FindPlane(const TopoDS_Shape& S,
60 const Standard_Real Tol)
65 //=======================================================================
68 //=======================================================================
70 void BRepBuilderAPI_FindPlane::Init(const TopoDS_Shape& S,
71 const Standard_Real Tol)
73 Standard_Real tolerance = Tol;
76 // compute the tolerance
78 for (ex.Init(S,TopAbs_EDGE); ex.More(); ex.Next()) {
79 Standard_Real t = BRep_Tool::Tolerance(TopoDS::Edge(ex.Current()));
80 if (t > tolerance) tolerance = t;
83 Standard_Real tol2 = tolerance*tolerance;
84 // try to find an analytical curve and calculate points
86 Standard_Real first, last;
87 Standard_Boolean found = Standard_False;
89 TColgp_SequenceOfPnt points;
90 Standard_Integer nbPnts;
92 for (ex.Init(S,TopAbs_EDGE); ex.More(); ex.Next()) {
93 Handle(Geom_Curve) c3d =
94 BRep_Tool::Curve(TopoDS::Edge(ex.Current()), loc, first, last);
97 Handle(Geom_Curve) c3dptr =
98 Handle(Geom_Curve)::DownCast(c3d->Transformed(loc.Transformation()));
100 Handle(Standard_Type) cType = c3dptr->DynamicType();
102 if (cType == STANDARD_TYPE(Geom_Line)) {
105 else if ((cType == STANDARD_TYPE(Geom_Circle)) ||
106 (cType == STANDARD_TYPE(Geom_Ellipse)) ||
107 (cType == STANDARD_TYPE(Geom_Parabola)) ||
108 (cType == STANDARD_TYPE(Geom_Hyperbola))) {
111 found = Standard_True;
112 Handle(Geom_Conic) Co = Handle(Geom_Conic)::DownCast(c3dptr);
113 P = new Geom_Plane(gp_Ax3(Co->Position()));
116 else if (cType == STANDARD_TYPE(Geom_BezierCurve)) {
117 Handle(Geom_BezierCurve) Co =
118 Handle(Geom_BezierCurve)::DownCast(c3dptr);
119 nbPnts = Co->NbPoles();
121 else if (cType == STANDARD_TYPE(Geom_BSplineCurve)) {
122 Handle(Geom_BSplineCurve) Co =
123 Handle(Geom_BSplineCurve)::DownCast(c3dptr);
124 nbPnts = Co->NbPoles();
131 for (Standard_Integer i=1; i<=nbPnts; i++) {
133 c3dptr->D0(first, p0);
135 else if (i == nbPnts) {
136 c3dptr->D0(last, p0);
139 c3dptr->D0(first+(last-first)/(nbPnts-1)*(i-1), p0);
147 // try to find a plane with the points
148 if (points.Length() > 2) {
150 Standard_Real disMax = 0.0;
151 gp_Pnt p0 = points(1);
153 for (Standard_Integer i=2; i<=points.Length(); i++) {
154 Standard_Real dist = p0.SquareDistance(points(i));
157 p1 = points(i); // it will be faster to store the point, otherwise it is necessary to find a value in a sequence
162 gp_Vec V1(p0, p1), V3;
163 Standard_Real proMax = 0.0;
165 for (Standard_Integer j=2; j<=points.Length(); j++) {
166 V3 = V1^gp_Vec(p0, points(j));
167 Standard_Real pro = V3.SquareMagnitude();
174 if (p0.SquareDistance(p2) > tol2) {
175 gp_Dir D1(V1), D2(gp_Vec(p0, p2));
176 if (!D1.IsParallel(D2, Precision::Angular())) {
177 P = new Geom_Plane(gp_Ax3(p0, D1.Crossed(D2), D1));
178 found = Standard_True;
186 // test if all points are on the plane
187 const gp_Pln& pln = P->Pln();
188 for (Standard_Integer i=1; i<=points.Length(); i++) {
189 if (pln.SquareDistance(points(i)) > tol2) {
190 found = Standard_False;
201 //=======================================================================
204 //=======================================================================
206 Standard_Boolean BRepBuilderAPI_FindPlane::Found() const
208 return !myPlane.IsNull();
211 //=======================================================================
214 //=======================================================================
216 Handle(Geom_Plane) BRepBuilderAPI_FindPlane::Plane() const