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;
import java.util.ArrayList;
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.model.graphics.PolylineData;
......@@ -58,4 +60,30 @@ public final class PolylineDataFactory {
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;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.awt.geom.Point2D;
import java.lang.reflect.Constructor;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import lcsb.mapviewer.common.Configuration;
import lcsb.mapviewer.model.graphics.PolylineData;
public class PolylineDataFactoryTest {
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testPrivateCotnstructor() throws Exception {
try {
Constructor<?> constr = PolylineDataFactory.class.getDeclaredConstructor(new Class<?>[] {});
constr.setAccessible(true);
assertNotNull(constr.newInstance(new Object[] {}));
} catch (Exception e) {
throw e;
}
}
@Test
public void testPrivateCotnstructor() throws Exception {
try {
Constructor<?> constr = PolylineDataFactory.class.getDeclaredConstructor(new Class<?>[] {});
constr.setAccessible(true);
assertNotNull(constr.newInstance(new Object[] {}));
} catch (Exception e) {
throw e;
}
}
@Test
public void testRemoveCollinearPoints2() throws Exception {
try {
PolylineData line = new PolylineData();
line.addPoint(new Point2D.Double(10, 20));
line.addPoint(new Point2D.Double(11, 30));
line.addPoint(new Point2D.Double(12, 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(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