PrimMesher.cs 81 KB

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