1 /*
2 * Licensed to the Hipparchus project 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 Hipparchus project 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 * https://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.hipparchus.geometry.partitioning;
18
19 import org.hipparchus.geometry.Point;
20 import org.hipparchus.geometry.Space;
21
22 /**
23 * Finder for interior points.
24 * @param <S> Type of the space.
25 * @param <P> Type of the points in space.
26 * @param <H> Type of the hyperplane.
27 * @param <I> Type of the sub-hyperplane.
28 * @since 4.0
29 */
30 public class InteriorPointFinder<S extends Space,
31 P extends Point<S, P>,
32 H extends Hyperplane<S, P, H, I>,
33 I extends SubHyperplane<S, P, H, I>>
34 implements BSPTreeVisitor<S, P, H, I> {
35
36 /** Default point to use for whole space. */
37 private final P defaultPoint;
38
39 /** Selected point. */
40 private BSPTree.InteriorPoint<S, P> selected;
41
42 /**
43 * Simple constructor.
44 * @param defaultPoint default point to use for whole space
45 */
46 public InteriorPointFinder(final P defaultPoint) {
47 this.defaultPoint = defaultPoint;
48 this.selected = null;
49 }
50
51 /** {@inheritDoc} */
52 @Override
53 public Order visitOrder(final BSPTree<S, P, H, I> node) {
54 return Order.MINUS_PLUS_SUB;
55 }
56
57 /** {@inheritDoc} */
58 @Override
59 public void visitInternalNode(final BSPTree<S, P, H, I> node) {
60 // nothing to do
61 }
62
63 /** {@inheritDoc} */
64 @Override
65 public void visitLeafNode(final BSPTree<S, P, H, I> node) {
66 if ((Boolean) node.getAttribute()) {
67 // this is an inside cell, look for the barycenter of edges/facets interior points
68 final BSPTree.InteriorPoint<S, P> interior = node.getInteriorPoint(defaultPoint);
69 if (selected == null || interior.getDistance() > selected.getDistance()) {
70 // this new point is farther away to edges/facets than the selected one, change selection
71 selected = interior;
72 }
73 }
74 }
75
76 /**
77 * Get the point found.
78 * @return found point (null if tree was empty)
79 */
80 public BSPTree.InteriorPoint<S, P> getPoint() {
81 return selected;
82 }
83
84 }