Skip to content

Commit

Permalink
Implement and test n4::boolean_shape::trans{,form}
Browse files Browse the repository at this point in the history
  • Loading branch information
gonzaponte committed Nov 2, 2023
1 parent 39697f3 commit e67c7ec
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
7 changes: 6 additions & 1 deletion nain4/src/n4-boolean-shape.hh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "G4Transform3D.hh"
#include <n4-shape.hh>

#include <type_traits>
Expand All @@ -16,10 +17,14 @@ struct boolean_shape : shape {
friend shape;
G4VSolid* solid() const override;


boolean_shape& trans (G4Transform3D& t ) { return transform(t); }
boolean_shape& transform(G4Transform3D& t ) { transformation = t * transformation; return *this; }
boolean_shape& rotate (G4RotationMatrix& r ) { transformation = HepGeom::Rotate3D{r} * transformation; return *this; }
boolean_shape& rotate_x(double delta ) { auto rot = G4RotationMatrix{}; rot.rotateX(delta); return rotate(rot);}
boolean_shape& rotate_y(double delta ) { auto rot = G4RotationMatrix{}; rot.rotateY(delta); return rotate(rot);}
boolean_shape& rotate_z(double delta ) { auto rot = G4RotationMatrix{}; rot.rotateZ(delta); return rotate(rot);}
boolean_shape& rotate (G4RotationMatrix& r ) { transformation = HepGeom::Rotate3D{r} * transformation; return *this; }

boolean_shape& rot (G4RotationMatrix& r ) { return rotate(r); }
boolean_shape& rot_x (double delta ) { return rotate_x(delta); }
boolean_shape& rot_y (double delta ) { return rotate_y(delta); }
Expand Down
30 changes: 30 additions & 0 deletions nain4/test/test-nain4.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,36 @@ TEST_CASE("nain boolean rotation", "[nain][geometry][boolean][rotation]") {
CHECK(with_rot -> EstimateCubicVolume(n, eps) == 0 );
}

TEST_CASE("boolean transform", "[boolean][transform]") {
auto l = 3*m;
auto rotation = G4RotationMatrix{}; rotation.rotateY(90 * deg);
auto translation = G4ThreeVector{0., 0., l/4};
auto transform = G4Transform3D{rotation, translation};

auto usolid1 = n4::box("box1").xy(1*m).z( l)
.sub( n4::box("box2").yz(1*m).x(l/4))
.transform(transform)
.solid();

auto usolid2 = n4::box("box1").xy(1*m).z( l)
.sub( n4::box("box2").yz(1*m).x(l/4))
.trans(transform)
.solid();

auto n = 100000;
auto eps = 1e-3;
auto vbox = l * 1*m * 1*m;

// The small box is 1/4 of the big box, so the result two
// disconnected boxes: one with 1/2 volume and another one with 1/4
// volume
CHECK( usolid1 -> EstimateCubicVolume(n, eps) / m3 == Approx(3*vbox/4 / m3).margin(3e-3));
CHECK( usolid1 -> EstimateCubicVolume(n, eps) / m3 == Approx(3*vbox/4 / m3).margin(3e-3));
CHECK( usolid2 -> EstimateCubicVolume(n, eps) / m3 == Approx(3*vbox/4 / m3).margin(3e-3));
CHECK( usolid2 -> EstimateCubicVolume(n, eps) / m3 == Approx(3*vbox/4 / m3).margin(3e-3));
}


TEST_CASE("nain envelope_of", "[nain][envelope_of]") {
auto material_1 = n4::material("G4_Fe");
auto material_2 = n4::material("G4_Au");
Expand Down

0 comments on commit e67c7ec

Please sign in to comment.