Skip to content
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

refactor(core): improve grid building routines #209

Merged
merged 8 commits into from
Oct 28, 2024
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
276 changes: 145 additions & 131 deletions honeycomb-core/src/cmap/builder/grid/building_routines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,88 +17,93 @@ pub fn build_2d_grid<T: CoordsFloat>(
let mut map: CMap2<T> =
CMap2::new_with_undefined_attributes(4 * n_square_x * n_square_y, manager);

// first, topology
(0..n_square_y).for_each(|y_idx| {
(0..n_square_x).for_each(|x_idx| {
(0..n_square_y)
.flat_map(|y_idx| (0..n_square_x).map(move |x_idx| (y_idx, x_idx)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on why flatten the iterations.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 5d61aa9

.for_each(|(y_idx, x_idx)| {
// compute dart IDs of the cell
let d1 = (1 + 4 * x_idx + n_square_x * 4 * y_idx) as DartIdentifier;
let (d2, d3, d4) = (d1 + 1, d1 + 2, d1 + 3);
map.one_link(d1, d2);
map.one_link(d2, d3);
map.one_link(d3, d4);
map.one_link(d4, d1);
// are we on the last col / row / both?
let last_column = x_idx == n_square_x - 1;
let last_row = y_idx == n_square_y - 1;

// edit topology
// d1
map.set_beta::<0>(d1, d4);
map.set_beta::<1>(d1, d2);
// d2
map.set_beta::<0>(d2, d1);
map.set_beta::<1>(d2, d3);
// d3
map.set_beta::<0>(d3, d2);
map.set_beta::<1>(d3, d4);
// d4
map.set_beta::<0>(d4, d3);
map.set_beta::<1>(d4, d1);
// if there is a right neighbor, sew sew
if x_idx != n_square_x - 1 {
if !last_column {
let right_neighbor = d2 + 6;
map.two_link(d2, right_neighbor);
map.set_beta::<2>(d2, right_neighbor);
map.set_beta::<2>(right_neighbor, d2);
}
// if there is an up neighbor, sew sew
if y_idx != n_square_y - 1 {
if !last_row {
let up_neighbor = d1 + (4 * n_square_x) as DartIdentifier;
map.two_link(d3, up_neighbor);
map.set_beta::<2>(d3, up_neighbor);
map.set_beta::<2>(up_neighbor, d3);
}
});
});

// then cells
(0..=n_square_y).for_each(|y_idx| {
(0..=n_square_x).for_each(|x_idx| {
// update the associated 0-cell
if (y_idx < n_square_y) & (x_idx < n_square_x) {
let base_dart = (1 + 4 * x_idx + n_square_x * 4 * y_idx) as DartIdentifier;
let vertex_id = map.vertex_id(base_dart);

// edit geometry
let vertex_id = map.vertex_id(d1);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx).unwrap() * len_per_x,
T::from(y_idx).unwrap() * len_per_y,
),
);
if last_column {
// that last column of 0-cell needs special treatment
// bc there are no "horizontal" associated dart
let vertex_id = map.vertex_id(d2);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx).unwrap() * len_per_x,
T::from(x_idx + 1).unwrap() * len_per_x,
T::from(y_idx).unwrap() * len_per_y,
),
);
let last_column = x_idx == n_square_x - 1;
let last_row = y_idx == n_square_y - 1;
if last_column {
// that last column of 0-cell needs special treatment
// bc there are no "horizontal" associated dart
let vertex_id = map.vertex_id(base_dart + 1);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx + 1).unwrap() * len_per_x,
T::from(y_idx).unwrap() * len_per_y,
),
);
}
if last_row {
// same as the case on x
let vertex_id = map.vertex_id(base_dart + 3);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx).unwrap() * len_per_x,
T::from(y_idx + 1).unwrap() * len_per_y,
),
);
}
if last_row & last_column {
// need to do the upper right corner
let vertex_id = map.vertex_id(base_dart + 2);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx + 1).unwrap() * len_per_x,
T::from(y_idx + 1).unwrap() * len_per_y,
),
);
}
}
if last_row {
// same as the case on x
let vertex_id = map.vertex_id(d4);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx).unwrap() * len_per_x,
T::from(y_idx + 1).unwrap() * len_per_y,
),
);
}
if last_row & last_column {
// need to do the upper right corner
let vertex_id = map.vertex_id(d3);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx + 1).unwrap() * len_per_x,
T::from(y_idx + 1).unwrap() * len_per_y,
),
);
}
});
});

// and then build faces
assert_eq!(map.fetch_faces().identifiers.len(), n_square_x * n_square_y);
debug_assert_eq!(map.fetch_faces().identifiers.len(), n_square_x * n_square_y);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave a comment stating why it can impact the performances.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 5d61aa9


map
}
Expand All @@ -113,94 +118,103 @@ pub fn build_2d_splitgrid<T: CoordsFloat>(
let mut map: CMap2<T> =
CMap2::new_with_undefined_attributes(6 * n_square_x * n_square_y, manager);

// first, topology
(0..n_square_y).for_each(|y_idx| {
(0..n_square_x).for_each(|x_idx| {
(0..n_square_y)
.flat_map(|y_idx| (0..n_square_x).map(move |x_idx| (y_idx, x_idx)))
.for_each(|(y_idx, x_idx)| {
// compute dart IDs of the cell
let d1 = (1 + 6 * (x_idx + n_square_x * y_idx)) as DartIdentifier;
let (d2, d3, d4, d5, d6) = (d1 + 1, d1 + 2, d1 + 3, d1 + 4, d1 + 5);
// bottom left triangle
map.one_link(d1, d2);
map.one_link(d2, d3);
map.one_link(d3, d1);
// top right triangle
map.one_link(d4, d5);
map.one_link(d5, d6);
map.one_link(d6, d4);
// are we on the last col / row / both?
let last_column = x_idx == n_square_x - 1;
let last_row = y_idx == n_square_y - 1;

// edit topology
// d1
map.set_beta::<0>(d1, d3);
map.set_beta::<1>(d1, d2);
// d2
map.set_beta::<0>(d2, d1);
map.set_beta::<1>(d2, d3);
// d3
map.set_beta::<0>(d3, d2);
map.set_beta::<1>(d3, d1);
// d4
map.set_beta::<0>(d4, d6);
map.set_beta::<1>(d4, d5);
// d5
map.set_beta::<0>(d5, d4);
map.set_beta::<1>(d5, d6);
// d6
map.set_beta::<0>(d6, d5);
map.set_beta::<1>(d6, d4);
// diagonal
map.two_link(d2, d4);
map.set_beta::<2>(d2, d4);
map.set_beta::<2>(d4, d2);

// if there is a right neighbor, sew sew
if x_idx != n_square_x - 1 {
if !last_column {
let right_neighbor = d1 + 8;
map.two_link(d5, right_neighbor);
map.set_beta::<2>(d5, right_neighbor);
map.set_beta::<2>(right_neighbor, d5);
}
// if there is an up neighbor, sew sew
if y_idx != n_square_x - 1 {
if !last_row {
let up_neighbor = d1 + (6 * n_square_x) as DartIdentifier;
map.two_link(d6, up_neighbor);
map.set_beta::<2>(d6, up_neighbor);
map.set_beta::<2>(up_neighbor, d6);
}
});
});

// then cells
(0..=n_square_y).for_each(|y_idx| {
(0..=n_square_x).for_each(|x_idx| {
// update the associated 0-cell
if (y_idx < n_square_y) & (x_idx < n_square_x) {
let base_dart = (1 + 6 * (x_idx + n_square_x * y_idx)) as DartIdentifier;
let vertex_id = map.vertex_id(base_dart);

// edit geometry
let vertex_id = map.vertex_id(d1);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx).unwrap() * len_per_x,
T::from(y_idx).unwrap() * len_per_y,
),
);
if last_column {
// that last column of 0-cell needs special treatment
// bc there are no "horizontal" associated dart
let vertex_id = map.vertex_id(d5);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx).unwrap() * len_per_x,
T::from(x_idx + 1).unwrap() * len_per_x,
T::from(y_idx).unwrap() * len_per_y,
),
);
let last_column = x_idx == n_square_x - 1;
let last_row = y_idx == n_square_y - 1;
if last_column {
// that last column of 0-cell needs special treatment
// bc there are no "horizontal" associated dart
let vertex_id = map.vertex_id(base_dart + 4);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx + 1).unwrap() * len_per_x,
T::from(y_idx).unwrap() * len_per_y,
),
);
}
if last_row {
// same as the case on x
let vertex_id = map.vertex_id(base_dart + 2);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx).unwrap() * len_per_x,
T::from(y_idx + 1).unwrap() * len_per_y,
),
);
}
if last_row & last_column {
// need to do the upper right corner
let vertex_id = map.vertex_id(base_dart + 5);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx + 1).unwrap() * len_per_x,
T::from(y_idx + 1).unwrap() * len_per_y,
),
);
}
}
if last_row {
// same as the case on x
let vertex_id = map.vertex_id(d3);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx).unwrap() * len_per_x,
T::from(y_idx + 1).unwrap() * len_per_y,
),
);
}
if last_row & last_column {
// need to do the upper right corner
let vertex_id = map.vertex_id(d6);
map.insert_vertex(
vertex_id,
origin
+ Vector2(
T::from(x_idx + 1).unwrap() * len_per_x,
T::from(y_idx + 1).unwrap() * len_per_y,
),
);
}
});
});

// rebuild faces
assert_eq!(
debug_assert_eq!(
map.fetch_faces().identifiers.len(),
n_square_x * n_square_y * 2
);
Expand Down