michael@0: /* Any copyright is dedicated to the Public Domain. michael@0: http://creativecommons.org/publicdomain/zero/1.0/ */ michael@0: "use strict"; michael@0: michael@0: function cloneUpdate(update) { michael@0: return { michael@0: rotation: quat4.create(update.rotation), michael@0: translation: vec3.create(update.translation) michael@0: }; michael@0: } michael@0: michael@0: function isExpectedUpdate(update1, update2) { michael@0: if (update1.length !== update2.length) { michael@0: return false; michael@0: } michael@0: for (let i = 0, len = update1.length; i < len; i++) { michael@0: if (!isApproxVec(update1[i].rotation, update2[i].rotation) || michael@0: !isApproxVec(update1[i].translation, update2[i].translation)) { michael@0: info("isExpectedUpdate expected " + JSON.stringify(update1), ", got " + michael@0: JSON.stringify(update2) + " instead."); michael@0: return false; michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: function test() { michael@0: let arcball1 = new TiltVisualizer.Arcball(window, 123, 456); michael@0: michael@0: is(arcball1.width, 123, michael@0: "The first arcball width wasn't set correctly."); michael@0: is(arcball1.height, 456, michael@0: "The first arcball height wasn't set correctly."); michael@0: is(arcball1.radius, 123, michael@0: "The first arcball radius wasn't implicitly set correctly."); michael@0: michael@0: michael@0: let arcball2 = new TiltVisualizer.Arcball(window, 987, 654); michael@0: michael@0: is(arcball2.width, 987, michael@0: "The second arcball width wasn't set correctly."); michael@0: is(arcball2.height, 654, michael@0: "The second arcball height wasn't set correctly."); michael@0: is(arcball2.radius, 654, michael@0: "The second arcball radius wasn't implicitly set correctly."); michael@0: michael@0: michael@0: let arcball3 = new TiltVisualizer.Arcball(window, 512, 512); michael@0: michael@0: let sphereVec = vec3.create(); michael@0: arcball3._pointToSphere(123, 456, 256, 512, 512, sphereVec); michael@0: michael@0: ok(isApproxVec(sphereVec, [-0.009765625, 0.390625, 0.9204980731010437]), michael@0: "The _pointToSphere() function didn't map the coordinates correctly."); michael@0: michael@0: let stack1 = []; michael@0: let expect1 = [ michael@0: { rotation: [ michael@0: -0.08877250552177429, 0.0242881178855896, michael@0: -0.04222869873046875, -0.9948599338531494], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.13086390495300293, 0.03413732722401619, michael@0: -0.06334304809570312, -0.9887855648994446], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.15138940513134003, 0.03854173421859741, michael@0: -0.07390022277832031, -0.9849540591239929], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.1615273654460907, 0.040619146078825, michael@0: -0.0791788101196289, -0.9828477501869202], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.16656573116779327, 0.04162723943591118, michael@0: -0.0818181037902832, -0.9817478656768799], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.16907735168933868, 0.042123712599277496, michael@0: -0.08313775062561035, -0.9811863303184509], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.17033125460147858, 0.042370058596134186, michael@0: -0.08379757404327393, -0.9809026718139648], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.17095772922039032, 0.04249274358153343, michael@0: -0.08412748575210571, -0.9807600975036621], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.17127084732055664, 0.04255397245287895, michael@0: -0.0842924416065216, -0.9806886315345764], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.171427384018898, 0.042584557086229324, michael@0: -0.08437491953372955, -0.9806528687477112], michael@0: translation: [0, 0, 0] }]; michael@0: michael@0: arcball3.mouseDown(10, 10, 1); michael@0: arcball3.mouseMove(10, 100); michael@0: for (let i1 = 0; i1 < 10; i1++) { michael@0: stack1.push(cloneUpdate(arcball3.update())); michael@0: } michael@0: michael@0: ok(isExpectedUpdate(stack1, expect1), michael@0: "Mouse down & move events didn't create the expected transform. results."); michael@0: michael@0: let stack2 = []; michael@0: let expect2 = [ michael@0: { rotation: [ michael@0: -0.1684110015630722, 0.04199237748980522, michael@0: -0.0827873945236206, -0.9813361167907715], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.16936375200748444, 0.04218007251620293, michael@0: -0.08328840136528015, -0.9811217188835144], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.17003019154071808, 0.04231100529432297, michael@0: -0.08363909274339676, -0.9809709787368774], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.17049652338027954, 0.042402446269989014, michael@0: -0.0838845893740654, -0.9808651208877563], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.17082282900810242, 0.042466338723897934, michael@0: -0.08405643701553345, -0.9807908535003662], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.17105120420455933, 0.04251104220747948, michael@0: -0.08417671173810959, -0.9807388186454773], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.17121103405952454, 0.04254228621721268, michael@0: -0.08426092565059662, -0.9807023406028748], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.17132291197776794, 0.042564138770103455, michael@0: -0.08431987464427948, -0.9806767106056213], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.1714012324810028, 0.04257945716381073, michael@0: -0.08436112850904465, -0.9806588888168335], michael@0: translation: [0, 0, 0] }, michael@0: { rotation: [ michael@0: -0.17145603895187378, 0.042590171098709106, michael@0: -0.08439001441001892, -0.9806463718414307], michael@0: translation: [0, 0, 0] }]; michael@0: michael@0: arcball3.mouseUp(100, 100); michael@0: for (let i2 = 0; i2 < 10; i2++) { michael@0: stack2.push(cloneUpdate(arcball3.update())); michael@0: } michael@0: michael@0: ok(isExpectedUpdate(stack2, expect2), michael@0: "Mouse up events didn't create the expected transformation results."); michael@0: michael@0: let stack3 = []; michael@0: let expect3 = [ michael@0: { rotation: [ michael@0: -0.17149439454078674, 0.04259764403104782, michael@0: -0.08441022783517838, -0.9806375503540039], michael@0: translation: [0, 0, -1] }, michael@0: { rotation: [ michael@0: -0.17152123153209686, 0.04260288551449776, michael@0: -0.08442437648773193, -0.980631411075592], michael@0: translation: [0, 0, -1.899999976158142] }, michael@0: { rotation: [ michael@0: -0.1715400665998459, 0.04260658100247383, michael@0: -0.08443428575992584, -0.9806271195411682], michael@0: translation: [0, 0, -2.7100000381469727] }, michael@0: { rotation: [ michael@0: -0.17155319452285767, 0.04260912910103798, michael@0: -0.08444121479988098, -0.9806240797042847], michael@0: translation: [0, 0, -3.439000129699707] }, michael@0: { rotation: [ michael@0: -0.17156240344047546, 0.042610932141542435, michael@0: -0.08444607257843018, -0.9806219935417175], michael@0: translation: [0, 0, -4.095099925994873] }, michael@0: { rotation: [ michael@0: -0.1715688556432724, 0.042612191289663315, michael@0: -0.08444946259260178, -0.9806205034255981], michael@0: translation: [0, 0, -4.685589790344238] }, michael@0: { rotation: [ michael@0: -0.17157337069511414, 0.04261308163404465, michael@0: -0.0844518393278122, -0.980619490146637], michael@0: translation: [0, 0, -5.217031002044678] }, michael@0: { rotation: [ michael@0: -0.17157652974128723, 0.0426136814057827, michael@0: -0.0844535157084465, -0.9806187748908997], michael@0: translation: [0, 0, -5.6953277587890625] }, michael@0: { rotation: [ michael@0: -0.17157875001430511, 0.04261413961648941, michael@0: -0.08445467799901962, -0.9806182980537415], michael@0: translation: [0, 0, -6.125794887542725] }, michael@0: { rotation: [ michael@0: -0.17158031463623047, 0.04261442646384239, michael@0: -0.08445550501346588, -0.980617880821228], michael@0: translation: [0, 0, -6.5132155418396] }]; michael@0: michael@0: arcball3.zoom(10); michael@0: for (let i3 = 0; i3 < 10; i3++) { michael@0: stack3.push(cloneUpdate(arcball3.update())); michael@0: } michael@0: michael@0: ok(isExpectedUpdate(stack3, expect3), michael@0: "Mouse zoom events didn't create the expected transformation results."); michael@0: michael@0: let stack4 = []; michael@0: let expect4 = [ michael@0: { rotation: [ michael@0: -0.17158135771751404, 0.04261462762951851, michael@0: -0.08445606380701065, -0.9806176424026489], michael@0: translation: [0, 0, -6.861894130706787] }, michael@0: { rotation: [ michael@0: -0.1715821474790573, 0.04261479899287224, michael@0: -0.08445646613836288, -0.9806175231933594], michael@0: translation: [0, 0, -7.1757049560546875] }, michael@0: { rotation: [ michael@0: -0.1715826541185379, 0.0426148846745491, michael@0: -0.08445674180984497, -0.980617344379425], michael@0: translation: [0, 0, -7.458134651184082] }, michael@0: { rotation: [ michael@0: -0.17158304154872894, 0.04261497035622597, michael@0: -0.08445693552494049, -0.9806172847747803], michael@0: translation: [0, 0, -7.7123212814331055] }, michael@0: { rotation: [ michael@0: -0.17158329486846924, 0.042615000158548355, michael@0: -0.08445708453655243, -0.9806172251701355], michael@0: translation: [0, 0, -7.941089153289795] }, michael@0: { rotation: [ michael@0: -0.17158347368240356, 0.04261505603790283, michael@0: -0.084457166492939, -0.9806172251701355], michael@0: translation: [0, 0, -8.146980285644531] }, michael@0: { rotation: [ michael@0: -0.1715836226940155, 0.04261508584022522, michael@0: -0.08445724099874496, -0.9806171655654907], michael@0: translation: [0, 0, -8.332282066345215] }, michael@0: { rotation: [ michael@0: -0.17158368229866028, 0.04261508584022522, michael@0: -0.08445728570222855, -0.980617105960846], michael@0: translation: [0, 0, -8.499053955078125] }, michael@0: { rotation: [ michael@0: -0.17158377170562744, 0.04261511191725731, michael@0: -0.08445732295513153, -0.980617105960846], michael@0: translation: [0, 0, -8.649148941040039] }, michael@0: { rotation: [ michael@0: -0.17158380150794983, 0.04261511191725731, michael@0: -0.08445733785629272, -0.980617105960846], michael@0: translation: [0, 0, -8.784234046936035] }]; michael@0: michael@0: arcball3.keyDown(arcball3.rotateKeys.left); michael@0: arcball3.keyDown(arcball3.rotateKeys.right); michael@0: arcball3.keyDown(arcball3.rotateKeys.up); michael@0: arcball3.keyDown(arcball3.rotateKeys.down); michael@0: arcball3.keyDown(arcball3.panKeys.left); michael@0: arcball3.keyDown(arcball3.panKeys.right); michael@0: arcball3.keyDown(arcball3.panKeys.up); michael@0: arcball3.keyDown(arcball3.panKeys.down); michael@0: for (let i4 = 0; i4 < 10; i4++) { michael@0: stack4.push(cloneUpdate(arcball3.update())); michael@0: } michael@0: michael@0: ok(isExpectedUpdate(stack4, expect4), michael@0: "Key down events didn't create the expected transformation results."); michael@0: michael@0: let stack5 = []; michael@0: let expect5 = [ michael@0: { rotation: [ michael@0: -0.1715838462114334, 0.04261511191725731, michael@0: -0.08445736765861511, -0.980617105960846], michael@0: translation: [0, 0, -8.905810356140137] }, michael@0: { rotation: [ michael@0: -0.1715838462114334, 0.04261511191725731, michael@0: -0.08445736765861511, -0.980617105960846], michael@0: translation: [0, 0, -9.015229225158691] }, michael@0: { rotation: [ michael@0: -0.1715838462114334, 0.04261511191725731, michael@0: -0.08445736765861511, -0.980617105960846], michael@0: translation: [0, 0, -9.113706588745117] }, michael@0: { rotation: [ michael@0: -0.1715838611125946, 0.04261511191725731, michael@0: -0.0844573825597763, -0.9806170463562012], michael@0: translation: [0, 0, -9.202336311340332] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, -9.282102584838867] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, -9.35389232635498] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, -9.418502807617188] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, -9.476652145385742] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, -9.528986930847168] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, -9.576087951660156] }]; michael@0: michael@0: arcball3.keyUp(arcball3.rotateKeys.left); michael@0: arcball3.keyUp(arcball3.rotateKeys.right); michael@0: arcball3.keyUp(arcball3.rotateKeys.up); michael@0: arcball3.keyUp(arcball3.rotateKeys.down); michael@0: arcball3.keyUp(arcball3.panKeys.left); michael@0: arcball3.keyUp(arcball3.panKeys.right); michael@0: arcball3.keyUp(arcball3.panKeys.up); michael@0: arcball3.keyUp(arcball3.panKeys.down); michael@0: for (let i5 = 0; i5 < 10; i5++) { michael@0: stack5.push(cloneUpdate(arcball3.update())); michael@0: } michael@0: michael@0: ok(isExpectedUpdate(stack5, expect5), michael@0: "Key up events didn't create the expected transformation results."); michael@0: michael@0: let stack6 = []; michael@0: let expect6 = [ michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, -9.618478775024414] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, -6.156630992889404] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 0.4590320587158203] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 9.913128852844238] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 21.921815872192383] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 36.22963333129883] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 52.60667037963867] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 70.84600067138672] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 90.76139831542969] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 112.18525695800781] }]; michael@0: michael@0: arcball3.keyDown(arcball3.zoomKeys["in"][0]); michael@0: arcball3.keyDown(arcball3.zoomKeys["in"][1]); michael@0: arcball3.keyDown(arcball3.zoomKeys["in"][2]); michael@0: for (let i6 = 0; i6 < 10; i6++) { michael@0: stack6.push(cloneUpdate(arcball3.update())); michael@0: } michael@0: arcball3.keyUp(arcball3.zoomKeys["in"][0]); michael@0: arcball3.keyUp(arcball3.zoomKeys["in"][1]); michael@0: arcball3.keyUp(arcball3.zoomKeys["in"][2]); michael@0: michael@0: ok(isExpectedUpdate(stack6, expect6), michael@0: "Key zoom in events didn't create the expected transformation results."); michael@0: michael@0: let stack7 = []; michael@0: let expect7 = [ michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 134.96673583984375] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 151.97006225585938] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 163.77305603027344] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 170.895751953125] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 173.80618286132812] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 172.92556762695312] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 168.6330108642578] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 161.26971435546875] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 151.1427459716797] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 138.52847290039062] }]; michael@0: michael@0: arcball3.keyDown(arcball3.zoomKeys["out"][0]); michael@0: arcball3.keyDown(arcball3.zoomKeys["out"][1]); michael@0: for (let i7 = 0; i7 < 10; i7++) { michael@0: stack7.push(cloneUpdate(arcball3.update())); michael@0: } michael@0: arcball3.keyUp(arcball3.zoomKeys["out"][0]); michael@0: arcball3.keyUp(arcball3.zoomKeys["out"][1]); michael@0: michael@0: ok(isExpectedUpdate(stack7, expect7), michael@0: "Key zoom out events didn't create the expected transformation results."); michael@0: michael@0: let stack8 = []; michael@0: let expect8 = [ michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 123.67562866210938] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 111.30806732177734] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 100.17726135253906] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 90.15953826904297] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 81.14358520507812] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 73.02922821044922] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 65.72630310058594] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 59.15367126464844] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 53.238304138183594] }, michael@0: { rotation: [ michael@0: -0.17158392071723938, 0.0426151417195797, michael@0: -0.0844573974609375, -0.980617105960846], michael@0: translation: [0, 0, 47.91447448730469] }]; michael@0: michael@0: arcball3.keyDown(arcball3.zoomKeys["unzoom"]); michael@0: for (let i8 = 0; i8 < 10; i8++) { michael@0: stack8.push(cloneUpdate(arcball3.update())); michael@0: } michael@0: arcball3.keyUp(arcball3.zoomKeys["unzoom"]); michael@0: michael@0: ok(isExpectedUpdate(stack8, expect8), michael@0: "Key zoom reset events didn't create the expected transformation results."); michael@0: michael@0: michael@0: arcball3.resize(123, 456); michael@0: is(arcball3.width, 123, michael@0: "The third arcball width wasn't updated correctly."); michael@0: is(arcball3.height, 456, michael@0: "The third arcball height wasn't updated correctly."); michael@0: is(arcball3.radius, 123, michael@0: "The third arcball radius wasn't implicitly updated correctly."); michael@0: }