1 /**************************************************************************
2 * mesh.c -- data structures and routines for processing terrain meshes
4 * Written by Curtis Olson, started May 1997.
6 * Copyright (C) 1997 Curtis L. Olson - curt@infoplane.com
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 * (Log is kept at end of this file)
24 **************************************************************************/
29 #include <stdlib.h> /* atof(), atoi() */
38 /* Temporary hack until we get the scenery management system running */
39 extern GLint mesh_hack;
40 extern struct mesh eg;
42 /* initialize the non-array mesh values */
43 void mesh_init(struct mesh *m) {
59 /* return a pointer to a new mesh structure (no data array allocated yet) */
60 struct mesh *(new_mesh)() {
61 struct mesh *mesh_ptr;
63 mesh_ptr = (struct mesh *)malloc(sizeof(struct mesh));
65 if ( mesh_ptr == 0 ) {
66 printf("Virtual memory exceeded\n");
70 mesh_ptr->cur_row = 0;
71 mesh_ptr->cur_col = 0;
77 /* return a pointer to a dynamically allocated array */
78 float *(new_mesh_data)(int nrows, int ncols) {
81 mesh_data_ptr = (float *)malloc(nrows * ncols * sizeof(float));
83 if ( mesh_data_ptr == 0 ) {
84 printf("Virtual memory exceeded\n");
88 printf("Allocated float(%d, %d)\n", nrows, ncols);
90 return(mesh_data_ptr);
94 /* set the option name in the mesh data structure */
95 void mesh_set_option_name(struct mesh *m, char *name) {
96 if ( strlen(name) < MAX_IDENT_LEN ) {
97 strcpy(m->option_name, name);
99 strncpy(m->option_name, name, MAX_IDENT_LEN - 1);
100 m->option_name[MAX_IDENT_LEN - 1] = '\0';
102 if ( strcmp(m->option_name, "do_data") == 0 ) {
110 /* set an option value in the mesh data structure */
111 void mesh_set_option_value(struct mesh *m, char *value) {
112 /* printf("Setting %s to %s\n", m->option_name, value); */
115 /* mesh data is a pseudo 2d array */
116 /* printf("Setting mesh_data[%d][%d] to %s\n", m->cur_row, m->cur_col,
118 m->mesh_data[m->cur_row * m->rows + m->cur_col] = atof(value);
120 if ( m->cur_col >= m->cols ) {
123 if ( m->cur_row > m->rows ) {
127 } else if ( strcmp(m->option_name, "origin_lon") == 0 ) {
128 m->originx = atof(value);
129 } else if ( strcmp(m->option_name, "origin_lat") == 0 ) {
130 m->originy = atof(value);
131 } else if ( strcmp(m->option_name, "rows") == 0 ) {
132 m->rows = atoi(value);
133 } else if ( strcmp(m->option_name, "cols") == 0 ) {
134 m->cols = atoi(value);
135 } else if ( strcmp(m->option_name, "row_step") == 0 ) {
136 m->row_step = atof(value);
137 } else if ( strcmp(m->option_name, "col_step") == 0 ) {
138 m->col_step = atof(value);
140 printf("Unknown option %s with value %s, ignoring ...\n",
141 m->option_name, value);
146 /* do whatever needs to be done with the mesh now that it's been
147 loaded, such as generating the OpenGL call list. */
148 void mesh_do_it(struct mesh *m) {
149 mesh_hack = mesh2GL(m);
153 /* return the current altitude based on mesh data. We should rewrite
154 * this to interpolate exact values, but for now this is good enough */
155 double mesh_altitude(double lon, double lat) {
156 /* we expect incoming (lon,lat) to be in arcsec for now */
158 double xlocal, ylocal, dx, dy, zA, zB, elev;
159 int x1, y1, z1, x2, y2, z2, x3, y3, z3;
162 /* determine if we are in the lower triangle or the upper triangle
170 then calculate our end points
173 xlocal = ( lon - eg.originx ) / eg.col_step;
174 ylocal = ( lat - eg.originy ) / eg.row_step;
176 xindex = (int)xlocal;
177 yindex = (int)ylocal;
179 dx = xlocal - xindex;
180 dy = ylocal - yindex;
184 printf(" Lower triangle\n");
188 z1 = eg.mesh_data[x1 * eg.rows + y1];
190 x2 = xindex + eg.col_step;
192 z2 = eg.mesh_data[x2 * eg.rows + y2];
194 x3 = xindex + eg.col_step;
195 y3 = yindex + eg.row_step;
196 z3 = eg.mesh_data[x3 * eg.rows + y3];
198 printf(" (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
199 printf(" (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
200 printf(" (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3);
202 zA = dx * (z2 - z1) / eg.col_step + z1;
203 zB = dx * (z3 - z1) / eg.col_step + z1;
205 printf(" zA = %.2f zB = %.2f\n", zA, zB);
207 elev = dy * (zB - zA) * eg.col_step / (eg.row_step * dx) + zA;
210 printf(" Upper triangle\n");
214 z1 = eg.mesh_data[x1 * eg.rows + y1];
217 y2 = yindex + eg.row_step;
218 z2 = eg.mesh_data[x2 * eg.rows + y2];
220 x3 = xindex + eg.col_step;
221 y3 = yindex + eg.row_step;
222 z3 = eg.mesh_data[x3 * eg.rows + y3];
224 printf(" (x1,y1,z1) = (%d,%d,%d)\n", x1, y1, z1);
225 printf(" (x2,y2,z2) = (%d,%d,%d)\n", x2, y2, z2);
226 printf(" (x3,y3,z3) = (%d,%d,%d)\n", x3, y3, z3);
228 zA = dy * (z2 - z1) / eg.row_step + z1;
229 zB = dy * (z3 - z1) / eg.row_step + z1;
231 printf(" zA = %.2f zB = %.2f\n", zA, zB );
232 printf(" dx = %.2f xB - xA = %.2f\n", dx,
233 eg.col_step * dy / eg.row_step);
235 elev = dx * (zB - zA) * eg.row_step / (eg.col_step * dy) + zA;
238 printf("Our true ground elevation is %.2f\n", elev);
245 /* Revision 1.10 1997/07/10 04:26:38 curt
246 /* We now can interpolated ground elevation for any position in the grid. We
247 /* can use this to enforce a "hard" ground. We still need to enforce some
248 /* bounds checking so that we don't try to lookup data points outside the
251 * Revision 1.9 1997/07/10 02:22:10 curt
252 * Working on terrain elevation interpolation routine.
254 * Revision 1.8 1997/07/09 21:31:15 curt
255 * Working on making the ground "hard."
257 * Revision 1.7 1997/07/08 18:20:13 curt
258 * Working on establishing a hard ground.
260 * Revision 1.6 1997/06/29 21:16:49 curt
261 * More twiddling with the Scenery Management system.
263 * Revision 1.5 1997/06/22 21:44:41 curt
264 * Working on intergrating the VRML (subset) parser.
266 * Revision 1.4 1997/05/30 19:30:17 curt
267 * The LaRCsim flight model is starting to look like it is working.
269 * Revision 1.3 1997/05/23 15:40:41 curt
270 * Added GNU copyright headers.
272 * Revision 1.2 1997/05/19 18:20:50 curt
273 * Slight change to origin key words.
275 * Revision 1.1 1997/05/16 16:07:04 curt