File size: 11,343 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// SPDX-License-Identifier: LGPL-2.1-or-later

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

 *   Copyright (c) 2005 Imetric 3D GmbH                                    *

 *                                                                         *

 *   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_IO_H
#define MESH_IO_H

#include <App/Material.h>
#include <Base/Matrix.h>

#include "MeshKernel.h"


namespace Base
{
class XMLReader;
class Writer;
}  // namespace Base

namespace MeshCore
{

class MeshKernel;

namespace MeshIO
{
enum Format

{
    Undefined,
    BMS,
    ASTL,
    BSTL,
    STL,
    OBJ,
    OFF,
    IDTF,
    MGL,
    IV,
    X3D,
    X3DZ,
    X3DOM,
    VRML,
    WRZ,
    NAS,
    PLY,
    APLY,
    PY,
    AMF,
    SMF,
    ASY,
    ThreeMF
};
enum Binding

{
    OVERALL,
    PER_VERTEX,
    PER_FACE
};
}  // namespace MeshIO

struct MeshExport Material

{
    Material() = default;
    MeshIO::Binding binding {MeshIO::OVERALL};
    mutable std::string library;

    std::vector<Base::Color> ambientColor;  /**< Defines the ambient color. */
    std::vector<Base::Color> diffuseColor;  /**< Defines the diffuse color. */
    std::vector<Base::Color> specularColor; /**< Defines the specular color. */
    std::vector<Base::Color> emissiveColor; /**< Defines the emissive color. */
    std::vector<float> shininess;
    std::vector<float> transparency;

    bool operator==(const Material& mat) const;
    bool operator!=(const Material& mat) const;
};

struct MeshExport Group

{
    std::vector<FacetIndex> indices;
    std::string name;
};

/**

 * The MeshInput class is able to read a mesh object from an input stream

 * in various formats.

 */
class MeshExport MeshInput

{
public:
    explicit MeshInput(MeshKernel& rclM)
        : _rclMesh(rclM)
        , _material(nullptr)
    {}
    MeshInput(MeshKernel& rclM, Material* m)
        : _rclMesh(rclM)
        , _material(m)
    {}
    const std::vector<std::string>& GetGroupNames() const
    {
        return _groupNames;
    }

    /// Loads the file, decided by extension
    bool LoadAny(const char* FileName);
    /// Loads from a stream and the given format
    bool LoadFormat(std::istream& input, MeshIO::Format fmt);
    /** Loads an STL file either in binary or ASCII format.

     * Therefore the file header gets checked to decide if the file is binary or not.

     */
    bool LoadSTL(std::istream& input);
    /** Loads an ASCII STL file. */
    bool LoadAsciiSTL(std::istream& input);
    /** Loads a binary STL file. */
    bool LoadBinarySTL(std::istream& input);
    /** Loads an OBJ Mesh file. */
    bool LoadOBJ(std::istream& input);
    /** Loads an OBJ Mesh file. */
    bool LoadOBJ(std::istream& input, const char* filename);
    /** Loads an SMF Mesh file. */
    bool LoadSMF(std::istream& input);
    /** Loads an OFF Mesh file. */
    bool LoadOFF(std::istream& input);
    /** Loads a PLY Mesh file. */
    bool LoadPLY(std::istream& input);
    /** Loads the mesh object from an XML file. */
    void LoadXML(Base::XMLReader& reader);
    /** Loads the mesh object from a 3MF file. */
    bool Load3MF(std::istream& input);
    /** Loads a node from an OpenInventor file. */
    bool LoadMeshNode(std::istream& input);
    /** Loads an OpenInventor file. */
    bool LoadInventor(std::istream& input);
    /** Loads a Nastran file. */
    bool LoadNastran(std::istream& input);
    /** Loads a Cadmould FE file. */
    bool LoadCadmouldFE(std::ifstream& input);

    static std::vector<std::string> supportedMeshFormats();
    static MeshIO::Format getFormat(const char* FileName);

private:
    MeshKernel& _rclMesh; /**< reference to mesh data structure */
    Material* _material;
    std::vector<std::string> _groupNames;
};

/**

 * The MeshOutput class is able to write a mesh object to an output stream

 * on various formats.

 */
class MeshExport MeshOutput

{
public:
    explicit MeshOutput(const MeshKernel& rclM)
        : _rclMesh(rclM)
        , _material(nullptr)
        , apply_transform(false)
    {}
    MeshOutput(const MeshKernel& rclM, const Material* m)
        : _rclMesh(rclM)
        , _material(m)
        , apply_transform(false)
    {}
    void SetObjectName(const std::string& n)
    {
        objectName = n;
    }
    void SetGroups(const std::vector<Group>& g)
    {
        _groups = g;
    }

    void Transform(const Base::Matrix4D&);
    /** Set custom data to the header of a binary STL.

     * If the data exceeds 80 characters then the characters too much

     * are ignored. If the data has less than 80 characters they are

     * automatically filled up with spaces.

     */
    static void SetSTLHeaderData(const std::string&);
    /**

     * Change the image size of the asymptote output.

     */
    static void SetAsymptoteSize(const std::string&, const std::string&);
    /// Determine the mesh format by file extension
    static MeshIO::Format GetFormat(const char* FileName);
    /// Saves the file, decided by extension if not explicitly given
    bool SaveAny(const char* FileName, MeshIO::Format f = MeshIO::Undefined) const;
    /// Saves to a stream and the given format
    bool SaveFormat(std::ostream& str, MeshIO::Format fmt) const;

    /** Saves the mesh object into an ASCII STL file. */
    bool SaveAsciiSTL(std::ostream& output) const;
    /** Saves the mesh object into a binary STL file. */
    bool SaveBinarySTL(std::ostream& output) const;
    /** Saves the mesh object into an OBJ file. */
    bool SaveOBJ(std::ostream& output) const;
    /** Saves the mesh object into an OBJ file. */
    bool SaveOBJ(std::ostream& output, const char* filename) const;
    /** Saves the mesh object into an SMF file. */
    bool SaveSMF(std::ostream& output) const;
    /** Saves the mesh object into an OFF file. */
    bool SaveOFF(std::ostream& output) const;
    /** Saves the mesh object into a binary PLY file. */
    bool SaveBinaryPLY(std::ostream& output) const;
    /** Saves the mesh object into an ASCII PLY file. */
    bool SaveAsciiPLY(std::ostream& output) const;
    /** Saves the mesh object into an asymptote file. */
    bool SaveAsymptote(std::ostream& output) const;
    /** Saves the mesh object into an XML file. */
    void SaveXML(Base::Writer& writer) const;
    /** Saves the mesh object into a 3MF file. */
    bool Save3MF(std::ostream& output) const;
    /** Saves a node to an OpenInventor file. */
    bool SaveMeshNode(std::ostream& output);
    /** Writes an IDTF file. */
    bool SaveIDTF(std::ostream& str) const;
    /** Writes an MGL file. */
    bool SaveMGL(std::ostream& str) const;
    /** Writes an OpenInventor file. */
    bool SaveInventor(std::ostream& output) const;
    /** Writes an X3D file. */
    bool SaveX3D(std::ostream& output) const;
    /** Writes an X3dom file. */
    bool SaveX3DOM(std::ostream& output) const;
    /** Writes a VRML file. */
    bool SaveVRML(std::ostream& output) const;
    /** Writes a Nastran file. */
    bool SaveNastran(std::ostream& output) const;
    /** Writes a Cadmould FE file. */
    bool SaveCadmouldFE(std::ostream& output) const;
    /** Writes a python module which creates a mesh */
    bool SavePython(std::ostream& str) const;

    static std::vector<std::string> supportedMeshFormats();

protected:
    /** Writes an X3D file. */
    bool SaveX3DContent(std::ostream& out, bool exportViewpoints) const;

private:
    const MeshKernel& _rclMesh; /**< reference to mesh data structure */
    const Material* _material;
    Base::Matrix4D _transform;
    bool apply_transform;
    std::string objectName;
    std::vector<Group> _groups;
    static std::string stl_header;
    static std::string asyWidth;
    static std::string asyHeight;
};

/*!

  The MeshCleanup class is a helper class to remove points from the point array that are not

  referenced by any facet. It also removes facet with point indices that are out of range.

 */
class MeshCleanup

{
public:
    /*!

      \brief Construction.

      \param p -- the point array

      \param f -- the facet array

     */
    MeshCleanup(MeshPointArray& p, MeshFacetArray& f);

    /*!

      \brief Set the material array.

      In case the material array sets the colors per vertex and

      \ref RemoveInvalids() removes points from the array the

      material array will be adjusted.

     */
    void SetMaterial(Material* mat);

    /*!

      \brief Remove unreferenced and invalid facets.

     */
    void RemoveInvalids();

private:
    /*!

      \brief Remove invalid facets.

     */
    void RemoveInvalidFacets();
    /*!

      \brief Remove invalid points.

     */
    void RemoveInvalidPoints();

private:
    MeshPointArray& pointArray;
    MeshFacetArray& facetArray;
    Material* materialArray {nullptr};
};

/*!

   The MeshPointFacetAdjacency class is a helper class to get all facets per vertex

   and set the neighbourhood of the facets.

   At this point the MeshFacetArray only references the points but does not have set

   the neighbourhood of two adjacent facets.

 */
class MeshPointFacetAdjacency

{
public:
    /*!

      \brief Construction.

      \param p -- the number of points

      \param f -- the facet array

     */
    MeshPointFacetAdjacency(std::size_t p, MeshFacetArray& f);

    /*!

      \brief Set the neighbourhood of two adjacent facets.

     */
    void SetFacetNeighbourhood();

private:
    /*!

      \brief Build up the adjacency information.

     */
    void Build();

private:
    std::size_t numPoints;
    MeshFacetArray& facets;
    std::vector<std::vector<std::size_t>> pointFacetAdjacency;
};


}  // namespace MeshCore

#endif  // MESH_IO_H