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

Too much control logic in main while loop #197

Open
adisidev opened this issue Sep 20, 2024 · 2 comments
Open

Too much control logic in main while loop #197

adisidev opened this issue Sep 20, 2024 · 2 comments
Labels
enhancement New feature or request important This is higher priority than average priority: high

Comments

@adisidev
Copy link
Collaborator

cartogram-cpp/src/main.cpp

Lines 269 to 396 in 99aed8f

while (
inset_state.n_finished_integrations() < max_integrations &&
(inset_state.max_area_error(false).value > max_permitted_area_error ||
std::abs(inset_state.area_expansion_factor() - 1.0) >
max_permitted_area_expansion)) {
std::cerr << "\nIntegration number "
<< inset_state.n_finished_integrations() << std::endl;
std::cerr << "Number of Points: " << inset_state.n_points() << std::endl;
// File prefix for output files for this integration
const std::string file_prefix =
inset_state.inset_name() + "_" +
std::to_string(inset_state.n_finished_integrations());
if (qtdt_method) {
// Create the Delaunay triangulation
time_tracker.start("Delaunay Triangulation");
inset_state.create_and_store_quadtree_cell_corners();
time_tracker.stop("Delaunay Triangulation");
time_tracker.start("Delaunay Triangulation");
inset_state.create_delaunay_t();
time_tracker.stop("Delaunay Triangulation");
if (plot_quadtree) {
// Draw the resultant quadtree and Delaunay triangulation
inset_state.write_quadtree(file_prefix + "_quadtree");
inset_state.write_delaunay_triangles(
file_prefix + "_delaunay_t",
false);
}
}
if (rays) {
// Fill density using ray-shooting method
time_tracker.start("Fill with Density (Ray Shooting Method)");
inset_state.fill_with_density_rays(plot_density);
time_tracker.stop("Fill with Density (Ray Shooting Method)");
} else {
time_tracker.start("Fill with Density (Clipping Method)");
inset_state.fill_with_density_clip(plot_density);
time_tracker.stop("Fill with Density (Clipping Method)");
}
const double blur_width = inset_state.blur_width();
if (blur_width > 0.0) {
time_tracker.start("Blur");
inset_state.blur_density(blur_width, plot_density);
time_tracker.stop("Blur");
}
if (qtdt_method) {
time_tracker.start("Flatten Density (Quadtree Method)");
bool passed = inset_state.flatten_density_with_node_vertices();
time_tracker.stop("Flatten Density (Quadtree Method)");
if (plot_quadtree) {
inset_state.write_delaunay_triangles(
file_prefix + "_delaunay_t_after_flatten",
true);
}
if (not passed) {
// Flatten density has failed and blur width will increase, and now
// try again
inset_state.increment_n_fails_during_flatten_density();
std::cerr << "Flatten density failed. Increasing blur width and "
"trying again."
<< std::endl;
std::cerr << "n_fails: "
<< inset_state.n_fails_during_flatten_density()
<< std::endl;
continue;
}
} else {
// Using entire grid
time_tracker.start("Flatten Density (Full Grid Method)");
inset_state.flatten_density();
time_tracker.stop("Flatten Density (Full Grid Method)");
}
if (qtdt_method) {
time_tracker.start("Update Delanuay Triangulation");
inset_state.update_delaunay_t();
time_tracker.stop("Update Delanuay Triangulation");
if (plot_quadtree) {
inset_state.write_delaunay_triangles(
file_prefix + "_updated_delaunay_t_projected",
true);
}
if (simplify) {
time_tracker.start("Densification (using Delanuay Triangles)");
inset_state.densify_geo_divs_using_delaunay_t();
time_tracker.stop("Densification (using Delanuay Triangles)");
}
// Project using the Delaunay triangulation
time_tracker.start("Project (Delanuay Triangulation)");
inset_state.project_with_delaunay_t(output_to_stdout);
time_tracker.stop("Project (Delanuay Triangulation)");
} else if (triangulation) {
// Choose diagonals that are inside grid cells, then densify.
time_tracker.start("Densification (using Grid Diagonals)");
inset_state.fill_grid_diagonals();
inset_state.densify_geo_divs();
time_tracker.stop("Densification (using Grid Diagonals)");
// Project with triangulation
time_tracker.start("Project (Triangulation)");
inset_state.project_with_triangulation();
time_tracker.stop("Project (Triangulation)");
} else {
// Project using bilinear interpolation
time_tracker.start("Project (Bilinear Interpolation)");
inset_state.project();
time_tracker.stop("Project (Bilinear Interpolation)");
}
if (simplify) {
time_tracker.start("Simplification");
inset_state.simplify(target_points_per_inset);
time_tracker.stop("Simplification");

@adisidev
Copy link
Collaborator Author

We check for too much control logic in the main while loop. This makes main.cpp quite unreadable. Since the basic steps are the same, I believe we should shift all these changes to a larger function that redirects control appropriately.

For instance, before project, instead of checking at every step whether we use qtdt or triangulation or billinear_interpolation, we only call inset_state.project() and we put the control logic within inset_state.project().

@adisidev adisidev added enhancement New feature or request important This is higher priority than average priority: high labels Sep 20, 2024
@adisidev
Copy link
Collaborator Author

This will also fix #189.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request important This is higher priority than average priority: high
Projects
None yet
Development

No branches or pull requests

1 participant