diff options
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_stl/imp_stl.c')
-rw-r--r-- | src/others/mimesh/libg3d-0.0.8/plugins/import/imp_stl/imp_stl.c | 200 |
1 files changed, 200 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_stl/imp_stl.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_stl/imp_stl.c new file mode 100644 index 0000000..04a0f4a --- /dev/null +++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_stl/imp_stl.c | |||
@@ -0,0 +1,200 @@ | |||
1 | /* | ||
2 | libg3d - 3D object loading library | ||
3 | |||
4 | Copyright (C) 2006 Oliver Dippel <o.dippel@gmx.de> | ||
5 | 2008 Markus Dahms <mad@automagically.de> | ||
6 | |||
7 | This library is free software; you can redistribute it and/or | ||
8 | modify it under the terms of the GNU Lesser General Public | ||
9 | License as published by the Free Software Foundation; either | ||
10 | version 2.1 of the License, or (at your option) any later version. | ||
11 | |||
12 | This library is distributed in the hope that it will be useful, | ||
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
15 | Lesser General Public License for more details. | ||
16 | |||
17 | You should have received a copy of the GNU Lesser General Public | ||
18 | License along with this library; if not, write to the Free Software | ||
19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
20 | */ | ||
21 | |||
22 | #include <stdio.h> | ||
23 | #include <stdlib.h> | ||
24 | #include <string.h> | ||
25 | #include <locale.h> | ||
26 | |||
27 | #include <g3d/types.h> | ||
28 | #include <g3d/stream.h> | ||
29 | #include <g3d/material.h> | ||
30 | |||
31 | /* | ||
32 | Infos for the STL(A)-Format: | ||
33 | http://www.csit.fsu.edu/~burkardt/data/stla/stla.html | ||
34 | Infos for the STL(B)-Format: | ||
35 | http://www.csit.fsu.edu/~burkardt/data/stlb/stlb.html | ||
36 | */ | ||
37 | |||
38 | #define STL_ASCII 0 | ||
39 | #define STL_BINARY 1 | ||
40 | |||
41 | static gboolean stl_load_binary(G3DContext *context, G3DModel *model, | ||
42 | G3DStream *stream); | ||
43 | static gboolean stl_load_text(G3DContext *context, G3DModel *model, | ||
44 | G3DStream *stream); | ||
45 | |||
46 | EAPI | ||
47 | gboolean plugin_load_model_from_stream(G3DContext *context, G3DStream *stream, | ||
48 | G3DModel *model, gpointer user_data) | ||
49 | { | ||
50 | gchar line[1024]; | ||
51 | guint32 type; | ||
52 | |||
53 | /* Check Filetype (ASCII or BINARY) */ | ||
54 | type = STL_BINARY; | ||
55 | while(!g3d_stream_eof(stream)) { | ||
56 | if(!g3d_stream_read_line(stream, line, 1023)) | ||
57 | break; | ||
58 | if(strstr(line, "solid")) { | ||
59 | setlocale(LC_NUMERIC, "C"); | ||
60 | type = STL_ASCII; | ||
61 | break; | ||
62 | } | ||
63 | } | ||
64 | /* rewind */ | ||
65 | g3d_stream_seek(stream, 0, G_SEEK_SET); | ||
66 | |||
67 | if (type == STL_BINARY) | ||
68 | return stl_load_binary(context, model, stream); | ||
69 | else | ||
70 | return stl_load_text(context, model, stream); | ||
71 | } | ||
72 | |||
73 | EAPI | ||
74 | gchar *plugin_description(G3DContext *context) | ||
75 | { | ||
76 | return g_strdup("STLA and STLB stereolithography models.\n" | ||
77 | "Author: Oliver Dippel"); | ||
78 | } | ||
79 | |||
80 | EAPI | ||
81 | gchar **plugin_extensions(G3DContext *context) | ||
82 | { | ||
83 | return g_strsplit("stl:stla:stlb", ":", 0); | ||
84 | } | ||
85 | |||
86 | /*****************************************************************************/ | ||
87 | |||
88 | static gboolean stl_load_binary(G3DContext *context, G3DModel *model, | ||
89 | G3DStream *stream) | ||
90 | { | ||
91 | G3DObject *object; | ||
92 | G3DMaterial *material; | ||
93 | G3DFace *face; | ||
94 | gchar name[81]; | ||
95 | guint32 num_faces, index = 0; | ||
96 | gint32 n, i, j; | ||
97 | |||
98 | #if DEBUG > 0 | ||
99 | g_debug("STL: format is BINARY"); | ||
100 | #endif | ||
101 | |||
102 | g3d_stream_read(stream, name, 80); | ||
103 | name[80] = 0; | ||
104 | num_faces = g3d_stream_read_int32_le(stream); | ||
105 | |||
106 | object = g_new0(G3DObject, 1); | ||
107 | object->name = g_strdup("STL-Model"); | ||
108 | model->objects = g_slist_append(model->objects, object); | ||
109 | |||
110 | material = g3d_material_new(); | ||
111 | material->name = g_strdup("default material"); | ||
112 | object->materials = g_slist_append(object->materials, material); | ||
113 | |||
114 | object->vertex_count = num_faces * 3; | ||
115 | #if DEBUG > 2 | ||
116 | g_debug("STL: BINARY: vertex_count: %i", object->vertex_count); | ||
117 | #endif | ||
118 | object->vertex_data = g_new0(G3DFloat, object->vertex_count * 3); | ||
119 | for(n = 0; n < num_faces; n ++) { | ||
120 | face = g_new0(G3DFace, 1); | ||
121 | face->material = material; | ||
122 | face->vertex_count = 3; | ||
123 | face->vertex_indices = g_new0(guint32, face->vertex_count); | ||
124 | face->vertex_indices[0] = index + 0; | ||
125 | face->vertex_indices[1] = index + 1; | ||
126 | face->vertex_indices[2] = index + 2; | ||
127 | object->faces = g_slist_prepend(object->faces, face); | ||
128 | /* normal */ | ||
129 | for(j = 0; j < 3; j ++) | ||
130 | g3d_stream_read_float_le(stream); | ||
131 | /* triangle */ | ||
132 | for(i = 0; i < 3; i ++) { | ||
133 | for(j = 0; j < 3; j ++) | ||
134 | object->vertex_data[index * 3 + j] = | ||
135 | g3d_stream_read_float_le(stream); | ||
136 | index ++; | ||
137 | } | ||
138 | /* 2 Byte Dummy read */ | ||
139 | g3d_stream_read_int16_le(stream); | ||
140 | } | ||
141 | return TRUE; | ||
142 | } | ||
143 | |||
144 | static gboolean stl_load_text(G3DContext *context, G3DModel *model, | ||
145 | G3DStream *stream) | ||
146 | { | ||
147 | G3DObject *object; | ||
148 | G3DMaterial *material; | ||
149 | G3DFace *face; | ||
150 | gchar line[1024]; | ||
151 | guint32 index = 0; | ||
152 | G3DFloat x, y, z; | ||
153 | |||
154 | #if DEBUG > 0 | ||
155 | g_debug("STL: format is ASCII"); | ||
156 | #endif | ||
157 | |||
158 | object = g_new0(G3DObject, 1); | ||
159 | object->name = g_strdup("STL-Model"); | ||
160 | model->objects = g_slist_append(model->objects, object); | ||
161 | material = g3d_material_new(); | ||
162 | material->name = g_strdup("default material"); | ||
163 | object->materials = g_slist_append(object->materials, material); | ||
164 | object->vertex_count = 0; | ||
165 | while(!g3d_stream_eof(stream)) { | ||
166 | line[0] = 0; | ||
167 | if(!g3d_stream_read_line(stream, line, 1023)) | ||
168 | break; | ||
169 | g_strstrip(line); | ||
170 | if(strncmp(line, "solid", 5) == 0) { | ||
171 | g_free(object->name); | ||
172 | object->name = g_strdup(line + 6); | ||
173 | } else if(strncmp(line, "vertex", 6) == 0) { | ||
174 | object->vertex_count ++; | ||
175 | object->vertex_data = g_realloc(object->vertex_data, | ||
176 | object->vertex_count * 3 * sizeof(G3DFloat)); | ||
177 | if(sscanf(line + 7, G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT, &x, &y, &z) == 3) { | ||
178 | object->vertex_data[(object->vertex_count - 1) * 3 + 0] = x; | ||
179 | object->vertex_data[(object->vertex_count - 1) * 3 + 1] = y; | ||
180 | object->vertex_data[(object->vertex_count - 1) * 3 + 2] = z; | ||
181 | } else { | ||
182 | #if DEBUG > 0 | ||
183 | g_debug("imp_stl: parse error in vertex line: %s", line); | ||
184 | #endif | ||
185 | } | ||
186 | } else if(strncmp(line, "facet", 5) == 0) { | ||
187 | face = g_new0(G3DFace, 1); | ||
188 | face->material = material; | ||
189 | face->vertex_count = 3; | ||
190 | face->vertex_indices = g_new0(guint32, face->vertex_count); | ||
191 | face->vertex_indices[0] = index + 0; | ||
192 | face->vertex_indices[1] = index + 1; | ||
193 | face->vertex_indices[2] = index + 2; | ||
194 | object->faces = g_slist_prepend(object->faces, face); | ||
195 | index += 3; | ||
196 | } | ||
197 | } | ||
198 | return TRUE; | ||
199 | } | ||
200 | |||