1   /* Copyright 2022-2025 Romain Serra
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  
18  package org.orekit.propagation.events;
19  
20  import org.hipparchus.geometry.euclidean.threed.Vector3D;
21  import org.junit.jupiter.api.Assertions;
22  import org.junit.jupiter.api.Test;
23  import org.mockito.Mockito;
24  import org.orekit.frames.FramesFactory;
25  import org.orekit.orbits.CartesianOrbit;
26  import org.orekit.propagation.SpacecraftState;
27  import org.orekit.propagation.analytical.KeplerianPropagator;
28  import org.orekit.propagation.events.handlers.EventHandler;
29  import org.orekit.propagation.events.handlers.StopOnIncreasing;
30  import org.orekit.time.AbsoluteDate;
31  import org.orekit.utils.Constants;
32  import org.orekit.utils.PVCoordinatesProvider;
33  import org.orekit.utils.TimeStampedPVCoordinates;
34  
35  class RelativeDistanceDetectorTest {
36  
37      @Test
38      void testCreate() {
39          // GIVEN
40          final double distanceThreshold = 1.;
41          final RelativeDistanceDetector distanceDetector = new RelativeDistanceDetector(
42                  Mockito.mock(PVCoordinatesProvider.class), distanceThreshold);
43          final EventHandler expectedHandler = new StopOnIncreasing();
44          // WHEN
45          final RelativeDistanceDetector detector = distanceDetector.create(distanceDetector.getDetectionSettings(), expectedHandler);
46          // THEN
47          Assertions.assertEquals(expectedHandler, detector.getHandler());
48      }
49  
50      @Test
51      void testGetDistanceThreshold() {
52          // GIVEN
53          final double expectedDistanceThreshold = 1.;
54          final RelativeDistanceDetector distanceDetector = new RelativeDistanceDetector(
55                  Mockito.mock(PVCoordinatesProvider.class), expectedDistanceThreshold);
56          // WHEN
57          final double actualDistanceThreshold = distanceDetector.getDistanceThreshold();
58          // THEN
59          Assertions.assertEquals(expectedDistanceThreshold, actualDistanceThreshold);
60      }
61  
62      @Test
63      void testG() {
64          // GIVEN
65          final double zeroDistanceThreshold = 0.;
66          final CartesianOrbit initialOrbit = createOrbit();
67          final KeplerianPropagator propagator = new KeplerianPropagator(initialOrbit);
68          final RelativeDistanceDetector distanceDetector = new RelativeDistanceDetector(propagator,
69                  zeroDistanceThreshold);
70          // WHEN
71          final double g = distanceDetector.g(new SpacecraftState(initialOrbit));
72          // THEN
73          Assertions.assertEquals(0., g, 1e-8);
74      }
75  
76      private CartesianOrbit createOrbit() {
77          final Vector3D position = new Vector3D(1e7, 2e3, 1e4);
78          final Vector3D velocity = new Vector3D(0., 6e3, -1e2);
79          final TimeStampedPVCoordinates pvCoordinates = new TimeStampedPVCoordinates(AbsoluteDate.ARBITRARY_EPOCH,
80                  position, velocity);
81          return new CartesianOrbit(pvCoordinates, FramesFactory.getGCRF(), Constants.EGM96_EARTH_MU);
82      }
83  
84  }