-
Notifications
You must be signed in to change notification settings - Fork 482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Change float to double in vec2d #652
Changes from all commits
2c556fd
5db3121
b91c435
c368dc0
05f4ef4
a980cfe
ac0b73a
640ff76
3e00300
1c74af4
f88783f
46b741e
01ad59e
8138da1
de903f2
a09e1c9
6410823
e7d72e9
0d038fa
d22a6b9
f8f4589
2539aa2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# miscapps | ||
|
||
These apps generate data, either static tables used in the library or test data used in regression tests. | ||
|
||
## Generate Test Data | ||
|
||
For a specific base cell at a specific res: | ||
|
||
./bin/cellToLatLngHier -p 800bfffffffffff -r 8 > tests/inputfiles/bc05r08centers.txt | ||
./bin/cellToBoundaryHier -p 800bfffffffffff -r 8 > tests/inputfiles/bc05r08cells.txt | ||
|
||
For all cells at a given res: | ||
|
||
./bin/h3ToHier -r 0 | xargs -I {} ./bin/cellToBoundaryHier -p {} -r 3 > tests/inputfiles/res03cells.txt | ||
|
||
For a random sample of cells at a given res: | ||
|
||
./bin/mkRandGeoBoundary -n 5000 -r 5 > tests/inputfiles/rand05cells.txt | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2017-2021 Uber Technologies, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <float.h> | ||
#include <stdlib.h> | ||
|
||
#include "bbox.h" | ||
#include "h3api.h" | ||
#include "latLng.h" | ||
#include "polygon.h" | ||
#include "test.h" | ||
#include "utility.h" | ||
|
||
SUITE(cellToBoundaryEdgeCases) { | ||
TEST(doublePrecisionVertex) { | ||
// The carefully constructed case here: | ||
// - A res 1 pentagon cell with distortion vertexes that change | ||
// when we use a double instead of a float in _v2dIntersect | ||
// - One of the previous (float-based) distortion vertexes | ||
// This is the only case yet found where a point indexed to the | ||
// cell is shown to be incorrectly outside of the geo boundary | ||
// when we use the float version. Presumably more could be found. | ||
H3Index cell = 0x81083ffffffffff; | ||
LatLng point = {.lat = H3_EXPORT(degsToRads)(61.890838431), | ||
.lng = H3_EXPORT(degsToRads)(8.644221328)}; | ||
|
||
CellBoundary boundary; | ||
t_assertSuccess(H3_EXPORT(cellToBoundary)(cell, &boundary)); | ||
|
||
LatLng *verts = boundary.verts; | ||
GeoLoop geoloop = {.numVerts = boundary.numVerts, .verts = verts}; | ||
|
||
BBox bbox; | ||
bboxFromGeoLoop(&geoloop, &bbox); | ||
|
||
H3Index cell2; | ||
t_assertSuccess(H3_EXPORT(latLngToCell)(&point, 1, &cell2)); | ||
// Check whether the point is physically inside the geo boundary | ||
if (cell2 == cell) { | ||
t_assert(pointInsideGeoLoop(&geoloop, &bbox, &point), | ||
"Boundary contains input point"); | ||
} else { | ||
t_assert(!pointInsideGeoLoop(&geoloop, &bbox, &point), | ||
"Boundary does not contain input point"); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -308,4 +308,69 @@ SUITE(cellsToLinkedMultiPolygon) { | |
&polygon) == E_FAILED, | ||
"invalid cells fail"); | ||
} | ||
|
||
TEST(gridDiskResolutions) { | ||
// This is a center-face base cell, no pentagon siblings | ||
H3Index baseCell = 0x8073fffffffffff; | ||
H3Index origin = baseCell; | ||
|
||
H3Index indexes[19] = {0}; | ||
int numHexes = 7; | ||
|
||
for (int res = 1; res < 15; res++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that even with this fix, both of these tests fail at res 0. I'm not sure where the additional imprecision lies for that case, but I think we need vertex mode to fix it. |
||
// Take the 2-disk of the center child at res | ||
t_assertSuccess( | ||
H3_EXPORT(cellToCenterChild)(baseCell, res, &origin)); | ||
t_assertSuccess(H3_EXPORT(gridDisk)(origin, 2, indexes)); | ||
|
||
// Test the polygon output | ||
LinkedGeoPolygon polygon; | ||
t_assertSuccess(H3_EXPORT(cellsToLinkedMultiPolygon)( | ||
indexes, numHexes, &polygon)); | ||
t_assert(countLinkedPolygons(&polygon) == 1, "1 polygon added"); | ||
t_assert(countLinkedLoops(&polygon) == 1, | ||
"1 loop on the first polygon"); | ||
t_assert(countLinkedCoords(polygon.first) == 18, | ||
"All coords for all hexes added to first loop"); | ||
|
||
H3_EXPORT(destroyLinkedMultiPolygon)(&polygon); | ||
} | ||
} | ||
|
||
TEST(gridDiskResolutionsPentagon) { | ||
// This is a pentagon base cell | ||
H3Index baseCell = 0x8031fffffffffff; | ||
H3Index origin = baseCell; | ||
|
||
H3Index diskIndexes[7] = {0}; | ||
H3Index indexes[6] = {0}; | ||
|
||
for (int res = 1; res < 15; res++) { | ||
// Take the 1-disk of the center child at res. Note: We can't take | ||
// the 2-disk here, as increased distortion around the pentagon will | ||
// still fail at res 1. TODO: Use a 2-ring, start at res 0 | ||
// when output is correct. | ||
t_assertSuccess( | ||
H3_EXPORT(cellToCenterChild)(baseCell, res, &origin)); | ||
t_assertSuccess(H3_EXPORT(gridDisk)(origin, 1, diskIndexes)); | ||
|
||
int j = 0; | ||
for (int i = 0; i < 7; i++) { | ||
if (diskIndexes[i]) indexes[j++] = diskIndexes[i]; | ||
} | ||
t_assert(j == 6, "Filled all 6 indexes"); | ||
|
||
// Test the polygon output | ||
LinkedGeoPolygon polygon; | ||
t_assertSuccess( | ||
H3_EXPORT(cellsToLinkedMultiPolygon)(indexes, 6, &polygon)); | ||
t_assert(countLinkedPolygons(&polygon) == 1, "1 polygon added"); | ||
t_assert(countLinkedLoops(&polygon) == 1, | ||
"1 loop on the first polygon"); | ||
t_assert(countLinkedCoords(polygon.first) == 15, | ||
"All coords for all hexes added to first loop"); | ||
|
||
H3_EXPORT(destroyLinkedMultiPolygon)(&polygon); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -747,8 +747,8 @@ void _faceIjkToCellBoundary(const FaceIJK *h, int res, int start, int length, | |
adjacent hexagon edge will lie completely on a single icosahedron | ||
face, and no additional vertex is required. | ||
*/ | ||
bool isIntersectionAtVertex = | ||
_v2dEquals(&orig2d0, &inter) || _v2dEquals(&orig2d1, &inter); | ||
bool isIntersectionAtVertex = _v2dAlmostEquals(&orig2d0, &inter) || | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this remove the only two usage of So I guess you could either rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point, will drop the exact version |
||
_v2dAlmostEquals(&orig2d1, &inter); | ||
if (!isIntersectionAtVertex) { | ||
_hex2dToGeo(&inter, centerIJK.face, adjRes, 1, | ||
&g->verts[g->numVerts]); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ | |
|
||
#include "vec2d.h" | ||
|
||
#include <float.h> | ||
#include <math.h> | ||
#include <stdio.h> | ||
|
||
|
@@ -46,7 +47,7 @@ void _v2dIntersect(const Vec2d *p0, const Vec2d *p1, const Vec2d *p2, | |
s2.x = p3->x - p2->x; | ||
s2.y = p3->y - p2->y; | ||
|
||
float t; | ||
double t; | ||
t = (s2.x * (p0->y - p2->y) - s2.y * (p0->x - p2->x)) / | ||
(-s2.x * s1.y + s1.x * s2.y); | ||
|
||
|
@@ -55,12 +56,12 @@ void _v2dIntersect(const Vec2d *p0, const Vec2d *p1, const Vec2d *p2, | |
} | ||
|
||
/** | ||
* Whether two 2D vectors are equal. Does not consider possible false | ||
* negatives due to floating-point errors. | ||
* Whether two 2D vectors are almost equal, within some threshold | ||
* @param v1 First vector to compare | ||
* @param v2 Second vector to compare | ||
* @return Whether the vectors are equal | ||
* @return Whether the vectors are almost equal | ||
*/ | ||
bool _v2dEquals(const Vec2d *v1, const Vec2d *v2) { | ||
return v1->x == v2->x && v1->y == v2->y; | ||
bool _v2dAlmostEquals(const Vec2d *v1, const Vec2d *v2) { | ||
return fabs(v1->x - v2->x) < FLT_EPSILON && | ||
fabs(v1->y - v2->y) < FLT_EPSILON; | ||
} | ||
Comment on lines
+64
to
67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this function do comparisons in single precision float instead of double? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this specifically how rand05cells.txt was generated in this PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes