File: molecularSurfaceGrid.C

package info (click to toggle)
ball 1.5.0%2Bgit20180813.37fc53c-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 239,888 kB
  • sloc: cpp: 326,149; ansic: 4,208; python: 2,303; yacc: 1,778; lex: 1,099; xml: 958; sh: 322; makefile: 95
file content (406 lines) | stat: -rw-r--r-- 11,785 bytes parent folder | download | duplicates (9)
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
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
// $Id: molecularSurfaceGrid.C,v 1.24 2005/12/23 17:03:03 amoll Exp $
//

#include <BALL/SOLVATION/molecularSurfaceGrid.h>
#include <BALL/KERNEL/forEach.h>

namespace BALL 
{
	const char CCONN__INSIDE = 0;
	const char CCONN__OUTSIDE = 1;
	const char CCONN__INSIDE_PROBE = (char) 64;

	// needed for quicksort (below)
	extern "C" int 
	#ifdef BALL_COMPILER_MSVC
	__cdecl 
	#endif
	comparePointerSizeIntPtr_(const void* a, const void* b)
	{
		return (int)(*(PointerSizeUInt*)a - *(PointerSizeUInt*)b);
	}



	TRegularData3D<char>* calculateSESGrid
		(const Vector3& lower, const Vector3& upper,
		 float spacing, const System& system, 
		 float probe_radius)
	{

		// points in the grid marked with

		PointerSizeUInt* fast_sphere;
		PointerSizeUInt* fast_sphere_relative;
		
		// contains the squared length of the diagonal distance in the grid
		float d;

		// contains the squared grid spacing
		float spacing2 = spacing * spacing;

		// the atom_radius
		float atom_radius;
		
		// The radius of the border points around an atom
		float R_b, R_b2;

		// the grid itself...
		TRegularData3D<char>	*grid;

		// squared distance of any two vectors
		float squared_distance;
		float x, y, z;
		Vector3 origin;

		// indices used in between to calculated the bounding boxes of spheres in the grid
		TRegularData3D<char>::IndexType upper_index;
		TRegularData3D<char>::IndexType lower_index;

		// pointer to grid data
		char* grid_value;
		
		// vector, describing the atom's coordinates
		Vector3 r0;

		// Here we go...
		// First, create the grid...
		grid = new TRegularData3D<char>(lower, upper - lower, Vector3(spacing));
		
		if (grid == 0)
		{
			return 0;
		}

		// ..then, calculate the grid's diagonal length
		d = sqrt(3.f) * spacing;

		// calculate some constants used for FAST access to the grid.
		origin = grid->getOrigin();

		// Nx is the number of points in the grid along the x-axis
		PointerSizeUInt Nx = grid->getSize().x;

		// Nxy is the number of points in an xy-plane
		PointerSizeUInt Nxy = Nx * grid->getSize().y;

		PointerSizeUInt i;
		PointerSizeUInt j;
		PointerSizeUInt k;

		// constructing the FAST probe sphere, a collection of points 
		// of a sphere on the grid relative to the sphere's origin
		
		PointerSizeUInt count;
		PointerSizeUInt relative_count;

		// the probe_radius (squared) in squared grid units
		unsigned short grid_radius;
		grid_radius = (unsigned short)((probe_radius) * (probe_radius) / spacing2 + 0.5);
		count = 0;
		for (i = 0; i < (PointerSizeUInt)(probe_radius / spacing + 2); i++)
		{
			for (j = 0; j < (PointerSizeUInt)(probe_radius / spacing + 2); j++)
			{
				for (k = 0; k < (PointerSizeUInt)(probe_radius / spacing + 2); k++)
				{
					if ((i * i + j * j + k * k) < grid_radius)
					{
						count++;
					}
				}
			}
		}

		fast_sphere = new PointerSizeUInt[count * 8];
		fast_sphere_relative = new PointerSizeUInt[count * 8];

		count = 0;
		for (i = 0; i < (PointerSizeUInt)(probe_radius / spacing + 2); i++)
			for (j = 0; j < (PointerSizeUInt)(probe_radius / spacing + 2); j++)
					for (k = 0; k < (PointerSizeUInt)(probe_radius / spacing + 2); k++)
						if ((i * i + j * j + k * k) < grid_radius){
							fast_sphere[count++] =  (long)k + (long)j * (long)Nx + (long)i * (long)Nxy;
							fast_sphere[count++] = -(long)k + (long)j * (long)Nx + (long)i * (long)Nxy;
							fast_sphere[count++] =  (long)k - (long)j * (long)Nx + (long)i * (long)Nxy;
							fast_sphere[count++] = -(long)k - (long)j * (long)Nx + (long)i * (long)Nxy;
							fast_sphere[count++] =  (long)k + (long)j * (long)Nx - (long)i * (long)Nxy;
							fast_sphere[count++] = -(long)k + (long)j * (long)Nx - (long)i * (long)Nxy;
							fast_sphere[count++] =  (long)k - (long)j * (long)Nx - (long)i * (long)Nxy;
							fast_sphere[count++] = -(long)k - (long)j * (long)Nx - (long)i * (long)Nxy; 
						}

		// now, for speed's sake, we sort the entries in fast_sphere
		qsort(fast_sphere, count, sizeof(long), comparePointerSizeIntPtr_);

		long last_index;
		relative_count = 1;
		last_index = fast_sphere[0];
		// loop variable
		PointerSizeUInt u;

		fast_sphere_relative[0] = fast_sphere[0];
		for (u = 1; u < count; u++){
			if (fast_sphere[u] != fast_sphere[u - 1]){
				fast_sphere_relative[relative_count] = fast_sphere[u] - last_index;
				last_index = fast_sphere[u];
				relative_count++;
			}
		}
					
		// throw away the original (unsorted) sphere data
		delete [] fast_sphere;

		// mark the whole grid with CCONN__OUTSIDE, meaning OUTSIDE
		// There will be three different marks: INSIDE(I), OUTSIDE(O), and BORDER(B)
		// BORDER is just used temporarily. In the end, The grid will just contain INSIDEs and OUTSIDEs

		memset((void*)&grid->getData(0), CCONN__OUTSIDE, grid->size() * sizeof(char));

		// for each atom do...
		AtomConstIterator	atom_iterator;
		BALL_FOREACH_ATOM(system, atom_iterator)
		{
			atom_radius = (*atom_iterator).getRadius();	

			// consider the atom only if it`s radius is large
			// against grid spacing
			if (atom_radius > spacing / 100)
			{
				R_b = atom_radius + probe_radius;
				R_b2 = R_b * R_b;
				
				r0 = atom_iterator->getPosition();
				lower_index = grid->getClosestIndex(Vector3(r0.x - R_b - d, r0.y - R_b - d, r0.z - R_b - d));
				upper_index = grid->getClosestIndex(Vector3(r0.x + R_b + d, r0.y + R_b + d, r0.z + R_b + d));

				char* data = &(grid->getData(0));
				for(k = lower_index.z; k <= upper_index.z; k++)
					for(j = lower_index.y; j <= upper_index.y; j++)
						for(i = lower_index.x; i <= upper_index.x; i++){
							x = (float)i * spacing + origin.x;
							y = (float)j * spacing + origin.y;
							z = (float)k * spacing + origin.z;

							grid_value = &(data[i + Nx * j + Nxy * k]);
															
								
							if (*grid_value != CCONN__INSIDE)
							{
								squared_distance =  (r0.x - x) * (r0.x - x)
																 +  (r0.y - y) * (r0.y - y)
																 +  (r0.z - z) * (r0.z - z);
								if (squared_distance <= R_b2)
								{
									*grid_value = CCONN__INSIDE;
								}
							}
						}
			}
		}

		// now check for all points in the grid area that has been changed
		// for points marked as BORDER 
		// If one of these points is found, remove all points marked INSIDE
		// which are less then probe_radius apart from the border point
		// (actually this means to "roll" the probe sphere along the border
		// and just retain points which haven't been touched by the probe sphere)
		PointerSizeUInt idx;
		PointerSizeUInt grid_pointer, grid_begin, grid_end;
		PointerSizeUInt* fast_sphere_end;
		PointerSizeUInt* sphere_pointer;

		unsigned short border;

		PointerSizeUInt border_count;
		border_count = 0;
					
		grid_begin = (PointerSizeUInt)grid->getData(0);
		grid_end = (PointerSizeUInt)grid->getData(grid->size()-1);

		PointerSizeUInt s;
		PointerSizeUInt t;
		PointerSizeUInt q;

		char* data = &(grid->getData(0));
		for (s = 1; s < grid->getSize().z - 1; s++)
		{
			for (t = 1; t < grid->getSize().y - 1; t++)
			{
				for (q = 1; q < grid->getSize().x - 1; q++)
				{
					// calculate the absolute grid index the hard way (faster!)
					idx = q + Nx * t + s * Nxy;

					if ((data[idx] & 127) == CCONN__OUTSIDE)
					{
						border	= data[idx - 1]
										+ data[idx + 1]
										+ data[idx - Nx]
										+ data[idx + Nx]
										+ data[idx - Nxy]
										+ data[idx + Nxy];

						border &= 127;
					
						if ((border > 0) && (border < 6))
						{			
							// Okay, we found a point on the boundary
							border_count++;												
							grid_pointer = reinterpret_cast<PointerSizeUInt>(&(data[idx]));
							fast_sphere_end = &(fast_sphere_relative[relative_count - 1]);

							for (sphere_pointer = fast_sphere_relative; sphere_pointer <= fast_sphere_end; sphere_pointer++)
							{
								grid_pointer += static_cast<PointerSizeUInt>(*sphere_pointer);
								if ((grid_pointer <= grid_end) && (grid_pointer >= grid_begin))
								{
									*((char*)grid_pointer) |= CCONN__INSIDE_PROBE;
								}
							}
						}
					}
				}
			}
		}

		delete [] fast_sphere_relative;


		// now, check for each point if it is inside
		// A point is outside, if it has not been marked inside
		// (inside the vdW-radius) and has not been marked inside
		// the probe_radius of any border point

		grid_value = &(grid->getData(0));
		PointerSizeUInt l;

		for (l = 0; l < grid->size(); l++)
		{
			*grid_value = (*grid_value == 0);
			grid_value++;
		}

		return &(*grid);
	}

	TRegularData3D<char>* calculateSASGrid
		(const Vector3& lower, const Vector3& upper,
		 float spacing, const System& system, float probe_radius)
	{

		// points in the grid marked with
		// CCONN__OUTSIDE are outside of the molecule (default)
		// CCONN__INSIDE are sure inside the molecule

		// contains the squared length of the diagonal distance in the grid
		float d;

		// the atom_radius
		float atom_radius;
		
		// The radius of the border points around an atom
		float R_b, R_b2;

		// the grid itself...
		TRegularData3D<char>	*grid;

		// squared distance of any two vectors
		float squared_distance;
		float x, y, z;
		Vector3 origin;


		// indices used in between to calculated the bounding boxes of spheres in the grid
		TRegularData3D<char>::IndexType upper_index;
		TRegularData3D<char>::IndexType lower_index;

		// pointer to grid data
		char *grid_value;
					
		// vector, describing the atom's coordinates
		Vector3 r0;
					
		// Here we go...
		// First, create the grid...

		grid = new TRegularData3D<char>(lower, upper - lower, Vector3(spacing));

		if (grid == 0)
		{
			throw Exception::OutOfMemory(__FILE__, __LINE__);
		}
				
		// ..then, calculate the grid's diagonal length
		d = sqrt(3.f) * spacing;

		// calculate some constants used for FAST access to the grid.
		origin = grid->getOrigin();

		// Nx is the number of points in the grid along the x-axis
		PointerSizeUInt Nx = grid->getSize().x;

		// Nxy is the number of points in an xy-plane
		PointerSizeUInt Nxy = Nx * grid->getSize().y;

		// mark the whole grid with CCONN__OUTSIDE, meaning OUTSIDE
		// There will be three different marks: INSIDE(I), OUTSIDE(O), and BORDER(B)
		// BORDER is just used temporarily. In the end, The grid will just contain INSIDEs and OUTSIDEs

		memset((void*)&grid->getData(0), CCONN__OUTSIDE, grid->size() * sizeof(char));

		// for each atom do...	
		char* data = &(grid->getData(0));
		AtomConstIterator	atom_iterator;
		BALL_FOREACH_ATOM(system, atom_iterator)
		{
			atom_radius = (*atom_iterator).getRadius();	

			// consider the atom only if it`s radius is large
			// against grid spacing
			if (atom_radius > spacing / 100)
			{
				R_b = atom_radius + probe_radius;
				R_b2 = R_b * R_b;

				r0 = (*atom_iterator).getPosition();
				lower_index = grid->getClosestIndex(Vector3(r0.x - R_b - d, r0.y - R_b - d, r0.z - R_b - d));

				upper_index = grid->getClosestIndex(Vector3(r0.x + R_b + d, r0.y + R_b + d, r0.z + R_b + d));

				for (PointerSizeUInt k = lower_index.z; k <= upper_index.z; k++)
				{
					for (PointerSizeUInt j = lower_index.y; j <= upper_index.y; j++)
					{
						for (PointerSizeUInt i = lower_index.x; i <= upper_index.x; i++)
						{
							x = (float)i * spacing + origin.x;
							y = (float)j * spacing + origin.y;
							z = (float)k * spacing + origin.z;

							grid_value = &(data[i + Nx * j + Nxy * k]);
															
							if (*grid_value != CCONN__INSIDE)
							{
								squared_distance =  (r0.x - x) * (r0.x - x)
																 +  (r0.y - y) * (r0.y - y)
																 +  (r0.z - z) * (r0.z - z);

								if (squared_distance <= R_b2)
								{
									*grid_value = CCONN__INSIDE;
								}
							}
						}
					}
				}
			}
		}

		return &(*grid);
	}

} // namespace BALL