File size: 7,617 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
// SPDX-License-Identifier: LGPL-2.1-or-later

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

 *   Copyright (c) 2005 Berthold Grupp                                     *

 *                                                                         *

 *   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 MESH_BUILDER_H
#define MESH_BUILDER_H

#include <set>
#include <vector>

#include "MeshKernel.h"


namespace Base
{
class SequencerLauncher;
}

namespace MeshCore
{
class MeshKernel;
class MeshPoint;
class MeshGeomFacet;

/**

 * Class for creating the mesh structure by adding facets. Building the structure needs 3 steps:

 * 1. initializing

 * 2. adding the facets

 * 3. finishing

 * \code

 * // Sample Code for building a mesh structure

 * MeshBuilder builder(someMeshReference);

 * builder.Initialize(numberOfFacets);

 * ...

 * for (...)

 *   builder.AddFacet(...);

 * ...

 * builder.Finish();

 * \endcode

 * @author Berthold Grupp

 */
class MeshExport MeshBuilder

{
private:
    /** @name Helper class */
    //@{
    class Edge

    {
    public:
        PointIndex pt1;
        PointIndex pt2;
        FacetIndex facetIdx;

        Edge(PointIndex p1, PointIndex p2, FacetIndex idx)
            : facetIdx {idx}
        {
            if (p1 > p2) {
                pt1 = p2;
                pt2 = p1;
            }
            else {
                pt1 = p1;
                pt2 = p2;
            }
        }

        bool operator<(const Edge& e) const
        {
            return (pt1 == e.pt1) ? (pt2 < e.pt2) : (pt1 < e.pt1);
        }

        bool operator>(const Edge& e) const
        {
            return (pt1 == e.pt1) ? (pt2 > e.pt2) : (pt1 > e.pt1);
        }

        bool operator==(const Edge& e) const
        {
            return (pt1 == e.pt1) && (pt2 == e.pt2);
        }
    };
    //@}

    MeshKernel& _meshKernel;
    std::set<MeshPoint> _points;
    Base::SequencerLauncher* _seq {nullptr};

    // keep an array of iterators pointing to the vertex inside the set to save memory
    using MeshPointIterator = std::pair<std::set<MeshPoint>::iterator, bool>;
    std::vector<MeshPointIterator> _pointsIterator;
    size_t _ptIdx {0};

    void SetNeighbourhood();
    // As it's forbidden to insert a degenerated facet but insert its vertices anyway we must remove
    // them
    void RemoveUnreferencedPoints();

public:
    explicit MeshBuilder(MeshKernel& kernel);
    ~MeshBuilder();

    MeshBuilder(const MeshBuilder&) = delete;
    MeshBuilder(MeshBuilder&&) = delete;
    MeshBuilder& operator=(const MeshBuilder&) = delete;
    MeshBuilder& operator=(MeshBuilder&&) = delete;

    /**

     * Set the tolerance for the comparison of points. Normally you don't need to set the tolerance.

     */
    void SetTolerance(float);

    /** Initializes the class. Must be done before adding facets

     * @param ctFacets count of facets.

     * @param deletion if true (default) the mesh-kernel will be cleared

     *     otherwise you can add new facets on an existing mesh-kernel

     * @remarks To be efficient you should add exactly \a ctFacets with

     * AddFacet(), otherwise you'll possibly run into wastage of memory

     * and performance problems.

     */
    void Initialize(size_t ctFacets, bool deletion = true);

    /** adding facets */
    /** Add new facet

     * @param facet \a the facet

     * @param takeFlag if true the flag from the MeshGeomFacet will be taken

     * @param takeProperty

     */
    void AddFacet(const MeshGeomFacet& facet, bool takeFlag = false, bool takeProperty = false);
    /** Add new facet

     */
    void AddFacet(

        const Base::Vector3f& pt1,

        const Base::Vector3f& pt2,

        const Base::Vector3f& pt3,

        const Base::Vector3f& normal,

        unsigned char flag = 0,

        unsigned long prop = 0

    );
    /** Add new facet

     * @param facetPoints Array of vectors (size 4) in order of vec1, vec2,

     *                    vec3, normal

     * @param flag

     * @param prop

     */
    void AddFacet(Base::Vector3f* facetPoints, unsigned char flag = 0, unsigned long prop = 0);

    /** Finishes building up the mesh structure. Must be done after adding facets.

     * @param freeMemory if false (default) only the memory of internal

     * structures gets freed, otherwise  additional unneeded memory in the

     * mesh structure is tried to be freed.

     * @remarks If you have called AddFacet() as many times as specified in

     * Initialize() then absolutely no memory is wasted and you can leave the

     * default value.

     */
    void Finish(bool freeMemory = false);

    friend class MeshKernel;

private:
    float _fSaveTolerance;
};

/**

 * Class for creating the mesh structure by adding facets. Building the structure needs 3 steps:

 * 1. initializing

 * 2. adding the facets

 * 3. finishing

 * \code

 * // Sample Code for building a mesh structure

 * MeshFastBuilder builder(someMeshReference);

 * builder.Initialize(numberOfFacets);

 * ...

 * for (...)

 *   builder.AddFacet(...);

 * ...

 * builder.Finish();

 * \endcode

 * @author Werner Mayer

 */
class MeshExport MeshFastBuilder

{
private:
    MeshKernel& _meshKernel;

public:
    using size_type = int;
    explicit MeshFastBuilder(MeshKernel& rclM);
    ~MeshFastBuilder();

    MeshFastBuilder(const MeshFastBuilder&) = delete;
    MeshFastBuilder(MeshFastBuilder&&) = delete;
    MeshFastBuilder& operator=(const MeshFastBuilder&) = delete;
    MeshFastBuilder& operator=(MeshFastBuilder&&) = delete;

    /** Initializes the class. Must be done before adding facets

     * @param ctFacets count of facets.

     */
    void Initialize(size_type ctFacets);
    /** Add new facet

     */
    void AddFacet(const Base::Vector3f* facetPoints);
    /** Add new facet

     */
    void AddFacet(const MeshGeomFacet& facetPoints);

    /** Finishes building up the mesh structure. Must be done after adding facets.

     */
    void Finish();

private:
    struct Private;
    Private* p;
};

}  // namespace MeshCore

#endif