File: draw_membranes.c

package info (click to toggle)
garlic 1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 4,440 kB
  • ctags: 1,403
  • sloc: ansic: 52,465; makefile: 1,133
file content (458 lines) | stat: -rw-r--r-- 14,098 bytes parent folder | download | duplicates (5)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* Copyright (C) 2001 Damir Zucic */

/*=============================================================================

				draw_membranes.c

Purpose:
	For each macromolecular complex,  draw the membrane,  if visible.

Input:
	(1) Pointer to MolComplexS structure.
	(2) Number of macromolecular complexes.
	(3) Pointer to ConfigS structure, with configuration data.
	(4) Pointer to GUIS structure.
	(5) Pointer to NearestAtomS structure, with information about the
	    atom occupying the given pixel.
	(6) The number of pixels in the main window free area.
	(7) The refreshI, used to check the  NearestAtomS associated with
	    a given pixel.

Output:
	(1) Membrane drawn for each  macromolecular complex,  if visible.
	(2) Return value.

Return value:
	(1) Positive always (trivial).

Notes:
	(1) Indentation is exceptionally 4 spaces.

	(2) First draw the first plane, then the second plane.

=============================================================================*/

#include <stdio.h>

#include <math.h>

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <X11/Xatom.h>

#include "defines.h"
#include "typedefs.h"

/*======function prototypes:=================================================*/

void		PlaneExtent_ (int *, int *, int *, int *,
			      PlaneS *, ConfigS *, int);
unsigned long	WeightColors_ (unsigned long, unsigned long, double, GUIS *);

/*======draw membranes:======================================================*/

int DrawMembranes_ (MolComplexS *mol_complexSP, int mol_complexesN,
		    ConfigS *configSP, GUIS *guiSP,
		    NearestAtomS *nearest_atomSP, size_t pixelsN,
		    unsigned int refreshI)
{
int		imagesN, imageI;
int		mol_complexI;
MolComplexS	*curr_mol_complexSP;
size_t		atomsN;
PlaneS		*plane1SP, *plane2SP;
int		center_screen_x, center_screen_y;
double		angle, sin_angle, cos_angle;
double		reciprocal_a, reciprocal_b;
double		reciprocal_diameter;
double		coeff;
int		screen_x_min, screen_x_max, screen_y_min, screen_y_max;
int		screen_x, screen_y;
double		x_relative, y_relative, x_rotated, y_rotated;
double		ratio_x, ratio_y, d;
double		plane_delta_z, plane_z;
size_t		pixelI;
NearestAtomS	*curr_pixelSP;
double		scale_factor;
unsigned long	plane_colorID, old_colorID, colorID;

/* Number of images: */
if (configSP->stereoF) imagesN = 2;
else imagesN = 1;

/* Draw the membrane (two planes), if it is defined */
/* and visible,  for each  macromolecular  complex: */
for (mol_complexI = 0; mol_complexI < mol_complexesN; mol_complexI++)
    {
    /* Pointer to current macromolecular complex: */
    curr_mol_complexSP = mol_complexSP + mol_complexI;

    /* Check is the membrane defined: */
    if (curr_mol_complexSP->membraneS.definedF == 0) continue;

    /* Check is the membrane visible: */
    if (curr_mol_complexSP->membraneS.hiddenF) continue;

    /* Prepare and check the number of atoms: */
    atomsN = curr_mol_complexSP->atomsN;
    if (atomsN == 0) continue;

    /* Pointers to planes: */
    plane1SP = &curr_mol_complexSP->membraneS.plane1S;
    plane2SP = &curr_mol_complexSP->membraneS.plane2S;

    /*------draw the first plane:--------------------------------------------*/

    /* Draw one image (mono) or two images (stereo): */
    for (imageI = 0; imageI < imagesN; imageI++)
	{
	/* Plane center position: */
	center_screen_x = plane1SP->center_screen_x[imageI];
	center_screen_y = plane1SP->center_screen_y;

	/* The shifted phi angle, used to rotate */
	/* the rectangle  bounding the ellipsa : */
	angle = plane1SP->normal_phi[imageI] - 4.712389;

	/* Sine and cosine of this angle: */
	sin_angle = sin (angle);
	cos_angle = cos (angle);

	/* Reciprocal values of half axes: */
	if (plane1SP->screen_a == 0.0) reciprocal_a = 0.0;
	else reciprocal_a = 1.0 / plane1SP->screen_a;
	if (plane1SP->screen_b[imageI] == 0.0) reciprocal_b = 0.0;
	else reciprocal_b = 1.0 / plane1SP->screen_b[imageI];

	/* Reciprocal diameter (required for color fading effect): */
	if (plane1SP->circle_radius == 0.0) reciprocal_diameter = 0.0;
	else reciprocal_diameter = 0.5 / plane1SP->circle_radius;

	/* The coefficient used to calculate z value for a given pixel: */
	if (plane1SP->screen_b[imageI] == 0.0) coeff = 0.0;
	else
	    {
	    coeff = plane1SP->circle_radius *
		    sin (plane1SP->normal_theta[imageI]) /
		    plane1SP->screen_b[imageI];
	    if (plane1SP->normal_theta[imageI] >= 1.5707963) coeff *= -1;
	    }

	/* Find the plane extent: */
	PlaneExtent_ (&screen_x_min, &screen_x_max,
		      &screen_y_min, &screen_y_max,
		      plane1SP, configSP, imageI);

	/* Scan the rectangle bounding ellipse: */
	for (screen_x = screen_x_min; screen_x <= screen_x_max; screen_x++)
	    {
	    for (screen_y = screen_y_min; screen_y <= screen_y_max; screen_y++)
		{
		/* Pixel position relative to the plane center position: */
		x_relative = screen_x - center_screen_x;
		y_relative = screen_y - center_screen_y;

		/* Rotated pixel position (rotation is anticlockwise here): */
		x_rotated =  x_relative * cos_angle + y_relative * sin_angle;
		y_rotated = -x_relative * sin_angle + y_relative * cos_angle;

		/* Check is it inside bounding ellipse: */
		ratio_x = x_rotated * reciprocal_a;
		ratio_y = y_rotated * reciprocal_b;
		d = ratio_x * ratio_x + ratio_y * ratio_y;
		if (d > 1.0) continue;

		/* Calculate the z value for this pixel: */
		plane_delta_z = coeff * y_rotated;
		plane_z = plane1SP->center_z[imageI] + plane_delta_z;

		/* The current pixel index: */
		pixelI = guiSP->main_win_free_area_width * screen_y + screen_x;

		/* Check pixel index: */
		if (pixelI >= pixelsN) continue;

		/* Pointer to  NearestAtomS struct. */
		/* assigned to current coordinates: */
		curr_pixelSP = nearest_atomSP + pixelI;

		/* If something was drawn to the current */
		/* pixel in this step, compare z values: */
		if (curr_pixelSP->last_refreshI == refreshI)
		    {
		    /* If the point drawn before is closer to the */
		    /* observer than plane, do not draw anything: */
		    if (curr_pixelSP->z < plane_z)
			{
			continue;
			}

		    /* If the plane is closer than the */
		    /* point drawn before, mix colors: */
		    else
			{
			/* The plane color: */
			scale_factor = (plane_delta_z +
					plane1SP->circle_radius) *
					reciprocal_diameter;
			if (scale_factor < 0.0) scale_factor = 0.0;
			if (scale_factor > 1.0) scale_factor = 1.0;
			if (plane1SP->visible_sideI[imageI] == 0)
			    {
			    plane_colorID = WeightColors_ (
					plane1SP->top_near_colorID,
					plane1SP->top_far_colorID,
					scale_factor, guiSP);
			    }
			else
			    {
			    plane_colorID = WeightColors_ (
					plane1SP->bottom_near_colorID,
					plane1SP->bottom_far_colorID,
					scale_factor, guiSP);
			    }

			/* The color of the object which was */
			/* drawn  before  the current plane: */
			old_colorID = curr_pixelSP->colorID;

			/* The weighting factor which should be used */
			/* to mix the plane color and the old color: */
			scale_factor = plane1SP->transparency;

			/* Mix the plane color and the old color: */
			colorID = WeightColors_ (plane_colorID,
						 old_colorID,
						 scale_factor, guiSP);
			}
		    }

		/* If nothing was drawn to the current pixel */
		/* in this drawing step,  prepare the color: */
		else
		    {
		    /* Prepare the scale factor used to weight (mix) colors: */
		    scale_factor = (plane_delta_z +
				    plane1SP->circle_radius) *
				    reciprocal_diameter;
		    if (scale_factor < 0.0) scale_factor = 0.0;
		    if (scale_factor > 1.0) scale_factor = 1.0;

		    /* Prepare the color by weighting */
		    /* (mixing)  near and  far color: */
		    if (plane1SP->visible_sideI[imageI] == 0)
			{
			colorID = WeightColors_ (
					plane1SP->top_near_colorID,
					plane1SP->top_far_colorID,
					scale_factor, guiSP);
			}
		    else
			{
			colorID = WeightColors_ (
					plane1SP->bottom_near_colorID,
					plane1SP->bottom_far_colorID,
					scale_factor, guiSP);
			}

		    /* Set the style index associated with the current pixel */
		    /* to signal  that only plane  was drawn to  this pixel: */
		    curr_pixelSP->styleI = PLANE_STYLE;
		    }

		/* Draw point: */
		XSetForeground (guiSP->displaySP, guiSP->theGCA[0], colorID);
		XDrawPoint (guiSP->displaySP, guiSP->main_hidden_pixmapID,
			    guiSP->theGCA[0], screen_x, screen_y);

		/* Update refresh index,  z value and */
		/* color value for the current pixel: */
		curr_pixelSP->last_refreshI = refreshI;
		curr_pixelSP->z = plane_z;
		curr_pixelSP->colorID = colorID;
		}
	    }
	}

    /*------draw the second plane:-------------------------------------------*/

    /* Draw one image (mono) or two images (stereo): */
    for (imageI = 0; imageI < imagesN; imageI++)
	{
	/* Plane center position: */
	center_screen_x = plane2SP->center_screen_x[imageI];
	center_screen_y = plane2SP->center_screen_y;

	/* The shifted phi angle, used to rotate */
	/* the rectangle  bounding the ellipsa : */
	angle = plane2SP->normal_phi[imageI] - 4.712389;

	/* Sine and cosine of this angle: */
	sin_angle = sin (angle);
	cos_angle = cos (angle);

	/* Reciprocal values of half axes: */
	if (plane2SP->screen_a == 0.0) reciprocal_a = 0.0;
	else reciprocal_a = 1.0 / plane2SP->screen_a;
	if (plane2SP->screen_b[imageI] == 0.0) reciprocal_b = 0.0;
	else reciprocal_b = 1.0 / plane2SP->screen_b[imageI];

	/* Reciprocal diameter (required for color fading effect): */
	if (plane2SP->circle_radius == 0.0) reciprocal_diameter = 0.0;
	else reciprocal_diameter = 0.5 / plane2SP->circle_radius;

	/* The coefficient used to calculate z value for a given pixel: */
	if (plane2SP->screen_b[imageI] == 0.0) coeff = 0.0;
	else
	    {
	    coeff = plane2SP->circle_radius *
		    sin (plane2SP->normal_theta[imageI]) /
		    plane2SP->screen_b[imageI];
	    if (plane2SP->normal_theta[imageI] >= 1.5707963) coeff *= -1;
	    }

	/* Find the plane extent: */
	PlaneExtent_ (&screen_x_min, &screen_x_max,
		      &screen_y_min, &screen_y_max,
		      plane2SP, configSP, imageI);

	/* Scan the rectangle bounding ellipse: */
	for (screen_x = screen_x_min; screen_x <= screen_x_max; screen_x++)
	    {
	    for (screen_y = screen_y_min; screen_y <= screen_y_max; screen_y++)
		{
		/* Pixel position relative to the plane center position: */
		x_relative = screen_x - center_screen_x;
		y_relative = screen_y - center_screen_y;

		/* Rotated pixel position (rotation is anticlockwise here): */
		x_rotated =  x_relative * cos_angle + y_relative * sin_angle;
		y_rotated = -x_relative * sin_angle + y_relative * cos_angle;

		/* Check is it inside bounding ellipse: */
		ratio_x = x_rotated * reciprocal_a;
		ratio_y = y_rotated * reciprocal_b;
		d = ratio_x * ratio_x + ratio_y * ratio_y;
		if (d > 1.0) continue;

		/* Calculate the z value for this pixel: */
		plane_delta_z = coeff * y_rotated;
		plane_z = plane2SP->center_z[imageI] + plane_delta_z;

		/* The current pixel index: */
		pixelI = guiSP->main_win_free_area_width * screen_y + screen_x;

		/* Check pixel index: */
		if (pixelI >= pixelsN) continue;

		/* Pointer to  NearestAtomS struct. */
		/* assigned to current coordinates: */
		curr_pixelSP = nearest_atomSP + pixelI;

		/* If something was drawn to the current */
		/* pixel in this step, compare z values: */
		if (curr_pixelSP->last_refreshI == refreshI)
		    {
		    /* If the point drawn before is closer to the */
		    /* observer than plane, do not draw anything: */
		    if (curr_pixelSP->z < plane_z)
			{
			continue;
			}

		    /* If the plane is closer than the */
		    /* point drawn before, mix colors: */
		    else
			{
			/* The plane color: */
			scale_factor = (plane_delta_z +
					plane2SP->circle_radius) *
					reciprocal_diameter;
			if (scale_factor < 0.0) scale_factor = 0.0;
			if (scale_factor > 1.0) scale_factor = 1.0;
			if (plane2SP->visible_sideI[imageI] == 0)
			    {
			    plane_colorID = WeightColors_ (
					plane2SP->top_near_colorID,
					plane2SP->top_far_colorID,
					scale_factor, guiSP);
			    }
			else
			    {
			    plane_colorID = WeightColors_ (
					plane2SP->bottom_near_colorID,
					plane2SP->bottom_far_colorID,
					scale_factor, guiSP);
			    }

			/* The color of the object which was */
			/* drawn  before  the current plane: */
			old_colorID = curr_pixelSP->colorID;

			/* The weighting factor which should be used */
			/* to mix the plane color and the old color: */
			scale_factor = plane2SP->transparency;

			/* Mix the plane color and the old color: */
			colorID = WeightColors_ (plane_colorID,
						 old_colorID,
						 scale_factor, guiSP);
			}
		    }

		/* If nothing was drawn to the current pixel */
		/* in this drawing step,  prepare the color: */
		else
		    {
		    /* Prepare the scale factor used to weight (mix) colors: */
		    scale_factor = (plane_delta_z +
				    plane2SP->circle_radius) *
				    reciprocal_diameter;
		    if (scale_factor < 0.0) scale_factor = 0.0;
		    if (scale_factor > 1.0) scale_factor = 1.0;

		    /* Prepare the color by weighting */
		    /* (mixing)  near and  far color: */
		    if (plane2SP->visible_sideI[imageI] == 0)
			{
			colorID = WeightColors_ (
					plane2SP->top_near_colorID,
					plane2SP->top_far_colorID,
					scale_factor, guiSP);
			}
		    else
			{
			colorID = WeightColors_ (
					plane2SP->bottom_near_colorID,
					plane2SP->bottom_far_colorID,
					scale_factor, guiSP);
			}

		    /* Set the style index associated with the current pixel */
		    /* to signal  that only plane  was drawn to  this pixel: */
		    curr_pixelSP->styleI = PLANE_STYLE;
		    }

		/* Draw point: */
		XSetForeground (guiSP->displaySP, guiSP->theGCA[0], colorID);
		XDrawPoint (guiSP->displaySP, guiSP->main_hidden_pixmapID,
			    guiSP->theGCA[0], screen_x, screen_y);

		/* Update refresh index,  z value and */
		/* color value for the current pixel: */
		curr_pixelSP->last_refreshI = refreshI;
		curr_pixelSP->z = plane_z;
		curr_pixelSP->colorID = colorID;
		}
	    }
	}
    }

/* Return positive value (trivial): */
return 1;
}

/*===========================================================================*/