Example #1
0
    def replace(self, gl_object):
        if not gl_object.get_fixed():
            state = gl_object.__getstate__()
            state.pop("name", None)
            state.pop("transformation", None)
            new = self.get_new(gl_object.transformation.t)

            if(self.current_object == "Fragment"): #fragments are inserted at frames - have no refs
                target_object = new.children[1]
            else:
                target_object = new
            for reference in gl_object.references[::-1]:
                if not reference.check_target(target_object):
                    return
            parent = gl_object.parent
            import copy
            primitive.Add(new, parent)
            if(self.current_object == "Fragment"):
                # rotation
                Bond = context.application.plugins.get_node("Bond")
                if len(gl_object.references) == 1 and isinstance(gl_object.references[0].parent, Bond):
                    bond1 = gl_object.references[0].parent
                    direction1 = bond1.shortest_vector_relative_to(parent)
                    if bond1.children[0].target != gl_object:
                        direction1 *= -1
                    bond2 = new.children[0].references[0].parent
                    direction2 = bond2.shortest_vector_relative_to(parent)
                    if bond2.children[0].target != target_object:
                        direction2 *= -1
                    axis = numpy.cross(direction2, direction1)
                    if numpy.linalg.norm(axis) < 1e-8:
                        axis = random_orthonormal(direction1)
                    angle = compute_angle(direction1, direction2)
                    rotation = Rotation()
                    rotation.set_rotation_properties(angle,axis,False)
                    primitive.Transform(new, rotation)
                else:
                    bond1 = None
                # tranlsation
                translation = Translation()
                pos_old = new.children[1].get_frame_relative_to(parent).t
                pos_new = gl_object.transformation.t
                translation.t = pos_new - pos_old
                primitive.Transform(new, translation)
                if bond1 != None:
                    # bond length
                    old_length = numpy.linalg.norm(direction1)
                    new_length = bonds.get_length(new.children[1].number, bond1.get_neighbor(gl_object).number)
                    translation = Translation()
                    translation.t = -direction1/old_length*(new_length-old_length)
                    primitive.Transform(new, translation)

            for reference in gl_object.references[::-1]:
                reference.set_target(target_object)
            primitive.Delete(gl_object)
            if(self.current_object == "Fragment"):
                primitive.Delete(new.children[0])
                # get rid of frame
                UnframeAbsolute = context.application.plugins.get_action("UnframeAbsolute")
                UnframeAbsolute([new])
Example #2
0
    def default_parameters(cls):
        rotation2 = Rotation()
        rotation2.set_rotation_properties(0.0, [1, 0, 0], False)

        result = Parameters()
        result.connect_description1 = (Expression("True"), Expression("node.get_radius()"))
        result.repulse_description1 = (Expression("True"), Expression("node.get_radius()"))
        result.connect_description2 = (Expression("True"), Expression("node.get_radius()"))
        result.repulse_description2 = (Expression("True"), Expression("node.get_radius()"))
        result.action_radius = 7*angstrom
        result.distance_tolerance = 0.1*angstrom
        result.hit_tolerance = 0.1*angstrom
        result.allow_inversions = True
        result.minimum_triangle_size = 0.1*angstrom
        result.rotation_tolerance = 0.05
        result.rotation2 = Undefined(rotation2)
        return result
Example #3
0
    def do_rotation(self, rotation_angle, rotation_axis):
        rotation_axis = numpy.dot(self.eye_rotation, rotation_axis)
        if self.rotation_axis is not None:
            if numpy.dot(self.rotation_axis, rotation_axis) > 0:
                rotation_axis = self.rotation_axis
            else:
                rotation_axis = -self.rotation_axis
        rotation = Rotation()
        rotation.set_rotation_properties(rotation_angle, rotation_axis, False)
        transformation = self.victim.transformation

        if isinstance(self.victim.transformation, Translation):
            transformation.t -= self.rotation_center
            transformation.t = numpy.dot(numpy.transpose(rotation.r), transformation.t)
            transformation.t += self.rotation_center
        if isinstance(self.victim.transformation, Rotation):
            transformation.r = numpy.dot(numpy.transpose(rotation.r), transformation.r)
        self.victim.revalidate_transformation_list()
        context.application.main.drawing_area.queue_draw()
        #self.victim.invalidate_transformation_list()
        self.changed = True
Example #4
0
        def add_hydrogens(atom):
            existing_bonds = list(atom.yield_bonds())
            num_bonds = len(existing_bonds)
            bond_length = bonds.get_length(atom.number, 1, BOND_SINGLE)

            if num_bonds == 0:
                H = Atom(name="auto H", number=1)
                H.transformation.t = atom.transformation.t + numpy.array([0,bond_length,0])
                primitive.Add(H, atom.parent)
                bond = Bond(name="aut H bond", targets=[atom, H])
                primitive.Add(bond, atom.parent)
                existing_bonds.append(bond)
                num_bonds = 1

            used_valence = 0
            oposite_direction = numpy.zeros(3, float)
            for bond in existing_bonds:
                shortest_vector = bond.shortest_vector_relative_to(atom.parent)
                if bond.children[1].target == atom:
                    shortest_vector *= -1
                oposite_direction -= shortest_vector

                if bond.bond_type == BOND_SINGLE:
                    used_valence += 1
                elif bond.bond_type == BOND_DOUBLE:
                    used_valence += 2
                elif bond.bond_type == BOND_TRIPLE:
                    used_valence += 3

            oposite_direction /= numpy.linalg.norm(oposite_direction)

            num_hydrogens = valence_el(atom.number) - 2*lone_pairs(atom.number) - used_valence
            if num_hydrogens <= 0:
                return

            hybride_count = num_hydrogens + lone_pairs(atom.number) + num_bonds - (used_valence - num_bonds)
            num_sites = num_hydrogens + lone_pairs(atom.number)
            rotation = Rotation()
            rotation.set_rotation_properties(2*math.pi / float(num_sites), oposite_direction, False)
            opening_key = (hybride_count, num_sites)
            opening_angle = self.opening_angles.get(opening_key)
            if opening_angle is None:
                return

            if num_bonds == 1:
                first_bond = existing_bonds[0]
                other_atom = first_bond.children[0].target
                if other_atom == atom:
                    other_atom = first_bond.children[1].target
                other_bonds = [bond for bond in other_atom.yield_bonds() if bond != first_bond]
                if len(other_bonds) > 0:
                    normal = other_bonds[0].shortest_vector_relative_to(atom.parent)
                    normal -= numpy.dot(normal, oposite_direction) * oposite_direction
                    normal /= numpy.linalg.norm(normal)
                    if other_bonds[0].children[0].target == other_atom:
                        normal *= -1
                else:
                    normal = random_orthonormal(oposite_direction)
            elif num_bonds == 2:
                normal = numpy.cross(oposite_direction, existing_bonds[0].shortest_vector_relative_to(atom.parent))
                normal /= numpy.linalg.norm(normal)
            elif num_bonds == 3:
                normal = random_orthonormal(oposite_direction)
            else:
                return

            h_pos = bond_length*(oposite_direction*math.cos(opening_angle) + normal*math.sin(opening_angle))

            for i in range(num_hydrogens):
                H = Atom(name="auto H", number=1)
                H.transformation.t = atom.transformation.t + h_pos
                primitive.Add(H, atom.parent)
                bond = Bond(name="aut H bond", targets=[atom, H])
                primitive.Add(bond, atom.parent)
                h_pos = rotation.vector_apply(h_pos)
Example #5
0
 def do_rotation(self, drawing_area, rotation_angle, rotation_axis):
     camera = context.application.camera
     rotation = Rotation()
     rotation.set_rotation_properties(rotation_angle, rotation_axis, False)
     camera.rotation.apply_before(rotation)
     drawing_area.queue_draw()