JSONHandler.cs 5.05 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System.IO;
using System.Collections.Generic;
using SimpleJSON;
using UnityEngine;

namespace dlu
{

public class JSONHandler
{

    private JSONHandler()
    {
        // Note that these are static!
        JSONNode.VectorContainerType = JSONContainerType.Object;
        JSONNode.QuaternionContainerType = JSONContainerType.Object;
    }

    private static JSONHandler _instance = null;

    public static JSONHandler instance
    {
        get
        {
            if (JSONHandler._instance == null)
            {
                JSONHandler._instance = new JSONHandler();
                JSONHandler._instance.RefreshSemanticLabels();
            }
            return JSONHandler._instance;
        }
    }

    private Dictionary<string, GameObject> _sceneDict;
    public Dictionary<string, GameObject> sceneDict
    {
        get
        {
            return this._sceneDict;
        }

        private set
        {
            this._sceneDict = value;
        }
    }


    private SemanticBehaviour[] _semanticBehaviours;
    [HideInInspector] public SemanticBehaviour[] semanticBehaviours
    {
        get
        {
            return this._semanticBehaviours;
        }

        set
        {
            sceneDict = new Dictionary<string, GameObject>();
            foreach (SemanticBehaviour sb in value)
            {
                sceneDict[sb.GetComponent<InstanceIdentifier>().name] = sb.gameObject;
            }
            this._semanticBehaviours = value;
        }
    }

    public void RefreshSemanticLabels()
    {
        JSONHandler._instance.semanticBehaviours = GameObject.FindObjectsOfType<SemanticBehaviour>();
    }

    public JSONArray LoadInstruction(string json)
    {
        return JSON.Parse(File.ReadAllText(json)) as JSONArray;
    }

    public JSONNode GenerateContextJSON()
    {
        string screenShotPath = $"{Application.persistentDataPath}/context.png";
        ScreenCapture.CaptureScreenshot(screenShotPath, 3);

        Dictionary<string, JSONObject> sceneDict = new Dictionary<string, JSONObject>();

        JSONArray sceneArray = new JSONArray();
        foreach (SemanticBehaviour sb in this.semanticBehaviours)
        {
            JSONObject jsonObject = null;
            string name = sb.GetComponent<InstanceIdentifier>().name;

            if (!sceneDict.ContainsKey(name))
            {
                jsonObject = new JSONObject();
                jsonObject.Add("name", name);
                jsonObject.Add("is_a", new JSONArray());
                jsonObject.Add("components", new JSONObject());

                sceneDict[name] = jsonObject;
                sceneArray.Add(sceneDict[name]);
            }

            jsonObject = sceneDict[name];
            jsonObject["is_a"].Add(sb.is_a);

            switch (sb)
            {
                case Entity entity: Write(jsonObject, entity); break;
                case DesignedContainer designedContainer: Write(jsonObject, designedContainer); break;
                default:
                    Debug.LogWarning($"No Write handler for SemanticBehaviour \"{sb.GetType().Name}\"");
                    break;
            }
        }

        JSONObject metaObject = new JSONObject();
        metaObject.Add("image", screenShotPath);
        JSONObject contextObject = new JSONObject();
        contextObject.Add("meta", metaObject);
        contextObject.Add("context", sceneArray);
        return contextObject;
    }

    private static void Write(JSONObject jsonObject, Entity entity)
    {
        JSONObject transform = new JSONObject();
        transform["position"] = entity.transform.position;
        transform["orientation"] = entity.transform.rotation;
        jsonObject["components"]["transform"] = transform;

        Vector3 scale = entity.transform.lossyScale;
        MeshFilter meshFilter = entity.GetComponent<MeshFilter>();
        if (meshFilter != null)
        {
            Mesh mesh = meshFilter.sharedMesh;
            Vector3 boundsCenterWorld = mesh.bounds.center;
            boundsCenterWorld.Scale(scale);
            Vector3 boundsExtentsWorld = mesh.bounds.center;
            boundsExtentsWorld.Scale(scale);
            Vector3 boundsSizeWorld = mesh.bounds.center;
            boundsSizeWorld.Scale(scale);
            JSONObject bounds = new JSONObject();
            bounds["center"] = boundsCenterWorld;
            bounds["extents"] = boundsExtentsWorld;
            bounds["size"] = boundsSizeWorld;
            jsonObject["components"]["bounds"] = bounds;
        }
    }

    private static void Write(JSONObject jsonObject, DesignedContainer designedContainer)
    {
        JSONArray containment = new JSONArray();
        foreach (Collider collider in designedContainer.containmentParts)
        {
            JSONObject part = new JSONObject();
            part.Add("type", collider.GetType().Name);
            part.Add("center", collider.bounds.center);
            part.Add("size", collider.bounds.size);
            part.Add("extents", collider.bounds.extents);
            containment.Add(part);
        }
        jsonObject["components"]["containerVolume"] = containment;
    }
}

}