threeJsHelper.js 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732
  1. IPE.app = {};
  2. IPE.helper = {};
  3. IPE.interceptors = {};
  4. String.prototype.endsWith = function(suffix) {
  5. return this.indexOf(suffix, this.length - suffix.length) !== -1;
  6. };
  7. Array.prototype.last = function() {
  8. return this[this.length-1];
  9. }
  10. IPE.app = {
  11. init : function(config) {
  12. var headerHeight = $('header').height();
  13. if($(window).height() < 500) {
  14. $("body").animate({ scrollTop: headerHeight }, "slow");
  15. }
  16. $.each(IPE.interceptors, function(index, interceptor) {
  17. interceptor.init();
  18. });
  19. Globals.init();
  20. IPE.app.initializeHelper();
  21. IPE.app.bindEvents();
  22. IPE.app.initData();
  23. tempRenderer = new THREE.WebGLRenderer();
  24. maximumTextureSize = tempRenderer.context.getParameter(tempRenderer.context.MAX_TEXTURE_SIZE);
  25. // we have to take an power of 2 less
  26. maximumTextureSize /= 2;
  27. if(config.defaultSize > maximumTextureSize) {
  28. config.defaultSize = maximumTextureSize;
  29. }
  30. // Getting info file
  31. $.ajax({
  32. url: infoUrl + config.defaultSize,
  33. success: function (data) {
  34. textureInfo = data;
  35. originalSizeArray[0] = textureInfo.originalSize.sizex;
  36. originalSizeArray[1] = textureInfo.originalSize.sizey;
  37. originalSizeArray[2] = textureInfo.originalSize.sizez;
  38. imgInfo = data.info;
  39. currentVolumeDimension.xmin = 0;
  40. currentVolumeDimension.xmax = textureInfo.originalSize.sizex;
  41. currentVolumeDimension.ymin = 0;
  42. currentVolumeDimension.ymax = textureInfo.originalSize.sizey;
  43. currentVolumeDimension.zmin = imgInfo.imgMin;
  44. currentVolumeDimension.zmax = imgInfo.imgMax;
  45. // downloading first image and starting
  46. helper.downloadImage(spriteUrl + textureInfo.sprites[0][0].$oid, 0, 0, true);
  47. $(document).trigger('basicsReady');
  48. },
  49. dataType: 'json'
  50. });
  51. container = document.getElementById( 'container' );
  52. $container = $(container);
  53. containerHeader = document.getElementById( 'container-header' );
  54. $containerHeader = $(containerHeader);
  55. containerOverview = document.getElementById('container-overview');
  56. $containerOverview = $(containerOverview);
  57. statsHelper = new StatsHelper($containerHeader.children('#container-stats'));
  58. // until now, it has to be initialized here, needs a new internal structure
  59. zoomHelper = new ZoomHelper();
  60. $(window).on('scroll', function(e) {
  61. if($(this).scrollTop() >= headerHeight) {
  62. $container.css('position', 'relative');
  63. $container.css('top', $(this).scrollTop() - headerHeight);
  64. } else {
  65. $container.css('top', 0);
  66. }
  67. });
  68. },
  69. initializeHelper: function() {
  70. helper = new Helper();
  71. geometryHelper = new GeometryHelper();
  72. stepHelper = new StepHelper();
  73. overviewHelper = new OverviewHelper();
  74. transferHelper = new TransferHelper();
  75. opacityPlotter = new OpacityPlotter();
  76. },
  77. initData: function() {
  78. volumeId = $('#volumeId').val();
  79. $resSpan = $('span#currentRes');
  80. volumeUrl = IPE.util.buildUrl('volumes/' + volumeId + '/');
  81. infoUrl = volumeUrl + 'texture-info/';
  82. spriteUrl = volumeUrl + 'sprite/';
  83. waitDiv = $('div#wait');
  84. helper.initializeShaders(IPE.util.buildUrl('static/shaders/three-shader.vert'), function(script) {
  85. vertexShader = script
  86. });
  87. helper.initializeShaders(IPE.util.buildUrl('static/shaders/three-shader-simple.frag'), function(script) {
  88. fragmentSimleShader = script
  89. });
  90. helper.initializeShaders(IPE.util.buildUrl('static/shaders/three-shader.frag'), function(script) {
  91. fragmentShader = script
  92. });
  93. },
  94. bindEvents : function() {
  95. $(document).on('firstImageOnLoad',function (e, firstImageParams){
  96. initTextures(imgInfo, firstImageParams, true);
  97. initFrameControls();
  98. stepHelper.initStepControls();
  99. initSliders();
  100. });
  101. $(document).on('imageOnLoad', function(e, imageParams) {
  102. initTextures(imgInfo, imageParams, false);
  103. if(useDownloadedTextureImmediately) {
  104. updateTexture();
  105. useDownloadedTextureImmediately = false;
  106. updateFrameControls();
  107. }
  108. });
  109. $(document).on('firstFrameSpritesCompleted', function() {
  110. init();
  111. addResolutionLinks();
  112. overviewHelper.initOverview(containerOverview);
  113. animate();
  114. });
  115. showPopup = false;
  116. $('#save-canvas').click(function(e, params) {
  117. if(params != undefined && params.source != undefined && params.source == 'animate-function') {
  118. window.open(params.imageUrl);
  119. return false;
  120. } else {
  121. saveImage = true;
  122. return false;
  123. }
  124. });
  125. $(window).on('resize', function() {
  126. var rendererSizeHolder = calculateWidthHeight();
  127. width = rendererSizeHolder.width.px;
  128. height = rendererSizeHolder.height.px;
  129. cameraRTT.aspect = width / height;
  130. cameraRTT.updateProjectionMatrix();
  131. camera.aspect = width / height;
  132. camera.updateProjectionMatrix();
  133. renderer.setSize( width, height );
  134. renderer.domElement.style.width = rendererSizeHolder.width.percent + '%';
  135. renderer.domElement.style.height = rendererSizeHolder.height.percent + '%';
  136. });
  137. $resolutionLinks.on('click', 'a', function(e) {
  138. e.preventDefault();
  139. e.stopPropagation();
  140. var $this = $(this);
  141. if($this.attr('href') == undefined) {
  142. return false;
  143. }
  144. requestedSize = $this.attr('data');
  145. $('.res-link').each(function(index, value) {
  146. var $value = $(value);
  147. $value.attr('href', '#');
  148. });
  149. $this.removeAttr('href');
  150. $.ajax({
  151. // todo test after remove of sync flag
  152. url: infoUrl + requestedSize,
  153. success: function (data) {
  154. framesDownloaded = 0;
  155. textures = {};
  156. currentTexture = 0;
  157. textureInfo = data;
  158. imgInfo = data.info;
  159. imgMin = imgInfo.imgMin;
  160. imgMax = imgInfo.imgMax;
  161. useDownloadedTextureImmediately = true;
  162. helper.downloadImage(spriteUrl + textureInfo.sprites[0][0].$oid, 0, 0, false);
  163. $resSpan.text(requestedSize);
  164. if($('input#hide-content-small-devices').is(':visible')) {
  165. $('input#hide-content-small-devices').trigger('click');
  166. }
  167. },
  168. dataType: 'json'
  169. });
  170. return false;
  171. });
  172. $('div.controls').on('click', function() {
  173. var $this = $(this),
  174. bottomControlsContainer = $('div#bottom-controls-container'),
  175. bottomControls = $('div#bottom-controls'),
  176. filledById = '',
  177. thisControlHolder = null;
  178. filledById = hideBottomControlsContainer();
  179. if(filledById === $this.attr('id')) {
  180. return;
  181. }
  182. thisControlHolder = $this.children('.control-holder')[0];
  183. thisControlHolder = $(thisControlHolder);
  184. bottomControls.append(thisControlHolder.children());
  185. bottomControls.attr('filledBy', $this.attr('id'));
  186. bottomControlsContainer.removeClass('hidden');
  187. // inform, that it is showed
  188. $this.trigger('controlsShowed');
  189. });
  190. $('input#hide-bottom-controls-container').on('click', function() {
  191. hideBottomControlsContainer();
  192. });
  193. $('div#menu-buttons-small-devices input').on('click', function() {
  194. var $this = $(this),
  195. target = $this.attr('data'),
  196. $target = $('#' + target),
  197. $overlay = $('div#overlay-small-devices'),
  198. $content = $('div#content-small-devices');
  199. $overlay.attr('source', target);
  200. $content.append($target.children());
  201. $overlay.show();
  202. });
  203. $('input#hide-content-small-devices').on('click', function() {
  204. var $overlay = $('div#overlay-small-devices'),
  205. $content = $('div#content-small-devices'),
  206. source = $overlay.attr('source'),
  207. $source = $('#' + source);
  208. $overlay.hide();
  209. $overlay.attr('source', '');
  210. $source.append($content.children());
  211. });
  212. var buttonAdvCons = $('input#show-adv-con');
  213. buttonAdvCons.on('click', function() {
  214. var advCons = $('div#advanced-controls');
  215. if(advCons.hasClass('hidden')) {
  216. advCons.removeClass('hidden');
  217. buttonAdvCons.val('hide adv cons');
  218. } else {
  219. advCons.addClass('hidden');
  220. buttonAdvCons.val('show adv cons');
  221. }
  222. });
  223. $(document).on('initFinished', function() {
  224. $canvas.addClass('context-menu');
  225. $.contextMenu({
  226. selector: 'canvas.context-menu',
  227. callback: function(key, options) {
  228. if(key === 'save') {
  229. saveImage = true;
  230. }
  231. },
  232. items: {
  233. "save": {name: "Open image in new tab"}
  234. }
  235. });
  236. });
  237. $('input#adjust-isodata-thresholding-bottom-controls-container').on('click', function() {
  238. console.log(textureInfo.info.thresholdIndex_isodata);
  239. GenericSlider.setPosSlider(
  240. slider = $( "#gray-slider" ),
  241. sliderInputs = $('input.gray'),
  242. minGray,
  243. textureInfo.info.thresholdIndex_isodata
  244. );
  245. $(document).trigger('opacityBordersChanged', { minGray: minGray, maxGray: textureInfo.info.thresholdIndex_isodata});
  246. });
  247. $('input#adjust-otsu-thresholding-bottom-controls-container').on('click', function() {
  248. console.log(textureInfo.info.thresholdIndex_otsu);
  249. GenericSlider.setPosSlider(
  250. slider = $( "#gray-slider" ),
  251. sliderInputs = $('input.gray'),
  252. minGray,
  253. textureInfo.info.thresholdIndex_otsu
  254. );
  255. $(document).trigger('opacityBordersChanged', { minGray: minGray, maxGray: textureInfo.info.thresholdIndex_otsu});
  256. });
  257. $('input#adjust-yen-thresholding-bottom-controls-container').on('click', function() {
  258. console.log(textureInfo.info.thresholdIndex_yen);
  259. GenericSlider.setPosSlider(
  260. slider = $( "#gray-slider" ),
  261. sliderInputs = $('input.gray'),
  262. minGray,
  263. textureInfo.info.thresholdIndex_yen
  264. );
  265. $(document).trigger('opacityBordersChanged', { minGray: minGray, maxGray: textureInfo.info.thresholdIndex_yen});
  266. });
  267. }
  268. }
  269. function initTextures(imgInfo, imageParams, whileInit) {
  270. // set initial array to index
  271. if(textures[imageParams.textureIndex] == undefined
  272. || textures[imageParams.textureIndex] == null) {
  273. textures[imageParams.textureIndex] = [];
  274. }
  275. // using texture
  276. texture = initTexture(imageParams.texture);
  277. textures[imageParams.textureIndex].push(texture);
  278. // fetch new sprites of same time frame
  279. if(imageParams.spriteIndex < imgInfo.numberOfSprites - 1) {
  280. nextSpriteIndex = imageParams.spriteIndex + 1;
  281. nextSpriteId = textureInfo.sprites[imageParams.textureIndex][nextSpriteIndex].$oid
  282. helper.downloadImage(spriteUrl + nextSpriteId, imageParams.textureIndex, nextSpriteIndex, whileInit);
  283. return;
  284. }
  285. // inform the rest, that the first frame is loaded completely
  286. if(whileInit && imageParams.textureIndex == 0) {
  287. $(document).trigger('firstFrameSpritesCompleted');
  288. }
  289. // todo some logic
  290. if(!imgInfo.multipleFrames) {
  291. currentTexture = 0;
  292. return;
  293. } else {
  294. var numberOfFrames = Object.keys(textureInfo.sprites).length,
  295. nextIndex = null,
  296. nextFrameName = null,
  297. nextFramePath = null;
  298. framesDownloaded++;
  299. if(framesDownloaded == 1) {
  300. currentTexture = 0;
  301. }
  302. currentMaxFrame = imgInfo.frameFrom + framesDownloaded - 1;
  303. if(framesDownloaded > 1) {
  304. // updating frame control maximums
  305. updateFrameControlsMaxFrame();
  306. }
  307. if(numberOfFrames == framesDownloaded) {
  308. return;
  309. } else {
  310. nextIndex = framesDownloaded;
  311. nextFrameName = textureInfo.sprites[nextIndex][0].$oid;
  312. var framePath = spriteUrl + nextFrameName;
  313. helper.downloadImage(framePath, nextIndex, false);
  314. }
  315. }
  316. }
  317. function initTexture(texture) {
  318. texture.magFilter = THREE.LinearFilter;
  319. texture.minFilter = THREE.LinearFilter;
  320. texture.needsUpdate = true;
  321. return texture;
  322. }
  323. function getCurrentTexture() {
  324. return textures[currentTexture];
  325. }
  326. function useFrameControls() {
  327. return (imgInfo.multipleFrames && (imgInfo.frameTo - imgInfo.frameFrom) != 0);
  328. }
  329. function initFrameControls() {
  330. if(!useFrameControls()) {
  331. return;
  332. }
  333. $('#frame-controls').removeClass('hidden');
  334. var $frameSlider = $('#frame-slider'),
  335. $playButton = $('#frame-play'),
  336. $pauseButton = $('#frame-pause'),
  337. $currentFrameInput = $('#currentFrame'),
  338. playId = $playButton.attr('id'),
  339. pauseId = $pauseButton.attr('id'),
  340. minFrame = imgInfo.frameFrom;
  341. $currentFrameInput.val(minFrame);
  342. var play = function() {
  343. if(Object.keys(textures).length == 1) {
  344. console.warn('only one texture loaded, can not play yet');
  345. return;
  346. }
  347. $playButton.addClass('hidden');
  348. $pauseButton.removeClass('hidden');
  349. playFrames = true;
  350. $frameSlider.slider({disabled: true});
  351. $currentFrameInput.prop('disabled', true);
  352. }
  353. var pause = function() {
  354. $playButton.removeClass('hidden');
  355. $pauseButton.addClass('hidden');
  356. playFrames = false;
  357. $frameSlider.slider({disabled: false});
  358. $currentFrameInput.prop('disabled', false);
  359. }
  360. $('.frame-button').click(function() {
  361. var $this = $(this),
  362. thisId = $this.attr('id');
  363. if(thisId == playId) {
  364. play();
  365. } else if(thisId == pauseId) {
  366. pause();
  367. }
  368. });
  369. $(document).on('zoomActionFinished', function(e, params) {
  370. zoomedIn = params.zoomAction == 'zoomIn';
  371. if(zoomedIn) {
  372. pause();
  373. currentTexture = minFrame + params.textureNumber;
  374. updateFrameControls();
  375. $frameSlider.slider({disabled: true});
  376. $currentFrameInput.prop('disabled', true);
  377. $playButton.prop('disabled', true);
  378. $pauseButton.prop('disabled', true);
  379. } else {
  380. $frameSlider.slider({disabled: false});
  381. $currentFrameInput.prop('disabled', false);
  382. $playButton.prop('disabled', false);
  383. $pauseButton.prop('disabled', false);
  384. }
  385. });
  386. $frameSlider.slider(
  387. {
  388. min: minFrame,
  389. max: currentMaxFrame,
  390. slide: function( event, ui ) {
  391. currentTexture = ui.value - minFrame;
  392. updateTexture();
  393. $currentFrameInput.val(ui.value);
  394. }
  395. }
  396. );
  397. $currentFrameInput.change(function() {
  398. var value = parseInt($(this).val());
  399. if(value >= imgInfo.frameFrom && currentMaxFrame >= value) {
  400. currentTexture = value - minFrame;
  401. updateTexture();
  402. $frameSlider.slider('value', value);
  403. } else {
  404. $(this).val($frameSlider.slider('value'));
  405. }
  406. });
  407. }
  408. function updateFrameControlsMaxFrame() {
  409. if(!useFrameControls()) {
  410. return;
  411. }
  412. var $frameSlider = $('#frame-slider');
  413. $frameSlider.slider('option', 'max', currentMaxFrame);
  414. }
  415. function updateFrameControls() {
  416. if(!useFrameControls()) {
  417. return;
  418. }
  419. var $frameSlider = $('#frame-slider'),
  420. $currentFrameInput = $('#currentFrame'),
  421. minFrame = imgInfo.frameFrom,
  422. value = currentTexture + minFrame;
  423. $frameSlider.slider('value', value);
  424. $currentFrameInput.val(value);
  425. }
  426. function init() {
  427. rendererSizeHolder = calculateWidthHeight();
  428. width = rendererSizeHolder.width.px;
  429. height = rendererSizeHolder.height.px;
  430. //width = window.innerWidth;
  431. //height = window.innerHeight;
  432. camera = new THREE.PerspectiveCamera( 45, width / height, .1, 1000 );
  433. camera.position.z = 2;
  434. cameraRTT = new THREE.PerspectiveCamera( 45, width / height, .1, 1000 );
  435. cameraRTT.position.z = 2;
  436. scene = new THREE.Scene();
  437. sceneRTT = new THREE.Scene();
  438. rtTexture = new THREE.WebGLRenderTarget( width, height, { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat } );
  439. var geometry = geometryHelper.createBoxGeometry(originalDimension, currentVolumeDimension);
  440. var attributes = {
  441. frontColor: {type: 'c', value: [] }
  442. }
  443. material = new THREE.ShaderMaterial({
  444. attributes: attributes,
  445. vertexShader: vertexShader,
  446. fragmentShader: fragmentSimleShader,
  447. side: THREE.FrontSide
  448. });
  449. var spriteMaps = getCurrentTexture();
  450. var transferImageData = transferHelper.getCurrentTransferFunction();
  451. var transferTexture = new THREE.Texture(transferImageData);
  452. transferTexture.needsUpdate = true;
  453. var uniforms = {
  454. tBackground: {type: 't', value: rtTexture},
  455. tvSlices: {type: 'tv', value: spriteMaps},
  456. tTransfer: { type: 't', value: transferTexture},
  457. steps: {type: 'f', value: 100},
  458. numberOfSlices: {type: 'f', value: imgInfo.numberOfSlices },
  459. numberOfSprites: {type: 'f', value: imgInfo.numberOfSprites },
  460. slicesPerSprite: {type: 'f', value: imgInfo.slicesPerSprite },
  461. slicesOverX: {type: 'f', value: imgInfo.slicesOverX },
  462. slicesOverY: {type: 'f', value: imgInfo.slicesOverY },
  463. };
  464. materialScreen = new THREE.ShaderMaterial({
  465. attributes: attributes,
  466. uniforms: uniforms,
  467. vertexShader: vertexShader,
  468. fragmentShader: fragmentShader,
  469. side: THREE.BackSide,
  470. transparent: true
  471. });
  472. backgroundBox = new THREE.Mesh(geometry, material);
  473. sceneRTT.add(backgroundBox);
  474. frontBox = new THREE.Mesh(geometry, materialScreen);//
  475. scene.add(frontBox);
  476. objectsToIntersect.push(frontBox);
  477. translateBoxes(-0.5, -0.5, -0.5 * currentVolumeDimension.getZStretchFactor());
  478. rotateBoxesX(Math.PI / -2);
  479. //rotateBoxesY(Math.PI);
  480. //rotateBoxesZ(Math.PI);
  481. renderer = new THREE.WebGLRenderer();
  482. renderer.setSize(width, height);
  483. renderer.domElement.style.width = rendererSizeHolder.width.percent + '%';
  484. renderer.domElement.style.height = rendererSizeHolder.height.percent + '%';
  485. renderer.setClearColor(0xffffff, 1);
  486. container.appendChild(renderer.domElement);
  487. $canvas = $('canvas', $container);
  488. controls1 = new THREE.OrbitControls( camera, renderer.domElement );
  489. controls1.damping = 0.2;
  490. controls2 = new THREE.OrbitControls( cameraRTT, renderer.domElement );
  491. controls2.damping = 0.2;
  492. rendererContext = renderer.context;
  493. raycaster = new THREE.Raycaster();
  494. $(document).trigger('initFinished');
  495. }
  496. function rotateBoxesX(radian) {
  497. rotationMatrix = new THREE.Matrix4().makeRotationX(radian);
  498. rotateBoxes(rotationMatrix);
  499. }
  500. function rotateBoxesY(radian) {
  501. rotationMatrix = new THREE.Matrix4().makeRotationY(radian);
  502. rotateBoxes(rotationMatrix);
  503. }
  504. function rotateBoxesZ(radian) {
  505. rotationMatrix = new THREE.Matrix4().makeRotationZ(radian);
  506. rotateBoxes(rotationMatrix);
  507. }
  508. function rotateBoxes(matrix) {
  509. backgroundBox.applyMatrix(rotationMatrix);
  510. frontBox.applyMatrix(rotationMatrix);
  511. }
  512. function translateBoxes(x, y, z) {
  513. translationMatrix = new THREE.Matrix4().makeTranslation(x, y, z);
  514. backgroundBox.applyMatrix(translationMatrix);
  515. frontBox.applyMatrix(translationMatrix);
  516. }
  517. function initSliders() {
  518. initLayerSliders();
  519. // init gray value slider
  520. GenericSlider.initSlider(
  521. slider = $( "#gray-slider" ),
  522. sliderInputs = $('input.gray'),
  523. minGray,
  524. maxGray,
  525. minGray,
  526. maxGray,
  527. function(values) {
  528. $(document).trigger(
  529. 'opacityBordersChanged',
  530. {
  531. minGray: values[0],
  532. maxGray : values[1]
  533. }
  534. );
  535. }
  536. );
  537. $('#gray-slider-container').on('controlsShowed', function() {
  538. var values = $("#gray-slider").slider('values');
  539. $(document).trigger('opacityBordersChanged', { minGray: values[0], maxGray: values[1] });
  540. });
  541. GenericSlider.initSimpleSlider(
  542. slider = $('#background-slider'),
  543. sliderInput = $('#background-input'),
  544. startVal = 255,
  545. 0,
  546. 255,
  547. function(value) {
  548. var colorFloat = calculateGrayFloat(value);
  549. renderer.setClearColor(
  550. new THREE.Color(colorFloat, colorFloat, colorFloat),
  551. 1
  552. );
  553. }
  554. );
  555. }
  556. function initLayerSliders() {
  557. // init xlayer slider
  558. GenericSlider.initSlider(
  559. $( "#xlayer-slider" ),
  560. sliderInputs = $('input.xlayer'),
  561. currentVolumeDimension.xmin,
  562. currentVolumeDimension.xmax,
  563. currentVolumeDimension.xmin,
  564. currentVolumeDimension.xmax,
  565. function(values) {
  566. dimMin = currentVolumeDimension.xmin;
  567. dimMax = currentVolumeDimension.xmax;
  568. currentDimension.xmin = calculateLayerFloat(values[0], dimMin, dimMax);
  569. currentDimension.xmax = calculateLayerFloat(values[1], dimMin, dimMax);
  570. updateCubeLayers(currentDimension);
  571. },
  572. function() {
  573. values = Array(2);
  574. values[0] = calculateLayerInt(currentDimension.xmin, currentVolumeDimension.xmin, currentVolumeDimension.xmax );
  575. values[1] = calculateLayerInt(currentDimension.xmax, currentVolumeDimension.xmin, currentVolumeDimension.xmax );
  576. return values;
  577. }
  578. );
  579. // init ylayer slider
  580. GenericSlider.initSlider(
  581. $( "#ylayer-slider" ),
  582. sliderInputs = $('input.ylayer'),
  583. currentVolumeDimension.ymin,
  584. currentVolumeDimension.ymax,
  585. currentVolumeDimension.ymin,
  586. currentVolumeDimension.ymax,
  587. function(values) {
  588. dimMin = currentVolumeDimension.ymin;
  589. dimMax = currentVolumeDimension.ymax;
  590. currentDimension.ymin = calculateLayerFloat(values[0], dimMin, dimMax);
  591. currentDimension.ymax = calculateLayerFloat(values[1], dimMin, dimMax);
  592. updateCubeLayers(currentDimension);
  593. },
  594. function() {
  595. values = Array(2);
  596. values[0] = calculateLayerInt(currentDimension.ymin, currentVolumeDimension.ymin, currentVolumeDimension.ymax );
  597. values[1] = calculateLayerInt(currentDimension.ymax, currentVolumeDimension.ymin, currentVolumeDimension.ymax );
  598. return values;
  599. }
  600. );
  601. // init zlayer slider
  602. GenericSlider.initSlider(
  603. $( "#zlayer-slider" ),
  604. sliderInputs = $('input.zlayer'),
  605. currentVolumeDimension.zmin,
  606. currentVolumeDimension.zmax,
  607. currentVolumeDimension.zmin,
  608. currentVolumeDimension.zmax,
  609. function(values) {
  610. dimMin = currentVolumeDimension.zmin;
  611. dimMax = currentVolumeDimension.zmax;
  612. currentDimension.zmin = calculateLayerFloat(values[0], dimMin, dimMax),
  613. currentDimension.zmax = calculateLayerFloat(values[1], dimMin, dimMax);
  614. updateCubeLayers(currentDimension);
  615. },
  616. function() {
  617. values = Array(2);
  618. values[0] = calculateLayerInt(currentDimension.zmin, currentVolumeDimension.zmin, currentVolumeDimension.zmax );
  619. values[1] = calculateLayerInt(currentDimension.zmax, currentVolumeDimension.zmin, currentVolumeDimension.zmax );
  620. return values;
  621. }
  622. );
  623. }
  624. function repositionLayerSliders() {
  625. initLayerSliders();
  626. $.each($('.slider.layer'), function(index, value) {
  627. $(value).trigger('reposition');
  628. });
  629. updateCubeLayers(currentDimension);
  630. }
  631. var prevTime = Date.now();
  632. var fps = 0;
  633. var frames = 0;
  634. function animate() {
  635. requestAnimationFrame( animate );
  636. $container.trigger('animation');
  637. var numberOfTextures = Object.keys(textures).length;
  638. var now = Date.now();
  639. frames++;
  640. if(now > prevTime + 1000) {
  641. fps = (frames * 1000) / (now - prevTime);
  642. if(adjustSteps) {
  643. if(fps < 10) {
  644. stepHelper.decreaseSteps();
  645. } else if(fps > 15) {
  646. stepHelper.increaseSteps();
  647. }
  648. }
  649. prevTime = now;
  650. frames = 0;
  651. }
  652. if(playFrames && numberOfTextures > 1) {
  653. var timeStamp = (new Date()).getTime();
  654. if(timeStamp - textureTimestamp > 400) {
  655. currentTexture++;
  656. if(currentTexture >= numberOfTextures) {
  657. currentTexture = 0;
  658. }
  659. updateTexture();
  660. textureTimestamp = timeStamp;
  661. updateFrameControls();
  662. }
  663. }
  664. if(nextCubeDeletionTimeStamp == null && intersectCubesTimeStamps.length != 0) {
  665. nextCubeDeletionTimeStamp = intersectCubesTimeStamps.shift();
  666. }
  667. if((Date.now() - nextCubeDeletionTimeStamp) > 1500) {
  668. cubeToDelete = intersectCubes.shift();
  669. scene.remove(cubeToDelete);
  670. nextCubeDeletionTimeStamp = null;
  671. }
  672. if(transferHelper.transferFunctionChanged()) {
  673. var transfer = new THREE.Texture(transferHelper.getCurrentTransferFunction());
  674. transfer.needsUpdate = true;
  675. materialScreen.uniforms.tTransfer.value = transfer;
  676. }
  677. cameraPoint = camera.position.clone();
  678. overviewHelper.updateCameraPosition(cameraPoint);
  679. statsHelper.update();
  680. controls1.update();
  681. controls2.update();
  682. render();
  683. if(saveImage) {
  684. $('#save-canvas').trigger('click', { source: 'animate-function', imageUrl: renderer.domElement.toDataURL() });
  685. saveImage = false;
  686. }
  687. }
  688. function updateTexture(textures) {
  689. if(textures == undefined || textures == null) {
  690. textures = getCurrentTexture();
  691. }
  692. $.each(textures, function(index, texture) {
  693. value.needsUpdate = true;
  694. });
  695. materialScreen.uniforms.tvSlices.value = textures;
  696. }
  697. function render() {
  698. renderer.clear();
  699. renderer.context.clearDepth(-50.0);
  700. renderer.context.depthFunc(renderer.context.GEQUAL);
  701. renderer.render(sceneRTT, cameraRTT, rtTexture, true);
  702. renderer.context.clearDepth(50.0);
  703. renderer.context.depthFunc(renderer.context.LEQUAL);
  704. renderer.render(scene, camera);
  705. overviewHelper.render();
  706. }
  707. function updateCubeLayers(geometryDimension) {
  708. var geometry = geometryHelper.createBoxGeometry(geometryDimension, currentVolumeDimension);
  709. var colorArray = geometry.attributes.frontColor.array,
  710. positionArray = geometry.attributes.position.array;
  711. frontBox.geometry.attributes.frontColor.array = colorArray;
  712. frontBox.geometry.attributes.frontColor.needsUpdate = true;
  713. frontBox.geometry.attributes.position.array = positionArray;
  714. frontBox.geometry.attributes.position.needsUpdate = true;
  715. }
  716. // invert function of calculateLayerFloat
  717. function calculateLayerInt(position, dimMin, dimMax) {
  718. if(position <= 0.005) {
  719. return dimMin;
  720. }
  721. if(position >= 0.995) {
  722. return dimMax;
  723. }
  724. value = Math.round(position * (dimMax - dimMin) + dimMin);
  725. return value;
  726. }
  727. // calculate the float value for a layer input integer
  728. // 150 of [0, 299] will be 0.5
  729. function calculateLayerFloat(value, dimMin, dimMax) {
  730. var floatVal = (value - dimMin) / (dimMax - dimMin);
  731. if (floatVal == 0) {
  732. return 0.005;
  733. }
  734. if (floatVal == 1) {
  735. return 0.995;
  736. }
  737. return floatVal;
  738. }
  739. function calculateGrayFloat(value) {
  740. return value / 255.0;
  741. }
  742. function distance (v1, v2) {
  743. dx = v1.x - v2.x;
  744. dy = v1.y - v2.y;
  745. dz = v1.z - v2.z;
  746. return Math.sqrt(dx*dx+dy*dy+dz*dz);
  747. }
  748. function calculateWidthHeight() {
  749. downScaleFactor = 0.66;
  750. valueholder = {
  751. height: {},
  752. width: {}
  753. }
  754. containerHeight = $container.height();
  755. volumeInfoHeight = $('#volume-information').height();
  756. headerHeight = $('header').height();
  757. valueholder.height.px = (containerHeight - volumeInfoHeight) * downScaleFactor;
  758. volumeInfoHeightPercent = Math.floor(volumeInfoHeight / containerHeight * 100);
  759. valueholder.height.percent = 100 - volumeInfoHeightPercent;
  760. valueholder.width.px = $container.width() * downScaleFactor;
  761. valueholder.width.percent = 100;
  762. return valueholder;
  763. }
  764. function hideBottomControlsContainer() {
  765. var bottomControlsContainer = $('div#bottom-controls-container'),
  766. bottomControls = $('div#bottom-controls'),
  767. filledById = bottomControls.attr('filledBy'),
  768. filledBy = $('#' + filledById);
  769. // writing data back to original, because append MOVES the children
  770. if(filledBy.length == 1) {
  771. filledBy = $(filledBy[0]);
  772. filledByControlHolder = filledBy.children('.control-holder')[0];
  773. $(filledByControlHolder).append(bottomControls.children());
  774. }
  775. bottomControls.attr('filledBy', '');
  776. bottomControlsContainer.addClass('hidden');
  777. return filledById;
  778. }
  779. function addResolutionLinks() {
  780. $resSpan.text(config.defaultSize);
  781. textureSizes = config.textureSizes;
  782. textureSizes.forEach(function(size) {
  783. if(size > maximumTextureSize) {
  784. return;
  785. }
  786. href = size == config.defaultSize ? '' : 'href="#"';
  787. $resolutionLinks.append('<a '+href+' class="res-link" data="'+size+'">'+size+'</a> ');
  788. });
  789. }
  790. ;var volumeId;
  791. var stats, container, $container, containerHeader, $containerHeader, $canvas, cameraRTT, camera, sceneRTT, scene, renderer, controls, controls2;
  792. var maximumTextureSize;
  793. var containerOverview, $containerOverview;
  794. var $resSpan;
  795. var $resolutionLinks;
  796. var raycaster;
  797. var width, height;
  798. var steps, adjustSteps;
  799. var rtTexture, material;
  800. var backgroundBox, frontBox;
  801. var objectsToIntersect = [];
  802. var minGray = 5, maxGray = 255;
  803. var originalDimension,
  804. zoomedDimension,
  805. currentDimension,
  806. originalVolumeDimension,
  807. zoomedVolumeDimension,
  808. currentVolumeDimension,
  809. volumeUrl,
  810. infoUrl,
  811. spriteUrl,
  812. imgInfo = null,
  813. textureInfo = null,
  814. originalSizeArray = Array(3),
  815. zoomedInfo,
  816. rotationMatrix;
  817. var textures = {},
  818. currentTexture = 0,
  819. useDownloadedTextureImmediately = false,
  820. textureTimestamp = (new Date()).getTime(),
  821. playFrames = false,
  822. currentMaxFrame = 0;
  823. var framesDownloaded = 0,
  824. cachedImages = {};
  825. var vertexShader = null,
  826. fragmentSimleShader = null,
  827. fragmentShader = null;
  828. var intersectCubes = [],
  829. intersectCubesTimeStamps = []
  830. nextCubeDeletionTimeStamp = null,
  831. zoomedIn = false;
  832. var helper,
  833. geometryHelper,
  834. stepHelper,
  835. overviewHelper,
  836. transferHelper,
  837. zoomHelper,
  838. opacityPlotter,
  839. statsHelper;
  840. var waitDiv;
  841. var saveImage = false;
  842. // prefetch first image to cache it
  843. var progressContainer,
  844. imageProgress,
  845. progressSpan;
  846. var Globals = (function() {
  847. return {
  848. init: function() {
  849. originalDimension = new GeometryDimension();
  850. zoomedDimension = new GeometryDimension();
  851. currentDimension = originalDimension;
  852. originalVolumeDimension = new VolumeDimension();
  853. zoomedVolumeDimension = new VolumeDimension();
  854. currentVolumeDimension = originalVolumeDimension;
  855. $resolutionLinks = $('#resolution-links');
  856. }
  857. }
  858. }());
  859. ;var GeometryHelper = function() {
  860. return {
  861. createBoxGeometry: function(dimension, volumeDimension) {
  862. var vertexPos = [
  863. //front face first
  864. [dimension.xmin, dimension.ymin, dimension.zmax],
  865. [dimension.xmax, dimension.ymin, dimension.zmax],
  866. [dimension.xmax, dimension.ymax, dimension.zmax],
  867. //front face second
  868. [dimension.xmin, dimension.ymin, dimension.zmax],
  869. [dimension.xmax, dimension.ymax, dimension.zmax],
  870. [dimension.xmin, dimension.ymax, dimension.zmax],
  871. // back face first
  872. [dimension.xmin, dimension.ymin, dimension.zmin],
  873. [dimension.xmin, dimension.ymax, dimension.zmin],
  874. [dimension.xmax, dimension.ymax, dimension.zmin],
  875. // back face second
  876. [dimension.xmin, dimension.ymin, dimension.zmin],
  877. [dimension.xmax, dimension.ymax, dimension.zmin],
  878. [dimension.xmax, dimension.ymin, dimension.zmin],
  879. // top face first
  880. [dimension.xmin, dimension.ymax, dimension.zmin],
  881. [dimension.xmin, dimension.ymax, dimension.zmax],
  882. [dimension.xmax, dimension.ymax, dimension.zmax],
  883. // top face second
  884. [dimension.xmin, dimension.ymax, dimension.zmin],
  885. [dimension.xmax, dimension.ymax, dimension.zmax],
  886. [dimension.xmax, dimension.ymax, dimension.zmin],
  887. // bottom face first
  888. [dimension.xmin, dimension.ymin, dimension.zmin],
  889. [dimension.xmax, dimension.ymin, dimension.zmin],
  890. [dimension.xmax, dimension.ymin, dimension.zmax],
  891. // bottom face second
  892. [dimension.xmin, dimension.ymin, dimension.zmin],
  893. [dimension.xmax, dimension.ymin, dimension.zmax],
  894. [dimension.xmin, dimension.ymin, dimension.zmax],
  895. // right face first
  896. [dimension.xmax, dimension.ymin, dimension.zmin],
  897. [dimension.xmax, dimension.ymax, dimension.zmin],
  898. [dimension.xmax, dimension.ymax, dimension.zmax],
  899. // right face second
  900. [dimension.xmax, dimension.ymin, dimension.zmin],
  901. [dimension.xmax, dimension.ymax, dimension.zmax],
  902. [dimension.xmax, dimension.ymin, dimension.zmax],
  903. // left face first
  904. [dimension.xmin, dimension.ymin, dimension.zmin],
  905. [dimension.xmin, dimension.ymin, dimension.zmax],
  906. [dimension.xmin, dimension.ymax, dimension.zmax],
  907. // left face second
  908. [dimension.xmin, dimension.ymin, dimension.zmin],
  909. [dimension.xmin, dimension.ymax, dimension.zmax],
  910. [dimension.xmin, dimension.ymax, dimension.zmin]
  911. ];
  912. var positions = [];
  913. var colors = [];
  914. for(var i = 0; i < vertexPos.length; i++) {
  915. var backCounter = vertexPos.length - 1 - i,
  916. x = vertexPos[backCounter][0],
  917. y = vertexPos[backCounter][1],
  918. z = vertexPos[backCounter][2];
  919. positions.push(x);
  920. positions.push(y);
  921. positions.push(z * volumeDimension.getZStretchFactor());
  922. colors.push(x);
  923. colors.push(y);
  924. colors.push(z);
  925. colors.push(1.0);
  926. }
  927. var geometry = new THREE.BufferGeometry();
  928. var bufferPositions = new Float32Array(positions);
  929. geometry.addAttribute( 'position', new THREE.BufferAttribute( bufferPositions, 3 ) );
  930. geometry.addAttribute( 'frontColor', new THREE.BufferAttribute(new Float32Array(colors), 4));
  931. geometry.computeBoundingSphere();
  932. return geometry;
  933. }
  934. }
  935. }
  936. var GeometryDimension = function() {
  937. this.xmin = 0.005;
  938. this.xmax = 0.995;
  939. this.ymin = 0.005;
  940. this.ymax = 0.995;
  941. this.zmin = 0.005;
  942. this.zmax = 0.995;
  943. }
  944. var VolumeDimension = function() {
  945. this.xmin;
  946. this.xmax;
  947. this.ymin;
  948. this.ymax;
  949. this.zmin;
  950. this.zmax;
  951. return {
  952. getZStretchFactor: function() {
  953. return (this.zmax - this.zmin + 1) / (this.xmax - this.xmin);
  954. }
  955. }
  956. }
  957. ;var Helper = function() {
  958. var messagePopup = $('#popup'),
  959. popupVisible = messagePopup.hasClass('hidden'),
  960. progressContainer = $('#progress-container'),
  961. imageProgress = $('#image-progress'),
  962. progressSpan = $('#texturePath');
  963. return {
  964. initializeShaders: function(url, callback) {
  965. $.ajax({
  966. url: url,
  967. error: function() {
  968. console.error('failed to load shader file from: ' + url);
  969. },
  970. success: callback
  971. });
  972. },
  973. downloadImage: function (texturePath, textureIndex, spriteIndex, whileInit) {
  974. me = this;
  975. onProgressHolder = me.showProgress(texturePath);
  976. if(!texturePath.endsWith('/')) {
  977. texturePath += '/';
  978. }
  979. var xmlHttp = new XMLHttpRequest();
  980. xmlHttp.open('GET', texturePath, true);
  981. xmlHttp.onprogress = onProgressHolder.onProgressFunction;
  982. xmlHttp.onload = function(e) {
  983. var blob = new Blob([xmlHttp.response], {
  984. type: this.getResponseHeader('content-type')
  985. });
  986. var blobSrc = window.URL.createObjectURL(blob);
  987. var image = new Image();
  988. var texture = new THREE.Texture(image);
  989. image.onload = function() {
  990. me.hideProgress();
  991. texture.needsUpdate = true;
  992. var params = {
  993. textureIndex: textureIndex,
  994. spriteIndex: spriteIndex,
  995. texture: texture
  996. };
  997. if(textureIndex == 0 && whileInit) {
  998. $(document).trigger('firstImageOnLoad', params);
  999. } else {
  1000. $(document).trigger('imageOnLoad', params);
  1001. }
  1002. }
  1003. image.src = blobSrc;
  1004. };
  1005. xmlHttp.responseType = 'blob';
  1006. xmlHttp.send();
  1007. },
  1008. showProgress: function(texturePath) {
  1009. progressContainer.removeClass('hidden');
  1010. progressSpan.text(texturePath);
  1011. return {
  1012. onProgressFunction: function(e) {
  1013. if (e.lengthComputable) {
  1014. value = Math.floor(e.loaded / e.total * 100);
  1015. imageProgress.val(value);
  1016. }
  1017. }
  1018. }
  1019. },
  1020. hideProgress: function() {
  1021. imageProgress.val(0);
  1022. progressSpan.text('');
  1023. progressContainer.addClass('hidden');
  1024. },
  1025. displayMessage: function(message, fadeOutAutomatically, callback, timeout) {
  1026. var me = this;
  1027. messagePopup.html(message);
  1028. messagePopup.show();
  1029. if(fadeOutAutomatically) {
  1030. this.fadeoutMessage(callback, timeout);
  1031. }
  1032. },
  1033. fadeoutMessage: function(callback, timeout) {
  1034. if(timeout == undefined) {
  1035. timeout = 0;
  1036. }
  1037. setTimeout(function() {
  1038. messagePopup.fadeOut(400, function() {
  1039. $(this).html('');
  1040. if(callback) {
  1041. callback();
  1042. }
  1043. });
  1044. }, timeout);
  1045. }
  1046. };
  1047. }
  1048. ;var OpacityPlotter = function() {
  1049. // we have 255 colors
  1050. var coordinateLength = 255;
  1051. // set with respect on plotter height
  1052. var height = 40;
  1053. // array for holding the x coordinates
  1054. var coordinates = Array(coordinateLength);
  1055. // array for holding the y coordinates values elementOf [0, height]
  1056. var linePosition = Array(coordinateLength);
  1057. // array holding the resulting opacities, that will be generated
  1058. // of line position, values elementof [0, 255]
  1059. var opacities = Array(coordinateLength);
  1060. for(counter = 0; counter < coordinateLength; counter++) {
  1061. coordinates[counter] = counter;
  1062. linePosition[counter] = 0;
  1063. opacities[counter] = 255;
  1064. }
  1065. var stage = new Kinetic.Stage({
  1066. container : 'opacity-plot',
  1067. width : coordinateLength,
  1068. height: height
  1069. });
  1070. var layer = new Kinetic.Layer();
  1071. var rect = new Kinetic.Rect({
  1072. x: 0,
  1073. y: 0,
  1074. width: stage.getWidth(),
  1075. height: stage.getHeight(),
  1076. fill: '#cccccc'
  1077. });
  1078. layer.add(rect);
  1079. var spline = new Kinetic.Line({
  1080. points: getResultingPoints(),
  1081. stroke: 'red',
  1082. strokeWidth: 3,
  1083. lineCap: 'round'
  1084. });
  1085. layer.add(spline);
  1086. stage.add(layer);
  1087. stage.on("mousedown", function() {
  1088. processEvent();
  1089. });
  1090. stage.on("mousemove", function(e) {
  1091. if(e.evt.which == 1) {
  1092. processEvent();
  1093. }
  1094. });
  1095. $('#transfer-function-definition').on('controlsShowed', function() {
  1096. populateOpacities();
  1097. });
  1098. $('input#opacity-visible').on('click', function() {
  1099. setToFixedValue(255);
  1100. });
  1101. $('input#opacity-invisible').on('click', function() {
  1102. setToFixedValue(0);
  1103. });
  1104. function setToFixedValue(value) {
  1105. var opacityValue = value;
  1106. var yValue = height - ((value / 255) * height)
  1107. for(counter = 0; counter < opacities.length; counter++) {
  1108. opacities[counter] = opacityValue ;
  1109. linePosition[counter] = yValue;
  1110. }
  1111. populateOpacities();
  1112. spline.setPoints(getResultingPoints());
  1113. layer.draw();
  1114. }
  1115. function populateOpacities() {
  1116. $(document).trigger('opacityChanged', { opacities: opacities.slice() });
  1117. }
  1118. function processEvent() {
  1119. var mousePosition = stage.getPointerPosition();
  1120. calculateOpacities(mousePosition);
  1121. spline.setPoints(getResultingPoints());
  1122. layer.draw();
  1123. }
  1124. function calculateOpacities(mousePosition) {
  1125. // Set area of click to this value
  1126. var xMin = (mousePosition.x - 2 >= 0) ? mousePosition.x - 2 : 0;
  1127. var xMax = (mousePosition.x + 2 <= 255) ? mousePosition.x + 2 : 255;
  1128. var opacityValue = (height - mousePosition.y) / height;
  1129. opacityValue *= 255;
  1130. for(counter = xMin; counter <= xMax; counter++) {
  1131. linePosition[counter] = mousePosition.y;
  1132. opacities[counter] = opacityValue;
  1133. }
  1134. populateOpacities();
  1135. }
  1136. function getResultingPoints() {
  1137. return combineArrays(coordinates, linePosition);
  1138. }
  1139. function combineArrays(array1, array2) {
  1140. return $.map(array1, function(v, i) { return [v, array2[i]]; });
  1141. }
  1142. }
  1143. ;var StatsHelper = function(parentElement) {
  1144. if(IPE.environment != 'dev') {
  1145. // fill parentElement with emptyness
  1146. parentElement.append('&nbsp;');
  1147. // we don't want to do display anything
  1148. // in production
  1149. return {
  1150. update: function() {}
  1151. };
  1152. }
  1153. var stats = new Stats();
  1154. var parentElement = parentElement;
  1155. parentElement.append(stats.domElement);
  1156. return {
  1157. update: function() {
  1158. stats.update();
  1159. }
  1160. }
  1161. };
  1162. ;var StepHelper = function() {
  1163. // default is the step 100
  1164. currentStepIndex = 2;
  1165. // steps just defined
  1166. availableSteps = [10, 20, 50, 80, 100, 150, 200];
  1167. return {
  1168. decreaseSteps: function() {
  1169. var currentStepIndex = this.determineCurrentStepIndex();
  1170. if(currentStepIndex > 0) {
  1171. this.updateStepValue(availableSteps[currentStepIndex - 1], true);
  1172. }
  1173. },
  1174. increaseSteps: function() {
  1175. var currentStepIndex = this.determineCurrentStepIndex();
  1176. if(currentStepIndex > -1 && currentStepIndex < availableSteps.length - 1) {
  1177. this.updateStepValue(availableSteps[currentStepIndex + 1], true);
  1178. }
  1179. },
  1180. determineCurrentStepIndex: function() {
  1181. var minDeviation = null,
  1182. minDeviationIndex;
  1183. // first we have to find an appropiate predefined stepValue for input stepvalue
  1184. for(var index = 0; index < availableSteps.length; index++) {
  1185. deviation = Math.abs(steps - availableSteps[index]);
  1186. if(minDeviation == null || deviation < minDeviation) {
  1187. minDeviation = deviation;
  1188. minDeviationIndex = index;
  1189. }
  1190. }
  1191. if(minDeviation !== null) {
  1192. return minDeviationIndex;
  1193. } else {
  1194. return -1;
  1195. }
  1196. },
  1197. updateStepValue: function(stepValue, updateInputField) {
  1198. steps = stepValue;
  1199. materialScreen.uniforms.steps.value = steps;
  1200. if(updateInputField) {
  1201. $('#steps').val(stepValue);
  1202. }
  1203. },
  1204. initStepControls: function() {
  1205. var me = this,
  1206. stepsInput = $('#steps'),
  1207. stepsAdjustInput = $('#steps-adjust');
  1208. steps = stepsInput.val();
  1209. stepsInput.change(function() {
  1210. steps = stepsInput.val();
  1211. me.updateStepValue(steps, false);
  1212. });
  1213. adjustSteps = stepsAdjustInput.is(':checked');
  1214. stepsAdjustInput.change(function() {
  1215. adjustSteps = $(this).is(':checked');
  1216. if(adjustSteps) {
  1217. stepsInput.prop('disabled', true);
  1218. } else {
  1219. stepsInput.prop('disabled', false);
  1220. }
  1221. });
  1222. }
  1223. };
  1224. };
  1225. ;var TransferHelper = function() {
  1226. var me = this,
  1227. changed = false,
  1228. canvas = document.getElementById('transfer-function'),
  1229. $canvas = $(canvas),
  1230. context = canvas.getContext('2d'),
  1231. hiddenCanvas = document.getElementById('hidden-transfer'),
  1232. hiddenContext = hiddenCanvas.getContext('2d'),
  1233. transferSpanHolder = $('#transfer-span-holder'),
  1234. transfer = $('#transfer'),
  1235. disabled = true,
  1236. handles = $('span.transfer'),
  1237. $standardTf = $('img#standard-tf').get(0),
  1238. offsetLeft = {
  1239. blue: 0,
  1240. turquois: 0.25,
  1241. green: 0.5,
  1242. orange: 0.75,
  1243. red: 1
  1244. },
  1245. colorCodes = {
  1246. blue: '#0000FF',
  1247. turquois: '#00FFFF',
  1248. green: '#00FF00',
  1249. orange: '#FFFF00',
  1250. red: '#FF0000'
  1251. },
  1252. opacities = Array(255);
  1253. //black values are disabled defaultly
  1254. opacities[0] = 0;
  1255. for(counter = 0; counter < opacities.length; counter++) {
  1256. opacities[counter] = 255;
  1257. if (counter <= minGray) {
  1258. opacities[counter] = 0;
  1259. }
  1260. }
  1261. $('input#toggle-transfer').on('click', function() {
  1262. if(disabled) {
  1263. enableTransferFunction();
  1264. } else {
  1265. disableTransferFunction();
  1266. }
  1267. });
  1268. var getOffsetLeft = function() {
  1269. return transfer.offset().left - transfer.parent().offset().left;
  1270. }
  1271. var removePxOffString = function(str) {
  1272. return parseInt(str.replace('px', ''));
  1273. }
  1274. var updateGradient = function() {
  1275. // Create gradient
  1276. var grd = context.createLinearGradient(0,0,255,0);
  1277. $.each(handles, function(index, value) {
  1278. var $value = $(value),
  1279. colorName = $value.attr('color'),
  1280. colorOffsetLeft = offsetLeft[colorName],
  1281. colorCode = colorCodes[colorName];
  1282. grd.addColorStop(colorOffsetLeft, colorCode);
  1283. });
  1284. // Fill with gradient
  1285. context.fillStyle = grd;
  1286. context.fillRect(0,0,255, 10);
  1287. applyOpacities();
  1288. changed = true;
  1289. };
  1290. var applyOpacities = function() {
  1291. var imageData = context.getImageData(0, 0, 255, 1),
  1292. data = imageData.data;
  1293. for(counter = 0; counter < opacities.length; counter++) {
  1294. data[(counter * 4) + 3] = opacities[counter];
  1295. }
  1296. hiddenContext.putImageData(imageData, 0, 0, 0, 0, 255, 1);
  1297. }
  1298. var updateHandlePosition = function(element) {
  1299. var colorLeft = removePxOffString($(element).css('left')) - getOffsetLeft(),
  1300. colorOffsetLeft = colorLeft / $canvas.width(),
  1301. colorName = $(element).attr('color');
  1302. if(colorOffsetLeft > 1.0) {
  1303. colorOffsetLeft = 1.0;
  1304. }
  1305. if(colorOffsetLeft < 0.0) {
  1306. colorOffsetLeft = 0.0;
  1307. }
  1308. offsetLeft[colorName] = colorOffsetLeft;
  1309. updateGradient();
  1310. };
  1311. var enableTransferFunction = function() {
  1312. $.each(handles, function(index, value) {
  1313. var $value = $(value),
  1314. colorName = $value.attr('color'),
  1315. colorOffsetLeft = offsetLeft[colorName],
  1316. colorLeft = getOffsetLeft() + ($canvas.width() * colorOffsetLeft),
  1317. colorCode = colorCodes[colorName];
  1318. $value.css({left: colorLeft, 'background-color': colorCode });
  1319. $(value).draggable({
  1320. containment: '#transfer-span-holder',
  1321. scroll: false,
  1322. axis: 'x',
  1323. cursor: "crosshair",
  1324. stop: function() {
  1325. updateHandlePosition(this);
  1326. },
  1327. drag: function() {
  1328. updateHandlePosition(this);
  1329. }
  1330. });
  1331. });
  1332. showHandles();
  1333. updateGradient();
  1334. disabled = false;
  1335. };
  1336. var disableTransferFunction = function() {
  1337. context.drawImage($standardTf, 0, 0, 255, 10);
  1338. applyOpacities();
  1339. hideHandles();
  1340. disabled = true;
  1341. changed = true;
  1342. };
  1343. var showHandles = function() {
  1344. $.each(handles, function(index, value) {
  1345. $(value).removeClass('hidden');
  1346. });
  1347. };
  1348. var hideHandles = function() {
  1349. $.each(handles, function(index, value) {
  1350. $(value).addClass('hidden');
  1351. });
  1352. };
  1353. disableTransferFunction();
  1354. $(document).on('opacityChanged', function(e, params) {
  1355. if(params != undefined && params.opacities != undefined) {
  1356. opacities = params.opacities;
  1357. applyOpacities();
  1358. changed = true;
  1359. }
  1360. });
  1361. $(document).on('opacityBordersChanged', function(e, params) {
  1362. var minGray = 0,
  1363. maxGray = 255;
  1364. if(params != undefined && params.minGray != undefined && params.maxGray != undefined) {
  1365. minGray = params.minGray;
  1366. maxGray = params.maxGray;
  1367. for(counter = 0; counter < 255; counter++) {
  1368. var opacity = 255;
  1369. if(counter < minGray || counter > maxGray) {
  1370. opacity = 1;
  1371. }
  1372. opacities[counter] = opacity;
  1373. }
  1374. applyOpacities();
  1375. changed = true;
  1376. }
  1377. });
  1378. return {
  1379. transferFunctionChanged : function() {
  1380. return changed;
  1381. },
  1382. getCurrentTransferFunction : function() {
  1383. changed = false;
  1384. return hiddenContext.getImageData(0, 0, 255, 1);
  1385. }
  1386. };
  1387. }
  1388. ;var OverviewHelper = function() {
  1389. var sceneOveriew, rendererOverview, cameraOverview, innerCubeOverview;
  1390. var originalZStretchFactor;
  1391. //private method
  1392. var addInnerCubeOverView = function(x, y, z) {
  1393. var innerCubeGeometry = new THREE.BoxGeometry( x, y, z );
  1394. var innerCubeMaterial = new THREE.MeshBasicMaterial( {color: 0xaaaaaa} );
  1395. innerCubeOverview = new THREE.Mesh( innerCubeGeometry, innerCubeMaterial );
  1396. sceneOverview.add( innerCubeOverview );
  1397. }
  1398. var addInnerFullCube = function() {
  1399. addInnerCubeOverView(1, 1 * originalZStretchFactor, 1);
  1400. };
  1401. var initiateEventListener = function(containerOverviewElement) {
  1402. // change overview box
  1403. $(document).on('zoomActionFinished', function(e, params) {
  1404. zoomedIn = params.zoomAction == 'zoomIn' || zoomedInfo != null;
  1405. sceneOverview.remove(innerCubeOverview);
  1406. if(zoomedIn) {
  1407. xWidth = zoomedInfo.dimension.xywidth / textureInfo.originalSize.sizex;
  1408. yWidth = zoomedInfo.dimension.xywidth / textureInfo.originalSize.sizey;
  1409. zWidth = zoomedInfo.numberOfSlices / textureInfo.originalSize.sizez;
  1410. xOffset = zoomedInfo.dimension.xmin / textureInfo.originalSize.sizex;
  1411. yOffset = zoomedInfo.dimension.ymin / textureInfo.originalSize.sizey;
  1412. zOffset = zoomedInfo.dimension.zmin / (textureInfo.originalSize.sizez * originalZStretchFactor);
  1413. addInnerCubeOverView(xWidth, yWidth, zWidth);
  1414. // main cube was rotated around x axis, so have to swap y and z coordinates
  1415. // for the overview
  1416. innerCubeOverview.position.x = -0.5 + xOffset + xWidth / 2;
  1417. innerCubeOverview.position.y = (-0.5 + zOffset + zWidth / 2);
  1418. innerCubeOverview.position.z = (-0.5 + yOffset + yWidth / 2) * -1;
  1419. } else {
  1420. addInnerFullCube();
  1421. }
  1422. });
  1423. var headerHeight = $('header').height();
  1424. $(window).on('scroll', function(e) {
  1425. $containerOverviewElement = $(containerOverviewElement)
  1426. if($(this).scrollTop() >= headerHeight) {
  1427. $containerOverviewElement.css('position', 'relative');
  1428. $containerOverviewElement.css('top', $(this).scrollTop() - headerHeight);
  1429. } else {
  1430. $containerOverviewElement.css('top', 0);
  1431. }
  1432. });
  1433. }
  1434. var addTextsToScene = function() {
  1435. var shapeParameters = {font: 'helvetiker', size: 0.2, height: 0.05},
  1436. shape = new THREE.TextGeometry("0", shapeParameters),
  1437. wrapper = new THREE.MeshBasicMaterial({color: 0x000000}),
  1438. words = new THREE.Mesh(shape, wrapper);
  1439. words.position.x = -0.6;
  1440. words.position.y = -0.6;
  1441. words.position.z = 0.52;
  1442. sceneOverview.add(words);
  1443. }
  1444. var addCubeEdges = function() {
  1445. // fix for cube
  1446. var numberOfEdges = 12;
  1447. var xColorEdge = new THREE.Color(1,0,0);
  1448. var yColorEdge = new THREE.Color(0,1,0);
  1449. var zColorEdge = new THREE.Color(0,0,1);
  1450. var geometry = new THREE.BufferGeometry();
  1451. var material = new THREE.LineBasicMaterial({ vertexColors: THREE.VertexColors });
  1452. var colors = new Float32Array( numberOfEdges * 2 * 3 );
  1453. var zMin = -0.5 * originalZStretchFactor;
  1454. var zMax = 0.5 * originalZStretchFactor;
  1455. var positions = new Float32Array([
  1456. // edges in x direction
  1457. // front face
  1458. -0.5, zMin, -0.5, 0.5, zMin, -0.5,
  1459. -0.5, zMax, -0.5, 0.5, zMax, -0.5,
  1460. // back face
  1461. -0.5, zMin, 0.5, 0.5, zMin, 0.5,
  1462. -0.5, zMax, 0.5, 0.5, zMax, 0.5,
  1463. // edges in y direction
  1464. // front face
  1465. -0.5, zMin, -0.5, -0.5, zMax, -0.5,
  1466. 0.5, zMin, -0.5, 0.5, zMax, -0.5,
  1467. // back face
  1468. -0.5, zMin, 0.5, -0.5, zMax, 0.5,
  1469. 0.5, zMin, 0.5, 0.5, zMax, 0.5,
  1470. // edges in z direction
  1471. // left side
  1472. -0.5, zMin, -0.5, -0.5, zMin, 0.5,
  1473. -0.5, zMax, -0.5, -0.5, zMax, 0.5,
  1474. // right side
  1475. 0.5, zMin, -0.5, 0.5, zMin, 0.5,
  1476. 0.5, zMax, -0.5, 0.5, zMax, 0.5
  1477. ]);
  1478. for(edgeCounter = 0; edgeCounter < 12 * 2; edgeCounter++) {
  1479. var colorToUse = null
  1480. if(edgeCounter <= 7) {
  1481. colorToUse = xColorEdge;
  1482. } else if(edgeCounter <= 15) {
  1483. colorToUse = zColorEdge;
  1484. } else {
  1485. colorToUse = yColorEdge;
  1486. }
  1487. colors[edgeCounter * 3 + 0] = colorToUse.r;
  1488. colors[edgeCounter * 3 + 1] = colorToUse.g;
  1489. colors[edgeCounter * 3 + 2] = colorToUse.b;
  1490. }
  1491. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  1492. geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  1493. var lines = new THREE.Line( geometry, material, THREE.LinePieces );
  1494. sceneOverview.add( lines );
  1495. }
  1496. return {
  1497. initOverview: function(containerOverviewElement) {
  1498. originalZStretchFactor = currentVolumeDimension.getZStretchFactor();
  1499. sceneOverview = new THREE.Scene();
  1500. cameraOverview = new THREE.PerspectiveCamera( 45, 150 / 150, .1, 1000 );
  1501. cameraOverview.position.z = 2;
  1502. var cubeGeometry = new THREE.BoxGeometry( 1, 1, 1 );
  1503. var cubeMaterial = new THREE.MeshBasicMaterial( {color: 0x000000} );
  1504. var cubeOverview = new THREE.Mesh( cubeGeometry, cubeMaterial );
  1505. var edgesHelper = new THREE.EdgesHelper(cubeOverview, 0x000000);
  1506. edgesHelper.material.linewidth = 2;
  1507. //sceneOverview.add( edgesHelper );
  1508. addInnerFullCube();
  1509. addCubeEdges();
  1510. addTextsToScene();
  1511. rendererOverview = new THREE.WebGLRenderer();
  1512. rendererOverview.setSize(200, 200);
  1513. rendererOverview.setClearColor(0xffffff, 1);
  1514. containerOverviewElement.appendChild(rendererOverview.domElement);
  1515. initiateEventListener(containerOverviewElement);
  1516. },
  1517. updateCameraPosition: function(position) {
  1518. cameraPoint.normalize().multiplyScalar(4);
  1519. cameraPoint = sceneOverview.position.clone().add(cameraPoint);
  1520. cameraOverview.position.x = cameraPoint.x;
  1521. cameraOverview.position.y = cameraPoint.y;
  1522. cameraOverview.position.z = cameraPoint.z;
  1523. cameraOverview.lookAt(sceneOverview.position);
  1524. },
  1525. render: function() {
  1526. rendererOverview.render(sceneOverview, cameraOverview);
  1527. }
  1528. }
  1529. }
  1530. ;var VolumeFace = function(orientation, direction) {
  1531. this.orientation = orientation;
  1532. this.direction = direction;
  1533. }
  1534. VolumeFace.prototype = {
  1535. constructor: VolumeFace,
  1536. orientation: 0,
  1537. direction: 0,
  1538. getAxisOfNaviation: function() {
  1539. var axis = {
  1540. plane: {
  1541. ver: null,
  1542. hor: null
  1543. },
  1544. slider: null
  1545. };
  1546. if(this.orientation == VolumeFaceOrientation.XY) {
  1547. axis.plane.ver = new Orientation(Axis.Y);
  1548. axis.plane.hor = new Orientation(Axis.X);
  1549. axis.slider = new Orientation(Axis.Z);
  1550. } else if(this.orientation == VolumeFaceOrientation.YZ) {
  1551. axis.plane.ver = new Orientation(Axis.Z);
  1552. axis.plane.hor = new Orientation(Axis.Y);
  1553. axis.slider = new Orientation(Axis.X);
  1554. } else if(this.orientation == VolumeFaceOrientation.XZ) {
  1555. axis.plane.ver = new Orientation(Axis.Z);
  1556. axis.plane.hor = new Orientation(Axis.X);
  1557. axis.slider = new Orientation(Axis.Y);
  1558. }
  1559. return axis;
  1560. },
  1561. getIdentifier: function() {
  1562. return this.orientation + '-' + this.direction;
  1563. }
  1564. };
  1565. var VolumeFaceOrientation = {
  1566. XY: 0,
  1567. XZ: 1,
  1568. YZ: 2
  1569. }
  1570. var VolumeFaceDirection = {
  1571. FRONT: 0,
  1572. BACK: 1
  1573. }
  1574. function determineVolumeFace(cubeFaceNormal) {
  1575. var orientation, direction;
  1576. if(cubeFaceNormal.x != 0) {
  1577. orientation = VolumeFaceOrientation.YZ;
  1578. direction = determineFaceOrientation(cubeFaceNormal.x);
  1579. } else if(cubeFaceNormal.y != 0) {
  1580. orientation = VolumeFaceOrientation.XZ;
  1581. direction = determineFaceOrientation(cubeFaceNormal.y);
  1582. } else if(cubeFaceNormal.z != 0) {
  1583. orientation = VolumeFaceOrientation.XY;
  1584. direction = determineFaceOrientation(cubeFaceNormal.z);
  1585. }
  1586. return new VolumeFace(orientation, direction);
  1587. }
  1588. function determineFaceOrientation(normalComp) {
  1589. return normalComp > 0 ? VolumeFaceDirection.FRONT : VolumeFaceDirection.BACK;
  1590. }
  1591. function isFrontFace(orientation, direction) {
  1592. return orientation == VolumeFaceOrientation.XY
  1593. && direction == VolumeFaceDirection.FRONT;
  1594. }
  1595. function isBackFace(orientation, direction) {
  1596. return orientation == VolumeFaceOrientation.XY
  1597. && direction == VolumeFaceDirection.BACK;
  1598. }
  1599. function isLeftFace(orientation, direction) {
  1600. return orientation == VolumeFaceOrientation.YZ
  1601. && direction == VolumeFaceDirection.FRONT;
  1602. }
  1603. function isRightFace(orientation, direction) {
  1604. return orientation == VolumeFaceOrientation.YZ
  1605. && direction == VolumeFaceDirection.BACK;
  1606. }
  1607. function isTopFace(orientation, direction) {
  1608. return orientation == VolumeFaceOrientation.XZ
  1609. && direction == VolumeFaceDirection.FRONT;
  1610. }
  1611. function isBottomFace(orientation, direction) {
  1612. return orientation == VolumeFaceOrientation.XZ
  1613. && direction == VolumeFaceDirection.BACK;
  1614. }
  1615. ;var ZoomHelper = function() {
  1616. var navigationPlaneHorLabel = $('#navigation-plane-hor-label'),
  1617. navigationPlaneVerLabel = $('#navigation-plane-ver-label'),
  1618. navigationSliderLabel = $('#navigation-slider-label'),
  1619. zoomStatus = $('#zoom-status'),
  1620. zoomInButton = $('input#zoomin'),
  1621. zoomOutButton = $('input#zoomout'),
  1622. navigationPlane = $('div#navigation-plane'),
  1623. navigationPlaneParent = navigationPlane.parent(),
  1624. navigationSlider = $('div#navigation-slider'),
  1625. navigationSliderParent = navigationSlider.parent(),
  1626. displayTimestamp = null,
  1627. planeOrientation = {
  1628. ver: new Orientation(Axis.Y, OffsetTo.LEFT),
  1629. hor: new Orientation(Axis.X, OffsetTo.TOP),
  1630. direction: VolumeFaceDirection.BACK,
  1631. orientation: VolumeFaceOrientation.XY
  1632. },
  1633. sliderOrientation = {axis: new Orientation(Axis.Z, OffsetTo.TOP) },
  1634. zoomedIn,
  1635. zoomedInfoArray = {
  1636. dimension: Array(3),
  1637. offset: Array(3)
  1638. },
  1639. oldZoomedInfos = [];
  1640. var lastExecutionTime = Date.now();
  1641. // adjust navigation related navigations
  1642. $container.on('animation', function() {
  1643. determineAxisLabels();
  1644. determinePlanePosition();
  1645. determineSliderPosition();
  1646. });
  1647. // one has to press ctrl AND alt while clicking
  1648. $container.on('click', 'canvas', function(event){
  1649. event.stopPropagation();
  1650. event.preventDefault();
  1651. // keyboard shortcut activated or button zoom in pressed before?
  1652. if((event.ctrlKey == false || event.altKey == false)
  1653. && !zoomInActivated) {
  1654. return;
  1655. }
  1656. var mouseX = ( (event.clientX - $canvas.offset().left) / ($canvas.width()) ) * 2 - 1,
  1657. mouseY = - ( (event.clientY - $canvas.offset().top) / ($canvas.height()) ) * 2 + 1;
  1658. var vector = new THREE.Vector3( mouseX, mouseY, camera.near);
  1659. vector = vector.unproject( camera );
  1660. var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
  1661. var intersects = raycaster.intersectObjects( [frontBox] );
  1662. if(intersects.length > 0) {
  1663. if(!handleZoomStatus(true)) {
  1664. return false;
  1665. }
  1666. var intersect = intersects[0],
  1667. point = intersect.point;
  1668. var geometry = new THREE.BoxGeometry( 0.1, 0.1, 0.1 );
  1669. var material = new THREE.MeshBasicMaterial( {
  1670. color: new THREE.Color( 0xaaaaaa ),
  1671. opacity: 0.3
  1672. });
  1673. mesh = new THREE.Mesh( geometry, material );
  1674. mesh.position.x = point.x;
  1675. mesh.position.y = point.y;
  1676. mesh.position.z = point.z;
  1677. scene.add(mesh);
  1678. intersectCubes[intersectCubes.length] = mesh;
  1679. intersectCubesTimeStamps[intersectCubesTimeStamps.length] = Date.now();
  1680. var realPoint = point.clone();
  1681. getSubvolume(point, false, 'zoomIn', false);
  1682. }
  1683. });
  1684. function getSubvolume(realPoint, alreadyTranslated, zoomAction, sameDepth, fromDraggable) {
  1685. var requestedRes = typeof(window.requestedSize)=="undefined" ? config.defaultSize : requestedSize;
  1686. if(alreadyTranslated == undefined || !alreadyTranslated) {
  1687. realPoint = realPoint.applyMatrix4((new THREE.Matrix4).makeRotationX(Math.PI / -2));
  1688. // retranslate the point into original position beginning from 0,0,0
  1689. realPoint.add(new THREE.Vector3(0.5, 0.5, 0.5));
  1690. }
  1691. volumePointArray = realPoint.toArray();
  1692. var processingUrl = IPE.util.buildUrl('volumes/' + volumeId + '/processing/sub/');
  1693. $.each(volumePointArray, function(index, value) {
  1694. dimWidth = null;
  1695. // have to decide, whether we in zoomed in or zoomed out status
  1696. if(zoomedInfo == undefined || zoomedInfo == null || fromDraggable) {
  1697. if(index == 0) {
  1698. dimWidth = textureInfo.originalSize.sizex;
  1699. } else if(index == 1) {
  1700. dimWidth = textureInfo.originalSize.sizey;
  1701. } else {
  1702. dimWidth = textureInfo.originalSize.sizez;
  1703. }
  1704. } else {
  1705. if(index < 2) {
  1706. dimWidth = zoomedInfo.dimension.xywidth;
  1707. } else {
  1708. dimWidth = zoomedInfo.numberOfSlices;
  1709. }
  1710. }
  1711. value *= dimWidth;
  1712. // due to interpolation algorithm, we had to rotate the whole cube, that it matches
  1713. // the display of imagej
  1714. // so we have to map the current pointer values to the real volume values
  1715. // but only for the y and z direction --> the x direction is all right
  1716. if(index > 0) {
  1717. value = dimWidth - value;
  1718. }
  1719. if(!fromDraggable && zoomedInfo) {
  1720. if(index == 0) {
  1721. value = value + zoomedInfo.dimension.xmin;
  1722. } else if(index == 1) {
  1723. value = value + zoomedInfo.dimension.ymin;
  1724. } else {
  1725. value = value + zoomedInfo.dimension.zmin;
  1726. }
  1727. }
  1728. value = Math.floor(value);
  1729. volumePointArray[index] = value;
  1730. processingUrl += value + '/';
  1731. });
  1732. frameNumber = imgInfo.frameFrom + currentTexture;
  1733. processingUrl += frameNumber + '/';
  1734. depth = 1;
  1735. if(zoomAction == 'zoomIn') {
  1736. if(zoomedInfo != null){
  1737. if(sameDepth) {
  1738. depth = zoomedInfo.depth;
  1739. } else {
  1740. depth = zoomedInfo.depth + 1;
  1741. }
  1742. }
  1743. } else if(zoomAction == 'zoomOut') {
  1744. if(zoomedInfo.depth > 1) {
  1745. depth = zoomedInfo.depth - 1;
  1746. }
  1747. }
  1748. while(oldZoomedInfos.length > 0
  1749. && oldZoomedInfos.last().zoomedInfo.depth >= depth)
  1750. {
  1751. oldZoomedInfos.pop();
  1752. }
  1753. processingUrl += depth + '/';
  1754. processingUrl += requestedRes + '/';
  1755. $.ajax({
  1756. url: processingUrl,
  1757. success: function(data) {
  1758. var available = false;
  1759. var availableTestCounter = 0;
  1760. zoomedInfo = data;
  1761. oldZoomedInfos.push({
  1762. point: realPoint,
  1763. translated: true,
  1764. zoomedInfo: zoomedInfo
  1765. });
  1766. // this function is called recursive by the settimeout statement
  1767. availableTest = function() {
  1768. $.ajax({
  1769. url: IPE.util.buildUrl('volumes/' + volumeId + '/processing/sprite/' + zoomedInfo.spriteId + '/available/'),
  1770. success: function(data) {
  1771. available = data.available;
  1772. },
  1773. dataType: 'json',
  1774. async: false
  1775. });
  1776. if(available) {
  1777. // subvolume available: fetch it
  1778. fetchSubVolume(zoomAction);
  1779. } else if(availableTestCounter < 80) {
  1780. // abort condition not reached --> try again
  1781. setTimeout(availableTest, 500);
  1782. availableTestCounter++;
  1783. } else {
  1784. oldZoomedInfos.pop();
  1785. // abort condition reached --> error handling
  1786. $(document).trigger('zoomActionFinished', { zoomAction: zoomAction, error: true });
  1787. }
  1788. };
  1789. setTimeout(availableTest, 500);
  1790. },
  1791. dataType: 'json',
  1792. async: true,
  1793. error: function(data) {
  1794. helper.displayMessage('Sorry, an error occured during processing zoom', true, null, 2500);
  1795. }
  1796. });
  1797. };
  1798. var fetchSubVolume = function(zoomAction) {
  1799. imageSrc = IPE.util.buildUrl('volumes/' + volumeId + '/processing/sprite/' + zoomedInfo.spriteId + '/show/');
  1800. var image = new Image();
  1801. var texture = initTexture(new THREE.Texture(image));
  1802. image.onload = function() {
  1803. materialScreen.uniforms.numberOfSlices.value = zoomedInfo.numberOfSlices;
  1804. materialScreen.uniforms.numberOfSlices.needsupdate = true;
  1805. materialScreen.uniforms.slicesOverX.value = zoomedInfo.slicesOverX;
  1806. materialScreen.uniforms.slicesOverX.needsUpdate = true;
  1807. materialScreen.uniforms.slicesOverY.value = zoomedInfo.slicesOverY;
  1808. materialScreen.uniforms.slicesOverY.needsUpdate = true;
  1809. materialScreen.uniforms.numberOfSprites.value = 1;
  1810. materialScreen.uniforms.numberOfSprites.needsUpdate = true;
  1811. materialScreen.uniforms.slicesPerSprite.value = zoomedInfo.numberOfSlices;
  1812. materialScreen.uniforms.slicesPerSprite.needsUpdate = true;
  1813. updateTexture([texture]);
  1814. zoomedVolumeDimension.xmin = zoomedInfo.dimension.xmin;
  1815. zoomedVolumeDimension.xmax = zoomedInfo.dimension.xmin + zoomedInfo.dimension.xywidth - 1;
  1816. zoomedVolumeDimension.ymin = zoomedInfo.dimension.ymin;
  1817. zoomedVolumeDimension.ymax = zoomedInfo.dimension.ymin + zoomedInfo.dimension.xywidth - 1;
  1818. zoomedVolumeDimension.zmin = zoomedInfo.dimension.zmin;
  1819. zoomedVolumeDimension.zmax = zoomedInfo.dimension.zmin + zoomedInfo.numberOfSlices - 1;
  1820. originalVolumeDimension = currentVolumeDimension;
  1821. originalDimension = currentDimension;
  1822. currentVolumeDimension = zoomedVolumeDimension;
  1823. repositionLayerSliders();
  1824. $(document).trigger('zoomActionFinished', {
  1825. zoomAction: zoomAction, textureNumber: frameNumber
  1826. });
  1827. }
  1828. image.src = imageSrc;
  1829. };
  1830. $container.on('contextmenu', function(event){
  1831. if(event.ctrlKey == false) {
  1832. return;
  1833. }
  1834. if(event.altKey == false) {
  1835. return;
  1836. }
  1837. if(!handleZoomStatus(false)) {
  1838. return false;
  1839. }
  1840. return zoomOut();
  1841. });
  1842. function zoomOut() {
  1843. currentZoomPoint = oldZoomedInfos.pop().point;
  1844. if(oldZoomedInfos.length > 0) {
  1845. // using the current point to get a new subvolume
  1846. getSubvolume(currentZoomPoint, true, 'zoomOut', true);
  1847. return false;
  1848. }
  1849. else
  1850. {
  1851. zoomedInfo = null;
  1852. materialScreen.uniforms.numberOfSlices.value = textureInfo.info.numberOfSlices;
  1853. materialScreen.uniforms.numberOfSlices.needsupdate = true;
  1854. materialScreen.uniforms.slicesOverX.value = textureInfo.info.slicesOverX;
  1855. materialScreen.uniforms.slicesOverX.needsUpdate = true;
  1856. materialScreen.uniforms.slicesOverY.value = textureInfo.info.slicesOverY;
  1857. materialScreen.uniforms.slicesOverY.needsUpdate = true;
  1858. materialScreen.uniforms.numberOfSprites.value = imgInfo.numberOfSprites;
  1859. materialScreen.uniforms.numberOfSprites.needsUpdate = true;
  1860. materialScreen.uniforms.slicesPerSprite.value = imgInfo.slicesPerSprite;
  1861. materialScreen.uniforms.slicesPerSprite.needsUpdate = true;
  1862. updateTexture();
  1863. currentVolumeDimension = originalVolumeDimension;
  1864. currentDimension = originalDimension;
  1865. repositionLayerSliders();
  1866. }
  1867. $(document).trigger('zoomActionFinished', {
  1868. zoomAction: 'zoomOut'
  1869. });
  1870. return false;
  1871. }
  1872. function zoomInAllowed() {
  1873. return !zoomedIn || zoomedInfo.dimension.xywidth > 300;
  1874. }
  1875. /**
  1876. * return: procceed or not
  1877. */
  1878. function handleZoomStatus(zoomIn) {
  1879. zoomInButton.removeClass('red');
  1880. zoomInActivated = false;
  1881. if(zoomIn && !zoomInAllowed()) {
  1882. return false;
  1883. } else if(!zoomIn && !zoomedIn) {
  1884. return false;
  1885. }
  1886. waitDiv.removeClass('hidden');
  1887. displayMessage = zoomIn ? 'Generating zoomed subvolume, please wait a moment' : 'Zooming out';
  1888. helper.displayMessage(displayMessage);
  1889. displayTimestamp = Date.now();
  1890. return true;
  1891. }
  1892. // general function to disable stuff like messages etc.
  1893. $(document).on('zoomActionFinished', function(e, params) {
  1894. zoomedIn = zoomedInfo != null;
  1895. timeout = 2000 - (Date.now() - displayTimestamp);
  1896. if(timeout < 0) {
  1897. timeout = 0;
  1898. }
  1899. helper.fadeoutMessage(null, timeout);
  1900. zoomedStatusText = '';
  1901. if(zoomedIn) {
  1902. zoomStatus.addClass('green-background');
  1903. zoomedStatusText = 'zoomed in';
  1904. $.each($('.res-link'), function(index, value) {
  1905. $(value).addClass('disabled');
  1906. });
  1907. if(zoomedInfo.dimension.xywidth <= 300) {
  1908. zoomInButton.prop('disabled', true);
  1909. }
  1910. } else {
  1911. zoomStatus.removeClass('green-background');
  1912. zoomedStatusText = 'zoomed out';
  1913. if(params.error) {
  1914. errorMessage = 'Error during subvolume generation - zoomed out again';
  1915. helper.displayMessage(errorMessage, true, null, 5000);
  1916. }
  1917. $.each($('.res-link'), function(index, value) {
  1918. $(value).removeClass('disabled');
  1919. });
  1920. zoomInButton.prop('disabled', false);
  1921. }
  1922. zoomStatus.html(zoomedStatusText);
  1923. waitDiv.addClass('hidden');
  1924. });
  1925. // draggable related stuff
  1926. // TODO for multilevel zooming
  1927. $(document).on('zoomActionFinished', function(e, params) {
  1928. zoomedIn = zoomedInfo != null;
  1929. if(zoomedIn) {
  1930. zoomedInfoArray.offset[Axis.X] = zoomedVolumeDimension.xmin;
  1931. zoomedInfoArray.offset[Axis.Y] = zoomedVolumeDimension.ymin;
  1932. zoomedInfoArray.offset[Axis.Z] = zoomedVolumeDimension.zmin;
  1933. zoomedInfoArray.dimension[Axis.X] = zoomedInfo.dimension.xywidth;
  1934. zoomedInfoArray.dimension[Axis.Y] = zoomedInfo.dimension.xywidth;
  1935. zoomedInfoArray.dimension[Axis.Z] = zoomedInfo.numberOfSlices;
  1936. } else {
  1937. navigationPlane.hide();
  1938. navigationPlaneParent.removeClass('white');
  1939. navigationSlider.hide();
  1940. navigationSliderParent.removeClass('white');
  1941. }
  1942. navigationPlane.removeClass('no-pos-set');
  1943. navigationSlider.removeClass('no-pos-set');
  1944. });
  1945. navigationPlane.draggable({
  1946. containment: '#navigation-plane-container',
  1947. scroll: false,
  1948. stop: function() {
  1949. zoomedDraggableChanged();
  1950. }
  1951. });
  1952. navigationSlider.draggable({
  1953. containment: '#navigation-slider-container',
  1954. scroll: false,
  1955. stop: function() {
  1956. zoomedDraggableChanged();
  1957. }
  1958. });
  1959. function zoomedDraggableChanged() {
  1960. waitDiv.removeClass('hidden');
  1961. var planeWidthHalf = navigationPlane.width() / 2,
  1962. planeHeightHalf = navigationPlane.height() / 2,
  1963. planeOffsetLeft = navigationPlane.offset().left - navigationPlaneParent.offset().left - 1,
  1964. planeOffsetTop = navigationPlane.offset().top - navigationPlaneParent.offset().top - 1,
  1965. sliderHeightHalf = navigationSlider.height() / 2,
  1966. sliderOffsetTop = navigationSlider.offset().top - navigationSliderParent.offset().top - 1,
  1967. orientation = planeOrientation.orientation,
  1968. direction = planeOrientation.direction;
  1969. // set correct border values due to messure errors
  1970. if(planeOffsetLeft < 0) {
  1971. planeOffsetLeft = 0;
  1972. } else if(planeOffsetLeft > navigationPlaneParent.width() - navigationPlane.width()) {
  1973. planeOffsetLeft = navigationPlaneParent.width() - navigationPlane.width();
  1974. }
  1975. if(planeOffsetTop < 0) {
  1976. planeOffsetTop = 0;
  1977. } else if(planeOffsetTop > navigationPlaneParent.height() - navigationPlane.height()) {
  1978. planeOffsetTop = navigationPlaneParent.height() - navigationPlane.height();
  1979. }
  1980. if(sliderOffsetTop < 0) {
  1981. sliderOffsetTop = 0;
  1982. } else if(sliderOffsetTop > navigationSliderParent.height() - navigationSlider.height()) {
  1983. sliderOffsetTop = navigationSliderParent.height() - navigationSlider.height();
  1984. }
  1985. planeOffsetLeft = planeOffsetLeft + planeWidthHalf;
  1986. planeOffsetTop = planeOffsetTop + planeHeightHalf;
  1987. sliderOffsetTop = sliderOffsetTop + sliderHeightHalf;
  1988. horAxis = planeOrientation.hor.axis;
  1989. verAxis = planeOrientation.ver.axis;
  1990. pointAsArray = Array(3);
  1991. pointAsArray[horAxis] = planeOffsetLeft / navigationPlaneParent.width();
  1992. pointAsArray[verAxis] = planeOffsetTop / navigationPlaneParent.height();
  1993. /*if(planeOrientation.direction == VolumeFaceDirection.BACK) {
  1994. pointAsArray[horAxis] = 1.0 - pointAsArray[horAxis];
  1995. } else if(planeOrientation.orientation == VolumeFaceOrientation.XZ) {
  1996. pointAsArray[horAxis] = 1.0 - pointAsArray[horAxis];
  1997. }*/
  1998. if((planeOrientation.orientation == VolumeFaceOrientation.XY
  1999. && planeOrientation.direction == VolumeFaceDirection.BACK)
  2000. || (planeOrientation.orientation == VolumeFaceOrientation.YZ
  2001. && planeOrientation.direction == VolumeFaceDirection.BACK)
  2002. || (planeOrientation.orientation == VolumeFaceOrientation.XZ
  2003. && planeOrientation.direction == VolumeFaceDirection.BACK)) {
  2004. pointAsArray[horAxis] = 1.0 - pointAsArray[horAxis];
  2005. }
  2006. pointAsArray[sliderOrientation.axis] = sliderOffsetTop / navigationSliderParent.height();
  2007. /*if((sliderOrientation.axis == Axis.Z
  2008. && planeOrientation.direction == VolumeFaceDirection.FRONT)
  2009. || (sliderOrientation.axis == Axis.Y
  2010. && planeOrientation.direction == VolumeFaceDirection.FRONT)
  2011. || (sliderOrientation.axis == Axis.X
  2012. && planeOrientation.direction == VolumeFaceDirection.BACK)) {
  2013. pointAsArray[sliderOrientation.axis] = 1.0 - pointAsArray[sliderOrientation.axis];
  2014. }*/
  2015. if((sliderOrientation.axis == Axis.X
  2016. && planeOrientation.direction == VolumeFaceDirection.FRONT)
  2017. || (sliderOrientation.axis == Axis.Y
  2018. && planeOrientation.direction == VolumeFaceDirection.BACK)
  2019. || (sliderOrientation.axis == Axis.Z
  2020. && planeOrientation.direction == VolumeFaceDirection.BACK)) {
  2021. pointAsArray[sliderOrientation.axis] = 1.0 - pointAsArray[sliderOrientation.axis];
  2022. }
  2023. point = new THREE.Vector3(pointAsArray[0], pointAsArray[1], pointAsArray[2]);
  2024. getSubvolume(point, true, 'zoomIn', true, true);
  2025. };
  2026. var zoomInActivated = false;
  2027. zoomInButton.on('click', function() {
  2028. zoomInActivated = !zoomInActivated;
  2029. if(zoomInActivated) {
  2030. $(this).addClass('red');
  2031. } else {
  2032. $(this).removeClass('red');
  2033. }
  2034. });
  2035. zoomOutButton.on('click', function() {
  2036. zoomInButton.removeClass('red');
  2037. if(!handleZoomStatus(false)) {
  2038. return false;
  2039. }
  2040. return zoomOut();
  2041. });
  2042. function determineAxisLabels() {
  2043. if(Date.now() - lastExecutionTime > 100) {
  2044. var lookAtVector = new THREE.Vector3(0,0, -1);
  2045. lookAtVector.applyQuaternion(camera.quaternion);
  2046. xIterator = -0.4;
  2047. yIterator = -0.4;
  2048. facesIntersect = {};
  2049. interSectStartingPoint = new THREE.Vector3(xIterator, yIterator, camera.near);
  2050. for(xIterator = -0.4; xIterator <= 0.4; xIterator += 0.2) {
  2051. for(yIterator = -0.4; yIterator <= 0.4; yIterator += 0.2) {
  2052. interSectStartingPoint.x = xIterator;
  2053. interSectStartingPoint.y = yIterator;
  2054. vector = interSectStartingPoint.unproject( camera );
  2055. var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
  2056. var intersects = raycaster.intersectObjects( [frontBox] );
  2057. if(intersects.length > 0) {
  2058. var intersect = intersects[0],
  2059. point = intersect.point,
  2060. face = intersect.face,
  2061. faceObject = determineVolumeFace(face.normal),
  2062. faceIdent = faceObject.getIdentifier();
  2063. alreadyAdded = Object.keys(facesIntersect).indexOf(faceIdent) >= 0;
  2064. if(alreadyAdded) {
  2065. facesIntersect[faceIdent].count++;
  2066. } else {
  2067. facesIntersect[faceIdent] = { faceObject: faceObject, count: 1};
  2068. }
  2069. }
  2070. }
  2071. }
  2072. frontFace = { count: 0 };
  2073. for(faceIdent in facesIntersect) {
  2074. if(facesIntersect[faceIdent].count > frontFace.count) {
  2075. frontFace = facesIntersect[faceIdent];
  2076. }
  2077. }
  2078. if(frontFace.faceObject != undefined) {
  2079. volumeFace = frontFace.faceObject;
  2080. axisOfNaviation = volumeFace.getAxisOfNaviation();
  2081. planeOrientation = axisOfNaviation.plane;
  2082. planeOrientation.hor.offsetTo = OffsetTo.LEFT;
  2083. planeOrientation.ver.offsetTo = OffsetTo.TOP;
  2084. planeOrientation.direction = volumeFace.direction;
  2085. planeOrientation.orientation = volumeFace.orientation;
  2086. sliderOrientation = axisOfNaviation.slider;
  2087. sliderOrientation.offsetTo = OffsetTo.TOP;
  2088. navigationPlaneHorLabel.html(planeOrientation.hor.getLabel());
  2089. navigationPlaneVerLabel.html(planeOrientation.ver.getLabel());
  2090. navigationSliderLabel.html(sliderOrientation.getLabel());
  2091. }
  2092. lastExecutionTime = Date.now();
  2093. }
  2094. }
  2095. function determinePlanePosition() {
  2096. if(!zoomedIn) {
  2097. return;
  2098. }
  2099. if(navigationPlane.hasClass('ui-draggable-dragging')
  2100. || navigationPlane.hasClass('no-pos-set')) {
  2101. navigationPlane.addClass('no-pos-set');
  2102. return;
  2103. }
  2104. horAxis = planeOrientation.hor.axis;
  2105. verAxis = planeOrientation.ver.axis;
  2106. planeRelativeX = navigationPlaneParent.width() / originalSizeArray[horAxis];
  2107. planeRelativeY = navigationPlaneParent.height() / originalSizeArray[verAxis];
  2108. planeWidth = planeRelativeX * zoomedInfoArray.dimension[horAxis];
  2109. planeHeight = planeRelativeY * zoomedInfoArray.dimension[verAxis];
  2110. planeWidth = Math.ceil(planeWidth);
  2111. planeHeight = Math.ceil(planeHeight);
  2112. planeOffsetLeft = zoomedInfoArray.offset[horAxis] * planeRelativeX;
  2113. if((planeOrientation.direction == VolumeFaceDirection.BACK
  2114. && planeOrientation.orientation == VolumeFaceOrientation.XZ)
  2115. || (planeOrientation.direction == VolumeFaceDirection.FRONT
  2116. && planeOrientation.orientation == VolumeFaceOrientation.YZ)) {
  2117. planeOffsetLeft = navigationPlaneParent.width() - (planeOffsetLeft + navigationPlane.width());
  2118. }
  2119. planeOffsetTop = zoomedInfoArray.offset[verAxis] * planeRelativeY;
  2120. if(planeOrientation.orientation == VolumeFaceOrientation.XZ
  2121. || planeOrientation.orientation == VolumeFaceOrientation.YZ){
  2122. planeOffsetTop = navigationPlaneParent.height() - (planeOffsetTop + navigationPlane.height());
  2123. }
  2124. navigationPlane.css({
  2125. 'top': planeOffsetTop + 'px',
  2126. 'left' : planeOffsetLeft + 'px',
  2127. 'width': planeWidth + 'px',
  2128. 'height': planeHeight + 'px'
  2129. });
  2130. navigationPlane.show();
  2131. navigationPlaneParent.addClass('white');
  2132. }
  2133. function determineSliderPosition() {
  2134. if(!zoomedIn) {
  2135. return;
  2136. }
  2137. if(navigationSlider.hasClass('ui-draggable-dragging')
  2138. || navigationSlider.hasClass('no-pos-set')) {
  2139. navigationSlider.addClass('no-pos-set');
  2140. return;
  2141. }
  2142. sliderRelative = navigationSliderParent.height() / originalSizeArray[sliderOrientation.axis];
  2143. sliderOffsetTop = zoomedInfoArray.offset[sliderOrientation.axis] * sliderRelative;
  2144. sliderHeight = Math.ceil(sliderRelative * zoomedInfoArray.dimension[sliderOrientation.axis]);
  2145. if(planeOrientation.direction == VolumeFaceDirection.FRONT) {
  2146. sliderOffsetTop = navigationSliderParent.height() - (navigationSlider.height() + sliderOffsetTop);
  2147. }
  2148. navigationSlider.css({
  2149. 'top': sliderOffsetTop + 'px',
  2150. 'height': sliderHeight + 'px'
  2151. });
  2152. navigationSlider.show();
  2153. navigationSliderParent.addClass('white');
  2154. }
  2155. };
  2156. function radToDeg(rad) {
  2157. return rad * 180 / Math.PI;
  2158. }
  2159. ;var Orientation = function(axis, offsetTo) {
  2160. this._axis = axis;
  2161. this._offsetTo = null;
  2162. }
  2163. Orientation.prototype = {
  2164. constructor: Orientation,
  2165. getLabel: function() {
  2166. switch(this._axis) {
  2167. case(Axis.X):
  2168. return 'x-axis';
  2169. case(Axis.Y):
  2170. return 'y-axis';
  2171. case(Axis.Z):
  2172. return 'z-axis';
  2173. }
  2174. },
  2175. get axis() {
  2176. return this._axis;
  2177. },
  2178. get offsetTo() {
  2179. return this._offsetTo;
  2180. },
  2181. set offsetTo(value) {
  2182. this._offsetTo = value;
  2183. }
  2184. }
  2185. var Axis = {
  2186. X: 0,
  2187. Y: 1,
  2188. Z: 2
  2189. }
  2190. var OffsetTo = {
  2191. TOP: 0,
  2192. LEFT: 1,
  2193. BOTTOM: 2,
  2194. RIGHT: 3
  2195. }
  2196. ;IPE.interceptors.SendInterceptor = (function() {
  2197. return {
  2198. init: function() {
  2199. $(document).ajaxSend(function (e, xhr, settings) {
  2200. if(settings.url.indexOf('/static/') === -1) {
  2201. if(!settings.url.endsWith('/')) {
  2202. settings.url += '/';
  2203. }
  2204. }
  2205. });
  2206. }
  2207. };
  2208. }())
  2209. ;var NavigationPlane = function(element, horizontalLabel, verticalLabel) {
  2210. this._element = element;
  2211. this._parent = element.parent();
  2212. this._horizontalLabel = horizontalLabel;
  2213. this._verticalLabel = verticalLabel;
  2214. this._zoomedInfoArray = null;
  2215. this._volumeFace = null;
  2216. }
  2217. NavigationPlane.prototype = {
  2218. constructor: NavigationPlane,
  2219. set zoomedInfoArray(value) {
  2220. this._zoomedInfoArray = value;
  2221. },
  2222. set volumeFace(value) {
  2223. this._volumeFace = value;
  2224. },
  2225. }
  2226. ;