From 86ac4dae4502d57a8ac9ba7ebda47fd6e2ee1aa2 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Fri, 21 Jun 2024 14:24:51 +0200 Subject: [PATCH] Show lines --- app.py | 26 ++------------------------ calibration.py | 20 ++++++++++++++++++-- static/calibration-visualiser.js | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 50 insertions(+), 28 deletions(-) diff --git a/app.py b/app.py index 3672e96..fc8ace5 100755 --- a/app.py +++ b/app.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + from flask import Flask, render_template, send_from_directory app = Flask(__name__) @@ -13,30 +15,6 @@ def calibration(): return render_template('calibration.html') -@app.route("/api/calibration-data") -def calibration_data(): - return { - 'leds': [ - {'name': 'led_0010', 'position': [4.7568647, -7.99294013, 7.96453843]}, - {'name': 'led_0020', 'position': [-2.72752819, -9.82011953, 9.24452802]}, - {'name': 'led_0030', 'position': [-7.71848326, -3.84971189, 8.54659339]}, - {'name': 'led_0040', 'position': [-5.50536593, 8.13132868, 10.61159615]}, - {'name': 'led_0050', 'position': [3.2939535, 9.13207673, 8.55962855]}, - {'name': 'led_0060', 'position': [9.95077473, 3.18431267, 10.01643023]}, - {'name': 'led_0070', 'position': [4.80205465, -4.56261438, 4.56638023]}, - {'name': 'led_0080', 'position': [-3.37632619, -7.72890365, 6.26207315]}, - {'name': 'led_0090', 'position': [-7.05810901, 3.15661764, 6.64505902]} - ], - 'spheres': [ - [0.30415745, -0.17907307, 16.7683142], - [3.98316763, 3.44079019, 17.03800586], - [-2.83333147, 4.22778253, 16.95089368], - [2.88769696, -4.29023411, 16.96762258], - [-2.48223398, -4.19363008, 16.89485543], - ], - } - - @app.route('/static/') def send_static(path): return send_from_directory('static', path) diff --git a/calibration.py b/calibration.py index 8eba988..c24b025 100755 --- a/calibration.py +++ b/calibration.py @@ -1,5 +1,7 @@ #!/usr/bin/env python +import io +import json import functools import numpy as np import os @@ -90,8 +92,17 @@ def calibrate(input_dir: str): # Calculate the positions of the light sources light_positions = utils.lines_intersections(sphere_centers, estimated_lights) + print(estimated_lights) + # Return value as dictionnary - return [{'name': name, 'position': position} for name, position in zip(image_names, light_positions)], sphere_centers + return { + 'leds': [{ + 'name': name, + 'position': position.tolist(), + 'directions': estimated_lights[i].tolist(), + } for i, (name, position) in enumerate(zip(image_names, light_positions))], + 'spheres': sphere_centers.tolist(), + } def main(): @@ -99,7 +110,12 @@ def main(): print_error('Expected path to images as argument') sys.exit(1) - print(calibrate(sys.argv[1])) + calib = calibrate(sys.argv[1]) + + with open('data/calibration.json', 'w') as f: + json.dump(calib, f, indent=4) + + print(calib) if __name__ == '__main__': diff --git a/static/calibration-visualiser.js b/static/calibration-visualiser.js index 33d340c..9ed9f13 100644 --- a/static/calibration-visualiser.js +++ b/static/calibration-visualiser.js @@ -116,6 +116,27 @@ class Led extends THREE.Mesh { this.isHovering = false; } + setLines(lines, spheres) { + const material = new THREE.LineBasicMaterial({ + color: 0x0000ff + }); + for (let index = 0; index < lines.length; index++) { + let line = lines[index]; + let sphere = spheres[index]; + let vertices = new Float32Array([ + -this.position.x - sphere[1], + -this.position.y - sphere[0], + -this.position.z + sphere[2], + -line[1] * 100, -line[0] * 100, line[2] * 100, + ]); + let geometry = new THREE.BufferGeometry(); + geometry.setAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) ); + let mesh = new THREE.Line(geometry, material); + mesh.visible = false; + this.add(mesh); + } + } + hover() { if (this.on) { return; @@ -141,12 +162,18 @@ class Led extends THREE.Mesh { turnOn() { this.on = true; + for (let child of this.children) { + child.visible = true; + } this.refreshColor(); } turnOff() { this.on = false; + for (let child of this.children) { + child.visible = false; + } this.refreshColor(); } @@ -173,7 +200,7 @@ async function init(dataPath, domElementArg = document.body) { beforeAnimationPosition = new THREE.Vector3(); beforeAnimationTarget = new THREE.Vector3(); - let request = await fetch('/api/calibration-data'); + let request = await fetch('/data/calibration.json'); let data = await request.json(); domElement = domElementArg; @@ -198,6 +225,7 @@ async function init(dataPath, domElementArg = document.body) { sphere.position.z = row[2]; sphere.name = ledInfo.name; sphere.layers.enable(1); + sphere.setLines(ledInfo.directions, data.spheres); leds.add(sphere); } @@ -395,7 +423,7 @@ function selectLed(led) { led.turnOn(); pointLight.intensity = 100; pointLight.position.copy(led.position); - ledView.src = 'data/small/' + led.name + '.PNG'; + ledView.src = 'data/small/' + led.name; ledView.style.display = "block"; selectedObject.innerText = led.name; }