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

org.opentripplanner.visibility.Polyline 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.Collections;

/**
 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
 */
class Polyline {

    ArrayList vertices = new ArrayList();

    VLPoint get(int i) {
        return vertices.get(i);
    }

    int size() {
        return vertices.size();
    }

    double distance(VLPoint point_temp) {
        return point_temp.distance(this);
    }

    double length() {
        double length_temp = 0;
        for (int i = 1; i <= vertices.size() - 1; i++)
            length_temp += vertices.get(i - 1).distance(vertices.get(i));
        return length_temp;
    }

    double diameter() {
        // Precondition: nonempty Polyline.
        assert (size() > 0);

        double running_max = 0;
        for (int i = 0; i < size() - 1; i++) {
            for (int j = i + 1; j < size(); j++) {
                if (get(i).distance(get(j)) > running_max)
                    running_max = get(i).distance(get(j));
            }
        }
        return running_max;
    }

    BoundingBox bbox() {
        // Precondition: nonempty Polyline.
        assert (vertices.size() > 0);

        BoundingBox bounding_box = new BoundingBox();
        double x_min = vertices.get(0).x, x_max = vertices.get(0).x, y_min = vertices.get(0).y, y_max = vertices
                .get(0).y;
        for (int i = 1; i < vertices.size(); i++) {
            if (x_min > vertices.get(i).x) {
                x_min = vertices.get(i).x;
            }
            if (x_max < vertices.get(i).x) {
                x_max = vertices.get(i).x;
            }
            if (y_min > vertices.get(i).y) {
                y_min = vertices.get(i).y;
            }
            if (y_max < vertices.get(i).y) {
                y_max = vertices.get(i).y;
            }
        }
        bounding_box.x_min = x_min;
        bounding_box.x_max = x_max;
        bounding_box.y_min = y_min;
        bounding_box.y_max = y_max;
        return bounding_box;
    }

    void eliminate_redundant_vertices(double epsilon) {
        // Trivial case
        if (vertices.size() < 3)
            return;

        // Store new minimal length list of vertices
        ArrayList vertices_temp = new ArrayList(vertices.size());

        // Place holders
        int first = 0;
        int second = 1;
        int third = 2;

        // Add first vertex
        vertices_temp.add(get(first));

        while (third < vertices.size()) {
            // if second redundant
            if (new LineSegment(get(first), get(third)).distance(get(second)) <= epsilon) {
                // =>skip it
                second = third;
                third++;
            }
            // else second not redundant
            else {
                // =>add it.
                vertices_temp.add(get(second));
                first = second;
                second = third;
                third++;
            }
        }

        // Add last vertex
        vertices_temp.add(vertices.get(vertices.size() - 1));

        // Update list of vertices
        vertices = vertices_temp;
    }

    void reverse() {
        Collections.reverse(vertices);
    }

    public String toString() {
        String outs = "";
        for (int i = 0; i < size(); i++)
            outs += get(i) + "\n";
        return outs;
    }

    void append(Polyline polyline) {
        vertices.ensureCapacity(vertices.size() + polyline.vertices.size());
        for (int i = 0; i < polyline.vertices.size(); i++) {
            vertices.add(polyline.vertices.get(i));
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy