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