LCOV - code coverage report
Current view: top level - unit-tests - geometry_tests.cxx (source / functions) Hit Total Coverage
Test: code_coverage_filter.info Lines: 100 103 97.1 %
Date: 2024-03-26 01:44:15 Functions: 22 22 100.0 %
Branches: 171 448 38.2 %

           Branch data     Line data    Source code
       1                 :            : #include "pdt_plan.h"
       2                 :            : #include "read_mesh.h"
       3                 :            : 
       4                 :            : #include "test_common.h"
       5                 :            : #include "gtest/gtest.h"
       6                 :            : 
       7                 :            : #include <cmath>
       8                 :            : #include <random>
       9                 :            : 
      10                 :            : //! This test tests the functionality of generating a random vector that is orthogonal to another vector in any direction
      11 [ +  - ][ +  - ]:         16 : GTEST_TEST(VectorGeometryTests, GenerateRandomPerpendicularVectorTest)
         [ +  - ][ -  + ]
      12                 :            : {
      13                 :          2 :     unsigned num_tests = 100;
      14                 :            : 
      15         [ +  - ]:          2 :     std::mt19937 mt_random_generator(100); 
      16                 :            :     
      17         [ +  - ]:          2 :     std::uniform_real_distribution<float> rng_f(0.0f, 1000.f);
      18                 :            : 
      19         [ +  + ]:        202 :     for (unsigned i = 0; i < num_tests; i++)
      20                 :            :     {
      21                 :            :         // generate a random vector
      22 [ +  - ][ +  - ]:        400 :         SP_Point rand_end0(rng_f(mt_random_generator), rng_f(mt_random_generator), rng_f(mt_random_generator));
         [ +  - ][ +  - ]
      23 [ +  - ][ +  - ]:        400 :         SP_Point rand_end1(rng_f(mt_random_generator), rng_f(mt_random_generator), rng_f(mt_random_generator));
         [ +  - ][ +  - ]
      24                 :            :         
      25         [ +  - ]:        400 :         SP_Line rand_line(rand_end0, rand_end1); 
      26                 :            : 
      27                 :            :         // get the perpendicular vector
      28         [ +  - ]:        400 :         SP_Point rand_perp = rand_line.get_rand_perpendicular_vector();
      29                 :            : 
      30 [ +  - ][ +  - ]:        200 :         double actual = sp_dot(rand_perp, rand_end1 - rand_end0);
      31                 :            : 
      32 [ +  - ][ -  + ]:        200 :         EXPECT_NEAR(actual, 0.0, 1e-4) << "Testing random perpendicular vectors. Iteration: " << i;
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      33                 :            :     }
      34                 :          2 : }
      35                 :            : 
      36 [ +  - ][ +  - ]:         16 : GTEST_TEST(VectorGeometryTests, PerpendicularDistanceVectorTest)
         [ +  - ][ -  + ]
      37                 :            : {
      38                 :            :     /** This test tests the functionality of calculating the perpendicular distance of a point from a line */
      39                 :          2 :     unsigned num_tests = 100;
      40                 :          2 :     srand(time(NULL));
      41                 :            : 
      42         [ +  + ]:        202 :     for (unsigned i = 0; i < num_tests; i++)
      43                 :            :     {
      44                 :            :         // generate a random vector to act as the point of projection of the point onto the line
      45         [ +  - ]:        400 :         SP_Point rand_cross((1000.f*rand())/RAND_MAX, (1000.f*rand())/RAND_MAX, (1000.f*rand())/RAND_MAX);
      46                 :            : 
      47                 :            :         // generate a random point
      48         [ +  - ]:        400 :         SP_Point rand_point((1000.f*rand())/RAND_MAX, (1000.f*rand())/RAND_MAX, (1000.f*rand())/RAND_MAX);
      49                 :            : 
      50                 :            :         // generate the line from the two points
      51         [ +  - ]:        400 :         SP_Line rand_tmp_line(rand_cross, rand_point);
      52         [ +  - ]:        400 :         SP_Point rand_tmp_vec = rand_tmp_line.get_rand_perpendicular_vector();
      53 [ +  - ][ +  - ]:        400 :         SP_Line rand_line(rand_cross, rand_cross + rand_tmp_vec);
      54                 :            : 
      55                 :            :         // get the perpendicular distance
      56         [ +  - ]:        200 :         double actual = rand_line.get_distance_pt_line(rand_point);
      57         [ +  - ]:        200 :         double expected = rand_cross.get_distance_from(rand_point);
      58                 :            : 
      59                 :            :         // calculate error
      60                 :            :         double err;
      61         [ +  - ]:        200 :         if (actual >= DOUBLE_ACCURACY) {
      62                 :        200 :             err = abs(actual - expected)/actual;
      63         [ #  # ]:          0 :         } else if (expected >= DOUBLE_ACCURACY) {
      64                 :          0 :             err = abs(actual - expected)/expected;
      65                 :          0 :         } else err = 0.0;
      66                 :            : 
      67                 :            :         // check error
      68 [ +  - ][ -  + ]:        200 :         EXPECT_NEAR(err, 0.0, 1e-4) << "Testing the perpendicular distance of a point from a line. Iteration: " << i;
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
      69                 :            :     }
      70                 :          2 : }
      71                 :            : 
      72                 :            : #define PARAM_FILE string(PDT_SPACE_SRC_DIR) + "/unit-tests/input_params/test_params/tumor1_geometry_params.xml"
      73                 :            : #define GOLDEN_FILE string(PDT_SPACE_SRC_DIR) + "/unit-tests/golden_results/Colin27_tagged_tumor_centroids_1.txt"
      74 [ +  - ][ +  - ]:         16 : GTEST_TEST(VectorGeometryTests, ComputeTetraCentroidTest)
         [ +  - ][ -  + ]
      75                 :            : {
      76                 :            :     /**
      77                 :            :      * This test tests the functionality of computing the centroid of a tetrahedron by reading 
      78                 :            :      * the Colin27_tagged_tumor_1 mesh, computes and compares all centroids of all tetrahedra.
      79                 :            :      */
      80                 :            : 
      81                 :            :     // read mesh file
      82 [ +  - ][ +  - ]:          2 :     pdt_plan * plan = new pdt_plan(PARAM_FILE);
         [ +  - ][ +  - ]
      83                 :            : 
      84                 :            :     // plan->set_parameters(MESH_FILE_PATH, DATA_NAME, OPTICAL_FILE, true, "");
      85                 :            :     // plan->read_mesh();
      86         [ +  - ]:          2 :     mesh * data = plan->get_mesh();
      87                 :            : 
      88 [ +  - ][ +  - ]:          4 :     vector<tetrahedron*> tetra_list = data->get_list_of_tetrahedra();
      89                 :            : 
      90                 :            :     // compute centroids of all tetrahedra
      91                 :          4 :     vector<double> actual;
      92         [ +  + ]:     846754 :     for (unsigned i = 0; i < tetra_list.size(); i++) {
      93         [ +  - ]:    1693504 :         SP_Point centroid = tetra_list[i]->compute_centroid_of();
      94 [ +  - ][ +  - ]:     846752 :         actual.push_back(centroid.get_xcoord());
      95 [ +  - ][ +  - ]:     846752 :         actual.push_back(centroid.get_ycoord());
      96 [ +  - ][ +  - ]:     846752 :         actual.push_back(centroid.get_zcoord());
      97                 :            :     }
      98                 :            : 
      99                 :            :     // read golden file
     100 [ +  - ][ +  - ]:          4 :     vector<double> expected = readGoldenResult(GOLDEN_FILE);
                 [ +  - ]
     101                 :            : 
     102                 :            :     // compares and checks if error is less than 0.001%
     103 [ +  - ][ +  - ]:          2 :     compare_results (expected, actual, 0.00001, "centroid");
         [ +  - ][ +  - ]
     104                 :          2 : }
     105                 :            : 
     106                 :            : //! This test test the functionality of finding the shortest distance between two lines 
     107 [ +  - ][ +  - ]:         16 : GTEST_TEST(VectorGeometryTests, ShortestDistanceLines)
         [ +  - ][ -  + ]
     108                 :            : {
     109         [ +  - ]:          2 :     std::mt19937 mt_random_generator(100); 
     110                 :            :     
     111         [ +  - ]:          2 :     std::uniform_real_distribution<float> rng_f(-100.0f, 100.f);
     112                 :            :     
     113                 :            :     // 1- Parallel Lines
     114 [ +  - ][ +  - ]:          4 :     SP_Line l1(SP_Point(125.0f, 73.0f, 50.0f), SP_Point(135.0f, 73.0f, 50.0f)); 
                 [ +  - ]
     115 [ +  - ][ +  - ]:          4 :     SP_Line l2(SP_Point(122.0f, 73.0f, 60.0f), SP_Point(142.0f, 73.0f, 60.0f)); 
         [ +  - ][ +  - ]
     116                 :            : 
     117         [ +  - ]:          2 :     double d = l1.get_distance_from(l2); 
     118 [ +  - ][ -  + ]:          2 :     EXPECT_FLOAT_EQ(d, 10.0f) << "Testing distance between lines " << l1 << " and " << l2;
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
                 [ #  # ]
     119                 :            : 
     120                 :            :     // 2- Intersecting 
     121 [ +  - ][ +  - ]:          2 :     l1 = SP_Line(SP_Point(-2.0f, 0.0f, 0.0f), SP_Point(1.0f, 0.0f, 0.0f));
                 [ +  - ]
     122 [ +  - ][ +  - ]:          2 :     l2 = SP_Line(SP_Point(-1.0f, -1.0f, -1.0f), SP_Point(1.0f, 1.0f, 1.0f));
                 [ +  - ]
     123                 :            : 
     124 [ +  - ][ +  - ]:          2 :     EXPECT_FLOAT_EQ(l1.get_distance_from(l2), 0.0f) << "Testing distance between intersecting lines: " << l1 << " and " << l2;
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     125                 :            : 
     126                 :            :     // 3- Non intersecting and non-parallel lines 
     127 [ +  - ][ +  - ]:          2 :     l2 = SP_Line(SP_Point(-5.0f, 0.0f, 0.0f), SP_Point(1.0f, 5.0f, 5.0f)); 
                 [ +  - ]
     128                 :            : 
     129 [ +  - ][ +  - ]:          2 :     EXPECT_NEAR(l1.get_distance_from(l2), 2.28748, 1e-4) << "Testing distance between lines: " << l1 << " and " << l2;
         [ -  + ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     130                 :            : 
     131                 :            :     // 4- Run random inputs to make sure it does not crash
     132                 :          2 :     unsigned num_tests = 100; 
     133 [ +  + ][ +  - ]:        202 :     for (unsigned i = 0; i < num_tests; i++) 
     134                 :            :     {
     135 [ +  - ][ +  - ]:        200 :         float x1 = rng_f(mt_random_generator), x2 = rng_f(mt_random_generator);
     136 [ +  - ][ +  - ]:        200 :         float x3 = rng_f(mt_random_generator), x4 = rng_f(mt_random_generator);
     137 [ +  - ][ +  - ]:        200 :         float y1 = rng_f(mt_random_generator), y2 = rng_f(mt_random_generator); 
     138 [ +  - ][ +  - ]:        200 :         float y3 = rng_f(mt_random_generator), y4 = rng_f(mt_random_generator); 
     139 [ +  - ][ +  - ]:        200 :         float z1 = rng_f(mt_random_generator), z2 = rng_f(mt_random_generator); 
     140 [ +  - ][ +  - ]:        200 :         float z3 = rng_f(mt_random_generator), z4 = rng_f(mt_random_generator); 
     141                 :            : 
     142 [ +  - ][ +  - ]:        200 :         l1 = SP_Line(SP_Point(x1,y1,z1), SP_Point(x2,y2,z2));
                 [ +  - ]
     143 [ +  - ][ +  - ]:        200 :         l2 = SP_Line(SP_Point(x3,y3,z3), SP_Point(x4,y4,z4));
                 [ +  - ]
     144                 :            :         
     145         [ +  - ]:        200 :         double dist = l1.get_distance_from(l2); 
     146 [ -  + ][ #  # ]:        200 :         ASSERT_TRUE(dist >= 0.0); 
         [ #  # ][ #  # ]
         [ #  # ][ +  - ]
     147                 :            :     }
     148                 :            : }
     149                 :            : 
     150                 :            : //! This test tests the functionality of getting the intersection of a line with a plane
     151 [ +  - ][ +  - ]:         16 : GTEST_TEST(VectorGeometryTests, IntersectionWithPlaneTest) {
         [ +  - ][ -  + ]
     152                 :          2 :     srand(time(NULL));
     153                 :            : 
     154                 :            :     // parallel line with plane
     155 [ +  - ][ +  - ]:          4 :     SP_Line l = SP_Line(SP_Point(1000.0f*rand()/RAND_MAX, 1000.0f*rand()/RAND_MAX, 1000.0f*rand()/RAND_MAX), SP_Point(1000.0f*rand()/RAND_MAX, 1000.0f*rand()/RAND_MAX, 1000.0f*rand()/RAND_MAX));
                 [ +  - ]
     156 [ +  - ][ +  - ]:          4 :     SP_Point n = sp_normalize(l.get_rand_perpendicular_vector());
     157         [ +  - ]:          4 :     SP_Point c = SP_Point(1000.0f*rand()/RAND_MAX, 1000.0f*rand()/RAND_MAX, 1000.0f*rand()/RAND_MAX);
     158         [ +  - ]:          4 :     SP_Point res;
     159                 :            : 
     160         [ +  - ]:          2 :     bool success = l.get_intersection_with_plane(n, c, res);
     161                 :            : 
     162 [ -  + ][ #  # ]:          2 :     EXPECT_FALSE(success) << "Testing intersection of parallel line and plane.";
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     163                 :            : 
     164                 :            :     // single intersection
     165         [ +  - ]:          4 :     SP_Point dir = l.end0 - l.end1;
     166         [ -  + ]:          2 :     do {
     167 [ +  - ][ +  - ]:          2 :         n = sp_normalize(SP_Point(rand(), rand(), rand()));
     168         [ +  - ]:          2 :     } while (sp_dot(n, dir) == 0);
     169                 :          2 :     double dist = 1000.0 * rand()/RAND_MAX;
     170         [ +  - ]:          2 :     dir = sp_normalize(dir);
     171 [ +  - ][ +  - ]:          4 :     SP_Point expected_intersection = l.end0 + dist*dir;
     172 [ +  - ][ +  - ]:          4 :     SP_Line normal = SP_Line(SP_Point(0.0f, 0.0f, 0.0f), n);
     173         [ +  - ]:          2 :     dir = normal.get_rand_perpendicular_vector();
     174 [ +  - ][ +  - ]:          2 :     c = expected_intersection + (rand() * 1000.0/RAND_MAX) * dir;
     175         [ +  - ]:          2 :     success = l.get_intersection_with_plane(n, c, res);
     176                 :            : 
     177 [ -  + ][ #  # ]:          2 :     EXPECT_TRUE(success) << "Testing intersection of line and plane, function returned false.";
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     178 [ +  - ][ +  - ]:          2 :     EXPECT_NEAR(res.get_xcoord(), expected_intersection.get_xcoord(), 5e-3) << "Testing intersection of line and plane, x coordinate.";
         [ +  - ][ -  + ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     179 [ +  - ][ +  - ]:          2 :     EXPECT_NEAR(res.get_ycoord(), expected_intersection.get_ycoord(), 5e-3) << "Testing intersection of line and plane, y coordinate.";
         [ +  - ][ -  + ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     180 [ +  - ][ +  - ]:          2 :     EXPECT_NEAR(res.get_zcoord(), expected_intersection.get_zcoord(), 5e-3) << "Testing intersection of line and plane, z coordinate.";
         [ +  - ][ -  + ]
         [ #  # ][ #  # ]
         [ #  # ][ #  # ]
     181                 :            : 
     182                 :            :     // Run random inputs to make sure it does not crash
     183                 :          2 :     unsigned num_tests = 100; 
     184         [ +  + ]:        202 :     for (unsigned i = 0; i < num_tests; i++) 
     185                 :            :     {
     186                 :        200 :         float x1 = 1000.0f*rand()/RAND_MAX, x2 = 1000.0f*rand()/RAND_MAX;
     187                 :        200 :         float x3 = 1000.0f*rand()/RAND_MAX, x4 = 1000.0f*rand()/RAND_MAX;
     188                 :        200 :         float y1 = 1000.0f*rand()/RAND_MAX, y2 = 1000.0f*rand()/RAND_MAX; 
     189                 :        200 :         float y3 = 1000.0f*rand()/RAND_MAX, y4 = 1000.0f*rand()/RAND_MAX; 
     190                 :        200 :         float z1 = 1000.0f*rand()/RAND_MAX, z2 = 1000.0f*rand()/RAND_MAX; 
     191                 :        200 :         float z3 = 1000.0f*rand()/RAND_MAX, z4 = 1000.0f*rand()/RAND_MAX; 
     192                 :            : 
     193 [ +  - ][ +  - ]:        200 :         l = SP_Line(SP_Point(x1,y1,z1), SP_Point(x2,y2,z2));
                 [ +  - ]
     194         [ +  - ]:        200 :         n = SP_Point(x3,y3,z3);
     195         [ +  - ]:        200 :         c = SP_Point(x4,y4,z4);
     196                 :            :         
     197         [ +  - ]:        200 :         success = l.get_intersection_with_plane(n, c, res);
     198                 :            :     }
     199 [ +  - ][ +  - ]:         10 : }

Generated by: LCOV version 1.12