|
| 1 | +/* |
| 2 | + * Copyright 2026 Uber Technologies, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** @file testVec3.c |
| 18 | + * @brief Tests the Vec3d helpers used by the geodesic polyfill path. |
| 19 | + */ |
| 20 | + |
| 21 | +#include <float.h> |
| 22 | +#include <math.h> |
| 23 | + |
| 24 | +#include "h3Index.h" |
| 25 | +#include "test.h" |
| 26 | +#include "vec3d.h" |
| 27 | + |
| 28 | +SUITE(Vec3d) { |
| 29 | + TEST(dotProduct) { |
| 30 | + Vec3d a = {.x = 1.0, .y = 0.0, .z = 0.0}; |
| 31 | + Vec3d b = {.x = -1.0, .y = 0.0, .z = 0.0}; |
| 32 | + t_assert(vec3Dot(a, b) == -1.0, "dot product matches expected value"); |
| 33 | + } |
| 34 | + |
| 35 | + TEST(crossProductOrthogonality) { |
| 36 | + Vec3d i = {.x = 1.0, .y = 0.0, .z = 0.0}; |
| 37 | + Vec3d j = {.x = 0.0, .y = 1.0, .z = 0.0}; |
| 38 | + Vec3d k = vec3Cross(i, j); |
| 39 | + t_assert(fabs(k.x - 0.0) < DBL_EPSILON, "x component zero"); |
| 40 | + t_assert(fabs(k.y - 0.0) < DBL_EPSILON, "y component zero"); |
| 41 | + t_assert(fabs(k.z - 1.0) < DBL_EPSILON, "z component one"); |
| 42 | + t_assert(fabs(vec3Dot(k, i)) < DBL_EPSILON, "cross is orthogonal to i"); |
| 43 | + t_assert(fabs(vec3Dot(k, j)) < DBL_EPSILON, "cross is orthogonal to j"); |
| 44 | + } |
| 45 | + |
| 46 | + TEST(normalizeAndMagnitude) { |
| 47 | + Vec3d v = {.x = 3.0, .y = -4.0, .z = 12.0}; |
| 48 | + double magSq = vec3NormSq(v); |
| 49 | + t_assert(fabs(magSq - 169.0) < DBL_EPSILON, |
| 50 | + "magnitude squared matches"); |
| 51 | + t_assert(fabs(vec3Norm(v) - 13.0) < DBL_EPSILON, "magnitude matches"); |
| 52 | + |
| 53 | + vec3Normalize(&v); |
| 54 | + t_assert(fabs(vec3Norm(v) - 1.0) < DBL_EPSILON, |
| 55 | + "normalized vector is unit"); |
| 56 | + |
| 57 | + Vec3d zero = {.x = 0.0, .y = 0.0, .z = 0.0}; |
| 58 | + vec3Normalize(&zero); |
| 59 | + t_assert(zero.x == 0.0 && zero.y == 0.0 && zero.z == 0.0, |
| 60 | + "zero vector remains unchanged when normalizing"); |
| 61 | + } |
| 62 | + |
| 63 | + TEST(distance) { |
| 64 | + Vec3d a = {.x = 0.0, .y = 0.0, .z = 0.0}; |
| 65 | + Vec3d b = {.x = 1.0, .y = 2.0, .z = 2.0}; |
| 66 | + t_assert(fabs(vec3DistSq(a, b) - 9.0) < DBL_EPSILON, |
| 67 | + "distance squared matches"); |
| 68 | + } |
| 69 | + |
| 70 | + TEST(latLngToVec3_unitSphere) { |
| 71 | + LatLng geo = {.lat = 0.5, .lng = -1.3}; |
| 72 | + Vec3d v = latLngToVec3(geo); |
| 73 | + t_assert(fabs(vec3Norm(v) - 1.0) < DBL_EPSILON, |
| 74 | + "converted vector lives on the unit sphere"); |
| 75 | + } |
| 76 | + |
| 77 | + TEST(vec3ToCell_invalidRes) { |
| 78 | + Vec3d v = {.x = 1.0, .y = 0.0, .z = 0.0}; |
| 79 | + H3Index out; |
| 80 | + t_assert(vec3ToCell(&v, -1, &out) == E_RES_DOMAIN, |
| 81 | + "negative resolution is rejected"); |
| 82 | + t_assert(vec3ToCell(&v, 16, &out) == E_RES_DOMAIN, |
| 83 | + "resolution above max is rejected"); |
| 84 | + } |
| 85 | + |
| 86 | + TEST(cellToVec3_unitSphere) { |
| 87 | + // cellToVec3 should return a point on the unit sphere. |
| 88 | + LatLng p = {.lat = 0.6, .lng = -1.2}; |
| 89 | + H3Index h; |
| 90 | + t_assertSuccess(H3_EXPORT(latLngToCell)(&p, 5, &h)); |
| 91 | + |
| 92 | + Vec3d v; |
| 93 | + t_assertSuccess(cellToVec3(h, &v)); |
| 94 | + t_assert(fabs(vec3Norm(v) - 1.0) < DBL_EPSILON, |
| 95 | + "cellToVec3 result is on the unit sphere"); |
| 96 | + } |
| 97 | + |
| 98 | + TEST(cellToVec3_matchesCellToLatLng) { |
| 99 | + // vec3ToLatLng(cellToVec3(cell)) should agree with cellToLatLng. |
| 100 | + LatLng p = {.lat = 0.3, .lng = 2.1}; |
| 101 | + H3Index h; |
| 102 | + t_assertSuccess(H3_EXPORT(latLngToCell)(&p, 7, &h)); |
| 103 | + |
| 104 | + Vec3d v; |
| 105 | + t_assertSuccess(cellToVec3(h, &v)); |
| 106 | + LatLng fromVec3 = vec3ToLatLng(v); |
| 107 | + |
| 108 | + LatLng fromCell; |
| 109 | + t_assertSuccess(H3_EXPORT(cellToLatLng)(h, &fromCell)); |
| 110 | + |
| 111 | + t_assert(fabs(fromVec3.lat - fromCell.lat) < DBL_EPSILON, |
| 112 | + "lat matches cellToLatLng"); |
| 113 | + t_assert(fabs(fromVec3.lng - fromCell.lng) < DBL_EPSILON, |
| 114 | + "lng matches cellToLatLng"); |
| 115 | + } |
| 116 | + |
| 117 | + TEST(cellToVec3_roundTrip) { |
| 118 | + // vec3ToCell(cellToVec3(cell)) should return the same cell. |
| 119 | + LatLng p = {.lat = -0.4, .lng = 0.8}; |
| 120 | + H3Index h; |
| 121 | + t_assertSuccess(H3_EXPORT(latLngToCell)(&p, 9, &h)); |
| 122 | + |
| 123 | + Vec3d v; |
| 124 | + t_assertSuccess(cellToVec3(h, &v)); |
| 125 | + |
| 126 | + H3Index h2; |
| 127 | + t_assertSuccess(vec3ToCell(&v, 9, &h2)); |
| 128 | + t_assert(h2 == h, "round-trip through Vec3d returns same cell"); |
| 129 | + } |
| 130 | + |
| 131 | + TEST(cellToVec3_invalidCell) { |
| 132 | + Vec3d v; |
| 133 | + t_assert(cellToVec3(0x7fffffffffffffff, &v) == E_CELL_INVALID, |
| 134 | + "invalid cell gives E_CELL_INVALID"); |
| 135 | + } |
| 136 | + |
| 137 | + TEST(vec3ToCell_nonFinite) { |
| 138 | + H3Index out; |
| 139 | + Vec3d nanX = {.x = NAN, .y = 0.0, .z = 0.0}; |
| 140 | + t_assert(vec3ToCell(&nanX, 0, &out) == E_DOMAIN, "NaN x is rejected"); |
| 141 | + Vec3d infY = {.x = 0.0, .y = INFINITY, .z = 0.0}; |
| 142 | + t_assert(vec3ToCell(&infY, 0, &out) == E_DOMAIN, |
| 143 | + "infinite y is rejected"); |
| 144 | + Vec3d infZ = {.x = 0.0, .y = 0.0, .z = -INFINITY}; |
| 145 | + t_assert(vec3ToCell(&infZ, 0, &out) == E_DOMAIN, |
| 146 | + "infinite z is rejected"); |
| 147 | + } |
| 148 | +} |
0 commit comments