|
@@ -9,12 +9,83 @@ const oscClient = new Client('127.0.0.1', 3333);
|
|
|
let alive = [];
|
|
|
let fseq;
|
|
|
|
|
|
+var objTriangle = [] ;
|
|
|
+
|
|
|
function getHypotenuse(touch1, touch2) {
|
|
|
var x = Math.abs(touch1.x - touch2.x);
|
|
|
var y = Math.abs(touch1.y - touch2.y);
|
|
|
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+function getTop(dotTrio) {
|
|
|
+ var dist01 = getHypotenuse(dotTrio[0], dotTrio[1]);
|
|
|
+ var dist02 = getHypotenuse(dotTrio[0], dotTrio[2]);
|
|
|
+ var dist12 = getHypotenuse(dotTrio[1], dotTrio[2]);
|
|
|
+
|
|
|
+ var diff01m02 = Math.abs(dist01 - dist02);
|
|
|
+ var diff01m12 = Math.abs(dist01 - dist12);
|
|
|
+ var diff02m12 = Math.abs(dist02 - dist12);
|
|
|
+
|
|
|
+ if (diff01m02 < diff02m12 && diff01m02 < diff01m12) {return 0;}
|
|
|
+ else if (diff01m12<diff01m02 && diff01m12<diff02m12) {return 1;}
|
|
|
+ else if (diff02m12<diff01m02 && diff02m12<diff01m12) {return 2;}
|
|
|
+}
|
|
|
+
|
|
|
+function getAngleApex(dotTrio, topIndex) {
|
|
|
+ var dotA, dotB, dotC;
|
|
|
+ dotB = dotTrio[topIndex];
|
|
|
+ if (topIndex == 0) {
|
|
|
+ dotA = dotTrio[1];
|
|
|
+ dotC = dotTrio[2];
|
|
|
+ }
|
|
|
+ else if (topIndex == 1) {
|
|
|
+ dotA = dotTrio[0];
|
|
|
+ dotC = dotTrio[2];
|
|
|
+ }
|
|
|
+ else if (topIndex == 2) {
|
|
|
+ dotA = dotTrio[0];
|
|
|
+ dotC = dotTrio[1];
|
|
|
+ }
|
|
|
+
|
|
|
+ var AB = [dotB.x - dotA.x, dotB.y - dotA.y] ;
|
|
|
+ var CB = [dotB.x - dotC.x, dotB.y - dotC.y] ;
|
|
|
+
|
|
|
+ var dotProd = (AB[0] * CB[0] + AB[1] * CB[1]);
|
|
|
+ var crossProd = (AB[0]*CB[1] - AB[1] * CB[0]);
|
|
|
+
|
|
|
+ var alpha = Math.atan2(crossProd, dotProd);
|
|
|
+ //return alpha ;
|
|
|
+ return Math.floor(alpha * 180. / Math.PI + 0.5) ;
|
|
|
+}
|
|
|
+
|
|
|
+function getOrientation(dotTrio, topIndex) {
|
|
|
+ var dotA, dotB, dotC;
|
|
|
+ dotB = dotTrio[topIndex];
|
|
|
+ if (topIndex == 0) {
|
|
|
+ dotA = dotTrio[1];
|
|
|
+ dotC = dotTrio[2];
|
|
|
+ }
|
|
|
+ else if (topIndex == 1) {
|
|
|
+ dotA = dotTrio[0];
|
|
|
+ dotC = dotTrio[2];
|
|
|
+ }
|
|
|
+ else if (topIndex == 2) {
|
|
|
+ dotA = dotTrio[0];
|
|
|
+ dotC = dotTrio[1];
|
|
|
+ }
|
|
|
+
|
|
|
+ var middlePt = [(dotA.x+dotC.x)/2 ,(dotA.y+dotC.y)/2 ] ;
|
|
|
+ var diff = [dotB.x - middlePt[0], dotB.y - middlePt[1]] ;
|
|
|
+ var length = Math.sqrt(Math.pow(diff[0],2) + Math.pow(diff[1], 2) ) ;
|
|
|
+ //normalize diff
|
|
|
+ diff = [diff[0]/length, diff[1]/length];
|
|
|
+ var rad = Math.atan2(diff[0], diff[1]) ;
|
|
|
+ return Math.floor( -1 * rad * 180 / Math.PI) ;
|
|
|
+ //return length ;
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
const server = require('http').Server(app);
|
|
|
|
|
|
app.use(bodyParser.json());
|
|
@@ -103,7 +174,7 @@ app.post('/json', function (req, res) {
|
|
|
var hyp = getHypotenuse(dots[i], dots[j]);
|
|
|
/* on garde uniquement les segments inférieurs à 550px
|
|
|
* cette valeur devra être une variable de configuration */
|
|
|
- if (!alreadyExists && hyp <= 550) {
|
|
|
+ if (!alreadyExists && hyp <= 750) {
|
|
|
segments.push({
|
|
|
identifiers: [i, j],
|
|
|
x1: dots[i].x,
|
|
@@ -148,31 +219,49 @@ app.post('/json', function (req, res) {
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
- // console.log(triangles, triangles.length);
|
|
|
+ console.log(triangles, triangles.length);
|
|
|
+
|
|
|
+ //MOD TITI
|
|
|
+ //objet pour stocker les informations des triangles identifiés (points, centre, apexAngle, orientation, indice apex, width, height)
|
|
|
+
|
|
|
+ objTriangle = {} ;
|
|
|
|
|
|
/* Définition de l'apex */
|
|
|
triangles.forEach(triangle => {
|
|
|
- /* on récupère les 3 côtés (segments) du triangle */
|
|
|
- const segment1 = segments.find(segment => {
|
|
|
- return segment.identifiers.includes(triangle[0]) && segment.identifiers.includes(triangle[1]);
|
|
|
- });
|
|
|
- const segment2 = segments.find(segment => {
|
|
|
- return segment.identifiers.includes(triangle[1]) && segment.identifiers.includes(triangle[2]);
|
|
|
- });
|
|
|
- const segment3 = segments.find(segment => {
|
|
|
- return segment.identifiers.includes(triangle[0]) && segment.identifiers.includes(triangle[2]);
|
|
|
- });
|
|
|
- /* on trouve quel côté est le plus court
|
|
|
- * NOTE: dans notre cas, l'apex sera toujours le point opposé au plus petit côté
|
|
|
- * cette méthode n'est pas généralisable à d'autres projets */
|
|
|
- const smallestSegment = [segment1, segment2, segment3].reduce(function(prev, current) {
|
|
|
- return (prev.hyp < current.hyp) ? prev : current
|
|
|
- });
|
|
|
- /* on déduit quel point du triangle est l'apex */
|
|
|
- const apexDot = triangle.find(dot => {
|
|
|
- return dot !== smallestSegment.identifiers[0] && dot !== smallestSegment.identifiers[1];
|
|
|
- });
|
|
|
- console.log(apexDot);
|
|
|
+ objTriangle.dots = [];
|
|
|
+ objTriangle.dots[0] = dots[triangle[0]];
|
|
|
+ objTriangle.dots[1] = dots[triangle[1]];
|
|
|
+ objTriangle.dots[2] = dots[triangle[2]];
|
|
|
+
|
|
|
+ objTriangle.apex = getTop(objTriangle.dots);
|
|
|
+ objTriangle.center = [(objTriangle.dots[0].x+objTriangle.dots[1].x+objTriangle.dots[2].x)/3 ,
|
|
|
+ (objTriangle.dots[0].y+objTriangle.dots[1].y+objTriangle.dots[2].y)/3] ;
|
|
|
+
|
|
|
+ objTriangle.angleApex = getAngleApex(objTriangle.dots, objTriangle.apex) ;
|
|
|
+ objTriangle.orientation = getOrientation(objTriangle.dots, objTriangle.apex) ;
|
|
|
+ // /* on récupère les 3 côtés (segments) du triangle */
|
|
|
+ // const segment1 = segments.find(segment => {
|
|
|
+ // return segment.identifiers.includes(triangle[0]) && segment.identifiers.includes(triangle[1]);
|
|
|
+ // });
|
|
|
+ // const segment2 = segments.find(segment => {
|
|
|
+ // return segment.identifiers.includes(triangle[1]) && segment.identifiers.includes(triangle[2]);
|
|
|
+ // });
|
|
|
+ // const segment3 = segments.find(segment => {
|
|
|
+ // return segment.identifiers.includes(triangle[0]) && segment.identifiers.includes(triangle[2]);
|
|
|
+ // });
|
|
|
+ // /* on trouve quel côté est le plus court
|
|
|
+ // * NOTE: dans notre cas, l'apex sera toujours le point opposé au plus petit côté
|
|
|
+ // * cette méthode n'est pas généralisable à d'autres projets */
|
|
|
+ // const smallestSegment = [segment1, segment2, segment3].reduce(function(prev, current) {
|
|
|
+ // return (prev.hyp < current.hyp) ? prev : current
|
|
|
+ // });
|
|
|
+
|
|
|
+ // /* on déduit quel point du triangle est l'apex */
|
|
|
+ // const apexDot = triangle.find(dot => {
|
|
|
+ // return dot !== smallestSegment.identifiers[0] && dot !== smallestSegment.identifiers[1];
|
|
|
+ // });
|
|
|
+ console.log(objTriangle.apex );
|
|
|
+ console.log("centerPos : " + objTriangle.center + " orientation : " + objTriangle.orientation);
|
|
|
|
|
|
/* Reste à faire:
|
|
|
* trouver la valeur de l'angle de l'apex (théorème des cosinus)
|
|
@@ -182,32 +271,99 @@ app.post('/json', function (req, res) {
|
|
|
});
|
|
|
|
|
|
|
|
|
+ //plante vite
|
|
|
+ // if (objTriangle.dots != undefined){
|
|
|
+ // let oscBundleObj ;
|
|
|
+ // oscBundleObj = new Bundle(
|
|
|
+ // [ '/tuio/2Dobj', 'source', `tangibles${req.body.section.toString()}@127.0.0.1` ],
|
|
|
+ // [ '/tuio/2Dobj', 'alive', 1 ],
|
|
|
+ // [
|
|
|
+ // '/tuio/2Dobj',
|
|
|
+ // 'set',
|
|
|
+ // 1,
|
|
|
+ // 1,
|
|
|
+ // objTriangle.center[0],
|
|
|
+ // objTriangle.center[1],
|
|
|
+ // objTriangle.orientation,
|
|
|
+ // 0.0,
|
|
|
+ // 0.0,
|
|
|
+ // 0.0,
|
|
|
+ // 0.0,
|
|
|
+ // 0.0
|
|
|
+ // ],
|
|
|
+ // [ '/tuio/2Dobj', 'fseq', fseq ]
|
|
|
+ // );
|
|
|
+ // // // objTriangle.forEach(triangleIndex => {
|
|
|
+ // // // oscBundle.append(
|
|
|
+ // // // [
|
|
|
+ // // // '/tuio/2Dobj',
|
|
|
+ // // // 'set',
|
|
|
+ // // // triangleIndex,
|
|
|
+ // // // triangleIndex,
|
|
|
+ // // // objTriangle[triangleIndex].center[0],
|
|
|
+ // // // objTriangle[triangleIndex].center[1],
|
|
|
+ // // // objTriangle[triangleIndex].orientation,
|
|
|
+ // // // 0.0,
|
|
|
+ // // // 0.0,
|
|
|
+ // // // 0.0,
|
|
|
+ // // // 0.0,
|
|
|
+ // // // 0.0
|
|
|
+ // // // ]
|
|
|
+ // // // );
|
|
|
+ // // // });
|
|
|
+ // console.log(oscBundleObj);
|
|
|
+ // oscClient.send(oscBundleObj, () => {
|
|
|
+ // res.status(200).json(JSON.stringify(req.body));
|
|
|
+ // });
|
|
|
+ // }
|
|
|
+
|
|
|
|
|
|
-
|
|
|
- oscBundle = new Bundle(
|
|
|
- [ '/tuio/2Dcur', 'source', `tangibles${req.body.section.toString()}@127.0.0.1` ],
|
|
|
- aliveMessage,
|
|
|
- [ '/tuio/2Dcur', 'fseq', fseq ]
|
|
|
- );
|
|
|
- touches.forEach(touch => {
|
|
|
- oscBundle.append(
|
|
|
- [
|
|
|
- '/tuio/2Dcur',
|
|
|
+ // oscBundle = new Bundle(
|
|
|
+ // [ '/tuio/2Dcur', 'source', `tangibles${req.body.section.toString()}@127.0.0.1` ],
|
|
|
+ // aliveMessage,
|
|
|
+ // [ '/tuio/2Dcur', 'fseq', fseq ]
|
|
|
+ // );
|
|
|
+ // touches.forEach(touch => {
|
|
|
+ // oscBundle.append(
|
|
|
+ // [
|
|
|
+ // '/tuio/2Dcur',
|
|
|
+ // 'set',
|
|
|
+ // req.body.changedTouches[touch].identifier,
|
|
|
+ // req.body.changedTouches[touch].clientX / req.body.screenW,
|
|
|
+ // req.body.changedTouches[touch].clientY / req.body.screenH,
|
|
|
+ // 0.0,
|
|
|
+ // 0.0
|
|
|
+ // ]
|
|
|
+ // );
|
|
|
+ // });
|
|
|
+ oscBundle = new Bundle ;
|
|
|
+ oscBundle.append([ '/tuio/2Dobj', 'alive', 1 ]);
|
|
|
+ if (objTriangle.dots != undefined){
|
|
|
+
|
|
|
+ oscBundle.append([
|
|
|
+ '/tuio/2Dobj',
|
|
|
'set',
|
|
|
- req.body.changedTouches[touch].identifier,
|
|
|
- req.body.changedTouches[touch].clientX / req.body.screenW,
|
|
|
- req.body.changedTouches[touch].clientY / req.body.screenH,
|
|
|
+ 1,
|
|
|
+ 1,
|
|
|
+ objTriangle.center[0],
|
|
|
+ objTriangle.center[1],
|
|
|
+ objTriangle.orientation,
|
|
|
+ 0.0,
|
|
|
+ 0.0,
|
|
|
+ 0.0,
|
|
|
0.0,
|
|
|
0.0
|
|
|
- ]
|
|
|
- );
|
|
|
- });
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ oscBundle.append([ '/tuio/2Dobj', 'fseq', fseq ]);
|
|
|
+
|
|
|
oscClient.send(oscBundle, () => {
|
|
|
res.status(200).json(JSON.stringify(req.body));
|
|
|
- });
|
|
|
- } else {
|
|
|
- res.status(400).send();
|
|
|
- }
|
|
|
+ });}
|
|
|
+
|
|
|
+ else {
|
|
|
+ res.status(400).send();
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
|