Debian Codegraph @

debian/external_deps/fetk/gamer/src/biom/PDB2Mesh.C

raw
1 /*
2 * ***************************************************************************
3 * BIMoS = < Biomedical Image-based Modeling and Simulation >
4 * Copyright (C) 2009-2010 -- Zeyun Yu ([email protected])
5 * Dept. Computer Science, The University of Wisconsin-Milwaukee
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * ***************************************************************************
21 */
22
23 /*
24 * ***************************************************************************
25 * File: PDB2Mesh.C < ... >
26 *
27 * Author: Zeyun Yu
28 *
29 * Purpose: Convert PDB into 3D Volume using binary distance transform
30 * ***************************************************************************
31 */
32
33 #include <gamer/biom.h>
34 #include "gamercf.h"
35
36 typedef struct {
37 float x; // vertex coordinate
38 float y;
39 float z;
40 unsigned char neigh; // first bit: +x; second bit: -x
41 // third bit: +y; fourth bit: -y
42 // fifth bit: +z; sixth bit: -z
43 unsigned short px; // the corresponding index
44 unsigned short py;
45 unsigned short pz;
46 }MOL_VERTEX;
47
48 int *segment_index;
49 int *atom_index;
50 INT4VECT *quads;
51 MOL_VERTEX *vertex;
52 int vert_num;
53 int quad_num;
54 int xdim,ydim,zdim;
55
56
57 int CheckFaceCorner(float x, float y, float z);
58 void ExtractSES(MinHeapS*,SEEDS *, int *, int, int, int, int *, int, ATOM *, float);
59 void SetAtomIndex(int, int, int, int);
60 int ExtractSAS(int atom_num, ATOM *atom_list);
61 char CheckManifold(int i, int j, int k);
62 float GetAngle(int a, int b, int c);
63 void ReadPDB(char *filename, int *atom_num, ATOM **atomlist,
64 float min[3], float max[3]);
65
66
67 SurfaceMesh* SurfaceMesh_readPDB(char* input_name)
68 {
69 int i,j,k;
70 int a,b,c,d;
71 float orig[3],span[3];
72 int dim[3];
73 int m,n,l,num;
74 double threshold;
75 double nx,ny,nz;
76 int xydim,xyzdim;
77 clock_t begin, finish;
78 MinHeapS* min_heap;
79 SEEDS *AllSeeds;
80 SurfaceMesh *surfmesh;
81 int atom_num;
82 ATOM *atom_list = NULL;
83 float min[3], max[3];
84
85 // Read in the PDB file
86 ReadPDB(input_name, &atom_num, &atom_list, min, max);
87
88 xdim = (int)(((max[0]-min[0])+1)*DIM_SCALE);
89 ydim = (int)(((max[1]-min[1])+1)*DIM_SCALE);
90 zdim = (int)(((max[2]-min[2])+1)*DIM_SCALE);
91 xydim = xdim*ydim;
92 xyzdim = xdim*ydim*zdim;
93 //printf("dimension: %d X %d X %d\n",xdim,ydim,zdim);
94
95 atom_index = (int*)malloc(sizeof(int)*xyzdim);
96 segment_index = (int*)malloc(sizeof(int)*xyzdim);
97
98 for (k=0; k<xyzdim; k++)
99 atom_index[k] = 0;
100
101 orig[0] = min[0];
102 orig[1] = min[1];
103 orig[2] = min[2];
104 span[0] = (max[0] - min[0]) / (double)(xdim-1);
105 span[1] = (max[1] - min[1]) / (double)(ydim-1);
106 span[2] = (max[2] - min[2]) / (double)(zdim-1);
107 dim[0] = xdim;
108 dim[1] = ydim;
109 dim[2] = zdim;
110
111 for (m=0; m < atom_num; m++) {
112 atom_list[m].x = (atom_list[m].x-orig[0])/span[0];
113 atom_list[m].y = (atom_list[m].y-orig[1])/span[1];
114 atom_list[m].z = (atom_list[m].z-orig[2])/span[2];
115 atom_list[m].radius = (atom_list[m].radius+1.5)/((span[0]+span[1]+span[2])/3.0);
116 }
117
118 begin = clock();
119 num = ExtractSAS(atom_num,atom_list);
120 finish = clock();
121 //printf(" Extract SAS voxels: CPU Time = %f seconds \n",(double)(finish-begin)/CLOCKS_PER_SEC);
122 //printf(" Number of boundary voxels: %d\n\n",num);
123
124
125 begin = clock();
126 threshold = 1.5/((span[0]+span[1]+span[2])/3.0);
127 min_heap=(MinHeapS*)malloc(sizeof(MinHeapS));
128 min_heap->x = (unsigned short *)malloc(sizeof(unsigned short)*num*3);
129 min_heap->y = (unsigned short *)malloc(sizeof(unsigned short)*num*3);
130 min_heap->z = (unsigned short *)malloc(sizeof(unsigned short)*num*3);
131 min_heap->seed = (int *)malloc(sizeof(int)*num*3);
132 min_heap->dist = (float *)malloc(sizeof(float)*num*3);
133 AllSeeds = (SEEDS *)malloc(sizeof(SEEDS)*num);
134 ExtractSES(min_heap, AllSeeds, segment_index, xdim,ydim,zdim,atom_index,atom_num,atom_list, threshold*threshold);
135 finish = clock();
136 //printf(" Extract SES voxels: CPU Time = %f seconds \n\n",(double)(finish-begin)/CLOCKS_PER_SEC);
137
138
139
140 // detect and fix non-manifolds !
141 while (1) {
142 b = 0;
143 for (k=0; k<zdim; k++)
144 for (j=0; j<ydim; j++)
145 for (i=0; i<xdim; i++) {
146 if (segment_index[IndexVect(i,j,k)] == MaxVal) {
147 if (!CheckManifold(i, j, k)) { // non-manifold occurs
148 segment_index[IndexVect(i,j,k)] = 0;
149 min_heap->x[min_heap->size] = i;
150 min_heap->y[min_heap->size] = j;
151 min_heap->z[min_heap->size] = k;
152 min_heap->size++;
153 b++;
154 }
155 }
156 }
157 //printf("non-manifold = %d \n",b);
158 if (b == 0)
159 break;
160 }
161
162
163
164 // generate the surface mesh
165 vertex = (MOL_VERTEX *)malloc(sizeof(MOL_VERTEX)*num*8);
166 quads = (INT4VECT *)malloc(sizeof(INT4VECT)*num*6);
167 for (k=0; k<num*8; k++) {
168 vertex[k].neigh = 0;
169 }
170 for (k=0; k<xyzdim; k++)
171 atom_index[k] = -1;
172 begin = clock();
173 vert_num = 0;
174 quad_num = 0;
175 for(num = 0; num < min_heap->size; num++) {
176 i = min_heap->x[num];
177 j = min_heap->y[num];
178 k = min_heap->z[num];
179
180 // back face
181 if (segment_index[IndexVect(i-1,j,k)] == MaxVal) {
182 a = CheckFaceCorner(i-0.5, j-0.5, k-0.5);
183 vertex[a].neigh |= 40; // +y and +z
184 b = CheckFaceCorner(i-0.5, j-0.5, k+0.5);
185 vertex[b].neigh |= 24; // +y and -z
186 c = CheckFaceCorner(i-0.5, j+0.5, k+0.5);
187 vertex[c].neigh |= 20; // -y and -z
188 d = CheckFaceCorner(i-0.5, j+0.5, k-0.5);
189 vertex[d].neigh |= 36; // -y and +z
190
191 quads[quad_num].a = a;
192 quads[quad_num].b = b;
193 quads[quad_num].c = c;
194 quads[quad_num].d = d;
195 quad_num++;
196 }
197
198 // front face
199 if (segment_index[IndexVect(i+1,j,k)] == MaxVal) {
200 a = CheckFaceCorner(i+0.5, j-0.5, k-0.5);
201 vertex[a].neigh |= 40; // +y and +z
202 b = CheckFaceCorner(i+0.5, j+0.5, k-0.5);
203 vertex[b].neigh |= 36; // -y and +z
204 c = CheckFaceCorner(i+0.5, j+0.5, k+0.5);
205 vertex[c].neigh |= 20; // -y and -z
206 d = CheckFaceCorner(i+0.5, j-0.5, k+0.5);
207 vertex[d].neigh |= 24; // +y and -z
208
209 quads[quad_num].a = a;
210 quads[quad_num].b = b;
211 quads[quad_num].c = c;
212 quads[quad_num].d = d;
213 quad_num++;
214 }
215
216 // left face
217 if (segment_index[IndexVect(i,j-1,k)] == MaxVal) {
218 a = CheckFaceCorner(i+0.5, j-0.5, k-0.5);
219 vertex[a].neigh |= 33; // -x and +z
220 b = CheckFaceCorner(i+0.5, j-0.5, k+0.5);
221 vertex[b].neigh |= 17; // -x and -z
222 c = CheckFaceCorner(i-0.5, j-0.5, k+0.5);
223 vertex[c].neigh |= 18; // +x and -z
224 d = CheckFaceCorner(i-0.5, j-0.5, k-0.5);
225 vertex[d].neigh |= 34; // +x and +z
226
227 quads[quad_num].a = a;
228 quads[quad_num].b = b;
229 quads[quad_num].c = c;
230 quads[quad_num].d = d;
231 quad_num++;
232 }
233
234 // right face
235 if (segment_index[IndexVect(i,j+1,k)] == MaxVal) {
236 a = CheckFaceCorner(i+0.5, j+0.5, k-0.5);
237 vertex[a].neigh |= 33; // -x and +z
238 b = CheckFaceCorner(i-0.5, j+0.5, k-0.5);
239 vertex[b].neigh |= 34; // +x and +z
240 c = CheckFaceCorner(i-0.5, j+0.5, k+0.5);
241 vertex[c].neigh |= 18; // +x and -z
242 d = CheckFaceCorner(i+0.5, j+0.5, k+0.5);
243 vertex[d].neigh |= 17; // -x and -z
244
245 quads[quad_num].a = a;
246 quads[quad_num].b = b;
247 quads[quad_num].c = c;
248 quads[quad_num].d = d;
249 quad_num++;
250 }
251
252 // bottom face
253 if (segment_index[IndexVect(i,j,k-1)] == MaxVal) {
254 a = CheckFaceCorner(i+0.5, j-0.5, k-0.5);
255 vertex[a].neigh |= 9; // -x and +y
256 b = CheckFaceCorner(i-0.5, j-0.5, k-0.5);
257 vertex[b].neigh |= 10; // +x and +y
258 c = CheckFaceCorner(i-0.5, j+0.5, k-0.5);
259 vertex[c].neigh |= 6; // +x and -y
260 d = CheckFaceCorner(i+0.5, j+0.5, k-0.5);
261 vertex[d].neigh |= 5; // -x and -y
262
263 quads[quad_num].a = a;
264 quads[quad_num].b = b;
265 quads[quad_num].c = c;
266 quads[quad_num].d = d;
267 quad_num++;
268 }
269
270 // top face
271 if (segment_index[IndexVect(i,j,k+1)] == MaxVal) {
272 a = CheckFaceCorner(i+0.5, j-0.5, k+0.5);
273 vertex[a].neigh |= 9; // -x and +y
274 b = CheckFaceCorner(i+0.5, j+0.5, k+0.5);
275 vertex[b].neigh |= 5; // -x and -y
276 c = CheckFaceCorner(i-0.5, j+0.5, k+0.5);
277 vertex[c].neigh |= 6; // +x and -y
278 d = CheckFaceCorner(i-0.5, j-0.5, k+0.5);
279 vertex[d].neigh |= 10; // +x and +y
280
281 quads[quad_num].a = a;
282 quads[quad_num].b = b;
283 quads[quad_num].c = c;
284 quads[quad_num].d = d;
285 quad_num++;
286 }
287 }
288 finish = clock();
289 //printf(" Generate quad meshes: CPU Time = %f seconds \n",(double)(finish-begin)/CLOCKS_PER_SEC);
290 //printf(" vert-num : %d -- quad-num: %d \n\n",vert_num,quad_num);
291
292
293
294 // Smooth the mesh
295 begin = clock();
296 unsigned char neighbor;
297 for (num = 0; num < 3; num++) {
298 for (n = 0; n < vert_num; n++) {
299 nx = 0;
300 ny = 0;
301 nz = 0;
302 m = 0;
303 neighbor = vertex[n].neigh;
304
305 i = vertex[n].px;
306 j = vertex[n].py;
307 k = vertex[n].pz;
308 if (neighbor & 1) {
309 m++;
310 l = atom_index[IndexVect(i-1,j,k)];
311 nx += vertex[l].x;
312 ny += vertex[l].y;
313 nz += vertex[l].z;
314 }
315 if (neighbor & 2) {
316 m++;
317 l = atom_index[IndexVect(i+1,j,k)];
318 nx += vertex[l].x;
319 ny += vertex[l].y;
320 nz += vertex[l].z;
321 }
322 if (neighbor & 4) {
323 m++;
324 l = atom_index[IndexVect(i,j-1,k)];
325 nx += vertex[l].x;
326 ny += vertex[l].y;
327 nz += vertex[l].z;
328 }
329 if (neighbor & 8) {
330 m++;
331 l = atom_index[IndexVect(i,j+1,k)];
332 nx += vertex[l].x;
333 ny += vertex[l].y;
334 nz += vertex[l].z;
335 }
336 if (neighbor & 16) {
337 m++;
338 l = atom_index[IndexVect(i,j,k-1)];
339 nx += vertex[l].x;
340 ny += vertex[l].y;
341 nz += vertex[l].z;
342 }
343 if (neighbor & 32) {
344 m++;
345 l = atom_index[IndexVect(i,j,k+1)];
346 nx += vertex[l].x;
347 ny += vertex[l].y;
348 nz += vertex[l].z;
349 }
350
351 // update the position
352 vertex[n].x = nx/(float)m;
353 vertex[n].y = ny/(float)m;
354 vertex[n].z = nz/(float)m;
355 }
356 }
357 finish = clock();
358 //printf(" Smooth the quad meshes: CPU Time = %f seconds \n\n",(double)(finish-begin)/CLOCKS_PER_SEC);
359
360 // Allocate memory
361 surfmesh = SurfaceMesh_ctor(vert_num, quad_num*2);
362
363 // write vertices
364 for (i = 0; i < surfmesh->nv; i++) {
365 surfmesh->vertex[i].x = vertex[i].x*span[0]+orig[0];
366 surfmesh->vertex[i].y = vertex[i].y*span[1]+orig[1];
367 surfmesh->vertex[i].z = vertex[i].z*span[2]+orig[2];
368 }
369 // write triangles
370 float angle, angle1, angle2;
371 for (i = 0; i < quad_num; i++) {
372 a = quads[i].a;
373 b = quads[i].b;
374 c = quads[i].c;
375 d = quads[i].d;
376
377 angle1 = -999.0;
378 angle2 = -999.0;
379 angle = GetAngle(a,b,c);
380 if (angle > angle1)
381 angle1 = angle;
382 angle = GetAngle(a,d,c);
383 if (angle > angle1)
384 angle1 = angle;
385 angle = GetAngle(c,a,b);
386 if (angle > angle1)
387 angle1 = angle;
388 angle = GetAngle(c,a,d);
389 if (angle > angle1)
390 angle1 = angle;
391
392 angle = GetAngle(b,a,d);
393 if (angle > angle2)
394 angle2 = angle;
395 angle = GetAngle(b,c,d);
396 if (angle > angle2)
397 angle2 = angle;
398 angle = GetAngle(d,a,b);
399 if (angle > angle2)
400 angle2 = angle;
401 angle = GetAngle(d,b,c);
402 if (angle > angle2)
403 angle2 = angle;
404
405 if (angle1 <= angle2) {
406 surfmesh->face[2*i].a = a;
407 surfmesh->face[2*i].b = b;
408 surfmesh->face[2*i].c = c;
409 surfmesh->face[2*i+1].a = a;
410 surfmesh->face[2*i+1].b = c;
411 surfmesh->face[2*i+1].c = d;
412 }
413 else {
414 surfmesh->face[2*i].a = a;
415 surfmesh->face[2*i].b = b;
416 surfmesh->face[2*i].c = d;
417 surfmesh->face[2*i+1].a = b;
418 surfmesh->face[2*i+1].b = c;
419 surfmesh->face[2*i+1].c = d;
420 }
421 }
422
423 /* write to disk
424 FILE *fout;
425 if ((fout=fopen("output1.off", "wb"))==NULL){
426 printf("write error...\n");
427 exit(0);
428 };
429
430 fprintf(fout, "OFF\n");
431 fprintf(fout, "%d %d %d\n",vert_num,quad_num*2,vert_num+quad_num*2-2);
432 for (i = 0; i < vert_num; i++)
433 fprintf(fout, "%f %f %f \n",surfmesh->vertex[i].x,surfmesh->vertex[i].y,surfmesh->vertex[i].z);
434
435 for (i = 0; i < quad_num*2; i++) {
436 fprintf(fout, "3 %d %d %d\n",surfmesh->face[i].a,surfmesh->face[i].b,surfmesh->face[i].c);
437 }
438 fclose(fout);
439 */
440
441 free(AllSeeds);
442 free(segment_index);
443 free(atom_index);
444 free(atom_list);
445 free(min_heap->x);
446 free(min_heap->y);
447 free(min_heap->z);
448 free(min_heap->seed);
449 free(min_heap->dist);
450 free(min_heap);
451 free(vertex);
452 free(quads);
453
454 return surfmesh;
455 }
456
457
458
459 float GetAngle(int a, int b, int c)
460 {
461 float ax,ay,az;
462 float bx,by,bz;
463 float dist;
464
465 ax = vertex[b].x - vertex[a].x;
466 ay = vertex[b].y - vertex[a].y;
467 az = vertex[b].z - vertex[a].z;
468 dist = sqrt(ax*ax+ay*ay+az*az);
469 if (dist > 0) {
470 ax /= dist;
471 ay /= dist;
472 az /= dist;
473 }
474 bx = vertex[c].x - vertex[a].x;
475 by = vertex[c].y - vertex[a].y;
476 bz = vertex[c].z - vertex[a].z;
477 dist = sqrt(bx*bx+by*by+bz*bz);
478 if (dist > 0) {
479 bx /= dist;
480 by /= dist;
481 bz /= dist;
482 }
483
484 return (ax*bx+ay*by+az*bz);
485 }
486
487
488
489
490 char CheckManifold(int i, int j, int k)
491 {
492 char manifold, nonmanifold;
493 int m,n,l;
494
495 manifold = 1;
496 for (l=k-1; l<=k+1; l++) {
497 if (!manifold)
498 break;
499 for (n=j-1; n<=j+1; n++) {
500 if (!manifold)
501 break;
502 for (m=i-1; m<=i+1; m++) {
503 if (m!=i || n!=j || l!=k) {
504 if (segment_index[IndexVect(m,n,l)] == MaxVal) {
505 nonmanifold = 1;
506 if (m!=i && segment_index[IndexVect(m,j,k)] == MaxVal)
507 nonmanifold = 0;
508 if (n!=j && segment_index[IndexVect(i,n,k)] == MaxVal)
509 nonmanifold = 0;
510 if (l!=k && segment_index[IndexVect(i,j,l)] == MaxVal)
511 nonmanifold = 0;
512
513 if (nonmanifold)
514 manifold = 0;
515 }
516 }
517 if (!manifold)
518 break;
519 }
520 }
521 }
522
523 return(manifold);
524 }
525
526
527
528 int CheckFaceCorner(float x, float y, float z)
529 {
530 int m,n,l;
531 int a;
532
533 m = (int)x;
534 n = (int)y;
535 l = (int)z;
536 if (atom_index[IndexVect(m,n,l)] < 0) {
537 vertex[vert_num].x = x;
538 vertex[vert_num].y = y;
539 vertex[vert_num].z = z;
540 vertex[vert_num].px = m;
541 vertex[vert_num].py = n;
542 vertex[vert_num].pz = l;
543 atom_index[IndexVect(m,n,l)] = vert_num;
544 a = vert_num;
545 vert_num++;
546 }
547 else {
548 a = atom_index[IndexVect(m,n,l)];
549 }
550
551 return (a);
552 }
553
554
555
556 FLT2VECT FindIntersection(int n, int m, int j, int k, ATOM *atom_list)
557 {
558 FLT2VECT intersect;
559 int i;
560 char reorder;
561 double cx1,cy1,cz1;
562 double cx2,cy2,cz2;
563 double dist,radius1,radius2;
564 double cos_alpha;
565 double ax,ay,az;
566 double cx,cy,cz;
567
568
569 n--;
570 m--;
571 reorder = 0;
572 // always make the first atom on the left
573 // and the second on the right
574 if (atom_list[n].x > atom_list[m].x) {
575 i = n;
576 n = m;
577 m = i;
578 reorder = 1;
579 }
580
581 radius1 = atom_list[n].radius;
582 cx1 = atom_list[n].x;
583 cy1 = atom_list[n].y;
584 cz1 = atom_list[n].z;
585 radius2 = atom_list[m].radius;
586 cx2 = atom_list[m].x;
587 cy2 = atom_list[m].y;
588 cz2 = atom_list[m].z;
589
590 if (cx1 == cx2) {
591 if ((radius1*radius1-(k-cz1)*(k-cz1)-(j-cy1)*(j-cy1)) >=
592 (radius2*radius2-(k-cz2)*(k-cz2)-(j-cy2)*(j-cy2))) {
593 intersect.x = (float)MaxVal;
594 intersect.y = -(float)MaxVal;
595 }
596 else {
597 intersect.x = -(float)MaxVal;
598 intersect.y = (float)MaxVal;
599 }
600 }
601 else {
602 dist = sqrt((cx2-cx1)*(cx2-cx1)+(cy2-cy1)*(cy2-cy1)+(cz2-cz1)*(cz2-cz1));
603 cos_alpha = (radius1*radius1+dist*dist-radius2*radius2)/(2.0*radius1*dist);
604 ax = (cx2 - cx1)/dist;
605 ay = (cy2 - cy1)/dist;
606 az = (cz2 - cz1)/dist;
607 dist = radius1 * cos_alpha;
608 cx = dist*ax+cx1;
609 cy = dist*ay+cy1;
610 cz = dist*az+cz1;
611 dist = cx*ax + (cy-j)*ay + (cz-k)*az;
612
613 if (reorder == 0) {
614 intersect.x = (float)(dist/ax);
615 intersect.y = (float)MaxVal;
616 }
617 else {
618 intersect.x = -(float)MaxVal;
619 intersect.y = (float)(dist/ax);
620 }
621 }
622
623 return (intersect);
624 }
625
626
627
628 int ExtractSAS(int atom_num, ATOM *atom_list)
629 {
630 int i,j,k;
631 int m,n,l;
632 int dim[3], c[3];
633 int amax[3],amin[3];
634 float radius;
635 float x,y,z;
636 FLT2VECT intersect;
637
638
639 dim[0] = xdim;
640 dim[1] = ydim;
641 dim[2] = zdim;
642
643
644 for (m=0; m < atom_num; m++) {
645
646 radius = atom_list[m].radius;
647
648 // compute the dataset coordinates of the atom's center
649 c[0] = (int)(atom_list[m].x + 0.5);
650 c[1] = (int)(atom_list[m].y + 0.5);
651 c[2] = (int)(atom_list[m].z + 0.5);
652
653 // then compute the bounding box of the atom
654 for (j=0; j < 3; j++) {
655 int tmp;
656 tmp = (int)(c[j] - radius - 1);
657 tmp = (tmp < 0) ? 0 : tmp;
658 amin[j] = tmp;
659 tmp = (int)(c[j] + radius + 1);
660 tmp = (tmp > (dim[j]-1)) ? (dim[j]-1) : tmp;
661 amax[j] = tmp;
662 }
663
664 // begin blurring kernel
665 radius = radius*radius;
666 for(k = amin[2]; k <= amax[2]; k++) {
667 for(j = amin[1]; j <= amax[1]; j++) {
668 for(i = amin[0]; i <= amax[0]; i++) {
669 x = i;
670 y = j;
671 z = k;
672 if ((x-atom_list[m].x)*(x-atom_list[m].x)+
673 (y-atom_list[m].y)*(y-atom_list[m].y)+
674 (z-atom_list[m].z)*(z-atom_list[m].z) <= radius) {
675 if (atom_index[IndexVect(i,j,k)] > 0) {
676 intersect = FindIntersection(atom_index[IndexVect(i,j,k)], m+1, j, k, atom_list);
677 if (i >= intersect.x && i <= intersect.y)
678 atom_index[IndexVect(i,j,k)] = m+1;
679 }
680 else
681 atom_index[IndexVect(i,j,k)] = m+1;
682 }
683 }
684 }
685 }
686
687 }
688
689
690 // find voxels on the border
691 int total = 0;
692 for (l=1; l<zdim-1; l++)
693 for (n=1; n<ydim-1; n++)
694 for (m=1; m<xdim-1; m++) {
695 if (atom_index[IndexVect(m,n,l)]) {
696 int count = 0;
697 for (k=max(l-1,0); k<=min(l+1,zdim-1); k++)
698 for (j=max(n-1,0); j<=min(n+1,ydim-1); j++)
699 for (i=max(m-1,0); i<=min(m+1,xdim-1); i++) {
700 if (((i==m&&j==n) || (i==m&&k==l) || (k==l&&j==n) ) &&
701 atom_index[IndexVect(i,j,k)] == 0)
702 count = 1;
703 }
704 if (count) {
705 atom_index[IndexVect(m,n,l)] = -atom_index[IndexVect(m,n,l)];
706 total++;
707 }
708 }
709 }
710
711
712 return(total);
713 }