InsidePointFinder.java

  1. /* Copyright 2002-2018 CS Systèmes d'Information
  2.  * Licensed to CS Systèmes d'Information (CS) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * CS 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.orekit.models.earth.tessellation;

  18. import java.util.List;

  19. import org.hipparchus.geometry.euclidean.threed.Vector3D;
  20. import org.hipparchus.geometry.partitioning.BSPTree;
  21. import org.hipparchus.geometry.partitioning.BSPTreeVisitor;
  22. import org.hipparchus.geometry.partitioning.Region.Location;
  23. import org.hipparchus.geometry.spherical.twod.Edge;
  24. import org.hipparchus.geometry.spherical.twod.S2Point;
  25. import org.hipparchus.geometry.spherical.twod.Sphere2D;
  26. import org.hipparchus.geometry.spherical.twod.SphericalPolygonsSet;
  27. import org.hipparchus.geometry.spherical.twod.Vertex;

  28. /** BSP Tree visitor aimed at finding a point strictly inside a spherical zone.
  29.  * <p>
  30.  * This class is heavily based on the class PropertiesComputer from the
  31.  * Hipparchus library, also distributed under the terms of
  32.  * the Apache Software License V2.
  33.  * </p>
  34.  * @author Luc Maisonobe
  35.  */
  36. class InsideFinder implements BSPTreeVisitor<Sphere2D> {

  37.     /** Zone of interest. */
  38.     private final SphericalPolygonsSet zone;

  39.     /** Inside point. */
  40.     private S2Point insidePointSecondChoice;

  41.     /** Inside point. */
  42.     private S2Point insidePointFirstChoice;

  43.     /** Simple constructor.
  44.      * @param zone zone of interest
  45.      */
  46.     InsideFinder(final SphericalPolygonsSet zone) {
  47.         this.zone                    = zone;
  48.         this.insidePointFirstChoice  = null;
  49.         this.insidePointSecondChoice = null;
  50.     }

  51.     /** {@inheritDoc} */
  52.     @Override
  53.     public Order visitOrder(final BSPTree<Sphere2D> node) {
  54.         return Order.MINUS_PLUS_SUB;
  55.     }

  56.     /** {@inheritDoc} */
  57.     @Override
  58.     public void visitInternalNode(final BSPTree<Sphere2D> node) {
  59.     }

  60.     /** {@inheritDoc} */
  61.     @Override
  62.     public void visitLeafNode(final BSPTree<Sphere2D> node) {

  63.         // we have already found a good point
  64.         if (insidePointFirstChoice != null) {
  65.             return;
  66.         }

  67.         if ((Boolean) node.getAttribute()) {

  68.             // transform this inside leaf cell into a simple convex polygon
  69.             final SphericalPolygonsSet convex =
  70.                     new SphericalPolygonsSet(node.pruneAroundConvexCell(Boolean.TRUE,
  71.                                                                         Boolean.FALSE,
  72.                                                                         null),
  73.                                                                         zone.getTolerance());

  74.             // extract the start of the single loop boundary of the convex cell
  75.             final List<Vertex> boundary = convex.getBoundaryLoops();
  76.             final Vertex start = boundary.get(0);
  77.             int n = 0;
  78.             Vector3D sumB = Vector3D.ZERO;
  79.             for (Edge e = start.getOutgoing(); n == 0 || e.getStart() != start; e = e.getEnd().getOutgoing()) {
  80.                 sumB = new Vector3D(1, sumB, e.getLength(), e.getCircle().getPole());
  81.                 n++;
  82.             }

  83.             final S2Point candidate = new S2Point(sumB);

  84.             // check the candidate point is really considered inside
  85.             // it may appear outside if the current leaf cell is very thin
  86.             // and checkPoint selects another (very close) tree leaf node
  87.             if (zone.checkPoint(candidate) == Location.INSIDE) {
  88.                 insidePointFirstChoice = candidate;
  89.             } else {
  90.                 insidePointSecondChoice = candidate;
  91.             }

  92.         }

  93.     }

  94.     /** Get the inside point.
  95.      * @return inside point, or null if the region is empty
  96.      */
  97.     public S2Point getInsidePoint() {
  98.         return insidePointFirstChoice != null ? insidePointFirstChoice : insidePointSecondChoice;
  99.     }

  100. }