Skip to content
Snippets Groups Projects
blm.js 5.27 KiB
Newer Older
Lucio Zambon's avatar
Lucio Zambon committed
// jshint esversion: 6 
	export function label(blm, params, camera, THREE) {
		const widthHalf = window.innerWidth / 2, heightHalf = window.innerHeight / 2;
		if (blm[params.blm].obj.length) $('.blm.'+params.blm).each(function(i, obj) {
			const id = $(this)[0].id;
			if (typeof blm[params.blm].tag[id] != 'undefined' && typeof blm[params.blm].obj[i] != 'undefined') {
				const pos = blm[params.blm].obj[i].position.clone();
				pos.project(camera);
				const frustum = new THREE.Frustum();
				const matrix = new THREE.Matrix4().multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse);
				frustum.setFromProjectionMatrix(matrix);
				const dist = Math.abs(camera.position.x-blm[params.blm].tag[id].position.x)+Math.abs(camera.position.y-blm[params.blm].tag[id].position.y)+Math.abs(camera.position.z-blm[params.blm].tag[$(this)[0].id].position.z);
				document.getElementById(id).style.display = (frustum.containsPoint(blm[params.blm].tag[id].position) && dist>140 && dist<25000*params.highlightScale)? 'block': 'none';
				document.getElementById(id).style.top = (-(pos.y * heightHalf) + heightHalf - 20)+'px';
				const val = typeof blm[params.blm].val[blm[params.blm].map[i]] == 'undefined'? '???': blm[params.blm].val[blm[params.blm].map[i]].toPrecision(3);
				document.getElementById(id).innerHTML = id+': <span class="blmVal">'+val+'</span>';
				if (blm[params.blm].dir[i]>0) document.getElementById(id).style.left = ((pos.x * widthHalf ) + widthHalf + 35)+'px'; else document.getElementById(id).style.right =  (widthHalf - (pos.x * widthHalf ) + 35)+'px';
			}
		});
	}
	export function menu(lattice, facilities, params, gui, blm, componentCreator) {
		params.blm = '';
		if (document.location.search.indexOf('blm')>-1) {params.blm = document.location.search.indexOf('blm'); myswitch(blm, params, componentCreator);}
		gui.add(params, 'blm', facilities).onChange(function() {myswitch(blm, params, componentCreator);});
		for (let f in facilities) {
			const b = facilities[f];
			if (b=='') continue;
			console.log('blmMenu() - ',b);
			blm[b] = {map: [], dir: [], tag: {}, val: [], obj: [], confsrc: lattice[b].blm.confsrc, datasrc: lattice[b].blm.datasrc, skip: lattice[b].blm.skip};
			fetch(lattice[b].blm.confsrc)
				.then((response) => {return response.json();})
				.then((blmData) => {
				let blmCounter = 0;
				let photoCounter = 0;
				for (let i in blmData) {
					if (!blmData[i] || blmData[i].indexOf('/')==-1) continue;
					const name = blmData[i].split('/')[2].toUpperCase();
					if (name.indexOf(blm[b].skip)>-1) continue;
					for (let bl in blm[b].obj) {
						if (name==blm[b].obj[bl].name.replace('BPM','BLM') || name=='BLM_'+blm[b].obj[bl].name) {blm[b].map[bl] = blmCounter; blmCounter++; break;}
					}
				}
			});
		}
	}
	export function myswitch(blm, params, componentCreator) {
		if (blm.reader !== false) clearInterval(blm.reader);
		blm.reader = false;
		$('.blm').css('display', 'none');
		if (typeof blm[blm.oldIndex] != 'undefined') for (let i=0; i<blm[blm.oldIndex].obj.length; i++) componentCreator.blmUpdate(blm[blm.oldIndex].obj[i], 0);
		if (params.blm=='') {
			$('#application').hide();
			$('#applicationFrame').removeAttr("src");
		} 
		else {
			blm.oldIndex = params.blm;
			read(blm, params);
			blm.reader = setInterval(read, 1000, blm, params);
			$('#application').show();	
			$('.blm.'+params.blm).css('display', 'block');
			$('#applicationFrame').attr("src", "/spa/index.html?s=blm&src="+params.blm+(document.location.search.indexOf('demo')>-1? '&demo': ''));
			$("#applicationFrame").height(window.innerHeight); 
		}
		
	}
	export function render(blm, params, componentCreator, camera, THREE) {
		if (params.blm!='' && typeof blm[params.blm] !== 'undefined' && blm[params.blm].val != [] && blm[params.blm].map != [] && window.blmTime != blm.acqTime) {
			console.log('blmRender()', params.blm,  blm[params.blm].val, blm[params.blm].map, window.blmTime, blm.acqTime);
			blm.acqTime = window.blmTime;
			const blmMax = Math.max.apply(null, blm[params.blm].val.map(Math.abs)); // https://stackoverflow.com/questions/29515761/find-absolute-max-value-in-javascript-array
			let blmPeak = (Math.log10(blmMax) + 2) * 5000;
			blmPeak = blmPeak>20000? 20000: (blmPeak<2000? 2000: blmPeak);
			const blmFactor = blmPeak/blmMax;
			for (let i=0; i<blm[params.blm].obj.length; i++) {
				componentCreator.blmUpdate(blm[params.blm].obj[i], blm[params.blm].val[blm[params.blm].map[i]], blm[params.blm].dir[i], 100, blmFactor);
			}
		}
		if (params.blm) {label(blm, params, camera, THREE);}
	}
	export function read(blm, params) {
		fetch(blm[params.blm].datasrc)
		.then((response) => {return response.json();})
		.then((eventData) => {
			blm[params.blm].val = eventData;
			window.blmTime = +new Date();
		});
	}
	export function append(blm, facility, mycomp, direction) {
		if (!blm[facility]) return;
		if (mycomp.name.indexOf('PHOTO')==-1) {
			blm[facility].obj.push(mycomp);
			blm[facility].tag[mycomp.name] = mycomp;
			blm[facility].dir.push(direction*(mycomp.name.indexOf('_R')==-1? 1: -1));
			const div = document.createElement('div');
			div.id = mycomp.name;
			div.className = 'blm '+facility;
			//div.style.cssText = 'position: absolute; color: white; display: none;';
			div.style.cssText = 'position: absolute; color: white;';
			div.innerHTML = mycomp.name;
			document.body.appendChild(div);
		}
	}