From 190f576cd6b96e4c67d94ee805ea4cd903b80f4e Mon Sep 17 00:00:00 2001
From: Lucio Zambon <lucio.zambon@elettra.eu>
Date: Tue, 14 Jan 2025 04:08:05 +0000
Subject: [PATCH] Update simulator2d.js

---
 simulator2d.js | 74 +++++++++++++++++++++++++++++++++-----------------
 1 file changed, 49 insertions(+), 25 deletions(-)

diff --git a/simulator2d.js b/simulator2d.js
index 8ff9428..aaa526d 100644
--- a/simulator2d.js
+++ b/simulator2d.js
@@ -187,9 +187,9 @@
 							);
 						}
 						appendSvg("path", {id:facility+"_simulatorhor", class: "simulatorhor trajectory "+facility, visibility:visibility, name:"simulatorhor", fill: "none", "stroke-width":"150", "stroke":"red", "stroke-opacity":"0.6", d: dhor.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')}, simulator_clicked_hor, false, false, '.svg-pan-zoom_viewport');
-						appendSvg("path", {id:facility+"_simulatorreferencehor", class: "simulatorhor trajectory "+facility, visibility:visibility, name:"simulatorreferencehor", fill: "none", "stroke-width":"150", "stroke":"darkred", "stroke-opacity":"0.9", d: dhor.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')}, simulator_clicked_hor, false, false, '.svg-pan-zoom_viewport');
+						// appendSvg("path", {id:facility+"_simulatorreferencehor", class: "simulatorhor trajectory "+facility, visibility:visibility, name:"simulatorreferencehor", fill: "none", "stroke-width":"150", "stroke":"darkred", "stroke-opacity":"0.9", d: dhor.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')}, simulator_clicked_hor, false, false, '.svg-pan-zoom_viewport');
 						appendSvg("path", {id:facility+"_simulatorver", class: "simulatorver trajectory "+facility, visibility:visibility, name:"simulatorver", fill: "none", "stroke-width":"150", "stroke":"limegreen", "stroke-opacity":"0.6", d: dver.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')}, simulator_clicked_ver, false, false, '.svg-pan-zoom_viewport');
-						appendSvg("path", {id:facility+"_simulatorreferencever", class: "simulatorver trajectory "+facility, visibility:visibility, name:"simulatorreferencever", fill: "none", "stroke-width":"150", "stroke":"darkgreen", "stroke-opacity":"0.9", d: dver.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')}, simulator_clicked_ver, false, false, '.svg-pan-zoom_viewport');
+						// appendSvg("path", {id:facility+"_simulatorreferencever", class: "simulatorver trajectory "+facility, visibility:visibility, name:"simulatorreferencever", fill: "none", "stroke-width":"150", "stroke":"darkgreen", "stroke-opacity":"0.9", d: dver.join(' ')+(lattice[facility].sections[0].chamber?' Z':'')}, simulator_clicked_ver, false, false, '.svg-pan-zoom_viewport');
 						// console.log("path", {id:facility+"_simulatorhor", name:"simulatorhor", d: dver.join(' ')+' Z'});
 					}
 				}
@@ -217,25 +217,45 @@
 		main.style.cursor = 'crosshair';
 		// main.addEventListener(measurementEvent, measurementListener, false);
 		main.addEventListener('mousemove', function(e) {getRulerPosition(main, e);});
-		rulerSwitch(500, 500);
+		rulerSwitch(0, 0);
 	}
 	function getRulerPosition(main, event) {
 		const rect = main.getBoundingClientRect();
 		const x = event.clientX - rect.left;
 		const y = event.clientY - rect.top;
-		$('#ruler1').attr('transform', 'translate('+ ((x - panZoomPanther.getPan().x)/panZoomPanther.getSizes().realZoom)+' '+((y - panZoomPanther.getPan().y)/panZoomPanther.getSizes().realZoom)+')');
+		const vertex = [];
+		let p;
+		let minDistance = Infinity;
+		let d = 0;
+		let dindex = -1;
+		const x1 = (x - panZoomPanther.getPan().x) / panZoomPanther.getSizes().realZoom;
+		const y1 = (y - panZoomPanther.getPan().y) / panZoomPanther.getSizes().realZoom;
+		for (let i=0; i < lattice.sr.sections.length; i++) {
+			const j = (i+1) % lattice.sr.sections.length;
+			vertex[i] = lattice.sr.sections[i].start;
+			d = pDistance(x1, y1, vertex[i].x, vertex[i].z, lattice.sr.sections[j].start.x, lattice.sr.sections[j].start.z);
+			if (minDistance > d.dist) { minDistance = d.dist; dindex = i; p = d;}
+		}
+		const i = dindex;
+		const j = (i+1) % lattice.sr.sections.length;
+		const alpha = 180/Math.PI*Math.atan2(lattice.sr.sections[j].start.z-lattice.sr.sections[i].start.z, lattice.sr.sections[j].start.x-lattice.sr.sections[i].start.x);
+		console.log(dindex, p.dist, p.x, p.y, alpha);
+		// $('#ruler1').attr('transform', 'translate('+ ((p.x - panZoomPanther.getPan().x)/panZoomPanther.getSizes().realZoom)+' '+((p.y - panZoomPanther.getPan().y)/panZoomPanther.getSizes().realZoom)+')');
+		$('#ruler').attr('transform', 'translate('+ (p.x)+' '+(p.y)+'),rotate('+alpha+')');
 	}
 	function rulerSwitch(cx, cy) {
-		appendSvg("line", {id: 'ruler1', x1: cx, y1: cy, x2: cx+1000, y2: cy, style:"display: block", stroke:"yellow", "stroke-width":100});
-		/*for (let i=1; i<32; i++) {
-			appendSvg("line", {id: 'tick'+i, x1: cx, y1: cy, x2: cx, y2: cy, style:"display: block", fill:"yellow", stroke:"black","stroke-width":1});
-			appendSvg("text", {id: 'tickLabel'+i, x: cx, y: cy, fill:"white", stroke:"black","stroke-width":1, "font-family":"Arial", "font-size":20, "font-weight":"bold", "text-anchor": "start"});
-		}*/
+		appendSvg("g", {id: 'ruler'}, false, false, 'rulerclass');
+		appendSvg("line", {id: 'ruler1', x1: cx-10000, y1: cy, x2: cx+10000, y2: cy, style:"display: block", stroke:"yellow", "stroke-width":100}, false, false, 'rulerclass', '#ruler');
+		for (let i=-10; i<=10; i++) {
+			const xt = (cx+i*1000);
+			appendSvg("line", {id: 'tick1'+(i+11), x1: xt, y1: cy, x2: xt, y2: cy+300, style:"display: block", stroke:"yellow","stroke-width":100}, false, false, 'rulerclass', '#ruler');
+			// appendSvg("text", {id: 'tickLabel'+i, x: cx, y: cy, fill:"white", stroke:"black","stroke-width":1, "font-family":"Arial", "font-size":20, "font-weight":"bold", "text-anchor": "start"});
+		}
 	}
 	function simulatorSwitch(simulatorData, params) {
 		simulator = params.simulator;
-		$("#sr_simulatorreferencehor").attr('visibility', "hidden");
-		$("#sr_simulatorreferencever").attr('visibility', "hidden");
+		// $("#sr_simulatorreferencehor").attr('visibility', "hidden");
+		// $("#sr_simulatorreferencever").attr('visibility', "hidden");
 		scaleHor.$fill.style.width = "50%";
 		scaleVer.$fill.style.width = "50%";
 		if (simulatorData.reader !== false) {
@@ -253,8 +273,8 @@
 				if (typeof $('#'+facility+"_simulatorhor").attr('visibility') != 'undefined') {
 					$('#'+facility+"_simulatorhor").attr('visibility', "hidden");
 					$('#'+facility+"_simulatorver").attr('visibility', "hidden");
-					$('#'+facility+"_simulatorreferencehor").attr('visibility', "hidden");
-					$('#'+facility+"_simulatorreferencever").attr('visibility', "hidden");
+					// $('#'+facility+"_simulatorreferencehor").attr('visibility', "hidden");
+					// $('#'+facility+"_simulatorreferencever").attr('visibility', "hidden");
 				}
 			}
 			$('.simulatorgauge').hide();
@@ -272,8 +292,8 @@
 				if (typeof $('#'+facility+"_simulatorhor").attr('visibility') != 'undefined') {
 					$('#'+facility+"_simulatorhor").attr('visibility', "visible");
 					$('#'+facility+"_simulatorver").attr('visibility', "visible");
-					if (localStorage.getItem('reference'+simulator+'hor')!=null) $('#'+facility+"_simulatorreferencehor").attr('visibility', "visible");
-					if (localStorage.getItem('reference'+simulator+'ver')!=null) $('#'+facility+"_simulatorreferencever").attr('visibility', "visible");
+					// if (localStorage.getItem('reference'+simulator+'hor')!=null) $('#'+facility+"_simulatorreferencehor").attr('visibility', "visible");
+					// if (localStorage.getItem('reference'+simulator+'ver')!=null) $('#'+facility+"_simulatorreferencever").attr('visibility', "visible");
 				}
 				// console.log('#'+facility+"_simulatorhor", $('#'+facility+"_simulatorhor").attr('visibility'), typeof simulatorData[facility]);
 				if (typeof simulatorData[facility] == 'undefined' || typeof simulatorData[facility].map == 'undefined') continue;
@@ -324,6 +344,8 @@
 		const referenceDataVer = [];
 		const referencehor = JSON.parse(localStorage.getItem('reference'+simulator+'hor'));
 		const referencever = JSON.parse(localStorage.getItem('reference'+simulator+'ver'));
+		$('#hor_set').css('background-color',localStorage.getItem('reference'+simulator+'hor')!=null?'red':'lightgrey')
+		$('#ver_set').css('background-color',localStorage.getItem('reference'+simulator+'ver')!=null?'green':'lightgrey')
 		if (val.Hor) {
 			for (i=0; i<val.Hor.length; i++) {
 				if (typeof compData[facility].map[i] == 'undefined') continue;
@@ -332,34 +354,36 @@
 				const pos = compData[facility].pos[j];
 				if (pos==null) continue;
 				const beta = Math.PI*compData[facility].dir[j]/180;
+				const vh = localStorage.getItem('reference'+simulator+'hor')==null? val.Hor[i]: val.Hor[i] - referencehor[i];
 				// https://en.wikipedia.org/wiki/Logarithmic_scale#Extensions
-				const valHor = logScaleHor? Math.sign(val.Hor[i])*Math.log10(1+ Math.abs(val.Hor[i]*Math.LN10)): val.Hor[i];
-				if (localStorage.getItem('reference'+simulator+'hor')!=null) refHor = logScaleHor? Math.sign(referencehor[i])*Math.log10(1+ Math.abs(referencehor[i]*Math.LN10)): referencehor[i];
-				const valVer = logScaleVer? Math.sign(val.Ver[i])*Math.log10(1+ Math.abs(val.Ver[i]*Math.LN10)): val.Ver[i];
-				if (localStorage.getItem('reference'+simulator+'ver')!=null) refVer = logScaleVer? Math.sign(referencever[i])*Math.log10(1+ Math.abs(referencever[i]*Math.LN10)): referencever[i];
+				const valHor = logScaleHor? Math.sign(vh)*Math.log10(1+ Math.abs(vh*Math.LN10)): vh;
+				// if (localStorage.getItem('reference'+simulator+'hor')!=null) refHor = logScaleHor? Math.sign(referencehor[i])*Math.log10(1+ Math.abs(referencehor[i]*Math.LN10)): referencehor[i];
+				const vv = localStorage.getItem('reference'+simulator+'ver')==null? val.Ver[i]: val.Ver[i] - referencever[i];
+				const valVer = logScaleVer? Math.sign(vv)*Math.log10(1+ Math.abs(vv*Math.LN10)): vv;
+				// if (localStorage.getItem('reference'+simulator+'ver')!=null) refVer = logScaleVer? Math.sign(referencever[i])*Math.log10(1+ Math.abs(referencever[i]*Math.LN10)): referencever[i];
 				// if (i<20) console.log('i', i, 'pos',pos, 'dir', compData[facility].dir[i], 'facility', facility, beta, Math.cos(beta), Math.sin(beta));
 				simulatorDataHor.push((simulatorDataHor.length==0?'M':'L')+ 
 					Math.round(pos[0] - valHor*simulatorFactorHor*Math.sin(beta)) + ' ' + 
 					Math.round(pos[1] + valHor*simulatorFactorHor*Math.cos(beta))
 				);
-				if (localStorage.getItem('reference'+simulator+'hor')!=null) referenceDataHor.push((referenceDataHor.length==0?'M':'L')+ 
+				/*if (localStorage.getItem('reference'+simulator+'hor')!=null) referenceDataHor.push((referenceDataHor.length==0?'M':'L')+ 
 					Math.round(pos[0] - refHor*simulatorFactorHor*Math.sin(beta)) + ' ' + 
 					Math.round(pos[1] + refHor*simulatorFactorHor*Math.cos(beta))
-				);
+				);*/
 				simulatorDataVer.push((simulatorDataVer.length==0?'M':'L')+ 
 					Math.round(pos[0] - valVer*simulatorFactorVer*Math.sin(beta)) + ' ' + 
 					Math.round(pos[1] + valVer*simulatorFactorVer*Math.cos(beta))
 				);
-				if (localStorage.getItem('reference'+simulator+'ver')!=null) referenceDataVer.push((referenceDataVer.length==0?'M':'L')+ 
+				/*if (localStorage.getItem('reference'+simulator+'ver')!=null) referenceDataVer.push((referenceDataVer.length==0?'M':'L')+ 
 					Math.round(pos[0] - refVer*simulatorFactorVer*Math.sin(beta)) + ' ' + 
 					Math.round(pos[1] + refVer*simulatorFactorVer*Math.cos(beta))
-				);
+				);*/
 			}
 			// console.log('Hor', simulatorDataHor.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
 			$('#'+facility+"_simulatorhor").attr('d', simulatorDataHor.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
 			$('#'+facility+"_simulatorver").attr('d', simulatorDataVer.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
-			if (localStorage.getItem('reference'+simulator+'hor')!=null) $('#'+facility+"_simulatorreferencehor").attr('d', referenceDataHor.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
-			if (localStorage.getItem('reference'+simulator+'ver')!=null) $('#'+facility+"_simulatorreferencever").attr('d', referenceDataVer.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
+			// if (localStorage.getItem('reference'+simulator+'hor')!=null) $('#'+facility+"_simulatorreferencehor").attr('d', referenceDataHor.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
+			// if (localStorage.getItem('reference'+simulator+'ver')!=null) $('#'+facility+"_simulatorreferencever").attr('d', referenceDataVer.join(' ')+(lattice[facility].sections[0].chamber?' Z':''));
 		}
 	}
 	function simulatorDisplay(params) {
-- 
GitLab