PrimMesher.cs 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176
  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 OpenSim 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 override string ToString()
  89. {
  90. return "< X: " + this.X.ToString() + ", Y: " + this.Y.ToString() + ", Z: " + this.Z.ToString() + ", W: " + this.W.ToString() + ">";
  91. }
  92. }
  93. public struct Coord
  94. {
  95. public float X;
  96. public float Y;
  97. public float Z;
  98. public Coord(float x, float y, float z)
  99. {
  100. this.X = x;
  101. this.Y = y;
  102. this.Z = z;
  103. }
  104. public float Length()
  105. {
  106. return (float)Math.Sqrt(this.X * this.X + this.Y * this.Y + this.Z * this.Z);
  107. }
  108. public Coord Invert()
  109. {
  110. this.X = -this.X;
  111. this.Y = -this.Y;
  112. this.Z = -this.Z;
  113. return this;
  114. }
  115. public Coord Normalize()
  116. {
  117. const float MAG_THRESHOLD = 0.0000001f;
  118. float mag = Length();
  119. // Catch very small rounding errors when normalizing
  120. if (mag > MAG_THRESHOLD)
  121. {
  122. float oomag = 1.0f / mag;
  123. this.X *= oomag;
  124. this.Y *= oomag;
  125. this.Z *= oomag;
  126. }
  127. else
  128. {
  129. this.X = 0.0f;
  130. this.Y = 0.0f;
  131. this.Z = 0.0f;
  132. }
  133. return this;
  134. }
  135. public override string ToString()
  136. {
  137. return this.X.ToString() + " " + this.Y.ToString() + " " + this.Z.ToString();
  138. }
  139. public static Coord Cross(Coord c1, Coord c2)
  140. {
  141. return new Coord(
  142. c1.Y * c2.Z - c2.Y * c1.Z,
  143. c1.Z * c2.X - c2.Z * c1.X,
  144. c1.X * c2.Y - c2.X * c1.Y
  145. );
  146. }
  147. public static Coord operator +(Coord v, Coord a)
  148. {
  149. return new Coord(v.X + a.X, v.Y + a.Y, v.Z + a.Z);
  150. }
  151. public static Coord operator *(Coord v, Coord m)
  152. {
  153. return new Coord(v.X * m.X, v.Y * m.Y, v.Z * m.Z);
  154. }
  155. public static Coord operator *(Coord v, Quat q)
  156. {
  157. // From http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/transforms/
  158. Coord c2 = new Coord(0.0f, 0.0f, 0.0f);
  159. c2.X = q.W * q.W * v.X +
  160. 2f * q.Y * q.W * v.Z -
  161. 2f * q.Z * q.W * v.Y +
  162. q.X * q.X * v.X +
  163. 2f * q.Y * q.X * v.Y +
  164. 2f * q.Z * q.X * v.Z -
  165. q.Z * q.Z * v.X -
  166. q.Y * q.Y * v.X;
  167. c2.Y =
  168. 2f * q.X * q.Y * v.X +
  169. q.Y * q.Y * v.Y +
  170. 2f * q.Z * q.Y * v.Z +
  171. 2f * q.W * q.Z * v.X -
  172. q.Z * q.Z * v.Y +
  173. q.W * q.W * v.Y -
  174. 2f * q.X * q.W * v.Z -
  175. q.X * q.X * v.Y;
  176. c2.Z =
  177. 2f * q.X * q.Z * v.X +
  178. 2f * q.Y * q.Z * v.Y +
  179. q.Z * q.Z * v.Z -
  180. 2f * q.W * q.Y * v.X -
  181. q.Y * q.Y * v.Z +
  182. 2f * q.W * q.X * v.Y -
  183. q.X * q.X * v.Z +
  184. q.W * q.W * v.Z;
  185. return c2;
  186. }
  187. }
  188. public struct UVCoord
  189. {
  190. public float U;
  191. public float V;
  192. public UVCoord(float u, float v)
  193. {
  194. this.U = u;
  195. this.V = v;
  196. }
  197. }
  198. public struct Face
  199. {
  200. public int primFace;
  201. // vertices
  202. public int v1;
  203. public int v2;
  204. public int v3;
  205. //normals
  206. public int n1;
  207. public int n2;
  208. public int n3;
  209. // uvs
  210. public int uv1;
  211. public int uv2;
  212. public int uv3;
  213. public Face(int v1, int v2, int v3)
  214. {
  215. primFace = 0;
  216. this.v1 = v1;
  217. this.v2 = v2;
  218. this.v3 = v3;
  219. this.n1 = 0;
  220. this.n2 = 0;
  221. this.n3 = 0;
  222. this.uv1 = 0;
  223. this.uv2 = 0;
  224. this.uv3 = 0;
  225. }
  226. public Face(int v1, int v2, int v3, int n1, int n2, int n3)
  227. {
  228. primFace = 0;
  229. this.v1 = v1;
  230. this.v2 = v2;
  231. this.v3 = v3;
  232. this.n1 = n1;
  233. this.n2 = n2;
  234. this.n3 = n3;
  235. this.uv1 = 0;
  236. this.uv2 = 0;
  237. this.uv3 = 0;
  238. }
  239. public Coord SurfaceNormal(List<Coord> coordList)
  240. {
  241. Coord c1 = coordList[this.v1];
  242. Coord c2 = coordList[this.v2];
  243. Coord c3 = coordList[this.v3];
  244. Coord edge1 = new Coord(c2.X - c1.X, c2.Y - c1.Y, c2.Z - c1.Z);
  245. Coord edge2 = new Coord(c3.X - c1.X, c3.Y - c1.Y, c3.Z - c1.Z);
  246. return Coord.Cross(edge1, edge2).Normalize();
  247. }
  248. }
  249. public struct ViewerFace
  250. {
  251. public int primFaceNumber;
  252. public Coord v1;
  253. public Coord v2;
  254. public Coord v3;
  255. public Coord n1;
  256. public Coord n2;
  257. public Coord n3;
  258. public UVCoord uv1;
  259. public UVCoord uv2;
  260. public UVCoord uv3;
  261. public ViewerFace(int primFaceNumber)
  262. {
  263. this.primFaceNumber = primFaceNumber;
  264. this.v1 = new Coord();
  265. this.v2 = new Coord();
  266. this.v3 = new Coord();
  267. this.n1 = new Coord();
  268. this.n2 = new Coord();
  269. this.n3 = new Coord();
  270. this.uv1 = new UVCoord();
  271. this.uv2 = new UVCoord();
  272. this.uv3 = new UVCoord();
  273. }
  274. public void Scale(float x, float y, float z)
  275. {
  276. this.v1.X *= x;
  277. this.v1.Y *= y;
  278. this.v1.Z *= z;
  279. this.v2.X *= x;
  280. this.v2.Y *= y;
  281. this.v2.Z *= z;
  282. this.v3.X *= x;
  283. this.v3.Y *= y;
  284. this.v3.Z *= z;
  285. }
  286. public void AddRot(Quat q)
  287. {
  288. this.v1 *= q;
  289. this.v2 *= q;
  290. this.v3 *= q;
  291. this.n1 *= q;
  292. this.n2 *= q;
  293. this.n3 *= q;
  294. }
  295. public void CalcSurfaceNormal()
  296. {
  297. Coord edge1 = new Coord(this.v2.X - this.v1.X, this.v2.Y - this.v1.Y, this.v2.Z - this.v1.Z);
  298. Coord edge2 = new Coord(this.v3.X - this.v1.X, this.v3.Y - this.v1.Y, this.v3.Z - this.v1.Z);
  299. this.n1 = this.n2 = this.n3 = Coord.Cross(edge1, edge2).Normalize();
  300. }
  301. }
  302. internal struct Angle
  303. {
  304. internal float angle;
  305. internal float X;
  306. internal float Y;
  307. internal Angle(float angle, float x, float y)
  308. {
  309. this.angle = angle;
  310. this.X = x;
  311. this.Y = y;
  312. }
  313. }
  314. internal class AngleList
  315. {
  316. private float iX, iY; // intersection point
  317. private Angle[] angles3 =
  318. {
  319. new Angle(0.0f, 1.0f, 0.0f),
  320. new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
  321. new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
  322. new Angle(1.0f, 1.0f, 0.0f)
  323. };
  324. private Coord[] normals3 =
  325. {
  326. new Coord(0.25f, 0.4330127019f, 0.0f).Normalize(),
  327. new Coord(-0.5f, 0.0f, 0.0f).Normalize(),
  328. new Coord(0.25f, -0.4330127019f, 0.0f).Normalize(),
  329. new Coord(0.25f, 0.4330127019f, 0.0f).Normalize()
  330. };
  331. private Angle[] angles4 =
  332. {
  333. new Angle(0.0f, 1.0f, 0.0f),
  334. new Angle(0.25f, 0.0f, 1.0f),
  335. new Angle(0.5f, -1.0f, 0.0f),
  336. new Angle(0.75f, 0.0f, -1.0f),
  337. new Angle(1.0f, 1.0f, 0.0f)
  338. };
  339. private Coord[] normals4 =
  340. {
  341. new Coord(0.5f, 0.5f, 0.0f).Normalize(),
  342. new Coord(-0.5f, 0.5f, 0.0f).Normalize(),
  343. new Coord(-0.5f, -0.5f, 0.0f).Normalize(),
  344. new Coord(0.5f, -0.5f, 0.0f).Normalize(),
  345. new Coord(0.5f, 0.5f, 0.0f).Normalize()
  346. };
  347. private Angle[] angles24 =
  348. {
  349. new Angle(0.0f, 1.0f, 0.0f),
  350. new Angle(0.041666666666666664f, 0.96592582628906831f, 0.25881904510252074f),
  351. new Angle(0.083333333333333329f, 0.86602540378443871f, 0.5f),
  352. new Angle(0.125f, 0.70710678118654757f, 0.70710678118654746f),
  353. new Angle(0.16666666666666667f, 0.5f, 0.8660254037844386f),
  354. new Angle(0.20833333333333331f, 0.25881904510252096f, 0.9659258262890682f),
  355. new Angle(0.25f, 0.0f, 1.0f),
  356. new Angle(0.29166666666666663f, -0.25881904510252063f, 0.96592582628906831f),
  357. new Angle(0.33333333333333333f, -0.5f, 0.86602540378443871f),
  358. new Angle(0.375f, -0.70710678118654746f, 0.70710678118654757f),
  359. new Angle(0.41666666666666663f, -0.86602540378443849f, 0.5f),
  360. new Angle(0.45833333333333331f, -0.9659258262890682f, 0.25881904510252102f),
  361. new Angle(0.5f, -1.0f, 0.0f),
  362. new Angle(0.54166666666666663f, -0.96592582628906842f, -0.25881904510252035f),
  363. new Angle(0.58333333333333326f, -0.86602540378443882f, -0.5f),
  364. new Angle(0.62499999999999989f, -0.70710678118654791f, -0.70710678118654713f),
  365. new Angle(0.66666666666666667f, -0.5f, -0.86602540378443837f),
  366. new Angle(0.70833333333333326f, -0.25881904510252152f, -0.96592582628906809f),
  367. new Angle(0.75f, 0.0f, -1.0f),
  368. new Angle(0.79166666666666663f, 0.2588190451025203f, -0.96592582628906842f),
  369. new Angle(0.83333333333333326f, 0.5f, -0.86602540378443904f),
  370. new Angle(0.875f, 0.70710678118654735f, -0.70710678118654768f),
  371. new Angle(0.91666666666666663f, 0.86602540378443837f, -0.5f),
  372. new Angle(0.95833333333333326f, 0.96592582628906809f, -0.25881904510252157f),
  373. new Angle(1.0f, 1.0f, 0.0f)
  374. };
  375. private Angle interpolatePoints(float newPoint, Angle p1, Angle p2)
  376. {
  377. float m = (newPoint - p1.angle) / (p2.angle - p1.angle);
  378. return new Angle(newPoint, p1.X + m * (p2.X - p1.X), p1.Y + m * (p2.Y - p1.Y));
  379. }
  380. private void intersection(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4)
  381. { // ref: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
  382. double denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1);
  383. double uaNumerator = (x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3);
  384. if (denom != 0.0)
  385. {
  386. double ua = uaNumerator / denom;
  387. iX = (float)(x1 + ua * (x2 - x1));
  388. iY = (float)(y1 + ua * (y2 - y1));
  389. }
  390. }
  391. internal List<Angle> angles;
  392. internal List<Coord> normals;
  393. internal void makeAngles(int sides, float startAngle, float stopAngle)
  394. {
  395. angles = new List<Angle>();
  396. normals = new List<Coord>();
  397. double twoPi = System.Math.PI * 2.0;
  398. float twoPiInv = 1.0f / (float)twoPi;
  399. if (sides < 1)
  400. throw new Exception("number of sides not greater than zero");
  401. if (stopAngle <= startAngle)
  402. throw new Exception("stopAngle not greater than startAngle");
  403. if ((sides == 3 || sides == 4 || sides == 24))
  404. {
  405. startAngle *= twoPiInv;
  406. stopAngle *= twoPiInv;
  407. Angle[] sourceAngles;
  408. if (sides == 3)
  409. sourceAngles = angles3;
  410. else if (sides == 4)
  411. sourceAngles = angles4;
  412. else sourceAngles = angles24;
  413. int startAngleIndex = (int)(startAngle * sides);
  414. int endAngleIndex = sourceAngles.Length - 1;
  415. if (stopAngle < 1.0f)
  416. endAngleIndex = (int)(stopAngle * sides) + 1;
  417. if (endAngleIndex == startAngleIndex)
  418. endAngleIndex++;
  419. for (int angleIndex = startAngleIndex; angleIndex < endAngleIndex + 1; angleIndex++)
  420. {
  421. angles.Add(sourceAngles[angleIndex]);
  422. if (sides == 3)
  423. normals.Add(normals3[angleIndex]);
  424. else if (sides == 4)
  425. normals.Add(normals4[angleIndex]);
  426. }
  427. if (startAngle > 0.0f)
  428. angles[0] = interpolatePoints(startAngle, angles[0], angles[1]);
  429. if (stopAngle < 1.0f)
  430. {
  431. int lastAngleIndex = angles.Count - 1;
  432. angles[lastAngleIndex] = interpolatePoints(stopAngle, angles[lastAngleIndex - 1], angles[lastAngleIndex]);
  433. }
  434. }
  435. else
  436. {
  437. double stepSize = twoPi / sides;
  438. int startStep = (int)(startAngle / stepSize);
  439. double angle = stepSize * startStep;
  440. int step = startStep;
  441. double stopAngleTest = stopAngle;
  442. if (stopAngle < twoPi)
  443. {
  444. stopAngleTest = stepSize * ((int)(stopAngle / stepSize) + 1);
  445. if (stopAngleTest < stopAngle)
  446. stopAngleTest += stepSize;
  447. if (stopAngleTest > twoPi)
  448. stopAngleTest = twoPi;
  449. }
  450. while (angle <= stopAngleTest)
  451. {
  452. Angle newAngle;
  453. newAngle.angle = (float)angle;
  454. newAngle.X = (float)System.Math.Cos(angle);
  455. newAngle.Y = (float)System.Math.Sin(angle);
  456. angles.Add(newAngle);
  457. step += 1;
  458. angle = stepSize * step;
  459. }
  460. if (startAngle > angles[0].angle)
  461. {
  462. Angle newAngle;
  463. 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));
  464. newAngle.angle = startAngle;
  465. newAngle.X = iX;
  466. newAngle.Y = iY;
  467. angles[0] = newAngle;
  468. }
  469. int index = angles.Count - 1;
  470. if (stopAngle < angles[index].angle)
  471. {
  472. Angle newAngle;
  473. 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));
  474. newAngle.angle = stopAngle;
  475. newAngle.X = iX;
  476. newAngle.Y = iY;
  477. angles[index] = newAngle;
  478. }
  479. }
  480. }
  481. }
  482. /// <summary>
  483. /// generates a profile for extrusion
  484. /// </summary>
  485. internal class Profile
  486. {
  487. private const float twoPi = 2.0f * (float)Math.PI;
  488. internal List<Coord> coords;
  489. internal List<Face> faces;
  490. internal List<Coord> vertexNormals;
  491. internal List<float> us;
  492. internal List<UVCoord> faceUVs;
  493. internal List<int> faceNumbers;
  494. internal Coord faceNormal = new Coord(0.0f, 0.0f, 1.0f);
  495. internal Coord cutNormal1 = new Coord();
  496. internal Coord cutNormal2 = new Coord();
  497. internal int numOuterVerts = 0;
  498. internal int numHollowVerts = 0;
  499. internal bool calcVertexNormals = false;
  500. internal int bottomFaceNumber = 0;
  501. internal int numPrimFaces = 0;
  502. internal Profile()
  503. {
  504. this.coords = new List<Coord>();
  505. this.faces = new List<Face>();
  506. this.vertexNormals = new List<Coord>();
  507. this.us = new List<float>();
  508. this.faceUVs = new List<UVCoord>();
  509. this.faceNumbers = new List<int>();
  510. }
  511. internal Profile(int sides, float profileStart, float profileEnd, float hollow, int hollowSides, bool createFaces, bool calcVertexNormals)
  512. {
  513. this.calcVertexNormals = calcVertexNormals;
  514. this.coords = new List<Coord>();
  515. this.faces = new List<Face>();
  516. this.vertexNormals = new List<Coord>();
  517. this.us = new List<float>();
  518. this.faceUVs = new List<UVCoord>();
  519. this.faceNumbers = new List<int>();
  520. Coord center = new Coord(0.0f, 0.0f, 0.0f);
  521. List<Coord> hollowCoords = new List<Coord>();
  522. List<Coord> hollowNormals = new List<Coord>();
  523. List<float> hollowUs = new List<float>();
  524. bool hasHollow = (hollow > 0.0f);
  525. bool hasProfileCut = (profileStart > 0.0f || profileEnd < 1.0f);
  526. AngleList angles = new AngleList();
  527. AngleList hollowAngles = new AngleList();
  528. float xScale = 0.5f;
  529. float yScale = 0.5f;
  530. if (sides == 4) // corners of a square are sqrt(2) from center
  531. {
  532. xScale = 0.707f;
  533. yScale = 0.707f;
  534. }
  535. float startAngle = profileStart * twoPi;
  536. float stopAngle = profileEnd * twoPi;
  537. try { angles.makeAngles(sides, startAngle, stopAngle); }
  538. catch (Exception ex)
  539. {
  540. Console.WriteLine("makeAngles failed: Exception: " + ex.ToString());
  541. Console.WriteLine("sides: " + sides.ToString() + " startAngle: " + startAngle.ToString() + " stopAngle: " + stopAngle.ToString());
  542. return;
  543. }
  544. this.numOuterVerts = angles.angles.Count;
  545. // flag to create as few triangles as possible for 3 or 4 side profile
  546. //bool simpleFace = (sides < 5 && !(hasHollow || hasProfileCut));
  547. bool simpleFace = (sides < 5 && !hasHollow && !hasProfileCut);
  548. if (hasHollow)
  549. {
  550. if (sides == hollowSides)
  551. hollowAngles = angles;
  552. else
  553. {
  554. try { hollowAngles.makeAngles(hollowSides, startAngle, stopAngle); }
  555. catch (Exception ex)
  556. {
  557. Console.WriteLine("makeAngles failed: Exception: " + ex.ToString());
  558. Console.WriteLine("sides: " + sides.ToString() + " startAngle: " + startAngle.ToString() + " stopAngle: " + stopAngle.ToString());
  559. return;
  560. }
  561. }
  562. this.numHollowVerts = hollowAngles.angles.Count;
  563. }
  564. else if (!simpleFace)
  565. {
  566. this.coords.Add(center);
  567. if (this.calcVertexNormals)
  568. this.vertexNormals.Add(new Coord(0.0f, 0.0f, 1.0f));
  569. this.us.Add(0.0f);
  570. }
  571. float z = 0.0f;
  572. Angle angle;
  573. Coord newVert = new Coord();
  574. if (hasHollow && hollowSides != sides)
  575. {
  576. int numHollowAngles = hollowAngles.angles.Count;
  577. for (int i = 0; i < numHollowAngles; i++)
  578. {
  579. angle = hollowAngles.angles[i];
  580. newVert.X = hollow * xScale * angle.X;
  581. newVert.Y = hollow * yScale * angle.Y;
  582. newVert.Z = z;
  583. hollowCoords.Add(newVert);
  584. if (this.calcVertexNormals)
  585. {
  586. if (hollowSides < 5)
  587. hollowNormals.Add(hollowAngles.normals[i].Invert());
  588. else
  589. hollowNormals.Add(new Coord(-angle.X, -angle.Y, 0.0f));
  590. hollowUs.Add(angle.angle * hollow);
  591. }
  592. }
  593. }
  594. int index = 0;
  595. int numAngles = angles.angles.Count;
  596. for (int i = 0; i < numAngles; i++)
  597. {
  598. //int iNext = i == numAngles ? i + 1 : 0;
  599. angle = angles.angles[i];
  600. newVert.X = angle.X * xScale;
  601. newVert.Y = angle.Y * yScale;
  602. newVert.Z = z;
  603. this.coords.Add(newVert);
  604. if (this.calcVertexNormals)
  605. {
  606. if (sides < 5)
  607. {
  608. this.vertexNormals.Add(angles.normals[i]);
  609. float u = angle.angle;
  610. this.us.Add(u);
  611. }
  612. else
  613. {
  614. this.vertexNormals.Add(new Coord(angle.X, angle.Y, 0.0f));
  615. this.us.Add(angle.angle);
  616. }
  617. }
  618. if (hasHollow)
  619. {
  620. if (hollowSides == sides)
  621. {
  622. newVert.X *= hollow;
  623. newVert.Y *= hollow;
  624. newVert.Z = z;
  625. hollowCoords.Add(newVert);
  626. if (this.calcVertexNormals)
  627. {
  628. if (sides < 5)
  629. {
  630. hollowNormals.Add(angles.normals[i].Invert());
  631. }
  632. else
  633. hollowNormals.Add(new Coord(-angle.X, -angle.Y, 0.0f));
  634. hollowUs.Add(angle.angle * hollow);
  635. }
  636. }
  637. }
  638. else if (!simpleFace && createFaces && angle.angle > 0.0001f)
  639. {
  640. Face newFace = new Face();
  641. newFace.v1 = 0;
  642. newFace.v2 = index;
  643. newFace.v3 = index + 1;
  644. this.faces.Add(newFace);
  645. }
  646. index += 1;
  647. }
  648. if (hasHollow)
  649. {
  650. hollowCoords.Reverse();
  651. if (this.calcVertexNormals)
  652. {
  653. hollowNormals.Reverse();
  654. hollowUs.Reverse();
  655. }
  656. if (createFaces)
  657. {
  658. int numOuterVerts = this.coords.Count;
  659. int numHollowVerts = hollowCoords.Count;
  660. int numTotalVerts = numOuterVerts + numHollowVerts;
  661. if (numOuterVerts == numHollowVerts)
  662. {
  663. Face newFace = new Face();
  664. for (int coordIndex = 0; coordIndex < numOuterVerts - 1; coordIndex++)
  665. {
  666. newFace.v1 = coordIndex;
  667. newFace.v2 = coordIndex + 1;
  668. newFace.v3 = numTotalVerts - coordIndex - 1;
  669. this.faces.Add(newFace);
  670. newFace.v1 = coordIndex + 1;
  671. newFace.v2 = numTotalVerts - coordIndex - 2;
  672. newFace.v3 = numTotalVerts - coordIndex - 1;
  673. this.faces.Add(newFace);
  674. }
  675. }
  676. else
  677. {
  678. if (numOuterVerts < numHollowVerts)
  679. {
  680. Face newFace = new Face();
  681. int j = 0; // j is the index for outer vertices
  682. int maxJ = numOuterVerts - 1;
  683. for (int i = 0; i < numHollowVerts; i++) // i is the index for inner vertices
  684. {
  685. if (j < maxJ)
  686. if (angles.angles[j + 1].angle - hollowAngles.angles[i].angle < hollowAngles.angles[i].angle - angles.angles[j].angle + 0.000001f)
  687. {
  688. newFace.v1 = numTotalVerts - i - 1;
  689. newFace.v2 = j;
  690. newFace.v3 = j + 1;
  691. this.faces.Add(newFace);
  692. j += 1;
  693. }
  694. newFace.v1 = j;
  695. newFace.v2 = numTotalVerts - i - 2;
  696. newFace.v3 = numTotalVerts - i - 1;
  697. this.faces.Add(newFace);
  698. }
  699. }
  700. else // numHollowVerts < numOuterVerts
  701. {
  702. Face newFace = new Face();
  703. int j = 0; // j is the index for inner vertices
  704. int maxJ = numHollowVerts - 1;
  705. for (int i = 0; i < numOuterVerts; i++)
  706. {
  707. if (j < maxJ)
  708. if (hollowAngles.angles[j + 1].angle - angles.angles[i].angle < angles.angles[i].angle - hollowAngles.angles[j].angle + 0.000001f)
  709. {
  710. newFace.v1 = i;
  711. newFace.v2 = numTotalVerts - j - 2;
  712. newFace.v3 = numTotalVerts - j - 1;
  713. this.faces.Add(newFace);
  714. j += 1;
  715. }
  716. newFace.v1 = numTotalVerts - j - 1;
  717. newFace.v2 = i;
  718. newFace.v3 = i + 1;
  719. this.faces.Add(newFace);
  720. }
  721. }
  722. }
  723. }
  724. this.coords.AddRange(hollowCoords);
  725. if (this.calcVertexNormals)
  726. {
  727. this.vertexNormals.AddRange(hollowNormals);
  728. this.us.AddRange(hollowUs);
  729. }
  730. }
  731. if (simpleFace && createFaces)
  732. {
  733. if (sides == 3)
  734. this.faces.Add(new Face(0, 1, 2));
  735. else if (sides == 4)
  736. {
  737. this.faces.Add(new Face(0, 1, 2));
  738. this.faces.Add(new Face(0, 2, 3));
  739. }
  740. }
  741. if (calcVertexNormals && hasProfileCut)
  742. {
  743. if (hasHollow)
  744. {
  745. int lastOuterVertIndex = this.numOuterVerts - 1;
  746. this.cutNormal1.X = this.coords[0].Y - this.coords[this.coords.Count - 1].Y;
  747. this.cutNormal1.Y = -(this.coords[0].X - this.coords[this.coords.Count - 1].X);
  748. this.cutNormal2.X = this.coords[lastOuterVertIndex + 1].Y - this.coords[lastOuterVertIndex].Y;
  749. this.cutNormal2.Y = -(this.coords[lastOuterVertIndex + 1].X - this.coords[lastOuterVertIndex].X);
  750. }
  751. else
  752. {
  753. this.cutNormal1.X = this.vertexNormals[1].Y;
  754. this.cutNormal1.Y = -this.vertexNormals[1].X;
  755. this.cutNormal2.X = -this.vertexNormals[this.vertexNormals.Count - 2].Y;
  756. this.cutNormal2.Y = this.vertexNormals[this.vertexNormals.Count - 2].X;
  757. }
  758. this.cutNormal1.Normalize();
  759. this.cutNormal2.Normalize();
  760. }
  761. this.MakeFaceUVs();
  762. hollowCoords = null;
  763. hollowNormals = null;
  764. hollowUs = null;
  765. if (calcVertexNormals)
  766. { // calculate prim face numbers
  767. // I know it's ugly but so is the whole concept of prim face numbers
  768. int faceNum = 1;
  769. int startVert = hasProfileCut && !hasHollow ? 1 : 0;
  770. if (startVert > 0)
  771. this.faceNumbers.Add(0);
  772. for (int i = 0; i < numOuterVerts; i++)
  773. this.faceNumbers.Add(sides < 5 ? faceNum++ : faceNum);
  774. if (sides > 4)
  775. faceNum++;
  776. if (hasProfileCut)
  777. this.faceNumbers.Add(0);
  778. for (int i = 0; i < numHollowVerts; i++)
  779. this.faceNumbers.Add(faceNum++);
  780. this.bottomFaceNumber = faceNum++;
  781. if (hasHollow && hasProfileCut)
  782. this.faceNumbers.Add(faceNum++);
  783. for (int i = 0; i < this.faceNumbers.Count; i++)
  784. if (this.faceNumbers[i] == 0)
  785. this.faceNumbers[i] = faceNum++;
  786. this.numPrimFaces = faceNum;
  787. }
  788. }
  789. internal void MakeFaceUVs()
  790. {
  791. this.faceUVs = new List<UVCoord>();
  792. foreach (Coord c in this.coords)
  793. this.faceUVs.Add(new UVCoord(1.0f - (0.5f + c.X), 1.0f - (0.5f - c.Y)));
  794. }
  795. internal Profile Clone()
  796. {
  797. return this.Clone(true);
  798. }
  799. internal Profile Clone(bool needFaces)
  800. {
  801. Profile clone = new Profile();
  802. clone.coords.AddRange(this.coords);
  803. clone.faceUVs.AddRange(this.faceUVs);
  804. if (needFaces)
  805. clone.faces.AddRange(this.faces);
  806. if ((clone.calcVertexNormals = this.calcVertexNormals) == true)
  807. {
  808. clone.vertexNormals.AddRange(this.vertexNormals);
  809. clone.faceNormal = this.faceNormal;
  810. clone.cutNormal1 = this.cutNormal1;
  811. clone.cutNormal2 = this.cutNormal2;
  812. clone.us.AddRange(this.us);
  813. clone.faceNumbers.AddRange(this.faceNumbers);
  814. }
  815. clone.numOuterVerts = this.numOuterVerts;
  816. clone.numHollowVerts = this.numHollowVerts;
  817. return clone;
  818. }
  819. internal void AddPos(Coord v)
  820. {
  821. this.AddPos(v.X, v.Y, v.Z);
  822. }
  823. internal void AddPos(float x, float y, float z)
  824. {
  825. int i;
  826. int numVerts = this.coords.Count;
  827. Coord vert;
  828. for (i = 0; i < numVerts; i++)
  829. {
  830. vert = this.coords[i];
  831. vert.X += x;
  832. vert.Y += y;
  833. vert.Z += z;
  834. this.coords[i] = vert;
  835. }
  836. }
  837. internal void AddRot(Quat q)
  838. {
  839. int i;
  840. int numVerts = this.coords.Count;
  841. for (i = 0; i < numVerts; i++)
  842. this.coords[i] *= q;
  843. if (this.calcVertexNormals)
  844. {
  845. int numNormals = this.vertexNormals.Count;
  846. for (i = 0; i < numNormals; i++)
  847. this.vertexNormals[i] *= q;
  848. this.faceNormal *= q;
  849. this.cutNormal1 *= q;
  850. this.cutNormal2 *= q;
  851. }
  852. }
  853. internal void Scale(float x, float y)
  854. {
  855. int i;
  856. int numVerts = this.coords.Count;
  857. Coord vert;
  858. for (i = 0; i < numVerts; i++)
  859. {
  860. vert = this.coords[i];
  861. vert.X *= x;
  862. vert.Y *= y;
  863. this.coords[i] = vert;
  864. }
  865. }
  866. /// <summary>
  867. /// Changes order of the vertex indices and negates the center vertex normal. Does not alter vertex normals of radial vertices
  868. /// </summary>
  869. internal void FlipNormals()
  870. {
  871. int i;
  872. int numFaces = this.faces.Count;
  873. Face tmpFace;
  874. int tmp;
  875. for (i = 0; i < numFaces; i++)
  876. {
  877. tmpFace = this.faces[i];
  878. tmp = tmpFace.v3;
  879. tmpFace.v3 = tmpFace.v1;
  880. tmpFace.v1 = tmp;
  881. this.faces[i] = tmpFace;
  882. }
  883. if (this.calcVertexNormals)
  884. {
  885. int normalCount = this.vertexNormals.Count;
  886. if (normalCount > 0)
  887. {
  888. Coord n = this.vertexNormals[normalCount - 1];
  889. n.Z = -n.Z;
  890. this.vertexNormals[normalCount - 1] = n;
  891. }
  892. }
  893. this.faceNormal.X = -this.faceNormal.X;
  894. this.faceNormal.Y = -this.faceNormal.Y;
  895. this.faceNormal.Z = -this.faceNormal.Z;
  896. int numfaceUVs = this.faceUVs.Count;
  897. for (i = 0; i < numfaceUVs; i++)
  898. {
  899. UVCoord uv = this.faceUVs[i];
  900. uv.V = 1.0f - uv.V;
  901. this.faceUVs[i] = uv;
  902. }
  903. }
  904. internal void AddValue2FaceVertexIndices(int num)
  905. {
  906. int numFaces = this.faces.Count;
  907. Face tmpFace;
  908. for (int i = 0; i < numFaces; i++)
  909. {
  910. tmpFace = this.faces[i];
  911. tmpFace.v1 += num;
  912. tmpFace.v2 += num;
  913. tmpFace.v3 += num;
  914. this.faces[i] = tmpFace;
  915. }
  916. }
  917. internal void AddValue2FaceNormalIndices(int num)
  918. {
  919. if (this.calcVertexNormals)
  920. {
  921. int numFaces = this.faces.Count;
  922. Face tmpFace;
  923. for (int i = 0; i < numFaces; i++)
  924. {
  925. tmpFace = this.faces[i];
  926. tmpFace.n1 += num;
  927. tmpFace.n2 += num;
  928. tmpFace.n3 += num;
  929. this.faces[i] = tmpFace;
  930. }
  931. }
  932. }
  933. internal void DumpRaw(String path, String name, String title)
  934. {
  935. if (path == null)
  936. return;
  937. String fileName = name + "_" + title + ".raw";
  938. String completePath = Path.Combine(path, fileName);
  939. StreamWriter sw = new StreamWriter(completePath);
  940. for (int i = 0; i < this.faces.Count; i++)
  941. {
  942. string s = this.coords[this.faces[i].v1].ToString();
  943. s += " " + this.coords[this.faces[i].v2].ToString();
  944. s += " " + this.coords[this.faces[i].v3].ToString();
  945. sw.WriteLine(s);
  946. }
  947. sw.Close();
  948. }
  949. }
  950. public class PrimMesh
  951. {
  952. private const float twoPi = 2.0f * (float)Math.PI;
  953. public List<Coord> coords;
  954. public List<Coord> normals;
  955. public List<Face> faces;
  956. public List<ViewerFace> viewerFaces;
  957. private int sides = 4;
  958. private int hollowSides = 4;
  959. private float profileStart = 0.0f;
  960. private float profileEnd = 1.0f;
  961. private float hollow = 0.0f;
  962. public int twistBegin = 0;
  963. public int twistEnd = 0;
  964. public float topShearX = 0.0f;
  965. public float topShearY = 0.0f;
  966. public float pathCutBegin = 0.0f;
  967. public float pathCutEnd = 1.0f;
  968. public float dimpleBegin = 0.0f;
  969. public float dimpleEnd = 1.0f;
  970. public float skew = 0.0f;
  971. public float holeSizeX = 1.0f; // called pathScaleX in pbs
  972. public float holeSizeY = 0.25f;
  973. public float taperX = 0.0f;
  974. public float taperY = 0.0f;
  975. public float radius = 0.0f;
  976. public float revolutions = 1.0f;
  977. public int stepsPerRevolution = 24;
  978. private bool hasProfileCut = false;
  979. private bool hasHollow = false;
  980. public bool calcVertexNormals = false;
  981. private bool normalsProcessed = false;
  982. public bool viewerMode = false;
  983. public int numPrimFaces = 0;
  984. /// <summary>
  985. /// Human readable string representation of the parameters used to create a mesh.
  986. /// </summary>
  987. /// <returns></returns>
  988. public string ParamsToDisplayString()
  989. {
  990. string s = "";
  991. s += "sides..................: " + this.sides.ToString();
  992. s += "\nhollowSides..........: " + this.hollowSides.ToString();
  993. s += "\nprofileStart.........: " + this.profileStart.ToString();
  994. s += "\nprofileEnd...........: " + this.profileEnd.ToString();
  995. s += "\nhollow...............: " + this.hollow.ToString();
  996. s += "\ntwistBegin...........: " + this.twistBegin.ToString();
  997. s += "\ntwistEnd.............: " + this.twistEnd.ToString();
  998. s += "\ntopShearX............: " + this.topShearX.ToString();
  999. s += "\ntopShearY............: " + this.topShearY.ToString();
  1000. s += "\npathCutBegin.........: " + this.pathCutBegin.ToString();
  1001. s += "\npathCutEnd...........: " + this.pathCutEnd.ToString();
  1002. s += "\ndimpleBegin..........: " + this.dimpleBegin.ToString();
  1003. s += "\ndimpleEnd............: " + this.dimpleEnd.ToString();
  1004. s += "\nskew.................: " + this.skew.ToString();
  1005. s += "\nholeSizeX............: " + this.holeSizeX.ToString();
  1006. s += "\nholeSizeY............: " + this.holeSizeY.ToString();
  1007. s += "\ntaperX...............: " + this.taperX.ToString();
  1008. s += "\ntaperY...............: " + this.taperY.ToString();
  1009. s += "\nradius...............: " + this.radius.ToString();
  1010. s += "\nrevolutions..........: " + this.revolutions.ToString();
  1011. s += "\nstepsPerRevolution...: " + this.stepsPerRevolution.ToString();
  1012. return s;
  1013. }
  1014. /// <summary>
  1015. /// Constructs a PrimMesh object and creates the profile for extrusion.
  1016. /// </summary>
  1017. /// <param name="sides"></param>
  1018. /// <param name="profileStart"></param>
  1019. /// <param name="profileEnd"></param>
  1020. /// <param name="hollow"></param>
  1021. /// <param name="hollowSides"></param>
  1022. public PrimMesh(int sides, float profileStart, float profileEnd, float hollow, int hollowSides)
  1023. {
  1024. this.coords = new List<Coord>();
  1025. this.faces = new List<Face>();
  1026. this.sides = sides;
  1027. this.profileStart = profileStart;
  1028. this.profileEnd = profileEnd;
  1029. this.hollow = hollow;
  1030. this.hollowSides = hollowSides;
  1031. if (sides < 3)
  1032. this.sides = 3;
  1033. if (hollowSides < 3)
  1034. this.hollowSides = 3;
  1035. if (profileStart < 0.0f)
  1036. this.profileStart = 0.0f;
  1037. if (profileEnd > 1.0f)
  1038. this.profileEnd = 1.0f;
  1039. if (profileEnd < 0.02f)
  1040. this.profileEnd = 0.02f;
  1041. if (profileStart >= profileEnd)
  1042. this.profileStart = profileEnd - 0.02f;
  1043. if (hollow > 1.0f)
  1044. this.hollow = 1.0f;
  1045. if (hollow < 0.0f)
  1046. this.hollow = 0.0f;
  1047. this.hasProfileCut = (this.profileStart > 0.0f || this.profileEnd < 1.0f);
  1048. this.hasHollow = (this.hollow > 0.001f);
  1049. }
  1050. /// <summary>
  1051. /// Extrudes a profile along a straight line path. Used for prim types box, cylinder, and prism.
  1052. /// </summary>
  1053. public void ExtrudeLinear()
  1054. {
  1055. this.coords = new List<Coord>();
  1056. this.faces = new List<Face>();
  1057. if (this.viewerMode)
  1058. {
  1059. this.viewerFaces = new List<ViewerFace>();
  1060. this.calcVertexNormals = true;
  1061. }
  1062. if (this.calcVertexNormals)
  1063. this.normals = new List<Coord>();
  1064. int step = 0;
  1065. int steps = 1;
  1066. float length = this.pathCutEnd - this.pathCutBegin;
  1067. normalsProcessed = false;
  1068. if (this.viewerMode && this.sides == 3)
  1069. {
  1070. // prisms don't taper well so add some vertical resolution
  1071. // other prims may benefit from this but just do prisms for now
  1072. if (Math.Abs(this.taperX) > 0.01 || Math.Abs(this.taperY) > 0.01)
  1073. steps = (int)(steps * 4.5 * length);
  1074. }
  1075. float twistBegin = this.twistBegin / 360.0f * twoPi;
  1076. float twistEnd = this.twistEnd / 360.0f * twoPi;
  1077. float twistTotal = twistEnd - twistBegin;
  1078. float twistTotalAbs = Math.Abs(twistTotal);
  1079. if (twistTotalAbs > 0.01f)
  1080. steps += (int)(twistTotalAbs * 3.66); // dahlia's magic number
  1081. float start = -0.5f;
  1082. float stepSize = length / (float)steps;
  1083. float percentOfPathMultiplier = stepSize;
  1084. float xProfileScale = 1.0f;
  1085. float yProfileScale = 1.0f;
  1086. float xOffset = 0.0f;
  1087. float yOffset = 0.0f;
  1088. float zOffset = start;
  1089. float xOffsetStepIncrement = this.topShearX / steps;
  1090. float yOffsetStepIncrement = this.topShearY / steps;
  1091. float percentOfPath = this.pathCutBegin;
  1092. zOffset += percentOfPath;
  1093. float hollow = this.hollow;
  1094. // sanity checks
  1095. float initialProfileRot = 0.0f;
  1096. if (this.sides == 3)
  1097. {
  1098. if (this.hollowSides == 4)
  1099. {
  1100. if (hollow > 0.7f)
  1101. hollow = 0.7f;
  1102. hollow *= 0.707f;
  1103. }
  1104. else hollow *= 0.5f;
  1105. }
  1106. else if (this.sides == 4)
  1107. {
  1108. initialProfileRot = 1.25f * (float)Math.PI;
  1109. if (this.hollowSides != 4)
  1110. hollow *= 0.707f;
  1111. }
  1112. else if (this.sides == 24 && this.hollowSides == 4)
  1113. hollow *= 1.414f;
  1114. Profile profile = new Profile(this.sides, this.profileStart, this.profileEnd, hollow, this.hollowSides, true, calcVertexNormals);
  1115. this.numPrimFaces = profile.numPrimFaces;
  1116. int cut1Vert = -1;
  1117. int cut2Vert = -1;
  1118. if (hasProfileCut)
  1119. {
  1120. cut1Vert = hasHollow ? profile.coords.Count - 1 : 0;
  1121. cut2Vert = hasHollow ? profile.numOuterVerts - 1 : profile.numOuterVerts;
  1122. }
  1123. if (initialProfileRot != 0.0f)
  1124. {
  1125. profile.AddRot(new Quat(new Coord(0.0f, 0.0f, 1.0f), initialProfileRot));
  1126. if (viewerMode)
  1127. profile.MakeFaceUVs();
  1128. }
  1129. Coord lastCutNormal1 = new Coord();
  1130. Coord lastCutNormal2 = new Coord();
  1131. float lastV = 1.0f;
  1132. bool done = false;
  1133. while (!done)
  1134. {
  1135. Profile newLayer = profile.Clone();
  1136. if (this.taperX == 0.0f)
  1137. xProfileScale = 1.0f;
  1138. else if (this.taperX > 0.0f)
  1139. xProfileScale = 1.0f - percentOfPath * this.taperX;
  1140. else xProfileScale = 1.0f + (1.0f - percentOfPath) * this.taperX;
  1141. if (this.taperY == 0.0f)
  1142. yProfileScale = 1.0f;
  1143. else if (this.taperY > 0.0f)
  1144. yProfileScale = 1.0f - percentOfPath * this.taperY;
  1145. else yProfileScale = 1.0f + (1.0f - percentOfPath) * this.taperY;
  1146. if (xProfileScale != 1.0f || yProfileScale != 1.0f)
  1147. newLayer.Scale(xProfileScale, yProfileScale);
  1148. float twist = twistBegin + twistTotal * percentOfPath;
  1149. if (twist != 0.0f)
  1150. newLayer.AddRot(new Quat(new Coord(0.0f, 0.0f, 1.0f), twist));
  1151. newLayer.AddPos(xOffset, yOffset, zOffset);
  1152. if (step == 0)
  1153. {
  1154. newLayer.FlipNormals();
  1155. // add the top faces to the viewerFaces list here
  1156. if (this.viewerMode)
  1157. {
  1158. Coord faceNormal = newLayer.faceNormal;
  1159. ViewerFace newViewerFace = new ViewerFace(0);
  1160. int numFaces = newLayer.faces.Count;
  1161. List<Face> faces = newLayer.faces;
  1162. for (int i = 0; i < numFaces; i++)
  1163. {
  1164. Face face = faces[i];
  1165. newViewerFace.v1 = newLayer.coords[face.v1];
  1166. newViewerFace.v2 = newLayer.coords[face.v2];
  1167. newViewerFace.v3 = newLayer.coords[face.v3];
  1168. newViewerFace.n1 = faceNormal;
  1169. newViewerFace.n2 = faceNormal;
  1170. newViewerFace.n3 = faceNormal;
  1171. newViewerFace.uv1 = newLayer.faceUVs[face.v1];
  1172. newViewerFace.uv2 = newLayer.faceUVs[face.v2];
  1173. newViewerFace.uv3 = newLayer.faceUVs[face.v3];
  1174. this.viewerFaces.Add(newViewerFace);
  1175. }
  1176. }
  1177. }
  1178. // append this layer
  1179. int coordsLen = this.coords.Count;
  1180. newLayer.AddValue2FaceVertexIndices(coordsLen);
  1181. this.coords.AddRange(newLayer.coords);
  1182. if (this.calcVertexNormals)
  1183. {
  1184. newLayer.AddValue2FaceNormalIndices(this.normals.Count);
  1185. this.normals.AddRange(newLayer.vertexNormals);
  1186. }
  1187. if (percentOfPath < this.pathCutBegin + 0.01f || percentOfPath > this.pathCutEnd - 0.01f)
  1188. this.faces.AddRange(newLayer.faces);
  1189. // fill faces between layers
  1190. int numVerts = newLayer.coords.Count;
  1191. Face newFace = new Face();
  1192. if (step > 0)
  1193. {
  1194. int startVert = coordsLen + 1;
  1195. int endVert = this.coords.Count;
  1196. if (sides < 5 || this.hasProfileCut || hollow > 0.0f)
  1197. startVert--;
  1198. for (int i = startVert; i < endVert; i++)
  1199. {
  1200. int iNext = i + 1;
  1201. if (i == endVert - 1)
  1202. iNext = startVert;
  1203. int whichVert = i - startVert;
  1204. newFace.v1 = i;
  1205. newFace.v2 = i - numVerts;
  1206. newFace.v3 = iNext - numVerts;
  1207. this.faces.Add(newFace);
  1208. newFace.v2 = iNext - numVerts;
  1209. newFace.v3 = iNext;
  1210. this.faces.Add(newFace);
  1211. if (this.viewerMode)
  1212. {
  1213. // add the side faces to the list of viewerFaces here
  1214. int primFaceNum = 1;
  1215. if (whichVert >= sides)
  1216. primFaceNum = 2;
  1217. ViewerFace newViewerFace1 = new ViewerFace(primFaceNum);
  1218. ViewerFace newViewerFace2 = new ViewerFace(primFaceNum);
  1219. float u1 = newLayer.us[whichVert];
  1220. float u2 = 1.0f;
  1221. if (whichVert < newLayer.us.Count - 1)
  1222. u2 = newLayer.us[whichVert + 1];
  1223. if (whichVert == cut1Vert || whichVert == cut2Vert)
  1224. {
  1225. u1 = 0.0f;
  1226. u2 = 1.0f;
  1227. }
  1228. else if (sides < 5)
  1229. { // boxes and prisms have one texture face per side of the prim, so the U values have to be scaled
  1230. // to reflect the entire texture width
  1231. u1 *= sides;
  1232. u2 *= sides;
  1233. u2 -= (int)u1;
  1234. u1 -= (int)u1;
  1235. if (u2 < 0.1f)
  1236. u2 = 1.0f;
  1237. //newViewerFace2.primFaceNumber = newViewerFace1.primFaceNumber = whichVert + 1;
  1238. }
  1239. newViewerFace1.uv1.U = u1;
  1240. newViewerFace1.uv2.U = u1;
  1241. newViewerFace1.uv3.U = u2;
  1242. newViewerFace1.uv1.V = 1.0f - percentOfPath;
  1243. newViewerFace1.uv2.V = lastV;
  1244. newViewerFace1.uv3.V = lastV;
  1245. newViewerFace2.uv1.U = u1;
  1246. newViewerFace2.uv2.U = u2;
  1247. newViewerFace2.uv3.U = u2;
  1248. newViewerFace2.uv1.V = 1.0f - percentOfPath;
  1249. newViewerFace2.uv2.V = lastV;
  1250. newViewerFace2.uv3.V = 1.0f - percentOfPath;
  1251. newViewerFace1.v1 = this.coords[i];
  1252. newViewerFace1.v2 = this.coords[i - numVerts];
  1253. newViewerFace1.v3 = this.coords[iNext - numVerts];
  1254. newViewerFace2.v1 = this.coords[i];
  1255. newViewerFace2.v2 = this.coords[iNext - numVerts];
  1256. newViewerFace2.v3 = this.coords[iNext];
  1257. // profile cut faces
  1258. if (whichVert == cut1Vert)
  1259. {
  1260. newViewerFace1.n1 = newLayer.cutNormal1;
  1261. newViewerFace1.n2 = newViewerFace1.n3 = lastCutNormal1;
  1262. newViewerFace2.n1 = newViewerFace2.n3 = newLayer.cutNormal1;
  1263. newViewerFace2.n2 = lastCutNormal1;
  1264. }
  1265. else if (whichVert == cut2Vert)
  1266. {
  1267. newViewerFace1.n1 = newLayer.cutNormal2;
  1268. newViewerFace1.n2 = newViewerFace1.n3 = lastCutNormal2;
  1269. newViewerFace2.n1 = newViewerFace2.n3 = newLayer.cutNormal2;
  1270. newViewerFace2.n2 = lastCutNormal2;
  1271. }
  1272. else // outer and hollow faces
  1273. {
  1274. if ((sides < 5 && whichVert < newLayer.numOuterVerts) || (hollowSides < 5 && whichVert >= newLayer.numOuterVerts))
  1275. {
  1276. newViewerFace1.CalcSurfaceNormal();
  1277. newViewerFace2.CalcSurfaceNormal();
  1278. }
  1279. else
  1280. {
  1281. newViewerFace1.n1 = this.normals[i];
  1282. newViewerFace1.n2 = this.normals[i - numVerts];
  1283. newViewerFace1.n3 = this.normals[iNext - numVerts];
  1284. newViewerFace2.n1 = this.normals[i];
  1285. newViewerFace2.n2 = this.normals[iNext - numVerts];
  1286. newViewerFace2.n3 = this.normals[iNext];
  1287. }
  1288. }
  1289. newViewerFace2.primFaceNumber = newViewerFace1.primFaceNumber = newLayer.faceNumbers[whichVert];
  1290. this.viewerFaces.Add(newViewerFace1);
  1291. this.viewerFaces.Add(newViewerFace2);
  1292. }
  1293. }
  1294. }
  1295. lastCutNormal1 = newLayer.cutNormal1;
  1296. lastCutNormal2 = newLayer.cutNormal2;
  1297. lastV = 1.0f - percentOfPath;
  1298. // calc the step for the next iteration of the loop
  1299. if (step < steps)
  1300. {
  1301. step += 1;
  1302. percentOfPath += percentOfPathMultiplier;
  1303. xOffset += xOffsetStepIncrement;
  1304. yOffset += yOffsetStepIncrement;
  1305. zOffset += stepSize;
  1306. if (percentOfPath > this.pathCutEnd)
  1307. done = true;
  1308. }
  1309. else done = true;
  1310. if (done && viewerMode)
  1311. {
  1312. // add the top faces to the viewerFaces list here
  1313. Coord faceNormal = newLayer.faceNormal;
  1314. ViewerFace newViewerFace = new ViewerFace();
  1315. newViewerFace.primFaceNumber = newLayer.bottomFaceNumber;
  1316. int numFaces = newLayer.faces.Count;
  1317. List<Face> faces = newLayer.faces;
  1318. for (int i = 0; i < numFaces; i++)
  1319. {
  1320. Face face = faces[i];
  1321. newViewerFace.v1 = newLayer.coords[face.v1 - coordsLen];
  1322. newViewerFace.v2 = newLayer.coords[face.v2 - coordsLen];
  1323. newViewerFace.v3 = newLayer.coords[face.v3 - coordsLen];
  1324. newViewerFace.n1 = faceNormal;
  1325. newViewerFace.n2 = faceNormal;
  1326. newViewerFace.n3 = faceNormal;
  1327. newViewerFace.uv1 = newLayer.faceUVs[face.v1 - coordsLen];
  1328. newViewerFace.uv2 = newLayer.faceUVs[face.v2 - coordsLen];
  1329. newViewerFace.uv3 = newLayer.faceUVs[face.v3 - coordsLen];
  1330. this.viewerFaces.Add(newViewerFace);
  1331. }
  1332. }
  1333. }
  1334. }
  1335. /// <summary>
  1336. /// Extrude a profile into a circular path prim mesh. Used for prim types torus, tube, and ring.
  1337. /// </summary>
  1338. public void ExtrudeCircular()
  1339. {
  1340. this.coords = new List<Coord>();
  1341. this.faces = new List<Face>();
  1342. if (this.viewerMode)
  1343. {
  1344. this.viewerFaces = new List<ViewerFace>();
  1345. this.calcVertexNormals = true;
  1346. }
  1347. if (this.calcVertexNormals)
  1348. this.normals = new List<Coord>();
  1349. int step = 0;
  1350. int steps = 24;
  1351. normalsProcessed = false;
  1352. float twistBegin = this.twistBegin / 360.0f * twoPi;
  1353. float twistEnd = this.twistEnd / 360.0f * twoPi;
  1354. float twistTotal = twistEnd - twistBegin;
  1355. // if the profile has a lot of twist, add more layers otherwise the layers may overlap
  1356. // and the resulting mesh may be quite inaccurate. This method is arbitrary and doesn't
  1357. // accurately match the viewer
  1358. float twistTotalAbs = Math.Abs(twistTotal);
  1359. if (twistTotalAbs > 0.01f)
  1360. {
  1361. if (twistTotalAbs > Math.PI * 1.5f)
  1362. steps *= 2;
  1363. if (twistTotalAbs > Math.PI * 3.0f)
  1364. steps *= 2;
  1365. }
  1366. float yPathScale = this.holeSizeY * 0.5f;
  1367. float pathLength = this.pathCutEnd - this.pathCutBegin;
  1368. float totalSkew = this.skew * 2.0f * pathLength;
  1369. float skewStart = this.pathCutBegin * 2.0f * this.skew - this.skew;
  1370. float xOffsetTopShearXFactor = this.topShearX * (0.25f + 0.5f * (0.5f - this.holeSizeY));
  1371. float yShearCompensation = 1.0f + Math.Abs(this.topShearY) * 0.25f;
  1372. // It's not quite clear what pushY (Y top shear) does, but subtracting it from the start and end
  1373. // angles appears to approximate it's effects on path cut. Likewise, adding it to the angle used
  1374. // to calculate the sine for generating the path radius appears to approximate it's effects there
  1375. // too, but there are some subtle differences in the radius which are noticeable as the prim size
  1376. // increases and it may affect megaprims quite a bit. The effect of the Y top shear parameter on
  1377. // the meshes generated with this technique appear nearly identical in shape to the same prims when
  1378. // displayed by the viewer.
  1379. float startAngle = (twoPi * this.pathCutBegin * this.revolutions) - this.topShearY * 0.9f;
  1380. float endAngle = (twoPi * this.pathCutEnd * this.revolutions) - this.topShearY * 0.9f;
  1381. float stepSize = twoPi / this.stepsPerRevolution;
  1382. step = (int)(startAngle / stepSize);
  1383. int firstStep = step;
  1384. float angle = startAngle;
  1385. float hollow = this.hollow;
  1386. // sanity checks
  1387. float initialProfileRot = 0.0f;
  1388. if (this.sides == 3)
  1389. {
  1390. initialProfileRot = (float)Math.PI;
  1391. if (this.hollowSides == 4)
  1392. {
  1393. if (hollow > 0.7f)
  1394. hollow = 0.7f;
  1395. hollow *= 0.707f;
  1396. }
  1397. else hollow *= 0.5f;
  1398. }
  1399. else if (this.sides == 4)
  1400. {
  1401. initialProfileRot = 0.25f * (float)Math.PI;
  1402. if (this.hollowSides != 4)
  1403. hollow *= 0.707f;
  1404. }
  1405. else if (this.sides > 4)
  1406. {
  1407. initialProfileRot = (float)Math.PI;
  1408. if (this.hollowSides == 4)
  1409. {
  1410. if (hollow > 0.7f)
  1411. hollow = 0.7f;
  1412. hollow /= 0.7f;
  1413. }
  1414. }
  1415. bool needEndFaces = false;
  1416. if (this.pathCutBegin != 0.0f || this.pathCutEnd != 1.0f)
  1417. needEndFaces = true;
  1418. else if (this.taperX != 0.0f || this.taperY != 0.0f)
  1419. needEndFaces = true;
  1420. else if (this.skew != 0.0f)
  1421. needEndFaces = true;
  1422. else if (twistTotal != 0.0f)
  1423. needEndFaces = true;
  1424. else if (this.radius != 0.0f)
  1425. needEndFaces = true;
  1426. Profile profile = new Profile(this.sides, this.profileStart, this.profileEnd, hollow, this.hollowSides, needEndFaces, calcVertexNormals);
  1427. this.numPrimFaces = profile.numPrimFaces;
  1428. int cut1Vert = -1;
  1429. int cut2Vert = -1;
  1430. if (hasProfileCut)
  1431. {
  1432. cut1Vert = hasHollow ? profile.coords.Count - 1 : 0;
  1433. cut2Vert = hasHollow ? profile.numOuterVerts - 1 : profile.numOuterVerts;
  1434. }
  1435. if (initialProfileRot != 0.0f)
  1436. {
  1437. profile.AddRot(new Quat(new Coord(0.0f, 0.0f, 1.0f), initialProfileRot));
  1438. if (viewerMode)
  1439. profile.MakeFaceUVs();
  1440. }
  1441. Coord lastCutNormal1 = new Coord();
  1442. Coord lastCutNormal2 = new Coord();
  1443. float lastV = 1.0f;
  1444. bool done = false;
  1445. while (!done) // loop through the length of the path and add the layers
  1446. {
  1447. bool isEndLayer = false;
  1448. if (angle <= startAngle + .01f || angle >= endAngle - .01f)
  1449. isEndLayer = true;
  1450. //Profile newLayer = profile.Clone(isEndLayer && needEndFaces);
  1451. Profile newLayer = profile.Clone();
  1452. float xProfileScale = (1.0f - Math.Abs(this.skew)) * this.holeSizeX;
  1453. float yProfileScale = this.holeSizeY;
  1454. float percentOfPath = angle / (twoPi * this.revolutions);
  1455. float percentOfAngles = (angle - startAngle) / (endAngle - startAngle);
  1456. if (this.taperX > 0.01f)
  1457. xProfileScale *= 1.0f - percentOfPath * this.taperX;
  1458. else if (this.taperX < -0.01f)
  1459. xProfileScale *= 1.0f + (1.0f - percentOfPath) * this.taperX;
  1460. if (this.taperY > 0.01f)
  1461. yProfileScale *= 1.0f - percentOfPath * this.taperY;
  1462. else if (this.taperY < -0.01f)
  1463. yProfileScale *= 1.0f + (1.0f - percentOfPath) * this.taperY;
  1464. if (xProfileScale != 1.0f || yProfileScale != 1.0f)
  1465. newLayer.Scale(xProfileScale, yProfileScale);
  1466. float radiusScale = 1.0f;
  1467. if (this.radius > 0.001f)
  1468. radiusScale = 1.0f - this.radius * percentOfPath;
  1469. else if (this.radius < 0.001f)
  1470. radiusScale = 1.0f + this.radius * (1.0f - percentOfPath);
  1471. float twist = twistBegin + twistTotal * percentOfPath;
  1472. float xOffset = 0.5f * (skewStart + totalSkew * percentOfAngles);
  1473. xOffset += (float)Math.Sin(angle) * xOffsetTopShearXFactor;
  1474. float yOffset = yShearCompensation * (float)Math.Cos(angle) * (0.5f - yPathScale) * radiusScale;
  1475. float zOffset = (float)Math.Sin(angle + this.topShearY) * (0.5f - yPathScale) * radiusScale;
  1476. // next apply twist rotation to the profile layer
  1477. if (twistTotal != 0.0f || twistBegin != 0.0f)
  1478. newLayer.AddRot(new Quat(new Coord(0.0f, 0.0f, 1.0f), twist));
  1479. // now orient the rotation of the profile layer relative to it's position on the path
  1480. // adding taperY to the angle used to generate the quat appears to approximate the viewer
  1481. newLayer.AddRot(new Quat(new Coord(1.0f, 0.0f, 0.0f), angle + this.topShearY));
  1482. newLayer.AddPos(xOffset, yOffset, zOffset);
  1483. if (isEndLayer && angle <= startAngle + .01f)
  1484. {
  1485. newLayer.FlipNormals();
  1486. // add the top faces to the viewerFaces list here
  1487. if (this.viewerMode && needEndFaces)
  1488. {
  1489. Coord faceNormal = newLayer.faceNormal;
  1490. ViewerFace newViewerFace = new ViewerFace();
  1491. newViewerFace.primFaceNumber = 0;
  1492. foreach (Face face in newLayer.faces)
  1493. {
  1494. newViewerFace.v1 = newLayer.coords[face.v1];
  1495. newViewerFace.v2 = newLayer.coords[face.v2];
  1496. newViewerFace.v3 = newLayer.coords[face.v3];
  1497. newViewerFace.n1 = faceNormal;
  1498. newViewerFace.n2 = faceNormal;
  1499. newViewerFace.n3 = faceNormal;
  1500. newViewerFace.uv1 = newLayer.faceUVs[face.v1];
  1501. newViewerFace.uv2 = newLayer.faceUVs[face.v2];
  1502. newViewerFace.uv3 = newLayer.faceUVs[face.v3];
  1503. this.viewerFaces.Add(newViewerFace);
  1504. }
  1505. }
  1506. }
  1507. // append the layer and fill in the sides
  1508. int coordsLen = this.coords.Count;
  1509. newLayer.AddValue2FaceVertexIndices(coordsLen);
  1510. this.coords.AddRange(newLayer.coords);
  1511. if (this.calcVertexNormals)
  1512. {
  1513. newLayer.AddValue2FaceNormalIndices(this.normals.Count);
  1514. this.normals.AddRange(newLayer.vertexNormals);
  1515. }
  1516. if (isEndLayer)
  1517. this.faces.AddRange(newLayer.faces);
  1518. // fill faces between layers
  1519. int numVerts = newLayer.coords.Count;
  1520. Face newFace = new Face();
  1521. if (step > firstStep)
  1522. {
  1523. int startVert = coordsLen + 1;
  1524. int endVert = this.coords.Count;
  1525. if (sides < 5 || this.hasProfileCut || hollow > 0.0f)
  1526. startVert--;
  1527. for (int i = startVert; i < endVert; i++)
  1528. {
  1529. int iNext = i + 1;
  1530. if (i == endVert - 1)
  1531. iNext = startVert;
  1532. int whichVert = i - startVert;
  1533. newFace.v1 = i;
  1534. newFace.v2 = i - numVerts;
  1535. newFace.v3 = iNext - numVerts;
  1536. this.faces.Add(newFace);
  1537. newFace.v2 = iNext - numVerts;
  1538. newFace.v3 = iNext;
  1539. this.faces.Add(newFace);
  1540. if (this.viewerMode)
  1541. {
  1542. // add the side faces to the list of viewerFaces here
  1543. ViewerFace newViewerFace1 = new ViewerFace();
  1544. ViewerFace newViewerFace2 = new ViewerFace();
  1545. float u1 = newLayer.us[whichVert];
  1546. float u2 = 1.0f;
  1547. if (whichVert < newLayer.us.Count - 1)
  1548. u2 = newLayer.us[whichVert + 1];
  1549. if (whichVert == cut1Vert || whichVert == cut2Vert)
  1550. {
  1551. u1 = 0.0f;
  1552. u2 = 1.0f;
  1553. }
  1554. else if (sides < 5)
  1555. { // boxes and prisms have one texture face per side of the prim, so the U values have to be scaled
  1556. // to reflect the entire texture width
  1557. u1 *= sides;
  1558. u2 *= sides;
  1559. u2 -= (int)u1;
  1560. u1 -= (int)u1;
  1561. if (u2 < 0.1f)
  1562. u2 = 1.0f;
  1563. //newViewerFace2.primFaceNumber = newViewerFace1.primFaceNumber = whichVert + 1;
  1564. }
  1565. newViewerFace1.uv1.U = u1;
  1566. newViewerFace1.uv2.U = u1;
  1567. newViewerFace1.uv3.U = u2;
  1568. newViewerFace1.uv1.V = 1.0f - percentOfPath;
  1569. newViewerFace1.uv2.V = lastV;
  1570. newViewerFace1.uv3.V = lastV;
  1571. newViewerFace2.uv1.U = u1;
  1572. newViewerFace2.uv2.U = u2;
  1573. newViewerFace2.uv3.U = u2;
  1574. newViewerFace2.uv1.V = 1.0f - percentOfPath;
  1575. newViewerFace2.uv2.V = lastV;
  1576. newViewerFace2.uv3.V = 1.0f - percentOfPath;
  1577. newViewerFace1.v1 = this.coords[i];
  1578. newViewerFace1.v2 = this.coords[i - numVerts];
  1579. newViewerFace1.v3 = this.coords[iNext - numVerts];
  1580. newViewerFace2.v1 = this.coords[i];
  1581. newViewerFace2.v2 = this.coords[iNext - numVerts];
  1582. newViewerFace2.v3 = this.coords[iNext];
  1583. // profile cut faces
  1584. if (whichVert == cut1Vert)
  1585. {
  1586. newViewerFace1.n1 = newLayer.cutNormal1;
  1587. newViewerFace1.n2 = newViewerFace1.n3 = lastCutNormal1;
  1588. newViewerFace2.n1 = newViewerFace2.n3 = newLayer.cutNormal1;
  1589. newViewerFace2.n2 = lastCutNormal1;
  1590. }
  1591. else if (whichVert == cut2Vert)
  1592. {
  1593. newViewerFace1.n1 = newLayer.cutNormal2;
  1594. newViewerFace1.n2 = newViewerFace1.n3 = lastCutNormal2;
  1595. newViewerFace2.n1 = newViewerFace2.n3 = newLayer.cutNormal2;
  1596. newViewerFace2.n2 = lastCutNormal2;
  1597. }
  1598. else // periphery faces
  1599. {
  1600. if (sides < 5 && whichVert < newLayer.numOuterVerts)
  1601. {
  1602. newViewerFace1.n1 = this.normals[i];
  1603. newViewerFace1.n2 = this.normals[i - numVerts];
  1604. newViewerFace1.n3 = this.normals[i - numVerts];
  1605. newViewerFace2.n1 = this.normals[i];
  1606. newViewerFace2.n2 = this.normals[i - numVerts];
  1607. newViewerFace2.n3 = this.normals[i];
  1608. }
  1609. else if (hollowSides < 5 && whichVert >= newLayer.numOuterVerts)
  1610. {
  1611. newViewerFace1.n1 = this.normals[iNext];
  1612. newViewerFace1.n2 = this.normals[iNext - numVerts];
  1613. newViewerFace1.n3 = this.normals[iNext - numVerts];
  1614. newViewerFace2.n1 = this.normals[iNext];
  1615. newViewerFace2.n2 = this.normals[iNext - numVerts];
  1616. newViewerFace2.n3 = this.normals[iNext];
  1617. }
  1618. else
  1619. {
  1620. newViewerFace1.n1 = this.normals[i];
  1621. newViewerFace1.n2 = this.normals[i - numVerts];
  1622. newViewerFace1.n3 = this.normals[iNext - numVerts];
  1623. newViewerFace2.n1 = this.normals[i];
  1624. newViewerFace2.n2 = this.normals[iNext - numVerts];
  1625. newViewerFace2.n3 = this.normals[iNext];
  1626. }
  1627. }
  1628. newViewerFace1.primFaceNumber = newViewerFace2.primFaceNumber = newLayer.faceNumbers[whichVert];
  1629. this.viewerFaces.Add(newViewerFace1);
  1630. this.viewerFaces.Add(newViewerFace2);
  1631. }
  1632. }
  1633. }
  1634. lastCutNormal1 = newLayer.cutNormal1;
  1635. lastCutNormal2 = newLayer.cutNormal2;
  1636. lastV = 1.0f - percentOfPath;
  1637. // calculate terms for next iteration
  1638. // calculate the angle for the next iteration of the loop
  1639. if (angle >= endAngle)
  1640. done = true;
  1641. else
  1642. {
  1643. step += 1;
  1644. angle = stepSize * step;
  1645. if (angle > endAngle)
  1646. angle = endAngle;
  1647. }
  1648. if (done && viewerMode && needEndFaces)
  1649. {
  1650. // add the bottom faces to the viewerFaces list here
  1651. Coord faceNormal = newLayer.faceNormal;
  1652. ViewerFace newViewerFace = new ViewerFace();
  1653. newViewerFace.primFaceNumber = newLayer.bottomFaceNumber;
  1654. foreach (Face face in newLayer.faces)
  1655. {
  1656. newViewerFace.v1 = newLayer.coords[face.v1 - coordsLen];
  1657. newViewerFace.v2 = newLayer.coords[face.v2 - coordsLen];
  1658. newViewerFace.v3 = newLayer.coords[face.v3 - coordsLen];
  1659. newViewerFace.n1 = faceNormal;
  1660. newViewerFace.n2 = faceNormal;
  1661. newViewerFace.n3 = faceNormal;
  1662. newViewerFace.uv1 = newLayer.faceUVs[face.v1 - coordsLen];
  1663. newViewerFace.uv2 = newLayer.faceUVs[face.v2 - coordsLen];
  1664. newViewerFace.uv3 = newLayer.faceUVs[face.v3 - coordsLen];
  1665. this.viewerFaces.Add(newViewerFace);
  1666. }
  1667. }
  1668. }
  1669. }
  1670. private Coord SurfaceNormal(Coord c1, Coord c2, Coord c3)
  1671. {
  1672. Coord edge1 = new Coord(c2.X - c1.X, c2.Y - c1.Y, c2.Z - c1.Z);
  1673. Coord edge2 = new Coord(c3.X - c1.X, c3.Y - c1.Y, c3.Z - c1.Z);
  1674. Coord normal = Coord.Cross(edge1, edge2);
  1675. normal.Normalize();
  1676. return normal;
  1677. }
  1678. private Coord SurfaceNormal(Face face)
  1679. {
  1680. return SurfaceNormal(this.coords[face.v1], this.coords[face.v2], this.coords[face.v3]);
  1681. }
  1682. /// <summary>
  1683. /// Calculate the surface normal for a face in the list of faces
  1684. /// </summary>
  1685. /// <param name="faceIndex"></param>
  1686. /// <returns></returns>
  1687. public Coord SurfaceNormal(int faceIndex)
  1688. {
  1689. int numFaces = this.faces.Count;
  1690. if (faceIndex < 0 || faceIndex >= numFaces)
  1691. throw new Exception("faceIndex out of range");
  1692. return SurfaceNormal(this.faces[faceIndex]);
  1693. }
  1694. /// <summary>
  1695. /// Calculate surface normals for all of the faces in the list of faces in this mesh
  1696. /// </summary>
  1697. public void CalcNormals()
  1698. {
  1699. if (normalsProcessed)
  1700. return;
  1701. normalsProcessed = true;
  1702. int numFaces = faces.Count;
  1703. if (!this.calcVertexNormals)
  1704. this.normals = new List<Coord>();
  1705. for (int i = 0; i < numFaces; i++)
  1706. {
  1707. Face face = faces[i];
  1708. this.normals.Add(SurfaceNormal(i).Normalize());
  1709. int normIndex = normals.Count - 1;
  1710. face.n1 = normIndex;
  1711. face.n2 = normIndex;
  1712. face.n3 = normIndex;
  1713. this.faces[i] = face;
  1714. }
  1715. }
  1716. /// <summary>
  1717. /// Adds a value to each XYZ vertex coordinate in the mesh
  1718. /// </summary>
  1719. /// <param name="x"></param>
  1720. /// <param name="y"></param>
  1721. /// <param name="z"></param>
  1722. public void AddPos(float x, float y, float z)
  1723. {
  1724. int i;
  1725. int numVerts = this.coords.Count;
  1726. Coord vert;
  1727. for (i = 0; i < numVerts; i++)
  1728. {
  1729. vert = this.coords[i];
  1730. vert.X += x;
  1731. vert.Y += y;
  1732. vert.Z += z;
  1733. this.coords[i] = vert;
  1734. }
  1735. }
  1736. /// <summary>
  1737. /// Rotates the mesh
  1738. /// </summary>
  1739. /// <param name="q"></param>
  1740. public void AddRot(Quat q)
  1741. {
  1742. int i;
  1743. int numVerts = this.coords.Count;
  1744. for (i = 0; i < numVerts; i++)
  1745. this.coords[i] *= q;
  1746. if (this.normals != null)
  1747. {
  1748. int numNormals = this.normals.Count;
  1749. for (i = 0; i < numNormals; i++)
  1750. this.normals[i] *= q;
  1751. }
  1752. if (this.viewerFaces != null)
  1753. {
  1754. int numViewerFaces = this.viewerFaces.Count;
  1755. for (i = 0; i < numViewerFaces; i++)
  1756. {
  1757. ViewerFace v = this.viewerFaces[i];
  1758. v.v1 *= q;
  1759. v.v2 *= q;
  1760. v.v3 *= q;
  1761. v.n1 *= q;
  1762. v.n2 *= q;
  1763. v.n3 *= q;
  1764. this.viewerFaces[i] = v;
  1765. }
  1766. }
  1767. }
  1768. /// <summary>
  1769. /// Scales the mesh
  1770. /// </summary>
  1771. /// <param name="x"></param>
  1772. /// <param name="y"></param>
  1773. /// <param name="z"></param>
  1774. public void Scale(float x, float y, float z)
  1775. {
  1776. int i;
  1777. int numVerts = this.coords.Count;
  1778. //Coord vert;
  1779. Coord m = new Coord(x, y, z);
  1780. for (i = 0; i < numVerts; i++)
  1781. this.coords[i] *= m;
  1782. if (this.viewerFaces != null)
  1783. {
  1784. int numViewerFaces = this.viewerFaces.Count;
  1785. for (i = 0; i < numViewerFaces; i++)
  1786. {
  1787. ViewerFace v = this.viewerFaces[i];
  1788. v.v1 *= m;
  1789. v.v2 *= m;
  1790. v.v3 *= m;
  1791. this.viewerFaces[i] = v;
  1792. }
  1793. }
  1794. }
  1795. /// <summary>
  1796. /// Dumps the mesh to a Blender compatible "Raw" format file
  1797. /// </summary>
  1798. /// <param name="path"></param>
  1799. /// <param name="name"></param>
  1800. /// <param name="title"></param>
  1801. public void DumpRaw(String path, String name, String title)
  1802. {
  1803. if (path == null)
  1804. return;
  1805. String fileName = name + "_" + title + ".raw";
  1806. String completePath = Path.Combine(path, fileName);
  1807. StreamWriter sw = new StreamWriter(completePath);
  1808. for (int i = 0; i < this.faces.Count; i++)
  1809. {
  1810. string s = this.coords[this.faces[i].v1].ToString();
  1811. s += " " + this.coords[this.faces[i].v2].ToString();
  1812. s += " " + this.coords[this.faces[i].v3].ToString();
  1813. sw.WriteLine(s);
  1814. }
  1815. sw.Close();
  1816. }
  1817. }
  1818. }