001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.math.geometry.path;
031
032import java.util.Iterator;
033import java.util.List;
034
035import org.openimaj.math.geometry.line.Line2d;
036import org.openimaj.math.geometry.point.Point2d;
037import org.openimaj.math.geometry.point.PointList;
038
039/**
040 * A polyline is a {@link Path2d} implicitly made up of {@link Line2d} segments
041 * based on the assumption of ordered points held in a {@link PointList}.
042 *
043 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
044 */
045public class Polyline extends PointList implements Path2d {
046        /**
047         * Construct a {@link Polyline} from points
048         *
049         * @param points
050         *            the points
051         */
052        public Polyline(Point2d... points) {
053                super(points);
054        }
055
056        /**
057         * Construct a {@link Polyline} from points
058         *
059         * @param points
060         *            the points
061         */
062        public Polyline(List<? extends Point2d> points) {
063                this(points, false);
064        }
065
066        /**
067         * Construct a {@link Polyline} from the points, possibly copying the points
068         * first
069         *
070         * @param points
071         *            the points
072         * @param copy
073         *            should the points be copied
074         */
075        public Polyline(List<? extends Point2d> points, boolean copy) {
076                super(points, copy);
077        }
078
079        /**
080         * Construct a {@link Polyline} from line segments
081         *
082         * @param lineIterator
083         *            a line segment iterator
084         */
085        public Polyline(Iterator<Line2d> lineIterator) {
086                Point2d end = null;
087                while (lineIterator.hasNext()) {
088                        final Line2d line = lineIterator.next();
089                        this.points.add(line.begin);
090                        end = line.end;
091                }
092                if (end != null)
093                        this.points.add(end);
094        }
095
096        @Override
097        public Point2d begin() {
098                return points.get(0);
099        }
100
101        @Override
102        public Point2d end() {
103                return points.get(points.size() - 1);
104        }
105
106        @Override
107        public Polyline asPolyline() {
108                return this;
109        }
110
111        @Override
112        public Iterator<Line2d> lineIterator() {
113                return new Iterator<Line2d>() {
114                        int i = 0;
115
116                        @Override
117                        public boolean hasNext() {
118                                return i < points.size() - 1;
119                        }
120
121                        @Override
122                        public Line2d next() {
123                                final Line2d line = new Line2d(points.get(i), points.get(i + 1));
124                                i++;
125                                return line;
126                        }
127
128                        @Override
129                        public void remove() {
130                                throw new UnsupportedOperationException();
131                        }
132                };
133        }
134
135        @Override
136        public double calculateLength() {
137                double length = 0;
138                for (int i = 0; i < points.size() - 1; i++) {
139                        length += Line2d.distance(points.get(i), points.get(i + 1));
140                }
141                return length;
142        }
143}