InsidePointFinder.java

  1. /* Copyright 2002-2025 CS GROUP
  2.  * Licensed to CS GROUP (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.Circle;
  24. import org.hipparchus.geometry.spherical.twod.Edge;
  25. import org.hipparchus.geometry.spherical.twod.S2Point;
  26. import org.hipparchus.geometry.spherical.twod.Sphere2D;
  27. import org.hipparchus.geometry.spherical.twod.SphericalPolygonsSet;
  28. import org.hipparchus.geometry.spherical.twod.SubCircle;
  29. import org.hipparchus.geometry.spherical.twod.Vertex;

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

  39.     /** Zone of interest. */
  40.     private final SphericalPolygonsSet zone;

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

  43.     /** Inside point. */
  44.     private S2Point insidePointFirstChoice;

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

  53.     /** {@inheritDoc} */
  54.     @Override
  55.     public Order visitOrder(final BSPTree<Sphere2D, S2Point, Circle, SubCircle> node) {
  56.         return Order.MINUS_PLUS_SUB;
  57.     }

  58.     /** {@inheritDoc} */
  59.     @Override
  60.     public void visitInternalNode(final BSPTree<Sphere2D, S2Point, Circle, SubCircle> node) {
  61.     }

  62.     /** {@inheritDoc} */
  63.     @Override
  64.     public void visitLeafNode(final BSPTree<Sphere2D, S2Point, Circle, SubCircle> node) {

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

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

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

  76.             // extract the start of the single loop boundary of the convex cell
  77.             final List<Vertex> boundary = convex.getBoundaryLoops();
  78.             final Vertex start = boundary.get(0);
  79.             int n = 0;

  80.             // Initialize centroid coordinates
  81.             Vector3D centroid = Vector3D.ZERO;

  82.             // Iterate through each edge in the boundary loop
  83.             for (Edge e = start.getOutgoing(); n == 0 || e.getStart() != start; e = e.getEnd().getOutgoing()) {
  84.                 // Get the 3D coordinates of the start and end points of the edge
  85.                 final Vector3D startPoint = e.getStart().getLocation().getVector();
  86.                 final Vector3D endPoint   = e.getEnd().getLocation().getVector();

  87.                 // Add the coordinates of the start and end points to the centroid
  88.                 centroid = centroid.add(startPoint).add(endPoint);

  89.                 // Increment the counter
  90.                 n++;
  91.             }

  92.             // Calculate the average centroid coordinates
  93.             centroid = centroid.scalarMultiply(1.0 / (2 * n));

  94.             // Project the centroid coordinates onto the sphere to get the candidate point
  95.             final S2Point candidate = new S2Point(centroid.normalize());

  96.             // check the candidate point is really considered inside
  97.             // it may appear outside if the current leaf cell is very thin
  98.             // and checkPoint selects another (very close) tree leaf node
  99.             if (zone.checkPoint(candidate) == Location.INSIDE) {
  100.                 insidePointFirstChoice = candidate;
  101.             } else {
  102.                 insidePointSecondChoice = candidate;
  103.             }

  104.         }

  105.     }

  106.     /** Get the inside point.
  107.      * @return inside point, or null if the region is empty
  108.      */
  109.     public S2Point getInsidePoint() {
  110.         return insidePointFirstChoice != null ? insidePointFirstChoice : insidePointSecondChoice;
  111.     }

  112. }