From c78735c79bf278e0c2578428c76a225c72d6c40f Mon Sep 17 00:00:00 2001 From: Maximilian Reininghaus <maximilian.reininghaus@kit.edu> Date: Tue, 16 Oct 2018 11:55:42 +0200 Subject: [PATCH] factory method for creation of root CS --- Documentation/Examples/geometry_example.cc | 6 +++--- Documentation/Examples/helix_example.cc | 3 +-- Framework/Cascade/Cascade.h | 2 +- Framework/Geometry/CoordinateSystem.h | 18 ++++++++++++++---- Framework/Geometry/Sphere.h | 4 ++-- Framework/Geometry/testGeometry.cc | 12 ++++++------ 6 files changed, 27 insertions(+), 18 deletions(-) diff --git a/Documentation/Examples/geometry_example.cc b/Documentation/Examples/geometry_example.cc index dac31edc4..048347aee 100644 --- a/Documentation/Examples/geometry_example.cc +++ b/Documentation/Examples/geometry_example.cc @@ -24,7 +24,7 @@ using namespace corsika::units::si; int main() { // define the root coordinate system - CoordinateSystem root; + auto const root = CoordinateSystem::CreateRootCS(); // another CS defined by a translation relative to the root CS CoordinateSystem cs2 = root.translate({0_m, 0_m, 1_m}); @@ -50,10 +50,10 @@ int main() { std::cout << "p2-p1 norm^2: " << norm << std::endl; Sphere s(p1, 10_m); // define a sphere around a point with a radius - std::cout << "p1 inside s: " << s.isInside(p2) << std::endl; + std::cout << "p1 inside s: " << s.Contains(p2) << std::endl; Sphere s2(p1, 3_um); // another sphere - std::cout << "p1 inside s2: " << s2.isInside(p2) << std::endl; + std::cout << "p1 inside s2: " << s2.Contains(p2) << std::endl; // let's try parallel projections: auto const v1 = Vector<length_d>(root, {1_m, 1_m, 0_m}); diff --git a/Documentation/Examples/helix_example.cc b/Documentation/Examples/helix_example.cc index aea320270..2797a30a1 100644 --- a/Documentation/Examples/helix_example.cc +++ b/Documentation/Examples/helix_example.cc @@ -22,8 +22,7 @@ using namespace corsika::geometry; using namespace corsika::units::si; int main() { - - CoordinateSystem root; + auto const root = CoordinateSystem::CreateRootCS(); Point const r0(root, {0_m, 0_m, 0_m}); auto const omegaC = 2 * M_PI * 1_Hz; diff --git a/Framework/Cascade/Cascade.h b/Framework/Cascade/Cascade.h index b2aa17216..f5d88b33f 100644 --- a/Framework/Cascade/Cascade.h +++ b/Framework/Cascade/Cascade.h @@ -66,7 +66,7 @@ namespace corsika::cascade { void Step(Particle& particle) { [[maybe_unused]] double nextStep = fProcesseList.MinStepLength(particle); // corsika::utls::ignore(nextStep); - corsika::geometry::CoordinateSystem root; + auto const root = corsika::geometry::CoordinateSystem::CreateRootCS(); corsika::geometry::LineTrajectory trajectory( // trajectory is not yet used. this is a dummy. corsika::geometry::Point(root, {0_m, 0_m, 0_m}), diff --git a/Framework/Geometry/CoordinateSystem.h b/Framework/Geometry/CoordinateSystem.h index a6a091290..089a3f80c 100644 --- a/Framework/Geometry/CoordinateSystem.h +++ b/Framework/Geometry/CoordinateSystem.h @@ -30,15 +30,17 @@ namespace corsika::geometry { CoordinateSystem(CoordinateSystem const& reference, EigenTransform const& transf) : reference(&reference) , transf(transf) {} + + CoordinateSystem() + : // for creating the root CS + transf(EigenTransform::Identity()) {} public: + static auto CreateRootCS() { return CoordinateSystem(); } + static EigenTransform GetTransformation(CoordinateSystem const& c1, CoordinateSystem const& c2); - CoordinateSystem() - : // for creating the root CS - transf(EigenTransform::Identity()) {} - auto& operator=(const CoordinateSystem& pCS) { reference = pCS.reference; transf = pCS.transf; @@ -52,6 +54,10 @@ namespace corsika::geometry { } auto rotate(QuantityVector<phys::units::length_d> axis, double angle) const { + if (axis.eVector.isZero()) { + throw std::string("null-vector given as axis parameter"); + } + EigenTransform const rotation{Eigen::AngleAxisd(angle, axis.eVector.normalized())}; return CoordinateSystem(*this, rotation); @@ -59,6 +65,10 @@ namespace corsika::geometry { auto translateAndRotate(QuantityVector<phys::units::length_d> translation, QuantityVector<phys::units::length_d> axis, double angle) { + if (axis.eVector.isZero()) { + throw std::string("null-vector given as axis parameter"); + } + EigenTransform const transf{Eigen::AngleAxisd(angle, axis.eVector.normalized()) * EigenTranslation(translation.eVector)}; diff --git a/Framework/Geometry/Sphere.h b/Framework/Geometry/Sphere.h index 436c8e402..6ab7c8b96 100644 --- a/Framework/Geometry/Sphere.h +++ b/Framework/Geometry/Sphere.h @@ -26,8 +26,8 @@ namespace corsika::geometry { : center(pCenter) , radius(pRadius) {} - //! returns true if the Point p is within the sphere - auto isInside(Point const& p) const { + //! returns true if the Point \a p is within the sphere + auto Contains(Point const& p) const { return radius * radius > (center - p).squaredNorm(); } }; diff --git a/Framework/Geometry/testGeometry.cc b/Framework/Geometry/testGeometry.cc index 31be0d360..98527c0c1 100644 --- a/Framework/Geometry/testGeometry.cc +++ b/Framework/Geometry/testGeometry.cc @@ -28,7 +28,7 @@ using namespace corsika::units::si; double constexpr absMargin = 1.0e-8; TEST_CASE("transformations between CoordinateSystems") { - CoordinateSystem rootCS; + CoordinateSystem rootCS = CoordinateSystem::CreateRootCS(); REQUIRE(CoordinateSystem::GetTransformation(rootCS, rootCS) .isApprox(EigenTransform::Identity())); @@ -45,7 +45,7 @@ TEST_CASE("transformations between CoordinateSystems") { Approx(0).margin(absMargin)); SECTION("unconnected CoordinateSystems") { - CoordinateSystem rootCS2; + CoordinateSystem rootCS2 = CoordinateSystem::CreateRootCS(); REQUIRE_THROWS(CoordinateSystem::GetTransformation(rootCS, rootCS2)); } @@ -126,18 +126,18 @@ TEST_CASE("transformations between CoordinateSystems") { } TEST_CASE("Sphere") { - CoordinateSystem rootCS; + CoordinateSystem rootCS = CoordinateSystem::CreateRootCS(); Point center(rootCS, {0_m, 3_m, 4_m}); Sphere sphere(center, 5_m); SECTION("isInside") { - REQUIRE_FALSE(sphere.isInside(Point(rootCS, {100_m, 0_m, 0_m}))); - REQUIRE(sphere.isInside(Point(rootCS, {2_m, 3_m, 4_m}))); + REQUIRE_FALSE(sphere.Contains(Point(rootCS, {100_m, 0_m, 0_m}))); + REQUIRE(sphere.Contains(Point(rootCS, {2_m, 3_m, 4_m}))); } } TEST_CASE("Trajectories") { - CoordinateSystem rootCS; + CoordinateSystem rootCS = CoordinateSystem::CreateRootCS(); Point r0(rootCS, {0_m, 0_m, 0_m}); SECTION("Line") { -- GitLab