PrimMesher.cs 59 KB

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