FieldTransformGenerator.java

  1. /* Copyright 2002-2017 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. package org.orekit.frames;

  18. import java.util.ArrayList;
  19. import java.util.List;

  20. import org.hipparchus.Field;
  21. import org.hipparchus.RealFieldElement;
  22. import org.orekit.errors.OrekitException;
  23. import org.orekit.errors.OrekitExceptionWrapper;
  24. import org.orekit.time.AbsoluteDate;
  25. import org.orekit.time.FieldAbsoluteDate;
  26. import org.orekit.utils.GenericTimeStampedCache;
  27. import org.orekit.utils.TimeStampedGenerator;

  28. /** Generator to use field transforms in {@link GenericTimeStampedCache}.
  29.  * @see GenericTimeStampedCache
  30.  * @since 9.0
  31.  * @author Luc Maisonobe
  32.  */
  33. public class FieldTransformGenerator<T extends RealFieldElement<T>> implements TimeStampedGenerator<FieldTransform<T>> {

  34.     /** Field to which the elements belong. */
  35.     private final Field<T> field;

  36.     /** Number of neighbors. */
  37.     private int neighborsSize;

  38.     /** Underlying provider. */
  39.     private final TransformProvider provider;

  40.     /** Step size. */
  41.     private final double step;

  42.     /** simple constructor.
  43.      * @param field field to which the elements belong
  44.      * @param neighborsSize number of neighbors
  45.      * @param provider underlying provider
  46.      * @param step step size
  47.      */
  48.     public FieldTransformGenerator(final Field<T> field,
  49.                                    final int neighborsSize,
  50.                                    final TransformProvider provider,
  51.                                    final double step) {
  52.         this.field         = field;
  53.         this.neighborsSize = neighborsSize;
  54.         this.provider      = provider;
  55.         this.step          = step;
  56.     }

  57.     /** {@inheritDoc} */
  58.     public List<FieldTransform<T>> generate(final AbsoluteDate existingDate, final AbsoluteDate date) {

  59.         try {

  60.             final FieldAbsoluteDate<T> fieldDate = new FieldAbsoluteDate<>(field, date);
  61.             final List<FieldTransform<T>> generated = new ArrayList<>();

  62.             if (existingDate == null) {

  63.                 // no prior existing transforms, just generate a first set
  64.                 for (int i = 0; i < neighborsSize; ++i) {
  65.                     generated.add(provider.getTransform(fieldDate.shiftedBy(i * step)));
  66.                 }

  67.             } else {

  68.                 // some transforms have already been generated
  69.                 // add the missing ones up to specified date
  70.                 FieldAbsoluteDate<T> t = new FieldAbsoluteDate<>(field, existingDate);
  71.                 if (date.compareTo(t.toAbsoluteDate()) > 0) {
  72.                     // forward generation
  73.                     do {
  74.                         t = t.shiftedBy(step);
  75.                         generated.add(generated.size(), provider.getTransform(t));
  76.                     } while (t.compareTo(fieldDate) <= 0);
  77.                 } else {
  78.                     // backward generation
  79.                     do {
  80.                         t = t.shiftedBy(-step);
  81.                         generated.add(0, provider.getTransform(t));
  82.                     } while (t.compareTo(fieldDate) >= 0);
  83.                 }

  84.             }

  85.             // return the generated transforms
  86.             return generated;
  87.         } catch (OrekitException oe) {
  88.             throw new OrekitExceptionWrapper(oe);
  89.         }

  90.     }

  91. }