index.js 119 KB

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