diff --git a/Assets/5. Transfer Function/Scenes/Transfer Function.unity b/Assets/5. Transfer Function/Scenes/Transfer Function.unity index 9805137..47a77ca 100644 --- a/Assets/5. Transfer Function/Scenes/Transfer Function.unity +++ b/Assets/5. Transfer Function/Scenes/Transfer Function.unity @@ -213,6 +213,171 @@ m_Father: {fileID: 0} m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0} +--- !u!1 &211680248 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 211680251} + - component: {fileID: 211680250} + - component: {fileID: 211680249} + m_Layer: 0 + m_Name: EventSystem + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &211680249 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 211680248} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4f231c4fb786f3946a6b90b886c48677, type: 3} + m_Name: + m_EditorClassIdentifier: + m_HorizontalAxis: Horizontal + m_VerticalAxis: Vertical + m_SubmitButton: Submit + m_CancelButton: Cancel + m_InputActionsPerSecond: 10 + m_RepeatDelay: 0.5 + m_ForceModuleActive: 0 +--- !u!114 &211680250 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 211680248} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 76c392e42b5098c458856cdf6ecaaaa1, type: 3} + m_Name: + m_EditorClassIdentifier: + m_FirstSelected: {fileID: 0} + m_sendNavigationEvents: 1 + m_DragThreshold: 10 +--- !u!4 &211680251 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 211680248} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 5 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!1 &508286134 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 508286138} + - component: {fileID: 508286137} + - component: {fileID: 508286136} + - component: {fileID: 508286135} + m_Layer: 5 + m_Name: Canvas + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!114 &508286135 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 508286134} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: dc42784cf147c0c48a680349fa168899, type: 3} + m_Name: + m_EditorClassIdentifier: + m_IgnoreReversedGraphics: 1 + m_BlockingObjects: 0 + m_BlockingMask: + serializedVersion: 2 + m_Bits: 4294967295 +--- !u!114 &508286136 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 508286134} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 0cd44c1031e13a943bb63640046fad76, type: 3} + m_Name: + m_EditorClassIdentifier: + m_UiScaleMode: 0 + m_ReferencePixelsPerUnit: 100 + m_ScaleFactor: 1 + m_ReferenceResolution: {x: 800, y: 600} + m_ScreenMatchMode: 0 + m_MatchWidthOrHeight: 0 + m_PhysicalUnit: 3 + m_FallbackScreenDPI: 96 + m_DefaultSpriteDPI: 96 + m_DynamicPixelsPerUnit: 1 +--- !u!223 &508286137 +Canvas: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 508286134} + m_Enabled: 1 + serializedVersion: 3 + m_RenderMode: 0 + m_Camera: {fileID: 0} + m_PlaneDistance: 100 + m_PixelPerfect: 0 + m_ReceivesEvents: 1 + m_OverrideSorting: 0 + m_OverridePixelPerfect: 0 + m_SortingBucketNormalizedSize: 0 + m_AdditionalShaderChannelsFlag: 0 + m_SortingLayerID: 0 + m_SortingOrder: 0 + m_TargetDisplay: 0 +--- !u!224 &508286138 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 508286134} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 0, y: 0, z: 0} + m_Children: + - {fileID: 1331901475} + m_Father: {fileID: 0} + m_RootOrder: 4 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 0, y: 0} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 0, y: 0} + m_Pivot: {x: 0, y: 0} --- !u!1 &1000675180 GameObject: m_ObjectHideFlags: 0 @@ -223,6 +388,7 @@ m_Component: - component: {fileID: 1000675181} - component: {fileID: 1000675182} + - component: {fileID: 1000675183} m_Layer: 0 m_Name: SystemManager m_TagString: Untagged @@ -256,7 +422,26 @@ m_Script: {fileID: 11500000, guid: ade87100a2e14fa4e818d9571b0aff1b, type: 3} m_Name: m_EditorClassIdentifier: ---- !u!1 &1507480465 + start_point_vec: {x: 0, y: 0, z: 0} + center_points_vec: [] + prefabOBJ: {fileID: 510536687663676710, guid: da85052efecd4f14580a44a59eea8e29, + type: 3} +--- !u!114 &1000675183 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1000675180} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 1b8cf74834db2774598fc11346cf0901, type: 3} + m_Name: + m_EditorClassIdentifier: + light_source: {fileID: 73477751} + virtul_endscope: {fileID: 2108303124} + start_point_vec: {x: 0, y: 0, z: 0} +--- !u!1 &1007459479 GameObject: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} @@ -264,91 +449,204 @@ m_PrefabAsset: {fileID: 0} serializedVersion: 6 m_Component: - - component: {fileID: 1507480469} - - component: {fileID: 1507480468} - - component: {fileID: 1507480467} - - component: {fileID: 1507480466} - m_Layer: 0 - m_Name: Sphere + - component: {fileID: 1007459480} + - component: {fileID: 1007459482} + - component: {fileID: 1007459481} + m_Layer: 5 + m_Name: Text m_TagString: Untagged m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 m_IsActive: 1 ---- !u!135 &1507480466 -SphereCollider: +--- !u!224 &1007459480 +RectTransform: m_ObjectHideFlags: 0 m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1507480465} - m_Material: {fileID: 0} - m_IsTrigger: 0 - m_Enabled: 1 - serializedVersion: 2 - m_Radius: 0.5 - m_Center: {x: 0, y: 0, z: 0} ---- !u!23 &1507480467 -MeshRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1507480465} - m_Enabled: 1 - m_CastShadows: 1 - m_ReceiveShadows: 1 - m_DynamicOccludee: 1 - m_MotionVectors: 1 - m_LightProbeUsage: 1 - m_ReflectionProbeUsage: 1 - m_RayTracingMode: 2 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: - - {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0} - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_ReceiveGI: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 1 - m_SelectedEditorRenderState: 3 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_SortingLayerID: 0 - m_SortingLayer: 0 - m_SortingOrder: 0 ---- !u!33 &1507480468 -MeshFilter: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1507480465} - m_Mesh: {fileID: 10207, guid: 0000000000000000e000000000000000, type: 0} ---- !u!4 &1507480469 -Transform: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 1507480465} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: -7.12667, y: 47.705956, z: -297.5} - m_LocalScale: {x: 10, y: 10, z: 10} + m_GameObject: {fileID: 1007459479} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] - m_Father: {fileID: 0} - m_RootOrder: 4 + m_Father: {fileID: 1331901475} + m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 1, y: 1} + m_AnchoredPosition: {x: 0, y: 0} + m_SizeDelta: {x: 0, y: 0} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &1007459481 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1007459479} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 5f7201a12d95ffc409449d95f23cf332, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 0.19607843, g: 0.19607843, b: 0.19607843, a: 1} + m_RaycastTarget: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_FontData: + m_Font: {fileID: 10102, guid: 0000000000000000e000000000000000, type: 0} + m_FontSize: 14 + m_FontStyle: 0 + m_BestFit: 0 + m_MinSize: 10 + m_MaxSize: 40 + m_Alignment: 4 + m_AlignByGeometry: 0 + m_RichText: 1 + m_HorizontalOverflow: 0 + m_VerticalOverflow: 0 + m_LineSpacing: 1 + m_Text: Button +--- !u!222 &1007459482 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1007459479} + m_CullTransparentMesh: 0 +--- !u!1 &1331901474 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 1331901475} + - component: {fileID: 1331901478} + - component: {fileID: 1331901477} + - component: {fileID: 1331901476} + m_Layer: 5 + m_Name: Button + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!224 &1331901475 +RectTransform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1331901474} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_Children: + - {fileID: 1007459480} + m_Father: {fileID: 508286138} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} + m_AnchorMin: {x: 0, y: 1} + m_AnchorMax: {x: 0, y: 1} + m_AnchoredPosition: {x: 100, y: -100} + m_SizeDelta: {x: 160, y: 30} + m_Pivot: {x: 0.5, y: 0.5} +--- !u!114 &1331901476 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1331901474} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 4e29b1a8efbd4b44bb3f3716e73f07ff, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Navigation: + m_Mode: 3 + m_SelectOnUp: {fileID: 0} + m_SelectOnDown: {fileID: 0} + m_SelectOnLeft: {fileID: 0} + m_SelectOnRight: {fileID: 0} + m_Transition: 1 + m_Colors: + m_NormalColor: {r: 1, g: 1, b: 1, a: 1} + m_HighlightedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} + m_PressedColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 1} + m_SelectedColor: {r: 0.9607843, g: 0.9607843, b: 0.9607843, a: 1} + m_DisabledColor: {r: 0.78431374, g: 0.78431374, b: 0.78431374, a: 0.5019608} + m_ColorMultiplier: 1 + m_FadeDuration: 0.1 + m_SpriteState: + m_HighlightedSprite: {fileID: 0} + m_PressedSprite: {fileID: 0} + m_SelectedSprite: {fileID: 0} + m_DisabledSprite: {fileID: 0} + m_AnimationTriggers: + m_NormalTrigger: Normal + m_HighlightedTrigger: Highlighted + m_PressedTrigger: Pressed + m_SelectedTrigger: Selected + m_DisabledTrigger: Disabled + m_Interactable: 1 + m_TargetGraphic: {fileID: 1331901477} + m_OnClick: + m_PersistentCalls: + m_Calls: + - m_Target: {fileID: 1000675183} + m_MethodName: OnClick + m_Mode: 1 + m_Arguments: + m_ObjectArgument: {fileID: 0} + m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine + m_IntArgument: 0 + m_FloatArgument: 0 + m_StringArgument: + m_BoolArgument: 0 + m_CallState: 2 +--- !u!114 &1331901477 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1331901474} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: fe87c0e1cc204ed48ad3b37840f39efc, type: 3} + m_Name: + m_EditorClassIdentifier: + m_Material: {fileID: 0} + m_Color: {r: 1, g: 1, b: 1, a: 1} + m_RaycastTarget: 1 + m_OnCullStateChanged: + m_PersistentCalls: + m_Calls: [] + m_Sprite: {fileID: 10905, guid: 0000000000000000f000000000000000, type: 0} + m_Type: 1 + m_PreserveAspect: 0 + m_FillCenter: 1 + m_FillMethod: 4 + m_FillAmount: 1 + m_FillClockwise: 1 + m_FillOrigin: 0 + m_UseSpriteMesh: 0 + m_PixelsPerUnitMultiplier: 1 +--- !u!222 &1331901478 +CanvasRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 1331901474} + m_CullTransparentMesh: 0 --- !u!1 &1873212973 GameObject: m_ObjectHideFlags: 0 @@ -572,7 +870,7 @@ m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 2108303124} m_LocalRotation: {x: 0.43343452, y: -0.5586913, z: 0.43201023, w: 0.5597908} - m_LocalPosition: {x: 0.99, y: 3.4, z: -0.25} + m_LocalPosition: {x: 0.99, y: 330, z: -0.25} m_LocalScale: {x: 1, y: 1, z: 1} m_Children: [] m_Father: {fileID: 0} @@ -592,5 +890,5 @@ m_EditorClassIdentifier: x_sensi: 3 y_sensi: 3 - trans_scale: 0.0125 + trans_scale: 1 target_camera: {fileID: 2108303124} diff --git a/Assets/Scripts/Camera_udp.cs b/Assets/Scripts/Camera_udp.cs new file mode 100644 index 0000000..ad4a3ae --- /dev/null +++ b/Assets/Scripts/Camera_udp.cs @@ -0,0 +1,364 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using System.IO; +using System.Linq; +using System.Net; +using System.Net.Sockets; +using System.Text; + + +[System.Serializable] +public class Setting +{ + public float x_scale; + public float y_scale; + public float z_scale; + public string start_point_vec; + public string[] center_points_vec; +} + + +public class Camera_udp : MonoBehaviour +{ + //public GameObject prefabOBJ; + + public GameObject light_source; + public GameObject virtul_endscope; + + //中心点たどるよう変数 + public Vector3 start_point_vec; + List worldVertices = new List(); + List sortedWorldVertices = new List(); + List distanceOfPointsList = new List(); + int posIndex = 0; + + //csv出力用 + // float nomalizedNum = (125.6f / 12.4f); + int whileCount = 0; + // string basePath = "C:\\UnityWorkSpace\\"; + float beforeDistance = 0.0f; + + //ボタン制御用 + bool Is_starting = false; + + //udp関連 + static UdpClient udp; + // IPEndPoint remoteEP = null; + + + // ボタンが押された場合、今回呼び出される関数 + public void OnClick() + { + Debug.Log("押された!"); // ログを出力 + Is_starting = true; + GameObject obj = GameObject.Find("Canvas/Button"); + Destroy(obj); + } + + // Use this for initialization + void Start() + { + int LOCAL_PORT = 50007; + udp = new UdpClient(LOCAL_PORT); + udp.Client.ReceiveTimeout = 100; + + // var moveMatrix = centerLines.transform.localToWorldMatrix; + // var centerVertices = centerLines.GetComponent().mesh.vertices; + + //foreach (var vertice in centerVertices) + //{ + // worldVertices.Add(moveMatrix.MultiplyPoint3x4(vertice)); + //} + + string data_str = ""; + var reader = new StreamReader(Application.dataPath + "/setting.json"); ; + data_str = reader.ReadToEnd(); + var inputJson = JsonUtility.FromJson(data_str); + start_point_vec = decodeStr2Vec3(inputJson.center_points_vec[0]); + + foreach (var points_str in inputJson.center_points_vec) + { + worldVertices.Add(decodeStr2Vec3(points_str)); + } + light_source.transform.position = start_point_vec; + virtul_endscope.transform.position = start_point_vec; + + sortedWorldVertices = sortNearestPoint(light_source.transform.position, worldVertices); + + distanceOfPointsList = getDistanceOfPointList(sortedWorldVertices); + + if (worldVertices != null) + { + light_source.transform.LookAt(sortedWorldVertices[1]); + virtul_endscope.transform.LookAt(sortedWorldVertices[1]); + } + else Debug.Log("ヌル!!!"); + + + //foreach (var center_vec in sortedWorldVertices) + //{ + // GameObject obj = Instantiate(prefabOBJ, center_vec, Quaternion.identity); + //} + } + + + + /* + * 返り値: List + * 引数: カメラに近い順番に並んだ座標値のList + * 機能: 座標間の距離のListを作成する + */ + List getDistanceOfPointList(List movePoints) + { + var pointsDistanceList = new List(); + + 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(); + 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; + //unityMoveDistance = moveDistance; + beforeDistance = moveDistance; + } + + moveList.Clear(); + sr.Close(); + + whileCount++; + } + + //Debug.Log(whileCount); + return unityMoveDistance; + } + + + /* + * 返り値: List + * 引数: 最初のカメラ位置(Vector3), 世界系での中心座標(List) + * 機能: カメラから近い順にソートする(処理の都合上,第2引数のListは空になる.Copyとか足せばどうにかなる) + */ + List sortNearestPoint(Vector3 initCameraPoint, List centerVertices) + { + var sortedCenterVertices = new List(); + float minDistance = float.MaxValue; + var nearestVertex = new Vector3(); + + centerVertices.Distinct(); + + foreach (var vertex in centerVertices) + { + float tempMagnitude = (vertex - initCameraPoint).magnitude; + if (tempMagnitude < minDistance) + { + nearestVertex = vertex; + minDistance = tempMagnitude; + } + } + sortedCenterVertices.Add(nearestVertex); + worldVertices.Remove(nearestVertex); + + var currentVertex = nearestVertex; + int initialCount = centerVertices.Count; + + for (int i = 0; i < initialCount; i++) + { + minDistance = float.MaxValue; + + foreach (var vertex in worldVertices) + { + float tempMagnitude = (vertex - currentVertex).magnitude; + if (tempMagnitude < minDistance) + { + nearestVertex = vertex; + minDistance = tempMagnitude; + } + } + sortedCenterVertices.Add(nearestVertex); + worldVertices.Remove(nearestVertex); + currentVertex = nearestVertex; + } + + return sortedCenterVertices; + } + + /* + * 返り値: List + */ + + + /* + * 返り値: Vector3 + * 引数: 世界系の中心座標(List), その座標系でのインデックス(int) + * 機能:LookAt先のvectorを演算,自分の先のベクターを利用.ハイパーパラメータとしてVector間隔,重心決定範囲を設定 + */ + Vector3 calcBarycenter(List centers, int index) + { + // ハイパーパラメータ + int interval = 5; + int range = centers.Count / 50; + + //以下処理 + var tempVectors = new List(); + var maxIndex = centers.Count; + + for (int i = 0; i < range; i++) + { + if (maxIndex <= (index + (i * interval))) break; + tempVectors.Add(centers[index + (i * interval)]); + } + + return new Vector3(tempVectors.Average(V => V.x), tempVectors.Average(V => V.y), tempVectors.Average(V => V.z)); + } + + + /* + * 返り値: 無し + * 引数: 無し + * 機能: InvokeRepeating用,実質Update内の記述 + */ + void Update() + { + float insertionDistance = 0.0f; + + IPEndPoint remoteEP = null; + try + { + byte[] data = udp.Receive(ref remoteEP); + string text = Encoding.UTF8.GetString(data); + insertionDistance = float.Parse(text); + } + catch + { + + } + + //Debug.Log(posIndex); + + if (Is_starting) + { + if (0.0f < insertionDistance && posIndex != sortedWorldVertices.Count - 1) + { + float nearestDistance = (light_source.transform.position - sortedWorldVertices[posIndex + 1]).magnitude; + + if (insertionDistance <= nearestDistance) + { + light_source.transform.LookAt(sortedWorldVertices[posIndex + 1]); + light_source.transform.Translate(0.0f, 0.0f, insertionDistance); + } + else + { + insertionDistance -= nearestDistance; + posIndex++; + + while (true) + { + if (posIndex == sortedWorldVertices.Count - 1) + { + light_source.transform.position = sortedWorldVertices[posIndex]; + virtul_endscope.transform.position = light_source.transform.position; + Debug.Log("end"); + + break; + } + else if (insertionDistance < distanceOfPointsList[posIndex]) + { + light_source.transform.position = sortedWorldVertices[posIndex]; + // ここは理想はトランスレイトではなく,ポジションに変える.そうすれば視界も安定する. + light_source.transform.LookAt(sortedWorldVertices[posIndex + 1]); + light_source.transform.Translate(0.0f, 0.0f, insertionDistance); + // virtualEndoscope.transform.LookAt(calcBarycenter(sortedWorldVertices, posIndex)); + virtul_endscope.transform.position = light_source.transform.position; + + break; + } + + insertionDistance -= distanceOfPointsList[posIndex]; + posIndex++; + } + } + + } + else if (insertionDistance < 0.0f && posIndex != 0) + { + insertionDistance = -insertionDistance; + float nearestDistance = (light_source.transform.position - sortedWorldVertices[posIndex - 1]).magnitude; + + if (insertionDistance <= nearestDistance) + { + light_source.transform.LookAt(sortedWorldVertices[posIndex + 1]); + light_source.transform.Translate(0.0f, 0.0f, -insertionDistance); + } + else + { + insertionDistance -= nearestDistance; + + while (true) + { + if (posIndex == 0) + { + light_source.transform.position = sortedWorldVertices[posIndex]; + virtul_endscope.transform.position = light_source.transform.position; + + break; + } + else if (insertionDistance < distanceOfPointsList[posIndex - 1]) + { + float tempRatio = insertionDistance / distanceOfPointsList[posIndex - 1]; + Vector3 tempVector = sortedWorldVertices[posIndex] - sortedWorldVertices[posIndex - 1]; + light_source.transform.position = new Vector3(sortedWorldVertices[posIndex].x - (tempVector.x * tempRatio), sortedWorldVertices[posIndex].y - (tempVector.y * tempRatio), sortedWorldVertices[posIndex].z - (tempVector.z * tempRatio)); + //virtualEndoscope.transform.LookAt(calcBarycenter(sortedWorldVertices, posIndex)); + light_source.transform.LookAt(sortedWorldVertices[posIndex]); + virtul_endscope.transform.position = light_source.transform.position; + + posIndex--; + + break; + } + + insertionDistance -= distanceOfPointsList[posIndex - 1]; + posIndex--; + } + } + } + } + } + + + Vector3 decodeStr2Vec3(string vec_str) + { + string[] xyz_str = vec_str.Split(','); + return new Vector3(float.Parse(xyz_str[0]), float.Parse(xyz_str[1]), float.Parse(xyz_str[2])); + } +} diff --git a/Assets/Scripts/Camera_udp.cs.meta b/Assets/Scripts/Camera_udp.cs.meta new file mode 100644 index 0000000..30d161d --- /dev/null +++ b/Assets/Scripts/Camera_udp.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1b8cf74834db2774598fc11346cf0901 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/load_setting.cs b/Assets/Scripts/load_setting.cs index 6b84543..0de7acd 100644 --- a/Assets/Scripts/load_setting.cs +++ b/Assets/Scripts/load_setting.cs @@ -1,36 +1,54 @@ -using System.Collections; -using System.IO; -using System.Collections.Generic; +//using System.Collections; +//using System.IO; +//using System.Collections.Generic; +//using UnityEngine; -using UnityEngine; +//[System.Serializable] +//public class Setting +//{ +// public float x_scale; +// public float y_scale; +// public float z_scale; +// public string start_point_vec; +// public string[] center_points_vec; +//} -[System.Serializable] -public class Setting -{ - public float x_scale; - public float y_scale; - public float z_scale; +//public class load_setting : MonoBehaviour +//{ +// public Vector3 start_point_vec; +// public List center_points_vec = new List(); +// public GameObject prefabOBJ; - public string[] points_str; -} +// // Start is called before the first frame update +// void Start() +// { +// string data_str = ""; +// var reader = new StreamReader(Application.dataPath + "/setting.json"); ; +// data_str = reader.ReadToEnd(); +// var inputJson = JsonUtility.FromJson(data_str); +// start_point_vec = decodeStr2Vec3(inputJson.center_points_vec[0]); -public class load_setting : MonoBehaviour -{ - // Start is called before the first frame update - void Start() - { - string data_str = ""; - StreamReader reader; - Debug.Log(Application.dataPath + "/setting.json"); - reader = new StreamReader(Application.dataPath + "/setting.json"); - data_str = reader.ReadToEnd(); - Setting inputJson = JsonUtility.FromJson(data_str); - Debug.Log(inputJson.x_scale); - } +// foreach(var points_str in inputJson.center_points_vec) +// { +// center_points_vec.Add(decodeStr2Vec3(points_str)); +// } +// var abs_model_pos = GameObject.Find("Patient").transform.position; +// Debug.Log(abs_model_pos); +// foreach(var center_vec in center_points_vec) +// { +// GameObject obj = Instantiate(prefabOBJ, center_vec, Quaternion.identity); +// } +// } - // Update is called once per frame - void Update() - { +// // Update is called once per frame +// void Update() +// { - } -} +// } + +// Vector3 decodeStr2Vec3(string vec_str) +// { +// string[] xyz_str = vec_str.Split(','); +// return new Vector3(float.Parse(xyz_str[0]), float.Parse(xyz_str[1]), float.Parse(xyz_str[2])); +// } +//} diff --git a/Assets/setting.json b/Assets/setting.json index df75825..1b61d4e 100644 --- a/Assets/setting.json +++ b/Assets/setting.json @@ -1,5 +1,56 @@ { "x_scale": 340.48, "y_scale": 340.48, - "z_scale": 605.0 + "z_scale": 605.0, + "start_point_vec": "-12.2661328125,10.941328125,-287.5", + "center_points_vec": [ + "-12.2661328125,10.941328125,-287.5", + "-12.769645182291674,9.398320312500001,-282.49999999999994", + "-13.273157552083347,7.8553125,-277.49999999999994", + "-13.776669921875001,6.3123046875,-272.5", + "-13.551539713541674,4.207337239583346,-267.49999999999994", + "-13.326409505208346,2.102369791666692,-262.49999999999994", + "-13.101279296875001,-0.00259765625,-257.5", + "-11.61931640625,-1.6780859375000001,-252.5", + "-10.137353515625001,-3.3535742187500004,-247.5", + "-9.681897786458347,-4.910436197916654,-242.5", + "-9.226442057291674,-6.4672981770833085,-237.5", + "-8.770986328125,-8.02416015625,-232.5", + "-8.778779296875001,-13.040234375,-227.5", + "-8.786572265625,-18.05630859375,-222.5", + "-9.184013671875,-18.849892578125,-217.5", + "-8.11767578125,-21.21159505208331,-212.5", + "-7.051337890625001,-23.573297526041657,-207.5", + "-5.985,-25.935000000000002,-202.5", + "-5.815719401041673,-28.70583333333331,-197.5", + "-5.646438802083346,-31.476666666666656,-192.5", + "-5.477158203125001,-34.2475,-187.5", + "-4.492646484375,-39.062255859375,-182.5", + "-1.430442708333346,-40.337272135416654,-177.49999999999994", + "1.6317610677083083,-41.61228841145831,-172.5", + "4.69396484375,-42.887304687500006,-167.5", + "5.549892578125,-43.439306640625006,-162.5", + "5.961621093750001,-44.093916015625005,-157.5", + "5.9492822265625005,-43.62425976562499,-152.5", + "5.936943359375,-43.15460351562503,-147.50000000000006", + "5.9246044921875,-42.68494726562502,-142.50000000000006", + "5.912265625000001,-42.21529101562505,-137.50000000000006", + "5.8999267578125005,-41.745634765625,-132.5", + "5.887587890625,-41.275978515625035,-127.50000000000007", + "5.8752490234375,-40.80632226562503,-122.50000000000007", + "5.86291015625,-40.33666601562502,-117.50000000000007", + "5.850571289062501,-39.867009765625006,-112.5", + "5.838232421875,-39.397353515625,-107.5", + "1.070234375,-35.28396484375,-102.5", + "-3.697763671875,-31.170576171875002,-97.5", + "-3.7055566406250002,-27.94720947265625,-92.5", + "-3.7133496093750002,-24.7238427734375,-87.5", + "-3.7211425781250003,-21.500476074218753,-82.5", + "-3.7289355468750003,-18.277109375000002,-77.5", + "-0.678854166666673,-12.974427083333309,-72.5", + "2.3712272135416543,-7.671744791666654,-67.5", + "5.42130859375,-2.3690625,-62.5", + "10.358154296875,1.14296875,-57.5", + "15.295000000000002,4.655,-52.5" + ] } \ No newline at end of file diff --git a/InitUnitySettings/DicomProcessor.py b/InitUnitySettings/DicomProcessor.py index 82990d9..fcb78b9 100644 --- a/InitUnitySettings/DicomProcessor.py +++ b/InitUnitySettings/DicomProcessor.py @@ -47,6 +47,8 @@ self.pixel_spacing_dx, self.pixel_spacing_dy = sample_dfile.PixelSpacing self.slice_spacing = abs(sample_dfile.ImagePositionPatient[2] - sample_dfile2.ImagePositionPatient[2]) + self.scaling_f = np.array([self.pixel_spacing_dx, self.pixel_spacing_dy, self.slice_spacing]) + self.model_center_vec = np.array([self.dicom_row_num / 2, self.dicom_col_num / 2, self.dicom_size / 2]) self.image_position_patient = sample_dfile.ImagePositionPatient #なぜか3DSlicer上ではSが-0.25されている # self.image_position_patient[2] -= 2.5 @@ -116,8 +118,10 @@ return CT_img - def adapt_window(self, array, window_low, window_high): - return np.clip(array, window_low, window_high) + def ijk2relative_vec(self, ijk_coords): + relative_vec = ijk_coords - self.model_center_vec + relative_vec[1], relative_vec[2] = -relative_vec[1], -relative_vec[2] + return relative_vec * self.scaling_f def get_CT_by_index(self, dicom_index): # print(self.dicom_file_list[dicom_index]) diff --git a/InitUnitySettings/GUI.py b/InitUnitySettings/GUI.py index a160b46..23d64b9 100644 --- a/InitUnitySettings/GUI.py +++ b/InitUnitySettings/GUI.py @@ -1,4 +1,6 @@ import numpy as np +import os.path as osp +import json import cv2 from DicomProcessor import DicomProcessor @@ -134,10 +136,20 @@ # TODO startpointがNoneの時に警告を出す機能を入れる elif event == cv2.EVENT_LBUTTONUP and ((2 * self.button_width) < x and x < (3 * self.button_width)) and y <= self.button_height: print("click export") - # 1.中心からの距離からを計算 - self.Is_continue = False - self.setting_json["start_point"] = self.encode_array2str(self.start_point) - print('exit') + if self.start_point is not None: + # 1.中心からの距離からを計算 + self.Is_continue = False + self.setting_json["start_point_vec"] = self.encode_array2str(self.dicom_processor.ijk2relative_vec(self.start_point)) + + self.setting_json["center_points_vec"] = [] + pad_ijk_centers = [x for x in self.dicom_processor.linear_pad_list(self.ijk_eso_centers) if x is not None] + for points in pad_ijk_centers: + self.setting_json["center_points_vec"].append(self.encode_array2str(self.dicom_processor.ijk2relative_vec(points))) + with open(osp.join(self.args.output_dir, "setting.json"), "w") as f: + json.dump(self.setting_json, f, indent=4) + print('exit') + else: + print("start pointが選ばれていません") # click wc elif event == cv2.EVENT_LBUTTONUP and x < self.wc_width and self.button_height < y and not self.window_button_state["pushed_wc"]: