1
0

PrimMesher.cs 81 KB

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