Verified Commit a58dae6b authored by Sebastian Höffner's avatar Sebastian Höffner
Browse files

Enriching context with bounding boxes for elements and containers. Refactoring...

Enriching context with bounding boxes for elements and containers. Refactoring Editor/Behaviour for SceneController.
parent 8c6f2e56
This diff is collapsed.
......@@ -7477,79 +7477,6 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 5fc6ba51380c1458f9ecd3a8b7030dd5, type: 3}
m_Name:
m_EditorClassIdentifier:
instructionJson: instruction.json
contextJson: context.json
semanticBehaviours:
- {fileID: 1232511662}
- {fileID: 72409653}
- {fileID: 206085099}
- {fileID: 233609476}
- {fileID: 266244855}
- {fileID: 354212941}
- {fileID: 385145545}
- {fileID: 386471116}
- {fileID: 403154016}
- {fileID: 486434704}
- {fileID: 491616699}
- {fileID: 562335846}
- {fileID: 562335848}
- {fileID: 591176980}
- {fileID: 673992305}
- {fileID: 673992307}
- {fileID: 679120270}
- {fileID: 689074821}
- {fileID: 705507997}
- {fileID: 727899287}
- {fileID: 727899291}
- {fileID: 747210191}
- {fileID: 795689513}
- {fileID: 851663884}
- {fileID: 853770464}
- {fileID: 868937600}
- {fileID: 881902528}
- {fileID: 956961677}
- {fileID: 964251524}
- {fileID: 1008622060}
- {fileID: 1024849836}
- {fileID: 1026241613}
- {fileID: 1052582938}
- {fileID: 1137338309}
- {fileID: 1139129198}
- {fileID: 1139129200}
- {fileID: 1139129202}
- {fileID: 1140816810}
- {fileID: 1140816812}
- {fileID: 1166413353}
- {fileID: 1217411874}
- {fileID: 1238782161}
- {fileID: 1307777420}
- {fileID: 1331286835}
- {fileID: 1331286837}
- {fileID: 1403195200}
- {fileID: 1425282731}
- {fileID: 1444289940}
- {fileID: 1456574796}
- {fileID: 1581413263}
- {fileID: 1656686361}
- {fileID: 1724232370}
- {fileID: 1767828230}
- {fileID: 1808417845}
- {fileID: 1845090567}
- {fileID: 1887977818}
- {fileID: 1903869702}
- {fileID: 1918559639}
- {fileID: 1928384764}
- {fileID: 1963336214}
- {fileID: 1977428286}
- {fileID: 2002183462}
- {fileID: 2012739341}
- {fileID: 2072907047}
- {fileID: 2072914415}
- {fileID: 2109312688}
- {fileID: 2125088164}
- {fileID: 2138028496}
- {fileID: 2138746744}
- {fileID: 8529134863782672057}
--- !u!4 &1309245142
Transform:
m_ObjectHideFlags: 0
......
......@@ -3,6 +3,7 @@ using UnityEditor;
using System.Linq;
using SimpleJSON;
using System.IO;
using System.Collections.Generic;
namespace dlu
{
......@@ -10,20 +11,32 @@ namespace dlu
[CustomEditor(typeof(SceneController))]
public class SceneControllerEditor : Editor
{
private bool showLabels;
private bool exportContext;
private bool showLabels = false;
public SemanticBehaviour[] semanticBehaviours;
private JSONArray sceneJson;
private JSONArray instructionJson;
public SceneControllerEditor()
{
JSONNode.VectorContainerType = JSONContainerType.Object;
JSONNode.QuaternionContainerType = JSONContainerType.Object;
}
public override void OnInspectorGUI()
{
SceneController sc = (SceneController) target;
base.DrawDefaultInspector();
this.ImportInstruction(sc);
EditorGUILayout.Space();
this.ExportContext(sc);
EditorGUILayout.Space();
this.ShowSemanticLabels(sc);
}
private void ShowSemanticLabels(SceneController sc)
......@@ -32,12 +45,12 @@ public class SceneControllerEditor : Editor
if (showLabels)
{
if (sc.semanticBehaviours == null || GUILayout.Button("Refresh list of semantic labels"))
if (this.semanticBehaviours == null || GUILayout.Button("Refresh list of semantic labels"))
{
sc.FindSemanticLabels();
FindSemanticLabels();
}
foreach (SemanticBehaviour sb in sc.semanticBehaviours)
foreach (SemanticBehaviour sb in this.semanticBehaviours)
{
string label = sb.GetComponent<InstanceIdentifier>().name;
if (sb.is_a.Count() > 0)
......@@ -53,23 +66,105 @@ public class SceneControllerEditor : Editor
private void ExportContext(SceneController sc)
{
exportContext = EditorGUILayout.BeginFoldoutHeaderGroup(exportContext, "Export context");
if (exportContext)
{
if (sceneJson == null || GUILayout.Button("Refresh scene JSON"))
string exportFile = EditorGUILayout.TextField("Export Context File", "Assets/DLU/Resources/context.json");
if (sceneJson == null)
{
sceneJson = sc.GenerateContextJSON();
sceneJson = this.GenerateContextJSON();
}
EditorGUILayout.TextArea(sceneJson.ToString());
string exportFile = EditorGUILayout.TextField("Export file", "Assets/DLU/Resources/context.json");
if (GUILayout.Button("Export to file"))
{
File.WriteAllText(exportFile, sceneJson.ToString());
}
EditorGUILayout.TextArea(sceneJson == null ? "" : sceneJson.ToString());
}
EditorGUILayout.EndFoldoutHeaderGroup();
private void ImportInstruction(SceneController sc)
{
if (sc.instruction == null || GUILayout.Button("Import instruction JSON"))
{
sc.instruction = sc.LoadInstruction(sc.importInstructionFile);
}
EditorGUILayout.TextArea(sc.instruction == null ? "" : sc.instruction.ToString());
}
public void FindSemanticLabels()
{
this.semanticBehaviours = GameObject.FindObjectsOfType<SemanticBehaviour>();
}
public JSONArray GenerateContextJSON()
{
if (this.semanticBehaviours == null)
{
FindSemanticLabels();
}
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;
}
}
return sceneArray;
}
private void Write(JSONObject jsonObject, Entity entity)
{
JSONObject transform = new JSONObject();
transform["position"] = entity.transform.position;
transform["orientation"] = entity.transform.rotation;
jsonObject["components"]["transform"] = transform;
MeshFilter meshFilter = entity.GetComponent<MeshFilter>();
if (meshFilter != null)
{
Mesh mesh = meshFilter.sharedMesh;
JSONObject bounds = new JSONObject();
bounds["center"] = mesh.bounds.center;
bounds["extents"] = mesh.bounds.extents;
bounds["size"] = mesh.bounds.size;
jsonObject["components"]["bounds"] = bounds;
}
}
private 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;
}
}
......
using UnityEngine;
using SimpleJSON;
using System.Collections.Generic;
using System.IO;
namespace dlu
{
public class SceneController : MonoBehaviour
{
public string instructionJson = "instruction.json";
public string contextJson = "context.json";
[HideInInspector]
public SemanticBehaviour[] semanticBehaviours;
public SceneController()
{
JSONNode.VectorContainerType = JSONContainerType.Object;
JSONNode.QuaternionContainerType = JSONContainerType.Object;
}
public JSONArray instruction = null;
public string importInstructionFile = "Assets/DLU/Resources/instruction.json";
public void Start()
{
if (semanticBehaviours == null)
{
FindSemanticLabels();
}
// string jsonContent = Resources.Load<TextAsset>(instructionJson).text;
// JSONArray scene = JSON.Parse(jsonContent) as JSONArray;
// foreach (JSONObject obj in scene)
// {
// SetGameObjectState(obj);
// }
}
// private void SetGameObjectState(JSONObject sceneObject)
// {
// string name = sceneObject["name"];
// // Debug.Log($"name {name}");
// GameObject go = GameObject.Find($"Objects/{name}");
// if (go != null) {
// // Debug.Log(go);
// }
// }
public void FindSemanticLabels()
{
this.semanticBehaviours = GameObject.FindObjectsOfType<SemanticBehaviour>();
}
public JSONArray GenerateContextJSON()
{
if (this.semanticBehaviours == null)
if (this.instruction == null)
{
FindSemanticLabels();
}
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: Handle(jsonObject, entity); break;
default:
Debug.LogWarning($"No Handler for SemanticBehaviour \"{sb.GetType().Name}\"");
break;
}
this.instruction = LoadInstruction(this.importInstructionFile);
}
return sceneArray;
}
private void Handle(JSONObject jsonObject, Entity entity)
public JSONArray LoadInstruction(string json)
{
JSONObject transform = new JSONObject();
transform["position"] = entity.transform.position;
transform["orientation"] = entity.transform.rotation;
jsonObject["components"]["transform"] = transform;
return JSON.Parse(File.ReadAllText(json)) as JSONArray;
}
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment