Skip to content

Commit

Permalink
Merge pull request #1567 from anne-glerum/distance_to_polygon
Browse files Browse the repository at this point in the history
Add non-convex polygons to test and remove statement
  • Loading branch information
gassmoeller committed May 12, 2017
2 parents 0ee2b72 + 16058f7 commit 891c4e6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 13 deletions.
3 changes: 1 addition & 2 deletions include/aspect/utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,7 @@ namespace aspect
/**
* Given a 2d point and a list of points which form a polygon, compute the smallest
* distance of the point to the polygon. The sign is negative for points outside of
* the polygon and positive for points inside the polygon. Note that the polygon
* is required to be convex for this function.
* the polygon and positive for points inside the polygon.
*/
template <int dim>
double
Expand Down
30 changes: 25 additions & 5 deletions tests/prm_distance_polygon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,37 @@ int f()
polygon[2] = Point<2>(1.0,1.0);
polygon[3] = Point<2>(0.0,1.0);

// A concave polygon
std::vector< Point<2> > concave_polygon(5);
concave_polygon[0] = Point<2>(0.0,0.0);
concave_polygon[1] = Point<2>(1.0,0.0);
concave_polygon[2] = Point<2>(1.0,1.0);
concave_polygon[3] = Point<2>(0.5,0.5);
concave_polygon[4] = Point<2>(0.0,1.0);

// A selfcrossing polygon
std::vector< Point<2> > crossing_polygon(4);
crossing_polygon[0] = Point<2>(0.0,0.0);
crossing_polygon[1] = Point<2>(1.0,0.0);
crossing_polygon[2] = Point<2>(1.0,-1.0);
crossing_polygon[3] = Point<2>(0.0,1.0);


// Some points inside and outside the polygon
Point<2> points[] = {Point<2>(0.5,-1), Point<2>(0.5,0.5), Point<2>(0.001,0.2), Point<2>(2.0,2.0), Point<2>(0.001,0.75)};
Point<2> points[] = {Point<2>(0.5,-1), Point<2>(0.5,0.5), Point<2>(0.001,0.2), Point<2>(2.0,2.0), Point<2>(0.25,0.70)};

std::cout << "Testing distance to polygon function with the following parameters: (polygon) " << polygon[0] << ", " << polygon[1] << ", " << polygon[2] << ", " << polygon[3]
std::cout << "Testing distance to polygon function with the following parameters: (polygon 1) "
<< polygon[0] << ", " << polygon[1] << ", " << polygon[2] << ", " << polygon[3] << ", "
<< "(polygon 2) " << concave_polygon[0] << ", " << concave_polygon[1] << ", " << concave_polygon[2] << ", " << concave_polygon[3] << ", " << concave_polygon[4] << ", "
<< "(polygon 3) " << crossing_polygon[0] << ", " << crossing_polygon[1] << ", " << crossing_polygon[2] << ", " << crossing_polygon[3]
<< ", (points) "
<< points[0] << ", " << points[1] << ", " << points[2] << ", " << points[3] << ", "
<< points[4] << std::endl;
<< points[0] << ", " << points[1] << ", " << points[2] << ", " << points[3] << ", " << points[4] << std::endl;

for (unsigned int i = 0; i < 5; i++)
{
std::cout << "Minimal distance of point " << points[i] << " to polygon = " << signed_distance_to_polygon<dim>(polygon,points[i]) << std::endl;
std::cout << "Minimal distance of point " << points[i] << " to polygon 1 = " << signed_distance_to_polygon<dim>(polygon,points[i]) << std::endl;
std::cout << "Minimal distance of point " << points[i] << " to polygon 2 = " << signed_distance_to_polygon<dim>(concave_polygon,points[i]) << std::endl;
std::cout << "Minimal distance of point " << points[i] << " to polygon 3 = " << signed_distance_to_polygon<dim>(crossing_polygon,points[i]) << std::endl;
}

exit(0);
Expand Down
22 changes: 16 additions & 6 deletions tests/prm_distance_polygon/screen-output
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@
-----------------------------------------------------------------------------

Loading shared library <./libprm_distance_polygon.so>
Testing distance to polygon function with the following parameters: (polygon) 0 0, 1 0, 1 1, 0 1, (points) 0.5 -1, 0.5 0.5, 0.001 0.2, 2 2, 0.001 0.75
Minimal distance of point 0.5 -1 to polygon = -1
Minimal distance of point 0.5 0.5 to polygon = 0.5
Minimal distance of point 0.001 0.2 to polygon = 0.001
Minimal distance of point 2 2 to polygon = -1.41421
Minimal distance of point 0.001 0.75 to polygon = 0.001
Testing distance to polygon function with the following parameters: (polygon 1) 0 0, 1 0, 1 1, 0 1, (polygon 2) 0 0, 1 0, 1 1, 0.5 0.5, 0 1, (polygon 3) 0 0, 1 0, 1 -1, 0 1, (points) 0.5 -1, 0.5 0.5, 0.001 0.2, 2 2, 0.25 0.7
Minimal distance of point 0.5 -1 to polygon 1 = -1
Minimal distance of point 0.5 -1 to polygon 2 = -1
Minimal distance of point 0.5 -1 to polygon 3 = -0.447214
Minimal distance of point 0.5 0.5 to polygon 1 = 0.5
Minimal distance of point 0.5 0.5 to polygon 2 = 0
Minimal distance of point 0.5 0.5 to polygon 3 = -0.223607
Minimal distance of point 0.001 0.2 to polygon 1 = 0.001
Minimal distance of point 0.001 0.2 to polygon 2 = 0.001
Minimal distance of point 0.001 0.2 to polygon 3 = 0.001
Minimal distance of point 2 2 to polygon 1 = -1.41421
Minimal distance of point 2 2 to polygon 2 = -1.41421
Minimal distance of point 2 2 to polygon 3 = -2.23607
Minimal distance of point 0.25 0.7 to polygon 1 = 0.25
Minimal distance of point 0.25 0.7 to polygon 2 = 0.0353553
Minimal distance of point 0.25 0.7 to polygon 3 = -0.0894427

0 comments on commit 891c4e6

Please sign in to comment.