|
| 1 | +/* |
| 2 | + ISC License |
| 3 | +
|
| 4 | + Copyright (c) 2024, Laboratory for Atmospheric and Space Physics, University of Colorado at Boulder |
| 5 | +
|
| 6 | + Permission to use, copy, modify, and/or distribute this software for any |
| 7 | + purpose with or without fee is hereby granted, provided that the above |
| 8 | + copyright notice and this permission notice appear in all copies. |
| 9 | +
|
| 10 | + THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 11 | + WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 12 | + MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 13 | + ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 14 | + WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 15 | + ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 16 | + OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 17 | +
|
| 18 | + */ |
| 19 | + |
| 20 | +#include "architecture/utilities/bilinearInterpolation.hpp" |
| 21 | +#include <gtest/gtest.h> |
| 22 | +#include <random> |
| 23 | + |
| 24 | +std::random_device rd; |
| 25 | +std::default_random_engine generator(rd()); |
| 26 | +std::uniform_real_distribution<double> valueDistribution(-10, 10); |
| 27 | +std::uniform_real_distribution<double> boundDistribution(0, 2); |
| 28 | + |
| 29 | +TEST(BilinearInterpolationTest, HandlesNormalInputs) { |
| 30 | + double x = valueDistribution(generator); |
| 31 | + double x1 = x - boundDistribution(generator); |
| 32 | + double x2 = x + boundDistribution(generator); |
| 33 | + |
| 34 | + double y = valueDistribution(generator); |
| 35 | + double y1 = y - boundDistribution(generator); |
| 36 | + double y2 = y + boundDistribution(generator); |
| 37 | + |
| 38 | + double z11 = valueDistribution(generator); |
| 39 | + double z12 = valueDistribution(generator); |
| 40 | + double z21 = valueDistribution(generator); |
| 41 | + double z22 = valueDistribution(generator); |
| 42 | + |
| 43 | + // Bilinearly interpolate to solve for z |
| 44 | + double z = 1 / ((x2 - x1) * (y2 - y1)) * (z11 * (x2 - x) * (y2 - y) + z21 * (x - x1) * (y2 - y) |
| 45 | + + z12 * (x2 - x) * (y - y1) |
| 46 | + + z22 * (x - x1) * (y - y1)); |
| 47 | + |
| 48 | + EXPECT_EQ(bilinearInterpolation(x1, x2, y1, y2, z11, z12, z21, z22, x, y), z); |
| 49 | +} |
0 commit comments