File: spacefill2.c

package info (click to toggle)
garlic 1.6-1.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 4,440 kB
  • ctags: 1,403
  • sloc: ansic: 52,465; makefile: 1,134
file content (418 lines) | stat: -rw-r--r-- 13,057 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
/* Copyright (C) 2000-2003 Damir Zucic */

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

			    spacefill2.c

Purpose:
	Draw atoms as spheres, version 2 (spacefill style 2).

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) Spheres drawn to the hidden pixmap.
	(2) Return value.

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

Notes:
	(1) Indentation is exceptionally 4 spaces.

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

#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:=================================================*/

unsigned long	Sp2Color_ (AtomS *, GUIS *, double);

/*======draw atoms as spheres, version 2:====================================*/

int DrawSpacefill2_ (MolComplexS *mol_complexSP, int mol_complexesN,
		     ConfigS *configSP, GUIS *guiSP,
		     NearestAtomS *nearest_atomSP, size_t pixelsN,
		     unsigned int refreshI)
{
int			imageI, imagesN;
double			scale_factor, user_atomic_position;
int			left_edge[2], right_edge[2];
int			mol_complexI;
MolComplexS		*curr_mol_complexSP;
size_t			atomsN, atomI;
AtomS			*curr_atomSP;
int			screen_x0, screen_y0;
double			radius, radius_squared;
double			denominator, reciprocal_denominator;
int			screen_radius;
long			screen_radius_squared;
int			screen_x1, screen_y1, screen_x2, screen_y2;
int			screen_x, screen_y;
long			delta_x, delta_y, distance_squared;
double			z0, front_z, delta_z, z;
size_t			pixelI;
NearestAtomS		*curr_pixelSP;
double			denominator2, reciprocal_denominator2;
double			rho_squared;
VectorS			vectorS;
double			abs_value, abs_value_squared;
double			scalar_product, cos_angle;
unsigned long		colorID;
int			bondI;

/* Initialize the bond index. The negative value means that pixels */
/* filled  in this function  are not a part of  any chemical bond. */
bondI = -1;

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

/* The auxiliary scale factor: */
scale_factor = configSP->user_screen_atomic_distance *
	       configSP->atomic_to_screen_scale_x;

/* Prepare the user position in atomic coordinates: */
user_atomic_position = configSP->user_atomic_position;

/* Left and right image edge (in stereo mode there are two images): */
left_edge[0]  = configSP->image_screen_x0[0];
right_edge[0] = configSP->image_screen_x1[0];
left_edge[1]  = configSP->image_screen_x0[1];
right_edge[1] = configSP->image_screen_x1[1];

/*------first pass - fill NearestAtomS array with planar atoms:--------------*/

/* Draw 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;

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

    /* Draw atoms which have the given style: */
    for (atomI = 0; atomI < atomsN; atomI++)
	{
	/* Pointer to the current atom: */
	curr_atomSP = curr_mol_complexSP->atomSP + atomI;

	/* Check style: */
	if (curr_atomSP->raw_atomS.atom_styleI != SPACEFILL2) continue;

	/* Check is atom hidden: */
	if (curr_atomSP->hiddenF) continue;

	/* Check is atom inside slab: */
	if (!curr_atomSP->inside_slabF) continue;

	/* Check is atom inside window: */
	if (!curr_atomSP->inside_windowF) continue;

	/* Atomic radius, in atomic units: */
	radius = curr_atomSP->raw_atomS.radius;

	/* The y coordinate of atomic center, in screen units: */
	screen_y0 = curr_atomSP->raw_atomS.screen_y;

	/* Prepare one image (mono) or two images (stereo): */
	for (imageI = 0; imageI < imagesN; imageI++)
            {
	    /* The z coordinate of atomic center, in atomic units: */
	    z0 = curr_atomSP->raw_atomS.z[imageI];

	    /* Prepare the factor required for projection of atomic radius: */
	    denominator = z0 - user_atomic_position;
	    if (denominator == 0.0) continue;
	    reciprocal_denominator = 1.0 / denominator;

	    /* Atomic radius in screen units: */
	    screen_radius = (int) (radius *
				   scale_factor *
				   reciprocal_denominator +
				   0.5);

	    /* Screen radius squared (used to improve speed): */
	    screen_radius_squared = (long) screen_radius *
				    (long) screen_radius;

	    /* The x coordinate of atomic center, in screen units: */
	    screen_x0 = curr_atomSP->raw_atomS.screen_x[imageI];

	    /* Define the bounding rectangle: */
	    screen_x1 = screen_x0 - screen_radius;
	    screen_y1 = screen_y0 - screen_radius;
	    screen_x2 = screen_x0 + screen_radius;
	    screen_y2 = screen_y0 + screen_radius;

	    /* Horizontal scan: */
	    for (screen_x = screen_x1; screen_x <= screen_x2; screen_x++)
		{
		/* Check horizontal position: */
		if (screen_x <  left_edge[imageI])  continue;
		if (screen_x >= right_edge[imageI]) continue;

		/* Vertical scan: */
		for (screen_y = screen_y1; screen_y <= screen_y2; screen_y++)
		    {
		    /* Check vertical position: */
		    if (screen_y < 0) continue;
		    if (screen_y >= (int) guiSP->main_win_free_area_height)
			{
			continue;
			}

		    /* Check distance from the circle center: */
		    delta_x = (long) (screen_x - screen_x0);
		    delta_y = (long) (screen_y - screen_y0);
		    distance_squared = delta_x * delta_x + delta_y * delta_y;
		    if (distance_squared > screen_radius_squared) continue;

		    /* Prepare index  to the array */
		    /* of NearestAtomS structures: */
		    pixelI = guiSP->main_win_free_area_width * screen_y +
			     screen_x;

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

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

		    /* Check was  this pixel used  already in */
		    /* this drawing step;  if it was, compare */
		    /* the z value of the current atom with z */
		    /* value previously stored to this pixel: */
		    if (refreshI == curr_pixelSP->last_refreshI)
			{
			if (z0 > curr_pixelSP->z) continue;
			}

		    /* Refresh the content of NearestAtomS */
		    /* array  associated  with this pixel: */
		    curr_pixelSP->styleI = SPACEFILL2;
		    curr_pixelSP->last_refreshI = refreshI;
		    curr_pixelSP->mol_complexI = mol_complexI;
		    curr_pixelSP->atomI = atomI;
		    curr_pixelSP->bondI = bondI;
		    curr_pixelSP->z = z0;
		    }
		}
	
	    }	/* imageI loop */
	}	/* atomI  loop */
    }		/* mol_complexI loop */

/*------second pass - draw spherical atoms:----------------------------------*/

/* Draw 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;

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

    /* Draw atoms which have the given style: */
    for (atomI = 0; atomI < atomsN; atomI++)
	{
	/* Pointer to the current atom: */
	curr_atomSP = curr_mol_complexSP->atomSP + atomI;

	/* Check style: */
	if (curr_atomSP->raw_atomS.atom_styleI != SPACEFILL2) continue;

	/* Check is atom hidden: */
	if (curr_atomSP->hiddenF) continue;

	/* Check is atom inside slab: */
	if (!curr_atomSP->inside_slabF) continue;

	/* Check is atom inside window: */
	if (!curr_atomSP->inside_windowF) continue;

	/* Atomic radius, in atomic units: */
	radius = curr_atomSP->raw_atomS.radius;

	/* Squared atomic radius: */
	radius_squared = radius * radius;

	/* The y coordinate of atomic center, in screen units: */
	screen_y0 = curr_atomSP->raw_atomS.screen_y;

	/* Prepare one image (mono) or two images (stereo): */
	for (imageI = 0; imageI < imagesN; imageI++)
            {
	    /* The z coordinate of atomic center, in atomic units: */
	    z0 = curr_atomSP->raw_atomS.z[imageI];

	    /* The z coordinate of the nearest */
	    /* point of a given atom (sphere): */
	    front_z = z0 - radius;

	    /* Prepare the factor required for projection of atomic radius: */
	    denominator = z0 - user_atomic_position;
	    if (denominator == 0.0) continue;
	    reciprocal_denominator = 1.0 / denominator;

	    /* Atomic radius in screen units: */
	    screen_radius = (int) (radius *
				   scale_factor *
				   reciprocal_denominator +
				   0.5);

	    /* Square of the atomic radius (in screen units): */
	    screen_radius_squared = (long) screen_radius *
				    (long) screen_radius;

	    /* Reciprocal value will be used to improve performance: */
	    denominator2 = (double) screen_radius_squared;
	    if (denominator2 == 0.0) continue;
	    reciprocal_denominator2 = 1.0 / denominator2;

	    /* The x coordinate of atomic center, in screen units: */
	    screen_x0 = curr_atomSP->raw_atomS.screen_x[imageI];

	    /* Define the bounding rectangle: */
	    screen_x1 = screen_x0 - screen_radius;
	    screen_y1 = screen_y0 - screen_radius;
	    screen_x2 = screen_x0 + screen_radius;
	    screen_y2 = screen_y0 + screen_radius;

	    /* Horizontal scan: */
	    for (screen_x = screen_x1; screen_x <= screen_x2; screen_x++)
		{
		/* Check horizontal position: */
		if (screen_x < left_edge[imageI])  continue;
		if (screen_x > right_edge[imageI]) continue;

		/* Vertical scan: */
		for (screen_y = screen_y1; screen_y <= screen_y2; screen_y++)
		    {
		    /* Check vertical position: */
		    if (screen_y < 0) continue;
		    if (screen_y >= (int) guiSP->main_win_free_area_height)
			{
			continue;
			}

		    /* Check distance from the circle center: */
		    delta_x = (long) (screen_x - screen_x0);
		    delta_y = (long) (screen_y - screen_y0);
		    distance_squared = delta_x * delta_x + delta_y * delta_y;
		    if (distance_squared > screen_radius_squared) continue;

		    /* Prepare index  to the array */
		    /* of NearestAtomS structures: */
		    pixelI = guiSP->main_win_free_area_width * screen_y +
			     screen_x;

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

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

		    /* Check z value (this is just a quick check): */
		    if (refreshI == curr_pixelSP->last_refreshI)
			{
			if (front_z > curr_pixelSP->z) continue;
			}

		    /* Calculate the accurate z value: */
		    rho_squared = radius_squared *
				  (double) distance_squared *
				  reciprocal_denominator2;
		    delta_z = sqrt (fabs (radius_squared - rho_squared));
		    z = z0 - delta_z;

		    /* Check z value (precise check): */
		    if (refreshI == curr_pixelSP->last_refreshI)
			{
			if (z > curr_pixelSP->z) continue;
			}

		    /* The vector from sphere center to the current pixel: */
		    vectorS.x = (double) delta_x;
		    vectorS.y = (double) delta_y;
		    vectorS.z = - sqrt (fabs (screen_radius_squared -
					      vectorS.x * vectorS.x -
					      vectorS.y * vectorS.y));
		    abs_value_squared = vectorS.x * vectorS.x +
					vectorS.y * vectorS.y +
					vectorS.z * vectorS.z;
		    abs_value = sqrt (abs_value_squared);

		    /* The scalar product  between this */
		    /* vector and  light source vector: */
		    scalar_product = vectorS.x * configSP->light_vectorS.x +
				     vectorS.y * configSP->light_vectorS.y +
				     vectorS.z * configSP->light_vectorS.z;

		    /* Cosine of  the angle between  the vector to the */
		    /* current pixel and the light source unit vector: */
		    if (abs_value == 0.0) cos_angle = 0.0;
		    else cos_angle = scalar_product / abs_value;

		    /* Prepare color: */
		    colorID = Sp2Color_ (curr_atomSP, guiSP, cos_angle);

		    XSetForeground (guiSP->displaySP,
				    guiSP->theGCA[0],
				    colorID);

		    XDrawPoint (guiSP->displaySP,
				guiSP->main_hidden_pixmapID,
				guiSP->theGCA[0],
				screen_x, screen_y);

		    /* Refresh the content of NearestAtomS */
		    /* array  associated  with this pixel: */
		    curr_pixelSP->styleI = SPACEFILL2;
		    curr_pixelSP->last_refreshI = refreshI;
		    curr_pixelSP->mol_complexI = mol_complexI;
		    curr_pixelSP->atomI = atomI;
		    curr_pixelSP->bondI = bondI;
		    curr_pixelSP->z = z;
		    curr_pixelSP->colorID = colorID;
		    }
		}
	
	    }	/* imageI loop */
	}	/* atomI  loop */
    }		/* mol_complexI loop */

/*---------------------------------------------------------------------------*/

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

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