Skip to content

Commit

Permalink
Merge pull request #719 from AVSLab/feature/add-interpolation-functions
Browse files Browse the repository at this point in the history
Feature/add interpolation functions
  • Loading branch information
schaubh authored Jun 14, 2024
2 parents 222fd62 + 81578c7 commit 15014b4
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 10 deletions.
20 changes: 10 additions & 10 deletions docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ def __init__(self, newFiles=False):
def grabRelevantFiles(self,dir_path):
dirs_in_dir = glob(dir_path + '*/')
files_in_dir = glob(dir_path + "*.h")
files_in_dir.extend(glob(dir_path + "*.hpp"))
files_in_dir.extend(glob(dir_path + "*.c"))
files_in_dir.extend(glob(dir_path + "*.cpp"))
files_in_dir.extend(glob(dir_path + "*.py"))
Expand Down Expand Up @@ -346,7 +347,7 @@ def generateAutoDoc(self, path, files_paths):

# Sort the files by language
py_file_paths = sorted([s for s in files_paths if ".py" in s])
c_file_paths = sorted([s for s in files_paths if ".c" in s or ".cpp" in s or ".h" in s])
c_file_paths = sorted([s for s in files_paths if ".c" in s or ".cpp" in s or ".h" in s or ".hpp" in s])

# Create the .rst file for C-Modules

Expand Down Expand Up @@ -380,11 +381,7 @@ def generateAutoDoc(self, path, files_paths):
lines += ".. _" + c_file_basename + pathToFolder.split("/")[-1] + ":\n\n"
else:
lines += ".. _" + c_file_basename + ":\n\n"
if "fswMessages" in src_path \
or "simFswInterfaceMessages" in src_path \
or "simMessages" in src_path\
or "architecture" in src_path\
or "utilities" in src_path:
if "architecture" in src_path or "utilities" in src_path:
lines += c_file_basename + "\n" + "=" * (len(c_file_basename) + 8) + "\n\n"
else:
lines += "Module: " + c_file_basename + "\n" + "=" * (len(c_file_basename) + 8) + "\n\n"
Expand All @@ -398,8 +395,13 @@ def generateAutoDoc(self, path, files_paths):
lines += "----\n\n"

# Link the path with the modules for Breathe
module_files.extend([s for s in c_file_local_paths if c_file_basename in s])
module_files_temp.extend([s for s in c_file_local_paths if c_file_basename in s])
# make sure the list of files match the base name perfectly
# this avoids issues where one file name is contained in another
# file name
c_file_list_coarse = [s for s in c_file_local_paths if c_file_basename in s]
c_file_list = [file_name for file_name in c_file_list_coarse if file_name.rsplit(".", 1)[0] == c_file_basename]
module_files.extend(c_file_list)
module_files_temp.extend(c_file_list)

# Populate the module's .rst
for module_file in module_files_temp:
Expand Down Expand Up @@ -486,7 +488,6 @@ def run(self, srcDir):
shutil.rmtree(officialDoc)
# adjust the fileCrawler path to a local folder to just build a sub-system
breathe_projects_source = fileCrawler.run(officialSrc)
# breathe_projects_source = fileCrawler.run(officialSrc+"/fswAlgorithms/fswMessages")
# breathe_projects_source = fileCrawler.run(officialSrc+"/fswAlgorithms")
# breathe_projects_source = fileCrawler.run(officialSrc+"/simulation/environment")
# breathe_projects_source = fileCrawler.run(officialSrc+"/moduleTemplates")
Expand All @@ -506,4 +507,3 @@ def run(self, srcDir):

# Example of how to link C with Breathe
# breathe_projects_source = {"BasiliskFSW": ("../../src/fswAlgorithms/attControl/mrpFeedback", ['mrpFeedback.c', 'mrpFeedback.h'])}

58 changes: 58 additions & 0 deletions src/architecture/utilities/bilinearInterpolation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
ISC License
Copyright (c) 2024, Laboratory for Atmospheric and Space Physics, University of Colorado at Boulder
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef BILINEARINTERPOLATION_H
#define BILINEARINTERPOLATION_H

#include <cassert>

/*! This function uses bilinear interpolation to solve for the value of an unknown function of two variables f(x,y)
at the point (x,y).
@return double
@param x1 Data point x1
@param x2 Data point x2
@param y1 Data point y1
@param y2 Data point y2
@param z11 Function value at point (x1, y1)
@param z12 Function value at point (x1, y2)
@param z21 Function value at point (x2, y1)
@param z22 Function value at point (x2, y2)
@param x Function x coordinate for interpolation
@param y Function y coordinate for interpolation
*/
double bilinearInterpolation(double x1,
double x2,
double y1,
double y2,
double z11,
double z12,
double z21,
double z22,
double x,
double y) {

assert(x1 < x && x < x2);
assert(y1 < y && y < y2);

return 1 / ((x2 - x1) * (y2 - y1)) * (z11 * (x2 - x) * (y2 - y) + z21 * (x - x1) * (y2 - y)
+ z12 * (x2 - x) * (y - y1)
+ z22 * (x - x1) * (y - y1));
}

#endif // BILINEARINTERPOLATION_H
41 changes: 41 additions & 0 deletions src/architecture/utilities/linearInterpolation.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
ISC License
Copyright (c) 2024, Laboratory for Atmospheric and Space Physics, University of Colorado at Boulder
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef LINEARINTERPOLATION_H
#define LINEARINTERPOLATION_H

#include <cassert>

/*! This function uses linear interpolation to solve for the value of an unknown function of a single variables f(x)
at the point x.
@return double
@param x1 Data point x1
@param x2 Data point x2
@param y1 Function value at point x1
@param y2 Function value at point x2
@param x Function x coordinate for interpolation
*/
double linearInterpolation(double x1, double x2, double y1, double y2, double x) {

assert(x1 < x && x < x2);

return y1 * (x2 - x) / (x2 - x1) + y2 * (x - x1) / (x2 - x1);
}

#endif // LINEARINTERPOLATION_H
10 changes: 10 additions & 0 deletions src/architecture/utilities/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ add_executable(test_avsEigenMRP test_avsEigenMRP.cpp)
target_link_libraries(test_avsEigenMRP GTest::gtest_main)
target_link_libraries(test_avsEigenMRP ArchitectureUtilities)

add_executable(test_linearInterpolation test_linearInterpolation.cpp)
target_link_libraries(test_linearInterpolation GTest::gtest_main)
target_link_libraries(test_linearInterpolation ArchitectureUtilities)

add_executable(test_bilinearInterpolation test_bilinearInterpolation.cpp)
target_link_libraries(test_bilinearInterpolation GTest::gtest_main)
target_link_libraries(test_bilinearInterpolation ArchitectureUtilities)

if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64" AND CMAKE_GENERATOR STREQUAL "Xcode")
set(CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE PRE_TEST)
endif()
Expand All @@ -32,3 +40,5 @@ gtest_discover_tests(test_orbitalMotion)
gtest_discover_tests(test_saturate)
gtest_discover_tests(test_geodeticConversion)
gtest_discover_tests(test_avsEigenMRP)
gtest_discover_tests(test_linearInterpolation)
gtest_discover_tests(test_bilinearInterpolation)
49 changes: 49 additions & 0 deletions src/architecture/utilities/tests/test_bilinearInterpolation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
ISC License
Copyright (c) 2024, Laboratory for Atmospheric and Space Physics, University of Colorado at Boulder
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "architecture/utilities/bilinearInterpolation.hpp"
#include <gtest/gtest.h>
#include <random>

std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_real_distribution<double> valueDistribution(-10, 10);
std::uniform_real_distribution<double> boundDistribution(0, 2);

TEST(BilinearInterpolationTest, HandlesNormalInputs) {
double x = valueDistribution(generator);
double x1 = x - boundDistribution(generator);
double x2 = x + boundDistribution(generator);

double y = valueDistribution(generator);
double y1 = y - boundDistribution(generator);
double y2 = y + boundDistribution(generator);

double z11 = valueDistribution(generator);
double z12 = valueDistribution(generator);
double z21 = valueDistribution(generator);
double z22 = valueDistribution(generator);

// Bilinearly interpolate to solve for z
double z = 1 / ((x2 - x1) * (y2 - y1)) * (z11 * (x2 - x) * (y2 - y) + z21 * (x - x1) * (y2 - y)
+ z12 * (x2 - x) * (y - y1)
+ z22 * (x - x1) * (y - y1));

EXPECT_EQ(bilinearInterpolation(x1, x2, y1, y2, z11, z12, z21, z22, x, y), z);
}
42 changes: 42 additions & 0 deletions src/architecture/utilities/tests/test_linearInterpolation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
ISC License
Copyright (c) 2024, Laboratory for Atmospheric and Space Physics, University of Colorado at Boulder
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "architecture/utilities/linearInterpolation.hpp"
#include <gtest/gtest.h>
#include <random>

std::random_device rd;
std::default_random_engine generator(rd());
std::uniform_real_distribution<double> valueDistribution(-10, 10);
std::uniform_real_distribution<double> boundDistribution(0, 2);

TEST(LinearInterpolationTest, HandlesNormalInputs) {
double x = valueDistribution(generator);
double x1 = x - boundDistribution(generator);
double x2 = x + boundDistribution(generator);

double yUnused = valueDistribution(generator);
double y1 = yUnused - boundDistribution(generator);
double y2 = yUnused + boundDistribution(generator);

// Linearly interpolate to solve for y
double y = y1 * (x2 - x) / (x2 - x1) + y2 * (x - x1) / (x2 - x1);

EXPECT_EQ(linearInterpolation(x1, x2, y1, y2, x), y);
}

0 comments on commit 15014b4

Please sign in to comment.