1
0

PrimMesher.cs 82 KB

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