1
0

SculptMesh.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) Contributors
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Text;
  30. using System.IO;
  31. using System.Drawing;
  32. using System.Drawing.Imaging;
  33. namespace PrimMesher
  34. {
  35. public class SculptMesh
  36. {
  37. public List<Coord> coords;
  38. public List<Face> faces;
  39. public enum SculptType { sphere = 1, torus = 2, plane = 3, cylinder = 4 };
  40. public SculptMesh(Bitmap sculptBitmap, SculptType sculptType, int lod, bool mirror, bool invert)
  41. {
  42. if (mirror)
  43. invert = !invert;
  44. SculptMap smap = new SculptMap(sculptBitmap, lod);
  45. List<List<Coord>> rows = smap.ToRows(mirror);
  46. _SculptMesh(rows, sculptType, invert);
  47. }
  48. private void _SculptMesh(List<List<Coord>> rows, SculptType sculptType, bool invert)
  49. {
  50. coords = new List<Coord>();
  51. faces = new List<Face>();
  52. sculptType = (SculptType)(((int)sculptType) & 0x07);
  53. int width = rows[0].Count;
  54. int p1, p2, p3, p4;
  55. int imageX, imageY;
  56. if (sculptType != SculptType.plane)
  57. {
  58. if (rows.Count % 2 == 0)
  59. {
  60. for (int rowNdx = 0; rowNdx < rows.Count; rowNdx++)
  61. rows[rowNdx].Add(rows[rowNdx][0]);
  62. }
  63. else
  64. {
  65. int lastIndex = rows[0].Count - 1;
  66. for (int i = 0; i < rows.Count; i++)
  67. rows[i][0] = rows[i][lastIndex];
  68. }
  69. }
  70. Coord topPole = rows[0][width / 2];
  71. Coord bottomPole = rows[rows.Count - 1][width / 2];
  72. if (sculptType == SculptType.sphere)
  73. {
  74. if (rows.Count % 2 == 0)
  75. {
  76. int count = rows[0].Count;
  77. List<Coord> topPoleRow = new List<Coord>(count);
  78. List<Coord> bottomPoleRow = new List<Coord>(count);
  79. for (int i = 0; i < count; i++)
  80. {
  81. topPoleRow.Add(topPole);
  82. bottomPoleRow.Add(bottomPole);
  83. }
  84. rows.Insert(0, topPoleRow);
  85. rows.Add(bottomPoleRow);
  86. }
  87. else
  88. {
  89. int count = rows[0].Count;
  90. List<Coord> topPoleRow = rows[0];
  91. List<Coord> bottomPoleRow = rows[rows.Count - 1];
  92. for (int i = 0; i < count; i++)
  93. {
  94. topPoleRow[i] = topPole;
  95. bottomPoleRow[i] = bottomPole;
  96. }
  97. }
  98. }
  99. if (sculptType == SculptType.torus)
  100. rows.Add(rows[0]);
  101. int coordsDown = rows.Count;
  102. int coordsAcross = rows[0].Count;
  103. float widthUnit = 1.0f / (coordsAcross - 1);
  104. float heightUnit = 1.0f / (coordsDown - 1);
  105. for (imageY = 0; imageY < coordsDown; imageY++)
  106. {
  107. int rowOffset = imageY * coordsAcross;
  108. for (imageX = 0; imageX < coordsAcross; imageX++)
  109. {
  110. /*
  111. * p1-----p2
  112. * | \ f2 |
  113. * | \ |
  114. * | f1 \|
  115. * p3-----p4
  116. */
  117. p4 = rowOffset + imageX;
  118. p3 = p4 - 1;
  119. p2 = p4 - coordsAcross;
  120. p1 = p3 - coordsAcross;
  121. this.coords.Add(rows[imageY][imageX]);
  122. if (imageY > 0 && imageX > 0)
  123. {
  124. Face f1, f2;
  125. if (invert)
  126. {
  127. f1 = new Face(p1, p4, p3);
  128. f2 = new Face(p1, p2, p4);
  129. }
  130. else
  131. {
  132. f1 = new Face(p1, p3, p4);
  133. f2 = new Face(p1, p4, p2);
  134. }
  135. faces.Add(f1);
  136. faces.Add(f2);
  137. }
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// Duplicates a SculptMesh object. All object properties are copied by value, including lists.
  143. /// </summary>
  144. /// <returns></returns>
  145. public SculptMesh Copy()
  146. {
  147. return new SculptMesh(this);
  148. }
  149. public SculptMesh(SculptMesh sm)
  150. {
  151. coords = new List<Coord>(sm.coords);
  152. faces = new List<Face>(sm.faces);
  153. }
  154. public void Scale(float x, float y, float z)
  155. {
  156. int i;
  157. int numVerts = this.coords.Count;
  158. Coord m = new Coord(x, y, z);
  159. for (i = 0; i < numVerts; i++)
  160. this.coords[i] *= m;
  161. }
  162. public void DumpRaw(String path, String name, String title)
  163. {
  164. if (path == null)
  165. return;
  166. String fileName = name + "_" + title + ".raw";
  167. String completePath = System.IO.Path.Combine(path, fileName);
  168. using(StreamWriter sw = new StreamWriter(completePath))
  169. {
  170. for (int i = 0; i < faces.Count; i++)
  171. {
  172. sw.Write(coords[faces[i].v1].ToString());
  173. sw.Write(coords[faces[i].v2].ToString());
  174. sw.WriteLine(coords[faces[i].v3].ToString());
  175. }
  176. }
  177. }
  178. }
  179. }