Skip to content

Commit

Permalink
Merge pull request #1040 from allenai/nanna-testing
Browse files Browse the repository at this point in the history
Procedural testing
  • Loading branch information
AlvaroHG committed Jul 26, 2022
2 parents 79e92c5 + c658efd commit 243aa90
Show file tree
Hide file tree
Showing 19 changed files with 1,393 additions and 78 deletions.
15 changes: 15 additions & 0 deletions ai2thor/tests/test_unity.py
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,17 @@ def test_change_resolution_image_synthesis(fifo_controller):
renderDepthImage=True,
renderSemanticSegmentation=True,
)
def depth_to_gray_rgb(data):
return (255.0 / data.max() * (data - data.min())).astype(np.uint8)

def save_image(name, image, flip_br=False):
import cv2
img = image
print("is none? {0} is none".format(img is None))
if flip_br:
img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
cv2.imwrite("{}.png".format(name), img)

fifo_controller.step("RotateRight")
fifo_controller.step("RotateLeft")
fifo_controller.step("RotateRight")
Expand All @@ -1139,6 +1150,10 @@ def test_change_resolution_image_synthesis(fifo_controller):
assert event.depth_frame.shape == (300, 300)
assert event.instance_segmentation_frame.shape == (300, 300, 3)
assert event.semantic_segmentation_frame.shape == (300, 300, 3)
print("is none? {0} is none other {1} ".format( event.depth_frame is None, first_depth_frame is None))
save_image("depth_after_resolution_change_300_300.png", depth_to_gray_rgb(event.depth_frame))
save_image("before_after_resolution_change_300_300.png", depth_to_gray_rgb(first_depth_frame))

assert np.allclose(event.depth_frame, first_depth_frame, atol=0.001)
assert np.array_equal(event.instance_segmentation_frame, first_instance_frame)
assert np.array_equal(event.semantic_segmentation_frame, first_sem_frame)
Expand Down
15 changes: 5 additions & 10 deletions unity/Assets/Scripts/BaseFPSAgentController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4623,26 +4623,21 @@ float allowedError
);
}

GameObject asset = assetMap.getAsset(assetId);
// GameObject asset = assetMap.getAsset(assetId);

var result = ProceduralTools.getHoleAssetBoundingBox(assetId);

var holeMetadata = asset.GetComponentInChildren<HoleMetadata>();
if (holeMetadata == null) {
if (result == null) {
actionFinished(
success: false,
errorMessage: $"Asset '{assetId}' does not have a HoleMetadata component, it's probably not a connector like a door or window or component has to be added in the prefab."
);

}
else {
var diff = holeMetadata.Max - holeMetadata.Min;

diff = new Vector3(Math.Abs(diff.x), Math.Abs(diff.y), Math.Abs(diff.z));// - holeMetadata.Margin;
// inverse offset for the asset
var min = new Vector3(holeMetadata.Min.x, -holeMetadata.Min.y, -holeMetadata.Min.z);
// var max = new Vector3(-holeMetadata.Max.x, holeMetadata.Max.y, holeMetadata.Max.z);
actionFinished(
success: false,
actionReturn: new BoundingBoxWithOffset() { min=Vector3.zero, max=diff, offset=min}
actionReturn: result
);
}
}
Expand Down
191 changes: 191 additions & 0 deletions unity/Assets/Scripts/DebugInputField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3951,6 +3951,197 @@ public class DebugInputField : MonoBehaviour {
f.WriteLine(prefabsStr);
break;
}
case "proc_arr": {
var arr = new int[][] {
new int[]{2, 2, 2, 2},
new int[]{2, 1, 1, 2},
new int[]{2, 1, 1, 2},
new int[]{2, 1, 1, 2},
new int[]{2, 2, 2, 2},
};




var layout = $@"
2 2 2 2
2 1 1 2
2 1 1 2
2 2 2 2
";


var doors = @"
2 = 2 2
2 = 1 2
2 1 1 2
2 2 2 2";

var objects = @"
2 2 2 2
2 1 1 2
2 * 1 2
2 2 2 +";
// var arr = new int[][] {
// new int[]{0, 0, 0},
// new int[]{0, 1, 0},
// new int[]{0, 0, 0}
// };

// 2: [ (0, 0),(0, 1),(0, 2),(0, 3),(1, 3),(2, 3),(3, 3) ]
// 1: [ (1, 1),(1, 2),(2, 2),(3, 2),(3, 1),(2, 1) ]

// 1: [ ((0, 0), (1, 1)),((1, 1), (1, 2)),((0, 0), (1, 2)),((1, 2), (2, 2)),((0, 0), (2, 2)),((2, 2), (2, 1)),((0, 0), (2, 1)),((2, 1), (1, 1)) ]

var house = Templates.createHouseFromTemplate(
new HouseTemplate() {
id = "house_0",
layout = $@"
0 0 0 0 0 0
0 2 2 2 2 0
0 2 2 2 2 0
0 1 1 1 1 0
0 1 1 1 1 0
0 0 0 0 0 0
",
objectsLayouts = new List<string>() {
$@"
0 0 0 0 0 0
0 2 2 2 2 0
0 2 2 2 = 0
0 1 1 1 = 0
0 1 * 1 + 0
0 0 0 0 0 0
"
,
$@"
0 0 0 0 0 0
0 2 2 2 2 0
0 2 2 2 2 0
0 1 1 1 1 0
0 1 1 1 $ 0
0 0 0 0 0 0
"
},
// layout = $@"
// 2 2 2 2 2
// 2 2 2 2 2
// 2 2 1 1 2
// 2 2 1 1 2
// 2 2 2 2 2
// ",
// objectsLayouts = new List<string>() {
// $@"
// 2 2 2 2 2
// 2 2 = 2 2
// 2 2 = 1 2
// 2 2 * 1 2
// 2 2 2 2 +
// "
// ,
// $@"
// 2 2 2 2 2
// 2 2 2 2 2
// 2 2 1 1 2
// 2 2 1 1 2
// 2 2 2 2 $
// "
// },
rooms = new Dictionary<string, RoomTemplate>() {
{"1", new RoomTemplate(){
wallTemplate = new Thor.Procedural.Data.PolygonWall() {
color = SerializableColor.fromUnityColor(Color.red),
unlit = true
},
floorTemplate = new Thor.Procedural.Data.RoomHierarchy() {
floorMaterial = "DarkWoodFloors",
roomType = "Bedroom"
},
wallHeight = 3.0f
}},
{"2", new RoomTemplate(){
wallTemplate = new Thor.Procedural.Data.PolygonWall() {
color = SerializableColor.fromUnityColor(Color.blue),
unlit = true
},
floorTemplate = new Thor.Procedural.Data.RoomHierarchy() {
floorMaterial = "RedBrick",
roomType = "LivingRoom"
},
wallHeight = 3.0f
}}
},
holes = new Dictionary<string, Thor.Procedural.Data.WallRectangularHole>() {
{"=", new Thor.Procedural.Data.Door(){
openness = 1.0f,
assetId = "Doorway_1",
room0 = "1"

}}
},
objects = new Dictionary<string, Thor.Procedural.Data.HouseObject>() {
{"*", new Thor.Procedural.Data.HouseObject(){
assetId = "Dining_Table_16_2",
rotation = new FlexibleRotation() { axis = new Vector3(0, 1, 0), degrees = 90}
}},
{"+", new Thor.Procedural.Data.HouseObject(){
assetId = "Chair_007_1"
}},
{"$", new Thor.Procedural.Data.HouseObject(){
assetId = "Apple_4",
position = new Vector3(0, 2, 0)
}}
},
proceduralParameters = new ProceduralParameters() {
ceilingMaterial = "ps_mat",
floorColliderThickness = 1.0f,
receptacleHeight = 0.7f,
skyboxId = "Sky1",
}
}

);

var temp = @"
{
'rooms': {
'1': {
'walls': {
'color': {
'r': 255,
'g': 0,
'b': 0
}
},
'floor': {
'floorMaterial': 'WoodGrain_Brown',
'roomType': 'LivingRoom'
}
}
}
}
";



var jsonResolver = new ShouldSerializeContractResolver();
var houseString = Newtonsoft.Json.JsonConvert.SerializeObject(
house,
Newtonsoft.Json.Formatting.None,
new Newtonsoft.Json.JsonSerializerSettings() {
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
ContractResolver = jsonResolver
}
);
Debug.Log("####### HOUSE\n" + houseString);

ProceduralTools.CreateHouse(house, ProceduralTools.GetMaterials());

Debug.Log("####### HOUSE Created \n");
break;
}
}

// StartCoroutine(CheckIfactionCompleteWasSetToTrueAfterWaitingALittleBit(splitcommand[0]));
Expand Down
51 changes: 51 additions & 0 deletions unity/Assets/Scripts/ProceduralData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
using System.Linq;
using UnityStandardAssets.Characters.FirstPerson;
using System;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using MessagePack.Resolvers;
using MessagePack.Formatters;
using MessagePack;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;


namespace Thor.Procedural.Data {
// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);

Expand Down Expand Up @@ -660,4 +663,52 @@ public class RectangleRoom : Room {
// this.walls = walls;
// }
}

public static class ExtensionMethods {
public static T DeepClone<T>(this T obj)
{
// Don't serialize a null object, simply return the default for that object
if (ReferenceEquals(obj, null)) {
return default;
}

// initialize inner objects individually
// for example in default constructor some list property initialized with some values,
// but in 'source' these items are cleaned -
// without ObjectCreationHandling.Replace default constructor values will be added to result
var jsonResolver = new ShouldSerializeContractResolver();
var str = Newtonsoft.Json.JsonConvert.SerializeObject(
obj,
Newtonsoft.Json.Formatting.None,
new Newtonsoft.Json.JsonSerializerSettings() {
ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore,
ContractResolver = jsonResolver
}
);

var jObj = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(str);

return jObj.ToObject<T>();
}

public static TValue GetValueOrDefault<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue = default(TValue))
{
TValue value;
return dictionary.TryGetValue(key, out value) ? value : defaultValue;
}

public static int AddCount<TKey>(this Dictionary<TKey, int> dictionary, TKey key, int count = 1)
{
int value;
dictionary.TryGetValue(key, out value);
if (dictionary.ContainsKey(key)) {
dictionary[key] = dictionary[key] + count;
}
else {
dictionary[key] = count;
}
return dictionary[key];
}
}

}
Loading

0 comments on commit 243aa90

Please sign in to comment.