1   /* Copyright 2002-2025 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.gnss.antenna;
18  
19  import org.hipparchus.util.FastMath;
20  import org.hipparchus.util.MathUtils;
21  
22  /**
23   * Interpolator for 2D phase center variation data.
24   *
25   * @author Luc Maisonobe
26   * @since 9.2
27   */
28  public class TwoDVariation implements PhaseCenterVariationFunction {
29  
30      /** Start polar angle. */
31      private final double polarStart;
32  
33      /** Step between grid points. */
34      private final double polarStep;
35  
36      /** Step between grid points. */
37      private final double azimuthStep;
38  
39      /** Sampled phase center variations. */
40      private final double[][] variations;
41  
42      /** Simple constructor.
43       * @param polarStart start polar angle
44       * @param polarStep between grid points
45       * @param azimuthStep step between grid points
46       * @param variations sampled phase center variations
47       */
48      public TwoDVariation(final double polarStart, final double polarStep,
49                           final double azimuthStep, final double[][] variations) {
50          this.polarStart  = polarStart;
51          this.polarStep   = polarStep;
52          this.azimuthStep = azimuthStep;
53          this.variations  = new double[variations.length][];
54          for (int i = 0; i < variations.length; ++i) {
55              this.variations[i] = variations[i].clone();
56          }
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public double value(final double polarAngle, final double azimuthAngle) {
62  
63          // find surrounding points
64          final double az      = MathUtils.normalizeAngle(azimuthAngle, FastMath.PI);
65          final int    iBase   = (int) FastMath.floor(az / azimuthStep);
66          final int    i       = FastMath.max(0, FastMath.min(variations.length - 2, iBase));
67          final int    jBase   = (int) FastMath.floor((polarAngle - polarStart) / polarStep);
68          final int    j       = FastMath.max(0, FastMath.min(variations[i].length - 2, jBase));
69  
70          final double aInf    = i * azimuthStep;
71          final double aSup    = aInf + azimuthStep;
72          final double pInf    = polarStart + j * polarStep;
73          final double pSup    = pInf + polarStep;
74  
75          final double vInfInf = variations[i][j];
76          final double vInfSup = variations[i][j + 1];
77          final double vSupInf = variations[i + 1][j];
78          final double vSupSup = variations[i + 1][j + 1];
79  
80          // bilinear interpolation
81          final double vInf = ((polarAngle - pInf) * vInfSup + (pSup - polarAngle) * vInfInf) / polarStep;
82          final double vSup = ((polarAngle - pInf) * vSupSup + (pSup - polarAngle) * vSupInf) / polarStep;
83          return ((az - aInf) * vSup + (aSup - az) * vInf) / azimuthStep;
84  
85      }
86  
87  }