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  
19  import java.io.Serializable;
20  import java.util.Arrays;
21  import java.util.Comparator;
22  
23  import org.hipparchus.util.FastMath;
24  import org.hipparchus.util.MathUtils;
25  import org.orekit.errors.OrekitIllegalArgumentException;
26  import org.orekit.errors.OrekitMessages;
27  
28  /**
29   * Class for modeling the ground elevation values around a given point.
30   * <p>
31   * Instances of this class can be considered to be immutable
32   * @author Hank Grabowski
33   * @since 6.1
34   */
35  public class ElevationMask implements Serializable {
36  
37      /** Serializable UID. */
38      private static final long serialVersionUID = 20131118L;
39  
40      /** Azimuth-elevation mask. */
41      private final double[][] azelmask;
42  
43      /**
44       * Creates an instance of an Elevation mask based on the passed in parameter.
45       * @param mask azimuth-elevation mask (rad). First column (i.e. mask[i][0])
46       *             should contain azimuth values and the second column (i.e. mask[i][1])
47       *             should contain corresponding elevations
48       */
49      public ElevationMask(final double[][] mask) {
50          this.azelmask = checkMask(mask);
51      }
52  
53      /** Get the interpolated elevation for a given azimuth according to the mask.
54       * @param azimuth azimuth (rad)
55       * @return elevation angle (rad)
56       */
57      public double getElevation(final double azimuth) {
58          double elevation = 0.0;
59          boolean fin = false;
60          for (int i = 1; i < azelmask.length & !fin; i++) {
61              if (azimuth <= azelmask[i][0]) {
62                  fin = true;
63                  final double azd = azelmask[i - 1][0];
64                  final double azf = azelmask[i][0];
65                  final double eld = azelmask[i - 1][1];
66                  final double elf = azelmask[i][1];
67                  elevation = eld + (azimuth - azd) * (elf - eld) / (azf - azd);
68              }
69          }
70          return elevation;
71      }
72  
73      /** Checking and ordering the azimuth-elevation tabulation.
74       * @param azimelev azimuth-elevation tabulation to be checked and ordered
75       * @return ordered azimuth-elevation tabulation ordered
76       */
77      private static double[][] checkMask(final double[][] azimelev) {
78  
79          // Copy of the given mask
80          final double[][] mask = new double[azimelev.length + 2][azimelev[0].length];
81          for (int i = 0; i < azimelev.length; i++) {
82              System.arraycopy(azimelev[i], 0, mask[i + 1], 0, azimelev[i].length);
83              // Reducing azimuth between 0 and 2*Pi
84              mask[i + 1][0] = MathUtils.normalizeAngle(mask[i + 1][0], FastMath.PI);
85          }
86  
87          // Sorting the mask with respect to azimuth
88          Arrays.sort(mask, 1, mask.length - 1, new Comparator<double[]>() {
89              public int compare(final double[] d1, final double[] d2) {
90                  return Double.compare(d1[0], d2[0]);
91              }
92          });
93  
94          // Extending the mask in order to cover [0, 2PI] in azimuth
95          mask[0][0] = mask[mask.length - 2][0] - MathUtils.TWO_PI;
96          mask[0][1] = mask[mask.length - 2][1];
97          mask[mask.length - 1][0] = mask[1][0] + MathUtils.TWO_PI;
98          mask[mask.length - 1][1] = mask[1][1];
99  
100         // Checking the sorted mask: same azimuth modulo 2PI must have same elevation
101         for (int i = 1; i < mask.length; i++) {
102             if (Double.compare(mask[i - 1][0], mask[i][0]) == 0) {
103                 if (Double.compare(mask[i - 1][1], mask[i][1]) != 0) {
104                     throw new OrekitIllegalArgumentException(OrekitMessages.UNEXPECTED_TWO_ELEVATION_VALUES_FOR_ONE_AZIMUTH,
105                                                              mask[i - 1][1], mask[i][1], mask[i][0]);
106                 }
107             }
108         }
109 
110         return mask;
111     }
112 
113 
114 }