예제 #1
0
    /// <summary>
    /// Split this node on two.
    /// </summary>
    public void Split()
    {
        var newNode = SplineFormer.AddNodeImmediately(this);

        newNode.transform.position  = this.transform.position;
        newNode.transform.position += (LeftP - LocalPosition).normalized * 2f * SplineFormer.VisualOptions.NodeSize;
        this.transform.position    += (RightP - LocalPosition).normalized * 2f * SplineFormer.VisualOptions.NodeSize;
    }
예제 #2
0
    // Gets new data from Sense
    public void NewData(string d)
    {
        // Remove any existing sankey
        foreach (Transform child in transform)
        {
            GameObject.Destroy(child.gameObject);
        }
        transform.rotation = Quaternion.identity;
        transform.position = new Vector3(0, 0, 0);
        pathGOs.Clear();

        Debug.Log("data: " + d);
        JSONNode JPaths = JSON.Parse(d);
        //var pathList = new List<Path>();
        var Paths          = new Dictionary <int, Dictionary <string, int> >();
        var CompletedPaths = new Dictionary <int, float>();
        var Groups         = new Dictionary <int, Dictionary <string, float> >();
        int MaxSteps       = 0;

        for (int i = 0; i < JPaths.AsArray.Count; i++)
        {
            // Splice path
            string p = JPaths[i];
            //Debug.Log("path: " + p);
            string   path  = p.Substring(1, p.Length - 2);
            string[] nodes = path.Split(',');
            if (nodes.Length < 2)
            {
                continue;
            }

            for (int j = 0; j < nodes.Length; j++)
            {
                string n = nodes[j];
                //Debug.Log("node: " + n);
                if (Paths.ContainsKey(j))
                {
                    if (Paths[j].ContainsKey(n))
                    {
                        Paths[j][n]++;
                    }
                    else
                    {
                        Paths[j][n] = 1;
                    }
                }
                else
                {
                    Paths.Add(j, new Dictionary <string, int>());
                    Paths[j][n] = 1;
                }
                MaxSteps = MaxSteps > Paths[j][n] ? MaxSteps : Paths[j][n];
                //Original
                //MaxSteps = MaxSteps > Paths[j][n] ? MaxSteps : Paths[j][n];

                MaxSteps = MaxSteps > nodes.Length ? MaxSteps : nodes.Length;
                Debug.Log("SANKEY MaxSteps: " + MaxSteps);
            }
        }

        for (int i = 0; i < JPaths.AsArray.Count; i++)
        {
            string   p     = JPaths[i];
            string   path  = p.Substring(1, p.Length - 2);
            string[] nodes = path.Split(',');
            if (nodes.Length < 2)
            {
                continue;
            }
            if (nodes.Length < Paths.Count)
            {
                Paths[Paths.Count - 1][nodes[nodes.Length - 1]] = 1;
            }
        }
        Debug.Log("Last pos length: " + Paths[Paths.Count - 1].Count);

        for (int i = 0; i < JPaths.AsArray.Count; i++)
        {
            // Splice path
            string p = JPaths[i];
            Debug.Log("path: " + p);
            string   path  = p.Substring(1, p.Length - 2);
            string[] nodes = path.Split(',');
            if (nodes.Length < 2)
            {
                continue;
            }

            GameObject go = createSpline();
            Vector3    pp = transform.position;
            //            go.transform.position = new Vector3(pp.x, chartHeight / (float)JPaths.AsArray.Count * (float)i, pp.z);
            go.transform.position = new Vector3(pp.x, pp.y, pp.z);
            pathGOs.Add(go);

            // Setup nodes
            SplineFormer sf = go.GetComponent <SplineFormer>();

            for (int j = 0; j < nodes.Length; j++)
            {
                string n = nodes[j];
                //Debug.Log("node: " + n);

                //                float zStep= chartLength / (nodes.Length-1);
                float zStep = chartLength / MaxSteps;
                //                float zPos = zStep * (float)j;
                float zPos;
                zPos = j < (nodes.Length - 1) ? zStep * (float)j : chartLength;

                if (j < nodes.Length - 1)
                {
                    if (CompletedPaths.ContainsKey(j))
                    {
                        CompletedPaths[j]++;
                    }
                    else
                    {
                        CompletedPaths.Add(j, 1f);
                    }
                }
                else
                {
                    if (CompletedPaths.ContainsKey(Paths[Paths.Count - 1].Count))
                    {
                        CompletedPaths[Paths[Paths.Count - 1].Count]++;
                    }
                    else
                    {
                        CompletedPaths.Add(Paths[Paths.Count - 1].Count, 1f);
                    }
                }

                SplineNode sn = null;
                if (j > 1)
                {
                    sn = sf.AddNodeImmediately();
                    Vector3 f = sn.transform.position;
                    float   hOffset;
                    if (j < nodes.Length - 1)
                    {
                        if (Groups.ContainsKey(j))
                        {
                            if (Groups[j].ContainsKey(n))
                            {
                                hOffset = Groups[j][n];
                            }
                            else
                            {
                                Groups[j][n] = chartHeight / (float)(Paths[j].Count + 1) * CompletedPaths[j];
                                hOffset      = Groups[j][n];
                            }
                        }
                        else
                        {
                            Groups.Add(j, new Dictionary <string, float>());
                            Groups[j][n] = chartHeight / (float)(Paths[j].Count + 1) * CompletedPaths[j];
                            hOffset      = Groups[j][n];
                        }
                    }
                    else
                    {
                        //hOffset = chartHeight / (float)(Paths[Paths.Count - 1].Count + 1) * CompletedPaths[Paths[Paths.Count - 1].Count];
                        if (Groups.ContainsKey(Paths.Count - 1))
                        {
                            if (Groups[Paths.Count - 1].ContainsKey(n))
                            {
                                hOffset = Groups[Paths.Count - 1][n];
                            }
                            else
                            {
                                Groups[Paths.Count - 1][n] = chartHeight / (float)(Paths[Paths.Count - 1].Count + 1) * CompletedPaths[Paths[Paths.Count - 1].Count];
                                hOffset = Groups[Paths.Count - 1][n];
                            }
                        }
                        else
                        {
                            Groups.Add(Paths.Count - 1, new Dictionary <string, float>());
                            Groups[Paths.Count - 1][n] = chartHeight / (float)(Paths[Paths.Count - 1].Count + 1) * CompletedPaths[Paths[Paths.Count - 1].Count];
                            hOffset = Groups[Paths.Count - 1][n];
                        }
                    }


                    f = f + Vector3.up * hOffset;
                    sn.transform.position = new Vector3(f.x, f.y, zPos);
                }
                else if (j == 1)
                {
                    sn = sf.Nodes[j];
                    Vector3 f = sn.transform.position;
                    float   hOffset;
                    if (j < nodes.Length - 1)
                    {
                        //Debug.Log("Path count: " + Paths[j][n]);
                        if (Paths[j][n] > 1)
                        {
                            if (Groups.ContainsKey(j))
                            {
                                if (Groups[j].ContainsKey(n))
                                {
                                    hOffset = Groups[j][n];
                                }
                                else
                                {
                                    Groups[j][n] = chartHeight / (float)(Paths[j].Count + 1) * CompletedPaths[j];
                                    hOffset      = Groups[j][n];
                                }
                            }
                            else
                            {
                                Groups.Add(j, new Dictionary <string, float>());
                                Groups[j][n] = chartHeight / (float)(Paths[j].Count + 1) * CompletedPaths[j];
                                hOffset      = Groups[j][n];
                            }
                        }
                        else
                        {
                            hOffset = chartHeight / (float)(Paths[j].Count + 1) * CompletedPaths[j];
                        }
                    }
                    else
                    {
                        //hOffset = chartHeight / (float)(Paths[Paths.Count - 1].Count + 1) * CompletedPaths[Paths[Paths.Count - 1].Count];
                        if (Groups.ContainsKey(Paths.Count - 1))
                        {
                            if (Groups[Paths.Count - 1].ContainsKey(n))
                            {
                                hOffset = Groups[Paths.Count - 1][n];
                            }
                            else
                            {
                                Groups[Paths.Count - 1][n] = chartHeight / (float)(Paths[Paths.Count - 1].Count + 1) * CompletedPaths[Paths[Paths.Count - 1].Count];
                                hOffset = Groups[Paths.Count - 1][n];
                            }
                        }
                        else
                        {
                            Groups.Add(Paths.Count - 1, new Dictionary <string, float>());
                            Groups[Paths.Count - 1][n] = chartHeight / (float)(Paths[Paths.Count - 1].Count + 1) * CompletedPaths[Paths[Paths.Count - 1].Count];
                            hOffset = Groups[Paths.Count - 1][n];
                        }
                    }

                    f = f + Vector3.up * hOffset;
                    sn.transform.position = new Vector3(f.x, f.y, zPos);
                }
                else if (j == 0)
                {
                    sn = sf.Nodes[j];
                    Vector3 f = sn.transform.position;

                    float yStep = chartHeight / (float)JPaths.AsArray.Count;
                    ////                    f = f + Vector3.up * -((float)i-1f) * yStep;
                    //                    f = f + Vector3.up * chartHeight / (float)(Paths[j].Count+1);
                    //Debug.Log("NumNodesAtPos " + j + ": " + Paths[j].Count + " for path " + i + " = " + chartHeight / (float)(Paths[j].Count + 1));

                    if (Groups.ContainsKey(j))
                    {
                        if (!Groups[j].ContainsKey(n))
                        {
                            Groups[j][n] = chartHeight / (float)(Paths[j].Count + 1);
                        }
                    }
                    else
                    {
                        Groups.Add(j, new Dictionary <string, float>());
                        Groups[j][n] = chartHeight / (float)(Paths[j].Count + 1);
                    }

                    f = f + Vector3.up * Groups[j][n];
                    sn.transform.position = f;
                }

                // Add Sphere Nodes to nodes
                GameObject spNode = (GameObject)Instantiate(Resources.Load("SankeyNode"), sn.transform.position, sn.transform.rotation);
                Vector3    vec    = new Vector3(0f, 1f, 0f);
                spNode.transform.Rotate(vec, 180f);
                spNode.transform.SetParent(sn.transform);
                float _nodeScaler = Mathf.Lerp(0.1f, 0.2f, Paths[j][n] / MaxSteps);
                spNode.transform.localScale = new Vector3(_nodeScaler, _nodeScaler, _nodeScaler);

                // Add Text Labels to nodes
                GameObject label = spNode.transform.GetChild(0).gameObject;
                label.transform.RotateAround(sn.transform.position, Vector3.up, 90);
                GameObject txt = label.transform.GetChild(0).gameObject;
                txt.GetComponent <UnityEngine.UI.Text>().text = n;

                // Add Tap Handler
                SelectMgr hit = spNode.AddComponent <SelectMgr>();
            }
            sf.InvalidateMesh();
        }

        // Move Sankey above patient

        /*Vector3 pPos = patient.transform.position;
         * pPos.x -= 0.4f;
         * pPos.y += 0.8f;
         * pPos.z -= 0.5f;
         * gameObject.transform.position = pPos;
         * Quaternion pRot = patient.transform.rotation;
         * pRot.y -= Mathf.PI * 0.25f;
         * gameObject.transform.rotation = pRot;
         */
        ReTag(gameObject.transform, LayerMask.NameToLayer("Sankey"));
    }