$darkmode
DENOPTIM
MoleculeUtilsTest.java
Go to the documentation of this file.
1package denoptim.utils;
2
3/*
4 * DENOPTIM
5 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no>
6 * and Marco Foscato <marco.foscato@uib.no>
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published
10 * by the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21import static denoptim.utils.MoleculeUtils.getPoint3d;
22import static org.junit.jupiter.api.Assertions.assertFalse;
23import static org.junit.jupiter.api.Assertions.assertTrue;
24
25import javax.vecmath.Point2d;
26import javax.vecmath.Point3d;
27
28import org.junit.jupiter.api.Test;
29import org.openscience.cdk.Atom;
30import org.openscience.cdk.interfaces.IAtom;
31import org.openscience.cdk.interfaces.IAtomContainer;
32import org.openscience.cdk.interfaces.IChemObjectBuilder;
33import org.openscience.cdk.silent.SilentChemObjectBuilder;
34
42{
43 private IChemObjectBuilder builder = SilentChemObjectBuilder.getInstance();
44
45//------------------------------------------------------------------------------
46
47 @Test
48 public void testGetPoint3d() throws Exception
49 {
50 IAtom a = new Atom();
51 assertTrue(areCloseEnough(getPoint3d(a).x,0.0));
52 assertTrue(areCloseEnough(getPoint3d(a).y,0.0));
53 assertTrue(areCloseEnough(getPoint3d(a).z,0.0));
54
55 a.setPoint2d(new Point2d(2.6, -4.2));
56 assertTrue(areCloseEnough(getPoint3d(a).x,2.6));
57 assertTrue(areCloseEnough(getPoint3d(a).y,-4.2));
58 assertTrue(areCloseEnough(getPoint3d(a).z,0.0));
59
60 a.setPoint3d(new Point3d(2.6, -4.2, 6.4));
61 assertTrue(areCloseEnough(getPoint3d(a).x,2.6));
62 assertTrue(areCloseEnough(getPoint3d(a).y,-4.2));
63 assertTrue(areCloseEnough(getPoint3d(a).z,6.4));
64
65 assertFalse(areCloseEnough(1.00001, 1.00002));
66 }
67
68//------------------------------------------------------------------------------
69
70 private boolean areCloseEnough(double a, double b)
71 {
72 double delta = 0.0000001;
73 return Math.abs(a-b) <= delta;
74 }
75
76//------------------------------------------------------------------------------
77
78 @Test
79 public void testCalculateCentroid() throws Exception
80 {
81 IAtomContainer mol = builder.newAtomContainer();
82 mol.addAtom(new Atom("H", new Point3d(1.2,-2.9,2.5)));
83 mol.addAtom(new Atom("H", new Point3d(2.3,-4.8,4.2)));
84 mol.addAtom(new Atom("H", new Point3d(4.5,2.6,-5.4)));
85
86 Point3d c = MoleculeUtils.calculateCentroid(mol);
87 assertTrue(areCloseEnough(2.6666666,c.x), "Wrong value in X");
88 assertTrue(areCloseEnough(-1.7,c.y), "Wrong value in Y");
89 assertTrue(areCloseEnough(0.4333333,c.z), "Wrong value in Z");
90 }
91
92//------------------------------------------------------------------------------
93
94}
Utilities for molecule conversion.
static Point3d calculateCentroid(IAtomContainer mol)
Calculated the centroid of the given molecule.
Unit test for DENOPTIMMoleculeUtils.
boolean areCloseEnough(double a, double b)