1 #include <deal.II/opencascade/utilities.h> 3 #ifdef DEAL_II_WITH_OPENCASCADE 5 #include <deal.II/base/point.h> 6 #include <deal.II/base/utilities.h> 7 #include <deal.II/base/exceptions.h> 9 #include <boost/bind.hpp> 15 DEAL_II_DISABLE_EXTRA_DIAGNOSTICS
17 #include <IGESControl_Controller.hxx> 18 #include <IGESControl_Reader.hxx> 19 #include <IGESControl_Writer.hxx> 21 #include <STEPControl_Controller.hxx> 22 #include <STEPControl_Reader.hxx> 23 #include <STEPControl_Writer.hxx> 26 #include <TopoDS_Shape.hxx> 27 #include <TopoDS_Face.hxx> 28 #include <TopoDS_Edge.hxx> 29 #include <TopExp_Explorer.hxx> 31 #include <Handle_Standard_Transient.hxx> 33 #include <TColStd_SequenceOfTransient.hxx> 34 #include <TColStd_HSequenceOfTransient.hxx> 35 #include <TColgp_HArray1OfPnt.hxx> 40 #include <GeomAPI_ProjectPointOnSurf.hxx> 41 #include <GeomAPI_ProjectPointOnCurve.hxx> 42 #include <IntCurvesFace_ShapeIntersector.hxx> 44 #include <BRepTools.hxx> 45 #include <BRepAdaptor_Curve.hxx> 46 #include <BRepAdaptor_HCurve.hxx> 47 #include <BRepAdaptor_HCompCurve.hxx> 48 #include <BRepAdaptor_Surface.hxx> 49 #include <BRep_Builder.hxx> 50 #include <BRepBuilderAPI_Transform.hxx> 51 #include <BRepBuilderAPI_MakeEdge.hxx> 52 #include <BRepAlgo_Section.hxx> 54 #include <Geom_Plane.hxx> 55 #include <Geom_BoundedCurve.hxx> 56 #include <GeomAPI_Interpolate.hxx> 57 #include <GeomConvert_CompCurveToBSplineCurve.hxx> 58 #include <GeomLProp_SLProps.hxx> 60 #include <GCPnts_AbscissaPoint.hxx> 61 #include <ShapeAnalysis_Surface.hxx> 68 DEAL_II_NAMESPACE_OPEN
72 std_cxx11::tuple<unsigned int, unsigned int, unsigned int>
76 unsigned int n_faces=0, n_edges=0, n_vertices=0;
77 for (exp.Init(shape, TopAbs_FACE);
78 exp.More(); exp.Next(), ++n_faces)
80 for (exp.Init(shape, TopAbs_EDGE);
81 exp.More(); exp.Next(), ++n_edges)
83 for (exp.Init(shape, TopAbs_VERTEX);
84 exp.More(); exp.Next(), ++n_vertices)
86 return std_cxx11::tuple<unsigned int, unsigned int, unsigned int>(n_faces, n_edges, n_vertices);
90 std::vector<TopoDS_Face> &faces,
91 std::vector<TopoDS_Edge> &edges,
92 std::vector<TopoDS_Vertex> &vertices)
99 for (exp.Init(shape, TopAbs_FACE); exp.More(); exp.Next())
101 faces.push_back(TopoDS::Face(exp.Current()));
103 for (exp.Init(shape, TopAbs_EDGE); exp.More(); exp.Next())
105 edges.push_back(TopoDS::Edge(exp.Current()));
107 for (exp.Init(shape, TopAbs_VERTEX); exp.More(); exp.Next())
109 vertices.push_back(TopoDS::Vertex(exp.Current()));
115 std::vector<TopoDS_Compound> &compounds,
116 std::vector<TopoDS_CompSolid> &compsolids,
117 std::vector<TopoDS_Solid> &solids,
118 std::vector<TopoDS_Shell> &shells,
119 std::vector<TopoDS_Wire> &wires)
122 compsolids.resize(0);
128 for (exp.Init(shape, TopAbs_COMPOUND); exp.More(); exp.Next())
130 compounds.push_back(TopoDS::Compound(exp.Current()));
132 for (exp.Init(shape, TopAbs_COMPSOLID); exp.More(); exp.Next())
134 compsolids.push_back(TopoDS::CompSolid(exp.Current()));
136 for (exp.Init(shape, TopAbs_SOLID); exp.More(); exp.Next())
138 solids.push_back(TopoDS::Solid(exp.Current()));
140 for (exp.Init(shape, TopAbs_SHELL); exp.More(); exp.Next())
142 shells.push_back(TopoDS::Shell(exp.Current()));
144 for (exp.Init(shape, TopAbs_WIRE); exp.More(); exp.Next())
146 wires.push_back(TopoDS::Wire(exp.Current()));
152 return gp_Pnt(p(0), p(1), p(2));
158 return Point<3>(p.X(), p.Y(), p.Z());
163 const double tolerance)
165 const double rel_tol=std::max(tolerance, std::max(p1.
norm(), p2.
norm())*tolerance);
166 if (direction.
norm() > 0.0)
167 return (p1*direction < p2*direction-rel_tol);
169 for (
int d=2; d>=0; --d)
170 if (p1[d] < p2[d]-rel_tol)
172 else if (p2[d] < p1[d]-rel_tol)
182 const double scale_factor)
184 IGESControl_Reader reader;
185 IFSelect_ReturnStatus stat;
186 stat = reader.ReadFile(filename.c_str());
190 Standard_Boolean failsonly = Standard_False;
191 IFSelect_PrintCount mode = IFSelect_ItemsByEntity;
192 reader.PrintCheckLoad (failsonly, mode);
194 Standard_Integer nRoots = reader.TransferRoots();
204 scale.SetScale (Origin, scale_factor);
206 TopoDS_Shape sh = reader.OneShape();
207 BRepBuilderAPI_Transform trans(sh, scale);
209 return trans.Shape();
213 const std::string &filename)
215 IGESControl_Controller::Init();
216 IGESControl_Writer ICW (
"MM", 0);
217 Standard_Boolean ok = ICW.AddShape (shape);
220 Standard_Boolean OK = ICW.Write (filename.c_str());
225 const double scale_factor)
227 STEPControl_Reader reader;
228 IFSelect_ReturnStatus stat;
229 stat = reader.ReadFile(filename.c_str());
233 Standard_Boolean failsonly = Standard_False;
234 IFSelect_PrintCount mode = IFSelect_ItemsByEntity;
235 reader.PrintCheckLoad (failsonly, mode);
237 Standard_Integer nRoots = reader.TransferRoots();
247 scale.SetScale (Origin, scale_factor);
249 TopoDS_Shape sh = reader.OneShape();
250 BRepBuilderAPI_Transform trans(sh, scale);
252 return trans.Shape();
256 const std::string &filename)
258 STEPControl_Controller::Init();
259 STEPControl_Writer SCW;
260 IFSelect_ReturnStatus status;
261 status = SCW.Transfer(shape, STEPControl_AsIs);
264 status = SCW.Write(filename.c_str());
266 AssertThrow(status == IFSelect_RetDone,
ExcMessage(
"Failed to write translated shape to STEP file."));
271 double tolerance = 0.0;
273 std::vector<TopoDS_Face> faces;
274 std::vector<TopoDS_Edge> edges;
275 std::vector<TopoDS_Vertex> vertices;
282 for (
unsigned int i=0; i<vertices.size(); ++i)
283 tolerance = fmax(tolerance,BRep_Tool::Tolerance(vertices[i]));
285 for (
unsigned int i=0; i<edges.size(); ++i)
286 tolerance = fmax(tolerance,BRep_Tool::Tolerance(edges[i]));
288 for (
unsigned int i=0; i<faces.size(); ++i)
289 tolerance = fmax(tolerance,BRep_Tool::Tolerance(faces[i]));
302 Handle(Geom_Plane) plane =
new Geom_Plane(c_x,c_y,c_z,c);
303 BRepAlgo_Section section(in_shape, plane);
304 TopoDS_Shape edges = section.Shape();
309 const double tolerance)
311 TopoDS_Edge out_shape;
312 TopoDS_Shape edges = in_shape;
313 std::vector<Handle_Geom_BoundedCurve> intersections;
317 gp_Pnt PIn(0.0,0.0,0.0);
318 gp_Pnt PFin(0.0,0.0,0.0);
319 gp_Pnt PMid(0.0,0.0,0.0);
320 TopExp_Explorer edgeExplorer(edges , TopAbs_EDGE);
322 while (edgeExplorer.More())
324 edge = TopoDS::Edge(edgeExplorer.Current());
325 Handle(Geom_Curve) curve = BRep_Tool::Curve(edge,L,First,Last);
326 intersections.push_back(Handle(Geom_BoundedCurve)::DownCast(curve));
332 unsigned int numIntersEdges = intersections.size();
335 GeomConvert_CompCurveToBSplineCurve convert_bspline(intersections[0]);
337 bool check =
false, one_added =
true, one_failed=
true;
338 std::vector<bool> added(numIntersEdges,
false);
340 while (one_added ==
true)
344 for (
unsigned int i=1; i<numIntersEdges; ++i)
345 if (added[i] ==
false)
347 Handle(Geom_Curve) curve = intersections[i];
348 Handle(Geom_BoundedCurve) bcurve = Handle(Geom_BoundedCurve)::DownCast(curve);
349 check = convert_bspline.Add(bcurve,tolerance,0,1,0);
353 Handle(Geom_BoundedCurve) bcurve = Handle(Geom_BoundedCurve)::DownCast(curve);
354 check = convert_bspline.Add(bcurve,tolerance,0,1,0);
356 one_failed = one_failed || (check ==
false);
357 one_added = one_added || (check ==
true);
362 Assert(one_failed ==
false,
363 ExcMessage(
"Joining some of the Edges failed."));
365 Handle(Geom_Curve) bspline = convert_bspline.BSplineCurve();
367 out_shape = BRepBuilderAPI_MakeEdge(bspline);
375 const double tolerance)
379 gp_Pnt P0 =
point(origin);
380 gp_Ax1 gpaxis(P0, gp_Dir(direction[0], direction[1], direction[2]));
384 gp_Pnt Pproj(0.0,0.0,0.0);
388 IntCurvesFace_ShapeIntersector Inters;
389 Inters.Load(in_shape,tolerance);
393 Inters.Perform(line,-RealLast(),+RealLast());
396 double minDistance = 1e7;
399 for (
int i=0; i<Inters.NbPnt(); ++i)
401 distance =
point(origin).Distance(Inters.Pnt(i+1));
403 if (distance < minDistance)
405 minDistance = distance;
406 result =
point(Inters.Pnt(i+1));
416 const double tolerance)
419 unsigned int n_vertices = curve_points.size();
421 if (direction*direction > 0)
423 std::sort(curve_points.begin(), curve_points.end(),
428 Handle(TColgp_HArray1OfPnt) vertices =
new TColgp_HArray1OfPnt(1,n_vertices);
429 for (
unsigned int vertex=0; vertex<n_vertices; ++vertex)
431 vertices->SetValue(vertex+1,
point(curve_points[vertex]));
435 GeomAPI_Interpolate bspline_generator(vertices, closed, tolerance);
436 bspline_generator.Perform();
437 Assert( (bspline_generator.IsDone()),
ExcMessage(
"Interpolated bspline generation failed"));
439 Handle(Geom_BSplineCurve) bspline = bspline_generator.Curve();
440 TopoDS_Edge out_shape = BRepBuilderAPI_MakeEdge(bspline);
444 std_cxx11::tuple<Point<3>, TopoDS_Shape, double,
double>
447 const double tolerance)
450 gp_Pnt Pproj =
point(origin);
452 double minDistance = 1e7;
453 gp_Pnt tmp_proj(0.0,0.0,0.0);
455 unsigned int counter = 0;
456 unsigned int face_counter = 0;
458 TopoDS_Shape out_shape;
462 for (exp.Init(in_shape, TopAbs_FACE); exp.More(); exp.Next())
464 TopoDS_Face face = TopoDS::Face(exp.Current());
468 Handle(Geom_Surface) SurfToProj = BRep_Tool::Surface(face);
470 ShapeAnalysis_Surface projector(SurfToProj);
471 gp_Pnt2d proj_params = projector.ValueOfUV(
point(origin), tolerance);
473 SurfToProj->D0(proj_params.X(),proj_params.Y(),tmp_proj);
476 if (distance < minDistance)
478 minDistance = distance;
493 for (exp.Init(in_shape, TopAbs_EDGE); exp.More(); exp.Next())
495 TopoDS_Edge edge = TopoDS::Edge(exp.Current());
496 if (!BRep_Tool::Degenerated(edge))
504 Handle(Geom_Curve) CurveToProj = BRep_Tool::Curve(edge,L,First,Last);
506 GeomAPI_ProjectPointOnCurve Proj(
point(origin),CurveToProj);
507 unsigned int num_proj_points = Proj.NbPoints();
508 if ((num_proj_points > 0) && (Proj.LowerDistance() < minDistance))
510 minDistance = Proj.LowerDistance();
511 Pproj = Proj.NearestPoint();
513 u=Proj.LowerDistanceParameter();
520 return std_cxx11::tuple<Point<3>, TopoDS_Shape, double,
double>
521 (
point(Pproj),out_shape, u, v);
527 const double tolerance)
529 std_cxx11::tuple<Point<3>, TopoDS_Shape, double,
double>
531 return std_cxx11::get<0>(ref);
534 std_cxx11::tuple<Point<3>,
Point<3>,
double>
536 const Point<3> &origin,
537 const double tolerance)
540 std_cxx11::tuple<Point<3>, TopoDS_Shape, double,
double>
545 TopoDS_Shape &out_shape = std_cxx11::get<1>(shape_and_params);
546 double &u = std_cxx11::get<2>(shape_and_params);
547 double &v = std_cxx11::get<3>(shape_and_params);
551 std_cxx11::tuple<unsigned int, unsigned int, unsigned int>
numbers =
555 Assert(std_cxx11::get<0>(numbers) > 0,
556 ExcMessage(
"Could not find normal: the shape containing the closest point has 0 faces."));
557 Assert(std_cxx11::get<0>(numbers) < 2,
558 ExcMessage(
"Could not find normal: the shape containing the closest point has more than 1 face."));
562 exp.Init(out_shape, TopAbs_FACE);
563 TopoDS_Face face = TopoDS::Face(exp.Current());
571 switch (in_shape.ShapeType())
575 BRepAdaptor_Surface surf(TopoDS::Face(in_shape));
576 return point(surf.Value(u,v));
580 BRepAdaptor_Curve curve(TopoDS::Edge(in_shape));
581 return point(curve.Value(u));
584 Assert(
false, ExcUnsupportedShape());
589 std_cxx11::tuple<Point<3>, Point<3>,
double >
595 Handle(Geom_Surface) SurfToProj = BRep_Tool::Surface(face);
596 GeomLProp_SLProps props(SurfToProj, u, v, 1, 1e-7);
597 gp_Pnt Value = props.Value();
599 gp_Dir Normal = props.Normal();
600 Assert(props.IsCurvatureDefined(),
ExcMessage(
"Curvature is not well defined!"));
601 Standard_Real Mean_Curvature = props.MeanCurvature();
602 Point<3> normal = Point<3>(Normal.X(),Normal.Y(),Normal.Z());
603 return std_cxx11::tuple<Point<3>, Point<3>,
double>(
point(Value), normal, Mean_Curvature);
611 BRepAdaptor_Surface surf(face);
612 const double u0 = surf.FirstUParameter();
613 const double u1 = surf.LastUParameter();
614 const double v0 = surf.FirstVParameter();
615 const double v1 = surf.LastVParameter();
617 std::vector<CellData<2> > cells;
618 std::vector<Point<3> > vertices;
621 vertices.push_back(
point(surf.Value(u0,v0)));
622 vertices.push_back(
point(surf.Value(u1,v0)));
623 vertices.push_back(
point(surf.Value(u0,v1)));
624 vertices.push_back(
point(surf.Value(u1,v1)));
627 for (
unsigned int i=0; i<4; ++i)
630 cells.push_back(cell);
636 DEAL_II_NAMESPACE_CLOSE
std_cxx11::tuple< Point< 3 >, Point< 3 >, double > closest_point_and_differential_forms(const TopoDS_Shape &in_shape, const Point< 3 > &origin, const double tolerance=1e-7)
TopoDS_Shape read_IGES(const std::string &filename, const double scale_factor=1e-3)
Point< 3 > line_intersection(const TopoDS_Shape &in_shape, const Point< 3 > &origin, const Tensor< 1, 3 > &direction, const double tolerance=1e-7)
TopoDS_Shape intersect_plane(const TopoDS_Shape &in_shape, const double c_x, const double c_y, const double c_z, const double c, const double tolerance=1e-7)
TopoDS_Shape read_STEP(const std::string &filename, const double scale_factor=1e-3)
::ExceptionBase & ExcMessage(std::string arg1)
#define AssertThrow(cond, exc)
numbers::NumberTraits< Number >::real_type norm() const
Point< 3 > push_forward(const TopoDS_Shape &in_shape, const double u, const double v)
void write_IGES(const TopoDS_Shape &shape, const std::string &filename)
TopoDS_Edge interpolation_curve(std::vector< Point< 3 > > &curve_points, const Tensor< 1, 3 > &direction=Tensor< 1, 3 >(), const bool closed=false, const double tolerance=1e-7)
std_cxx11::tuple< unsigned int, unsigned int, unsigned int > count_elements(const TopoDS_Shape &shape)
std_cxx11::tuple< Point< 3 >, Point< 3 >, double > push_forward_and_differential_forms(const TopoDS_Face &face, const double u, const double v, const double tolerance=1e-7)
void write_STEP(const TopoDS_Shape &shape, const std::string &filename)
#define Assert(cond, exc)
virtual void create_triangulation(const std::vector< Point< spacedim > > &vertices, const std::vector< CellData< dim > > &cells, const SubCellData &subcelldata)
void extract_compound_shapes(const TopoDS_Shape &shape, std::vector< TopoDS_Compound > &compounds, std::vector< TopoDS_CompSolid > &compsolids, std::vector< TopoDS_Solid > &solids, std::vector< TopoDS_Shell > &shells, std::vector< TopoDS_Wire > &wires)
numbers::NumberTraits< Number >::real_type distance(const Point< dim, Number > &p) const
std_cxx11::tuple< Point< 3 >, TopoDS_Shape, double, double > project_point_and_pull_back(const TopoDS_Shape &in_shape, const Point< 3 > &origin, const double tolerance=1e-7)
Point< 3 > closest_point(const TopoDS_Shape &in_shape, const Point< 3 > &origin, const double tolerance=1e-7)
double get_shape_tolerance(const TopoDS_Shape &shape)
TopoDS_Edge join_edges(const TopoDS_Shape &in_shape, const double tolerance=1e-7)
void extract_geometrical_shapes(const TopoDS_Shape &shape, std::vector< TopoDS_Face > &faces, std::vector< TopoDS_Edge > &edges, std::vector< TopoDS_Vertex > &vertices)
Point< 3 > point(const gp_Pnt &p)
bool point_compare(const Point< 3 > &p1, const Point< 3 > &p2, const Tensor< 1, 3 > &direction=Tensor< 1, 3 >(), const double tolerance=1e-10)
void create_triangulation(const TopoDS_Face &face, Triangulation< 2, 3 > &tria)
unsigned int vertices[GeometryInfo< structdim >::vertices_per_cell]