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 */
017 package org.apache.commons.math3.geometry.euclidean.oned;
018
019 import org.apache.commons.math3.geometry.Vector;
020 import org.apache.commons.math3.geometry.partitioning.Hyperplane;
021
022 /** This class represents a 1D oriented hyperplane.
023 * <p>An hyperplane in 1D is a simple point, its orientation being a
024 * boolean.</p>
025 * <p>Instances of this class are guaranteed to be immutable.</p>
026 * @version $Id: OrientedPoint.java 1416643 2012-12-03 19:37:14Z tn $
027 * @since 3.0
028 */
029 public class OrientedPoint implements Hyperplane<Euclidean1D> {
030
031 /** Vector location. */
032 private Vector1D location;
033
034 /** Orientation. */
035 private boolean direct;
036
037 /** Simple constructor.
038 * @param location location of the hyperplane
039 * @param direct if true, the plus side of the hyperplane is towards
040 * abscissas greater than {@code location}
041 */
042 public OrientedPoint(final Vector1D location, final boolean direct) {
043 this.location = location;
044 this.direct = direct;
045 }
046
047 /** Copy the instance.
048 * <p>Since instances are immutable, this method directly returns
049 * the instance.</p>
050 * @return the instance itself
051 */
052 public OrientedPoint copySelf() {
053 return this;
054 }
055
056 /** {@inheritDoc} */
057 public double getOffset(final Vector<Euclidean1D> point) {
058 final double delta = ((Vector1D) point).getX() - location.getX();
059 return direct ? delta : -delta;
060 }
061
062 /** Build a region covering the whole hyperplane.
063 * <p>Since this class represent zero dimension spaces which does
064 * not have lower dimension sub-spaces, this method returns a dummy
065 * implementation of a {@link
066 * org.apache.commons.math3.geometry.partitioning.SubHyperplane SubHyperplane}.
067 * This implementation is only used to allow the {@link
068 * org.apache.commons.math3.geometry.partitioning.SubHyperplane
069 * SubHyperplane} class implementation to work properly, it should
070 * <em>not</em> be used otherwise.</p>
071 * @return a dummy sub hyperplane
072 */
073 public SubOrientedPoint wholeHyperplane() {
074 return new SubOrientedPoint(this, null);
075 }
076
077 /** Build a region covering the whole space.
078 * @return a region containing the instance (really an {@link
079 * IntervalsSet IntervalsSet} instance)
080 */
081 public IntervalsSet wholeSpace() {
082 return new IntervalsSet();
083 }
084
085 /** {@inheritDoc} */
086 public boolean sameOrientationAs(final Hyperplane<Euclidean1D> other) {
087 return !(direct ^ ((OrientedPoint) other).direct);
088 }
089
090 /** Get the hyperplane location on the real line.
091 * @return the hyperplane location
092 */
093 public Vector1D getLocation() {
094 return location;
095 }
096
097 /** Check if the hyperplane orientation is direct.
098 * @return true if the plus side of the hyperplane is towards
099 * abscissae greater than hyperplane location
100 */
101 public boolean isDirect() {
102 return direct;
103 }
104
105 /** Revert the instance.
106 */
107 public void revertSelf() {
108 direct = !direct;
109 }
110
111 }