View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.geometry.core.internal;
    18
    19  import org.apache.commons.geometry.core.RegionLocation;
    20  import org.apache.commons.geometry.core.partitioning.Hyperplane;
    21  import org.apache.commons.geometry.core.partitioning.HyperplaneBoundedRegion;
    22  import org.apache.commons.geometry.core.partitioning.Split;
    23  import org.apache.commons.geometry.core.partitioning.test.PartitionTestUtils;
    24  import org.apache.commons.geometry.core.partitioning.test.TestLine;
    25  import org.apache.commons.geometry.core.partitioning.test.TestPoint1D;
    26  import org.apache.commons.geometry.core.partitioning.test.TestPoint2D;
    27  import org.junit.jupiter.api.Assertions;
    28  import org.junit.jupiter.api.Test;
    29
    30  class HyperplaneSubsetsTest {
    31
    32      @Test
    33      void testClassify() {
    34          // arrange
    35          final TestLine line = TestLine.X_AXIS;
    36          final StubRegion1D region = new StubRegion1D();
    37
    38          // act/assert
    39          Assertions.assertEquals(RegionLocation.INSIDE,
    40                  HyperplaneSubsets.classifyAgainstEmbeddedRegion(new TestPoint2D(-1, 0), line, region));
    41          Assertions.assertEquals(RegionLocation.BOUNDARY,
    42                  HyperplaneSubsets.classifyAgainstEmbeddedRegion(new TestPoint2D(0, 0), line, region));
    43
    44          Assertions.assertEquals(RegionLocation.OUTSIDE,
    45                  HyperplaneSubsets.classifyAgainstEmbeddedRegion(new TestPoint2D(0, 1), line, region));
    46          Assertions.assertEquals(RegionLocation.OUTSIDE,
    47                  HyperplaneSubsets.classifyAgainstEmbeddedRegion(new TestPoint2D(-1, 1), line, region));
    48          Assertions.assertEquals(RegionLocation.OUTSIDE,
    49                  HyperplaneSubsets.classifyAgainstEmbeddedRegion(new TestPoint2D(-1, -1), line, region));
    50      }
    51
    52      @Test
    53      void testClosest() {
    54          // arrange
    55          final TestLine line = TestLine.X_AXIS;
    56          final StubRegion1D region = new StubRegion1D();
    57          final StubRegion1D emptyRegion = new StubRegion1D(true);
    58
    59          // act/assert
    60          PartitionTestUtils.assertPointsEqual(new TestPoint2D(-1, 0),
    61                  HyperplaneSubsets.closestToEmbeddedRegion(new TestPoint2D(-1, 0), line, region));
    62
    63          PartitionTestUtils.assertPointsEqual(new TestPoint2D(0, 0),
    64                  HyperplaneSubsets.closestToEmbeddedRegion(new TestPoint2D(0, 0), line, region));
    65          PartitionTestUtils.assertPointsEqual(new TestPoint2D(0, 0),
    66                  HyperplaneSubsets.closestToEmbeddedRegion(new TestPoint2D(1, 0), line, region));
    67          PartitionTestUtils.assertPointsEqual(new TestPoint2D(0, 0),
    68                  HyperplaneSubsets.closestToEmbeddedRegion(new TestPoint2D(1, 1), line, region));
    69          PartitionTestUtils.assertPointsEqual(new TestPoint2D(0, 0),
    70                  HyperplaneSubsets.closestToEmbeddedRegion(new TestPoint2D(1, -1), line, region));
    71
    72          PartitionTestUtils.assertPointsEqual(new TestPoint2D(-1, 0),
    73                  HyperplaneSubsets.closestToEmbeddedRegion(new TestPoint2D(-1, 1), line, region));
    74          PartitionTestUtils.assertPointsEqual(new TestPoint2D(-1, 0),
    75                  HyperplaneSubsets.closestToEmbeddedRegion(new TestPoint2D(-1, -1), line, region));
    76
    77          Assertions.assertNull(HyperplaneSubsets.closestToEmbeddedRegion(TestPoint2D.ZERO, line, emptyRegion));
    78      }
    79
    80      /** Stub region implementation. Negative numbers are on the inside of the region.
    81       */
    82      private static class StubRegion1D implements HyperplaneBoundedRegion<TestPoint1D> {
    83
    84          private final boolean empty;
    85
    86          StubRegion1D() {
    87              this(false);
    88          }
    89
    90          StubRegion1D(final boolean empty) {
    91              this.empty = empty;
    92          }
    93
    94          @Override
    95          public boolean isFull() {
    96              throw new UnsupportedOperationException();
    97          }
    98
    99          @Override
    100         public boolean isEmpty() {
    101             throw new UnsupportedOperationException();
    102         }
    103
    104         @Override
    105         public double getSize() {
    106             throw new UnsupportedOperationException();
    107         }
    108
    109         @Override
    110         public double getBoundarySize() {
    111             throw new UnsupportedOperationException();
    112         }
    113
    114         @Override
    115         public TestPoint1D getCentroid() {
    116             throw new UnsupportedOperationException();
    117         }
    118
    119         @Override
    120         public RegionLocation classify(final TestPoint1D pt) {
    121             if (!empty) {
    122                 final double sign = PartitionTestUtils.PRECISION.signum(pt.getX());
    123
    124                 if (sign < 0) {
    125                     return RegionLocation.INSIDE;
    126                 } else if (sign == 0) {
    127                     return RegionLocation.BOUNDARY;
    128                 }
    129             }
    130             return RegionLocation.OUTSIDE;
    131         }
    132
    133         @Override
    134         public TestPoint1D project(final TestPoint1D pt) {
    135             return empty ? null : new TestPoint1D(0);
    136         }
    137
    138         @Override
    139         public Split<? extends HyperplaneBoundedRegion<TestPoint1D>> split(final Hyperplane<TestPoint1D> splitter) {
    140             throw new UnsupportedOperationException();
    141         }
    142     }
    143 }