Debian Codegraph @

debian/external_deps/fetk/gamer/src/biom/RegionGrowing.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: RegionGrowing.C < ... >
26 *
27 * Author: Zeyun Yu
28 *
29 * Purpose: Find the SES using the fast marching method
30 * ***************************************************************************
31 */
32
33
34 #include <gamer/biom.h>
35 #include "gamercf.h"
36
37
38 #define MaxDist 29999
39
40 int xdim1,ydim1,zdim1;
41 MinHeapS* min_heap;
42 SEEDS *AllSeeds;
43 int *heap_pointer;
44 ATOM *atom_list;
45 float threshold;
46 unsigned short min_x,min_y,min_z;
47 int min_seed;
48 float min_dist;
49
50
51 void GetMinimum(void);
52 void InsertHeap(int, int, int, float);
53 void UpdateHeap(int, int, int,float);
54 void Marching(void);
55 FLTVECT FindSeed(float, float, float, int);
56
57
58
59 void ExtractSES(MinHeapS *mheap, SEEDS *all_seeds, int *heappointer, int xd, int yd, int zd,
60 int *atom_index, int atom_num, ATOM *atomlist, float thresh)
61 {
62 int i,j,k;
63 int m,n,l, num, c;
64 int index,index1;
65 float dist;
66 FLTVECT seed;
67 char visited;
68
69
70 xdim1 = xd;
71 ydim1 = yd;
72 zdim1 = zd;
73 atom_list = atomlist;
74 min_heap = mheap;
75 AllSeeds = all_seeds;
76 heap_pointer = heappointer;
77 threshold = thresh;
78
79 /* Initialize */
80 index = 0;
81 min_heap->size = 0;
82 for (k=0; k<zdim1; k++)
83 for (j=0; j<ydim1; j++)
84 for (i=0; i<xdim1; i++) {
85 if (atom_index[IndexVect1(i,j,k)] < 0) {
86 for (num = 0; num < MaxAtom; num++)
87 AllSeeds[index].atom[num] = -1;
88 num = 0;
89 for (l=k-1; l<=k+1; l++)
90 for (n=j-1; n<=j+1; n++)
91 for (m=i-1; m<=i+1; m++) {
92 if (m==i||n==j||l==k) {
93 index1 = atom_index[IndexVect1(m,n,l)];
94 if (index1 < 0) {
95 index1 = -index1-1;
96 visited = 0;
97 for (c=0; c<num; c++) {
98 if (index1 == AllSeeds[index].atom[c])
99 visited = 1;
100 }
101 if (visited == 0) {
102 AllSeeds[index].atom[num] = index1;
103 num++;
104 if (num == MaxAtom)
105 num--;
106 }
107 }
108 }
109 }
110
111 seed = FindSeed(i,j,k,index);
112 AllSeeds[index].seedx = seed.x;
113 AllSeeds[index].seedy = seed.y;
114 AllSeeds[index].seedz = seed.z;
115 dist = (seed.x-i)*(seed.x-i) + (seed.y-j)*(seed.y-j) + (seed.z-k)*(seed.z-k);
116 min_seed = index;
117 InsertHeap(i,j,k, dist);
118
119 index++;
120 }
121 else if (atom_index[IndexVect1(i,j,k)] > 0) {
122 heap_pointer[IndexVect1(i,j,k)] = MaxVal;
123 }
124 else {
125 heap_pointer[IndexVect1(i,j,k)] = -11;
126 }
127 }
128
129
130 /* Fast Marching Method */
131 while (1){
132 GetMinimum();
133 if (min_dist >= MaxDist-0.001)
134 break;
135
136 Marching();
137 }
138
139 }
140
141
142
143
144 void GetMinimum(void)
145 {
146 int pointer, left, right;
147 float dist;
148
149 min_x = min_heap->x[0];
150 min_y = min_heap->y[0];
151 min_z = min_heap->z[0];
152 min_seed = min_heap->seed[0];
153 min_dist = min_heap->dist[0];
154
155
156 if (min_dist == MaxDist)
157 return;
158
159 heap_pointer[IndexVect1(min_heap->x[0],min_heap->y[0],min_heap->z[0])] = -3;
160
161 min_heap->size--;
162 dist=min_heap->dist[min_heap->size];
163
164 pointer=1;
165 while (pointer <= min_heap->size/2) {
166 left=2*pointer;
167 right=2*pointer+1;
168 if ((min_heap->dist[left-1] <= min_heap->dist[right-1]) && (min_heap->dist[left-1] < dist)) {
169 min_heap->x[pointer-1]=min_heap->x[left-1];
170 min_heap->y[pointer-1]=min_heap->y[left-1];
171 min_heap->z[pointer-1]=min_heap->z[left-1];
172 min_heap->seed[pointer-1]=min_heap->seed[left-1];
173 min_heap->dist[pointer-1]=min_heap->dist[left-1];
174 heap_pointer[IndexVect1(min_heap->x[pointer-1],min_heap->y[pointer-1],min_heap->z[pointer-1])] = pointer-1;
175 pointer=left;
176 }
177 else if ((min_heap->dist[left-1] > min_heap->dist[right-1]) && (min_heap->dist[right-1] < dist)){
178 min_heap->x[pointer-1]=min_heap->x[right-1];
179 min_heap->y[pointer-1]=min_heap->y[right-1];
180 min_heap->z[pointer-1]=min_heap->z[right-1];
181 min_heap->seed[pointer-1]=min_heap->seed[right-1];
182 min_heap->dist[pointer-1]=min_heap->dist[right-1];
183 heap_pointer[IndexVect1(min_heap->x[pointer-1],min_heap->y[pointer-1],min_heap->z[pointer-1])] = pointer-1;
184 pointer=right;
185 }
186 else break;
187 }
188
189 min_heap->x[pointer-1]=min_heap->x[min_heap->size];
190 min_heap->y[pointer-1]=min_heap->y[min_heap->size];
191 min_heap->z[pointer-1]=min_heap->z[min_heap->size];
192 min_heap->seed[pointer-1]=min_heap->seed[min_heap->size];
193 min_heap->dist[pointer-1]=dist;
194 heap_pointer[IndexVect1(min_heap->x[min_heap->size],min_heap->y[min_heap->size],min_heap->z[min_heap->size])] = pointer-1;
195
196 }
197
198 void InsertHeap(int x, int y, int z, float dist)
199 {
200 int pointer, parent;
201
202
203 min_heap->size++;
204 pointer=min_heap->size;
205
206 while (pointer > 1) {
207 if (pointer%2 == 0) {
208 parent=pointer/2;
209 if (dist < min_heap->dist[parent-1]) {
210 min_heap->x[pointer-1]=min_heap->x[parent-1];
211 min_heap->y[pointer-1]=min_heap->y[parent-1];
212 min_heap->z[pointer-1]=min_heap->z[parent-1];
213 min_heap->seed[pointer-1]=min_heap->seed[parent-1];
214 min_heap->dist[pointer-1]=min_heap->dist[parent-1];
215 heap_pointer[IndexVect1(min_heap->x[pointer-1],min_heap->y[pointer-1],min_heap->z[pointer-1])] = pointer-1;
216 pointer=parent;
217 }
218 else break;
219 }
220 else if (pointer%2 == 1){
221 parent=(pointer-1)/2;
222 if (dist < min_heap->dist[parent-1]) {
223 min_heap->x[pointer-1]=min_heap->x[parent-1];
224 min_heap->y[pointer-1]=min_heap->y[parent-1];
225 min_heap->z[pointer-1]=min_heap->z[parent-1];
226 min_heap->seed[pointer-1]=min_heap->seed[parent-1];
227 min_heap->dist[pointer-1]=min_heap->dist[parent-1];
228 heap_pointer[IndexVect1(min_heap->x[pointer-1],min_heap->y[pointer-1],min_heap->z[pointer-1])] = pointer-1;
229 pointer=parent;
230 }
231 else break;
232 }
233 }
234 min_heap->x[pointer-1]=x;
235 min_heap->y[pointer-1]=y;
236 min_heap->z[pointer-1]=z;
237 min_heap->seed[pointer-1]=min_seed;
238 min_heap->dist[pointer-1]=dist;
239
240 heap_pointer[IndexVect1(x,y,z)] = pointer-1;
241
242 }
243
244
245 void UpdateHeap(int x, int y, int z, float dist)
246 {
247 int pointer, parent;
248 int left, right;
249 char up = 0;
250
251
252 pointer=heap_pointer[IndexVect1(x,y,z)]+1;
253
254 // checking the upper elements
255 while (pointer > 1) {
256 if (pointer%2 == 0) {
257 parent=pointer/2;
258 if (dist < min_heap->dist[parent-1]) {
259 up = 1;
260 min_heap->x[pointer-1]=min_heap->x[parent-1];
261 min_heap->y[pointer-1]=min_heap->y[parent-1];
262 min_heap->z[pointer-1]=min_heap->z[parent-1];
263 min_heap->seed[pointer-1]=min_heap->seed[parent-1];
264 min_heap->dist[pointer-1]=min_heap->dist[parent-1];
265 heap_pointer[IndexVect1(min_heap->x[pointer-1],min_heap->y[pointer-1],min_heap->z[pointer-1])] = pointer-1;
266 pointer=parent;
267 }
268 else break;
269 }
270 else if (pointer%2 == 1){
271 parent=(pointer-1)/2;
272 if (dist < min_heap->dist[parent-1]) {
273 up = 1;
274 min_heap->x[pointer-1]=min_heap->x[parent-1];
275 min_heap->y[pointer-1]=min_heap->y[parent-1];
276 min_heap->z[pointer-1]=min_heap->z[parent-1];
277 min_heap->seed[pointer-1]=min_heap->seed[parent-1];
278 min_heap->dist[pointer-1]=min_heap->dist[parent-1];
279 heap_pointer[IndexVect1(min_heap->x[pointer-1],min_heap->y[pointer-1],min_heap->z[pointer-1])] = pointer-1;
280 pointer=parent;
281 }
282 else break;
283 }
284 }
285
286 if (up == 0) {
287 // checking the lower elements
288 while (pointer <= min_heap->size/2) {
289 left=2*pointer;
290 right=2*pointer+1;
291 if ((min_heap->dist[left-1] <= min_heap->dist[right-1]) && (min_heap->dist[left-1] < dist)) {
292 min_heap->x[pointer-1]=min_heap->x[left-1];
293 min_heap->y[pointer-1]=min_heap->y[left-1];
294 min_heap->z[pointer-1]=min_heap->z[left-1];
295 min_heap->seed[pointer-1]=min_heap->seed[left-1];
296 min_heap->dist[pointer-1]=min_heap->dist[left-1];
297 heap_pointer[IndexVect1(min_heap->x[pointer-1],min_heap->y[pointer-1],min_heap->z[pointer-1])] = pointer-1;
298 pointer=left;
299 }
300 else if ((min_heap->dist[left-1] > min_heap->dist[right-1]) && (min_heap->dist[right-1] < dist)){
301 min_heap->x[pointer-1]=min_heap->x[right-1];
302 min_heap->y[pointer-1]=min_heap->y[right-1];
303 min_heap->z[pointer-1]=min_heap->z[right-1];
304 min_heap->seed[pointer-1]=min_heap->seed[right-1];
305 min_heap->dist[pointer-1]=min_heap->dist[right-1];
306 heap_pointer[IndexVect1(min_heap->x[pointer-1],min_heap->y[pointer-1],min_heap->z[pointer-1])] = pointer-1;
307 pointer=right;
308 }
309 else break;
310 }
311 }
312
313
314 min_heap->x[pointer-1]=x;
315 min_heap->y[pointer-1]=y;
316 min_heap->z[pointer-1]=z;
317 min_heap->seed[pointer-1]=min_seed;
318 min_heap->dist[pointer-1]=dist;
319
320 heap_pointer[IndexVect1(x,y,z)] = pointer-1;
321
322 }
323
324
325
326 void Marching(void)
327 {
328 int tempt_x, tempt_y, tempt_z;
329 float dt,dist;
330 int neighbor,seed;
331 char boundary;
332 float min_seedx, min_seedy, min_seedz;
333 float seedx, seedy, seedz;
334
335
336 min_seedx = AllSeeds[min_seed].seedx;
337 min_seedy = AllSeeds[min_seed].seedy;
338 min_seedz = AllSeeds[min_seed].seedz;
339
340 boundary = 0;
341
342
343 /* ============================ */
344 tempt_x=max(min_x-1,0);
345 tempt_y=min_y;
346 tempt_z=min_z;
347 if (heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)] == MaxVal) {
348 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx)+(tempt_y-min_seedy)*(tempt_y-min_seedy)+(tempt_z-min_seedz)*(tempt_z-min_seedz);
349 if (dist <= threshold)
350 InsertHeap(tempt_x, tempt_y, tempt_z, dist);
351 else
352 boundary = 1;
353 }
354 else if (heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)] > -1) {
355 neighbor = heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)];
356 dt = min_heap->dist[neighbor];
357 if (dt < MaxDist) {
358 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx) + (tempt_y-min_seedy)*(tempt_y-min_seedy) + (tempt_z-min_seedz)*(tempt_z-min_seedz);
359 if (dist < dt)
360 UpdateHeap(tempt_x, tempt_y, tempt_z, dist);
361 }
362 else {
363 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx) + (tempt_y-min_seedy)*(tempt_y-min_seedy) + (tempt_z-min_seedz)*(tempt_z-min_seedz);
364 seed = min_heap->seed[neighbor];
365 seedx = AllSeeds[seed].seedx;
366 seedy = AllSeeds[seed].seedy;
367 seedz = AllSeeds[seed].seedz;
368 if (dist < (tempt_x-seedx)*(tempt_x-seedx)+(tempt_y-seedy)*(tempt_y-seedy)+(tempt_z-seedz)*(tempt_z-seedz))
369 UpdateHeap(tempt_x, tempt_y, tempt_z, MaxDist);
370 }
371 }
372 //else if (heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)] > -11) {
373
374
375 /* ============================ */
376 tempt_x=min(min_x+1,xdim1-1);
377 tempt_y=min_y;
378 tempt_z=min_z;
379 if (heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)] == MaxVal) {
380 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx)+(tempt_y-min_seedy)*(tempt_y-min_seedy)+(tempt_z-min_seedz)*(tempt_z-min_seedz);
381 if (dist <= threshold)
382 InsertHeap(tempt_x, tempt_y, tempt_z, dist);
383 else
384 boundary = 1;
385 }
386 else if (heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)] > -1) {
387 neighbor = heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)];
388 dt = min_heap->dist[neighbor];
389 if (dt < MaxDist) {
390 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx) + (tempt_y-min_seedy)*(tempt_y-min_seedy) + (tempt_z-min_seedz)*(tempt_z-min_seedz);
391 if (dist < dt)
392 UpdateHeap(tempt_x, tempt_y, tempt_z, dist);
393 }
394 else {
395 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx) + (tempt_y-min_seedy)*(tempt_y-min_seedy) + (tempt_z-min_seedz)*(tempt_z-min_seedz);
396 seed = min_heap->seed[neighbor];
397 seedx = AllSeeds[seed].seedx;
398 seedy = AllSeeds[seed].seedy;
399 seedz = AllSeeds[seed].seedz;
400 if (dist < (tempt_x-seedx)*(tempt_x-seedx)+(tempt_y-seedy)*(tempt_y-seedy)+(tempt_z-seedz)*(tempt_z-seedz))
401 UpdateHeap(tempt_x, tempt_y, tempt_z, MaxDist);
402 }
403 }
404
405
406 /* ============================ */
407 tempt_y=max(min_y-1,0);
408 tempt_x=min_x;
409 tempt_z=min_z;
410 if (heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)] == MaxVal) {
411 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx)+(tempt_y-min_seedy)*(tempt_y-min_seedy)+(tempt_z-min_seedz)*(tempt_z-min_seedz);
412 if (dist <= threshold)
413 InsertHeap(tempt_x, tempt_y, tempt_z, dist);
414 else
415 boundary = 1;
416 }
417 else if (heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)] > -1) {
418 neighbor = heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)];
419 dt = min_heap->dist[neighbor];
420 if (dt < MaxDist) {
421 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx) + (tempt_y-min_seedy)*(tempt_y-min_seedy) + (tempt_z-min_seedz)*(tempt_z-min_seedz);
422 if (dist < dt)
423 UpdateHeap(tempt_x, tempt_y, tempt_z, dist);
424 }
425 else {
426 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx) + (tempt_y-min_seedy)*(tempt_y-min_seedy) + (tempt_z-min_seedz)*(tempt_z-min_seedz);
427 seed = min_heap->seed[neighbor];
428 seedx = AllSeeds[seed].seedx;
429 seedy = AllSeeds[seed].seedy;
430 seedz = AllSeeds[seed].seedz;
431 if (dist < (tempt_x-seedx)*(tempt_x-seedx)+(tempt_y-seedy)*(tempt_y-seedy)+(tempt_z-seedz)*(tempt_z-seedz))
432 UpdateHeap(tempt_x, tempt_y, tempt_z, MaxDist);
433 }
434 }
435
436
437 /* ============================ */
438 tempt_y=min(min_y+1,ydim1-1);
439 tempt_x=min_x;
440 tempt_z=min_z;
441 if (heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)] == MaxVal) {
442 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx)+(tempt_y-min_seedy)*(tempt_y-min_seedy)+(tempt_z-min_seedz)*(tempt_z-min_seedz);
443 if (dist <= threshold)
444 InsertHeap(tempt_x, tempt_y, tempt_z, dist);
445 else
446 boundary = 1;
447 }
448 else if (heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)] > -1) {
449 neighbor = heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)];
450 dt = min_heap->dist[neighbor];
451 if (dt < MaxDist) {
452 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx) + (tempt_y-min_seedy)*(tempt_y-min_seedy) + (tempt_z-min_seedz)*(tempt_z-min_seedz);
453 if (dist < dt)
454 UpdateHeap(tempt_x, tempt_y, tempt_z, dist);
455 }
456 else {
457 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx) + (tempt_y-min_seedy)*(tempt_y-min_seedy) + (tempt_z-min_seedz)*(tempt_z-min_seedz);
458 seed = min_heap->seed[neighbor];
459 seedx = AllSeeds[seed].seedx;
460 seedy = AllSeeds[seed].seedy;
461 seedz = AllSeeds[seed].seedz;
462 if (dist < (tempt_x-seedx)*(tempt_x-seedx)+(tempt_y-seedy)*(tempt_y-seedy)+(tempt_z-seedz)*(tempt_z-seedz))
463 UpdateHeap(tempt_x, tempt_y, tempt_z, MaxDist);
464 }
465 }
466
467
468 /* ============================ */
469 tempt_z=max(min_z-1,0);
470 tempt_x=min_x;
471 tempt_y=min_y;
472 if (heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)] == MaxVal) {
473 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx)+(tempt_y-min_seedy)*(tempt_y-min_seedy)+(tempt_z-min_seedz)*(tempt_z-min_seedz);
474 if (dist <= threshold)
475 InsertHeap(tempt_x, tempt_y, tempt_z, dist);
476 else
477 boundary = 1;
478 }
479 else if (heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)] > -1) {
480 neighbor = heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)];
481 dt = min_heap->dist[neighbor];
482 if (dt < MaxDist) {
483 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx) + (tempt_y-min_seedy)*(tempt_y-min_seedy) + (tempt_z-min_seedz)*(tempt_z-min_seedz);
484 if (dist < dt)
485 UpdateHeap(tempt_x, tempt_y, tempt_z, dist);
486 }
487 else {
488 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx) + (tempt_y-min_seedy)*(tempt_y-min_seedy) + (tempt_z-min_seedz)*(tempt_z-min_seedz);
489 seed = min_heap->seed[neighbor];
490 seedx = AllSeeds[seed].seedx;
491 seedy = AllSeeds[seed].seedy;
492 seedz = AllSeeds[seed].seedz;
493 if (dist < (tempt_x-seedx)*(tempt_x-seedx)+(tempt_y-seedy)*(tempt_y-seedy)+(tempt_z-seedz)*(tempt_z-seedz))
494 UpdateHeap(tempt_x, tempt_y, tempt_z, MaxDist);
495 }
496 }
497
498
499 /* ============================ */
500 tempt_z=min(min_z+1,zdim1-1);
501 tempt_x=min_x;
502 tempt_y=min_y;
503 if (heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)] == MaxVal) {
504 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx)+(tempt_y-min_seedy)*(tempt_y-min_seedy)+(tempt_z-min_seedz)*(tempt_z-min_seedz);
505 if (dist <= threshold)
506 InsertHeap(tempt_x, tempt_y, tempt_z, dist);
507 else
508 boundary = 1;
509 }
510 else if (heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)] > -1) {
511 neighbor = heap_pointer[IndexVect1(tempt_x,tempt_y,tempt_z)];
512 dt = min_heap->dist[neighbor];
513 if (dt < MaxDist) {
514 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx) + (tempt_y-min_seedy)*(tempt_y-min_seedy) + (tempt_z-min_seedz)*(tempt_z-min_seedz);
515 if (dist < dt)
516 UpdateHeap(tempt_x, tempt_y, tempt_z, dist);
517 }
518 else {
519 dist = (tempt_x-min_seedx)*(tempt_x-min_seedx) + (tempt_y-min_seedy)*(tempt_y-min_seedy) + (tempt_z-min_seedz)*(tempt_z-min_seedz);
520 seed = min_heap->seed[neighbor];
521 seedx = AllSeeds[seed].seedx;
522 seedy = AllSeeds[seed].seedy;
523 seedz = AllSeeds[seed].seedz;
524 if (dist < (tempt_x-seedx)*(tempt_x-seedx)+(tempt_y-seedy)*(tempt_y-seedy)+(tempt_z-seedz)*(tempt_z-seedz))
525 UpdateHeap(tempt_x, tempt_y, tempt_z, MaxDist);
526 }
527 }
528
529
530 if (boundary)
531 InsertHeap(min_x,min_y,min_z, MaxDist);
532
533 }
534
535
536
537 FLTVECT FindSeed(float x, float y, float z, int index)
538 {
539 double cx1,cy1,cz1;
540 double cx2,cy2,cz2;
541 double cx3,cy3,cz3;
542 double dist,radius1,radius2,radius3;
543 double cos_alpha;
544 double ax,ay,az;
545 double bx,by,bz;
546 double dx,dy,dz;
547 double hx,hy,hz;
548 int atom1,atom2,atom3;
549 int num,total;
550 FLTVECT tmp;
551
552
553 // AllSeeds[index].atom[0] is always >= 0
554 atom1 = AllSeeds[index].atom[0];
555 atom2 = AllSeeds[index].atom[1];
556 atom3 = AllSeeds[index].atom[2];
557
558 // contain only one atom
559 if (atom2 < 0) {
560 radius1 = atom_list[atom1].radius;
561 cx1 = atom_list[atom1].x;
562 cy1 = atom_list[atom1].y;
563 cz1 = atom_list[atom1].z;
564 dx = x-cx1;
565 dy = y-cy1;
566 dz = z-cz1;
567 dist = sqrt(dx*dx + dy*dy + dz*dz);
568
569 tmp.x = cx1 + radius1*dx/dist;
570 tmp.y = cy1 + radius1*dy/dist;
571 tmp.z = cz1 + radius1*dz/dist;
572 }
573 // contain only two atoms
574 else if (atom3 < 0) {
575 radius1 = atom_list[atom1].radius;
576 cx1 = atom_list[atom1].x;
577 cy1 = atom_list[atom1].y;
578 cz1 = atom_list[atom1].z;
579 radius2 = atom_list[atom2].radius;
580 cx2 = atom_list[atom2].x;
581 cy2 = atom_list[atom2].y;
582 cz2 = atom_list[atom2].z;
583 dist = sqrt((cx2-cx1)*(cx2-cx1)+(cy2-cy1)*(cy2-cy1)+(cz2-cz1)*(cz2-cz1));
584 cos_alpha = (radius1*radius1+dist*dist-radius2*radius2)/(2.0*radius1*dist);
585
586 ax = (cx2 - cx1)/dist;
587 ay = (cy2 - cy1)/dist;
588 az = (cz2 - cz1)/dist;
589 bx = x-cx1;
590 by = y-cy1;
591 bz = z-cz1;
592 dx = ay*bz - az*by;
593 dy = az*bx - ax*bz;
594 dz = ax*by - ay*bx;
595 hx = dy*az - dz*ay;
596 hy = dz*ax - dx*az;
597 hz = dx*ay - dy*ax;
598 dist = sqrt(hx*hx+hy*hy+hz*hz);
599 hx /= dist;
600 hy /= dist;
601 hz /= dist;
602 dist = radius1*sqrt(1.0-cos_alpha*cos_alpha);
603
604 radius1 *= cos_alpha;
605 tmp.x = radius1*ax+cx1 + dist*hx;
606 tmp.y = radius1*ay+cy1 + dist*hy;
607 tmp.z = radius1*az+cz1 + dist*hz;
608 }
609 // contain three or more atoms
610 else {
611 hx = 0;
612 hy = 0;
613 hz = 0;
614 total = 0;
615 num = 2;
616 while (num < MaxAtom) {
617 if (AllSeeds[index].atom[num] < 0)
618 break;
619 atom1 = AllSeeds[index].atom[num-2];
620 atom2 = AllSeeds[index].atom[num-1];
621 atom3 = AllSeeds[index].atom[num];
622
623 radius1 = atom_list[atom1].radius;
624 cx1 = atom_list[atom1].x;
625 cy1 = atom_list[atom1].y;
626 cz1 = atom_list[atom1].z;
627 radius2 = atom_list[atom2].radius;
628 cx2 = atom_list[atom2].x;
629 cy2 = atom_list[atom2].y;
630 cz2 = atom_list[atom2].z;
631 radius3 = atom_list[atom3].radius;
632 cx3 = atom_list[atom3].x;
633 cy3 = atom_list[atom3].y;
634 cz3 = atom_list[atom3].z;
635
636 ax = x;
637 ay = y;
638 az = z;
639 bx = ax;
640 by = ay;
641 bz = az;
642 while (1) {
643 // projection to the first atom sphere
644 dx = ax-cx1;
645 dy = ay-cy1;
646 dz = az-cz1;
647 dist = radius1/sqrt(dx*dx+dy*dy+dz*dz);
648 ax = cx1 + dist*dx;
649 ay = cy1 + dist*dy;
650 az = cz1 + dist*dz;
651 // projection to the second atom sphere
652 dx = ax-cx2;
653 dy = ay-cy2;
654 dz = az-cz2;
655 dist = radius2/sqrt(dx*dx+dy*dy+dz*dz);
656 ax = cx2 + dist*dx;
657 ay = cy2 + dist*dy;
658 az = cz2 + dist*dz;
659 // projection to the third atom sphere
660 dx = ax-cx3;
661 dy = ay-cy3;
662 dz = az-cz3;
663 dist = radius3/sqrt(dx*dx+dy*dy+dz*dz);
664 ax = cx3 + dist*dx;
665 ay = cy3 + dist*dy;
666 az = cz3 + dist*dz;
667
668 dist = sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by)+(az-bz)*(az-bz));
669 if (dist < 0.001) {
670 hx += ax;
671 hy += ay;
672 hz += az;
673 total++;
674 break;
675 }
676 else {
677 bx = ax;
678 by = ay;
679 bz = az;
680 }
681 }
682 num++;
683 }
684 tmp.x = hx/(float)total;
685 tmp.y = hy/(float)total;
686 tmp.z = hz/(float)total;
687 }
688
689 return (tmp);
690 }
691