o3dgc.js 128 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913
  1. /*global ArrayBuffer, Uint32Array, Int32Array, Float32Array, Int8Array, Uint8Array, window, performance, Console*/
  2. /*
  3. Copyright (c) 2013 Khaled Mammou - Advanced Micro Devices, Inc.
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. var o3dgc = (function () {
  21. "use strict";
  22. var module, local;
  23. module = {};
  24. local = {};
  25. local.O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0 = 7;
  26. local.O3DGC_BINARY_STREAM_MAX_SYMBOL0 = 127; // ((1 << O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0) >>> 0) - 1;
  27. local.O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1 = 6;
  28. local.O3DGC_BINARY_STREAM_MAX_SYMBOL1 = 63; // ((1 << O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1) >>> 0) - 1;
  29. local.O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32 = 5; // Math.floor((32 + O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0 - 1) / O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0);
  30. local.O3DGC_BIG_ENDIAN = 0;
  31. local.O3DGC_LITTLE_ENDIAN = 1;
  32. local.O3DGC_MAX_DOUBLE = 1.79769e+308;
  33. local.O3DGC_MIN_LONG = -2147483647;
  34. local.O3DGC_MAX_LONG = 2147483647;
  35. local.O3DGC_MAX_UCHAR8 = 255;
  36. local.O3DGC_MAX_TFAN_SIZE = 256;
  37. local.O3DGC_MAX_ULONG = 4294967295;
  38. local.O3DGC_SC3DMC_START_CODE = 0x00001F1;
  39. local.O3DGC_DV_START_CODE = 0x00001F2;
  40. local.O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES = 256;
  41. local.O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES = 256;
  42. local.O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES = 32;
  43. local.O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS = 2;
  44. local.O3DGC_SC3DMC_BINARIZATION_FL = 0; // Fixed Length (not supported)
  45. local.O3DGC_SC3DMC_BINARIZATION_BP = 1; // BPC (not supported)
  46. local.O3DGC_SC3DMC_BINARIZATION_FC = 2; // 4 bits Coding (not supported)
  47. local.O3DGC_SC3DMC_BINARIZATION_AC = 3; // Arithmetic Coding (not supported)
  48. local.O3DGC_SC3DMC_BINARIZATION_AC_EGC = 4; // Arithmetic Coding & EGCk
  49. local.O3DGC_SC3DMC_BINARIZATION_ASCII = 5; // Arithmetic Coding & EGCk
  50. local.O3DGC_STREAM_TYPE_UNKOWN = 0;
  51. local.O3DGC_STREAM_TYPE_ASCII = 1;
  52. local.O3DGC_STREAM_TYPE_BINARY = 2;
  53. local.O3DGC_SC3DMC_NO_PREDICTION = 0; // supported
  54. local.O3DGC_SC3DMC_DIFFERENTIAL_PREDICTION = 1; // supported
  55. local.O3DGC_SC3DMC_XOR_PREDICTION = 2; // not supported
  56. local.O3DGC_SC3DMC_ADAPTIVE_DIFFERENTIAL_PREDICTION = 3; // not supported
  57. local.O3DGC_SC3DMC_CIRCULAR_DIFFERENTIAL_PREDICTION = 4; // not supported
  58. local.O3DGC_SC3DMC_PARALLELOGRAM_PREDICTION = 5; // supported
  59. local.O3DGC_SC3DMC_SURF_NORMALS_PREDICTION = 6; // supported
  60. local.O3DGC_SC3DMC_ENCODE_MODE_QBCR = 0; // not supported
  61. local.O3DGC_SC3DMC_ENCODE_MODE_SVA = 1; // not supported
  62. local.O3DGC_SC3DMC_ENCODE_MODE_TFAN = 2; // supported
  63. local.O3DGC_DYNAMIC_VECTOR_ENCODE_MODE_LIFT = 0;
  64. local.O3DGC_MIN_NEIGHBORS_SIZE = 128;
  65. local.O3DGC_MIN_NUM_NEIGHBORS_SIZE = 16;
  66. local.O3DGC_TFANS_MIN_SIZE_ALLOCATED_VERTICES_BUFFER = 128;
  67. local.O3DGC_TFANS_MIN_SIZE_TFAN_SIZE_BUFFER = 8;
  68. local.O3DGC_DEFAULT_VECTOR_SIZE = 32;
  69. module.O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_UNKOWN = 0;
  70. module.O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_POSITION = 1;
  71. module.O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_NORMAL = 2;
  72. module.O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_COLOR = 3;
  73. module.O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_TEXCOORD = 4;
  74. module.O3DGC_IFS_FLOAT_ATTRIBUTE_TYPE_WEIGHT = 5;
  75. module.O3DGC_IFS_INT_ATTRIBUTE_TYPE_UNKOWN = 0;
  76. module.O3DGC_IFS_INT_ATTRIBUTE_TYPE_INDEX = 1;
  77. module.O3DGC_IFS_INT_ATTRIBUTE_TYPE_JOINT_ID = 2;
  78. module.O3DGC_IFS_INT_ATTRIBUTE_TYPE_INDEX_BUFFER_ID = 3;
  79. module.O3DGC_OK = 0;
  80. module.O3DGC_ERROR_BUFFER_FULL = 1;
  81. module.O3DGC_ERROR_CORRUPTED_STREAM = 5;
  82. module.O3DGC_ERROR_NON_SUPPORTED_FEATURE = 6;
  83. module.O3DGC_ERROR_AC = 7;
  84. function SystemEndianness() {
  85. var a, b, c;
  86. b = new ArrayBuffer(4);
  87. a = new Uint32Array(b);
  88. c = new Uint8Array(b);
  89. a[0] = 1;
  90. if (c[0] === 1) {
  91. return local.O3DGC_LITTLE_ENDIAN;
  92. }
  93. return local.O3DGC_BIG_ENDIAN;
  94. }
  95. // SC3DMCStats class
  96. module.SC3DMCStats = function () {
  97. this.m_timeCoord = 0;
  98. this.m_timeNormal = 0;
  99. this.m_timeCoordIndex = 0;
  100. this.m_timeFloatAttribute = new Float32Array(local.O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES);
  101. this.m_timeIntAttribute = new Float32Array(local.O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES);
  102. this.m_timeReorder = 0;
  103. this.m_streamSizeCoord = 0;
  104. this.m_streamSizeNormal = 0;
  105. this.m_streamSizeCoordIndex = 0;
  106. this.m_streamSizeFloatAttribute = new Uint32Array(local.O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES);
  107. this.m_streamSizeIntAttribute = new Uint32Array(local.O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES);
  108. };
  109. // SC3DMCTriplet class
  110. module.SC3DMCTriplet = function (a, b, c) {
  111. this.m_a = a;
  112. this.m_b = b;
  113. this.m_c = c;
  114. };
  115. module.SC3DMCTriplet.prototype.Less = function (rhs) {
  116. var res;
  117. if (this.m_c !== rhs.m_c) {
  118. res = (this.m_c < rhs.m_c);
  119. } else if (this.m_b !== rhs.m_b) {
  120. res = (this.m_b < rhs.m_b);
  121. } else {
  122. res = (this.m_a < rhs.m_a);
  123. }
  124. return res;
  125. };
  126. module.SC3DMCTriplet.prototype.Equal = function (rhs) {
  127. return (this.m_c === rhs.m_c && this.m_b === rhs.m_b && this.m_a === rhs.m_a);
  128. };
  129. // SC3DMCPredictor class
  130. module.SC3DMCPredictor = function () {
  131. this.m_id = new module.SC3DMCTriplet(-1, -1, -1);
  132. this.m_pred = new Float32Array(local.O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES);
  133. };
  134. // fix me: optimize this function (e.g., binary search)
  135. function InsertPredictor(e, nPred, list, dimFloatArray) {
  136. var pos, foundOrInserted, j, j1, j0, h, i;
  137. pos = -1;
  138. foundOrInserted = false;
  139. j1 = nPred.m_value;
  140. j0 = 0;
  141. for (j = j0; j < j1; ++j) {
  142. if (e.Equal(list[j].m_id)) {
  143. foundOrInserted = true;
  144. break;
  145. } else if (e.Less(list[j].m_id)) {
  146. if (nPred.m_value < local.O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS) {
  147. ++nPred.m_value;
  148. }
  149. for (h = nPred.m_value - 1; h > j; --h) {
  150. list[h].m_id.m_a = list[h - 1].m_id.m_a;
  151. list[h].m_id.m_b = list[h - 1].m_id.m_b;
  152. list[h].m_id.m_c = list[h - 1].m_id.m_c;
  153. for (i = 0; i < dimFloatArray; ++i) {
  154. list[h].m_pred[i] = list[h - 1].m_pred[i];
  155. }
  156. }
  157. list[j].m_id.m_a = e.m_a;
  158. list[j].m_id.m_b = e.m_b;
  159. list[j].m_id.m_c = e.m_c;
  160. pos = j;
  161. foundOrInserted = true;
  162. break;
  163. }
  164. }
  165. if (!foundOrInserted && nPred.m_value < local.O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS) {
  166. pos = nPred.m_value++;
  167. list[pos].m_id.m_a = e.m_a;
  168. list[pos].m_id.m_b = e.m_b;
  169. list[pos].m_id.m_c = e.m_c;
  170. }
  171. return pos;
  172. }
  173. // Timer class
  174. if (typeof window.performance === 'undefined') {
  175. window.performance = {};
  176. }
  177. if (!window.performance.now) {
  178. local.nowOffset = Date.now();
  179. if (performance.timing && performance.timing.navigationStart) {
  180. local.nowOffset = performance.timing.navigationStart;
  181. }
  182. window.performance.now = function now() {
  183. return Date.now() - local.nowOffset;
  184. };
  185. }
  186. module.Timer = function () {
  187. this.m_start = 0;
  188. this.m_end = 0;
  189. };
  190. module.Timer.prototype.Tic = function () {
  191. this.m_start = window.performance.now();
  192. };
  193. module.Timer.prototype.Toc = function () {
  194. this.m_end = window.performance.now();
  195. };
  196. module.Timer.prototype.GetElapsedTime = function () {
  197. return this.m_end - this.m_start;
  198. };
  199. // Vec3 class
  200. module.Vec3 = function (x, y, z) {
  201. this.m_x = x;
  202. this.m_y = y;
  203. this.m_z = z;
  204. };
  205. module.Vec3.prototype.Set = function (x, y, z) {
  206. this.m_x = x;
  207. this.m_y = y;
  208. this.m_z = z;
  209. };
  210. module.Vec3.prototype.Sub = function (lhs, rhs) {
  211. this.m_x = lhs.m_x - rhs.m_x;
  212. this.m_y = lhs.m_y - rhs.m_y;
  213. this.m_z = lhs.m_z - rhs.m_z;
  214. };
  215. module.Vec3.prototype.Add = function (lhs, rhs) {
  216. this.m_x = lhs.m_x + rhs.m_x;
  217. this.m_y = lhs.m_y + rhs.m_y;
  218. this.m_z = lhs.m_z + rhs.m_z;
  219. };
  220. module.Vec3.prototype.SelfAdd = function (v) {
  221. this.m_x += v.m_x;
  222. this.m_y += v.m_y;
  223. this.m_z += v.m_z;
  224. };
  225. module.Vec3.prototype.Cross = function (lhs, rhs) {
  226. this.m_x = lhs.m_y * rhs.m_z - lhs.m_z * rhs.m_y;
  227. this.m_y = lhs.m_z * rhs.m_x - lhs.m_x * rhs.m_z;
  228. this.m_z = lhs.m_x * rhs.m_y - lhs.m_y * rhs.m_x;
  229. };
  230. module.Vec3.prototype.GetNorm = function () {
  231. return Math.sqrt(this.m_x * this.m_x + this.m_y * this.m_y + this.m_z * this.m_z);
  232. };
  233. function SphereToCube(vin, vout) {
  234. var ax, ay, az;
  235. ax = Math.abs(vin.m_x);
  236. ay = Math.abs(vin.m_y);
  237. az = Math.abs(vin.m_z);
  238. if (az >= ax && az >= ay) {
  239. if (vin.m_z >= 0) {
  240. vout.m_z = 0;
  241. vout.m_x = vin.m_x;
  242. vout.m_y = vin.m_y;
  243. } else {
  244. vout.m_z = 1;
  245. vout.m_x = -vin.m_x;
  246. vout.m_y = -vin.m_y;
  247. }
  248. } else if (ay >= ax && ay >= az) {
  249. if (vin.m_y >= 0) {
  250. vout.m_z = 2;
  251. vout.m_x = vin.m_z;
  252. vout.m_y = vin.m_x;
  253. } else {
  254. vout.m_z = 3;
  255. vout.m_x = -vin.m_z;
  256. vout.m_y = -vin.m_x;
  257. }
  258. } else {
  259. if (vin.m_x >= 0) {
  260. vout.m_z = 4;
  261. vout.m_x = vin.m_y;
  262. vout.m_y = vin.m_z;
  263. } else {
  264. vout.m_z = 5;
  265. vout.m_x = -vin.m_y;
  266. vout.m_y = -vin.m_z;
  267. }
  268. }
  269. }
  270. local.CubeToSphere = {
  271. 0: function (vin, vout) {
  272. vout.m_x = vin.m_x;
  273. vout.m_y = vin.m_y;
  274. vout.m_z = Math.sqrt(Math.max(0.0, 1.0 - vout.m_x * vout.m_x - vout.m_y * vout.m_y));
  275. },
  276. 1: function (vin, vout) {
  277. vout.m_x = -vin.m_x;
  278. vout.m_y = -vin.m_y;
  279. vout.m_z = -Math.sqrt(Math.max(0.0, 1.0 - vout.m_x * vout.m_x - vout.m_y * vout.m_y));
  280. },
  281. 2: function (vin, vout) {
  282. vout.m_z = vin.m_x;
  283. vout.m_x = vin.m_y;
  284. vout.m_y = Math.sqrt(Math.max(0.0, 1.0 - vout.m_x * vout.m_x - vout.m_z * vout.m_z));
  285. },
  286. 3: function (vin, vout) {
  287. vout.m_z = -vin.m_x;
  288. vout.m_x = -vin.m_y;
  289. vout.m_y = -Math.sqrt(Math.max(0.0, 1.0 - vout.m_x * vout.m_x - vout.m_z * vout.m_z));
  290. },
  291. 4: function (vin, vout) {
  292. vout.m_y = vin.m_x;
  293. vout.m_z = vin.m_y;
  294. vout.m_x = Math.sqrt(Math.max(0.0, 1.0 - vout.m_y * vout.m_y - vout.m_z * vout.m_z));
  295. },
  296. 5: function (vin, vout) {
  297. vout.m_y = -vin.m_x;
  298. vout.m_z = -vin.m_y;
  299. vout.m_x = -Math.sqrt(Math.max(0.0, 1.0 - vout.m_y * vout.m_y - vout.m_z * vout.m_z));
  300. }
  301. };
  302. function IntToUInt(value) {
  303. return (value < 0) ? (-1 - (2 * value)) : (2 * value);
  304. }
  305. function UIntToInt(uiValue) {
  306. return (uiValue & 1) ? -((uiValue + 1) >>> 1) : ((uiValue >>> 1));
  307. }
  308. module.Iterator = function () {
  309. this.m_count = 0;
  310. };
  311. module.NumberRef = function () {
  312. this.m_value = 0;
  313. };
  314. // BinaryStream class
  315. module.BinaryStream = function (buffer) {
  316. this.m_endianness = SystemEndianness();
  317. this.m_buffer = buffer;
  318. this.m_stream = new Uint8Array(this.m_buffer);
  319. this.m_localBuffer = new ArrayBuffer(4);
  320. this.m_localBufferViewUChar8 = new Uint8Array(this.m_localBuffer);
  321. this.m_localBufferViewFloat32 = new Float32Array(this.m_localBuffer);
  322. this.m_localBufferViewUInt32 = new Uint32Array(this.m_localBuffer);
  323. };
  324. module.BinaryStream.prototype.ReadFloat32Bin = function (bsIterator) {
  325. if (this.m_endianness === local.O3DGC_BIG_ENDIAN) {
  326. this.m_localBufferViewUChar8[3] = this.m_stream[bsIterator.m_count++];
  327. this.m_localBufferViewUChar8[2] = this.m_stream[bsIterator.m_count++];
  328. this.m_localBufferViewUChar8[1] = this.m_stream[bsIterator.m_count++];
  329. this.m_localBufferViewUChar8[0] = this.m_stream[bsIterator.m_count++];
  330. } else {
  331. this.m_localBufferViewUChar8[0] = this.m_stream[bsIterator.m_count++];
  332. this.m_localBufferViewUChar8[1] = this.m_stream[bsIterator.m_count++];
  333. this.m_localBufferViewUChar8[2] = this.m_stream[bsIterator.m_count++];
  334. this.m_localBufferViewUChar8[3] = this.m_stream[bsIterator.m_count++];
  335. }
  336. return this.m_localBufferViewFloat32[0];
  337. };
  338. module.BinaryStream.prototype.ReadUInt32Bin = function (bsIterator) {
  339. if (this.m_endianness === local.O3DGC_BIG_ENDIAN) {
  340. this.m_localBufferViewUChar8[3] = this.m_stream[bsIterator.m_count++];
  341. this.m_localBufferViewUChar8[2] = this.m_stream[bsIterator.m_count++];
  342. this.m_localBufferViewUChar8[1] = this.m_stream[bsIterator.m_count++];
  343. this.m_localBufferViewUChar8[0] = this.m_stream[bsIterator.m_count++];
  344. } else {
  345. this.m_localBufferViewUChar8[0] = this.m_stream[bsIterator.m_count++];
  346. this.m_localBufferViewUChar8[1] = this.m_stream[bsIterator.m_count++];
  347. this.m_localBufferViewUChar8[2] = this.m_stream[bsIterator.m_count++];
  348. this.m_localBufferViewUChar8[3] = this.m_stream[bsIterator.m_count++];
  349. }
  350. return this.m_localBufferViewUInt32[0];
  351. };
  352. module.BinaryStream.prototype.ReadUChar8Bin = function (bsIterator) {
  353. return this.m_stream[bsIterator.m_count++];
  354. };
  355. module.BinaryStream.prototype.ReadUInt32ASCII = function (bsIterator) {
  356. var value, shift, i;
  357. value = 0;
  358. shift = 0;
  359. for (i = 0; i < local.O3DGC_BINARY_STREAM_NUM_SYMBOLS_UINT32; ++i) {
  360. value += (this.m_stream[bsIterator.m_count++] << shift) >>> 0;
  361. shift += local.O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0;
  362. }
  363. return value;
  364. };
  365. module.BinaryStream.prototype.ReadFloat32ASCII = function (bsIterator) {
  366. var value = this.ReadUInt32ASCII(bsIterator);
  367. if (this.m_endianness === local.O3DGC_BIG_ENDIAN) {
  368. this.m_localBufferViewUChar8[3] = value & local.O3DGC_MAX_UCHAR8;
  369. value >>>= 8;
  370. this.m_localBufferViewUChar8[2] = value & local.O3DGC_MAX_UCHAR8;
  371. value >>>= 8;
  372. this.m_localBufferViewUChar8[1] = value & local.O3DGC_MAX_UCHAR8;
  373. value >>>= 8;
  374. this.m_localBufferViewUChar8[0] = value & local.O3DGC_MAX_UCHAR8;
  375. } else {
  376. this.m_localBufferViewUChar8[0] = value & local.O3DGC_MAX_UCHAR8;
  377. value >>>= 8;
  378. this.m_localBufferViewUChar8[1] = value & local.O3DGC_MAX_UCHAR8;
  379. value >>>= 8;
  380. this.m_localBufferViewUChar8[2] = value & local.O3DGC_MAX_UCHAR8;
  381. value >>>= 8;
  382. this.m_localBufferViewUChar8[3] = value & local.O3DGC_MAX_UCHAR8;
  383. }
  384. return this.m_localBufferViewFloat32[0];
  385. };
  386. module.BinaryStream.prototype.ReadIntASCII = function (bsIterator) {
  387. return UIntToInt(this.ReadUIntASCII(bsIterator));
  388. };
  389. module.BinaryStream.prototype.ReadUIntASCII = function (bsIterator) {
  390. var i, x, value;
  391. value = this.m_stream[bsIterator.m_count++];
  392. if (value === local.O3DGC_BINARY_STREAM_MAX_SYMBOL0) {
  393. i = 0;
  394. do {
  395. x = this.m_stream[bsIterator.m_count++];
  396. value += ((x >>> 1) << i) >>> 0;
  397. i += local.O3DGC_BINARY_STREAM_BITS_PER_SYMBOL1;
  398. } while (x & 1);
  399. }
  400. return value;
  401. };
  402. module.BinaryStream.prototype.ReadUCharASCII = function (bsIterator) {
  403. return this.m_stream[bsIterator.m_count++];
  404. };
  405. module.BinaryStream.prototype.ReadFloat32 = function (bsIterator, streamType) {
  406. if (streamType === local.O3DGC_STREAM_TYPE_ASCII) {
  407. return this.ReadFloat32ASCII(bsIterator);
  408. }
  409. return this.ReadFloat32Bin(bsIterator);
  410. };
  411. module.BinaryStream.prototype.ReadUInt32 = function (bsIterator, streamType) {
  412. if (streamType === local.O3DGC_STREAM_TYPE_ASCII) {
  413. return this.ReadUInt32ASCII(bsIterator);
  414. }
  415. return this.ReadUInt32Bin(bsIterator);
  416. };
  417. module.BinaryStream.prototype.ReadUChar = function (bsIterator, streamType) {
  418. if (streamType === local.O3DGC_STREAM_TYPE_ASCII) {
  419. return this.ReadUCharASCII(bsIterator);
  420. }
  421. return this.ReadUChar8Bin(bsIterator);
  422. };
  423. module.BinaryStream.prototype.GetBuffer = function (bsIterator, size) {
  424. return new Uint8Array(this.m_buffer, bsIterator.m_count, size);
  425. };
  426. // Copyright (c) 2004 Amir Said (said@ieee.org) & William A. Pearlman (pearlw@ecse.rpi.edu)
  427. // All rights reserved.
  428. local.O3DGC_AC_MIN_LENGTH = 0x01000000; // threshold for renormalization
  429. local.O3DGC_AC_MAX_LENGTH = 0xFFFFFFFF; // maximum AC interval length
  430. local.O3DGC_AC_BM_LENGTH_SHIFT = 13; // Maximum values for binary models length bits discarded before mult.
  431. local.O3DGC_AC_BM_MAX_COUNT = (1 << local.O3DGC_AC_BM_LENGTH_SHIFT) >>> 0; // for adaptive models
  432. local.O3DGC_AC_DM_LENGTH_SHIFT = 15; // Maximum values for general models length bits discarded before mult.
  433. local.O3DGC_AC_DM_MAX_COUNT = (1 << local.O3DGC_AC_DM_LENGTH_SHIFT) >>> 0; // for adaptive models
  434. // StaticBitModel class
  435. module.StaticBitModel = function () {
  436. this.m_bit0Prob = (1 << (local.O3DGC_AC_BM_LENGTH_SHIFT - 1)) >>> 0; // p0 = 0.5
  437. };
  438. module.StaticBitModel.prototype.SetProbability = function (p) {
  439. this.m_bit0Prob = Math.floor(p * ((1 << local.O3DGC_AC_BM_LENGTH_SHIFT) >>> 0));
  440. };
  441. // AdaptiveBitModel class
  442. module.AdaptiveBitModel = function () {
  443. // initialization to equiprobable model
  444. this.m_updateCycle = 4;
  445. this.m_bitsUntilUpdate = 4;
  446. this.m_bit0Prob = (1 << (local.O3DGC_AC_BM_LENGTH_SHIFT - 1)) >>> 0;
  447. this.m_bit0Count = 1;
  448. this.m_bitCount = 2;
  449. };
  450. module.AdaptiveBitModel.prototype.Reset = function () {
  451. this.m_updateCycle = 4;
  452. this.m_bitsUntilUpdate = 4;
  453. this.m_bit0Prob = (1 << (local.O3DGC_AC_BM_LENGTH_SHIFT - 1)) >>> 0;
  454. this.m_bit0Count = 1;
  455. this.m_bitCount = 2;
  456. };
  457. module.AdaptiveBitModel.prototype.Update = function () {
  458. // halve counts when a threshold is reached
  459. if ((this.m_bitCount += this.m_updateCycle) > local.O3DGC_AC_BM_MAX_COUNT) {
  460. this.m_bitCount = (this.m_bitCount + 1) >>> 1;
  461. this.m_bit0Count = (this.m_bit0Count + 1) >>> 1;
  462. if (this.m_bit0Count === this.m_bitCount) {
  463. ++this.m_bitCount;
  464. }
  465. }
  466. // compute scaled bit 0 probability
  467. var scale = Math.floor(0x80000000 / this.m_bitCount);
  468. this.m_bit0Prob = (this.m_bit0Count * scale) >>> (31 - local.O3DGC_AC_BM_LENGTH_SHIFT);
  469. // set frequency of model updates
  470. this.m_updateCycle = (5 * this.m_updateCycle) >>> 2;
  471. if (this.m_updateCycle > 64) {
  472. this.m_updateCycle = 64;
  473. }
  474. this.m_bitsUntilUpdate = this.m_updateCycle;
  475. };
  476. // AdaptiveDataModel class
  477. module.AdaptiveDataModel = function () {
  478. this.m_buffer = {};
  479. this.m_distribution = {};
  480. this.m_symbolCount = {};
  481. this.m_decoderTable = {};
  482. this.m_totalCount = 0;
  483. this.m_updateCycle = 0;
  484. this.m_symbolsUntilUpdate = 0;
  485. this.m_dataSymbols = 0;
  486. this.m_lastSymbol = 0;
  487. this.m_tableSize = 0;
  488. this.m_tableShift = 0;
  489. };
  490. module.AdaptiveDataModel.prototype.Update = function () {
  491. var n, sum, s, scale, k, max_cycle, w;
  492. // halve counts when a threshold is reached
  493. if ((this.m_totalCount += this.m_updateCycle) > local.O3DGC_AC_DM_MAX_COUNT) {
  494. this.m_totalCount = 0;
  495. for (n = 0; n < this.m_dataSymbols; ++n) {
  496. this.m_totalCount += (this.m_symbolCount[n] = (this.m_symbolCount[n] + 1) >>> 1);
  497. }
  498. }
  499. // compute cumulative distribution, decoder table
  500. sum = 0;
  501. s = 0;
  502. scale = Math.floor(0x80000000 / this.m_totalCount);
  503. if (this.m_tableSize === 0) {
  504. for (k = 0; k < this.m_dataSymbols; ++k) {
  505. this.m_distribution[k] = (scale * sum) >>> (31 - local.O3DGC_AC_DM_LENGTH_SHIFT);
  506. sum += this.m_symbolCount[k];
  507. }
  508. } else {
  509. for (k = 0; k < this.m_dataSymbols; ++k) {
  510. this.m_distribution[k] = (scale * sum) >>> (31 - local.O3DGC_AC_DM_LENGTH_SHIFT);
  511. sum += this.m_symbolCount[k];
  512. w = this.m_distribution[k] >>> this.m_tableShift;
  513. while (s < w) {
  514. this.m_decoderTable[++s] = k - 1;
  515. }
  516. }
  517. this.m_decoderTable[0] = 0;
  518. while (s <= this.m_tableSize) {
  519. this.m_decoderTable[++s] = this.m_dataSymbols - 1;
  520. }
  521. }
  522. // set frequency of model updates
  523. this.m_updateCycle = (5 * this.m_updateCycle) >>> 2;
  524. max_cycle = ((this.m_dataSymbols + 6) << 3) >>> 0;
  525. if (this.m_updateCycle > max_cycle) {
  526. this.m_updateCycle = max_cycle;
  527. }
  528. this.m_symbolsUntilUpdate = this.m_updateCycle;
  529. };
  530. module.AdaptiveDataModel.prototype.Reset = function () {
  531. var k;
  532. if (this.m_dataSymbols === 0) {
  533. return;
  534. }
  535. // restore probability estimates to uniform distribution
  536. this.m_totalCount = 0;
  537. this.m_updateCycle = this.m_dataSymbols;
  538. for (k = 0; k < this.m_dataSymbols; ++k) {
  539. this.m_symbolCount[k] = 1;
  540. }
  541. this.Update();
  542. this.m_symbolsUntilUpdate = this.m_updateCycle = (this.m_dataSymbols + 6) >>> 1;
  543. };
  544. module.AdaptiveDataModel.prototype.SetAlphabet = function (number_of_symbols) {
  545. if ((number_of_symbols < 2) || (number_of_symbols > (1 << 11))) {
  546. Console.log("invalid number of data symbols");
  547. return module.O3DGC_ERROR_AC;
  548. }
  549. if (this.m_dataSymbols !== number_of_symbols) { // assign memory for data model
  550. this.m_dataSymbols = number_of_symbols;
  551. this.m_lastSymbol = this.m_dataSymbols - 1;
  552. // define size of table for fast decoding
  553. if (this.m_dataSymbols > 16) {
  554. var table_bits = 3;
  555. while (this.m_dataSymbols > ((1 << (table_bits + 2)) >>> 0)) {
  556. ++table_bits;
  557. }
  558. this.m_tableSize = (1 << table_bits) >>> 0;
  559. this.m_tableShift = local.O3DGC_AC_DM_LENGTH_SHIFT - table_bits;
  560. this.m_buffer = new ArrayBuffer(4 * (2 * this.m_dataSymbols + this.m_tableSize + 2));
  561. this.m_distribution = new Uint32Array(this.m_buffer, 0, this.m_dataSymbols);
  562. this.m_symbolCount = new Uint32Array(this.m_buffer, 4 * this.m_dataSymbols, this.m_dataSymbols);
  563. this.m_decoderTable = new Uint32Array(this.m_buffer, 8 * this.m_dataSymbols, this.m_tableSize + 2);
  564. } else {// small alphabet: no table needed
  565. this.m_tableSize = this.m_tableShift = 0;
  566. this.m_buffer = new ArrayBuffer(4 * 2 * this.m_dataSymbols);
  567. this.m_distribution = new Uint32Array(this.m_buffer, 0, this.m_dataSymbols);
  568. this.m_symbolCount = new Uint32Array(this.m_buffer, 4 * this.m_dataSymbols, this.m_dataSymbols);
  569. this.m_decoderTable = {};
  570. }
  571. }
  572. this.Reset(); // initialize model
  573. return module.O3DGC_OK;
  574. };
  575. // ArithmeticDecoder class
  576. module.ArithmeticDecoder = function () {
  577. this.m_codeBuffer = {};
  578. this.m_acShift = 0;
  579. this.m_base = 0;
  580. this.m_value = 0;
  581. this.m_length = 0; // arithmetic coding state
  582. this.m_bufferSize = 0;
  583. this.m_mode = 0; // mode: 0 = undef, 1 = encoder, 2 = decoder
  584. };
  585. module.ArithmeticDecoder.prototype.SetBuffer = function (max_code_bytes, user_buffer) {
  586. if (max_code_bytes === 0) {
  587. Console.log("invalid codec buffer size");
  588. return module.O3DGC_ERROR_AC;
  589. }
  590. if (this.m_mode !== 0) {
  591. Console.log("cannot set buffer while encoding or decoding");
  592. return module.O3DGC_ERROR_AC;
  593. }
  594. this.m_bufferSize = max_code_bytes;
  595. this.m_codeBuffer = user_buffer;
  596. };
  597. module.ArithmeticDecoder.prototype.StartDecoder = function () {
  598. if (this.m_mode !== 0) {
  599. Console.log("cannot start decoder");
  600. return module.O3DGC_ERROR_AC;
  601. }
  602. if (this.m_bufferSize === 0) {
  603. Console.log("no code buffer set");
  604. return module.O3DGC_ERROR_AC;
  605. }
  606. // initialize decoder: interval, pointer, initial code value
  607. this.m_mode = 2;
  608. this.m_length = local.O3DGC_AC_MAX_LENGTH;
  609. this.m_acShift = 3;
  610. this.m_value = ((this.m_codeBuffer[0] << 24) | (this.m_codeBuffer[1] << 16) | (this.m_codeBuffer[2] << 8) | (this.m_codeBuffer[3])) >>> 0;
  611. };
  612. module.ArithmeticDecoder.prototype.StopDecoder = function () {
  613. if (this.m_mode !== 2) {
  614. Console.log("invalid to stop decoder");
  615. return module.O3DGC_ERROR_AC;
  616. }
  617. this.m_mode = 0;
  618. };
  619. module.ArithmeticDecoder.prototype.GetBit = function () {
  620. this.m_length >>>= 1; // halve interval
  621. var bit = (this.m_value >= this.m_length); // decode bit
  622. if (bit) {
  623. this.m_value -= this.m_length; // move base
  624. }
  625. if (this.m_length < local.O3DGC_AC_MIN_LENGTH) {
  626. this.RenormDecInterval(); // renormalization
  627. }
  628. return bit;
  629. };
  630. module.ArithmeticDecoder.prototype.GetBits = function (bits) {
  631. var s = Math.floor(this.m_value / (this.m_length >>>= bits)); // decode symbol, change length
  632. this.m_value -= this.m_length * s; // update interval
  633. if (this.m_length < local.O3DGC_AC_MIN_LENGTH) {
  634. this.RenormDecInterval(); // renormalization
  635. }
  636. return s;
  637. };
  638. module.ArithmeticDecoder.prototype.DecodeStaticBitModel = function (M) {
  639. var x, bit;
  640. x = M.m_bit0Prob * (this.m_length >>> local.O3DGC_AC_BM_LENGTH_SHIFT); // product l x p0
  641. bit = (this.m_value >= x); // decision
  642. // update & shift interval
  643. if (!bit) {
  644. this.m_length = x;
  645. } else {
  646. this.m_value -= x; // shifted interval base = 0
  647. this.m_length -= x;
  648. }
  649. if (this.m_length < local.O3DGC_AC_MIN_LENGTH) {
  650. this.RenormDecInterval(); // renormalization
  651. }
  652. return bit; // return data bit value
  653. };
  654. module.ArithmeticDecoder.prototype.DecodeAdaptiveBitModel = function (M) {
  655. var x, bit;
  656. x = M.m_bit0Prob * (this.m_length >>> local.O3DGC_AC_BM_LENGTH_SHIFT); // product l x p0
  657. bit = (this.m_value >= x); // decision
  658. // update interval
  659. if (!bit) {
  660. this.m_length = x;
  661. ++M.m_bit0Count;
  662. } else {
  663. this.m_value -= x;
  664. this.m_length -= x;
  665. }
  666. if (this.m_length < local.O3DGC_AC_MIN_LENGTH) {
  667. this.RenormDecInterval(); // renormalization
  668. }
  669. if (--M.m_bitsUntilUpdate === 0) {
  670. M.Update(); // periodic model update
  671. }
  672. return bit; // return data bit value
  673. };
  674. module.ArithmeticDecoder.prototype.DecodeAdaptiveDataModel = function (M) {
  675. var n, s, x, y, t, dv, z, m;
  676. y = this.m_length;
  677. if (M.m_tableSize > 0) { // use table look-up for faster decoding
  678. dv = Math.floor(this.m_value / (this.m_length >>>= local.O3DGC_AC_DM_LENGTH_SHIFT));
  679. t = dv >>> M.m_tableShift;
  680. s = M.m_decoderTable[t]; // initial decision based on table look-up
  681. n = M.m_decoderTable[t + 1] + 1;
  682. while (n > s + 1) { // finish with bisection search
  683. m = (s + n) >>> 1;
  684. if (M.m_distribution[m] > dv) {
  685. n = m;
  686. } else {
  687. s = m;
  688. }
  689. }
  690. // compute products
  691. x = M.m_distribution[s] * this.m_length;
  692. if (s !== M.m_lastSymbol) {
  693. y = M.m_distribution[s + 1] * this.m_length;
  694. }
  695. } else { // decode using only multiplications
  696. x = s = 0;
  697. this.m_length >>>= local.O3DGC_AC_DM_LENGTH_SHIFT;
  698. m = (n = M.m_dataSymbols) >>> 1;
  699. // decode via bisection search
  700. do {
  701. z = this.m_length * M.m_distribution[m];
  702. if (z > this.m_value) {
  703. n = m;
  704. y = z; // value is smaller
  705. } else {
  706. s = m;
  707. x = z; // value is larger or equal
  708. }
  709. } while ((m = (s + n) >>> 1) !== s);
  710. }
  711. this.m_value -= x; // update interval
  712. this.m_length = y - x;
  713. if (this.m_length < local.O3DGC_AC_MIN_LENGTH) {
  714. this.RenormDecInterval(); // renormalization
  715. }
  716. ++M.m_symbolCount[s];
  717. if (--M.m_symbolsUntilUpdate === 0) {
  718. M.Update(false); // periodic model update
  719. }
  720. return s;
  721. };
  722. module.ArithmeticDecoder.prototype.ExpGolombDecode = function (k, bModel0, bModel1) {
  723. var symbol, binary_symbol, l;
  724. symbol = 0;
  725. binary_symbol = 0;
  726. do {
  727. l = this.DecodeAdaptiveBitModel(bModel1);
  728. if (l) {
  729. symbol += (1 << k) >>> 0;
  730. k++;
  731. }
  732. } while (l);
  733. while (k--) { //next binary part
  734. if (this.DecodeStaticBitModel(bModel0)) {
  735. binary_symbol = (binary_symbol | (1 << k)) >>> 0;
  736. }
  737. }
  738. return (symbol + binary_symbol);
  739. };
  740. module.ArithmeticDecoder.prototype.RenormDecInterval = function () {
  741. do { // read least-significant byte
  742. this.m_value = ((this.m_value << 8) | this.m_codeBuffer[++this.m_acShift]) >>> 0;
  743. this.m_length = (this.m_length << 8) >>> 0;
  744. } while (this.m_length < local.O3DGC_AC_MIN_LENGTH); // length multiplied by 256
  745. };
  746. module.ArithmeticDecoder.prototype.DecodeIntACEGC = function (mModelValues, bModel0, bModel1, exp_k, M) {
  747. var uiValue = this.DecodeAdaptiveDataModel(mModelValues);
  748. if (uiValue === M) {
  749. uiValue += this.ExpGolombDecode(exp_k, bModel0, bModel1);
  750. }
  751. return UIntToInt(uiValue);
  752. };
  753. module.ArithmeticDecoder.prototype.DecodeUIntACEGC = function (mModelValues, bModel0, bModel1, exp_k, M) {
  754. var uiValue = this.DecodeAdaptiveDataModel(mModelValues);
  755. if (uiValue === M) {
  756. uiValue += this.ExpGolombDecode(exp_k, bModel0, bModel1);
  757. }
  758. return uiValue;
  759. };
  760. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  761. // FIFO class
  762. module.FIFO = function () {
  763. this.m_data = {};
  764. this.m_allocated = 0;
  765. this.m_size = 0;
  766. this.m_start = 0;
  767. this.m_end = 0;
  768. };
  769. module.FIFO.prototype.Clear = function () {
  770. this.m_start = this.m_end = this.m_size = 0;
  771. };
  772. module.FIFO.prototype.GetAllocatedSize = function () {
  773. return this.m_allocated;
  774. };
  775. module.FIFO.prototype.GetSize = function () {
  776. return this.m_size;
  777. };
  778. module.FIFO.prototype.Allocate = function (size) {
  779. if (size > this.m_allocated) {
  780. this.m_allocated = size;
  781. this.m_data = new Int32Array(this.m_allocated);
  782. }
  783. this.Clear();
  784. return module.O3DGC_OK;
  785. };
  786. module.FIFO.prototype.PopFirst = function () {
  787. --this.m_size;
  788. var current = this.m_start++;
  789. if (this.m_start === this.m_allocated) {
  790. this.m_end = 0;
  791. }
  792. return this.m_data[current];
  793. };
  794. module.FIFO.prototype.PushBack = function (value) {
  795. --this.m_size;
  796. this.m_data[this.m_end] = value;
  797. ++this.m_size;
  798. ++this.m_end;
  799. if (this.m_end === this.m_allocated) {
  800. this.m_end = 0;
  801. }
  802. };
  803. // IndexedFaceSet class
  804. module.IndexedFaceSet = function () {
  805. this.m_nCoordIndex = 0;
  806. this.m_nCoord = 0;
  807. this.m_nNormal = 0;
  808. this.m_numFloatAttributes = 0;
  809. this.m_numIntAttributes = 0;
  810. this.m_creaseAngle = 30.0;
  811. this.m_ccw = true;
  812. this.m_solid = true;
  813. this.m_convex = true;
  814. this.m_isTriangularMesh = true;
  815. this.m_coordMin = new Float32Array(3);
  816. this.m_coordMax = new Float32Array(3);
  817. this.m_normalMin = new Float32Array(3);
  818. this.m_normalMax = new Float32Array(3);
  819. this.m_nFloatAttribute = new Uint32Array(local.O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES);
  820. this.m_nIntAttribute = new Uint32Array(local.O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES);
  821. this.m_dimFloatAttribute = new Uint32Array(local.O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES);
  822. this.m_dimIntAttribute = new Uint32Array(local.O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES);
  823. this.m_typeFloatAttribute = new Uint32Array(local.O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES);
  824. this.m_typeIntAttribute = new Uint32Array(local.O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES);
  825. this.m_minFloatAttributeBuffer = new ArrayBuffer(4 * local.O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES * local.O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES);
  826. this.m_minFloatAttribute = new Float32Array(this.m_minFloatAttributeBuffer);
  827. this.m_maxFloatAttributeBuffer = new ArrayBuffer(4 * local.O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES * local.O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES);
  828. this.m_maxFloatAttribute = new Float32Array(this.m_maxFloatAttributeBuffer);
  829. this.m_coordIndex = {};
  830. this.m_coord = {};
  831. this.m_normal = {};
  832. this.m_floatAttribute = [];
  833. this.m_intAttribute = [];
  834. };
  835. module.IndexedFaceSet.prototype.GetNCoordIndex = function () {
  836. return this.m_nCoordIndex;
  837. };
  838. module.IndexedFaceSet.prototype.GetNCoordIndex = function () {
  839. return this.m_nCoordIndex;
  840. };
  841. module.IndexedFaceSet.prototype.GetNCoord = function () {
  842. return this.m_nCoord;
  843. };
  844. module.IndexedFaceSet.prototype.GetNNormal = function () {
  845. return this.m_nNormal;
  846. };
  847. module.IndexedFaceSet.prototype.GetNFloatAttribute = function (a) {
  848. return this.m_nFloatAttribute[a];
  849. };
  850. module.IndexedFaceSet.prototype.GetNIntAttribute = function (a) {
  851. return this.m_nIntAttribute[a];
  852. };
  853. module.IndexedFaceSet.prototype.GetNumFloatAttributes = function () {
  854. return this.m_numFloatAttributes;
  855. };
  856. module.IndexedFaceSet.prototype.GetNumIntAttributes = function () {
  857. return this.m_numIntAttributes;
  858. };
  859. module.IndexedFaceSet.prototype.GetCoordMinArray = function () {
  860. return this.m_coordMin;
  861. };
  862. module.IndexedFaceSet.prototype.GetCoordMaxArray = function () {
  863. return this.m_coordMax;
  864. };
  865. module.IndexedFaceSet.prototype.GetNormalMinArray = function () {
  866. return this.m_normalMin;
  867. };
  868. module.IndexedFaceSet.prototype.GetNormalMaxArray = function () {
  869. return this.m_normalMax;
  870. };
  871. module.IndexedFaceSet.prototype.GetFloatAttributeMinArray = function (a) {
  872. return (new Float32Array(this.m_minFloatAttributeBuffer, a * local.O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES * 4, this.GetFloatAttributeDim(a)));
  873. };
  874. module.IndexedFaceSet.prototype.GetFloatAttributeMaxArray = function (a) {
  875. return (new Float32Array(this.m_maxFloatAttributeBuffer, a * local.O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES * 4, this.GetFloatAttributeDim(a)));
  876. };
  877. module.IndexedFaceSet.prototype.GetFloatAttributeDim = function (a) {
  878. return this.m_dimFloatAttribute[a];
  879. };
  880. module.IndexedFaceSet.prototype.GetIntAttributeDim = function (a) {
  881. return this.m_dimIntAttribute[a];
  882. };
  883. module.IndexedFaceSet.prototype.GetFloatAttributeType = function (a) {
  884. return this.m_typeFloatAttribute[a];
  885. };
  886. module.IndexedFaceSet.prototype.GetIntAttributeType = function (a) {
  887. return this.m_typeIntAttribute[a];
  888. };
  889. module.IndexedFaceSet.prototype.GetFloatAttributeMax = function (a, dim) {
  890. return this.m_maxFloatAttribute[a * local.O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES + dim];
  891. };
  892. module.IndexedFaceSet.prototype.GetCreaseAngle = function () {
  893. return this.m_creaseAngle;
  894. };
  895. module.IndexedFaceSet.prototype.GetCreaseAngle = function () {
  896. return this.m_creaseAngle;
  897. };
  898. module.IndexedFaceSet.prototype.GetCCW = function () {
  899. return this.m_ccw;
  900. };
  901. module.IndexedFaceSet.prototype.GetSolid = function () {
  902. return this.m_solid;
  903. };
  904. module.IndexedFaceSet.prototype.GetConvex = function () {
  905. return this.m_convex;
  906. };
  907. module.IndexedFaceSet.prototype.GetIsTriangularMesh = function () {
  908. return this.m_isTriangularMesh;
  909. };
  910. module.IndexedFaceSet.prototype.GetCoordIndex = function () {
  911. return this.m_coordIndex;
  912. };
  913. module.IndexedFaceSet.prototype.GetCoordIndex = function () {
  914. return this.m_coordIndex;
  915. };
  916. module.IndexedFaceSet.prototype.GetCoord = function () {
  917. return this.m_coord;
  918. };
  919. module.IndexedFaceSet.prototype.GetNormal = function () {
  920. return this.m_normal;
  921. };
  922. module.IndexedFaceSet.prototype.GetFloatAttribute = function (a) {
  923. return this.m_floatAttribute[a];
  924. };
  925. module.IndexedFaceSet.prototype.GetIntAttribute = function (a) {
  926. return this.m_intAttribute[a];
  927. };
  928. module.IndexedFaceSet.prototype.SetNCoordIndex = function (nCoordIndex) {
  929. this.m_nCoordIndex = nCoordIndex;
  930. };
  931. module.IndexedFaceSet.prototype.SetNNormalIndex = function (nNormalIndex) {
  932. };
  933. module.IndexedFaceSet.prototype.SetNormalPerVertex = function (perVertex) {
  934. };
  935. module.IndexedFaceSet.prototype.SetNFloatAttributeIndex = function (nFloatAttributeIndex) {
  936. };
  937. module.IndexedFaceSet.prototype.SetNIntAttributeIndex = function (nIntAttributeIndex) {
  938. };
  939. module.IndexedFaceSet.prototype.SetFloatAttributePerVertex = function (perVertex) {
  940. };
  941. module.IndexedFaceSet.prototype.SetIntAttributePerVertex = function (perVertex) {
  942. };
  943. module.IndexedFaceSet.prototype.SetNCoord = function (nCoord) {
  944. this.m_nCoord = nCoord;
  945. };
  946. module.IndexedFaceSet.prototype.SetNNormal = function (nNormal) {
  947. this.m_nNormal = nNormal;
  948. };
  949. module.IndexedFaceSet.prototype.SetNumFloatAttributes = function (numFloatAttributes) {
  950. this.m_numFloatAttributes = numFloatAttributes;
  951. };
  952. module.IndexedFaceSet.prototype.SetNumIntAttributes = function (numIntAttributes) {
  953. this.m_numIntAttributes = numIntAttributes;
  954. };
  955. module.IndexedFaceSet.prototype.SetCreaseAngle = function (creaseAngle) {
  956. this.m_creaseAngle = creaseAngle;
  957. };
  958. module.IndexedFaceSet.prototype.SetCCW = function (ccw) {
  959. this.m_ccw = ccw;
  960. };
  961. module.IndexedFaceSet.prototype.SetSolid = function (solid) {
  962. this.m_solid = solid;
  963. };
  964. module.IndexedFaceSet.prototype.SetConvex = function (convex) {
  965. this.m_convex = convex;
  966. };
  967. module.IndexedFaceSet.prototype.SetIsTriangularMesh = function (isTriangularMesh) {
  968. this.m_isTriangularMesh = isTriangularMesh;
  969. };
  970. module.IndexedFaceSet.prototype.SetCoordMin = function (j, min) {
  971. this.m_coordMin[j] = min;
  972. };
  973. module.IndexedFaceSet.prototype.SetCoordMax = function (j, max) {
  974. this.m_coordMax[j] = max;
  975. };
  976. module.IndexedFaceSet.prototype.SetNormalMin = function (j, min) {
  977. this.m_normalMin[j] = min;
  978. };
  979. module.IndexedFaceSet.prototype.SetNormalMax = function (j, max) {
  980. this.m_normalMax[j] = max;
  981. };
  982. module.IndexedFaceSet.prototype.SetNFloatAttribute = function (a, nFloatAttribute) {
  983. this.m_nFloatAttribute[a] = nFloatAttribute;
  984. };
  985. module.IndexedFaceSet.prototype.SetNIntAttribute = function (a, nIntAttribute) {
  986. this.m_nIntAttribute[a] = nIntAttribute;
  987. };
  988. module.IndexedFaceSet.prototype.SetFloatAttributeDim = function (a, d) {
  989. this.m_dimFloatAttribute[a] = d;
  990. };
  991. module.IndexedFaceSet.prototype.SetIntAttributeDim = function (a, d) {
  992. this.m_dimIntAttribute[a] = d;
  993. };
  994. module.IndexedFaceSet.prototype.SetFloatAttributeType = function (a, d) {
  995. this.m_typeFloatAttribute[a] = d;
  996. };
  997. module.IndexedFaceSet.prototype.SetIntAttributeType = function (a, d) {
  998. this.m_typeIntAttribute[a] = d;
  999. };
  1000. module.IndexedFaceSet.prototype.SetFloatAttributeMin = function (a, dim, min) {
  1001. this.m_minFloatAttribute[a * local.O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES + dim] = min;
  1002. };
  1003. module.IndexedFaceSet.prototype.SetFloatAttributeMax = function (a, dim, max) {
  1004. this.m_maxFloatAttribute[a * local.O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES + dim] = max;
  1005. };
  1006. module.IndexedFaceSet.prototype.SetCoordIndex = function (coordIndex) {
  1007. this.m_coordIndex = coordIndex;
  1008. };
  1009. module.IndexedFaceSet.prototype.SetCoord = function (coord) {
  1010. this.m_coord = coord;
  1011. };
  1012. module.IndexedFaceSet.prototype.SetNormal = function (normal) {
  1013. this.m_normal = normal;
  1014. };
  1015. module.IndexedFaceSet.prototype.SetFloatAttribute = function (a, floatAttribute) {
  1016. this.m_floatAttribute[a] = floatAttribute;
  1017. };
  1018. module.IndexedFaceSet.prototype.SetIntAttribute = function (a, intAttribute) {
  1019. this.m_intAttribute[a] = intAttribute;
  1020. };
  1021. // SC3DMCEncodeParams class
  1022. module.SC3DMCEncodeParams = function () {
  1023. var a;
  1024. this.m_numFloatAttributes = 0;
  1025. this.m_numIntAttributes = 0;
  1026. this.m_floatAttributeQuantBits = new Uint32Array(local.O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES);
  1027. this.m_floatAttributePredMode = new Uint32Array(local.O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES);
  1028. this.m_intAttributePredMode = new Uint32Array(local.O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES);
  1029. this.m_encodeMode = local.O3DGC_SC3DMC_ENCODE_MODE_TFAN;
  1030. this.m_streamTypeMode = local.O3DGC_STREAM_TYPE_ASCII;
  1031. this.m_coordQuantBits = 14;
  1032. this.m_normalQuantBits = 8;
  1033. this.m_coordPredMode = local.O3DGC_SC3DMC_PARALLELOGRAM_PREDICTION;
  1034. this.m_normalPredMode = local.O3DGC_SC3DMC_SURF_NORMALS_PREDICTION;
  1035. for (a = 0; a < local.O3DGC_SC3DMC_MAX_NUM_FLOAT_ATTRIBUTES; ++a) {
  1036. this.m_floatAttributePredMode[a] = local.O3DGC_SC3DMC_PARALLELOGRAM_PREDICTION;
  1037. }
  1038. for (a = 0; a < local.O3DGC_SC3DMC_MAX_NUM_INT_ATTRIBUTES; ++a) {
  1039. this.m_intAttributePredMode[a] = local.O3DGC_SC3DMC_DIFFERENTIAL_PREDICTION;
  1040. }
  1041. };
  1042. module.SC3DMCEncodeParams.prototype.GetStreamType = function () {
  1043. return this.m_streamTypeMode;
  1044. };
  1045. module.SC3DMCEncodeParams.prototype.GetEncodeMode = function () {
  1046. return this.m_encodeMode;
  1047. };
  1048. module.SC3DMCEncodeParams.prototype.GetNumFloatAttributes = function () {
  1049. return this.m_numFloatAttributes;
  1050. };
  1051. module.SC3DMCEncodeParams.prototype.GetNumIntAttributes = function () {
  1052. return this.m_numIntAttributes;
  1053. };
  1054. module.SC3DMCEncodeParams.prototype.GetCoordQuantBits = function () {
  1055. return this.m_coordQuantBits;
  1056. };
  1057. module.SC3DMCEncodeParams.prototype.GetNormalQuantBits = function () {
  1058. return this.m_normalQuantBits;
  1059. };
  1060. module.SC3DMCEncodeParams.prototype.GetFloatAttributeQuantBits = function (a) {
  1061. return this.m_floatAttributeQuantBits[a];
  1062. };
  1063. module.SC3DMCEncodeParams.prototype.GetCoordPredMode = function () {
  1064. return this.m_coordPredMode;
  1065. };
  1066. module.SC3DMCEncodeParams.prototype.GetNormalPredMode = function () {
  1067. return this.m_normalPredMode;
  1068. };
  1069. module.SC3DMCEncodeParams.prototype.GetFloatAttributePredMode = function (a) {
  1070. return this.m_floatAttributePredMode[a];
  1071. };
  1072. module.SC3DMCEncodeParams.prototype.GetIntAttributePredMode = function (a) {
  1073. return this.m_intAttributePredMode[a];
  1074. };
  1075. module.SC3DMCEncodeParams.prototype.GetCoordPredMode = function () {
  1076. return this.m_coordPredMode;
  1077. };
  1078. module.SC3DMCEncodeParams.prototype.GetNormalPredMode = function () {
  1079. return this.m_normalPredMode;
  1080. };
  1081. module.SC3DMCEncodeParams.prototype.GetFloatAttributePredMode = function (a) {
  1082. return this.m_floatAttributePredMode[a];
  1083. };
  1084. module.SC3DMCEncodeParams.prototype.GetIntAttributePredMode = function (a) {
  1085. return this.m_intAttributePredMode[a];
  1086. };
  1087. module.SC3DMCEncodeParams.prototype.SetStreamType = function (streamTypeMode) {
  1088. this.m_streamTypeMode = streamTypeMode;
  1089. };
  1090. module.SC3DMCEncodeParams.prototype.SetEncodeMode = function (encodeMode) {
  1091. this.m_encodeMode = encodeMode;
  1092. };
  1093. module.SC3DMCEncodeParams.prototype.SetNumFloatAttributes = function (numFloatAttributes) {
  1094. this.m_numFloatAttributes = numFloatAttributes;
  1095. };
  1096. module.SC3DMCEncodeParams.prototype.SetNumIntAttributes = function (numIntAttributes) {
  1097. this.m_numIntAttributes = numIntAttributes;
  1098. };
  1099. module.SC3DMCEncodeParams.prototype.SetCoordQuantBits = function (coordQuantBits) {
  1100. this.m_coordQuantBits = coordQuantBits;
  1101. };
  1102. module.SC3DMCEncodeParams.prototype.SetNormalQuantBits = function (normalQuantBits) {
  1103. this.m_normalQuantBits = normalQuantBits;
  1104. };
  1105. module.SC3DMCEncodeParams.prototype.SetFloatAttributeQuantBits = function (a, q) {
  1106. this.m_floatAttributeQuantBits[a] = q;
  1107. };
  1108. module.SC3DMCEncodeParams.prototype.SetCoordPredMode = function (coordPredMode) {
  1109. this.m_coordPredMode = coordPredMode;
  1110. };
  1111. module.SC3DMCEncodeParams.prototype.SetNormalPredMode = function (normalPredMode) {
  1112. this.m_normalPredMode = normalPredMode;
  1113. };
  1114. module.SC3DMCEncodeParams.prototype.SetFloatAttributePredMode = function (a, p) {
  1115. this.m_floatAttributePredMode[a] = p;
  1116. };
  1117. module.SC3DMCEncodeParams.prototype.SetIntAttributePredMode = function (a, p) {
  1118. this.m_intAttributePredMode[a] = p;
  1119. };
  1120. // AdjacencyInfo class
  1121. module.AdjacencyInfo = function () {
  1122. this.m_neighborsSize = 0; // actual allocated size for m_neighbors
  1123. this.m_numNeighborsSize = 0; // actual allocated size for m_numNeighbors
  1124. this.m_numElements = 0; // number of elements
  1125. this.m_neighbors = {};
  1126. this.m_numNeighbors = {};
  1127. };
  1128. module.AdjacencyInfo.prototype.Allocate = function (numNeighborsSize, neighborsSize) {
  1129. this.m_numElements = numNeighborsSize;
  1130. if (neighborsSize > this.m_neighborsSize) {
  1131. this.m_neighborsSize = neighborsSize;
  1132. this.m_neighbors = new Int32Array(this.m_neighborsSize);
  1133. }
  1134. if (numNeighborsSize > this.m_numNeighborsSize) {
  1135. this.m_numNeighborsSize = numNeighborsSize;
  1136. this.m_numNeighbors = new Int32Array(this.m_numNeighborsSize);
  1137. }
  1138. return module.O3DGC_OK;
  1139. };
  1140. module.AdjacencyInfo.prototype.AllocateNumNeighborsArray = function (numElements) {
  1141. if (numElements > this.m_numNeighborsSize) {
  1142. this.m_numNeighborsSize = numElements;
  1143. this.m_numNeighbors = new Int32Array(this.m_numNeighborsSize);
  1144. }
  1145. this.m_numElements = numElements;
  1146. return module.O3DGC_OK;
  1147. };
  1148. module.AdjacencyInfo.prototype.AllocateNeighborsArray = function () {
  1149. var i;
  1150. for (i = 1; i < this.m_numElements; ++i) {
  1151. this.m_numNeighbors[i] += this.m_numNeighbors[i - 1];
  1152. }
  1153. if (this.m_numNeighbors[this.m_numElements - 1] > this.m_neighborsSize) {
  1154. this.m_neighborsSize = this.m_numNeighbors[this.m_numElements - 1];
  1155. this.m_neighbors = new Int32Array(this.m_neighborsSize);
  1156. }
  1157. return module.O3DGC_OK;
  1158. };
  1159. module.AdjacencyInfo.prototype.ClearNumNeighborsArray = function () {
  1160. var i;
  1161. for (i = 0; i < this.m_numElements; ++i) {
  1162. this.m_numNeighbors[i] = 0;
  1163. }
  1164. return module.O3DGC_OK;
  1165. };
  1166. module.AdjacencyInfo.prototype.ClearNeighborsArray = function () {
  1167. var i;
  1168. for (i = 0; i < this.m_neighborsSize; ++i) {
  1169. this.m_neighbors[i] = -1;
  1170. }
  1171. return module.O3DGC_OK;
  1172. };
  1173. module.AdjacencyInfo.prototype.Begin = function (element) {
  1174. return (element > 0) ? this.m_numNeighbors[element - 1] : 0;
  1175. };
  1176. module.AdjacencyInfo.prototype.End = function (element) {
  1177. return this.m_numNeighbors[element];
  1178. };
  1179. module.AdjacencyInfo.prototype.AddNeighbor = function (element, neighbor) {
  1180. var p, p0, p1;
  1181. p0 = this.Begin(element);
  1182. p1 = this.End(element);
  1183. for (p = p0; p < p1; ++p) {
  1184. if (this.m_neighbors[p] === -1) {
  1185. this.m_neighbors[p] = neighbor;
  1186. return module.O3DGC_OK;
  1187. }
  1188. }
  1189. return module.O3DGC_ERROR_BUFFER_FULL;
  1190. };
  1191. module.AdjacencyInfo.prototype.GetNeighbor = function (element) {
  1192. return this.m_neighbors[element];
  1193. };
  1194. module.AdjacencyInfo.prototype.GetNumNeighbors = function (element) {
  1195. return this.End(element) - this.Begin(element);
  1196. };
  1197. module.AdjacencyInfo.prototype.GetNumNeighborsBuffer = function () {
  1198. return this.m_numNeighbors;
  1199. };
  1200. module.AdjacencyInfo.prototype.GetNeighborsBuffer = function () {
  1201. return this.m_neighbors;
  1202. };
  1203. // Vector class
  1204. module.Vector = function () {
  1205. this.m_data = {};
  1206. this.m_allocated = 0;
  1207. this.m_size = 0;
  1208. };
  1209. module.Vector.prototype.Clear = function () {
  1210. this.m_size = 0;
  1211. };
  1212. module.Vector.prototype.Get = function (i) {
  1213. return this.m_data[i];
  1214. };
  1215. module.Vector.prototype.GetAllocatedSize = function () {
  1216. return this.m_allocated;
  1217. };
  1218. module.Vector.prototype.GetSize = function () {
  1219. return this.m_size;
  1220. };
  1221. module.Vector.prototype.GetBuffer = function () {
  1222. return this.m_data;
  1223. };
  1224. module.Vector.prototype.SetSize = function (size) {
  1225. this.m_size = size;
  1226. };
  1227. module.Vector.prototype.Allocate = function (size) {
  1228. var i, tmp_data;
  1229. if (size > this.m_allocated) {
  1230. this.m_allocated = size;
  1231. tmp_data = new Int32Array(this.m_allocated);
  1232. if (this.m_size > 0) {
  1233. for (i = 0; i < this.m_size; ++i) {
  1234. tmp_data[i] = this.m_data[i];
  1235. }
  1236. }
  1237. this.m_data = tmp_data;
  1238. }
  1239. };
  1240. module.Vector.prototype.PushBack = function (value) {
  1241. var i, tmp_data;
  1242. if (this.m_size === this.m_allocated) {
  1243. this.m_allocated *= 2;
  1244. if (this.m_allocated < local.O3DGC_DEFAULT_VECTOR_SIZE) {
  1245. this.m_allocated = local.O3DGC_DEFAULT_VECTOR_SIZE;
  1246. }
  1247. tmp_data = new Int32Array(this.m_allocated);
  1248. if (this.m_size > 0) {
  1249. for (i = 0; i < this.m_size; ++i) {
  1250. tmp_data[i] = this.m_data[i];
  1251. }
  1252. }
  1253. this.m_data = tmp_data;
  1254. }
  1255. this.m_data[this.m_size++] = value;
  1256. };
  1257. // CompressedTriangleFans class
  1258. module.CompressedTriangleFans = function () {
  1259. this.m_numTFANs = new module.Vector();
  1260. this.m_degrees = new module.Vector();
  1261. this.m_configs = new module.Vector();
  1262. this.m_operations = new module.Vector();
  1263. this.m_indices = new module.Vector();
  1264. this.m_trianglesOrder = new module.Vector();
  1265. this.m_streamType = local.O3DGC_STREAM_TYPE_UNKOWN;
  1266. };
  1267. module.CompressedTriangleFans.prototype.GetStreamType = function () {
  1268. return this.m_streamType;
  1269. };
  1270. module.CompressedTriangleFans.prototype.SetStreamType = function (streamType) {
  1271. this.m_streamType = streamType;
  1272. };
  1273. module.CompressedTriangleFans.prototype.Clear = function () {
  1274. this.m_numTFANs.Clear();
  1275. this.m_degrees.Clear();
  1276. this.m_configs.Clear();
  1277. this.m_operations.Clear();
  1278. this.m_indices.Clear();
  1279. return module.O3DGC_OK;
  1280. };
  1281. module.CompressedTriangleFans.prototype.Allocate = function (numVertices, numTriangles) {
  1282. this.m_numTFANs.Allocate(numVertices);
  1283. this.m_degrees.Allocate(2 * numVertices);
  1284. this.m_configs.Allocate(2 * numVertices);
  1285. this.m_operations.Allocate(2 * numVertices);
  1286. this.m_indices.Allocate(2 * numVertices);
  1287. this.m_trianglesOrder.Allocate(numTriangles);
  1288. this.Clear();
  1289. return module.O3DGC_OK;
  1290. };
  1291. module.CompressedTriangleFans.prototype.PushNumTFans = function (numTFans) {
  1292. this.m_numTFANs.PushBack(numTFans);
  1293. };
  1294. module.CompressedTriangleFans.prototype.ReadNumTFans = function (it) {
  1295. return this.m_numTFANs.Get(it.m_count++);
  1296. };
  1297. module.CompressedTriangleFans.prototype.PushDegree = function (degree) {
  1298. this.m_degrees.PushBack(degree);
  1299. };
  1300. module.CompressedTriangleFans.prototype.ReadDegree = function (it) {
  1301. return this.m_degrees.Get(it.m_count++);
  1302. };
  1303. module.CompressedTriangleFans.prototype.PushConfig = function (config) {
  1304. this.m_configs.PushBack(config);
  1305. };
  1306. module.CompressedTriangleFans.prototype.ReadConfig = function (it) {
  1307. return this.m_configs.Get(it.m_count++);
  1308. };
  1309. module.CompressedTriangleFans.prototype.PushOperation = function (op) {
  1310. this.m_operations.PushBack(op);
  1311. };
  1312. module.CompressedTriangleFans.prototype.ReadOperation = function (it) {
  1313. return this.m_operations.Get(it.m_count++);
  1314. };
  1315. module.CompressedTriangleFans.prototype.PushIndex = function (index) {
  1316. this.m_indices.PushBack(index);
  1317. };
  1318. module.CompressedTriangleFans.prototype.ReadIndex = function (it) {
  1319. return this.m_indices.Get(it.m_count++);
  1320. };
  1321. module.CompressedTriangleFans.prototype.PushTriangleIndex = function (index) {
  1322. this.m_trianglesOrder.PushBack(IntToUInt(index));
  1323. };
  1324. module.CompressedTriangleFans.prototype.ReadTriangleIndex = function (it) {
  1325. return UIntToInt(this.m_trianglesOrder.Get(it.m_count++));
  1326. };
  1327. module.CompressedTriangleFans.prototype.LoadUIntData = function (data, bstream, it) {
  1328. var size, i;
  1329. bstream.ReadUInt32ASCII(it);
  1330. size = bstream.ReadUInt32ASCII(it);
  1331. data.Allocate(size);
  1332. data.Clear();
  1333. for (i = 0; i < size; ++i) {
  1334. data.PushBack(bstream.ReadUIntASCII(it));
  1335. }
  1336. return module.O3DGC_OK;
  1337. };
  1338. module.CompressedTriangleFans.prototype.LoadIntData = function (data, bstream, it) {
  1339. var size, i;
  1340. bstream.ReadUInt32ASCII(it);
  1341. size = bstream.ReadUInt32ASCII(it);
  1342. data.Allocate(size);
  1343. data.Clear();
  1344. for (i = 0; i < size; ++i) {
  1345. data.PushBack(bstream.ReadIntASCII(it));
  1346. }
  1347. return module.O3DGC_OK;
  1348. };
  1349. module.CompressedTriangleFans.prototype.LoadBinData = function (data, bstream, it) {
  1350. var size, symbol, i, h;
  1351. bstream.ReadUInt32ASCII(it);
  1352. size = bstream.ReadUInt32ASCII(it);
  1353. data.Allocate(size * local.O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0);
  1354. data.Clear();
  1355. i = 0;
  1356. while (i < size) {
  1357. symbol = bstream.ReadUCharASCII(it);
  1358. for (h = 0; h < local.O3DGC_BINARY_STREAM_BITS_PER_SYMBOL0; ++h) {
  1359. data.PushBack(symbol & 1);
  1360. symbol >>>= 1;
  1361. ++i;
  1362. }
  1363. }
  1364. return module.O3DGC_OK;
  1365. };
  1366. module.CompressedTriangleFans.prototype.LoadUIntAC = function (data, M, bstream, it) {
  1367. var sizeSize, size, minValue, buffer, acd, mModelValues, i;
  1368. sizeSize = bstream.ReadUInt32Bin(it) - 12;
  1369. size = bstream.ReadUInt32Bin(it);
  1370. if (size === 0) {
  1371. return module.O3DGC_OK;
  1372. }
  1373. minValue = bstream.ReadUInt32Bin(it);
  1374. buffer = bstream.GetBuffer(it, sizeSize);
  1375. it.m_count += sizeSize;
  1376. data.Allocate(size);
  1377. acd = new module.ArithmeticDecoder();
  1378. acd.SetBuffer(sizeSize, buffer);
  1379. acd.StartDecoder();
  1380. mModelValues = new module.AdaptiveDataModel();
  1381. mModelValues.SetAlphabet(M + 1);
  1382. for (i = 0; i < size; ++i) {
  1383. data.PushBack(acd.DecodeAdaptiveDataModel(mModelValues) + minValue);
  1384. }
  1385. return module.O3DGC_OK;
  1386. };
  1387. module.CompressedTriangleFans.prototype.LoadIntACEGC = function (data, M, bstream, it) {
  1388. var sizeSize, size, minValue, buffer, acd, mModelValues, bModel0, bModel1, value, i;
  1389. sizeSize = bstream.ReadUInt32Bin(it) - 12;
  1390. size = bstream.ReadUInt32Bin(it);
  1391. if (size === 0) {
  1392. return module.O3DGC_OK;
  1393. }
  1394. minValue = bstream.ReadUInt32Bin(it) - local.O3DGC_MAX_LONG;
  1395. buffer = bstream.GetBuffer(it, sizeSize);
  1396. it.m_count += sizeSize;
  1397. data.Allocate(size);
  1398. acd = new module.ArithmeticDecoder();
  1399. acd.SetBuffer(sizeSize, buffer);
  1400. acd.StartDecoder();
  1401. mModelValues = new module.AdaptiveDataModel();
  1402. mModelValues.SetAlphabet(M + 2);
  1403. bModel0 = new module.StaticBitModel();
  1404. bModel1 = new module.AdaptiveBitModel();
  1405. for (i = 0; i < size; ++i) {
  1406. value = acd.DecodeAdaptiveDataModel(mModelValues);
  1407. if (value === M) {
  1408. value += acd.ExpGolombDecode(0, bModel0, bModel1);
  1409. }
  1410. data.PushBack(value + minValue);
  1411. }
  1412. return module.O3DGC_OK;
  1413. };
  1414. module.CompressedTriangleFans.prototype.LoadBinAC = function (data, bstream, it) {
  1415. var sizeSize, size, buffer, acd, bModel, i;
  1416. sizeSize = bstream.ReadUInt32Bin(it) - 8;
  1417. size = bstream.ReadUInt32Bin(it);
  1418. if (size === 0) {
  1419. return module.O3DGC_OK;
  1420. }
  1421. buffer = bstream.GetBuffer(it, sizeSize);
  1422. it.m_count += sizeSize;
  1423. data.Allocate(size);
  1424. acd = new module.ArithmeticDecoder();
  1425. acd.SetBuffer(sizeSize, buffer);
  1426. acd.StartDecoder();
  1427. bModel = new module.AdaptiveBitModel();
  1428. for (i = 0; i < size; ++i) {
  1429. data.PushBack(acd.DecodeAdaptiveBitModel(bModel));
  1430. }
  1431. return module.O3DGC_OK;
  1432. };
  1433. module.CompressedTriangleFans.prototype.Load = function (bstream, iterator, decodeTrianglesOrder, streamType) {
  1434. if (streamType === local.O3DGC_STREAM_TYPE_ASCII) {
  1435. this.LoadUIntData(this.m_numTFANs, bstream, iterator);
  1436. this.LoadUIntData(this.m_degrees, bstream, iterator);
  1437. this.LoadUIntData(this.m_configs, bstream, iterator);
  1438. this.LoadBinData(this.m_operations, bstream, iterator);
  1439. this.LoadIntData(this.m_indices, bstream, iterator);
  1440. if (decodeTrianglesOrder) {
  1441. this.LoadUIntData(this.m_trianglesOrder, bstream, iterator);
  1442. }
  1443. } else {
  1444. this.LoadIntACEGC(this.m_numTFANs, 4, bstream, iterator);
  1445. this.LoadIntACEGC(this.m_degrees, 16, bstream, iterator);
  1446. this.LoadUIntAC(this.m_configs, 10, bstream, iterator);
  1447. this.LoadBinAC(this.m_operations, bstream, iterator);
  1448. this.LoadIntACEGC(this.m_indices, 8, bstream, iterator);
  1449. if (decodeTrianglesOrder) {
  1450. this.LoadIntACEGC(this.m_trianglesOrder, 16, bstream, iterator);
  1451. }
  1452. }
  1453. return module.O3DGC_OK;
  1454. };
  1455. // TriangleFans class
  1456. module.TriangleFans = function () {
  1457. this.m_verticesAllocatedSize = 0;
  1458. this.m_sizeTFANAllocatedSize = 0;
  1459. this.m_numTFANs = 0;
  1460. this.m_numVertices = 0;
  1461. this.m_sizeTFAN = {};
  1462. this.m_vertices = {};
  1463. };
  1464. module.TriangleFans.prototype.Allocate = function (sizeTFAN, verticesSize) {
  1465. this.m_numTFANs = 0;
  1466. this.m_numVertices = 0;
  1467. if (this.m_verticesAllocatedSize < verticesSize) {
  1468. this.m_verticesAllocatedSize = verticesSize;
  1469. this.m_vertices = new Int32Array(this.m_verticesAllocatedSize);
  1470. }
  1471. if (this.m_sizeTFANAllocatedSize < sizeTFAN) {
  1472. this.m_sizeTFANAllocatedSize = sizeTFAN;
  1473. this.m_sizeTFAN = new Int32Array(this.m_sizeTFANAllocatedSize);
  1474. }
  1475. return module.O3DGC_OK;
  1476. };
  1477. module.TriangleFans.prototype.Clear = function () {
  1478. this.m_numTFANs = 0;
  1479. this.m_numVertices = 0;
  1480. return module.O3DGC_OK;
  1481. };
  1482. module.TriangleFans.prototype.AddVertex = function (vertex) {
  1483. var i, tmp_vertices;
  1484. ++this.m_numVertices;
  1485. if (this.m_numVertices > this.m_verticesAllocatedSize) {
  1486. this.m_verticesAllocatedSize *= 2;
  1487. tmp_vertices = new Int32Array(this.m_verticesAllocatedSize);
  1488. for (i = 0; i < this.m_numVertices; ++i) {
  1489. tmp_vertices[i] = this.m_vertices[i];
  1490. }
  1491. this.m_vertices = tmp_vertices;
  1492. }
  1493. this.m_vertices[this.m_numVertices - 1] = vertex;
  1494. ++this.m_sizeTFAN[this.m_numTFANs - 1];
  1495. return module.O3DGC_OK;
  1496. };
  1497. module.TriangleFans.prototype.AddTFAN = function () {
  1498. var i, tmp_sizeTFAN;
  1499. ++this.m_numTFANs;
  1500. if (this.m_numTFANs > this.m_sizeTFANAllocatedSize) {
  1501. this.m_sizeTFANAllocatedSize *= 2;
  1502. tmp_sizeTFAN = new Int32Array(this.m_sizeTFANAllocatedSize);
  1503. for (i = 0; i < this.m_numTFANs; ++i) {
  1504. tmp_sizeTFAN[i] = this.m_sizeTFAN[i];
  1505. }
  1506. this.m_sizeTFAN = tmp_sizeTFAN;
  1507. }
  1508. this.m_sizeTFAN[this.m_numTFANs - 1] = (this.m_numTFANs > 1) ? this.m_sizeTFAN[this.m_numTFANs - 2] : 0;
  1509. return module.O3DGC_OK;
  1510. };
  1511. module.TriangleFans.prototype.Begin = function (tfan) {
  1512. return (tfan > 0) ? this.m_sizeTFAN[tfan - 1] : 0;
  1513. };
  1514. module.TriangleFans.prototype.End = function (tfan) {
  1515. return this.m_sizeTFAN[tfan];
  1516. };
  1517. module.TriangleFans.prototype.GetVertex = function (vertex) {
  1518. return this.m_vertices[vertex];
  1519. };
  1520. module.TriangleFans.prototype.GetTFANSize = function (tfan) {
  1521. return this.End(tfan) - this.Begin(tfan);
  1522. };
  1523. module.TriangleFans.prototype.GetNumTFANs = function () {
  1524. return this.m_numTFANs;
  1525. };
  1526. module.TriangleFans.prototype.GetNumVertices = function () {
  1527. return this.m_numVertices;
  1528. };
  1529. // TriangleListDecoder class
  1530. module.TriangleListDecoder = function () {
  1531. this.m_itNumTFans = new module.Iterator();
  1532. this.m_itDegree = new module.Iterator();
  1533. this.m_itConfig = new module.Iterator();
  1534. this.m_itOperation = new module.Iterator();
  1535. this.m_itIndex = new module.Iterator();
  1536. this.m_maxNumVertices = 0;
  1537. this.m_maxNumTriangles = 0;
  1538. this.m_numTriangles = 0;
  1539. this.m_numVertices = 0;
  1540. this.m_tempTrianglesSize = 0;
  1541. this.m_vertexCount = 0;
  1542. this.m_triangleCount = 0;
  1543. this.m_numConqueredTriangles = 0;
  1544. this.m_numVisitedVertices = 0;
  1545. this.m_triangles = {};
  1546. this.m_tempTriangles = {};
  1547. this.m_visitedVertices = {};
  1548. this.m_visitedVerticesValence = {};
  1549. this.m_vertexToTriangle = new module.AdjacencyInfo();
  1550. this.m_ctfans = new module.CompressedTriangleFans();
  1551. this.m_tfans = new module.TriangleFans();
  1552. this.m_streamType = local.O3DGC_STREAM_TYPE_ASCII;
  1553. this.m_decodeTrianglesOrder = false;
  1554. this.m_decodeVerticesOrder = false;
  1555. this.m_processConfig = {
  1556. 0: function (decoder, degree) { // ops: 1000001 vertices: -1 -2
  1557. var u;
  1558. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[0]);
  1559. for (u = 1; u < degree - 1; ++u) {
  1560. decoder.m_visitedVertices[decoder.m_numVisitedVertices++] = decoder.m_vertexCount;
  1561. decoder.m_tfans.AddVertex(decoder.m_vertexCount++);
  1562. }
  1563. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[1]);
  1564. },
  1565. 1: function (decoder, degree, focusVertex) { // ops: 1xxxxxx1 vertices: -1 x x x x x -2
  1566. var u, op, index;
  1567. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[0]);
  1568. for (u = 1; u < degree - 1; ++u) {
  1569. op = decoder.m_ctfans.ReadOperation(decoder.m_itOperation);
  1570. if (op === 1) {
  1571. index = decoder.m_ctfans.ReadIndex(decoder.m_itIndex);
  1572. if (index < 0) {
  1573. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[-index - 1]);
  1574. } else {
  1575. decoder.m_tfans.AddVertex(index + focusVertex);
  1576. }
  1577. } else {
  1578. decoder.m_visitedVertices[decoder.m_numVisitedVertices++] = decoder.m_vertexCount;
  1579. decoder.m_tfans.AddVertex(decoder.m_vertexCount++);
  1580. }
  1581. }
  1582. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[1]);
  1583. },
  1584. 2: function (decoder, degree) { // ops: 00000001 vertices: -1
  1585. var u;
  1586. for (u = 0; u < degree - 1; ++u) {
  1587. decoder.m_visitedVertices[decoder.m_numVisitedVertices++] = decoder.m_vertexCount;
  1588. decoder.m_tfans.AddVertex(decoder.m_vertexCount++);
  1589. }
  1590. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[0]);
  1591. },
  1592. 3: function (decoder, degree) { // ops: 00000001 vertices: -2
  1593. var u;
  1594. for (u = 0; u < degree - 1; ++u) {
  1595. decoder.m_visitedVertices[decoder.m_numVisitedVertices++] = decoder.m_vertexCount;
  1596. decoder.m_tfans.AddVertex(decoder.m_vertexCount++);
  1597. }
  1598. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[1]);
  1599. },
  1600. 4: function (decoder, degree) {// ops: 10000000 vertices: -1
  1601. var u;
  1602. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[0]);
  1603. for (u = 1; u < degree; ++u) {
  1604. decoder.m_visitedVertices[decoder.m_numVisitedVertices++] = decoder.m_vertexCount;
  1605. decoder.m_tfans.AddVertex(decoder.m_vertexCount++);
  1606. }
  1607. },
  1608. 5: function (decoder, degree) { // ops: 10000000 vertices: -2
  1609. var u;
  1610. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[1]);
  1611. for (u = 1; u < degree; ++u) {
  1612. decoder.m_visitedVertices[decoder.m_numVisitedVertices++] = decoder.m_vertexCount;
  1613. decoder.m_tfans.AddVertex(decoder.m_vertexCount++);
  1614. }
  1615. },
  1616. 6: function (decoder, degree) { // ops: 00000000 vertices:
  1617. var u;
  1618. for (u = 0; u < degree; ++u) {
  1619. decoder.m_visitedVertices[decoder.m_numVisitedVertices++] = decoder.m_vertexCount;
  1620. decoder.m_tfans.AddVertex(decoder.m_vertexCount++);
  1621. }
  1622. },
  1623. 7: function (decoder, degree) { // ops: 1000001 vertices: -2 -1
  1624. var u;
  1625. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[1]);
  1626. for (u = 1; u < degree - 1; ++u) {
  1627. decoder.m_visitedVertices[decoder.m_numVisitedVertices++] = decoder.m_vertexCount;
  1628. decoder.m_tfans.AddVertex(decoder.m_vertexCount++);
  1629. }
  1630. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[0]);
  1631. },
  1632. 8: function (decoder, degree, focusVertex) { // ops: 1xxxxxx1 vertices: -2 x x x x x -1
  1633. var u, op, index;
  1634. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[1]);
  1635. for (u = 1; u < degree - 1; ++u) {
  1636. op = decoder.m_ctfans.ReadOperation(decoder.m_itOperation);
  1637. if (op === 1) {
  1638. index = decoder.m_ctfans.ReadIndex(decoder.m_itIndex);
  1639. if (index < 0) {
  1640. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[-index - 1]);
  1641. } else {
  1642. decoder.m_tfans.AddVertex(index + focusVertex);
  1643. }
  1644. } else {
  1645. decoder.m_visitedVertices[decoder.m_numVisitedVertices++] = decoder.m_vertexCount;
  1646. decoder.m_tfans.AddVertex(decoder.m_vertexCount++);
  1647. }
  1648. }
  1649. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[0]);
  1650. },
  1651. 9: function (decoder, degree, focusVertex) { // general case
  1652. var u, op, index;
  1653. for (u = 0; u < degree; ++u) {
  1654. op = decoder.m_ctfans.ReadOperation(decoder.m_itOperation);
  1655. if (op === 1) {
  1656. index = decoder.m_ctfans.ReadIndex(decoder.m_itIndex);
  1657. if (index < 0) {
  1658. decoder.m_tfans.AddVertex(decoder.m_visitedVertices[-index - 1]);
  1659. } else {
  1660. decoder.m_tfans.AddVertex(index + focusVertex);
  1661. }
  1662. } else {
  1663. decoder.m_visitedVertices[decoder.m_numVisitedVertices++] = decoder.m_vertexCount;
  1664. decoder.m_tfans.AddVertex(decoder.m_vertexCount++);
  1665. }
  1666. }
  1667. }
  1668. };
  1669. };
  1670. module.TriangleListDecoder.prototype.GetStreamType = function () {
  1671. return this.m_streamType;
  1672. };
  1673. module.TriangleListDecoder.prototype.GetReorderTriangles = function () {
  1674. return this.m_decodeTrianglesOrder;
  1675. };
  1676. module.TriangleListDecoder.prototype.GetReorderVertices = function () {
  1677. return this.m_decodeVerticesOrder;
  1678. };
  1679. module.TriangleListDecoder.prototype.SetStreamType = function (streamType) {
  1680. this.m_streamType = streamType;
  1681. };
  1682. module.TriangleListDecoder.prototype.GetVertexToTriangle = function () {
  1683. return this.m_vertexToTriangle;
  1684. };
  1685. module.TriangleListDecoder.prototype.Reorder = function () {
  1686. var triangles, numTriangles, order, it, prevTriangleIndex, tempTriangles, t, i;
  1687. if (this.m_decodeTrianglesOrder) {
  1688. triangles = this.m_triangles;
  1689. numTriangles = this.m_numTriangles;
  1690. order = this.m_ctfans.m_trianglesOrder.m_data;
  1691. tempTriangles = this.m_tempTriangles;
  1692. tempTriangles.set(triangles);
  1693. it = 0;
  1694. prevTriangleIndex = 0;
  1695. for (i = 0; i < numTriangles; ++i) {
  1696. t = UIntToInt(order[it++]) + prevTriangleIndex;
  1697. triangles[3 * t] = tempTriangles[3 * i];
  1698. triangles[3 * t + 1] = tempTriangles[3 * i + 1];
  1699. triangles[3 * t + 2] = tempTriangles[3 * i + 2];
  1700. prevTriangleIndex = t + 1;
  1701. }
  1702. }
  1703. return module.O3DGC_OK;
  1704. };
  1705. module.TriangleListDecoder.prototype.CompueLocalConnectivityInfo = function (focusVertex) {
  1706. var visitedVertices, visitedVerticesValence, triangles, vertexToTriangle, beginV2T, endV2T, numConqueredTriangles, foundOrInserted, numVisitedVertices, tmp, i, j, k, h, x, y, t, p, v;
  1707. visitedVertices = this.m_visitedVertices;
  1708. visitedVerticesValence = this.m_visitedVerticesValence;
  1709. triangles = this.m_triangles;
  1710. vertexToTriangle = this.m_vertexToTriangle;
  1711. beginV2T = vertexToTriangle.Begin(focusVertex);
  1712. endV2T = vertexToTriangle.End(focusVertex);
  1713. numConqueredTriangles = 0;
  1714. numVisitedVertices = 0;
  1715. t = 0;
  1716. for (i = beginV2T; (t >= 0) && (i < endV2T); ++i) {
  1717. t = vertexToTriangle.GetNeighbor(i);
  1718. if (t >= 0) {
  1719. ++numConqueredTriangles;
  1720. p = 3 * t;
  1721. // extract visited vertices
  1722. for (k = 0; k < 3; ++k) {
  1723. v = triangles[p + k];
  1724. if (v > focusVertex) { // vertices are insertices by increasing traversal order
  1725. foundOrInserted = false;
  1726. for (j = 0; j < numVisitedVertices; ++j) {
  1727. if (v === visitedVertices[j]) {
  1728. visitedVerticesValence[j]++;
  1729. foundOrInserted = true;
  1730. break;
  1731. } else if (v < visitedVertices[j]) {
  1732. ++numVisitedVertices;
  1733. for (h = numVisitedVertices - 1; h > j; --h) {
  1734. visitedVertices[h] = visitedVertices[h - 1];
  1735. visitedVerticesValence[h] = visitedVerticesValence[h - 1];
  1736. }
  1737. visitedVertices[j] = v;
  1738. visitedVerticesValence[j] = 1;
  1739. foundOrInserted = true;
  1740. break;
  1741. }
  1742. }
  1743. if (!foundOrInserted) {
  1744. visitedVertices[numVisitedVertices] = v;
  1745. visitedVerticesValence[numVisitedVertices] = 1;
  1746. numVisitedVertices++;
  1747. }
  1748. }
  1749. }
  1750. }
  1751. }
  1752. // re-order visited vertices by taking into account their valence (i.e., # of conquered triangles incident to each vertex)
  1753. // in order to avoid config. 9
  1754. if (numVisitedVertices > 2) {
  1755. for (x = 1; x < numVisitedVertices; ++x) {
  1756. if (visitedVerticesValence[x] === 1) {
  1757. y = x;
  1758. while ((y > 0) && (visitedVerticesValence[y] < visitedVerticesValence[y - 1])) {
  1759. tmp = visitedVerticesValence[y];
  1760. visitedVerticesValence[y] = visitedVerticesValence[y - 1];
  1761. visitedVerticesValence[y - 1] = tmp;
  1762. tmp = visitedVertices[y];
  1763. visitedVertices[y] = visitedVertices[y - 1];
  1764. visitedVertices[y - 1] = tmp;
  1765. --y;
  1766. }
  1767. }
  1768. }
  1769. }
  1770. this.m_numConqueredTriangles = numConqueredTriangles;
  1771. this.m_numVisitedVertices = numVisitedVertices;
  1772. return module.O3DGC_OK;
  1773. };
  1774. module.TriangleListDecoder.prototype.DecompressTFAN = function (focusVertex) {
  1775. var vertexToTriangle, triangles, itDegree, itConfig, tfans, ntfans, processConfig, ctfans, triangleCount, numConqueredTriangles, degree, config, k0, k1, b, c, t, f, k;
  1776. vertexToTriangle = this.m_vertexToTriangle;
  1777. triangles = this.m_triangles;
  1778. itDegree = this.m_itDegree;
  1779. itConfig = this.m_itConfig;
  1780. tfans = this.m_tfans;
  1781. processConfig = this.m_processConfig;
  1782. ctfans = this.m_ctfans;
  1783. triangleCount = this.m_triangleCount;
  1784. numConqueredTriangles = this.m_numConqueredTriangles;
  1785. ntfans = ctfans.ReadNumTFans(this.m_itNumTFans);
  1786. if (ntfans > 0) {
  1787. for (f = 0; f < ntfans; ++f) {
  1788. tfans.AddTFAN();
  1789. degree = ctfans.ReadDegree(itDegree) + 2 - numConqueredTriangles;
  1790. config = ctfans.ReadConfig(itConfig);
  1791. k0 = tfans.GetNumVertices();
  1792. tfans.AddVertex(focusVertex);
  1793. processConfig[config](this, degree, focusVertex);
  1794. k1 = tfans.GetNumVertices();
  1795. b = tfans.GetVertex(k0 + 1);
  1796. for (k = k0 + 2; k < k1; ++k) {
  1797. c = tfans.GetVertex(k);
  1798. t = triangleCount * 3;
  1799. triangles[t++] = focusVertex;
  1800. triangles[t++] = b;
  1801. triangles[t] = c;
  1802. vertexToTriangle.AddNeighbor(focusVertex, triangleCount);
  1803. vertexToTriangle.AddNeighbor(b, triangleCount);
  1804. vertexToTriangle.AddNeighbor(c, triangleCount);
  1805. b = c;
  1806. triangleCount++;
  1807. }
  1808. }
  1809. }
  1810. this.m_triangleCount = triangleCount;
  1811. return module.O3DGC_OK;
  1812. };
  1813. module.TriangleListDecoder.prototype.Decompress = function () {
  1814. var focusVertex;
  1815. for (focusVertex = 0; focusVertex < this.m_numVertices; ++focusVertex) {
  1816. if (focusVertex === this.m_vertexCount) {
  1817. this.m_vertexCount++; // insert focusVertex
  1818. }
  1819. this.CompueLocalConnectivityInfo(focusVertex);
  1820. this.DecompressTFAN(focusVertex);
  1821. }
  1822. return module.O3DGC_OK;
  1823. };
  1824. module.TriangleListDecoder.prototype.Init = function (triangles, numTriangles, numVertices, maxSizeV2T) {
  1825. var i, numNeighbors;
  1826. this.m_numTriangles = numTriangles;
  1827. this.m_numVertices = numVertices;
  1828. this.m_triangles = triangles;
  1829. this.m_vertexCount = 0;
  1830. this.m_triangleCount = 0;
  1831. this.m_itNumTFans.m_count = 0;
  1832. this.m_itDegree.m_count = 0;
  1833. this.m_itConfig.m_count = 0;
  1834. this.m_itOperation.m_count = 0;
  1835. this.m_itIndex.m_count = 0;
  1836. if (this.m_numVertices > this.m_maxNumVertices) {
  1837. this.m_maxNumVertices = this.m_numVertices;
  1838. this.m_visitedVerticesValence = new Int32Array(this.m_numVertices);
  1839. this.m_visitedVertices = new Int32Array(this.m_numVertices);
  1840. }
  1841. if (this.m_decodeTrianglesOrder && this.m_tempTrianglesSize < this.m_numTriangles) {
  1842. this.m_tempTrianglesSize = this.m_numTriangles;
  1843. this.m_tempTriangles = new Int32Array(3 * this.m_tempTrianglesSize);
  1844. }
  1845. this.m_ctfans.SetStreamType(this.m_streamType);
  1846. this.m_ctfans.Allocate(this.m_numVertices, this.m_numTriangles);
  1847. this.m_tfans.Allocate(2 * this.m_numVertices, 8 * this.m_numVertices);
  1848. // compute vertex-to-triangle adjacency information
  1849. this.m_vertexToTriangle.AllocateNumNeighborsArray(numVertices);
  1850. numNeighbors = this.m_vertexToTriangle.GetNumNeighborsBuffer();
  1851. for (i = 0; i < numVertices; ++i) {
  1852. numNeighbors[i] = maxSizeV2T;
  1853. }
  1854. this.m_vertexToTriangle.AllocateNeighborsArray();
  1855. this.m_vertexToTriangle.ClearNeighborsArray();
  1856. return module.O3DGC_OK;
  1857. };
  1858. module.TriangleListDecoder.prototype.Decode = function (triangles, numTriangles, numVertices, bstream, it) {
  1859. var compressionMask, maxSizeV2T;
  1860. compressionMask = bstream.ReadUChar(it, this.m_streamType);
  1861. this.m_decodeTrianglesOrder = ((compressionMask & 2) !== 0);
  1862. this.m_decodeVerticesOrder = ((compressionMask & 1) !== 0);
  1863. if (this.m_decodeVerticesOrder) { // vertices reordering not supported
  1864. return module.O3DGC_ERROR_NON_SUPPORTED_FEATURE;
  1865. }
  1866. maxSizeV2T = bstream.ReadUInt32(it, this.m_streamType);
  1867. this.Init(triangles, numTriangles, numVertices, maxSizeV2T);
  1868. this.m_ctfans.Load(bstream, it, this.m_decodeTrianglesOrder, this.m_streamType);
  1869. this.Decompress();
  1870. return module.O3DGC_OK;
  1871. };
  1872. // SC3DMCDecoder class
  1873. module.SC3DMCDecoder = function () {
  1874. var i;
  1875. this.m_iterator = new module.Iterator();
  1876. this.m_streamSize = 0;
  1877. this.m_params = new module.SC3DMCEncodeParams();
  1878. this.m_triangleListDecoder = new module.TriangleListDecoder();
  1879. this.m_quantFloatArray = {};
  1880. this.m_orientation = {};
  1881. this.m_normals = {};
  1882. this.m_quantFloatArraySize = 0;
  1883. this.m_normalsSize = 0;
  1884. this.m_orientationSize = 0;
  1885. this.m_stats = new module.SC3DMCStats();
  1886. this.m_streamType = local.O3DGC_STREAM_TYPE_UNKOWN;
  1887. this.m_neighbors = [];
  1888. this.m_idelta = new Float32Array(local.O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES);
  1889. this.m_minNormal = new Float32Array(2);
  1890. this.m_maxNormal = new Float32Array(2);
  1891. this.m_minNormal[0] = this.m_minNormal[1] = -2;
  1892. this.m_maxNormal[0] = this.m_maxNormal[1] = 2;
  1893. for (i = 0; i < local.O3DGC_SC3DMC_MAX_DIM_ATTRIBUTES; ++i) {
  1894. this.m_neighbors[i] = new module.SC3DMCPredictor();
  1895. }
  1896. };
  1897. module.SC3DMCDecoder.prototype.GetStats = function () {
  1898. return this.m_stats;
  1899. };
  1900. module.SC3DMCDecoder.prototype.DecodeHeader = function (ifs, bstream) {
  1901. var c0, start_code, mask, j, a, d;
  1902. c0 = this.m_iterator.m_count;
  1903. start_code = bstream.ReadUInt32(this.m_iterator, local.O3DGC_STREAM_TYPE_BINARY);
  1904. if (start_code !== local.O3DGC_SC3DMC_START_CODE) {
  1905. this.m_iterator.m_count = c0;
  1906. start_code = bstream.ReadUInt32(this.m_iterator, local.O3DGC_STREAM_TYPE_ASCII);
  1907. if (start_code !== local.O3DGC_SC3DMC_START_CODE) {
  1908. return module.O3DGC_ERROR_CORRUPTED_STREAM;
  1909. }
  1910. this.m_streamType = local.O3DGC_STREAM_TYPE_ASCII;
  1911. } else {
  1912. this.m_streamType = local.O3DGC_STREAM_TYPE_BINARY;
  1913. }
  1914. this.m_streamSize = bstream.ReadUInt32(this.m_iterator, this.m_streamType);
  1915. this.m_params.SetEncodeMode(bstream.ReadUChar(this.m_iterator, this.m_streamType));
  1916. ifs.SetCreaseAngle(bstream.ReadFloat32(this.m_iterator, this.m_streamType));
  1917. mask = bstream.ReadUChar(this.m_iterator, this.m_streamType);
  1918. ifs.SetCCW((mask & 1) === 1);
  1919. ifs.SetSolid((mask & 2) === 1);
  1920. ifs.SetConvex((mask & 4) === 1);
  1921. ifs.SetIsTriangularMesh((mask & 8) === 1);
  1922. ifs.SetNCoord(bstream.ReadUInt32(this.m_iterator, this.m_streamType));
  1923. ifs.SetNNormal(bstream.ReadUInt32(this.m_iterator, this.m_streamType));
  1924. ifs.SetNumFloatAttributes(bstream.ReadUInt32(this.m_iterator, this.m_streamType));
  1925. ifs.SetNumIntAttributes(bstream.ReadUInt32(this.m_iterator, this.m_streamType));
  1926. if (ifs.GetNCoord() > 0) {
  1927. ifs.SetNCoordIndex(bstream.ReadUInt32(this.m_iterator, this.m_streamType));
  1928. for (j = 0; j < 3; ++j) {
  1929. ifs.SetCoordMin(j, bstream.ReadFloat32(this.m_iterator, this.m_streamType));
  1930. ifs.SetCoordMax(j, bstream.ReadFloat32(this.m_iterator, this.m_streamType));
  1931. }
  1932. this.m_params.SetCoordQuantBits(bstream.ReadUChar(this.m_iterator, this.m_streamType));
  1933. }
  1934. if (ifs.GetNNormal() > 0) {
  1935. ifs.SetNNormalIndex(bstream.ReadUInt32(this.m_iterator, this.m_streamType));
  1936. for (j = 0; j < 3; ++j) {
  1937. ifs.SetNormalMin(j, bstream.ReadFloat32(this.m_iterator, this.m_streamType));
  1938. ifs.SetNormalMax(j, bstream.ReadFloat32(this.m_iterator, this.m_streamType));
  1939. }
  1940. ifs.SetNormalPerVertex(bstream.ReadUChar(this.m_iterator, this.m_streamType) === 1);
  1941. this.m_params.SetNormalQuantBits(bstream.ReadUChar(this.m_iterator, this.m_streamType));
  1942. }
  1943. for (a = 0; a < ifs.GetNumFloatAttributes(); ++a) {
  1944. ifs.SetNFloatAttribute(a, bstream.ReadUInt32(this.m_iterator, this.m_streamType));
  1945. if (ifs.GetNFloatAttribute(a) > 0) {
  1946. ifs.SetNFloatAttributeIndex(a, bstream.ReadUInt32(this.m_iterator, this.m_streamType));
  1947. d = bstream.ReadUChar(this.m_iterator, this.m_streamType);
  1948. ifs.SetFloatAttributeDim(a, d);
  1949. for (j = 0; j < d; ++j) {
  1950. ifs.SetFloatAttributeMin(a, j, bstream.ReadFloat32(this.m_iterator, this.m_streamType));
  1951. ifs.SetFloatAttributeMax(a, j, bstream.ReadFloat32(this.m_iterator, this.m_streamType));
  1952. }
  1953. ifs.SetFloatAttributePerVertex(a, bstream.ReadUChar(this.m_iterator, this.m_streamType) === 1);
  1954. ifs.SetFloatAttributeType(a, bstream.ReadUChar(this.m_iterator, this.m_streamType));
  1955. this.m_params.SetFloatAttributeQuantBits(a, bstream.ReadUChar(this.m_iterator, this.m_streamType));
  1956. }
  1957. }
  1958. for (a = 0; a < ifs.GetNumIntAttributes(); ++a) {
  1959. ifs.SetNIntAttribute(a, bstream.ReadUInt32(this.m_iterator, this.m_streamType));
  1960. if (ifs.GetNIntAttribute(a) > 0) {
  1961. ifs.SetNIntAttributeIndex(a, bstream.ReadUInt32(this.m_iterator, this.m_streamType));
  1962. ifs.SetIntAttributeDim(a, bstream.ReadUChar(this.m_iterator, this.m_streamType));
  1963. ifs.SetIntAttributePerVertex(a, bstream.ReadUChar(this.m_iterator, this.m_streamType) === 1);
  1964. ifs.SetIntAttributeType(a, bstream.ReadUChar(this.m_iterator, this.m_streamType));
  1965. }
  1966. }
  1967. return module.O3DGC_OK;
  1968. };
  1969. function DeltaPredictors(triangles, ta, v, nPred, neighbors, dimFloatArray, quantFloatArray, stride) {
  1970. var ws, k, p, w, i, id;
  1971. id = new module.SC3DMCTriplet(-1, -1, -1);
  1972. for (k = 0; k < 3; ++k) {
  1973. w = triangles[ta * 3 + k];
  1974. if (w < v) {
  1975. id.m_a = -1;
  1976. id.m_b = -1;
  1977. id.m_c = w;
  1978. p = InsertPredictor(id, nPred, neighbors, dimFloatArray);
  1979. if (p !== -1) {
  1980. ws = w * stride;
  1981. for (i = 0; i < dimFloatArray; ++i) {
  1982. neighbors[p].m_pred[i] = quantFloatArray[ws + i];
  1983. }
  1984. }
  1985. }
  1986. }
  1987. }
  1988. function ParallelogramPredictors(triangles, ta, v, nPred, neighbors, dimFloatArray, quantFloatArray, stride, v2T, v2TNeighbors) {
  1989. var ta3, tb3, as, bs, cs, a, b, c, x, i, k, u1_begin, u1_end, u1, tb, foundB, p, id;
  1990. ta3 = ta * 3;
  1991. id = new module.SC3DMCTriplet(-1, -1, -1);
  1992. if (triangles[ta3] === v) {
  1993. a = triangles[ta3 + 1];
  1994. b = triangles[ta3 + 2];
  1995. } else if (triangles[ta3 + 1] === v) {
  1996. a = triangles[ta3];
  1997. b = triangles[ta3 + 2];
  1998. } else {
  1999. a = triangles[ta3];
  2000. b = triangles[ta3 + 1];
  2001. }
  2002. if (a < v && b < v) {
  2003. u1_begin = v2T.Begin(a);
  2004. u1_end = v2T.End(a);
  2005. for (u1 = u1_begin; u1 < u1_end; ++u1) {
  2006. tb = v2TNeighbors[u1];
  2007. if (tb < 0) {
  2008. break;
  2009. }
  2010. tb3 = tb * 3;
  2011. c = -1;
  2012. foundB = false;
  2013. for (k = 0; k < 3; ++k) {
  2014. x = triangles[tb3 + k];
  2015. if (x === b) {
  2016. foundB = true;
  2017. } else if (x < v && x !== a) {
  2018. c = x;
  2019. }
  2020. }
  2021. if (c !== -1 && foundB) {
  2022. if (a < b) {
  2023. id.m_a = a;
  2024. id.m_b = b;
  2025. } else {
  2026. id.m_a = b;
  2027. id.m_b = a;
  2028. }
  2029. id.m_c = (-c - 1);
  2030. p = InsertPredictor(id, nPred, neighbors, dimFloatArray);
  2031. if (p !== -1) {
  2032. as = a * stride;
  2033. bs = b * stride;
  2034. cs = c * stride;
  2035. for (i = 0; i < dimFloatArray; ++i) {
  2036. neighbors[p].m_pred[i] = quantFloatArray[as + i] + quantFloatArray[bs + i] - quantFloatArray[cs + i];
  2037. }
  2038. }
  2039. }
  2040. }
  2041. }
  2042. }
  2043. module.SC3DMCDecoder.prototype.DecodeIntArrayBinary = function (intArray,
  2044. numIntArray,
  2045. dimIntArray,
  2046. stride,
  2047. ifs,
  2048. predMode,
  2049. bstream) {
  2050. var testPredEnabled, bestPred, i, u, ta, u_begin, u_end, buffer, iterator, streamType, predResidual, acd, bModel0, bModel1, mModelPreds, v2T, v2TNeighbors, triangles, size, start, streamSize, mask, binarization, iteratorPred, exp_k, M, id, mModelValues, neighbors, normals, nPred, v;
  2051. iterator = this.m_iterator;
  2052. streamType = this.m_streamType;
  2053. acd = new module.ArithmeticDecoder();
  2054. bModel0 = new module.StaticBitModel();
  2055. bModel1 = new module.AdaptiveBitModel();
  2056. mModelPreds = new module.AdaptiveDataModel();
  2057. mModelPreds.SetAlphabet(local.O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS + 1);
  2058. v2T = this.m_triangleListDecoder.GetVertexToTriangle();
  2059. v2TNeighbors = v2T.m_neighbors;
  2060. triangles = ifs.GetCoordIndex();
  2061. size = numIntArray * dimIntArray;
  2062. start = iterator.m_count;
  2063. streamSize = bstream.ReadUInt32(iterator, streamType); // bitsream size
  2064. mask = bstream.ReadUChar(iterator, streamType);
  2065. binarization = (mask >>> 4) & 7;
  2066. predMode.m_value = mask & 7;
  2067. streamSize -= (iterator.m_count - start);
  2068. iteratorPred = new module.Iterator();
  2069. iteratorPred.m_count = iterator.m_count + streamSize;
  2070. exp_k = 0;
  2071. M = 0;
  2072. id = new module.SC3DMCTriplet(-1, -1, -1);
  2073. if (binarization !== local.O3DGC_SC3DMC_BINARIZATION_AC_EGC) {
  2074. return module.O3DGC_ERROR_CORRUPTED_STREAM;
  2075. }
  2076. buffer = bstream.GetBuffer(iterator, streamSize);
  2077. iterator.m_count += streamSize;
  2078. acd.SetBuffer(streamSize, buffer);
  2079. acd.StartDecoder();
  2080. exp_k = acd.ExpGolombDecode(0, bModel0, bModel1);
  2081. M = acd.ExpGolombDecode(0, bModel0, bModel1);
  2082. mModelValues = new module.AdaptiveDataModel();
  2083. mModelValues.SetAlphabet(M + 2);
  2084. neighbors = this.m_neighbors;
  2085. normals = this.m_normals;
  2086. nPred = new module.NumberRef();
  2087. testPredEnabled = predMode.m_value !== local.O3DGC_SC3DMC_NO_PREDICTION;
  2088. for (v = 0; v < numIntArray; ++v) {
  2089. nPred.m_value = 0;
  2090. if (v2T.GetNumNeighbors(v) > 0 && testPredEnabled) {
  2091. u_begin = v2T.Begin(v);
  2092. u_end = v2T.End(v);
  2093. for (u = u_begin; u < u_end; ++u) {
  2094. ta = v2TNeighbors[u];
  2095. if (ta < 0) {
  2096. break;
  2097. }
  2098. DeltaPredictors(triangles, ta, v, nPred, neighbors, dimIntArray, intArray, stride);
  2099. }
  2100. }
  2101. if (nPred.m_value > 1) {
  2102. bestPred = acd.DecodeAdaptiveDataModel(mModelPreds);
  2103. for (i = 0; i < dimIntArray; ++i) {
  2104. predResidual = acd.DecodeIntACEGC(mModelValues, bModel0, bModel1, exp_k, M);
  2105. intArray[v * stride + i] = predResidual + neighbors[bestPred].m_pred[i];
  2106. }
  2107. } else if (v > 0 && predMode.m_value !== local.O3DGC_SC3DMC_NO_PREDICTION) {
  2108. for (i = 0; i < dimIntArray; ++i) {
  2109. predResidual = acd.DecodeIntACEGC(mModelValues, bModel0, bModel1, exp_k, M);
  2110. intArray[v * stride + i] = predResidual + intArray[(v - 1) * stride + i];
  2111. }
  2112. } else {
  2113. for (i = 0; i < dimIntArray; ++i) {
  2114. predResidual = acd.DecodeUIntACEGC(mModelValues, bModel0, bModel1, exp_k, M);
  2115. intArray[v * stride + i] = predResidual;
  2116. }
  2117. }
  2118. }
  2119. iterator.m_count = iteratorPred.m_count;
  2120. return module.O3DGC_OK;
  2121. };
  2122. module.SC3DMCDecoder.prototype.DecodeIntArrayASCII = function (intArray,
  2123. numIntArray,
  2124. dimIntArray,
  2125. stride,
  2126. ifs,
  2127. predMode,
  2128. bstream) {
  2129. var testPredEnabled, iterator, streamType, predResidual, v2T, v2TNeighbors, triangles, size, start, streamSize, mask, binarization, iteratorPred, id, neighbors, normals, nPred, v, u_begin, u_end, u, ta, i, bestPred;
  2130. iterator = this.m_iterator;
  2131. streamType = this.m_streamType;
  2132. v2T = this.m_triangleListDecoder.GetVertexToTriangle();
  2133. v2TNeighbors = v2T.m_neighbors;
  2134. triangles = ifs.GetCoordIndex();
  2135. size = numIntArray * dimIntArray;
  2136. start = iterator.m_count;
  2137. streamSize = bstream.ReadUInt32(iterator, streamType); // bitsream size
  2138. mask = bstream.ReadUChar(iterator, streamType);
  2139. binarization = (mask >>> 4) & 7;
  2140. predMode.m_value = mask & 7;
  2141. streamSize -= (iterator.m_count - start);
  2142. iteratorPred = new module.Iterator();
  2143. iteratorPred.m_count = iterator.m_count + streamSize;
  2144. id = new module.SC3DMCTriplet(-1, -1, -1);
  2145. if (binarization !== local.O3DGC_SC3DMC_BINARIZATION_ASCII) {
  2146. return module.O3DGC_ERROR_CORRUPTED_STREAM;
  2147. }
  2148. bstream.ReadUInt32(iteratorPred, streamType); // predictors bitsream size
  2149. neighbors = this.m_neighbors;
  2150. normals = this.m_normals;
  2151. nPred = new module.NumberRef();
  2152. testPredEnabled = predMode.m_value !== local.O3DGC_SC3DMC_NO_PREDICTION;
  2153. for (v = 0; v < numIntArray; ++v) {
  2154. nPred.m_value = 0;
  2155. if (v2T.GetNumNeighbors(v) > 0 && testPredEnabled) {
  2156. u_begin = v2T.Begin(v);
  2157. u_end = v2T.End(v);
  2158. for (u = u_begin; u < u_end; ++u) {
  2159. ta = v2TNeighbors[u];
  2160. if (ta < 0) {
  2161. break;
  2162. }
  2163. DeltaPredictors(triangles, ta, v, nPred, neighbors, dimIntArray, intArray, stride);
  2164. }
  2165. }
  2166. if (nPred.m_value > 1) {
  2167. bestPred = bstream.ReadUCharASCII(iteratorPred);
  2168. for (i = 0; i < dimIntArray; ++i) {
  2169. predResidual = bstream.ReadIntASCII(iterator);
  2170. intArray[v * stride + i] = predResidual + neighbors[bestPred].m_pred[i];
  2171. }
  2172. } else if (v > 0 && predMode.m_value !== local.O3DGC_SC3DMC_NO_PREDICTION) {
  2173. for (i = 0; i < dimIntArray; ++i) {
  2174. predResidual = bstream.ReadIntASCII(iterator);
  2175. intArray[v * stride + i] = predResidual + intArray[(v - 1) * stride + i];
  2176. }
  2177. } else {
  2178. for (i = 0; i < dimIntArray; ++i) {
  2179. predResidual = bstream.ReadUIntASCII(iterator);
  2180. intArray[v * stride + i] = predResidual;
  2181. }
  2182. }
  2183. }
  2184. iterator.m_count = iteratorPred.m_count;
  2185. return module.O3DGC_OK;
  2186. };
  2187. module.SC3DMCDecoder.prototype.DecodeIntArray = function (intArray,
  2188. numIntArray,
  2189. dimIntArray,
  2190. stride,
  2191. ifs,
  2192. predMode,
  2193. bstream) {
  2194. if (this.m_streamType === local.O3DGC_STREAM_TYPE_ASCII) {
  2195. return this.DecodeIntArrayASCII(intArray, numIntArray, dimIntArray, stride, ifs, predMode, bstream);
  2196. }
  2197. return this.DecodeIntArrayBinary(intArray, numIntArray, dimIntArray, stride, ifs, predMode, bstream);
  2198. };
  2199. function ComputeNormals(triangles, ntris, coords, nvert, normals) {
  2200. var t3, v, n, t, a, b, c, d1, d2, n0;
  2201. n0 = new module.Vec3();
  2202. d1 = new module.Vec3();
  2203. d2 = new module.Vec3();
  2204. n = nvert * 3;
  2205. for (v = 0; v < n; ++v) {
  2206. normals[v] = 0;
  2207. }
  2208. for (t = 0; t < ntris; ++t) {
  2209. t3 = t * 3;
  2210. a = triangles[t3] * 3;
  2211. b = triangles[t3 + 1] * 3;
  2212. c = triangles[t3 + 2] * 3;
  2213. d1.m_x = coords[b] - coords[a];
  2214. d1.m_y = coords[b + 1] - coords[a + 1];
  2215. d1.m_z = coords[b + 2] - coords[a + 2];
  2216. d2.m_x = coords[c] - coords[a];
  2217. d2.m_y = coords[c + 1] - coords[a + 1];
  2218. d2.m_z = coords[c + 2] - coords[a + 2];
  2219. n0.m_x = d1.m_y * d2.m_z - d1.m_z * d2.m_y;
  2220. n0.m_y = d1.m_z * d2.m_x - d1.m_x * d2.m_z;
  2221. n0.m_z = d1.m_x * d2.m_y - d1.m_y * d2.m_x;
  2222. normals[a] += n0.m_x;
  2223. normals[a + 1] += n0.m_y;
  2224. normals[a + 2] += n0.m_z;
  2225. normals[b] += n0.m_x;
  2226. normals[b + 1] += n0.m_y;
  2227. normals[b + 2] += n0.m_z;
  2228. normals[c] += n0.m_x;
  2229. normals[c + 1] += n0.m_y;
  2230. normals[c + 2] += n0.m_z;
  2231. }
  2232. }
  2233. module.SC3DMCDecoder.prototype.ProcessNormals = function (ifs) {
  2234. var v3, v2, nvert, normalSize, normals, quantFloatArray, orientation, triangles, n0, n1, v, rna0, rnb0, ni1, norm0;
  2235. nvert = ifs.GetNNormal();
  2236. normalSize = ifs.GetNNormal() * 3;
  2237. if (this.m_normalsSize < normalSize) {
  2238. this.m_normalsSize = normalSize;
  2239. this.m_normals = new Float32Array(this.m_normalsSize);
  2240. }
  2241. normals = this.m_normals;
  2242. quantFloatArray = this.m_quantFloatArray;
  2243. orientation = this.m_orientation;
  2244. triangles = ifs.GetCoordIndex();
  2245. ComputeNormals(triangles, ifs.GetNCoordIndex(), quantFloatArray, nvert, normals);
  2246. n0 = new module.Vec3();
  2247. n1 = new module.Vec3();
  2248. for (v = 0; v < nvert; ++v) {
  2249. v3 = 3 * v;
  2250. n0.m_x = normals[v3];
  2251. n0.m_y = normals[v3 + 1];
  2252. n0.m_z = normals[v3 + 2];
  2253. norm0 = Math.sqrt(n0.m_x * n0.m_x + n0.m_y * n0.m_y + n0.m_z * n0.m_z);
  2254. if (norm0 === 0.0) {
  2255. norm0 = 1.0;
  2256. }
  2257. SphereToCube(n0, n1);
  2258. rna0 = n1.m_x / norm0;
  2259. rnb0 = n1.m_y / norm0;
  2260. ni1 = n1.m_z + orientation[v];
  2261. orientation[v] = ni1;
  2262. if ((ni1 >>> 1) !== (n1.m_z >>> 1)) {
  2263. rna0 = 0.0;
  2264. rnb0 = 0.0;
  2265. }
  2266. v2 = v * 2;
  2267. normals[v2] = rna0;
  2268. normals[v2 + 1] = rnb0;
  2269. }
  2270. return module.O3DGC_OK;
  2271. };
  2272. module.SC3DMCDecoder.prototype.IQuantize = function (floatArray,
  2273. numFloatArray,
  2274. dimFloatArray,
  2275. stride,
  2276. minFloatArray,
  2277. maxFloatArray,
  2278. nQBits,
  2279. predMode) {
  2280. var v, nin, nout, orientation, normals, CubeToSphere;
  2281. if (predMode.m_value === local.O3DGC_SC3DMC_SURF_NORMALS_PREDICTION) {
  2282. CubeToSphere = local.CubeToSphere;
  2283. orientation = this.m_orientation;
  2284. normals = this.m_normals;
  2285. nin = new module.Vec3(0, 0, 0);
  2286. nout = new module.Vec3(0, 0, 0);
  2287. this.IQuantizeFloatArray(floatArray, numFloatArray, dimFloatArray, stride, this.m_minNormal, this.m_maxNormal, nQBits + 1);
  2288. for (v = 0; v < numFloatArray; ++v) {
  2289. nin.m_x = floatArray[stride * v] + normals[2 * v];
  2290. nin.m_y = floatArray[stride * v + 1] + normals[2 * v + 1];
  2291. nin.m_z = orientation[v];
  2292. CubeToSphere[nin.m_z](nin, nout);
  2293. floatArray[stride * v] = nout.m_x;
  2294. floatArray[stride * v + 1] = nout.m_y;
  2295. floatArray[stride * v + 2] = nout.m_z;
  2296. }
  2297. } else {
  2298. this.IQuantizeFloatArray(floatArray, numFloatArray, dimFloatArray, stride, minFloatArray, maxFloatArray, nQBits);
  2299. }
  2300. };
  2301. module.SC3DMCDecoder.prototype.DecodeFloatArrayBinary = function (floatArray,
  2302. numFloatArray,
  2303. dimFloatArray,
  2304. stride,
  2305. minFloatArray,
  2306. maxFloatArray,
  2307. nQBits,
  2308. ifs,
  2309. predMode,
  2310. bstream) {
  2311. var maxNPred, testPredEnabled, testParaPredEnabled, bestPred, dModel, buffer, quantFloatArray, neighbors, normals, nPred, ta, i, v, u, u_begin, u_end, iterator, orientation, streamType, predResidual, acd, bModel0, bModel1, mModelPreds, v2T, v2TNeighbors, triangles, size, start, streamSize, mask, binarization, iteratorPred, exp_k, M, mModelValues;
  2312. iterator = this.m_iterator;
  2313. orientation = this.m_orientation;
  2314. streamType = this.m_streamType;
  2315. acd = new module.ArithmeticDecoder();
  2316. bModel0 = new module.StaticBitModel();
  2317. bModel1 = new module.AdaptiveBitModel();
  2318. mModelPreds = new module.AdaptiveDataModel();
  2319. maxNPred = local.O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS;
  2320. mModelPreds.SetAlphabet(maxNPred + 1);
  2321. v2T = this.m_triangleListDecoder.GetVertexToTriangle();
  2322. v2TNeighbors = v2T.m_neighbors;
  2323. triangles = ifs.GetCoordIndex();
  2324. size = numFloatArray * dimFloatArray;
  2325. start = iterator.m_count;
  2326. streamSize = bstream.ReadUInt32(iterator, streamType);
  2327. mask = bstream.ReadUChar(iterator, streamType);
  2328. binarization = (mask >>> 4) & 7;
  2329. predMode.m_value = mask & 7;
  2330. streamSize -= (iterator.m_count - start);
  2331. iteratorPred = new module.Iterator();
  2332. iteratorPred.m_count = iterator.m_count + streamSize;
  2333. exp_k = 0;
  2334. M = 0;
  2335. if (binarization !== local.O3DGC_SC3DMC_BINARIZATION_AC_EGC) {
  2336. return module.O3DGC_ERROR_CORRUPTED_STREAM;
  2337. }
  2338. buffer = bstream.GetBuffer(iterator, streamSize);
  2339. iterator.m_count += streamSize;
  2340. acd.SetBuffer(streamSize, buffer);
  2341. acd.StartDecoder();
  2342. exp_k = acd.ExpGolombDecode(0, bModel0, bModel1);
  2343. M = acd.ExpGolombDecode(0, bModel0, bModel1);
  2344. mModelValues = new module.AdaptiveDataModel();
  2345. mModelValues.SetAlphabet(M + 2);
  2346. if (predMode.m_value === local.O3DGC_SC3DMC_SURF_NORMALS_PREDICTION) {
  2347. if (this.m_orientationSize < size) {
  2348. this.m_orientationSize = size;
  2349. this.m_orientation = new Int8Array(this.m_orientationSize);
  2350. orientation = this.m_orientation;
  2351. }
  2352. dModel = new module.AdaptiveDataModel();
  2353. dModel.SetAlphabet(12);
  2354. for (i = 0; i < numFloatArray; ++i) {
  2355. orientation[i] = UIntToInt(acd.DecodeAdaptiveDataModel(dModel));
  2356. }
  2357. this.ProcessNormals(ifs);
  2358. dimFloatArray = 2;
  2359. }
  2360. if (this.m_quantFloatArraySize < size) {
  2361. this.m_quantFloatArraySize = size;
  2362. this.m_quantFloatArray = new Int32Array(this.m_quantFloatArraySize);
  2363. }
  2364. quantFloatArray = this.m_quantFloatArray;
  2365. neighbors = this.m_neighbors;
  2366. normals = this.m_normals;
  2367. nPred = new module.NumberRef();
  2368. testPredEnabled = predMode.m_value !== local.O3DGC_SC3DMC_NO_PREDICTION;
  2369. testParaPredEnabled = predMode.m_value === local.O3DGC_SC3DMC_PARALLELOGRAM_PREDICTION;
  2370. for (v = 0; v < numFloatArray; ++v) {
  2371. nPred.m_value = 0;
  2372. if (v2T.GetNumNeighbors(v) > 0 && testPredEnabled) {
  2373. u_begin = v2T.Begin(v);
  2374. u_end = v2T.End(v);
  2375. if (testParaPredEnabled) {
  2376. for (u = u_begin; u < u_end; ++u) {
  2377. ta = v2TNeighbors[u];
  2378. if (ta < 0) {
  2379. break;
  2380. }
  2381. ParallelogramPredictors(triangles, ta, v, nPred, neighbors, dimFloatArray, quantFloatArray, stride, v2T, v2TNeighbors);
  2382. }
  2383. }
  2384. if (nPred.m_value < maxNPred) {
  2385. for (u = u_begin; u < u_end; ++u) {
  2386. ta = v2TNeighbors[u];
  2387. if (ta < 0) {
  2388. break;
  2389. }
  2390. DeltaPredictors(triangles, ta, v, nPred, neighbors, dimFloatArray, quantFloatArray, stride);
  2391. }
  2392. }
  2393. }
  2394. if (nPred.m_value > 1) {
  2395. bestPred = acd.DecodeAdaptiveDataModel(mModelPreds);
  2396. for (i = 0; i < dimFloatArray; ++i) {
  2397. predResidual = acd.DecodeIntACEGC(mModelValues, bModel0, bModel1, exp_k, M);
  2398. quantFloatArray[v * stride + i] = predResidual + neighbors[bestPred].m_pred[i];
  2399. }
  2400. } else if (v > 0 && testPredEnabled) {
  2401. for (i = 0; i < dimFloatArray; ++i) {
  2402. predResidual = acd.DecodeIntACEGC(mModelValues, bModel0, bModel1, exp_k, M);
  2403. quantFloatArray[v * stride + i] = predResidual + quantFloatArray[(v - 1) * stride + i];
  2404. }
  2405. } else {
  2406. for (i = 0; i < dimFloatArray; ++i) {
  2407. predResidual = acd.DecodeUIntACEGC(mModelValues, bModel0, bModel1, exp_k, M);
  2408. quantFloatArray[v * stride + i] = predResidual;
  2409. }
  2410. }
  2411. }
  2412. iterator.m_count = iteratorPred.m_count;
  2413. this.IQuantize(floatArray, numFloatArray, dimFloatArray, stride, minFloatArray, maxFloatArray, nQBits, predMode);
  2414. return module.O3DGC_OK;
  2415. };
  2416. module.SC3DMCDecoder.prototype.DecodeFloatArrayASCII = function (floatArray,
  2417. numFloatArray,
  2418. dimFloatArray,
  2419. stride,
  2420. minFloatArray,
  2421. maxFloatArray,
  2422. nQBits,
  2423. ifs,
  2424. predMode,
  2425. bstream) {
  2426. var maxNPred, testPredEnabled, testParaPredEnabled, iterator, orientation, streamType, predResidual, v2T, v2TNeighbors, triangles, size, start, streamSize, mask, binarization, iteratorPred, quantFloatArray, neighbors, normals, nPred, v, u, u_begin, u_end, ta, i, bestPred;
  2427. maxNPred = local.O3DGC_SC3DMC_MAX_PREDICTION_NEIGHBORS;
  2428. iterator = this.m_iterator;
  2429. orientation = this.m_orientation;
  2430. streamType = this.m_streamType;
  2431. v2T = this.m_triangleListDecoder.GetVertexToTriangle();
  2432. v2TNeighbors = v2T.m_neighbors;
  2433. triangles = ifs.GetCoordIndex();
  2434. size = numFloatArray * dimFloatArray;
  2435. start = iterator.m_count;
  2436. streamSize = bstream.ReadUInt32(iterator, streamType);
  2437. mask = bstream.ReadUChar(iterator, streamType);
  2438. binarization = (mask >>> 4) & 7;
  2439. predMode.m_value = mask & 7;
  2440. streamSize -= (iterator.m_count - start);
  2441. iteratorPred = new module.Iterator();
  2442. iteratorPred.m_count = iterator.m_count + streamSize;
  2443. if (binarization !== local.O3DGC_SC3DMC_BINARIZATION_ASCII) {
  2444. return module.O3DGC_ERROR_CORRUPTED_STREAM;
  2445. }
  2446. bstream.ReadUInt32(iteratorPred, streamType);
  2447. if (predMode.m_value === local.O3DGC_SC3DMC_SURF_NORMALS_PREDICTION) {
  2448. if (this.m_orientationSize < numFloatArray) {
  2449. this.m_orientationSize = numFloatArray;
  2450. this.m_orientation = new Int8Array(this.m_orientationSize);
  2451. orientation = this.m_orientation;
  2452. }
  2453. for (i = 0; i < numFloatArray; ++i) {
  2454. orientation[i] = bstream.ReadIntASCII(iterator);
  2455. }
  2456. this.ProcessNormals(ifs);
  2457. dimFloatArray = 2;
  2458. }
  2459. if (this.m_quantFloatArraySize < size) {
  2460. this.m_quantFloatArraySize = size;
  2461. this.m_quantFloatArray = new Int32Array(this.m_quantFloatArraySize);
  2462. }
  2463. quantFloatArray = this.m_quantFloatArray;
  2464. neighbors = this.m_neighbors;
  2465. normals = this.m_normals;
  2466. nPred = new module.NumberRef();
  2467. testPredEnabled = predMode.m_value !== local.O3DGC_SC3DMC_NO_PREDICTION;
  2468. testParaPredEnabled = predMode.m_value === local.O3DGC_SC3DMC_PARALLELOGRAM_PREDICTION;
  2469. for (v = 0; v < numFloatArray; ++v) {
  2470. nPred.m_value = 0;
  2471. if (v2T.GetNumNeighbors(v) > 0 && testPredEnabled) {
  2472. u_begin = v2T.Begin(v);
  2473. u_end = v2T.End(v);
  2474. if (testParaPredEnabled) {
  2475. for (u = u_begin; u < u_end; ++u) {
  2476. ta = v2TNeighbors[u];
  2477. if (ta < 0) {
  2478. break;
  2479. }
  2480. ParallelogramPredictors(triangles, ta, v, nPred, neighbors, dimFloatArray, quantFloatArray, stride, v2T, v2TNeighbors);
  2481. }
  2482. }
  2483. if (nPred.m_value < maxNPred) {
  2484. for (u = u_begin; u < u_end; ++u) {
  2485. ta = v2TNeighbors[u];
  2486. if (ta < 0) {
  2487. break;
  2488. }
  2489. DeltaPredictors(triangles, ta, v, nPred, neighbors, dimFloatArray, quantFloatArray, stride);
  2490. }
  2491. }
  2492. }
  2493. if (nPred.m_value > 1) {
  2494. bestPred = bstream.ReadUCharASCII(iteratorPred);
  2495. for (i = 0; i < dimFloatArray; ++i) {
  2496. predResidual = bstream.ReadIntASCII(iterator);
  2497. quantFloatArray[v * stride + i] = predResidual + neighbors[bestPred].m_pred[i];
  2498. }
  2499. } else if (v > 0 && predMode.m_value !== local.O3DGC_SC3DMC_NO_PREDICTION) {
  2500. for (i = 0; i < dimFloatArray; ++i) {
  2501. predResidual = bstream.ReadIntASCII(iterator);
  2502. quantFloatArray[v * stride + i] = predResidual + quantFloatArray[(v - 1) * stride + i];
  2503. }
  2504. } else {
  2505. for (i = 0; i < dimFloatArray; ++i) {
  2506. predResidual = bstream.ReadUIntASCII(iterator);
  2507. quantFloatArray[v * stride + i] = predResidual;
  2508. }
  2509. }
  2510. }
  2511. iterator.m_count = iteratorPred.m_count;
  2512. this.IQuantize(floatArray, numFloatArray, dimFloatArray, stride, minFloatArray, maxFloatArray, nQBits, predMode);
  2513. return module.O3DGC_OK;
  2514. };
  2515. module.SC3DMCDecoder.prototype.DecodeFloatArray = function (floatArray,
  2516. numFloatArray,
  2517. dimFloatArray,
  2518. stride,
  2519. minFloatArray,
  2520. maxFloatArray,
  2521. nQBits,
  2522. ifs,
  2523. predMode,
  2524. bstream) {
  2525. if (this.m_streamType === local.O3DGC_STREAM_TYPE_ASCII) {
  2526. return this.DecodeFloatArrayASCII(floatArray, numFloatArray, dimFloatArray, stride, minFloatArray, maxFloatArray, nQBits, ifs, predMode, bstream);
  2527. }
  2528. return this.DecodeFloatArrayBinary(floatArray, numFloatArray, dimFloatArray, stride, minFloatArray, maxFloatArray, nQBits, ifs, predMode, bstream);
  2529. };
  2530. module.SC3DMCDecoder.prototype.IQuantizeFloatArray = function (floatArray, numFloatArray, dimFloatArray, stride, minFloatArray, maxFloatArray, nQBits) {
  2531. var idelta, quantFloatArray, d, r, v;
  2532. idelta = this.m_idelta;
  2533. quantFloatArray = this.m_quantFloatArray;
  2534. for (d = 0; d < dimFloatArray; ++d) {
  2535. r = maxFloatArray[d] - minFloatArray[d];
  2536. if (r > 0.0) {
  2537. idelta[d] = r / (((1 << nQBits) >>> 0) - 1);
  2538. } else {
  2539. idelta[d] = 1.0;
  2540. }
  2541. }
  2542. for (v = 0; v < numFloatArray; ++v) {
  2543. for (d = 0; d < dimFloatArray; ++d) {
  2544. floatArray[v * stride + d] = quantFloatArray[v * stride + d] * idelta[d] + minFloatArray[d];
  2545. }
  2546. }
  2547. return module.O3DGC_OK;
  2548. };
  2549. module.SC3DMCDecoder.prototype.DecodePlayload = function (ifs, bstream) {
  2550. var params, iterator, stats, predMode, timer, ret, a;
  2551. params = this.m_params;
  2552. iterator = this.m_iterator;
  2553. stats = this.m_stats;
  2554. predMode = new module.NumberRef();
  2555. timer = new module.Timer();
  2556. ret = module.O3DGC_OK;
  2557. this.m_triangleListDecoder.SetStreamType(this.m_streamType);
  2558. stats.m_streamSizeCoordIndex = iterator.m_count;
  2559. timer.Tic();
  2560. this.m_triangleListDecoder.Decode(ifs.GetCoordIndex(), ifs.GetNCoordIndex(), ifs.GetNCoord(), bstream, iterator);
  2561. timer.Toc();
  2562. stats.m_timeCoordIndex = timer.GetElapsedTime();
  2563. stats.m_streamSizeCoordIndex = iterator.m_count - stats.m_streamSizeCoordIndex;
  2564. // decode coord
  2565. stats.m_streamSizeCoord = iterator.m_count;
  2566. timer.Tic();
  2567. if (ifs.GetNCoord() > 0) {
  2568. ret = this.DecodeFloatArray(ifs.GetCoord(), ifs.GetNCoord(), 3, 3, ifs.GetCoordMinArray(), ifs.GetCoordMaxArray(), params.GetCoordQuantBits(), ifs, predMode, bstream);
  2569. params.SetCoordPredMode(predMode.m_value);
  2570. }
  2571. if (ret !== module.O3DGC_OK) {
  2572. return ret;
  2573. }
  2574. timer.Toc();
  2575. stats.m_timeCoord = timer.GetElapsedTime();
  2576. stats.m_streamSizeCoord = iterator.m_count - stats.m_streamSizeCoord;
  2577. // decode Normal
  2578. stats.m_streamSizeNormal = iterator.m_count;
  2579. timer.Tic();
  2580. if (ifs.GetNNormal() > 0) {
  2581. ret = this.DecodeFloatArray(ifs.GetNormal(), ifs.GetNNormal(), 3, 3, ifs.GetNormalMinArray(), ifs.GetNormalMaxArray(), params.GetNormalQuantBits(), ifs, predMode, bstream);
  2582. params.SetNormalPredMode(predMode.m_value);
  2583. }
  2584. if (ret !== module.O3DGC_OK) {
  2585. return ret;
  2586. }
  2587. timer.Toc();
  2588. stats.m_timeNormal = timer.GetElapsedTime();
  2589. stats.m_streamSizeNormal = iterator.m_count - stats.m_streamSizeNormal;
  2590. // decode FloatAttributes
  2591. for (a = 0; a < ifs.GetNumFloatAttributes(); ++a) {
  2592. stats.m_streamSizeFloatAttribute[a] = iterator.m_count;
  2593. timer.Tic();
  2594. ret = this.DecodeFloatArray(ifs.GetFloatAttribute(a), ifs.GetNFloatAttribute(a), ifs.GetFloatAttributeDim(a), ifs.GetFloatAttributeDim(a), ifs.GetFloatAttributeMinArray(a), ifs.GetFloatAttributeMaxArray(a), params.GetFloatAttributeQuantBits(a), ifs, predMode, bstream);
  2595. params.SetFloatAttributePredMode(a, predMode.m_value);
  2596. timer.Toc();
  2597. stats.m_timeFloatAttribute[a] = timer.GetElapsedTime();
  2598. stats.m_streamSizeFloatAttribute[a] = iterator.m_count - stats.m_streamSizeFloatAttribute[a];
  2599. }
  2600. if (ret !== module.O3DGC_OK) {
  2601. return ret;
  2602. }
  2603. // decode IntAttributes
  2604. for (a = 0; a < ifs.GetNumIntAttributes(); ++a) {
  2605. stats.m_streamSizeIntAttribute[a] = iterator.m_count;
  2606. timer.Tic();
  2607. ret = this.DecodeIntArray(ifs.GetIntAttribute(a), ifs.GetNIntAttribute(a), ifs.GetIntAttributeDim(a), ifs.GetIntAttributeDim(a), ifs, predMode, bstream);
  2608. params.SetIntAttributePredMode(a, predMode.m_value);
  2609. timer.Toc();
  2610. stats.m_timeIntAttribute[a] = timer.GetElapsedTime();
  2611. stats.m_streamSizeIntAttribute[a] = iterator.m_count - stats.m_streamSizeIntAttribute[a];
  2612. }
  2613. if (ret !== module.O3DGC_OK) {
  2614. return ret;
  2615. }
  2616. timer.Tic();
  2617. this.m_triangleListDecoder.Reorder();
  2618. timer.Toc();
  2619. stats.m_timeReorder = timer.GetElapsedTime();
  2620. return ret;
  2621. };
  2622. // DVEncodeParams class
  2623. module.DVEncodeParams = function () {
  2624. this.m_encodeMode = local.O3DGC_DYNAMIC_VECTOR_ENCODE_MODE_LIFT;
  2625. this.m_streamTypeMode = local.O3DGC_STREAM_TYPE_ASCII;
  2626. this.m_quantBits = 10;
  2627. };
  2628. module.DVEncodeParams.prototype.GetStreamType = function () {
  2629. return this.m_streamTypeMode;
  2630. };
  2631. module.DVEncodeParams.prototype.GetEncodeMode = function () {
  2632. return this.m_encodeMode;
  2633. };
  2634. module.DVEncodeParams.prototype.GetQuantBits = function () {
  2635. return this.m_quantBits;
  2636. };
  2637. module.DVEncodeParams.prototype.SetStreamType = function (streamTypeMode) {
  2638. this.m_streamTypeMode = streamTypeMode;
  2639. };
  2640. module.DVEncodeParams.prototype.SetEncodeMode = function (encodeMode) {
  2641. this.m_encodeMode = encodeMode;
  2642. };
  2643. module.DVEncodeParams.prototype.SetQuantBits = function (quantBits) {
  2644. this.m_quantBits = quantBits;
  2645. };
  2646. // DynamicVector class
  2647. module.DynamicVector = function () {
  2648. this.m_num = 0;
  2649. this.m_dim = 0;
  2650. this.m_stride = 0;
  2651. this.m_max = {};
  2652. this.m_min = {};
  2653. this.m_vectors = {};
  2654. };
  2655. module.DynamicVector.prototype.GetNVector = function () {
  2656. return this.m_num;
  2657. };
  2658. module.DynamicVector.prototype.GetDimVector = function () {
  2659. return this.m_dim;
  2660. };
  2661. module.DynamicVector.prototype.GetStride = function () {
  2662. return this.m_stride;
  2663. };
  2664. module.DynamicVector.prototype.GetMinArray = function () {
  2665. return this.m_min;
  2666. };
  2667. module.DynamicVector.prototype.GetMaxArray = function () {
  2668. return this.m_max;
  2669. };
  2670. module.DynamicVector.prototype.GetVectors = function () {
  2671. return this.m_vectors;
  2672. };
  2673. module.DynamicVector.prototype.GetMin = function (j) {
  2674. return this.m_min[j];
  2675. };
  2676. module.DynamicVector.prototype.GetMax = function (j) {
  2677. return this.m_max[j];
  2678. };
  2679. module.DynamicVector.prototype.SetNVector = function (num) {
  2680. this.m_num = num;
  2681. };
  2682. module.DynamicVector.prototype.SetDimVector = function (dim) {
  2683. this.m_dim = dim;
  2684. };
  2685. module.DynamicVector.prototype.SetStride = function (stride) {
  2686. this.m_stride = stride;
  2687. };
  2688. module.DynamicVector.prototype.SetMinArray = function (min) {
  2689. this.m_min = min;
  2690. };
  2691. module.DynamicVector.prototype.SetMaxArray = function (max) {
  2692. this.m_max = max;
  2693. };
  2694. module.DynamicVector.prototype.SetMin = function (j, min) {
  2695. this.m_min[j] = min;
  2696. };
  2697. module.DynamicVector.prototype.SetMax = function (j, max) {
  2698. this.m_max[j] = max;
  2699. };
  2700. module.DynamicVector.prototype.SetVectors = function (vectors) {
  2701. this.m_vectors = vectors;
  2702. };
  2703. // DynamicVectorDecoder class
  2704. module.DynamicVectorDecoder = function () {
  2705. this.m_streamSize = 0;
  2706. this.m_maxNumVectors = 0;
  2707. this.m_numVectors = 0;
  2708. this.m_dimVectors = 0;
  2709. this.m_quantVectors = {};
  2710. this.m_iterator = new module.Iterator();
  2711. this.m_streamType = local.O3DGC_STREAM_TYPE_UNKOWN;
  2712. this.m_params = new module.DVEncodeParams();
  2713. };
  2714. module.DynamicVectorDecoder.prototype.GetStreamType = function () {
  2715. return this.m_streamType;
  2716. };
  2717. module.DynamicVectorDecoder.prototype.GetIterator = function () {
  2718. return this.m_iterator;
  2719. };
  2720. module.DynamicVectorDecoder.prototype.SetStreamType = function (streamType) {
  2721. this.m_streamType = streamType;
  2722. };
  2723. module.DynamicVectorDecoder.prototype.SetIterator = function (iterator) {
  2724. this.m_iterator = iterator;
  2725. };
  2726. module.DynamicVectorDecoder.prototype.IUpdate = function (data, shift, size) {
  2727. var p, size1;
  2728. size1 = size - 1;
  2729. p = 2;
  2730. data[shift] -= data[shift + 1] >> 1;
  2731. while (p < size1) {
  2732. data[shift + p] -= (data[shift + p - 1] + data[shift + p + 1] + 2) >> 2;
  2733. p += 2;
  2734. }
  2735. if (p === size1) {
  2736. data[shift + p] -= data[shift + p - 1] >> 1;
  2737. }
  2738. return module.O3DGC_OK;
  2739. };
  2740. module.DynamicVectorDecoder.prototype.IPredict = function (data, shift, size) {
  2741. var p, size1;
  2742. size1 = size - 1;
  2743. p = 1;
  2744. while (p < size1) {
  2745. data[shift + p] += (data[shift + p - 1] + data[shift + p + 1] + 1) >> 1;
  2746. p += 2;
  2747. }
  2748. if (p === size1) {
  2749. data[shift + p] += data[shift + p - 1];
  2750. }
  2751. return module.O3DGC_OK;
  2752. };
  2753. module.DynamicVectorDecoder.prototype.Merge = function (data, shift, size) {
  2754. var i, h, a, b, tmp;
  2755. h = (size >> 1) + (size & 1);
  2756. a = h - 1;
  2757. b = h;
  2758. while (a > 0) {
  2759. for (i = a; i < b; i += 2) {
  2760. tmp = data[shift + i];
  2761. data[shift + i] = data[shift + i + 1];
  2762. data[shift + i + 1] = tmp;
  2763. }
  2764. --a;
  2765. ++b;
  2766. }
  2767. return module.O3DGC_OK;
  2768. };
  2769. module.DynamicVectorDecoder.prototype.ITransform = function (data, shift, size) {
  2770. var n, even, k, i;
  2771. n = size;
  2772. even = 0;
  2773. k = 0;
  2774. even += ((n & 1) << k++) >>> 0;
  2775. while (n > 1) {
  2776. n = (n >> 1) + ((n & 1) >>> 0);
  2777. even += ((n & 1) << k++) >>> 0;
  2778. }
  2779. for (i = k - 2; i >= 0; --i) {
  2780. n = ((n << 1) >>> 0) - (((even >>> i) & 1)) >>> 0;
  2781. this.Merge(data, shift, n);
  2782. this.IUpdate(data, shift, n);
  2783. this.IPredict(data, shift, n);
  2784. }
  2785. return module.O3DGC_OK;
  2786. };
  2787. module.DynamicVectorDecoder.prototype.IQuantize = function (floatArray,
  2788. numFloatArray,
  2789. dimFloatArray,
  2790. stride,
  2791. minFloatArray,
  2792. maxFloatArray,
  2793. nQBits) {
  2794. var quantVectors, r, idelta, size, d, v;
  2795. quantVectors = this.m_quantVectors;
  2796. size = numFloatArray * dimFloatArray;
  2797. for (d = 0; d < dimFloatArray; ++d) {
  2798. r = maxFloatArray[d] - minFloatArray[d];
  2799. if (r > 0.0) {
  2800. idelta = r / (((1 << nQBits) >>> 0) - 1);
  2801. } else {
  2802. idelta = 1.0;
  2803. }
  2804. for (v = 0; v < numFloatArray; ++v) {
  2805. floatArray[v * stride + d] = quantVectors[v + d * numFloatArray] * idelta + minFloatArray[d];
  2806. }
  2807. }
  2808. return module.O3DGC_OK;
  2809. };
  2810. module.DynamicVectorDecoder.prototype.DecodeHeader = function (dynamicVector, bstream) {
  2811. var iterator, c0, start_code, streamType;
  2812. iterator = this.m_iterator;
  2813. c0 = iterator.m_count;
  2814. start_code = bstream.ReadUInt32(iterator, local.O3DGC_STREAM_TYPE_BINARY);
  2815. if (start_code !== local.O3DGC_DV_START_CODE) {
  2816. iterator.m_count = c0;
  2817. start_code = bstream.ReadUInt32(iterator, local.O3DGC_STREAM_TYPE_ASCII);
  2818. if (start_code !== local.O3DGC_DV_START_CODE) {
  2819. return module.O3DGC_ERROR_CORRUPTED_STREAM;
  2820. }
  2821. this.m_streamType = local.O3DGC_STREAM_TYPE_ASCII;
  2822. } else {
  2823. this.m_streamType = local.O3DGC_STREAM_TYPE_BINARY;
  2824. }
  2825. streamType = this.m_streamType;
  2826. this.m_streamSize = bstream.ReadUInt32(iterator, streamType);
  2827. this.m_params.SetEncodeMode(bstream.ReadUChar(iterator, streamType));
  2828. dynamicVector.SetNVector(bstream.ReadUInt32(iterator, streamType));
  2829. if (dynamicVector.GetNVector() > 0) {
  2830. dynamicVector.SetDimVector(bstream.ReadUInt32(iterator, streamType));
  2831. this.m_params.SetQuantBits(bstream.ReadUChar(iterator, streamType));
  2832. }
  2833. return module.O3DGC_OK;
  2834. };
  2835. module.DynamicVectorDecoder.prototype.DecodePlayload = function (dynamicVector, bstream) {
  2836. var size, iterator, streamType, ret, start, streamSize, dim, num, j, acd, bModel0, bModel1, exp_k, M, buffer, mModelValues, quantVectors, v, d;
  2837. iterator = this.m_iterator;
  2838. streamType = this.m_streamType;
  2839. ret = module.O3DGC_OK;
  2840. start = iterator.m_count;
  2841. streamSize = bstream.ReadUInt32(iterator, streamType);
  2842. dim = dynamicVector.GetDimVector();
  2843. num = dynamicVector.GetNVector();
  2844. size = dim * num;
  2845. for (j = 0; j < dynamicVector.GetDimVector(); ++j) {
  2846. dynamicVector.SetMin(j, bstream.ReadFloat32(iterator, streamType));
  2847. dynamicVector.SetMax(j, bstream.ReadFloat32(iterator, streamType));
  2848. }
  2849. acd = new module.ArithmeticDecoder();
  2850. bModel0 = new module.StaticBitModel();
  2851. bModel1 = new module.AdaptiveBitModel();
  2852. streamSize -= (iterator.m_count - start);
  2853. exp_k = 0;
  2854. M = 0;
  2855. if (streamType === local.O3DGC_STREAM_TYPE_BINARY) {
  2856. buffer = bstream.GetBuffer(iterator, streamSize);
  2857. iterator.m_count += streamSize;
  2858. acd.SetBuffer(streamSize, buffer);
  2859. acd.StartDecoder();
  2860. exp_k = acd.ExpGolombDecode(0, bModel0, bModel1);
  2861. M = acd.ExpGolombDecode(0, bModel0, bModel1);
  2862. }
  2863. mModelValues = new module.AdaptiveDataModel();
  2864. mModelValues.SetAlphabet(M + 2);
  2865. if (this.m_maxNumVectors < size) {
  2866. this.m_maxNumVectors = size;
  2867. this.m_quantVectors = new Int32Array(this.m_maxNumVectors);
  2868. }
  2869. quantVectors = this.m_quantVectors;
  2870. if (streamType === local.O3DGC_STREAM_TYPE_ASCII) {
  2871. for (v = 0; v < num; ++v) {
  2872. for (d = 0; d < dim; ++d) {
  2873. quantVectors[d * num + v] = bstream.ReadIntASCII(iterator);
  2874. }
  2875. }
  2876. } else {
  2877. for (v = 0; v < num; ++v) {
  2878. for (d = 0; d < dim; ++d) {
  2879. quantVectors[d * num + v] = acd.DecodeIntACEGC(mModelValues, bModel0, bModel1, exp_k, M);
  2880. }
  2881. }
  2882. }
  2883. for (d = 0; d < dim; ++d) {
  2884. this.ITransform(quantVectors, d * num, num);
  2885. }
  2886. this.IQuantize(dynamicVector.GetVectors(), num, dim,
  2887. dynamicVector.GetStride(), dynamicVector.GetMinArray(),
  2888. dynamicVector.GetMaxArray(), this.m_params.GetQuantBits());
  2889. return ret;
  2890. };
  2891. return module;
  2892. })();