Browse Source

implement logging csv

Kenkart 2 years ago
parent
commit
dbc5b26f9d

+ 203 - 203
Assets/Scenes/Main.unity

@@ -827,6 +827,41 @@ PrefabInstance:
       objectReference: {fileID: 2100000, guid: f38c3a4df1a13aa43aa34ed48cb326f7, type: 2}
     m_RemovedComponents: []
   m_SourcePrefab: {fileID: 100100000, guid: c90faeda1ce307e418ee73344aa5e431, type: 3}
+--- !u!21 &74447604
+Material:
+  serializedVersion: 6
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_Name: Sprites/Default
+  m_Shader: {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
+  m_ShaderKeywords: 
+  m_LightmapFlags: 4
+  m_EnableInstancingVariants: 0
+  m_DoubleSidedGI: 0
+  m_CustomRenderQueue: -1
+  stringTagMap: {}
+  disabledShaderPasses: []
+  m_SavedProperties:
+    serializedVersion: 3
+    m_TexEnvs:
+    - _AlphaTex:
+        m_Texture: {fileID: 0}
+        m_Scale: {x: 1, y: 1}
+        m_Offset: {x: 0, y: 0}
+    - _MainTex:
+        m_Texture: {fileID: 0}
+        m_Scale: {x: 1, y: 1}
+        m_Offset: {x: 0, y: 0}
+    m_Floats:
+    - PixelSnap: 0
+    - _EnableExternalAlpha: 0
+    m_Colors:
+    - _Color: {r: 1, g: 1, b: 1, a: 1}
+    - _Flip: {r: 1, g: 1, b: 1, a: 1}
+    - _RendererColor: {r: 1, g: 1, b: 1, a: 1}
+  m_BuildTextureStacks: []
 --- !u!4 &110068039 stripped
 Transform:
   m_CorrespondingSourceObject: {fileID: 5672364630614476697, guid: a8d819c88b06d5d479cad532c162c475, type: 3}
@@ -906,11 +941,11 @@ PrefabInstance:
     - target: {fileID: 2348914, guid: 4d293c8e162f3874b982baadd71153d2, type: 3}
       propertyPath: m_Materials.Array.data[0]
       value: 
-      objectReference: {fileID: 404286851}
+      objectReference: {fileID: 74447604}
     - target: {fileID: 3380982, guid: 4d293c8e162f3874b982baadd71153d2, type: 3}
       propertyPath: m_Mesh
       value: 
-      objectReference: {fileID: 1934167013}
+      objectReference: {fileID: 644281035}
     m_RemovedComponents: []
   m_SourcePrefab: {fileID: 100100000, guid: 4d293c8e162f3874b982baadd71153d2, type: 3}
 --- !u!4 &110216683 stripped
@@ -1886,6 +1921,7 @@ MonoBehaviour:
   m_Name: 
   m_EditorClassIdentifier: 
   bsv: {fileID: 189736995}
+  viveInput: {fileID: 573329779}
 --- !u!4 &336955517
 Transform:
   m_ObjectHideFlags: 0
@@ -2044,41 +2080,6 @@ Transform:
   m_CorrespondingSourceObject: {fileID: 5675013305860159573, guid: 2f3fe288da751a2428fc82d8e5ed9f7e, type: 3}
   m_PrefabInstance: {fileID: 1558668310}
   m_PrefabAsset: {fileID: 0}
---- !u!21 &404286851
-Material:
-  serializedVersion: 6
-  m_ObjectHideFlags: 0
-  m_CorrespondingSourceObject: {fileID: 0}
-  m_PrefabInstance: {fileID: 0}
-  m_PrefabAsset: {fileID: 0}
-  m_Name: Sprites/Default
-  m_Shader: {fileID: 10753, guid: 0000000000000000f000000000000000, type: 0}
-  m_ShaderKeywords: 
-  m_LightmapFlags: 4
-  m_EnableInstancingVariants: 0
-  m_DoubleSidedGI: 0
-  m_CustomRenderQueue: -1
-  stringTagMap: {}
-  disabledShaderPasses: []
-  m_SavedProperties:
-    serializedVersion: 3
-    m_TexEnvs:
-    - _AlphaTex:
-        m_Texture: {fileID: 0}
-        m_Scale: {x: 1, y: 1}
-        m_Offset: {x: 0, y: 0}
-    - _MainTex:
-        m_Texture: {fileID: 0}
-        m_Scale: {x: 1, y: 1}
-        m_Offset: {x: 0, y: 0}
-    m_Floats:
-    - PixelSnap: 0
-    - _EnableExternalAlpha: 0
-    m_Colors:
-    - _Color: {r: 1, g: 1, b: 1, a: 1}
-    - _Flip: {r: 1, g: 1, b: 1, a: 1}
-    - _RendererColor: {r: 1, g: 1, b: 1, a: 1}
-  m_BuildTextureStacks: []
 --- !u!1001 &422535275
 PrefabInstance:
   m_ObjectHideFlags: 0
@@ -2650,6 +2651,7 @@ MonoBehaviour:
   m_EditorClassIdentifier: 
   playerReplay: {fileID: 65057765}
   bsv: {fileID: 189736995}
+  csvTime: 0
 --- !u!4 &573329780
 Transform:
   m_ObjectHideFlags: 0
@@ -2793,6 +2795,170 @@ PrefabInstance:
       objectReference: {fileID: 0}
     m_RemovedComponents: []
   m_SourcePrefab: {fileID: 100100000, guid: 3f4814451272e874f9d7dc1534b22b63, type: 3}
+--- !u!43 &644281035
+Mesh:
+  m_ObjectHideFlags: 0
+  m_CorrespondingSourceObject: {fileID: 0}
+  m_PrefabInstance: {fileID: 0}
+  m_PrefabAsset: {fileID: 0}
+  m_Name: 
+  serializedVersion: 10
+  m_SubMeshes:
+  - serializedVersion: 2
+    firstByte: 0
+    indexCount: 24
+    topology: 0
+    baseVertex: 0
+    firstVertex: 0
+    vertexCount: 8
+    localAABB:
+      m_Center: {x: 0, y: 0.01, z: 0}
+      m_Extent: {x: 1.65, y: 0, z: 1.275}
+  m_Shapes:
+    vertices: []
+    shapes: []
+    channels: []
+    fullWeights: []
+  m_BindPose: []
+  m_BoneNameHashes: 
+  m_RootBoneNameHash: 0
+  m_BonesAABB: []
+  m_VariableBoneCountWeights:
+    m_Data: 
+  m_MeshCompression: 0
+  m_IsReadable: 1
+  m_KeepVertices: 1
+  m_KeepIndices: 1
+  m_IndexFormat: 0
+  m_IndexBuffer: 000004000100010004000500010005000200020005000600020006000300030006000700030007000000000007000400
+  m_VertexData:
+    serializedVersion: 3
+    m_VertexCount: 8
+    m_Channels:
+    - stream: 0
+      offset: 0
+      format: 0
+      dimension: 3
+    - stream: 0
+      offset: 0
+      format: 0
+      dimension: 0
+    - stream: 0
+      offset: 0
+      format: 0
+      dimension: 0
+    - stream: 0
+      offset: 12
+      format: 0
+      dimension: 4
+    - stream: 0
+      offset: 28
+      format: 0
+      dimension: 2
+    - stream: 0
+      offset: 0
+      format: 0
+      dimension: 0
+    - stream: 0
+      offset: 0
+      format: 0
+      dimension: 0
+    - stream: 0
+      offset: 0
+      format: 0
+      dimension: 0
+    - stream: 0
+      offset: 0
+      format: 0
+      dimension: 0
+    - stream: 0
+      offset: 0
+      format: 0
+      dimension: 0
+    - stream: 0
+      offset: 0
+      format: 0
+      dimension: 0
+    - stream: 0
+      offset: 0
+      format: 0
+      dimension: 0
+    - stream: 0
+      offset: 0
+      format: 0
+      dimension: 0
+    - stream: 0
+      offset: 0
+      format: 0
+      dimension: 0
+    m_DataSize: 288
+    _typelessdata: 0000c03f0ad7233c000090bf000000000000803f0000803f0000803f00000000000000000000c0bf0ad7233c000090bf000000000000803f0000803f0000803f0000803f000000000000c0bf0ad7233c0000903f000000000000803f0000803f0000803f00000000000000000000c03f0ad7233c0000903f000000000000803f0000803f0000803f0000803f000000003333d33f0ad7233c3333a3bf000000000000803f0000803f00000000000000000000803f3333d3bf0ad7233c3333a3bf000000000000803f0000803f000000000000803f0000803f3333d3bf0ad7233c3333a33f000000000000803f0000803f00000000000000000000803f3333d33f0ad7233c3333a33f000000000000803f0000803f000000000000803f0000803f
+  m_CompressedMesh:
+    m_Vertices:
+      m_NumItems: 0
+      m_Range: 0
+      m_Start: 0
+      m_Data: 
+      m_BitSize: 0
+    m_UV:
+      m_NumItems: 0
+      m_Range: 0
+      m_Start: 0
+      m_Data: 
+      m_BitSize: 0
+    m_Normals:
+      m_NumItems: 0
+      m_Range: 0
+      m_Start: 0
+      m_Data: 
+      m_BitSize: 0
+    m_Tangents:
+      m_NumItems: 0
+      m_Range: 0
+      m_Start: 0
+      m_Data: 
+      m_BitSize: 0
+    m_Weights:
+      m_NumItems: 0
+      m_Data: 
+      m_BitSize: 0
+    m_NormalSigns:
+      m_NumItems: 0
+      m_Data: 
+      m_BitSize: 0
+    m_TangentSigns:
+      m_NumItems: 0
+      m_Data: 
+      m_BitSize: 0
+    m_FloatColors:
+      m_NumItems: 0
+      m_Range: 0
+      m_Start: 0
+      m_Data: 
+      m_BitSize: 0
+    m_BoneIndices:
+      m_NumItems: 0
+      m_Data: 
+      m_BitSize: 0
+    m_Triangles:
+      m_NumItems: 0
+      m_Data: 
+      m_BitSize: 0
+    m_UVInfo: 0
+  m_LocalAABB:
+    m_Center: {x: 0, y: 0.01, z: 0}
+    m_Extent: {x: 1.65, y: 0, z: 1.275}
+  m_MeshUsageFlags: 0
+  m_BakedConvexCollisionMesh: 
+  m_BakedTriangleCollisionMesh: 
+  m_MeshMetrics[0]: 1
+  m_MeshMetrics[1]: 1
+  m_MeshOptimizationFlags: 1
+  m_StreamData:
+    serializedVersion: 2
+    offset: 0
+    size: 0
+    path: 
 --- !u!4 &644337836 stripped
 Transform:
   m_CorrespondingSourceObject: {fileID: 533855062970376285, guid: 01416d383f6bdc14e811adfc51255eaa, type: 3}
@@ -5398,8 +5564,6 @@ MonoBehaviour:
   boneMaterial: {fileID: 2100000, guid: f2ea145c63353784985576f08398a815, type: 2}
   transparentMat: {fileID: 2100000, guid: 803645c956e5ae64a9b99ae13a11cd41, type: 2}
   bsv: {fileID: 189736995}
-  wristLeftTriggered: 0
-  wristRightTriggered: 0
 --- !u!4 &1143995018
 Transform:
   m_ObjectHideFlags: 0
@@ -8728,170 +8892,6 @@ PrefabInstance:
       objectReference: {fileID: 0}
     m_RemovedComponents: []
   m_SourcePrefab: {fileID: 100100000, guid: a8d819c88b06d5d479cad532c162c475, type: 3}
---- !u!43 &1934167013
-Mesh:
-  m_ObjectHideFlags: 0
-  m_CorrespondingSourceObject: {fileID: 0}
-  m_PrefabInstance: {fileID: 0}
-  m_PrefabAsset: {fileID: 0}
-  m_Name: 
-  serializedVersion: 10
-  m_SubMeshes:
-  - serializedVersion: 2
-    firstByte: 0
-    indexCount: 24
-    topology: 0
-    baseVertex: 0
-    firstVertex: 0
-    vertexCount: 8
-    localAABB:
-      m_Center: {x: 0, y: 0.01, z: 0}
-      m_Extent: {x: 1.65, y: 0, z: 1.275}
-  m_Shapes:
-    vertices: []
-    shapes: []
-    channels: []
-    fullWeights: []
-  m_BindPose: []
-  m_BoneNameHashes: 
-  m_RootBoneNameHash: 0
-  m_BonesAABB: []
-  m_VariableBoneCountWeights:
-    m_Data: 
-  m_MeshCompression: 0
-  m_IsReadable: 1
-  m_KeepVertices: 1
-  m_KeepIndices: 1
-  m_IndexFormat: 0
-  m_IndexBuffer: 000004000100010004000500010005000200020005000600020006000300030006000700030007000000000007000400
-  m_VertexData:
-    serializedVersion: 3
-    m_VertexCount: 8
-    m_Channels:
-    - stream: 0
-      offset: 0
-      format: 0
-      dimension: 3
-    - stream: 0
-      offset: 0
-      format: 0
-      dimension: 0
-    - stream: 0
-      offset: 0
-      format: 0
-      dimension: 0
-    - stream: 0
-      offset: 12
-      format: 0
-      dimension: 4
-    - stream: 0
-      offset: 28
-      format: 0
-      dimension: 2
-    - stream: 0
-      offset: 0
-      format: 0
-      dimension: 0
-    - stream: 0
-      offset: 0
-      format: 0
-      dimension: 0
-    - stream: 0
-      offset: 0
-      format: 0
-      dimension: 0
-    - stream: 0
-      offset: 0
-      format: 0
-      dimension: 0
-    - stream: 0
-      offset: 0
-      format: 0
-      dimension: 0
-    - stream: 0
-      offset: 0
-      format: 0
-      dimension: 0
-    - stream: 0
-      offset: 0
-      format: 0
-      dimension: 0
-    - stream: 0
-      offset: 0
-      format: 0
-      dimension: 0
-    - stream: 0
-      offset: 0
-      format: 0
-      dimension: 0
-    m_DataSize: 288
-    _typelessdata: 0000c03f0ad7233c000090bf000000000000803f0000803f0000803f00000000000000000000c0bf0ad7233c000090bf000000000000803f0000803f0000803f0000803f000000000000c0bf0ad7233c0000903f000000000000803f0000803f0000803f00000000000000000000c03f0ad7233c0000903f000000000000803f0000803f0000803f0000803f000000003333d33f0ad7233c3333a3bf000000000000803f0000803f00000000000000000000803f3333d3bf0ad7233c3333a3bf000000000000803f0000803f000000000000803f0000803f3333d3bf0ad7233c3333a33f000000000000803f0000803f00000000000000000000803f3333d33f0ad7233c3333a33f000000000000803f0000803f000000000000803f0000803f
-  m_CompressedMesh:
-    m_Vertices:
-      m_NumItems: 0
-      m_Range: 0
-      m_Start: 0
-      m_Data: 
-      m_BitSize: 0
-    m_UV:
-      m_NumItems: 0
-      m_Range: 0
-      m_Start: 0
-      m_Data: 
-      m_BitSize: 0
-    m_Normals:
-      m_NumItems: 0
-      m_Range: 0
-      m_Start: 0
-      m_Data: 
-      m_BitSize: 0
-    m_Tangents:
-      m_NumItems: 0
-      m_Range: 0
-      m_Start: 0
-      m_Data: 
-      m_BitSize: 0
-    m_Weights:
-      m_NumItems: 0
-      m_Data: 
-      m_BitSize: 0
-    m_NormalSigns:
-      m_NumItems: 0
-      m_Data: 
-      m_BitSize: 0
-    m_TangentSigns:
-      m_NumItems: 0
-      m_Data: 
-      m_BitSize: 0
-    m_FloatColors:
-      m_NumItems: 0
-      m_Range: 0
-      m_Start: 0
-      m_Data: 
-      m_BitSize: 0
-    m_BoneIndices:
-      m_NumItems: 0
-      m_Data: 
-      m_BitSize: 0
-    m_Triangles:
-      m_NumItems: 0
-      m_Data: 
-      m_BitSize: 0
-    m_UVInfo: 0
-  m_LocalAABB:
-    m_Center: {x: 0, y: 0.01, z: 0}
-    m_Extent: {x: 1.65, y: 0, z: 1.275}
-  m_MeshUsageFlags: 0
-  m_BakedConvexCollisionMesh: 
-  m_BakedTriangleCollisionMesh: 
-  m_MeshMetrics[0]: 1
-  m_MeshMetrics[1]: 1
-  m_MeshOptimizationFlags: 1
-  m_StreamData:
-    serializedVersion: 2
-    offset: 0
-    size: 0
-    path: 
 --- !u!1001 &1937750070
 PrefabInstance:
   m_ObjectHideFlags: 0

+ 37 - 5
Assets/Scripts/BodyComparer.cs

@@ -7,10 +7,26 @@ using Valve.VR;
 public class BodyComparer : MonoBehaviour
 {
     public BodySourceView bsv;
+    public ViveInput viveInput;
 
     private List<JointsData> jointsDataDemo = new List<JointsData>();
     private List<float> recordingTimesDemo = new List<float>();
 
+    // For saving to CSV
+    private List<List<Vector3>> posJointsDemos = new List<List<Vector3>>();
+    private List<List<Vector3>> posJointsBodys = new List<List<Vector3>>();
+    private List<float> csvTimes = new List<float>();
+
+    // TODO: Testing (delete later)
+    private void Update()
+    {
+        if (Input.GetKeyDown(KeyCode.S))
+        {
+            Debug.Log("Save to CSV");
+            CSVWriter.WriteCSV(posJointsDemos, posJointsBodys, csvTimes);
+        }
+    }
+
     public void SetDataDemo(List<JointsData> jointsDataDemo, List<float> recordingTimesDemo)
     {
         this.jointsDataDemo = jointsDataDemo;
@@ -19,9 +35,14 @@ public class BodyComparer : MonoBehaviour
 
     public IEnumerator StartCompare()
     {
+        if (bsv.body == null)
+            yield break;
+
         for (int i = 0; i < jointsDataDemo.Count; i++)
         {
             Transform body = bsv.body.transform;
+            List<Vector3> oneBodyDemo = new List<Vector3>();
+            List<Vector3> oneBodyReal = new List<Vector3>();
 
             for (Kinect.JointType jt = Kinect.JointType.SpineBase; jt <= Kinect.JointType.ThumbRight; jt++)
             {
@@ -31,16 +52,18 @@ public class BodyComparer : MonoBehaviour
                         || jt == Kinect.JointType.HandTipLeft || jt == Kinect.JointType.HandTipRight)
                     continue;
 
-
                 JointsData jd = jointsDataDemo[i];
                 Vector3 posJointDemo = new Vector3(jd.jointsPositionsX[(int)jt], jd.jointsPositionsY[(int)jt], jd.jointsPositionsZ[(int)jt]);
+                Vector3 posJointBody = body.GetChild((int)jt).position;
+
+                // Save positions demo and real body
+                oneBodyDemo.Add(posJointDemo);
+                oneBodyReal.Add(posJointBody);
 
-                //float distance = (joints[index].position - posJointDemo).magnitude;
-                float distance = (body.GetChild((int)jt).position - posJointDemo).magnitude;
+                float distance = (posJointBody - posJointDemo).magnitude;
                 
                 if (distance > 0.15f)
                 {
-                    //joints[index].GetComponent<Renderer>().material.color = Color.red;
                     body.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.red;
 
                     // TODO: Haptic feedback
@@ -49,11 +72,15 @@ public class BodyComparer : MonoBehaviour
                 }
                 else
                 {
-                    //joints[index].GetComponent<Renderer>().material.color = Color.white;
                     body.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
                 }
             }
 
+            // Save it to csv later
+            posJointsDemos.Add(oneBodyDemo);
+            posJointsBodys.Add(oneBodyReal);
+            csvTimes.Add(viveInput.csvTime);
+
             float waitTime = 0.01f;
             if (i < recordingTimesDemo.Count - 1)
             {
@@ -76,4 +103,9 @@ public class BodyComparer : MonoBehaviour
             bodyEnd.GetChild((int)jt).GetComponent<Renderer>().material.color = Color.white;
         }
      }
+
+    public void WriteCSV()
+    {
+        CSVWriter.WriteCSV(posJointsDemos, posJointsBodys, csvTimes);
+    }
 }

+ 52 - 0
Assets/Scripts/CSVWriter.cs

@@ -0,0 +1,52 @@
+using System.Collections;
+using System.Collections.Generic;
+using System.IO;
+using UnityEngine;
+
+public static class CSVWriter
+{
+    private static string path = Application.dataPath + "/demo_and_body_positions.csv";
+
+    public static void WriteCSV(List<List<Vector3>> posJointsDemos, List<List<Vector3>> posJointsBodys, List<float> csvTimes)
+    {
+        string[] jointsName = { "SpineBase", "SpineMid", "Neck", "ShoulderLeft", "ElbowLeft", "WristLeft", "ShoulderRight",
+        "ElbowRight", "WristRight", "HipLeft", "KneeLeft", "AnkleLeft", "FootLeft", "HipRight", "KneeRight", "AnkleRight",
+        "FootRight", "SpineShoulder" };
+
+        string demos = "";
+        string bodys = "";
+
+        foreach(string name in jointsName)
+        {
+            demos += "Demo_" + name + ",";
+            bodys += "Body_" + name + ",";
+        }
+
+
+        StreamWriter sw = new StreamWriter(path);
+        sw.WriteLine(demos + bodys + ",time");
+
+        for (int i = 0; i < posJointsDemos.Count; i++)
+        {
+            List<Vector3> demo = posJointsDemos[i];
+            List<Vector3> body = posJointsDemos[i];
+
+            string d = "";
+            string b = "";
+
+            for(int j = 0; i < demo.Count; j++)
+            {
+                d += Vector3ToString(demo[j]) + ",";
+                b += Vector3ToString(body[j]) + ",";
+            }
+
+            sw.WriteLine(b + d + csvTimes[i]);
+        }
+        sw.Close();
+    }
+
+    private static string Vector3ToString(Vector3 vector)
+    {
+        return vector.x.ToString() + ";" + vector.y.ToString() + ";" + vector.z.ToString();
+    }
+}

+ 11 - 0
Assets/Scripts/CSVWriter.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 2d13fde8f39bf564aaf2b4508ddb52b0
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: 

+ 5 - 0
Assets/Scripts/PlayerReplay.cs

@@ -337,5 +337,10 @@ public class PlayerReplay : MonoBehaviour
         textFinish.SetActive(true);
         yield return new WaitForSeconds(3);
         textFinish.SetActive(false);
+
+        // Write positions to csv
+        bc.WriteCSV();
+
+        ViveInput.StopPlaying();
     }
 }

+ 17 - 4
Assets/Scripts/ViveInput.cs

@@ -16,6 +16,9 @@ public class ViveInput : MonoBehaviour
     private bool isRecording;
     private float recordingTime;
 
+    private static bool isPlaying;
+    public float csvTime;
+
     private void Start()
     {
         grabPinch = SteamVR_Actions.default_GrabPinch;
@@ -46,9 +49,10 @@ public class ViveInput : MonoBehaviour
             }
         }
 
-        if (grabGrib.GetStateDown(SteamVR_Input_Sources.Any))
+        if (grabGrib.GetStateDown(SteamVR_Input_Sources.Any) && !isPlaying)
         {
             Debug.Log("Loading...");
+            isPlaying = true;
             playerReplay.Load();
         }
 
@@ -57,18 +61,27 @@ public class ViveInput : MonoBehaviour
             playerReplay.AddJoints(recordingTime);
             recordingTime += Time.deltaTime;
         }
+
+        if (isPlaying)
+        {
+            csvTime += Time.deltaTime;
+        }
     }
 
     public static IEnumerator WaitForControllerPress()
     {
-        bool done = false;
-        while (!done)
+        while (true)
         {
             if (SteamVR_Actions.default_GrabPinch.GetStateDown(SteamVR_Input_Sources.Any) || Input.GetKeyDown(KeyCode.Space))
             {
-                done = true;
+                break;
             }
             yield return null;
         }
     }
+
+    public static void StopPlaying()
+    {
+        isPlaying = false;
+    }
 }