Skip to content
Snippets Groups Projects
Commit 94b7d285 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

method that allows to remove collinear points from line

parent e5d1690f
No related branches found
No related tags found
3 merge requests!630WIP: Resolve "The privileges of a new user are not saved in some cases",!442Problem with exporting to celldesigner and parsing from celldesigner,!440Problem with exporting to celldesigner
...@@ -4,6 +4,8 @@ import java.awt.geom.Point2D; ...@@ -4,6 +4,8 @@ import java.awt.geom.Point2D;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.common.geometry.LineTransformation;
import lcsb.mapviewer.converter.model.celldesigner.structure.fields.EditPoints; import lcsb.mapviewer.converter.model.celldesigner.structure.fields.EditPoints;
import lcsb.mapviewer.model.graphics.PolylineData; import lcsb.mapviewer.model.graphics.PolylineData;
...@@ -58,4 +60,30 @@ public final class PolylineDataFactory { ...@@ -58,4 +60,30 @@ public final class PolylineDataFactory {
return createPolylineDataFromEditPoints(baseA, baseB, new ArrayList<Point2D>()); return createPolylineDataFromEditPoints(baseA, baseB, new ArrayList<Point2D>());
} }
} }
/**
* Creates {@link PolylineData} object from the parameter that doesn't contain
* collinear points.
*
* @param ld
* original line
* @return line without collinear points
*/
public static PolylineData removeCollinearPoints(PolylineData ld) {
LineTransformation lt = new LineTransformation();
PolylineData result = new PolylineData(ld);
int pointNumber = 1;
while (pointNumber < result.getPoints().size() - 1) {
Point2D previousPoint = result.getPoints().get(pointNumber - 1);
Point2D nextPoint = result.getPoints().get(pointNumber + 1);
Point2D currentPoint = result.getPoints().get(pointNumber);
Point2D closestPointOnLine = lt.closestPointOnSegmentLineToPoint(previousPoint, nextPoint, currentPoint);
if (closestPointOnLine.distance(currentPoint) <= Configuration.EPSILON) {
result.getPoints().remove(pointNumber);
} else {
pointNumber++;
}
}
return result;
}
} }
package lcsb.mapviewer.converter.model.celldesigner.geometry.helper; package lcsb.mapviewer.converter.model.celldesigner.geometry.helper;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import java.awt.geom.Point2D;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.model.graphics.PolylineData;
public class PolylineDataFactoryTest { public class PolylineDataFactoryTest {
@AfterClass @Test
public static void tearDownAfterClass() throws Exception { public void testPrivateCotnstructor() throws Exception {
} try {
Constructor<?> constr = PolylineDataFactory.class.getDeclaredConstructor(new Class<?>[] {});
@Before constr.setAccessible(true);
public void setUp() throws Exception { assertNotNull(constr.newInstance(new Object[] {}));
} } catch (Exception e) {
throw e;
@After }
public void tearDown() throws Exception { }
}
@Test
@Test public void testRemoveCollinearPoints2() throws Exception {
public void testPrivateCotnstructor() throws Exception { try {
try { PolylineData line = new PolylineData();
Constructor<?> constr = PolylineDataFactory.class.getDeclaredConstructor(new Class<?>[] {}); line.addPoint(new Point2D.Double(10, 20));
constr.setAccessible(true); line.addPoint(new Point2D.Double(11, 30));
assertNotNull(constr.newInstance(new Object[] {})); line.addPoint(new Point2D.Double(12, 40));
} catch (Exception e) { PolylineData modifiedLine = PolylineDataFactory.removeCollinearPoints(line);
throw e; assertEquals(2, modifiedLine.getPoints().size());
} assertEquals(0, modifiedLine.getBeginPoint().distance(new Point2D.Double(10, 20)), Configuration.EPSILON);
} assertEquals(0, modifiedLine.getEndPoint().distance(new Point2D.Double(12, 40)), Configuration.EPSILON);
} catch (Exception e) {
throw e;
}
}
@Test
public void testRemoveCollinearPoints() throws Exception {
try {
PolylineData line = new PolylineData();
line.addPoint(new Point2D.Double(10, 20));
line.addPoint(new Point2D.Double(10, 30));
line.addPoint(new Point2D.Double(10, 40));
line.addPoint(new Point2D.Double(10, 40));
PolylineData modifiedLine = PolylineDataFactory.removeCollinearPoints(line);
assertEquals(2, modifiedLine.getPoints().size());
assertEquals(0, modifiedLine.getBeginPoint().distance(new Point2D.Double(10, 20)), Configuration.EPSILON);
assertEquals(0, modifiedLine.getEndPoint().distance(new Point2D.Double(10, 40)), Configuration.EPSILON);
} catch (Exception e) {
throw e;
}
}
@Test
public void testRemoveCollinearPointsWithoutCollinearPoints() throws Exception {
try {
PolylineData line = new PolylineData();
line.addPoint(new Point2D.Double(10, 20));
line.addPoint(new Point2D.Double(10, 30));
line.addPoint(new Point2D.Double(20, 40));
line.addPoint(new Point2D.Double(10, 40));
PolylineData modifiedLine = PolylineDataFactory.removeCollinearPoints(line);
assertEquals(4, modifiedLine.getPoints().size());
} catch (Exception e) {
throw e;
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment