ElevationMask.java

  1. /* Copyright 2013 Applied Defense Solutions, Inc.
  2.  * Licensed to CS Communication & Systèmes (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.utils;

  18. import java.io.Serializable;
  19. import java.util.Arrays;
  20. import java.util.Comparator;

  21. import org.apache.commons.math3.util.FastMath;
  22. import org.apache.commons.math3.util.MathUtils;
  23. import org.orekit.errors.OrekitException;
  24. import org.orekit.errors.OrekitMessages;

  25. /**
  26.  * Class for modeling the ground elevation values around a given point.
  27.  * <p>
  28.  * Instances of this class can be considered to be immutable
  29.  * @author Hank Grabowski
  30.  * @since 6.1
  31.  */
  32. public class ElevationMask implements Serializable {

  33.     /** Serializable UID. */
  34.     private static final long serialVersionUID = 20131118L;

  35.     /** Azimuth-elevation mask. */
  36.     private final double[][] azelmask;

  37.     /**
  38.      * Creates an instance of an Elevation mask based on the passed in parameter.
  39.      * @param mask azimuth-elevation mask (rad)
  40.      */
  41.     public ElevationMask(final double[][] mask) {
  42.         this.azelmask = checkMask(mask);
  43.     }

  44.     /** Get the interpolated elevation for a given azimuth according to the mask.
  45.      * @param azimuth azimuth (rad)
  46.      * @return elevation angle (rad)
  47.      */
  48.     public double getElevation(final double azimuth) {
  49.         double elevation = 0.0;
  50.         boolean fin = false;
  51.         for (int i = 1; i < azelmask.length & !fin; i++) {
  52.             if (azimuth <= azelmask[i][0]) {
  53.                 fin = true;
  54.                 final double azd = azelmask[i - 1][0];
  55.                 final double azf = azelmask[i][0];
  56.                 final double eld = azelmask[i - 1][1];
  57.                 final double elf = azelmask[i][1];
  58.                 elevation = eld + (azimuth - azd) * (elf - eld) / (azf - azd);
  59.             }
  60.         }
  61.         return elevation;
  62.     }

  63.     /** Checking and ordering the azimuth-elevation tabulation.
  64.      * @param azimelev azimuth-elevation tabulation to be checked and ordered
  65.      * @return ordered azimuth-elevation tabulation ordered
  66.      */
  67.     private static double[][] checkMask(final double[][] azimelev) {

  68.         // Copy of the given mask
  69.         final double[][] mask = new double[azimelev.length + 2][azimelev[0].length];
  70.         for (int i = 0; i < azimelev.length; i++) {
  71.             System.arraycopy(azimelev[i], 0, mask[i + 1], 0, azimelev[i].length);
  72.             // Reducing azimuth between 0 and 2*Pi
  73.             mask[i + 1][0] = MathUtils.normalizeAngle(mask[i + 1][0], FastMath.PI);
  74.         }

  75.         // Sorting the mask with respect to azimuth
  76.         Arrays.sort(mask, 1, mask.length - 1, new Comparator<double[]>() {
  77.             public int compare(final double[] d1, final double[] d2) {
  78.                 return Double.compare(d1[0], d2[0]);
  79.             }
  80.         });

  81.         // Extending the mask in order to cover [0, 2PI] in azimuth
  82.         mask[0][0] = mask[mask.length - 2][0] - MathUtils.TWO_PI;
  83.         mask[0][1] = mask[mask.length - 2][1];
  84.         mask[mask.length - 1][0] = mask[1][0] + MathUtils.TWO_PI;
  85.         mask[mask.length - 1][1] = mask[1][1];

  86.         // Checking the sorted mask: same azimuth modulo 2PI must have same elevation
  87.         for (int i = 1; i < mask.length; i++) {
  88.             if (Double.compare(mask[i - 1][0], mask[i][0]) == 0) {
  89.                 if (Double.compare(mask[i - 1][1], mask[i][1]) != 0) {
  90.                     throw OrekitException.createIllegalArgumentException(OrekitMessages.UNEXPECTED_TWO_ELEVATION_VALUES_FOR_ONE_AZIMUTH, mask[i - 1][1], mask[i][1], mask[i][0]);
  91.                 }
  92.             }
  93.         }

  94.         return mask;
  95.     }


  96. }