Skip to content

Commit

Permalink
Fix isovist
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-f committed Sep 24, 2018
1 parent e8b5295 commit 87f76b2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class VisibilityAlgorithm {
private double maxDistance;
private List<SegmentString> originalSegments = new ArrayList<>();
private double epsilon = 1e-6;
private int numPoints = 100;
private int numPoints = 32;

public VisibilityAlgorithm(double maxDistance) {
this.maxDistance = maxDistance;
Expand Down Expand Up @@ -92,6 +92,7 @@ public Polygon getIsoVist(Coordinate position, boolean addEnvelope) {

for (int idSegment = 0; idSegment < bounded.size(); idSegment++) {
SegmentString segment = bounded.get(idSegment);
System.out.println(String.format(Locale.ROOT,"segments.push([[%g, %g],[%g, %g]]);", segment.getCoordinate(0).x, segment.getCoordinate(0).y, segment.getCoordinate(1).x, segment.getCoordinate(1).y));
// Convert segment to angle relative to viewPoint
for(int j=0; j < 2; ++j) {
final Coordinate pt = segment.getCoordinate(j);
Expand Down Expand Up @@ -156,7 +157,7 @@ public Polygon getIsoVist(Coordinate position, boolean addEnvelope) {
polygon.add(vertex);
Coordinate cur = intersectLines(bounded.get(heap.get(0)), position, vertex);
if(cur != null && !cur.equals2D(vertex, epsilon)) {
polygon.add(vertex);
polygon.add(cur);
}
} else if(shorten) {
polygon.add(intersectLines(bounded.get(oldSegment), position, vertex));
Expand Down Expand Up @@ -266,7 +267,7 @@ private void remove(int index, List<Integer> heap, Coordinate position, List<Seg
heap.set(cur, temp);
cur = left;
} else if(right < heap.size() && lessThan(heap.get(right), heap.get(cur), position, segments, destination)) {
map.set(heap.get(left), cur);
map.set(heap.get(right), cur);
map.set(heap.get(cur), right);
int temp = heap.get(right);
heap.set(right, heap.get(cur));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.vividsolutions.jts.geom.*;
import com.vividsolutions.jts.io.*;
import com.vividsolutions.jts.math.Vector2D;
import com.vividsolutions.jts.util.*;
import org.junit.Test;

import java.util.ArrayList;
Expand All @@ -13,57 +14,70 @@

public class VisibilityAlgorithmTest {

/**
* Test without geometries
*/
@Test
public void testIsoVistEmpty() {
VisibilityAlgorithm c = new VisibilityAlgorithm(50);
/**
* Test without geometries
*/
@Test
public void testIsoVistEmpty() {
VisibilityAlgorithm c = new VisibilityAlgorithm(50);

Polygon poly = c.getIsoVist(new Coordinate(0, 0), true);
Polygon poly = c.getIsoVist(new Coordinate(0, 0), true);

assertEquals(101, poly.getNumPoints());
}
assertEquals(33, poly.getNumPoints());
}

/**
* Test with geometry crossing 0 coordinates
* @throws ParseException
*/
@Test
public void testIsoVistCross0() throws ParseException {
WKTReader wktReader = new WKTReader();
/**
* Test with geometry crossing 0 coordinates
*
* @throws ParseException
*/
@Test
public void testIsoVistCross0() throws ParseException {
WKTReader wktReader = new WKTReader();

VisibilityAlgorithm c = new VisibilityAlgorithm(50);
VisibilityAlgorithm c = new VisibilityAlgorithm(50);

Geometry bound = wktReader.read("POLYGON ((-100 -100, -100 100, 100 100, 100 -100, -100 -100))");
c.addGeometry(bound);
Geometry poly2 = wktReader.read("POLYGON ((-31 -9.8, -32 10.2, -12.1 10.5, -12 -9.6, -31 -9.8))");
c.addGeometry(poly2);
Geometry bound = wktReader.read("POLYGON ((-100 -100, -100 100, 100 100, 100 -100, -100 -100))");
c.addGeometry(bound);
Geometry poly2 = wktReader.read("POLYGON ((-31 -9.8, -32 10.2, -12.1 10.5, -12 -9.6, -31 -9.8))");
c.addGeometry(poly2);

Polygon isoVist = c.getIsoVist(new Coordinate(0, 0), false);
Polygon isoVist = c.getIsoVist(new Coordinate(0, 0), false);

assertFalse(isoVist.contains(poly2.getCentroid()));
System.out.println(isoVist.toText());
}
assertFalse(isoVist.contains(poly2.getCentroid()));
}

/**
* Test with geometry with only positive values
* @throws ParseException
*/
@Test
public void testIsoVistNoCross() throws ParseException {
WKTReader wktReader = new WKTReader();
/**
* Test with geometry with only positive values
*
* @throws ParseException
*/
@Test
public void testIsoVistNoCross() throws ParseException {
WKTReader wktReader = new WKTReader();

VisibilityAlgorithm c = new VisibilityAlgorithm(50);
VisibilityAlgorithm c = new VisibilityAlgorithm(50);

Geometry bound = wktReader.read("POLYGON ((0 0, 0 200, 200 200, 200 0, 0 0))");
c.addGeometry(bound);
Geometry poly2 = wktReader.read("POLYGON ((69 90.2, 68 110.2, 87.9 110.5, 88 90.4, 69 90.2))");
c.addGeometry(poly2);
Geometry bound = wktReader.read("POLYGON ((0 0, 0 200, 200 200, 200 0, 0 0))");
c.addGeometry(bound);
Geometry poly2 = wktReader.read("POLYGON ((69 90.2, 68 110.2, 87.9 110.5, 88 90.4, 69 90.2))");
c.addGeometry(poly2);

Polygon isoVist = c.getIsoVist(new Coordinate(100, 100), false);
Polygon isoVist = c.getIsoVist(new Coordinate(100, 100), false);

assertFalse(isoVist.contains(poly2.getCentroid()));
System.out.println(isoVist.toText());
}
assertFalse(isoVist.contains(poly2.getCentroid()));
}

@Test
public void testIsoVistMultiplePoly() throws ParseException {
WKTReader wktReader = new WKTReader();

VisibilityAlgorithm c = new VisibilityAlgorithm(10);
Geometry poly = wktReader.read("MULTIPOLYGON (((1 1, 1 3, 2 4, 3 3, 3 2, 2 2, 1 1)),((3 5, 4 3, 4 2, 3 1, 5 1, 5 4, 3 5)))");
c.addGeometry(poly);
Polygon isoVist = c.getIsoVist(new Coordinate(3.5, 2.5), true);

System.out.println(isoVist.toText());

}
}

0 comments on commit 87f76b2

Please sign in to comment.