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.orekit.propagation.sampling;
18  
19  import org.hipparchus.util.FastMath;
20  import org.orekit.errors.OrekitException;
21  import org.orekit.propagation.SpacecraftState;
22  import org.orekit.time.AbsoluteDate;
23  
24  /**
25   * This class wraps an object implementing {@link OrekitFixedStepHandler}
26   * into a {@link OrekitStepHandler}.
27  
28   * <p>It mirrors the <code>StepNormalizer</code> interface from <a
29   * href="https://hipparchus.org/">Hipparchus</a> but
30   * provides a space-dynamics interface to the methods.</p>
31   * @author Luc Maisonobe
32   */
33  public class OrekitStepNormalizer implements OrekitStepHandler {
34  
35      /** Fixed time step. */
36      private double h;
37  
38      /** Underlying step handler. */
39      private OrekitFixedStepHandler handler;
40  
41      /** Last State vector. */
42      private SpacecraftState lastState;
43  
44      /** Integration direction indicator. */
45      private boolean forward;
46  
47      /** Simple constructor.
48       * @param h fixed time step (sign is not used)
49       * @param handler fixed time step handler to wrap
50       */
51      public OrekitStepNormalizer(final double h, final OrekitFixedStepHandler handler) {
52          this.h         = FastMath.abs(h);
53          this.handler   = handler;
54          this.lastState = null;
55          this.forward   = true;
56      }
57  
58      /** Determines whether this handler needs dense output.
59       * This handler needs dense output in order to provide data at
60       * regularly spaced steps regardless of the steps the propagator
61       * uses, so this method always returns true.
62       * @return always true
63       */
64      public boolean requiresDenseOutput() {
65          return true;
66      }
67  
68      /** {@inheritDoc} */
69      public void init(final SpacecraftState s0, final AbsoluteDate t)
70          throws OrekitException {
71          lastState = null;
72          forward   = true;
73          handler.init(s0, t, h);
74      }
75  
76      /**
77       * Handle the last accepted step.
78       * @param interpolator interpolator for the last accepted step. For
79       * efficiency purposes, the various propagators reuse the same
80       * object on each call, so if the instance wants to keep it across
81       * all calls (for example to provide at the end of the propagation a
82       * continuous model valid throughout the propagation range), it
83       * should build a local copy using the clone method and store this
84       * copy.
85       * @param isLast true if the step is the last one
86       * @throws OrekitException this exception is propagated to the
87       * caller if the underlying user function triggers one
88       */
89      public void handleStep(final OrekitStepInterpolator interpolator, final boolean isLast)
90          throws OrekitException {
91  
92          if (lastState == null) {
93              // initialize lastState in the first step case
94              lastState = interpolator.getPreviousState();
95          }
96  
97          // take the propagation direction into account
98          double step = h;
99          forward = interpolator.isForward();
100         if (!forward) {
101             step = -h;
102         }
103 
104 
105         // use the interpolator to push fixed steps events to the underlying handler
106         AbsoluteDate nextTime = lastState.getDate().shiftedBy(step);
107         boolean nextInStep = forward ^ (nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0);
108         while (nextInStep) {
109 
110             // output the stored previous step
111             handler.handleStep(lastState, false);
112 
113             // store the next step
114             lastState = interpolator.getInterpolatedState(nextTime);
115 
116             // prepare next iteration
117             nextTime = nextTime.shiftedBy(step);
118             nextInStep = forward ^ (nextTime.compareTo(interpolator.getCurrentState().getDate()) > 0);
119 
120         }
121 
122         if (isLast) {
123             // there will be no more steps,
124             // the stored one should be flagged as being the last
125             handler.handleStep(lastState, true);
126         }
127 
128     }
129 
130 }