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.partitioning.test;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.geometry.core.RegionLocation;
24  import org.apache.commons.geometry.core.Transform;
25  import org.apache.commons.geometry.core.partitioning.Hyperplane;
26  import org.apache.commons.geometry.core.partitioning.HyperplaneConvexSubset;
27  import org.apache.commons.geometry.core.partitioning.HyperplaneSubset;
28  import org.apache.commons.geometry.core.partitioning.Split;
29  
30  /** Class containing a collection line segments. This class should only be used for
31   * testing purposes.
32   */
33  public final class TestLineSegmentCollection implements HyperplaneSubset<TestPoint2D> {
34      /** The collection of line-segments making up the subset.
35       */
36      private final List<TestLineSegment> segments;
37  
38      /** Create a new instance with the given line segments. The segments
39       * are all assumed to lie on the same hyperplane.
40       * @param segments the segments to use in the collection
41       * @throws IllegalArgumentException if the collection is null or empty
42       */
43      public TestLineSegmentCollection(final List<TestLineSegment> segments) {
44          this.segments = Collections.unmodifiableList(new ArrayList<>(segments));
45      }
46  
47      /** Get the list of line segments comprising the collection.
48       * @return the list of line segments in the collection
49       */
50      public List<TestLineSegment> getLineSegments() {
51          return segments;
52      }
53  
54      /** {@inheritDoc} */
55      @Override
56      public Hyperplane<TestPoint2D> getHyperplane() {
57          throw new UnsupportedOperationException();
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public boolean isFull() {
63          for (final TestLineSegment seg : segments) {
64              if (seg.isFull()) {
65                  return true;
66              }
67          }
68          return false;
69      }
70  
71      /** {@inheritDoc} */
72      @Override
73      public boolean isEmpty() {
74          for (final TestLineSegment seg : segments) {
75              if (!seg.isEmpty()) {
76                  return false;
77              }
78          }
79          return true;
80      }
81  
82      /** {@inheritDoc} */
83      @Override
84      public boolean isInfinite() {
85          for (final TestLineSegment seg : segments) {
86              if (seg.isInfinite()) {
87                  return true;
88              }
89          }
90          return false;
91      }
92  
93      /** {@inheritDoc} */
94      @Override
95      public boolean isFinite() {
96          return !isInfinite();
97      }
98  
99      /** {@inheritDoc} */
100     @Override
101     public double getSize() {
102         double size = 0.0;
103 
104         for (final TestLineSegment seg : segments) {
105             size += seg.getSize();
106         }
107 
108         return size;
109     }
110 
111     /** {@inheritDoc} */
112     @Override
113     public TestPoint2D getCentroid() {
114         throw new UnsupportedOperationException();
115     }
116 
117     /** {@inheritDoc} */
118     @Override
119     public Split<TestLineSegmentCollection> split(final Hyperplane<TestPoint2D> splitter) {
120         final List<TestLineSegment> minusList = new ArrayList<>();
121         final List<TestLineSegment> plusList = new ArrayList<>();
122 
123         for (final TestLineSegment segment : segments) {
124             final Split<TestLineSegment> split = segment.split(splitter);
125 
126             if (split.getMinus() != null) {
127                 minusList.add(split.getMinus());
128             }
129 
130             if (split.getPlus() != null) {
131                 plusList.add(split.getPlus());
132             }
133         }
134 
135         final TestLineSegmentCollection minus = minusList.isEmpty() ?
136                 null :
137                 new TestLineSegmentCollection(minusList);
138 
139         final TestLineSegmentCollection plus = plusList.isEmpty() ?
140                 null :
141                 new TestLineSegmentCollection(plusList);
142 
143         return new Split<>(minus, plus);
144     }
145 
146     /** {@inheritDoc} */
147     @Override
148     public RegionLocation classify(final TestPoint2D point) {
149 
150         // simply return the first value that is not outside;
151         // this is decidedly not robust but should work for testing purposes
152         for (final TestLineSegment seg : segments) {
153             final RegionLocation loc = seg.classify(point);
154             if (loc != RegionLocation.OUTSIDE) {
155                 return loc;
156             }
157         }
158 
159         return RegionLocation.OUTSIDE;
160     }
161 
162     /** {@inheritDoc} */
163     @Override
164     public TestPoint2D closest(final TestPoint2D point) {
165         TestPoint2D closest = null;
166         double minDist = -1;
167 
168         for (final TestLineSegment seg : segments) {
169             final TestPoint2D pt = seg.closest(point);
170             final double dist = pt.distance(point);
171             if (minDist < 0 || dist < minDist) {
172                 minDist = dist;
173                 closest = pt;
174             }
175         }
176 
177         return closest;
178 
179     }
180 
181     /** {@inheritDoc} */
182     @Override
183     public List<HyperplaneConvexSubset<TestPoint2D>> toConvex() {
184         return new ArrayList<>(segments);
185     }
186 
187     /** {@inheritDoc} */
188     @Override
189     public HyperplaneSubset<TestPoint2D> transform(final Transform<TestPoint2D> transform) {
190         throw new UnsupportedOperationException();
191     }
192 }