]> Pileus Git - grits/blob - src/roam.h
7c7371dff434d9833f10b981b60317e0685dc143
[grits] / src / roam.h
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #ifndef __ROAM_H__
19 #define __ROAM_H__
20
21 #include "gpqueue.h"
22
23 /* Roam */
24 typedef struct _RoamView     RoamView;
25 typedef struct _RoamPoint    RoamPoint;
26 typedef struct _RoamTriangle RoamTriangle;
27 typedef struct _RoamDiamond  RoamDiamond;
28 typedef struct _RoamSphere   RoamSphere;
29 /**
30  * RoamHeightFunc:
31  * @lat:       the latitude
32  * @lon:       the longitude
33  * @user_data: user data passed to the function
34  *
35  * See #GisHeightFunc
36  *
37  * Returns: the elevation
38  */
39 typedef gdouble (*RoamHeightFunc)(gdouble lat, gdouble lon, gpointer user_data);
40
41 /* Misc */
42 /**
43  * RoamView:
44  * @model:   model view matrix
45  * @proj:    projection matrix
46  * @view:    viewport matrix
47  * @version: version
48  *
49  * Stores projection matrices
50  */
51 struct _RoamView {
52         gdouble model[16];
53         gdouble proj[16];
54         gint view[4];
55         gint version;
56         gdouble pos[3];
57 };
58
59 /*************
60  * RoamPoint *
61  *************/
62 /**
63  * RoamPoint:
64  *
65  * Points are used as vertices for triangles. A single point my be shared among
66  * several triangles in order to conceive space and avoid recalculating
67  * projections. Points also store a lot of cached data. The normal vertex normal
68  * is the averaged surface normal of each associated triangle.
69  */
70 struct _RoamPoint {
71         /*< private >*/
72         gdouble  x, y, z;    /* Model coordinates */
73         gdouble  px, py, pz; /* Projected coordinates */
74         gint     pversion;   /* Version of cached projection */
75
76         gint     tris;       /* Count of associated triangles */
77         gdouble  norm[3];    /* Vertex normal */
78
79         /* For get_intersect */
80         gdouble  lat, lon, elev;
81
82         /* For terrain */
83         RoamHeightFunc height_func;
84         gpointer       height_data;
85 };
86 RoamPoint *roam_point_new(double lat, double lon, double elev);
87 void roam_point_add_triangle(RoamPoint *point, RoamTriangle *triangle);
88 void roam_point_remove_triangle(RoamPoint *point, RoamTriangle *triangle);
89 void roam_point_update_height(RoamPoint *point);
90 void roam_point_update_projection(RoamPoint *point, RoamView *view);
91
92 /****************
93  * RoamTriangle *
94  ****************/
95 /**
96  * RoamTriangle:
97  *
98  * Triangles are one of the key datatypes in ROAM. The surface is made up of
99  * triangles. Each triangle has an associated "error". When the surface is being
100  * updated after the view changes, each triangles error is updated. Afterwards
101  * the triangles with the most error are split int to triangles, each with a
102  * lower error than the original.
103  *
104  * Triangles store a lot of data about their location in the mesh so that they
105  * can be split and merged (unsplit) without having to recreate the mesh.
106  */
107 struct _RoamTriangle {
108         /*< private >*/
109         /* Left, middle and right vertices */
110         struct { RoamPoint    *l,*m,*r; } p;
111
112         /* Left, base, and right neighbor triangles */
113         struct { RoamTriangle *l,*b,*r; } t;
114
115         RoamPoint *split;      /* Split point */
116         RoamDiamond *parent;   /* Parent diamond */
117         RoamTriangle *kids[2]; /* Higher-res triangles */
118         double norm[3];        /* Surface normal */
119         double error;          /* Screen space error */
120         GPQueueHandle handle;
121
122         /* For get_intersect */
123         struct { gdouble n,s,e,w; } edge;
124 };
125 RoamTriangle *roam_triangle_new(RoamPoint *l, RoamPoint *m, RoamPoint *r,
126                 RoamDiamond *parent);
127 void roam_triangle_free(RoamTriangle *triangle);
128 void roam_triangle_add(RoamTriangle *triangle,
129                 RoamTriangle *left, RoamTriangle *base, RoamTriangle *right,
130                 RoamSphere *sphere);
131 void roam_triangle_remove(RoamTriangle *triangle, RoamSphere *sphere);
132 void roam_triangle_update_errors(RoamTriangle *triangle, RoamSphere *sphere);
133 void roam_triangle_split(RoamTriangle *triangle, RoamSphere *sphere);
134 void roam_triangle_draw(RoamTriangle *triangle);
135 void roam_triangle_draw_normal(RoamTriangle *triangle);
136
137 /***************
138  * RoamDiamond *
139  ***************/
140 /**
141  * RoamDiamond:
142  *
143  * When two adjacent triangles are split, they, along with the four new child
144  * triangles, are added to a diamond which keeps track of them.
145  *
146  * Like triangles, diamond have an error associated with it. However, when a
147  * diamonds error is small enough it is "merged". That is, the diamond along
148  * with the child triangles is removed and the original two triangles triangles
149  * are added back into the mesh.
150  */
151 struct _RoamDiamond {
152         /*< private >*/
153         RoamTriangle *parents[2]; /* Parent triangles */
154         double error;             /* Screen space error */
155         gboolean active;          /* For internal use */
156         GPQueueHandle handle;
157 };
158 RoamDiamond *roam_diamond_new(RoamTriangle *parent0, RoamTriangle *parent1);
159 void roam_diamond_add(RoamDiamond *diamond, RoamSphere *sphere);
160 void roam_diamond_remove(RoamDiamond *diamond, RoamSphere *sphere);
161 void roam_diamond_merge(RoamDiamond *diamond, RoamSphere *sphere);
162 void roam_diamond_update_errors(RoamDiamond *diamond, RoamSphere *sphere);
163
164 /**************
165  * RoamSphere *
166  **************/
167 /**
168  * RoamSphere:
169  *
170  * The sphere keeps track of the triangles and diamonds in the mesh. 
171  *
172  * Originally the sphere consists of only 8 triangles forming a octahedron.
173  * These triangles are quickly split to create a smoother sphere.
174  */
175 struct _RoamSphere {
176         /*< private >*/
177         GPQueue *triangles; /* List of triangles */
178         GPQueue *diamonds;  /* List of diamonds */
179         RoamView *view;     /* Current projection */
180         gint polys;         /* Polygon count */
181
182         /* For get_intersect */
183         RoamTriangle *roots[8]; /* Original 8 triangles */
184 };
185 RoamSphere *roam_sphere_new();
186 void roam_sphere_update_view(RoamSphere *sphere);
187 void roam_sphere_update_errors(RoamSphere *sphere);
188 void roam_sphere_split_one(RoamSphere *sphere);
189 void roam_sphere_merge_one(RoamSphere *sphere);
190 gint roam_sphere_split_merge(RoamSphere *sphere);
191 void roam_sphere_draw(RoamSphere *sphere);
192 void roam_sphere_draw_normals(RoamSphere *sphere);
193 GList *roam_sphere_get_intersect(RoamSphere *sphere, gboolean all,
194                 gdouble n, gdouble s, gdouble e, gdouble w);
195 void roam_sphere_free(RoamSphere *sphere);
196
197 #endif