def test_area(self): A = Point3D(0, 0, 2) B = Point3D(0, 1, 2) C = Point3D(1, 0, 2) triangle = Triangle3D(A, B, C) self.assertEqual(triangle.area, 0.5)
def test_vector_from_point_sub_point(self): A = Point3D(1, 2, 3) B = Point3D(6, 5, 4) V = B - A self.assertEqual(V.x, 5) self.assertEqual(V.y, 3) self.assertEqual(V.z, 1)
def test_point_projection_on_horizontal_plane(self): """ M(0.5,0.5,1.) + | ^ z | x B(1,0,0) | A(0,0,0) < +------|--------------+ / | / / + / / P(0.5,0.5,0) / +---------------------+ C(0,1,0) y """ A = Point3D(0, 0, 0) B = Point3D(1, 0, 0) C = Point3D(0, 1, 0) P = Point3D(0.5, 0.5, 0) M = Point3D(0.5, 0.5, 1) plane = Plane(point=P, normal=M-P) projection = plane.project_point(M) self.assertAlmostEqual(projection.x, P.x) self.assertAlmostEqual(projection.y, P.y) self.assertAlmostEqual(projection.z, P.z)
def test_point_projection_on_vertical_plane(self): """ ^ z + C(0,0,1) / | / | / | M(0.5,0.5,0.5) x + P | x---------------<----|---+ + A(0,0,0) | / | / | / + B(0,1,0) y """ A = Point3D(0,0,0) B = Point3D(0,1,0) C = Point3D(0,0,1) P = Point3D(0. , 0.5, 0.5) M = Point3D(0.5, 0.5, 0.5) plane = Plane(point=P, normal=M-P) projection = plane.project_point(M) self.assertAlmostEqual(projection.x, P.x) self.assertAlmostEqual(projection.y, P.y) self.assertAlmostEqual(projection.z, P.z)
def test_create_segment(self): A = Point3D(1,2,3) B = Point3D(6,5,3) segment = Segment3D(A,B) self.assertEqual(segment.B.y, 5) segment.B.y = 3 self.assertEqual(segment.B.y, 3) segment.B = Point3D(-1, -2, -3) self.assertEqual(segment.B.y, -2)
def test_to_cylindrical(self): A = Point3D(2, 0, 0.5) P = A.to_cylindrical() self.assertAlmostEqual(P.r, 2) self.assertAlmostEqual(P.theta, 0) self.assertEqual(P.z, 0.5) A = Point3D(0, 2, 0.5) P = A.to_cylindrical() self.assertAlmostEqual(P.r, 2.) self.assertAlmostEqual(P.theta, pi / 2) self.assertEqual(P.z, 0.5)
def test_triangle_and_segment_are_parallel(self): A = Point3D(0, 0, 0) B = Point3D(1, 0, 0) C = Point3D(0, 1, 0) triangle = Triangle3D(A, B, C) S = Point3D(0., 0.5, 1) T = Point3D(0.5, 0., 1) segment = Segment3D(S, T) with self.assertRaisesRegex(ValueError, 'There is no intersection'): intersec3d_triangle_segment(triangle, segment)
def test_no_intersection(self): A = Point3D(0, 0, 0) B = Point3D(1, 0, 0) C = Point3D(0, 1, 0) triangle = Triangle3D(A, B, C) S = Point3D(0.25, 0.25, -1) T = Point3D(0.25, 0.25, -0.5) segment = Segment3D(S, T) with self.assertRaisesRegex(ValueError, 'There is no intersection'): intersec3d_triangle_segment(triangle, segment)
def test_triangle_and_segment_are_in_same_plane(self): A = Point3D(0, 0, 0) B = Point3D(1, 0, 0) C = Point3D(0, 1, 0) triangle = Triangle3D(A, B, C) S = Point3D(0., 0.5, 0) T = Point3D(0.5, 0., 0) segment = Segment3D(S, T) msg = 'Triangle and segment are in the same plane' with self.assertRaisesRegex(ValueError, msg): intersec3d_triangle_segment(triangle, segment)
def test_triangle_is_a_point(self): A = Point3D(0, 0, 0) B = Point3D(0, 0, 0) C = Point3D(0, 0, 0) triangle = Triangle3D(A, B, C) S = Point3D(0.25, 0.25, -1) T = Point3D(0.25, 0.25, 1) segment = Segment3D(S, T) msg = 'Triangle is degenerated \(a segment or point\)' with self.assertRaisesRegex(ValueError, msg): intersec3d_triangle_segment(triangle, segment)
def test_normal(self): A = Point3D(3, 0, 0) B = Point3D(0, 6, 0) C = Point3D(0, 0, 9) triangle = Triangle3D(A, B, C) E = triangle.center self.assertAlmostEqual(E.x, 1.) self.assertAlmostEqual(E.y, 2.) self.assertAlmostEqual(E.z, 3.)
def test_symetric_point(self): A = Point3D(0, 0, 0) B = Point3D(0, 1, 0) C = Point3D(1, 0, 0) triangle = Triangle3D(A, B, C) P = Point3D(1, 2, -3) S = triangle.symetric_point(P) self.assertEqual(S.x, 1) self.assertEqual(S.y, 2) self.assertEqual(S.z, 3)
def test_create_triangle(self): A = Point3D(0, 0, 0) B = Point3D(0, 1, 0) C = Point3D(1, 0, 0) triangle = Triangle3D(A, B, C) self.assertEqual(triangle.B.y, 1) # Change coordiante. triangle.B.y = 3 self.assertEqual(triangle.B.y, 3) # Change point. triangle.B = Point3D(-1, -2, -3) self.assertEqual(triangle.B.y, -2)
def test_creation(self): A = Point3D(0, 0, 0) B = Point3D(1, 0, 0) C = Point3D(0, 1, 0) P = Point3D(0.5, 0.5, 0) M = Point3D(0.5, 0.5, 1) normal = M - P plane = Plane(point=P, normal=normal) self.assertEqual(plane.point.x, 0.5) self.assertEqual(plane.normal.z, 1)
def test_intersection_in_triangle(self): """ C T y ^ | - / | z | I - |/ A--/--------B +-----> x S """ A = Point3D(0, 0, 0) B = Point3D(1, 0, 0) C = Point3D(0, 1, 0) triangle = Triangle3D(A, B, C) S = Point3D(0.25, 0.25, -1) T = Point3D(0.25, 0.25, 1) segment = Segment3D(S, T) I = intersec3d_triangle_segment(triangle, segment) self.assertEqual(I.x, 0.25) self.assertEqual(I.y, 0.25) self.assertEqual(I.z, 0.)
def test_property_x(self): A = Point3D(1, 2, 3) self.assertEqual(A.x, 1) A.x = 10 self.assertEqual(A.x, 10)
def test_str(self): A = Point3D(3, 2, 1) expected_string = "Point3D(3.0, 2.0, 1.0)" string = str(A) self.assertEqual(string, expected_string)
def test_distance(self): A = Point3D(3, 2, 1) B = Point3D(4, 3, 2) dist = A.distance(B) expected_dist = sqrt(3.) self.assertAlmostEqual(dist, expected_dist)
def test_index(self): A = Point3D(1, 2, 3) self.assertEqual(A.index, 0) B = Point3D(1, 2, 3, index=8) self.assertEqual(B.index, 8)
def setUp(self): self.A = Point3D(1, 1, 2) self.B = Point3D(6, 0, 1) self.C = Point3D(7, 3, 3) self.T = Triangle3D(self.A, self.B, self.C)