PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/NerdSharp.Net_Studio/NerdSharp_UberNet/Science/Comp_Sci/Shapes/ThreeDShapes/ThreeDPolygon.cs

https://bitbucket.org/pastageek/scide-cad-scilife
C# | 195 lines | 146 code | 21 blank | 28 comment | 8 complexity | ad9988908d08820b7bcc3701b6e63af3 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Windows.Media.Media3D;
  15. using Geom = NerdSharp.Net_Studio.NerdSharp_UberNet.Science.Math.Utils.Geometry;
  16. namespace NerdSharp.Net_Studio.NerdSharp_UberNet.Science.Comp_Sci.Shapes.ThreeDShapes
  17. {
  18. public class ThreeDPolygon
  19. {
  20. [Flags]
  21. public enum PolygonNames
  22. {
  23. triangle = 3, square, pentagon, hexagon, octagon,
  24. nonagon, decagon, unidecagon, dodecagon, tridecagon, circle = 100
  25. }
  26. #region instance vars
  27. private List<Triangle> triangles = null;
  28. internal List<Triangle> Triangles
  29. {
  30. get { return triangles; }
  31. set { triangles = value; }
  32. }
  33. private List<Triangle> outterTriangles = null;
  34. private int sides = 3;
  35. public int Sides
  36. {
  37. get { return sides; }
  38. set
  39. {
  40. if (value >= 3)
  41. {
  42. sides = value;
  43. }
  44. }
  45. }
  46. private Brush brush = null;
  47. public Brush Brush
  48. {
  49. get { return brush; }
  50. set { brush = value; }
  51. }
  52. private double sideLength = 1d;
  53. public double SideLength
  54. {
  55. get { return sideLength; }
  56. set
  57. {
  58. if (value > 0.0d)
  59. {
  60. sideLength = value;
  61. }
  62. }
  63. }
  64. private string name = "";
  65. public string Name
  66. {
  67. get { return name; }
  68. set { name = value; }
  69. }
  70. private int externalTriangles = 0;
  71. private double interiorAngle = 0.0d;
  72. private Model3DGroup mod3DGrp = null;
  73. private Point3D startPoint;
  74. private int remainingTriangles;
  75. #endregion
  76. public ThreeDPolygon(int sides, Brush brush, double sideLength,
  77. string name, Model3DGroup mod3DGrp, Point3D startPoint)
  78. {
  79. //nothing less than a triangle
  80. if (sides < 3)
  81. {
  82. throw new ArgumentException("Three or more shall be the number of sides of a polygon.");
  83. }
  84. //asigning instance variables
  85. this.startPoint = startPoint;
  86. this.SideLength = sideLength;
  87. this.Sides = sides;
  88. this.brush = brush;
  89. //total triangles always equals 2 less than the count of its sides
  90. triangles = new List<Triangle>(sides - 2);
  91. /*the number of external triangles to be rendered on first pass of the for loop
  92. is the sides integer divided by two*/
  93. externalTriangles = sides / 2;
  94. /* Calculates the remaining triangles that make up the exterior
  95. * shape. This allows iteration to render the polygon in a
  96. * regular manner.
  97. * */
  98. remainingTriangles = triangles.Count - externalTriangles;
  99. //list of the outter triangles for reference for conneting to other shapes
  100. outterTriangles = new List<Triangle>(externalTriangles);
  101. this.interiorAngle = Geom.InterriorAngle(Sides);
  102. this.mod3DGrp = mod3DGrp;
  103. //builds the polygon using the 3D library via triangles
  104. Add3DShape();
  105. }
  106. #region _3DShape Members
  107. /// <summary>
  108. /// Adds the shape to a Model3DGroup
  109. /// </summary>
  110. public void Add3DShape()
  111. {
  112. //local vars for 3D modeling
  113. MeshGeometry3D meshGeom3D = new MeshGeometry3D();
  114. Point3DCollection pointCol = new Point3DCollection(sides);
  115. //temp variable used for makking all the triangles
  116. Triangle einExternTriAng = null;
  117. //adds to lists for later references
  118. int iterations = 0;
  119. //produces the external triangles
  120. try
  121. {
  122. Point3D pt1 = new Point3D(startPoint.X, startPoint.Y, startPoint.Z);
  123. Point3D pt2 = new Point3D();
  124. Point3D pt3 = new Point3D();
  125. do
  126. {
  127. if (iterations == 0)
  128. {
  129. this.outterTriangles.Add(einExternTriAng);
  130. }
  131. for (int i = 0; i < externalTriangles; i++)
  132. {
  133. Point3DCollection extrnTrigAngPoints = new Point3DCollection();
  134. //calculates and adds points need for each outer triangle for the polygon
  135. extrnTrigAngPoints.Add(pt1);
  136. extrnTrigAngPoints.Add(pt2);
  137. extrnTrigAngPoints.Add(pt3);
  138. Int32Collection extrnTriAngVerticies = new Int32Collection();
  139. einExternTriAng = new Triangle
  140. (extrnTrigAngPoints, extrnTriAngVerticies, this.brush, meshGeom3D);
  141. }
  142. //helps keep track of do while loop iterations for keeping track of external triangles
  143. iterations++;
  144. } while (remainingTriangles >= 3);
  145. //iterates internal triangles until only one triangle is left to be made
  146. }
  147. catch (Exception ex)
  148. {
  149. MessageBox.Show(ex.ToString());
  150. }
  151. }
  152. /// <summary>
  153. /// This adds a control of either type UserControl or (add later)
  154. /// to the face of the polygon. This is a hack around to make making
  155. /// polyhedra with 2D controls much easier.
  156. /// </summary>
  157. /// <param name="control">The control to add to the polygon</param>
  158. /// <returns></returns>
  159. public void AddControlsToShape(object control)
  160. {
  161. VisualBrush visBrsh = new VisualBrush();
  162. if (control is UserControl)
  163. {
  164. visBrsh.Visual = (UserControl)control;
  165. }
  166. else
  167. {
  168. throw new ArgumentException("Cannot take this type for a user control");
  169. }
  170. this.brush = visBrsh;
  171. }
  172. #endregion
  173. }
  174. }