AggregateBoundedAttitudeProvider.java

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

  18. import java.util.Collection;
  19. import java.util.Map.Entry;
  20. import java.util.NavigableMap;
  21. import java.util.TreeMap;

  22. import org.hipparchus.CalculusFieldElement;
  23. import org.hipparchus.geometry.euclidean.threed.FieldRotation;
  24. import org.hipparchus.geometry.euclidean.threed.Rotation;
  25. import org.orekit.errors.OrekitException;
  26. import org.orekit.errors.OrekitMessages;
  27. import org.orekit.frames.Frame;
  28. import org.orekit.time.AbsoluteDate;
  29. import org.orekit.time.FieldAbsoluteDate;
  30. import org.orekit.utils.FieldPVCoordinatesProvider;
  31. import org.orekit.utils.PVCoordinatesProvider;

  32. /**
  33.  * A {@link BoundedAttitudeProvider} that covers a larger time span from several constituent
  34.  * attitude providers that cover shorter time spans.
  35.  *
  36.  * @author Bryan Cazabonne
  37.  * @since 10.3
  38.  */
  39. public class AggregateBoundedAttitudeProvider implements BoundedAttitudeProvider {

  40.     /** Constituent attitude provider. */
  41.     private final NavigableMap<AbsoluteDate, BoundedAttitudeProvider> providers;

  42.     /**
  43.      * Constructor.
  44.      * @param providers attitude providers that provide the backing data for this instance.
  45.      *                  There must be at least one attitude provider in the collection.
  46.      *                  If there are gaps between the {@link BoundedAttitudeProvider#getMaxDate()}
  47.      *                  of one attitude provider and the {@link BoundedAttitudeProvider#getMinDate()}
  48.      *                  of the next attitude provider an exception may be thrown by any method of
  49.      *                  this class at any time. If there are overlaps between the the {@link
  50.      *                  BoundedAttitudeProvider#getMaxDate()} of one attitude provider and the {@link
  51.      *                  BoundedAttitudeProvider#getMinDate()} of the next attitude provider then the
  52.      *                  attitude provider with the latest {@link BoundedAttitudeProvider#getMinDate()}
  53.      *                  is used.
  54.      */
  55.     public AggregateBoundedAttitudeProvider(final Collection<? extends BoundedAttitudeProvider> providers) {

  56.         // Check if the collection is empty
  57.         if (providers.isEmpty()) {
  58.             throw new OrekitException(OrekitMessages.NOT_ENOUGH_ATTITUDE_PROVIDERS);
  59.         }

  60.         // Initialize map
  61.         this.providers = new TreeMap<>();

  62.         // Loop on providers
  63.         for (final BoundedAttitudeProvider provider : providers) {
  64.             // Fill collection
  65.             this.providers.put(provider.getMinDate(), provider);
  66.         }

  67.     }

  68.     /** {@inheritDoc} */
  69.     @Override
  70.     public Attitude getAttitude(final PVCoordinatesProvider pvProv, final AbsoluteDate date,
  71.                                 final Frame frame) {

  72.         // Get the attitude provider for the given date
  73.         final BoundedAttitudeProvider provider = getAttitudeProvider(date);

  74.         // Build attitude
  75.         return provider.getAttitude(pvProv, date, frame);

  76.     }

  77.     /** {@inheritDoc} */
  78.     @Override
  79.     public <T extends CalculusFieldElement<T>> FieldAttitude<T> getAttitude(final FieldPVCoordinatesProvider<T> pvProv,
  80.                                                                         final FieldAbsoluteDate<T> date, final Frame frame) {

  81.         // Get the attitude provider for the given date
  82.         final BoundedAttitudeProvider provider = getAttitudeProvider(date.toAbsoluteDate());

  83.         // Build attitude
  84.         return provider.getAttitude(pvProv, date, frame);

  85.     }

  86.     /** {@inheritDoc} */
  87.     @Override
  88.     public Rotation getAttitudeRotation(final PVCoordinatesProvider pvProv, final AbsoluteDate date, final Frame frame) {
  89.         return getAttitudeProvider(date).getAttitudeRotation(pvProv, date, frame);
  90.     }

  91.     /** {@inheritDoc} */
  92.     @Override
  93.     public <T extends CalculusFieldElement<T>> FieldRotation<T> getAttitudeRotation(final FieldPVCoordinatesProvider<T> pvProv,
  94.                                                                                     final FieldAbsoluteDate<T> date,
  95.                                                                                     final Frame frame) {
  96.         return getAttitudeProvider(date.toAbsoluteDate()).getAttitudeRotation(pvProv, date, frame);
  97.     }

  98.     @Override
  99.     public AbsoluteDate getMinDate() {
  100.         return providers.firstEntry().getValue().getMinDate();
  101.     }

  102.     /** {@inheritDoc} */
  103.     @Override
  104.     public AbsoluteDate getMaxDate() {
  105.         return providers.lastEntry().getValue().getMaxDate();
  106.     }

  107.     /**
  108.      * Get the attitude provider to use for the given date.
  109.      * @param date of query
  110.      * @return attitude provider to use on date.
  111.      */
  112.     private BoundedAttitudeProvider getAttitudeProvider(final AbsoluteDate date) {
  113.         final Entry<AbsoluteDate, BoundedAttitudeProvider> attitudeEntry = providers.floorEntry(date);
  114.         if (attitudeEntry != null) {
  115.             return attitudeEntry.getValue();
  116.         } else {
  117.             // Let the first attitude provider throw the exception
  118.             return providers.firstEntry().getValue();
  119.         }
  120.     }

  121. }