diff options
Diffstat (limited to 'src/others/mimesh/libg3d-0.0.8/plugins/import/imp_acf/imp_acf_airfoil.c')
-rw-r--r-- | src/others/mimesh/libg3d-0.0.8/plugins/import/imp_acf/imp_acf_airfoil.c | 166 |
1 files changed, 166 insertions, 0 deletions
diff --git a/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_acf/imp_acf_airfoil.c b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_acf/imp_acf_airfoil.c new file mode 100644 index 0000000..169725d --- /dev/null +++ b/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_acf/imp_acf_airfoil.c | |||
@@ -0,0 +1,166 @@ | |||
1 | /* $Id$ */ | ||
2 | |||
3 | /* | ||
4 | libg3d - 3D object loading library | ||
5 | |||
6 | Copyright (C) 2005-2009 Markus Dahms <mad@automagically.de> | ||
7 | |||
8 | This library is free software; you can redistribute it and/or | ||
9 | modify it under the terms of the GNU Lesser General Public | ||
10 | License as published by the Free Software Foundation; either | ||
11 | version 2.1 of the License, or (at your option) any later version. | ||
12 | |||
13 | This library is distributed in the hope that it will be useful, | ||
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
16 | Lesser General Public License for more details. | ||
17 | |||
18 | You should have received a copy of the GNU Lesser General Public | ||
19 | License along with this library; if not, write to the Free Software | ||
20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
21 | */ | ||
22 | #include <string.h> | ||
23 | #include <stdlib.h> | ||
24 | #include <stdio.h> | ||
25 | #include <locale.h> | ||
26 | |||
27 | #include <glib.h> | ||
28 | #include <g3d/types.h> | ||
29 | |||
30 | #include "imp_acf_airfoil.h" | ||
31 | |||
32 | static AcfAirfoil *acf_airfoil_read(const gchar *path); | ||
33 | |||
34 | AcfAirfoilDb *acf_airfoil_init(void) | ||
35 | { | ||
36 | AcfAirfoilDb *db; | ||
37 | AcfAirfoil *afl; | ||
38 | GDir *dir; | ||
39 | const gchar *dirname, *filename; | ||
40 | gchar *path; | ||
41 | GError *error = NULL; | ||
42 | |||
43 | setlocale(LC_NUMERIC, "C"); | ||
44 | |||
45 | dirname = g_getenv("AIRFOIL_DIR"); | ||
46 | if(!(dirname && g_file_test(dirname, G_FILE_TEST_IS_DIR))) { | ||
47 | #if DEBUG > 0 | ||
48 | g_warning("ACF: could not load airfoils"); | ||
49 | #endif | ||
50 | return NULL; | ||
51 | } | ||
52 | |||
53 | dir = g_dir_open(dirname, 0, &error); | ||
54 | if(error != NULL) { | ||
55 | g_warning("ACF: failed to open airfoil directory '%s': %s", | ||
56 | dirname, error->message); | ||
57 | g_error_free(error); | ||
58 | return NULL; | ||
59 | } | ||
60 | |||
61 | db = g_new0(AcfAirfoilDb, 1); | ||
62 | db->db = g_hash_table_new(g_str_hash, g_str_equal); | ||
63 | |||
64 | filename = g_dir_read_name(dir); | ||
65 | while(filename != NULL) { | ||
66 | if(strcmp(filename + strlen(filename) - 4, ".dat") == 0) { | ||
67 | path = g_strdup_printf("%s%c%s", dirname, G_DIR_SEPARATOR, | ||
68 | filename); | ||
69 | afl = acf_airfoil_read(path); | ||
70 | g_free(path); | ||
71 | if(afl != NULL) { | ||
72 | g_hash_table_insert(db->db, afl->filename, afl); | ||
73 | db->airfoils = g_slist_append(db->airfoils, afl); | ||
74 | #if DEBUG > 2 | ||
75 | g_debug("ACF: airfoil %s loaded", filename); | ||
76 | #endif | ||
77 | } | ||
78 | } | ||
79 | filename = g_dir_read_name(dir); | ||
80 | } | ||
81 | |||
82 | #if DEBUG > 0 | ||
83 | g_debug("ACF: %d airfoils loaded", g_slist_length(db->airfoils)); | ||
84 | #endif | ||
85 | g_dir_close(dir); | ||
86 | |||
87 | return db; | ||
88 | } | ||
89 | |||
90 | void acf_airfoil_free(AcfAirfoil *afl) | ||
91 | { | ||
92 | if(afl->filename) | ||
93 | g_free(afl->filename); | ||
94 | if(afl->description) | ||
95 | g_free(afl->description); | ||
96 | if(afl->vertex_data) | ||
97 | g_free(afl->vertex_data); | ||
98 | g_free(afl); | ||
99 | } | ||
100 | |||
101 | void acf_airfoil_cleanup(AcfAirfoilDb *db) | ||
102 | { | ||
103 | AcfAirfoil *afl; | ||
104 | GSList *item; | ||
105 | |||
106 | g_hash_table_destroy(db->db); | ||
107 | item = db->airfoils; | ||
108 | while(item) { | ||
109 | afl = item->data; | ||
110 | item = g_slist_remove(item, afl); | ||
111 | acf_airfoil_free(afl); | ||
112 | } | ||
113 | g_free(db); | ||
114 | } | ||
115 | |||
116 | AcfAirfoil *acf_airfoil_lookup(AcfAirfoilDb *db, const gchar *aflname) | ||
117 | { | ||
118 | return g_hash_table_lookup(db->db, aflname); | ||
119 | } | ||
120 | |||
121 | static AcfAirfoil *acf_airfoil_read(const gchar *path) | ||
122 | { | ||
123 | AcfAirfoil *afl; | ||
124 | FILE *f; | ||
125 | gchar buffer[BUFSIZ + 1]; | ||
126 | guint32 off; | ||
127 | G3DFloat x, y; | ||
128 | |||
129 | f = fopen(path, "r"); | ||
130 | if(f == NULL) | ||
131 | return NULL; | ||
132 | |||
133 | afl = g_new0(AcfAirfoil, 1); | ||
134 | fgets(buffer, BUFSIZ, f); | ||
135 | buffer[BUFSIZ] = '\0'; | ||
136 | g_strstrip(buffer); | ||
137 | afl->filename = g_path_get_basename(path); | ||
138 | afl->description = g_strdup(buffer); | ||
139 | |||
140 | while(!feof(f)) { | ||
141 | memset(buffer, '\0', BUFSIZ + 1); | ||
142 | fgets(buffer, BUFSIZ, f); | ||
143 | g_strstrip(buffer); | ||
144 | if(strlen(buffer) == 0) | ||
145 | continue; | ||
146 | if(sscanf(buffer, G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT, &x, &y) == 2) { | ||
147 | if((x < 0.0) || (x > 1.0)) | ||
148 | continue; | ||
149 | off = afl->vertex_count; | ||
150 | afl->vertex_count ++; | ||
151 | afl->vertex_data = g_realloc(afl->vertex_data, | ||
152 | afl->vertex_count * 2 * sizeof(G3DFloat)); | ||
153 | afl->vertex_data[off * 2 + 0] = x; | ||
154 | afl->vertex_data[off * 2 + 1] = y; | ||
155 | } else { | ||
156 | #if DEBUG > 2 | ||
157 | g_debug("ACF: airfoil: failed to parse line in %s: %s", | ||
158 | afl->filename, buffer); | ||
159 | #endif | ||
160 | } | ||
161 | } | ||
162 | |||
163 | fclose(f); | ||
164 | |||
165 | return afl; | ||
166 | } | ||