File: export_stl.h

package info (click to toggle)
meshlab 2020.09%2Bdfsg1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 45,132 kB
  • sloc: cpp: 400,238; ansic: 31,952; javascript: 1,578; sh: 387; yacc: 238; lex: 139; python: 86; makefile: 30
file content (194 lines) | stat: -rw-r--r-- 6,861 bytes parent folder | download | duplicates (2)
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
/****************************************************************************
* VCGLib                                                            o o     *
* Visual and Computer Graphics Library                            o     o   *
*                                                                _   O  _   *
* Copyright(C) 2004-2016                                           \/)\/    *
* Visual Computing Lab                                            /\/|      *
* ISTI - Italian National Research Council                           |      *
*                                                                    \      *
* All rights reserved.                                                      *
*                                                                           *
* This program is free software; you can redistribute it and/or modify      *
* it under the terms of the GNU General Public License as published by      *
* the Free Software Foundation; either version 2 of the License, or         *
* (at your option) any later version.                                       *
*                                                                           *
* This program 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 General Public License (http://www.gnu.org/licenses/gpl.txt)          *
* for more details.                                                         *
*                                                                           *
****************************************************************************/

/****************************************************************************
  History

$Log: not supported by cvs2svn $
Revision 1.8  2006/09/18 08:55:33  cignoni
Corrected return value of save function (zero is no error)

Revision 1.7  2006/01/30 13:43:59  cignoni
Added GetExportMaskCapability

Revision 1.6  2006/01/13 15:47:43  cignoni
Uniformed return type to the style of Open. Now every export function returns 0 in case of success.

Revision 1.5  2005/12/01 00:58:56  cignoni
Added and removed typenames for gcc compiling...

Revision 1.4  2004/10/28 00:52:45  cignoni
Better Doxygen documentation

Revision 1.3  2004/03/09 21:26:47  cignoni
cr lf mismatch

Revision 1.2  2004/03/03 15:35:53  cignoni
Yet another cr lf mismatch

Revision 1.3  2004/02/19 15:28:01  ponchio
*** empty log message ***

Revision 1.2  2004/02/13 02:18:57  cignoni
Edited Comments and GPL license


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

#ifndef __VCGLIB_EXPORT_STL
#define __VCGLIB_EXPORT_STL

#include <stdio.h>

namespace vcg {
namespace tri {
namespace io {

/**
This class encapsulate a filter for opening stl (sterolitograpy) meshes.
The stl format is quite simple and rather un-flexible. It just stores, in ascii or binary the, unindexed, geometry of the faces.
*/
template <class SaveMeshType>
class ExporterSTL
{
public:
typedef typename SaveMeshType::FaceType FaceType;
typedef unsigned short CallBackSTLFaceAttribute(const SaveMeshType &m, const FaceType &f);

static int Save(SaveMeshType &m, const char * filename, const int &mask, CallBackPos *)
{
 return Save(m,filename,true,mask);
}

static int Save(SaveMeshType &m, const char * filename , bool binary =true, int mask=0, const char *objectname=0, bool magicsMode=0)
{
  typedef typename SaveMeshType::FaceIterator FaceIterator;
    FILE *fp;

    fp = fopen(filename,"wb");
    if(fp==0)
        return 1;

    if(binary)
    {
        // Write Header
        char header[128]="VCG                                                                                                  ";
        if(objectname)	strncpy(header,objectname,80);
        if(magicsMode)
        {
          strncpy(header,"COLOR=XXX MATERIAL=AAA BBB CCC                                                                       ",80);
          for(int i=0;i<3;++i)
          {
            header[0x06+i]=0x7f;
            header[0x13+i]=0x7f;
            header[0x17+i]=0x7f;
            header[0x1b+i]=0x7f;
          }
        }
        fwrite(header,80,1,fp);
        // write number of facets
        fwrite(&m.fn,1,sizeof(int),fp);
        Point3f p;
        unsigned short attributes=0;
        for(FaceIterator fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
        {
            // For each triangle write the normal, the three coords and a short set to zero
            p.Import(vcg::TriangleNormal(*fi).Normalize());
            fwrite(p.V(),3,sizeof(float),fp);

            for(int k=0;k<3;++k){
                p.Import((*fi).V(k)->P());
                fwrite(p.V(),3,sizeof(float),fp);
            }
            if ((mask & Mask::IOM_FACECOLOR) && tri::HasPerFaceColor(m))
            {
              if(magicsMode) attributes = 32768 | vcg::Color4b::ToUnsignedR5G5B5(fi->C());
                        else attributes = 32768 | vcg::Color4b::ToUnsignedB5G5R5(fi->C());
            }
            fwrite(&attributes,1,sizeof(short),fp);
        }
    }
    else
    {
        if(objectname) fprintf(fp,"solid %s\n",objectname);
        else fprintf(fp,"solid vcg\n");

        Point3f p;
        FaceIterator fi;
        for(fi=m.face.begin(); fi!=m.face.end(); ++fi) if( !(*fi).IsD() )
        {
        // For each triangle write the normal, the three coords and a short set to zero
            p.Import(TriangleNormal(*fi).Normalize());
            fprintf(fp,"  facet normal %13e %13e %13e\n",p[0],p[1],p[2]);
            fprintf(fp,"    outer loop\n");
            for(int k=0;k<3;++k){
                p.Import((*fi).V(k)->P());
                fprintf(fp,"      vertex  %13e %13e %13e\n",p[0],p[1],p[2]);
            }
            fprintf(fp,"    endloop\n");
            fprintf(fp,"  endfacet\n");
        }
        fprintf(fp,"endsolid vcg\n");
    }
	int result = 0;
	if (ferror(fp)) result = 2;
    fclose(fp);
	return result;
}
static const char *ErrorMsg(int error)
{
	static std::vector<std::string> stl_error_msg;
	if (stl_error_msg.empty())
	{
		stl_error_msg.resize(3);
		stl_error_msg[0] = "No errors";
		stl_error_msg[1] = "Can't open file";
		stl_error_msg[2] = "Output Stream error";
	}

	if (error>2 || error<0) return "Unknown error";
	else return stl_error_msg[error].c_str();
};

/*
    returns mask of capability one define with what are the saveable information of the format.
*/
static int GetExportMaskCapability()
{
    int capability = 0;
    capability |= vcg::tri::io::Mask::IOM_VERTCOORD;
    capability |= vcg::tri::io::Mask::IOM_FACEINDEX;
    capability |= vcg::tri::io::Mask::IOM_FACECOLOR;
    return capability;
}


}; // end class

} // end Namespace tri
} // end Namespace io
} // end Namespace vcg



#endif