server.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. const express = require('express');
  2. const { Bundle, Client } = require('node-osc');
  3. const path = require('path');
  4. const bodyParser = require('body-parser');
  5. const app = express();
  6. const oscClient = new Client('127.0.0.1', 3333);
  7. let alive = [];
  8. let fseq;
  9. let objTriangle = [] ;
  10. function getHypotenuse(touch1, touch2) {
  11. const x = Math.abs(touch1.x - touch2.x);
  12. const y = Math.abs(touch1.y - touch2.y);
  13. return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
  14. }
  15. function getTop(dotTrio) {
  16. const dist01 = getHypotenuse(dotTrio[0], dotTrio[1]);
  17. const dist02 = getHypotenuse(dotTrio[0], dotTrio[2]);
  18. const dist12 = getHypotenuse(dotTrio[1], dotTrio[2]);
  19. const diff01m02 = Math.abs(dist01 - dist02);
  20. const diff01m12 = Math.abs(dist01 - dist12);
  21. const diff02m12 = Math.abs(dist02 - dist12);
  22. if (diff01m02 < diff02m12 && diff01m02 < diff01m12) {return 0;}
  23. else if (diff01m12<diff01m02 && diff01m12<diff02m12) {return 1;}
  24. else if (diff02m12<diff01m02 && diff02m12<diff01m12) {return 2;}
  25. }
  26. function getAngleApex(dotTrio, topIndex) {
  27. let dotA;
  28. let dotB;
  29. let dotC;
  30. dotB = dotTrio[topIndex];
  31. if (topIndex == 0) {
  32. dotA = dotTrio[1];
  33. dotC = dotTrio[2];
  34. }
  35. else if (topIndex == 1) {
  36. dotA = dotTrio[0];
  37. dotC = dotTrio[2];
  38. }
  39. else if (topIndex == 2) {
  40. dotA = dotTrio[0];
  41. dotC = dotTrio[1];
  42. }
  43. const AB = [dotB.x - dotA.x, dotB.y - dotA.y] ;
  44. const CB = [dotB.x - dotC.x, dotB.y - dotC.y] ;
  45. const dotProd = (AB[0] * CB[0] + AB[1] * CB[1]);
  46. const crossProd = (AB[0]*CB[1] - AB[1] * CB[0]);
  47. const alpha = Math.atan2(crossProd, dotProd);
  48. //return alpha ;
  49. return Math.floor(alpha * 180. / Math.PI + 0.5) ;
  50. }
  51. function getOrientation(dotTrio, topIndex) {
  52. let dotA;
  53. let dotB;
  54. let dotC;
  55. dotB = dotTrio[topIndex];
  56. if (topIndex == 0) {
  57. dotA = dotTrio[1];
  58. dotC = dotTrio[2];
  59. }
  60. else if (topIndex == 1) {
  61. dotA = dotTrio[0];
  62. dotC = dotTrio[2];
  63. }
  64. else if (topIndex == 2) {
  65. dotA = dotTrio[0];
  66. dotC = dotTrio[1];
  67. }
  68. const middlePt = [(dotA.x+dotC.x)/2 ,(dotA.y+dotC.y)/2 ] ;
  69. let diff = [dotB.x - middlePt[0], dotB.y - middlePt[1]] ;
  70. const length = Math.sqrt(Math.pow(diff[0],2) + Math.pow(diff[1], 2) ) ;
  71. //normalize diff
  72. diff = [diff[0]/length, diff[1]/length];
  73. const rad = Math.atan2(diff[0], diff[1]) ;
  74. return Math.floor( -1 * rad * 180 / Math.PI) ;
  75. //return length ;
  76. }
  77. const server = require('http').Server(app);
  78. app.use(bodyParser.json());
  79. app.use(bodyParser.urlencoded({extended:true}));
  80. app.use(express.static('./frontend/assets'));
  81. app.get('/', function (req, res) {
  82. res.sendFile('./frontend/index.html', {
  83. root: path.resolve(__dirname + '/..')
  84. })
  85. });
  86. app.post('/json', function (req, res) {
  87. if (req.body.debug) {
  88. //console.log(req.body);
  89. }
  90. let oscBundle;
  91. if (req.body.event === 'touchend') {
  92. fseq = fseq ? fseq + 1 : 1;
  93. const aliveMessage = [ '/tuio/2Dcur', 'alive' ].concat(alive);
  94. oscBundle = new Bundle(
  95. [ '/tuio/2Dcur', 'source', `tangibles${req.body.section.toString()}@127.0.0.1` ],
  96. aliveMessage,
  97. [ '/tuio/2Dcur', 'fseq', fseq ],
  98. [
  99. '/tuio/2Dcur',
  100. 'del',
  101. req.body.changedTouches[0].identifier
  102. ]
  103. );
  104. oscClient.send(oscBundle, () => {
  105. const index = alive.indexOf(req.body.changedTouches[0].identifier);
  106. alive.splice(index, 1);
  107. if (alive.length === 0) {
  108. oscBundle = new Bundle(
  109. [ '/tuio/2Dcur', 'source', `tangibles${req.body.section.toString()}@127.0.0.1` ],
  110. [ '/tuio/2Dcur', 'alive' ],
  111. [ '/tuio/2Dcur', 'fseq', fseq ]
  112. );
  113. oscClient.send(oscBundle, () => {
  114. res.status(200).send();
  115. fseq = 0;
  116. });
  117. } else {
  118. res.status(200).send();
  119. }
  120. });
  121. } else {
  122. if (req.body.changedTouches && req.body.changedTouches.length && req.body.changedTouches.length > 0) {
  123. fseq = fseq ? fseq + 1 : 1;
  124. const touches = Object.keys(req.body.changedTouches);
  125. const aliveMessage = [ '/tuio/2Dcur', 'alive' ].concat(alive);
  126. touches.forEach(touch => {
  127. const id = req.body.changedTouches[touch].identifier;
  128. if (!alive.includes(id)) {
  129. alive.push(id);
  130. aliveMessage.push(id);
  131. }
  132. });
  133. /* Listage de tous les points */
  134. const dots = [];
  135. touches.forEach(function(touch) {
  136. dots.push({
  137. id: req.body.changedTouches[touch].identifier,
  138. x: req.body.changedTouches[touch].clientX,
  139. y: req.body.changedTouches[touch].clientY
  140. });
  141. });
  142. // console.log(dots, dots.length);
  143. /* Listage des segments */
  144. const segments = [];
  145. if (dots.length > 2) {
  146. for (var i = 0; i < dots.length; i++) {
  147. for (var j = 0; j < dots.length; j++) {
  148. if (j !== i) {
  149. /* on vérifie que le segment n'est pas déjà listé */
  150. const alreadyExists = segments.find(segment => {
  151. return segment.identifiers.includes(i) && segment.identifiers.includes(j);
  152. });
  153. /* on calcule la taille du segment (l'hypoténuse) */
  154. var hyp = getHypotenuse(dots[i], dots[j]);
  155. /* on garde uniquement les segments inférieurs à 550px
  156. * cette valeur devra être une variable de configuration */
  157. if (!alreadyExists && hyp <= 750) {
  158. segments.push({
  159. identifiers: [i, j],
  160. x1: dots[i].x,
  161. x2: dots[j].x,
  162. y1: dots[i].y,
  163. y2: dots[j].y,
  164. hyp
  165. });
  166. }
  167. }
  168. }
  169. }
  170. }
  171. // console.log(segments, segments.length);
  172. /* Listage des triangles */
  173. const triangles = [];
  174. /* on boucle sur les segments */
  175. segments.forEach((segment) => {
  176. const dot1 = segment.identifiers[0];
  177. const dot2 = segment.identifiers[1];
  178. /* on vérifie que le triangle n'est pas déjà listé */
  179. const alreadyExists = triangles.find(triangle => {
  180. return triangle.includes(dot1) && triangle.includes(dot2);
  181. });
  182. if (!alreadyExists) {
  183. /* on cherche les segments qui contiennent un des 2 points du segment actuel
  184. * ex: si le segment actuel est AB, on cherche un segment contenant A (pour AC) et un autre contenant B (pour BC) */
  185. const found1 = segments.findIndex(seg => {
  186. return (seg.identifiers.includes(dot1) && !seg.identifiers.includes(dot2));
  187. });
  188. const found2 = segments.findIndex(seg => {
  189. return (seg.identifiers.includes(dot2) && !seg.identifiers.includes(dot1));
  190. });
  191. /* si on trouve bien les 2 segments (AC et BC), on peut créer un triangle */
  192. if (found1 !== -1 && found2 !== -1) {
  193. /* on devine quel est le 3ème point du triangle par rapport au segment actuel (le point C par rapport au segment AB) */
  194. const dot3 = segments[found1].identifiers.find(identifier => {
  195. return identifier !== dot1 && identifier !== dot2;
  196. });
  197. triangles.push([dot1, dot2, dot3]);
  198. }
  199. }
  200. });
  201. console.log(triangles, triangles.length);
  202. //MOD TITI
  203. //objet pour stocker les informations des triangles identifiés (points, centre, apexAngle, orientation, indice apex, width, height)
  204. objTriangle = {} ;
  205. /* Définition de l'apex */
  206. triangles.forEach(triangle => {
  207. objTriangle.dots = [];
  208. objTriangle.dots[0] = dots[triangle[0]];
  209. objTriangle.dots[1] = dots[triangle[1]];
  210. objTriangle.dots[2] = dots[triangle[2]];
  211. objTriangle.apex = getTop(objTriangle.dots);
  212. objTriangle.center = [
  213. (objTriangle.dots[0].x+objTriangle.dots[1].x+objTriangle.dots[2].x)/3 ,
  214. (objTriangle.dots[0].y+objTriangle.dots[1].y+objTriangle.dots[2].y)/3
  215. ];
  216. objTriangle.angleApex = getAngleApex(objTriangle.dots, objTriangle.apex) ;
  217. objTriangle.orientation = getOrientation(objTriangle.dots, objTriangle.apex) ;
  218. // /* on récupère les 3 côtés (segments) du triangle */
  219. // const segment1 = segments.find(segment => {
  220. // return segment.identifiers.includes(triangle[0]) && segment.identifiers.includes(triangle[1]);
  221. // });
  222. // const segment2 = segments.find(segment => {
  223. // return segment.identifiers.includes(triangle[1]) && segment.identifiers.includes(triangle[2]);
  224. // });
  225. // const segment3 = segments.find(segment => {
  226. // return segment.identifiers.includes(triangle[0]) && segment.identifiers.includes(triangle[2]);
  227. // });
  228. // /* on trouve quel côté est le plus court
  229. // * NOTE: dans notre cas, l'apex sera toujours le point opposé au plus petit côté
  230. // * cette méthode n'est pas généralisable à d'autres projets */
  231. // const smallestSegment = [segment1, segment2, segment3].reduce(function(prev, current) {
  232. // return (prev.hyp < current.hyp) ? prev : current
  233. // });
  234. // /* on déduit quel point du triangle est l'apex */
  235. // const apexDot = triangle.find(dot => {
  236. // return dot !== smallestSegment.identifiers[0] && dot !== smallestSegment.identifiers[1];
  237. // });
  238. console.log(objTriangle.apex);
  239. console.log('centerPos : ' + objTriangle.center + ' orientation : ' + objTriangle.orientation);
  240. /* Reste à faire:
  241. * trouver la valeur de l'angle de l'apex (théorème des cosinus)
  242. * trouver le "centre" du triangle (via les orthocentres ou médianes??)
  243. * déduire l'orientation du triangle
  244. */
  245. });
  246. //plante vite
  247. // if (objTriangle.dots != undefined){
  248. // let oscBundleObj ;
  249. // oscBundleObj = new Bundle(
  250. // [ '/tuio/2Dobj', 'source', `tangibles${req.body.section.toString()}@127.0.0.1` ],
  251. // [ '/tuio/2Dobj', 'alive', 1 ],
  252. // [
  253. // '/tuio/2Dobj',
  254. // 'set',
  255. // 1,
  256. // 1,
  257. // objTriangle.center[0],
  258. // objTriangle.center[1],
  259. // objTriangle.orientation,
  260. // 0.0,
  261. // 0.0,
  262. // 0.0,
  263. // 0.0,
  264. // 0.0
  265. // ],
  266. // [ '/tuio/2Dobj', 'fseq', fseq ]
  267. // );
  268. // // // objTriangle.forEach(triangleIndex => {
  269. // // // oscBundle.append(
  270. // // // [
  271. // // // '/tuio/2Dobj',
  272. // // // 'set',
  273. // // // triangleIndex,
  274. // // // triangleIndex,
  275. // // // objTriangle[triangleIndex].center[0],
  276. // // // objTriangle[triangleIndex].center[1],
  277. // // // objTriangle[triangleIndex].orientation,
  278. // // // 0.0,
  279. // // // 0.0,
  280. // // // 0.0,
  281. // // // 0.0,
  282. // // // 0.0
  283. // // // ]
  284. // // // );
  285. // // // });
  286. // console.log(oscBundleObj);
  287. // oscClient.send(oscBundleObj, () => {
  288. // res.status(200).send();
  289. // });
  290. // }
  291. // oscBundle = new Bundle(
  292. // [ '/tuio/2Dcur', 'source', `tangibles${req.body.section.toString()}@127.0.0.1` ],
  293. // aliveMessage,
  294. // [ '/tuio/2Dcur', 'fseq', fseq ]
  295. // );
  296. // touches.forEach(touch => {
  297. // oscBundle.append(
  298. // [
  299. // '/tuio/2Dcur',
  300. // 'set',
  301. // req.body.changedTouches[touch].identifier,
  302. // req.body.changedTouches[touch].clientX / req.body.screenW,
  303. // req.body.changedTouches[touch].clientY / req.body.screenH,
  304. // 0.0,
  305. // 0.0
  306. // ]
  307. // );
  308. // });
  309. oscBundle = new Bundle ;
  310. oscBundle.append([ '/tuio/2Dobj', 'alive', 1 ]);
  311. if (objTriangle.dots != undefined){
  312. oscBundle.append([
  313. '/tuio/2Dobj',
  314. 'set',
  315. 1,
  316. 1,
  317. objTriangle.center[0],
  318. objTriangle.center[1],
  319. objTriangle.orientation,
  320. 0.0,
  321. 0.0,
  322. 0.0,
  323. 0.0,
  324. 0.0
  325. ]);
  326. }
  327. oscBundle.append([ '/tuio/2Dobj', 'fseq', fseq ]);
  328. oscClient.send(oscBundle, () => {
  329. res.status(200).send();
  330. });
  331. } else {
  332. res.status(400).send();
  333. }
  334. }
  335. });
  336. server.listen(5001, function () {
  337. console.log('Votre app est disponible sur localhost:5001 !')
  338. });