All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opentripplanner.visibility.VisibilityGraph Maven / Gradle / Ivy

There is a newer version: 2.5.0
Show newest version
package org.opentripplanner.visibility;

import java.util.ArrayList;
import java.util.Collection;

/**
 Ported by David Turner from Visilibity, by Karl J. Obermeyer


 This port undoubtedly introduced a number of bugs (and removed some features).

 Bug reports should be directed to the OpenTripPlanner project, unless they
 can be reproduced in the original VisiLibity
 */
public class VisibilityGraph {

    // the number of vertices in each Polygon of corresponding Environment
    ArrayList vertex_counts;

    // n-by-n adjacency matrix data stored as 2D dynamic array
    boolean[][] adjacency_matrix;

    int n;

    public VisibilityGraph(Environment environment, double epsilon) {
        this(environment, epsilon, null);
    }

    public VisibilityGraph(Environment environment, double epsilon, Collection origins) {
        n = environment.n();

        // fill vertex_counts
        vertex_counts = new ArrayList(environment.h());
        for (int i = 0; i < environment.h(); i++)
            vertex_counts.add(environment.get(i).n());

        adjacency_matrix = new boolean[n][n];
        // fill adjacency matrix by checking for inclusion in the
        // visibility polygons
        for (int k1 = 0; k1 < n; k1++) {
            VLPoint point1 = environment.kth_point(k1);
            if (origins != null && !origins.contains(point1)) 
                    continue;
            VLPolygon polygon_temp = new VisibilityPolygon(point1, environment, epsilon);
            for (int k2 = 0; k2 < n; k2++) {
                if (k1 == k2)
                    adjacency_matrix[k1][k1] = true;
                else {
                    VLPoint point2 = environment.kth_point(k2);
                    if (origins == null || origins.contains(point2)) {
                        adjacency_matrix[k1][k2] = adjacency_matrix[k2][k1] = point2.in(
                                polygon_temp, epsilon);
                    }
                }
            }
        }
    }

    public VisibilityGraph(ArrayList points, Environment environment, double epsilon) {
        vertex_counts = new ArrayList(environment.h());
        n = points.size();

        // fill vertex_counts
        vertex_counts.add(n);

        adjacency_matrix = new boolean[n][n];

        // fill adjacency matrix by checking for inclusion in the
        // visibility polygons
        VLPolygon polygontemp;
        for (int k1 = 0; k1 < n; k1++) {
            polygontemp = new VisibilityPolygon(points.get(k1), environment, epsilon);
            for (int k2 = 0; k2 < n; k2++) {
                if (k1 == k2)
                    adjacency_matrix[k1][k1] = true;
                else
                    adjacency_matrix[k1][k2] = adjacency_matrix[k2][k1] = points.get(k2).in(
                            polygontemp, epsilon);
            }
        }
    }

    public boolean get(int polygon1, int vertex1, int polygon2, int vertex2) {
        return adjacency_matrix[get_vertex_index(polygon1, vertex1)][get_vertex_index(polygon2, vertex2)];
    }

    public boolean get(int k1, int k2) {
        return adjacency_matrix[k1][k2];
    }


    // original code called this two_to_one, incomprehensibly
    public int get_vertex_index(int polygon, int vertex) {
        int k = 0;

        for (int counter = 0; counter < polygon; counter++)
            k += vertex_counts.get(counter);
        k += vertex;

        return k;
    }

    public String toString() {
        String outs = "";
        for (int k1 = 0; k1 < n; k1++) {
            for (int k2 = 0; k2 < n; k2++) {
                outs += get(k1, k2) ? "1" : "0";
                if (k2 < n - 1)
                    outs += "  ";
                else
                    outs += "\n";
            }
        }

        return outs;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy