Exemplo n.º 1
0
 public void SetAtomicRadius()
 {
     try
     {
         float radius = AtomicRadii.GetCovalentRadius(atom_.element_);
         SetRadius(radius);
     }
     catch
     {
         return;
     }
 }
Exemplo n.º 2
0
    /* Gradually move the model towards or away from the user, given a selected object, atom or bond */
    private void MoveTowardsSelectedObject(float speed)
    {
        if (selection_plane_previous_ == null)
        {
            return;
        }
        /* Calculate desired position of the selected object, just in front of the camera */
        SelectionPlaneSpheres   plane1 = selection_plane_previous_.GetComponent <SelectionPlaneSpheres>();
        SelectionPlaneCylinders plane2 = selection_plane_previous_.GetComponent <SelectionPlaneCylinders>();

        /* Calculate the desired distance that we want the selected object to be in front of the user */
        float   distance = 0;
        Vector3 target;

        if (plane1 != null)
        {
            target = plane1.center_sphere_.transform.position;
            if (GetVisualizationMethod() == VisualizationMethod.BALL_AND_STICK)
            {
                distance = 6 * AtomicRadii.ball_and_stick_radius;
            }
            else
            {
                distance = 4 * AtomicRadii.GetCovalentRadius(selected_atom_.atom_.element_);
            }
        }
        else if (plane2 != null)
        {
            target   = plane2.center_cylinder_.transform.position + plane2.center_cylinder_.transform.up * plane2.center_cylinder_.height_ / 2;
            distance = plane2.center_cylinder_.height_;
        }
        else
        {
            return;
        }
        Vector3 movement_direction = Vector3.Normalize(target - Camera.main.transform.position);

        Vector3 desired_position = target - movement_direction * distance;

        /* Calculate the movement speed, scale it based on the distance to the object */
        speed = speed * Vector3.Distance(desired_position, Camera.main.transform.position);

        /* Change position */
        transform.position = transform.position - speed * Time.deltaTime * movement_direction;
    }
Exemplo n.º 3
0
    void Start()
    {
        List <Atom>        atoms;
        List <List <int> > connections;
        string             model_name;

        try {
            model_name = ParseInputModelFile();
            PDBParser.ParseAtomsAndConnections(@"Assets/MModels/" + model_name, out atoms, out connections);
        }
        catch (System.IO.IOException) {
            print("Parsing input error");
            return;
        }


        /*  Spawn the objects */
        foreach (Atom atom in atoms)
        {
            /* Units in Nano meters */
            Vector3 atom_position = new Vector3(atom.x_, atom.y_, atom.z_);
            atoms_bounding_box_.AddPoint(atom_position);

            /* Instantiate the atom */
            GameObject temp = Instantiate(prefab_atom, atom_position, Quaternion.identity);
            temp.transform.parent = transform;
            temp.isStatic         = this.gameObject.isStatic;

            /* Find ISphere component, and set the atom information */
            ISphere isphere = temp.GetComponent <ISphere>();
            isphere.atom_ = atom;
            ispheres_.Add(isphere);

            /* Insert to the dictionaries used */
            InsertToAtomsDictionary(isphere);
            InsertToResiudesDictionary(isphere);
            InsertToChainsDictionary(isphere);
        }

        /* Parse connections, currently the application does not do something with these connections */
        foreach (List <int> c in connections)
        {
            int atom_id = c[0];
            for (int i = 1; i < c.Count; i++)
            {
                ISphere connection_isphere = ispheres_[c[i]];
                ispheres_[atom_id].connections_.Add(connection_isphere);
            }
        }

        /* Spawn bonds */
        Transform bonds_transform = transform.GetChild(0);
        int       bonds           = 0;

        /* For all resisudes */
        foreach (KeyValuePair <int, List <ISphere> > value in residue_dictionary)
        {
            /* Get combinations of two atoms */
            List <ISphere> resiude_atoms = value.Value;
            for (int ia = 0; ia < resiude_atoms.Count; ia++)
            {
                ISphere a                 = resiude_atoms[ia];
                Vector3 a_position        = a.transform.position;
                float   a_covalent_radius = AtomicRadii.GetCovalentRadius(a.atom_.element_);
                for (int ib = 0; ib < resiude_atoms.Count; ib++)
                {
                    if (!(ia > ib))
                    {
                        continue;
                    }
                    ISphere b = resiude_atoms[ib];

                    Vector3 b_position        = b.transform.position;
                    float   b_covalent_radius = AtomicRadii.radii_covalent[b.atom_.element_];

                    /* If their distance is smaller then the sume of radius + plus a bias, then spawn a bond */
                    float distance = Vector3.Distance(a_position, b_position);
                    if (distance <= a_covalent_radius + b_covalent_radius + 0.015)
                    {
                        bonds++;
                        GameObject temp = Instantiate(prefab_bond, a_position, Quaternion.identity);
                        temp.transform.parent = bonds_transform;
                        temp.isStatic         = this.gameObject.isStatic;

                        /* Rotate it accordingly */
                        Vector3    direction  = b_position - a_position;
                        Quaternion toRotation = Quaternion.FromToRotation(new Vector3(0, 1, 0), direction);
                        temp.transform.rotation = toRotation;

                        /* Set size and radius */
                        ICylinder icylinder = temp.GetComponent <ICylinder>();
                        icylinder.radius_ = AtomicRadii.ball_and_stick_bond_radius;
                        icylinder.height_ = distance;
                    }
                }
            }
        }
        Debug.Log("Spawned: " + bonds + " bonds");

        /* Position the model and the camera in the world */
        SetCameraAndPanelBoxPosition(atoms_bounding_box_);

        bonds_selected_[0] = null;
        bonds_selected_[1] = null;

        /* Set some default info on the world panel */
        SELECTION_MODE_SPHERE_RADIUS = AtomicRadii.ball_and_stick_radius * 5.0f;
        transform.GetChild(1).GetComponent <ModePanel>().SetRadius(SELECTION_MODE_SPHERE_RADIUS);

        info_ui_ = Camera.main.transform.Find("AtomInfoBox").GetComponent <AtomInfoBox>();
    }
Exemplo n.º 4
0
    /* FPS counter */
    //void OnGUI() {
    //    GUI.Label(new Rect(0, 0, 100, 100), (1.0f / Time.smoothDeltaTime).ToString());
    //}

    void Update()
    {
        /* If the bring forward the panel button is hit, the ray cast only in UI layer */
        if (Input.GetKey(KeyCode.Space))
        {
            bool ui_hit = RayCastUIlayer();
            if (ui_hit)
            {
                return;
            }
        }

        RaycastHit hit;

        /*
         * Perform ray casting towards the camera direction, move the ray origin slightly forward to avoid intersections with spheres that
         * we are currently inside
         */
        bool ray_cast_hit = Physics.Raycast(Camera.main.transform.position + Camera.main.transform.forward * AtomicRadii.ball_and_stick_radius, Camera.main.transform.forward, out hit, 100.0f);

        /* Process model movement input */
        if (Input.GetKey(KeyCode.Keypad1))
        {
            MoveTowardsSelectedObject(speed_object_towards_move);
        }

        if (Input.GetKey(KeyCode.Keypad7))
        {
            MoveTowardsSelectedObject(-speed_object_towards_move);
        }

        if (Input.GetKey(KeyCode.Keypad4))
        {
            transform.position = transform.position - Camera.main.transform.right * speed_object_vertical_move;
        }

        if (Input.GetKey(KeyCode.Keypad6))
        {
            transform.position = transform.position + Camera.main.transform.right * speed_object_vertical_move;
        }

        if (Input.GetKey(KeyCode.Keypad8))
        {
            transform.position = transform.position + Camera.main.transform.up * speed_object_vertical_move;
        }

        if (Input.GetKey(KeyCode.Keypad5))
        {
            transform.position = transform.position - Camera.main.transform.up * speed_object_vertical_move;
        }

        /* Check ray cast against UI */
        if (ray_cast_hit)
        {
            Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * hit.distance, Color.white);

            ButtonEvent button = hit.transform.GetComponent <ButtonEvent>();
            if (button != null)
            {
                ProcessRayCastUIHit(hit);
            }
        }

        /* Process state machine */
        if (state == STATE.EXPLORING_ATOMS)
        {
            /* If there is a selected atom, and the discard button is pressed, then destory selection */
            if (selected_atom_ != null)
            {
                if (Input.GetKeyDown(KeyCode.Escape) == true)
                {
                    ClearHighlighted();
                    selected_atom_ = null;
                    Destroy(selection_plane_previous_);
                    return;
                }

                return;
            }

            /* Else process ray cast hit */
            if (ray_cast_hit)
            {
                ProcessRayCastHit(hit);
            }
            else
            {
                /* If ray cast failed, clear highlighting */
                ClearHighlighted();
                if (exploring_method_ != ExploringMethod.CHAINS)
                {
                    ClearColored();
                }

                //Debug.DrawRay(Camera.main.transform.position, Camera.main.transform.forward * 1000, Color.white);
            }
        }
        else if (state == STATE.ATOM_DISTANCES)
        {
            /* If discard button, then reset state */
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                ResetState(true);
                return;
            }

            /* if we have selected an atom, process distances */
            if (selected_atom_ != null)
            {
                SelectionPlaneSpheres plane = selection_plane_previous_.GetComponent <SelectionPlaneSpheres>();

                /*
                 * Get the current selected atom, and if it's different than the previous, and the marked button is pressed
                 * mark atom, and calculate distance
                 */
                int     current_index    = atom_selected_id_ % 4;
                ISphere previously_added = ((current_index == 0) ? atoms_selected_[3] : atoms_selected_[current_index - 1]);
                if (Input.GetKeyDown(KeyCode.E) && previously_added != plane.center_sphere_)
                {
                    atoms_selected_[current_index] = plane.center_sphere_;

                    atom_selected_id_++;

                    /* If marked atom object is null, spawn it */
                    if (marked_atom_object_ == null)
                    {
                        marked_atom_object_ = Instantiate(prefab_marked_atom_, plane.center_sphere_.transform.position, Quaternion.identity);
                        marked_atom_object_.transform.parent = this.transform;
                    }
                    /* Calculate radius of the atom based on the viusalization method, and move marked atom object */
                    float selected_atom_radius;
                    if (GetVisualizationMethod() == VisualizationMethod.BALL_AND_STICK)
                    {
                        selected_atom_radius = AtomicRadii.ball_and_stick_radius;
                    }
                    else
                    {
                        selected_atom_radius = AtomicRadii.GetCovalentRadius(plane.center_sphere_.atom_.element_);
                    }
                    marked_atom_object_.transform.position = plane.center_sphere_.transform.position + 1.4f * Vector3.up * selected_atom_radius;
                    marked_sphere_ = plane.center_sphere_.gameObject;

                    /* If the previously marked atom is not null, then calculate distance */
                    if (previously_added != null)
                    {
                        Vector3 middle = (previously_added.transform.position + plane.center_sphere_.transform.position) / 2;

                        /* Destroy the previous atom distance object from the world */
                        if (atom_distance_previous_ != null)
                        {
                            Destroy(atom_distance_previous_);
                        }
                        /* and spawn the new in the middle of the distance */
                        atom_distance_previous_ = Instantiate(prefab_atom_distance_, middle, Quaternion.identity);
                        atom_distance_previous_.transform.parent = transform;
                        AtomDistance temp = atom_distance_previous_.GetComponent <AtomDistance>();
                        temp.atom1_ = previously_added;
                        temp.atom2_ = plane.center_sphere_;
                    }
                }
                return;
            }

            /* If there is not selected atom, process ray casting as usual */
            if (ray_cast_hit)
            {
                ProcessRayCastHit(hit);
            }
            else
            {
                ClearHighlighted();
                if (exploring_method_ != ExploringMethod.CHAINS)
                {
                    ClearColored();
                }
            }
        }
        else if (state == STATE.BOND_ANGLES)
        {
            /* If discard button is hit, reset state */
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                ResetState(true);
                return;
            }

            /* If selection is spawned, process selected bonds */
            if (selected_bond_ != null)
            {
                SelectionPlaneCylinders plane = selection_plane_previous_.GetComponent <SelectionPlaneCylinders>();

                int       current_index    = bonds_selected_id_ % 2;
                ICylinder previously_added = ((current_index == 0) ? bonds_selected_[1] : bonds_selected_[current_index - 1]);
                /* if the precious is different than the currently selected, select bond, and the arc */
                if (previously_added != plane.center_cylinder_)
                {
                    bonds_selected_[current_index] = plane.center_cylinder_;

                    bonds_selected_id_++;

                    if (previously_added != null)
                    {
                        if (arc_previous_ != null)
                        {
                            Destroy(arc_previous_);
                        }
                        SpawnArc(bonds_selected_[0], bonds_selected_[1]);
                    }
                }
                return;
            }

            /* Else, process ray casting for bonds */
            if (ray_cast_hit)
            {
                ClearHighlighted();

                ICylinder icylinder = hit.transform.GetComponent <ICylinder>();

                if (icylinder != null)
                {
                    icylinder.SetHighlighted(HighlightColors.HIGHLIGHT_COLOR.WHITE);
                    previously_highlighted_bond_ = icylinder;

                    /* If bond seleted, spawn selection object */
                    if (Input.GetMouseButtonDown(0) == true)
                    {
                        selected_bond_ = icylinder;
                        SpawnSelectionPlaneCylinders();
                    }
                }
            }
            else
            {
                ClearHighlighted();
            }
        }
        else if (state == STATE.TORSION_ANGLE)
        {
            /* if the torsion planed has been spawned, highlight the atoms that participate in it */
            if (torsion_plane_spawned_)
            {
                atoms_selected_[0].SetHighlighted(HighlightColors.HIGHLIGHT_COLOR.GREEN);
                atoms_selected_[1].SetHighlighted(HighlightColors.HIGHLIGHT_COLOR.GREEN);
                atoms_selected_[2].SetHighlighted(HighlightColors.HIGHLIGHT_COLOR.GREEN);
                atoms_selected_[3].SetHighlighted(HighlightColors.HIGHLIGHT_COLOR.GREEN);
            }

            /* If discard is pushed, and the torsion planed is spawned, go back to atom selection
             * else reset and destroy selection
             */
            if (Input.GetKeyDown(KeyCode.Escape))
            {
                if (torsion_plane_spawned_)
                {
                    ISphere last_added_sphere = ((atom_selected_id_ == 0) ? atoms_selected_[3] : atoms_selected_[atom_selected_id_ - 1]);
                    ResetState(false);
                    selected_atom_ = last_added_sphere;
                    SpawnSelectionPlaneSpheres();
                    info_ui_.SetAtom(last_added_sphere);
                }
                else
                {
                    ResetState(true);
                }

                return;
            }

            /* If selection is spawned, then process the currently selected atom */
            if (selected_atom_ != null)
            {
                SelectionPlaneSpheres plane = selection_plane_previous_.GetComponent <SelectionPlaneSpheres>();

                ISphere previously_added = ((atom_selected_id_ == 0) ? atoms_selected_[3] : atoms_selected_[atom_selected_id_ - 1]);
                if (Input.GetKeyDown(KeyCode.E) && atom_selected_id_ < 4 && previously_added != plane.center_sphere_)
                {
                    atoms_selected_[atom_selected_id_ % 4] = plane.center_sphere_;
                    info_ui_.SetTorsionAtom(atom_selected_id_, plane.center_sphere_.atom_.name_);

                    atom_selected_id_++;

                    /* If reached 4 selected atoms, spawn the torsion plane object */
                    if (atom_selected_id_ == 4)
                    {
                        SpawnTorsionAngle();
                        selected_atom_ = null;
                        Destroy(selection_plane_previous_);
                        torsion_plane_spawned_ = true;
                    }
                }
                return;
            }

            /* Else ray cast as usual */
            if (ray_cast_hit && !torsion_plane_spawned_)
            {
                ProcessRayCastHit(hit);
            }
            else
            {
                ClearHighlighted();
                if (exploring_method_ != ExploringMethod.CHAINS)
                {
                    ClearColored();
                }
            }
        } /* End of torsion angle state */
    }
Exemplo n.º 5
0
    public void ChangeModeToBondAngles()
    {
        /*
         * If the previous state is not bond angles, and an atom was selected, then find the closest bond
         * and move the selection there
         */
        if (state != STATE.BOND_ANGLES)
        {
            ISphere currently_selected_sphere = null;
            if (selection_plane_previous_ != null)
            {
                currently_selected_sphere = selection_plane_previous_.GetComponent <SelectionPlaneSpheres>().center_sphere_;
            }
            ResetState(true);

            if (currently_selected_sphere != null)
            {
                ICylinder nearest_bond = GetNearestBond(currently_selected_sphere.transform.position, 2 * AtomicRadii.GetCovalentRadius(currently_selected_sphere.atom_.element_));
                if (nearest_bond != null)
                {
                    selected_bond_ = nearest_bond;
                    SpawnSelectionPlaneCylinders();
                }
            }
        }
        else
        {
            ResetState(false);
        }

        state = STATE.BOND_ANGLES;
        transform.GetChild(1).GetComponent <ModePanel>().SetState(state);
        info_ui_.ResetInfo();
    }
Exemplo n.º 6
0
    /* The following change function are called by string reference, from the world UI buttons */
    public void ChangeVisualizationMethod()
    {
        if (visualization_method_ == VisualizationMethod.BALL_AND_STICK)
        {
            SetVisualizationMethod(VisualizationMethod.SPACE_FILLING);

            /*
             * If we are to change visualization method to space filling, change the marked atom object
             * since its position depends on the radius, and if we are bond angles mode, destroy reset state
             * since there are no bonds anymore
             */
            if (marked_atom_object_ != null)
            {
                ISphere sphere = marked_sphere_.GetComponent <ISphere>();
                marked_atom_object_.transform.position = sphere.transform.position + 1.4f * Vector3.up * AtomicRadii.GetCovalentRadius(sphere.atom_.element_);
            }
            if (state == STATE.BOND_ANGLES)
            {
                ResetState(true);
            }
        }
        else
        {
            SetVisualizationMethod(VisualizationMethod.BALL_AND_STICK);

            if (marked_atom_object_ != null)
            {
                ISphere sphere = marked_sphere_.GetComponent <ISphere>();
                marked_atom_object_.transform.position = sphere.transform.position + 1.4f * Vector3.up * AtomicRadii.ball_and_stick_radius;
            }
        }
    }
Exemplo n.º 7
0
    // Start is called before the first frame update
    void Start()
    {
        List <Atom>        atoms;
        List <List <int> > connections;

        PDBParser.ParseAtomsAndConnections(@"Assets/MModels/1tes.pdb", out atoms, out connections);
        //PDBParser.ParseAtomsAndConnections(@"Assets/MModels/4f0h.pdb", out atoms, out connections);
        //PDBParser.ParseAtomsAndConnections(@"Assets/MModels/1s5l.pdb", out atoms, out connections);
        //PDBParser.ParseAtomsAndConnections(@"Assets/MModels/1ea4.pdb", out atoms, out connections);

        Bounds atoms_bounding_box = new Bounds();

        foreach (Atom atom in atoms)
        {
            /* Units in Nano meters */
            Vector3 atom_position = new Vector3(atom.x_, atom.y_, atom.z_);
            atoms_bounding_box.Encapsulate(atom_position);

            /* Instantiate the object */
            GameObject temp = Instantiate(prefab_atom, atom_position, Quaternion.identity);
            temp.transform.parent = transform;
            temp.isStatic         = this.gameObject.isStatic;

            InsertToResiudesDictionary(atom, temp);
        }

        Transform bonds_transform = transform.GetChild(0);
        int       bonds           = 0;

        foreach (KeyValuePair <int, List <Tuple <GameObject, Atom> > > value in residue_dictionary)
        {
            List <Tuple <GameObject, Atom> > resiude_atoms = value.Value;
            for (int ia = 0; ia < resiude_atoms.Count; ia++)
            {
                GameObject a                 = resiude_atoms[ia].Item1;
                Vector3    a_position        = a.transform.position;
                float      a_covalent_radius = AtomicRadii.GetCovalentRadius(resiude_atoms[ia].Item2.element_);
                for (int ib = 0; ib < resiude_atoms.Count; ib++)
                {
                    if (!(ia > ib))
                    {
                        continue;
                    }
                    GameObject b = resiude_atoms[ib].Item1;

                    Vector3 b_position        = b.transform.position;
                    float   b_covalent_radius = AtomicRadii.radii_covalent[resiude_atoms[ib].Item2.element_];

                    float distance = Vector3.Distance(a_position, b_position);
                    if (distance <= a_covalent_radius + b_covalent_radius + 0.015)
                    {
                        bonds++;
                        GameObject temp = Instantiate(prefab_bond, a_position, Quaternion.identity);
                        temp.transform.parent = bonds_transform;
                        temp.isStatic         = this.gameObject.isStatic;

                        Vector3    direction  = b_position - a_position;
                        Quaternion toRotation = Quaternion.FromToRotation(new Vector3(0, 1, 0), direction);
                        temp.transform.rotation = toRotation;

                        ICylinderBench icylinder = temp.GetComponent <ICylinderBench>();
                        if (icylinder != null)
                        {
                            icylinder.radius_ = AtomicRadii.ball_and_stick_bond_radius;
                            icylinder.height_ = distance;
                        }
                        else
                        {
                            temp.transform.localScale = new Vector3(0.5f, 0.5f * distance, 0.5f);
                        }
                    }
                }
            }
        }
        Debug.Log("Spawned: " + bonds + " bonds");
    }
Exemplo n.º 8
0
    void Update()
    {
        /* Calculate the coordiinate system transformation matrix */
        CalculateInverseTransform();

        /* highlight and color center sphere */
        center_sphere_.SetHighlighted(HighlightColors.HIGHLIGHT_COLOR.GREEN);
        center_sphere_.SetCPKColor();

        /* Calculate positions and reset colors and highlighting for spheres within the radius */
        plane_positions_ = new List <Vector3>(spheres_.Count);
        for (int i = 0; i < spheres_.Count; i++)
        {
            ISphere s = spheres_[i];
            Vector4 sphere_world_position = new Vector4(s.transform.position.x, s.transform.position.y, s.transform.position.z, 1);
            Vector4 sphere_plane_position = ITM_ * sphere_world_position;
            plane_positions_.Add(sphere_plane_position);
            s.SetHighlighted(0);
            s.SetCPKColor();
        }

        /* Calculate spheres available for selection and their directions */
        FillArray();

        /* Highlight the spheres that can be navigated to, set the arrow directions based on the visualization used, or set the colors */
        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                int s = array_[i, j];
                /* If no sphere mapped in this direction, skip */
                if (s == -1)
                {
                    continue;
                }
                spheres_[s].SetHighlighted(HighlightColors.HIGHLIGHT_COLOR.WHITE);

                if (visualization == SelectionVisualizationMethod.COLOR_CIRCLE)
                {
                    spheres_[s].SetColor(colors_[j, i]);
                }
                else
                {
                    /* If arrows navigation used, calculate atomic radius used based on the atoms viusalization method */
                    float atom_radius;
                    if (atoms_object_.GetVisualizationMethod() == Atoms.VisualizationMethod.BALL_AND_STICK)
                    {
                        atom_radius = AtomicRadii.ball_and_stick_radius;
                    }
                    else
                    {
                        atom_radius = AtomicRadii.GetCovalentRadius(spheres_[s].atom_.element_);
                    }

                    /* and set the arrow objects in front of the atom */
                    arrows_[j, i].SetActive(true);
                    arrows_[j, i].transform.position = spheres_[s].transform.position +
                                                       Vector3.Normalize(Camera.main.transform.position - spheres_[s].transform.position) * 1.2f * atom_radius;

                    /* make arrows face the camera */
                    arrows_[j, i].transform.rotation = Quaternion.LookRotation(arrows_[j, i].transform.position - Camera.main.transform.position, Camera.main.transform.up);
                }
            }
        }

        /* Get the index for the selected sphere based on the control input */
        int sphere_index = GetDirectionInput();

        if (sphere_index != -1)
        {
            ISphere s = spheres_[sphere_index];

            /* If clicked as well, move the selection */
            if (Input.GetMouseButtonDown(0))
            {
                MoveSelectionToSphere(s);
            }
            else
            {
                /* Make input selected sphere white */
                s.SetColor(Color.white);
            }
        }
    }