]> Pileus Git - grits/blob - src/roam.h
11eb171d444833212dd288483a35d4adacb2d6a9
[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 };
57
58 /*************
59  * RoamPoint *
60  *************/
61 /**
62  * RoamPoint:
63  *
64  * Points are used as vertices for triangles. A single point my be shared among
65  * several triangles in order to conceive space and avoid recalculating
66  * projections. Points also store a lot of cached data. The normal vertex normal
67  * is the averaged surface normal of each associated triangle.
68  */
69 struct _RoamPoint {
70         /*< private >*/
71         gdouble  x, y, z;    /* Model coordinates */
72         gdouble  px, py, pz; /* Projected coordinates */
73         gint     pversion;   /* Version of cached projection */
74
75         gint     tris;       /* Count of associated triangles */
76         gdouble  norm[3];    /* Vertex normal */
77
78         /* For get_intersect */
79         gdouble  lat, lon, elev;
80
81         /* For terrain */
82         RoamHeightFunc height_func;
83         gpointer       height_data;
84 };
85 RoamPoint *roam_point_new(double lat, double lon, double elev);
86 void roam_point_add_triangle(RoamPoint *point, RoamTriangle *triangle);
87 void roam_point_remove_triangle(RoamPoint *point, RoamTriangle *triangle);
88 void roam_point_update_height(RoamPoint *point);
89 void roam_point_update_projection(RoamPoint *point, RoamView *view);
90
91 /****************
92  * RoamTriangle *
93  ****************/
94 /**
95  * RoamTriangle:
96  *
97  * Triangles are one of the key datatypes in ROAM. The surface is made up of
98  * triangles. Each triangle has an associated "error". When the surface is being
99  * updated after the view changes, each triangles error is updated. Afterwards
100  * the triangles with the most error are split int to triangles, each with a
101  * lower error than the original.
102  *
103  * Triangles store a lot of data about their location in the mesh so that they
104  * can be split and merged (unsplit) without having to recreate the mesh.
105  */
106 struct _RoamTriangle {
107         /*< private >*/
108         /* Left, middle and right vertices */
109         struct { RoamPoint    *l,*m,*r; } p;
110
111         /* Left, base, and right neighbor triangles */
112         struct { RoamTriangle *l,*b,*r; } t;
113
114         RoamPoint *split;     /* Split point */
115         RoamDiamond *parent;  /* Parent diamond */
116         double norm[3];       /* Surface normal */
117         double error;         /* Screen space error */
118         GPQueueHandle handle;
119
120         /* For get_intersect */
121         struct { gdouble n,s,e,w; } edge;
122         RoamTriangle *kids[2];
123 };
124 RoamTriangle *roam_triangle_new(RoamPoint *l, RoamPoint *m, RoamPoint *r);
125 void roam_triangle_free(RoamTriangle *triangle);
126 void roam_triangle_add(RoamTriangle *triangle,
127                 RoamTriangle *left, RoamTriangle *base, RoamTriangle *right,
128                 RoamSphere *sphere);
129 void roam_triangle_remove(RoamTriangle *triangle, RoamSphere *sphere);
130 void roam_triangle_update_errors(RoamTriangle *triangle, RoamSphere *sphere);
131 void roam_triangle_split(RoamTriangle *triangle, RoamSphere *sphere);
132 void roam_triangle_draw(RoamTriangle *triangle);
133 void roam_triangle_draw_normal(RoamTriangle *triangle);
134
135 /***************
136  * RoamDiamond *
137  ***************/
138 /**
139  * RoamDiamond:
140  *
141  * When two adjacent triangles are split, they, along with the four new child
142  * triangles, are added to a diamond which keeps track of them.
143  *
144  * Like triangles, diamond have an error associated with it. However, when a
145  * diamonds error is small enough it is "merged". That is, the diamond along
146  * with the child triangles is removed and the original two triangles triangles
147  * are added back into the mesh.
148  */
149 struct _RoamDiamond {
150         /*< private >*/
151         RoamTriangle *kids[4];    /* Child triangles */
152         RoamTriangle *parents[2]; /* Parent triangles */
153         double error;             /* Screen space error */
154         gboolean active;          /* For internal use */
155         GPQueueHandle handle;
156 };
157 RoamDiamond *roam_diamond_new(
158                 RoamTriangle *parent0, RoamTriangle *parent1,
159                 RoamTriangle *kid0, RoamTriangle *kid1,
160                 RoamTriangle *kid2, RoamTriangle *kid3);
161 void roam_diamond_add(RoamDiamond *diamond, RoamSphere *sphere);
162 void roam_diamond_remove(RoamDiamond *diamond, RoamSphere *sphere);
163 void roam_diamond_merge(RoamDiamond *diamond, RoamSphere *sphere);
164 void roam_diamond_update_errors(RoamDiamond *diamond, RoamSphere *sphere);
165
166 /**************
167  * RoamSphere *
168  **************/
169 /**
170  * RoamSphere:
171  *
172  * The sphere keeps track of the triangles and diamonds in the mesh. 
173  *
174  * Originally the sphere consists of only 8 triangles forming a octahedron.
175  * These triangles are quickly split to create a smoother sphere.
176  */
177 struct _RoamSphere {
178         /*< private >*/
179         GPQueue *triangles; /* List of triangles */
180         GPQueue *diamonds;  /* List of diamonds */
181         RoamView *view;     /* Current projection */
182         gint polys;         /* Polygon count */
183
184         /* For get_intersect */
185         RoamTriangle *roots[8]; /* Original 8 triangles */
186 };
187 RoamSphere *roam_sphere_new();
188 void roam_sphere_update_view(RoamSphere *sphere);
189 void roam_sphere_update_errors(RoamSphere *sphere);
190 void roam_sphere_split_one(RoamSphere *sphere);
191 void roam_sphere_merge_one(RoamSphere *sphere);
192 gint roam_sphere_split_merge(RoamSphere *sphere);
193 void roam_sphere_draw(RoamSphere *sphere);
194 void roam_sphere_draw_normals(RoamSphere *sphere);
195 GList *roam_sphere_get_intersect(RoamSphere *sphere, gboolean all,
196                 gdouble n, gdouble s, gdouble e, gdouble w);
197 void roam_sphere_free(RoamSphere *sphere);
198
199 #endif