File: align_normal.c

package info (click to toggle)
garlic 1.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,516 kB
  • sloc: ansic: 52,465; makefile: 2,254
file content (317 lines) | stat: -rw-r--r-- 10,730 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
/* Copyright (C) 2000, 2001 Damir Zucic */

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

				align_normal.c

Purpose:
	Align normal vector of the specified complex with the specified axis.
	The entire  macromolecular complex  will be rotated.  The membrane is 
	not properly rotated!

Input:
	(1) Pointer to MolComplexS structure.
	(2) Pointer to ConfigS structure.
	(3) Axis identifier (1 = x, 2 = y, 3 = z).
	(4) Orientation identifier (-1 = antiparallel, 1 = parallel).

Output:
	(1) The macromolecular complex will be rotated.
	(2) Return value.

Return value:
	(1) Positive on success.
	(2) Zero if no alignment is necessary.
	(3) Negative on failure.

========includes:============================================================*/

#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		VectorProduct_ (VectorS *, VectorS *, VectorS *);
double		AbsoluteValue_ (VectorS *);
double		ScalarProduct_ (VectorS *, VectorS *);
int		PrepareStereoData_ (MolComplexS *, ConfigS *);

/*======align normal vector with given axis:=================================*/

int AlignNormalVector_ (MolComplexS *mol_complexSP, ConfigS *configSP,
			int axisID, int orientationID)
{
size_t		atomsN, atomI;
VectorS		normal_vectorS;
VectorS		target_vectorS;
VectorS		rotation_axis_vectorS;
double		a1, a2;
double		angle, cos_angle, sin_angle;
double		absolute_value, reciprocal_abs_value;
VectorS		unit_vector1S, unit_vector2S, unit_vector3S;
double		cos_stereo_angle, sin_stereo_angle;
double		x0, y0, z0;
AtomS		*curr_atomSP;
VectorS		radius_vectorS;
double		projection1, projection2;
PlaneS		*curr_planeSP;
double		x1, z1, x, z, x_new, z_new;

/* Check the pointer to the macromolecular complex: */
if (!mol_complexSP) return -1;

/* Are there any atoms at all? */
atomsN = mol_complexSP->atomsN;
if (atomsN == 0) return -2;

/* Copy the normal vector: */
normal_vectorS.x = mol_complexSP->planeS.normal_x[0];
normal_vectorS.y = mol_complexSP->planeS.normal_y;
normal_vectorS.z = mol_complexSP->planeS.normal_z[0];

/* Prepare  the target vector.  After rotation  the normal */
/* vector will be parallel or antiparallel to this vector: */
switch (axisID)
	{
	/* x axis: */
	case 1:
		target_vectorS.x = 1;
		target_vectorS.y = 0;
		target_vectorS.z = 0;
		break;

	/* y axis: */
	case 2:
		target_vectorS.x = 0;
		target_vectorS.y = 1;
		target_vectorS.z = 0;
		break;

	/* z axis: */
	case 3:
		target_vectorS.x = 0;
		target_vectorS.y = 0;
		target_vectorS.z = 1;
		break;

	default:
		return -3;
	}

/* Check the target orientation: */
if (orientationID == -1)
	{
	target_vectorS.x *= -1;
	target_vectorS.y *= -1;
	target_vectorS.z *= -1;
	}
else if (orientationID != 1) return -4;

/* The vector which defines the rotation axis: */
VectorProduct_ (&rotation_axis_vectorS, &normal_vectorS, &target_vectorS);

/* Prepare the rotation angle  and cosine and sine of this */
/* angle. Right-handed rotation is positive. Remember that */
/* arc cosine is very sensitive to  floating point errors: */
a1 = AbsoluteValue_ (&normal_vectorS);
a2 = AbsoluteValue_ (&target_vectorS);
if ((a1 == 0.0) || (a2 == 0.0)) return -5;
cos_angle = ScalarProduct_ (&normal_vectorS, &target_vectorS) / (a1 * a2);
if (cos_angle <= -1.0) angle = 3.1415927;
else if (cos_angle >= 1.0) angle = 0.0;
else angle = acos (cos_angle);
sin_angle = sin (angle);

/* Prepare and check the absolute value of the rotation axis vector: */
absolute_value = AbsoluteValue_ (&rotation_axis_vectorS);
if (absolute_value == 0.0) return -6;

/* Prepare the unit vector which defines the rotation axis: */
reciprocal_abs_value = 1.0 / absolute_value;
unit_vector1S.x = rotation_axis_vectorS.x * reciprocal_abs_value;
unit_vector1S.y = rotation_axis_vectorS.y * reciprocal_abs_value;
unit_vector1S.z = rotation_axis_vectorS.z * reciprocal_abs_value;

/* Calculate cosine and sine of the stereo angle: */
cos_stereo_angle = cos (configSP->stereo_angle);
sin_stereo_angle = sin (configSP->stereo_angle);

/* Copy the rotation center coordinates: */
x0 = mol_complexSP->rotation_center_vectorS.x;
y0 = mol_complexSP->rotation_center_vectorS.y;
z0 = mol_complexSP->rotation_center_vectorS.z;

/* Rotate every atom: */
for (atomI = 0; atomI < atomsN; atomI++)
	{
	/* Pointer to the current atom: */
	curr_atomSP = mol_complexSP->atomSP + atomI;

	/* Coordinates relative to the rotation center: */
	radius_vectorS.x = curr_atomSP->raw_atomS.x[0] - x0;
	radius_vectorS.y = curr_atomSP->raw_atomS.y    - y0;
	radius_vectorS.z = curr_atomSP->raw_atomS.z[0] - z0;

	/* Prepare the third unit vector (the second is prepared last): */
	VectorProduct_ (&unit_vector3S, &unit_vector1S, &radius_vectorS);

	/* If the absolute value is equal to zero it is */
	/* not necessary  to rotate  the radius vector: */
	absolute_value = AbsoluteValue_ (&unit_vector3S);
	if (absolute_value == 0.0) continue;

	/* Normalize the third unit vector: */
	reciprocal_abs_value = 1.0 / absolute_value;
	unit_vector3S.x = unit_vector3S.x * reciprocal_abs_value;
	unit_vector3S.y = unit_vector3S.y * reciprocal_abs_value;
	unit_vector3S.z = unit_vector3S.z * reciprocal_abs_value;

	/* Prepare the second unit vector: */
	VectorProduct_ (&unit_vector2S, &unit_vector3S, &unit_vector1S);

	/* Two projections of the radius vector: */
	projection1 = ScalarProduct_ (&radius_vectorS, &unit_vector1S);
	projection2 = ScalarProduct_ (&radius_vectorS, &unit_vector2S);

	/* Rotate the radius vector: */
	radius_vectorS.x = projection1 * unit_vector1S.x +
			   projection2 * unit_vector2S.x * cos_angle +
			   projection2 * unit_vector3S.x * sin_angle;
	radius_vectorS.y = projection1 * unit_vector1S.y +
			   projection2 * unit_vector2S.y * cos_angle +
			   projection2 * unit_vector3S.y * sin_angle;
	radius_vectorS.z = projection1 * unit_vector1S.z +
			   projection2 * unit_vector2S.z * cos_angle +
			   projection2 * unit_vector3S.z * sin_angle;

	/* Translate and store new coordinates: */
	curr_atomSP->raw_atomS.x[0] = radius_vectorS.x + x0;
	curr_atomSP->raw_atomS.y    = radius_vectorS.y + y0;
	curr_atomSP->raw_atomS.z[0] = radius_vectorS.z + z0;
	}

/* Prepare stereo data, if required: */
if (configSP->stereoF) PrepareStereoData_ (mol_complexSP, configSP);

/* Set the position_changedF: */
mol_complexSP->position_changedF = 1;

/* Prepare the pointer to the current plane: */
curr_planeSP = &mol_complexSP->planeS;

/* Rotate the plane center: */

/* Position of the plane center relative to the rotation center: */
radius_vectorS.x = curr_planeSP->center_x[0] - x0;
radius_vectorS.y = curr_planeSP->center_y    - y0;
radius_vectorS.z = curr_planeSP->center_z[0] - z0;

/* Prepare the third unit vector: */
VectorProduct_ (&unit_vector3S, &unit_vector1S, &radius_vectorS);

/* If the absolute value is different from zero */
/* it is necessary to rotate the radius vector: */
absolute_value = AbsoluteValue_ (&unit_vector3S);
if (absolute_value != 0.0)
	{
	/* Normalize the third unit vector: */
	reciprocal_abs_value = 1.0 / absolute_value;
	unit_vector3S.x = unit_vector3S.x * reciprocal_abs_value;
	unit_vector3S.y = unit_vector3S.y * reciprocal_abs_value;
	unit_vector3S.z = unit_vector3S.z * reciprocal_abs_value;

	/* The second unit vector: */
	VectorProduct_ (&unit_vector2S, &unit_vector3S, &unit_vector1S);

	/* Two projections of the radius vector: */
	projection1 = ScalarProduct_ (&radius_vectorS, &unit_vector1S);
	projection2 = ScalarProduct_ (&radius_vectorS, &unit_vector2S);

	/* Rotate the radius vector: */
	radius_vectorS.x = projection1 * unit_vector1S.x +
			   projection2 * unit_vector2S.x * cos_angle +
			   projection2 * unit_vector3S.x * sin_angle;
	radius_vectorS.y = projection1 * unit_vector1S.y +
			   projection2 * unit_vector2S.y * cos_angle +
			   projection2 * unit_vector3S.y * sin_angle;
	radius_vectorS.z = projection1 * unit_vector1S.z +
			   projection2 * unit_vector2S.z * cos_angle +
			   projection2 * unit_vector3S.z * sin_angle;

	/* Translate and store the rotated plane center: */
	curr_planeSP->center_x[0] = radius_vectorS.x + x0;
	curr_planeSP->center_y    = radius_vectorS.y + y0;
	curr_planeSP->center_z[0] = radius_vectorS.z + z0;

	/* Prepare stereo data for the plane center: */
	x1 = mol_complexSP->geometric_center_vectorS.x;
	z1 = mol_complexSP->geometric_center_vectorS.z;
	x = curr_planeSP->center_x[0] - x1;
	z = curr_planeSP->center_z[0] - z1;
	x_new =  x * cos_stereo_angle + z * sin_stereo_angle;
	z_new = -x * sin_stereo_angle + z * cos_stereo_angle;
	curr_planeSP->center_x[1] = x_new + x1;
	curr_planeSP->center_z[1] = z_new + z1;
	}

/* Rotate the normal vector: */

/* Prepare the third unit vector: */
VectorProduct_ (&unit_vector3S, &unit_vector1S, &normal_vectorS);

/* If the absolute value is zero there is */
/* no need  to rotate  the normal vector: */
absolute_value = AbsoluteValue_ (&unit_vector3S);
if (absolute_value == 0.0) return 1;

/* Normalize the third unit vector: */
reciprocal_abs_value = 1.0 / absolute_value;
unit_vector3S.x = unit_vector3S.x * reciprocal_abs_value;
unit_vector3S.y = unit_vector3S.y * reciprocal_abs_value;
unit_vector3S.z = unit_vector3S.z * reciprocal_abs_value;

/* The second unit vector: */
VectorProduct_ (&unit_vector2S, &unit_vector3S, &unit_vector1S);

/* Two projections of the radius vector: */
projection1 = ScalarProduct_ (&normal_vectorS, &unit_vector1S);
projection2 = ScalarProduct_ (&normal_vectorS, &unit_vector2S);

/* Rotate the radius vector: */
normal_vectorS.x = projection1 * unit_vector1S.x +
		   projection2 * unit_vector2S.x * cos_angle +
		   projection2 * unit_vector3S.x * sin_angle;
normal_vectorS.y = projection1 * unit_vector1S.y +
		   projection2 * unit_vector2S.y * cos_angle +
		   projection2 * unit_vector3S.y * sin_angle;
normal_vectorS.z = projection1 * unit_vector1S.z +
		   projection2 * unit_vector2S.z * cos_angle +
		   projection2 * unit_vector3S.z * sin_angle;

/* Translate and store the rotated normal vector: */
curr_planeSP->normal_x[0] = normal_vectorS.x;
curr_planeSP->normal_y    = normal_vectorS.y;
curr_planeSP->normal_z[0] = normal_vectorS.z;

/* Prepare stereo data for normal vector: */
curr_planeSP->normal_x[1] =  curr_planeSP->normal_x[0] * cos_stereo_angle +
			     curr_planeSP->normal_z[0] * sin_stereo_angle;
curr_planeSP->normal_z[1] = -curr_planeSP->normal_x[0] * sin_stereo_angle +
			     curr_planeSP->normal_z[0] * cos_stereo_angle;

/* Return positive value on success: */
return 2;
}

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