Anonymous avatar Anonymous committed c880f77

added MoleculeTest

Comments (0)

Files changed (1)

src/test/java/org/xmlcml/svg2xml/chem/MoleculeTest.java

 import java.io.File;
 import java.util.List;
 
+import junit.framework.Assert;
+
+import org.apache.log4j.Logger;
 import org.junit.Test;
+import org.xmlcml.euclid.Angle;
+import org.xmlcml.euclid.Angle.Units;
 import org.xmlcml.graphics.svg.SVGElement;
+import org.xmlcml.graphics.svg.SVGG;
+import org.xmlcml.graphics.svg.SVGLine;
 import org.xmlcml.graphics.svg.SVGPath;
-import org.xmlcml.graphics.svg.path.PathPrimitiveList;
+import org.xmlcml.graphics.svg.SVGSVG;
 import org.xmlcml.svg2xml.Fixtures;
 
 /** test reading molecules
  */
 public class MoleculeTest {
 
+	private final static Logger LOG = Logger.getLogger(MoleculeTest.class);
+	private static final Angle MAX_ANGLE = new Angle(0.02, Units.RADIANS);
+	private static final Double MAX_WIDTH = 1.0;
+
+	
 	@Test
-	public void testRoundedLines() {
-		List<SVGPath>pathList = SVGPath.extractPaths(SVGElement.readAndCreateSVG(new File(Fixtures.MOLECULE_DIR, "image.g.2.13.svg")));
-		PathPrimitiveList primList = pathList.get(0).ensurePrimitives();
+	public void testCreateLines() {
+		List<SVGPath> pathList = SVGPath.extractPaths(SVGElement.readAndCreateSVG(new File(Fixtures.MOLECULE_DIR, "image.g.2.13"+".svg")));
+		Assert.assertEquals("paths", 13, pathList.size());
+		SVGG g = new SVGG();
+		int i = 0;
+		for (SVGPath path : pathList) {
+			LOG.trace(path.getSignature());
+			SVGPath newPath = path.replaceAllUTurnsByButt(MAX_ANGLE);
+			if (newPath != null) {
+				SVGLine line = newPath.createLineFromMLLLL(MAX_ANGLE, MAX_WIDTH);
+				if (line != null) {
+					g.appendChild(line);
+				} else {
+					LOG.debug("Failed line"+i);
+					newPath.setFill("red");
+					g.appendChild(newPath.copy());
+				}
+			} else {
+				LOG.debug("Failed Caps"+i);
+				path.setFill("blue");
+				g.appendChild(path.copy());
+			}
+			i++;
+		}
+		SVGSVG.wrapAndWriteAsSVG(g, new File("target/moleculeLines"+"image.g.2.13"+".svg"));
 	}
 
 	@Test
 	public void testnonWedgeBondsAndElements() {
-//		image.g.2.13.svg
+		createLinesFromOutlines("image.g.2.13", MAX_WIDTH, MAX_ANGLE, 13);
 	}
+
 	
 	@Test
 	public void testnonWedgeBondsAndElements1() {
-//		image.g.2.16.svg
+		createLinesFromOutlines("image.g.2.16", MAX_WIDTH, MAX_ANGLE, 20);
 	}
 	
 	@Test
 	public void testSubscripts() {
-//		image.g.2.11.svg
+		createLinesFromOutlines("image.g.2.11", MAX_WIDTH, MAX_ANGLE, 6);
 		
 	}
 	
 	@Test
 	public void testWedges() {
-//		image.g.2.18.svg
+		createLinesFromOutlines("image.g.2.18", MAX_WIDTH, MAX_ANGLE, 21);
 		
 	}
 	
 	@Test
 	public void testNoRingsOrWedges() {
-//		image.g.2.23.svg
+		createLinesFromOutlines("image.g.2.23", MAX_WIDTH, MAX_ANGLE, 24);
 		
 	}
 	
 	@Test
 	public void testHard() {
-//		image.g.2.25.svg
+		createLinesFromOutlines("image.g.2.25", MAX_WIDTH, MAX_ANGLE, 24);
 		
 	}
+	
+	// ================= HELPERS ===============
+	private static void createLinesFromOutlines(String fileRoot, Double maxWidth, Angle angleEps, int pathCount) {
+		List<SVGPath> pathList = SVGPath.extractPaths(SVGElement.readAndCreateSVG(new File(Fixtures.MOLECULE_DIR, fileRoot+".svg")));
+		Assert.assertEquals("paths", pathCount, pathList.size());
+		SVGG g = new SVGG();
+		int i = 0;
+		for (SVGPath path : pathList) {
+			LOG.trace(path.getSignature());
+			SVGPath newPath = path.replaceAllUTurnsByButt(angleEps);
+			if (newPath != null) {
+				SVGLine line = newPath.createLineFromMLLLL(angleEps, maxWidth);
+				if (line != null) {
+					g.appendChild(line);
+				} else {
+					LOG.debug("Failed line"+i);
+					newPath.setFill("red");
+					g.appendChild(newPath.copy());
+				}
+			} else {
+				LOG.debug("Failed Caps"+i);
+				path.setFill("blue");
+				g.appendChild(path.copy());
+			}
+			i++;
+		}
+		SVGSVG.wrapAndWriteAsSVG(g, new File("target/moleculeLines"+fileRoot+".svg"));
+	}
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.