]> Pileus Git - grits/blob - src/objects/grits-poly.c
Improve performance of GritsPoly
[grits] / src / objects / grits-poly.c
1 /*
2  * Copyright (C) 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 /**
19  * SECTION:grits-poly
20  * @short_description: Single point polys
21  *
22  * Each #GritsPoly represents a 3 dimentional polygon.
23  */
24
25 #include <config.h>
26 #include <GL/gl.h>
27 #include <GL/glu.h>
28 #include "grits-poly.h"
29
30 /* Drawing */
31 static void grits_poly_tess(gdouble (**points)[3])
32 {
33         //g_debug("GritsPoly: tess");
34         GLUtesselator *tess = gluNewTess();
35         gluTessCallback(tess, GLU_TESS_BEGIN,  (_GLUfuncptr)glBegin);
36         gluTessCallback(tess, GLU_TESS_VERTEX, (_GLUfuncptr)glVertex3dv);
37         gluTessCallback(tess, GLU_TESS_END,    (_GLUfuncptr)glEnd);
38         for (int pi = 0; points[pi]; pi++) {
39                 gluTessBeginPolygon(tess, NULL);
40                 gluTessBeginContour(tess);
41                 for (int ci = 0; points[pi][ci][0]; ci++) {
42                         gluTessVertex(tess,
43                                 points[pi][ci],
44                                 points[pi][ci]);
45                 }
46                 gluTessEndContour(tess);
47                 gluTessEndPolygon(tess);
48         }
49         gluDeleteTess(tess);
50 }
51
52 static void grits_poly_outline(gdouble (**points)[3])
53 {
54         //g_debug("GritsPoly: outline");
55         for (int pi = 0; points[pi]; pi++) {
56                 glBegin(GL_POLYGON);
57                 for (int ci = 0; points[pi][ci][0] &&
58                                  points[pi][ci][1] &&
59                                  points[pi][ci][2]; ci++)
60                         glVertex3dv(points[pi][ci]);
61                 glEnd();
62         }
63 }
64
65 static gboolean grits_poly_runlist(GritsPoly *poly, int i,
66                 void (*render)(gdouble(**)[3]))
67 {
68         //g_debug("GritsPoly: genlist");
69         if (poly->list[i]) {
70                 glCallList(poly->list[i]);
71         } else {
72                 guint list = glGenLists(1);
73                 glNewList(list, GL_COMPILE_AND_EXECUTE);
74                 render(poly->points);
75                 glEndList();
76                 poly->list[i] = list;
77         }
78         return FALSE;
79 }
80
81 static void grits_poly_draw(GritsObject *_poly, GritsOpenGL *opengl)
82 {
83         //g_debug("GritsPoly: draw");
84         GritsPoly *poly = GRITS_POLY(_poly);
85
86         glPushAttrib(GL_COLOR_BUFFER_BIT | GL_ENABLE_BIT | GL_CURRENT_BIT);
87         glDisable(GL_TEXTURE_2D);
88         glDisable(GL_ALPHA_TEST);
89         glDisable(GL_CULL_FACE);
90         glDisable(GL_LIGHTING);
91         glEnable(GL_POLYGON_OFFSET_FILL);
92         glPolygonOffset(1, 1);
93         if (poly->color[3]) {
94                 glColor4dv(poly->color);
95                 grits_poly_runlist(poly, 0, grits_poly_tess);
96         }
97         glLineWidth(poly->width);
98         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
99         if (poly->border[3]) {
100                 glColor4dv(poly->border);
101                 grits_poly_runlist(poly, 1, grits_poly_outline);
102         }
103         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
104         glPopAttrib();
105 }
106
107 static void grits_poly_pick(GritsObject *_poly, GritsOpenGL *opengl)
108 {
109         //g_debug("GritsPoly: pick");
110         GritsPoly *poly = GRITS_POLY(_poly);
111         glPushAttrib(GL_ENABLE_BIT);
112         glDisable(GL_CULL_FACE);
113         grits_poly_runlist(poly, 0, grits_poly_tess);
114         glPopAttrib();
115 }
116
117 /**
118  * grits_poly_new:
119  * @points:  TODO
120  * @npoints: TODO
121  *
122  * Create a new GritsPoly which TODO.
123  *
124  * Returns: the new #GritsPoly.
125  */
126 GritsPoly *grits_poly_new(gdouble (**points)[3])
127 {
128         //g_debug("GritsPoly: new - %p", points);
129         GritsPoly *poly = g_object_new(GRITS_TYPE_POLY, NULL);
130         poly->points    = points;
131         return poly;
132 }
133
134 GritsPoly *grits_poly_parse(const gchar *str,
135                 const gchar *poly_sep, const gchar *point_sep, const gchar *coord_sep)
136 {
137         /* Split and count polygons */
138         gchar **spolys = g_strsplit(str, poly_sep, -1);
139         int     npolys = g_strv_length(spolys);
140
141         GritsBounds bounds = {-90, 90, -180, 180};
142         gdouble (**polys)[3] = (gpointer)g_new0(double*, npolys+1);
143         for (int pi = 0; pi < npolys; pi++) {
144                 /* Split and count coordinates */
145                 gchar **scoords = g_strsplit(spolys[pi], point_sep, -1);
146                 int     ncoords = g_strv_length(scoords);
147
148                 /* Create binary coords */
149                 gdouble (*coords)[3] = (gpointer)g_new0(gdouble, 3*(ncoords+1));
150                 for (int ci = 0; ci < ncoords; ci++) {
151                         gdouble lat, lon;
152                         sscanf(scoords[ci], "%lf,%lf", &lat, &lon);
153                         if (lat > bounds.n) bounds.n = lat;
154                         if (lat < bounds.s) bounds.s = lat;
155                         if (lon > bounds.e) bounds.e = lon;
156                         if (lon < bounds.w) bounds.w = lon;
157                         lle2xyz(lat, lon, 0,
158                                         &coords[ci][0],
159                                         &coords[ci][1],
160                                         &coords[ci][2]);
161                 }
162
163                 /* Insert coords into poly array */
164                 polys[pi] = coords;
165                 g_strfreev(scoords);
166         }
167
168         /* Create GritsPoly */
169         GritsPoly *poly = grits_poly_new(polys);
170         GRITS_OBJECT(poly)->center.lat  = (bounds.n + bounds.s)/2;
171         GRITS_OBJECT(poly)->center.lon  = lon_avg(bounds.e, bounds.w);
172         GRITS_OBJECT(poly)->center.elev = 0;
173         GRITS_OBJECT(poly)->skip        = GRITS_SKIP_CENTER;
174         return poly;
175 }
176
177 /* GObject code */
178 G_DEFINE_TYPE(GritsPoly, grits_poly, GRITS_TYPE_OBJECT);
179 static void grits_poly_init(GritsPoly *poly)
180 {
181         poly->border[0] = 1;
182         poly->border[1] = 1;
183         poly->border[2] = 1;
184         poly->border[3] = 0.2;
185         poly->width     = 1;
186         GRITS_OBJECT(poly)->skip = GRITS_SKIP_STATE;
187 }
188
189 static void grits_poly_finalize(GObject *_poly)
190 {
191         g_debug("GritsPoly: finalize");
192         GritsPoly *poly = GRITS_POLY(_poly);
193         (void)poly;
194         // TODO: free points
195 }
196
197 static void grits_poly_class_init(GritsPolyClass *klass)
198 {
199         g_debug("GritsPoly: class_init");
200         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
201         gobject_class->finalize = grits_poly_finalize;
202
203         GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
204         object_class->draw = grits_poly_draw;
205         object_class->pick = grits_poly_pick;
206 }