1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
// Purpose: Simple Unit tests for the Boolean volumes
//-- ensure asserts are compiled in
#undef NDEBUG
#include "VecGeom/base/FpeEnable.h"
#include "VecGeom/base/Vector3D.h"
#include "VecGeom/volumes/BooleanVolume.h"
#include "VecGeom/volumes/Tube.h"
#include "VecGeom/volumes/Box.h"
#include "ApproxEqual.h"
#include <cmath>
using namespace vecgeom;
using Vec3D_t = vecgeom::Vector3D<vecgeom::Precision>;
int TestBooleans()
{
double L = 10.; // global scale
// make a combination of union and subtraction
// BOX - ( TUBE + TUBE ) aka a box with 2 holes
UnplacedBox box(L, L, L);
auto bv = box.Capacity();
auto ba = box.SurfaceArea();
GenericUnplacedTube tube(0., 0.9 * L / 4., L, 0., vecgeom::kTwoPi);
LogicalVolume lbox("box", &box);
auto placedbox = lbox.Place();
// test capacity + surface area
auto tv = tube.Capacity();
auto etv = tube.EstimateCapacity(10000000);
assert(std::abs(tv - etv) / etv < 1E-3);
auto ta = tube.SurfaceArea();
auto eta = tube.EstimateSurfaceArea(10000000);
assert(std::abs(ta - eta) / ta < 1E-2);
LogicalVolume ltube("tube", &tube);
double HalfDiagL = std::sqrt(2.) * L; // Half diagonal length of box
double QuarterDiagL = HalfDiagL / 2.;
auto upperhole = ltube.Place(new Transformation3D(-L / 4, -L / 4, 0.));
auto lowerhole = ltube.Place(new Transformation3D(L / 4, L / 4, 0.));
UnplacedBooleanVolume<kUnion> holes(kUnion, upperhole, lowerhole); // 2 holes as a union
LogicalVolume lholes("CombinedTubes", &holes);
auto placedholes1 = lholes.Place();
auto placedholes2 = lholes.Place(new Transformation3D(-L / 2, -L / 2, 0.));
UnplacedBooleanVolume<kSubtraction> boxminusholes(kSubtraction, placedbox, placedholes2);
LogicalVolume lboxminusholes("CombinedBoolean", &boxminusholes);
auto placedcombinedboolean = lboxminusholes.Place();
// Check Extent and cached BBox
Vec3D_t minExtent, maxExtent, minBBox, maxBBox;
holes.Extent(minExtent, maxExtent);
holes.GetBBox(minBBox, maxBBox);
assert(ApproxEqual(minExtent, minBBox));
assert(ApproxEqual(maxExtent, maxBBox));
boxminusholes.Extent(minExtent, maxExtent);
boxminusholes.GetBBox(minBBox, maxBBox);
assert(ApproxEqual(minExtent, minBBox));
assert(ApproxEqual(maxExtent, maxBBox));
auto capacity = boxminusholes.Capacity();
assert(capacity > 0);
assert(std::fabs(capacity - (bv - 2. * tv)) / capacity < 1E-03);
auto sarea = boxminusholes.SurfaceArea();
assert(sarea > 0);
// ?? assert(sarea < (ba + 2. * ta));
assert(sarea > ba);
// Tests on the union first
// Contains
{
bool c = placedholes1->Contains(Vec3D_t(0., 0., 0.));
assert(!c);
}
{
bool c = placedholes1->Contains(Vec3D_t(-L / 4, -L / 4, 0.));
assert(c);
}
{
bool c = placedholes1->Contains(Vec3D_t(L / 4, L / 4, 0.));
assert(c);
}
{
double d = placedholes1->DistanceToIn(Vec3D_t(0., 0., 0.), Vec3D_t(0., 0., 1.));
assert(d == kInfLength);
}
{
double d = placedholes1->DistanceToIn(Vec3D_t(0., 0., 0.), Vec3D_t(1., 0., 0.));
assert(d == kInfLength);
}
{
double d = placedholes1->DistanceToIn(Vec3D_t(0., 0., 0.), Vec3D_t(0., 1., 0.));
assert(d == kInfLength);
}
{
double d = placedholes1->DistanceToIn(Vec3D_t(0., 0., 0.), Vec3D_t(1., 1., 0.).Normalized());
double d2 = placedholes1->DistanceToIn(Vec3D_t(0., 0., 0.), Vec3D_t(-1., -1., 0.).Normalized());
double dt1 = lowerhole->DistanceToIn(Vec3D_t(0., 0., 0.), Vec3D_t(1., 1., 0.).Normalized());
assert(ApproxEqual<Precision>(dt1, d));
assert(ApproxEqual<Precision>(d, d2));
assert(ApproxEqual<Precision>(d, QuarterDiagL / 2. - tube.rmax()));
}
{
double d = placedholes1->PlacedDistanceToOut(Vec3D_t(L / 4, L / 4, 0.), Vec3D_t(1., 1., 0.).Normalized());
assert(ApproxEqual<Precision>(d, tube.rmax()));
}
{
double d = placedholes1->PlacedDistanceToOut(Vec3D_t(-L / 4, -L / 4, 0.), Vec3D_t(1., 1., 0.).Normalized());
assert(ApproxEqual<Precision>(d, tube.rmax()));
}
// Test the combined boolean
{
bool c = placedcombinedboolean->Contains(Vec3D_t(0., 0., 0.));
assert(c);
}
{
bool c = placedcombinedboolean->Contains(Vec3D_t(-L / 2, -L / 2, 0.));
assert(c);
}
{
bool c = placedcombinedboolean->Contains(Vec3D_t(-3. * L / 4., -3. * L / 4., 0.));
assert(!c);
}
{
bool c = placedcombinedboolean->Contains(Vec3D_t(-L / 4., -L / 4., 0.));
assert(!c);
}
{
bool c = placedcombinedboolean->Contains(Vec3D_t(+L / 4., +L / 4., 0.));
assert(c);
}
{
bool c = placedcombinedboolean->Contains(Vec3D_t(+3 * L / 4., +3. * L / 4., 0.));
assert(c);
}
// Test the combined boolean DistanceToOut
{
double d = placedcombinedboolean->DistanceToOut(Vec3D_t(0., 0., 0.), Vec3D_t(1., 0., 0.));
assert(ApproxEqual<Precision>(d, 10.));
}
{
double d = placedcombinedboolean->DistanceToOut(Vec3D_t(0., 0., 0.), Vec3D_t(-1., -0., -0.));
assert(ApproxEqual<Precision>(d, 10.));
}
{
double d = placedcombinedboolean->DistanceToOut(Vec3D_t(0., 0., 0.), Vec3D_t(-0., -1., -0.));
assert(ApproxEqual<Precision>(d, 10.));
}
{
double d = placedcombinedboolean->DistanceToOut(Vec3D_t(0., -0., 0.), Vec3D_t(-0., 0., -1.));
assert(ApproxEqual<Precision>(d, 10.));
}
{
double d = placedcombinedboolean->DistanceToOut(Vec3D_t(-L / 4, 0., 0.), Vec3D_t(0., -1., 0));
assert(ApproxEqual<Precision>(d, (L / 2. - 2. * tube.rmax()) / 2.));
}
{
double d = placedcombinedboolean->DistanceToOut(Vec3D_t(-L / 4, 0., 0.), Vec3D_t(0., 1., 0));
assert(ApproxEqual<Precision>(d, 10.));
}
{
double d = placedcombinedboolean->DistanceToOut(Vec3D_t(-3. * L / 4, 0., 0.), Vec3D_t(0., -1., 0));
assert(ApproxEqual<Precision>(d, L / 2. + (L / 2. - 2. * tube.rmax()) / 2.));
}
{
double d = placedcombinedboolean->DistanceToOut(Vec3D_t(-L / 2, -L / 2., 0.), Vec3D_t(1., 1., 0).Normalized());
assert(ApproxEqual<Precision>(d, QuarterDiagL / 2. - tube.rmax()));
}
{
double d = placedcombinedboolean->DistanceToOut(Vec3D_t(L, L, 0.), Vec3D_t(-1., -1., 0).Normalized());
assert(d > std::sqrt(2) * L && d < std::sqrt(2) * L * 3 / 2.);
}
// Test the combined boolean DistanceToIn
{
// needs fix
// double d = placedcombinedboolean->DistanceToIn(Vec3D_t(0., 0., 0.), Vec3D_t(1., 0., 0.));
// assert(d <= 0.);
} {
double d = placedcombinedboolean->DistanceToIn(Vec3D_t(-20., 0., 0.), Vec3D_t(1., -0., -0.));
assert(ApproxEqual<Precision>(d, 10.));
}
{
double d = placedcombinedboolean->DistanceToIn(Vec3D_t(0., -20., 0.), Vec3D_t(0., 1., -0.));
assert(ApproxEqual<Precision>(d, 10.));
}
{
double d = placedcombinedboolean->DistanceToIn(Vec3D_t(20., 20., 20.), Vec3D_t(-1., -1., -1.).Normalized());
assert(ApproxEqual<Precision>(d, 10. * std::sqrt(3.)));
}
{
double d = placedcombinedboolean->DistanceToIn(Vec3D_t(-L / 4., -L / 4., 0.), Vec3D_t(-1., -0., -0.));
assert(ApproxEqual<Precision>(d, tube.rmax()));
}
{
double d = placedcombinedboolean->DistanceToIn(Vec3D_t(-L / 4. - tube.rmax(), -L / 4., 0.), Vec3D_t(1., -0., -0.));
assert(ApproxEqual<Precision>(d, 2. * tube.rmax()));
}
{
double d = placedcombinedboolean->DistanceToIn(Vec3D_t(-L / 4., -L / 4., 0.), Vec3D_t(0, -1., -0.));
assert(ApproxEqual<Precision>(d, tube.rmax()));
}
{
double d = placedcombinedboolean->DistanceToIn(Vec3D_t(-L / 4., -L / 4., 0.), Vec3D_t(0, -0., -1.));
assert(d == kInfLength);
}
{
double d = placedcombinedboolean->DistanceToIn(Vec3D_t(-L / 4., -L / 4., 0.), Vec3D_t(-0, -0, -1.));
assert(d == kInfLength);
}
{
// needs reworking verification
// double d = placedcombinedboolean->DistanceToIn(Vec3D_t(-L / 4 - tube.rmax(), -L / 4., 0.), Vec3D_t(-0, -0,
// -1.));
// assert(d == kInfLength);
} {
// needs reworking + verification
// double d = placedcombinedboolean->DistanceToIn(Vec3D_t(-L / 4 - tube.rmax() - 0.01, -L / 4., 0.), Vec3D_t(-0, -0,
// -1.));
// assert(d <= 0.);
}
// Make combined union of the tubes with a slab
double Lslab = L / 4. * std::sqrt(2.);
UnplacedBox slab(Lslab, L / 10., L / 10.);
LogicalVolume lslab("slab", &slab);
// two disconnetected tubes are now connected via the rotated slab
UnplacedBooleanVolume<kUnion> tubesandslabunion(kUnion, placedholes1,
lslab.Place(new Transformation3D(0., 0., 0., 0., 0., 45)));
auto placedtubesslabunion = (new LogicalVolume("tubesandslabunion", &tubesandslabunion))->Place();
{
double d = placedtubesslabunion->PlacedDistanceToOut(Vec3D_t(0., 0., 0.), Vec3D_t(1., 1., 0.).Normalized());
assert(ApproxEqual<Precision>(d, L / 4 * std::sqrt(2) + tube.rmax()));
}
{
double d = placedtubesslabunion->PlacedDistanceToOut(Vec3D_t(-L / 4, -L / 4, 0.), Vec3D_t(1., 1., 0.).Normalized());
assert(ApproxEqual<Precision>(d, L / 2 * std::sqrt(2) + tube.rmax()));
}
{
double d = placedtubesslabunion->PlacedDistanceToOut(Vec3D_t(L / 4, L / 4, 0.), Vec3D_t(1., 1., 0.).Normalized());
assert(ApproxEqual<Precision>(d, tube.rmax()));
}
{
double d =
placedtubesslabunion->PlacedDistanceToOut(Vec3D_t(-L / 4, -L / 4, 0.), Vec3D_t(-1., -1., 0.).Normalized());
assert(ApproxEqual<Precision>(d, tube.rmax()));
}
{
double d = placedtubesslabunion->PlacedDistanceToOut(Vec3D_t(L / 4, L / 4, 0.), Vec3D_t(-1., -1., 0.).Normalized());
assert(ApproxEqual<Precision>(d, L / 2 * std::sqrt(2) + tube.rmax()));
}
return 0;
}
int main(int argc, char *argv[])
{
return TestBooleans();
}
|