thomasfn

Untitled

May 16th, 2014
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.55 KB | None | 0 0
  1. /// <summary>
  2. /// Triangulates the specified brush
  3. /// </summary>
  4. /// <param name="brush"></param>
  5. /// <returns></returns>
  6. private ModelInput BrushToModel(Brush brush)
  7. {
  8. // First, we need to get all brush sides and planes associated with the brush
  9. BrushSide[] sides = new BrushSide[brush.NumSides];
  10. for (int i = 0; i < brush.NumSides; i++)
  11. sides[i] = loader.BrushSides[i];
  12. Plane[] planes = new Plane[sides.Length];
  13. Mat4[] planemats = new Mat4[sides.Length];
  14. for (int i = 0; i < sides.Length; i++)
  15. {
  16. planes[i] = loader.Planes[sides[i].PlaneNum];
  17. planemats[i] = MathUtil.ConstructPlaneMatrix(planes[i]);
  18. }
  19.  
  20. // Next, we should calculate all intersections and build a list of all possible vertices
  21. // At the same time, store indices for vertices found on each plane
  22. List<Vec3> vertices = new List<Vec3>();
  23. List<int>[] planeindices = new List<int>[sides.Length];
  24. for (int i = 0; i < sides.Length; i++)
  25. planeindices[i] = new List<int>();
  26. for (int i = 0; i < sides.Length - 2; i++)
  27. {
  28. // Get the plane
  29. Plane plane0 = planes[i];
  30.  
  31. // We should find combinations of 3 planes which produce intersections
  32. for (int j = i + 1; j < sides.Length; j++)
  33. {
  34. Plane plane1 = planes[j];
  35. for (int k = j + 1; k < sides.Length; k++)
  36. {
  37. Plane plane2 = planes[k];
  38.  
  39. // Now, test for intersection
  40. Vec3 intersectpt;
  41. if (MathUtil.IntersectPlanesPoint(plane0, plane1, plane2, out intersectpt))
  42. {
  43. // Ok we found an intersection, add the point (or locate the indice for it)
  44. int idx;
  45. bool found = false;
  46. for (idx = 0; idx < vertices.Count; idx++)
  47. if (MathUtil.VectorEquals(vertices[idx], intersectpt))
  48. {
  49. found = true;
  50. break;
  51. }
  52. if (!found)
  53. {
  54. idx = vertices.Count;
  55. vertices.Add(intersectpt);
  56. }
  57.  
  58. // Add the point to the planes
  59. if (!planeindices[i].Contains(idx)) planeindices[i].Add(idx);
  60. if (!planeindices[j].Contains(idx)) planeindices[j].Add(idx);
  61. if (!planeindices[k].Contains(idx)) planeindices[k].Add(idx);
  62. }
  63. }
  64. }
  65. }
  66.  
  67. // Triangulate all planes
  68. List<int> indices = new List<int>();
  69. for (int i = 0; i < sides.Length; i++)
  70. {
  71. // Get the vertices relative to the plane
  72. List<int> myindices = planeindices[i];
  73. Vec2[] planevertices = new Vec2[myindices.Count];
  74. Plane plane = planes[i];
  75. int leftmost = -1;
  76. Vec2 leftmostpt = default(Vec2);
  77. for (int j = 0; j < myindices.Count; j++)
  78. {
  79. // Get the vertex, transform and store
  80. Vec3 vertex = vertices[myindices[j]];
  81. Vec4 localtoplane = MathUtil.RoundCoordinate(planemats[i] * Vec4.FromVec3(vertex, 1.0));
  82. Vec2 planevertex = new Vec2() { x = localtoplane.x, y = localtoplane.y };
  83. planevertices[j] = planevertex;
  84.  
  85. // Check leftmost
  86. if (leftmost == -1 || planevertex.x < leftmostpt.x)
  87. {
  88. leftmost = j;
  89. leftmostpt = planevertex;
  90. }
  91. }
  92.  
  93. // Start at vertex a, build convex hull
  94. int[] hull = new int[myindices.Count];
  95. int pointonhull = leftmost;
  96. int k = 0;
  97. int endpoint;
  98. do
  99. {
  100. endpoint = 0;
  101. hull[k++] = pointonhull;
  102. Vec2 curpoint = planevertices[pointonhull];
  103. for (int m = 0; m < myindices.Count; m++)
  104. if (m != pointonhull)
  105. {
  106. Vec2 mpoint = planevertices[m];
  107. if (endpoint == pointonhull || MathUtil.PointLine(mpoint, curpoint, planevertices[endpoint]) == MathUtil.PointLineRelationship.Left)
  108. endpoint = m;
  109. }
  110. pointonhull = endpoint;
  111. } while (endpoint != hull[0]);
  112.  
  113. // Triangulate hull
  114. for (int j = 1; j < myindices.Count - 1; j++)
  115. {
  116. indices.Add(myindices[0]);
  117. indices.Add(myindices[j]);
  118. indices.Add(myindices[j + 1]);
  119. }
  120. }
  121.  
  122. // Return the model
  123. return new ModelInput(vertices.ToArray(), new int[][] { indices.ToArray() });
  124. }
Advertisement
Add Comment
Please, Sign In to add comment