PrimMesher.cs 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707
  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. namespace PrimMesher
  32. {
  33. public struct Quat
  34. {
  35. /// <summary>X value</summary>
  36. public float X;
  37. /// <summary>Y value</summary>
  38. public float Y;
  39. /// <summary>Z value</summary>
  40. public float Z;
  41. /// <summary>W value</summary>
  42. public float W;
  43. public Quat(float x, float y, float z, float w)
  44. {
  45. X = x;
  46. Y = y;
  47. Z = z;
  48. W = w;
  49. }
  50. public Quat(Coord axis, float angle)
  51. {
  52. axis = axis.Normalize();
  53. angle *= 0.5f;
  54. float c = (float)Math.Cos(angle);
  55. float s = (float)Math.Sin(angle);
  56. X = axis.X * s;
  57. Y = axis.Y * s;
  58. Z = axis.Z * s;
  59. W = c;
  60. Normalize();
  61. }
  62. public float Length()
  63. {
  64. return (float)Math.Sqrt(X * X + Y * Y + Z * Z + W * W);
  65. }
  66. public Quat Normalize()
  67. {
  68. const float MAG_THRESHOLD = 0.0000001f;
  69. float mag = Length();
  70. // Catch very small rounding errors when normalizing
  71. if (mag > MAG_THRESHOLD)
  72. {
  73. float oomag = 1f / mag;
  74. X *= oomag;
  75. Y *= oomag;
  76. Z *= oomag;
  77. W *= oomag;
  78. }
  79. else
  80. {
  81. X = 0f;
  82. Y = 0f;
  83. Z = 0f;
  84. W = 1f;
  85. }
  86. return this;
  87. }
  88. public static Quat operator *(Quat q1, Quat q2)
  89. {
  90. float x = q1.W * q2.X + q1.X * q2.W + q1.Y * q2.Z - q1.Z * q2.Y;
  91. float y = q1.W * q2.Y - q1.X * q2.Z + q1.Y * q2.W + q1.Z * q2.X;
  92. float z = q1.W * q2.Z + q1.X * q2.Y - q1.Y * q2.X + q1.Z * q2.W;
  93. float w = q1.W * q2.W - q1.X * q2.X - q1.Y * q2.Y - q1.Z * q2.Z;
  94. return new Quat(x, y, z, w);
  95. }
  96. public override string ToString()
  97. {
  98. return "< X: " + this.X.ToString() + ", Y: " + this.Y.ToString() + ", Z: " + this.Z.ToString() + ", W: " + this.W.ToString() + ">";
  99. }
  100. }
  101. public struct Coord
  102. {
  103. public float X;
  104. public float Y;
  105. public float Z;
  106. public Coord(float x, float y, float z)
  107. {
  108. this.X = x;
  109. this.Y = y;
  110. this.Z = z;
  111. }
  112. public float Length()
  113. {
  114. return (float)Math.Sqrt(this.X * this.X + this.Y * this.Y + this.Z * this.Z);
  115. }
  116. public Coord Invert()
  117. {
  118. this.X = -this.X;
  119. this.Y = -this.Y;
  120. this.Z = -this.Z;
  121. return this;
  122. }
  123. public Coord Normalize()
  124. {
  125. const float MAG_THRESHOLD = 0.0000001f;
  126. float mag = Length();
  127. // Catch very small rounding errors when normalizing
  128. if (mag > MAG_THRESHOLD)
  129. {
  130. float oomag = 1.0f / mag;
  131. this.X *= oomag;
  132. this.Y *= oomag;
  133. this.Z *= oomag;
  134. }
  135. else
  136. {
  137. this.X = 0.0f;
  138. this.Y = 0.0f;
  139. this.Z = 0.0f;
  140. }
  141. return this;
  142. }
  143. public override string ToString()
  144. {
  145. return this.X.ToString() + " " + this.Y.ToString() + " " + this.Z.ToString();
  146. }
  147. public static Coord Cross(Coord c1, Coord c2)
  148. {
  149. return new Coord(
  150. c1.Y * c2.Z - c2.Y * c1.Z,
  151. c1.Z * c2.X - c2.Z * c1.X,
  152. c1.X * c2.Y - c2.X * c1.Y
  153. );
  154. }
  155. public static Coord operator +(Coord v, Coord a)
  156. {
  157. return new Coord(v.X + a.X, v.Y + a.Y, v.Z + a.Z);
  158. }
  159. public static Coord operator *(Coord v, Coord m)
  160. {
  161. return new Coord(v.X * m.X, v.Y * m.Y, v.Z * m.Z);
  162. }
  163. public static Coord operator *(Coord v, Quat q)
  164. {
  165. // From http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/transforms/
  166. Coord c2 = new Coord(0.0f, 0.0f, 0.0f);
  167. c2.X = q.W * q.W * v.X +
  168. 2f * q.Y * q.W * v.Z -
  169. 2f * q.Z * q.W * v.Y +
  170. q.X * q.X * v.X +
  171. 2f * q.Y * q.X * v.Y +
  172. 2f * q.Z * q.X * v.Z -
  173. q.Z * q.Z * v.X -
  174. q.Y * q.Y * v.X;
  175. c2.Y =
  176. 2f * q.X * q.Y * v.X +
  177. q.Y * q.Y * v.Y +
  178. 2f * q.Z * q.Y * v.Z +
  179. 2f * q.W * q.Z * v.X -
  180. q.Z * q.Z * v.Y +
  181. q.W * q.W * v.Y -
  182. 2f * q.X * q.W * v.Z -
  183. q.X * q.X * v.Y;
  184. c2.Z =
  185. 2f * q.X * q.Z * v.X +
  186. 2f * q.Y * q.Z * v.Y +
  187. q.Z * q.Z * v.Z -
  188. 2f * q.W * q.Y * v.X -
  189. q.Y * q.Y * v.Z +
  190. 2f * q.W * q.X * v.Y -
  191. q.X * q.X * v.Z +
  192. q.W * q.W * v.Z;
  193. return c2;
  194. }
  195. }
  196. public struct Face
  197. {
  198. public int primFace;
  199. // vertices
  200. public int v1;
  201. public int v2;
  202. public int v3;
  203. public Face(int v1, int v2, int v3)
  204. {
  205. primFace = 0;
  206. this.v1 = v1;
  207. this.v2 = v2;
  208. this.v3 = v3;
  209. }
  210. public Coord SurfaceNormal(List<Coord> coordList)
  211. {
  212. Coord c1 = coordList[this.v1];
  213. Coord c2 = coordList[this.v2];
  214. Coord c3 = coordList[this.v3];
  215. Coord edge1 = new Coord(c2.X - c1.X, c2.Y - c1.Y, c2.Z - c1.Z);
  216. Coord edge2 = new Coord(c3.X - c1.X, c3.Y - c1.Y, c3.Z - c1.Z);
  217. return Coord.Cross(edge1, edge2).Normalize();
  218. }
  219. }
  220. internal struct Angle
  221. {
  222. internal float angle;
  223. internal float X;
  224. internal float Y;
  225. internal Angle(float angle, float x, float y)
  226. {
  227. this.angle = angle;
  228. this.X = x;
  229. this.Y = y;
  230. }
  231. }
  232. internal class AngleList
  233. {
  234. private float iX, iY; // intersection point
  235. private static Angle[] angles3 =
  236. {
  237. new Angle(0.0f, 1.0f, 0.0f),
  238. new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
  239. new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
  240. new Angle(1.0f, 1.0f, 0.0f)
  241. };
  242. private static Angle[] angles4 =
  243. {
  244. new Angle(0.0f, 1.0f, 0.0f),
  245. new Angle(0.25f, 0.0f, 1.0f),
  246. new Angle(0.5f, -1.0f, 0.0f),
  247. new Angle(0.75f, 0.0f, -1.0f),
  248. new Angle(1.0f, 1.0f, 0.0f)
  249. };
  250. private static Angle[] angles6 =
  251. {
  252. new Angle(0.0f, 1.0f, 0.0f),
  253. new Angle(0.16666666666666667f, 0.5f, 0.8660254037844386f),
  254. new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
  255. new Angle(0.5f, -1.0f, 0.0f),
  256. new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
  257. new Angle(0.83333333333333326f, 0.5f, -0.86602540378443904f),
  258. new Angle(1.0f, 1.0f, 0.0f)
  259. };
  260. private static Angle[] angles12 =
  261. {
  262. new Angle(0.0f, 1.0f, 0.0f),
  263. new Angle(0.083333333333333329f, 0.86602540378443871f, 0.5f),
  264. new Angle(0.16666666666666667f, 0.5f, 0.8660254037844386f),
  265. new Angle(0.25f, 0.0f, 1.0f),
  266. new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
  267. new Angle(0.41666666666666663f, -0.86602540378443849f, 0.5f),
  268. new Angle(0.5f, -1.0f, 0.0f),
  269. new Angle(0.58333333333333326f, -0.86602540378443882f, -0.5f),
  270. new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
  271. new Angle(0.75f, 0.0f, -1.0f),
  272. new Angle(0.83333333333333326f, 0.5f, -0.86602540378443904f),
  273. new Angle(0.91666666666666663f, 0.86602540378443837f, -0.5f),
  274. new Angle(1.0f, 1.0f, 0.0f)
  275. };
  276. private static Angle[] angles24 =
  277. {
  278. new Angle(0.0f, 1.0f, 0.0f),
  279. new Angle(0.041666666666666664f, 0.96592582628906831f, 0.25881904510252074f),
  280. new Angle(0.083333333333333329f, 0.86602540378443871f, 0.5f),
  281. new Angle(0.125f, 0.70710678118654757f, 0.70710678118654746f),
  282. new Angle(0.16666666666666667f, 0.5f, 0.8660254037844386f),
  283. new Angle(0.20833333333333331f, 0.25881904510252096f, 0.9659258262890682f),
  284. new Angle(0.25f, 0.0f, 1.0f),
  285. new Angle(0.29166666666666663f, -0.25881904510252063f, 0.96592582628906831f),
  286. new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
  287. new Angle(0.375f, -0.70710678118654746f, 0.70710678118654757f),
  288. new Angle(0.41666666666666663f, -0.86602540378443849f, 0.5f),
  289. new Angle(0.45833333333333331f, -0.9659258262890682f, 0.25881904510252102f),
  290. new Angle(0.5f, -1.0f, 0.0f),
  291. new Angle(0.54166666666666663f, -0.96592582628906842f, -0.25881904510252035f),
  292. new Angle(0.58333333333333326f, -0.86602540378443882f, -0.5f),
  293. new Angle(0.62499999999999989f, -0.70710678118654791f, -0.70710678118654713f),
  294. new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
  295. new Angle(0.70833333333333326f, -0.25881904510252152f, -0.96592582628906809f),
  296. new Angle(0.75f, 0.0f, -1.0f),
  297. new Angle(0.79166666666666663f, 0.2588190451025203f, -0.96592582628906842f),
  298. new Angle(0.83333333333333326f, 0.5f, -0.86602540378443904f),
  299. new Angle(0.875f, 0.70710678118654735f, -0.70710678118654768f),
  300. new Angle(0.91666666666666663f, 0.86602540378443837f, -0.5f),
  301. new Angle(0.95833333333333326f, 0.96592582628906809f, -0.25881904510252157f),
  302. new Angle(1.0f, 1.0f, 0.0f)
  303. };
  304. private Angle interpolatePoints(float newPoint, Angle p1, Angle p2)
  305. {
  306. float m = (newPoint - p1.angle) / (p2.angle - p1.angle);
  307. return new Angle(newPoint, p1.X + m * (p2.X - p1.X), p1.Y + m * (p2.Y - p1.Y));
  308. }
  309. private void intersection(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
  310. { // ref: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
  311. double denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
  312. double uaNumerator = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
  313. if (denom != 0.0)
  314. {
  315. double ua = uaNumerator / denom;
  316. iX = (float)(x1 + ua * (x2 - x1));
  317. iY = (float)(y1 + ua * (y2 - y1));
  318. }
  319. }
  320. internal List<Angle> angles;
  321. internal void makeAngles(int sides, float startAngle, float stopAngle, bool hasCut)
  322. {
  323. angles = new List<Angle>();
  324. const double twoPi = System.Math.PI * 2.0;
  325. const float twoPiInv = (float)(1.0d / twoPi);
  326. if (sides < 1)
  327. throw new Exception("number of sides not greater than zero");
  328. if (stopAngle <= startAngle)
  329. throw new Exception("stopAngle not greater than startAngle");
  330. if ((sides == 3 || sides == 4 || sides == 6 || sides == 12 || sides == 24))
  331. {
  332. startAngle *= twoPiInv;
  333. stopAngle *= twoPiInv;
  334. Angle[] sourceAngles;
  335. switch (sides)
  336. {
  337. case 3:
  338. sourceAngles = angles3;
  339. break;
  340. case 4:
  341. sourceAngles = angles4;
  342. break;
  343. case 6:
  344. sourceAngles = angles6;
  345. break;
  346. case 12:
  347. sourceAngles = angles12;
  348. break;
  349. default:
  350. sourceAngles = angles24;
  351. break;
  352. }
  353. int startAngleIndex = (int)(startAngle * sides);
  354. int endAngleIndex = sourceAngles.Length - 1;
  355. if (hasCut)
  356. {
  357. if (stopAngle < 1.0f)
  358. endAngleIndex = (int)(stopAngle * sides) + 1;
  359. if (endAngleIndex == startAngleIndex)
  360. endAngleIndex++;
  361. for (int angleIndex = startAngleIndex; angleIndex < endAngleIndex + 1; angleIndex++)
  362. {
  363. angles.Add(sourceAngles[angleIndex]);
  364. }
  365. if (startAngle > 0.0f)
  366. angles[0] = interpolatePoints(startAngle, angles[0], angles[1]);
  367. if (stopAngle < 1.0f)
  368. {
  369. int lastAngleIndex = angles.Count - 1;
  370. angles[lastAngleIndex] = interpolatePoints(stopAngle, angles[lastAngleIndex - 1], angles[lastAngleIndex]);
  371. }
  372. }
  373. else
  374. {
  375. for (int angleIndex = startAngleIndex; angleIndex < endAngleIndex; angleIndex++)
  376. angles.Add(sourceAngles[angleIndex]);
  377. }
  378. }
  379. else
  380. {
  381. double stepSize = twoPi / sides;
  382. int startStep = (int)(startAngle / stepSize);
  383. double angle = stepSize * startStep;
  384. int step = startStep;
  385. double stopAngleTest = stopAngle;
  386. if (stopAngle < twoPi)
  387. {
  388. stopAngleTest = stepSize * ((int)(stopAngle / stepSize) + 1);
  389. if (stopAngleTest < stopAngle)
  390. stopAngleTest += stepSize;
  391. if (stopAngleTest > twoPi)
  392. stopAngleTest = twoPi;
  393. }
  394. while (angle <= stopAngleTest)
  395. {
  396. Angle newAngle;
  397. newAngle.angle = (float)angle;
  398. newAngle.X = (float)System.Math.Cos(angle);
  399. newAngle.Y = (float)System.Math.Sin(angle);
  400. angles.Add(newAngle);
  401. step += 1;
  402. angle = stepSize * step;
  403. }
  404. if (startAngle > angles[0].angle)
  405. {
  406. Angle newAngle;
  407. intersection(angles[0].X, angles[0].Y, angles[1].X, angles[1].Y, 0.0f, 0.0f, (float)Math.Cos(startAngle), (float)Math.Sin(startAngle));
  408. newAngle.angle = startAngle;
  409. newAngle.X = iX;
  410. newAngle.Y = iY;
  411. angles[0] = newAngle;
  412. }
  413. int index = angles.Count - 1;
  414. if (stopAngle < angles[index].angle)
  415. {
  416. Angle newAngle;
  417. intersection(angles[index - 1].X, angles[index - 1].Y, angles[index].X, angles[index].Y, 0.0f, 0.0f, (float)Math.Cos(stopAngle), (float)Math.Sin(stopAngle));
  418. newAngle.angle = stopAngle;
  419. newAngle.X = iX;
  420. newAngle.Y = iY;
  421. angles[index] = newAngle;
  422. }
  423. }
  424. }
  425. }
  426. /// <summary>
  427. /// generates a profile for extrusion
  428. /// </summary>
  429. public class Profile
  430. {
  431. private const float twoPi = 2.0f * (float)Math.PI;
  432. public string errorMessage = null;
  433. public List<Coord> coords;
  434. public List<Face> faces;
  435. // use these for making individual meshes for each prim face
  436. public List<int> outerCoordIndices = null;
  437. public List<int> hollowCoordIndices = null;
  438. public int numOuterVerts = 0;
  439. public int numHollowVerts = 0;
  440. public int outerFaceNumber = -1;
  441. public int hollowFaceNumber = -1;
  442. public int bottomFaceNumber = 0;
  443. public int numPrimFaces = 0;
  444. public Profile()
  445. {
  446. coords = new List<Coord>();
  447. faces = new List<Face>();
  448. }
  449. public Profile(int sides, float profileStart, float profileEnd, float hollow, int hollowSides, bool hasProfileCut, bool createFaces)
  450. {
  451. const float halfSqr2 = 0.7071067811866f;
  452. coords = new List<Coord>();
  453. faces = new List<Face>();
  454. List<Coord> hollowCoords = new List<Coord>();
  455. bool hasHollow = (hollow > 0.0f);
  456. AngleList angles = new AngleList();
  457. AngleList hollowAngles = new AngleList();
  458. float xScale = 0.5f;
  459. float yScale = 0.5f;
  460. if (sides == 4) // corners of a square are sqrt(2) from center
  461. {
  462. xScale = halfSqr2;
  463. yScale = halfSqr2;
  464. }
  465. float startAngle = profileStart * twoPi;
  466. float stopAngle = profileEnd * twoPi;
  467. try { angles.makeAngles(sides, startAngle, stopAngle,hasProfileCut); }
  468. catch (Exception ex)
  469. {
  470. errorMessage = "makeAngles failed: Exception: " + ex.ToString()
  471. + "\nsides: " + sides.ToString() + " startAngle: " + startAngle.ToString() + " stopAngle: " + stopAngle.ToString();
  472. return;
  473. }
  474. numOuterVerts = angles.angles.Count;
  475. Angle angle;
  476. Coord newVert = new Coord();
  477. // flag to create as few triangles as possible for 3 or 4 side profile
  478. bool simpleFace = (sides < 5 && !hasHollow && !hasProfileCut);
  479. if (hasHollow)
  480. {
  481. if (sides == hollowSides)
  482. hollowAngles = angles;
  483. else
  484. {
  485. try { hollowAngles.makeAngles(hollowSides, startAngle, stopAngle, hasProfileCut); }
  486. catch (Exception ex)
  487. {
  488. errorMessage = "makeAngles failed: Exception: " + ex.ToString()
  489. + "\nsides: " + sides.ToString() + " startAngle: " + startAngle.ToString() + " stopAngle: " + stopAngle.ToString();
  490. return;
  491. }
  492. int numHollowAngles = hollowAngles.angles.Count;
  493. for (int i = 0; i < numHollowAngles; i++)
  494. {
  495. angle = hollowAngles.angles[i];
  496. newVert.X = hollow * xScale * angle.X;
  497. newVert.Y = hollow * yScale * angle.Y;
  498. newVert.Z = 0.0f;
  499. hollowCoords.Add(newVert);
  500. }
  501. }
  502. numHollowVerts = hollowAngles.angles.Count;
  503. }
  504. else if (!simpleFace)
  505. {
  506. Coord center = new Coord(0.0f, 0.0f, 0.0f);
  507. this.coords.Add(center);
  508. }
  509. int numAngles = angles.angles.Count;
  510. bool hollowsame = (hasHollow && hollowSides == sides);
  511. for (int i = 0; i < numAngles; i++)
  512. {
  513. angle = angles.angles[i];
  514. newVert.X = angle.X * xScale;
  515. newVert.Y = angle.Y * yScale;
  516. newVert.Z = 0.0f;
  517. coords.Add(newVert);
  518. if (hollowsame)
  519. {
  520. newVert.X *= hollow;
  521. newVert.Y *= hollow;
  522. hollowCoords.Add(newVert);
  523. }
  524. }
  525. if (hasHollow)
  526. {
  527. hollowCoords.Reverse();
  528. coords.AddRange(hollowCoords);
  529. if (createFaces)
  530. {
  531. int numTotalVerts = numOuterVerts + numHollowVerts;
  532. if (numOuterVerts == numHollowVerts)
  533. {
  534. Face newFace = new Face();
  535. for (int coordIndex = 0; coordIndex < numOuterVerts - 1; coordIndex++)
  536. {
  537. newFace.v1 = coordIndex;
  538. newFace.v2 = coordIndex + 1;
  539. newFace.v3 = numTotalVerts - coordIndex - 1;
  540. faces.Add(newFace);
  541. newFace.v1 = coordIndex + 1;
  542. newFace.v2 = numTotalVerts - coordIndex - 2;
  543. newFace.v3 = numTotalVerts - coordIndex - 1;
  544. faces.Add(newFace);
  545. }
  546. if (!hasProfileCut)
  547. {
  548. newFace.v1 = numOuterVerts - 1;
  549. newFace.v2 = 0;
  550. newFace.v3 = numOuterVerts;
  551. faces.Add(newFace);
  552. newFace.v1 = 0;
  553. newFace.v2 = numTotalVerts - 1;
  554. newFace.v3 = numOuterVerts;
  555. faces.Add(newFace);
  556. }
  557. }
  558. else if (numOuterVerts < numHollowVerts)
  559. {
  560. Face newFace = new Face();
  561. int j = 0; // j is the index for outer vertices
  562. int i;
  563. int maxJ = numOuterVerts - 1;
  564. float curHollowAngle = 0;
  565. for (i = 0; i < numHollowVerts; i++) // i is the index for inner vertices
  566. {
  567. curHollowAngle = hollowAngles.angles[i].angle;
  568. if (j < maxJ)
  569. {
  570. if (angles.angles[j + 1].angle - curHollowAngle < curHollowAngle - angles.angles[j].angle + 0.000001f)
  571. {
  572. newFace.v1 = numTotalVerts - i - 1;
  573. newFace.v2 = j;
  574. newFace.v3 = j + 1;
  575. faces.Add(newFace);
  576. j++;
  577. }
  578. }
  579. else
  580. {
  581. if (1.0f - curHollowAngle < curHollowAngle - angles.angles[j].angle + 0.000001f)
  582. break;
  583. }
  584. newFace.v1 = j;
  585. newFace.v2 = numTotalVerts - i - 2;
  586. newFace.v3 = numTotalVerts - i - 1;
  587. faces.Add(newFace);
  588. }
  589. if (!hasProfileCut)
  590. {
  591. if (i == numHollowVerts)
  592. {
  593. newFace.v1 = numTotalVerts - numHollowVerts;
  594. newFace.v2 = maxJ;
  595. newFace.v3 = 0;
  596. faces.Add(newFace);
  597. }
  598. else
  599. {
  600. if (1.0f - curHollowAngle < curHollowAngle - angles.angles[maxJ].angle + 0.000001f)
  601. {
  602. newFace.v1 = numTotalVerts - i - 1;
  603. newFace.v2 = maxJ;
  604. newFace.v3 = 0;
  605. faces.Add(newFace);
  606. }
  607. for (; i < numHollowVerts - 1; i++)
  608. {
  609. newFace.v1 = 0;
  610. newFace.v2 = numTotalVerts - i - 2;
  611. newFace.v3 = numTotalVerts - i - 1;
  612. faces.Add(newFace);
  613. }
  614. }
  615. newFace.v1 = 0;
  616. newFace.v2 = numTotalVerts - 1;
  617. newFace.v3 = numTotalVerts - numHollowVerts;
  618. faces.Add(newFace);
  619. }
  620. }
  621. else // numHollowVerts < numOuterVerts
  622. {
  623. Face newFace = new Face();
  624. int j = 0; // j is the index for inner vertices
  625. int maxJ = numHollowVerts - 1;
  626. for (int i = 0; i < numOuterVerts; i++)
  627. {
  628. if (j < maxJ)
  629. if (hollowAngles.angles[j + 1].angle - angles.angles[i].angle < angles.angles[i].angle - hollowAngles.angles[j].angle + 0.000001f)
  630. {
  631. newFace.v1 = i;
  632. newFace.v2 = numTotalVerts - j - 2;
  633. newFace.v3 = numTotalVerts - j - 1;
  634. faces.Add(newFace);
  635. j += 1;
  636. }
  637. newFace.v1 = numTotalVerts - j - 1;
  638. newFace.v2 = i;
  639. newFace.v3 = i + 1;
  640. faces.Add(newFace);
  641. }
  642. if (!hasProfileCut)
  643. {
  644. int i = numOuterVerts - 1;
  645. if (hollowAngles.angles[0].angle - angles.angles[i].angle < angles.angles[i].angle - hollowAngles.angles[maxJ].angle + 0.000001f)
  646. {
  647. newFace.v1 = 0;
  648. newFace.v2 = numTotalVerts - 1;
  649. newFace.v3 = numTotalVerts - maxJ - 1;
  650. faces.Add(newFace);
  651. }
  652. newFace.v1 = numTotalVerts - maxJ - 1;
  653. newFace.v2 = i;
  654. newFace.v3 = 0;
  655. faces.Add(newFace);
  656. }
  657. }
  658. }
  659. }
  660. else if (createFaces)
  661. {
  662. if (simpleFace)
  663. {
  664. if (sides == 3)
  665. faces.Add(new Face(0, 1, 2));
  666. else if (sides == 4)
  667. {
  668. faces.Add(new Face(0, 1, 2));
  669. faces.Add(new Face(0, 2, 3));
  670. }
  671. }
  672. else
  673. {
  674. for (int i = 1; i < numAngles ; i++)
  675. {
  676. Face newFace = new Face();
  677. newFace.v1 = 0;
  678. newFace.v2 = i;
  679. newFace.v3 = i + 1;
  680. faces.Add(newFace);
  681. }
  682. if (!hasProfileCut)
  683. {
  684. Face newFace = new Face();
  685. newFace.v1 = 0;
  686. newFace.v2 = numAngles;
  687. newFace.v3 = 1;
  688. faces.Add(newFace);
  689. }
  690. }
  691. }
  692. hollowCoords = null;
  693. }
  694. public Profile Copy()
  695. {
  696. return Copy(true);
  697. }
  698. public Profile Copy(bool needFaces)
  699. {
  700. Profile copy = new Profile();
  701. copy.coords.AddRange(coords);
  702. if (needFaces)
  703. copy.faces.AddRange(faces);
  704. copy.numOuterVerts = numOuterVerts;
  705. copy.numHollowVerts = numHollowVerts;
  706. return copy;
  707. }
  708. public void AddPos(Coord v)
  709. {
  710. this.AddPos(v.X, v.Y, v.Z);
  711. }
  712. public void AddPos(float x, float y, float z)
  713. {
  714. int i;
  715. int numVerts = coords.Count;
  716. Coord vert;
  717. for (i = 0; i < numVerts; i++)
  718. {
  719. vert = coords[i];
  720. vert.X += x;
  721. vert.Y += y;
  722. vert.Z += z;
  723. this.coords[i] = vert;
  724. }
  725. }
  726. public void AddRot(Quat q)
  727. {
  728. int i;
  729. int numVerts = coords.Count;
  730. for (i = 0; i < numVerts; i++)
  731. coords[i] *= q;
  732. }
  733. public void Scale(float x, float y)
  734. {
  735. int i;
  736. int numVerts = coords.Count;
  737. Coord vert;
  738. for (i = 0; i < numVerts; i++)
  739. {
  740. vert = coords[i];
  741. vert.X *= x;
  742. vert.X = (float)Math.Round(vert.X,5);
  743. vert.Y *= y;
  744. vert.Y = (float)Math.Round(vert.Y,5);
  745. coords[i] = vert;
  746. }
  747. if(x == 0f || y == 0f)
  748. faces = new List<Face>();
  749. }
  750. /// <summary>
  751. /// Changes order of the vertex indices and negates the center vertex normal. Does not alter vertex normals of radial vertices
  752. /// </summary>
  753. public void FlipNormals()
  754. {
  755. int numFaces = faces.Count;
  756. if(numFaces == 0)
  757. return;
  758. int i;
  759. Face tmpFace;
  760. int tmp;
  761. for (i = 0; i < numFaces; i++)
  762. {
  763. tmpFace = faces[i];
  764. tmp = tmpFace.v3;
  765. tmpFace.v3 = tmpFace.v1;
  766. tmpFace.v1 = tmp;
  767. faces[i] = tmpFace;
  768. }
  769. }
  770. public void AddValue2FaceVertexIndices(int num)
  771. {
  772. int numFaces = faces.Count;
  773. if(numFaces == 0)
  774. return;
  775. Face tmpFace;
  776. for (int i = 0; i < numFaces; i++)
  777. {
  778. tmpFace = faces[i];
  779. tmpFace.v1 += num;
  780. tmpFace.v2 += num;
  781. tmpFace.v3 += num;
  782. faces[i] = tmpFace;
  783. }
  784. }
  785. public void DumpRaw(String path, String name, String title)
  786. {
  787. if (path == null)
  788. return;
  789. String fileName = name + "_" + title + ".raw";
  790. String completePath = System.IO.Path.Combine(path, fileName);
  791. StreamWriter sw = new StreamWriter(completePath);
  792. for (int i = 0; i < faces.Count; i++)
  793. {
  794. string s = coords[faces[i].v1].ToString();
  795. s += " " + coords[faces[i].v2].ToString();
  796. s += " " + coords[faces[i].v3].ToString();
  797. sw.WriteLine(s);
  798. }
  799. sw.Close();
  800. }
  801. }
  802. public struct PathNode
  803. {
  804. public Coord position;
  805. public Quat rotation;
  806. public float xScale;
  807. public float yScale;
  808. public float percentOfPath;
  809. }
  810. public enum PathType { Linear = 0, Circular = 1, Flexible = 2 }
  811. public class Path
  812. {
  813. public List<PathNode> pathNodes = new List<PathNode>();
  814. public float twistBegin = 0.0f;
  815. public float twistEnd = 0.0f;
  816. public float topShearX = 0.0f;
  817. public float topShearY = 0.0f;
  818. public float pathCutBegin = 0.0f;
  819. public float pathCutEnd = 1.0f;
  820. public float dimpleBegin = 0.0f;
  821. public float dimpleEnd = 1.0f;
  822. public float skew = 0.0f;
  823. public float holeSizeX = 1.0f; // called pathScaleX in pbs
  824. public float holeSizeY = 0.25f;
  825. public float taperX = 0.0f;
  826. public float taperY = 0.0f;
  827. public float radius = 0.0f;
  828. public float revolutions = 1.0f;
  829. public int stepsPerRevolution = 24;
  830. private const float twoPi = 2.0f * (float)Math.PI;
  831. public void Create(PathType pathType, int steps)
  832. {
  833. if (taperX > .9999f)
  834. taperX = 1.0f;
  835. else if (taperX < -.9999f)
  836. taperX = -1.0f;
  837. if (taperY > .9999f)
  838. taperY = 1.0f;
  839. else if (taperY < -.9999f)
  840. taperY = -1.0f;
  841. if (pathType == PathType.Linear || pathType == PathType.Flexible)
  842. {
  843. int step = 0;
  844. float length = pathCutEnd - pathCutBegin;
  845. float twistTotal = twistEnd - twistBegin;
  846. float twistTotalAbs = Math.Abs(twistTotal);
  847. if (twistTotalAbs > 0.01f)
  848. steps += (int)(twistTotalAbs * 3.66); // dahlia's magic number
  849. float start = -0.5f;
  850. float stepSize = length / (float)steps;
  851. float percentOfPathMultiplier = stepSize * 0.999999f;
  852. float xOffset = topShearX * pathCutBegin;
  853. float yOffset = topShearY * pathCutBegin;
  854. float zOffset = start;
  855. float xOffsetStepIncrement = topShearX * length / steps;
  856. float yOffsetStepIncrement = topShearY * length / steps;
  857. float percentOfPath = pathCutBegin;
  858. zOffset += percentOfPath;
  859. // sanity checks
  860. bool done = false;
  861. while (!done)
  862. {
  863. PathNode newNode = new PathNode();
  864. newNode.xScale = 1.0f;
  865. if (taperX > 0.0f)
  866. newNode.xScale -= percentOfPath * taperX;
  867. else if(taperX < 0.0f)
  868. newNode.xScale += (1.0f - percentOfPath) * taperX;
  869. newNode.yScale = 1.0f;
  870. if (taperY > 0.0f)
  871. newNode.yScale -= percentOfPath * taperY;
  872. else if(taperY < 0.0f)
  873. newNode.yScale += (1.0f - percentOfPath) * taperY;
  874. float twist = twistBegin + twistTotal * percentOfPath;
  875. newNode.rotation = new Quat(new Coord(0.0f, 0.0f, 1.0f), twist);
  876. newNode.position = new Coord(xOffset, yOffset, zOffset);
  877. newNode.percentOfPath = percentOfPath;
  878. pathNodes.Add(newNode);
  879. if (step < steps)
  880. {
  881. step += 1;
  882. percentOfPath += percentOfPathMultiplier;
  883. xOffset += xOffsetStepIncrement;
  884. yOffset += yOffsetStepIncrement;
  885. zOffset += stepSize;
  886. if (percentOfPath > pathCutEnd)
  887. done = true;
  888. }
  889. else done = true;
  890. }
  891. } // end of linear path code
  892. else // pathType == Circular
  893. {
  894. float twistTotal = twistEnd - twistBegin;
  895. // if the profile has a lot of twist, add more layers otherwise the layers may overlap
  896. // and the resulting mesh may be quite inaccurate. This method is arbitrary and doesn't
  897. // accurately match the viewer
  898. float twistTotalAbs = Math.Abs(twistTotal);
  899. if (twistTotalAbs > 0.01f)
  900. {
  901. if (twistTotalAbs > Math.PI * 1.5f)
  902. steps *= 2;
  903. if (twistTotalAbs > Math.PI * 3.0f)
  904. steps *= 2;
  905. }
  906. float yPathScale = holeSizeY * 0.5f;
  907. float pathLength = pathCutEnd - pathCutBegin;
  908. float totalSkew = skew * 2.0f * pathLength;
  909. float skewStart = pathCutBegin * 2.0f * skew - skew;
  910. float xOffsetTopShearXFactor = topShearX * (0.25f + 0.5f * (0.5f - holeSizeY));
  911. float yShearCompensation = 1.0f + Math.Abs(topShearY) * 0.25f;
  912. // It's not quite clear what pushY (Y top shear) does, but subtracting it from the start and end
  913. // angles appears to approximate it's effects on path cut. Likewise, adding it to the angle used
  914. // to calculate the sine for generating the path radius appears to approximate it's effects there
  915. // too, but there are some subtle differences in the radius which are noticeable as the prim size
  916. // increases and it may affect megaprims quite a bit. The effect of the Y top shear parameter on
  917. // the meshes generated with this technique appear nearly identical in shape to the same prims when
  918. // displayed by the viewer.
  919. float startAngle = (twoPi * pathCutBegin * revolutions) - topShearY * 0.9f;
  920. float endAngle = (twoPi * pathCutEnd * revolutions) - topShearY * 0.9f;
  921. float stepSize = twoPi / stepsPerRevolution;
  922. int step = (int)(startAngle / stepSize);
  923. float angle = startAngle;
  924. bool done = false;
  925. while (!done) // loop through the length of the path and add the layers
  926. {
  927. PathNode newNode = new PathNode();
  928. float xProfileScale = (1.0f - Math.Abs(skew)) * holeSizeX;
  929. float yProfileScale = holeSizeY;
  930. float percentOfPath = angle / (twoPi * revolutions);
  931. float percentOfAngles = (angle - startAngle) / (endAngle - startAngle);
  932. if (taperX > 0.01f)
  933. xProfileScale *= 1.0f - percentOfPath * taperX;
  934. else if (taperX < -0.01f)
  935. xProfileScale *= 1.0f + (1.0f - percentOfPath) * taperX;
  936. if (taperY > 0.01f)
  937. yProfileScale *= 1.0f - percentOfPath * taperY;
  938. else if (taperY < -0.01f)
  939. yProfileScale *= 1.0f + (1.0f - percentOfPath) * taperY;
  940. newNode.xScale = xProfileScale;
  941. newNode.yScale = yProfileScale;
  942. float radiusScale = 1.0f;
  943. if (radius > 0.001f)
  944. radiusScale = 1.0f - radius * percentOfPath;
  945. else if (radius < 0.001f)
  946. radiusScale = 1.0f + radius * (1.0f - percentOfPath);
  947. float twist = twistBegin + twistTotal * percentOfPath;
  948. float xOffset = 0.5f * (skewStart + totalSkew * percentOfAngles);
  949. xOffset += (float)Math.Sin(angle) * xOffsetTopShearXFactor;
  950. float yOffset = yShearCompensation * (float)Math.Cos(angle) * (0.5f - yPathScale) * radiusScale;
  951. float zOffset = (float)Math.Sin(angle + topShearY) * (0.5f - yPathScale) * radiusScale;
  952. newNode.position = new Coord(xOffset, yOffset, zOffset);
  953. // now orient the rotation of the profile layer relative to it's position on the path
  954. // adding taperY to the angle used to generate the quat appears to approximate the viewer
  955. newNode.rotation = new Quat(new Coord(1.0f, 0.0f, 0.0f), angle + topShearY);
  956. // next apply twist rotation to the profile layer
  957. if (twistTotal != 0.0f || twistBegin != 0.0f)
  958. newNode.rotation *= new Quat(new Coord(0.0f, 0.0f, 1.0f), twist);
  959. newNode.percentOfPath = percentOfPath;
  960. pathNodes.Add(newNode);
  961. // calculate terms for next iteration
  962. // calculate the angle for the next iteration of the loop
  963. if (angle >= endAngle - 0.01)
  964. done = true;
  965. else
  966. {
  967. step += 1;
  968. angle = stepSize * step;
  969. if (angle > endAngle)
  970. angle = endAngle;
  971. }
  972. }
  973. }
  974. }
  975. }
  976. public class PrimMesh
  977. {
  978. public string errorMessage = "";
  979. private const float twoPi = 2.0f * (float)Math.PI;
  980. public List<Coord> coords;
  981. // public List<Coord> normals;
  982. public List<Face> faces;
  983. private int sides = 4;
  984. private int hollowSides = 4;
  985. private float profileStart = 0.0f;
  986. private float profileEnd = 1.0f;
  987. private float hollow = 0.0f;
  988. public int twistBegin = 0;
  989. public int twistEnd = 0;
  990. public float topShearX = 0.0f;
  991. public float topShearY = 0.0f;
  992. public float pathCutBegin = 0.0f;
  993. public float pathCutEnd = 1.0f;
  994. public float dimpleBegin = 0.0f;
  995. public float dimpleEnd = 1.0f;
  996. public float skew = 0.0f;
  997. public float holeSizeX = 1.0f; // called pathScaleX in pbs
  998. public float holeSizeY = 0.25f;
  999. public float taperX = 0.0f;
  1000. public float taperY = 0.0f;
  1001. public float radius = 0.0f;
  1002. public float revolutions = 1.0f;
  1003. public int stepsPerRevolution = 24;
  1004. private bool hasProfileCut = false;
  1005. private bool hasHollow = false;
  1006. public int numPrimFaces = 0;
  1007. /// <summary>
  1008. /// Human readable string representation of the parameters used to create a mesh.
  1009. /// </summary>
  1010. /// <returns></returns>
  1011. public string ParamsToDisplayString()
  1012. {
  1013. string s = "";
  1014. s += "sides..................: " + this.sides.ToString();
  1015. s += "\nhollowSides..........: " + this.hollowSides.ToString();
  1016. s += "\nprofileStart.........: " + this.profileStart.ToString();
  1017. s += "\nprofileEnd...........: " + this.profileEnd.ToString();
  1018. s += "\nhollow...............: " + this.hollow.ToString();
  1019. s += "\ntwistBegin...........: " + this.twistBegin.ToString();
  1020. s += "\ntwistEnd.............: " + this.twistEnd.ToString();
  1021. s += "\ntopShearX............: " + this.topShearX.ToString();
  1022. s += "\ntopShearY............: " + this.topShearY.ToString();
  1023. s += "\npathCutBegin.........: " + this.pathCutBegin.ToString();
  1024. s += "\npathCutEnd...........: " + this.pathCutEnd.ToString();
  1025. s += "\ndimpleBegin..........: " + this.dimpleBegin.ToString();
  1026. s += "\ndimpleEnd............: " + this.dimpleEnd.ToString();
  1027. s += "\nskew.................: " + this.skew.ToString();
  1028. s += "\nholeSizeX............: " + this.holeSizeX.ToString();
  1029. s += "\nholeSizeY............: " + this.holeSizeY.ToString();
  1030. s += "\ntaperX...............: " + this.taperX.ToString();
  1031. s += "\ntaperY...............: " + this.taperY.ToString();
  1032. s += "\nradius...............: " + this.radius.ToString();
  1033. s += "\nrevolutions..........: " + this.revolutions.ToString();
  1034. s += "\nstepsPerRevolution...: " + this.stepsPerRevolution.ToString();
  1035. s += "\nhasProfileCut........: " + this.hasProfileCut.ToString();
  1036. s += "\nhasHollow............: " + this.hasHollow.ToString();
  1037. return s;
  1038. }
  1039. public bool HasProfileCut
  1040. {
  1041. get { return hasProfileCut; }
  1042. set { hasProfileCut = value; }
  1043. }
  1044. public bool HasHollow
  1045. {
  1046. get { return hasHollow; }
  1047. }
  1048. /// <summary>
  1049. /// Constructs a PrimMesh object and creates the profile for extrusion.
  1050. /// </summary>
  1051. /// <param name="sides"></param>
  1052. /// <param name="profileStart"></param>
  1053. /// <param name="profileEnd"></param>
  1054. /// <param name="hollow"></param>
  1055. /// <param name="hollowSides"></param>
  1056. /// <param name="sphereMode"></param>
  1057. public PrimMesh(int _sides, float _profileStart, float _profileEnd, float _hollow, int _hollowSides)
  1058. {
  1059. coords = new List<Coord>();
  1060. faces = new List<Face>();
  1061. sides = _sides;
  1062. profileStart = _profileStart;
  1063. profileEnd = _profileEnd;
  1064. hollow = _hollow;
  1065. hollowSides = _hollowSides;
  1066. if (sides < 3)
  1067. sides = 3;
  1068. if (hollowSides < 3)
  1069. hollowSides = 3;
  1070. if (profileStart < 0.0f)
  1071. profileStart = 0.0f;
  1072. if (profileEnd > 1.0f)
  1073. profileEnd = 1.0f;
  1074. if (profileEnd < 0.02f)
  1075. profileEnd = 0.02f;
  1076. if (profileStart >= profileEnd)
  1077. profileStart = profileEnd - 0.02f;
  1078. if (hollow > 0.99f)
  1079. hollow = 0.99f;
  1080. if (hollow < 0.0f)
  1081. hollow = 0.0f;
  1082. }
  1083. /// <summary>
  1084. /// Extrudes a profile along a path.
  1085. /// </summary>
  1086. public void Extrude(PathType pathType)
  1087. {
  1088. bool needEndFaces = false;
  1089. coords = new List<Coord>();
  1090. faces = new List<Face>();
  1091. int steps = 1;
  1092. float length = pathCutEnd - pathCutBegin;
  1093. hasProfileCut = this.profileEnd - this.profileStart < 0.9999f;
  1094. hasHollow = (this.hollow > 0.001f);
  1095. float twistBegin = this.twistBegin / 360.0f * twoPi;
  1096. float twistEnd = this.twistEnd / 360.0f * twoPi;
  1097. float twistTotal = twistEnd - twistBegin;
  1098. float twistTotalAbs = Math.Abs(twistTotal);
  1099. if (twistTotalAbs > 0.01f)
  1100. steps += (int)(twistTotalAbs * 3.66); // dahlia's magic number
  1101. float hollow = this.hollow;
  1102. float initialProfileRot = 0.0f;
  1103. if (pathType == PathType.Circular)
  1104. {
  1105. needEndFaces = false;
  1106. if (pathCutBegin != 0.0f || pathCutEnd != 1.0f)
  1107. needEndFaces = true;
  1108. else if (taperX != 0.0f || taperY != 0.0f)
  1109. needEndFaces = true;
  1110. else if (skew != 0.0f)
  1111. needEndFaces = true;
  1112. else if (twistTotal != 0.0f)
  1113. needEndFaces = true;
  1114. else if (radius != 0.0f)
  1115. needEndFaces = true;
  1116. }
  1117. else needEndFaces = true;
  1118. if (pathType == PathType.Circular)
  1119. {
  1120. if (sides == 3)
  1121. {
  1122. initialProfileRot = (float)Math.PI;
  1123. if (hollowSides == 4)
  1124. {
  1125. if (hollow > 0.7f)
  1126. hollow = 0.7f;
  1127. hollow *= 0.707f;
  1128. }
  1129. else hollow *= 0.5f;
  1130. }
  1131. else if (sides == 4)
  1132. {
  1133. initialProfileRot = 0.25f * (float)Math.PI;
  1134. if (hollowSides != 4)
  1135. hollow *= 0.707f;
  1136. }
  1137. else if (sides > 4)
  1138. {
  1139. initialProfileRot = (float)Math.PI;
  1140. if (hollowSides == 4)
  1141. {
  1142. if (hollow > 0.7f)
  1143. hollow = 0.7f;
  1144. hollow /= 0.7f;
  1145. }
  1146. }
  1147. }
  1148. else
  1149. {
  1150. if (sides == 3)
  1151. {
  1152. if (hollowSides == 4)
  1153. {
  1154. if (hollow > 0.7f)
  1155. hollow = 0.7f;
  1156. hollow *= 0.707f;
  1157. }
  1158. else hollow *= 0.5f;
  1159. }
  1160. else if (sides == 4)
  1161. {
  1162. initialProfileRot = 1.25f * (float)Math.PI;
  1163. if (hollowSides != 4)
  1164. hollow *= 0.707f;
  1165. }
  1166. else if (sides == 24 && hollowSides == 4)
  1167. hollow *= 1.414f;
  1168. }
  1169. Profile profile = new Profile(sides, profileStart, profileEnd, hollow, hollowSides,
  1170. HasProfileCut,true);
  1171. errorMessage = profile.errorMessage;
  1172. numPrimFaces = profile.numPrimFaces;
  1173. if (initialProfileRot != 0.0f)
  1174. {
  1175. profile.AddRot(new Quat(new Coord(0.0f, 0.0f, 1.0f), initialProfileRot));
  1176. }
  1177. Path path = new Path();
  1178. path.twistBegin = twistBegin;
  1179. path.twistEnd = twistEnd;
  1180. path.topShearX = topShearX;
  1181. path.topShearY = topShearY;
  1182. path.pathCutBegin = pathCutBegin;
  1183. path.pathCutEnd = pathCutEnd;
  1184. path.dimpleBegin = dimpleBegin;
  1185. path.dimpleEnd = dimpleEnd;
  1186. path.skew = skew;
  1187. path.holeSizeX = holeSizeX;
  1188. path.holeSizeY = holeSizeY;
  1189. path.taperX = taperX;
  1190. path.taperY = taperY;
  1191. path.radius = radius;
  1192. path.revolutions = revolutions;
  1193. path.stepsPerRevolution = stepsPerRevolution;
  1194. path.Create(pathType, steps);
  1195. int lastNode = path.pathNodes.Count - 1;
  1196. for (int nodeIndex = 0; nodeIndex < path.pathNodes.Count; nodeIndex++)
  1197. {
  1198. PathNode node = path.pathNodes[nodeIndex];
  1199. Profile newLayer = profile.Copy();
  1200. newLayer.Scale(node.xScale, node.yScale);
  1201. newLayer.AddRot(node.rotation);
  1202. newLayer.AddPos(node.position);
  1203. // append this layer
  1204. int coordsStart = coords.Count;
  1205. coords.AddRange(newLayer.coords);
  1206. if (needEndFaces && nodeIndex == 0 && newLayer.faces.Count > 0)
  1207. {
  1208. newLayer.AddValue2FaceVertexIndices(coordsStart);
  1209. newLayer.FlipNormals();
  1210. faces.AddRange(newLayer.faces);
  1211. }
  1212. // fill faces between layers
  1213. List<Face> linkfaces = new List<Face>();
  1214. int numVerts = newLayer.coords.Count;
  1215. Face newFace1 = new Face();
  1216. Face newFace2 = new Face();
  1217. if (nodeIndex > 0)
  1218. {
  1219. int startVert = coordsStart;
  1220. int endVert = coords.Count;
  1221. if (!hasProfileCut)
  1222. {
  1223. if(numVerts > 5 && !hasHollow)
  1224. startVert++;
  1225. int i = startVert;
  1226. for (int l = 0; l < profile.numOuterVerts - 1; l++)
  1227. {
  1228. newFace1.v1 = i;
  1229. newFace1.v2 = i - numVerts;
  1230. newFace1.v3 = i + 1;
  1231. linkfaces.Add(newFace1);
  1232. newFace2.v1 = i + 1;
  1233. newFace2.v2 = i - numVerts;
  1234. newFace2.v3 = i + 1 - numVerts;
  1235. linkfaces.Add(newFace2);
  1236. i++;
  1237. }
  1238. newFace1.v1 = i;
  1239. newFace1.v2 = i - numVerts;
  1240. newFace1.v3 = startVert;
  1241. linkfaces.Add(newFace1);
  1242. newFace2.v1 = startVert;
  1243. newFace2.v2 = i - numVerts;
  1244. newFace2.v3 = startVert - numVerts;
  1245. linkfaces.Add(newFace2);
  1246. if (hasHollow)
  1247. {
  1248. startVert = ++i;
  1249. for (int l = 0; l < profile.numHollowVerts - 1; l++)
  1250. {
  1251. newFace1.v1 = i;
  1252. newFace1.v2 = i - numVerts;
  1253. newFace1.v3 = i + 1;
  1254. linkfaces.Add(newFace1);
  1255. newFace2.v1 = i + 1;
  1256. newFace2.v2 = i - numVerts;
  1257. newFace2.v3 = i + 1 - numVerts;
  1258. linkfaces.Add(newFace2);
  1259. i++;
  1260. }
  1261. newFace1.v1 = i;
  1262. newFace1.v2 = i - numVerts;
  1263. newFace1.v3 = startVert;
  1264. linkfaces.Add(newFace1);
  1265. newFace2.v1 = startVert;
  1266. newFace2.v2 = i - numVerts;
  1267. newFace2.v3 = startVert - numVerts;
  1268. linkfaces.Add(newFace2);
  1269. }
  1270. }
  1271. else
  1272. {
  1273. for (int i = startVert; i < endVert; i++)
  1274. {
  1275. int iNext = i + 1;
  1276. if (i == endVert - 1)
  1277. iNext = startVert;
  1278. newFace1.v1 = i;
  1279. newFace1.v2 = i - numVerts;
  1280. newFace1.v3 = iNext;
  1281. linkfaces.Add(newFace1);
  1282. newFace2.v1 = iNext;
  1283. newFace2.v2 = i - numVerts;
  1284. newFace2.v3 = iNext - numVerts;
  1285. linkfaces.Add(newFace2);
  1286. }
  1287. }
  1288. }
  1289. if(linkfaces.Count > 0)
  1290. faces.AddRange(linkfaces);
  1291. if (needEndFaces && nodeIndex == lastNode && newLayer.faces.Count > 0)
  1292. {
  1293. newLayer.AddValue2FaceVertexIndices(coordsStart);
  1294. faces.AddRange(newLayer.faces);
  1295. }
  1296. } // for (int nodeIndex = 0; nodeIndex < path.pathNodes.Count; nodeIndex++)
  1297. // more cleanup will be done at Meshmerizer.cs
  1298. }
  1299. /// <summary>
  1300. /// DEPRICATED - use Extrude(PathType.Linear) instead
  1301. /// Extrudes a profile along a straight line path. Used for prim types box, cylinder, and prism.
  1302. /// </summary>
  1303. ///
  1304. public void ExtrudeLinear()
  1305. {
  1306. Extrude(PathType.Linear);
  1307. }
  1308. /// <summary>
  1309. /// DEPRICATED - use Extrude(PathType.Circular) instead
  1310. /// Extrude a profile into a circular path prim mesh. Used for prim types torus, tube, and ring.
  1311. /// </summary>
  1312. ///
  1313. public void ExtrudeCircular()
  1314. {
  1315. Extrude(PathType.Circular);
  1316. }
  1317. private Coord SurfaceNormal(Coord c1, Coord c2, Coord c3)
  1318. {
  1319. Coord edge1 = new Coord(c2.X - c1.X, c2.Y - c1.Y, c2.Z - c1.Z);
  1320. Coord edge2 = new Coord(c3.X - c1.X, c3.Y - c1.Y, c3.Z - c1.Z);
  1321. Coord normal = Coord.Cross(edge1, edge2);
  1322. normal.Normalize();
  1323. return normal;
  1324. }
  1325. private Coord SurfaceNormal(Face face)
  1326. {
  1327. return SurfaceNormal(this.coords[face.v1], this.coords[face.v2], this.coords[face.v3]);
  1328. }
  1329. /// <summary>
  1330. /// Calculate the surface normal for a face in the list of faces
  1331. /// </summary>
  1332. /// <param name="faceIndex"></param>
  1333. /// <returns></returns>
  1334. public Coord SurfaceNormal(int faceIndex)
  1335. {
  1336. int numFaces = this.faces.Count;
  1337. if (faceIndex < 0 || faceIndex >= numFaces)
  1338. throw new Exception("faceIndex out of range");
  1339. return SurfaceNormal(this.faces[faceIndex]);
  1340. }
  1341. /// <summary>
  1342. /// Duplicates a PrimMesh object. All object properties are copied by value, including lists.
  1343. /// </summary>
  1344. /// <returns></returns>
  1345. public PrimMesh Copy()
  1346. {
  1347. PrimMesh copy = new PrimMesh(this.sides, this.profileStart, this.profileEnd, this.hollow, this.hollowSides);
  1348. copy.twistBegin = this.twistBegin;
  1349. copy.twistEnd = this.twistEnd;
  1350. copy.topShearX = this.topShearX;
  1351. copy.topShearY = this.topShearY;
  1352. copy.pathCutBegin = this.pathCutBegin;
  1353. copy.pathCutEnd = this.pathCutEnd;
  1354. copy.dimpleBegin = this.dimpleBegin;
  1355. copy.dimpleEnd = this.dimpleEnd;
  1356. copy.skew = this.skew;
  1357. copy.holeSizeX = this.holeSizeX;
  1358. copy.holeSizeY = this.holeSizeY;
  1359. copy.taperX = this.taperX;
  1360. copy.taperY = this.taperY;
  1361. copy.radius = this.radius;
  1362. copy.revolutions = this.revolutions;
  1363. copy.stepsPerRevolution = this.stepsPerRevolution;
  1364. copy.numPrimFaces = this.numPrimFaces;
  1365. copy.errorMessage = this.errorMessage;
  1366. copy.coords = new List<Coord>(this.coords);
  1367. copy.faces = new List<Face>(this.faces);
  1368. return copy;
  1369. }
  1370. /// <summary>
  1371. /// Adds a value to each XYZ vertex coordinate in the mesh
  1372. /// </summary>
  1373. /// <param name="x"></param>
  1374. /// <param name="y"></param>
  1375. /// <param name="z"></param>
  1376. public void AddPos(float x, float y, float z)
  1377. {
  1378. int i;
  1379. int numVerts = this.coords.Count;
  1380. Coord vert;
  1381. for (i = 0; i < numVerts; i++)
  1382. {
  1383. vert = this.coords[i];
  1384. vert.X += x;
  1385. vert.Y += y;
  1386. vert.Z += z;
  1387. this.coords[i] = vert;
  1388. }
  1389. }
  1390. /// <summary>
  1391. /// Rotates the mesh
  1392. /// </summary>
  1393. /// <param name="q"></param>
  1394. public void AddRot(Quat q)
  1395. {
  1396. int i;
  1397. int numVerts = this.coords.Count;
  1398. for (i = 0; i < numVerts; i++)
  1399. this.coords[i] *= q;
  1400. }
  1401. #if VERTEX_INDEXER
  1402. public VertexIndexer GetVertexIndexer()
  1403. {
  1404. return null;
  1405. }
  1406. #endif
  1407. /// <summary>
  1408. /// Scales the mesh
  1409. /// </summary>
  1410. /// <param name="x"></param>
  1411. /// <param name="y"></param>
  1412. /// <param name="z"></param>
  1413. public void Scale(float x, float y, float z)
  1414. {
  1415. int i;
  1416. int numVerts = this.coords.Count;
  1417. //Coord vert;
  1418. Coord m = new Coord(x, y, z);
  1419. for (i = 0; i < numVerts; i++)
  1420. this.coords[i] *= m;
  1421. }
  1422. /// <summary>
  1423. /// Dumps the mesh to a Blender compatible "Raw" format file
  1424. /// </summary>
  1425. /// <param name="path"></param>
  1426. /// <param name="name"></param>
  1427. /// <param name="title"></param>
  1428. public void DumpRaw(String path, String name, String title)
  1429. {
  1430. if (path == null)
  1431. return;
  1432. String fileName = name + "_" + title + ".raw";
  1433. String completePath = System.IO.Path.Combine(path, fileName);
  1434. StreamWriter sw = new StreamWriter(completePath);
  1435. for (int i = 0; i < this.faces.Count; i++)
  1436. {
  1437. string s = this.coords[this.faces[i].v1].ToString();
  1438. s += " " + this.coords[this.faces[i].v2].ToString();
  1439. s += " " + this.coords[this.faces[i].v3].ToString();
  1440. sw.WriteLine(s);
  1441. }
  1442. sw.Close();
  1443. }
  1444. }
  1445. }