def test_rotateX_Y_Z(self): geometry = THREE.BufferGeometry() geometry.addAttribute( "position", THREE.BufferAttribute(ctypesArray("f", [1, 2, 3, 4, 5, 6]), 3)) pos = geometry.attributes["position"].array geometry.rotateX(180 * DegToRad) # object was rotated around x so all items should be flipped but the x ones self.assertTrue( pos[0] == 1 and pos[1] == -2 and pos[2] == -3 and \ pos[3] == 4 and pos[4] == -5 and pos[5] == -6 ) # vertices were rotated around x by 180 degrees geometry.rotateY(180 * DegToRad) # vertices were rotated around y so all items should be flipped again but the y ones self.assertTrue( pos[0] == -1 and pos[1] == -2 and pos[2] == 3 and \ pos[3] == -4 and pos[4] == -5 and pos[5] == 6 ) # vertices were rotated around y by 180 degrees geometry.rotateZ(180 * DegToRad) # vertices were rotated around z so all items should be flipped again but the z ones self.assertTrue( pos[0] == 1 and pos[1] == 2 and pos[2] == 3 and \ pos[3] == 4 and pos[4] == 5 and pos[5] == 6 ) # vertices were rotated around z by 180 degrees
def getBSForVertices(self, vertices): geometry = THREE.BufferGeometry() geometry.addAttribute( "position", THREE.BufferAttribute(ctypesArray("f", vertices), 3)) geometry.computeBoundingSphere() return geometry.boundingSphere
def test_fromBufferGeometry(self): bufferGeometry = THREE.BufferGeometry() bufferGeometry.addAttribute( "position", THREE.BufferAttribute( ctypesArray("f", [1, 2, 3, 4, 5, 6, 7, 8, 9]), 3)) bufferGeometry.addAttribute( "color", THREE.BufferAttribute( ctypesArray("f", [0, 0, 0, 0.5, 0.5, 0.5, 1, 1, 1]), 3)) bufferGeometry.addAttribute( "normal", THREE.BufferAttribute( ctypesArray("f", [0, 1, 0, 1, 0, 1, 1, 1, 0]), 3)) bufferGeometry.addAttribute( "uv", THREE.BufferAttribute(ctypesArray("f", [0, 0, 0, 1, 1, 1]), 2)) bufferGeometry.addAttribute( "uv2", THREE.BufferAttribute(ctypesArray("f", [0, 0, 0, 1, 1, 1]), 2)) geometry = THREE.Geometry().fromBufferGeometry(bufferGeometry) colors = geometry.colors self.assertTrue( colors[0].r == 0 and colors[0].g == 0 and colors[0].b == 0 and \ colors[1].r == 0.5 and colors[1].g == 0.5 and colors[1].b == 0.5 and \ colors[2].r == 1 and colors[2].g == 1 and colors[2].b == 1 ) # colors were created well vertices = geometry.vertices self.assertTrue( vertices[0].x == 1 and vertices[0].y == 2 and vertices[0].z == 3 and \ vertices[1].x == 4 and vertices[1].y == 5 and vertices[1].z == 6 and \ vertices[2].x == 7 and vertices[2].y == 8 and vertices[2].z == 9 ) # vertices were created well vNormals = geometry.faces[0].vertexNormals self.assertTrue( vNormals[0].x == 0 and vNormals[0].y == 1 and vNormals[0].z == 0 and \ vNormals[1].x == 1 and vNormals[1].y == 0 and vNormals[1].z == 1 and \ vNormals[2].x == 1 and vNormals[2].y == 1 and vNormals[2].z == 0 ) # vertex normals were created well
def test_copyAt(self): attr = THREE.BufferAttribute( ctypesArray("f", [1, 2, 3, 4, 5, 6, 7, 8, 9]), 3) attr2 = THREE.BufferAttribute(ctypesArray("f", 9), 3) attr2.copyAt(1, attr, 2) attr2.copyAt(0, attr, 1) attr2.copyAt(2, attr, 0) i = attr.array i2 = attr2.array # should be [4, 5, 6, 7, 8, 9, 1, 2, 3] self.assertTrue(i2[0] == i[3] and i2[1] == i[4] and i2[2] == i[5]) # chunck copied to correct place self.assertTrue(i2[3] == i[6] and i2[4] == i[7] and i2[5] == i[8]) # chunck copied to correct place self.assertTrue(i2[6] == i[0] and i2[7] == i[1] and i2[8] == i[2]) # chunck copied to correct place
def test_clone(self): attr = THREE.BufferAttribute(ctypesArray("f", [1, 2, 3, 4, 0.12, -12]), 2) attrCopy = attr.clone() self.assertEqual(len(attr.array), len(attrCopy.array)) # attribute was cloned for i in xrange(len(attr.array)): self.assertEqual(attr.array[i], attrCopy.array[i]) # array item is equal
def test_copyColorsArray(self): attr = THREE.BufferAttribute(ctypesArray("f", 6), 3) attr.copyColorsArray([THREE.Color(0, 0.5, 1), THREE.Color(0.25, 1, 0)]) i = attr.array self.assertTrue(i[0] == 0 and i[1] == 0.5 and i[2] == 1) # first color was copied correctly self.assertTrue(i[3] == 0.25 and i[4] == 1 and i[5] == 0) # second color was copied correctly
def test_copyIndicesArray(self): attr = THREE.BufferAttribute(ctypesArray("f", 6), 3) attr.copyIndicesArray([Expando(a=1, b=2, c=3), Expando(a=4, b=5, c=6)]) i = attr.array self.assertTrue(i[0] == 1 and i[1] == 2 and i[2] == 3) # first indices were copied correctly self.assertTrue(i[3] == 4 and i[4] == 5 and i[5] == 6) # second indices were copied correctly
def test_copyVector2sArray(self): attr = THREE.BufferAttribute(ctypesArray("f", 4), 2) attr.copyVector2sArray([THREE.Vector2(1, 2), THREE.Vector2(4, 5)]) i = attr.array self.assertTrue(i[0] == 1 and i[1] == 2) # first vector was copied correctly self.assertTrue(i[2] == 4 and i[3] == 5) # second vector was copied correctly
def test_translate(self): geometry = THREE.BufferGeometry() geometry.addAttribute( "position", THREE.BufferAttribute(ctypesArray("f", [1, 2, 3, 4, 5, 6]), 3)) pos = geometry.attributes["position"].array geometry.translate(10, 20, 30) self.assertTrue( pos[0] == 11 and pos[1] == 22 and pos[2] == 33 and \ pos[3] == 14 and pos[4] == 25 and pos[5] == 36 ) # vertices were translated
def test_copy(self): geometry = THREE.BufferGeometry() geometry.addAttribute( "attrName", THREE.BufferAttribute(ctypesArray("f", [1, 2, 3, 4, 5, 6]), 3)) geometry.addAttribute( "attrName2", THREE.BufferAttribute(ctypesArray("f", [0, 1, 3, 5, 6]), 1)) copy = THREE.BufferGeometry().copy(geometry) self.assertTrue(copy != geometry and geometry.id != copy.id) # object was created for key in geometry.attributes: attribute = geometry.attributes[key] self.assertIsNotNone(attribute) # all attributes where copied for i in xrange(len(attribute.array)): self.assertEqual(attribute.array[i], copy.attributes[key]. array[i]) # values of the attribute are equal
def getNormalsForVertices(self, vertices): geometry = THREE.BufferGeometry() geometry.addAttribute( "position", THREE.BufferAttribute(ctypesArray("f", vertices), 3)) geometry.computeVertexNormals() self.assertTrue("normal" in geometry.attributes) # normal attribute was created return geometry.attributes["normal"].array
def test_scale(self): geometry = THREE.BufferGeometry() geometry.addAttribute( "position", THREE.BufferAttribute(ctypesArray("f", [-1, -1, -1, 2, 2, 2]), 3)) pos = geometry.attributes["position"].array geometry.scale(1, 2, 3) self.assertTrue( pos[0] == -1 and pos[1] == -2 and pos[2] == -3 and \ pos[3] == 2 and pos[4] == 4 and pos[5] == 6 ) # vertices were scaled
def test_copyVector3sArray(self): attr = THREE.BufferAttribute(ctypesArray("f", 6), 2) attr.copyVector3sArray( [THREE.Vector3(1, 2, 3), THREE.Vector3(10, 20, 30)]) i = attr.array self.assertTrue(i[0] == 1 and i[1] == 2 and i[2] == 3) # first vector was copied correctly self.assertTrue(i[3] == 10 and i[4] == 20 and i[5] == 30) # second vector was copied correctly
def test_merge(self): geometry1 = THREE.BufferGeometry() geometry1.addAttribute( "attrName", THREE.BufferAttribute(ctypesArray("f", [1, 2, 3, 0, 0, 0]), 3)) geometry2 = THREE.BufferGeometry() geometry2.addAttribute( "attrName", THREE.BufferAttribute(ctypesArray("f", [4, 5, 6]), 3)) attr = geometry1.attributes["attrName"].array geometry1.merge(geometry2, 1) # merged array should be 1, 2, 3, 4, 5, 6 for i in xrange(len(attr)): self.assertEqual(attr[i], i + 1) geometry1.merge(geometry2) self.assertTrue( attr[0] == 4 and attr[1] == 5 and attr[2] == 6) # copied the 3 attributes without offset
def test_copy(self): attr = THREE.BufferAttribute(ctypesArray("f", [1, 2, 3, 4, 5, 6]), 3) attr.setDynamic(True) attr.needsUpdate = True attrCopy = THREE.BufferAttribute(None, None).copy(attr) self.assertEqual(attr.count, attrCopy.count) # count is equal self.assertEqual(attr.itemSize, attrCopy.itemSize) # itemSize is equal self.assertEqual(attr.dynamic, attrCopy.dynamic) # dynamic is equal self.assertEqual(len(attr.array), len(attrCopy.array)) # array length is equal self.assertEqual(attr.version, 1) # version is not copied which is good self.assertEqual(attrCopy.version, 0) # version is not copied which is good
def test_center(self): geometry = THREE.BufferGeometry() geometry.addAttribute( "position", THREE.BufferAttribute( ctypesArray("f", [-1, -1, -1, 1, 1, 1, 4, 4, 4]), 3)) geometry.center() pos = geometry.attributes["position"].array bb = geometry.boundingBox # the boundingBox should go from (-1, -1, -1) to (4, 4, 4) so it has a size of (5, 5, 5) # after centering it the vertices should be placed between (-2.5, -2.5, -2.5) and (2.5, 2.5, 2.5) self.assertTrue( pos[0] == -2.5 and pos[1] == -2.5 and pos[2], -2.5 and \ pos[3] == -0.5 and pos[4] == -0.5 and pos[5] == -0.5 and \ pos[6] == 2.5 and pos[7] == 2.5 and pos[8] == 2.5 ) # vertices were replaced by boundingBox dimensions
def test_add_delAttribute(self): geometry = THREE.BufferGeometry() attributeName = "position" self.assertFalse(attributeName in geometry.attributes) # no attribute defined geometry.addAttribute( attributeName, THREE.BufferAttribute(ctypesArray("f", [1, 2, 3]), 1)) self.assertTrue(attributeName in geometry.attributes) # attribute is defined geometry.removeAttribute(attributeName) self.assertFalse(attributeName in geometry.attributes) # no attribute defined
def test_applyMatrix(self): geometry = THREE.BufferGeometry() geometry.addAttribute("position", THREE.BufferAttribute(ctypesArray("f", 6), 3)) matrix = THREE.Matrix4().set(1, 0, 0, 1.5, 0, 1, 0, -2, 0, 0, 1, 3, 0, 0, 0, 1) geometry.applyMatrix(matrix) position = geometry.attributes["position"].array m = matrix.elements self.assertTrue( position[0] == m[12] and position[1] == m[13] and position[2] == m[14]) # position was extracted from matrix self.assertTrue(position[3] == m[12] and position[4] == m[13] and position[5] == m[14]) # position was extracted from matrix twice self.assertEqual(geometry.attributes["position"].version, 1) # version was increased during update
def test_count(self): self.assertEqual( THREE.BufferAttribute(ctypesArray("f", [1, 2, 3, 4, 5, 6]), 3).count, 2) # count is equal to the number of chunks