def test_map(self): eq = self.almostEqual p = Point(1, 2, 3) m = Transform().translate((2, 3, 4)) eq(m.map(p), (3, 5, 7)) p = Point(1, 2, 3) m = Transform().scale((2, 2, 2)) eq(m.map(p), (2, 4, 6)) p = ((1, 2, 3), (3, 2, 1)) m = Transform().scale((2, 2, 2)) res = m.map(p) eq(( res[0][0], res[0][1], res[0][2], res[1][0], res[1][1], res[1][2], ), (2.0, 4.0, 6.0, 6.0, 4.0, 2.0)) p = (Point(1, 2, 3), Point(3, 2, 1)) m = Transform().scale((2, 2, 2)) res = m.map(p) eq(( res[0][0], res[0][1], res[0][2], res[1][0], res[1][1], res[1][2], ), (2.0, 4.0, 6.0, 6.0, 4.0, 2.0))
def test_init(self): b1 = AABBox() self.assertTrue(b1.min == Point(-.5, -.5, -.5)) self.assertTrue(b1.max == Point(.5, .5, .5)) b2 = AABBox((-.5, -.5, -.5), (.5, .5, .5)) self.almostEqual(b2.min, (-.5, -.5, -.5)) self.almostEqual(b2.max, (.5, .5, .5))
def test_transform(self): p = Plane.fromNormal((0.,0.,0.), (0.,0.,1.)) tr = Transform().translate(0.,0.,1.) self.assertTrue(p.distanceTo((0.,0.,-1.5)) == -1.5) self.assertTrue(p.origin == Point(0.,0.,0.)) p.transform(tr) self.assertTrue(p.distanceTo((0.,0.,-1.5)) == -2.5) self.assertTrue(p.origin == Point(0.,0.,1.))
def test_fromFrame(self): p1 = Plane.fromFrame((0.,0.,0.), (-1.,0.,0.), (0.,-1.,0.)) self.assertTrue(p1.origin == Point(0.,0.,0.)) self.assertTrue(p1.xaxis == Vector(-1.,0.,0.)) self.assertTrue(p1.yaxis == Vector(0.,-1.,0.)) self.assertTrue(p1.zaxis == Vector(0.,0.,1.)) self.assertTrue(p1.a == 0.) self.assertTrue(p1.b == 0.) self.assertTrue(p1.c == 1.) self.assertTrue(p1.d == 0.)
def test_hasPlane(self): eq = self.assertEqual aeq = self.almostEqual e5 = Edge().createCircle(center=(0., 0., 0.), normal=(0., 0., 1.), radius=1.) eq(e5.hasPlane(), True) pnt = Point(-1., -1., -1.) vec = Vector(-1., -1., -1.) e5.hasPlane(pnt, vec) aeq(pnt, (0., 0., 0.)) aeq(vec, (0., 0., 1.))
def test_init_and_accessors(self): eq = self.almostEqual p = Point(1., 2., 3.) eq((p.x, p.y, p.z), (1., 2., 3.)) p.x, p.y, p.z = -1., -2., -3. eq((p.x, p.y, p.z), (-1., -2., -3.)) eq(Point(1., 2.), (1., 2., 0.)) eq(Point(), (0., 0., 0)) eq(Point((1., 2., 3.)), (1., 2., 3.)) eq(Point((1., 2.)), (1., 2., 0.)) eq(Point(()), (0., 0., 0))
def test_maximumCoordinate(self): eq = self.assertEqual eq(Point(0., 4., 2.).maximumCoordinate(), 4.)
def test_closestPoint(self): p1 = Plane((0.,0.,0.), (-1.,0.,0.), (0.,-1.,0.)) self.assertTrue(p1.closestPoint((1.,2.,-1.5)) == Point(1.,2.,0.))
def test_arithmeticops(self): eq = self.almostEqual # __abs__ eq(Point(1., 2., 3.), abs(Point(-1., -2., -3.))) # __neg__ eq(-Point(1., 2., 3.), Point(-1., -2., -3.)) # __pos__ eq(+Point(1., 2., -3.), Point(1., 2., -3.)) # __add__ eq(Point(1., 2., 3.) + Point(-1., -2., -3.), Point(0., 0., 0.)) # __iadd__ p1 = Point(1., 2., 3.) p1 += Point(-1., -2., -3.) eq(p1, Point(0., 0., 0.)) # __sub__ self.assertTrue(Point(1., 2., 3.) - Point(-1., -2., -3.) == Point(2., 4., 6.)) # __isub__ p1 = Point(1., 2., 3.) p1 -= Point(1., 2., 3.) self.assertTrue(p1 == Point(0., 0., 0.)) # __mul__ eq(Point(1., 2., 3.) * 2., Point(2.0, 4.0, 6.0)) eq(2. * Point(1., 2., 3.), Point(2.0, 4.0, 6.0)) p = Point(1.,2.,3.) m = Transform().translate(3.,2.,1.) q = p * m eq(q, Point(4.,4.,4.)) # __rmul__ eq(2. * Point(1., 2., 3.), Point(2.0, 4.0, 6.0)) # __imul__ p1 = Point(1.,2.,3.) p1 *= 2. eq(p1, Point(2., 4., 6.)) # __div__ eq(Point(1., 2., 3.) / 2., Point(1./2., 2./2., 3./2.)) # __idiv__ p1 = Point(1., 2., 3.) p1 /= 2. eq(p1, Point(1./2., 2./2., 3./2.)) # __rdiv__ eq(1. / Point(1., 2., 3.), Point(1./1., 1./2., 1./3.))
def test__richcmp__(self): EPS = 2*2.2204460492503132e-16 # == self.assertTrue(not Point(1., 2., 3.) == False) self.assertTrue(Point(1., 2., 3.) == Point(1., 2., 3.)) self.assertTrue(not Point(1.1, 2.1, 3.1) == Point(1., 2., 3.)) self.assertTrue(Point(1. + EPS/2., 2. + EPS/2., 3. + EPS/2.) == Point(1., 2., 3.)) self.assertTrue(not Point(1. + 2.*EPS, 2. + 2.*EPS, 3. + 2.*EPS) == Point(1., 2., 3.)) #!= self.assertTrue(Point(1., 2., 3.) != True) self.assertTrue(Point(1., 2., 3.) != Point(3., 2., 1.)) self.assertTrue(not Point(1., 2., 3.) != Point(1., 2., 3.)) self.assertTrue(not Point(1. + EPS/2., 2. + EPS/2., 3. + EPS/2.) != Point(1., 2., 3.)) self.assertTrue(Point(1. + 2.*EPS, 2. + 2.*EPS, 3. + 2.*EPS) != Point(1., 2., 3.)) #< self.assertTrue(Point(.9, 1.9, 2.9) < Point(1., 2., 3.)) self.assertTrue(not Point(1.1, 2.1, 3.1) < Point(1., 2., 3.)) #<= self.assertTrue(Point(1., 2., 3.) <= Point(1., 2., 3.)) self.assertTrue(not Point(1.1, 2.1, 3.1) <= Point(1., 2., 3.)) self.assertTrue(Point(1. + EPS/2., 2. + EPS/2., 3. + EPS/2.) <= Point(1., 2., 3.)) self.assertTrue(not Point(1. + 2.*EPS, 2. + 2.*EPS, 3. + 2.*EPS) <= Point(1., 2., 3.)) #> self.assertTrue(Point(1., 2., 3.) > Point(.9, 1.9, 2.9)) self.assertTrue(not Point(1., 2., 3.) > Point(1.1, 2.1, 3.1)) #>= self.assertTrue(Point(1., 2., 3.) >= Point(1., 2., 3.)) self.assertTrue(not Point(1., 2., 3.) >= Point(1.1, 2.1, 3.1)) self.assertTrue(Point(1. + EPS/2., 2. + EPS/2., 3. + EPS/2.) >= Point(1., 2., 3.)) self.assertTrue(not Point(1. - 2.*EPS, 2. - 2.*EPS, 3. - 2.*EPS) >= Point(1., 2., 3.))
def test___getitem__(self): eq = self.almostEqual p = Point(1., 2., 3.) eq((p[0], p[1], p[2]), (1., 2., 3.)) p.x, p.y, p.z = -1., -2., -3. eq((p.x, p.y, p.z), (-1., -2., -3.))
def test__str__(self): assert str(Point()).startswith(Point().__class__.__name__)
def test__repr__(self): eq = self.almostEqual p = Point(eval(repr(Point(1,2,3)))) eq((p.x, p.y, p.z), (1., 2., 3.))
def test_distance(self): eq = self.assertAlmostEqual eq(distance(Point(1.,1.,1.), Point(2.,3.,4.)), sqrt(14))
def test_set(self): eq = self.almostEqual p = Point(1., 2., 3.) p.set(4., 5., 6.) eq(p, (4., 5., 6.)) p = Point(1., 2., 3.) p.set(4., 5.) eq(p, (4., 5., 3.)) p = Point(1., 2., 3.) p.set(4.) eq(p, (4., 2., 3.)) p = Point(1., 2., 3.) p.set() eq(p, (1., 2., 3.)) p = Point(1., 2., 3.) p.set((4., 5., 6.)) eq(p, (4., 5., 6.)) p = Point(1., 2., 3.) p.set((4., 5.)) eq(p, (4., 5., 3.)) p = Point(1., 2., 3.) p.set((4.,)) eq(p, (4., 2., 3.)) p = Point(1., 2., 3.) p.set(()) eq(p, (1., 2., 3.))
def test_maximumCoordinateIndex(self): self.assertTrue(Point(0., 4., 2.).maximumCoordinateIndex() == 1)
def test_isZero(self): self.assertTrue(Point(0., 0., 0.).isZero()) self.assertTrue(not Point(1., 2., 3.).isZero())