Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-f committed Sep 21, 2018
1 parent 22b2650 commit b586a9f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public Polygon getIsoVist(Coordinate position) {
List<SegmentString> bounded = new ArrayList<>(originalSegments);
GeometricShapeFactory geometricShapeFactory = new GeometricShapeFactory();
geometricShapeFactory.setCentre(position);
geometricShapeFactory.setWidth(maxDistance / 2);
geometricShapeFactory.setHeight(maxDistance / 2);
geometricShapeFactory.setWidth(maxDistance * 2);
geometricShapeFactory.setHeight(maxDistance * 2);
geometricShapeFactory.setNumPoints(numPoints);
addGeometry(bounded, geometricShapeFactory.createEllipse());
addPolygon(bounded, geometricShapeFactory.createEllipse());

// Intersection with bounding circle
bounded = fixSegments(bounded);
Expand Down Expand Up @@ -299,28 +299,35 @@ public static void addLineString(List<SegmentString> segments, LineString lineSt
}
}

private static void addGeometry(List<SegmentString> segments, Geometry geometry) {
if (geometry instanceof GeometryCollection) {
private static void addPolygon(List<SegmentString> segments, Polygon poly) {
addLineString(segments, poly.getExteriorRing());
final int ringCount = poly.getNumInteriorRing();
// Keep interior ring if the viewpoint is inside the polygon
for (int nr = 0; nr < ringCount; nr++) {
addLineString(segments, poly.getInteriorRingN(nr));
}
}

private static void addGeometry(List<SegmentString> segments, GeometryCollection geometry) {
int geoCount = geometry.getNumGeometries();
for (int n = 0; n < geoCount; n++) {
Geometry simpleGeom = geometry.getGeometryN(n);
if (simpleGeom instanceof LineString) {
addLineString(segments, (LineString) simpleGeom);
} else if (simpleGeom instanceof Polygon) {
Polygon poly = ((Polygon) simpleGeom);
addLineString(segments, poly.getExteriorRing());
final int ringCount = poly.getNumInteriorRing();
// Keep interior ring if the viewpoint is inside the polygon
for (int nr = 0; nr < ringCount; nr++) {
addLineString(segments, poly.getInteriorRingN(nr));
}
addPolygon(segments, (Polygon)simpleGeom);
}
}
}
}

public void addGeometry(Geometry geometry) {
addGeometry(originalSegments, geometry);
if (geometry instanceof LineString) {
addLineString(originalSegments, (LineString) geometry);
} else if (geometry instanceof Polygon) {
addPolygon(originalSegments, (Polygon) geometry);
} else if(geometry instanceof GeometryCollection) {
addGeometry(originalSegments, (GeometryCollection) geometry);
}
}

/**
Expand All @@ -340,15 +347,15 @@ public Vertex(int idSegment, int vertexIndex, double angle) {

@Override
public int compareTo(Vertex o) {
int res = Double.compare(o.angle, this.angle);
int res = Double.compare(angle, o.angle);
if (res != 0) {
return res;
}
res = Integer.compare(o.idSegment, idSegment);
res = Integer.compare(idSegment, o.idSegment);
if (res != 0) {
return res;
}
return Integer.compare(o.vertexIndex, vertexIndex);
return Integer.compare(vertexIndex, o.vertexIndex);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.h2gis.utilities.jts_utils;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.LineSegment;
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.math.Vector2D;
import org.junit.Test;

Expand All @@ -14,159 +13,12 @@
public class VisibilityAlgorithmTest {

@Test
public void InterpolateTest1() {
Coordinate origin = new Coordinate(10, 10);
LineSegment segment = new LineSegment(new Coordinate(14, 6), new Coordinate(14,14));
Coordinate projPoint = segment.closestPoint(origin);
Vector2D v1 = new Vector2D(origin, segment.p0);
Vector2D v2 = new Vector2D(origin, segment.p1);
public void testIsoVistEmpty() {
VisibilityAlgorithm c = new VisibilityAlgorithm(50);

VisibilityAlgorithm.Limit limit = new VisibilityAlgorithm.Limit(v1.angle(), v1.length(), v2.angle(), v2.length());
assertEquals(projPoint.distance(origin), limit.interpolate(new Vector2D(origin, projPoint).angle()), 1e-6);
}

@Test
public void InterpolateTest2() {
Coordinate origin = new Coordinate(10, 10);
LineSegment segment = new LineSegment(new Coordinate(15, 6), new Coordinate(14,14));
Coordinate projPoint = segment.closestPoint(origin);
Vector2D v1 = new Vector2D(origin, segment.p0);
Vector2D v2 = new Vector2D(origin, segment.p1);

VisibilityAlgorithm.Limit limit = new VisibilityAlgorithm.Limit(v1.angle(), v1.length(), v2.angle(), v2.length());
assertEquals(projPoint.distance(origin), limit.interpolate(new Vector2D(origin, projPoint).angle()), 1e-6);
}


@Test
public void InterpolateTest3() {
Coordinate origin = new Coordinate(10, 10);
LineSegment segment = new LineSegment(new Coordinate(14, 6), new Coordinate(15,14));
Coordinate projPoint = segment.closestPoint(origin);
Vector2D v1 = new Vector2D(origin, segment.p0);
Vector2D v2 = new Vector2D(origin, segment.p1);

VisibilityAlgorithm.Limit limit = new VisibilityAlgorithm.Limit(v1.angle(), v1.length(), v2.angle(), v2.length());
assertEquals(projPoint.distance(origin), limit.interpolate(new Vector2D(origin, projPoint).angle()), 1e-6);
}


@Test
public void InterpolateTest4() {
Coordinate origin = new Coordinate(10, 10);
LineSegment segment = new LineSegment(new Coordinate(5, 12), new Coordinate(12,14));
Coordinate projPoint = segment.closestPoint(origin);
Vector2D v1 = new Vector2D(origin, segment.p0);
Vector2D v2 = new Vector2D(origin, segment.p1);

VisibilityAlgorithm.Limit limit = new VisibilityAlgorithm.Limit(v1.angle(), v1.length(), v2.angle(), v2.length());
assertEquals(projPoint.distance(origin), limit.interpolate(new Vector2D(origin, projPoint).angle()), 1e-6);
}


/**
* Check with angle out of Limit angles
*/
@Test
public void InterpolateTestOut1() {
Coordinate origin = new Coordinate(10, 10);
LineSegment segment = new LineSegment(new Coordinate(14, 12), new Coordinate(14,15));
Vector2D v1 = new Vector2D(origin, segment.p0);
Vector2D v2 = new Vector2D(origin, segment.p1);

VisibilityAlgorithm.Limit limit = new VisibilityAlgorithm.Limit(v1.angle(), v1.length(), v2.angle(), v2.length());
assertEquals(4, limit.interpolate(0), 1e-6);
}

@Test
public void testIsoVist1() {
VisibilityAlgorithm c = new VisibilityAlgorithm(50, new Coordinate(0, 0));

c.addSegment(new Coordinate(1, 2), new Coordinate(1, 4));
c.addSegment(new Coordinate(3, 1), new Coordinate(-1, 5));

Set<VisibilityAlgorithm.Limit> limits = c.getLimits();

assertEquals(4, limits.size());
}

@Test
public void testIsoVist2() {
VisibilityAlgorithm c = new VisibilityAlgorithm(50, new Coordinate(0, 0));

c.addSegment(new Coordinate(1, 3), new Coordinate(1, 5));
c.addSegment(new Coordinate(1, 5), new Coordinate(3, 5));

List<LineSegment> limits = new ArrayList<>();
for(VisibilityAlgorithm.Limit limit : c.getLimits()) {
limits.add(limit.createSegment(new Vector2D()));
}
assertEquals(2, limits.size());

assertEquals(limits.get(0).toString(),0 ,limits.get(0).p0.distance(new Coordinate(3,5)), 1e-6);
assertEquals(limits.get(0).toString(),0 ,limits.get(0).p1.distance(new Coordinate(1 , 5)), 1e-6);

assertEquals(limits.get(1).toString(), 0 ,limits.get(1).p0.distance(new Coordinate(1.0, 3.0)), 1e-6);
assertEquals(limits.get(1).toString(),0 ,limits.get(1).p1.distance(new Coordinate(1, 5)), 1e-6);
Polygon poly = c.getIsoVist(new Coordinate(0, 0));

assertEquals(100, poly.getNumPoints());
}

@Test
public void testIsoVist3() {
VisibilityAlgorithm c = new VisibilityAlgorithm(50, new Coordinate(0, 0));

c.addSegment(new Coordinate(1, 3), new Coordinate(1, 5));
c.addSegment(new Coordinate(1, 5), new Coordinate(3, 5));
c.addSegment(new Coordinate(0, 3), new Coordinate(4, 6));

List<LineSegment> limits = new ArrayList<>();
for(VisibilityAlgorithm.Limit limit : c.getLimits()) {
limits.add(limit.createSegment(new Vector2D()));
}

assertEquals(7, limits.size());

assertEquals(limits.get(0).toString(),0 ,limits.get(0).p0.distance(new Coordinate(4,6)), 1e-6);
assertEquals(limits.get(0).toString(),0 ,limits.get(0).p1.distance(new Coordinate(2.666666666666668 , 5)), 1e-6);

assertEquals(limits.get(1).toString(), 0 ,limits.get(1).p0.distance(new Coordinate(3.0, 5.0)), 1e-6);
assertEquals(limits.get(1).toString(),0 ,limits.get(1).p1.distance(new Coordinate(2.666666666666668, 5)), 1e-6);

assertEquals(limits.get(2).toString(), 0 ,limits.get(2).p0.distance(new Coordinate(2.666666666666668, 5.0)), 1e-6);
assertEquals(limits.get(2).toString(),0 ,limits.get(2).p1.distance(new Coordinate(1, 3.75)), 1e-6);

assertEquals(limits.get(3).toString(), 0 ,limits.get(3).p0.distance(new Coordinate(2.666666666666668, 5.0)), 1e-6);
assertEquals(limits.get(3).toString(),0 ,limits.get(3).p1.distance(new Coordinate(1, 5)), 1e-6);

assertEquals(limits.get(4).toString(), 0 ,limits.get(4).p0.distance(new Coordinate(1, 3.0)), 1e-6);
assertEquals(limits.get(4).toString(),0 ,limits.get(4).p1.distance(new Coordinate(1, 3.75)), 1e-6);

assertEquals(limits.get(5).toString(), 0 ,limits.get(5).p0.distance(new Coordinate(1, 3.75)), 1e-6);
assertEquals(limits.get(5).toString(),0 ,limits.get(5).p1.distance(new Coordinate(1, 5)), 1e-6);

assertEquals(limits.get(6).toString(), 0 ,limits.get(6).p0.distance(new Coordinate(1, 3.75)), 1e-6);
assertEquals(limits.get(6).toString(),0 ,limits.get(6).p1.distance(new Coordinate(0, 3)), 1e-6);

}

@Test
public void testIsoVistCrossPi() {
VisibilityAlgorithm c = new VisibilityAlgorithm(50, new Coordinate(0, 0));

c.addSegment(new Coordinate(-2, -1), new Coordinate(3, 4));

List<LineSegment> limits = new ArrayList<>();
for(VisibilityAlgorithm.Limit limit : c.getLimits()) {
limits.add(limit.createSegment(new Vector2D()));
}

assertEquals(2, limits.size());

assertEquals(limits.get(0).toString(),0 ,limits.get(0).p0.distance(new Coordinate(-1,0)), 1e-6);
assertEquals(limits.get(0).toString(),0 ,limits.get(0).p1.distance(new Coordinate(-2 , -1)), 1e-6);

assertEquals(limits.get(1).toString(), 0 ,limits.get(1).p0.distance(new Coordinate(3, 4)), 1e-6);
assertEquals(limits.get(1).toString(),0 ,limits.get(1).p1.distance(new Coordinate(-1, 0)), 1e-6);

}
}

0 comments on commit b586a9f

Please sign in to comment.