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

Remove use of sscanf for reading surface coefficients #2574

Merged
merged 2 commits into from
Jun 24, 2023
Merged
Changes from all 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
131 changes: 27 additions & 104 deletions src/surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <cmath>
#include <complex>
#include <initializer_list>
#include <set>
#include <utility>

Expand Down Expand Up @@ -34,100 +35,21 @@ vector<unique_ptr<Surface>> surfaces;
// Helper functions for reading the "coeffs" node of an XML surface element
//==============================================================================

void read_coeffs(pugi::xml_node surf_node, int surf_id, double& c1)
{
// Check the given number of coefficients.
std::string coeffs = get_node_value(surf_node, "coeffs");
int n_words = word_count(coeffs);
if (n_words != 1) {
fatal_error(fmt::format(
"Surface {} expects 1 coeff but was given {}", surf_id, n_words));
}

// Parse the coefficients.
int stat = sscanf(coeffs.c_str(), "%lf", &c1);
if (stat != 1) {
fatal_error(fmt::format(
"Something went wrong reading coeffs for surface {}", surf_id));
}
}

void read_coeffs(
pugi::xml_node surf_node, int surf_id, double& c1, double& c2, double& c3)
{
// Check the given number of coefficients.
std::string coeffs = get_node_value(surf_node, "coeffs");
int n_words = word_count(coeffs);
if (n_words != 3) {
fatal_error(fmt::format(
"Surface {} expects 3 coeffs but was given {}", surf_id, n_words));
}

// Parse the coefficients.
int stat = sscanf(coeffs.c_str(), "%lf %lf %lf", &c1, &c2, &c3);
if (stat != 3) {
fatal_error(fmt::format(
"Something went wrong reading coeffs for surface {}", surf_id));
}
}

void read_coeffs(pugi::xml_node surf_node, int surf_id, double& c1, double& c2,
double& c3, double& c4)
{
// Check the given number of coefficients.
std::string coeffs = get_node_value(surf_node, "coeffs");
int n_words = word_count(coeffs);
if (n_words != 4) {
fatal_error(fmt::format(
"Surface {} expects 4 coeffs but was given ", surf_id, n_words));
}

// Parse the coefficients.
int stat = sscanf(coeffs.c_str(), "%lf %lf %lf %lf", &c1, &c2, &c3, &c4);
if (stat != 4) {
fatal_error(fmt::format(
"Something went wrong reading coeffs for surface {}", surf_id));
}
}

void read_coeffs(pugi::xml_node surf_node, int surf_id, double& c1, double& c2,
double& c3, double& c4, double& c5, double& c6)
{
// Check the given number of coefficients.
std::string coeffs = get_node_value(surf_node, "coeffs");
int n_words = word_count(coeffs);
if (n_words != 6) {
fatal_error(fmt::format(
"Surface {} expects 6 coeffs but was given {}", surf_id, n_words));
}

// Parse the coefficients.
int stat = sscanf(
coeffs.c_str(), "%lf %lf %lf %lf %lf %lf", &c1, &c2, &c3, &c4, &c5, &c6);
if (stat != 6) {
fatal_error(fmt::format(
"Something went wrong reading coeffs for surface {}", surf_id));
}
}

void read_coeffs(pugi::xml_node surf_node, int surf_id, double& c1, double& c2,
double& c3, double& c4, double& c5, double& c6, double& c7, double& c8,
double& c9, double& c10)
pugi::xml_node surf_node, int surf_id, std::initializer_list<double*> coeffs)
{
// Check the given number of coefficients.
std::string coeffs = get_node_value(surf_node, "coeffs");
int n_words = word_count(coeffs);
if (n_words != 10) {
fatal_error(fmt::format(
"Surface {} expects 10 coeffs but was given {}", surf_id, n_words));
auto coeffs_file = get_node_array<double>(surf_node, "coeffs");
if (coeffs_file.size() != coeffs.size()) {
fatal_error(
fmt::format("Surface {} expects {} coefficient but was given {}", surf_id,
coeffs.size(), coeffs_file.size()));
}

// Parse the coefficients.
int stat = sscanf(coeffs.c_str(), "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
&c1, &c2, &c3, &c4, &c5, &c6, &c7, &c8, &c9, &c10);
if (stat != 10) {
fatal_error(fmt::format(
"Something went wrong reading coeffs for surface {}", surf_id));
// Copy the coefficients
int i = 0;
for (auto c : coeffs) {
*c = coeffs_file[i++];
}
}

Expand Down Expand Up @@ -279,7 +201,7 @@ double axis_aligned_plane_distance(

SurfaceXPlane::SurfaceXPlane(pugi::xml_node surf_node) : CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, x0_);
read_coeffs(surf_node, id_, {&x0_});
}

double SurfaceXPlane::evaluate(Position r) const
Expand Down Expand Up @@ -319,7 +241,7 @@ BoundingBox SurfaceXPlane::bounding_box(bool pos_side) const

SurfaceYPlane::SurfaceYPlane(pugi::xml_node surf_node) : CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, y0_);
read_coeffs(surf_node, id_, {&y0_});
}

double SurfaceYPlane::evaluate(Position r) const
Expand Down Expand Up @@ -359,7 +281,7 @@ BoundingBox SurfaceYPlane::bounding_box(bool pos_side) const

SurfaceZPlane::SurfaceZPlane(pugi::xml_node surf_node) : CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, z0_);
read_coeffs(surf_node, id_, {&z0_});
}

double SurfaceZPlane::evaluate(Position r) const
Expand Down Expand Up @@ -399,7 +321,7 @@ BoundingBox SurfaceZPlane::bounding_box(bool pos_side) const

SurfacePlane::SurfacePlane(pugi::xml_node surf_node) : CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, A_, B_, C_, D_);
read_coeffs(surf_node, id_, {&A_, &B_, &C_, &D_});
}

double SurfacePlane::evaluate(Position r) const
Expand Down Expand Up @@ -518,7 +440,7 @@ Direction axis_aligned_cylinder_normal(
SurfaceXCylinder::SurfaceXCylinder(pugi::xml_node surf_node)
: CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, y0_, z0_, radius_);
read_coeffs(surf_node, id_, {&y0_, &z0_, &radius_});
}

double SurfaceXCylinder::evaluate(Position r) const
Expand Down Expand Up @@ -561,7 +483,7 @@ BoundingBox SurfaceXCylinder::bounding_box(bool pos_side) const
SurfaceYCylinder::SurfaceYCylinder(pugi::xml_node surf_node)
: CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, x0_, z0_, radius_);
read_coeffs(surf_node, id_, {&x0_, &z0_, &radius_});
}

double SurfaceYCylinder::evaluate(Position r) const
Expand Down Expand Up @@ -605,7 +527,7 @@ BoundingBox SurfaceYCylinder::bounding_box(bool pos_side) const
SurfaceZCylinder::SurfaceZCylinder(pugi::xml_node surf_node)
: CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, x0_, y0_, radius_);
read_coeffs(surf_node, id_, {&x0_, &y0_, &radius_});
}

double SurfaceZCylinder::evaluate(Position r) const
Expand Down Expand Up @@ -648,7 +570,7 @@ BoundingBox SurfaceZCylinder::bounding_box(bool pos_side) const

SurfaceSphere::SurfaceSphere(pugi::xml_node surf_node) : CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, x0_, y0_, z0_, radius_);
read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_});
}

double SurfaceSphere::evaluate(Position r) const
Expand Down Expand Up @@ -814,7 +736,7 @@ Direction axis_aligned_cone_normal(

SurfaceXCone::SurfaceXCone(pugi::xml_node surf_node) : CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, x0_, y0_, z0_, radius_sq_);
read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
}

double SurfaceXCone::evaluate(Position r) const
Expand Down Expand Up @@ -846,7 +768,7 @@ void SurfaceXCone::to_hdf5_inner(hid_t group_id) const

SurfaceYCone::SurfaceYCone(pugi::xml_node surf_node) : CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, x0_, y0_, z0_, radius_sq_);
read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
}

double SurfaceYCone::evaluate(Position r) const
Expand Down Expand Up @@ -878,7 +800,7 @@ void SurfaceYCone::to_hdf5_inner(hid_t group_id) const

SurfaceZCone::SurfaceZCone(pugi::xml_node surf_node) : CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, x0_, y0_, z0_, radius_sq_);
read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &radius_sq_});
}

double SurfaceZCone::evaluate(Position r) const
Expand Down Expand Up @@ -910,7 +832,8 @@ void SurfaceZCone::to_hdf5_inner(hid_t group_id) const

SurfaceQuadric::SurfaceQuadric(pugi::xml_node surf_node) : CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, A_, B_, C_, D_, E_, F_, G_, H_, J_, K_);
read_coeffs(
surf_node, id_, {&A_, &B_, &C_, &D_, &E_, &F_, &G_, &H_, &J_, &K_});
}

double SurfaceQuadric::evaluate(Position r) const
Expand Down Expand Up @@ -1062,7 +985,7 @@ double torus_distance(double x1, double x2, double x3, double u1, double u2,

SurfaceXTorus::SurfaceXTorus(pugi::xml_node surf_node) : CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, x0_, y0_, z0_, A_, B_, C_);
read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
}

void SurfaceXTorus::to_hdf5_inner(hid_t group_id) const
Expand Down Expand Up @@ -1115,7 +1038,7 @@ Direction SurfaceXTorus::normal(Position r) const

SurfaceYTorus::SurfaceYTorus(pugi::xml_node surf_node) : CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, x0_, y0_, z0_, A_, B_, C_);
read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
}

void SurfaceYTorus::to_hdf5_inner(hid_t group_id) const
Expand Down Expand Up @@ -1168,7 +1091,7 @@ Direction SurfaceYTorus::normal(Position r) const

SurfaceZTorus::SurfaceZTorus(pugi::xml_node surf_node) : CSGSurface(surf_node)
{
read_coeffs(surf_node, id_, x0_, y0_, z0_, A_, B_, C_);
read_coeffs(surf_node, id_, {&x0_, &y0_, &z0_, &A_, &B_, &C_});
}

void SurfaceZTorus::to_hdf5_inner(hid_t group_id) const
Expand Down