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