File: render.c

package info (click to toggle)
gdis 0.89-2.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 7,888 kB
  • ctags: 4,879
  • sloc: ansic: 64,430; makefile: 281; python: 115; perl: 114; sh: 107
file content (276 lines) | stat: -rw-r--r-- 6,973 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Copyright (C) 2003 by Sean David Fleming

sean@ivec.org

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

The GNU GPL can also be found at http://www.gnu.org
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#include "gdis.h"
#include "coords.h"
#include "matrix.h"
#include "opengl.h"
#include "render.h"
#include "interface.h"

/* externals */
extern struct sysenv_pak sysenv;
extern GtkWidget *window;
extern GdkPixmap *pixmap;

/************************************************************/
/* process data structures to get current model state label */
/************************************************************/
gchar *get_mode_label(struct model_pak *data)
{
gchar *label;

switch(data->mode)
  {
  case FREE:
    label = g_strdup("normal");
    break;
  case ATOM_ADD:
    label = g_strdup("add atoms");
    break;
  case BOND_DELETE:
    label = g_strdup("delete bonds");
    break;
  case BOND_SINGLE:
    label = g_strdup_printf("add single bonds (%1d/2)", data->state);
    break;
  case BOND_DOUBLE:
    label = g_strdup_printf("add double bonds (%1d/2)", data->state);
    break;
  case BOND_TRIPLE:
    label = g_strdup_printf("add triple bonds (%1d/2)", data->state);
    break;
  case BOND_INFO:
    label = g_strdup("bonds");
    break;
  case DIST_INFO:
    label = g_strdup_printf("distance selection (%1d/2)", data->state);
    break;
  case ANGLE_INFO:
    label = g_strdup_printf("angle selection (%1d/3)", data->state);
    break;
  case DIHEDRAL_INFO:
    label = g_strdup_printf("dihedral selection (%1d/4)", data->state);
    break;
  case DEFINE_RIBBON:
    label = g_strdup("define ribbon");
    break;
  case DEFINE_VECTOR:
    label = g_strdup_printf("define vector (%1d/2)", data->state);
    break;
  case DEFINE_PLANE:
    label = g_strdup_printf("define plane (%1d/3)", data->state);
    break;
  case ANIMATING:
    label = g_strdup_printf("Frame: %-4d", data->cur_frame);
    break;
  case RECORD:
    label = g_strdup("recording");
    break;
  case SELECT_FRAGMENT:
    label = g_strdup_printf("select bonded atoms (%1d/2)", data->state);
    break;

  default:
    label = g_strdup("undefined");
    break;
  }

return(label);
}

/*********************************************/
/* construct all pipe lists for bond drawing */
/*********************************************/
#define PIPE_DEPTH 4
void render_make_pipes(GSList **pipes, struct model_pak *model)
{
gint i;
gdouble radius, mp1[4], mp2[4], x[3];
gdouble colour1[4], colour2[4];
GSList *list;
struct pipe_pak *pipe;
struct bond_pak *bond;
struct core_pak *core1, *core2;

/* checks */
g_assert(model != NULL);

/*
printf("-------------------------------------------------------------\n");
dump_bonds(model);
printf("-------------------------------------------------------------\n");
*/

/* init the return list(s) */
for (i=PIPE_DEPTH ; i-- ; )
  pipes[i] = NULL;

/* common pipe width */
radius = sysenv.render.stick_rad;

/* enumerate bonds to construct the pipe list */
for (list=model->bonds; list ; list=g_slist_next(list))
  {
  bond = list->data;

  if (bond->status == DELETED)
    continue;
  if (bond->status == HIDDEN)
    continue;

/* the two atoms */
  core1 = bond->atom1;
  core2 = bond->atom2;

/* NEW - bailout modes */
  if (core1->render_mode == ZONE)
    continue;
  if (core2->render_mode == ZONE)
    continue;

  if (core1->status & OFF_SCREEN)
    if (core2->status & OFF_SCREEN)
      continue;

/* NEW - cope with phonon animation moving the cores */
  ARR3SET(x, core1->offset);
  ARR3ADD(x, core2->offset);
  VEC3MUL(x, 0.5);

/* compute midpoint 1 coords */
  ARR3SET(mp1, core1->x);
  ARR3ADD(mp1, bond->offset);
  mp1[3] = 1.0;
  ARR3ADD(mp1, x);
  vec4mat(model->display_lattice, mp1);

/* compute midpoint 2 coords */
/* midpoints may be different (split by pbc) */
  ARR3SET(mp2, core2->x);
  ARR3SUB(mp2, bond->offset);
  mp2[3] = 1.0;
  ARR3ADD(mp2, x);
  vec4mat(model->display_lattice, mp2);

/* deleted/ zeol hidden */
  if (core1->status & (DELETED | ZEOL_HIDDEN))
    continue;
  if (core2->status & (DELETED | ZEOL_HIDDEN))
    continue;

/* colour setup */
  switch (bond->type)
    {
    case BOND_HBOND:
      VEC4SET(colour1, 1.0, 1.0, 0.6, 0.0);
      VEC4SET(colour2, 1.0, 1.0, 0.6, 0.0);
      break;

    default:
      ARR4SET(colour1, core1->colour);
      VEC3MUL(colour1, 1.0/65535.0);
      ARR4SET(colour2, core2->colour);
      VEC3MUL(colour2, 1.0/65535.0);
    }

/* setup half-bond (pipe) for core1 */
  if (!(core1->status & HIDDEN) && core1->render_mode != CPK)
    {
/* init pipe */
    pipe = g_malloc(sizeof(struct pipe_pak));
    ARR3SET(pipe->v1, core1->rx);
    ARR3SET(pipe->v2, mp1);
    pipe->radius = radius;
    ARR4SET(pipe->colour, colour1);

/* assign to appropriate pipe list */
    if (core1->render_mode == STICK)
      pipes[3] = g_slist_prepend(pipes[3], pipe);
    else
      {
      if (core1->ghost)
        pipes[1] = g_slist_prepend(pipes[1], pipe);
      else
        {
        if (core1->render_wire)
          pipes[2] = g_slist_prepend(pipes[2], pipe);
        else
          pipes[0] = g_slist_prepend(pipes[0], pipe);
        }
      }
    }

/* setup half-bond (pipe) for core1 */
  if (!(core2->status & HIDDEN) && core2->render_mode != CPK)
    {
/* init pipe */
    pipe = g_malloc(sizeof(struct pipe_pak));
    ARR3SET(pipe->v1, core2->rx);
    ARR3SET(pipe->v2, mp2);
    pipe->radius = radius;
    ARR4SET(pipe->colour, colour2);

/* assign to appropriate pipe list */
    if (core2->render_mode == STICK)
      pipes[3] = g_slist_prepend(pipes[3], pipe);
    else
      {
      if (core2->ghost)
        pipes[1] = g_slist_prepend(pipes[1], pipe);
      else
        {
        if (core2->render_wire)
          pipes[2] = g_slist_prepend(pipes[2], pipe);
        else
          pipes[0] = g_slist_prepend(pipes[0], pipe);
        }
      }
    }
  }
}

/*****************************/
/* pipe z-ordering primitive */ 
/*****************************/
gint render_pipe_depth_sort(struct pipe_pak *p1, struct pipe_pak *p2)
{
gdouble z1, z2;

/* use pipe z-midpoints for comparison */
z1 = 0.5 * (p1->v1[2] + p1->v2[2]);
z2 = 0.5 * (p2->v1[2] + p2->v2[2]);

if (z1 > z2)
  return(-1);
return(1);
}

GSList *render_sort_pipes(GSList *pipes)
{
return(g_slist_sort(pipes, (gpointer) render_pipe_depth_sort));
}