File: occ_face.cpp

package info (click to toggle)
netgen 6.2.2601%2Bdfsg1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,076 kB
  • sloc: cpp: 166,627; tcl: 6,310; python: 2,868; sh: 528; makefile: 90
file content (293 lines) | stat: -rw-r--r-- 9,279 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

#include <BRepGProp.hxx>
#include <BRep_Tool.hxx>
#include <GeomAPI_ProjectPointOnCurve.hxx>
#include <BRepLProp_SLProps.hxx>

#pragma clang diagnostic pop

#include "occ_edge.hpp"
#include "occ_face.hpp"
#include "occgeom.hpp"

namespace netgen
{
    OCCFace::OCCFace(TopoDS_Shape dshape)
        : face(TopoDS::Face(dshape))
    {
        BRepGProp::SurfaceProperties (dshape, props);
        bbox = ::netgen::GetBoundingBox(face);

        surface = BRep_Tool::Surface(face);
        shape_analysis = new ShapeAnalysis_Surface( surface );
        tolerance = BRep_Tool::Tolerance( face );
    }

    size_t OCCFace::GetNBoundaries() const
    {
        return 0;
    }

    Point<3> OCCFace::GetCenter() const
    {
        return occ2ng( props.CentreOfMass() );
    }

    Array<Segment> OCCFace::GetBoundary(const Mesh& mesh) const
    {
        auto & geom = dynamic_cast<OCCGeometry&>(*mesh.GetGeometry());

        auto n_edges = geom.GetNEdges();
        constexpr int UNUSED = 0;
        constexpr int FORWARD = 1;
        constexpr int REVERSED = 2;
        constexpr int BOTH = 3;

        Array<int> edge_orientation(n_edges);
        edge_orientation = UNUSED;

        Array<Handle(Geom2d_Curve)> curve_on_face[BOTH];
        curve_on_face[FORWARD].SetSize(n_edges);
        curve_on_face[REVERSED].SetSize(n_edges);

        Array<TopoDS_Edge> edge_on_face[BOTH];
        edge_on_face[FORWARD].SetSize(n_edges);
        edge_on_face[REVERSED].SetSize(n_edges);

        // In case the face is INTERNAL, we need to orient it to FORWARD to get proper orientation for the edges
        // (relative to the face) otherwise, all edges are also INTERNAL
        auto oriented_face = TopoDS_Face(face);
        if(oriented_face.Orientation() == TopAbs_INTERNAL)
          oriented_face.Orientation(TopAbs_FORWARD);

        for(auto edge_ : GetEdges(oriented_face))
        {
            auto edge = TopoDS::Edge(edge_);
            auto edgenr = geom.GetEdge(edge).nr;
            auto & orientation = edge_orientation[edgenr];
            double s0, s1;
            auto cof = BRep_Tool::CurveOnSurface (edge, oriented_face, s0, s1);
            if(edge.Orientation() == TopAbs_FORWARD || edge.Orientation() == TopAbs_INTERNAL)
            {
                curve_on_face[FORWARD][edgenr] = cof;
                orientation += FORWARD;
                edge_on_face[FORWARD][edgenr] = edge;
            }
            if(edge.Orientation() == TopAbs_REVERSED)
            {
                curve_on_face[REVERSED][edgenr] = cof;
                orientation += REVERSED;
                edge_on_face[REVERSED][edgenr] = edge;
            }
            if(edge.Orientation() == TopAbs_INTERNAL)
            {
              // add reversed edge
              auto r_edge = TopoDS::Edge(edge.Reversed());
              auto cof = BRep_Tool::CurveOnSurface (r_edge, oriented_face, s0, s1);
              curve_on_face[REVERSED][edgenr] = cof;
              orientation += REVERSED;
              edge_on_face[REVERSED][edgenr] = r_edge;
            }

            if(orientation > BOTH)
                throw Exception("have edge more than twice in face " + ToString(nr) + " " + properties.GetName() + ", orientation: " + ToString(orientation));
        }

        Array<Segment> boundary;
        for (auto seg : mesh.LineSegments())
        {
            auto edgenr = seg.epgeominfo[0].edgenr;
            auto orientation = edge_orientation[edgenr];

            if(orientation == UNUSED)
                continue;

            for(const auto ORIENTATION : {FORWARD, REVERSED})
            {
                if((orientation & ORIENTATION) == 0)
                    continue;

                // auto cof = curve_on_face[ORIENTATION][edgenr];
                auto edge = edge_on_face[ORIENTATION][edgenr];
                double s0, s1;
                auto cof = BRep_Tool::CurveOnSurface (edge, face, s0, s1);

                double s[2] = { seg.epgeominfo[0].dist, seg.epgeominfo[1].dist };

                // dist is in [0,1], map parametrization to [s0, s1]
                s[0] = s0 + s[0]*(s1-s0);
                s[1] = s0 + s[1]*(s1-s0);

                // fixes normal-vector roundoff problem when endpoint is cone-tip
                double delta = s[1]-s[0];
                s[0] += 1e-10*delta;
                s[1] -= 1e-10*delta;

                for(auto i : Range(2))
                {
                    // take uv from CurveOnSurface as start value but project again for better accuracy
                    // (cof->Value yields wrong values (outside of surface) for complicated faces
                    auto uv = cof->Value(s[i]);
                    PointGeomInfo gi;
                    gi.u = uv.X();
                    gi.v = uv.Y();
                    Point<3> pproject = mesh[seg[i]];
                    ProjectPointGI(pproject, gi);
                    seg.epgeominfo[i].u = gi.u;
                    seg.epgeominfo[i].v = gi.v;
                }

                bool do_swap = ORIENTATION == REVERSED;
                if(seg.epgeominfo[1].dist < seg.epgeominfo[0].dist)
                  do_swap = !do_swap;

                if(do_swap)
                {
                    swap(seg[0], seg[1]);
                    swap(seg.epgeominfo[0].dist, seg.epgeominfo[1].dist);
                    swap(seg.epgeominfo[0].u, seg.epgeominfo[1].u);
                    swap(seg.epgeominfo[0].v, seg.epgeominfo[1].v);
                }

                boundary.Append(seg);
            }
        }
        return boundary;
    }

    PointGeomInfo OCCFace::Project(Point<3>& p) const
    {
        auto suval = shape_analysis->ValueOfUV(ng2occ(p), tolerance);
        double u,v;
        suval.Coord(u, v);
        p = occ2ng(surface->Value( u, v ));

        PointGeomInfo gi;
        gi.trignum = nr+1;
        gi.u = u;
        gi.v = v;
        return gi;
    }

    bool OCCFace::ProjectPointGI(Point<3>& p_, PointGeomInfo& gi) const
    {
      /*
        static Timer t("OCCFace::ProjectPointGI");
        RegionTimer rt(t);
        // *testout << "input, uv = " << gi.u << ", " << gi.v << endl;
        auto suval = shape_analysis->NextValueOfUV({gi.u, gi.v}, ng2occ(p_), tolerance);
        gi.trignum = nr+1;
        suval.Coord(gi.u, gi.v);
        // *testout << "result, uv = " << gi.u << ", " << gi.v << endl;
        p_ = occ2ng(surface->Value( gi.u, gi.v ));        
        return true;
      */
        // Old code: do newton iterations manually
        double u = gi.u;
        double v = gi.v;
        auto p = ng2occ(p_);
        auto x = surface->Value (u,v);
      
        if (p.SquareDistance(x) <= sqr(PROJECTION_TOLERANCE)) return true;
      
        gp_Vec du, dv;
        surface->D1(u,v,x,du,dv);
      
        int count = 0;
        gp_Pnt xold;
        gp_Vec n;
        double det, lambda, mu;
      
        do {
           count++;

           n = du^dv;

           det = Det3 (n.X(), du.X(), dv.X(),
              n.Y(), du.Y(), dv.Y(),
              n.Z(), du.Z(), dv.Z());

           if (det < 1e-15) return false;

           lambda = Det3 (n.X(), p.X()-x.X(), dv.X(),
              n.Y(), p.Y()-x.Y(), dv.Y(),
              n.Z(), p.Z()-x.Z(), dv.Z())/det;

           mu     = Det3 (n.X(), du.X(), p.X()-x.X(),
              n.Y(), du.Y(), p.Y()-x.Y(),
              n.Z(), du.Z(), p.Z()-x.Z())/det;

           u += lambda;
           v += mu;

           xold = x;
           surface->D1(u,v,x,du,dv);

        } while (xold.SquareDistance(x) > sqr(PROJECTION_TOLERANCE) && count < 50);

        //    (*testout) << "FastProject count: " << count << endl;

        if (count == 50) return false;

        p_ = occ2ng(x);
        gi.u = u; gi.v = v;

        return true;
    }

    Point<3> OCCFace::GetPoint(const PointGeomInfo& gi) const
    {
        return occ2ng(surface->Value( gi.u, gi.v ));
    }

    void OCCFace::CalcEdgePointGI(const GeometryEdge& edge,
            double t,
            EdgePointGeomInfo& egi) const
    {
        throw Exception(ToString("not implemented") + __FILE__ + ":" + ToString(__LINE__));
    }

    Box<3> OCCFace::GetBoundingBox() const
    {
        return bbox;
    }


    double OCCFace::GetCurvature(const PointGeomInfo& gi) const
    {
        BRepAdaptor_Surface sf(face, Standard_True);
        BRepLProp_SLProps prop2(sf, 2, 1e-5);
        prop2.SetParameters (gi.u, gi.v);
        return max(fabs(prop2.MinCurvature()),
                   fabs(prop2.MaxCurvature()));
    }

    void OCCFace::RestrictH(Mesh& mesh, const MeshingParameters& mparam) const
    {
        throw Exception(ToString("not implemented") + __FILE__ + ":" + ToString(__LINE__));
    }

    Vec<3> OCCFace::GetNormal(const Point<3>& p, const PointGeomInfo* gi) const
    {
        PointGeomInfo gi_;
        if(gi==nullptr)
        {
            auto p_ = p;
            gi_ = Project(p_);
            gi = &gi_;
        }

        gp_Pnt pnt;
        gp_Vec du, dv;
        surface->D1(gi->u,gi->v,pnt,du,dv);
        auto n = Cross (occ2ng(du), occ2ng(dv));
        n.Normalize();
        if (face.Orientation() == TopAbs_REVERSED)
            n *= -1;
        return n;
    }


}