Skip to content

Instantly share code, notes, and snippets.

@gilleain
Created June 12, 2011 20:47
Show Gist options
  • Save gilleain/1021965 to your computer and use it in GitHub Desktop.
Save gilleain/1021965 to your computer and use it in GitHub Desktop.
Ring fill generator for CDK
package customgenerator;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import javax.vecmath.Point2d;
import javax.vecmath.Vector2d;
import org.openscience.cdk.geometry.GeometryTools;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IRingSet;
import org.openscience.cdk.renderer.RendererModel;
import org.openscience.cdk.renderer.elements.ElementGroup;
import org.openscience.cdk.renderer.elements.IRenderingElement;
import org.openscience.cdk.renderer.elements.PathElement;
import org.openscience.cdk.renderer.generators.IGenerator;
import org.openscience.cdk.renderer.generators.IGeneratorParameter;
import org.openscience.cdk.ringsearch.SSSRFinder;
public class RingPlateGenerator implements IGenerator<IAtomContainer> {
@Override
public List<IGeneratorParameter<?>> getParameters() {
return new ArrayList<IGeneratorParameter<?>>();
}
@Override
public IRenderingElement generate(IAtomContainer atomContainer, RendererModel model) {
ElementGroup ringPaths = new ElementGroup();
SSSRFinder ringFinder = new SSSRFinder(atomContainer);
IRingSet ringSet = ringFinder.findSSSR();
for (IAtomContainer ring : ringSet.atomContainers()) {
ringPaths.add(generateRing(ring));
}
return ringPaths;
}
private PathElement generateRing(IAtomContainer atomContainer) {
Point2d center = GeometryTools.get2DCenter(atomContainer);
List<Point2d> points = new ArrayList<Point2d>();
for (IAtom atom : atomContainer.atoms()) {
Point2d p = atom.getPoint2d();
Vector2d v = new Vector2d(center);
v.sub(p);
v.scaleAdd(0.2, v, p);
points.add(new Point2d(v));
}
return new PathElement(points, Color.DARK_GRAY);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment