~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/geometry/surface/NeighborList.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2004-2007  The Chemistry Development Kit (CDK) project
 
3
 * 
 
4
 * Contact: cdk-devel@lists.sourceforge.net
 
5
 *
 
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.
 
10
 * 
 
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.
 
15
 * 
 
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. 
 
19
 */
 
20
 
 
21
package org.openscience.cdk.geometry.surface;
 
22
 
 
23
import java.util.ArrayList;
 
24
import java.util.HashMap;
 
25
 
 
26
import org.openscience.cdk.interfaces.IAtom;
 
27
 
 
28
/**
 
29
 * Creates a list of atoms neighboring each atom in the molecule.
 
30
 *
 
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.
 
35
 *
 
36
 * @author Rajarshi Guha
 
37
 * @cdk.created 2005-05-09
 
38
 * @cdk.module extra
 
39
 */
 
40
public class NeighborList {
 
41
    HashMap boxes;
 
42
    double box_size;
 
43
    IAtom[] atoms;
 
44
 
 
45
 
 
46
    public NeighborList(IAtom[] atoms, double radius) {
 
47
        this.atoms = atoms;
 
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]);
 
52
 
 
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 );
 
57
            } else {
 
58
                this.boxes.put( key, new ArrayList() );
 
59
            }
 
60
        }
 
61
    }
 
62
 
 
63
    private String getKeyString(IAtom atom) {
 
64
        double x = atom.getPoint3d().x;
 
65
        double y = atom.getPoint3d().y;
 
66
        double z = atom.getPoint3d().z;
 
67
 
 
68
        int k1,k2,k3;
 
69
        k1 = (int)(Math.floor(x/box_size));
 
70
        k2 = (int)(Math.floor(y/box_size));
 
71
        k3 = (int)(Math.floor(z/box_size));
 
72
 
 
73
        String key = 
 
74
            Integer.toString(k1) + " " + 
 
75
            Integer.toString(k2) + " " +
 
76
            Integer.toString(k3) + " " ;
 
77
        return(key);
 
78
    }
 
79
    private int[] getKeyArray(IAtom atom) {
 
80
        double x = atom.getPoint3d().x;
 
81
        double y = atom.getPoint3d().y;
 
82
        double z = atom.getPoint3d().z;
 
83
 
 
84
        int k1,k2,k3;
 
85
        k1 = (int)(Math.floor(x/box_size));
 
86
        k2 = (int)(Math.floor(y/box_size));
 
87
        k3 = (int)(Math.floor(z/box_size));
 
88
 
 
89
        int[] ret = { k1, k2, k3 };
 
90
        return(ret);
 
91
    }
 
92
 
 
93
 
 
94
    public int getNumberOfNeighbors(int i) {
 
95
        return getNeighbors(i).length;
 
96
    }
 
97
 
 
98
    public int[] getNeighbors(int ii) {
 
99
        double max_dist_2 = this.box_size*this.box_size;
 
100
 
 
101
        IAtom ai = this.atoms[ii];
 
102
        int[] key = getKeyArray(ai);
 
103
        ArrayList nlist = new ArrayList();
 
104
 
 
105
        int[] bval = {-1,0,1};
 
106
        for (int i = 0; i < bval.length; i++) {
 
107
            int x = bval[i];
 
108
            for (int j = 0; j < bval.length; j++) {
 
109
                int y = bval[j];
 
110
                for (int k = 0; k < bval.length; k++) {
 
111
                    int z = bval[k];
 
112
 
 
113
                    String keyj = 
 
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();
 
121
                            if (i2 != ii) {
 
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) );
 
128
                            }
 
129
                        }
 
130
                    }
 
131
                }
 
132
            }
 
133
        }
 
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();
 
137
        return(ret);
 
138
    }
 
139
}
 
140
 
 
141