def test_z(self): """Get/set z co-ordinate.""" v = Point3D(2.5, 6.7, -4.6) # get z attribute self.assertEqual(v.z, -4.6, "Getting z attribute failed.") # set z attribute v.z = 157.3 self.assertEqual(v.z, 157.3, "Setting z attribute failed.")
def test_contains(self): """Method contains() is virtual and should raise an exception if called.""" n = Primitive() with self.assertRaises( NotImplementedError, msg= "Virtual method did not raise NotImplementedError exception when called." ): n.contains(Point3D())
def test_y(self): """Get/set y co-ordinate.""" v = Point3D(2.5, 6.7, -4.6) # get y attribute self.assertEqual(v.y, 6.7, "Getting y attribute failed.") # set y attribute v.y = -7.1 self.assertEqual(v.y, -7.1, "Setting y attribute failed.")
def test_x(self): """Get/set x co-ordinate.""" v = Point3D(2.5, 6.7, -4.6) # get x attribute self.assertEqual(v.x, 2.5, "Getting x attribute failed.") # set x attribute v.x = 10.0 self.assertEqual(v.x, 10.0, "Setting x attribute failed.")
def test_iter(self): """Obtain values by iteration.""" p = Point3D(2.5, 6.7, -4.6) l = list(p) self.assertEqual( len(l), 3, "Iteration failed to return the correct number of items.") self.assertEqual(l[0], 2.5, "Iteration failed [X].") self.assertEqual(l[1], 6.7, "Iteration failed [Y].") self.assertEqual(l[2], -4.6, "Iteration failed [Z].")
def test_vector_initialise(self): """Initialisation.""" v = Vector3D(3, -4, 5) n = Normal3D(6, 3, -9) p = Point3D(-5, -2, 10) # check Vector3D can be initialise by other vector objects r = Vector3D(*n) self.assertEqual(r.x, 6, "Vector3D initialisation failed [X].") self.assertEqual(r.y, 3, "Vector3D initialisation failed [Y].") self.assertEqual(r.z, -9, "Vector3D initialisation failed [Z].") r = Vector3D(*p) self.assertEqual(r.x, -5, "Vector3D initialisation failed [X].") self.assertEqual(r.y, -2, "Vector3D initialisation failed [Y].") self.assertEqual(r.z, 10, "Vector3D initialisation failed [Z].") # check Normal3D can be initialise by other vector objects r = Normal3D(*v) self.assertEqual(r.x, 3, "Normal3D initialisation failed [X].") self.assertEqual(r.y, -4, "Normal3D initialisation failed [Y].") self.assertEqual(r.z, 5, "Normal3D initialisation failed [Z].") r = Normal3D(*p) self.assertEqual(r.x, -5, "Normal3D initialisation failed [X].") self.assertEqual(r.y, -2, "Normal3D initialisation failed [Y].") self.assertEqual(r.z, 10, "Normal3D initialisation failed [Z].") # check Point3D can be initialise by other vector objects r = Point3D(*v) self.assertEqual(r.x, 3, "Point3D initialisation failed [X].") self.assertEqual(r.y, -4, "Point3D initialisation failed [Y].") self.assertEqual(r.z, 5, "Point3D initialisation failed [Z].") r = Point3D(*n) self.assertEqual(r.x, 6, "Point3D initialisation failed [X].") self.assertEqual(r.y, 3, "Point3D initialisation failed [Y].") self.assertEqual(r.z, -9, "Point3D initialisation failed [Z].")
def test_copy(self): """Testing method copy().""" v = Point3D(1.0, 2.0, 3.0) r = v.copy() # check a new instance has been created by modifying the original v.x = 5.0 v.y = 6.0 v.z = 7.0 self.assertEqual(r.x, 1.0, "Copy failed [X].") self.assertEqual(r.y, 2.0, "Copy failed [Y].") self.assertEqual(r.z, 3.0, "Copy failed [Z].")
def test_vector_add(self): """Add operator.""" v = Vector3D(3, -4, 5) n = Normal3D(6, 3, -9) p = Point3D(-5, -2, 10) # Vector3D and Normal3D r = v + n self.assertTrue(isinstance(r, Vector3D), "Vector3D addition did not return a Vector3D.") self.assertEqual(r.x, 3 + 6, "Vector3D + Normal3D failed [X].") self.assertEqual(r.y, -4 + 3, "Vector3D + Normal3D failed [X].") self.assertEqual(r.z, 5 - 9, "Vector3D + Normal3D failed [X].") r = n + v self.assertTrue(isinstance(r, Normal3D), "Vector3D addition did not return a Normal3D.") self.assertEqual(r.x, 3 + 6, "Normal3D + Vector3D failed [X].") self.assertEqual(r.y, -4 + 3, "Normal3D + Vector3D failed [X].") self.assertEqual(r.z, 5 - 9, "Normal3D + Vector3D failed [X].") # Point3D and Vector3D r = p + v self.assertTrue(isinstance(r, Point3D), "Vector3D addition did not return a Point3D.") self.assertEqual(r.x, -5 + 3, "Point3D + Vector3D failed [X].") self.assertEqual(r.y, -2 - 4, "Point3D + Vector3D failed [X].") self.assertEqual(r.z, 10 + 5, "Point3D + Vector3D failed [X].") with self.assertRaises( TypeError, msg="Vector3D + Point3D should have raise a TypeError."): r = v + p # Point3D and Normal3D r = p + n self.assertTrue(isinstance(r, Point3D), "Vector3D addition did not return a Point3D.") self.assertEqual(r.x, -5 + 6, "Point3D + Normal3D failed [X].") self.assertEqual(r.y, -2 + 3, "Point3D + Normal3D failed [X].") self.assertEqual(r.z, 10 - 9, "Point3D + Normal3D failed [X].") with self.assertRaises( TypeError, msg="Normal3D + Point3D should have raise a TypeError."): r = n + p
def test_vector_subtract(self): """Subtract operator.""" v = Vector3D(3, -4, 5) n = Normal3D(6, 3, -9) p = Point3D(-5, -2, 10) # Vector3D and Normal3D r = v - n self.assertTrue(isinstance(r, Vector3D), "Vector3D addition did not return a Vector3D.") self.assertEqual(r.x, 3 - 6, "Vector3D - Normal3D failed [X].") self.assertEqual(r.y, -4 - 3, "Vector3D - Normal3D failed [X].") self.assertEqual(r.z, 5 + 9, "Vector3D - Normal3D failed [X].") r = n - v self.assertTrue(isinstance(r, Normal3D), "Vector3D addition did not return a Normal3D.") self.assertEqual(r.x, -3 + 6, "Normal3D - Vector3D failed [X].") self.assertEqual(r.y, 4 + 3, "Normal3D - Vector3D failed [X].") self.assertEqual(r.z, -5 - 9, "Normal3D - Vector3D failed [X].") # Point3D and Vector3D r = p - v self.assertTrue(isinstance(r, Point3D), "Vector3D addition did not return a Point3D.") self.assertEqual(r.x, -5 - 3, "Point3D - Vector3D failed [X].") self.assertEqual(r.y, -2 + 4, "Point3D - Vector3D failed [X].") self.assertEqual(r.z, 10 - 5, "Point3D - Vector3D failed [X].") with self.assertRaises( TypeError, msg="Vector3D - Point3D should have raise a TypeError."): r = v - p # Point3D and Normal3D r = p - n self.assertTrue(isinstance(r, Point3D), "Vector3D addition did not return a Point3D.") self.assertEqual(r.x, -5 - 6, "Point3D - Normal3D failed [X].") self.assertEqual(r.y, -2 - 3, "Point3D - Normal3D failed [X].") self.assertEqual(r.z, 10 + 9, "Point3D - Normal3D failed [X].") with self.assertRaises( TypeError, msg="Normal3D - Point3D should have raise a TypeError."): r = n - p
def test_not_equal(self): """Inequality operator.""" self.assertFalse( Point3D(1, 2, 3) != Point3D(1, 2, 3), "Inequality operator returned true for equal points.") self.assertTrue( Point3D(5, 2, 3) != Point3D(1, 2, 3), "Inequality operator returned false for a point with non-equal x components." ) self.assertTrue( Point3D(1, 5, 3) != Point3D(1, 2, 3), "Inequality operator returned false for a point with non-equal y components." ) self.assertTrue( Point3D(1, 2, 5) != Point3D(1, 2, 3), "Inequality operator returned false for a point with non-equal z components." )
def test_transform(self): """Testing method transform().""" m = AffineMatrix3D([[1, 2, 3, 4], [5, 6, 2, 8], [9, 10, 4, 9], [4, 14, 15, 16]]) v = Point3D(-1, 2, 6) r = v.transform(m) self.assertTrue(isinstance(r, Point3D), "Transform did not return a Point3D.") w = (4 * -1 + 14 * 2 + 15 * 6 + 16) self.assertEqual(r.x, (1 * -1 + 2 * 2 + 3 * 6 + 4) / w, "Transform failed [X].") self.assertEqual(r.y, (5 * -1 + 6 * 2 + 2 * 6 + 8) / w, "Transform failed [Y].") self.assertEqual(r.z, (9 * -1 + 10 * 2 + 4 * 6 + 9) / w, "Transform failed [Z].")
def test_affine_vector_multiply(self): """Matrix-Vector3D multiply.""" m = AffineMatrix3D([[1, 2, 3, 4], [5, 6, 2, 8], [9, 10, 4, 9], [4, 14, 15, 16]]) v = Vector3D(-1, 2, 6) n = Normal3D(-1, 2, 6) p = Point3D(-1, 2, 6) # AffineMatrix3D * Vector3D r = m * v self.assertTrue( isinstance(r, Vector3D), "AffineMatrix3D * Vector3D did not return a Vector3D.") self.assertEqual(r.x, 1 * -1 + 2 * 2 + 3 * 6, "AffineMatrix3D * Vector3D failed [X].") self.assertEqual(r.y, 5 * -1 + 6 * 2 + 2 * 6, "AffineMatrix3D * Vector3D failed [Y].") self.assertEqual(r.z, 9 * -1 + 10 * 2 + 4 * 6, "AffineMatrix3D * Vector3D failed [Z].") # Vector3D * AffineMatrix3D with self.assertRaises( TypeError, msg="Vector3D * AffineMatrix3D should have raise a TypeError." ): r = v * m # AffineMatrix3D * Normal3D r = m * n self.assertTrue( isinstance(r, Normal3D), "AffineMatrix3D * Normal3D did not return a Normal3D.") self.assertAlmostEqual(r.x, 258 / 414 * -1 + -381 / 414 * 2 + 210 / 414 * 6, places=14, msg="AffineMatrix3D * Normal3D failed [X].") self.assertAlmostEqual(r.y, -132 / 414 * -1 + 81 / 414 * 2 + -162 / 414 * 6, places=14, msg="AffineMatrix3D * Normal3D failed [Y].") self.assertAlmostEqual(r.z, 120 / 414 * -1 + -36 / 414 * 2 + 72 / 414 * 6, places=14, msg="AffineMatrix3D * Normal3D failed [Z].") # Normal3D * AffineMatrix3D with self.assertRaises( TypeError, msg="Normal3D * AffineMatrix3D should have raise a TypeError." ): r = n * m # AffineMatrix3D * Point3D r = m * p self.assertTrue(isinstance(r, Point3D), "AffineMatrix3D * Point3D did not return a Point3D.") w = (4 * -1 + 14 * 2 + 15 * 6 + 16) self.assertEqual(r.x, (1 * -1 + 2 * 2 + 3 * 6 + 4) / w, "AffineMatrix3D * Point3D failed [X].") self.assertEqual(r.y, (5 * -1 + 6 * 2 + 2 * 6 + 8) / w, "AffineMatrix3D * Point3D failed [Y].") self.assertEqual(r.z, (9 * -1 + 10 * 2 + 4 * 6 + 9) / w, "AffineMatrix3D * Point3D failed [Z].") # Point3D * AffineMatrix3D with self.assertRaises( TypeError, msg="Point3D * AffineMatrix3D should have raise a TypeError."): p = p * m
def test_initialise_invalid(self): """Initialisation with invalid types should raise a TypeError.""" with self.assertRaises(TypeError, msg="Initialised with a string."): Point3D("spoon")
for sample in samples: theta.append(degrees(atan2(sample.z, sqrt(sample.x**2 + sample.y**2)))) phi.append(degrees(atan2(sample.y, sample.x))) plt.figure() plt.plot(phi, theta, '.', markersize=1) plt.xlim(-180, 180) plt.ylim(-90, 90) plt.title(title) samples = 100000 # Target tuple: (sphere_centre, sphere_radius, target_weight) targets = [ (Point3D(0, 1, 5), 2, 1), (Point3D(-5, -3, 0), 3, 1), (Point3D(4, 6, 1.5), 1, 1), (Point3D(50, -25, 70), 1, 1), ] observation_point = Point3D(0, 0, 0) # generate samplers hemisphere = TargettedHemisphereSampler(targets) sphere = TargettedSphereSampler(targets) # sample for origin point and point at (0, 0, -10) h_samples = hemisphere(observation_point, samples=samples) s_samples = sphere(observation_point, samples=samples)
import matplotlib.pyplot as plt from raysect.core.math import TriangleSampler3D, Point3D def plot_samples(samples): plt.figure() for s in samples: plt.plot(s.x, s.y, 'k.') plt.figure() for s in samples: plt.plot(s.x, s.z, 'r.') ts1 = TriangleSampler3D(Point3D(0, 0, 0), Point3D(1, 0, 0), Point3D(0, 1, 0)) samples = ts1(1000) plot_samples(samples) ts2 = TriangleSampler3D(Point3D(-1, -1, 0), Point3D(0, 1, 0), Point3D(1, 0.1, 0)) samples = ts2(1000) plot_samples(samples) ts3 = TriangleSampler3D(Point3D(-1, -1, -0.5), Point3D(0, 1, 1), Point3D(1, 0.1, 0)) samples = ts3(1000) plot_samples(samples) plt.show()