From d73e8abf2dd26c869a1f39a5795cfd9b9b95cf16 Mon Sep 17 00:00:00 2001 From: thery Date: Wed, 1 May 2024 07:18:45 +0200 Subject: [PATCH] Adding straight line --- www/grid.html | 2 + www/grid.js | 121 ++++++++++++++++++++++++++++++++++++- www/jSmoothTrajectories.ml | 15 +++-- 3 files changed, 130 insertions(+), 8 deletions(-) diff --git a/www/grid.html b/www/grid.html index 8452fd9..d2d69b3 100755 --- a/www/grid.html +++ b/www/grid.html @@ -28,6 +28,8 @@ + +

diff --git a/www/grid.js b/www/grid.js index 0b353fe..291fa68 100644 --- a/www/grid.js +++ b/www/grid.js @@ -256,6 +256,10 @@ function cleanCurve () { } function getCurve() { + console.log("getCurve\n"); + if (positions == null) { + return; + } let val = ""; val += outVal(positions.fX) + outVal(positions.fZ) + outVal(positions.tX) + outVal(positions.tZ); @@ -332,6 +336,105 @@ function getCurve() { } } +/* The straight */ + +var straightFlag = true; + +const straightButtons = + document.querySelectorAll('input[name="Show Straight"]'); + +for (const straightButton of straightButtons) { + straightButton.addEventListener("click", setStraight, false); +} + +function setStraight() { + straightFlag = straightButtons[0].checked; + cleanStraight(); + cleanCurve(); + if (straightFlag) { + getStraight(); + } else { + getCurve(); + } + renderer.render( scene, camera ); +} + + +var straights = []; +const smaterial = new THREE.LineBasicMaterial( { color: 'orange' } ); +setStraight(); + + +function cleanStraight () { + let i = 0; + console.log("straights " + straights); + while (i < straights.length) + for (const straight of straights) { + scene.remove(straight); + i++; + } + renderer.render( scene, camera ); + straights = []; +} + +function getStraight() { + console.log("getStraight\n"); + if (positions == null) { + return; + } + let val = ""; + val += outVal(positions.fX) + outVal(positions.fZ) + + outVal(positions.tX) + outVal(positions.tZ); + if (borders.length != 2) { + return; + } + if (borders[0].fZ <= borders[1].fZ) { + val += outVal(borders[0].fX) + outVal(borders[0].fZ) + + outVal(borders[0].tX) + outVal(borders[0].tZ); + val += outVal(borders[1].fX) + outVal(borders[1].fZ) + + outVal(borders[1].tX) + outVal(borders[1].tZ); + } else { + val += outVal(borders[1].fX) + outVal(borders[1].fZ) + + outVal(borders[1].tX) + outVal(borders[1].tZ); + val += outVal(borders[0].fX) + outVal(borders[0].fZ) + + outVal(borders[0].tX) + outVal(borders[0].tZ); + } + for (const obstacle of obstacles) { + val += outVal(obstacle.fX) + outVal(obstacle.fZ) + + outVal(obstacle.tX) + outVal(obstacle.tZ); + } + console.log("boarders " + borders.length + " obstacles " + obstacles.length); + console.log("val " + val); + let res = ocamlLib.straight(val); + console.log("res " + res); + let res1 = res.split(' ').map(Number); + let i = 0; + while (i < res1.length) { + if (res1[i] == 1) { + /* Straight line */ + let fx = res1[i + 2] / res1 [i + 3] * gSize - 0.5 - gSize/2; + let fy = 0.3; + let fz = res1[i + 4] / res1 [i + 5] * gSize - 0.5 - gSize/2; + let tx = res1[i + 6] / res1 [i + 7] * gSize - 0.5 - gSize/2; + let ty = 0.3; + let tz = res1[i + 8] / res1 [i + 9] * gSize - 0.5 - gSize/2; + console.log("Adding a line" + fx + " " + fz + " " + tx + " " + tz); + let epoints = []; + epoints.push( new THREE.Vector3(fx, fy, fz) ); + epoints.push( new THREE.Vector3(tx, ty, tz)); + let egeometry = new THREE.BufferGeometry().setFromPoints( epoints ); + let sline = new THREE.Line( egeometry, smaterial ); + straights.push(sline); + scene.add( sline ); + renderer.render( scene, camera ); + i += 10; + } else if (res1[i] == 2) { + i += 14; + } else { + i++; + } + } +} /* The modality */ @@ -346,6 +449,7 @@ for (const radioButton of radioButtons) { function setModality() { cleanCurve(); + cleanStraight(); fromValid = false; toValid = false; fromCube.position.y = -0.2; @@ -400,6 +504,7 @@ function onDocumentMouseDown( event ) { fromCube.position.y = -0.2; toCube.position.y = -0.2; cleanCurve(); + cleanStraight(); renderer.render( scene, camera ); } if (fromValid) { @@ -419,6 +524,7 @@ function onDocumentMouseDown( event ) { return; } cleanCurve(); + cleanStraight(); addObstacle(fromX, fromZ, toX, toZ); } if (modality == "positions") { @@ -430,7 +536,12 @@ function onDocumentMouseDown( event ) { renderer.render( scene, camera ); positions = {fX : fromX, fZ : fromZ, tX : toX, tZ : toZ } cleanCurve(); - getCurve(); + cleanStraight(); + if (straightFlag) { + getStraight(); + } else { + getCurve(); + } } } else { fromValid = true; @@ -441,6 +552,7 @@ function onDocumentMouseDown( event ) { fromCube.position.x = fromX; toCube.position.y = -0.2; cleanCurve(); + cleanStraight(); renderer.render( scene, camera ); } } @@ -504,7 +616,12 @@ document.getElementById('loadButton').addEventListener('click', function() { renderer.render( scene, camera ); positions = {fX : fX, fZ : fZ, tX : tX, tZ : tZ } cleanCurve(); - getCurve(); + cleanStraight(); + if (straightFlag) { + getStraight(); + } else { + getCurve(); + } renderer.render( scene, camera ); } }; diff --git a/www/jSmoothTrajectories.ml b/www/jSmoothTrajectories.ml index 67f8520..f06aeb1 100644 --- a/www/jSmoothTrajectories.ml +++ b/www/jSmoothTrajectories.ml @@ -91,7 +91,7 @@ let call_smooth s = l2stringr (curve_elements2n v) -let call_smooth1 s = +let call_straight s = let l = string2ln s in match l with | p1n1 :: p1d1 :: p1n2 :: p1d2 :: p2n1 :: p2d1 :: p2n2 ::p2d2 :: @@ -99,11 +99,13 @@ let call_smooth1 s = e2n1 :: e2d1 :: e2n2 :: e2d2 :: e2n3 :: e2d3 :: e2n4 :: e2d4 :: ls -> let es = list2es ls in - ((n2edge e1n1 e1d1 e1n2 e1d2 e1n3 e1d3 e1n4 e1d4), - (n2edge e2n1 e2d1 e2n2 e2d2 e2n3 e2d3 e2n4 e2d4), - es , - (n2pt p1n1 p1d1 p1n2 p1d2), - (n2pt p2n1 p2d1 p2n2 p2d2)) + let v = qstraight_point_to_point (n2edge e1n1 e1d1 e1n2 e1d2 e1n3 e1d3 e1n4 e1d4) + (n2edge e2n1 e2d1 e2n2 e2d2 e2n3 e2d3 e2n4 e2d4) + es + (n2pt p1n1 p1d1 p1n2 p1d2) + (n2pt p2n1 p2d1 p2n2 p2d2) in + l2stringr (curve_elements2n v) + let rec cells_element2n ce = match ce with @@ -132,6 +134,7 @@ let call_cells s = let _ = Js.export "ocamlLib" (object%js + method straight s = Js.string (call_smooth (Js.to_string s)) method smooth s = Js.string (call_smooth (Js.to_string s)) method cells s = Js.string (call_cells (Js.to_string s)) end)