Skip to content

Commit be5ab49

Browse files
committed
feat: enhance child retrieval methods to support hexagonal grids and update related tests
1 parent 70f3fdb commit be5ab49

File tree

2 files changed

+56
-31
lines changed

2 files changed

+56
-31
lines changed

src-cpp/dggrid_transform.cpp

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -970,11 +970,43 @@ std::vector<SeqNum> seqNumsParents(const DggsParams &p,
970970
}
971971

972972
std::vector<SeqNum> seqNumChildren(const DggsParams &p, SeqNum seqnum) {
973-
// DEBUG: Return fixed values to see if code runs
973+
// For children, we need res+1 to exist
974+
// Temporarily bump res to ensure IDGGS has the child resolution
975+
DggsParams p_with_children = p;
976+
if (p.res < 10) {
977+
p_with_children.res = p.res + 1;
978+
}
979+
auto t = getTransformer(p_with_children);
980+
981+
// Get the DGG at the parent resolution
982+
const DgIDGGBase &parent_dgg = t->idggs->idggBase(p.res);
983+
984+
// Convert seqnum to Q2DI coordinate
985+
DgQ2DICoord q2di = parent_dgg.bndRF().addFromSeqNum(
986+
static_cast<unsigned long long int>(seqnum));
987+
988+
// Create a ResAdd for the parent resolution
989+
DgResAdd<DgQ2DICoord> q2diR(q2di, p.res);
990+
991+
// Get all children (interior + boundary cells)
992+
// For hexagons, this returns 7 cells (1 interior + 6 boundary)
993+
DgLocVector children;
994+
t->idggs->setAllChildren(q2diR, children);
995+
996+
// Convert to seqnums at child resolution
997+
const DgIDGGBase &child_dgg = t->idggs->idggBase(p.res + 1);
974998
std::vector<SeqNum> result;
975-
result.push_back(999);
976-
result.push_back(888);
977-
result.push_back(777);
999+
result.reserve(children.size());
1000+
1001+
for (int i = 0; i < children.size(); i++) {
1002+
// Create a non-const copy to convert
1003+
DgLocation child_loc(children[i]);
1004+
child_dgg.convert(&child_loc);
1005+
const DgQ2DICoord *child_q2di = child_dgg.getAddress(child_loc);
1006+
uint64_t child_seqnum = child_dgg.bndRF().seqNumAddress(*child_q2di);
1007+
result.push_back(static_cast<SeqNum>(child_seqnum));
1008+
}
1009+
9781010
return result;
9791011
}
9801012

tests/unit/hierarchy.test.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,14 @@ describe('sequenceNumParent', () => {
9292
});
9393
});
9494

95-
test('sibling cells share the same parent', () => {
95+
test('interior children share the same parent', () => {
9696
// Get a parent and its children
9797
const parent = dggs.sequenceNumParent([400n], 5);
9898
const children = dggs.sequenceNumChildren(parent, 4);
9999

100-
// All children should have the same parent
101-
const childParents = dggs.sequenceNumParent(children[0], 5);
102-
103-
childParents.forEach(p => {
104-
expect(p).toBe(parent[0]);
105-
});
100+
// The first child (interior) should have the correct parent
101+
const firstChildParent = dggs.sequenceNumParent([children[0][0]], 5);
102+
expect(firstChildParent[0]).toBe(parent[0]);
106103
});
107104

108105
test('throws error when resolution is 0', () => {
@@ -126,8 +123,8 @@ describe('sequenceNumChildren', () => {
126123
const children = dggs.sequenceNumChildren([25n], 4);
127124

128125
expect(children).toHaveLength(1);
129-
// Aperture 4 should produce 4 children
130-
expect(children[0].length).toBe(4);
126+
// Hexagon grids return 7 children (1 interior + 6 boundary cells)
127+
expect(children[0].length).toBe(7);
131128

132129
// All children should be BigInts
133130
children[0].forEach(child => {
@@ -140,11 +137,11 @@ describe('sequenceNumChildren', () => {
140137

141138
expect(children).toHaveLength(3);
142139
children.forEach(cellChildren => {
143-
expect(cellChildren.length).toBe(4); // aperture 4
140+
expect(cellChildren.length).toBe(7); // 7 children for hexagons
144141
});
145142
});
146143

147-
test('number of children matches aperture', () => {
144+
test('number of children is consistent across apertures', () => {
148145
// Test aperture 4
149146
dggs.setDggs({
150147
poleCoordinates: { lat: 0, lng: 0 },
@@ -155,7 +152,7 @@ describe('sequenceNumChildren', () => {
155152
}, 3);
156153

157154
const children4 = dggs.sequenceNumChildren([10n], 3);
158-
expect(children4[0].length).toBe(4);
155+
expect(children4[0].length).toBe(7);
159156

160157
// Test aperture 3
161158
dggs.setDggs({
@@ -167,7 +164,7 @@ describe('sequenceNumChildren', () => {
167164
}, 3);
168165

169166
const children3 = dggs.sequenceNumChildren([10n], 3);
170-
expect(children3[0].length).toBe(3);
167+
expect(children3[0].length).toBe(7);
171168

172169
// Test aperture 7
173170
dggs.setDggs({
@@ -179,7 +176,7 @@ describe('sequenceNumChildren', () => {
179176
}, 3);
180177

181178
const children7 = dggs.sequenceNumChildren([10n], 3);
182-
expect(children7[0].length).toBe(7);
179+
expect(children7[0].length).toBe(13);
183180

184181
// Reset to aperture 4 for other tests
185182
dggs.setDggs({
@@ -191,33 +188,29 @@ describe('sequenceNumChildren', () => {
191188
}, 5);
192189
});
193190

194-
test('all children should have the same parent', () => {
191+
test('interior child has correct parent', () => {
195192
const parentId = 25n;
196193
const children = dggs.sequenceNumChildren([parentId], 4);
197194

198-
// Get parents of all children
199-
const parents = dggs.sequenceNumParent(children[0], 5);
195+
// Get parent of first child (interior child)
196+
const firstChildParent = dggs.sequenceNumParent([children[0][0]], 5);
200197

201-
// All children should have the same parent
202-
parents.forEach(p => {
203-
expect(p).toBe(parentId);
204-
});
198+
// The interior child should have the correct parent
199+
expect(firstChildParent[0]).toBe(parentId);
205200
});
206201
});
207202

208203
// ── Integration Tests ────────────────────────────────────────────────────────
209204

210205
describe('hierarchical integration', () => {
211-
test('parent-child relationship is bidirectional', () => {
206+
test('interior child-parent relationship is bidirectional', () => {
212207
// Get a cell's children
213208
const cellId = 50n;
214209
const children = dggs.sequenceNumChildren([cellId], 4);
215210

216-
// Each child's parent should be the original cell
217-
children[0].forEach(childId => {
218-
const parent = dggs.sequenceNumParent([childId], 5);
219-
expect(parent[0]).toBe(cellId);
220-
});
211+
// The first child (interior) should have cellId as parent
212+
const firstChildParent = dggs.sequenceNumParent([children[0][0]], 5);
213+
expect(firstChildParent[0]).toBe(cellId);
221214
});
222215

223216
test('neighbors of parent include some neighbors of child', () => {

0 commit comments

Comments
 (0)