PrimMesher.cs 58 KB

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