def test_superpose(self): # create a few test sets with random data points, including degenerate # situations. (e.g. one point, two points, linear points) references = [ # a list of 2-tuples: (points, degenerate) (numpy.random.normal(0, 5, (n, 3)), False) for n in xrange(4, 40) ] + [ #(numpy.array([[0,0,1]], float), True), #(numpy.array([[0,0,0],[0,0,1]], float), True), #(numpy.array([[0,0,0],[0,0,1],[0,0,2]], float), True), #(numpy.array([[0,0,0],[0,0,1],[0,0,2],[0,0,4]], float), True), #(numpy.random.normal(0, 5, (3, 3)), True) ] # Do a random transformation on the points randomized = [] for points, degenerate in references: #points[:] -= points.mean(axis=0) axis = random_unit(3) angle = numpy.random.uniform(0, numpy.pi * 2) transformation = Complete() transformation.set_rotation_properties(angle, axis, False) transformation.t[:] = numpy.random.normal(0, 5, 3) randomized.append( (numpy.array([transformation.vector_apply(p) for p in points]), transformation)) for (ref_points, degenerate), (tr_points, transf) in zip(references, randomized): check_transf = superpose(ref_points, tr_points) # check whether the rotation matrix is orthogonal self.assertArraysAlmostEqual( numpy.dot(check_transf.r, check_transf.r.transpose()), numpy.identity(3, float), 1e-5) # first check whether check_transf brings the tr_points back to the ref_points check_points = numpy.dot( tr_points, check_transf.r.transpose()) + check_transf.t self.assertArraysAlmostEqual(ref_points, check_points, 1e-5, doabs=True) if not degenerate: # if the set of points is not degenerate, check whether transf and check_transf # are each other inverses tmp = Complete() tmp.apply_before(transf) tmp.apply_before(check_transf) self.assertArraysAlmostEqual( numpy.dot(transf.r, check_transf.r), numpy.identity(3, float), 1e-5) self.assertArrayAlmostZero(tmp.t, 1e-5) # Add some distortion to the transformed points randomized = [] for tr_points, transf in randomized: tr_points[:] += numpy.random.normal(0, 1.0, len(tr_points)) # Do a blind test for (ref_points, degenerate), (tr_points, transf) in zip(references, randomized): superpose(ref_points, tr_points)
def setUp(self): self.test_transformations = [] for i in xrange(20): test_transformation = Complete() test_transformation.set_rotation_properties(random.random()*math.pi*2, numpy.random.uniform(-3, 3, 3), random.sample([True, False], 1)[0]) test_transformation.t = numpy.random.uniform(-3, 3, 3) self.test_transformations.append(test_transformation)
def setUp(self): self.test_transformations = [] for i in xrange(20): test_transformation = Complete() test_transformation.set_rotation_properties( random.random() * math.pi * 2, numpy.random.uniform(-3, 3, 3), random.sample([True, False], 1)[0]) test_transformation.t = numpy.random.uniform(-3, 3, 3) self.test_transformations.append(test_transformation)
def test_superpose(self): # create a few test sets with random data points, including degenerate # situations. (e.g. one point, two points, linear points) references = [ # a list of 2-tuples: (points, degenerate) (numpy.random.normal(0, 5, (n, 3)), False) for n in xrange(4, 40) ] + [ #(numpy.array([[0,0,1]], float), True), #(numpy.array([[0,0,0],[0,0,1]], float), True), #(numpy.array([[0,0,0],[0,0,1],[0,0,2]], float), True), #(numpy.array([[0,0,0],[0,0,1],[0,0,2],[0,0,4]], float), True), #(numpy.random.normal(0, 5, (3, 3)), True) ] # Do a random transformation on the points randomized = [] for points, degenerate in references: #points[:] -= points.mean(axis=0) axis = random_unit(3) angle = numpy.random.uniform(0, numpy.pi*2) transformation = Complete() transformation.set_rotation_properties(angle, axis, False) transformation.t[:] = numpy.random.normal(0, 5, 3) randomized.append(( numpy.array([transformation.vector_apply(p) for p in points]), transformation )) for (ref_points, degenerate), (tr_points, transf) in zip(references, randomized): check_transf = superpose(ref_points, tr_points) # check whether the rotation matrix is orthogonal self.assertArraysAlmostEqual(numpy.dot(check_transf.r, check_transf.r.transpose()), numpy.identity(3, float), 1e-5) # first check whether check_transf brings the tr_points back to the ref_points check_points = numpy.dot(tr_points, check_transf.r.transpose()) + check_transf.t self.assertArraysAlmostEqual(ref_points, check_points, 1e-5, doabs=True) if not degenerate: # if the set of points is not degenerate, check whether transf and check_transf # are each other inverses tmp = Complete() tmp.apply_before(transf) tmp.apply_before(check_transf) self.assertArraysAlmostEqual(numpy.dot(transf.r, check_transf.r), numpy.identity(3, float), 1e-5) self.assertArrayAlmostZero(tmp.t, 1e-5) # Add some distortion to the transformed points randomized = [] for tr_points, transf in randomized: tr_points[:] += numpy.random.normal(0, 1.0, len(tr_points)) # Do a blind test for (ref_points, degenerate), (tr_points, transf) in zip(references, randomized): superpose(ref_points, tr_points)
def do_test(self, coordinates, masses, expected_is): reference = MolecularDescriptorTV1(coordinates, masses) self.assert_(reference.inversion_symmetric == expected_is) for counter in xrange(3): transformation = Complete() transformation.set_rotation_properties( random.uniform(0, 2 * math.pi), numpy.random.uniform(-1, 1, (3, )), False, ) transformation.t = numpy.random.uniform(-5, 5, (3, )) new_coordinates = numpy.array([ transformation.vector_apply(coordinate) for coordinate in coordinates ], float) new_descriptor = MolecularDescriptorTV1(new_coordinates, masses) self.assert_(reference.compare_structure(new_descriptor)) self.assert_(not reference.compare_global_rotation(new_descriptor)) self.assert_( not reference.compare_global_translation(new_descriptor))
def do_test(self, coordinates, masses, expected_is): reference = MolecularDescriptorTV1(coordinates, masses) self.assert_(reference.inversion_symmetric == expected_is) for counter in xrange(3): transformation = Complete() transformation.set_rotation_properties( random.uniform(0, 2*math.pi), numpy.random.uniform(-1, 1, (3,)), False, ) transformation.t = numpy.random.uniform(-5, 5, (3,)) new_coordinates = numpy.array([ transformation.vector_apply(coordinate) for coordinate in coordinates ], float) new_descriptor = MolecularDescriptorTV1(new_coordinates, masses) self.assert_(reference.compare_structure(new_descriptor)) self.assert_(not reference.compare_global_rotation(new_descriptor)) self.assert_(not reference.compare_global_translation(new_descriptor))
class Vector(GLReferentBase): # # State # def initnonstate(self): GLReferentBase.initnonstate(self) self.orientation = Complete() self.set_children([ SpatialReference(prefix="Begin"), SpatialReference(prefix="End") ]) # # Dialog fields (see action EditProperties) # dialog_fields = set([ DialogFieldInfo("Basic", (0, 2), fields.read.VectorLength( label_text="Vector length" )), ]) # # Draw # def draw(self): self.calc_vector_dimensions() context.application.vis_backend.transform(self.orientation) # # Revalidation # def revalidate_total_list(self): if self.gl_active: vb = context.application.vis_backend vb.begin_list(self.total_list) if self.visible: vb.push_name(self.draw_list) vb.push_matrix() self.draw_selection() vb.call_list(self.draw_list) vb.pop_matrix() vb.pop_name() vb.end_list() self.total_list_valid = True def revalidate_draw_list(self): if self.gl_active: GLReferentBase.revalidate_draw_list(self) def revalidate_boundingbox_list(self): if self.gl_active: vb = context.application.vis_backend #print "Compiling selection list (" + str(self.boundingbox_list) + "): " + str(self.name) vb.begin_list(self.boundingbox_list) vb.push_matrix() vb.transform(self.orientation) self.revalidate_bounding_box() self.bounding_box.draw() vb.pop_matrix() vb.end_list() self.boundingbox_list_valid = True # # Frame # def get_bounding_box_in_parent_frame(self): return self.bounding_box.transformed(self.orientation) # # Vector # def shortest_vector_relative_to(self, other): b = self.children[0].translation_relative_to(other) e = self.children[1].translation_relative_to(other) if (b is None) or (e is None): return None else: return self.parent.shortest_vector(e - b) def calc_vector_dimensions(self): relative_translation = self.shortest_vector_relative_to(self.parent) if relative_translation is None: self.length = 0 else: self.length = math.sqrt(numpy.dot(relative_translation, relative_translation)) if self.length > 0: self.orientation.t = self.children[0].translation_relative_to(self.parent) #axis = numpy.cross(relative_translation, numpy.array([1.0, 0.0, 0.0])) c = relative_translation[2] / self.length if c >= 1.0: self.orientation.set_rotation_properties(0, numpy.array([1.0, 0.0, 0.0]), False) elif c <= -1.0: self.orientation.set_rotation_properties(math.pi, numpy.array([1.0, 0.0, 0.0]), False) else: x, y = relative_translation[0], relative_translation[1] if abs(x) < abs(y): signy = {True: 1, False: -1}[y >= 0] a = -signy b = signy * x / y else: signx = {True: 1, False: -1}[x >= 0] a = -signx * y / x b = signx self.orientation.set_rotation_properties(math.acos(c), numpy.array([a, b, 0.0]), False) def define_target(self, reference, new_target): GLReferentBase.define_target(self, reference, new_target) self.invalidate_boundingbox_list() self.invalidate_draw_list() def target_moved(self, reference, target): GLReferentBase.target_moved(self, reference, target) self.invalidate_boundingbox_list() self.invalidate_draw_list()