Skip to content

Commit

Permalink
Adding straight line
Browse files Browse the repository at this point in the history
  • Loading branch information
thery committed May 1, 2024
1 parent acac84a commit d73e8ab
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 8 deletions.
2 changes: 2 additions & 0 deletions www/grid.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
<label for="Position">Starting and ending positions</label>
<input type="checkbox" id="cell" name="Show Cells">
<label> Show Cells </label>
<input type="checkbox" id="straight" name="Show Straight">
<label> Show Straight </label>
<p>
<button id="loadButton">Load</button>
<button id="saveButton">Save</button>
Expand Down
121 changes: 119 additions & 2 deletions www/grid.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 */

Expand All @@ -346,6 +449,7 @@ for (const radioButton of radioButtons) {

function setModality() {
cleanCurve();
cleanStraight();
fromValid = false;
toValid = false;
fromCube.position.y = -0.2;
Expand Down Expand Up @@ -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) {
Expand All @@ -419,6 +524,7 @@ function onDocumentMouseDown( event ) {
return;
}
cleanCurve();
cleanStraight();
addObstacle(fromX, fromZ, toX, toZ);
}
if (modality == "positions") {
Expand All @@ -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;
Expand All @@ -441,6 +552,7 @@ function onDocumentMouseDown( event ) {
fromCube.position.x = fromX;
toCube.position.y = -0.2;
cleanCurve();
cleanStraight();
renderer.render( scene, camera );
}
}
Expand Down Expand Up @@ -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 );
}
};
Expand Down
15 changes: 9 additions & 6 deletions www/jSmoothTrajectories.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,21 @@ 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 ::
e1n1 :: e1d1 :: e1n2 :: e1d2 :: e1n3 :: e1d3 :: e1n4 :: e1d4 ::
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
Expand Down Expand Up @@ -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)

0 comments on commit d73e8ab

Please sign in to comment.