-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#5): ✨ Add menu sections for measurements
This new menu section will display measurements of points and distances, and display extra attributes
- Loading branch information
Showing
4 changed files
with
326 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| #measurements_list .mcard{background:#2d373c;border:1px solid #1c2428;border-radius:4px;margin:6px 4px;padding:6px 8px;font-family:inherit;font-size:12px;color:#eee;} | ||
| #measurements_list .mheader{display:flex;align-items:center;margin-bottom:4px;font-weight:bold;} | ||
| #measurements_list .mstatus{width:8px;height:8px;border-radius:50%;background:#68d96e;margin-right:6px;} | ||
| #measurements_list .mdelete{margin-left:auto;cursor:pointer;color:#d55;font-weight:bold;background:transparent;border:none;font-size:14px;} | ||
| #measurements_list table{width:100%;border-collapse:collapse;margin-top:4px;} | ||
| #measurements_list td{padding:2px 4px;} | ||
| #measurements_list tr:nth-child(even){background:#3a454b;} | ||
| #measurements_list .valueRow td{font-weight:bold;text-align:center;background:#4a555b;} | ||
| #measurements_list .attrRow td{font-size:11px;color:#cfd5d8;} | ||
| #measurements_list .empty{opacity:.6;padding:8px;text-align:center;} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| export function initMeasurementsPanel(viewer){ | ||
| const container = document.getElementById('measurements_list'); | ||
| if(!container){ | ||
| console.warn('Measurements list container not found'); | ||
| return; | ||
| } | ||
|
|
||
| const state = new Map(); | ||
| let needsRender = true; | ||
|
|
||
| function formatNumber(n, p=2){ | ||
| if(n===undefined||n===null||isNaN(n)) return '-'; | ||
| return Number(n).toFixed(p); | ||
| } | ||
|
|
||
| function distance(a,b){ | ||
| const A = a.position || a; const B = b.position || b; | ||
| return Math.sqrt((A.x-B.x)**2 + (A.y-B.y)**2 + (A.z-B.z)**2); | ||
| } | ||
|
|
||
| function inferType(m){ | ||
| if(m.points && m.points.length===1) return 'potCoordinate'; | ||
| if(m.points && m.points.length>1) return 'potDistance'; | ||
| return 'Measurement'; | ||
| } | ||
|
|
||
| function addMeasurement(m){ | ||
| state.set(m.uuid, {measurement: m, dirty:true}); | ||
| needsRender = true; | ||
| } | ||
|
|
||
| function removeMeasurement(m){ | ||
| state.delete(m.uuid); | ||
| needsRender = true; | ||
| } | ||
|
|
||
| function markDirty(m){ | ||
| const entry = state.get(m.uuid); | ||
| if(entry){ entry.dirty = true; needsRender = true; } | ||
| } | ||
|
|
||
| function collectAttributes(point){ | ||
| if(!point) return []; | ||
| const attrs = []; | ||
| const accepted = point.accepted ?? point.ACCEPTED ?? point.Accepted; | ||
| const tvu = point.TVU ?? point.tvu; | ||
| const thu = point.THU ?? point.thu; | ||
| if(accepted !== undefined) attrs.push(['accepted', accepted]); | ||
| if(tvu !== undefined) attrs.push(['TVU', tvu]); | ||
| if(thu !== undefined) attrs.push(['THU', thu]); | ||
| return attrs; | ||
| } | ||
|
|
||
| function render(){ | ||
| if(!needsRender) return; | ||
| needsRender = false; | ||
| container.innerHTML = ''; | ||
| if(state.size===0){ | ||
| container.innerHTML = '<div class="empty">No measurements</div>'; | ||
| return; | ||
| } | ||
|
|
||
| for(const {measurement:m} of state.values()){ | ||
| const points = m.points || []; | ||
| const card = document.createElement('div'); | ||
| card.className='mcard'; | ||
|
|
||
| let body = '<table>'; | ||
| for(const pt of points){ | ||
| const vec = pt.position || pt; | ||
| body += `<tr><td colspan="3">${formatNumber(vec.x)}, ${formatNumber(vec.y)}, ${formatNumber(vec.z)}</td></tr>`; | ||
| const attrs = collectAttributes(pt); | ||
| for(const [k,v] of attrs){ | ||
| body += `<tr class="attrRow"><td colspan="3">${k}: ${v}</td></tr>`; | ||
| } | ||
| } | ||
| if(points.length>1){ | ||
| let total=0; | ||
| for(let i=0;i<points.length-1;i++){ | ||
| const seg = distance(points[i], points[i+1]); | ||
| total+=seg; | ||
| body += `<tr class="valueRow"><td colspan="3">${formatNumber(seg)}</td></tr>`; | ||
| } | ||
| body += `<tr class="valueRow"><td colspan="3">${formatNumber(total)}</td></tr>`; | ||
| } | ||
| body += '</table>'; | ||
| card.innerHTML = ` | ||
| <div class="mheader"><div class="mstatus"></div><span>${m.name || inferType(m)}</span><button class="mdelete" data-uuid="${m.uuid}" title="Delete">×</button></div> | ||
| ${body}`; | ||
| container.appendChild(card); | ||
| } | ||
| } | ||
|
|
||
| viewer.scene.addEventListener('measurement_added', e=> addMeasurement(e.measurement)); | ||
| viewer.scene.addEventListener('measurement_removed', e=> removeMeasurement(e.measurement)); | ||
|
|
||
| viewer.addEventListener('update', ()=>{ | ||
| for(const entry of state.values()){ | ||
| const m = entry.measurement; | ||
| if(!m.points) continue; | ||
| // If any point still at (0,0,0) while others not, mark dirty; or if distances change | ||
| if(m.points.some(pt => (pt.position||pt).x!==0 || (pt.position||pt).y!==0 || (pt.position||pt).z!==0)){ | ||
| if(entry.dirty){ | ||
| } else if(Math.random()<0.02){ // light periodic refresh safety | ||
| entry.dirty=true; | ||
| } | ||
| } | ||
| } | ||
| if([...state.values()].some(e=>e.dirty)){ | ||
| for(const e of state.values()) e.dirty=false; | ||
| needsRender=true; | ||
| } | ||
| render(); | ||
| }); | ||
|
|
||
| // Initial existing measurements | ||
| for(const m of viewer.scene.measurements){ addMeasurement(m); } | ||
| render(); | ||
|
|
||
| container.addEventListener('click', e=>{ | ||
| const btn = e.target.closest('button.mdelete'); | ||
| if(!btn) return; | ||
| const m = state.get(btn.getAttribute('data-uuid'))?.measurement; | ||
| if(m){ viewer.scene.removeMeasurement(m); } | ||
| }); | ||
| } |