Behavior.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. // -----------------------------------------------------------------------
  2. // <copyright file="Behavior.cs">
  3. // Original Triangle code by Jonathan Richard Shewchuk, http://www.cs.cmu.edu/~quake/triangle.html
  4. // Triangle.NET code by Christian Woltering, http://triangle.codeplex.com/
  5. // </copyright>
  6. // -----------------------------------------------------------------------
  7. namespace UnityEngine.U2D.Animation.TriangleNet
  8. {
  9. using System;
  10. using Animation.TriangleNet.Geometry;
  11. /// <summary>
  12. /// Controls the behavior of the meshing software.
  13. /// </summary>
  14. class Behavior
  15. {
  16. bool poly = false;
  17. bool quality = false;
  18. bool varArea = false;
  19. bool convex = false;
  20. bool jettison = false;
  21. bool boundaryMarkers = true;
  22. bool noHoles = false;
  23. bool conformDel = false;
  24. Func<ITriangle, double, bool> usertest;
  25. int noBisect = 0;
  26. double minAngle = 0.0;
  27. double maxAngle = 0.0;
  28. double maxArea = -1.0;
  29. internal bool fixedArea = false;
  30. internal bool useSegments = true;
  31. internal bool useRegions = false;
  32. internal double goodAngle = 0.0;
  33. internal double maxGoodAngle = 0.0;
  34. internal double offconstant = 0.0;
  35. /// <summary>
  36. /// Creates an instance of the Behavior class.
  37. /// </summary>
  38. public Behavior(bool quality = false, double minAngle = 20.0)
  39. {
  40. if (quality)
  41. {
  42. this.quality = true;
  43. this.minAngle = minAngle;
  44. Update();
  45. }
  46. }
  47. /// <summary>
  48. /// Update quality options dependencies.
  49. /// </summary>
  50. private void Update()
  51. {
  52. this.quality = true;
  53. if (this.minAngle < 0 || this.minAngle > 60)
  54. {
  55. this.minAngle = 0;
  56. this.quality = false;
  57. Log.Instance.Warning("Invalid quality option (minimum angle).", "Mesh.Behavior");
  58. }
  59. if ((this.maxAngle != 0.0) && (this.maxAngle < 60 || this.maxAngle > 180))
  60. {
  61. this.maxAngle = 0;
  62. this.quality = false;
  63. Log.Instance.Warning("Invalid quality option (maximum angle).", "Mesh.Behavior");
  64. }
  65. this.useSegments = this.Poly || this.Quality || this.Convex;
  66. this.goodAngle = Math.Cos(this.MinAngle * Math.PI / 180.0);
  67. this.maxGoodAngle = Math.Cos(this.MaxAngle * Math.PI / 180.0);
  68. if (this.goodAngle == 1.0)
  69. {
  70. this.offconstant = 0.0;
  71. }
  72. else
  73. {
  74. this.offconstant = 0.475 * Math.Sqrt((1.0 + this.goodAngle) / (1.0 - this.goodAngle));
  75. }
  76. this.goodAngle *= this.goodAngle;
  77. }
  78. #region Static properties
  79. /// <summary>
  80. /// No exact arithmetic.
  81. /// </summary>
  82. internal static bool NoExact { get; set; }
  83. #endregion
  84. #region Public properties
  85. /// <summary>
  86. /// Quality mesh generation.
  87. /// </summary>
  88. public bool Quality
  89. {
  90. get { return quality; }
  91. set
  92. {
  93. quality = value;
  94. if (quality)
  95. {
  96. Update();
  97. }
  98. }
  99. }
  100. /// <summary>
  101. /// Minimum angle constraint.
  102. /// </summary>
  103. public double MinAngle
  104. {
  105. get { return minAngle; }
  106. set { minAngle = value; Update(); }
  107. }
  108. /// <summary>
  109. /// Maximum angle constraint.
  110. /// </summary>
  111. public double MaxAngle
  112. {
  113. get { return maxAngle; }
  114. set { maxAngle = value; Update(); }
  115. }
  116. /// <summary>
  117. /// Maximum area constraint.
  118. /// </summary>
  119. public double MaxArea
  120. {
  121. get { return maxArea; }
  122. set
  123. {
  124. maxArea = value;
  125. fixedArea = value > 0.0;
  126. }
  127. }
  128. /// <summary>
  129. /// Apply a maximum triangle area constraint.
  130. /// </summary>
  131. public bool VarArea
  132. {
  133. get { return varArea; }
  134. set { varArea = value; }
  135. }
  136. /// <summary>
  137. /// Input is a Planar Straight Line Graph.
  138. /// </summary>
  139. public bool Poly
  140. {
  141. get { return poly; }
  142. set { poly = value; }
  143. }
  144. /// <summary>
  145. /// Apply a user-defined triangle constraint.
  146. /// </summary>
  147. public Func<ITriangle, double, bool> UserTest
  148. {
  149. get { return usertest; }
  150. set { usertest = value; }
  151. }
  152. /// <summary>
  153. /// Enclose the convex hull with segments.
  154. /// </summary>
  155. public bool Convex
  156. {
  157. get { return convex; }
  158. set { convex = value; }
  159. }
  160. /// <summary>
  161. /// Conforming Delaunay (all triangles are truly Delaunay).
  162. /// </summary>
  163. public bool ConformingDelaunay
  164. {
  165. get { return conformDel; }
  166. set { conformDel = value; }
  167. }
  168. /// <summary>
  169. /// Suppresses boundary segment splitting.
  170. /// </summary>
  171. /// <remarks>
  172. /// 0 = split segments
  173. /// 1 = no new vertices on the boundary
  174. /// 2 = prevent all segment splitting, including internal boundaries
  175. /// </remarks>
  176. public int NoBisect
  177. {
  178. get { return noBisect; }
  179. set
  180. {
  181. noBisect = value;
  182. if (noBisect < 0 || noBisect > 2)
  183. {
  184. noBisect = 0;
  185. }
  186. }
  187. }
  188. /// <summary>
  189. /// Compute boundary information.
  190. /// </summary>
  191. public bool UseBoundaryMarkers
  192. {
  193. get { return boundaryMarkers; }
  194. set { boundaryMarkers = value; }
  195. }
  196. /// <summary>
  197. /// Ignores holes in polygons.
  198. /// </summary>
  199. public bool NoHoles
  200. {
  201. get { return noHoles; }
  202. set { noHoles = value; }
  203. }
  204. /// <summary>
  205. /// Jettison unused vertices from output.
  206. /// </summary>
  207. public bool Jettison
  208. {
  209. get { return jettison; }
  210. set { jettison = value; }
  211. }
  212. #endregion
  213. }
  214. }