From 50ebfa1b321b62aa8d671b38e62685fccc311434 Mon Sep 17 00:00:00 2001 From: Egon Willighagen Date: Wed, 21 Oct 2009 20:24:35 +0200 Subject: [PATCH] Reimplemented shiftContainer(IAtomContainer, Rectangle2D, Rectange2D, 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 --- .../cdk/geometry/GeometryTools.java | 30 ++++++++ .../cdk/geometry/GeometryToolsTest.java | 76 +++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/src/main/org/openscience/cdk/geometry/GeometryTools.java b/src/main/org/openscience/cdk/geometry/GeometryTools.java index 3459373f158..490fa39d6b4 100644 --- a/src/main/org/openscience/cdk/geometry/GeometryTools.java +++ b/src/main/org/openscience/cdk/geometry/GeometryTools.java @@ -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; + } + } } diff --git a/src/test/org/openscience/cdk/geometry/GeometryToolsTest.java b/src/test/org/openscience/cdk/geometry/GeometryToolsTest.java index fa4c1ae66e8..59ecde5fdce 100644 --- a/src/test/org/openscience/cdk/geometry/GeometryToolsTest.java +++ b/src/test/org/openscience/cdk/geometry/GeometryToolsTest.java @@ -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 + ); + } + } }