PBJ.cs 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  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. namespace PBJ
  29. {
  30. public class IMessage
  31. {
  32. public virtual Google.ProtocolBuffers.IMessage _PBJISuper { get { return null; } }
  33. protected virtual bool _HasAllPBJFields { get { return true; } }
  34. public void WriteTo(Google.ProtocolBuffers.CodedOutputStream output)
  35. {
  36. _PBJISuper.WriteTo(output);
  37. }
  38. public override bool Equals(object other)
  39. {
  40. return _PBJISuper.Equals(other);
  41. }
  42. public int SerializedSize { get { return _PBJISuper.SerializedSize; } }
  43. public override int GetHashCode() { return _PBJISuper.GetHashCode(); }
  44. public override string ToString()
  45. {
  46. return _PBJISuper.ToString();
  47. }
  48. public virtual IBuilder WeakCreateBuilderForType() { return null; }
  49. public Google.ProtocolBuffers.ByteString ToByteString()
  50. {
  51. return _PBJISuper.ToByteString();
  52. }
  53. public byte[] ToByteArray()
  54. {
  55. return _PBJISuper.ToByteArray();
  56. }
  57. public void WriteTo(global::System.IO.Stream output)
  58. {
  59. _PBJISuper.WriteTo(output);
  60. }
  61. // Google.ProtocolBuffers.MessageDescriptor DescriptorForType { get {return _PBJISuper.DescriptorForType;} }
  62. public Google.ProtocolBuffers.UnknownFieldSet UnknownFields { get { return _PBJISuper.UnknownFields; } }
  63. public class IBuilder
  64. {
  65. public virtual Google.ProtocolBuffers.IBuilder _PBJISuper { get { return null; } }
  66. protected virtual bool _HasAllPBJFields { get { return true; } }
  67. }
  68. }
  69. public struct Vector2f
  70. {
  71. public float x;
  72. public float y;
  73. public Vector2f(float _x, float _y)
  74. {
  75. x = _x; y = _y;
  76. }
  77. public Vector2f(Vector2f cpy)
  78. {
  79. x = cpy.x; y = cpy.y;
  80. }
  81. public Vector2f Negate()
  82. {
  83. return new Vector2f(-x, -y);
  84. }
  85. public Vector2f Add(Vector2f rhs)
  86. {
  87. return new Vector2f(x + rhs.x, y + rhs.y);
  88. }
  89. public Vector2f Subtract(Vector2f rhs)
  90. {
  91. return new Vector2f(x - rhs.x, y - rhs.y);
  92. }
  93. public Vector2f Multiply(Vector2f rhs)
  94. {
  95. return new Vector2f(x * rhs.x, y * rhs.y);
  96. }
  97. public Vector2f Multiply(float s)
  98. {
  99. return new Vector2f(x * s, y * s);
  100. }
  101. public Vector2f Divide(Vector2f rhs)
  102. {
  103. return new Vector2f(x / rhs.x, y / rhs.y);
  104. }
  105. public Vector2f Divide(float s)
  106. {
  107. return new Vector2f(x / s, y / s);
  108. }
  109. public float Dot(Vector2f rhs)
  110. {
  111. return (x * rhs.x + y * rhs.y);
  112. }
  113. public void Normalize()
  114. {
  115. float len = Length;
  116. if (len != 0.0)
  117. {
  118. x /= len; y /= len;
  119. }
  120. }
  121. public Vector2f Normalized
  122. {
  123. get
  124. {
  125. Vector2f normed = new Vector2f(this);
  126. normed.Normalize();
  127. return normed;
  128. }
  129. }
  130. public float SquaredLength
  131. {
  132. get
  133. {
  134. return (x * x + y * y);
  135. }
  136. }
  137. public float Length
  138. {
  139. get
  140. {
  141. return (float)Math.Sqrt(SquaredLength);
  142. }
  143. }
  144. public override string ToString()
  145. {
  146. return String.Format("<{0}, {1}>", x, y);
  147. }
  148. public static Vector2f operator -(Vector2f uo)
  149. {
  150. return uo.Negate();
  151. }
  152. public static Vector2f operator +(Vector2f lhs, Vector2f rhs)
  153. {
  154. return lhs.Add(rhs);
  155. }
  156. public static Vector2f operator -(Vector2f lhs, Vector2f rhs)
  157. {
  158. return lhs.Subtract(rhs);
  159. }
  160. public static Vector2f operator *(Vector2f lhs, Vector2f rhs)
  161. {
  162. return lhs.Multiply(rhs);
  163. }
  164. public static Vector2f operator *(Vector2f lhs, float rhs)
  165. {
  166. return lhs.Multiply(rhs);
  167. }
  168. public static Vector2f operator *(float lhs, Vector2f rhs)
  169. {
  170. return rhs.Multiply(lhs);
  171. }
  172. public static Vector2f operator /(Vector2f lhs, Vector2f rhs)
  173. {
  174. return lhs.Divide(rhs);
  175. }
  176. public static Vector2f operator /(Vector2f lhs, float rhs)
  177. {
  178. return lhs.Divide(rhs);
  179. }
  180. } // struct Vector2f
  181. public struct Vector2d
  182. {
  183. public double x;
  184. public double y;
  185. public Vector2d(double _x, double _y)
  186. {
  187. x = _x; y = _y;
  188. }
  189. public Vector2d(Vector2d cpy)
  190. {
  191. x = cpy.x; y = cpy.y;
  192. }
  193. public Vector2d Negate()
  194. {
  195. return new Vector2d(-x, -y);
  196. }
  197. public Vector2d Add(Vector2d rhs)
  198. {
  199. return new Vector2d(x + rhs.x, y + rhs.y);
  200. }
  201. public Vector2d Subtract(Vector2d rhs)
  202. {
  203. return new Vector2d(x - rhs.x, y - rhs.y);
  204. }
  205. public Vector2d Multiply(Vector2d rhs)
  206. {
  207. return new Vector2d(x * rhs.x, y * rhs.y);
  208. }
  209. public Vector2d Multiply(double s)
  210. {
  211. return new Vector2d(x * s, y * s);
  212. }
  213. public Vector2d Divide(Vector2d rhs)
  214. {
  215. return new Vector2d(x / rhs.x, y / rhs.y);
  216. }
  217. public Vector2d Divide(double s)
  218. {
  219. return new Vector2d(x / s, y / s);
  220. }
  221. public double Dot(Vector2d rhs)
  222. {
  223. return (x * rhs.x + y * rhs.y);
  224. }
  225. public void Normalize()
  226. {
  227. double len = Length;
  228. if (len != 0.0)
  229. {
  230. x /= len; y /= len;
  231. }
  232. }
  233. public Vector2d Normalized
  234. {
  235. get
  236. {
  237. Vector2d normed = new Vector2d(this);
  238. normed.Normalize();
  239. return normed;
  240. }
  241. }
  242. public double SquaredLength
  243. {
  244. get
  245. {
  246. return (x * x + y * y);
  247. }
  248. }
  249. public double Length
  250. {
  251. get
  252. {
  253. return Math.Sqrt(SquaredLength);
  254. }
  255. }
  256. public override string ToString()
  257. {
  258. return String.Format("<{0}, {1}>", x, y);
  259. }
  260. public static Vector2d operator -(Vector2d uo)
  261. {
  262. return uo.Negate();
  263. }
  264. public static Vector2d operator +(Vector2d lhs, Vector2d rhs)
  265. {
  266. return lhs.Add(rhs);
  267. }
  268. public static Vector2d operator -(Vector2d lhs, Vector2d rhs)
  269. {
  270. return lhs.Subtract(rhs);
  271. }
  272. public static Vector2d operator *(Vector2d lhs, Vector2d rhs)
  273. {
  274. return lhs.Multiply(rhs);
  275. }
  276. public static Vector2d operator *(Vector2d lhs, double rhs)
  277. {
  278. return lhs.Multiply(rhs);
  279. }
  280. public static Vector2d operator *(double lhs, Vector2d rhs)
  281. {
  282. return rhs.Multiply(lhs);
  283. }
  284. public static Vector2d operator /(Vector2d lhs, Vector2d rhs)
  285. {
  286. return lhs.Divide(rhs);
  287. }
  288. public static Vector2d operator /(Vector2d lhs, double rhs)
  289. {
  290. return lhs.Divide(rhs);
  291. }
  292. } // struct Vector2d
  293. public struct Vector3f
  294. {
  295. public float x;
  296. public float y;
  297. public float z;
  298. public Vector3f(float _x, float _y, float _z)
  299. {
  300. x = _x; y = _y; z = _z;
  301. }
  302. public Vector3f(Vector3f cpy)
  303. {
  304. x = cpy.x; y = cpy.y; z = cpy.z;
  305. }
  306. public Vector3f Negate()
  307. {
  308. return new Vector3f(-x, -y, -z);
  309. }
  310. public Vector3f Add(Vector3f rhs)
  311. {
  312. return new Vector3f(x + rhs.x, y + rhs.y, z + rhs.z);
  313. }
  314. public Vector3f Subtract(Vector3f rhs)
  315. {
  316. return new Vector3f(x - rhs.x, y - rhs.y, z - rhs.z);
  317. }
  318. public Vector3f Multiply(Vector3f rhs)
  319. {
  320. return new Vector3f(x * rhs.x, y * rhs.y, z * rhs.z);
  321. }
  322. public Vector3f Multiply(float s)
  323. {
  324. return new Vector3f(x * s, y * s, z * s);
  325. }
  326. public Vector3f Divide(Vector3f rhs)
  327. {
  328. return new Vector3f(x / rhs.x, y / rhs.y, z / rhs.z);
  329. }
  330. public Vector3f Divide(float s)
  331. {
  332. return new Vector3f(x / s, y / s, z / s);
  333. }
  334. public float Dot(Vector3f rhs)
  335. {
  336. return (x * rhs.x + y * rhs.y + z * rhs.z);
  337. }
  338. public Vector3f Cross(Vector3f rhs)
  339. {
  340. return new Vector3f(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
  341. }
  342. public void Normalize()
  343. {
  344. float len = Length;
  345. if (len != 0.0)
  346. {
  347. x /= len; y /= len; z /= len;
  348. }
  349. }
  350. public Vector3f Normalized
  351. {
  352. get
  353. {
  354. Vector3f normed = new Vector3f(this);
  355. normed.Normalize();
  356. return normed;
  357. }
  358. }
  359. public float SquaredLength
  360. {
  361. get
  362. {
  363. return (x * x + y * y + z * z);
  364. }
  365. }
  366. public float Length
  367. {
  368. get
  369. {
  370. return (float)Math.Sqrt(SquaredLength);
  371. }
  372. }
  373. public override string ToString()
  374. {
  375. return String.Format("<{0}, {1}, {2}>", x, y, z);
  376. }
  377. public static Vector3f operator -(Vector3f uo)
  378. {
  379. return uo.Negate();
  380. }
  381. public static Vector3f operator +(Vector3f lhs, Vector3f rhs)
  382. {
  383. return lhs.Add(rhs);
  384. }
  385. public static Vector3f operator -(Vector3f lhs, Vector3f rhs)
  386. {
  387. return lhs.Subtract(rhs);
  388. }
  389. public static Vector3f operator *(Vector3f lhs, Vector3f rhs)
  390. {
  391. return lhs.Multiply(rhs);
  392. }
  393. public static Vector3f operator *(Vector3f lhs, float rhs)
  394. {
  395. return lhs.Multiply(rhs);
  396. }
  397. public static Vector3f operator *(float lhs, Vector3f rhs)
  398. {
  399. return rhs.Multiply(lhs);
  400. }
  401. public static Vector3f operator /(Vector3f lhs, Vector3f rhs)
  402. {
  403. return lhs.Divide(rhs);
  404. }
  405. public static Vector3f operator /(Vector3f lhs, float rhs)
  406. {
  407. return lhs.Divide(rhs);
  408. }
  409. } // struct Vector3f
  410. public struct Vector3d
  411. {
  412. public double x;
  413. public double y;
  414. public double z;
  415. public Vector3d(double _x, double _y, double _z)
  416. {
  417. x = _x; y = _y; z = _z;
  418. }
  419. public Vector3d(Vector3d cpy)
  420. {
  421. x = cpy.x; y = cpy.y; z = cpy.z;
  422. }
  423. public Vector3d Negate()
  424. {
  425. return new Vector3d(-x, -y, -z);
  426. }
  427. public Vector3d Add(Vector3d rhs)
  428. {
  429. return new Vector3d(x + rhs.x, y + rhs.y, z + rhs.z);
  430. }
  431. public Vector3d Subtract(Vector3d rhs)
  432. {
  433. return new Vector3d(x - rhs.x, y - rhs.y, z - rhs.z);
  434. }
  435. public Vector3d Multiply(Vector3d rhs)
  436. {
  437. return new Vector3d(x * rhs.x, y * rhs.y, z * rhs.z);
  438. }
  439. public Vector3d Multiply(double s)
  440. {
  441. return new Vector3d(x * s, y * s, z * s);
  442. }
  443. public Vector3d Divide(Vector3d rhs)
  444. {
  445. return new Vector3d(x / rhs.x, y / rhs.y, z / rhs.z);
  446. }
  447. public Vector3d Divide(double s)
  448. {
  449. return new Vector3d(x / s, y / s, z / s);
  450. }
  451. public double Dot(Vector3d rhs)
  452. {
  453. return (x * rhs.x + y * rhs.y + z * rhs.z);
  454. }
  455. public Vector3d Cross(Vector3d rhs)
  456. {
  457. return new Vector3d(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x);
  458. }
  459. public void Normalize()
  460. {
  461. double len = Length;
  462. if (len != 0.0)
  463. {
  464. x /= len; y /= len; z /= len;
  465. }
  466. }
  467. public Vector3d Normalized
  468. {
  469. get
  470. {
  471. Vector3d normed = new Vector3d(this);
  472. normed.Normalize();
  473. return normed;
  474. }
  475. }
  476. public double SquaredLength
  477. {
  478. get
  479. {
  480. return (x * x + y * y + z * z);
  481. }
  482. }
  483. public double Length
  484. {
  485. get
  486. {
  487. return Math.Sqrt(SquaredLength);
  488. }
  489. }
  490. public override string ToString()
  491. {
  492. return String.Format("<{0}, {1}, {2}>", x, y, z);
  493. }
  494. public static Vector3d operator -(Vector3d uo)
  495. {
  496. return uo.Negate();
  497. }
  498. public static Vector3d operator +(Vector3d lhs, Vector3d rhs)
  499. {
  500. return lhs.Add(rhs);
  501. }
  502. public static Vector3d operator -(Vector3d lhs, Vector3d rhs)
  503. {
  504. return lhs.Subtract(rhs);
  505. }
  506. public static Vector3d operator *(Vector3d lhs, Vector3d rhs)
  507. {
  508. return lhs.Multiply(rhs);
  509. }
  510. public static Vector3d operator *(Vector3d lhs, double rhs)
  511. {
  512. return lhs.Multiply(rhs);
  513. }
  514. public static Vector3d operator *(double lhs, Vector3d rhs)
  515. {
  516. return rhs.Multiply(lhs);
  517. }
  518. public static Vector3d operator /(Vector3d lhs, Vector3d rhs)
  519. {
  520. return lhs.Divide(rhs);
  521. }
  522. public static Vector3d operator /(Vector3d lhs, double rhs)
  523. {
  524. return lhs.Divide(rhs);
  525. }
  526. } // struct Vector3d
  527. public struct Quaternion
  528. {
  529. public float w;
  530. public float x;
  531. public float y;
  532. public float z;
  533. public Quaternion(float _w, float _x, float _y, float _z)
  534. {
  535. w = _w; x = _x; y = _y; z = _z;
  536. }
  537. public Quaternion(Quaternion cpy)
  538. {
  539. w = cpy.w; x = cpy.x; y = cpy.y; z = cpy.z;
  540. }
  541. public static readonly Quaternion Identity = new Quaternion((float)1.0, (float)0.0, (float)0.0, (float)0.0);
  542. public static Quaternion FromAxisAngle(Vector3f axis, float rads)
  543. {
  544. float halfAngle = rads * 0.5f;
  545. float sinHalf = (float)Math.Sin(halfAngle);
  546. float w = (float)Math.Cos(halfAngle);
  547. float x = sinHalf * axis.x;
  548. float y = sinHalf * axis.y;
  549. float z = sinHalf * axis.z;
  550. return new Quaternion(w, x, y, z);
  551. }
  552. public static Quaternion FromAxisAngle(Vector3d axis, float rads)
  553. {
  554. float halfAngle = rads * 0.5f;
  555. float sinHalf = (float)Math.Sin(halfAngle);
  556. float w = (float)Math.Cos(halfAngle);
  557. float x = (float)(sinHalf * axis.x);
  558. float y = (float)(sinHalf * axis.y);
  559. float z = (float)(sinHalf * axis.z);
  560. return new Quaternion(w, x, y, z);
  561. }
  562. public Quaternion Add(Quaternion rhs)
  563. {
  564. return new Quaternion(w + rhs.w, x + rhs.x, y + rhs.y, z + rhs.z);
  565. }
  566. public Quaternion Subtract(Quaternion rhs)
  567. {
  568. return new Quaternion(w - rhs.w, x - rhs.x, y - rhs.y, z - rhs.z);
  569. }
  570. public Quaternion Multiply(Quaternion rhs)
  571. {
  572. return new Quaternion(
  573. w * rhs.w - x * rhs.x - y * rhs.y - z * rhs.z,
  574. w * rhs.x + x * rhs.w + y * rhs.z - z * rhs.y,
  575. w * rhs.y + y * rhs.w + z * rhs.x - x * rhs.z,
  576. w * rhs.z + z * rhs.w + x * rhs.y - y * rhs.x
  577. );
  578. }
  579. public Vector3f Multiply(Vector3f rhs)
  580. {
  581. Vector3f qvec = new Vector3f(x, y, z);
  582. Vector3f uv = qvec.Cross(rhs);
  583. Vector3f uuv = qvec.Cross(uv);
  584. uv *= 2.0f * w;
  585. uuv *= 2.0f;
  586. return rhs + uv + uuv;
  587. }
  588. public Vector3d Multiply(Vector3d rhs)
  589. {
  590. Vector3d qvec = new Vector3d(x, y, z);
  591. Vector3d uv = qvec.Cross(rhs);
  592. Vector3d uuv = qvec.Cross(uv);
  593. uv *= 2.0f * w;
  594. uuv *= 2.0f;
  595. return rhs + uv + uuv;
  596. }
  597. public Quaternion Multiply(float rhs)
  598. {
  599. return new Quaternion(w * rhs, x * rhs, y * rhs, z * rhs);
  600. }
  601. public Quaternion Negate()
  602. {
  603. return new Quaternion(-w, -x, -y, -z);
  604. }
  605. public float Dot(Quaternion rhs)
  606. {
  607. return (w * rhs.w + x * rhs.x + y * rhs.y + z * rhs.z);
  608. }
  609. public float Norm
  610. {
  611. get
  612. {
  613. return (float)Math.Sqrt(w * w + x * x + y * y + z * z);
  614. }
  615. }
  616. public float SquareNorm
  617. {
  618. get
  619. {
  620. return (w * w + x * x + y * y + z * z);
  621. }
  622. }
  623. public void Normalize()
  624. {
  625. float len = SquareNorm;
  626. if (len == 0.0) return;
  627. float factor = 1.0f / (float)Math.Sqrt(len);
  628. this *= factor;
  629. }
  630. public Quaternion Normalized
  631. {
  632. get
  633. {
  634. Quaternion q = new Quaternion(this);
  635. q.Normalize();
  636. return q;
  637. }
  638. }
  639. public Quaternion Inverse
  640. {
  641. get
  642. {
  643. float norm = SquareNorm;
  644. if (norm > 0.0)
  645. {
  646. double invnorm = 1.0 / norm;
  647. return new Quaternion((float)(w * invnorm), (float)(-x * invnorm), (float)(-y * invnorm), (float)(-z * invnorm));
  648. }
  649. else
  650. return new Quaternion((float)0.0, 0.0f, 0.0f, 0.0f);
  651. }
  652. }
  653. public override string ToString()
  654. {
  655. return String.Format("<{0}, {1}, {2}, {3}>", w, x, y, z);
  656. }
  657. public static Quaternion operator -(Quaternion uo)
  658. {
  659. return uo.Negate();
  660. }
  661. public static Quaternion operator +(Quaternion lhs, Quaternion rhs)
  662. {
  663. return lhs.Add(rhs);
  664. }
  665. public static Quaternion operator -(Quaternion lhs, Quaternion rhs)
  666. {
  667. return lhs.Subtract(rhs);
  668. }
  669. public static Vector3f operator *(Quaternion lhs, Vector3f rhs)
  670. {
  671. return lhs.Multiply(rhs);
  672. }
  673. public static Vector3d operator *(Quaternion lhs, Vector3d rhs)
  674. {
  675. return lhs.Multiply(rhs);
  676. }
  677. public static Quaternion operator *(Quaternion lhs, Quaternion rhs)
  678. {
  679. return lhs.Multiply(rhs);
  680. }
  681. public static Quaternion operator *(Quaternion lhs, float rhs)
  682. {
  683. return lhs.Multiply(rhs);
  684. }
  685. public static Quaternion operator *(float lhs, Quaternion rhs)
  686. {
  687. return rhs.Multiply(lhs);
  688. }
  689. } // struct Quaternion
  690. public struct Vector4f
  691. {
  692. public float x;
  693. public float y;
  694. public float z;
  695. public float w;
  696. public Vector4f(float _x, float _y, float _z, float _w)
  697. {
  698. x = _x; y = _y; z = _z; w = _w;
  699. }
  700. public Vector4f(Vector4f cpy)
  701. {
  702. x = cpy.x; y = cpy.y; z = cpy.z; w = cpy.w;
  703. }
  704. public Vector4f Negate()
  705. {
  706. return new Vector4f(-x, -y, -z, -w);
  707. }
  708. public Vector4f Add(Vector4f rhs)
  709. {
  710. return new Vector4f(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w);
  711. }
  712. public Vector4f Subtract(Vector4f rhs)
  713. {
  714. return new Vector4f(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w);
  715. }
  716. public Vector4f Multiply(Vector4f rhs)
  717. {
  718. return new Vector4f(x * rhs.x, y * rhs.y, z * rhs.z, w * rhs.w);
  719. }
  720. public Vector4f Multiply(float s)
  721. {
  722. return new Vector4f(x * s, y * s, z * s, w * s);
  723. }
  724. public Vector4f Divide(Vector4f rhs)
  725. {
  726. return new Vector4f(x / rhs.x, y / rhs.y, z / rhs.z, w / rhs.w);
  727. }
  728. public Vector4f Divide(float s)
  729. {
  730. return new Vector4f(x / s, y / s, z / s, w / s);
  731. }
  732. public float Dot(Vector4f rhs)
  733. {
  734. return (x * rhs.x + y * rhs.y + z * rhs.z + w * rhs.w);
  735. }
  736. public void Normalize()
  737. {
  738. float len = Length;
  739. if (len != 0.0)
  740. {
  741. x /= len; y /= len; z /= len; w /= len;
  742. }
  743. }
  744. public Vector4f Normalized
  745. {
  746. get
  747. {
  748. Vector4f normed = new Vector4f(this);
  749. normed.Normalize();
  750. return normed;
  751. }
  752. }
  753. public float SquaredLength
  754. {
  755. get
  756. {
  757. return (x * x + y * y + z * z + w * w);
  758. }
  759. }
  760. public float Length
  761. {
  762. get
  763. {
  764. return (float)Math.Sqrt(SquaredLength);
  765. }
  766. }
  767. public override string ToString()
  768. {
  769. return String.Format("<{0}, {1}, {2}, {3}>", x, y, z, w);
  770. }
  771. public static Vector4f operator -(Vector4f uo)
  772. {
  773. return uo.Negate();
  774. }
  775. public static Vector4f operator +(Vector4f lhs, Vector4f rhs)
  776. {
  777. return lhs.Add(rhs);
  778. }
  779. public static Vector4f operator -(Vector4f lhs, Vector4f rhs)
  780. {
  781. return lhs.Subtract(rhs);
  782. }
  783. public static Vector4f operator *(Vector4f lhs, Vector4f rhs)
  784. {
  785. return lhs.Multiply(rhs);
  786. }
  787. public static Vector4f operator *(Vector4f lhs, float rhs)
  788. {
  789. return lhs.Multiply(rhs);
  790. }
  791. public static Vector4f operator *(float lhs, Vector4f rhs)
  792. {
  793. return rhs.Multiply(lhs);
  794. }
  795. public static Vector4f operator /(Vector4f lhs, Vector4f rhs)
  796. {
  797. return lhs.Divide(rhs);
  798. }
  799. public static Vector4f operator /(Vector4f lhs, float rhs)
  800. {
  801. return lhs.Divide(rhs);
  802. }
  803. } // struct Vector4f
  804. public struct Vector4d
  805. {
  806. public double x;
  807. public double y;
  808. public double z;
  809. public double w;
  810. public Vector4d(double _x, double _y, double _z, double _w)
  811. {
  812. x = _x; y = _y; z = _z; w = _w;
  813. }
  814. public Vector4d(Vector4d cpy)
  815. {
  816. x = cpy.x; y = cpy.y; z = cpy.z; w = cpy.w;
  817. }
  818. public Vector4d Negate()
  819. {
  820. return new Vector4d(-x, -y, -z, -w);
  821. }
  822. public Vector4d Add(Vector4d rhs)
  823. {
  824. return new Vector4d(x + rhs.x, y + rhs.y, z + rhs.z, w + rhs.w);
  825. }
  826. public Vector4d Subtract(Vector4d rhs)
  827. {
  828. return new Vector4d(x - rhs.x, y - rhs.y, z - rhs.z, w - rhs.w);
  829. }
  830. public Vector4d Multiply(Vector4d rhs)
  831. {
  832. return new Vector4d(x * rhs.x, y * rhs.y, z * rhs.z, w * rhs.w);
  833. }
  834. public Vector4d Multiply(double s)
  835. {
  836. return new Vector4d(x * s, y * s, z * s, w * s);
  837. }
  838. public Vector4d Divide(Vector4d rhs)
  839. {
  840. return new Vector4d(x / rhs.x, y / rhs.y, z / rhs.z, w / rhs.w);
  841. }
  842. public Vector4d Divide(double s)
  843. {
  844. return new Vector4d(x / s, y / s, z / s, w / s);
  845. }
  846. public double Dot(Vector4d rhs)
  847. {
  848. return (x * rhs.x + y * rhs.y + z * rhs.z + w * rhs.w);
  849. }
  850. public void Normalize()
  851. {
  852. double len = Length;
  853. if (len != 0.0)
  854. {
  855. x /= len; y /= len; z /= len; w /= len;
  856. }
  857. }
  858. public Vector4d Normalized
  859. {
  860. get
  861. {
  862. Vector4d normed = new Vector4d(this);
  863. normed.Normalize();
  864. return normed;
  865. }
  866. }
  867. public double SquaredLength
  868. {
  869. get
  870. {
  871. return (x * x + y * y + z * z + w * w);
  872. }
  873. }
  874. public double Length
  875. {
  876. get
  877. {
  878. return Math.Sqrt(SquaredLength);
  879. }
  880. }
  881. public override string ToString()
  882. {
  883. return String.Format("<{0}, {1}, {2}, {3}>", x, y, z, w);
  884. }
  885. public static Vector4d operator -(Vector4d uo)
  886. {
  887. return uo.Negate();
  888. }
  889. public static Vector4d operator +(Vector4d lhs, Vector4d rhs)
  890. {
  891. return lhs.Add(rhs);
  892. }
  893. public static Vector4d operator -(Vector4d lhs, Vector4d rhs)
  894. {
  895. return lhs.Subtract(rhs);
  896. }
  897. public static Vector4d operator *(Vector4d lhs, Vector4d rhs)
  898. {
  899. return lhs.Multiply(rhs);
  900. }
  901. public static Vector4d operator *(Vector4d lhs, double rhs)
  902. {
  903. return lhs.Multiply(rhs);
  904. }
  905. public static Vector4d operator *(double lhs, Vector4d rhs)
  906. {
  907. return rhs.Multiply(lhs);
  908. }
  909. public static Vector4d operator /(Vector4d lhs, Vector4d rhs)
  910. {
  911. return lhs.Divide(rhs);
  912. }
  913. public static Vector4d operator /(Vector4d lhs, double rhs)
  914. {
  915. return lhs.Divide(rhs);
  916. }
  917. } // struct Vector4d
  918. public struct BoundingBox3f3f
  919. {
  920. Vector3f mMin;
  921. Vector3f mDiag;
  922. public BoundingBox3f3f(float minx, float miny, float minz, float diagx, float diagy, float diagz)
  923. {
  924. mMin.x = minx;
  925. mMin.y = miny;
  926. mMin.z = minz;
  927. mDiag.x = diagx;
  928. mDiag.y = diagy;
  929. mDiag.z = diagz;
  930. }
  931. public BoundingBox3f3f(Vector3f min, Vector3f max)
  932. {
  933. mMin = min;
  934. mDiag = (max - min);
  935. }
  936. public BoundingBox3f3f(BoundingBox3f3f cpy, Vector3f scale)
  937. {
  938. mMin.x = (float)(cpy.mMin.x * scale.x);
  939. mMin.y = (float)(cpy.mMin.y * scale.y);
  940. mMin.z = (float)(cpy.mMin.z * scale.z);
  941. mDiag.x = (float)(cpy.mDiag.x * scale.x);
  942. mDiag.y = (float)(cpy.mDiag.y * scale.y);
  943. mDiag.z = (float)(cpy.mDiag.z * scale.z);
  944. }
  945. public Vector3f Min
  946. {
  947. get
  948. {
  949. return new Vector3f(mMin.x, mMin.y, mMin.z);
  950. }
  951. }
  952. public Vector3f Max
  953. {
  954. get
  955. {
  956. return new Vector3f(mMin.x + mDiag.x, mMin.y + mDiag.y, mMin.z + mDiag.z);
  957. }
  958. }
  959. public Vector3f Diag
  960. {
  961. get
  962. {
  963. return new Vector3f(mDiag.x, mDiag.y, mDiag.z);
  964. }
  965. }
  966. public override string ToString()
  967. {
  968. return "[" + this.Min.ToString() + " - " + this.Max.ToString() + "]";
  969. }
  970. public BoundingBox3f3f Merge(BoundingBox3f3f other)
  971. {
  972. Vector3f thisMax = Max;
  973. Vector3f otherMax = other.Max;
  974. bool xless = other.mMin.x > mMin.x;
  975. bool yless = other.mMin.y > mMin.y;
  976. bool zless = other.mMin.z > mMin.z;
  977. bool xmore = otherMax.x < thisMax.x;
  978. bool ymore = otherMax.y < thisMax.y;
  979. bool zmore = otherMax.z < thisMax.z;
  980. return new BoundingBox3f3f(xless ? mMin.x : other.mMin.x,
  981. yless ? mMin.y : other.mMin.y,
  982. zless ? mMin.z : other.mMin.z,
  983. xmore ? (xless ? mDiag.x : otherMax.x - mMin.x) : (xless ? thisMax.x - other.mMin.x : other.mDiag.x),
  984. ymore ? (yless ? mDiag.y : otherMax.y - mMin.y) : (yless ? thisMax.y - other.mMin.y : other.mDiag.y),
  985. zmore ? (zless ? mDiag.z : otherMax.z - mMin.z) : (zless ? thisMax.z - other.mMin.z : other.mDiag.z));
  986. }
  987. } // struct BoundingBox
  988. public struct BoundingBox3d3f
  989. {
  990. Vector3d mMin;
  991. Vector3f mDiag;
  992. public BoundingBox3d3f(double minx, double miny, double minz, float diagx, float diagy, float diagz)
  993. {
  994. mMin.x = minx;
  995. mMin.y = miny;
  996. mMin.z = minz;
  997. mDiag.x = diagx;
  998. mDiag.y = diagy;
  999. mDiag.z = diagz;
  1000. }
  1001. public BoundingBox3d3f(Vector3d min, Vector3f max)
  1002. {
  1003. mMin = min;
  1004. mDiag = new Vector3f((float)(max.x - min.x),
  1005. (float)(max.y - min.y),
  1006. (float)(max.z - min.z));
  1007. }
  1008. public BoundingBox3d3f(BoundingBox3d3f cpy, Vector3d scale)
  1009. {
  1010. mMin.x = (double)(cpy.mMin.x * scale.x);
  1011. mMin.y = (double)(cpy.mMin.y * scale.y);
  1012. mMin.z = (double)(cpy.mMin.z * scale.z);
  1013. mDiag.x = (float)(cpy.mDiag.x * scale.x);
  1014. mDiag.y = (float)(cpy.mDiag.y * scale.y);
  1015. mDiag.z = (float)(cpy.mDiag.z * scale.z);
  1016. }
  1017. public Vector3d Min
  1018. {
  1019. get
  1020. {
  1021. return new Vector3d(mMin.x, mMin.y, mMin.z);
  1022. }
  1023. }
  1024. public Vector3d Max
  1025. {
  1026. get
  1027. {
  1028. return new Vector3d(mMin.x + mDiag.x, mMin.y + mDiag.y, mMin.z + mDiag.z);
  1029. }
  1030. }
  1031. public Vector3d Diag
  1032. {
  1033. get
  1034. {
  1035. return new Vector3d(mDiag.x, mDiag.y, mDiag.z);
  1036. }
  1037. }
  1038. public override string ToString()
  1039. {
  1040. return "[" + this.Min.ToString() + " - " + this.Max.ToString() + "]";
  1041. }
  1042. public BoundingBox3d3f Merge(BoundingBox3d3f other)
  1043. {
  1044. Vector3d thisMax = Max;
  1045. Vector3d otherMax = other.Max;
  1046. bool xless = other.mMin.x > mMin.x;
  1047. bool yless = other.mMin.y > mMin.y;
  1048. bool zless = other.mMin.z > mMin.z;
  1049. bool xmore = otherMax.x < thisMax.x;
  1050. bool ymore = otherMax.y < thisMax.y;
  1051. bool zmore = otherMax.z < thisMax.z;
  1052. return new BoundingBox3d3f(xless ? mMin.x : other.mMin.x,
  1053. yless ? mMin.y : other.mMin.y,
  1054. zless ? mMin.z : other.mMin.z,
  1055. (float)(xmore ? (xless ? mDiag.x : otherMax.x - mMin.x) : (xless ? thisMax.x - other.mMin.x : other.mDiag.x)),
  1056. (float)(ymore ? (yless ? mDiag.y : otherMax.y - mMin.y) : (yless ? thisMax.y - other.mMin.y : other.mDiag.y)),
  1057. (float)(zmore ? (zless ? mDiag.z : otherMax.z - mMin.z) : (zless ? thisMax.z - other.mMin.z : other.mDiag.z)));
  1058. }
  1059. } // struct BoundingBox
  1060. public struct BoundingSphere3f
  1061. {
  1062. Vector3f mCenter;
  1063. float mRadius;
  1064. public BoundingSphere3f(float x, float y, float z, float r)
  1065. {
  1066. mCenter = new Vector3f(x, y, z);
  1067. mRadius = r;
  1068. }
  1069. public BoundingSphere3f(Vector3f center, float radius)
  1070. {
  1071. mCenter = center;
  1072. mRadius = radius;
  1073. }
  1074. public BoundingSphere3f(BoundingSphere3f cpy, float scale)
  1075. {
  1076. mCenter = cpy.mCenter;
  1077. mRadius = cpy.mRadius * scale;
  1078. }
  1079. public Vector3f Center
  1080. {
  1081. get
  1082. {
  1083. return new Vector3f(mCenter.x, mCenter.y, mCenter.z);
  1084. }
  1085. }
  1086. public float Radius
  1087. {
  1088. get
  1089. {
  1090. return mRadius;
  1091. }
  1092. }
  1093. public override string ToString()
  1094. {
  1095. return "[" + this.Center.ToString() + " : " + this.Radius.ToString() + "]";
  1096. }
  1097. } // struct BoundingSphere3f
  1098. public struct BoundingSphere3d
  1099. {
  1100. Vector3d mCenter;
  1101. float mRadius;
  1102. public BoundingSphere3d(double x, double y, double z, float r)
  1103. {
  1104. mCenter.x = x;
  1105. mCenter.y = y;
  1106. mCenter.z = z;
  1107. mRadius = r;
  1108. }
  1109. public BoundingSphere3d(Vector3d center, float radius)
  1110. {
  1111. mCenter = center;
  1112. mRadius = radius;
  1113. }
  1114. public BoundingSphere3d(BoundingSphere3d cpy, float scale)
  1115. {
  1116. mCenter = cpy.mCenter;
  1117. mRadius = cpy.mRadius * scale;
  1118. }
  1119. public Vector3d Center
  1120. {
  1121. get
  1122. {
  1123. return new Vector3d(mCenter.x, mCenter.y, mCenter.z);
  1124. }
  1125. }
  1126. public float Radius
  1127. {
  1128. get
  1129. {
  1130. return mRadius;
  1131. }
  1132. }
  1133. public override string ToString()
  1134. {
  1135. return "[" + this.Center.ToString() + " : " + this.Radius.ToString() + "]";
  1136. }
  1137. } // struct BoundingSphere3f
  1138. public struct UUID
  1139. {
  1140. ulong mLowOrderBytes;
  1141. ulong mHighOrderBytes;
  1142. static ulong SetUUIDlow(Google.ProtocolBuffers.ByteString data, int offset)
  1143. {
  1144. ulong LowOrderBytes = 0;
  1145. int shiftVal = 0;
  1146. for (int i = 0; i < 8; ++i)
  1147. {
  1148. ulong temp = data[i];
  1149. LowOrderBytes |= (temp << shiftVal);
  1150. shiftVal += 8;
  1151. }
  1152. return LowOrderBytes;
  1153. }
  1154. static ulong SetUUIDhigh(Google.ProtocolBuffers.ByteString data)
  1155. {
  1156. return SetUUIDlow(data, 8);
  1157. }
  1158. static ulong SetUUIDlow(byte[] data, int offset)
  1159. {
  1160. ulong LowOrderBytes = 0;
  1161. int shiftVal = 0;
  1162. for (int i = 0; i < 8; ++i)
  1163. {
  1164. ulong temp = data[i];
  1165. LowOrderBytes |= (temp << shiftVal);
  1166. shiftVal += 8;
  1167. }
  1168. return LowOrderBytes;
  1169. }
  1170. static ulong SetUUIDhigh(byte[] data)
  1171. {
  1172. return SetUUIDlow(data, 8);
  1173. }
  1174. public bool SetUUID(byte[] data)
  1175. {
  1176. if (data.Length == 16)
  1177. {
  1178. mLowOrderBytes = 0;
  1179. mHighOrderBytes = 0;
  1180. mLowOrderBytes = SetUUIDlow(data, 0);
  1181. mHighOrderBytes = SetUUIDlow(data, 8);
  1182. return true;
  1183. }
  1184. else
  1185. {
  1186. return false;
  1187. }
  1188. }
  1189. public byte[] GetUUID()
  1190. {
  1191. byte[] data = new byte[16];
  1192. int shiftVal = 0;
  1193. for (int i = 0; i < 8; ++i)
  1194. {
  1195. ulong temp = 0xff;
  1196. temp = (mLowOrderBytes & (temp << shiftVal));
  1197. temp = (temp >> shiftVal);
  1198. data[i] = (byte)temp;
  1199. shiftVal += 8;
  1200. }
  1201. shiftVal = 0;
  1202. for (int i = 8; i < 16; ++i)
  1203. {
  1204. ulong temp = 0xff;
  1205. temp = (mHighOrderBytes & (temp << shiftVal));
  1206. temp = (temp >> shiftVal);
  1207. data[i] = (byte)temp;
  1208. shiftVal += 8;
  1209. }
  1210. return data;
  1211. }
  1212. public static UUID Empty = new UUID(new byte[16]);
  1213. public UUID(byte[] data)
  1214. {
  1215. if (data.Length != 16)
  1216. {
  1217. throw new System.ArgumentException("UUIDs must be provided 16 bytes");
  1218. }
  1219. mLowOrderBytes = SetUUIDlow(data, 0);
  1220. mHighOrderBytes = SetUUIDhigh(data);
  1221. }
  1222. public UUID(Google.ProtocolBuffers.ByteString data)
  1223. {
  1224. if (data.Length != 16)
  1225. {
  1226. throw new System.ArgumentException("UUIDs must be provided 16 bytes");
  1227. }
  1228. mLowOrderBytes = SetUUIDlow(data, 0);
  1229. mHighOrderBytes = SetUUIDhigh(data);
  1230. }
  1231. }
  1232. public struct SHA256
  1233. {
  1234. ulong mLowOrderBytes;
  1235. ulong mLowMediumOrderBytes;
  1236. ulong mMediumHighOrderBytes;
  1237. ulong mHighOrderBytes;
  1238. static ulong SetLMH(Google.ProtocolBuffers.ByteString data, int offset)
  1239. {
  1240. ulong LowOrderBytes = 0;
  1241. int shiftVal = 0;
  1242. for (int i = 0; i < 8; ++i)
  1243. {
  1244. ulong temp = data[i];
  1245. LowOrderBytes |= (temp << shiftVal);
  1246. shiftVal += 8;
  1247. }
  1248. return LowOrderBytes;
  1249. }
  1250. static ulong SetLow(Google.ProtocolBuffers.ByteString data)
  1251. {
  1252. return SetLMH(data, 0);
  1253. }
  1254. static ulong SetLowMedium(Google.ProtocolBuffers.ByteString data)
  1255. {
  1256. return SetLMH(data, 8);
  1257. }
  1258. static ulong SetMediumHigh(Google.ProtocolBuffers.ByteString data)
  1259. {
  1260. return SetLMH(data, 16);
  1261. }
  1262. static ulong SetHigh(Google.ProtocolBuffers.ByteString data)
  1263. {
  1264. return SetLMH(data, 24);
  1265. }
  1266. static ulong SetLMH(byte[] data, int offset)
  1267. {
  1268. ulong LowOrderBytes = 0;
  1269. int shiftVal = 0;
  1270. for (int i = 0; i < 8; ++i)
  1271. {
  1272. ulong temp = data[i];
  1273. LowOrderBytes |= (temp << shiftVal);
  1274. shiftVal += 8;
  1275. }
  1276. return LowOrderBytes;
  1277. }
  1278. static ulong SetLow(byte[] data)
  1279. {
  1280. return SetLMH(data, 0);
  1281. }
  1282. static ulong SetLowMedium(byte[] data)
  1283. {
  1284. return SetLMH(data, 8);
  1285. }
  1286. static ulong SetMediumHigh(byte[] data)
  1287. {
  1288. return SetLMH(data, 16);
  1289. }
  1290. static ulong SetHigh(byte[] data)
  1291. {
  1292. return SetLMH(data, 24);
  1293. }
  1294. public bool SetSHA256(byte[] data)
  1295. {
  1296. if (data.Length == 32)
  1297. {
  1298. mLowOrderBytes = SetLow(data);
  1299. mLowMediumOrderBytes = SetLowMedium(data);
  1300. mMediumHighOrderBytes = SetMediumHigh(data);
  1301. mHighOrderBytes = SetHigh(data);
  1302. return true;
  1303. }
  1304. else
  1305. {
  1306. return false;
  1307. }
  1308. }
  1309. public byte[] GetBinaryData()
  1310. {
  1311. byte[] data = new byte[32];
  1312. int shiftVal = 0;
  1313. for (int i = 0; i < 8; ++i)
  1314. {
  1315. ulong temp = 0xff;
  1316. temp = (mLowOrderBytes & (temp << shiftVal));
  1317. temp = (temp >> shiftVal);
  1318. data[i] = (byte)temp;
  1319. shiftVal += 8;
  1320. }
  1321. shiftVal = 0;
  1322. for (int i = 8; i < 16; ++i)
  1323. {
  1324. ulong temp = 0xff;
  1325. temp = (mLowMediumOrderBytes & (temp << shiftVal));
  1326. temp = (temp >> shiftVal);
  1327. data[i] = (byte)temp;
  1328. shiftVal += 8;
  1329. }
  1330. shiftVal = 0;
  1331. for (int i = 16; i < 24; ++i)
  1332. {
  1333. ulong temp = 0xff;
  1334. temp = (mMediumHighOrderBytes & (temp << shiftVal));
  1335. temp = (temp >> shiftVal);
  1336. data[i] = (byte)temp;
  1337. shiftVal += 8;
  1338. }
  1339. shiftVal = 0;
  1340. for (int i = 24; i < 32; ++i)
  1341. {
  1342. ulong temp = 0xff;
  1343. temp = (mHighOrderBytes & (temp << shiftVal));
  1344. temp = (temp >> shiftVal);
  1345. data[i] = (byte)temp;
  1346. shiftVal += 8;
  1347. }
  1348. return data;
  1349. }
  1350. public static SHA256 Empty = new SHA256(new byte[32]);
  1351. public SHA256(byte[] data)
  1352. {
  1353. if (data.Length != 32)
  1354. {
  1355. throw new System.ArgumentException("SHA256s must be provided 32 bytes");
  1356. }
  1357. mLowOrderBytes = SetLow(data);
  1358. mLowMediumOrderBytes = SetLowMedium(data);
  1359. mMediumHighOrderBytes = SetMediumHigh(data);
  1360. mHighOrderBytes = SetHigh(data);
  1361. }
  1362. public SHA256(Google.ProtocolBuffers.ByteString data)
  1363. {
  1364. if (data.Length != 32)
  1365. {
  1366. throw new System.ArgumentException("SHA256s must be provided 32 bytes");
  1367. }
  1368. mLowOrderBytes = SetLow(data);
  1369. mLowMediumOrderBytes = SetLowMedium(data);
  1370. mMediumHighOrderBytes = SetMediumHigh(data);
  1371. mHighOrderBytes = SetHigh(data);
  1372. }
  1373. }
  1374. public struct Time
  1375. {
  1376. ulong usec;
  1377. public Time(ulong usec_since_epoch)
  1378. {
  1379. usec = usec_since_epoch;
  1380. }
  1381. public ulong toMicro()
  1382. {
  1383. return usec;
  1384. }
  1385. }
  1386. public class Duration
  1387. {
  1388. long usec;
  1389. public Duration(long time_since)
  1390. {
  1391. usec = time_since;
  1392. }
  1393. public long toMicro()
  1394. {
  1395. return usec;
  1396. }
  1397. }
  1398. class _PBJ
  1399. {
  1400. public static bool ValidateBool(bool d)
  1401. {
  1402. return true;
  1403. }
  1404. public static bool ValidateDouble(double d)
  1405. {
  1406. return true;
  1407. }
  1408. public static bool ValidateFloat(float d)
  1409. {
  1410. return true;
  1411. }
  1412. public static bool ValidateUint64(ulong d)
  1413. {
  1414. return true;
  1415. }
  1416. public static bool ValidateUint32(uint d)
  1417. {
  1418. return true;
  1419. }
  1420. public static bool ValidateUint16(ushort d)
  1421. {
  1422. return true;
  1423. }
  1424. public static bool ValidateUint8(byte d)
  1425. {
  1426. return true;
  1427. }
  1428. public static bool ValidateInt64(long d)
  1429. {
  1430. return true;
  1431. }
  1432. public static bool ValidateInt32(int d)
  1433. {
  1434. return true;
  1435. }
  1436. public static bool ValidateInt16(short d)
  1437. {
  1438. return true;
  1439. }
  1440. public static bool ValidateInt8(sbyte d)
  1441. {
  1442. return true;
  1443. }
  1444. public static bool ValidateString<S>(S input)
  1445. {
  1446. return true;
  1447. }
  1448. public static bool ValidateBytes<B>(B input)
  1449. {
  1450. return true;
  1451. }
  1452. public static bool ValidateUuid(Google.ProtocolBuffers.ByteString input)
  1453. {
  1454. return input.Length == 16;
  1455. }
  1456. public static bool ValidateSha256(Google.ProtocolBuffers.ByteString input)
  1457. {
  1458. return input.Length == 32;
  1459. }
  1460. public static bool ValidateAngle(float input)
  1461. {
  1462. return input >= 0 && input <= 3.1415926536 * 2.0;
  1463. }
  1464. public static bool ValidateTime(ulong input)
  1465. {
  1466. return true;
  1467. }
  1468. public static bool ValidateDuration(long input)
  1469. {
  1470. return true;
  1471. }
  1472. public static bool ValidateFlags(ulong input, ulong verification)
  1473. {
  1474. return (input & verification) == input;
  1475. }
  1476. public static bool CastBool(bool d)
  1477. {
  1478. return d;
  1479. }
  1480. public static double CastDouble(double d)
  1481. {
  1482. return d;
  1483. }
  1484. public static float CastFloat(float d)
  1485. {
  1486. return d;
  1487. }
  1488. public static ulong CastUint64(ulong d)
  1489. {
  1490. return d;
  1491. }
  1492. public static uint CastUint32(uint d)
  1493. {
  1494. return d;
  1495. }
  1496. public static ushort CastUint16(ushort d)
  1497. {
  1498. return d;
  1499. }
  1500. public static byte CastUint8(byte d)
  1501. {
  1502. return d;
  1503. }
  1504. public static long CastInt64(long d)
  1505. {
  1506. return d;
  1507. }
  1508. public static int CastInt32(int d)
  1509. {
  1510. return d;
  1511. }
  1512. public static short CastInt16(short d)
  1513. {
  1514. return d;
  1515. }
  1516. public static sbyte CastInt8(sbyte d)
  1517. {
  1518. return d;
  1519. }
  1520. public static S CastString<S>(S input)
  1521. {
  1522. return input;
  1523. }
  1524. public static B CastBytes<B>(B input)
  1525. {
  1526. return input;
  1527. }
  1528. public static bool CastBool()
  1529. {
  1530. return false;
  1531. }
  1532. public static double CastDouble()
  1533. {
  1534. return 0;
  1535. }
  1536. public static float CastFloat()
  1537. {
  1538. return 0;
  1539. }
  1540. public static ulong CastUint64()
  1541. {
  1542. return 0;
  1543. }
  1544. public static uint CastUint32()
  1545. {
  1546. return 0;
  1547. }
  1548. public static ushort CastUint16()
  1549. {
  1550. return 0;
  1551. }
  1552. public static byte CastUint8()
  1553. {
  1554. return 0;
  1555. }
  1556. public static long CastInt64()
  1557. {
  1558. return 0;
  1559. }
  1560. public static int CastInt32()
  1561. {
  1562. return 0;
  1563. }
  1564. public static short CastInt16()
  1565. {
  1566. return 0;
  1567. }
  1568. public static sbyte CastInt8()
  1569. {
  1570. return 0;
  1571. }
  1572. public static string CastString()
  1573. {
  1574. return "";
  1575. }
  1576. public static Google.ProtocolBuffers.ByteString CastBytes()
  1577. {
  1578. return Google.ProtocolBuffers.ByteString.Empty;
  1579. }
  1580. public static ulong CastFlags(ulong data, ulong allFlagsOn)
  1581. {
  1582. return allFlagsOn & data;
  1583. }
  1584. public static ulong CastFlags(ulong allFlagsOn)
  1585. {
  1586. return 0;
  1587. }
  1588. public static Vector3f CastNormal(float x, float y)
  1589. {
  1590. float neg = (x > 1.5f || y > 1.5f) ? -1.0f : 1.0f;
  1591. if (x > 1.5)
  1592. x -= 3;
  1593. if (y > 1.5)
  1594. y -= 3;
  1595. return new Vector3f(x, y, neg - neg * (float)Math.Sqrt(x * x + y * y));
  1596. }
  1597. public static Vector3f CastNormal()
  1598. {
  1599. return new Vector3f(0, 1, 0);
  1600. }
  1601. public static Vector2f CastVector2f(float x, float y)
  1602. {
  1603. return new Vector2f(x, y);
  1604. }
  1605. public static Vector2f CastVector2f()
  1606. {
  1607. return new Vector2f(0, 0);
  1608. }
  1609. public static Vector3f CastVector3f(float x, float y, float z)
  1610. {
  1611. return new Vector3f(x, y, z);
  1612. }
  1613. public static Vector3f CastVector3f()
  1614. {
  1615. return new Vector3f(0, 0, 0);
  1616. }
  1617. public static Vector4f CastVector4f(float x, float y, float z, float w)
  1618. {
  1619. return new Vector4f(x, y, z, w);
  1620. }
  1621. public static Vector4f CastVector4f()
  1622. {
  1623. return new Vector4f(0, 0, 0, 0);
  1624. }
  1625. public static Vector2d CastVector2d(double x, double y)
  1626. {
  1627. return new Vector2d(x, y);
  1628. }
  1629. public static Vector2d CastVector2d()
  1630. {
  1631. return new Vector2d(0, 0);
  1632. }
  1633. public static Vector3d CastVector3d(double x, double y, double z)
  1634. {
  1635. return new Vector3d(x, y, z);
  1636. }
  1637. public static Vector3d CastVector3d()
  1638. {
  1639. return new Vector3d(0, 0, 0);
  1640. }
  1641. public static Vector4d CastVector4d(double x, double y, double z, double w)
  1642. {
  1643. return new Vector4d(x, y, z, w);
  1644. }
  1645. public static Vector4d CastVector4d()
  1646. {
  1647. return new Vector4d(0, 0, 0, 0);
  1648. }
  1649. public static BoundingSphere3f CastBoundingsphere3f(float x, float y, float z, float r)
  1650. {
  1651. return new BoundingSphere3f(new Vector3f(x, y, z), r);
  1652. }
  1653. public static BoundingSphere3d CastBoundingsphere3d(double x, double y, double z, double r)
  1654. {
  1655. return new BoundingSphere3d(new Vector3d(x, y, z), (float)r);
  1656. }
  1657. public static BoundingSphere3f CastBoundingsphere3f()
  1658. {
  1659. return new BoundingSphere3f(new Vector3f(0, 0, 0), 0);
  1660. }
  1661. public static BoundingSphere3d CastBoundingsphere3d()
  1662. {
  1663. return new BoundingSphere3d(new Vector3d(0, 0, 0), (float)0);
  1664. }
  1665. public static BoundingBox3f3f CastBoundingbox3f3f(float x, float y, float z, float dx, float dy, float dz)
  1666. {
  1667. return new BoundingBox3f3f(x, y, z, dx, dy, dz);
  1668. }
  1669. public static BoundingBox3d3f CastBoundingbox3d3f(double x, double y, double z, double dx, double dy, double dz)
  1670. {
  1671. return new BoundingBox3d3f(x, y, z, (float)dx, (float)dy, (float)dz);
  1672. }
  1673. public static BoundingBox3f3f CastBoundingbox3f3f()
  1674. {
  1675. return new BoundingBox3f3f(new Vector3f(0, 0, 0), new Vector3f(0, 0, 0));
  1676. }
  1677. public static BoundingBox3d3f CastBoundingbox3d3f()
  1678. {
  1679. return new BoundingBox3d3f(0, 0, 0, 0, 0, 0);
  1680. }
  1681. public static Quaternion CastQuaternion(float x, float y, float z)
  1682. {
  1683. float neg = (x > 1.5 || y > 1.5 || z > 1.5) ? -1.0f : 1.0f;
  1684. if (x > 1.5)
  1685. x -= 3.0f;
  1686. if (y > 1.5)
  1687. y -= 3.0f;
  1688. if (z > 1.5)
  1689. z -= 3.0f;
  1690. return new Quaternion(neg - neg * (float)Math.Sqrt(x * x + y * y + z * z), x, y, z);
  1691. }
  1692. public static Quaternion CastQuaternion()
  1693. {
  1694. return new Quaternion(1, 0, 0, 0);
  1695. }
  1696. public static UUID CastUuid(Google.ProtocolBuffers.ByteString input)
  1697. {
  1698. return new UUID(input);
  1699. }
  1700. public static SHA256 CastSha256(Google.ProtocolBuffers.ByteString input)
  1701. {
  1702. return new SHA256(input);
  1703. }
  1704. public static SHA256 CastSha256()
  1705. {
  1706. return SHA256.Empty;
  1707. }
  1708. public static UUID CastUuid()
  1709. {
  1710. return UUID.Empty;
  1711. }
  1712. public static float CastAngle(float d)
  1713. {
  1714. return d;
  1715. }
  1716. public static float CastAngle()
  1717. {
  1718. return 0;
  1719. }
  1720. public static Time CastTime(ulong t)
  1721. {
  1722. return new Time(t);
  1723. }
  1724. public static Time CastTime()
  1725. {
  1726. return new Time(0);
  1727. }
  1728. public static Duration CastDuration(long t)
  1729. {
  1730. return new Duration(t);
  1731. }
  1732. public static Duration CastDuration()
  1733. {
  1734. return new Duration(0);
  1735. }
  1736. public static T Construct<T>(T retval)
  1737. {
  1738. return retval;
  1739. }
  1740. public static long Construct(Duration d)
  1741. {
  1742. return d.toMicro();
  1743. }
  1744. public static ulong Construct(Time t)
  1745. {
  1746. return t.toMicro();
  1747. }
  1748. public static Google.ProtocolBuffers.ByteString Construct(UUID u)
  1749. {
  1750. byte[] data = u.GetUUID();
  1751. Google.ProtocolBuffers.ByteString retval = Google.ProtocolBuffers.ByteString.CopyFrom(data, 0, 16);
  1752. return retval;
  1753. }
  1754. public static Google.ProtocolBuffers.ByteString Construct(SHA256 u)
  1755. {
  1756. byte[] data = u.GetBinaryData();
  1757. Google.ProtocolBuffers.ByteString retval = Google.ProtocolBuffers.ByteString.CopyFrom(data, 0, 16);
  1758. return retval;
  1759. }
  1760. public static float[] ConstructNormal(Vector3f d)
  1761. {
  1762. return new float[] { d.x + (d.z < 0 ? 3.0f : 0.0f), d.y };
  1763. }
  1764. public static float[] ConstructQuaternion(Quaternion d)
  1765. {
  1766. return new float[] { d.x + (d.w < 0 ? 3.0f : 0.0f), d.y, d.z };
  1767. }
  1768. public static float[] ConstructVector2f(Vector2f d)
  1769. {
  1770. return new float[] { d.x, d.y };
  1771. }
  1772. public static double[] ConstructVector2d(Vector2d d)
  1773. {
  1774. return new double[] { d.x, d.y };
  1775. }
  1776. public static float[] ConstructVector3f(Vector3f d)
  1777. {
  1778. return new float[] { d.x, d.y, d.z };
  1779. }
  1780. public static double[] ConstructVector3d(Vector3d d)
  1781. {
  1782. return new double[] { d.x, d.y, d.z };
  1783. }
  1784. public static float[] ConstructVector4f(Vector4f d)
  1785. {
  1786. return new float[] { d.x, d.y, d.z, d.w };
  1787. }
  1788. public static double[] ConstructVector4d(Vector4d d)
  1789. {
  1790. return new double[] { d.x, d.y, d.z, d.w };
  1791. }
  1792. public static float[] ConstructBoundingsphere3f(BoundingSphere3f d)
  1793. {
  1794. return new float[] { d.Center.x, d.Center.y, d.Center.z, d.Radius };
  1795. }
  1796. public static double[] ConstructBoundingsphere3d(BoundingSphere3d d)
  1797. {
  1798. return new double[] { d.Center.x, d.Center.y, d.Center.z, d.Radius };
  1799. }
  1800. public static float[] ConstructBoundingbox3f3f(BoundingBox3f3f d)
  1801. {
  1802. return new float[] { d.Min.x, d.Min.y, d.Min.z, d.Diag.x, d.Diag.y, d.Diag.z };
  1803. }
  1804. public static double[] ConstructBoundingbox3d3f(BoundingBox3d3f d)
  1805. {
  1806. return new double[] { d.Min.x, d.Min.y, d.Min.z, d.Diag.x, d.Diag.y, d.Diag.z };
  1807. }
  1808. }
  1809. }