2 * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
19 * SECTION:grits-volume
20 * @short_description: 3-D gridded vovlume
22 * Each #GritsVolume consistes of a 3-dimentional grid of data points each
23 * consisting of a value.
25 * Currently iso-surfaces are extracted and displayed when rendering the volume
33 #include "grits-volume.h"
36 static void draw_points(VolGrid *grid, gdouble level)
38 glPointSize(200./grid->xs);
40 for (int x = 0; x < grid->xs; x++)
41 for (int y = 0; y < grid->ys; y++)
42 for (int z = 0; z < grid->zs; z++) {
43 VolPoint *pt = vol_grid_get(grid, x, y, z);
44 if (pt->value < level)
47 g_debug("(%d,%d,%d) value=%f", x, y, z, pt->value);
49 glColor4f(1.0, 1.0, 1.0, pt->value/100);
50 glVertex3dv((double*)&pt->c);
55 static void draw_iso(GList *tris)
57 g_debug("GritsVolume: draw_iso");
58 glDisable(GL_CULL_FACE);
59 glBegin(GL_TRIANGLES);
60 for (GList *cur = tris; cur; cur = cur->next) {
61 VolTriangle *tri = cur->data;
62 VolCoord c[3] = {tri->v[0]->c, tri->v[1]->c, tri->v[2]->c };
63 VolCoord n[3] = {tri->v[0]->norm, tri->v[1]->norm, tri->v[2]->norm};
65 /* Normalize normal vector */
66 for (int i = 0; i < 3; i++) {
73 n[i].x = n[i].x / total;
74 n[i].y = n[i].y / total;
75 n[i].z = n[i].z / total;
78 //for (int i = 0; i < 3; i++)
79 // g_debug("norm=%f,%f,%f",
80 // n[i].x, n[i].y, n[i].z);
81 //glNormal3dv((double*)&tri->norm);
82 glNormal3dv((double*)&n[0]); glVertex3dv((double*)&c[0]);
83 glNormal3dv((double*)&n[1]); glVertex3dv((double*)&c[1]);
84 glNormal3dv((double*)&n[2]); glVertex3dv((double*)&c[2]);
89 static void draw(GritsObject *_volume, GritsOpenGL *opengl)
91 g_debug("GritsVolume: draw");
92 GritsVolume *volume = GRITS_VOLUME(_volume);
96 switch (volume->disp) {
97 case GRITS_VOLUME_SURFACE:
98 glDisable(GL_COLOR_MATERIAL);
99 amb[0] = (double)volume->color[0] / 0xff;
100 amb[1] = (double)volume->color[1] / 0xff;
101 amb[2] = (double)volume->color[2] / 0xff;
103 dif[0] = (double)volume->color[0] / 0xff;
104 dif[1] = (double)volume->color[1] / 0xff;
105 dif[2] = (double)volume->color[2] / 0xff;
107 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, amb);
108 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, dif);
109 draw_iso(volume->tris);
111 case GRITS_VOLUME_POINTS:
112 draw_points(volume->grid, volume->level);
118 gboolean update_iso(gpointer _volume)
120 GritsVolume *volume = _volume;
122 g_list_foreach(volume->tris, (GFunc)vol_triangle_free, NULL);
123 g_list_free(volume->tris);
125 volume->tris = marching_cubes(volume->grid, volume->level);
126 volume->update_id = 0;
127 grits_object_queue_draw(GRITS_OBJECT(volume));
134 void grits_volume_set_level(GritsVolume *volume, gdouble level)
136 volume->level = level;
137 if (!volume->update_id)
138 volume->update_id = g_idle_add(update_iso, volume);
141 GritsVolume *grits_volume_new(VolGrid *grid)
143 g_debug("GritsVolume: new - %p[%d][%d][%d]",
144 grid, grid->xs, grid->ys, grid->zs);
145 GritsVolume *volume = g_object_new(GRITS_TYPE_VOLUME, NULL);
151 G_DEFINE_TYPE(GritsVolume, grits_volume, GRITS_TYPE_OBJECT);
152 static void grits_volume_init(GritsVolume *volume)
156 static void grits_volume_finalize(GObject *_volume)
158 GritsVolume *volume = GRITS_VOLUME(_volume);
159 volume->color[0] = 1;
160 volume->color[1] = 1;
161 volume->color[2] = 1;
162 volume->color[3] = 1;
163 //g_debug("GritsVolume: finalize - %s", volume->label);
166 static void grits_volume_class_init(GritsVolumeClass *klass)
168 g_debug("GritsVolume: class_init");
169 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
170 gobject_class->finalize = grits_volume_finalize;
172 GritsObjectClass *object_class = GRITS_OBJECT_CLASS(klass);
173 object_class->draw = draw;