Skip to content

Commit

Permalink
Linear placement of cumulated atoms.
Browse files Browse the repository at this point in the history
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed May 2, 2014
1 parent 593a106 commit 3a9104c
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tool/sdg/src/main/java/org/openscience/cdk/layout/AtomPlacer.java
Expand Up @@ -38,6 +38,7 @@
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.annotations.TestClass;
import org.openscience.cdk.annotations.TestMethod;
import org.openscience.cdk.config.Elements;
import org.openscience.cdk.exception.CDKException;
import org.openscience.cdk.geometry.BondTools;
import org.openscience.cdk.geometry.GeometryTools;
Expand Down Expand Up @@ -364,17 +365,41 @@ public void placeLinearChain(IAtomContainer atomContainer, Vector2d initialBondV
IAtom atom = null;
Point2d atomPoint = null;
IAtom nextAtom = null;
IBond prevBond = null, currBond = null;
for (int f = 0; f < atomContainer.getAtomCount() - 1; f++)
{
atom = atomContainer.getAtom(f);
nextAtom = atomContainer.getAtom(f + 1);
currBond = atomContainer.getBond(atom, nextAtom);
atomPoint = new Point2d(atom.getPoint2d());
bondVector.normalize();
bondVector.scale(bondLength);
atomPoint.add(bondVector);
nextAtom.setPoint2d(atomPoint);
nextAtom.setFlag(CDKConstants.ISPLACED, true);
boolean trans=false;

// quick fix to handle geometry of cumulated, CC=C=CC, ideally it
// would be useful to know the geometries
if (prevBond != null
&& IBond.Order.DOUBLE.equals(prevBond.getOrder())
&& IBond.Order.DOUBLE.equals(currBond.getOrder())) {

int atomicNumber = atom.getAtomicNumber();
int charge = atom.getFormalCharge();

if (charge == 0
&& (Elements.Carbon.number() == atomicNumber
|| Elements.Germanium.number() == atomicNumber
|| Elements.Silicon.number() == atomicNumber)) {

// double length of the last bond to determing next placement
Point2d p = new Point2d(prevBond.getConnectedAtom(atom).getPoint2d());
p.interpolate(atom.getPoint2d(), 2);
nextAtom.setPoint2d(p);
}
}

if(GeometryTools.has2DCoordinates(atomContainer)){
try{
if(f>2 && BondTools.isValidDoubleBondConfiguration(withh,withh.getBond(withh.getAtom(f-2),withh.getAtom(f-1)))){
Expand All @@ -387,6 +412,8 @@ public void placeLinearChain(IAtomContainer atomContainer, Vector2d initialBondV
}else{
bondVector = getNextBondVector(nextAtom, atom, GeometryTools.get2DCenter(molecule),true);
}

prevBond = currBond;
}

// BUGFIX part 2 - restore hydrogen counts
Expand Down
Expand Up @@ -26,12 +26,20 @@
import java.util.List;

import javax.vecmath.Point2d;
import javax.vecmath.Vector2d;

import org.junit.Assert;
import org.junit.Test;
import org.openscience.cdk.Atom;
import org.openscience.cdk.CDKConstants;
import org.openscience.cdk.CDKTestCase;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IBond;
import org.openscience.cdk.silent.AtomContainer;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.closeTo;

/**
* @author maclean
Expand Down Expand Up @@ -67,5 +75,93 @@ public void triangleTest() {
Assert.assertNotNull(atom.getPoint2d());
}
}

@Test public void cumulated_x2() {
IAtomContainer m = new AtomContainer(5, 4, 0, 0);
m.addAtom(atom("C", 3));
m.addAtom(atom("C", 1));
m.addAtom(atom("C", 0));
m.addAtom(atom("C", 1));
m.addAtom(atom("C", 3));
m.addBond(0, 1, IBond.Order.SINGLE);
m.addBond(1, 2, IBond.Order.DOUBLE);
m.addBond(2, 3, IBond.Order.DOUBLE);
m.addBond(3, 4, IBond.Order.SINGLE);
m.getAtom(0).setPoint2d(new Point2d(0, 0));
m.getAtom(0).setFlag(CDKConstants.ISPLACED, true);

AtomPlacer atomPlacer = new AtomPlacer();
atomPlacer.setMolecule(m);
atomPlacer.placeLinearChain(m, new Vector2d(0, 1.5), 1.5);

Point2d p1 = m.getAtom(1).getPoint2d();
Point2d p2 = m.getAtom(2).getPoint2d();
Point2d p3 = m.getAtom(3).getPoint2d();

Vector2d p2p1 = new Vector2d(p1.x - p2.x,
p1.y - p2.y);
Vector2d p2p3 = new Vector2d(p3.x - p2.x,
p3.y - p2.y);

p2p1.normalize();
p2p3.normalize();

double theta = Math.acos(p2p1.x * p2p3.x + p2p1.y * p2p3.y);

assertThat(theta, closeTo(Math.PI, 0.05));
}


@Test public void cumulated_x3() {
IAtomContainer m = new AtomContainer(6, 4, 0, 0);
m.addAtom(atom("C", 3));
m.addAtom(atom("C", 1));
m.addAtom(atom("C", 0));
m.addAtom(atom("C", 0));
m.addAtom(atom("C", 1));
m.addAtom(atom("C", 3));
m.addBond(0, 1, IBond.Order.SINGLE);
m.addBond(1, 2, IBond.Order.DOUBLE);
m.addBond(2, 3, IBond.Order.DOUBLE);
m.addBond(3, 4, IBond.Order.DOUBLE);
m.addBond(4, 5, IBond.Order.SINGLE);
m.getAtom(0).setPoint2d(new Point2d(0, 0));
m.getAtom(0).setFlag(CDKConstants.ISPLACED, true);

AtomPlacer atomPlacer = new AtomPlacer();
atomPlacer.setMolecule(m);
atomPlacer.placeLinearChain(m, new Vector2d(0, 1.5), 1.5);


Point2d p1 = m.getAtom(1).getPoint2d();
Point2d p2 = m.getAtom(2).getPoint2d();
Point2d p3 = m.getAtom(3).getPoint2d();
Point2d p4 = m.getAtom(4).getPoint2d();

Vector2d p2p1 = new Vector2d(p1.x - p2.x,
p1.y - p2.y);
Vector2d p2p3 = new Vector2d(p3.x - p2.x,
p3.y - p2.y);
Vector2d p3p2 = new Vector2d(p2.x - p3.x,
p2.y - p3.y);
Vector2d p3p4 = new Vector2d(p4.x - p3.x,
p4.y - p3.y);

p2p1.normalize();
p2p3.normalize();
p3p2.normalize();
p3p4.normalize();

assertThat(Math.acos(p2p1.x * p2p3.x + p2p1.y * p2p3.y),
closeTo(Math.PI, 0.05));
assertThat(Math.acos(p3p2.x * p3p4.x + p3p2.y * p3p4.y),
closeTo(Math.PI, 0.05));

}

static IAtom atom(String symbol, int hCount) {
IAtom a = new Atom(symbol);
a.setImplicitHydrogenCount(hCount);
return a;
}
}

0 comments on commit 3a9104c

Please sign in to comment.