PrimMesher.cs 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202
  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 bool calcVertexNormals = false;
  530. internal int bottomFaceNumber = 0;
  531. internal int numPrimFaces = 0;
  532. internal Profile()
  533. {
  534. this.coords = new List<Coord>();
  535. this.faces = new List<Face>();
  536. this.vertexNormals = new List<Coord>();
  537. this.us = new List<float>();
  538. this.faceUVs = new List<UVCoord>();
  539. this.faceNumbers = new List<int>();
  540. }
  541. internal Profile(int sides, float profileStart, float profileEnd, float hollow, int hollowSides, bool createFaces, bool calcVertexNormals)
  542. {
  543. this.calcVertexNormals = calcVertexNormals;
  544. this.coords = new List<Coord>();
  545. this.faces = new List<Face>();
  546. this.vertexNormals = new List<Coord>();
  547. this.us = new List<float>();
  548. this.faceUVs = new List<UVCoord>();
  549. this.faceNumbers = new List<int>();
  550. Coord center = new Coord(0.0f, 0.0f, 0.0f);
  551. //bool hasCenter = false;
  552. List<Coord> hollowCoords = new List<Coord>();
  553. List<Coord> hollowNormals = new List<Coord>();
  554. List<float> hollowUs = new List<float>();
  555. if (calcVertexNormals)
  556. {
  557. this.outerCoordIndices = new List<int>();
  558. this.hollowCoordIndices = new List<int>();
  559. this.cut1CoordIndices = new List<int>();
  560. this.cut2CoordIndices = new List<int>();
  561. }
  562. bool hasHollow = (hollow > 0.0f);
  563. bool hasProfileCut = (profileStart > 0.0f || profileEnd < 1.0f);
  564. AngleList angles = new AngleList();
  565. AngleList hollowAngles = new AngleList();
  566. float xScale = 0.5f;
  567. float yScale = 0.5f;
  568. if (sides == 4) // corners of a square are sqrt(2) from center
  569. {
  570. xScale = 0.707f;
  571. yScale = 0.707f;
  572. }
  573. float startAngle = profileStart * twoPi;
  574. float stopAngle = profileEnd * twoPi;
  575. try { angles.makeAngles(sides, startAngle, stopAngle); }
  576. catch (Exception ex)
  577. {
  578. errorMessage = "makeAngles failed: Exception: " + ex.ToString()
  579. + "\nsides: " + sides.ToString() + " startAngle: " + startAngle.ToString() + " stopAngle: " + stopAngle.ToString();
  580. return;
  581. }
  582. this.numOuterVerts = angles.angles.Count;
  583. // flag to create as few triangles as possible for 3 or 4 side profile
  584. bool simpleFace = (sides < 5 && !hasHollow && !hasProfileCut);
  585. if (hasHollow)
  586. {
  587. if (sides == hollowSides)
  588. hollowAngles = angles;
  589. else
  590. {
  591. try { hollowAngles.makeAngles(hollowSides, startAngle, stopAngle); }
  592. catch (Exception ex)
  593. {
  594. errorMessage = "makeAngles failed: Exception: " + ex.ToString()
  595. + "\nsides: " + sides.ToString() + " startAngle: " + startAngle.ToString() + " stopAngle: " + stopAngle.ToString();
  596. return;
  597. }
  598. }
  599. this.numHollowVerts = hollowAngles.angles.Count;
  600. }
  601. else if (!simpleFace)
  602. {
  603. this.coords.Add(center);
  604. //hasCenter = true;
  605. if (this.calcVertexNormals)
  606. this.vertexNormals.Add(new Coord(0.0f, 0.0f, 1.0f));
  607. this.us.Add(0.0f);
  608. }
  609. float z = 0.0f;
  610. Angle angle;
  611. Coord newVert = new Coord();
  612. if (hasHollow && hollowSides != sides)
  613. {
  614. int numHollowAngles = hollowAngles.angles.Count;
  615. for (int i = 0; i < numHollowAngles; i++)
  616. {
  617. angle = hollowAngles.angles[i];
  618. newVert.X = hollow * xScale * angle.X;
  619. newVert.Y = hollow * yScale * angle.Y;
  620. newVert.Z = z;
  621. hollowCoords.Add(newVert);
  622. if (this.calcVertexNormals)
  623. {
  624. if (hollowSides < 5)
  625. hollowNormals.Add(hollowAngles.normals[i].Invert());
  626. else
  627. hollowNormals.Add(new Coord(-angle.X, -angle.Y, 0.0f));
  628. hollowUs.Add(angle.angle * hollow);
  629. }
  630. }
  631. }
  632. int index = 0;
  633. int numAngles = angles.angles.Count;
  634. for (int i = 0; i < numAngles; i++)
  635. {
  636. angle = angles.angles[i];
  637. newVert.X = angle.X * xScale;
  638. newVert.Y = angle.Y * yScale;
  639. newVert.Z = z;
  640. this.coords.Add(newVert);
  641. if (this.calcVertexNormals)
  642. {
  643. this.outerCoordIndices.Add(this.coords.Count - 1);
  644. if (sides < 5)
  645. {
  646. this.vertexNormals.Add(angles.normals[i]);
  647. float u = angle.angle;
  648. this.us.Add(u);
  649. }
  650. else
  651. {
  652. this.vertexNormals.Add(new Coord(angle.X, angle.Y, 0.0f));
  653. this.us.Add(angle.angle);
  654. }
  655. }
  656. if (hasHollow)
  657. {
  658. if (hollowSides == sides)
  659. {
  660. newVert.X *= hollow;
  661. newVert.Y *= hollow;
  662. newVert.Z = z;
  663. hollowCoords.Add(newVert);
  664. if (this.calcVertexNormals)
  665. {
  666. if (sides < 5)
  667. {
  668. hollowNormals.Add(angles.normals[i].Invert());
  669. }
  670. else
  671. hollowNormals.Add(new Coord(-angle.X, -angle.Y, 0.0f));
  672. hollowUs.Add(angle.angle * hollow);
  673. }
  674. }
  675. }
  676. else if (!simpleFace && createFaces && angle.angle > 0.0001f)
  677. {
  678. Face newFace = new Face();
  679. newFace.v1 = 0;
  680. newFace.v2 = index;
  681. newFace.v3 = index + 1;
  682. this.faces.Add(newFace);
  683. }
  684. index += 1;
  685. }
  686. if (hasHollow)
  687. {
  688. hollowCoords.Reverse();
  689. if (this.calcVertexNormals)
  690. {
  691. hollowNormals.Reverse();
  692. hollowUs.Reverse();
  693. }
  694. if (createFaces)
  695. {
  696. int numOuterVerts = this.coords.Count;
  697. int numHollowVerts = hollowCoords.Count;
  698. int numTotalVerts = numOuterVerts + numHollowVerts;
  699. if (numOuterVerts == numHollowVerts)
  700. {
  701. Face newFace = new Face();
  702. for (int coordIndex = 0; coordIndex < numOuterVerts - 1; coordIndex++)
  703. {
  704. newFace.v1 = coordIndex;
  705. newFace.v2 = coordIndex + 1;
  706. newFace.v3 = numTotalVerts - coordIndex - 1;
  707. this.faces.Add(newFace);
  708. newFace.v1 = coordIndex + 1;
  709. newFace.v2 = numTotalVerts - coordIndex - 2;
  710. newFace.v3 = numTotalVerts - coordIndex - 1;
  711. this.faces.Add(newFace);
  712. }
  713. }
  714. else
  715. {
  716. if (numOuterVerts < numHollowVerts)
  717. {
  718. Face newFace = new Face();
  719. int j = 0; // j is the index for outer vertices
  720. int maxJ = numOuterVerts - 1;
  721. for (int i = 0; i < numHollowVerts; i++) // i is the index for inner vertices
  722. {
  723. if (j < maxJ)
  724. if (angles.angles[j + 1].angle - hollowAngles.angles[i].angle < hollowAngles.angles[i].angle - angles.angles[j].angle + 0.000001f)
  725. {
  726. newFace.v1 = numTotalVerts - i - 1;
  727. newFace.v2 = j;
  728. newFace.v3 = j + 1;
  729. this.faces.Add(newFace);
  730. j += 1;
  731. }
  732. newFace.v1 = j;
  733. newFace.v2 = numTotalVerts - i - 2;
  734. newFace.v3 = numTotalVerts - i - 1;
  735. this.faces.Add(newFace);
  736. }
  737. }
  738. else // numHollowVerts < numOuterVerts
  739. {
  740. Face newFace = new Face();
  741. int j = 0; // j is the index for inner vertices
  742. int maxJ = numHollowVerts - 1;
  743. for (int i = 0; i < numOuterVerts; i++)
  744. {
  745. if (j < maxJ)
  746. if (hollowAngles.angles[j + 1].angle - angles.angles[i].angle < angles.angles[i].angle - hollowAngles.angles[j].angle + 0.000001f)
  747. {
  748. newFace.v1 = i;
  749. newFace.v2 = numTotalVerts - j - 2;
  750. newFace.v3 = numTotalVerts - j - 1;
  751. this.faces.Add(newFace);
  752. j += 1;
  753. }
  754. newFace.v1 = numTotalVerts - j - 1;
  755. newFace.v2 = i;
  756. newFace.v3 = i + 1;
  757. this.faces.Add(newFace);
  758. }
  759. }
  760. }
  761. }
  762. if (calcVertexNormals)
  763. {
  764. foreach (Coord hc in hollowCoords)
  765. {
  766. this.coords.Add(hc);
  767. hollowCoordIndices.Add(this.coords.Count - 1);
  768. }
  769. }
  770. else
  771. this.coords.AddRange(hollowCoords);
  772. if (this.calcVertexNormals)
  773. {
  774. this.vertexNormals.AddRange(hollowNormals);
  775. this.us.AddRange(hollowUs);
  776. }
  777. }
  778. if (simpleFace && createFaces)
  779. {
  780. if (sides == 3)
  781. this.faces.Add(new Face(0, 1, 2));
  782. else if (sides == 4)
  783. {
  784. this.faces.Add(new Face(0, 1, 2));
  785. this.faces.Add(new Face(0, 2, 3));
  786. }
  787. }
  788. if (calcVertexNormals && hasProfileCut)
  789. {
  790. if (hasHollow)
  791. {
  792. int lastOuterVertIndex = this.numOuterVerts - 1;
  793. this.cut1CoordIndices.Add(0);
  794. this.cut1CoordIndices.Add(this.coords.Count - 1);
  795. this.cut2CoordIndices.Add(lastOuterVertIndex + 1);
  796. this.cut2CoordIndices.Add(lastOuterVertIndex);
  797. this.cutNormal1.X = this.coords[0].Y - this.coords[this.coords.Count - 1].Y;
  798. this.cutNormal1.Y = -(this.coords[0].X - this.coords[this.coords.Count - 1].X);
  799. this.cutNormal2.X = this.coords[lastOuterVertIndex + 1].Y - this.coords[lastOuterVertIndex].Y;
  800. this.cutNormal2.Y = -(this.coords[lastOuterVertIndex + 1].X - this.coords[lastOuterVertIndex].X);
  801. }
  802. else
  803. {
  804. this.cutNormal1.X = this.vertexNormals[1].Y;
  805. this.cutNormal1.Y = -this.vertexNormals[1].X;
  806. this.cutNormal2.X = -this.vertexNormals[this.vertexNormals.Count - 2].Y;
  807. this.cutNormal2.Y = this.vertexNormals[this.vertexNormals.Count - 2].X;
  808. }
  809. this.cutNormal1.Normalize();
  810. this.cutNormal2.Normalize();
  811. }
  812. this.MakeFaceUVs();
  813. hollowCoords = null;
  814. hollowNormals = null;
  815. hollowUs = null;
  816. if (calcVertexNormals)
  817. { // calculate prim face numbers
  818. // face number order is top, outer, hollow, bottom, start cut, end cut
  819. // I know it's ugly but so is the whole concept of prim face numbers
  820. int faceNum = 1; // start with outer faces
  821. int startVert = hasProfileCut && !hasHollow ? 1 : 0;
  822. if (startVert > 0)
  823. this.faceNumbers.Add(-1);
  824. for (int i = 0; i < numOuterVerts - 1; i++)
  825. this.faceNumbers.Add(sides < 5 ? faceNum++ : faceNum);
  826. //if (!hasHollow && !hasProfileCut)
  827. // this.bottomFaceNumber = faceNum++;
  828. this.faceNumbers.Add(hasProfileCut ? -1 : faceNum++);
  829. if (sides > 4 && (hasHollow || hasProfileCut))
  830. faceNum++;
  831. if (hasHollow)
  832. {
  833. for (int i = 0; i < numHollowVerts; i++)
  834. this.faceNumbers.Add(faceNum);
  835. faceNum++;
  836. }
  837. //if (hasProfileCut || hasHollow)
  838. // this.bottomFaceNumber = faceNum++;
  839. this.bottomFaceNumber = faceNum++;
  840. if (hasHollow && hasProfileCut)
  841. this.faceNumbers.Add(faceNum++);
  842. for (int i = 0; i < this.faceNumbers.Count; i++)
  843. if (this.faceNumbers[i] == -1)
  844. this.faceNumbers[i] = faceNum++;
  845. this.numPrimFaces = faceNum;
  846. }
  847. }
  848. internal void MakeFaceUVs()
  849. {
  850. this.faceUVs = new List<UVCoord>();
  851. foreach (Coord c in this.coords)
  852. this.faceUVs.Add(new UVCoord(1.0f - (0.5f + c.X), 1.0f - (0.5f - c.Y)));
  853. }
  854. internal Profile Copy()
  855. {
  856. return this.Copy(true);
  857. }
  858. internal Profile Copy(bool needFaces)
  859. {
  860. Profile copy = new Profile();
  861. copy.coords.AddRange(this.coords);
  862. copy.faceUVs.AddRange(this.faceUVs);
  863. if (needFaces)
  864. copy.faces.AddRange(this.faces);
  865. if ((copy.calcVertexNormals = this.calcVertexNormals) == true)
  866. {
  867. copy.vertexNormals.AddRange(this.vertexNormals);
  868. copy.faceNormal = this.faceNormal;
  869. copy.cutNormal1 = this.cutNormal1;
  870. copy.cutNormal2 = this.cutNormal2;
  871. copy.us.AddRange(this.us);
  872. copy.faceNumbers.AddRange(this.faceNumbers);
  873. copy.cut1CoordIndices = new List<int>(this.cut1CoordIndices);
  874. copy.cut2CoordIndices = new List<int>(this.cut2CoordIndices);
  875. copy.hollowCoordIndices = new List<int>(this.hollowCoordIndices);
  876. copy.outerCoordIndices = new List<int>(this.outerCoordIndices);
  877. }
  878. copy.numOuterVerts = this.numOuterVerts;
  879. copy.numHollowVerts = this.numHollowVerts;
  880. return copy;
  881. }
  882. internal void AddPos(Coord v)
  883. {
  884. this.AddPos(v.X, v.Y, v.Z);
  885. }
  886. internal void AddPos(float x, float y, float z)
  887. {
  888. int i;
  889. int numVerts = this.coords.Count;
  890. Coord vert;
  891. for (i = 0; i < numVerts; i++)
  892. {
  893. vert = this.coords[i];
  894. vert.X += x;
  895. vert.Y += y;
  896. vert.Z += z;
  897. this.coords[i] = vert;
  898. }
  899. }
  900. internal void AddRot(Quat q)
  901. {
  902. int i;
  903. int numVerts = this.coords.Count;
  904. for (i = 0; i < numVerts; i++)
  905. this.coords[i] *= q;
  906. if (this.calcVertexNormals)
  907. {
  908. int numNormals = this.vertexNormals.Count;
  909. for (i = 0; i < numNormals; i++)
  910. this.vertexNormals[i] *= q;
  911. this.faceNormal *= q;
  912. this.cutNormal1 *= q;
  913. this.cutNormal2 *= q;
  914. }
  915. }
  916. internal void Scale(float x, float y)
  917. {
  918. int i;
  919. int numVerts = this.coords.Count;
  920. Coord vert;
  921. for (i = 0; i < numVerts; i++)
  922. {
  923. vert = this.coords[i];
  924. vert.X *= x;
  925. vert.Y *= y;
  926. this.coords[i] = vert;
  927. }
  928. }
  929. /// <summary>
  930. /// Changes order of the vertex indices and negates the center vertex normal. Does not alter vertex normals of radial vertices
  931. /// </summary>
  932. internal void FlipNormals()
  933. {
  934. int i;
  935. int numFaces = this.faces.Count;
  936. Face tmpFace;
  937. int tmp;
  938. for (i = 0; i < numFaces; i++)
  939. {
  940. tmpFace = this.faces[i];
  941. tmp = tmpFace.v3;
  942. tmpFace.v3 = tmpFace.v1;
  943. tmpFace.v1 = tmp;
  944. this.faces[i] = tmpFace;
  945. }
  946. if (this.calcVertexNormals)
  947. {
  948. int normalCount = this.vertexNormals.Count;
  949. if (normalCount > 0)
  950. {
  951. Coord n = this.vertexNormals[normalCount - 1];
  952. n.Z = -n.Z;
  953. this.vertexNormals[normalCount - 1] = n;
  954. }
  955. }
  956. this.faceNormal.X = -this.faceNormal.X;
  957. this.faceNormal.Y = -this.faceNormal.Y;
  958. this.faceNormal.Z = -this.faceNormal.Z;
  959. int numfaceUVs = this.faceUVs.Count;
  960. for (i = 0; i < numfaceUVs; i++)
  961. {
  962. UVCoord uv = this.faceUVs[i];
  963. uv.V = 1.0f - uv.V;
  964. this.faceUVs[i] = uv;
  965. }
  966. }
  967. internal void AddValue2FaceVertexIndices(int num)
  968. {
  969. int numFaces = this.faces.Count;
  970. Face tmpFace;
  971. for (int i = 0; i < numFaces; i++)
  972. {
  973. tmpFace = this.faces[i];
  974. tmpFace.v1 += num;
  975. tmpFace.v2 += num;
  976. tmpFace.v3 += num;
  977. this.faces[i] = tmpFace;
  978. }
  979. }
  980. internal void AddValue2FaceNormalIndices(int num)
  981. {
  982. if (this.calcVertexNormals)
  983. {
  984. int numFaces = this.faces.Count;
  985. Face tmpFace;
  986. for (int i = 0; i < numFaces; i++)
  987. {
  988. tmpFace = this.faces[i];
  989. tmpFace.n1 += num;
  990. tmpFace.n2 += num;
  991. tmpFace.n3 += num;
  992. this.faces[i] = tmpFace;
  993. }
  994. }
  995. }
  996. internal void DumpRaw(String path, String name, String title)
  997. {
  998. if (path == null)
  999. return;
  1000. String fileName = name + "_" + title + ".raw";
  1001. String completePath = System.IO.Path.Combine(path, fileName);
  1002. StreamWriter sw = new StreamWriter(completePath);
  1003. for (int i = 0; i < this.faces.Count; i++)
  1004. {
  1005. string s = this.coords[this.faces[i].v1].ToString();
  1006. s += " " + this.coords[this.faces[i].v2].ToString();
  1007. s += " " + this.coords[this.faces[i].v3].ToString();
  1008. sw.WriteLine(s);
  1009. }
  1010. sw.Close();
  1011. }
  1012. }
  1013. public struct PathNode
  1014. {
  1015. public Coord position;
  1016. public Quat rotation;
  1017. public float xScale;
  1018. public float yScale;
  1019. public float percentOfPath;
  1020. }
  1021. public enum PathType { Linear = 0, Circular = 1, Flexible = 2 }
  1022. public class Path
  1023. {
  1024. public List<PathNode> pathNodes = new List<PathNode>();
  1025. public float twistBegin = 0.0f;
  1026. public float twistEnd = 0.0f;
  1027. public float topShearX = 0.0f;
  1028. public float topShearY = 0.0f;
  1029. public float pathCutBegin = 0.0f;
  1030. public float pathCutEnd = 1.0f;
  1031. public float dimpleBegin = 0.0f;
  1032. public float dimpleEnd = 1.0f;
  1033. public float skew = 0.0f;
  1034. public float holeSizeX = 1.0f; // called pathScaleX in pbs
  1035. public float holeSizeY = 0.25f;
  1036. public float taperX = 0.0f;
  1037. public float taperY = 0.0f;
  1038. public float radius = 0.0f;
  1039. public float revolutions = 1.0f;
  1040. public int stepsPerRevolution = 24;
  1041. private const float twoPi = 2.0f * (float)Math.PI;
  1042. public void Create(PathType pathType, int steps)
  1043. {
  1044. if (pathType == PathType.Linear || pathType == PathType.Flexible)
  1045. {
  1046. int step = 0;
  1047. float length = this.pathCutEnd - this.pathCutBegin;
  1048. float twistTotal = twistEnd - twistBegin;
  1049. float twistTotalAbs = Math.Abs(twistTotal);
  1050. if (twistTotalAbs > 0.01f)
  1051. steps += (int)(twistTotalAbs * 3.66); // dahlia's magic number
  1052. float start = -0.5f;
  1053. float stepSize = length / (float)steps;
  1054. float percentOfPathMultiplier = stepSize;
  1055. float xOffset = 0.0f;
  1056. float yOffset = 0.0f;
  1057. float zOffset = start;
  1058. float xOffsetStepIncrement = this.topShearX / steps;
  1059. float yOffsetStepIncrement = this.topShearY / steps;
  1060. float percentOfPath = this.pathCutBegin;
  1061. zOffset += percentOfPath;
  1062. // sanity checks
  1063. bool done = false;
  1064. while (!done)
  1065. {
  1066. PathNode newNode = new PathNode();
  1067. newNode.xScale = 1.0f;
  1068. if (this.taperX == 0.0f)
  1069. newNode.xScale = 1.0f;
  1070. else if (this.taperX > 0.0f)
  1071. newNode.xScale = 1.0f - percentOfPath * this.taperX;
  1072. else newNode.xScale = 1.0f + (1.0f - percentOfPath) * this.taperX;
  1073. newNode.yScale = 1.0f;
  1074. if (this.taperY == 0.0f)
  1075. newNode.yScale = 1.0f;
  1076. else if (this.taperY > 0.0f)
  1077. newNode.yScale = 1.0f - percentOfPath * this.taperY;
  1078. else newNode.yScale = 1.0f + (1.0f - percentOfPath) * this.taperY;
  1079. float twist = twistBegin + twistTotal * percentOfPath;
  1080. newNode.rotation = new Quat(new Coord(0.0f, 0.0f, 1.0f), twist);
  1081. newNode.position = new Coord(xOffset, yOffset, zOffset);
  1082. newNode.percentOfPath = percentOfPath;
  1083. pathNodes.Add(newNode);
  1084. if (step < steps)
  1085. {
  1086. step += 1;
  1087. percentOfPath += percentOfPathMultiplier;
  1088. xOffset += xOffsetStepIncrement;
  1089. yOffset += yOffsetStepIncrement;
  1090. zOffset += stepSize;
  1091. if (percentOfPath > this.pathCutEnd)
  1092. done = true;
  1093. }
  1094. else done = true;
  1095. }
  1096. } // end of linear path code
  1097. else // pathType == Circular
  1098. {
  1099. float twistTotal = twistEnd - twistBegin;
  1100. // if the profile has a lot of twist, add more layers otherwise the layers may overlap
  1101. // and the resulting mesh may be quite inaccurate. This method is arbitrary and doesn't
  1102. // accurately match the viewer
  1103. float twistTotalAbs = Math.Abs(twistTotal);
  1104. if (twistTotalAbs > 0.01f)
  1105. {
  1106. if (twistTotalAbs > Math.PI * 1.5f)
  1107. steps *= 2;
  1108. if (twistTotalAbs > Math.PI * 3.0f)
  1109. steps *= 2;
  1110. }
  1111. float yPathScale = this.holeSizeY * 0.5f;
  1112. float pathLength = this.pathCutEnd - this.pathCutBegin;
  1113. float totalSkew = this.skew * 2.0f * pathLength;
  1114. float skewStart = this.pathCutBegin * 2.0f * this.skew - this.skew;
  1115. float xOffsetTopShearXFactor = this.topShearX * (0.25f + 0.5f * (0.5f - this.holeSizeY));
  1116. float yShearCompensation = 1.0f + Math.Abs(this.topShearY) * 0.25f;
  1117. // It's not quite clear what pushY (Y top shear) does, but subtracting it from the start and end
  1118. // angles appears to approximate it's effects on path cut. Likewise, adding it to the angle used
  1119. // to calculate the sine for generating the path radius appears to approximate it's effects there
  1120. // too, but there are some subtle differences in the radius which are noticeable as the prim size
  1121. // increases and it may affect megaprims quite a bit. The effect of the Y top shear parameter on
  1122. // the meshes generated with this technique appear nearly identical in shape to the same prims when
  1123. // displayed by the viewer.
  1124. float startAngle = (twoPi * this.pathCutBegin * this.revolutions) - this.topShearY * 0.9f;
  1125. float endAngle = (twoPi * this.pathCutEnd * this.revolutions) - this.topShearY * 0.9f;
  1126. float stepSize = twoPi / this.stepsPerRevolution;
  1127. int step = (int)(startAngle / stepSize);
  1128. // int firstStep = step;
  1129. float angle = startAngle;
  1130. bool done = false;
  1131. while (!done) // loop through the length of the path and add the layers
  1132. {
  1133. PathNode newNode = new PathNode();
  1134. float xProfileScale = (1.0f - Math.Abs(this.skew)) * this.holeSizeX;
  1135. float yProfileScale = this.holeSizeY;
  1136. float percentOfPath = angle / (twoPi * this.revolutions);
  1137. float percentOfAngles = (angle - startAngle) / (endAngle - startAngle);
  1138. if (this.taperX > 0.01f)
  1139. xProfileScale *= 1.0f - percentOfPath * this.taperX;
  1140. else if (this.taperX < -0.01f)
  1141. xProfileScale *= 1.0f + (1.0f - percentOfPath) * this.taperX;
  1142. if (this.taperY > 0.01f)
  1143. yProfileScale *= 1.0f - percentOfPath * this.taperY;
  1144. else if (this.taperY < -0.01f)
  1145. yProfileScale *= 1.0f + (1.0f - percentOfPath) * this.taperY;
  1146. newNode.xScale = xProfileScale;
  1147. newNode.yScale = yProfileScale;
  1148. float radiusScale = 1.0f;
  1149. if (this.radius > 0.001f)
  1150. radiusScale = 1.0f - this.radius * percentOfPath;
  1151. else if (this.radius < 0.001f)
  1152. radiusScale = 1.0f + this.radius * (1.0f - percentOfPath);
  1153. float twist = twistBegin + twistTotal * percentOfPath;
  1154. float xOffset = 0.5f * (skewStart + totalSkew * percentOfAngles);
  1155. xOffset += (float)Math.Sin(angle) * xOffsetTopShearXFactor;
  1156. float yOffset = yShearCompensation * (float)Math.Cos(angle) * (0.5f - yPathScale) * radiusScale;
  1157. float zOffset = (float)Math.Sin(angle + this.topShearY) * (0.5f - yPathScale) * radiusScale;
  1158. newNode.position = new Coord(xOffset, yOffset, zOffset);
  1159. // now orient the rotation of the profile layer relative to it's position on the path
  1160. // adding taperY to the angle used to generate the quat appears to approximate the viewer
  1161. newNode.rotation = new Quat(new Coord(1.0f, 0.0f, 0.0f), angle + this.topShearY);
  1162. // next apply twist rotation to the profile layer
  1163. if (twistTotal != 0.0f || twistBegin != 0.0f)
  1164. newNode.rotation *= new Quat(new Coord(0.0f, 0.0f, 1.0f), twist);
  1165. newNode.percentOfPath = percentOfPath;
  1166. pathNodes.Add(newNode);
  1167. // calculate terms for next iteration
  1168. // calculate the angle for the next iteration of the loop
  1169. if (angle >= endAngle - 0.01)
  1170. done = true;
  1171. else
  1172. {
  1173. step += 1;
  1174. angle = stepSize * step;
  1175. if (angle > endAngle)
  1176. angle = endAngle;
  1177. }
  1178. }
  1179. }
  1180. }
  1181. }
  1182. public class PrimMesh
  1183. {
  1184. public string errorMessage = "";
  1185. private const float twoPi = 2.0f * (float)Math.PI;
  1186. public List<Coord> coords;
  1187. public List<Coord> normals;
  1188. public List<Face> faces;
  1189. public List<ViewerFace> viewerFaces;
  1190. private int sides = 4;
  1191. private int hollowSides = 4;
  1192. private float profileStart = 0.0f;
  1193. private float profileEnd = 1.0f;
  1194. private float hollow = 0.0f;
  1195. public int twistBegin = 0;
  1196. public int twistEnd = 0;
  1197. public float topShearX = 0.0f;
  1198. public float topShearY = 0.0f;
  1199. public float pathCutBegin = 0.0f;
  1200. public float pathCutEnd = 1.0f;
  1201. public float dimpleBegin = 0.0f;
  1202. public float dimpleEnd = 1.0f;
  1203. public float skew = 0.0f;
  1204. public float holeSizeX = 1.0f; // called pathScaleX in pbs
  1205. public float holeSizeY = 0.25f;
  1206. public float taperX = 0.0f;
  1207. public float taperY = 0.0f;
  1208. public float radius = 0.0f;
  1209. public float revolutions = 1.0f;
  1210. public int stepsPerRevolution = 24;
  1211. private bool hasProfileCut = false;
  1212. private bool hasHollow = false;
  1213. public bool calcVertexNormals = false;
  1214. private bool normalsProcessed = false;
  1215. public bool viewerMode = false;
  1216. public int numPrimFaces = 0;
  1217. /// <summary>
  1218. /// Human readable string representation of the parameters used to create a mesh.
  1219. /// </summary>
  1220. /// <returns></returns>
  1221. public string ParamsToDisplayString()
  1222. {
  1223. string s = "";
  1224. s += "sides..................: " + this.sides.ToString();
  1225. s += "\nhollowSides..........: " + this.hollowSides.ToString();
  1226. s += "\nprofileStart.........: " + this.profileStart.ToString();
  1227. s += "\nprofileEnd...........: " + this.profileEnd.ToString();
  1228. s += "\nhollow...............: " + this.hollow.ToString();
  1229. s += "\ntwistBegin...........: " + this.twistBegin.ToString();
  1230. s += "\ntwistEnd.............: " + this.twistEnd.ToString();
  1231. s += "\ntopShearX............: " + this.topShearX.ToString();
  1232. s += "\ntopShearY............: " + this.topShearY.ToString();
  1233. s += "\npathCutBegin.........: " + this.pathCutBegin.ToString();
  1234. s += "\npathCutEnd...........: " + this.pathCutEnd.ToString();
  1235. s += "\ndimpleBegin..........: " + this.dimpleBegin.ToString();
  1236. s += "\ndimpleEnd............: " + this.dimpleEnd.ToString();
  1237. s += "\nskew.................: " + this.skew.ToString();
  1238. s += "\nholeSizeX............: " + this.holeSizeX.ToString();
  1239. s += "\nholeSizeY............: " + this.holeSizeY.ToString();
  1240. s += "\ntaperX...............: " + this.taperX.ToString();
  1241. s += "\ntaperY...............: " + this.taperY.ToString();
  1242. s += "\nradius...............: " + this.radius.ToString();
  1243. s += "\nrevolutions..........: " + this.revolutions.ToString();
  1244. s += "\nstepsPerRevolution...: " + this.stepsPerRevolution.ToString();
  1245. return s;
  1246. }
  1247. /// <summary>
  1248. /// Constructs a PrimMesh object and creates the profile for extrusion.
  1249. /// </summary>
  1250. /// <param name="sides"></param>
  1251. /// <param name="profileStart"></param>
  1252. /// <param name="profileEnd"></param>
  1253. /// <param name="hollow"></param>
  1254. /// <param name="hollowSides"></param>
  1255. public PrimMesh(int sides, float profileStart, float profileEnd, float hollow, int hollowSides)
  1256. {
  1257. this.coords = new List<Coord>();
  1258. this.faces = new List<Face>();
  1259. this.sides = sides;
  1260. this.profileStart = profileStart;
  1261. this.profileEnd = profileEnd;
  1262. this.hollow = hollow;
  1263. this.hollowSides = hollowSides;
  1264. if (sides < 3)
  1265. this.sides = 3;
  1266. if (hollowSides < 3)
  1267. this.hollowSides = 3;
  1268. if (profileStart < 0.0f)
  1269. this.profileStart = 0.0f;
  1270. if (profileEnd > 1.0f)
  1271. this.profileEnd = 1.0f;
  1272. if (profileEnd < 0.02f)
  1273. this.profileEnd = 0.02f;
  1274. if (profileStart >= profileEnd)
  1275. this.profileStart = profileEnd - 0.02f;
  1276. if (hollow > 0.99f)
  1277. this.hollow = 0.99f;
  1278. if (hollow < 0.0f)
  1279. this.hollow = 0.0f;
  1280. this.hasProfileCut = (this.profileStart > 0.0f || this.profileEnd < 1.0f);
  1281. this.hasHollow = (this.hollow > 0.001f);
  1282. }
  1283. /// <summary>
  1284. /// Extrudes a profile along a path.
  1285. /// </summary>
  1286. public void Extrude(PathType pathType)
  1287. {
  1288. this.coords = new List<Coord>();
  1289. this.faces = new List<Face>();
  1290. if (this.viewerMode)
  1291. {
  1292. this.viewerFaces = new List<ViewerFace>();
  1293. this.calcVertexNormals = true;
  1294. }
  1295. if (this.calcVertexNormals)
  1296. this.normals = new List<Coord>();
  1297. int steps = 1;
  1298. float length = this.pathCutEnd - this.pathCutBegin;
  1299. normalsProcessed = false;
  1300. if (this.viewerMode && this.sides == 3)
  1301. {
  1302. // prisms don't taper well so add some vertical resolution
  1303. // other prims may benefit from this but just do prisms for now
  1304. if (Math.Abs(this.taperX) > 0.01 || Math.Abs(this.taperY) > 0.01)
  1305. steps = (int)(steps * 4.5 * length);
  1306. }
  1307. float twistBegin = this.twistBegin / 360.0f * twoPi;
  1308. float twistEnd = this.twistEnd / 360.0f * twoPi;
  1309. float twistTotal = twistEnd - twistBegin;
  1310. float twistTotalAbs = Math.Abs(twistTotal);
  1311. if (twistTotalAbs > 0.01f)
  1312. steps += (int)(twistTotalAbs * 3.66); // dahlia's magic number
  1313. float hollow = this.hollow;
  1314. // sanity checks
  1315. float initialProfileRot = 0.0f;
  1316. if (pathType == PathType.Circular)
  1317. {
  1318. if (this.sides == 3)
  1319. {
  1320. initialProfileRot = (float)Math.PI;
  1321. if (this.hollowSides == 4)
  1322. {
  1323. if (hollow > 0.7f)
  1324. hollow = 0.7f;
  1325. hollow *= 0.707f;
  1326. }
  1327. else hollow *= 0.5f;
  1328. }
  1329. else if (this.sides == 4)
  1330. {
  1331. initialProfileRot = 0.25f * (float)Math.PI;
  1332. if (this.hollowSides != 4)
  1333. hollow *= 0.707f;
  1334. }
  1335. else if (this.sides > 4)
  1336. {
  1337. initialProfileRot = (float)Math.PI;
  1338. if (this.hollowSides == 4)
  1339. {
  1340. if (hollow > 0.7f)
  1341. hollow = 0.7f;
  1342. hollow /= 0.7f;
  1343. }
  1344. }
  1345. }
  1346. else
  1347. {
  1348. if (this.sides == 3)
  1349. {
  1350. if (this.hollowSides == 4)
  1351. {
  1352. if (hollow > 0.7f)
  1353. hollow = 0.7f;
  1354. hollow *= 0.707f;
  1355. }
  1356. else hollow *= 0.5f;
  1357. }
  1358. else if (this.sides == 4)
  1359. {
  1360. initialProfileRot = 1.25f * (float)Math.PI;
  1361. if (this.hollowSides != 4)
  1362. hollow *= 0.707f;
  1363. }
  1364. else if (this.sides == 24 && this.hollowSides == 4)
  1365. hollow *= 1.414f;
  1366. }
  1367. Profile profile = new Profile(this.sides, this.profileStart, this.profileEnd, hollow, this.hollowSides, true, calcVertexNormals);
  1368. this.errorMessage = profile.errorMessage;
  1369. this.numPrimFaces = profile.numPrimFaces;
  1370. int cut1Vert = -1;
  1371. int cut2Vert = -1;
  1372. if (hasProfileCut)
  1373. {
  1374. cut1Vert = hasHollow ? profile.coords.Count - 1 : 0;
  1375. cut2Vert = hasHollow ? profile.numOuterVerts - 1 : profile.numOuterVerts;
  1376. }
  1377. if (initialProfileRot != 0.0f)
  1378. {
  1379. profile.AddRot(new Quat(new Coord(0.0f, 0.0f, 1.0f), initialProfileRot));
  1380. if (viewerMode)
  1381. profile.MakeFaceUVs();
  1382. }
  1383. Coord lastCutNormal1 = new Coord();
  1384. Coord lastCutNormal2 = new Coord();
  1385. float lastV = 1.0f;
  1386. Path path = new Path();
  1387. path.twistBegin = twistBegin;
  1388. path.twistEnd = twistEnd;
  1389. path.topShearX = topShearX;
  1390. path.topShearY = topShearY;
  1391. path.pathCutBegin = pathCutBegin;
  1392. path.pathCutEnd = pathCutEnd;
  1393. path.dimpleBegin = dimpleBegin;
  1394. path.dimpleEnd = dimpleEnd;
  1395. path.skew = skew;
  1396. path.holeSizeX = holeSizeX;
  1397. path.holeSizeY = holeSizeY;
  1398. path.taperX = taperX;
  1399. path.taperY = taperY;
  1400. path.radius = radius;
  1401. path.revolutions = revolutions;
  1402. path.stepsPerRevolution = stepsPerRevolution;
  1403. path.Create(pathType, steps);
  1404. bool needEndFaces = false;
  1405. if (pathType == PathType.Circular)
  1406. {
  1407. needEndFaces = false;
  1408. if (this.pathCutBegin != 0.0f || this.pathCutEnd != 1.0f)
  1409. needEndFaces = true;
  1410. else if (this.taperX != 0.0f || this.taperY != 0.0f)
  1411. needEndFaces = true;
  1412. else if (this.skew != 0.0f)
  1413. needEndFaces = true;
  1414. else if (twistTotal != 0.0f)
  1415. needEndFaces = true;
  1416. else if (this.radius != 0.0f)
  1417. needEndFaces = true;
  1418. }
  1419. else needEndFaces = true;
  1420. for (int nodeIndex = 0; nodeIndex < path.pathNodes.Count; nodeIndex++)
  1421. {
  1422. PathNode node = path.pathNodes[nodeIndex];
  1423. Profile newLayer = profile.Copy();
  1424. newLayer.Scale(node.xScale, node.yScale);
  1425. newLayer.AddRot(node.rotation);
  1426. newLayer.AddPos(node.position);
  1427. if (needEndFaces && nodeIndex == 0)
  1428. {
  1429. newLayer.FlipNormals();
  1430. // add the top faces to the viewerFaces list here
  1431. if (this.viewerMode)
  1432. {
  1433. Coord faceNormal = newLayer.faceNormal;
  1434. ViewerFace newViewerFace = new ViewerFace(profile.bottomFaceNumber);
  1435. int numFaces = newLayer.faces.Count;
  1436. List<Face> faces = newLayer.faces;
  1437. for (int i = 0; i < numFaces; i++)
  1438. {
  1439. Face face = faces[i];
  1440. newViewerFace.v1 = newLayer.coords[face.v1];
  1441. newViewerFace.v2 = newLayer.coords[face.v2];
  1442. newViewerFace.v3 = newLayer.coords[face.v3];
  1443. newViewerFace.coordIndex1 = face.v1;
  1444. newViewerFace.coordIndex2 = face.v2;
  1445. newViewerFace.coordIndex3 = face.v3;
  1446. newViewerFace.n1 = faceNormal;
  1447. newViewerFace.n2 = faceNormal;
  1448. newViewerFace.n3 = faceNormal;
  1449. newViewerFace.uv1 = newLayer.faceUVs[face.v1];
  1450. newViewerFace.uv2 = newLayer.faceUVs[face.v2];
  1451. newViewerFace.uv3 = newLayer.faceUVs[face.v3];
  1452. this.viewerFaces.Add(newViewerFace);
  1453. }
  1454. }
  1455. } // if (nodeIndex == 0)
  1456. // append this layer
  1457. int coordsLen = this.coords.Count;
  1458. // int lastCoordsLen = coordsLen;
  1459. newLayer.AddValue2FaceVertexIndices(coordsLen);
  1460. this.coords.AddRange(newLayer.coords);
  1461. if (this.calcVertexNormals)
  1462. {
  1463. newLayer.AddValue2FaceNormalIndices(this.normals.Count);
  1464. this.normals.AddRange(newLayer.vertexNormals);
  1465. }
  1466. if (node.percentOfPath < this.pathCutBegin + 0.01f || node.percentOfPath > this.pathCutEnd - 0.01f)
  1467. this.faces.AddRange(newLayer.faces);
  1468. // fill faces between layers
  1469. int numVerts = newLayer.coords.Count;
  1470. Face newFace = new Face();
  1471. if (nodeIndex > 0)
  1472. {
  1473. int startVert = coordsLen + 1;
  1474. int endVert = this.coords.Count;
  1475. if (sides < 5 || this.hasProfileCut || hollow > 0.0f)
  1476. startVert--;
  1477. for (int i = startVert; i < endVert; i++)
  1478. {
  1479. int iNext = i + 1;
  1480. if (i == endVert - 1)
  1481. iNext = startVert;
  1482. int whichVert = i - startVert;
  1483. newFace.v1 = i;
  1484. newFace.v2 = i - numVerts;
  1485. newFace.v3 = iNext - numVerts;
  1486. this.faces.Add(newFace);
  1487. newFace.v2 = iNext - numVerts;
  1488. newFace.v3 = iNext;
  1489. this.faces.Add(newFace);
  1490. if (this.viewerMode)
  1491. {
  1492. // add the side faces to the list of viewerFaces here
  1493. int primFaceNum = profile.faceNumbers[whichVert];
  1494. if (!needEndFaces)
  1495. primFaceNum -= 1;
  1496. ViewerFace newViewerFace1 = new ViewerFace(primFaceNum);
  1497. ViewerFace newViewerFace2 = new ViewerFace(primFaceNum);
  1498. float u1 = newLayer.us[whichVert];
  1499. float u2 = 1.0f;
  1500. if (whichVert < newLayer.us.Count - 1)
  1501. u2 = newLayer.us[whichVert + 1];
  1502. if (whichVert == cut1Vert || whichVert == cut2Vert)
  1503. {
  1504. u1 = 0.0f;
  1505. u2 = 1.0f;
  1506. }
  1507. else if (sides < 5)
  1508. {
  1509. if (whichVert < profile.numOuterVerts)
  1510. { // boxes and prisms have one texture face per side of the prim, so the U values have to be scaled
  1511. // to reflect the entire texture width
  1512. u1 *= sides;
  1513. u2 *= sides;
  1514. u2 -= (int)u1;
  1515. u1 -= (int)u1;
  1516. if (u2 < 0.1f)
  1517. u2 = 1.0f;
  1518. }
  1519. else if (whichVert > profile.coords.Count - profile.numHollowVerts - 1)
  1520. {
  1521. u1 *= 2.0f;
  1522. u2 *= 2.0f;
  1523. }
  1524. }
  1525. newViewerFace1.uv1.U = u1;
  1526. newViewerFace1.uv2.U = u1;
  1527. newViewerFace1.uv3.U = u2;
  1528. newViewerFace1.uv1.V = 1.0f - node.percentOfPath;
  1529. newViewerFace1.uv2.V = lastV;
  1530. newViewerFace1.uv3.V = lastV;
  1531. newViewerFace2.uv1.U = u1;
  1532. newViewerFace2.uv2.U = u2;
  1533. newViewerFace2.uv3.U = u2;
  1534. newViewerFace2.uv1.V = 1.0f - node.percentOfPath;
  1535. newViewerFace2.uv2.V = lastV;
  1536. newViewerFace2.uv3.V = 1.0f - node.percentOfPath;
  1537. newViewerFace1.v1 = this.coords[i];
  1538. newViewerFace1.v2 = this.coords[i - numVerts];
  1539. newViewerFace1.v3 = this.coords[iNext - numVerts];
  1540. newViewerFace2.v1 = this.coords[i];
  1541. newViewerFace2.v2 = this.coords[iNext - numVerts];
  1542. newViewerFace2.v3 = this.coords[iNext];
  1543. newViewerFace1.coordIndex1 = i;
  1544. newViewerFace1.coordIndex2 = i - numVerts;
  1545. newViewerFace1.coordIndex3 = iNext - numVerts;
  1546. newViewerFace2.coordIndex1 = i;
  1547. newViewerFace2.coordIndex2 = iNext - numVerts;
  1548. newViewerFace2.coordIndex3 = iNext;
  1549. // profile cut faces
  1550. if (whichVert == cut1Vert)
  1551. {
  1552. newViewerFace1.n1 = newLayer.cutNormal1;
  1553. newViewerFace1.n2 = newViewerFace1.n3 = lastCutNormal1;
  1554. newViewerFace2.n1 = newViewerFace2.n3 = newLayer.cutNormal1;
  1555. newViewerFace2.n2 = lastCutNormal1;
  1556. }
  1557. else if (whichVert == cut2Vert)
  1558. {
  1559. newViewerFace1.n1 = newLayer.cutNormal2;
  1560. newViewerFace1.n2 = newViewerFace1.n3 = lastCutNormal2;
  1561. newViewerFace2.n1 = newViewerFace2.n3 = newLayer.cutNormal2;
  1562. newViewerFace2.n2 = lastCutNormal2;
  1563. }
  1564. else // outer and hollow faces
  1565. {
  1566. if ((sides < 5 && whichVert < newLayer.numOuterVerts) || (hollowSides < 5 && whichVert >= newLayer.numOuterVerts))
  1567. { // looks terrible when path is twisted... need vertex normals here
  1568. newViewerFace1.CalcSurfaceNormal();
  1569. newViewerFace2.CalcSurfaceNormal();
  1570. }
  1571. else
  1572. {
  1573. newViewerFace1.n1 = this.normals[i];
  1574. newViewerFace1.n2 = this.normals[i - numVerts];
  1575. newViewerFace1.n3 = this.normals[iNext - numVerts];
  1576. newViewerFace2.n1 = this.normals[i];
  1577. newViewerFace2.n2 = this.normals[iNext - numVerts];
  1578. newViewerFace2.n3 = this.normals[iNext];
  1579. }
  1580. }
  1581. this.viewerFaces.Add(newViewerFace1);
  1582. this.viewerFaces.Add(newViewerFace2);
  1583. }
  1584. }
  1585. }
  1586. lastCutNormal1 = newLayer.cutNormal1;
  1587. lastCutNormal2 = newLayer.cutNormal2;
  1588. lastV = 1.0f - node.percentOfPath;
  1589. if (needEndFaces && nodeIndex == path.pathNodes.Count - 1 && viewerMode)
  1590. {
  1591. // add the top faces to the viewerFaces list here
  1592. Coord faceNormal = newLayer.faceNormal;
  1593. ViewerFace newViewerFace = new ViewerFace();
  1594. newViewerFace.primFaceNumber = 0;
  1595. int numFaces = newLayer.faces.Count;
  1596. List<Face> faces = newLayer.faces;
  1597. for (int i = 0; i < numFaces; i++)
  1598. {
  1599. Face face = faces[i];
  1600. newViewerFace.v1 = newLayer.coords[face.v1 - coordsLen];
  1601. newViewerFace.v2 = newLayer.coords[face.v2 - coordsLen];
  1602. newViewerFace.v3 = newLayer.coords[face.v3 - coordsLen];
  1603. newViewerFace.coordIndex1 = face.v1 - coordsLen;
  1604. newViewerFace.coordIndex2 = face.v2 - coordsLen;
  1605. newViewerFace.coordIndex3 = face.v3 - coordsLen;
  1606. newViewerFace.n1 = faceNormal;
  1607. newViewerFace.n2 = faceNormal;
  1608. newViewerFace.n3 = faceNormal;
  1609. newViewerFace.uv1 = newLayer.faceUVs[face.v1 - coordsLen];
  1610. newViewerFace.uv2 = newLayer.faceUVs[face.v2 - coordsLen];
  1611. newViewerFace.uv3 = newLayer.faceUVs[face.v3 - coordsLen];
  1612. this.viewerFaces.Add(newViewerFace);
  1613. }
  1614. }
  1615. } // for (int nodeIndex = 0; nodeIndex < path.pathNodes.Count; nodeIndex++)
  1616. }
  1617. /// <summary>
  1618. /// DEPRICATED - use Extrude(PathType.Linear) instead
  1619. /// Extrudes a profile along a straight line path. Used for prim types box, cylinder, and prism.
  1620. /// </summary>
  1621. ///
  1622. public void ExtrudeLinear()
  1623. {
  1624. this.Extrude(PathType.Linear);
  1625. }
  1626. /// <summary>
  1627. /// DEPRICATED - use Extrude(PathType.Circular) instead
  1628. /// Extrude a profile into a circular path prim mesh. Used for prim types torus, tube, and ring.
  1629. /// </summary>
  1630. ///
  1631. public void ExtrudeCircular()
  1632. {
  1633. this.Extrude(PathType.Circular);
  1634. }
  1635. private Coord SurfaceNormal(Coord c1, Coord c2, Coord c3)
  1636. {
  1637. Coord edge1 = new Coord(c2.X - c1.X, c2.Y - c1.Y, c2.Z - c1.Z);
  1638. Coord edge2 = new Coord(c3.X - c1.X, c3.Y - c1.Y, c3.Z - c1.Z);
  1639. Coord normal = Coord.Cross(edge1, edge2);
  1640. normal.Normalize();
  1641. return normal;
  1642. }
  1643. private Coord SurfaceNormal(Face face)
  1644. {
  1645. return SurfaceNormal(this.coords[face.v1], this.coords[face.v2], this.coords[face.v3]);
  1646. }
  1647. /// <summary>
  1648. /// Calculate the surface normal for a face in the list of faces
  1649. /// </summary>
  1650. /// <param name="faceIndex"></param>
  1651. /// <returns></returns>
  1652. public Coord SurfaceNormal(int faceIndex)
  1653. {
  1654. int numFaces = this.faces.Count;
  1655. if (faceIndex < 0 || faceIndex >= numFaces)
  1656. throw new Exception("faceIndex out of range");
  1657. return SurfaceNormal(this.faces[faceIndex]);
  1658. }
  1659. /// <summary>
  1660. /// Duplicates a PrimMesh object. All object properties are copied by value, including lists.
  1661. /// </summary>
  1662. /// <returns></returns>
  1663. public PrimMesh Copy()
  1664. {
  1665. PrimMesh copy = new PrimMesh(this.sides, this.profileStart, this.profileEnd, this.hollow, this.hollowSides);
  1666. copy.twistBegin = this.twistBegin;
  1667. copy.twistEnd = this.twistEnd;
  1668. copy.topShearX = this.topShearX;
  1669. copy.topShearY = this.topShearY;
  1670. copy.pathCutBegin = this.pathCutBegin;
  1671. copy.pathCutEnd = this.pathCutEnd;
  1672. copy.dimpleBegin = this.dimpleBegin;
  1673. copy.dimpleEnd = this.dimpleEnd;
  1674. copy.skew = this.skew;
  1675. copy.holeSizeX = this.holeSizeX;
  1676. copy.holeSizeY = this.holeSizeY;
  1677. copy.taperX = this.taperX;
  1678. copy.taperY = this.taperY;
  1679. copy.radius = this.radius;
  1680. copy.revolutions = this.revolutions;
  1681. copy.stepsPerRevolution = this.stepsPerRevolution;
  1682. copy.calcVertexNormals = this.calcVertexNormals;
  1683. copy.normalsProcessed = this.normalsProcessed;
  1684. copy.viewerMode = this.viewerMode;
  1685. copy.numPrimFaces = this.numPrimFaces;
  1686. copy.errorMessage = this.errorMessage;
  1687. copy.coords = new List<Coord>(this.coords);
  1688. copy.faces = new List<Face>(this.faces);
  1689. copy.viewerFaces = new List<ViewerFace>(this.viewerFaces);
  1690. copy.normals = new List<Coord>(this.normals);
  1691. return copy;
  1692. }
  1693. /// <summary>
  1694. /// Calculate surface normals for all of the faces in the list of faces in this mesh
  1695. /// </summary>
  1696. public void CalcNormals()
  1697. {
  1698. if (normalsProcessed)
  1699. return;
  1700. normalsProcessed = true;
  1701. int numFaces = faces.Count;
  1702. if (!this.calcVertexNormals)
  1703. this.normals = new List<Coord>();
  1704. for (int i = 0; i < numFaces; i++)
  1705. {
  1706. Face face = faces[i];
  1707. this.normals.Add(SurfaceNormal(i).Normalize());
  1708. int normIndex = normals.Count - 1;
  1709. face.n1 = normIndex;
  1710. face.n2 = normIndex;
  1711. face.n3 = normIndex;
  1712. this.faces[i] = face;
  1713. }
  1714. }
  1715. /// <summary>
  1716. /// Adds a value to each XYZ vertex coordinate in the mesh
  1717. /// </summary>
  1718. /// <param name="x"></param>
  1719. /// <param name="y"></param>
  1720. /// <param name="z"></param>
  1721. public void AddPos(float x, float y, float z)
  1722. {
  1723. int i;
  1724. int numVerts = this.coords.Count;
  1725. Coord vert;
  1726. for (i = 0; i < numVerts; i++)
  1727. {
  1728. vert = this.coords[i];
  1729. vert.X += x;
  1730. vert.Y += y;
  1731. vert.Z += z;
  1732. this.coords[i] = vert;
  1733. }
  1734. if (this.viewerFaces != null)
  1735. {
  1736. int numViewerFaces = this.viewerFaces.Count;
  1737. for (i = 0; i < numViewerFaces; i++)
  1738. {
  1739. ViewerFace v = this.viewerFaces[i];
  1740. v.AddPos(x, y, z);
  1741. this.viewerFaces[i] = v;
  1742. }
  1743. }
  1744. }
  1745. /// <summary>
  1746. /// Rotates the mesh
  1747. /// </summary>
  1748. /// <param name="q"></param>
  1749. public void AddRot(Quat q)
  1750. {
  1751. int i;
  1752. int numVerts = this.coords.Count;
  1753. for (i = 0; i < numVerts; i++)
  1754. this.coords[i] *= q;
  1755. if (this.normals != null)
  1756. {
  1757. int numNormals = this.normals.Count;
  1758. for (i = 0; i < numNormals; i++)
  1759. this.normals[i] *= q;
  1760. }
  1761. if (this.viewerFaces != null)
  1762. {
  1763. int numViewerFaces = this.viewerFaces.Count;
  1764. for (i = 0; i < numViewerFaces; i++)
  1765. {
  1766. ViewerFace v = this.viewerFaces[i];
  1767. v.v1 *= q;
  1768. v.v2 *= q;
  1769. v.v3 *= q;
  1770. v.n1 *= q;
  1771. v.n2 *= q;
  1772. v.n3 *= q;
  1773. this.viewerFaces[i] = v;
  1774. }
  1775. }
  1776. }
  1777. #if VERTEX_INDEXER
  1778. public VertexIndexer GetVertexIndexer()
  1779. {
  1780. if (this.viewerMode && this.viewerFaces.Count > 0)
  1781. return new VertexIndexer(this);
  1782. return null;
  1783. }
  1784. #endif
  1785. /// <summary>
  1786. /// Scales the mesh
  1787. /// </summary>
  1788. /// <param name="x"></param>
  1789. /// <param name="y"></param>
  1790. /// <param name="z"></param>
  1791. public void Scale(float x, float y, float z)
  1792. {
  1793. int i;
  1794. int numVerts = this.coords.Count;
  1795. //Coord vert;
  1796. Coord m = new Coord(x, y, z);
  1797. for (i = 0; i < numVerts; i++)
  1798. this.coords[i] *= m;
  1799. if (this.viewerFaces != null)
  1800. {
  1801. int numViewerFaces = this.viewerFaces.Count;
  1802. for (i = 0; i < numViewerFaces; i++)
  1803. {
  1804. ViewerFace v = this.viewerFaces[i];
  1805. v.v1 *= m;
  1806. v.v2 *= m;
  1807. v.v3 *= m;
  1808. this.viewerFaces[i] = v;
  1809. }
  1810. }
  1811. }
  1812. /// <summary>
  1813. /// Dumps the mesh to a Blender compatible "Raw" format file
  1814. /// </summary>
  1815. /// <param name="path"></param>
  1816. /// <param name="name"></param>
  1817. /// <param name="title"></param>
  1818. public void DumpRaw(String path, String name, String title)
  1819. {
  1820. if (path == null)
  1821. return;
  1822. String fileName = name + "_" + title + ".raw";
  1823. String completePath = System.IO.Path.Combine(path, fileName);
  1824. StreamWriter sw = new StreamWriter(completePath);
  1825. for (int i = 0; i < this.faces.Count; i++)
  1826. {
  1827. string s = this.coords[this.faces[i].v1].ToString();
  1828. s += " " + this.coords[this.faces[i].v2].ToString();
  1829. s += " " + this.coords[this.faces[i].v3].ToString();
  1830. sw.WriteLine(s);
  1831. }
  1832. sw.Close();
  1833. }
  1834. }
  1835. }