Skip to content

Commit

Permalink
Reimplemented shiftContainer(IAtomContainer, Rectangle2D, Rectange2D,…
Browse files Browse the repository at this point in the history
… double) originally implemented as jchempaint-primary patch 9200bdc4d68dc8f70373a62eaec51357b680d5e6 by Stefan Kuhn: fixing the detection of overlap, and added missing unit tests

Signed-off-by: Rajarshi Guha <rajarshi.guha@gmail.com>
  • Loading branch information
egonw authored and rajarshi committed Jan 14, 2010
1 parent 84a44e0 commit 50ebfa1
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/main/org/openscience/cdk/geometry/GeometryTools.java
Expand Up @@ -1426,4 +1426,34 @@ public static double getBondLengthAverage3D(IAtomContainer container) {
}
return bondLengthSum / bondCounter;
}

/**
* Shift the container horizontally to the right to make its bounds not
* overlap with the other bounds.
*
* @param container the {@link IAtomContainer} to shift to the right
* @param bounds the {@link Rectangle2D} of the {@link IAtomContainer}
* to shift
* @param last the bounds that is used as reference
* @param gap the gap between the two {@link Rectangle2D}s
* @return the {@link Rectangle2D} of the {@link IAtomContainer}
* after the shift
*/
public static Rectangle2D shiftContainer(
IAtomContainer container, Rectangle2D bounds, Rectangle2D last,
double gap) {
// determine if the containers are overlapping
if (last.getMaxX() + gap >= bounds.getMinX()) {
double xShift = bounds.getWidth() + last.getWidth() + gap;
Vector2d shift = new Vector2d(xShift, 0.0);
GeometryTools.translate2D(container, shift);
return new Rectangle2D.Double(bounds.getX() + xShift,
bounds.getY(),
bounds.getWidth(),
bounds.getHeight());
} else {
// the containers are not overlapping
return bounds;
}
}
}
76 changes: 76 additions & 0 deletions src/test/org/openscience/cdk/geometry/GeometryToolsTest.java
Expand Up @@ -377,5 +377,81 @@ public class GeometryToolsTest extends CDKTestCase {
Assert.assertEquals(GeometryTools.getLength2D(bond),2.23,0.01);
}


@Test public void
testShiftContainerHorizontal_IAtomContainer_Rectangle2D_Rectangle2D_double()
throws Exception {
IAtom atom1 = new Atom("C");
atom1.setPoint2d(new Point2d(0,1));
IAtom atom2 = new Atom("C");
atom2.setPoint2d(new Point2d(1,0));
IMolecule react1 = new Molecule();
react1.addAtom(atom1);
react1.addAtom(atom2);
IMolecule react2 = (IMolecule)react1.clone();

// shift the second molecule right
GeometryTools.shiftContainer(
react2,
GeometryTools.getRectangle2D(react2),
GeometryTools.getRectangle2D(react1),
1.0
);
// assert all coordinates of the second molecule moved right
AtomContainerDiff.diff(react1, react2);
for (int i=0; i<2; i++) {
atom1 = react1.getAtom(0);
atom2 = react2.getAtom(0);
// so, y coordinates should be the same
Assert.assertEquals(
atom1.getPoint2d().y, atom2.getPoint2d().y, 0.0
);
// but, x coordinates should not
Assert.assertTrue(
atom1.getPoint2d().x < atom2.getPoint2d().x
);
}
}

/**
* Unit tests that tests the situation where two vertical two-atom
* molecules are with the same x coordinates.
*
* @throws Exception Thrown when the cloning failed.
*/
@Test
public void testShiftContainerHorizontal_Two_vertical_molecules()
throws Exception {
IAtom atom1 = new Atom("C");
atom1.setPoint2d(new Point2d(0,0));
IAtom atom2 = new Atom("C");
atom2.setPoint2d(new Point2d(0,1));
IMolecule react1 = new Molecule();
react1.addAtom(atom1);
react1.addAtom(atom2);
IMolecule react2 = (IMolecule)react1.clone();

// shift the second molecule right
GeometryTools.shiftContainer(
react2,
GeometryTools.getRectangle2D(react2),
GeometryTools.getRectangle2D(react1),
1.0
);
// assert all coordinates of the second molecule moved right
AtomContainerDiff.diff(react1, react2);
for (int i=0; i<2; i++) {
atom1 = react1.getAtom(0);
atom2 = react2.getAtom(0);
// so, y coordinates should be the same
Assert.assertEquals(
atom1.getPoint2d().y, atom2.getPoint2d().y, 0.0
);
// but, x coordinates should not
Assert.assertTrue(
atom1.getPoint2d().x < atom2.getPoint2d().x
);
}
}
}

0 comments on commit 50ebfa1

Please sign in to comment.