001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math3.geometry.spherical.twod;
018
019import java.util.ArrayList;
020import java.util.List;
021
022/** Spherical polygons boundary vertex.
023 * @see SphericalPolygonsSet#getBoundaryLoops()
024 * @see Edge
025 * @since 3.3
026 */
027public class Vertex {
028
029    /** Vertex location. */
030    private final S2Point location;
031
032    /** Incoming edge. */
033    private Edge incoming;
034
035    /** Outgoing edge. */
036    private Edge outgoing;
037
038    /** Circles bound with this vertex. */
039    private final List<Circle> circles;
040
041    /** Build a non-processed vertex not owned by any node yet.
042     * @param location vertex location
043     */
044    Vertex(final S2Point location) {
045        this.location = location;
046        this.incoming = null;
047        this.outgoing = null;
048        this.circles  = new ArrayList<Circle>();
049    }
050
051    /** Get Vertex location.
052     * @return vertex location
053     */
054    public S2Point getLocation() {
055        return location;
056    }
057
058    /** Bind a circle considered to contain this vertex.
059     * @param circle circle to bind with this vertex
060     */
061    void bindWith(final Circle circle) {
062        circles.add(circle);
063    }
064
065    /** Get the common circle bound with both the instance and another vertex, if any.
066     * <p>
067     * When two vertices are both bound to the same circle, this means they are
068     * already handled by node associated with this circle, so there is no need
069     * to create a cut hyperplane for them.
070     * </p>
071     * @param vertex other vertex to check instance against
072     * @return circle bound with both the instance and another vertex, or null if the
073     * two vertices do not share a circle yet
074     */
075    Circle sharedCircleWith(final Vertex vertex) {
076        for (final Circle circle1 : circles) {
077            for (final Circle circle2 : vertex.circles) {
078                if (circle1 == circle2) {
079                    return circle1;
080                }
081            }
082        }
083        return null;
084    }
085
086    /** Set incoming edge.
087     * <p>
088     * The circle supporting the incoming edge is automatically bound
089     * with the instance.
090     * </p>
091     * @param incoming incoming edge
092     */
093    void setIncoming(final Edge incoming) {
094        this.incoming = incoming;
095        bindWith(incoming.getCircle());
096    }
097
098    /** Get incoming edge.
099     * @return incoming edge
100     */
101    public Edge getIncoming() {
102        return incoming;
103    }
104
105    /** Set outgoing edge.
106     * <p>
107     * The circle supporting the outgoing edge is automatically bound
108     * with the instance.
109     * </p>
110     * @param outgoing outgoing edge
111     */
112    void setOutgoing(final Edge outgoing) {
113        this.outgoing = outgoing;
114        bindWith(outgoing.getCircle());
115    }
116
117    /** Get outgoing edge.
118     * @return outgoing edge
119     */
120    public Edge getOutgoing() {
121        return outgoing;
122    }
123
124}