ARObjectInfo.cs
8.24 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Xml;
using UnityEngine;
namespace UnityEditor.XR.ARKit
{
/// <summary>
/// Represents metadata associated with a <c>.arobject</c> archive. These archives
/// are the source data for <see cref="UnityEngine.XR.ARKit.ARKitReferenceObjectEntry"/>.
/// </summary>
/// <seealso cref="ARObject"/>
/// <seealso cref="ARObjectImporter"/>
/// <seealso cref="UnityEngine.XR.ARKit.ARKitReferenceObjectEntry"/>
public struct ARObjectInfo : IEquatable<ARObjectInfo>
{
/// <summary>
/// Parses the <paramref name="plist"/> and constructs a new <see cref="ARObjectInfo"/>.
/// </summary>
/// <param name="plist">A valid <c>PlistDocument</c> to parse.</param>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="plist"/> is <c>null</c>.</exception>
public ARObjectInfo(XmlDocument plist)
{
if (plist == null)
throw new ArgumentNullException("plist");
// Parse the plist
var root = plist.DocumentElement;
var node = root.SelectSingleNode("descendant::dict");
var dict = ParseDict(node);
version = GetWithDefault(dict, "Version", 0);
trackingDataReference = GetWithDefault(dict, "TrackingDataReference", string.Empty);
imageReference = GetWithDefault(dict, "ImageReference", string.Empty);
if (dict.ContainsKey("ReferenceOrigin"))
{
var originDict = ParseDict(dict["ReferenceOrigin"]);
var rotation = GetWithDefault(originDict, "rotation", Quaternion.identity);
var translation = GetWithDefault(originDict, "translation", Vector3.zero);
referenceOrigin = new Pose(translation, rotation);
}
else
{
referenceOrigin = Pose.identity;
}
}
static int GetWithDefault(Dictionary<string, XmlNode> dict, string key, int defaultValue)
{
XmlNode node;
if (!dict.TryGetValue(key, out node))
return defaultValue;
if (node.Name != "integer")
return defaultValue;
return int.Parse(node.InnerText);
}
static string GetWithDefault(Dictionary<string, XmlNode> dict, string key, string defaultValue)
{
XmlNode node;
if (!dict.TryGetValue(key, out node))
return defaultValue;
if (node.Name != "string")
return defaultValue;
return node.InnerText;
}
static Dictionary<string, XmlNode> ParseDict(XmlNode node)
{
var dict = new Dictionary<string, XmlNode>();
XmlNodeList keys = node.SelectNodes("descendant::key");
foreach (XmlNode key in keys)
{
var value = key.NextSibling;
dict[key.InnerText] = value;
}
return dict;
}
static Vector3 GetWithDefault(Dictionary<string, XmlNode> dict, string key, Vector3 defaultValue)
{
XmlNode node;
if (!dict.TryGetValue(key, out node))
return defaultValue;
if (node.Name != "array")
return defaultValue;
XmlNodeList reals = node.SelectNodes("real");
if (reals == null)
return defaultValue;
return new Vector3(
float.Parse(reals[0].InnerText, CultureInfo.InvariantCulture),
float.Parse(reals[1].InnerText, CultureInfo.InvariantCulture),
-float.Parse(reals[2].InnerText, CultureInfo.InvariantCulture));
}
static Quaternion GetWithDefault(Dictionary<string, XmlNode> dict, string key, Quaternion defaultValue)
{
XmlNode node;
if (!dict.TryGetValue(key, out node))
return defaultValue;
if (node.Name != "array")
return defaultValue;
XmlNodeList reals = node.SelectNodes("real");
if (reals == null)
return defaultValue;
return new Quaternion(
float.Parse(reals[0].InnerText, CultureInfo.InvariantCulture),
float.Parse(reals[1].InnerText, CultureInfo.InvariantCulture),
-float.Parse(reals[2].InnerText, CultureInfo.InvariantCulture),
-float.Parse(reals[3].InnerText, CultureInfo.InvariantCulture));
}
/// <summary>
/// The filename of an image contained within the archive that can be used as a preview for the scanned object.
/// </summary>
public string imageReference { get; private set; }
/// <summary>
/// The reference origin for the scanned object.
/// </summary>
public Pose referenceOrigin { get; private set; }
/// <summary>
/// The filename of the source data representing the scanned object.
/// </summary>
public string trackingDataReference { get; private set; }
/// <summary>
/// The version of the metadata format.
/// </summary>
public int version { get; private set; }
/// <summary>
/// <c>true</c> if the <see cref="ARObjectInfo"/> represents valid data, otherwise <c>false</c>.
/// </summary>
public bool valid => version > 0;
/// <summary>
/// Tests for equality.
/// </summary>
/// <param name="other">The other <see cref="ARObjectInfo"/> to compare against.</param>
/// <returns>`True` if every field in <paramref name="other"/> is equal to this <see cref="ARObjectInfo"/>, otherwise false.</returns>
public bool Equals(ARObjectInfo other)
{
return
string.Equals(imageReference, other.imageReference) &&
referenceOrigin.Equals(other.referenceOrigin) &&
string.Equals(trackingDataReference, other.trackingDataReference) &&
(version == other.version);
}
/// <summary>
/// Tests for equality.
/// </summary>
/// <param name="obj">The `object` to compare against.</param>
/// <returns>`True` if <paramref name="obj"/> is of type <see cref="ARObjectInfo"/> and
/// <see cref="Equals(ARObjectInfo)"/> also returns `true`; otherwise `false`.</returns>
public override bool Equals(object obj)
{
if (!(obj is ARObjectInfo))
return false;
return Equals((ARObjectInfo)obj);
}
/// <summary>
/// Generates a hash suitable for use with containers like `HashSet` and `Dictionary`.
/// </summary>
/// <returns>A hash code generated from this object's fields.</returns>
public override int GetHashCode()
{
unchecked
{
var hash = (imageReference == null ? 0 : imageReference.GetHashCode());
hash = hash * 486187739 + referenceOrigin.GetHashCode();
hash = hash * 486187739 + (trackingDataReference == null ? 0 : trackingDataReference.GetHashCode());
hash = hash * 486187739 + version.GetHashCode();
return hash;
}
}
/// <summary>
/// Tests for equality. Same as <see cref="Equals(ARObjectInfo)"/>.
/// </summary>
/// <param name="lhs">The left-hand side of the comparison.</param>
/// <param name="rhs">The right-hand side of the comparison.</param>
/// <returns>`True` if <paramref name="lhs"/> is equal to <paramref name="rhs"/>, otherwise `false`.</returns>
public static bool operator ==(ARObjectInfo lhs, ARObjectInfo rhs) => lhs.Equals(rhs);
/// <summary>
/// Tests for inequality. Same as `!`<see cref="Equals(ARObjectInfo)"/>.
/// </summary>
/// <param name="lhs">The left-hand side of the comparison.</param>
/// <param name="rhs">The right-hand side of the comparison.</param>
/// <returns>`True` if <paramref name="lhs"/> is not equal to <paramref name="rhs"/>, otherwise `false`.</returns>
public static bool operator !=(ARObjectInfo lhs, ARObjectInfo rhs) => !lhs.Equals(rhs);
}
}