]> Pileus Git - grits/blob - src/objects/grits-volume.c
be08279be35c075d66be6f282d7548af7ed55a8d
[grits] / src / objects / grits-volume.c
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 /**
19  * SECTION:grits-volume
20  * @short_description: 3-D gridded vovlume
21  *
22  * Each #GritsVolume consistes of a 3-dimentional grid of data points each
23  * consisting of a value.
24  *
25  * Currently iso-surfaces are extracted and displayed when rendering the volume
26  * data.
27  */
28
29 #include <config.h>
30 #include <math.h>
31 #include <glib.h>
32 #include <GL/gl.h>
33 #include <gdk/gdkgl.h>
34 #include "grits-volume.h"
35
36 /* Drawing */
37 static void draw_points(VolGrid *grid, gdouble level)
38 {
39         glPointSize(200./grid->xs);
40         glBegin(GL_POINTS);
41         for (int x = 0; x < grid->xs; x++)
42         for (int y = 0; y < grid->ys; y++)
43         for (int z = 0; z < grid->zs; z++) {
44                 VolPoint *pt = vol_grid_get(grid, x, y, z);
45                 if (pt->value < level)
46                         continue;
47
48                 g_debug("(%d,%d,%d) value=%f", x, y, z, pt->value);
49
50                 glColor4f(1.0, 1.0, 1.0, pt->value/100);
51                 glVertex3dv((double*)&pt->c);
52         }
53         glEnd();
54 }
55
56 static void draw_iso(GList *tris)
57 {
58         g_debug("GritsVolume: draw_iso");
59         glDisable(GL_CULL_FACE);
60         glBegin(GL_TRIANGLES);
61         for (GList *cur = tris; cur; cur = cur->next) {
62                 VolTriangle *tri = cur->data;
63                 VolCoord c[3] = {tri->v[0]->c,    tri->v[1]->c,    tri->v[2]->c   };
64                 VolCoord n[3] = {tri->v[0]->norm, tri->v[1]->norm, tri->v[2]->norm};
65
66                 /* Normalize normal vector */
67                 for (int i = 0; i < 3; i++) {
68                         double total = sqrt(
69                                 n[i].x * n[i].x +
70                                 n[i].y * n[i].y +
71                                 n[i].z * n[i].z);
72                         if (total == 0)
73                                 continue; // wtf
74                         n[i].x = n[i].x / total;
75                         n[i].y = n[i].y / total;
76                         n[i].z = n[i].z / total;
77                 }
78
79                 //for (int i = 0; i < 3; i++)
80                 //      g_debug("norm=%f,%f,%f",
81                 //              n[i].x, n[i].y, n[i].z);
82                 //glNormal3dv((double*)&tri->norm);
83                 glNormal3dv((double*)&n[0]); glVertex3dv((double*)&c[0]);
84                 glNormal3dv((double*)&n[1]); glVertex3dv((double*)&c[1]);
85                 glNormal3dv((double*)&n[2]); glVertex3dv((double*)&c[2]);
86         }
87         glEnd();
88 }
89
90 static void draw(GritsObject *_volume, GritsOpenGL *opengl)
91 {
92         g_debug("GritsVolume: draw");
93         GritsVolume *volume = GRITS_VOLUME(_volume);
94
95         gfloat amb[4] = {};
96         gfloat dif[4] = {};
97         switch (volume->disp) {
98         case GRITS_VOLUME_SURFACE:
99                 glDisable(GL_COLOR_MATERIAL);
100                 amb[0] = (double)volume->color[0] / 0xff;
101                 amb[1] = (double)volume->color[1] / 0xff;
102                 amb[2] = (double)volume->color[2] / 0xff;
103                 amb[3] = 1;
104                 dif[0] = (double)volume->color[0] / 0xff;
105                 dif[1] = (double)volume->color[1] / 0xff;
106                 dif[2] = (double)volume->color[2] / 0xff;
107                 dif[3] = 1;
108                 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, amb);
109                 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, dif);
110                 draw_iso(volume->tris);
111                 break;
112         case GRITS_VOLUME_POINTS:
113                 draw_points(volume->grid, volume->level);
114                 break;
115         }
116 }
117
118 /* Idle update */
119 gboolean update_iso(gpointer _volume)
120 {
121         GritsVolume *volume = _volume;
122         if (volume->tris) {
123                 g_list_foreach(volume->tris, (GFunc)vol_triangle_free, NULL);
124                 g_list_free(volume->tris);
125         }
126         volume->tris = marching_cubes(volume->grid, volume->level);
127         volume->update_id = 0;
128         grits_object_queue_draw(GRITS_OBJECT(volume));
129         return FALSE;
130 }
131
132 /***********
133  * Methods *
134  ***********/
135 void grits_volume_set_level(GritsVolume *volume, gdouble level)
136 {
137         volume->level = level;
138         if (!volume->update_id)
139                 volume->update_id = g_idle_add(update_iso, volume);
140 }
141
142 GritsVolume *grits_volume_new(VolGrid *grid)
143 {
144         g_debug("GritsVolume: new - %p[%d][%d][%d]",
145                         grid, grid->xs, grid->ys, grid->zs);
146         GritsVolume *volume = g_object_new(GRITS_TYPE_VOLUME, NULL);
147         volume->grid = grid;
148         return volume;
149 }
150
151 /* GObject code */
152 G_DEFINE_TYPE(GritsVolume, grits_volume, GRITS_TYPE_OBJECT);
153 static void grits_volume_init(GritsVolume *volume)
154 {
155 }
156
157 static void grits_volume_finalize(GObject *_volume)
158 {
159         GritsVolume *volume = GRITS_VOLUME(_volume);
160         volume->color[0] = 1;
161         volume->color[1] = 1;
162         volume->color[2] = 1;
163         volume->color[3] = 1;
164         //g_debug("GritsVolume: finalize - %s", volume->label);
165 }
166
167 static void grits_volume_class_init(GritsVolumeClass *klass)
168 {
169         g_debug("GritsVolume: class_init");
170         GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
171         gobject_class->finalize = grits_volume_finalize;
172
173         GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
174         object_class->draw = draw;
175 }