From 348ff31243b0dd43cae71795b7850072bfce0543 Mon Sep 17 00:00:00 2001 From: hayakawa Date: Tue, 29 Aug 2023 12:01:52 -0700 Subject: [PATCH] Final cleanup of PR. Ready for review. --- .../Tissues/BoundingCylinderTissueTests.cs | 48 ++-- .../Tissues/BoundingVoxelTissueTests.cs | 48 ++-- .../CaplessCylinderTissueRegionTests.cs | 35 +-- .../Tissues/CaplessVoxelTissueRegionTests.cs | 15 +- .../Tissues/CylinderTissueRegionTests.cs | 33 +-- .../Tissues/EllipsoidTissueRegionTests.cs | 13 +- .../Tissues/VoxelTissueRegionTests.cs | 2 +- .../VirtualBoundariesExtensionMethods.cs | 224 ++++++++---------- src/Vts/MonteCarlo/Photon.cs | 48 ++-- .../Tissues/CaplessVoxelTissueRegion.cs | 2 +- .../Tissues/EllipsoidTissueRegion.cs | 4 +- 11 files changed, 226 insertions(+), 246 deletions(-) diff --git a/src/Vts.Test/MonteCarlo/Tissues/BoundingCylinderTissueTests.cs b/src/Vts.Test/MonteCarlo/Tissues/BoundingCylinderTissueTests.cs index 314b76bc6..55c4ca322 100644 --- a/src/Vts.Test/MonteCarlo/Tissues/BoundingCylinderTissueTests.cs +++ b/src/Vts.Test/MonteCarlo/Tissues/BoundingCylinderTissueTests.cs @@ -12,14 +12,14 @@ namespace Vts.Test.MonteCarlo.Tissues [TestFixture] public class BoundingCylinderTissueTests { - private BoundedTissue _oneLayerTissue, _twoLayerTissue; + private BoundedTissue _oneLayerTissueBoundedByCylinder, _twoLayerTissueBoundedByCylinder; /// /// Validate general constructor of Tissue for a one layer and two layer tissue cylinder /// [OneTimeSetUp] public void Create_instance_of_class() { - _oneLayerTissue = new BoundedTissue(new CaplessCylinderTissueRegion( + _oneLayerTissueBoundedByCylinder = new BoundedTissue(new CaplessCylinderTissueRegion( new Position(0, 0, 50), 1.0, 100.0, new OpticalProperties()), new ITissueRegion[] { @@ -33,7 +33,7 @@ public void Create_instance_of_class() new DoubleRange(100.0, double.PositiveInfinity), new OpticalProperties(0.0, 1e-10, 1.0, 1.0)) }); - _twoLayerTissue = new BoundedTissue(new CaplessCylinderTissueRegion( + _twoLayerTissueBoundedByCylinder = new BoundedTissue(new CaplessCylinderTissueRegion( new Position(0, 0, 50), 1.0, 100.0, new OpticalProperties()), new ITissueRegion[] { @@ -58,18 +58,18 @@ public void Create_instance_of_class() [Test] public void Verify_GetRegionIndex_method_returns_correct_result() { - var index = _oneLayerTissue.GetRegionIndex(new Position(10, 0, 0)); // outside cylinder + var index = _oneLayerTissueBoundedByCylinder.GetRegionIndex(new Position(10, 0, 0)); // outside cylinder Assert.AreEqual(3, index); - index = _oneLayerTissue.GetRegionIndex(new Position(0, 0, 2.5)); // inside cylinder + index = _oneLayerTissueBoundedByCylinder.GetRegionIndex(new Position(0, 0, 2.5)); // inside cylinder Assert.AreEqual(1, index); - index = _oneLayerTissue.GetRegionIndex(new Position(0, 0, 0)); // on cylinder is considered in + index = _oneLayerTissueBoundedByCylinder.GetRegionIndex(new Position(0, 0, 0)); // on cylinder is considered in Assert.AreEqual(1, index); // two layer results - index = _twoLayerTissue.GetRegionIndex(new Position(10, 0, 0)); // outside cylinder + index = _twoLayerTissueBoundedByCylinder.GetRegionIndex(new Position(10, 0, 0)); // outside cylinder Assert.AreEqual(4, index); - index = _twoLayerTissue.GetRegionIndex(new Position(0, 0, 2.5)); // inside cylinder + index = _twoLayerTissueBoundedByCylinder.GetRegionIndex(new Position(0, 0, 2.5)); // inside cylinder Assert.AreEqual(2, index); - index = _twoLayerTissue.GetRegionIndex(new Position(0, 0, 0)); // on cylinder is considered in + index = _twoLayerTissueBoundedByCylinder.GetRegionIndex(new Position(0, 0, 0)); // on cylinder is considered in Assert.AreEqual(1, index); } @@ -83,56 +83,56 @@ public void Verify_GetNeighborRegionIndex_method_returns_correct_result() new Position(-1, 0, 1), new Direction(1.0, 0, 0), 1.0, - _oneLayerTissue, + _oneLayerTissueBoundedByCylinder, 3, new Random()); - var index = _oneLayerTissue.GetNeighborRegionIndex(photon); + var index = _oneLayerTissueBoundedByCylinder.GetNeighborRegionIndex(photon); Assert.AreEqual(1, index); photon = new Photon( // on side of cylinder pointed out of it new Position(-1, 0, 1), new Direction(-1.0, 0, 0), 1.0, - _oneLayerTissue, + _oneLayerTissueBoundedByCylinder, 1, new Random()); - index = _oneLayerTissue.GetNeighborRegionIndex(photon); + index = _oneLayerTissueBoundedByCylinder.GetNeighborRegionIndex(photon); Assert.AreEqual(3, index); // two layer results photon = new Photon( // on side of cylinder pointed into LAYER 1 new Position(-1, 0, 0.5), new Direction(1.0, 0, 0), 1.0, - _twoLayerTissue, + _twoLayerTissueBoundedByCylinder, 4, new Random()); - index = _twoLayerTissue.GetNeighborRegionIndex(photon); + index = _twoLayerTissueBoundedByCylinder.GetNeighborRegionIndex(photon); Assert.AreEqual(1, index); photon = new Photon( // on side of cylinder in LAYER 1 pointed out of it new Position(-1, 0, 0.5), new Direction(-1.0, 0, 0), 1.0, - _twoLayerTissue, + _twoLayerTissueBoundedByCylinder, 1, new Random()); - index = _twoLayerTissue.GetNeighborRegionIndex(photon); + index = _twoLayerTissueBoundedByCylinder.GetNeighborRegionIndex(photon); Assert.AreEqual(4, index); photon = new Photon( // on side of cylinder pointed into LAYER 2 new Position(-1, 0, 1.5), new Direction(1.0, 0, 0), 1.0, - _twoLayerTissue, + _twoLayerTissueBoundedByCylinder, 4, new Random()); - index = _twoLayerTissue.GetNeighborRegionIndex(photon); + index = _twoLayerTissueBoundedByCylinder.GetNeighborRegionIndex(photon); Assert.AreEqual(2, index); photon = new Photon( // on side of cylinder in LAYER 2 pointed out of it new Position(-1, 0, 1.5), new Direction(-1.0, 0, 0), 1.0, - _twoLayerTissue, + _twoLayerTissueBoundedByCylinder, 1, new Random()); - index = _twoLayerTissue.GetNeighborRegionIndex(photon); + index = _twoLayerTissueBoundedByCylinder.GetNeighborRegionIndex(photon); Assert.AreEqual(4, index); } @@ -146,15 +146,15 @@ public void Verify_GetAngleRelativeToBoundaryNormal_method_returns_correct_resul new Position(0.0, 0.0, 0.0), new Direction(0.0, 0.0, 1.0), 1, - _twoLayerTissue, + _twoLayerTissueBoundedByCylinder, 1, new Random()); - var cosTheta = _twoLayerTissue.GetAngleRelativeToBoundaryNormal(photon); + var cosTheta = _twoLayerTissueBoundedByCylinder.GetAngleRelativeToBoundaryNormal(photon); Assert.AreEqual(1, cosTheta); // put on side of cylinder pointing in photon.DP.Position = new Position(1.0, 1.0, 5.0); photon.DP.Direction = new Direction(1.0, 0.0, 0.0); - cosTheta = _twoLayerTissue.GetAngleRelativeToBoundaryNormal(photon); + cosTheta = _twoLayerTissueBoundedByCylinder.GetAngleRelativeToBoundaryNormal(photon); Assert.IsTrue(Math.Abs(cosTheta - 1/Math.Sqrt(2)) < 1e-6); } diff --git a/src/Vts.Test/MonteCarlo/Tissues/BoundingVoxelTissueTests.cs b/src/Vts.Test/MonteCarlo/Tissues/BoundingVoxelTissueTests.cs index 7e695172c..b4af326a7 100644 --- a/src/Vts.Test/MonteCarlo/Tissues/BoundingVoxelTissueTests.cs +++ b/src/Vts.Test/MonteCarlo/Tissues/BoundingVoxelTissueTests.cs @@ -12,14 +12,14 @@ namespace Vts.Test.MonteCarlo.Tissues [TestFixture] public class BoundingVoxelTissueTests { - private BoundedTissue _oneLayerTissue, _twoLayerTissue; + private BoundedTissue _oneLayerTissueBoundedByVoxel, _twoLayerTissueBoundedByVoxel; /// /// Validate general constructor of Tissue for a one layer and two layer tissue voxel /// [OneTimeSetUp] public void Create_instance_of_class() { - _oneLayerTissue = new BoundedTissue(new CaplessVoxelTissueRegion( + _oneLayerTissueBoundedByVoxel = new BoundedTissue(new CaplessVoxelTissueRegion( new DoubleRange(-1, 1, 2), // x range new DoubleRange(-1, 1, 2), // y range new DoubleRange(0, 100.0, 2), // z range spans tissue @@ -36,7 +36,7 @@ public void Create_instance_of_class() new DoubleRange(100.0, double.PositiveInfinity), new OpticalProperties(0.0, 1e-10, 1.0, 1.0)) }); - _twoLayerTissue = new BoundedTissue(new CaplessVoxelTissueRegion( + _twoLayerTissueBoundedByVoxel = new BoundedTissue(new CaplessVoxelTissueRegion( new DoubleRange(-1, 1, 2), // x range new DoubleRange(-1, 1, 2), // y range new DoubleRange(0, 100.0, 2), // z range spans tissue @@ -64,18 +64,18 @@ public void Create_instance_of_class() [Test] public void Verify_GetRegionIndex_method_returns_correct_result() { - var index = _oneLayerTissue.GetRegionIndex(new Position(10, 0, 0)); // outside voxel + var index = _oneLayerTissueBoundedByVoxel.GetRegionIndex(new Position(10, 0, 0)); // outside voxel Assert.AreEqual(3, index); - index = _oneLayerTissue.GetRegionIndex(new Position(0, 0, 2.5)); // inside voxel + index = _oneLayerTissueBoundedByVoxel.GetRegionIndex(new Position(0, 0, 2.5)); // inside voxel Assert.AreEqual(1, index); - index = _oneLayerTissue.GetRegionIndex(new Position(0, 0, 0)); // on voxel is considered in + index = _oneLayerTissueBoundedByVoxel.GetRegionIndex(new Position(0, 0, 0)); // on voxel is considered in Assert.AreEqual(1, index); // two layer results - index = _twoLayerTissue.GetRegionIndex(new Position(10, 0, 0)); // outside voxel + index = _twoLayerTissueBoundedByVoxel.GetRegionIndex(new Position(10, 0, 0)); // outside voxel Assert.AreEqual(4, index); - index = _twoLayerTissue.GetRegionIndex(new Position(0, 0, 2.5)); // inside voxel + index = _twoLayerTissueBoundedByVoxel.GetRegionIndex(new Position(0, 0, 2.5)); // inside voxel Assert.AreEqual(2, index); - index = _twoLayerTissue.GetRegionIndex(new Position(0, 0, 0)); // on voxel is considered in + index = _twoLayerTissueBoundedByVoxel.GetRegionIndex(new Position(0, 0, 0)); // on voxel is considered in Assert.AreEqual(1, index); } @@ -89,56 +89,56 @@ public void Verify_GetNeighborRegionIndex_method_returns_correct_result() new Position(-1, 0, 1), new Direction(1.0, 0, 0), 1.0, - _oneLayerTissue, + _oneLayerTissueBoundedByVoxel, 3, new Random()); - var index = _oneLayerTissue.GetNeighborRegionIndex(photon); + var index = _oneLayerTissueBoundedByVoxel.GetNeighborRegionIndex(photon); Assert.AreEqual(1, index); photon = new Photon( // on side of voxel pointed out of it new Position(-1, 0, 1), new Direction(-1.0, 0, 0), 1.0, - _oneLayerTissue, + _oneLayerTissueBoundedByVoxel, 1, new Random()); - index = _oneLayerTissue.GetNeighborRegionIndex(photon); + index = _oneLayerTissueBoundedByVoxel.GetNeighborRegionIndex(photon); Assert.AreEqual(3, index); // two layer results photon = new Photon( // on side of voxel pointed into LAYER 1 new Position(-1, 0, 0.5), new Direction(1.0, 0, 0), 1.0, - _twoLayerTissue, + _twoLayerTissueBoundedByVoxel, 4, new Random()); - index = _twoLayerTissue.GetNeighborRegionIndex(photon); + index = _twoLayerTissueBoundedByVoxel.GetNeighborRegionIndex(photon); Assert.AreEqual(1, index); photon = new Photon( // on side of voxel in LAYER 1 pointed out of it new Position(-1, 0, 0.5), new Direction(-1.0, 0, 0), 1.0, - _twoLayerTissue, + _twoLayerTissueBoundedByVoxel, 1, new Random()); - index = _twoLayerTissue.GetNeighborRegionIndex(photon); + index = _twoLayerTissueBoundedByVoxel.GetNeighborRegionIndex(photon); Assert.AreEqual(4, index); photon = new Photon( // on side of voxel pointed into LAYER 2 new Position(-1, 0, 1.5), new Direction(1.0, 0, 0), 1.0, - _twoLayerTissue, + _twoLayerTissueBoundedByVoxel, 4, new Random()); - index = _twoLayerTissue.GetNeighborRegionIndex(photon); + index = _twoLayerTissueBoundedByVoxel.GetNeighborRegionIndex(photon); Assert.AreEqual(2, index); photon = new Photon( // on side of voxel in LAYER 2 pointed out of it new Position(-1, 0, 1.5), new Direction(-1.0, 0, 0), 1.0, - _twoLayerTissue, + _twoLayerTissueBoundedByVoxel, 1, new Random()); - index = _twoLayerTissue.GetNeighborRegionIndex(photon); + index = _twoLayerTissueBoundedByVoxel.GetNeighborRegionIndex(photon); Assert.AreEqual(4, index); } @@ -152,15 +152,15 @@ public void Verify_GetAngleRelativeToBoundaryNormal_method_returns_correct_resul new Position(0, 0, 0.0), new Direction(0.0, 0, 1.0), 1, - _twoLayerTissue, + _twoLayerTissueBoundedByVoxel, 1, new Random()); - var cosTheta = _twoLayerTissue.GetAngleRelativeToBoundaryNormal(photon); + var cosTheta = _twoLayerTissueBoundedByVoxel.GetAngleRelativeToBoundaryNormal(photon); Assert.AreEqual(1,cosTheta); // put on side of cylinder pointing in photon.DP.Position = new Position(-1.0, 0.0, 5.0); photon.DP.Direction = new Direction(1.0, 0.0, 0.0); - cosTheta = _twoLayerTissue.GetAngleRelativeToBoundaryNormal(photon); + cosTheta = _twoLayerTissueBoundedByVoxel.GetAngleRelativeToBoundaryNormal(photon); Assert.AreEqual(1, cosTheta); } diff --git a/src/Vts.Test/MonteCarlo/Tissues/CaplessCylinderTissueRegionTests.cs b/src/Vts.Test/MonteCarlo/Tissues/CaplessCylinderTissueRegionTests.cs index 970218977..91bf486f8 100644 --- a/src/Vts.Test/MonteCarlo/Tissues/CaplessCylinderTissueRegionTests.cs +++ b/src/Vts.Test/MonteCarlo/Tissues/CaplessCylinderTissueRegionTests.cs @@ -47,10 +47,10 @@ public void Validate_caplessCylinder_properties() /// Currently OnBoundary of an inclusion region isn't called by any code ckh 3/5/19. /// [Test] - public void verify_OnBoundary_method_returns_correct_result() + public void Verify_OnBoundary_method_returns_correct_result() { // OnBoundary returns true if *exactly* on boundary - bool result = _caplessCylinderTissueRegion.OnBoundary(new Position(0, 0, 1.0)); // on top cap boundary + var result = _caplessCylinderTissueRegion.OnBoundary(new Position(0, 0, 1.0)); // on top cap boundary Assert.IsFalse(result); result = _caplessCylinderTissueRegion.OnBoundary(new Position(0, 0, 3.0)); // on bottom cap boundary -> so false Assert.IsFalse(result); @@ -65,9 +65,9 @@ public void verify_OnBoundary_method_returns_correct_result() /// or *on* boundary. /// [Test] - public void verify_ContainsPosition_method_returns_correct_result() + public void Verify_ContainsPosition_method_returns_correct_result() { - bool result = _caplessCylinderTissueRegion.ContainsPosition(new Position(0, 0, 2.0)); // inside + var result = _caplessCylinderTissueRegion.ContainsPosition(new Position(0, 0, 2.0)); // inside Assert.IsTrue(result); result = _caplessCylinderTissueRegion.ContainsPosition(new Position(0, 0, 3.0)); // on boundary Assert.IsTrue(result); @@ -77,44 +77,49 @@ public void verify_ContainsPosition_method_returns_correct_result() /// Validate method RayIntersectBoundary return correct result /// [Test] - public void verify_RayIntersectBoundary_method_returns_correct_result() + public void Verify_RayIntersectBoundary_method_returns_correct_result() { // test intersection with sides - Photon photon = new Photon(); - photon.DP.Position = new Position(0, 0, 2); - photon.DP.Direction = new Direction(1, 0, 0); - photon.S = 2.0; // definitely intersect sides + var photon = new Photon + { + DP = + { + Position = new Position(0, 0, 2), + Direction = new Direction(1, 0, 0) + }, + S = 2.0 // definitely intersect sides + }; double distanceToBoundary; - bool result = _caplessCylinderTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); + var result = _caplessCylinderTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(true, result); Assert.AreEqual(1.0, distanceToBoundary); photon.S = 0.5; // definitely don't intersect sides result = _caplessCylinderTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(false, result); - Assert.AreEqual(Double.PositiveInfinity, distanceToBoundary); + Assert.AreEqual(double.PositiveInfinity, distanceToBoundary); photon.S = 1.0; // ends right at boundary => both out and no intersection result = _caplessCylinderTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(false, result); - Assert.AreEqual(Double.PositiveInfinity, distanceToBoundary); + Assert.AreEqual(double.PositiveInfinity, distanceToBoundary); // intersect cap of caplessCylinder tests photon.DP.Position = new Position(0, 0, 0); // intersect top cap photon.DP.Direction = new Direction(0, 0, 1); photon.S = 2.0; // make sure intersects top cap result = _caplessCylinderTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(false, result); - Assert.AreEqual(Double.PositiveInfinity, distanceToBoundary); + Assert.AreEqual(double.PositiveInfinity, distanceToBoundary); photon.DP.Position = new Position(0, 0, 4); // intersect bottom cap photon.DP.Direction = new Direction(0, 0, -1); photon.S = 2.0; // make sure intersects top cap result = _caplessCylinderTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(false, result); - Assert.AreEqual(Double.PositiveInfinity, distanceToBoundary); + Assert.AreEqual(double.PositiveInfinity, distanceToBoundary); photon.DP.Position = new Position(0, 0, 0); // intersect both photon.DP.Direction = new Direction(0, 0, 1); photon.S = 10.0; // make sure intersects both result = _caplessCylinderTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(false, result); - Assert.AreEqual(Double.PositiveInfinity, distanceToBoundary); + Assert.AreEqual(double.PositiveInfinity, distanceToBoundary); } } } diff --git a/src/Vts.Test/MonteCarlo/Tissues/CaplessVoxelTissueRegionTests.cs b/src/Vts.Test/MonteCarlo/Tissues/CaplessVoxelTissueRegionTests.cs index a7147f655..656010022 100644 --- a/src/Vts.Test/MonteCarlo/Tissues/CaplessVoxelTissueRegionTests.cs +++ b/src/Vts.Test/MonteCarlo/Tissues/CaplessVoxelTissueRegionTests.cs @@ -102,10 +102,15 @@ public void Verify_SurfaceNormal_method_returns_correct_result() [Test] public void Verify_RayIntersectBoundary_method_returns_correct_result() { - var photon = new Photon(); - photon.DP.Position = new Position(-2, 0, 2); - photon.DP.Direction = new Direction(1, 0, 0); - photon.S = 10.0; // definitely intersect + var photon = new Photon + { + DP = + { + Position = new Position(-2, 0, 2), + Direction = new Direction(1, 0, 0) + }, + S = 10.0 // definitely intersect + }; double distanceToBoundary; var result = _caplessVoxelTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(true, result); @@ -113,7 +118,7 @@ public void Verify_RayIntersectBoundary_method_returns_correct_result() photon.S = 0.5; // definitely don't intersect result = _caplessVoxelTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(false, result); - Assert.AreEqual(Double.PositiveInfinity, distanceToBoundary); + Assert.AreEqual(double.PositiveInfinity, distanceToBoundary); photon.S = 1.0; // ends right at boundary => intersection result = _caplessVoxelTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(true, result); diff --git a/src/Vts.Test/MonteCarlo/Tissues/CylinderTissueRegionTests.cs b/src/Vts.Test/MonteCarlo/Tissues/CylinderTissueRegionTests.cs index 39d4ee095..c4e980d4a 100644 --- a/src/Vts.Test/MonteCarlo/Tissues/CylinderTissueRegionTests.cs +++ b/src/Vts.Test/MonteCarlo/Tissues/CylinderTissueRegionTests.cs @@ -17,7 +17,7 @@ public class CylinderTissueRegionTests /// Validate general constructor of TissueRegion /// [OneTimeSetUp] - public void create_instance_of_class() + public void Create_instance_of_class() { _cylinderTissueRegion = new CylinderTissueRegion( new Position(0, 0, 2), // center @@ -29,7 +29,7 @@ public void create_instance_of_class() /// Validate general constructor of TissueRegion /// [Test] - public void validate_cylinder_properties() + public void Validate_cylinder_properties() { Assert.AreEqual(0.0, _cylinderTissueRegion.Center.X); Assert.AreEqual(0.0, _cylinderTissueRegion.Center.Y); @@ -47,10 +47,10 @@ public void validate_cylinder_properties() /// Currently OnBoundary of an inclusion region isn't called by any code ckh 3/5/19. /// [Test] - public void verify_OnBoundary_method_returns_correct_result() + public void Verify_OnBoundary_method_returns_correct_result() { // OnBoundary returns true if *exactly* on boundary - bool result = _cylinderTissueRegion.OnBoundary(new Position(0, 0, 1.0)); // on top cap boundary + var result = _cylinderTissueRegion.OnBoundary(new Position(0, 0, 1.0)); // on top cap boundary Assert.IsTrue(result); result = _cylinderTissueRegion.OnBoundary(new Position(0, 0, 3.0)); // on bottom cap boundary Assert.IsTrue(result); @@ -65,9 +65,9 @@ public void verify_OnBoundary_method_returns_correct_result() /// or *on* boundary. /// [Test] - public void verify_ContainsPosition_method_returns_correct_result() + public void Verify_ContainsPosition_method_returns_correct_result() { - bool result = _cylinderTissueRegion.ContainsPosition(new Position(0, 0, 2.0)); // inside + var result = _cylinderTissueRegion.ContainsPosition(new Position(0, 0, 2.0)); // inside Assert.IsTrue(result); result = _cylinderTissueRegion.ContainsPosition(new Position(0, 0, 3.0)); // on boundary Assert.IsTrue(result); @@ -77,25 +77,30 @@ public void verify_ContainsPosition_method_returns_correct_result() /// Validate method RayIntersectBoundary return correct result /// [Test] - public void verify_RayIntersectBoundary_method_returns_correct_result() + public void Verify_RayIntersectBoundary_method_returns_correct_result() { // test intersection with sides - Photon photon = new Photon(); - photon.DP.Position = new Position(0, 0, 2); - photon.DP.Direction = new Direction(1, 0, 0); - photon.S = 2.0; // definitely intersect sides + var photon = new Photon + { + DP = + { + Position = new Position(0, 0, 2), + Direction = new Direction(1, 0, 0) + }, + S = 2.0 // definitely intersect sides + }; double distanceToBoundary; - bool result = _cylinderTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); + var result = _cylinderTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(true, result); Assert.AreEqual(1.0, distanceToBoundary); photon.S = 0.5; // definitely don't intersect sides result = _cylinderTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(false, result); - Assert.AreEqual(Double.PositiveInfinity, distanceToBoundary); + Assert.AreEqual(double.PositiveInfinity, distanceToBoundary); photon.S = 1.0; // ends right at boundary => both out and no intersection result = _cylinderTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(false, result); - Assert.AreEqual(Double.PositiveInfinity, distanceToBoundary); + Assert.AreEqual(double.PositiveInfinity, distanceToBoundary); // intersect cap of cylinder tests photon.DP.Position = new Position(0, 0, 0); // intersect top cap photon.DP.Direction = new Direction(0, 0, 1); diff --git a/src/Vts.Test/MonteCarlo/Tissues/EllipsoidTissueRegionTests.cs b/src/Vts.Test/MonteCarlo/Tissues/EllipsoidTissueRegionTests.cs index 80528d408..736b6ba5f 100644 --- a/src/Vts.Test/MonteCarlo/Tissues/EllipsoidTissueRegionTests.cs +++ b/src/Vts.Test/MonteCarlo/Tissues/EllipsoidTissueRegionTests.cs @@ -85,10 +85,15 @@ public void Verify_SurfaceNormal_method_returns_correct_result() [Test] public void Verify_RayIntersectBoundary_method_returns_correct_result() { - var photon = new Photon(); - photon.DP.Position = new Position(-2, 0, 3); - photon.DP.Direction = new Direction(1, 0, 0); - photon.S = 10.0; // definitely intersect + var photon = new Photon + { + DP = + { + Position = new Position(-2, 0, 3), + Direction = new Direction(1, 0, 0) + }, + S = 10.0 // definitely intersect + }; double distanceToBoundary; var result = _ellipsoidTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(true, result); diff --git a/src/Vts.Test/MonteCarlo/Tissues/VoxelTissueRegionTests.cs b/src/Vts.Test/MonteCarlo/Tissues/VoxelTissueRegionTests.cs index c04cb754b..a0a9ff31b 100644 --- a/src/Vts.Test/MonteCarlo/Tissues/VoxelTissueRegionTests.cs +++ b/src/Vts.Test/MonteCarlo/Tissues/VoxelTissueRegionTests.cs @@ -121,7 +121,7 @@ public void Verify_RayIntersectBoundary_method_returns_correct_result() photon.S = 0.5; // definitely don't intersect result = _voxelTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(false, result); - Assert.AreEqual(Double.PositiveInfinity, distanceToBoundary); + Assert.AreEqual(double.PositiveInfinity, distanceToBoundary); photon.S = 1.0; // ends right at boundary => intersection result = _voxelTissueRegion.RayIntersectBoundary(photon, out distanceToBoundary); Assert.AreEqual(true, result); diff --git a/src/Vts/MonteCarlo/Extensions/VirtualBoundariesExtensionMethods.cs b/src/Vts/MonteCarlo/Extensions/VirtualBoundariesExtensionMethods.cs index d89b9c333..afdc3e96b 100644 --- a/src/Vts/MonteCarlo/Extensions/VirtualBoundariesExtensionMethods.cs +++ b/src/Vts/MonteCarlo/Extensions/VirtualBoundariesExtensionMethods.cs @@ -14,23 +14,19 @@ public static class VirtualBoundariesExtensionMethods /// true if surface VB, false if not public static bool IsSurfaceVirtualBoundary(this VirtualBoundaryType virtualBoundaryType) { - switch (virtualBoundaryType) + return virtualBoundaryType switch { - case VirtualBoundaryType.DiffuseReflectance: - case VirtualBoundaryType.DiffuseTransmittance: - case VirtualBoundaryType.SpecularReflectance: - case VirtualBoundaryType.pMCDiffuseReflectance: - case VirtualBoundaryType.pMCDiffuseTransmittance: - case VirtualBoundaryType.InternalSurface: - return true; - case VirtualBoundaryType.GenericVolumeBoundary: - case VirtualBoundaryType.BoundingVolume: - return false; - default: - throw new ArgumentOutOfRangeException( - "Virtual Boundary type not recognized: " + virtualBoundaryType); - - } + VirtualBoundaryType.DiffuseReflectance => true, + VirtualBoundaryType.DiffuseTransmittance => true, + VirtualBoundaryType.SpecularReflectance => true, + VirtualBoundaryType.pMCDiffuseReflectance => true, + VirtualBoundaryType.pMCDiffuseTransmittance => true, + VirtualBoundaryType.InternalSurface => true, + VirtualBoundaryType.GenericVolumeBoundary => false, + VirtualBoundaryType.BoundingVolume => false, + _ => throw new ArgumentOutOfRangeException("Virtual Boundary type not recognized: " + + virtualBoundaryType) + }; } /// /// Method to determine if VB is volume VB or not @@ -39,24 +35,19 @@ public static bool IsSurfaceVirtualBoundary(this VirtualBoundaryType virtualBoun /// true if volume VB, false if not public static bool IsVolumeVirtualBoundary(this VirtualBoundaryType virtualBoundaryType) { - switch (virtualBoundaryType) + return virtualBoundaryType switch { - case VirtualBoundaryType.GenericVolumeBoundary: - return true; - case VirtualBoundaryType.BoundingVolume: - return true; - case VirtualBoundaryType.DiffuseReflectance: - case VirtualBoundaryType.DiffuseTransmittance: - case VirtualBoundaryType.SpecularReflectance: - case VirtualBoundaryType.InternalSurface: - case VirtualBoundaryType.pMCDiffuseReflectance: - case VirtualBoundaryType.pMCDiffuseTransmittance: - return false; - default: - throw new ArgumentOutOfRangeException( - "Virtual Boundary type not recognized: " + virtualBoundaryType); - - } + VirtualBoundaryType.GenericVolumeBoundary => true, + VirtualBoundaryType.BoundingVolume => true, + VirtualBoundaryType.DiffuseReflectance => false, + VirtualBoundaryType.DiffuseTransmittance => false, + VirtualBoundaryType.SpecularReflectance => false, + VirtualBoundaryType.InternalSurface => false, + VirtualBoundaryType.pMCDiffuseReflectance => false, + VirtualBoundaryType.pMCDiffuseTransmittance => false, + _ => throw new ArgumentOutOfRangeException("Virtual Boundary type not recognized: " + + virtualBoundaryType) + }; } /// /// Method to determine if VB is surface reflectance VB or not @@ -65,23 +56,19 @@ public static bool IsVolumeVirtualBoundary(this VirtualBoundaryType virtualBound /// true of surface reflectance VB, false if not public static bool IsReflectanceSurfaceVirtualBoundary(this VirtualBoundaryType virtualBoundaryType) { - switch (virtualBoundaryType) + return virtualBoundaryType switch { - case VirtualBoundaryType.DiffuseReflectance: - case VirtualBoundaryType.pMCDiffuseReflectance: - return true; - case VirtualBoundaryType.DiffuseTransmittance: - case VirtualBoundaryType.SpecularReflectance: - case VirtualBoundaryType.GenericVolumeBoundary: - case VirtualBoundaryType.InternalSurface: - case VirtualBoundaryType.pMCDiffuseTransmittance: - case VirtualBoundaryType.BoundingVolume: - return false; - default: - throw new ArgumentOutOfRangeException( - "Virtual Boundary type not recognized: " + virtualBoundaryType); - - } + VirtualBoundaryType.DiffuseReflectance => true, + VirtualBoundaryType.pMCDiffuseReflectance => true, + VirtualBoundaryType.DiffuseTransmittance => false, + VirtualBoundaryType.SpecularReflectance => false, + VirtualBoundaryType.GenericVolumeBoundary => false, + VirtualBoundaryType.InternalSurface => false, + VirtualBoundaryType.pMCDiffuseTransmittance => false, + VirtualBoundaryType.BoundingVolume => false, + _ => throw new ArgumentOutOfRangeException("Virtual Boundary type not recognized: " + + virtualBoundaryType) + }; } /// /// Method to determine if transmittance surface VB or not @@ -90,23 +77,19 @@ public static bool IsReflectanceSurfaceVirtualBoundary(this VirtualBoundaryType /// true if transmittance surface VB, false if not public static bool IsTransmittanceSurfaceVirtualBoundary(this VirtualBoundaryType virtualBoundaryType) { - switch (virtualBoundaryType) + return virtualBoundaryType switch { - case VirtualBoundaryType.DiffuseTransmittance: - case VirtualBoundaryType.pMCDiffuseTransmittance: - return true; - case VirtualBoundaryType.DiffuseReflectance: - case VirtualBoundaryType.SpecularReflectance: - case VirtualBoundaryType.GenericVolumeBoundary: - case VirtualBoundaryType.InternalSurface: - case VirtualBoundaryType.pMCDiffuseReflectance: - case VirtualBoundaryType.BoundingVolume: - return false; - default: - throw new ArgumentOutOfRangeException( - "Virtual Boundary type not recognized: " + virtualBoundaryType); - - } + VirtualBoundaryType.DiffuseTransmittance => true, + VirtualBoundaryType.pMCDiffuseTransmittance => true, + VirtualBoundaryType.DiffuseReflectance => false, + VirtualBoundaryType.SpecularReflectance => false, + VirtualBoundaryType.GenericVolumeBoundary => false, + VirtualBoundaryType.InternalSurface => false, + VirtualBoundaryType.pMCDiffuseReflectance => false, + VirtualBoundaryType.BoundingVolume => false, + _ => throw new ArgumentOutOfRangeException("Virtual Boundary type not recognized: " + + virtualBoundaryType) + }; } /// /// Method to determine if specular surface VB or not @@ -115,23 +98,19 @@ public static bool IsTransmittanceSurfaceVirtualBoundary(this VirtualBoundaryTyp /// true if transmittance surface VB, false if not public static bool IsSpecularSurfaceVirtualBoundary(this VirtualBoundaryType virtualBoundaryType) { - switch (virtualBoundaryType) + return virtualBoundaryType switch { - case VirtualBoundaryType.SpecularReflectance: - return true; - case VirtualBoundaryType.DiffuseReflectance: - case VirtualBoundaryType.DiffuseTransmittance: - case VirtualBoundaryType.GenericVolumeBoundary: - case VirtualBoundaryType.InternalSurface: - case VirtualBoundaryType.pMCDiffuseReflectance: - case VirtualBoundaryType.pMCDiffuseTransmittance: - case VirtualBoundaryType.BoundingVolume: - return false; - default: - throw new ArgumentOutOfRangeException( - "Virtual Boundary type not recognized: " + virtualBoundaryType); - - } + VirtualBoundaryType.SpecularReflectance => true, + VirtualBoundaryType.DiffuseReflectance => false, + VirtualBoundaryType.DiffuseTransmittance => false, + VirtualBoundaryType.GenericVolumeBoundary => false, + VirtualBoundaryType.InternalSurface => false, + VirtualBoundaryType.pMCDiffuseReflectance => false, + VirtualBoundaryType.pMCDiffuseTransmittance => false, + VirtualBoundaryType.BoundingVolume => false, + _ => throw new ArgumentOutOfRangeException("Virtual Boundary type not recognized: " + + virtualBoundaryType) + }; } /// /// Method to determine if internal surface (dosimetry) VB or not @@ -140,23 +119,19 @@ public static bool IsSpecularSurfaceVirtualBoundary(this VirtualBoundaryType vir /// true if internal surface VB, false if not public static bool IsInternalSurfaceVirtualBoundary(this VirtualBoundaryType virtualBoundaryType) { - switch (virtualBoundaryType) + return virtualBoundaryType switch { - case VirtualBoundaryType.InternalSurface: - return true; - case VirtualBoundaryType.DiffuseReflectance: - case VirtualBoundaryType.DiffuseTransmittance: - case VirtualBoundaryType.SpecularReflectance: - case VirtualBoundaryType.GenericVolumeBoundary: - case VirtualBoundaryType.pMCDiffuseReflectance: - case VirtualBoundaryType.pMCDiffuseTransmittance: - case VirtualBoundaryType.BoundingVolume: - return false; - default: - throw new ArgumentOutOfRangeException( - "Virtual Boundary type not recognized: " + virtualBoundaryType); - - } + VirtualBoundaryType.InternalSurface => true, + VirtualBoundaryType.DiffuseReflectance => false, + VirtualBoundaryType.DiffuseTransmittance => false, + VirtualBoundaryType.SpecularReflectance => false, + VirtualBoundaryType.GenericVolumeBoundary => false, + VirtualBoundaryType.pMCDiffuseReflectance => false, + VirtualBoundaryType.pMCDiffuseTransmittance => false, + VirtualBoundaryType.BoundingVolume => false, + _ => throw new ArgumentOutOfRangeException("Virtual Boundary type not recognized: " + + virtualBoundaryType) + }; } /// @@ -166,23 +141,19 @@ public static bool IsInternalSurfaceVirtualBoundary(this VirtualBoundaryType vir /// true if generic volume VB, false if not public static bool IsGenericVolumeVirtualBoundary(this VirtualBoundaryType virtualBoundaryType) { - switch (virtualBoundaryType) + return virtualBoundaryType switch { - case VirtualBoundaryType.GenericVolumeBoundary: - return true; - case VirtualBoundaryType.DiffuseReflectance: - case VirtualBoundaryType.DiffuseTransmittance: - case VirtualBoundaryType.SpecularReflectance: - case VirtualBoundaryType.InternalSurface: - case VirtualBoundaryType.pMCDiffuseReflectance: - case VirtualBoundaryType.pMCDiffuseTransmittance: - case VirtualBoundaryType.BoundingVolume: - return false; - default: - throw new ArgumentOutOfRangeException( - "Virtual Boundary type not recognized: " + virtualBoundaryType); - - } + VirtualBoundaryType.GenericVolumeBoundary => true, + VirtualBoundaryType.DiffuseReflectance => false, + VirtualBoundaryType.DiffuseTransmittance => false, + VirtualBoundaryType.SpecularReflectance => false, + VirtualBoundaryType.InternalSurface => false, + VirtualBoundaryType.pMCDiffuseReflectance => false, + VirtualBoundaryType.pMCDiffuseTransmittance => false, + VirtualBoundaryType.BoundingVolume => false, + _ => throw new ArgumentOutOfRangeException("Virtual Boundary type not recognized: " + + virtualBoundaryType) + }; } /// /// Method to determine if perturbation Monte Carlo (pMC) VB or not @@ -191,22 +162,19 @@ public static bool IsGenericVolumeVirtualBoundary(this VirtualBoundaryType virtu /// true if pMC VB, false if not public static bool IspMCVirtualBoundary(this VirtualBoundaryType virtualBoundaryType) { - switch (virtualBoundaryType) + return virtualBoundaryType switch { - case VirtualBoundaryType.pMCDiffuseReflectance: - case VirtualBoundaryType.pMCDiffuseTransmittance: - return true; - case VirtualBoundaryType.GenericVolumeBoundary: - case VirtualBoundaryType.BoundingVolume: - case VirtualBoundaryType.DiffuseReflectance: - case VirtualBoundaryType.DiffuseTransmittance: - case VirtualBoundaryType.SpecularReflectance: - case VirtualBoundaryType.InternalSurface: - return false; - default: - throw new ArgumentOutOfRangeException( - "Virtual Boundary type not recognized: " + virtualBoundaryType); - } + VirtualBoundaryType.pMCDiffuseReflectance => true, + VirtualBoundaryType.pMCDiffuseTransmittance => true, + VirtualBoundaryType.GenericVolumeBoundary => false, + VirtualBoundaryType.BoundingVolume => false, + VirtualBoundaryType.DiffuseReflectance => false, + VirtualBoundaryType.DiffuseTransmittance => false, + VirtualBoundaryType.SpecularReflectance => false, + VirtualBoundaryType.InternalSurface => false, + _ => throw new ArgumentOutOfRangeException("Virtual Boundary type not recognized: " + + virtualBoundaryType) + }; } } } diff --git a/src/Vts/MonteCarlo/Photon.cs b/src/Vts/MonteCarlo/Photon.cs index 7caebc079..e09436430 100644 --- a/src/Vts/MonteCarlo/Photon.cs +++ b/src/Vts/MonteCarlo/Photon.cs @@ -117,19 +117,13 @@ public Photon() private void SetAbsorbAction(AbsorptionWeightingType awt) { - switch (awt) + Absorb = awt switch { - case AbsorptionWeightingType.Analog: - Absorb = AbsorbAnalog; - break; - case AbsorptionWeightingType.Continuous: - Absorb = AbsorbContinuous; - break; - case AbsorptionWeightingType.Discrete: - default: - Absorb = AbsorbDiscrete; - break; - } + AbsorptionWeightingType.Analog => AbsorbAnalog, + AbsorptionWeightingType.Continuous => AbsorbContinuous, + AbsorptionWeightingType.Discrete => AbsorbDiscrete, + _ => AbsorbDiscrete + }; } private void SetScatterAction(PhaseFunctionType st) { @@ -319,14 +313,13 @@ public void Scatter1D() /// public void AbsorbAnalog() { - if (_rng.NextDouble() > _tissue.Regions[CurrentRegionIndex].RegionOP.Mus / - (_tissue.Regions[CurrentRegionIndex].RegionOP.Mus + - _tissue.Regions[CurrentRegionIndex].RegionOP.Mua)) - { - DP.StateFlag = DP.StateFlag.Add(PhotonStateType.Absorbed); - DP.StateFlag = DP.StateFlag.Remove(PhotonStateType.Alive); - History.AddDPToHistory(DP); - } + if (!(_rng.NextDouble() > _tissue.Regions[CurrentRegionIndex].RegionOP.Mus / + (_tissue.Regions[CurrentRegionIndex].RegionOP.Mus + + _tissue.Regions[CurrentRegionIndex].RegionOP.Mua))) return; + + DP.StateFlag = DP.StateFlag.Add(PhotonStateType.Absorbed); + DP.StateFlag = DP.StateFlag.Remove(PhotonStateType.Alive); + History.AddDPToHistory(DP); } /// /// Method to de-weight for absorption according to discrete absorption weighting (DAW) @@ -368,14 +361,13 @@ public void TestDeath() { TestWeightAndDistance(); // if VB crossing flagged - if (DP.StateFlag.HasFlag(PhotonStateType.PseudoDiffuseReflectanceVirtualBoundary) || - DP.StateFlag.HasFlag(PhotonStateType.PseudoDiffuseTransmittanceVirtualBoundary) || - DP.StateFlag.HasFlag(PhotonStateType.PseudoSpecularReflectanceVirtualBoundary) || - DP.StateFlag.HasFlag(PhotonStateType.PseudoLateralBoundingVirtualBoundary)) - { - DP.StateFlag = DP.StateFlag.Remove(PhotonStateType.Alive); - History.AddDPToHistory(DP); - } + if (!DP.StateFlag.HasFlag(PhotonStateType.PseudoDiffuseReflectanceVirtualBoundary) && + !DP.StateFlag.HasFlag(PhotonStateType.PseudoDiffuseTransmittanceVirtualBoundary) && + !DP.StateFlag.HasFlag(PhotonStateType.PseudoSpecularReflectanceVirtualBoundary) && + !DP.StateFlag.HasFlag(PhotonStateType.PseudoLateralBoundingVirtualBoundary)) return; + + DP.StateFlag = DP.StateFlag.Remove(PhotonStateType.Alive); + History.AddDPToHistory(DP); } /// /// Method that kills photon due to Russian Roulette, maximum path length, etc. diff --git a/src/Vts/MonteCarlo/Tissues/CaplessVoxelTissueRegion.cs b/src/Vts/MonteCarlo/Tissues/CaplessVoxelTissueRegion.cs index 2d9b7bb8d..d583f1d66 100644 --- a/src/Vts/MonteCarlo/Tissues/CaplessVoxelTissueRegion.cs +++ b/src/Vts/MonteCarlo/Tissues/CaplessVoxelTissueRegion.cs @@ -70,7 +70,7 @@ public CaplessVoxelTissueRegion() : this( public Position Center { get => - new Position( + new( (X.Start + X.Stop)/2, (Y.Start + Y.Stop)/2, (Z.Start + Z.Stop)/2); diff --git a/src/Vts/MonteCarlo/Tissues/EllipsoidTissueRegion.cs b/src/Vts/MonteCarlo/Tissues/EllipsoidTissueRegion.cs index c86d6666d..ea0eac190 100644 --- a/src/Vts/MonteCarlo/Tissues/EllipsoidTissueRegion.cs +++ b/src/Vts/MonteCarlo/Tissues/EllipsoidTissueRegion.cs @@ -179,13 +179,13 @@ public bool RayIntersectBoundary(Photon photon, out double distanceToBoundary) var numberOfIntersections = 0; //number of intersections - if (root1 < 1 && root1 > 0) + if (root1 is < 1 and > 0) { numberOfIntersections += 1; root = root1; } - if (root2 < 1 && root2 > 0) + if (root2 is < 1 and > 0) { numberOfIntersections += 1; root = root2;