File size: 8,572 Bytes
985c397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// SPDX-License-Identifier: LGPL-2.1-or-later

/***************************************************************************

 *   Copyright (c) 2008 Juergen Riegel <juergen.riegel@web.de>             *

 *                                                                         *

 *   This file is part of the FreeCAD CAx development system.              *

 *                                                                         *

 *   This library is free software; you can redistribute it and/or         *

 *   modify it under the terms of the GNU Library General Public           *

 *   License as published by the Free Software Foundation; either          *

 *   version 2 of the License, or (at your option) any later version.      *

 *                                                                         *

 *   This library  is distributed in the hope that it will be useful,      *

 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *

 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *

 *   GNU Library General Public License for more details.                  *

 *                                                                         *

 *   You should have received a copy of the GNU Library General Public     *

 *   License along with this library; see the file COPYING.LIB. If not,    *

 *   write to the Free Software Foundation, Inc., 59 Temple Place,         *

 *   Suite 330, Boston, MA  02111-1307, USA                                *

 *                                                                         *

 ***************************************************************************/

#ifndef _CurveProjector_h_
#define _CurveProjector_h_

#include <limits>

#include <TopoDS_Edge.hxx>

#include <Mod/Mesh/App/Mesh.h>
#include <Mod/MeshPart/MeshPartGlobal.h>

#include <Standard_Version.hxx>

namespace MeshCore
{
class MeshKernel;
class MeshGeomFacet;
class MeshFacetGrid;
}  // namespace MeshCore

using MeshCore::MeshGeomFacet;
using MeshCore::MeshKernel;

namespace MeshPart
{

/** The father of all projection algorithms

 */
class MeshPartExport CurveProjector

{
public:
    CurveProjector(const TopoDS_Shape& aShape, const MeshKernel& pMesh);
    virtual ~CurveProjector() = default;

    struct FaceSplitEdge

    {
        MeshCore::FacetIndex ulFaceIndex;
        Base::Vector3f p1, p2;
    };

    template<class T>
    struct TopoDSLess

    {
        bool operator()(const T& x, const T& y) const
        {
#if OCC_VERSION_HEX >= 0x070800
            std::hash<T> hasher;
            return hasher(x) < hasher(y);
#else
            constexpr int max = std::numeric_limits<int>::max();
            return x.HashCode(max - 1) < y.HashCode(max - 1);
#endif
        }
    };

    using result_type = std::map<TopoDS_Edge, std::vector<FaceSplitEdge>, TopoDSLess<TopoDS_Edge>>;


    result_type& result()
    {
        return mvEdgeSplitPoints;
    }

    void writeIntersectionPointsToFile(const char* name = "export_pts.asc");

protected:
    virtual void Do() = 0;
    const TopoDS_Shape& _Shape;
    const MeshKernel& _Mesh;
    result_type mvEdgeSplitPoints;
};


/** Project by intersection face planes with the curve

 */
class MeshPartExport CurveProjectorShape: public CurveProjector
{
public:
    CurveProjectorShape(const TopoDS_Shape& aShape, const MeshKernel& pMesh);
    ~CurveProjectorShape() override = default;

    void projectCurve(const TopoDS_Edge& aEdge, std::vector<FaceSplitEdge>& vSplitEdges);

    bool findStartPoint(

        const MeshKernel& MeshK,

        const Base::Vector3f& Pnt,

        Base::Vector3f& Rslt,

        MeshCore::FacetIndex& FaceIndex

    );


protected:
    void Do() override;
};


/** Project by projecting a sampled curve to the mesh

 */
class MeshPartExport CurveProjectorSimple: public CurveProjector
{
public:
    CurveProjectorSimple(const TopoDS_Shape& aShape, const MeshKernel& pMesh);
    ~CurveProjectorSimple() override = default;

    /// helper to discredicice a Edge...
    void GetSampledCurves(

        const TopoDS_Edge& aEdge,

        std::vector<Base::Vector3f>& rclPoints,

        unsigned long ulNbOfPoints = 30

    );


    void projectCurve(

        const TopoDS_Edge& aEdge,

        const std::vector<Base::Vector3f>& rclPoints,

        std::vector<FaceSplitEdge>& vSplitEdges

    );

    bool findStartPoint(

        const MeshKernel& MeshK,

        const Base::Vector3f& Pnt,

        Base::Vector3f& Rslt,

        MeshCore::FacetIndex& FaceIndex

    );


protected:
    void Do() override;
};

/** Project by projecting a sampled curve to the mesh

 */
class MeshPartExport CurveProjectorWithToolMesh: public CurveProjector
{
public:
    struct LineSeg

    {
        Base::Vector3f p;
        Base::Vector3f n;
    };

    CurveProjectorWithToolMesh(const TopoDS_Shape& aShape, const MeshKernel& pMesh, MeshKernel& rToolMesh);
    ~CurveProjectorWithToolMesh() override = default;


    void makeToolMesh(const TopoDS_Edge& aEdge, std::vector<MeshGeomFacet>& cVAry);


    MeshKernel& ToolMesh;

protected:
    void Do() override;
};

/**

 * The MeshProjection class projects a shape onto a mesh.

 * @author Werner Mayer

 */
class MeshPartExport MeshProjection

{
public:
    /// Helper class
    struct SplitEdge

    {
        MeshCore::PointIndex uE0, uE1; /**< start and endpoint of an edge */
        Base::Vector3f cPt;            /**< Point on edge (\a uE0, \a uE1) */
    };
    struct Edge

    {
        Base::Vector3f cPt1;
        Base::Vector3f cPt2;
    };
    struct PolyLine

    {
        std::vector<Base::Vector3f> points;
    };

    explicit MeshProjection(const MeshKernel& rMesh);

    /**

     * @brief findSectionParameters

     * Find the parameters of the edge where when projecting the corresponding point will lie

     * on an edge of the mesh.

     * @param edge

     * @param dir

     * @param parameters

     */
    void findSectionParameters(

        const TopoDS_Edge& edge,

        const Base::Vector3f& dir,

        std::set<double>& parameters

    ) const;
    void discretize(

        const TopoDS_Edge& aEdge,

        std::vector<Base::Vector3f>& polyline,

        std::size_t minPoints = 2

    ) const;
    /**

     * Searches all edges that intersect with the projected curve \a aShape. Therefore \a aShape

     * must contain shapes of type TopoDS_Edge, other shape types are ignored. A possible solution

     * is taken if the distance between the curve point and the projected point is <= \a fMaxDist.

     */
    void projectToMesh(const TopoDS_Shape& aShape, float fMaxDist, std::vector<PolyLine>& rPolyLines) const;
    /**

     * @brief projectOnMesh

     * Projects the given points onto the mesh along a given direction. The points can can be

     * projected will be saved to \a pointsOut

     * @brief projectOnMesh

     * @param pointsIn

     * @param dir

     * @param tolerance

     * @param pointsOut

     */
    void projectOnMesh(

        const std::vector<Base::Vector3f>& pointsIn,

        const Base::Vector3f& dir,

        float tolerance,

        std::vector<Base::Vector3f>& pointsOut

    ) const;
    /**

     * Project all edges of the shape onto the mesh using parallel projection.

     */
    void projectParallelToMesh(

        const TopoDS_Shape& aShape,

        const Base::Vector3f& dir,

        std::vector<PolyLine>& rPolyLines

    ) const;
    /**

     * Project all polylines onto the mesh using parallel projection.

     */
    void projectParallelToMesh(

        const std::vector<PolyLine>& aEdges,

        const Base::Vector3f& dir,

        std::vector<PolyLine>& rPolyLines

    ) const;
    /**

     * Cuts the mesh at the curve defined by \a aShape. This method call @ref projectToMesh() to get

     * the split the facet at the found points. @see projectToMesh() for more details.

     */
    void splitMeshByShape(const TopoDS_Shape& aShape, float fMaxDist) const;

protected:
    void projectEdgeToEdge(

        const TopoDS_Edge& aCurve,

        float fMaxDist,

        const MeshCore::MeshFacetGrid& rGrid,

        std::vector<SplitEdge>& rSplitEdges

    ) const;
    bool findIntersection(const Edge&, const Edge&, const Base::Vector3f& dir, Base::Vector3f& res) const;

private:
    const MeshKernel& _rcMesh;
};

}  // namespace MeshPart

#endif