aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/mimesh/libg3d-0.0.8/plugins/import/imp_acf/imp_acf_airfoil.c
blob: 169725df11b62920f1d2b0d4448f3dc525ec4f14 (plain)
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
/* $Id$ */

/*
    libg3d - 3D object loading library

    Copyright (C) 2005-2009  Markus Dahms <mad@automagically.de>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <locale.h>

#include <glib.h>
#include <g3d/types.h>

#include "imp_acf_airfoil.h"

static AcfAirfoil *acf_airfoil_read(const gchar *path);

AcfAirfoilDb *acf_airfoil_init(void)
{
	AcfAirfoilDb *db;
	AcfAirfoil *afl;
	GDir *dir;
	const gchar *dirname, *filename;
	gchar *path;
	GError *error = NULL;

	setlocale(LC_NUMERIC, "C");

	dirname = g_getenv("AIRFOIL_DIR");
	if(!(dirname && g_file_test(dirname, G_FILE_TEST_IS_DIR))) {
#if DEBUG > 0
		g_warning("ACF: could not load airfoils");
#endif
		return NULL;
	}

	dir = g_dir_open(dirname, 0, &error);
	if(error != NULL) {
		g_warning("ACF: failed to open airfoil directory '%s': %s",
			dirname, error->message);
		g_error_free(error);
		return NULL;
	}

	db = g_new0(AcfAirfoilDb, 1);
	db->db = g_hash_table_new(g_str_hash, g_str_equal);

	filename = g_dir_read_name(dir);
	while(filename != NULL) {
		if(strcmp(filename + strlen(filename) - 4, ".dat") == 0) {
			path = g_strdup_printf("%s%c%s", dirname, G_DIR_SEPARATOR,
				filename);
			afl = acf_airfoil_read(path);
			g_free(path);
			if(afl != NULL) {
				g_hash_table_insert(db->db, afl->filename, afl);
				db->airfoils = g_slist_append(db->airfoils, afl);
#if DEBUG > 2
				g_debug("ACF: airfoil %s loaded", filename);
#endif
			}
		}
		filename = g_dir_read_name(dir);
	}

#if DEBUG > 0
	g_debug("ACF: %d airfoils loaded", g_slist_length(db->airfoils));
#endif
	g_dir_close(dir);

	return db;
}

void acf_airfoil_free(AcfAirfoil *afl)
{
	if(afl->filename)
		g_free(afl->filename);
	if(afl->description)
		g_free(afl->description);
	if(afl->vertex_data)
		g_free(afl->vertex_data);
	g_free(afl);
}

void acf_airfoil_cleanup(AcfAirfoilDb *db)
{
	AcfAirfoil *afl;
	GSList *item;

	g_hash_table_destroy(db->db);
	item = db->airfoils;
	while(item) {
		afl = item->data;
		item = g_slist_remove(item, afl);
		acf_airfoil_free(afl);
	}
	g_free(db);
}

AcfAirfoil *acf_airfoil_lookup(AcfAirfoilDb *db, const gchar *aflname)
{
	return g_hash_table_lookup(db->db, aflname);
}

static AcfAirfoil *acf_airfoil_read(const gchar *path)
{
	AcfAirfoil *afl;
	FILE *f;
	gchar buffer[BUFSIZ + 1];
	guint32 off;
	G3DFloat x, y;

	f = fopen(path, "r");
	if(f == NULL)
		return NULL;

	afl = g_new0(AcfAirfoil, 1);
	fgets(buffer, BUFSIZ, f);
	buffer[BUFSIZ] = '\0';
	g_strstrip(buffer);
	afl->filename = g_path_get_basename(path);
	afl->description = g_strdup(buffer);

	while(!feof(f)) {
		memset(buffer, '\0', BUFSIZ + 1);
		fgets(buffer, BUFSIZ, f);
		g_strstrip(buffer);
		if(strlen(buffer) == 0)
			continue;
		if(sscanf(buffer, G3D_SCANF_FLOAT " " G3D_SCANF_FLOAT, &x, &y) == 2) {
			if((x < 0.0) || (x > 1.0))
				continue;
			off = afl->vertex_count;
			afl->vertex_count ++;
			afl->vertex_data = g_realloc(afl->vertex_data,
				afl->vertex_count * 2 * sizeof(G3DFloat));
			afl->vertex_data[off * 2 + 0] = x;
			afl->vertex_data[off * 2 + 1] = y;
		} else {
#if DEBUG > 2
			g_debug("ACF: airfoil: failed to parse line in %s: %s",
				afl->filename, buffer);
#endif
		}
	}

	fclose(f);

	return afl;
}