FileProcessor.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // -----------------------------------------------------------------------
  2. // <copyright file="FileProcessor.cs" company="">
  3. // Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace UnityEngine.U2D.Animation.TriangleNet
  7. .IO
  8. {
  9. using System;
  10. using System.Collections.Generic;
  11. using Animation.TriangleNet.Geometry;
  12. using Animation.TriangleNet.Meshing;
  13. internal static class FileProcessor
  14. {
  15. static List<IFileFormat> formats;
  16. static FileProcessor()
  17. {
  18. formats = new List<IFileFormat>();
  19. // Add Triangle file format as default.
  20. formats.Add(new TriangleFormat());
  21. }
  22. internal static void Add(IFileFormat format)
  23. {
  24. formats.Add(format);
  25. }
  26. internal static bool IsSupported(string file)
  27. {
  28. foreach (var format in formats)
  29. {
  30. if (format.IsSupported(file))
  31. {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. #region Polygon read/write
  38. /// <summary>
  39. /// Read a file containing polygon geometry.
  40. /// </summary>
  41. /// <param name="filename">The path of the file to read.</param>
  42. /// <returns>An instance of the <see cref="IPolygon" /> class.</returns>
  43. internal static IPolygon Read(string filename)
  44. {
  45. foreach (IPolygonFormat format in formats)
  46. {
  47. if (format != null && format.IsSupported(filename))
  48. {
  49. return format.Read(filename);
  50. }
  51. }
  52. throw new Exception("File format not supported.");
  53. }
  54. /// <summary>
  55. /// Save a polygon geometry to disk.
  56. /// </summary>
  57. /// <param name="mesh">An instance of the <see cref="IPolygon" /> class.</param>
  58. /// <param name="filename">The path of the file to save.</param>
  59. internal static void Write(IPolygon polygon, string filename)
  60. {
  61. foreach (IPolygonFormat format in formats)
  62. {
  63. if (format != null && format.IsSupported(filename))
  64. {
  65. format.Write(polygon, filename);
  66. return;
  67. }
  68. }
  69. throw new Exception("File format not supported.");
  70. }
  71. #endregion
  72. #region Mesh read/write
  73. /// <summary>
  74. /// Read a file containing a mesh.
  75. /// </summary>
  76. /// <param name="filename">The path of the file to read.</param>
  77. /// <returns>An instance of the <see cref="IMesh" /> interface.</returns>
  78. internal static IMesh Import(string filename)
  79. {
  80. foreach (IMeshFormat format in formats)
  81. {
  82. if (format != null && format.IsSupported(filename))
  83. {
  84. return format.Import(filename);
  85. }
  86. }
  87. throw new Exception("File format not supported.");
  88. }
  89. /// <summary>
  90. /// Save a mesh to disk.
  91. /// </summary>
  92. /// <param name="mesh">An instance of the <see cref="IMesh" /> interface.</param>
  93. /// <param name="filename">The path of the file to save.</param>
  94. internal static void Write(IMesh mesh, string filename)
  95. {
  96. foreach (IMeshFormat format in formats)
  97. {
  98. if (format != null && format.IsSupported(filename))
  99. {
  100. format.Write(mesh, filename);
  101. return;
  102. }
  103. }
  104. throw new Exception("File format not supported.");
  105. }
  106. #endregion
  107. }
  108. }