index.js 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804
  1. 'use strict';
  2. var bbox = require('@turf/bbox');
  3. var area = require('@turf/area');
  4. var booleanPointInPolygon = require('@turf/boolean-point-in-polygon');
  5. var explode = require('@turf/explode');
  6. var invariant = require('@turf/invariant');
  7. var helpers = require('@turf/helpers');
  8. var objectAssign = require('object-assign');
  9. var meta = require('@turf/meta');
  10. function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
  11. var bbox__default = /*#__PURE__*/_interopDefaultLegacy(bbox);
  12. var area__default = /*#__PURE__*/_interopDefaultLegacy(area);
  13. var booleanPointInPolygon__default = /*#__PURE__*/_interopDefaultLegacy(booleanPointInPolygon);
  14. var explode__default = /*#__PURE__*/_interopDefaultLegacy(explode);
  15. var objectAssign__default = /*#__PURE__*/_interopDefaultLegacy(objectAssign);
  16. /**
  17. * Takes a {@link Point} grid and returns a correspondent matrix {Array<Array<number>>}
  18. * of the 'property' values
  19. *
  20. * @name gridToMatrix
  21. * @param {FeatureCollection<Point>} grid of points
  22. * @param {Object} [options={}] Optional parameters
  23. * @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
  24. * @param {boolean} [options.flip=false] returns the matrix upside-down
  25. * @param {boolean} [options.flags=false] flags, adding a `matrixPosition` array field ([row, column]) to its properties,
  26. * the grid points with coordinates on the matrix
  27. * @returns {Array<Array<number>>} matrix of property values
  28. * @example
  29. * var extent = [-70.823364, -33.553984, -70.473175, -33.302986];
  30. * var cellSize = 3;
  31. * var grid = turf.pointGrid(extent, cellSize);
  32. * // add a random property to each point between 0 and 60
  33. * for (var i = 0; i < grid.features.length; i++) {
  34. * grid.features[i].properties.elevation = (Math.random() * 60);
  35. * }
  36. * gridToMatrix(grid);
  37. * //= [
  38. * [ 1, 13, 10, 9, 10, 13, 18],
  39. * [34, 8, 5, 4, 5, 8, 13],
  40. * [10, 5, 2, 1, 2, 5, 4],
  41. * [ 0, 4, 56, 19, 1, 4, 9],
  42. * [10, 5, 2, 1, 2, 5, 10],
  43. * [57, 8, 5, 4, 5, 0, 57],
  44. * [ 3, 13, 10, 9, 5, 13, 18],
  45. * [18, 13, 10, 9, 78, 13, 18]
  46. * ]
  47. */
  48. function gridToMatrix(grid, options) {
  49. // Optional parameters
  50. options = options || {};
  51. if (!helpers.isObject(options)) throw new Error("options is invalid");
  52. var zProperty = options.zProperty || "elevation";
  53. var flip = options.flip;
  54. var flags = options.flags;
  55. // validation
  56. invariant.collectionOf(grid, "Point", "input must contain Points");
  57. var pointsMatrix = sortPointsByLatLng(grid, flip);
  58. var matrix = [];
  59. // create property matrix from sorted points
  60. // looping order matters here
  61. for (var r = 0; r < pointsMatrix.length; r++) {
  62. var pointRow = pointsMatrix[r];
  63. var row = [];
  64. for (var c = 0; c < pointRow.length; c++) {
  65. var point = pointRow[c];
  66. // Check if zProperty exist
  67. if (point.properties[zProperty]) row.push(point.properties[zProperty]);
  68. else row.push(0);
  69. // add flags
  70. if (flags === true) point.properties.matrixPosition = [r, c];
  71. }
  72. matrix.push(row);
  73. }
  74. return matrix;
  75. }
  76. /**
  77. * Sorts points by latitude and longitude, creating a 2-dimensional array of points
  78. *
  79. * @private
  80. * @param {FeatureCollection<Point>} points GeoJSON Point features
  81. * @param {boolean} [flip=false] returns the matrix upside-down
  82. * @returns {Array<Array<Point>>} points ordered by latitude and longitude
  83. */
  84. function sortPointsByLatLng(points, flip) {
  85. var pointsByLatitude = {};
  86. // divide points by rows with the same latitude
  87. meta.featureEach(points, function (point) {
  88. var lat = invariant.getCoords(point)[1];
  89. if (!pointsByLatitude[lat]) pointsByLatitude[lat] = [];
  90. pointsByLatitude[lat].push(point);
  91. });
  92. // sort points (with the same latitude) by longitude
  93. var orderedRowsByLatitude = Object.keys(pointsByLatitude).map(function (lat) {
  94. var row = pointsByLatitude[lat];
  95. var rowOrderedByLongitude = row.sort(function (a, b) {
  96. return invariant.getCoords(a)[0] - invariant.getCoords(b)[0];
  97. });
  98. return rowOrderedByLongitude;
  99. });
  100. // sort rows (of points with the same latitude) by latitude
  101. var pointMatrix = orderedRowsByLatitude.sort(function (a, b) {
  102. if (flip) return invariant.getCoords(a[0])[1] - invariant.getCoords(b[0])[1];
  103. else return invariant.getCoords(b[0])[1] - invariant.getCoords(a[0])[1];
  104. });
  105. return pointMatrix;
  106. }
  107. /*!
  108. * @license GNU Affero General Public License.
  109. * Copyright (c) 2015, 2015 Ronny Lorenz <ronny@tbi.univie.ac.at>
  110. * v. 1.2.0
  111. * https://github.com/RaumZeit/MarchingSquares.js
  112. *
  113. * MarchingSquaresJS is free software: you can redistribute it and/or modify
  114. * it under the terms of the GNU Affero General Public License as published by
  115. * the Free Software Foundation, either version 3 of the License, or
  116. * (at your option) any later version.
  117. *
  118. * MarchingSquaresJS is distributed in the hope that it will be useful,
  119. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  120. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  121. * GNU Affero General Public License for more details.
  122. *
  123. * As additional permission under GNU Affero General Public License version 3
  124. * section 7, third-party projects (personal or commercial) may distribute,
  125. * include, or link against UNMODIFIED VERSIONS of MarchingSquaresJS without the
  126. * requirement that said third-party project for that reason alone becomes
  127. * subject to any requirement of the GNU Affero General Public License version 3.
  128. * Any modifications to MarchingSquaresJS, however, must be shared with the public
  129. * and made available.
  130. *
  131. * In summary this:
  132. * - allows you to use MarchingSquaresJS at no cost
  133. * - allows you to use MarchingSquaresJS for both personal and commercial purposes
  134. * - allows you to distribute UNMODIFIED VERSIONS of MarchingSquaresJS under any
  135. * license as long as this license notice is included
  136. * - enables you to keep the source code of your program that uses MarchingSquaresJS
  137. * undisclosed
  138. * - forces you to share any modifications you have made to MarchingSquaresJS,
  139. * e.g. bug-fixes
  140. *
  141. * You should have received a copy of the GNU Affero General Public License
  142. * along with MarchingSquaresJS. If not, see <http://www.gnu.org/licenses/>.
  143. */
  144. var defaultSettings = {
  145. successCallback: null,
  146. verbose: false,
  147. polygons: false,
  148. };
  149. var settings = {};
  150. /*
  151. Compute isobands(s) of a scalar 2D field given a certain
  152. threshold and a bandwidth by applying the Marching Squares
  153. Algorithm. The function returns a list of path coordinates
  154. either for individual polygons within each grid cell, or the
  155. outline of connected polygons.
  156. */
  157. function isoBands(data, minV, bandwidth, options) {
  158. /* process options */
  159. options = options ? options : {};
  160. var optionKeys = Object.keys(defaultSettings);
  161. for (var i = 0; i < optionKeys.length; i++) {
  162. var key = optionKeys[i];
  163. var val = options[key];
  164. val =
  165. typeof val !== "undefined" && val !== null ? val : defaultSettings[key];
  166. settings[key] = val;
  167. }
  168. if (settings.verbose)
  169. console.log(
  170. "MarchingSquaresJS-isoBands: computing isobands for [" +
  171. minV +
  172. ":" +
  173. (minV + bandwidth) +
  174. "]"
  175. );
  176. var grid = computeBandGrid(data, minV, bandwidth);
  177. var ret;
  178. if (settings.polygons) {
  179. if (settings.verbose)
  180. console.log(
  181. "MarchingSquaresJS-isoBands: returning single polygons for each grid cell"
  182. );
  183. ret = BandGrid2Areas(grid);
  184. } else {
  185. if (settings.verbose)
  186. console.log(
  187. "MarchingSquaresJS-isoBands: returning polygon paths for entire data grid"
  188. );
  189. ret = BandGrid2AreaPaths(grid);
  190. }
  191. if (typeof settings.successCallback === "function")
  192. settings.successCallback(ret);
  193. return ret;
  194. }
  195. /*
  196. Thats all for the public interface, below follows the actual
  197. implementation
  198. */
  199. /* Some private variables */
  200. var Node0 = 64,
  201. Node1 = 16,
  202. Node2 = 4,
  203. Node3 = 1;
  204. /*
  205. The look-up tables for tracing back the contour path
  206. of isoBands
  207. */
  208. var isoBandNextXTL = [];
  209. var isoBandNextYTL = [];
  210. var isoBandNextOTL = [];
  211. var isoBandNextXTR = [];
  212. var isoBandNextYTR = [];
  213. var isoBandNextOTR = [];
  214. var isoBandNextXRT = [];
  215. var isoBandNextYRT = [];
  216. var isoBandNextORT = [];
  217. var isoBandNextXRB = [];
  218. var isoBandNextYRB = [];
  219. var isoBandNextORB = [];
  220. var isoBandNextXBL = [];
  221. var isoBandNextYBL = [];
  222. var isoBandNextOBL = [];
  223. var isoBandNextXBR = [];
  224. var isoBandNextYBR = [];
  225. var isoBandNextOBR = [];
  226. var isoBandNextXLT = [];
  227. var isoBandNextYLT = [];
  228. var isoBandNextOLT = [];
  229. var isoBandNextXLB = [];
  230. var isoBandNextYLB = [];
  231. var isoBandNextOLB = [];
  232. isoBandNextXRT[85] = isoBandNextXRB[85] = -1;
  233. isoBandNextYRT[85] = isoBandNextYRB[85] = 0;
  234. isoBandNextORT[85] = isoBandNextORB[85] = 1;
  235. isoBandNextXLT[85] = isoBandNextXLB[85] = 1;
  236. isoBandNextYLT[85] = isoBandNextYLB[85] = 0;
  237. isoBandNextOLT[85] = isoBandNextOLB[85] = 1;
  238. isoBandNextXTL[85] = isoBandNextXTR[85] = 0;
  239. isoBandNextYTL[85] = isoBandNextYTR[85] = -1;
  240. isoBandNextOTL[85] = isoBandNextOBL[85] = 0;
  241. isoBandNextXBR[85] = isoBandNextXBL[85] = 0;
  242. isoBandNextYBR[85] = isoBandNextYBL[85] = 1;
  243. isoBandNextOTR[85] = isoBandNextOBR[85] = 1;
  244. /* triangle cases */
  245. isoBandNextXLB[1] = isoBandNextXLB[169] = 0;
  246. isoBandNextYLB[1] = isoBandNextYLB[169] = -1;
  247. isoBandNextOLB[1] = isoBandNextOLB[169] = 0;
  248. isoBandNextXBL[1] = isoBandNextXBL[169] = -1;
  249. isoBandNextYBL[1] = isoBandNextYBL[169] = 0;
  250. isoBandNextOBL[1] = isoBandNextOBL[169] = 0;
  251. isoBandNextXRB[4] = isoBandNextXRB[166] = 0;
  252. isoBandNextYRB[4] = isoBandNextYRB[166] = -1;
  253. isoBandNextORB[4] = isoBandNextORB[166] = 1;
  254. isoBandNextXBR[4] = isoBandNextXBR[166] = 1;
  255. isoBandNextYBR[4] = isoBandNextYBR[166] = 0;
  256. isoBandNextOBR[4] = isoBandNextOBR[166] = 0;
  257. isoBandNextXRT[16] = isoBandNextXRT[154] = 0;
  258. isoBandNextYRT[16] = isoBandNextYRT[154] = 1;
  259. isoBandNextORT[16] = isoBandNextORT[154] = 1;
  260. isoBandNextXTR[16] = isoBandNextXTR[154] = 1;
  261. isoBandNextYTR[16] = isoBandNextYTR[154] = 0;
  262. isoBandNextOTR[16] = isoBandNextOTR[154] = 1;
  263. isoBandNextXLT[64] = isoBandNextXLT[106] = 0;
  264. isoBandNextYLT[64] = isoBandNextYLT[106] = 1;
  265. isoBandNextOLT[64] = isoBandNextOLT[106] = 0;
  266. isoBandNextXTL[64] = isoBandNextXTL[106] = -1;
  267. isoBandNextYTL[64] = isoBandNextYTL[106] = 0;
  268. isoBandNextOTL[64] = isoBandNextOTL[106] = 1;
  269. /* single trapezoid cases */
  270. isoBandNextXLT[2] = isoBandNextXLT[168] = 0;
  271. isoBandNextYLT[2] = isoBandNextYLT[168] = -1;
  272. isoBandNextOLT[2] = isoBandNextOLT[168] = 1;
  273. isoBandNextXLB[2] = isoBandNextXLB[168] = 0;
  274. isoBandNextYLB[2] = isoBandNextYLB[168] = -1;
  275. isoBandNextOLB[2] = isoBandNextOLB[168] = 0;
  276. isoBandNextXBL[2] = isoBandNextXBL[168] = -1;
  277. isoBandNextYBL[2] = isoBandNextYBL[168] = 0;
  278. isoBandNextOBL[2] = isoBandNextOBL[168] = 0;
  279. isoBandNextXBR[2] = isoBandNextXBR[168] = -1;
  280. isoBandNextYBR[2] = isoBandNextYBR[168] = 0;
  281. isoBandNextOBR[2] = isoBandNextOBR[168] = 1;
  282. isoBandNextXRT[8] = isoBandNextXRT[162] = 0;
  283. isoBandNextYRT[8] = isoBandNextYRT[162] = -1;
  284. isoBandNextORT[8] = isoBandNextORT[162] = 0;
  285. isoBandNextXRB[8] = isoBandNextXRB[162] = 0;
  286. isoBandNextYRB[8] = isoBandNextYRB[162] = -1;
  287. isoBandNextORB[8] = isoBandNextORB[162] = 1;
  288. isoBandNextXBL[8] = isoBandNextXBL[162] = 1;
  289. isoBandNextYBL[8] = isoBandNextYBL[162] = 0;
  290. isoBandNextOBL[8] = isoBandNextOBL[162] = 1;
  291. isoBandNextXBR[8] = isoBandNextXBR[162] = 1;
  292. isoBandNextYBR[8] = isoBandNextYBR[162] = 0;
  293. isoBandNextOBR[8] = isoBandNextOBR[162] = 0;
  294. isoBandNextXRT[32] = isoBandNextXRT[138] = 0;
  295. isoBandNextYRT[32] = isoBandNextYRT[138] = 1;
  296. isoBandNextORT[32] = isoBandNextORT[138] = 1;
  297. isoBandNextXRB[32] = isoBandNextXRB[138] = 0;
  298. isoBandNextYRB[32] = isoBandNextYRB[138] = 1;
  299. isoBandNextORB[32] = isoBandNextORB[138] = 0;
  300. isoBandNextXTL[32] = isoBandNextXTL[138] = 1;
  301. isoBandNextYTL[32] = isoBandNextYTL[138] = 0;
  302. isoBandNextOTL[32] = isoBandNextOTL[138] = 0;
  303. isoBandNextXTR[32] = isoBandNextXTR[138] = 1;
  304. isoBandNextYTR[32] = isoBandNextYTR[138] = 0;
  305. isoBandNextOTR[32] = isoBandNextOTR[138] = 1;
  306. isoBandNextXLB[128] = isoBandNextXLB[42] = 0;
  307. isoBandNextYLB[128] = isoBandNextYLB[42] = 1;
  308. isoBandNextOLB[128] = isoBandNextOLB[42] = 1;
  309. isoBandNextXLT[128] = isoBandNextXLT[42] = 0;
  310. isoBandNextYLT[128] = isoBandNextYLT[42] = 1;
  311. isoBandNextOLT[128] = isoBandNextOLT[42] = 0;
  312. isoBandNextXTL[128] = isoBandNextXTL[42] = -1;
  313. isoBandNextYTL[128] = isoBandNextYTL[42] = 0;
  314. isoBandNextOTL[128] = isoBandNextOTL[42] = 1;
  315. isoBandNextXTR[128] = isoBandNextXTR[42] = -1;
  316. isoBandNextYTR[128] = isoBandNextYTR[42] = 0;
  317. isoBandNextOTR[128] = isoBandNextOTR[42] = 0;
  318. /* single rectangle cases */
  319. isoBandNextXRB[5] = isoBandNextXRB[165] = -1;
  320. isoBandNextYRB[5] = isoBandNextYRB[165] = 0;
  321. isoBandNextORB[5] = isoBandNextORB[165] = 0;
  322. isoBandNextXLB[5] = isoBandNextXLB[165] = 1;
  323. isoBandNextYLB[5] = isoBandNextYLB[165] = 0;
  324. isoBandNextOLB[5] = isoBandNextOLB[165] = 0;
  325. isoBandNextXBR[20] = isoBandNextXBR[150] = 0;
  326. isoBandNextYBR[20] = isoBandNextYBR[150] = 1;
  327. isoBandNextOBR[20] = isoBandNextOBR[150] = 1;
  328. isoBandNextXTR[20] = isoBandNextXTR[150] = 0;
  329. isoBandNextYTR[20] = isoBandNextYTR[150] = -1;
  330. isoBandNextOTR[20] = isoBandNextOTR[150] = 1;
  331. isoBandNextXRT[80] = isoBandNextXRT[90] = -1;
  332. isoBandNextYRT[80] = isoBandNextYRT[90] = 0;
  333. isoBandNextORT[80] = isoBandNextORT[90] = 1;
  334. isoBandNextXLT[80] = isoBandNextXLT[90] = 1;
  335. isoBandNextYLT[80] = isoBandNextYLT[90] = 0;
  336. isoBandNextOLT[80] = isoBandNextOLT[90] = 1;
  337. isoBandNextXBL[65] = isoBandNextXBL[105] = 0;
  338. isoBandNextYBL[65] = isoBandNextYBL[105] = 1;
  339. isoBandNextOBL[65] = isoBandNextOBL[105] = 0;
  340. isoBandNextXTL[65] = isoBandNextXTL[105] = 0;
  341. isoBandNextYTL[65] = isoBandNextYTL[105] = -1;
  342. isoBandNextOTL[65] = isoBandNextOTL[105] = 0;
  343. isoBandNextXRT[160] = isoBandNextXRT[10] = -1;
  344. isoBandNextYRT[160] = isoBandNextYRT[10] = 0;
  345. isoBandNextORT[160] = isoBandNextORT[10] = 1;
  346. isoBandNextXRB[160] = isoBandNextXRB[10] = -1;
  347. isoBandNextYRB[160] = isoBandNextYRB[10] = 0;
  348. isoBandNextORB[160] = isoBandNextORB[10] = 0;
  349. isoBandNextXLB[160] = isoBandNextXLB[10] = 1;
  350. isoBandNextYLB[160] = isoBandNextYLB[10] = 0;
  351. isoBandNextOLB[160] = isoBandNextOLB[10] = 0;
  352. isoBandNextXLT[160] = isoBandNextXLT[10] = 1;
  353. isoBandNextYLT[160] = isoBandNextYLT[10] = 0;
  354. isoBandNextOLT[160] = isoBandNextOLT[10] = 1;
  355. isoBandNextXBR[130] = isoBandNextXBR[40] = 0;
  356. isoBandNextYBR[130] = isoBandNextYBR[40] = 1;
  357. isoBandNextOBR[130] = isoBandNextOBR[40] = 1;
  358. isoBandNextXBL[130] = isoBandNextXBL[40] = 0;
  359. isoBandNextYBL[130] = isoBandNextYBL[40] = 1;
  360. isoBandNextOBL[130] = isoBandNextOBL[40] = 0;
  361. isoBandNextXTL[130] = isoBandNextXTL[40] = 0;
  362. isoBandNextYTL[130] = isoBandNextYTL[40] = -1;
  363. isoBandNextOTL[130] = isoBandNextOTL[40] = 0;
  364. isoBandNextXTR[130] = isoBandNextXTR[40] = 0;
  365. isoBandNextYTR[130] = isoBandNextYTR[40] = -1;
  366. isoBandNextOTR[130] = isoBandNextOTR[40] = 1;
  367. /* single hexagon cases */
  368. isoBandNextXRB[37] = isoBandNextXRB[133] = 0;
  369. isoBandNextYRB[37] = isoBandNextYRB[133] = 1;
  370. isoBandNextORB[37] = isoBandNextORB[133] = 1;
  371. isoBandNextXLB[37] = isoBandNextXLB[133] = 0;
  372. isoBandNextYLB[37] = isoBandNextYLB[133] = 1;
  373. isoBandNextOLB[37] = isoBandNextOLB[133] = 0;
  374. isoBandNextXTL[37] = isoBandNextXTL[133] = -1;
  375. isoBandNextYTL[37] = isoBandNextYTL[133] = 0;
  376. isoBandNextOTL[37] = isoBandNextOTL[133] = 0;
  377. isoBandNextXTR[37] = isoBandNextXTR[133] = 1;
  378. isoBandNextYTR[37] = isoBandNextYTR[133] = 0;
  379. isoBandNextOTR[37] = isoBandNextOTR[133] = 0;
  380. isoBandNextXBR[148] = isoBandNextXBR[22] = -1;
  381. isoBandNextYBR[148] = isoBandNextYBR[22] = 0;
  382. isoBandNextOBR[148] = isoBandNextOBR[22] = 0;
  383. isoBandNextXLB[148] = isoBandNextXLB[22] = 0;
  384. isoBandNextYLB[148] = isoBandNextYLB[22] = -1;
  385. isoBandNextOLB[148] = isoBandNextOLB[22] = 1;
  386. isoBandNextXLT[148] = isoBandNextXLT[22] = 0;
  387. isoBandNextYLT[148] = isoBandNextYLT[22] = 1;
  388. isoBandNextOLT[148] = isoBandNextOLT[22] = 1;
  389. isoBandNextXTR[148] = isoBandNextXTR[22] = -1;
  390. isoBandNextYTR[148] = isoBandNextYTR[22] = 0;
  391. isoBandNextOTR[148] = isoBandNextOTR[22] = 1;
  392. isoBandNextXRT[82] = isoBandNextXRT[88] = 0;
  393. isoBandNextYRT[82] = isoBandNextYRT[88] = -1;
  394. isoBandNextORT[82] = isoBandNextORT[88] = 1;
  395. isoBandNextXBR[82] = isoBandNextXBR[88] = 1;
  396. isoBandNextYBR[82] = isoBandNextYBR[88] = 0;
  397. isoBandNextOBR[82] = isoBandNextOBR[88] = 1;
  398. isoBandNextXBL[82] = isoBandNextXBL[88] = -1;
  399. isoBandNextYBL[82] = isoBandNextYBL[88] = 0;
  400. isoBandNextOBL[82] = isoBandNextOBL[88] = 1;
  401. isoBandNextXLT[82] = isoBandNextXLT[88] = 0;
  402. isoBandNextYLT[82] = isoBandNextYLT[88] = -1;
  403. isoBandNextOLT[82] = isoBandNextOLT[88] = 0;
  404. isoBandNextXRT[73] = isoBandNextXRT[97] = 0;
  405. isoBandNextYRT[73] = isoBandNextYRT[97] = 1;
  406. isoBandNextORT[73] = isoBandNextORT[97] = 0;
  407. isoBandNextXRB[73] = isoBandNextXRB[97] = 0;
  408. isoBandNextYRB[73] = isoBandNextYRB[97] = -1;
  409. isoBandNextORB[73] = isoBandNextORB[97] = 0;
  410. isoBandNextXBL[73] = isoBandNextXBL[97] = 1;
  411. isoBandNextYBL[73] = isoBandNextYBL[97] = 0;
  412. isoBandNextOBL[73] = isoBandNextOBL[97] = 0;
  413. isoBandNextXTL[73] = isoBandNextXTL[97] = 1;
  414. isoBandNextYTL[73] = isoBandNextYTL[97] = 0;
  415. isoBandNextOTL[73] = isoBandNextOTL[97] = 1;
  416. isoBandNextXRT[145] = isoBandNextXRT[25] = 0;
  417. isoBandNextYRT[145] = isoBandNextYRT[25] = -1;
  418. isoBandNextORT[145] = isoBandNextORT[25] = 0;
  419. isoBandNextXBL[145] = isoBandNextXBL[25] = 1;
  420. isoBandNextYBL[145] = isoBandNextYBL[25] = 0;
  421. isoBandNextOBL[145] = isoBandNextOBL[25] = 1;
  422. isoBandNextXLB[145] = isoBandNextXLB[25] = 0;
  423. isoBandNextYLB[145] = isoBandNextYLB[25] = 1;
  424. isoBandNextOLB[145] = isoBandNextOLB[25] = 1;
  425. isoBandNextXTR[145] = isoBandNextXTR[25] = -1;
  426. isoBandNextYTR[145] = isoBandNextYTR[25] = 0;
  427. isoBandNextOTR[145] = isoBandNextOTR[25] = 0;
  428. isoBandNextXRB[70] = isoBandNextXRB[100] = 0;
  429. isoBandNextYRB[70] = isoBandNextYRB[100] = 1;
  430. isoBandNextORB[70] = isoBandNextORB[100] = 0;
  431. isoBandNextXBR[70] = isoBandNextXBR[100] = -1;
  432. isoBandNextYBR[70] = isoBandNextYBR[100] = 0;
  433. isoBandNextOBR[70] = isoBandNextOBR[100] = 1;
  434. isoBandNextXLT[70] = isoBandNextXLT[100] = 0;
  435. isoBandNextYLT[70] = isoBandNextYLT[100] = -1;
  436. isoBandNextOLT[70] = isoBandNextOLT[100] = 1;
  437. isoBandNextXTL[70] = isoBandNextXTL[100] = 1;
  438. isoBandNextYTL[70] = isoBandNextYTL[100] = 0;
  439. isoBandNextOTL[70] = isoBandNextOTL[100] = 0;
  440. /* single pentagon cases */
  441. isoBandNextXRB[101] = isoBandNextXRB[69] = 0;
  442. isoBandNextYRB[101] = isoBandNextYRB[69] = 1;
  443. isoBandNextORB[101] = isoBandNextORB[69] = 0;
  444. isoBandNextXTL[101] = isoBandNextXTL[69] = 1;
  445. isoBandNextYTL[101] = isoBandNextYTL[69] = 0;
  446. isoBandNextOTL[101] = isoBandNextOTL[69] = 0;
  447. isoBandNextXLB[149] = isoBandNextXLB[21] = 0;
  448. isoBandNextYLB[149] = isoBandNextYLB[21] = 1;
  449. isoBandNextOLB[149] = isoBandNextOLB[21] = 1;
  450. isoBandNextXTR[149] = isoBandNextXTR[21] = -1;
  451. isoBandNextYTR[149] = isoBandNextYTR[21] = 0;
  452. isoBandNextOTR[149] = isoBandNextOTR[21] = 0;
  453. isoBandNextXBR[86] = isoBandNextXBR[84] = -1;
  454. isoBandNextYBR[86] = isoBandNextYBR[84] = 0;
  455. isoBandNextOBR[86] = isoBandNextOBR[84] = 1;
  456. isoBandNextXLT[86] = isoBandNextXLT[84] = 0;
  457. isoBandNextYLT[86] = isoBandNextYLT[84] = -1;
  458. isoBandNextOLT[86] = isoBandNextOLT[84] = 1;
  459. isoBandNextXRT[89] = isoBandNextXRT[81] = 0;
  460. isoBandNextYRT[89] = isoBandNextYRT[81] = -1;
  461. isoBandNextORT[89] = isoBandNextORT[81] = 0;
  462. isoBandNextXBL[89] = isoBandNextXBL[81] = 1;
  463. isoBandNextYBL[89] = isoBandNextYBL[81] = 0;
  464. isoBandNextOBL[89] = isoBandNextOBL[81] = 1;
  465. isoBandNextXRT[96] = isoBandNextXRT[74] = 0;
  466. isoBandNextYRT[96] = isoBandNextYRT[74] = 1;
  467. isoBandNextORT[96] = isoBandNextORT[74] = 0;
  468. isoBandNextXRB[96] = isoBandNextXRB[74] = -1;
  469. isoBandNextYRB[96] = isoBandNextYRB[74] = 0;
  470. isoBandNextORB[96] = isoBandNextORB[74] = 1;
  471. isoBandNextXLT[96] = isoBandNextXLT[74] = 1;
  472. isoBandNextYLT[96] = isoBandNextYLT[74] = 0;
  473. isoBandNextOLT[96] = isoBandNextOLT[74] = 0;
  474. isoBandNextXTL[96] = isoBandNextXTL[74] = 1;
  475. isoBandNextYTL[96] = isoBandNextYTL[74] = 0;
  476. isoBandNextOTL[96] = isoBandNextOTL[74] = 1;
  477. isoBandNextXRT[24] = isoBandNextXRT[146] = 0;
  478. isoBandNextYRT[24] = isoBandNextYRT[146] = -1;
  479. isoBandNextORT[24] = isoBandNextORT[146] = 1;
  480. isoBandNextXBR[24] = isoBandNextXBR[146] = 1;
  481. isoBandNextYBR[24] = isoBandNextYBR[146] = 0;
  482. isoBandNextOBR[24] = isoBandNextOBR[146] = 1;
  483. isoBandNextXBL[24] = isoBandNextXBL[146] = 0;
  484. isoBandNextYBL[24] = isoBandNextYBL[146] = 1;
  485. isoBandNextOBL[24] = isoBandNextOBL[146] = 1;
  486. isoBandNextXTR[24] = isoBandNextXTR[146] = 0;
  487. isoBandNextYTR[24] = isoBandNextYTR[146] = -1;
  488. isoBandNextOTR[24] = isoBandNextOTR[146] = 0;
  489. isoBandNextXRB[6] = isoBandNextXRB[164] = -1;
  490. isoBandNextYRB[6] = isoBandNextYRB[164] = 0;
  491. isoBandNextORB[6] = isoBandNextORB[164] = 1;
  492. isoBandNextXBR[6] = isoBandNextXBR[164] = -1;
  493. isoBandNextYBR[6] = isoBandNextYBR[164] = 0;
  494. isoBandNextOBR[6] = isoBandNextOBR[164] = 0;
  495. isoBandNextXLB[6] = isoBandNextXLB[164] = 0;
  496. isoBandNextYLB[6] = isoBandNextYLB[164] = -1;
  497. isoBandNextOLB[6] = isoBandNextOLB[164] = 1;
  498. isoBandNextXLT[6] = isoBandNextXLT[164] = 1;
  499. isoBandNextYLT[6] = isoBandNextYLT[164] = 0;
  500. isoBandNextOLT[6] = isoBandNextOLT[164] = 0;
  501. isoBandNextXBL[129] = isoBandNextXBL[41] = 0;
  502. isoBandNextYBL[129] = isoBandNextYBL[41] = 1;
  503. isoBandNextOBL[129] = isoBandNextOBL[41] = 1;
  504. isoBandNextXLB[129] = isoBandNextXLB[41] = 0;
  505. isoBandNextYLB[129] = isoBandNextYLB[41] = 1;
  506. isoBandNextOLB[129] = isoBandNextOLB[41] = 0;
  507. isoBandNextXTL[129] = isoBandNextXTL[41] = -1;
  508. isoBandNextYTL[129] = isoBandNextYTL[41] = 0;
  509. isoBandNextOTL[129] = isoBandNextOTL[41] = 0;
  510. isoBandNextXTR[129] = isoBandNextXTR[41] = 0;
  511. isoBandNextYTR[129] = isoBandNextYTR[41] = -1;
  512. isoBandNextOTR[129] = isoBandNextOTR[41] = 0;
  513. isoBandNextXBR[66] = isoBandNextXBR[104] = 0;
  514. isoBandNextYBR[66] = isoBandNextYBR[104] = 1;
  515. isoBandNextOBR[66] = isoBandNextOBR[104] = 0;
  516. isoBandNextXBL[66] = isoBandNextXBL[104] = -1;
  517. isoBandNextYBL[66] = isoBandNextYBL[104] = 0;
  518. isoBandNextOBL[66] = isoBandNextOBL[104] = 1;
  519. isoBandNextXLT[66] = isoBandNextXLT[104] = 0;
  520. isoBandNextYLT[66] = isoBandNextYLT[104] = -1;
  521. isoBandNextOLT[66] = isoBandNextOLT[104] = 0;
  522. isoBandNextXTL[66] = isoBandNextXTL[104] = 0;
  523. isoBandNextYTL[66] = isoBandNextYTL[104] = -1;
  524. isoBandNextOTL[66] = isoBandNextOTL[104] = 1;
  525. isoBandNextXRT[144] = isoBandNextXRT[26] = -1;
  526. isoBandNextYRT[144] = isoBandNextYRT[26] = 0;
  527. isoBandNextORT[144] = isoBandNextORT[26] = 0;
  528. isoBandNextXLB[144] = isoBandNextXLB[26] = 1;
  529. isoBandNextYLB[144] = isoBandNextYLB[26] = 0;
  530. isoBandNextOLB[144] = isoBandNextOLB[26] = 1;
  531. isoBandNextXLT[144] = isoBandNextXLT[26] = 0;
  532. isoBandNextYLT[144] = isoBandNextYLT[26] = 1;
  533. isoBandNextOLT[144] = isoBandNextOLT[26] = 1;
  534. isoBandNextXTR[144] = isoBandNextXTR[26] = -1;
  535. isoBandNextYTR[144] = isoBandNextYTR[26] = 0;
  536. isoBandNextOTR[144] = isoBandNextOTR[26] = 1;
  537. isoBandNextXRB[36] = isoBandNextXRB[134] = 0;
  538. isoBandNextYRB[36] = isoBandNextYRB[134] = 1;
  539. isoBandNextORB[36] = isoBandNextORB[134] = 1;
  540. isoBandNextXBR[36] = isoBandNextXBR[134] = 0;
  541. isoBandNextYBR[36] = isoBandNextYBR[134] = 1;
  542. isoBandNextOBR[36] = isoBandNextOBR[134] = 0;
  543. isoBandNextXTL[36] = isoBandNextXTL[134] = 0;
  544. isoBandNextYTL[36] = isoBandNextYTL[134] = -1;
  545. isoBandNextOTL[36] = isoBandNextOTL[134] = 1;
  546. isoBandNextXTR[36] = isoBandNextXTR[134] = 1;
  547. isoBandNextYTR[36] = isoBandNextYTR[134] = 0;
  548. isoBandNextOTR[36] = isoBandNextOTR[134] = 0;
  549. isoBandNextXRT[9] = isoBandNextXRT[161] = -1;
  550. isoBandNextYRT[9] = isoBandNextYRT[161] = 0;
  551. isoBandNextORT[9] = isoBandNextORT[161] = 0;
  552. isoBandNextXRB[9] = isoBandNextXRB[161] = 0;
  553. isoBandNextYRB[9] = isoBandNextYRB[161] = -1;
  554. isoBandNextORB[9] = isoBandNextORB[161] = 0;
  555. isoBandNextXBL[9] = isoBandNextXBL[161] = 1;
  556. isoBandNextYBL[9] = isoBandNextYBL[161] = 0;
  557. isoBandNextOBL[9] = isoBandNextOBL[161] = 0;
  558. isoBandNextXLB[9] = isoBandNextXLB[161] = 1;
  559. isoBandNextYLB[9] = isoBandNextYLB[161] = 0;
  560. isoBandNextOLB[9] = isoBandNextOLB[161] = 1;
  561. /* 8-sided cases */
  562. isoBandNextXRT[136] = 0;
  563. isoBandNextYRT[136] = 1;
  564. isoBandNextORT[136] = 1;
  565. isoBandNextXRB[136] = 0;
  566. isoBandNextYRB[136] = 1;
  567. isoBandNextORB[136] = 0;
  568. isoBandNextXBR[136] = -1;
  569. isoBandNextYBR[136] = 0;
  570. isoBandNextOBR[136] = 1;
  571. isoBandNextXBL[136] = -1;
  572. isoBandNextYBL[136] = 0;
  573. isoBandNextOBL[136] = 0;
  574. isoBandNextXLB[136] = 0;
  575. isoBandNextYLB[136] = -1;
  576. isoBandNextOLB[136] = 0;
  577. isoBandNextXLT[136] = 0;
  578. isoBandNextYLT[136] = -1;
  579. isoBandNextOLT[136] = 1;
  580. isoBandNextXTL[136] = 1;
  581. isoBandNextYTL[136] = 0;
  582. isoBandNextOTL[136] = 0;
  583. isoBandNextXTR[136] = 1;
  584. isoBandNextYTR[136] = 0;
  585. isoBandNextOTR[136] = 1;
  586. isoBandNextXRT[34] = 0;
  587. isoBandNextYRT[34] = -1;
  588. isoBandNextORT[34] = 0;
  589. isoBandNextXRB[34] = 0;
  590. isoBandNextYRB[34] = -1;
  591. isoBandNextORB[34] = 1;
  592. isoBandNextXBR[34] = 1;
  593. isoBandNextYBR[34] = 0;
  594. isoBandNextOBR[34] = 0;
  595. isoBandNextXBL[34] = 1;
  596. isoBandNextYBL[34] = 0;
  597. isoBandNextOBL[34] = 1;
  598. isoBandNextXLB[34] = 0;
  599. isoBandNextYLB[34] = 1;
  600. isoBandNextOLB[34] = 1;
  601. isoBandNextXLT[34] = 0;
  602. isoBandNextYLT[34] = 1;
  603. isoBandNextOLT[34] = 0;
  604. isoBandNextXTL[34] = -1;
  605. isoBandNextYTL[34] = 0;
  606. isoBandNextOTL[34] = 1;
  607. isoBandNextXTR[34] = -1;
  608. isoBandNextYTR[34] = 0;
  609. isoBandNextOTR[34] = 0;
  610. isoBandNextXRT[35] = 0;
  611. isoBandNextYRT[35] = 1;
  612. isoBandNextORT[35] = 1;
  613. isoBandNextXRB[35] = 0;
  614. isoBandNextYRB[35] = -1;
  615. isoBandNextORB[35] = 1;
  616. isoBandNextXBR[35] = 1;
  617. isoBandNextYBR[35] = 0;
  618. isoBandNextOBR[35] = 0;
  619. isoBandNextXBL[35] = -1;
  620. isoBandNextYBL[35] = 0;
  621. isoBandNextOBL[35] = 0;
  622. isoBandNextXLB[35] = 0;
  623. isoBandNextYLB[35] = -1;
  624. isoBandNextOLB[35] = 0;
  625. isoBandNextXLT[35] = 0;
  626. isoBandNextYLT[35] = 1;
  627. isoBandNextOLT[35] = 0;
  628. isoBandNextXTL[35] = -1;
  629. isoBandNextYTL[35] = 0;
  630. isoBandNextOTL[35] = 1;
  631. isoBandNextXTR[35] = 1;
  632. isoBandNextYTR[35] = 0;
  633. isoBandNextOTR[35] = 1;
  634. /* 6-sided cases */
  635. isoBandNextXRT[153] = 0;
  636. isoBandNextYRT[153] = 1;
  637. isoBandNextORT[153] = 1;
  638. isoBandNextXBL[153] = -1;
  639. isoBandNextYBL[153] = 0;
  640. isoBandNextOBL[153] = 0;
  641. isoBandNextXLB[153] = 0;
  642. isoBandNextYLB[153] = -1;
  643. isoBandNextOLB[153] = 0;
  644. isoBandNextXTR[153] = 1;
  645. isoBandNextYTR[153] = 0;
  646. isoBandNextOTR[153] = 1;
  647. isoBandNextXRB[102] = 0;
  648. isoBandNextYRB[102] = -1;
  649. isoBandNextORB[102] = 1;
  650. isoBandNextXBR[102] = 1;
  651. isoBandNextYBR[102] = 0;
  652. isoBandNextOBR[102] = 0;
  653. isoBandNextXLT[102] = 0;
  654. isoBandNextYLT[102] = 1;
  655. isoBandNextOLT[102] = 0;
  656. isoBandNextXTL[102] = -1;
  657. isoBandNextYTL[102] = 0;
  658. isoBandNextOTL[102] = 1;
  659. isoBandNextXRT[155] = 0;
  660. isoBandNextYRT[155] = -1;
  661. isoBandNextORT[155] = 0;
  662. isoBandNextXBL[155] = 1;
  663. isoBandNextYBL[155] = 0;
  664. isoBandNextOBL[155] = 1;
  665. isoBandNextXLB[155] = 0;
  666. isoBandNextYLB[155] = 1;
  667. isoBandNextOLB[155] = 1;
  668. isoBandNextXTR[155] = -1;
  669. isoBandNextYTR[155] = 0;
  670. isoBandNextOTR[155] = 0;
  671. isoBandNextXRB[103] = 0;
  672. isoBandNextYRB[103] = 1;
  673. isoBandNextORB[103] = 0;
  674. isoBandNextXBR[103] = -1;
  675. isoBandNextYBR[103] = 0;
  676. isoBandNextOBR[103] = 1;
  677. isoBandNextXLT[103] = 0;
  678. isoBandNextYLT[103] = -1;
  679. isoBandNextOLT[103] = 1;
  680. isoBandNextXTL[103] = 1;
  681. isoBandNextYTL[103] = 0;
  682. isoBandNextOTL[103] = 0;
  683. /* 7-sided cases */
  684. isoBandNextXRT[152] = 0;
  685. isoBandNextYRT[152] = 1;
  686. isoBandNextORT[152] = 1;
  687. isoBandNextXBR[152] = -1;
  688. isoBandNextYBR[152] = 0;
  689. isoBandNextOBR[152] = 1;
  690. isoBandNextXBL[152] = -1;
  691. isoBandNextYBL[152] = 0;
  692. isoBandNextOBL[152] = 0;
  693. isoBandNextXLB[152] = 0;
  694. isoBandNextYLB[152] = -1;
  695. isoBandNextOLB[152] = 0;
  696. isoBandNextXLT[152] = 0;
  697. isoBandNextYLT[152] = -1;
  698. isoBandNextOLT[152] = 1;
  699. isoBandNextXTR[152] = 1;
  700. isoBandNextYTR[152] = 0;
  701. isoBandNextOTR[152] = 1;
  702. isoBandNextXRT[156] = 0;
  703. isoBandNextYRT[156] = -1;
  704. isoBandNextORT[156] = 1;
  705. isoBandNextXBR[156] = 1;
  706. isoBandNextYBR[156] = 0;
  707. isoBandNextOBR[156] = 1;
  708. isoBandNextXBL[156] = -1;
  709. isoBandNextYBL[156] = 0;
  710. isoBandNextOBL[156] = 0;
  711. isoBandNextXLB[156] = 0;
  712. isoBandNextYLB[156] = -1;
  713. isoBandNextOLB[156] = 0;
  714. isoBandNextXLT[156] = 0;
  715. isoBandNextYLT[156] = 1;
  716. isoBandNextOLT[156] = 1;
  717. isoBandNextXTR[156] = -1;
  718. isoBandNextYTR[156] = 0;
  719. isoBandNextOTR[156] = 1;
  720. isoBandNextXRT[137] = 0;
  721. isoBandNextYRT[137] = 1;
  722. isoBandNextORT[137] = 1;
  723. isoBandNextXRB[137] = 0;
  724. isoBandNextYRB[137] = 1;
  725. isoBandNextORB[137] = 0;
  726. isoBandNextXBL[137] = -1;
  727. isoBandNextYBL[137] = 0;
  728. isoBandNextOBL[137] = 0;
  729. isoBandNextXLB[137] = 0;
  730. isoBandNextYLB[137] = -1;
  731. isoBandNextOLB[137] = 0;
  732. isoBandNextXTL[137] = 1;
  733. isoBandNextYTL[137] = 0;
  734. isoBandNextOTL[137] = 0;
  735. isoBandNextXTR[137] = 1;
  736. isoBandNextYTR[137] = 0;
  737. isoBandNextOTR[137] = 1;
  738. isoBandNextXRT[139] = 0;
  739. isoBandNextYRT[139] = 1;
  740. isoBandNextORT[139] = 1;
  741. isoBandNextXRB[139] = 0;
  742. isoBandNextYRB[139] = -1;
  743. isoBandNextORB[139] = 0;
  744. isoBandNextXBL[139] = 1;
  745. isoBandNextYBL[139] = 0;
  746. isoBandNextOBL[139] = 0;
  747. isoBandNextXLB[139] = 0;
  748. isoBandNextYLB[139] = 1;
  749. isoBandNextOLB[139] = 0;
  750. isoBandNextXTL[139] = -1;
  751. isoBandNextYTL[139] = 0;
  752. isoBandNextOTL[139] = 0;
  753. isoBandNextXTR[139] = 1;
  754. isoBandNextYTR[139] = 0;
  755. isoBandNextOTR[139] = 1;
  756. isoBandNextXRT[98] = 0;
  757. isoBandNextYRT[98] = -1;
  758. isoBandNextORT[98] = 0;
  759. isoBandNextXRB[98] = 0;
  760. isoBandNextYRB[98] = -1;
  761. isoBandNextORB[98] = 1;
  762. isoBandNextXBR[98] = 1;
  763. isoBandNextYBR[98] = 0;
  764. isoBandNextOBR[98] = 0;
  765. isoBandNextXBL[98] = 1;
  766. isoBandNextYBL[98] = 0;
  767. isoBandNextOBL[98] = 1;
  768. isoBandNextXLT[98] = 0;
  769. isoBandNextYLT[98] = 1;
  770. isoBandNextOLT[98] = 0;
  771. isoBandNextXTL[98] = -1;
  772. isoBandNextYTL[98] = 0;
  773. isoBandNextOTL[98] = 1;
  774. isoBandNextXRT[99] = 0;
  775. isoBandNextYRT[99] = 1;
  776. isoBandNextORT[99] = 0;
  777. isoBandNextXRB[99] = 0;
  778. isoBandNextYRB[99] = -1;
  779. isoBandNextORB[99] = 1;
  780. isoBandNextXBR[99] = 1;
  781. isoBandNextYBR[99] = 0;
  782. isoBandNextOBR[99] = 0;
  783. isoBandNextXBL[99] = -1;
  784. isoBandNextYBL[99] = 0;
  785. isoBandNextOBL[99] = 1;
  786. isoBandNextXLT[99] = 0;
  787. isoBandNextYLT[99] = -1;
  788. isoBandNextOLT[99] = 0;
  789. isoBandNextXTL[99] = 1;
  790. isoBandNextYTL[99] = 0;
  791. isoBandNextOTL[99] = 1;
  792. isoBandNextXRB[38] = 0;
  793. isoBandNextYRB[38] = -1;
  794. isoBandNextORB[38] = 1;
  795. isoBandNextXBR[38] = 1;
  796. isoBandNextYBR[38] = 0;
  797. isoBandNextOBR[38] = 0;
  798. isoBandNextXLB[38] = 0;
  799. isoBandNextYLB[38] = 1;
  800. isoBandNextOLB[38] = 1;
  801. isoBandNextXLT[38] = 0;
  802. isoBandNextYLT[38] = 1;
  803. isoBandNextOLT[38] = 0;
  804. isoBandNextXTL[38] = -1;
  805. isoBandNextYTL[38] = 0;
  806. isoBandNextOTL[38] = 1;
  807. isoBandNextXTR[38] = -1;
  808. isoBandNextYTR[38] = 0;
  809. isoBandNextOTR[38] = 0;
  810. isoBandNextXRB[39] = 0;
  811. isoBandNextYRB[39] = 1;
  812. isoBandNextORB[39] = 1;
  813. isoBandNextXBR[39] = -1;
  814. isoBandNextYBR[39] = 0;
  815. isoBandNextOBR[39] = 0;
  816. isoBandNextXLB[39] = 0;
  817. isoBandNextYLB[39] = -1;
  818. isoBandNextOLB[39] = 1;
  819. isoBandNextXLT[39] = 0;
  820. isoBandNextYLT[39] = 1;
  821. isoBandNextOLT[39] = 0;
  822. isoBandNextXTL[39] = -1;
  823. isoBandNextYTL[39] = 0;
  824. isoBandNextOTL[39] = 1;
  825. isoBandNextXTR[39] = 1;
  826. isoBandNextYTR[39] = 0;
  827. isoBandNextOTR[39] = 0;
  828. /*
  829. Define helper functions for the polygon_table
  830. */
  831. /* triangle cases */
  832. var p00 = function (cell) {
  833. return [
  834. [cell.bottomleft, 0],
  835. [0, 0],
  836. [0, cell.leftbottom],
  837. ];
  838. };
  839. var p01 = function (cell) {
  840. return [
  841. [1, cell.rightbottom],
  842. [1, 0],
  843. [cell.bottomright, 0],
  844. ];
  845. };
  846. var p02 = function (cell) {
  847. return [
  848. [cell.topright, 1],
  849. [1, 1],
  850. [1, cell.righttop],
  851. ];
  852. };
  853. var p03 = function (cell) {
  854. return [
  855. [0, cell.lefttop],
  856. [0, 1],
  857. [cell.topleft, 1],
  858. ];
  859. };
  860. /* trapezoid cases */
  861. var p04 = function (cell) {
  862. return [
  863. [cell.bottomright, 0],
  864. [cell.bottomleft, 0],
  865. [0, cell.leftbottom],
  866. [0, cell.lefttop],
  867. ];
  868. };
  869. var p05 = function (cell) {
  870. return [
  871. [cell.bottomright, 0],
  872. [cell.bottomleft, 0],
  873. [1, cell.righttop],
  874. [1, cell.rightbottom],
  875. ];
  876. };
  877. var p06 = function (cell) {
  878. return [
  879. [1, cell.righttop],
  880. [1, cell.rightbottom],
  881. [cell.topleft, 1],
  882. [cell.topright, 1],
  883. ];
  884. };
  885. var p07 = function (cell) {
  886. return [
  887. [0, cell.leftbottom],
  888. [0, cell.lefttop],
  889. [cell.topleft, 1],
  890. [cell.topright, 1],
  891. ];
  892. };
  893. /* rectangle cases */
  894. var p08 = function (cell) {
  895. return [
  896. [0, 0],
  897. [0, cell.leftbottom],
  898. [1, cell.rightbottom],
  899. [1, 0],
  900. ];
  901. };
  902. var p09 = function (cell) {
  903. return [
  904. [1, 0],
  905. [cell.bottomright, 0],
  906. [cell.topright, 1],
  907. [1, 1],
  908. ];
  909. };
  910. var p10 = function (cell) {
  911. return [
  912. [1, 1],
  913. [1, cell.righttop],
  914. [0, cell.lefttop],
  915. [0, 1],
  916. ];
  917. };
  918. var p11 = function (cell) {
  919. return [
  920. [cell.bottomleft, 0],
  921. [0, 0],
  922. [0, 1],
  923. [cell.topleft, 1],
  924. ];
  925. };
  926. var p12 = function (cell) {
  927. return [
  928. [1, cell.righttop],
  929. [1, cell.rightbottom],
  930. [0, cell.leftbottom],
  931. [0, cell.lefttop],
  932. ];
  933. };
  934. var p13 = function (cell) {
  935. return [
  936. [cell.topleft, 1],
  937. [cell.topright, 1],
  938. [cell.bottomright, 0],
  939. [cell.bottomleft, 0],
  940. ];
  941. };
  942. /* square case */
  943. var p14 = function () {
  944. return [
  945. [0, 0],
  946. [0, 1],
  947. [1, 1],
  948. [1, 0],
  949. ];
  950. };
  951. /* pentagon cases */
  952. var p15 = function (cell) {
  953. return [
  954. [1, cell.rightbottom],
  955. [1, 0],
  956. [0, 0],
  957. [0, 1],
  958. [cell.topleft, 1],
  959. ];
  960. };
  961. /* 1211 || 1011 */
  962. var p16 = function (cell) {
  963. return [
  964. [cell.topright, 1],
  965. [1, 1],
  966. [1, 0],
  967. [0, 0],
  968. [0, cell.leftbottom],
  969. ];
  970. };
  971. /* 2111 || 0111 */
  972. var p17 = function (cell) {
  973. return [
  974. [1, 0],
  975. [cell.bottomright, 0],
  976. [0, cell.lefttop],
  977. [0, 1],
  978. [1, 1],
  979. ];
  980. };
  981. /* 1112 || 1110 */
  982. var p18 = function (cell) {
  983. return [
  984. [1, 1],
  985. [1, cell.righttop],
  986. [cell.bottomleft, 0],
  987. [0, 0],
  988. [0, 1],
  989. ];
  990. };
  991. /* 1121 || 1101 */
  992. var p19 = function (cell) {
  993. return [
  994. [1, cell.righttop],
  995. [1, cell.rightbottom],
  996. [0, cell.lefttop],
  997. [0, 1],
  998. [cell.topleft, 1],
  999. ];
  1000. };
  1001. /* 1200 || 1022 */
  1002. var p20 = function (cell) {
  1003. return [
  1004. [1, 1],
  1005. [1, cell.righttop],
  1006. [cell.bottomright, 0],
  1007. [cell.bottomleft, 0],
  1008. [cell.topright, 1],
  1009. ];
  1010. };
  1011. /* 0120 || 2102 */
  1012. var p21 = function (cell) {
  1013. return [
  1014. [1, cell.rightbottom],
  1015. [1, 0],
  1016. [cell.bottomright, 0],
  1017. [0, cell.leftbottom],
  1018. [0, cell.lefttop],
  1019. ];
  1020. };
  1021. /* 0012 || 2210 */
  1022. var p22 = function (cell) {
  1023. return [
  1024. [cell.topright, 1],
  1025. [cell.bottomleft, 0],
  1026. [0, 0],
  1027. [0, cell.leftbottom],
  1028. [cell.topleft, 1],
  1029. ];
  1030. };
  1031. /* 2001 || 0221 */
  1032. var p23 = function (cell) {
  1033. return [
  1034. [cell.bottomright, 0],
  1035. [cell.bottomleft, 0],
  1036. [0, cell.lefttop],
  1037. [0, 1],
  1038. [cell.topleft, 1],
  1039. ];
  1040. };
  1041. /* 1002 || 1220 */
  1042. var p24 = function (cell) {
  1043. return [
  1044. [1, 1],
  1045. [1, cell.righttop],
  1046. [0, cell.leftbottom],
  1047. [0, cell.lefttop],
  1048. [cell.topright, 1],
  1049. ];
  1050. };
  1051. /* 2100 || 0122 */
  1052. var p25 = function (cell) {
  1053. return [
  1054. [1, cell.rightbottom],
  1055. [1, 0],
  1056. [cell.bottomright, 0],
  1057. [cell.topleft, 1],
  1058. [cell.topright, 1],
  1059. ];
  1060. };
  1061. /* 0210 || 2012 */
  1062. var p26 = function (cell) {
  1063. return [
  1064. [1, cell.righttop],
  1065. [1, cell.rightbottom],
  1066. [cell.bottomleft, 0],
  1067. [0, 0],
  1068. [0, cell.leftbottom],
  1069. ];
  1070. };
  1071. /* 0021 || 2201 */
  1072. /*hexagon cases */
  1073. var p27 = function (cell) {
  1074. return [
  1075. [1, cell.rightbottom],
  1076. [1, 0],
  1077. [0, 0],
  1078. [0, cell.leftbottom],
  1079. [cell.topleft, 1],
  1080. [cell.topright, 1],
  1081. ];
  1082. };
  1083. /* 0211 || 2011 */
  1084. var p28 = function (cell) {
  1085. return [
  1086. [1, 1],
  1087. [1, 0],
  1088. [cell.bottomright, 0],
  1089. [0, cell.leftbottom],
  1090. [0, cell.lefttop],
  1091. [cell.topright, 1],
  1092. ];
  1093. };
  1094. /* 2110 || 0112 */
  1095. var p29 = function (cell) {
  1096. return [
  1097. [1, 1],
  1098. [1, cell.righttop],
  1099. [cell.bottomright, 0],
  1100. [cell.bottomleft, 0],
  1101. [0, cell.lefttop],
  1102. [0, 1],
  1103. ];
  1104. };
  1105. /* 1102 || 1120 */
  1106. var p30 = function (cell) {
  1107. return [
  1108. [1, cell.righttop],
  1109. [1, cell.rightbottom],
  1110. [cell.bottomleft, 0],
  1111. [0, 0],
  1112. [0, 1],
  1113. [cell.topleft, 1],
  1114. ];
  1115. };
  1116. /* 1021 || 1201 */
  1117. var p31 = function (cell) {
  1118. return [
  1119. [1, 1],
  1120. [1, cell.righttop],
  1121. [cell.bottomleft, 0],
  1122. [0, 0],
  1123. [0, cell.leftbottom],
  1124. [cell.topright, 1],
  1125. ];
  1126. };
  1127. /* 2101 || 0121 */
  1128. var p32 = function (cell) {
  1129. return [
  1130. [1, cell.rightbottom],
  1131. [1, 0],
  1132. [cell.bottomright, 0],
  1133. [0, cell.lefttop],
  1134. [0, 1],
  1135. [cell.topleft, 1],
  1136. ];
  1137. };
  1138. /* 1012 || 1210 */
  1139. /* 8-sided cases */
  1140. var p33 = function (cell) {
  1141. return [
  1142. [1, cell.righttop],
  1143. [1, cell.rightbottom],
  1144. [cell.bottomright, 0],
  1145. [cell.bottomleft, 0],
  1146. [0, cell.leftbottom],
  1147. [0, cell.lefttop],
  1148. [cell.topleft, 1],
  1149. [cell.topright, 1],
  1150. ];
  1151. };
  1152. /* flipped == 1 state for 0202 and 2020 */
  1153. /* 6-sided cases */
  1154. var p34 = function (cell) {
  1155. return [
  1156. [1, 1],
  1157. [1, cell.righttop],
  1158. [cell.bottomleft, 0],
  1159. [0, 0],
  1160. [0, cell.leftbottom],
  1161. [cell.topright, 1],
  1162. ];
  1163. };
  1164. /* 0101 with flipped == 1 || 2121 with flipped == 1 */
  1165. var p35 = function (cell) {
  1166. return [
  1167. [1, cell.rightbottom],
  1168. [1, 0],
  1169. [cell.bottomright, 0],
  1170. [0, cell.lefttop],
  1171. [0, 1],
  1172. [cell.topleft, 1],
  1173. ];
  1174. };
  1175. /* 1010 with flipped == 1 || 1212 with flipped == 1 */
  1176. /* 7-sided cases */
  1177. var p36 = function (cell) {
  1178. return [
  1179. [1, 1],
  1180. [1, cell.righttop],
  1181. [cell.bottomright, 0],
  1182. [cell.bottomleft, 0],
  1183. [0, cell.leftbottom],
  1184. [0, cell.lefttop],
  1185. [cell.topright, 1],
  1186. ];
  1187. };
  1188. /* 2120 with flipped == 1 || 0102 with flipped == 1 */
  1189. var p37 = function (cell) {
  1190. return [
  1191. [1, cell.righttop],
  1192. [1, cell.rightbottom],
  1193. [cell.bottomleft, 0],
  1194. [0, 0],
  1195. [0, cell.leftbottom],
  1196. [cell.topleft, 1],
  1197. [cell.topright, 1],
  1198. ];
  1199. };
  1200. /* 2021 with flipped == 1 || 0201 with flipped == 1 */
  1201. var p38 = function (cell) {
  1202. return [
  1203. [1, cell.righttop],
  1204. [1, cell.rightbottom],
  1205. [cell.bottomright, 0],
  1206. [cell.bottomleft, 0],
  1207. [0, cell.lefttop],
  1208. [0, 1],
  1209. [cell.topleft, 1],
  1210. ];
  1211. };
  1212. /* 1202 with flipped == 1 || 1020 with flipped == 1 */
  1213. var p39 = function (cell) {
  1214. return [
  1215. [1, cell.rightbottom],
  1216. [1, 0],
  1217. [cell.bottomright, 0],
  1218. [0, cell.leftbottom],
  1219. [0, cell.lefttop],
  1220. [cell.topleft, 1],
  1221. [cell.topright, 1],
  1222. ];
  1223. };
  1224. /* 0212 with flipped == 1 || 2010 with flipped == 1 */
  1225. /*
  1226. The lookup tables for edge number given the polygon
  1227. is entered at a specific location
  1228. */
  1229. var isoBandEdgeRT = [];
  1230. var isoBandEdgeRB = [];
  1231. var isoBandEdgeBR = [];
  1232. var isoBandEdgeBL = [];
  1233. var isoBandEdgeLB = [];
  1234. var isoBandEdgeLT = [];
  1235. var isoBandEdgeTL = [];
  1236. var isoBandEdgeTR = [];
  1237. /* triangle cases */
  1238. isoBandEdgeBL[1] = isoBandEdgeLB[1] = 18;
  1239. isoBandEdgeBL[169] = isoBandEdgeLB[169] = 18;
  1240. isoBandEdgeBR[4] = isoBandEdgeRB[4] = 12;
  1241. isoBandEdgeBR[166] = isoBandEdgeRB[166] = 12;
  1242. isoBandEdgeRT[16] = isoBandEdgeTR[16] = 4;
  1243. isoBandEdgeRT[154] = isoBandEdgeTR[154] = 4;
  1244. isoBandEdgeLT[64] = isoBandEdgeTL[64] = 22;
  1245. isoBandEdgeLT[106] = isoBandEdgeTL[106] = 22;
  1246. /* trapezoid cases */
  1247. isoBandEdgeBR[2] = isoBandEdgeLT[2] = 17;
  1248. isoBandEdgeBL[2] = isoBandEdgeLB[2] = 18;
  1249. isoBandEdgeBR[168] = isoBandEdgeLT[168] = 17;
  1250. isoBandEdgeBL[168] = isoBandEdgeLB[168] = 18;
  1251. isoBandEdgeRT[8] = isoBandEdgeBL[8] = 9;
  1252. isoBandEdgeRB[8] = isoBandEdgeBR[8] = 12;
  1253. isoBandEdgeRT[162] = isoBandEdgeBL[162] = 9;
  1254. isoBandEdgeRB[162] = isoBandEdgeBR[162] = 12;
  1255. isoBandEdgeRT[32] = isoBandEdgeTR[32] = 4;
  1256. isoBandEdgeRB[32] = isoBandEdgeTL[32] = 1;
  1257. isoBandEdgeRT[138] = isoBandEdgeTR[138] = 4;
  1258. isoBandEdgeRB[138] = isoBandEdgeTL[138] = 1;
  1259. isoBandEdgeLB[128] = isoBandEdgeTR[128] = 21;
  1260. isoBandEdgeLT[128] = isoBandEdgeTL[128] = 22;
  1261. isoBandEdgeLB[42] = isoBandEdgeTR[42] = 21;
  1262. isoBandEdgeLT[42] = isoBandEdgeTL[42] = 22;
  1263. /* rectangle cases */
  1264. isoBandEdgeRB[5] = isoBandEdgeLB[5] = 14;
  1265. isoBandEdgeRB[165] = isoBandEdgeLB[165] = 14;
  1266. isoBandEdgeBR[20] = isoBandEdgeTR[20] = 6;
  1267. isoBandEdgeBR[150] = isoBandEdgeTR[150] = 6;
  1268. isoBandEdgeRT[80] = isoBandEdgeLT[80] = 11;
  1269. isoBandEdgeRT[90] = isoBandEdgeLT[90] = 11;
  1270. isoBandEdgeBL[65] = isoBandEdgeTL[65] = 3;
  1271. isoBandEdgeBL[105] = isoBandEdgeTL[105] = 3;
  1272. isoBandEdgeRT[160] = isoBandEdgeLT[160] = 11;
  1273. isoBandEdgeRB[160] = isoBandEdgeLB[160] = 14;
  1274. isoBandEdgeRT[10] = isoBandEdgeLT[10] = 11;
  1275. isoBandEdgeRB[10] = isoBandEdgeLB[10] = 14;
  1276. isoBandEdgeBR[130] = isoBandEdgeTR[130] = 6;
  1277. isoBandEdgeBL[130] = isoBandEdgeTL[130] = 3;
  1278. isoBandEdgeBR[40] = isoBandEdgeTR[40] = 6;
  1279. isoBandEdgeBL[40] = isoBandEdgeTL[40] = 3;
  1280. /* pentagon cases */
  1281. isoBandEdgeRB[101] = isoBandEdgeTL[101] = 1;
  1282. isoBandEdgeRB[69] = isoBandEdgeTL[69] = 1;
  1283. isoBandEdgeLB[149] = isoBandEdgeTR[149] = 21;
  1284. isoBandEdgeLB[21] = isoBandEdgeTR[21] = 21;
  1285. isoBandEdgeBR[86] = isoBandEdgeLT[86] = 17;
  1286. isoBandEdgeBR[84] = isoBandEdgeLT[84] = 17;
  1287. isoBandEdgeRT[89] = isoBandEdgeBL[89] = 9;
  1288. isoBandEdgeRT[81] = isoBandEdgeBL[81] = 9;
  1289. isoBandEdgeRT[96] = isoBandEdgeTL[96] = 0;
  1290. isoBandEdgeRB[96] = isoBandEdgeLT[96] = 15;
  1291. isoBandEdgeRT[74] = isoBandEdgeTL[74] = 0;
  1292. isoBandEdgeRB[74] = isoBandEdgeLT[74] = 15;
  1293. isoBandEdgeRT[24] = isoBandEdgeBR[24] = 8;
  1294. isoBandEdgeBL[24] = isoBandEdgeTR[24] = 7;
  1295. isoBandEdgeRT[146] = isoBandEdgeBR[146] = 8;
  1296. isoBandEdgeBL[146] = isoBandEdgeTR[146] = 7;
  1297. isoBandEdgeRB[6] = isoBandEdgeLT[6] = 15;
  1298. isoBandEdgeBR[6] = isoBandEdgeLB[6] = 16;
  1299. isoBandEdgeRB[164] = isoBandEdgeLT[164] = 15;
  1300. isoBandEdgeBR[164] = isoBandEdgeLB[164] = 16;
  1301. isoBandEdgeBL[129] = isoBandEdgeTR[129] = 7;
  1302. isoBandEdgeLB[129] = isoBandEdgeTL[129] = 20;
  1303. isoBandEdgeBL[41] = isoBandEdgeTR[41] = 7;
  1304. isoBandEdgeLB[41] = isoBandEdgeTL[41] = 20;
  1305. isoBandEdgeBR[66] = isoBandEdgeTL[66] = 2;
  1306. isoBandEdgeBL[66] = isoBandEdgeLT[66] = 19;
  1307. isoBandEdgeBR[104] = isoBandEdgeTL[104] = 2;
  1308. isoBandEdgeBL[104] = isoBandEdgeLT[104] = 19;
  1309. isoBandEdgeRT[144] = isoBandEdgeLB[144] = 10;
  1310. isoBandEdgeLT[144] = isoBandEdgeTR[144] = 23;
  1311. isoBandEdgeRT[26] = isoBandEdgeLB[26] = 10;
  1312. isoBandEdgeLT[26] = isoBandEdgeTR[26] = 23;
  1313. isoBandEdgeRB[36] = isoBandEdgeTR[36] = 5;
  1314. isoBandEdgeBR[36] = isoBandEdgeTL[36] = 2;
  1315. isoBandEdgeRB[134] = isoBandEdgeTR[134] = 5;
  1316. isoBandEdgeBR[134] = isoBandEdgeTL[134] = 2;
  1317. isoBandEdgeRT[9] = isoBandEdgeLB[9] = 10;
  1318. isoBandEdgeRB[9] = isoBandEdgeBL[9] = 13;
  1319. isoBandEdgeRT[161] = isoBandEdgeLB[161] = 10;
  1320. isoBandEdgeRB[161] = isoBandEdgeBL[161] = 13;
  1321. /* hexagon cases */
  1322. isoBandEdgeRB[37] = isoBandEdgeTR[37] = 5;
  1323. isoBandEdgeLB[37] = isoBandEdgeTL[37] = 20;
  1324. isoBandEdgeRB[133] = isoBandEdgeTR[133] = 5;
  1325. isoBandEdgeLB[133] = isoBandEdgeTL[133] = 20;
  1326. isoBandEdgeBR[148] = isoBandEdgeLB[148] = 16;
  1327. isoBandEdgeLT[148] = isoBandEdgeTR[148] = 23;
  1328. isoBandEdgeBR[22] = isoBandEdgeLB[22] = 16;
  1329. isoBandEdgeLT[22] = isoBandEdgeTR[22] = 23;
  1330. isoBandEdgeRT[82] = isoBandEdgeBR[82] = 8;
  1331. isoBandEdgeBL[82] = isoBandEdgeLT[82] = 19;
  1332. isoBandEdgeRT[88] = isoBandEdgeBR[88] = 8;
  1333. isoBandEdgeBL[88] = isoBandEdgeLT[88] = 19;
  1334. isoBandEdgeRT[73] = isoBandEdgeTL[73] = 0;
  1335. isoBandEdgeRB[73] = isoBandEdgeBL[73] = 13;
  1336. isoBandEdgeRT[97] = isoBandEdgeTL[97] = 0;
  1337. isoBandEdgeRB[97] = isoBandEdgeBL[97] = 13;
  1338. isoBandEdgeRT[145] = isoBandEdgeBL[145] = 9;
  1339. isoBandEdgeLB[145] = isoBandEdgeTR[145] = 21;
  1340. isoBandEdgeRT[25] = isoBandEdgeBL[25] = 9;
  1341. isoBandEdgeLB[25] = isoBandEdgeTR[25] = 21;
  1342. isoBandEdgeRB[70] = isoBandEdgeTL[70] = 1;
  1343. isoBandEdgeBR[70] = isoBandEdgeLT[70] = 17;
  1344. isoBandEdgeRB[100] = isoBandEdgeTL[100] = 1;
  1345. isoBandEdgeBR[100] = isoBandEdgeLT[100] = 17;
  1346. /* 8-sided cases */
  1347. isoBandEdgeRT[34] = isoBandEdgeBL[34] = 9;
  1348. isoBandEdgeRB[34] = isoBandEdgeBR[34] = 12;
  1349. isoBandEdgeLB[34] = isoBandEdgeTR[34] = 21;
  1350. isoBandEdgeLT[34] = isoBandEdgeTL[34] = 22;
  1351. isoBandEdgeRT[136] = isoBandEdgeTR[136] = 4;
  1352. isoBandEdgeRB[136] = isoBandEdgeTL[136] = 1;
  1353. isoBandEdgeBR[136] = isoBandEdgeLT[136] = 17;
  1354. isoBandEdgeBL[136] = isoBandEdgeLB[136] = 18;
  1355. isoBandEdgeRT[35] = isoBandEdgeTR[35] = 4;
  1356. isoBandEdgeRB[35] = isoBandEdgeBR[35] = 12;
  1357. isoBandEdgeBL[35] = isoBandEdgeLB[35] = 18;
  1358. isoBandEdgeLT[35] = isoBandEdgeTL[35] = 22;
  1359. /* 6-sided cases */
  1360. isoBandEdgeRT[153] = isoBandEdgeTR[153] = 4;
  1361. isoBandEdgeBL[153] = isoBandEdgeLB[153] = 18;
  1362. isoBandEdgeRB[102] = isoBandEdgeBR[102] = 12;
  1363. isoBandEdgeLT[102] = isoBandEdgeTL[102] = 22;
  1364. isoBandEdgeRT[155] = isoBandEdgeBL[155] = 9;
  1365. isoBandEdgeLB[155] = isoBandEdgeTR[155] = 23;
  1366. isoBandEdgeRB[103] = isoBandEdgeTL[103] = 1;
  1367. isoBandEdgeBR[103] = isoBandEdgeLT[103] = 17;
  1368. /* 7-sided cases */
  1369. isoBandEdgeRT[152] = isoBandEdgeTR[152] = 4;
  1370. isoBandEdgeBR[152] = isoBandEdgeLT[152] = 17;
  1371. isoBandEdgeBL[152] = isoBandEdgeLB[152] = 18;
  1372. isoBandEdgeRT[156] = isoBandEdgeBR[156] = 8;
  1373. isoBandEdgeBL[156] = isoBandEdgeLB[156] = 18;
  1374. isoBandEdgeLT[156] = isoBandEdgeTR[156] = 23;
  1375. isoBandEdgeRT[137] = isoBandEdgeTR[137] = 4;
  1376. isoBandEdgeRB[137] = isoBandEdgeTL[137] = 1;
  1377. isoBandEdgeBL[137] = isoBandEdgeLB[137] = 18;
  1378. isoBandEdgeRT[139] = isoBandEdgeTR[139] = 4;
  1379. isoBandEdgeRB[139] = isoBandEdgeBL[139] = 13;
  1380. isoBandEdgeLB[139] = isoBandEdgeTL[139] = 20;
  1381. isoBandEdgeRT[98] = isoBandEdgeBL[98] = 9;
  1382. isoBandEdgeRB[98] = isoBandEdgeBR[98] = 12;
  1383. isoBandEdgeLT[98] = isoBandEdgeTL[98] = 22;
  1384. isoBandEdgeRT[99] = isoBandEdgeTL[99] = 0;
  1385. isoBandEdgeRB[99] = isoBandEdgeBR[99] = 12;
  1386. isoBandEdgeBL[99] = isoBandEdgeLT[99] = 19;
  1387. isoBandEdgeRB[38] = isoBandEdgeBR[38] = 12;
  1388. isoBandEdgeLB[38] = isoBandEdgeTR[38] = 21;
  1389. isoBandEdgeLT[38] = isoBandEdgeTL[38] = 22;
  1390. isoBandEdgeRB[39] = isoBandEdgeTR[39] = 5;
  1391. isoBandEdgeBR[39] = isoBandEdgeLB[39] = 16;
  1392. isoBandEdgeLT[39] = isoBandEdgeTL[39] = 22;
  1393. /*
  1394. The lookup tables for all different polygons that
  1395. may appear within a grid cell
  1396. */
  1397. var polygon_table = [];
  1398. /* triangle cases */
  1399. polygon_table[1] = polygon_table[169] = p00; /* 2221 || 0001 */
  1400. polygon_table[4] = polygon_table[166] = p01; /* 2212 || 0010 */
  1401. polygon_table[16] = polygon_table[154] = p02; /* 2122 || 0100 */
  1402. polygon_table[64] = polygon_table[106] = p03; /* 1222 || 1000 */
  1403. /* trapezoid cases */
  1404. polygon_table[168] = polygon_table[2] = p04; /* 2220 || 0002 */
  1405. polygon_table[162] = polygon_table[8] = p05; /* 2202 || 0020 */
  1406. polygon_table[138] = polygon_table[32] = p06; /* 2022 || 0200 */
  1407. polygon_table[42] = polygon_table[128] = p07; /* 0222 || 2000 */
  1408. /* rectangle cases */
  1409. polygon_table[5] = polygon_table[165] = p08; /* 0011 || 2211 */
  1410. polygon_table[20] = polygon_table[150] = p09; /* 0110 || 2112 */
  1411. polygon_table[80] = polygon_table[90] = p10; /* 1100 || 1122 */
  1412. polygon_table[65] = polygon_table[105] = p11; /* 1001 || 1221 */
  1413. polygon_table[160] = polygon_table[10] = p12; /* 2200 || 0022 */
  1414. polygon_table[130] = polygon_table[40] = p13; /* 2002 || 0220 */
  1415. /* square case */
  1416. polygon_table[85] = p14; /* 1111 */
  1417. /* pentagon cases */
  1418. polygon_table[101] = polygon_table[69] = p15; /* 1211 || 1011 */
  1419. polygon_table[149] = polygon_table[21] = p16; /* 2111 || 0111 */
  1420. polygon_table[86] = polygon_table[84] = p17; /* 1112 || 1110 */
  1421. polygon_table[89] = polygon_table[81] = p18; /* 1121 || 1101 */
  1422. polygon_table[96] = polygon_table[74] = p19; /* 1200 || 1022 */
  1423. polygon_table[24] = polygon_table[146] = p20; /* 0120 || 2102 */
  1424. polygon_table[6] = polygon_table[164] = p21; /* 0012 || 2210 */
  1425. polygon_table[129] = polygon_table[41] = p22; /* 2001 || 0221 */
  1426. polygon_table[66] = polygon_table[104] = p23; /* 1002 || 1220 */
  1427. polygon_table[144] = polygon_table[26] = p24; /* 2100 || 0122 */
  1428. polygon_table[36] = polygon_table[134] = p25; /* 0210 || 2012 */
  1429. polygon_table[9] = polygon_table[161] = p26; /* 0021 || 2201 */
  1430. /* hexagon cases */
  1431. polygon_table[37] = polygon_table[133] = p27; /* 0211 || 2011 */
  1432. polygon_table[148] = polygon_table[22] = p28; /* 2110 || 0112 */
  1433. polygon_table[82] = polygon_table[88] = p29; /* 1102 || 1120 */
  1434. polygon_table[73] = polygon_table[97] = p30; /* 1021 || 1201 */
  1435. polygon_table[145] = polygon_table[25] = p31; /* 2101 || 0121 */
  1436. polygon_table[70] = polygon_table[100] = p32; /* 1012 || 1210 */
  1437. /* 8-sided cases */
  1438. polygon_table[34] = function (c) {
  1439. return [p07(c), p05(c)];
  1440. }; /* 0202 || 2020 with flipped == 0 */
  1441. polygon_table[35] = p33; /* flipped == 1 state for 0202 and 2020 */
  1442. polygon_table[136] = function (c) {
  1443. return [p06(c), p04(c)];
  1444. }; /* 2020 || 0202 with flipped == 0 */
  1445. /* 6-sided cases */
  1446. polygon_table[153] = function (c) {
  1447. return [p02(c), p00(c)];
  1448. }; /* 0101 with flipped == 0 || 2121 with flipped == 2 */
  1449. polygon_table[102] = function (c) {
  1450. return [p01(c), p03(c)];
  1451. }; /* 1010 with flipped == 0 || 1212 with flipped == 2 */
  1452. polygon_table[155] = p34; /* 0101 with flipped == 1 || 2121 with flipped == 1 */
  1453. polygon_table[103] = p35; /* 1010 with flipped == 1 || 1212 with flipped == 1 */
  1454. /* 7-sided cases */
  1455. polygon_table[152] = function (c) {
  1456. return [p02(c), p04(c)];
  1457. }; /* 2120 with flipped == 2 || 0102 with flipped == 0 */
  1458. polygon_table[156] = p36; /* 2120 with flipped == 1 || 0102 with flipped == 1 */
  1459. polygon_table[137] = function (c) {
  1460. return [p06(c), p00(c)];
  1461. }; /* 2021 with flipped == 2 || 0201 with flipped == 0 */
  1462. polygon_table[139] = p37; /* 2021 with flipped == 1 || 0201 with flipped == 1 */
  1463. polygon_table[98] = function (c) {
  1464. return [p05(c), p03(c)];
  1465. }; /* 1202 with flipped == 2 || 1020 with flipped == 0 */
  1466. polygon_table[99] = p38; /* 1202 with flipped == 1 || 1020 with flipped == 1 */
  1467. polygon_table[38] = function (c) {
  1468. return [p01(c), p07(c)];
  1469. }; /* 0212 with flipped == 2 || 2010 with flipped == 0 */
  1470. polygon_table[39] = p39; /* 0212 with flipped == 1 || 2010 with flipped == 1 */
  1471. /*
  1472. ####################################
  1473. Some small helper functions
  1474. ####################################
  1475. */
  1476. /* assume that x1 == 1 && x0 == 0 */
  1477. function interpolateX(y, y0, y1) {
  1478. return (y - y0) / (y1 - y0);
  1479. }
  1480. function isArray(myArray) {
  1481. return myArray.constructor.toString().indexOf("Array") > -1;
  1482. }
  1483. /*
  1484. ####################################
  1485. Below is the actual Marching Squares implementation
  1486. ####################################
  1487. */
  1488. function computeBandGrid(data, minV, bandwidth) {
  1489. var rows = data.length - 1;
  1490. var cols = data[0].length - 1;
  1491. var BandGrid = { rows: rows, cols: cols, cells: [] };
  1492. var maxV = minV + Math.abs(bandwidth);
  1493. for (var j = 0; j < rows; ++j) {
  1494. BandGrid.cells[j] = [];
  1495. for (var i = 0; i < cols; ++i) {
  1496. /* compose the 4-trit corner representation */
  1497. var cval = 0;
  1498. var tl = data[j + 1][i];
  1499. var tr = data[j + 1][i + 1];
  1500. var br = data[j][i + 1];
  1501. var bl = data[j][i];
  1502. if (isNaN(tl) || isNaN(tr) || isNaN(br) || isNaN(bl)) {
  1503. continue;
  1504. }
  1505. cval |= tl < minV ? 0 : tl > maxV ? 128 : 64;
  1506. cval |= tr < minV ? 0 : tr > maxV ? 32 : 16;
  1507. cval |= br < minV ? 0 : br > maxV ? 8 : 4;
  1508. cval |= bl < minV ? 0 : bl > maxV ? 2 : 1;
  1509. var cval_real = +cval;
  1510. /* resolve ambiguity via averaging */
  1511. var flipped = 0;
  1512. if (
  1513. cval === 17 /* 0101 */ ||
  1514. cval === 18 /* 0102 */ ||
  1515. cval === 33 /* 0201 */ ||
  1516. cval === 34 /* 0202 */ ||
  1517. cval === 38 /* 0212 */ ||
  1518. cval === 68 /* 1010 */ ||
  1519. cval === 72 /* 1020 */ ||
  1520. cval === 98 /* 1202 */ ||
  1521. cval === 102 /* 1212 */ ||
  1522. cval === 132 /* 2010 */ ||
  1523. cval === 136 /* 2020 */ ||
  1524. cval === 137 /* 2021 */ ||
  1525. cval === 152 /* 2120 */ ||
  1526. cval === 153 /* 2121 */
  1527. ) {
  1528. var average = (tl + tr + br + bl) / 4;
  1529. /* set flipped state */
  1530. flipped = average > maxV ? 2 : average < minV ? 0 : 1;
  1531. /* adjust cval for flipped cases */
  1532. /* 8-sided cases */
  1533. if (cval === 34) {
  1534. if (flipped === 1) {
  1535. cval = 35;
  1536. } else if (flipped === 0) {
  1537. cval = 136;
  1538. }
  1539. } else if (cval === 136) {
  1540. if (flipped === 1) {
  1541. cval = 35;
  1542. flipped = 4;
  1543. } else if (flipped === 0) {
  1544. cval = 34;
  1545. }
  1546. } else if (cval === 17) {
  1547. /* 6-sided polygon cases */
  1548. if (flipped === 1) {
  1549. cval = 155;
  1550. flipped = 4;
  1551. } else if (flipped === 0) {
  1552. cval = 153;
  1553. }
  1554. } else if (cval === 68) {
  1555. if (flipped === 1) {
  1556. cval = 103;
  1557. flipped = 4;
  1558. } else if (flipped === 0) {
  1559. cval = 102;
  1560. }
  1561. } else if (cval === 153) {
  1562. if (flipped === 1) cval = 155;
  1563. } else if (cval === 102) {
  1564. if (flipped === 1) cval = 103;
  1565. } else if (cval === 152) {
  1566. /* 7-sided polygon cases */
  1567. if (flipped < 2) {
  1568. cval = 156;
  1569. flipped = 1;
  1570. }
  1571. } else if (cval === 137) {
  1572. if (flipped < 2) {
  1573. cval = 139;
  1574. flipped = 1;
  1575. }
  1576. } else if (cval === 98) {
  1577. if (flipped < 2) {
  1578. cval = 99;
  1579. flipped = 1;
  1580. }
  1581. } else if (cval === 38) {
  1582. if (flipped < 2) {
  1583. cval = 39;
  1584. flipped = 1;
  1585. }
  1586. } else if (cval === 18) {
  1587. if (flipped > 0) {
  1588. cval = 156;
  1589. flipped = 4;
  1590. } else {
  1591. cval = 152;
  1592. }
  1593. } else if (cval === 33) {
  1594. if (flipped > 0) {
  1595. cval = 139;
  1596. flipped = 4;
  1597. } else {
  1598. cval = 137;
  1599. }
  1600. } else if (cval === 72) {
  1601. if (flipped > 0) {
  1602. cval = 99;
  1603. flipped = 4;
  1604. } else {
  1605. cval = 98;
  1606. }
  1607. } else if (cval === 132) {
  1608. if (flipped > 0) {
  1609. cval = 39;
  1610. flipped = 4;
  1611. } else {
  1612. cval = 38;
  1613. }
  1614. }
  1615. }
  1616. /* add cell to BandGrid if it contains at least one polygon-side */
  1617. if (cval != 0 && cval != 170) {
  1618. var topleft,
  1619. topright,
  1620. bottomleft,
  1621. bottomright,
  1622. righttop,
  1623. rightbottom,
  1624. lefttop,
  1625. leftbottom;
  1626. topleft = topright = bottomleft = bottomright = righttop = rightbottom = lefttop = leftbottom = 0.5;
  1627. var edges = [];
  1628. /* do interpolation here */
  1629. /* 1st Triangles */
  1630. if (cval === 1) {
  1631. /* 0001 */
  1632. bottomleft = 1 - interpolateX(minV, br, bl);
  1633. leftbottom = 1 - interpolateX(minV, tl, bl);
  1634. edges.push(isoBandEdgeBL[cval]);
  1635. } else if (cval === 169) {
  1636. /* 2221 */
  1637. bottomleft = interpolateX(maxV, bl, br);
  1638. leftbottom = interpolateX(maxV, bl, tl);
  1639. edges.push(isoBandEdgeBL[cval]);
  1640. } else if (cval === 4) {
  1641. /* 0010 */
  1642. rightbottom = 1 - interpolateX(minV, tr, br);
  1643. bottomright = interpolateX(minV, bl, br);
  1644. edges.push(isoBandEdgeRB[cval]);
  1645. } else if (cval === 166) {
  1646. /* 2212 */
  1647. rightbottom = interpolateX(maxV, br, tr);
  1648. bottomright = 1 - interpolateX(maxV, br, bl);
  1649. edges.push(isoBandEdgeRB[cval]);
  1650. } else if (cval === 16) {
  1651. /* 0100 */
  1652. righttop = interpolateX(minV, br, tr);
  1653. topright = interpolateX(minV, tl, tr);
  1654. edges.push(isoBandEdgeRT[cval]);
  1655. } else if (cval === 154) {
  1656. /* 2122 */
  1657. righttop = 1 - interpolateX(maxV, tr, br);
  1658. topright = 1 - interpolateX(maxV, tr, tl);
  1659. edges.push(isoBandEdgeRT[cval]);
  1660. } else if (cval === 64) {
  1661. /* 1000 */
  1662. lefttop = interpolateX(minV, bl, tl);
  1663. topleft = 1 - interpolateX(minV, tr, tl);
  1664. edges.push(isoBandEdgeLT[cval]);
  1665. } else if (cval === 106) {
  1666. /* 1222 */
  1667. lefttop = 1 - interpolateX(maxV, tl, bl);
  1668. topleft = interpolateX(maxV, tl, tr);
  1669. edges.push(isoBandEdgeLT[cval]);
  1670. } else if (cval === 168) {
  1671. /* 2nd Trapezoids */
  1672. /* 2220 */
  1673. bottomright = interpolateX(maxV, bl, br);
  1674. bottomleft = interpolateX(minV, bl, br);
  1675. leftbottom = interpolateX(minV, bl, tl);
  1676. lefttop = interpolateX(maxV, bl, tl);
  1677. edges.push(isoBandEdgeBR[cval]);
  1678. edges.push(isoBandEdgeBL[cval]);
  1679. } else if (cval === 2) {
  1680. /* 0002 */
  1681. bottomright = 1 - interpolateX(minV, br, bl);
  1682. bottomleft = 1 - interpolateX(maxV, br, bl);
  1683. leftbottom = 1 - interpolateX(maxV, tl, bl);
  1684. lefttop = 1 - interpolateX(minV, tl, bl);
  1685. edges.push(isoBandEdgeBR[cval]);
  1686. edges.push(isoBandEdgeBL[cval]);
  1687. } else if (cval === 162) {
  1688. /* 2202 */
  1689. righttop = interpolateX(maxV, br, tr);
  1690. rightbottom = interpolateX(minV, br, tr);
  1691. bottomright = 1 - interpolateX(minV, br, bl);
  1692. bottomleft = 1 - interpolateX(maxV, br, bl);
  1693. edges.push(isoBandEdgeBR[cval]);
  1694. edges.push(isoBandEdgeBL[cval]);
  1695. } else if (cval === 8) {
  1696. /* 0020 */
  1697. righttop = 1 - interpolateX(minV, tr, br);
  1698. rightbottom = 1 - interpolateX(maxV, tr, br);
  1699. bottomright = interpolateX(maxV, bl, br);
  1700. bottomleft = interpolateX(minV, bl, br);
  1701. edges.push(isoBandEdgeRT[cval]);
  1702. edges.push(isoBandEdgeRB[cval]);
  1703. } else if (cval === 138) {
  1704. /* 2022 */
  1705. righttop = 1 - interpolateX(minV, tr, br);
  1706. rightbottom = 1 - interpolateX(maxV, tr, br);
  1707. topleft = 1 - interpolateX(maxV, tr, tl);
  1708. topright = 1 - interpolateX(minV, tr, tl);
  1709. edges.push(isoBandEdgeRT[cval]);
  1710. edges.push(isoBandEdgeRB[cval]);
  1711. } else if (cval === 32) {
  1712. /* 0200 */
  1713. righttop = interpolateX(maxV, br, tr);
  1714. rightbottom = interpolateX(minV, br, tr);
  1715. topleft = interpolateX(minV, tl, tr);
  1716. topright = interpolateX(maxV, tl, tr);
  1717. edges.push(isoBandEdgeRT[cval]);
  1718. edges.push(isoBandEdgeRB[cval]);
  1719. } else if (cval === 42) {
  1720. /* 0222 */
  1721. leftbottom = 1 - interpolateX(maxV, tl, bl);
  1722. lefttop = 1 - interpolateX(minV, tl, bl);
  1723. topleft = interpolateX(minV, tl, tr);
  1724. topright = interpolateX(maxV, tl, tr);
  1725. edges.push(isoBandEdgeLB[cval]);
  1726. edges.push(isoBandEdgeLT[cval]);
  1727. } else if (cval === 128) {
  1728. /* 2000 */
  1729. leftbottom = interpolateX(minV, bl, tl);
  1730. lefttop = interpolateX(maxV, bl, tl);
  1731. topleft = 1 - interpolateX(maxV, tr, tl);
  1732. topright = 1 - interpolateX(minV, tr, tl);
  1733. edges.push(isoBandEdgeLB[cval]);
  1734. edges.push(isoBandEdgeLT[cval]);
  1735. }
  1736. /* 3rd rectangle cases */
  1737. if (cval === 5) {
  1738. /* 0011 */
  1739. rightbottom = 1 - interpolateX(minV, tr, br);
  1740. leftbottom = 1 - interpolateX(minV, tl, bl);
  1741. edges.push(isoBandEdgeRB[cval]);
  1742. } else if (cval === 165) {
  1743. /* 2211 */
  1744. rightbottom = interpolateX(maxV, br, tr);
  1745. leftbottom = interpolateX(maxV, bl, tl);
  1746. edges.push(isoBandEdgeRB[cval]);
  1747. } else if (cval === 20) {
  1748. /* 0110 */
  1749. bottomright = interpolateX(minV, bl, br);
  1750. topright = interpolateX(minV, tl, tr);
  1751. edges.push(isoBandEdgeBR[cval]);
  1752. } else if (cval === 150) {
  1753. /* 2112 */
  1754. bottomright = 1 - interpolateX(maxV, br, bl);
  1755. topright = 1 - interpolateX(maxV, tr, tl);
  1756. edges.push(isoBandEdgeBR[cval]);
  1757. } else if (cval === 80) {
  1758. /* 1100 */
  1759. righttop = interpolateX(minV, br, tr);
  1760. lefttop = interpolateX(minV, bl, tl);
  1761. edges.push(isoBandEdgeRT[cval]);
  1762. } else if (cval === 90) {
  1763. /* 1122 */
  1764. righttop = 1 - interpolateX(maxV, tr, br);
  1765. lefttop = 1 - interpolateX(maxV, tl, bl);
  1766. edges.push(isoBandEdgeRT[cval]);
  1767. } else if (cval === 65) {
  1768. /* 1001 */
  1769. bottomleft = 1 - interpolateX(minV, br, bl);
  1770. topleft = 1 - interpolateX(minV, tr, tl);
  1771. edges.push(isoBandEdgeBL[cval]);
  1772. } else if (cval === 105) {
  1773. /* 1221 */
  1774. bottomleft = interpolateX(maxV, bl, br);
  1775. topleft = interpolateX(maxV, tl, tr);
  1776. edges.push(isoBandEdgeBL[cval]);
  1777. } else if (cval === 160) {
  1778. /* 2200 */
  1779. righttop = interpolateX(maxV, br, tr);
  1780. rightbottom = interpolateX(minV, br, tr);
  1781. leftbottom = interpolateX(minV, bl, tl);
  1782. lefttop = interpolateX(maxV, bl, tl);
  1783. edges.push(isoBandEdgeRT[cval]);
  1784. edges.push(isoBandEdgeRB[cval]);
  1785. } else if (cval === 10) {
  1786. /* 0022 */
  1787. righttop = 1 - interpolateX(minV, tr, br);
  1788. rightbottom = 1 - interpolateX(maxV, tr, br);
  1789. leftbottom = 1 - interpolateX(maxV, tl, bl);
  1790. lefttop = 1 - interpolateX(minV, tl, bl);
  1791. edges.push(isoBandEdgeRT[cval]);
  1792. edges.push(isoBandEdgeRB[cval]);
  1793. } else if (cval === 130) {
  1794. /* 2002 */
  1795. bottomright = 1 - interpolateX(minV, br, bl);
  1796. bottomleft = 1 - interpolateX(maxV, br, bl);
  1797. topleft = 1 - interpolateX(maxV, tr, tl);
  1798. topright = 1 - interpolateX(minV, tr, tl);
  1799. edges.push(isoBandEdgeBR[cval]);
  1800. edges.push(isoBandEdgeBL[cval]);
  1801. } else if (cval === 40) {
  1802. /* 0220 */
  1803. bottomright = interpolateX(maxV, bl, br);
  1804. bottomleft = interpolateX(minV, bl, br);
  1805. topleft = interpolateX(minV, tl, tr);
  1806. topright = interpolateX(maxV, tl, tr);
  1807. edges.push(isoBandEdgeBR[cval]);
  1808. edges.push(isoBandEdgeBL[cval]);
  1809. } else if (cval === 101) {
  1810. /* 4th single pentagon cases */
  1811. /* 1211 */
  1812. rightbottom = interpolateX(maxV, br, tr);
  1813. topleft = interpolateX(maxV, tl, tr);
  1814. edges.push(isoBandEdgeRB[cval]);
  1815. } else if (cval === 69) {
  1816. /* 1011 */
  1817. rightbottom = 1 - interpolateX(minV, tr, br);
  1818. topleft = 1 - interpolateX(minV, tr, tl);
  1819. edges.push(isoBandEdgeRB[cval]);
  1820. } else if (cval === 149) {
  1821. /* 2111 */
  1822. leftbottom = interpolateX(maxV, bl, tl);
  1823. topright = 1 - interpolateX(maxV, tr, tl);
  1824. edges.push(isoBandEdgeLB[cval]);
  1825. } else if (cval === 21) {
  1826. /* 0111 */
  1827. leftbottom = 1 - interpolateX(minV, tl, bl);
  1828. topright = interpolateX(minV, tl, tr);
  1829. edges.push(isoBandEdgeLB[cval]);
  1830. } else if (cval === 86) {
  1831. /* 1112 */
  1832. bottomright = 1 - interpolateX(maxV, br, bl);
  1833. lefttop = 1 - interpolateX(maxV, tl, bl);
  1834. edges.push(isoBandEdgeBR[cval]);
  1835. } else if (cval === 84) {
  1836. /* 1110 */
  1837. bottomright = interpolateX(minV, bl, br);
  1838. lefttop = interpolateX(minV, bl, tl);
  1839. edges.push(isoBandEdgeBR[cval]);
  1840. } else if (cval === 89) {
  1841. /* 1121 */
  1842. righttop = 1 - interpolateX(maxV, tr, br);
  1843. bottomleft = interpolateX(maxV, bl, br);
  1844. edges.push(isoBandEdgeBL[cval]);
  1845. } else if (cval === 81) {
  1846. /* 1101 */
  1847. righttop = interpolateX(minV, br, tr);
  1848. bottomleft = 1 - interpolateX(minV, br, bl);
  1849. edges.push(isoBandEdgeBL[cval]);
  1850. } else if (cval === 96) {
  1851. /* 1200 */
  1852. righttop = interpolateX(maxV, br, tr);
  1853. rightbottom = interpolateX(minV, br, tr);
  1854. lefttop = interpolateX(minV, bl, tl);
  1855. topleft = interpolateX(maxV, tl, tr);
  1856. edges.push(isoBandEdgeRT[cval]);
  1857. edges.push(isoBandEdgeRB[cval]);
  1858. } else if (cval === 74) {
  1859. /* 1022 */
  1860. righttop = 1 - interpolateX(minV, tr, br);
  1861. rightbottom = 1 - interpolateX(maxV, tr, br);
  1862. lefttop = 1 - interpolateX(maxV, tl, bl);
  1863. topleft = 1 - interpolateX(minV, tr, tl);
  1864. edges.push(isoBandEdgeRT[cval]);
  1865. edges.push(isoBandEdgeRB[cval]);
  1866. } else if (cval === 24) {
  1867. /* 0120 */
  1868. righttop = 1 - interpolateX(maxV, tr, br);
  1869. bottomright = interpolateX(maxV, bl, br);
  1870. bottomleft = interpolateX(minV, bl, br);
  1871. topright = interpolateX(minV, tl, tr);
  1872. edges.push(isoBandEdgeRT[cval]);
  1873. edges.push(isoBandEdgeBL[cval]);
  1874. } else if (cval === 146) {
  1875. /* 2102 */
  1876. righttop = interpolateX(minV, br, tr);
  1877. bottomright = 1 - interpolateX(minV, br, bl);
  1878. bottomleft = 1 - interpolateX(maxV, br, bl);
  1879. topright = 1 - interpolateX(maxV, tr, tl);
  1880. edges.push(isoBandEdgeRT[cval]);
  1881. edges.push(isoBandEdgeBL[cval]);
  1882. } else if (cval === 6) {
  1883. /* 0012 */
  1884. rightbottom = 1 - interpolateX(minV, tr, br);
  1885. bottomright = 1 - interpolateX(maxV, br, bl);
  1886. leftbottom = 1 - interpolateX(maxV, tl, bl);
  1887. lefttop = 1 - interpolateX(minV, tl, bl);
  1888. edges.push(isoBandEdgeRB[cval]);
  1889. edges.push(isoBandEdgeBR[cval]);
  1890. } else if (cval === 164) {
  1891. /* 2210 */
  1892. rightbottom = interpolateX(maxV, br, tr);
  1893. bottomright = interpolateX(minV, bl, br);
  1894. leftbottom = interpolateX(minV, bl, tl);
  1895. lefttop = interpolateX(maxV, bl, tl);
  1896. edges.push(isoBandEdgeRB[cval]);
  1897. edges.push(isoBandEdgeBR[cval]);
  1898. } else if (cval === 129) {
  1899. /* 2001 */
  1900. bottomleft = 1 - interpolateX(minV, br, bl);
  1901. leftbottom = interpolateX(maxV, bl, tl);
  1902. topleft = 1 - interpolateX(maxV, tr, tl);
  1903. topright = 1 - interpolateX(minV, tr, tl);
  1904. edges.push(isoBandEdgeBL[cval]);
  1905. edges.push(isoBandEdgeLB[cval]);
  1906. } else if (cval === 41) {
  1907. /* 0221 */
  1908. bottomleft = interpolateX(maxV, bl, br);
  1909. leftbottom = 1 - interpolateX(minV, tl, bl);
  1910. topleft = interpolateX(minV, tl, tr);
  1911. topright = interpolateX(maxV, tl, tr);
  1912. edges.push(isoBandEdgeBL[cval]);
  1913. edges.push(isoBandEdgeLB[cval]);
  1914. } else if (cval === 66) {
  1915. /* 1002 */
  1916. bottomright = 1 - interpolateX(minV, br, bl);
  1917. bottomleft = 1 - interpolateX(maxV, br, bl);
  1918. lefttop = 1 - interpolateX(maxV, tl, bl);
  1919. topleft = 1 - interpolateX(minV, tr, tl);
  1920. edges.push(isoBandEdgeBR[cval]);
  1921. edges.push(isoBandEdgeBL[cval]);
  1922. } else if (cval === 104) {
  1923. /* 1220 */
  1924. bottomright = interpolateX(maxV, bl, br);
  1925. bottomleft = interpolateX(minV, bl, br);
  1926. lefttop = interpolateX(minV, bl, tl);
  1927. topleft = interpolateX(maxV, tl, tr);
  1928. edges.push(isoBandEdgeBL[cval]);
  1929. edges.push(isoBandEdgeTL[cval]);
  1930. } else if (cval === 144) {
  1931. /* 2100 */
  1932. righttop = interpolateX(minV, br, tr);
  1933. leftbottom = interpolateX(minV, bl, tl);
  1934. lefttop = interpolateX(maxV, bl, tl);
  1935. topright = 1 - interpolateX(maxV, tr, tl);
  1936. edges.push(isoBandEdgeRT[cval]);
  1937. edges.push(isoBandEdgeLT[cval]);
  1938. } else if (cval === 26) {
  1939. /* 0122 */
  1940. righttop = 1 - interpolateX(maxV, tr, br);
  1941. leftbottom = 1 - interpolateX(maxV, tl, bl);
  1942. lefttop = 1 - interpolateX(minV, tl, bl);
  1943. topright = interpolateX(minV, tl, tr);
  1944. edges.push(isoBandEdgeRT[cval]);
  1945. edges.push(isoBandEdgeLT[cval]);
  1946. } else if (cval === 36) {
  1947. /* 0210 */
  1948. rightbottom = interpolateX(maxV, br, tr);
  1949. bottomright = interpolateX(minV, bl, br);
  1950. topleft = interpolateX(minV, tl, tr);
  1951. topright = interpolateX(maxV, tl, tr);
  1952. edges.push(isoBandEdgeRB[cval]);
  1953. edges.push(isoBandEdgeBR[cval]);
  1954. } else if (cval === 134) {
  1955. /* 2012 */
  1956. rightbottom = 1 - interpolateX(minV, tr, br);
  1957. bottomright = 1 - interpolateX(maxV, br, bl);
  1958. topleft = 1 - interpolateX(maxV, tr, tl);
  1959. topright = 1 - interpolateX(minV, tr, tl);
  1960. edges.push(isoBandEdgeRB[cval]);
  1961. edges.push(isoBandEdgeBR[cval]);
  1962. } else if (cval === 9) {
  1963. /* 0021 */
  1964. righttop = 1 - interpolateX(minV, tr, br);
  1965. rightbottom = 1 - interpolateX(maxV, tr, br);
  1966. bottomleft = interpolateX(maxV, bl, br);
  1967. leftbottom = 1 - interpolateX(minV, tl, bl);
  1968. edges.push(isoBandEdgeRT[cval]);
  1969. edges.push(isoBandEdgeRB[cval]);
  1970. } else if (cval === 161) {
  1971. /* 2201 */
  1972. righttop = interpolateX(maxV, br, tr);
  1973. rightbottom = interpolateX(minV, br, tr);
  1974. bottomleft = 1 - interpolateX(minV, br, bl);
  1975. leftbottom = interpolateX(maxV, bl, tl);
  1976. edges.push(isoBandEdgeRT[cval]);
  1977. edges.push(isoBandEdgeRB[cval]);
  1978. } else if (cval === 37) {
  1979. /* 5th single hexagon cases */
  1980. /* 0211 */
  1981. rightbottom = interpolateX(maxV, br, tr);
  1982. leftbottom = 1 - interpolateX(minV, tl, bl);
  1983. topleft = interpolateX(minV, tl, tr);
  1984. topright = interpolateX(maxV, tl, tr);
  1985. edges.push(isoBandEdgeRB[cval]);
  1986. edges.push(isoBandEdgeLB[cval]);
  1987. } else if (cval === 133) {
  1988. /* 2011 */
  1989. rightbottom = 1 - interpolateX(minV, tr, br);
  1990. leftbottom = interpolateX(maxV, bl, tl);
  1991. topleft = 1 - interpolateX(maxV, tr, tl);
  1992. topright = 1 - interpolateX(minV, tr, tl);
  1993. edges.push(isoBandEdgeRB[cval]);
  1994. edges.push(isoBandEdgeLB[cval]);
  1995. } else if (cval === 148) {
  1996. /* 2110 */
  1997. bottomright = interpolateX(minV, bl, br);
  1998. leftbottom = interpolateX(minV, bl, tl);
  1999. lefttop = interpolateX(maxV, bl, tl);
  2000. topright = 1 - interpolateX(maxV, tr, tl);
  2001. edges.push(isoBandEdgeBR[cval]);
  2002. edges.push(isoBandEdgeLT[cval]);
  2003. } else if (cval === 22) {
  2004. /* 0112 */
  2005. bottomright = 1 - interpolateX(maxV, br, bl);
  2006. leftbottom = 1 - interpolateX(maxV, tl, bl);
  2007. lefttop = 1 - interpolateX(minV, tl, bl);
  2008. topright = interpolateX(minV, tl, tr);
  2009. edges.push(isoBandEdgeBR[cval]);
  2010. edges.push(isoBandEdgeLT[cval]);
  2011. } else if (cval === 82) {
  2012. /* 1102 */
  2013. righttop = interpolateX(minV, br, tr);
  2014. bottomright = 1 - interpolateX(minV, br, bl);
  2015. bottomleft = 1 - interpolateX(maxV, br, bl);
  2016. lefttop = 1 - interpolateX(maxV, tl, bl);
  2017. edges.push(isoBandEdgeRT[cval]);
  2018. edges.push(isoBandEdgeBL[cval]);
  2019. } else if (cval === 88) {
  2020. /* 1120 */
  2021. righttop = 1 - interpolateX(maxV, tr, br);
  2022. bottomright = interpolateX(maxV, bl, br);
  2023. bottomleft = interpolateX(minV, bl, br);
  2024. lefttop = interpolateX(minV, bl, tl);
  2025. edges.push(isoBandEdgeRT[cval]);
  2026. edges.push(isoBandEdgeBL[cval]);
  2027. } else if (cval === 73) {
  2028. /* 1021 */
  2029. righttop = 1 - interpolateX(minV, tr, br);
  2030. rightbottom = 1 - interpolateX(maxV, tr, br);
  2031. bottomleft = interpolateX(maxV, bl, br);
  2032. topleft = 1 - interpolateX(minV, tr, tl);
  2033. edges.push(isoBandEdgeRT[cval]);
  2034. edges.push(isoBandEdgeRB[cval]);
  2035. } else if (cval === 97) {
  2036. /* 1201 */
  2037. righttop = interpolateX(maxV, br, tr);
  2038. rightbottom = interpolateX(minV, br, tr);
  2039. bottomleft = 1 - interpolateX(minV, br, bl);
  2040. topleft = interpolateX(maxV, tl, tr);
  2041. edges.push(isoBandEdgeRT[cval]);
  2042. edges.push(isoBandEdgeRB[cval]);
  2043. } else if (cval === 145) {
  2044. /* 2101 */
  2045. righttop = interpolateX(minV, br, tr);
  2046. bottomleft = 1 - interpolateX(minV, br, bl);
  2047. leftbottom = interpolateX(maxV, bl, tl);
  2048. topright = 1 - interpolateX(maxV, tr, tl);
  2049. edges.push(isoBandEdgeRT[cval]);
  2050. edges.push(isoBandEdgeLB[cval]);
  2051. } else if (cval === 25) {
  2052. /* 0121 */
  2053. righttop = 1 - interpolateX(maxV, tr, br);
  2054. bottomleft = interpolateX(maxV, bl, br);
  2055. leftbottom = 1 - interpolateX(minV, tl, bl);
  2056. topright = interpolateX(minV, tl, tr);
  2057. edges.push(isoBandEdgeRT[cval]);
  2058. edges.push(isoBandEdgeLB[cval]);
  2059. } else if (cval === 70) {
  2060. /* 1012 */
  2061. rightbottom = 1 - interpolateX(minV, tr, br);
  2062. bottomright = 1 - interpolateX(maxV, br, bl);
  2063. lefttop = 1 - interpolateX(maxV, tl, bl);
  2064. topleft = 1 - interpolateX(minV, tr, tl);
  2065. edges.push(isoBandEdgeRB[cval]);
  2066. edges.push(isoBandEdgeBR[cval]);
  2067. } else if (cval === 100) {
  2068. /* 1210 */
  2069. rightbottom = interpolateX(maxV, br, tr);
  2070. bottomright = interpolateX(minV, bl, br);
  2071. lefttop = interpolateX(minV, bl, tl);
  2072. topleft = interpolateX(maxV, tl, tr);
  2073. edges.push(isoBandEdgeRB[cval]);
  2074. edges.push(isoBandEdgeBR[cval]);
  2075. } else if (cval === 34) {
  2076. /* 8-sided cases */
  2077. /* 0202 || 2020 with flipped == 0 */
  2078. if (flipped === 0) {
  2079. righttop = 1 - interpolateX(minV, tr, br);
  2080. rightbottom = 1 - interpolateX(maxV, tr, br);
  2081. bottomright = interpolateX(maxV, bl, br);
  2082. bottomleft = interpolateX(minV, bl, br);
  2083. leftbottom = interpolateX(minV, bl, tl);
  2084. lefttop = interpolateX(maxV, bl, tl);
  2085. topleft = 1 - interpolateX(maxV, tr, tl);
  2086. topright = 1 - interpolateX(minV, tr, tl);
  2087. } else {
  2088. righttop = interpolateX(maxV, br, tr);
  2089. rightbottom = interpolateX(minV, br, tr);
  2090. bottomright = 1 - interpolateX(minV, br, bl);
  2091. bottomleft = 1 - interpolateX(maxV, br, bl);
  2092. leftbottom = 1 - interpolateX(maxV, tl, bl);
  2093. lefttop = 1 - interpolateX(minV, tl, bl);
  2094. topleft = interpolateX(minV, tl, tr);
  2095. topright = interpolateX(maxV, tl, tr);
  2096. }
  2097. edges.push(isoBandEdgeRT[cval]);
  2098. edges.push(isoBandEdgeRB[cval]);
  2099. edges.push(isoBandEdgeLB[cval]);
  2100. edges.push(isoBandEdgeLT[cval]);
  2101. } else if (cval === 35) {
  2102. /* flipped == 1 state for 0202, and 2020 with flipped == 4*/
  2103. if (flipped === 4) {
  2104. righttop = 1 - interpolateX(minV, tr, br);
  2105. rightbottom = 1 - interpolateX(maxV, tr, br);
  2106. bottomright = interpolateX(maxV, bl, br);
  2107. bottomleft = interpolateX(minV, bl, br);
  2108. leftbottom = interpolateX(minV, bl, tl);
  2109. lefttop = interpolateX(maxV, bl, tl);
  2110. topleft = 1 - interpolateX(maxV, tr, tl);
  2111. topright = 1 - interpolateX(minV, tr, tl);
  2112. } else {
  2113. righttop = interpolateX(maxV, br, tr);
  2114. rightbottom = interpolateX(minV, br, tr);
  2115. bottomright = 1 - interpolateX(minV, br, bl);
  2116. bottomleft = 1 - interpolateX(maxV, br, bl);
  2117. leftbottom = 1 - interpolateX(maxV, tl, bl);
  2118. lefttop = 1 - interpolateX(minV, tl, bl);
  2119. topleft = interpolateX(minV, tl, tr);
  2120. topright = interpolateX(maxV, tl, tr);
  2121. }
  2122. edges.push(isoBandEdgeRT[cval]);
  2123. edges.push(isoBandEdgeRB[cval]);
  2124. edges.push(isoBandEdgeBL[cval]);
  2125. edges.push(isoBandEdgeLT[cval]);
  2126. } else if (cval === 136) {
  2127. /* 2020 || 0202 with flipped == 0 */
  2128. if (flipped === 0) {
  2129. righttop = interpolateX(maxV, br, tr);
  2130. rightbottom = interpolateX(minV, br, tr);
  2131. bottomright = 1 - interpolateX(minV, br, bl);
  2132. bottomleft = 1 - interpolateX(maxV, br, bl);
  2133. leftbottom = 1 - interpolateX(maxV, tl, bl);
  2134. lefttop = 1 - interpolateX(minV, tl, bl);
  2135. topleft = interpolateX(minV, tl, tr);
  2136. topright = interpolateX(maxV, tl, tr);
  2137. } else {
  2138. righttop = 1 - interpolateX(minV, tr, br);
  2139. rightbottom = 1 - interpolateX(maxV, tr, br);
  2140. bottomright = interpolateX(maxV, bl, br);
  2141. bottomleft = interpolateX(minV, bl, br);
  2142. leftbottom = interpolateX(minV, bl, tl);
  2143. lefttop = interpolateX(maxV, bl, tl);
  2144. topleft = 1 - interpolateX(maxV, tr, tl);
  2145. topright = 1 - interpolateX(minV, tr, tl);
  2146. }
  2147. edges.push(isoBandEdgeRT[cval]);
  2148. edges.push(isoBandEdgeRB[cval]);
  2149. edges.push(isoBandEdgeLB[cval]);
  2150. edges.push(isoBandEdgeLT[cval]);
  2151. } else if (cval === 153) {
  2152. /* 6-sided polygon cases */
  2153. /* 0101 with flipped == 0 || 2121 with flipped == 2 */
  2154. if (flipped === 0) {
  2155. righttop = interpolateX(minV, br, tr);
  2156. bottomleft = 1 - interpolateX(minV, br, bl);
  2157. leftbottom = 1 - interpolateX(minV, tl, bl);
  2158. topright = interpolateX(minV, tl, tr);
  2159. } else {
  2160. righttop = 1 - interpolateX(maxV, tr, br);
  2161. bottomleft = interpolateX(maxV, bl, br);
  2162. leftbottom = interpolateX(maxV, bl, tl);
  2163. topright = 1 - interpolateX(maxV, tr, tl);
  2164. }
  2165. edges.push(isoBandEdgeRT[cval]);
  2166. edges.push(isoBandEdgeBL[cval]);
  2167. } else if (cval === 102) {
  2168. /* 1010 with flipped == 0 || 1212 with flipped == 2 */
  2169. if (flipped === 0) {
  2170. rightbottom = 1 - interpolateX(minV, tr, br);
  2171. bottomright = interpolateX(minV, bl, br);
  2172. lefttop = interpolateX(minV, bl, tl);
  2173. topleft = 1 - interpolateX(minV, tr, tl);
  2174. } else {
  2175. rightbottom = interpolateX(maxV, br, tr);
  2176. bottomright = 1 - interpolateX(maxV, br, bl);
  2177. lefttop = 1 - interpolateX(maxV, tl, bl);
  2178. topleft = interpolateX(maxV, tl, tr);
  2179. }
  2180. edges.push(isoBandEdgeRB[cval]);
  2181. edges.push(isoBandEdgeLT[cval]);
  2182. } else if (cval === 155) {
  2183. /* 0101 with flipped == 4 || 2121 with flipped == 1 */
  2184. if (flipped === 4) {
  2185. righttop = interpolateX(minV, br, tr);
  2186. bottomleft = 1 - interpolateX(minV, br, bl);
  2187. leftbottom = 1 - interpolateX(minV, tl, bl);
  2188. topright = interpolateX(minV, tl, tr);
  2189. } else {
  2190. righttop = 1 - interpolateX(maxV, tr, br);
  2191. bottomleft = interpolateX(maxV, bl, br);
  2192. leftbottom = interpolateX(maxV, bl, tl);
  2193. topright = 1 - interpolateX(maxV, tr, tl);
  2194. }
  2195. edges.push(isoBandEdgeRT[cval]);
  2196. edges.push(isoBandEdgeLB[cval]);
  2197. } else if (cval === 103) {
  2198. /* 1010 with flipped == 4 || 1212 with flipped == 1 */
  2199. if (flipped === 4) {
  2200. rightbottom = 1 - interpolateX(minV, tr, br);
  2201. bottomright = interpolateX(minV, bl, br);
  2202. lefttop = interpolateX(minV, bl, tl);
  2203. topleft = 1 - interpolateX(minV, tr, tl);
  2204. } else {
  2205. rightbottom = interpolateX(maxV, br, tr);
  2206. bottomright = 1 - interpolateX(maxV, br, bl);
  2207. lefttop = 1 - interpolateX(maxV, tl, bl);
  2208. topleft = interpolateX(maxV, tl, tr);
  2209. }
  2210. edges.push(isoBandEdgeRB[cval]);
  2211. edges.push(isoBandEdgeBR[cval]);
  2212. } else if (cval === 152) {
  2213. /* 7-sided polygon cases */
  2214. /* 2120 with flipped == 2 || 0102 with flipped == 0 */
  2215. if (flipped === 0) {
  2216. righttop = interpolateX(minV, br, tr);
  2217. bottomright = 1 - interpolateX(minV, br, bl);
  2218. bottomleft = 1 - interpolateX(maxV, br, bl);
  2219. leftbottom = 1 - interpolateX(maxV, tl, bl);
  2220. lefttop = 1 - interpolateX(minV, tl, bl);
  2221. topright = interpolateX(minV, tl, tr);
  2222. } else {
  2223. righttop = 1 - interpolateX(maxV, tr, br);
  2224. bottomright = interpolateX(maxV, bl, br);
  2225. bottomleft = interpolateX(minV, bl, br);
  2226. leftbottom = interpolateX(minV, bl, tl);
  2227. lefttop = interpolateX(maxV, bl, tl);
  2228. topright = 1 - interpolateX(maxV, tr, tl);
  2229. }
  2230. edges.push(isoBandEdgeRT[cval]);
  2231. edges.push(isoBandEdgeBR[cval]);
  2232. edges.push(isoBandEdgeBL[cval]);
  2233. } else if (cval === 156) {
  2234. /* 2120 with flipped == 1 || 0102 with flipped == 4 */
  2235. if (flipped === 4) {
  2236. righttop = interpolateX(minV, br, tr);
  2237. bottomright = 1 - interpolateX(minV, br, bl);
  2238. bottomleft = 1 - interpolateX(maxV, br, bl);
  2239. leftbottom = 1 - interpolateX(maxV, tl, bl);
  2240. lefttop = 1 - interpolateX(minV, tl, bl);
  2241. topright = interpolateX(minV, tl, tr);
  2242. } else {
  2243. righttop = 1 - interpolateX(maxV, tr, br);
  2244. bottomright = interpolateX(maxV, bl, br);
  2245. bottomleft = interpolateX(minV, bl, br);
  2246. leftbottom = interpolateX(minV, bl, tl);
  2247. lefttop = interpolateX(maxV, bl, tl);
  2248. topright = 1 - interpolateX(maxV, tr, tl);
  2249. }
  2250. edges.push(isoBandEdgeRT[cval]);
  2251. edges.push(isoBandEdgeBL[cval]);
  2252. edges.push(isoBandEdgeLT[cval]);
  2253. } else if (cval === 137) {
  2254. /* 2021 with flipped == 2 || 0201 with flipped == 0 */
  2255. if (flipped === 0) {
  2256. righttop = interpolateX(maxV, br, tr);
  2257. rightbottom = interpolateX(minV, br, tr);
  2258. bottomleft = 1 - interpolateX(minV, br, bl);
  2259. leftbottom = 1 - interpolateX(minV, tl, bl);
  2260. topleft = interpolateX(minV, tl, tr);
  2261. topright = interpolateX(maxV, tl, tr);
  2262. } else {
  2263. righttop = 1 - interpolateX(minV, tr, br);
  2264. rightbottom = 1 - interpolateX(maxV, tr, br);
  2265. bottomleft = interpolateX(maxV, bl, br);
  2266. leftbottom = interpolateX(maxV, bl, tl);
  2267. topleft = 1 - interpolateX(maxV, tr, tl);
  2268. topright = 1 - interpolateX(minV, tr, tl);
  2269. }
  2270. edges.push(isoBandEdgeRT[cval]);
  2271. edges.push(isoBandEdgeRB[cval]);
  2272. edges.push(isoBandEdgeBL[cval]);
  2273. } else if (cval === 139) {
  2274. /* 2021 with flipped == 1 || 0201 with flipped == 4 */
  2275. if (flipped === 4) {
  2276. righttop = interpolateX(maxV, br, tr);
  2277. rightbottom = interpolateX(minV, br, tr);
  2278. bottomleft = 1 - interpolateX(minV, br, bl);
  2279. leftbottom = 1 - interpolateX(minV, tl, bl);
  2280. topleft = interpolateX(minV, tl, tr);
  2281. topright = interpolateX(maxV, tl, tr);
  2282. } else {
  2283. righttop = 1 - interpolateX(minV, tr, br);
  2284. rightbottom = 1 - interpolateX(maxV, tr, br);
  2285. bottomleft = interpolateX(maxV, bl, br);
  2286. leftbottom = interpolateX(maxV, bl, tl);
  2287. topleft = 1 - interpolateX(maxV, tr, tl);
  2288. topright = 1 - interpolateX(minV, tr, tl);
  2289. }
  2290. edges.push(isoBandEdgeRT[cval]);
  2291. edges.push(isoBandEdgeRB[cval]);
  2292. edges.push(isoBandEdgeLB[cval]);
  2293. } else if (cval === 98) {
  2294. /* 1202 with flipped == 2 || 1020 with flipped == 0 */
  2295. if (flipped === 0) {
  2296. righttop = 1 - interpolateX(minV, tr, br);
  2297. rightbottom = 1 - interpolateX(maxV, tr, br);
  2298. bottomright = interpolateX(maxV, bl, br);
  2299. bottomleft = interpolateX(minV, bl, br);
  2300. lefttop = interpolateX(minV, bl, tl);
  2301. topleft = 1 - interpolateX(minV, tr, tl);
  2302. } else {
  2303. righttop = interpolateX(maxV, br, tr);
  2304. rightbottom = interpolateX(minV, br, tr);
  2305. bottomright = 1 - interpolateX(minV, br, bl);
  2306. bottomleft = 1 - interpolateX(maxV, br, bl);
  2307. lefttop = 1 - interpolateX(maxV, tl, bl);
  2308. topleft = interpolateX(maxV, tl, tr);
  2309. }
  2310. edges.push(isoBandEdgeRT[cval]);
  2311. edges.push(isoBandEdgeRB[cval]);
  2312. edges.push(isoBandEdgeLT[cval]);
  2313. } else if (cval === 99) {
  2314. /* 1202 with flipped == 1 || 1020 with flipped == 4 */
  2315. if (flipped === 4) {
  2316. righttop = 1 - interpolateX(minV, tr, br);
  2317. rightbottom = 1 - interpolateX(maxV, tr, br);
  2318. bottomright = interpolateX(maxV, bl, br);
  2319. bottomleft = interpolateX(minV, bl, br);
  2320. lefttop = interpolateX(minV, bl, tl);
  2321. topleft = 1 - interpolateX(minV, tr, tl);
  2322. } else {
  2323. righttop = interpolateX(maxV, br, tr);
  2324. rightbottom = interpolateX(minV, br, tr);
  2325. bottomright = 1 - interpolateX(minV, br, bl);
  2326. bottomleft = 1 - interpolateX(maxV, br, bl);
  2327. lefttop = 1 - interpolateX(maxV, tl, bl);
  2328. topleft = interpolateX(maxV, tl, tr);
  2329. }
  2330. edges.push(isoBandEdgeRT[cval]);
  2331. edges.push(isoBandEdgeRB[cval]);
  2332. edges.push(isoBandEdgeBL[cval]);
  2333. } else if (cval === 38) {
  2334. /* 0212 with flipped == 2 || 2010 with flipped == 0 */
  2335. if (flipped === 0) {
  2336. rightbottom = 1 - interpolateX(minV, tr, br);
  2337. bottomright = interpolateX(minV, bl, br);
  2338. leftbottom = interpolateX(minV, bl, tl);
  2339. lefttop = interpolateX(maxV, bl, tl);
  2340. topleft = 1 - interpolateX(maxV, tr, tl);
  2341. topright = 1 - interpolateX(minV, tr, tl);
  2342. } else {
  2343. rightbottom = interpolateX(maxV, br, tr);
  2344. bottomright = 1 - interpolateX(maxV, br, bl);
  2345. leftbottom = 1 - interpolateX(maxV, tl, bl);
  2346. lefttop = 1 - interpolateX(minV, tl, bl);
  2347. topleft = interpolateX(minV, tl, tr);
  2348. topright = interpolateX(maxV, tl, tr);
  2349. }
  2350. edges.push(isoBandEdgeRB[cval]);
  2351. edges.push(isoBandEdgeLB[cval]);
  2352. edges.push(isoBandEdgeLT[cval]);
  2353. } else if (cval === 39) {
  2354. /* 0212 with flipped == 1 || 2010 with flipped == 4 */
  2355. if (flipped === 4) {
  2356. rightbottom = 1 - interpolateX(minV, tr, br);
  2357. bottomright = interpolateX(minV, bl, br);
  2358. leftbottom = interpolateX(minV, bl, tl);
  2359. lefttop = interpolateX(maxV, bl, tl);
  2360. topleft = 1 - interpolateX(maxV, tr, tl);
  2361. topright = 1 - interpolateX(minV, tr, tl);
  2362. } else {
  2363. rightbottom = interpolateX(maxV, br, tr);
  2364. bottomright = 1 - interpolateX(maxV, br, bl);
  2365. leftbottom = 1 - interpolateX(maxV, tl, bl);
  2366. lefttop = 1 - interpolateX(minV, tl, bl);
  2367. topleft = interpolateX(minV, tl, tr);
  2368. topright = interpolateX(maxV, tl, tr);
  2369. }
  2370. edges.push(isoBandEdgeRB[cval]);
  2371. edges.push(isoBandEdgeBR[cval]);
  2372. edges.push(isoBandEdgeLT[cval]);
  2373. } else if (cval === 85) {
  2374. righttop = 1;
  2375. rightbottom = 0;
  2376. bottomright = 1;
  2377. bottomleft = 0;
  2378. leftbottom = 0;
  2379. lefttop = 1;
  2380. topleft = 0;
  2381. topright = 1;
  2382. }
  2383. if (
  2384. topleft < 0 ||
  2385. topleft > 1 ||
  2386. topright < 0 ||
  2387. topright > 1 ||
  2388. righttop < 0 ||
  2389. righttop > 1 ||
  2390. bottomright < 0 ||
  2391. bottomright > 1 ||
  2392. leftbottom < 0 ||
  2393. leftbottom > 1 ||
  2394. lefttop < 0 ||
  2395. lefttop > 1
  2396. ) {
  2397. console.log(
  2398. "MarchingSquaresJS-isoBands: " +
  2399. cval +
  2400. " " +
  2401. cval_real +
  2402. " " +
  2403. tl +
  2404. "," +
  2405. tr +
  2406. "," +
  2407. br +
  2408. "," +
  2409. bl +
  2410. " " +
  2411. flipped +
  2412. " " +
  2413. topleft +
  2414. " " +
  2415. topright +
  2416. " " +
  2417. righttop +
  2418. " " +
  2419. rightbottom +
  2420. " " +
  2421. bottomright +
  2422. " " +
  2423. bottomleft +
  2424. " " +
  2425. leftbottom +
  2426. " " +
  2427. lefttop
  2428. );
  2429. }
  2430. BandGrid.cells[j][i] = {
  2431. cval: cval,
  2432. cval_real: cval_real,
  2433. flipped: flipped,
  2434. topleft: topleft,
  2435. topright: topright,
  2436. righttop: righttop,
  2437. rightbottom: rightbottom,
  2438. bottomright: bottomright,
  2439. bottomleft: bottomleft,
  2440. leftbottom: leftbottom,
  2441. lefttop: lefttop,
  2442. edges: edges,
  2443. };
  2444. }
  2445. }
  2446. }
  2447. return BandGrid;
  2448. }
  2449. function BandGrid2AreaPaths(grid) {
  2450. var areas = [];
  2451. var rows = grid.rows;
  2452. var cols = grid.cols;
  2453. var currentPolygon = [];
  2454. for (var j = 0; j < rows; j++) {
  2455. for (var i = 0; i < cols; i++) {
  2456. if (
  2457. typeof grid.cells[j][i] !== "undefined" &&
  2458. grid.cells[j][i].edges.length > 0
  2459. ) {
  2460. /* trace back polygon path starting from this cell */
  2461. var cell = grid.cells[j][i];
  2462. /* get start coordinates */
  2463. var prev = getStartXY(cell),
  2464. next = null,
  2465. p = i,
  2466. q = j;
  2467. if (prev !== null) {
  2468. currentPolygon.push([prev.p[0] + p, prev.p[1] + q]);
  2469. //console.log(cell);
  2470. //console.log("coords: " + (prev.p[0] + p) + " " + (prev.p[1] + q));
  2471. }
  2472. do {
  2473. //console.log(p + "," + q);
  2474. //console.log(grid.cells[q][p]);
  2475. //console.log(grid.cells[q][p].edges);
  2476. //console.log("from : " + prev.x + " " + prev.y + " " + prev.o);
  2477. next = getExitXY(grid.cells[q][p], prev.x, prev.y, prev.o);
  2478. if (next !== null) {
  2479. //console.log("coords: " + (next.p[0] + p) + " " + (next.p[1] + q));
  2480. currentPolygon.push([next.p[0] + p, next.p[1] + q]);
  2481. p += next.x;
  2482. q += next.y;
  2483. prev = next;
  2484. } else {
  2485. //console.log("getExitXY() returned null!");
  2486. break;
  2487. }
  2488. //console.log("to : " + next.x + " " + next.y + " " + next.o);
  2489. /* special case, where we've reached the grid boundaries */
  2490. if (
  2491. q < 0 ||
  2492. q >= rows ||
  2493. p < 0 ||
  2494. p >= cols ||
  2495. typeof grid.cells[q][p] === "undefined"
  2496. ) {
  2497. /* to create a closed path, we need to trace our way
  2498. arround the missing data, until we find an entry
  2499. point again
  2500. */
  2501. /* set back coordinates of current cell */
  2502. p -= next.x;
  2503. q -= next.y;
  2504. //console.log("reached boundary at " + p + " " + q);
  2505. var missing = traceOutOfGridPath(
  2506. grid,
  2507. p,
  2508. q,
  2509. next.x,
  2510. next.y,
  2511. next.o
  2512. );
  2513. if (missing !== null) {
  2514. missing.path.forEach(function (pp) {
  2515. //console.log("coords: " + (pp[0]) + " " + (pp[1]));
  2516. currentPolygon.push(pp);
  2517. });
  2518. p = missing.i;
  2519. q = missing.j;
  2520. prev = missing;
  2521. } else {
  2522. break;
  2523. }
  2524. //console.log(grid.cells[q][p]);
  2525. }
  2526. } while (
  2527. typeof grid.cells[q][p] !== "undefined" &&
  2528. grid.cells[q][p].edges.length > 0
  2529. );
  2530. areas.push(currentPolygon);
  2531. //console.log("next polygon");
  2532. //console.log(currentPolygon);
  2533. currentPolygon = [];
  2534. if (grid.cells[j][i].edges.length > 0) i--;
  2535. }
  2536. }
  2537. }
  2538. return areas;
  2539. }
  2540. function traceOutOfGridPath(grid, i, j, d_x, d_y, d_o) {
  2541. var cell = grid.cells[j][i];
  2542. var cval = cell.cval_real;
  2543. var p = i + d_x,
  2544. q = j + d_y;
  2545. var path = [];
  2546. var closed = false;
  2547. while (!closed) {
  2548. //console.log("processing cell " + p + "," + q + " " + d_x + " " + d_y + " " + d_o);
  2549. if (
  2550. typeof grid.cells[q] === "undefined" ||
  2551. typeof grid.cells[q][p] === "undefined"
  2552. ) {
  2553. //console.log("which is undefined");
  2554. /* we can't move on, so we have to change direction to proceed further */
  2555. /* go back to previous cell */
  2556. q -= d_y;
  2557. p -= d_x;
  2558. cell = grid.cells[q][p];
  2559. cval = cell.cval_real;
  2560. /* check where we've left defined cells of the grid... */
  2561. if (d_y === -1) {
  2562. /* we came from top */
  2563. if (d_o === 0) {
  2564. /* exit left */
  2565. if (cval & Node3) {
  2566. /* lower left node is within range, so we move left */
  2567. path.push([p, q]);
  2568. d_x = -1;
  2569. d_y = 0;
  2570. d_o = 0;
  2571. } else if (cval & Node2) {
  2572. /* lower right node is within range, so we move right */
  2573. path.push([p + 1, q]);
  2574. d_x = 1;
  2575. d_y = 0;
  2576. d_o = 0;
  2577. } else {
  2578. /* close the path */
  2579. path.push([p + cell.bottomright, q]);
  2580. d_x = 0;
  2581. d_y = 1;
  2582. d_o = 1;
  2583. closed = true;
  2584. break;
  2585. }
  2586. } else if (cval & Node3) {
  2587. path.push([p, q]);
  2588. d_x = -1;
  2589. d_y = 0;
  2590. d_o = 0;
  2591. } else if (cval & Node2) {
  2592. path.push([p + cell.bottomright, q]);
  2593. d_x = 0;
  2594. d_y = 1;
  2595. d_o = 1;
  2596. closed = true;
  2597. break;
  2598. } else {
  2599. path.push([p + cell.bottomleft, q]);
  2600. d_x = 0;
  2601. d_y = 1;
  2602. d_o = 0;
  2603. closed = true;
  2604. break;
  2605. }
  2606. } else if (d_y === 1) {
  2607. /* we came from bottom */
  2608. //console.log("we came from bottom and hit a non-existing cell " + (p + d_x) + "," + (q + d_y) + "!");
  2609. if (d_o === 0) {
  2610. /* exit left */
  2611. if (cval & Node1) {
  2612. /* top right node is within range, so we move right */
  2613. path.push([p + 1, q + 1]);
  2614. d_x = 1;
  2615. d_y = 0;
  2616. d_o = 1;
  2617. } else if (!(cval & Node0)) {
  2618. /* found entry within same cell */
  2619. path.push([p + cell.topright, q + 1]);
  2620. d_x = 0;
  2621. d_y = -1;
  2622. d_o = 1;
  2623. closed = true;
  2624. //console.log("found entry from bottom at " + p + "," + q);
  2625. break;
  2626. } else {
  2627. path.push([p + cell.topleft, q + 1]);
  2628. d_x = 0;
  2629. d_y = -1;
  2630. d_o = 0;
  2631. closed = true;
  2632. break;
  2633. }
  2634. } else if (cval & Node1) {
  2635. path.push([p + 1, q + 1]);
  2636. d_x = 1;
  2637. d_y = 0;
  2638. d_o = 1;
  2639. } else {
  2640. /* move right */
  2641. path.push([p + 1, q + 1]);
  2642. d_x = 1;
  2643. d_y = 0;
  2644. d_o = 1;
  2645. //console.log("wtf");
  2646. //break;
  2647. }
  2648. } else if (d_x === -1) {
  2649. /* we came from right */
  2650. //console.log("we came from right and hit a non-existing cell at " + (p + d_x) + "," + (q + d_y) + "!");
  2651. if (d_o === 0) {
  2652. //console.log("continue at bottom");
  2653. if (cval & Node0) {
  2654. path.push([p, q + 1]);
  2655. d_x = 0;
  2656. d_y = 1;
  2657. d_o = 0;
  2658. //console.log("moving upwards to " + (p + d_x) + "," + (q + d_y) + "!");
  2659. } else if (!(cval & Node3)) {
  2660. /* there has to be an entry into the regular grid again! */
  2661. //console.log("exiting top");
  2662. path.push([p, q + cell.lefttop]);
  2663. d_x = 1;
  2664. d_y = 0;
  2665. d_o = 1;
  2666. closed = true;
  2667. break;
  2668. } else {
  2669. //console.log("exiting bottom");
  2670. path.push([p, q + cell.leftbottom]);
  2671. d_x = 1;
  2672. d_y = 0;
  2673. d_o = 0;
  2674. closed = true;
  2675. break;
  2676. }
  2677. } else {
  2678. //console.log("continue at top");
  2679. if (cval & Node0) {
  2680. path.push([p, q + 1]);
  2681. d_x = 0;
  2682. d_y = 1;
  2683. d_o = 0;
  2684. //console.log("moving upwards to " + (p + d_x) + "," + (q + d_y) + "!");
  2685. } else {
  2686. /* */
  2687. console.log("MarchingSquaresJS-isoBands: wtf");
  2688. break;
  2689. }
  2690. }
  2691. } else if (d_x === 1) {
  2692. /* we came from left */
  2693. //console.log("we came from left and hit a non-existing cell " + (p + d_x) + "," + (q + d_y) + "!");
  2694. if (d_o === 0) {
  2695. /* exit bottom */
  2696. if (cval & Node2) {
  2697. path.push([p + 1, q]);
  2698. d_x = 0;
  2699. d_y = -1;
  2700. d_o = 1;
  2701. } else {
  2702. path.push([p + 1, q + cell.rightbottom]);
  2703. d_x = -1;
  2704. d_y = 0;
  2705. d_o = 0;
  2706. closed = true;
  2707. break;
  2708. }
  2709. } else {
  2710. /* exit top */
  2711. if (cval & Node2) {
  2712. path.push([p + 1, q]);
  2713. d_x = 0;
  2714. d_y = -1;
  2715. d_o = 1;
  2716. } else if (!(cval & Node1)) {
  2717. path.push([p + 1, q + cell.rightbottom]);
  2718. d_x = -1;
  2719. d_y = 0;
  2720. d_o = 0;
  2721. closed = true;
  2722. break;
  2723. } else {
  2724. path.push([p + 1, q + cell.righttop]);
  2725. d_x = -1;
  2726. d_y = 0;
  2727. d_o = 1;
  2728. break;
  2729. }
  2730. }
  2731. } else {
  2732. /* we came from the same cell */
  2733. console.log("MarchingSquaresJS-isoBands: we came from nowhere!");
  2734. break;
  2735. }
  2736. } else {
  2737. /* try to find an entry into the regular grid again! */
  2738. cell = grid.cells[q][p];
  2739. cval = cell.cval_real;
  2740. //console.log("which is defined");
  2741. if (d_x === -1) {
  2742. if (d_o === 0) {
  2743. /* try to go downwards */
  2744. if (
  2745. typeof grid.cells[q - 1] !== "undefined" &&
  2746. typeof grid.cells[q - 1][p] !== "undefined"
  2747. ) {
  2748. d_x = 0;
  2749. d_y = -1;
  2750. d_o = 1;
  2751. } else if (cval & Node3) {
  2752. /* proceed searching in x-direction */
  2753. //console.log("proceeding in x-direction!");
  2754. path.push([p, q]);
  2755. } else {
  2756. /* we must have found an entry into the regular grid */
  2757. path.push([p + cell.bottomright, q]);
  2758. d_x = 0;
  2759. d_y = 1;
  2760. d_o = 1;
  2761. closed = true;
  2762. //console.log("found entry from bottom at " + p + "," + q);
  2763. break;
  2764. }
  2765. } else if (cval & Node0) {
  2766. /* proceed searchin in x-direction */
  2767. console.log("MarchingSquaresJS-isoBands: proceeding in x-direction!");
  2768. } else {
  2769. /* we must have found an entry into the regular grid */
  2770. console.log(
  2771. "MarchingSquaresJS-isoBands: found entry from top at " + p + "," + q
  2772. );
  2773. break;
  2774. }
  2775. } else if (d_x === 1) {
  2776. if (d_o === 0) {
  2777. console.log("MarchingSquaresJS-isoBands: wtf");
  2778. break;
  2779. } else {
  2780. /* try to go upwards */
  2781. if (
  2782. typeof grid.cells[q + 1] !== "undefined" &&
  2783. typeof grid.cells[q + 1][p] !== "undefined"
  2784. ) {
  2785. d_x = 0;
  2786. d_y = 1;
  2787. d_o = 0;
  2788. } else if (cval & Node1) {
  2789. path.push([p + 1, q + 1]);
  2790. d_x = 1;
  2791. d_y = 0;
  2792. d_o = 1;
  2793. } else {
  2794. /* found an entry point into regular grid! */
  2795. path.push([p + cell.topleft, q + 1]);
  2796. d_x = 0;
  2797. d_y = -1;
  2798. d_o = 0;
  2799. closed = true;
  2800. //console.log("found entry from bottom at " + p + "," + q);
  2801. break;
  2802. }
  2803. }
  2804. } else if (d_y === -1) {
  2805. if (d_o === 1) {
  2806. /* try to go right */
  2807. if (typeof grid.cells[q][p + 1] !== "undefined") {
  2808. d_x = 1;
  2809. d_y = 0;
  2810. d_o = 1;
  2811. } else if (cval & Node2) {
  2812. path.push([p + 1, q]);
  2813. d_x = 0;
  2814. d_y = -1;
  2815. d_o = 1;
  2816. } else {
  2817. /* found entry into regular grid! */
  2818. path.push([p + 1, q + cell.righttop]);
  2819. d_x = -1;
  2820. d_y = 0;
  2821. d_o = 1;
  2822. closed = true;
  2823. //console.log("found entry from top at " + p + "," + q);
  2824. break;
  2825. }
  2826. } else {
  2827. console.log("MarchingSquaresJS-isoBands: wtf");
  2828. break;
  2829. }
  2830. } else if (d_y === 1) {
  2831. if (d_o === 0) {
  2832. //console.log("we came from bottom left and proceed to the left");
  2833. /* try to go left */
  2834. if (typeof grid.cells[q][p - 1] !== "undefined") {
  2835. d_x = -1;
  2836. d_y = 0;
  2837. d_o = 0;
  2838. } else if (cval & Node0) {
  2839. path.push([p, q + 1]);
  2840. d_x = 0;
  2841. d_y = 1;
  2842. d_o = 0;
  2843. } else {
  2844. /* found an entry point into regular grid! */
  2845. path.push([p, q + cell.leftbottom]);
  2846. d_x = 1;
  2847. d_y = 0;
  2848. d_o = 0;
  2849. closed = true;
  2850. //console.log("found entry from bottom at " + p + "," + q);
  2851. break;
  2852. }
  2853. } else {
  2854. //console.log("we came from bottom right and proceed to the right");
  2855. console.log("MarchingSquaresJS-isoBands: wtf");
  2856. break;
  2857. }
  2858. } else {
  2859. console.log("MarchingSquaresJS-isoBands: where did we came from???");
  2860. break;
  2861. }
  2862. }
  2863. p += d_x;
  2864. q += d_y;
  2865. //console.log("going on to " + p + "," + q + " via " + d_x + " " + d_y + " " + d_o);
  2866. if (p === i && q === j) {
  2867. /* bail out, once we've closed a circle path */
  2868. break;
  2869. }
  2870. }
  2871. //console.log("exit with " + p + "," + q + " " + d_x + " " + d_y + " " + d_o);
  2872. return { path: path, i: p, j: q, x: d_x, y: d_y, o: d_o };
  2873. }
  2874. function deleteEdge(cell, edgeIdx) {
  2875. delete cell.edges[edgeIdx];
  2876. for (var k = edgeIdx + 1; k < cell.edges.length; k++) {
  2877. cell.edges[k - 1] = cell.edges[k];
  2878. }
  2879. cell.edges.pop();
  2880. }
  2881. function getStartXY(cell) {
  2882. if (cell.edges.length > 0) {
  2883. var e = cell.edges[cell.edges.length - 1];
  2884. //console.log("starting with edge " + e);
  2885. var cval = cell.cval_real;
  2886. switch (e) {
  2887. case 0:
  2888. if (cval & Node1) {
  2889. /* node 1 within range */
  2890. return { p: [1, cell.righttop], x: -1, y: 0, o: 1 };
  2891. } else {
  2892. /* node 1 below or above threshold */
  2893. return { p: [cell.topleft, 1], x: 0, y: -1, o: 0 };
  2894. }
  2895. case 1:
  2896. if (cval & Node2) {
  2897. return { p: [cell.topleft, 1], x: 0, y: -1, o: 0 };
  2898. } else {
  2899. return { p: [1, cell.rightbottom], x: -1, y: 0, o: 0 };
  2900. }
  2901. case 2:
  2902. if (cval & Node2) {
  2903. return { p: [cell.bottomright, 0], x: 0, y: 1, o: 1 };
  2904. } else {
  2905. return { p: [cell.topleft, 1], x: 0, y: -1, o: 0 };
  2906. }
  2907. case 3:
  2908. if (cval & Node3) {
  2909. return { p: [cell.topleft, 1], x: 0, y: -1, o: 0 };
  2910. } else {
  2911. return { p: [cell.bottomleft, 0], x: 0, y: 1, o: 0 };
  2912. }
  2913. case 4:
  2914. if (cval & Node1) {
  2915. return { p: [1, cell.righttop], x: -1, y: 0, o: 1 };
  2916. } else {
  2917. return { p: [cell.topright, 1], x: 0, y: -1, o: 1 };
  2918. }
  2919. case 5:
  2920. if (cval & Node2) {
  2921. return { p: [cell.topright, 1], x: 0, y: -1, o: 1 };
  2922. } else {
  2923. return { p: [1, cell.rightbottom], x: -1, y: 0, o: 0 };
  2924. }
  2925. case 6:
  2926. if (cval & Node2) {
  2927. return { p: [cell.bottomright, 0], x: 0, y: 1, o: 1 };
  2928. } else {
  2929. return { p: [cell.topright, 1], x: 0, y: -1, o: 1 };
  2930. }
  2931. case 7:
  2932. if (cval & Node3) {
  2933. return { p: [cell.topright, 1], x: 0, y: -1, o: 1 };
  2934. } else {
  2935. return { p: [cell.bottomleft, 0], x: 0, y: 1, o: 0 };
  2936. }
  2937. case 8:
  2938. if (cval & Node2) {
  2939. return { p: [cell.bottomright, 0], x: 0, y: 1, o: 1 };
  2940. } else {
  2941. return { p: [1, cell.righttop], x: -1, y: 0, o: 1 };
  2942. }
  2943. case 9:
  2944. if (cval & Node3) {
  2945. return { p: [1, cell.righttop], x: -1, y: 0, o: 1 };
  2946. } else {
  2947. return { p: [cell.bottomleft, 0], x: 0, y: 1, o: 0 };
  2948. }
  2949. case 10:
  2950. if (cval & Node3) {
  2951. return { p: [0, cell.leftbottom], x: 1, y: 0, o: 0 };
  2952. } else {
  2953. return { p: [1, cell.righttop], x: -1, y: 0, o: 1 };
  2954. }
  2955. case 11:
  2956. if (cval & Node0) {
  2957. return { p: [1, cell.righttop], x: -1, y: 0, o: 1 };
  2958. } else {
  2959. return { p: [0, cell.lefttop], x: 1, y: 0, o: 1 };
  2960. }
  2961. case 12:
  2962. if (cval & Node2) {
  2963. return { p: [cell.bottomright, 0], x: 0, y: 1, o: 1 };
  2964. } else {
  2965. return { p: [1, cell.rightbottom], x: -1, y: 0, o: 0 };
  2966. }
  2967. case 13:
  2968. if (cval & Node3) {
  2969. return { p: [1, cell.rightbottom], x: -1, y: 0, o: 0 };
  2970. } else {
  2971. return { p: [cell.bottomleft, 0], x: 0, y: 1, o: 0 };
  2972. }
  2973. case 14:
  2974. if (cval & Node3) {
  2975. return { p: [0, cell.leftbottom], x: 1, y: 0, o: 0 };
  2976. } else {
  2977. return { p: [1, cell.rightbottom], x: -1, y: 0, o: 0 };
  2978. }
  2979. case 15:
  2980. if (cval & Node0) {
  2981. return { p: [1, cell.rightbottom], x: -1, y: 0, o: 0 };
  2982. } else {
  2983. return { p: [0, cell.lefttop], x: 1, y: 0, o: 1 };
  2984. }
  2985. case 16:
  2986. if (cval & Node2) {
  2987. return { p: [cell.bottomright, 0], x: 0, y: 1, o: 1 };
  2988. } else {
  2989. return { p: [0, cell.leftbottom], x: 1, y: 0, o: 0 };
  2990. }
  2991. case 17:
  2992. if (cval & Node0) {
  2993. return { p: [cell.bottomright, 0], x: 0, y: 1, o: 1 };
  2994. } else {
  2995. return { p: [0, cell.lefttop], x: 1, y: 0, o: 1 };
  2996. }
  2997. case 18:
  2998. if (cval & Node3) {
  2999. return { p: [0, cell.leftbottom], x: 1, y: 0, o: 0 };
  3000. } else {
  3001. return { p: [cell.bottomleft, 0], x: 0, y: 1, o: 0 };
  3002. }
  3003. case 19:
  3004. if (cval & Node0) {
  3005. return { p: [cell.bottomleft, 0], x: 0, y: 1, o: 0 };
  3006. } else {
  3007. return { p: [0, cell.lefttop], x: 1, y: 0, o: 1 };
  3008. }
  3009. case 20:
  3010. if (cval & Node0) {
  3011. return { p: [cell.topleft, 1], x: 0, y: -1, o: 0 };
  3012. } else {
  3013. return { p: [0, cell.leftbottom], x: 1, y: 0, o: 0 };
  3014. }
  3015. case 21:
  3016. if (cval & Node1) {
  3017. return { p: [0, cell.leftbottom], x: 1, y: 0, o: 0 };
  3018. } else {
  3019. return { p: [cell.topright, 1], x: 0, y: -1, o: 1 };
  3020. }
  3021. case 22:
  3022. if (cval & Node0) {
  3023. return { p: [cell.topleft, 1], x: 0, y: -1, o: 0 };
  3024. } else {
  3025. return { p: [0, cell.lefttop], x: 1, y: 0, o: 1 };
  3026. }
  3027. case 23:
  3028. if (cval & Node1) {
  3029. return { p: [0, cell.lefttop], x: 1, y: 0, o: 1 };
  3030. } else {
  3031. return { p: [cell.topright, 1], x: 0, y: -1, o: 1 };
  3032. }
  3033. default:
  3034. console.log("MarchingSquaresJS-isoBands: edge index out of range!");
  3035. console.log(cell);
  3036. break;
  3037. }
  3038. }
  3039. return null;
  3040. }
  3041. function getExitXY(cell, x, y, o) {
  3042. var e,
  3043. id_x,
  3044. d_x,
  3045. d_y,
  3046. cval = cell.cval;
  3047. var d_o;
  3048. switch (x) {
  3049. case -1:
  3050. switch (o) {
  3051. case 0:
  3052. e = isoBandEdgeRB[cval];
  3053. d_x = isoBandNextXRB[cval];
  3054. d_y = isoBandNextYRB[cval];
  3055. d_o = isoBandNextORB[cval];
  3056. break;
  3057. default:
  3058. e = isoBandEdgeRT[cval];
  3059. d_x = isoBandNextXRT[cval];
  3060. d_y = isoBandNextYRT[cval];
  3061. d_o = isoBandNextORT[cval];
  3062. break;
  3063. }
  3064. break;
  3065. case 1:
  3066. switch (o) {
  3067. case 0:
  3068. e = isoBandEdgeLB[cval];
  3069. d_x = isoBandNextXLB[cval];
  3070. d_y = isoBandNextYLB[cval];
  3071. d_o = isoBandNextOLB[cval];
  3072. break;
  3073. default:
  3074. e = isoBandEdgeLT[cval];
  3075. d_x = isoBandNextXLT[cval];
  3076. d_y = isoBandNextYLT[cval];
  3077. d_o = isoBandNextOLT[cval];
  3078. break;
  3079. }
  3080. break;
  3081. default:
  3082. switch (y) {
  3083. case -1:
  3084. switch (o) {
  3085. case 0:
  3086. e = isoBandEdgeTL[cval];
  3087. d_x = isoBandNextXTL[cval];
  3088. d_y = isoBandNextYTL[cval];
  3089. d_o = isoBandNextOTL[cval];
  3090. break;
  3091. default:
  3092. e = isoBandEdgeTR[cval];
  3093. d_x = isoBandNextXTR[cval];
  3094. d_y = isoBandNextYTR[cval];
  3095. d_o = isoBandNextOTR[cval];
  3096. break;
  3097. }
  3098. break;
  3099. case 1:
  3100. switch (o) {
  3101. case 0:
  3102. e = isoBandEdgeBL[cval];
  3103. d_x = isoBandNextXBL[cval];
  3104. d_y = isoBandNextYBL[cval];
  3105. d_o = isoBandNextOBL[cval];
  3106. break;
  3107. default:
  3108. e = isoBandEdgeBR[cval];
  3109. d_x = isoBandNextXBR[cval];
  3110. d_y = isoBandNextYBR[cval];
  3111. d_o = isoBandNextOBR[cval];
  3112. break;
  3113. }
  3114. break;
  3115. }
  3116. break;
  3117. }
  3118. id_x = cell.edges.indexOf(e);
  3119. if (typeof cell.edges[id_x] !== "undefined") {
  3120. deleteEdge(cell, id_x);
  3121. } else {
  3122. //console.log("wrong edges...");
  3123. //console.log(x + " " + y + " " + o);
  3124. //console.log(cell);
  3125. return null;
  3126. }
  3127. cval = cell.cval_real;
  3128. switch (e) {
  3129. case 0:
  3130. if (cval & Node1) {
  3131. /* node 1 within range */
  3132. x = cell.topleft;
  3133. y = 1;
  3134. } else {
  3135. /* node 1 below or above threshold */
  3136. x = 1;
  3137. y = cell.righttop;
  3138. }
  3139. break;
  3140. case 1:
  3141. if (cval & Node2) {
  3142. x = 1;
  3143. y = cell.rightbottom;
  3144. } else {
  3145. x = cell.topleft;
  3146. y = 1;
  3147. }
  3148. break;
  3149. case 2:
  3150. if (cval & Node2) {
  3151. x = cell.topleft;
  3152. y = 1;
  3153. } else {
  3154. x = cell.bottomright;
  3155. y = 0;
  3156. }
  3157. break;
  3158. case 3:
  3159. if (cval & Node3) {
  3160. x = cell.bottomleft;
  3161. y = 0;
  3162. } else {
  3163. x = cell.topleft;
  3164. y = 1;
  3165. }
  3166. break;
  3167. case 4:
  3168. if (cval & Node1) {
  3169. x = cell.topright;
  3170. y = 1;
  3171. } else {
  3172. x = 1;
  3173. y = cell.righttop;
  3174. }
  3175. break;
  3176. case 5:
  3177. if (cval & Node2) {
  3178. x = 1;
  3179. y = cell.rightbottom;
  3180. } else {
  3181. x = cell.topright;
  3182. y = 1;
  3183. }
  3184. break;
  3185. case 6:
  3186. if (cval & Node2) {
  3187. x = cell.topright;
  3188. y = 1;
  3189. } else {
  3190. x = cell.bottomright;
  3191. y = 0;
  3192. }
  3193. break;
  3194. case 7:
  3195. if (cval & Node3) {
  3196. x = cell.bottomleft;
  3197. y = 0;
  3198. } else {
  3199. x = cell.topright;
  3200. y = 1;
  3201. }
  3202. break;
  3203. case 8:
  3204. if (cval & Node2) {
  3205. x = 1;
  3206. y = cell.righttop;
  3207. } else {
  3208. x = cell.bottomright;
  3209. y = 0;
  3210. }
  3211. break;
  3212. case 9:
  3213. if (cval & Node3) {
  3214. x = cell.bottomleft;
  3215. y = 0;
  3216. } else {
  3217. x = 1;
  3218. y = cell.righttop;
  3219. }
  3220. break;
  3221. case 10:
  3222. if (cval & Node3) {
  3223. x = 1;
  3224. y = cell.righttop;
  3225. } else {
  3226. x = 0;
  3227. y = cell.leftbottom;
  3228. }
  3229. break;
  3230. case 11:
  3231. if (cval & Node0) {
  3232. x = 0;
  3233. y = cell.lefttop;
  3234. } else {
  3235. x = 1;
  3236. y = cell.righttop;
  3237. }
  3238. break;
  3239. case 12:
  3240. if (cval & Node2) {
  3241. x = 1;
  3242. y = cell.rightbottom;
  3243. } else {
  3244. x = cell.bottomright;
  3245. y = 0;
  3246. }
  3247. break;
  3248. case 13:
  3249. if (cval & Node3) {
  3250. x = cell.bottomleft;
  3251. y = 0;
  3252. } else {
  3253. x = 1;
  3254. y = cell.rightbottom;
  3255. }
  3256. break;
  3257. case 14:
  3258. if (cval & Node3) {
  3259. x = 1;
  3260. y = cell.rightbottom;
  3261. } else {
  3262. x = 0;
  3263. y = cell.leftbottom;
  3264. }
  3265. break;
  3266. case 15:
  3267. if (cval & Node0) {
  3268. x = 0;
  3269. y = cell.lefttop;
  3270. } else {
  3271. x = 1;
  3272. y = cell.rightbottom;
  3273. }
  3274. break;
  3275. case 16:
  3276. if (cval & Node2) {
  3277. x = 0;
  3278. y = cell.leftbottom;
  3279. } else {
  3280. x = cell.bottomright;
  3281. y = 0;
  3282. }
  3283. break;
  3284. case 17:
  3285. if (cval & Node0) {
  3286. x = 0;
  3287. y = cell.lefttop;
  3288. } else {
  3289. x = cell.bottomright;
  3290. y = 0;
  3291. }
  3292. break;
  3293. case 18:
  3294. if (cval & Node3) {
  3295. x = cell.bottomleft;
  3296. y = 0;
  3297. } else {
  3298. x = 0;
  3299. y = cell.leftbottom;
  3300. }
  3301. break;
  3302. case 19:
  3303. if (cval & Node0) {
  3304. x = 0;
  3305. y = cell.lefttop;
  3306. } else {
  3307. x = cell.bottomleft;
  3308. y = 0;
  3309. }
  3310. break;
  3311. case 20:
  3312. if (cval & Node0) {
  3313. x = 0;
  3314. y = cell.leftbottom;
  3315. } else {
  3316. x = cell.topleft;
  3317. y = 1;
  3318. }
  3319. break;
  3320. case 21:
  3321. if (cval & Node1) {
  3322. x = cell.topright;
  3323. y = 1;
  3324. } else {
  3325. x = 0;
  3326. y = cell.leftbottom;
  3327. }
  3328. break;
  3329. case 22:
  3330. if (cval & Node0) {
  3331. x = 0;
  3332. y = cell.lefttop;
  3333. } else {
  3334. x = cell.topleft;
  3335. y = 1;
  3336. }
  3337. break;
  3338. case 23:
  3339. if (cval & Node1) {
  3340. x = cell.topright;
  3341. y = 1;
  3342. } else {
  3343. x = 0;
  3344. y = cell.lefttop;
  3345. }
  3346. break;
  3347. default:
  3348. console.log("MarchingSquaresJS-isoBands: edge index out of range!");
  3349. console.log(cell);
  3350. return null;
  3351. }
  3352. if (
  3353. typeof x === "undefined" ||
  3354. typeof y === "undefined" ||
  3355. typeof d_x === "undefined" ||
  3356. typeof d_y === "undefined" ||
  3357. typeof d_o === "undefined"
  3358. ) {
  3359. console.log("MarchingSquaresJS-isoBands: undefined value!");
  3360. console.log(cell);
  3361. console.log(x + " " + y + " " + d_x + " " + d_y + " " + d_o);
  3362. }
  3363. return { p: [x, y], x: d_x, y: d_y, o: d_o };
  3364. }
  3365. function BandGrid2Areas(grid) {
  3366. var areas = [];
  3367. var area_idx = 0;
  3368. grid.cells.forEach(function (g, j) {
  3369. g.forEach(function (gg, i) {
  3370. if (typeof gg !== "undefined") {
  3371. var a = polygon_table[gg.cval](gg);
  3372. if (typeof a === "object" && isArray(a)) {
  3373. if (typeof a[0] === "object" && isArray(a[0])) {
  3374. if (typeof a[0][0] === "object" && isArray(a[0][0])) {
  3375. a.forEach(function (aa) {
  3376. aa.forEach(function (aaa) {
  3377. aaa[0] += i;
  3378. aaa[1] += j;
  3379. });
  3380. areas[area_idx++] = aa;
  3381. });
  3382. } else {
  3383. a.forEach(function (aa) {
  3384. aa[0] += i;
  3385. aa[1] += j;
  3386. });
  3387. areas[area_idx++] = a;
  3388. }
  3389. } else {
  3390. console.log(
  3391. "MarchingSquaresJS-isoBands: bandcell polygon with malformed coordinates"
  3392. );
  3393. }
  3394. } else {
  3395. console.log(
  3396. "MarchingSquaresJS-isoBands: bandcell polygon with null coordinates"
  3397. );
  3398. }
  3399. }
  3400. });
  3401. });
  3402. return areas;
  3403. }
  3404. /**
  3405. * Takes a square or rectangular grid {@link FeatureCollection} of {@link Point} features with z-values and an array of
  3406. * value breaks and generates filled contour isobands.
  3407. *
  3408. * @name isobands
  3409. * @param {FeatureCollection<Point>} pointGrid input points - must be square or rectangular
  3410. * @param {Array<number>} breaks where to draw contours
  3411. * @param {Object} [options={}] options on output
  3412. * @param {string} [options.zProperty='elevation'] the property name in `points` from which z-values will be pulled
  3413. * @param {Object} [options.commonProperties={}] GeoJSON properties passed to ALL isobands
  3414. * @param {Array<Object>} [options.breaksProperties=[]] GeoJSON properties passed, in order, to the correspondent isoband (order defined by breaks)
  3415. * @returns {FeatureCollection<MultiPolygon>} a FeatureCollection of {@link MultiPolygon} features representing isobands
  3416. */
  3417. function isobands(pointGrid, breaks, options) {
  3418. // Optional parameters
  3419. options = options || {};
  3420. if (!helpers.isObject(options)) throw new Error("options is invalid");
  3421. var zProperty = options.zProperty || "elevation";
  3422. var commonProperties = options.commonProperties || {};
  3423. var breaksProperties = options.breaksProperties || [];
  3424. // Validation
  3425. invariant.collectionOf(pointGrid, "Point", "Input must contain Points");
  3426. if (!breaks) throw new Error("breaks is required");
  3427. if (!Array.isArray(breaks)) throw new Error("breaks is not an Array");
  3428. if (!helpers.isObject(commonProperties))
  3429. throw new Error("commonProperties is not an Object");
  3430. if (!Array.isArray(breaksProperties))
  3431. throw new Error("breaksProperties is not an Array");
  3432. // Isoband methods
  3433. var matrix = gridToMatrix(pointGrid, { zProperty: zProperty, flip: true });
  3434. var contours = createContourLines(matrix, breaks, zProperty);
  3435. contours = rescaleContours(contours, matrix, pointGrid);
  3436. var multipolygons = contours.map(function (contour, index) {
  3437. if (breaksProperties[index] && !helpers.isObject(breaksProperties[index])) {
  3438. throw new Error("Each mappedProperty is required to be an Object");
  3439. }
  3440. // collect all properties
  3441. var contourProperties = objectAssign__default['default'](
  3442. {},
  3443. commonProperties,
  3444. breaksProperties[index]
  3445. );
  3446. contourProperties[zProperty] = contour[zProperty];
  3447. var multiP = helpers.multiPolygon(contour.groupedRings, contourProperties);
  3448. return multiP;
  3449. });
  3450. return helpers.featureCollection(multipolygons);
  3451. }
  3452. /**
  3453. * Creates the contours lines (featuresCollection of polygon features) from the 2D data grid
  3454. *
  3455. * Marchingsquares process the grid data as a 3D representation of a function on a 2D plane, therefore it
  3456. * assumes the points (x-y coordinates) are one 'unit' distance. The result of the IsoBands function needs to be
  3457. * rescaled, with turfjs, to the original area and proportions on the map
  3458. *
  3459. * @private
  3460. * @param {Array<Array<number>>} matrix Grid Data
  3461. * @param {Array<number>} breaks Breaks
  3462. * @param {string} [property='elevation'] Property
  3463. * @returns {Array<any>} contours
  3464. */
  3465. function createContourLines(matrix, breaks, property) {
  3466. var contours = [];
  3467. for (var i = 1; i < breaks.length; i++) {
  3468. var lowerBand = +breaks[i - 1]; // make sure the breaks value is a number
  3469. var upperBand = +breaks[i];
  3470. var isobandsCoords = isoBands(matrix, lowerBand, upperBand - lowerBand);
  3471. // as per GeoJson rules for creating a Polygon, make sure the first element
  3472. // in the array of LinearRings represents the exterior ring (i.e. biggest area),
  3473. // and any subsequent elements represent interior rings (i.e. smaller area);
  3474. // this avoids rendering issues of the MultiPolygons on the map
  3475. var nestedRings = orderByArea(isobandsCoords);
  3476. var groupedRings = groupNestedRings(nestedRings);
  3477. var obj = {};
  3478. obj["groupedRings"] = groupedRings;
  3479. obj[property] = lowerBand + "-" + upperBand;
  3480. contours.push(obj);
  3481. }
  3482. return contours;
  3483. }
  3484. /**
  3485. * Transform isobands of 2D grid to polygons for the map
  3486. *
  3487. * @private
  3488. * @param {Array<any>} contours Contours
  3489. * @param {Array<Array<number>>} matrix Grid Data
  3490. * @param {Object} points Points by Latitude
  3491. * @returns {Array<any>} contours
  3492. */
  3493. function rescaleContours(contours, matrix, points) {
  3494. // get dimensions (on the map) of the original grid
  3495. var gridBbox = bbox__default['default'](points); // [ minX, minY, maxX, maxY ]
  3496. var originalWidth = gridBbox[2] - gridBbox[0];
  3497. var originalHeigth = gridBbox[3] - gridBbox[1];
  3498. // get origin, which is the first point of the last row on the rectangular data on the map
  3499. var x0 = gridBbox[0];
  3500. var y0 = gridBbox[1];
  3501. // get number of cells per side
  3502. var matrixWidth = matrix[0].length - 1;
  3503. var matrixHeight = matrix.length - 1;
  3504. // calculate the scaling factor between matrix and rectangular grid on the map
  3505. var scaleX = originalWidth / matrixWidth;
  3506. var scaleY = originalHeigth / matrixHeight;
  3507. var resize = function (point) {
  3508. point[0] = point[0] * scaleX + x0;
  3509. point[1] = point[1] * scaleY + y0;
  3510. };
  3511. // resize and shift each point/line of the isobands
  3512. contours.forEach(function (contour) {
  3513. contour.groupedRings.forEach(function (lineRingSet) {
  3514. lineRingSet.forEach(function (lineRing) {
  3515. lineRing.forEach(resize);
  3516. });
  3517. });
  3518. });
  3519. return contours;
  3520. }
  3521. /* utility functions */
  3522. /**
  3523. * Returns an array of coordinates (of LinearRings) in descending order by area
  3524. *
  3525. * @private
  3526. * @param {Array<LineString>} ringsCoords array of closed LineString
  3527. * @returns {Array} array of the input LineString ordered by area
  3528. */
  3529. function orderByArea(ringsCoords) {
  3530. var ringsWithArea = [];
  3531. var areas = [];
  3532. ringsCoords.forEach(function (coords) {
  3533. // var poly = polygon([points]);
  3534. var ringArea = area__default['default'](helpers.polygon([coords]));
  3535. // create an array of areas value
  3536. areas.push(ringArea);
  3537. // associate each lineRing with its area
  3538. ringsWithArea.push({ ring: coords, area: ringArea });
  3539. });
  3540. areas.sort(function (a, b) {
  3541. // bigger --> smaller
  3542. return b - a;
  3543. });
  3544. // create a new array of linearRings coordinates ordered by their area
  3545. var orderedByArea = [];
  3546. areas.forEach(function (area) {
  3547. for (var lr = 0; lr < ringsWithArea.length; lr++) {
  3548. if (ringsWithArea[lr].area === area) {
  3549. orderedByArea.push(ringsWithArea[lr].ring);
  3550. ringsWithArea.splice(lr, 1);
  3551. break;
  3552. }
  3553. }
  3554. });
  3555. return orderedByArea;
  3556. }
  3557. /**
  3558. * Returns an array of arrays of coordinates, each representing
  3559. * a set of (coordinates of) nested LinearRings,
  3560. * i.e. the first ring contains all the others
  3561. *
  3562. * @private
  3563. * @param {Array} orderedLinearRings array of coordinates (of LinearRings) in descending order by area
  3564. * @returns {Array<Array>} Array of coordinates of nested LinearRings
  3565. */
  3566. function groupNestedRings(orderedLinearRings) {
  3567. // create a list of the (coordinates of) LinearRings
  3568. var lrList = orderedLinearRings.map(function (lr) {
  3569. return { lrCoordinates: lr, grouped: false };
  3570. });
  3571. var groupedLinearRingsCoords = [];
  3572. while (!allGrouped(lrList)) {
  3573. for (var i = 0; i < lrList.length; i++) {
  3574. if (!lrList[i].grouped) {
  3575. // create new group starting with the larger not already grouped ring
  3576. var group = [];
  3577. group.push(lrList[i].lrCoordinates);
  3578. lrList[i].grouped = true;
  3579. var outerMostPoly = helpers.polygon([lrList[i].lrCoordinates]);
  3580. // group all the rings contained by the outermost ring
  3581. for (var j = i + 1; j < lrList.length; j++) {
  3582. if (!lrList[j].grouped) {
  3583. var lrPoly = helpers.polygon([lrList[j].lrCoordinates]);
  3584. if (isInside(lrPoly, outerMostPoly)) {
  3585. group.push(lrList[j].lrCoordinates);
  3586. lrList[j].grouped = true;
  3587. }
  3588. }
  3589. }
  3590. // insert the new group
  3591. groupedLinearRingsCoords.push(group);
  3592. }
  3593. }
  3594. }
  3595. return groupedLinearRingsCoords;
  3596. }
  3597. /**
  3598. * @private
  3599. * @param {Polygon} testPolygon polygon of interest
  3600. * @param {Polygon} targetPolygon polygon you want to compare with
  3601. * @returns {boolean} true if test-Polygon is inside target-Polygon
  3602. */
  3603. function isInside(testPolygon, targetPolygon) {
  3604. var points = explode__default['default'](testPolygon);
  3605. for (var i = 0; i < points.features.length; i++) {
  3606. if (!booleanPointInPolygon__default['default'](points.features[i], targetPolygon)) {
  3607. return false;
  3608. }
  3609. }
  3610. return true;
  3611. }
  3612. /**
  3613. * @private
  3614. * @param {Array<Object>} list list of objects which might contain the 'group' attribute
  3615. * @returns {boolean} true if all the objects in the list are marked as grouped
  3616. */
  3617. function allGrouped(list) {
  3618. for (var i = 0; i < list.length; i++) {
  3619. if (list[i].grouped === false) {
  3620. return false;
  3621. }
  3622. }
  3623. return true;
  3624. }
  3625. module.exports = isobands;
  3626. module.exports.default = isobands;