2
* Copyright (C) 2004-2007 The Chemistry Development Kit (CDK) project
4
* Contact: cdk-devel@lists.sourceforge.net
6
* This program is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Lesser General Public License
8
* as published by the Free Software Foundation; either version 2.1
9
* of the License, or (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
package org.openscience.cdk.geometry.surface;
23
import java.util.ArrayList;
24
import java.util.HashMap;
26
import org.openscience.cdk.interfaces.IAtom;
29
* Creates a list of atoms neighboring each atom in the molecule.
31
* <p>The routine is a simplified version of the neighbor list described
32
* in {@cdk.cite EIS95} and is based on the implementation by Peter McCluskey.
33
* Due to the fact that it divides the cube into a fixed number of sub cubes,
34
* some accuracy may be lost.
36
* @author Rajarshi Guha
37
* @cdk.created 2005-05-09
40
public class NeighborList {
46
public NeighborList(IAtom[] atoms, double radius) {
48
this.boxes = new HashMap();
49
this.box_size = 2 * radius;
50
for (int i = 0; i < atoms.length; i++) {
51
String key = getKeyString(atoms[i]);
53
if (this.boxes.containsKey(key)) {
54
ArrayList arl = (ArrayList)this.boxes.get(key);
55
arl.add( new Integer(i) );
56
this.boxes.put( key, arl );
58
this.boxes.put( key, new ArrayList() );
63
private String getKeyString(IAtom atom) {
64
double x = atom.getPoint3d().x;
65
double y = atom.getPoint3d().y;
66
double z = atom.getPoint3d().z;
69
k1 = (int)(Math.floor(x/box_size));
70
k2 = (int)(Math.floor(y/box_size));
71
k3 = (int)(Math.floor(z/box_size));
74
Integer.toString(k1) + " " +
75
Integer.toString(k2) + " " +
76
Integer.toString(k3) + " " ;
79
private int[] getKeyArray(IAtom atom) {
80
double x = atom.getPoint3d().x;
81
double y = atom.getPoint3d().y;
82
double z = atom.getPoint3d().z;
85
k1 = (int)(Math.floor(x/box_size));
86
k2 = (int)(Math.floor(y/box_size));
87
k3 = (int)(Math.floor(z/box_size));
89
int[] ret = { k1, k2, k3 };
94
public int getNumberOfNeighbors(int i) {
95
return getNeighbors(i).length;
98
public int[] getNeighbors(int ii) {
99
double max_dist_2 = this.box_size*this.box_size;
101
IAtom ai = this.atoms[ii];
102
int[] key = getKeyArray(ai);
103
ArrayList nlist = new ArrayList();
105
int[] bval = {-1,0,1};
106
for (int i = 0; i < bval.length; i++) {
108
for (int j = 0; j < bval.length; j++) {
110
for (int k = 0; k < bval.length; k++) {
114
Integer.toString(key[0]+x) + " " +
115
Integer.toString(key[1]+y) + " " +
116
Integer.toString(key[2]+z) + " " ;
117
if (boxes.containsKey(keyj)) {
118
ArrayList nbrs = (ArrayList)boxes.get(keyj);
119
for (int l = 0; l < nbrs.size(); l++) {
120
int i2 = ((Integer)nbrs.get(l)).intValue();
122
IAtom aj = atoms[i2];
123
double x12 = aj.getPoint3d().x - ai.getPoint3d().x;
124
double y12 = aj.getPoint3d().y - ai.getPoint3d().y;
125
double z12 = aj.getPoint3d().z - ai.getPoint3d().z;
126
double d2 = x12*x12 + y12*y12 + z12*z12;
127
if (d2 < max_dist_2) nlist.add( new Integer(i2) );
134
Object[] tmp = nlist.toArray();
135
int[] ret = new int[ tmp.length ];
136
for (int j = 0; j < tmp.length; j++) ret[j] = ((Integer)tmp[j]).intValue();