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  
18  package org.orekit.frames;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import org.orekit.errors.OrekitException;
24  import org.orekit.errors.OrekitExceptionWrapper;
25  import org.orekit.time.AbsoluteDate;
26  import org.orekit.utils.GenericTimeStampedCache;
27  import org.orekit.utils.TimeStampedGenerator;
28  
29  /** Generator to use transforms in {@link GenericTimeStampedCache}.
30   * @see GenericTimeStampedCache
31   * @since 9.0
32   * @author Luc Maisonobe
33   */
34  public class TransformGenerator implements TimeStampedGenerator<Transform> {
35  
36      /** Number of neighbors. */
37      private int neighborsSize;
38  
39      /** Underlying provider. */
40      private final TransformProvider provider;
41  
42      /** Step size. */
43      private final double step;
44  
45      /** simple constructor.
46       * @param neighborsSize number of neighbors
47       * @param provider underlying provider
48       * @param step step size
49       */
50      public TransformGenerator(final int neighborsSize,
51                                final TransformProvider provider,
52                                final double step) {
53          this.neighborsSize = neighborsSize;
54          this.provider      = provider;
55          this.step          = step;
56      }
57  
58      /** {@inheritDoc} */
59      public List<Transform> generate(final AbsoluteDate existingDate, final AbsoluteDate date) {
60  
61          try {
62              final List<Transform> generated = new ArrayList<>();
63  
64              if (existingDate == null) {
65  
66                  // no prior existing transforms, just generate a first one
67                  for (int i = 0; i < neighborsSize; ++i) {
68                      generated.add(provider.getTransform(date.shiftedBy(i * step)));
69                  }
70  
71              } else {
72  
73                  // some transforms have already been generated
74                  // add the missing ones up to specified date
75                  AbsoluteDate t = existingDate;
76                  if (date.compareTo(t) > 0) {
77                      // forward generation
78                      do {
79                          t = t.shiftedBy(step);
80                          generated.add(generated.size(), provider.getTransform(t));
81                      } while (t.compareTo(date) <= 0);
82                  } else {
83                      // backward generation
84                      do {
85                          t = t.shiftedBy(-step);
86                          generated.add(0, provider.getTransform(t));
87                      } while (t.compareTo(date) >= 0);
88                  }
89  
90              }
91  
92              // return the generated transforms
93              return generated;
94          } catch (OrekitException oe) {
95              throw new OrekitExceptionWrapper(oe);
96          }
97  
98      }
99  
100 }