PrimMesher.cs 61 KB

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