Skip to content

Commit

Permalink
Example Test for distance module
Browse files Browse the repository at this point in the history
  • Loading branch information
nguy8tri committed May 25, 2024
1 parent 93c5f5f commit 8c1e92f
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 5 deletions.
5 changes: 2 additions & 3 deletions src/distance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

#include "distance.hpp"

#define RADIUS_OF_EARTH 6378.0

namespace found {
distFromEarth SphericalDistanceDeterminationAlgorithm::Run(char *image,
Points &p, int imageWidth, int imageHeight) {
Expand Down Expand Up @@ -49,7 +47,7 @@ Vec3 center) {
}

decimal SphericalDistanceDeterminationAlgorithm::getDistance(decimal r) {
return RADIUS_OF_EARTH/r;
return radius_/r;
}

distFromEarth SphericalDistanceDeterminationAlgorithm::solve(Points& pts,
Expand All @@ -65,4 +63,5 @@ distFromEarth SphericalDistanceDeterminationAlgorithm::solve(Points& pts,

return center * h;
}

};
4 changes: 2 additions & 2 deletions src/distance.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class DistanceDeterminationAlgorithm {
public:

// Destroys this
virtual ~DistanceDeterminationAlgorithm();
virtual ~DistanceDeterminationAlgorithm() {};

/**
* Computes the distance of the satellite from Earth based on an image of Earth
Expand All @@ -39,7 +39,7 @@ class DistanceDeterminationAlgorithm {
class SphericalDistanceDeterminationAlgorithm : public DistanceDeterminationAlgorithm {
public:
SphericalDistanceDeterminationAlgorithm(float radius, Camera &cam) : cam_(cam), radius_(radius) {};
~SphericalDistanceDeterminationAlgorithm();
~SphericalDistanceDeterminationAlgorithm() {};

/**
* Place documentation here. Press enter to automatically make a new line
Expand Down
105 changes: 105 additions & 0 deletions test/distance-test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@

#include <catch.hpp>
#include <cmath>
#include <iostream>

#include "style.hpp"
#include "attitude-utils.hpp"
#include "camera.hpp"

#include "distance.hpp"


/* Using Directives */
using found::Camera;
using found::Vec3;
using found::Points;
using found::distFromEarth;
using found::decimal;
using found::SphericalDistanceDeterminationAlgorithm;


/* Common Constants */


// Radius of Earth (km)
#define RADIUS_OF_EARTH 6378.0
// Default DoubleEquals Tolerance
#define DEFAULT_TOLERANCE 1e-6


/* Test Macros */

/**
* Requires that vec1 == vec2 (using DecimalEquals)
*
* @param vec1 A Vec3 object
* @param vec2 A Vec3 object
* @param tolerance The tolerance for vec1 to be
* "equal" to vec2
*
* @post Will have REQUIRE'd that vec1 is equal to
* vec2, on a component basis, within tolerance
*/
#define VECTOR_EQUALS(vec1, vec2, tolerance) \
REQUIRE(DecimalEquals(vec1.x, vec2.x, tolerance)); \
REQUIRE(DecimalEquals(vec1.y, vec2.y, tolerance)); \
REQUIRE(DecimalEquals(vec1.z, vec2.z, tolerance));

/**
* Compares 2 decimals to make sure they are "equal"
*
* @param x1 The first decimal to compare
* @param x2 The second decimal to compare
* @param tolerance The maximum allowable error between the two
*
* @return true iff x1 and x2 are the same within tolerance,
* false otherwise
*/
bool DecimalEquals(decimal x1, decimal x2, decimal tolerance) {
return abs(x1 - x2) < tolerance;
}

std::ostream &operator<<(std::ostream &stream, const Vec3 &vector) {
stream << "(" << vector.x << ", " << vector.y << ", " << vector.z << ")";
return stream;
}

// Base Case I: The image captured contains an edge centered about the image

TEST_CASE("Base Case I: Entire Circle I") {
// Step I: Pick some distance (km) and a Camera
decimal x_E = RADIUS_OF_EARTH + 1000;
int imageWidth = 1024;
int imageHeight = 1024;
Camera cam(0.012, imageWidth, imageHeight);
distFromEarth expected = {x_E, 0, 0};

// Step II: Figure out my projection points

// a) Find the angle
decimal alpha = asin(RADIUS_OF_EARTH / x_E);

// b) Find the distance away from each projection point
decimal p = sqrt(x_E * x_E - RADIUS_OF_EARTH * RADIUS_OF_EARTH);

// c) Use 3 easy projections
Vec3 p1 = {p * cos(alpha), p * sin(alpha), 0};
Vec3 p2 = {p * cos(alpha), -p * sin(alpha), 0};
Vec3 p3 = {p * cos(alpha), 0, p * sin(alpha)};

// Step III: Use CTS to convert to 2D vectors
Points pts = {cam.SpatialToCamera(p1),
cam.SpatialToCamera(p2),
cam.SpatialToCamera(p3)};

// Step IV: Run It and Test!
SphericalDistanceDeterminationAlgorithm algo =
SphericalDistanceDeterminationAlgorithm(RADIUS_OF_EARTH, cam);

distFromEarth actual = algo.Run(nullptr, pts, imageWidth, imageHeight);

std::cout << "Actual Result: " << actual << std::endl;

VECTOR_EQUALS(expected, actual, DEFAULT_TOLERANCE);
}

0 comments on commit 8c1e92f

Please sign in to comment.