Ext.StructuralMetadataRoot.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  1. using OneOf;
  2. using SharpGLTF.Validation;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. namespace SharpGLTF.Schema2
  7. {
  8. public static class ExtStructuralMetadataRoot
  9. {
  10. public static void SetPropertyAttribute(
  11. this ModelRoot modelRoot,
  12. PropertyAttribute propertyAttribute,
  13. OneOf<StructuralMetadataSchema, Uri> schema)
  14. {
  15. SetPropertyAttributes(modelRoot, new List<PropertyAttribute>() { propertyAttribute }, schema);
  16. }
  17. public static void SetPropertyAttributes(
  18. this ModelRoot modelRoot,
  19. List<PropertyAttribute> propertyAttributes,
  20. OneOf<StructuralMetadataSchema, Uri> schema)
  21. {
  22. if (propertyAttributes == null || propertyAttributes.Count == 0) { modelRoot.RemoveExtensions<EXTStructuralMetadataRoot>(); return; }
  23. var ext = modelRoot.UseExtension<EXTStructuralMetadataRoot>();
  24. ext.PropertyAttributes = propertyAttributes;
  25. ext.AddSchema(schema);
  26. }
  27. public static void SetPropertyTexture(
  28. this ModelRoot modelRoot,
  29. PropertyTexture propertyTexture,
  30. OneOf<StructuralMetadataSchema, Uri> schema)
  31. {
  32. SetPropertyTextures(modelRoot, new List<PropertyTexture>() { propertyTexture }, schema);
  33. }
  34. public static void SetPropertyTextures(
  35. this ModelRoot modelRoot,
  36. List<PropertyTexture> propertyTextures,
  37. OneOf<StructuralMetadataSchema, Uri> schema)
  38. {
  39. if (propertyTextures == null || propertyTextures.Count == 0) { modelRoot.RemoveExtensions<EXTStructuralMetadataRoot>(); return; }
  40. var ext = modelRoot.UseExtension<EXTStructuralMetadataRoot>();
  41. ext.PropertyTextures = propertyTextures;
  42. ext.AddSchema(schema);
  43. }
  44. public static void SetPropertyTable(
  45. this ModelRoot modelRoot,
  46. PropertyTable propertyTable,
  47. OneOf<StructuralMetadataSchema, Uri> schema)
  48. {
  49. SetPropertyTables(modelRoot, new List<PropertyTable>() { propertyTable }, schema);
  50. }
  51. public static void SetPropertyTables(
  52. this ModelRoot modelRoot,
  53. List<PropertyTable> propertyTables,
  54. OneOf<StructuralMetadataSchema, Uri> schema)
  55. {
  56. if (propertyTables == null || propertyTables.Count == 0) { modelRoot.RemoveExtensions<EXTStructuralMetadataRoot>(); return; }
  57. var ext = modelRoot.UseExtension<EXTStructuralMetadataRoot>();
  58. ext.PropertyTables = propertyTables;
  59. ext.AddSchema(schema);
  60. }
  61. public static PropertyTableProperty GetArrayPropertyTableProperty<T>(this ModelRoot model, List<List<T>> values, bool CreateArrayOffsets = true)
  62. {
  63. var propertyTableProperty = new PropertyTableProperty();
  64. int logicalIndex = GetBufferView(model, values);
  65. propertyTableProperty.Values = logicalIndex;
  66. if (CreateArrayOffsets)
  67. {
  68. var arrayOffsets = BinaryTable.GetArrayOffsets(values);
  69. int logicalIndexOffsets = GetBufferView(model, arrayOffsets);
  70. propertyTableProperty.ArrayOffsets = logicalIndexOffsets;
  71. if (typeof(T) == typeof(string))
  72. {
  73. var stringValues = values.ConvertAll(x => x.ConvertAll(y => (string)Convert.ChangeType(y, typeof(string), CultureInfo.InvariantCulture)));
  74. var stringOffsets = BinaryTable.GetStringOffsets(stringValues);
  75. int offsets = GetBufferView(model, stringOffsets);
  76. propertyTableProperty.StringOffsets = offsets;
  77. }
  78. }
  79. return propertyTableProperty;
  80. }
  81. public static PropertyTableProperty GetPropertyTableProperty<T>(this ModelRoot model, List<T> values)
  82. {
  83. var propertyTableProperty = new PropertyTableProperty();
  84. int logicalIndex = GetBufferView(model, values);
  85. propertyTableProperty.Values = logicalIndex;
  86. if (typeof(T) == typeof(string))
  87. {
  88. var stringvalues = values.ConvertAll(x => (string)Convert.ChangeType(x, typeof(string), CultureInfo.InvariantCulture));
  89. var stringOffsets = BinaryTable.GetStringOffsets(stringvalues);
  90. int offsets = GetBufferView(model, stringOffsets);
  91. propertyTableProperty.StringOffsets = offsets;
  92. }
  93. return propertyTableProperty;
  94. }
  95. private static int GetBufferView<T>(this ModelRoot model, List<T> values)
  96. {
  97. var bytes = BinaryTable.GetBytes(values);
  98. var bufferView = model.UseBufferView(bytes);
  99. int logicalIndex = bufferView.LogicalIndex;
  100. return logicalIndex;
  101. }
  102. private static int GetBufferView<T>(this ModelRoot model, List<List<T>> values)
  103. {
  104. List<byte> bytes = BinaryTable.GetBytesForArray(values);
  105. var bufferView = model.UseBufferView(bytes.ToArray());
  106. int logicalIndex = bufferView.LogicalIndex;
  107. return logicalIndex;
  108. }
  109. }
  110. public partial class EXTStructuralMetadataRoot
  111. {
  112. private ModelRoot modelRoot;
  113. internal EXTStructuralMetadataRoot(ModelRoot modelRoot)
  114. {
  115. this.modelRoot = modelRoot;
  116. _propertyTables = new List<PropertyTable>();
  117. _propertyAttributes = new List<PropertyAttribute>();
  118. _propertyTextures = new List<PropertyTexture>();
  119. }
  120. internal void AddSchema(OneOf<StructuralMetadataSchema, Uri> schema)
  121. {
  122. schema.Switch(
  123. StructuralMetadataSchema => _schema = StructuralMetadataSchema,
  124. Uri => this.SchemaUri = Uri.ToString()
  125. );
  126. }
  127. internal List<PropertyTable> PropertyTables
  128. {
  129. get { return _propertyTables; }
  130. set { _propertyTables = value; }
  131. }
  132. internal string SchemaUri
  133. {
  134. get { return _schemaUri; }
  135. set { _schemaUri = value; }
  136. }
  137. internal List<PropertyAttribute> PropertyAttributes
  138. {
  139. get { return _propertyAttributes; }
  140. set { _propertyAttributes = value; }
  141. }
  142. internal StructuralMetadataSchema Schema
  143. {
  144. get { return _schema; }
  145. set { _schema = value; }
  146. }
  147. internal List<PropertyTexture> PropertyTextures
  148. {
  149. get { return _propertyTextures; }
  150. set { _propertyTextures = value; }
  151. }
  152. protected override void OnValidateReferences(ValidationContext validate)
  153. {
  154. foreach (var propertyTexture in PropertyTextures)
  155. {
  156. foreach (var propertyTextureProperty in propertyTexture.Properties)
  157. {
  158. var textureId = propertyTextureProperty.Value._LogicalTextureIndex;
  159. validate.IsNullOrIndex(nameof(propertyTexture), textureId, modelRoot.LogicalTextures);
  160. }
  161. }
  162. foreach (var propertyTable in PropertyTables)
  163. {
  164. Guard.NotNull(Schema.Classes[propertyTable.Class], nameof(propertyTable.Class), $"Schema must have class {propertyTable.Class}");
  165. foreach (var property in propertyTable.Properties)
  166. {
  167. Guard.NotNull(Schema.Classes[propertyTable.Class].Properties[property.Key], nameof(property.Key), $"Schema must have property {property.Key}");
  168. var values = property.Value.Values;
  169. validate.IsNullOrIndex(nameof(propertyTable), values, modelRoot.LogicalBufferViews);
  170. if (property.Value.ArrayOffsets.HasValue)
  171. {
  172. var arrayOffsets = property.Value.ArrayOffsets.Value;
  173. validate.IsNullOrIndex(nameof(propertyTable), arrayOffsets, modelRoot.LogicalBufferViews);
  174. }
  175. if (property.Value.StringOffsets.HasValue)
  176. {
  177. var stringOffsets = property.Value.StringOffsets.Value;
  178. validate.IsNullOrIndex(nameof(propertyTable), stringOffsets, modelRoot.LogicalBufferViews);
  179. }
  180. }
  181. }
  182. if (Schema != null)
  183. {
  184. foreach (var @class in Schema.Classes)
  185. {
  186. foreach (var property in @class.Value.Properties)
  187. {
  188. if (property.Value.Type == ElementType.ENUM)
  189. {
  190. Guard.IsTrue(Schema.Enums.ContainsKey(property.Value.EnumType), nameof(property.Value.EnumType), $"Enum {property.Value.EnumType} must be defined in schema");
  191. }
  192. }
  193. }
  194. }
  195. base.OnValidateReferences(validate);
  196. }
  197. protected override void OnValidateContent(ValidationContext result)
  198. {
  199. // check schema id is defined and valid
  200. if (Schema != null && !String.IsNullOrEmpty(Schema.Id))
  201. {
  202. var regex = "^[a-zA-Z_][a-zA-Z0-9_]*$";
  203. Guard.IsTrue(System.Text.RegularExpressions.Regex.IsMatch(Schema.Id, regex), nameof(Schema.Id));
  204. }
  205. foreach (var propertyTexture in PropertyTextures)
  206. {
  207. foreach (var propertyTextureProperty in propertyTexture.Properties)
  208. {
  209. var texCoord = propertyTextureProperty.Value.TextureCoordinate;
  210. var channels = propertyTextureProperty.Value.Channels;
  211. var index = propertyTextureProperty.Value._LogicalTextureIndex;
  212. Guard.MustBeGreaterThanOrEqualTo(texCoord, 0, nameof(texCoord));
  213. Guard.IsTrue(channels.Count > 0, nameof(channels), "Channels must be defined");
  214. Guard.IsTrue(index >= 0, nameof(index), "Index must be defined");
  215. }
  216. }
  217. foreach (var propertyTable in PropertyTables)
  218. {
  219. Guard.IsTrue(propertyTable.Class != null, nameof(propertyTable.Class), "Class must be defined");
  220. Guard.IsTrue(propertyTable.Count > 0, nameof(propertyTable.Count), "Count must be greater than 0");
  221. Guard.IsTrue(propertyTable.Properties.Count > 0, nameof(propertyTable.Properties), "Properties must be defined");
  222. }
  223. // Check one of schema or schemaUri is defined, but not both
  224. Guard.IsFalse(Schema != null && SchemaUri != null, "Schema/SchemaUri", "Schema and SchemaUri cannot both be defined");
  225. Guard.IsFalse(Schema == null && SchemaUri == null, "Schema/SchemaUri", "One of Schema and SchemaUri must be defined");
  226. base.OnValidateContent(result);
  227. }
  228. }
  229. public partial class PropertyTexture
  230. {
  231. public PropertyTexture()
  232. {
  233. _properties = new Dictionary<string, PropertyTextureProperty>();
  234. }
  235. public string Class
  236. {
  237. get { return _class; }
  238. set { _class = value; }
  239. }
  240. public Dictionary<string, PropertyTextureProperty> Properties
  241. {
  242. get { return _properties; }
  243. set { _properties = value; }
  244. }
  245. }
  246. public partial class PropertyTextureProperty
  247. {
  248. public PropertyTextureProperty()
  249. {
  250. _channels = new List<int>();
  251. }
  252. public List<int> Channels
  253. {
  254. get { return _channels; }
  255. set { _channels = value; }
  256. }
  257. }
  258. public partial class PropertyAttribute
  259. {
  260. public PropertyAttribute()
  261. {
  262. _properties = new Dictionary<string, PropertyAttributeProperty>();
  263. }
  264. public string Class
  265. {
  266. get { return _class; }
  267. set
  268. {
  269. if (value == null) { _class = null; return; }
  270. _class = value;
  271. }
  272. }
  273. public Dictionary<string, PropertyAttributeProperty> Properties
  274. {
  275. get { return _properties; }
  276. set
  277. {
  278. if (value == null) { _properties = null; return; }
  279. _properties = value;
  280. }
  281. }
  282. }
  283. public partial class PropertyAttributeProperty
  284. {
  285. public string Attribute
  286. {
  287. get { return _attribute; }
  288. set
  289. {
  290. if (value == null) { _attribute = null; return; }
  291. _attribute = value;
  292. }
  293. }
  294. }
  295. public partial class StructuralMetadataSchema
  296. {
  297. public StructuralMetadataSchema()
  298. {
  299. _classes = new Dictionary<string, StructuralMetadataClass>();
  300. _enums = new Dictionary<string, StructuralMetadataEnum>();
  301. }
  302. public Dictionary<string, StructuralMetadataClass> Classes
  303. {
  304. get { return _classes; }
  305. set
  306. {
  307. if (value == null) { _classes = null; return; }
  308. _classes = value;
  309. }
  310. }
  311. public string Id
  312. {
  313. get { return _id; }
  314. set
  315. {
  316. if (value == null) { _id = null; return; }
  317. _id = value;
  318. }
  319. }
  320. public string Version
  321. {
  322. get { return _version; }
  323. set
  324. {
  325. if (value == null) { _version = null; return; }
  326. _version = value;
  327. }
  328. }
  329. public string Name
  330. {
  331. get { return _name; }
  332. set
  333. {
  334. if (value == null) { _name = null; return; }
  335. _name = value;
  336. }
  337. }
  338. public string Description
  339. {
  340. get { return _description; }
  341. set
  342. {
  343. if (value == null) { _description = null; return; }
  344. _description = value;
  345. }
  346. }
  347. public Dictionary<string, StructuralMetadataEnum> Enums
  348. {
  349. get { return _enums; }
  350. set
  351. {
  352. if (value == null) { _enums = null; return; }
  353. _enums = value;
  354. }
  355. }
  356. }
  357. public partial class StructuralMetadataEnum
  358. {
  359. public StructuralMetadataEnum()
  360. {
  361. _values = new List<EnumValue>();
  362. }
  363. public string Name
  364. {
  365. get { return _name; }
  366. set
  367. {
  368. if (value == null) { _name = null; return; }
  369. _name = value;
  370. }
  371. }
  372. public string Description
  373. {
  374. get { return _description; }
  375. set
  376. {
  377. if (value == null) { _description = null; return; }
  378. _description = value;
  379. }
  380. }
  381. public List<EnumValue> Values
  382. {
  383. get { return _values; }
  384. set
  385. {
  386. if (value == null) { _values = null; return; }
  387. _values = value;
  388. }
  389. }
  390. }
  391. public partial class EnumValue
  392. {
  393. public string Name
  394. {
  395. get { return _name; }
  396. set
  397. {
  398. if (value == null) { _name = null; return; }
  399. _name = value;
  400. }
  401. }
  402. public int Value
  403. {
  404. get { return _value; }
  405. set
  406. {
  407. _value = value;
  408. }
  409. }
  410. }
  411. public partial class StructuralMetadataClass
  412. {
  413. public StructuralMetadataClass()
  414. {
  415. _properties = new Dictionary<string, ClassProperty>();
  416. }
  417. public Dictionary<string, ClassProperty> Properties
  418. {
  419. get { return _properties; }
  420. set
  421. {
  422. if (value == null) { _properties = null; return; }
  423. _properties = value;
  424. }
  425. }
  426. public string Name
  427. {
  428. get { return _name; }
  429. set
  430. {
  431. if (value == null) { _name = null; return; }
  432. _name = value;
  433. }
  434. }
  435. public string Description
  436. {
  437. get { return _description; }
  438. set
  439. {
  440. if (value == null) { _description = null; return; }
  441. _description = value;
  442. }
  443. }
  444. }
  445. public partial class ClassProperty
  446. {
  447. public string Name
  448. {
  449. get { return _name; }
  450. set
  451. {
  452. if (value == null) { _name = null; return; }
  453. _name = value;
  454. }
  455. }
  456. public string Description
  457. {
  458. get { return _description; }
  459. set
  460. {
  461. if (value == null) { _description = null; return; }
  462. _description = value;
  463. }
  464. }
  465. public ElementType Type
  466. {
  467. get { return _type; }
  468. set
  469. {
  470. _type = value;
  471. }
  472. }
  473. public string EnumType
  474. {
  475. get { return _enumType; }
  476. set
  477. {
  478. if (value == null) { _enumType = null; return; }
  479. _enumType = value;
  480. }
  481. }
  482. public DataType? ComponentType
  483. {
  484. get { return _componentType; }
  485. set
  486. {
  487. if (value == null) { _componentType = null; return; }
  488. _componentType = value;
  489. }
  490. }
  491. public bool? Required
  492. {
  493. get { return _required; }
  494. set
  495. {
  496. if (value == null) { _required = null; return; }
  497. _required = value;
  498. }
  499. }
  500. public bool? Normalized
  501. {
  502. get { return _normalized; }
  503. set
  504. {
  505. if (value == null) { _normalized = null; return; }
  506. _normalized = value;
  507. }
  508. }
  509. public bool? Array
  510. {
  511. get { return _array; }
  512. set
  513. {
  514. if (value == null) { _array = null; return; }
  515. _array = value;
  516. }
  517. }
  518. public int? Count
  519. {
  520. get { return _count; }
  521. set
  522. {
  523. if (value == null) { _count = null; return; }
  524. _count = value;
  525. }
  526. }
  527. }
  528. public partial class PropertyTable
  529. {
  530. public PropertyTable()
  531. {
  532. _properties = new Dictionary<string, PropertyTableProperty>();
  533. }
  534. public PropertyTable(string Class, int Count, string Name = "") : this()
  535. {
  536. _class = Class;
  537. _count = Count;
  538. _name = Name;
  539. }
  540. public string Name
  541. {
  542. get { return _name; }
  543. set
  544. {
  545. if (value == null) { _name = null; return; }
  546. _name = value;
  547. }
  548. }
  549. public string Class
  550. {
  551. get { return _class; }
  552. set
  553. {
  554. if (value == null) { _class = null; return; }
  555. _class = value;
  556. }
  557. }
  558. public int Count
  559. {
  560. get { return _count; }
  561. set
  562. {
  563. _count = value;
  564. }
  565. }
  566. public Dictionary<string, PropertyTableProperty> Properties
  567. {
  568. get { return _properties; }
  569. set {
  570. if (value == null) { _properties = null; return; }
  571. _properties = value; }
  572. }
  573. }
  574. public partial class PropertyTableProperty
  575. {
  576. public int Values
  577. {
  578. get { return _values; }
  579. set { _values = value; }
  580. }
  581. public int? ArrayOffsets
  582. {
  583. get { return _arrayOffsets; }
  584. set {
  585. if (value == null) { _arrayOffsets= null; return; }
  586. _arrayOffsets = value; }
  587. }
  588. public int? StringOffsets
  589. {
  590. get { return _stringOffsets; }
  591. set {
  592. if (value == null) { _stringOffsets = null; return; }
  593. _stringOffsets = value; }
  594. }
  595. }
  596. }