Skip to content

Commit

Permalink
Added a wavy bond type for unset stereobonds.
Browse files Browse the repository at this point in the history
Change-Id: Ied54df213317d58d19f5e9c56b78ecbc18c16f8b
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
Stephan Beisken authored and egonw committed Feb 20, 2013
1 parent 3386be2 commit 1e67f59
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 34 deletions.
Expand Up @@ -33,12 +33,19 @@
@TestClass("org.openscience.cdk.renderer.elements.WedgeLineElementTest")
public class WedgeLineElement extends LineElement {

/**
* If true, the wedge should be rendered as a dashed triangle.
*/
public final boolean isDashed;
/**
* If the bond is dashed ,wedged, or "up_or_down", i.e., not defined.
*/
public enum TYPE {
DASHED, WEDGED, INDIFF
}

/**
/**
* The type of the bond (dashed, wedged, not defined).
*/
public final TYPE type;

/**
* The direction indicates which way the wedge gets thicker.
*/
public final Direction direction;
Expand All @@ -60,34 +67,34 @@ public enum Direction {
* @param x2 the x-coordinate of the second point
* @param y2 the y-coordinate of the second point
* @param width the width of the wedge
* @param dashed if true, the wedge should be dashed
* @param type the bond is dashed ,wedged, or "up_or_down", i.e., not defined.
* @param direction the direction of the thickness
* @param color the color of the wedge
*/
@TestMethod("testConstructor")
public WedgeLineElement(double x1, double y1, double x2, double y2,
double width, boolean dashed, Direction direction, Color color) {
double width, TYPE type, Direction direction, Color color) {
super(x1, y1, x2, y2, width, color);
this.isDashed = dashed;
this.type = type;
this.direction = direction;
}

/**
* Make a wedge along the given line element.
*
* @param element the line element to use as the basic geometry
* @param dashed if true, the wedge should be dashed
* @param type if the bond is dashed ,wedged, or "up_or_down", i.e., not defined
* @param direction the direction of the thickness
* @param color the color of the wedge
*/
@TestMethod("testConstructor_LineElement")
public WedgeLineElement(LineElement element, boolean dashed,
public WedgeLineElement(LineElement element, TYPE type,
Direction direction, Color color) {
this(direction == Direction.toFirst ? element.secondPointX : element.firstPointX,
direction == Direction.toFirst ? element.secondPointY : element.firstPointY,
direction == Direction.toFirst ? element.firstPointX : element.secondPointX,
direction == Direction.toFirst ? element.firstPointY : element.secondPointY,
element.width, dashed, direction, color);
element.width, type, direction, color);
}

/**
Expand Down
Expand Up @@ -431,24 +431,28 @@ public LineElement generateInnerElement(
return new LineElement(u.x, u.y, w.x, w.y, width, color);
}

private IRenderingElement generateStereoElement(
IBond bond, RendererModel model) {

IBond.Stereo stereo = bond.getStereo();
boolean dashed = false;
Direction dir = Direction.toSecond;
if (stereo == IBond.Stereo.DOWN ||
stereo == IBond.Stereo.DOWN_INVERTED)
dashed = true;
if (stereo == IBond.Stereo.DOWN_INVERTED ||
stereo == IBond.Stereo.UP_INVERTED)
dir = Direction.toFirst;

IRenderingElement base = generateBondElement(
bond, IBond.Order.SINGLE, model);
return new WedgeLineElement(
(LineElement)base, dashed, dir, getColorForBond(bond, model));
}
private IRenderingElement generateStereoElement(
IBond bond, RendererModel model) {

IBond.Stereo stereo = bond.getStereo();
WedgeLineElement.TYPE type = WedgeLineElement.TYPE.WEDGED;
Direction dir = Direction.toSecond;
if (stereo == IBond.Stereo.DOWN ||
stereo == IBond.Stereo.DOWN_INVERTED)
type = WedgeLineElement.TYPE.DASHED;
if (stereo == IBond.Stereo.UP_OR_DOWN ||
stereo == IBond.Stereo.UP_OR_DOWN_INVERTED)
type = WedgeLineElement.TYPE.INDIFF;
if (stereo == IBond.Stereo.DOWN_INVERTED ||
stereo == IBond.Stereo.UP_INVERTED ||
stereo == IBond.Stereo.UP_OR_DOWN_INVERTED)
dir = Direction.toFirst;

IRenderingElement base = generateBondElement(
bond, IBond.Order.SINGLE, model);
return new WedgeLineElement(
(LineElement) base, type, dir, getColorForBond(bond, model));
}

/**
* Check to see if a bond is a double bond.
Expand Down
46 changes: 43 additions & 3 deletions src/main/org/openscience/cdk/renderer/visitor/AWTDrawVisitor.java
Expand Up @@ -235,11 +235,13 @@ private void visit(WedgeLineElement wedge) {
vertexB.add(normal);
vertexC.sub(normal);
this.graphics.setColor(wedge.color);
if (wedge.isDashed) {
if (wedge.type == WedgeLineElement.TYPE.DASHED) {
this.drawDashedWedge(vertexA, vertexB, vertexC);
} else {
} else if (wedge.type == WedgeLineElement.TYPE.WEDGED) {
this.drawFilledWedge(vertexA, vertexB, vertexC);
}
} else if (wedge.type == WedgeLineElement.TYPE.INDIFF) {
this.drawIndiffWedge(vertexA, vertexB, vertexC);
}
}

private void drawFilledWedge(
Expand Down Expand Up @@ -283,6 +285,44 @@ private void drawDashedWedge(
}
this.graphics.setStroke(storedStroke);
}

private void drawIndiffWedge(Point2d vertexA, Point2d vertexB, Point2d vertexC) {
// store the current stroke
Stroke storedStroke = this.graphics.getStroke();
this.graphics.setStroke(new BasicStroke(1, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));

// calculate the distances between lines
double distance = vertexB.distance(vertexA);
double gapFactor = 0.05;
double gap = distance * gapFactor;
double numberOfDashes = distance / gap;
double displacement = 0;

// draw by interpolating along the edges of the triangle
Point2d point1 = new Point2d();
boolean flip = false;
point1.interpolate(vertexA, vertexB, displacement);
int[] p1T = this.transformPoint(point1.x, point1.y);
displacement += gapFactor;
for (int i = 0; i < numberOfDashes; i++) {
Point2d point2 = new Point2d();
if (flip) {
point2.interpolate(vertexA, vertexC, displacement);
} else {
point2.interpolate(vertexA, vertexB, displacement);
}
flip = !flip;
int[] p2T = this.transformPoint(point2.x, point2.y);
this.graphics.drawLine(p1T[0], p1T[1], p2T[0], p2T[1]);
if (distance * (displacement + gapFactor) >= distance) {
break;
} else {
p1T = p2T;
displacement += gapFactor;
}
}
this.graphics.setStroke(storedStroke);
}

private void visit(AtomSymbolElement atomSymbol) {
this.graphics.setFont(this.fontManager.getFont());
Expand Down
Expand Up @@ -35,7 +35,7 @@ public class WedgeLineElementTest extends AbstractElementTest {
@BeforeClass
public static void setup() {
IRenderingElement element = new WedgeLineElement(
0,0,1,1, 1.0, true,
0,0,1,1, 1.0, WedgeLineElement.TYPE.DASHED,
WedgeLineElement.Direction.toFirst,
Color.orange
);
Expand All @@ -46,7 +46,7 @@ public static void setup() {
public void testConstructor_LineElement() {
IRenderingElement element = new WedgeLineElement(
new LineElement(0,0,1,1, 1.0, Color.red),
true,
WedgeLineElement.TYPE.DASHED,
WedgeLineElement.Direction.toFirst,
Color.orange
);
Expand Down

0 comments on commit 1e67f59

Please sign in to comment.