Newer
Older
move_esophagus_center / Assets / Scripts / CameraMoveCenter.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class CameraMoveCenter : MonoBehaviour
{

    public GameObject centerLines;
    public GameObject virtualEndoscope;

    //中心点たどるよう変数
    List<Vector3> worldVertices = new List<Vector3>();
    List<float> distanceOfPointsList = new List<float>();
    int posIndex = 0;

    //csv出力用
    float nomalizedNum = (125.6f / 12.4f);
    int whileCount = 0;
    string basePath = "C:\\UnityWorkSpace\\";
    float beforeDistance = 0.0f;

    // Use this for initialization
    void Start()
    {
        var moveMatrix = centerLines.transform.localToWorldMatrix;
        var centerVertices = centerLines.GetComponent<MeshFilter>().mesh.vertices;

        foreach (var vertice in centerVertices)
        {
            worldVertices.Add(moveMatrix.MultiplyPoint3x4(vertice));
        }

        distanceOfPointsList = getDistanceOfPointList(worldVertices);

        if (worldVertices != null)
        {
            virtualEndoscope.transform.position = worldVertices[0];
            virtualEndoscope.transform.LookAt(worldVertices[1]);
        }
        else Debug.Log("ヌル!!!");
    }

    // Update is called once per frame
    void Update()
    {
        string pathToCSV = basePath + whileCount.ToString() + ".csv";

        float insertDistance = getInsertionDistance(pathToCSV);

        if (0.0f < insertDistance && posIndex != worldVertices.Count - 1)
        {
            //ちゅうと半端な時の処理を追加

            while (true)
            {
                if (insertDistance < distanceOfPointsList[posIndex])
                {
                    //ここにいろいろ足す
                    virtualEndoscope.transform.position = worldVertices[posIndex];
                    virtualEndoscope.transform.LookAt(worldVertices[posIndex + 1]);

                    break;
                }

                insertDistance -= distanceOfPointsList[posIndex];
                posIndex++;
            }
        }
        else if (insertDistance < 0.0f && posIndex != 0)
        {
            insertDistance = -insertDistance;

            while (true)
            {
                if (insertDistance < distanceOfPointsList[posIndex - 1])
                {
                    virtualEndoscope.transform.position = worldVertices[posIndex];
                    virtualEndoscope.transform.LookAt(worldVertices[posIndex + 1]);

                    break;
                }

                insertDistance -= distanceOfPointsList[posIndex - 1];
                posIndex--;
            }
        }

        /*
        if(0.0f < insertDistance && posIndex != worldVertices.Count - 1)
        {
            float nextDistance = (virtualEndoscope.transform.position - worldVertices[posIndex + 1]).magnitude;
            
            if(insertDistance <= nextDistance)
            {
                virtualEndoscope.transform.Translate(0.0f, 0.0f, insertDistance);
            }
            else if(nextDistance < insertDistance)
            {
                insertDistance = insertDistance - nextDistance;
                posIndex++;
                //virtualEndoscope.transform.position = worldVertices[posIndex];
                //virtualEndoscope.transform.LookAt(worldVertices[posIndex + 1]);

                while (true)
                {
                    if(posIndex == worldVertices.Count - 1)
                    {
                        break;
                    }

                    insertDistance = insertDistance - distanceOfPointsList[posIndex];

                    if (insertDistance < 0.0f)
                    {
                        insertDistance = insertDistance + distanceOfPointsList[posIndex];
                        virtualEndoscope.transform.position = worldVertices[posIndex];
                        virtualEndoscope.transform.LookAt(worldVertices[posIndex + 1]);
                        virtualEndoscope.transform.Translate(0.0f, 0.0f, insertDistance);
                        break;
                    }

                    posIndex++;
                    //virtualEndoscope.transform.position = worldVertices[posIndex];
                    //virtualEndoscope.transform.LookAt(worldVertices[posIndex + 1]);
                }
            }
        }
        else if(insertDistance < 0.0f && posIndex != 0)
        {
            float beforeDistance = (virtualEndoscope.transform.position - worldVertices[posIndex - 1]).magnitude;

            if (insertDistance <= beforeDistance)
            {
                virtualEndoscope.transform.Translate(0.0f, 0.0f, -insertDistance);
            }
            else if (beforeDistance < insertDistance)
            {
                insertDistance = insertDistance - beforeDistance;
                posIndex--;
                //virtualEndoscope.transform.position = worldVertices[posIndex];
                //virtualEndoscope.transform.LookAt(worldVertices[posIndex + 1]);

                while (true)
                {
                    if (posIndex == 0)
                    {
                        break;
                    }

                    insertDistance = insertDistance - distanceOfPointsList[posIndex - 1];

                    if (insertDistance < 0.0f)
                    {
                        insertDistance = insertDistance + distanceOfPointsList[posIndex - 1];
                        virtualEndoscope.transform.position = worldVertices[posIndex];
                        virtualEndoscope.transform.LookAt(worldVertices[posIndex + 1]);
                        virtualEndoscope.transform.Translate(0.0f, 0.0f, -insertDistance);
                        break;
                    }

                    posIndex--;
                    //virtualEndoscope.transform.position = worldVertices[posIndex];
                    //virtualEndoscope.transform.LookAt(worldVertices[posIndex + 1]);
                }
            }
        }
        */
    }

    /*
     * 返り値: List<Vector3>
     * 引数: カメラに近い順番に並んだ座標値のList
     * 機能: 座標間の距離のListを作成する
     */
    List<float> getDistanceOfPointList(List<Vector3> movePoints)
    {
        var pointsDistanceList = new List<float>();

        for (int i = 0; i < movePoints.Count - 1; i++)
        {
            pointsDistanceList.Add((movePoints[i + 1] - movePoints[i]).magnitude);
        }

        return pointsDistanceList;
    }


    /*
     * 返り値: float
     * 引数: CSVファイルへのパス string
     * 機能: 挿入距離を取得
     */
    float getInsertionDistance(string pathToCSV)
    {
        float unityMoveDistance = 0.0f;
        float moveDistance = 0.0f;

        if (File.Exists(pathToCSV))
        {
            var moveList = new List<string>();
            var sr = new StreamReader(pathToCSV);

            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();
                string[] values = line.Split(',');
                moveList.AddRange(values);
            }

            moveDistance = float.Parse(moveList[0]);

            if (beforeDistance != moveDistance)
            {
                unityMoveDistance = moveDistance * nomalizedNum;
                beforeDistance = moveDistance;
            }

            moveList.Clear();
            sr.Close();

            whileCount++;
        }

        return unityMoveDistance;
    }
}