]> Pileus Git - grits/blob - src/roam.h
Revert "Test pure distance based errors"
[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         RoamTriangle *kids[2]; /* Higher-res triangles */
117         double norm[3];        /* Surface normal */
118         double error;          /* Screen space error */
119         GPQueueHandle handle;
120
121         /* For get_intersect */
122         struct { gdouble n,s,e,w; } edge;
123 };
124 RoamTriangle *roam_triangle_new(RoamPoint *l, RoamPoint *m, RoamPoint *r,
125                 RoamDiamond *parent);
126 void roam_triangle_free(RoamTriangle *triangle);
127 void roam_triangle_add(RoamTriangle *triangle,
128                 RoamTriangle *left, RoamTriangle *base, RoamTriangle *right,
129                 RoamSphere *sphere);
130 void roam_triangle_remove(RoamTriangle *triangle, RoamSphere *sphere);
131 void roam_triangle_update_errors(RoamTriangle *triangle, RoamSphere *sphere);
132 void roam_triangle_split(RoamTriangle *triangle, RoamSphere *sphere);
133 void roam_triangle_draw(RoamTriangle *triangle);
134 void roam_triangle_draw_normal(RoamTriangle *triangle);
135
136 /***************
137  * RoamDiamond *
138  ***************/
139 /**
140  * RoamDiamond:
141  *
142  * When two adjacent triangles are split, they, along with the four new child
143  * triangles, are added to a diamond which keeps track of them.
144  *
145  * Like triangles, diamond have an error associated with it. However, when a
146  * diamonds error is small enough it is "merged". That is, the diamond along
147  * with the child triangles is removed and the original two triangles triangles
148  * are added back into the mesh.
149  */
150 struct _RoamDiamond {
151         /*< private >*/
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(RoamTriangle *parent0, RoamTriangle *parent1);
158 void roam_diamond_add(RoamDiamond *diamond, RoamSphere *sphere);
159 void roam_diamond_remove(RoamDiamond *diamond, RoamSphere *sphere);
160 void roam_diamond_merge(RoamDiamond *diamond, RoamSphere *sphere);
161 void roam_diamond_update_errors(RoamDiamond *diamond, RoamSphere *sphere);
162
163 /**************
164  * RoamSphere *
165  **************/
166 /**
167  * RoamSphere:
168  *
169  * The sphere keeps track of the triangles and diamonds in the mesh. 
170  *
171  * Originally the sphere consists of only 8 triangles forming a octahedron.
172  * These triangles are quickly split to create a smoother sphere.
173  */
174 struct _RoamSphere {
175         /*< private >*/
176         GPQueue *triangles; /* List of triangles */
177         GPQueue *diamonds;  /* List of diamonds */
178         RoamView *view;     /* Current projection */
179         gint polys;         /* Polygon count */
180
181         /* For get_intersect */
182         RoamTriangle *roots[8]; /* Original 8 triangles */
183 };
184 RoamSphere *roam_sphere_new();
185 void roam_sphere_update_view(RoamSphere *sphere);
186 void roam_sphere_update_errors(RoamSphere *sphere);
187 void roam_sphere_split_one(RoamSphere *sphere);
188 void roam_sphere_merge_one(RoamSphere *sphere);
189 gint roam_sphere_split_merge(RoamSphere *sphere);
190 void roam_sphere_draw(RoamSphere *sphere);
191 void roam_sphere_draw_normals(RoamSphere *sphere);
192 GList *roam_sphere_get_intersect(RoamSphere *sphere, gboolean all,
193                 gdouble n, gdouble s, gdouble e, gdouble w);
194 void roam_sphere_free(RoamSphere *sphere);
195
196 #endif