Commit 35e4ddd4 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

closest point to line was inproperly computed

parent ab38d0b2
Pipeline #31056 passed with stage
in 22 minutes and 51 seconds
......@@ -14,6 +14,8 @@ minerva (15.0.3) stable; urgency=medium
another tab there was an error thrown (#1343)
* Bug fix: chebi and entrez annotations were incorrectly exported to GPML
(#1349)
* Bug fix: exporting to GPML sometimes resulted in invalid modification point
end for catalysis (#1350)
-- Piotr Gawron <piotr.gawron@uni.lu> Wed, 8 Jul 2020 16:00:00 +0200
......
......@@ -123,24 +123,35 @@ public final class Geo {
* @return point on line that is as close as possible to point
*/
public static Point2D closestPointOnPolyline(PolylineData mainLine, Point2D point) {
Point2D res = null;
double distance = distanceOnPolyline(mainLine, point);
double lenght = 0.0;
Point2D res = mainLine.getBeginPoint();
for (Line2D line : mainLine.getLines()) {
lenght += lineLen(line);
}
lenght *= distance;
for (Line2D line : mainLine.getLines()) {
if (lenght - lineLen(line) > 0) {
lenght -= lineLen(line);
} else {
double tmp = lenght / lineLen(line);
double x = line.getX1() + tmp * (line.getX2() - line.getX1());
double y = line.getY1() + tmp * (line.getY2() - line.getY1());
res = new Point2D.Double(x, y);
Point2D newPoint = getClosestPointOnSegment(line, point);
if (res.distanceSq(point) > newPoint.distanceSq(point)) {
res = newPoint;
}
}
return res;
}
static Point2D getClosestPointOnSegment(Line2D line, Point2D point) {
double xDelta = line.getX2() - line.getX1();
double yDelta = line.getY2() - line.getY1();
if ((xDelta == 0) && (yDelta == 0)) {
return new Point2D.Double(line.getX1(), line.getY1());
}
double u = ((point.getX() - line.getX1()) * xDelta + (point.getY() - line.getY1()) * yDelta)
/ (xDelta * xDelta + yDelta * yDelta);
if (u < 0) {
return new Point2D.Double(line.getX1(), line.getY1());
} else if (u > 1) {
return new Point2D.Double(line.getX2(), line.getY2());
} else {
return new Point2D.Double((int) Math.round(line.getX1() + u * xDelta),
(int) Math.round(line.getY1() + u * yDelta));
}
}
}
......@@ -6,11 +6,13 @@ import org.junit.runners.Suite.SuiteClasses;
import lcsb.mapviewer.wikipathway.XML.AllXmlTests;
import lcsb.mapviewer.wikipathway.model.AllModelTests;
import lcsb.mapviewer.wikipathway.utils.GeoTest;
@RunWith(Suite.class)
@SuiteClasses({ AllModelTests.class,
AllXmlTests.class,
ComplexReactionToModelTest.class,
GeoTest.class,
GPMLToModelTest.class,
ReactionElbowsTest.class,
ReactionGpmlInputToModelTest.class,
......
......@@ -23,7 +23,6 @@ import lcsb.mapviewer.model.map.compartment.SquareCompartment;
import lcsb.mapviewer.model.map.model.*;
import lcsb.mapviewer.model.map.reaction.*;
import lcsb.mapviewer.model.map.reaction.type.StateTransitionReaction;
import lcsb.mapviewer.model.map.reaction.type.TransportReaction;
import lcsb.mapviewer.model.map.species.*;
import lcsb.mapviewer.model.map.species.field.ModificationState;
import lcsb.mapviewer.model.map.species.field.Residue;
......@@ -307,7 +306,7 @@ public class ModelToGPMLTest extends WikipathwaysTestFunctions {
species.setX(1);
species.setY(1);
species.setZ(15);
species.setNamePoint(species.getCenter());
species.setNameHorizontalAlign(HorizontalAlign.CENTER);
species.setNameVerticalAlign(VerticalAlign.MIDDLE);
......@@ -419,16 +418,15 @@ public class ModelToGPMLTest extends WikipathwaysTestFunctions {
model.setHeight(1000);
model.setNotes("a < b");
Species p1 = createProtein();
p1.setNotes("<a href='https://google.lu/'>link</a>");
model.addElement(p1);
Species p2 = createProtein();
model.addElement(p2);
Reaction r= createReaction(p1, p2);
Reaction r = createReaction(p1, p2);
r.setNotes("<xml>tag</xml>");
model.addReaction(r);
......@@ -439,5 +437,4 @@ public class ModelToGPMLTest extends WikipathwaysTestFunctions {
assertEquals(0, new ModelComparator().compare(model, model2));
}
}
package lcsb.mapviewer.wikipathway.utils;
import static org.junit.Assert.assertEquals;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import org.junit.Test;
public class GeoTest {
@Test
public void testClosestPointOnSegment() {
Point2D result = Geo.getClosestPointOnSegment(new Line2D.Double(10, 10, 20, 20), new Point2D.Double(20, 10));
assertEquals(result, new Point2D.Double(15, 15));
}
@Test
public void testClosestPointOnSegmentEdge() {
Point2D result = Geo.getClosestPointOnSegment(new Line2D.Double(10, 10, 20, 20), new Point2D.Double(20, 0));
assertEquals(result, new Point2D.Double(10, 10));
}
@Test
public void testClosestPointOnSegmentEdge2() {
Point2D result = Geo.getClosestPointOnSegment(new Line2D.Double(10, 10, 20, 20), new Point2D.Double(20, 30));
assertEquals(result, new Point2D.Double(20, 20));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment