|
45
|
1 |
/* |
|
|
2 |
* This file is part of the TraKERS\Front IDILL package. |
|
|
3 |
* |
|
|
4 |
* (c) IRI <http://www.iri.centrepompidou.fr/> |
|
|
5 |
* |
|
|
6 |
* For the full copyright and license information, please view the LICENSE |
|
|
7 |
* file that was distributed with this source code. |
|
|
8 |
*/ |
|
|
9 |
|
|
|
10 |
/* |
|
|
11 |
* Projet : TraKERS |
|
|
12 |
* Module : Front IDILL |
|
|
13 |
* Fichier : curvesDetector.js |
|
|
14 |
* |
|
|
15 |
* Auteur : alexandre.bastien@iri.centrepompidou.fr |
|
|
16 |
* |
|
|
17 |
* Fonctionnalités : Détecteur de courbes de recherche, appelé par la classe de création du canvas de recherche. |
|
|
18 |
*/ |
|
|
19 |
|
|
|
20 |
/* |
|
|
21 |
* Détecteur de courbes (lignes droites, arcs de cercles et cercles). |
|
|
22 |
*/ |
|
|
23 |
function curvesDetector(divisions, sizeTreshold, dictionary, mosaic) |
|
|
24 |
{ |
|
|
25 |
//Précision de la détection de direction en divisions (par défaut 12, selon une modélisation horaire et non mathématique). |
|
|
26 |
this.divisions = divisions; |
|
|
27 |
//Taille limite pour un segment/courbe pour être considéré comme tel en px. |
|
|
28 |
this.sizeTreshold = sizeTreshold; |
|
|
29 |
|
|
|
30 |
this.dictionary = dictionary; |
|
|
31 |
|
|
|
32 |
this.mosaic = mosaic; |
|
|
33 |
|
|
|
34 |
//Code actuel généré. Il représente la structure de la courbe de recherche. |
|
|
35 |
this.actualCode = ''; |
|
|
36 |
|
|
|
37 |
this.joints = []; |
|
|
38 |
this.jInc = 0; |
|
|
39 |
|
|
|
40 |
//Longueur totale de la courbe. |
|
|
41 |
this.MPTotalDist = 0; |
|
|
42 |
this.SPTotalDist = 0; |
|
|
43 |
//Longueur de la partie actuelle (après début ou changement de direction). |
|
|
44 |
this.MPActualDist = 0; |
|
|
45 |
this.SPActualDist = 0; |
|
|
46 |
//Angles actuels. |
|
|
47 |
this.MPActualAngle = -1; |
|
|
48 |
this.SPActualAngle = -1; |
|
|
49 |
|
|
|
50 |
//Centre du repère du pointeur principal. |
|
|
51 |
this.MPrepX; |
|
|
52 |
this.MPrepY; |
|
|
53 |
//Centre du repère du pointeur secondaire. |
|
|
54 |
this.SPrepX; |
|
|
55 |
this.SPrepY; |
|
|
56 |
|
|
|
57 |
//Coordonnées actuelles du/des pointeur/s. |
|
|
58 |
this.MPx = 0; |
|
|
59 |
this.MPy = 0; |
|
|
60 |
this.SPx = 0; |
|
|
61 |
this.SPy = 0; |
|
|
62 |
//Coordonnées précédentes du/des pointeur/s. |
|
|
63 |
this.MPpx = 0; |
|
|
64 |
this.MPpy = 0; |
|
|
65 |
this.SPpx = 0; |
|
|
66 |
this.SPpy = 0; |
|
|
67 |
|
|
|
68 |
//Si les paramètres sont incorrects, on leur donne une valeur par défaut. |
|
|
69 |
if(isNaN(divisions) || divisions < 1 || divisions > 360) |
|
|
70 |
{ |
|
|
71 |
this.divisions = 12; |
|
|
72 |
} |
|
|
73 |
if(sizeTreshold < 1) |
|
|
74 |
{ |
|
|
75 |
sizeTreshold = 30; |
|
|
76 |
} |
|
|
77 |
} |
|
|
78 |
|
|
|
79 |
/* |
|
|
80 |
* Reinit les paramètres du détecteur. |
|
|
81 |
*/ |
|
|
82 |
curvesDetector.prototype.reinit = function() |
|
|
83 |
{ |
|
|
84 |
this.MPrepX = 0; |
|
|
85 |
this.MPrepY = 0; |
|
|
86 |
this.MPpx = 0; |
|
|
87 |
this.MPpy = 0; |
|
|
88 |
this.MPActualAngle = -1; |
|
|
89 |
this.SPActualAngle = -1; |
|
|
90 |
this.actualCode = ''; |
|
|
91 |
|
|
|
92 |
for(var i = 0 ; i < this.joints.length ; i++) |
|
|
93 |
{ |
|
|
94 |
if(this.joints[i]) |
|
|
95 |
{ |
|
|
96 |
this.joints[i].remove(); |
|
|
97 |
} |
|
|
98 |
} |
|
|
99 |
|
|
|
100 |
this.jInc = 0; |
|
|
101 |
this.MPActualDist = 0; |
|
|
102 |
this.MPTotalDist = 0; |
|
|
103 |
} |
|
|
104 |
|
|
|
105 |
/* |
|
|
106 |
* Met à jour les positions des pointeurs |
|
|
107 |
*/ |
|
|
108 |
curvesDetector.prototype.updatePos = function(mpx, mpy, spx, spy) |
|
|
109 |
{ |
|
|
110 |
//On met à jour les coordonnées récentes. |
|
|
111 |
this.MPx = mpx; |
|
|
112 |
this.MPy = mpy; |
|
|
113 |
|
|
|
114 |
//Si les coordonnées précédentes n'existent pas, alors on les met à jour. |
|
|
115 |
if(!this.MPpx) |
|
|
116 |
{ |
|
|
117 |
this.MPpx = mpx; |
|
|
118 |
} |
|
|
119 |
if(!this.MPpy) |
|
|
120 |
{ |
|
|
121 |
this.MPpy = mpy; |
|
|
122 |
} |
|
|
123 |
if(!this.MPrepX) |
|
|
124 |
{ |
|
|
125 |
this.MPrepX = mpx; |
|
|
126 |
} |
|
|
127 |
if(!this.MPrepY) |
|
|
128 |
{ |
|
|
129 |
this.MPrepY = mpy; |
|
|
130 |
} |
|
|
131 |
|
|
|
132 |
//Si on a un second pointeur. |
|
|
133 |
if(spx && spy) |
|
|
134 |
{ |
|
|
135 |
//On met les coordonnées à jour. |
|
|
136 |
this.SPx = spx; |
|
|
137 |
this.SPy = spy; |
|
|
138 |
|
|
|
139 |
//Si les coordonnées précédentes n'existent pas, alors on les met à jour. |
|
|
140 |
if(!this.SPpx) |
|
|
141 |
{ |
|
|
142 |
this.SPpx = spx; |
|
|
143 |
} |
|
|
144 |
if(!this.SPpy) |
|
|
145 |
{ |
|
|
146 |
this.SPpy = spy; |
|
|
147 |
} |
|
|
148 |
} |
|
|
149 |
|
|
|
150 |
this.updateDists(); |
|
|
151 |
} |
|
|
152 |
|
|
|
153 |
/* |
|
|
154 |
* Met à jour les distances parcourues. |
|
|
155 |
*/ |
|
|
156 |
curvesDetector.prototype.updateDists = function() |
|
|
157 |
{ |
|
|
158 |
var foundGestures; |
|
|
159 |
|
|
|
160 |
//Si on a de quoi calculer les distances. |
|
|
161 |
if(this.MPx && this.MPy && this.MPpx && this.MPpy && this.MPrepX && this.MPrepY) |
|
|
162 |
{ |
|
|
163 |
var MPDist = Math.floor(Math.sqrt((this.MPx - this.MPpx) * (this.MPx - this.MPpx) + (this.MPy - this.MPpy) * (this.MPy - this.MPpy))); |
|
|
164 |
this.MPTotalDist += MPDist; |
|
|
165 |
this.MPActualDist += MPDist; |
|
|
166 |
|
|
|
167 |
var MPCurrentA = -1; |
|
|
168 |
|
|
|
169 |
if(MPDist > 0) |
|
|
170 |
{ |
|
|
171 |
MPCurrentA = this.currentAngle(this.MPrepX, this.MPrepY, this.MPx, this.MPy); |
|
|
172 |
// console.log(MPCurrentA);//, this.MPActualDist); |
|
|
173 |
} |
|
|
174 |
|
|
|
175 |
if(this.MPActualDist > this.sizeTreshold && MPCurrentA != -1) |
|
|
176 |
{ |
|
|
177 |
console.log(this.MPActualAngle, MPCurrentA, this.MPActualAngle != -1, this.MPActualAngle != MPCurrentA); |
|
|
178 |
if(this.MPActualAngle == -1 || this.MPActualAngle != MPCurrentA) |
|
|
179 |
{ |
|
|
180 |
this.MPActualAngle = MPCurrentA; |
|
|
181 |
this.actualCode += 'D' + MPCurrentA; |
|
|
182 |
this.mosaic.actualCode = this.actualCode; |
|
|
183 |
|
|
|
184 |
foundGestures = this.codeToGestures(this.actualCode); |
|
|
185 |
|
|
|
186 |
console.log(+this.jInc+1, this.MPActualDist); |
|
|
187 |
console.log(this.actualCode); |
|
|
188 |
console.log(foundGestures); |
|
|
189 |
|
|
|
190 |
if(foundGestures.length == 0 || foundGestures.split(';').length != 1) |
|
|
191 |
{ |
|
|
192 |
console.log('many curves'); |
|
|
193 |
this.mosaic.curvesGesturesFound = true; |
|
|
194 |
$('.notifications').remove(); |
|
|
195 |
this.mosaic.curvesGestures(foundGestures); |
|
|
196 |
} |
|
|
197 |
else |
|
|
198 |
{ |
|
|
199 |
// console.log(this.mosaic.currentMode); |
|
|
200 |
this.mosaic.currentSearchGesture = foundGestures; |
|
|
201 |
|
|
|
202 |
if(this.mosaic.currentMode == "SEARCH" && this.mosaic.playerIsReady) |
|
|
203 |
{ |
|
|
204 |
this.mosaic.player.widgets[0].searchByGesture(foundGestures); |
|
|
205 |
this.mosaic.isCurrentlyInASearchByGesture = this.mosaic.player.widgets[0].isCurrentlyInASearchByGesture; |
|
|
206 |
|
|
|
207 |
$('.notifications').remove(); |
|
|
208 |
this.mosaic.searchGesture(foundGestures, 'valid'); |
|
|
209 |
|
|
|
210 |
foundGestures = ''; |
|
|
211 |
this.mosaic.curvesGesturesFound = false; |
|
|
212 |
|
|
|
213 |
this.mosaic.isSearchByCurvesOn = false; |
|
|
214 |
this.mosaic.leaveSearch(); |
|
|
215 |
} |
|
|
216 |
else if(this.mosaic.currentMode == "FILTER") |
|
|
217 |
{ |
|
|
218 |
if(this.mosaic.isMosaicFiltered) |
|
|
219 |
{ |
|
|
220 |
console.log('1 curve : ' + foundGestures); |
|
|
221 |
$('.notifications').remove(); |
|
|
222 |
this.mosaic.filterSearchedType = foundGestures; |
|
|
223 |
this.mosaic.filterGesture(foundGestures, 'valid'); |
|
|
224 |
this.mosaic.searchFilter(foundGestures); |
|
|
225 |
foundGestures = ''; |
|
|
226 |
this.mosaic.curvesGesturesFound = false; |
|
|
227 |
|
|
|
228 |
this.mosaic.isSearchByCurvesOn = false; |
|
|
229 |
this.mosaic.leaveSearch(); |
|
|
230 |
} |
|
|
231 |
} |
|
|
232 |
} |
|
|
233 |
|
|
|
234 |
this.MPActualDist = 0; |
|
|
235 |
this.jInc++ |
|
|
236 |
} |
|
|
237 |
else |
|
|
238 |
{ |
|
|
239 |
this.MPrepX = this.MPpx; |
|
|
240 |
this.MPrepY = this.MPpy; |
|
|
241 |
|
|
|
242 |
if(this.joints[this.jInc]) |
|
|
243 |
{ |
|
|
244 |
this.joints[this.jInc].remove(); |
|
|
245 |
} |
|
|
246 |
this.joints[this.jInc] = new paper.Path.Circle(new paper.Point(this.MPrepX, this.MPrepY), 10); |
|
|
247 |
this.joints[this.jInc].strokeColor = 'black'; |
|
|
248 |
this.joints[this.jInc].fillColor = 'green'; |
|
|
249 |
} |
|
|
250 |
|
|
|
251 |
if(this.MPActualAngle == -1) |
|
|
252 |
{ |
|
|
253 |
this.MPActualAngle = MPCurrentA; |
|
|
254 |
} |
|
|
255 |
} |
|
|
256 |
|
|
|
257 |
//On met à jour les coordonnées précédentes. |
|
|
258 |
if(this.MPpx != this.MPx) |
|
|
259 |
{ |
|
|
260 |
this.MPpx = this.MPx; |
|
|
261 |
} |
|
|
262 |
if(this.MPpy != this.MPy) |
|
|
263 |
{ |
|
|
264 |
this.MPpy = this.MPy; |
|
|
265 |
} |
|
|
266 |
} |
|
|
267 |
//Idem au cas où on aurait un deuxième pointeur. |
|
|
268 |
if(this.SPx && this.SPy && this.SPpx && this.SPpy) |
|
|
269 |
{ |
|
|
270 |
var SPDist = Math.floor(Math.sqrt((this.SPx - this.SPpx) * (this.SPx - this.SPpx) + (this.SPy - this.SPpy) * (this.SPy - this.SPpy))); |
|
|
271 |
this.SPTotalDist += SPDist; |
|
|
272 |
this.SPActualDist += SPDist; |
|
|
273 |
|
|
|
274 |
if(SPDist > 0) |
|
|
275 |
{ |
|
|
276 |
this.currentAngle(this.SPpx, this.SPpy, this.SPx, this.SPy); |
|
|
277 |
} |
|
|
278 |
|
|
|
279 |
//On met à jour les coordonnées précédentes. |
|
|
280 |
if(this.SPpx != this.SPx) |
|
|
281 |
{ |
|
|
282 |
this.SPpx = this.SPx; |
|
|
283 |
} |
|
|
284 |
if(this.SPpy != this.SPy) |
|
|
285 |
{ |
|
|
286 |
this.SPpy = this.SPy; |
|
|
287 |
} |
|
|
288 |
} |
|
|
289 |
} |
|
|
290 |
|
|
|
291 |
/* |
|
|
292 |
* Renvoie les noms de gestures du dictionnaire qui ont un code qui commence par le code en entrée. |
|
|
293 |
*/ |
|
|
294 |
curvesDetector.prototype.codeToGestures = function(code) |
|
|
295 |
{ |
|
|
296 |
//Variable qui va stocker tous les noms trouvés. |
|
|
297 |
var retNames = ''; |
|
|
298 |
|
|
|
299 |
//Pour tout le dictionnaire. |
|
|
300 |
for(var i = 0 ; i < this.dictionary.length ; i++) |
|
|
301 |
{ |
|
|
302 |
//Pour touts les codes de chaque gesture du dictionnaire. |
|
|
303 |
for(var j = 0 ; j < this.dictionary[i].codes.length ; j++) |
|
|
304 |
{ |
|
|
305 |
//Si le code en entrée est une partie début d'un des codes. |
|
|
306 |
if(this.dictionary[i].codes[j].indexOf(code) == 0) |
|
|
307 |
{ |
|
|
308 |
//On ajoute le nom de la gesture et on passe à la gesture suivante. |
|
|
309 |
retNames += this.dictionary[i].name + ';'; |
|
|
310 |
break; |
|
|
311 |
} |
|
|
312 |
} |
|
|
313 |
} |
|
|
314 |
//Comme on sépare chaque nom par un ;, il faut supprimer le dernier si au moins un nom a été trouvé. |
|
|
315 |
if(retNames.length > 0) |
|
|
316 |
{ |
|
|
317 |
retNames = retNames.substring(0, retNames.length-1); |
|
|
318 |
} |
|
|
319 |
|
|
|
320 |
//On renvoit les noms. |
|
|
321 |
return retNames; |
|
|
322 |
} |
|
|
323 |
|
|
|
324 |
/* |
|
|
325 |
* Calcule l'angle emprunté par le morceau de segment actuel. On prend A va vers B. |
|
|
326 |
*/ |
|
|
327 |
curvesDetector.prototype.currentAngle = function(xa, ya, xb, yb) |
|
|
328 |
{ |
|
|
329 |
//On calcule l'angle de la droite AB et des abscisses, et on effectue une rotation de 90° vers la gauche. |
|
|
330 |
var angleRad = Math.atan2((ya - yb), (xa - xb)) - Math.PI / 2; |
|
|
331 |
//On traduit les radians en divisions en passant de [-PI/2 ; PI/2] à [0 ; divisions - 1]. |
|
|
332 |
var angleDiv = Math.floor((angleRad > 0 ? angleRad : (2*Math.PI + angleRad)) * this.divisions / (2*Math.PI)); |
|
|
333 |
|
|
|
334 |
//L'angle initial est 0. |
|
|
335 |
if(angleDiv == this.divisions) |
|
|
336 |
{ |
|
|
337 |
angleDiv = 0; |
|
|
338 |
} |
|
|
339 |
|
|
|
340 |
return angleDiv; |
|
|
341 |
} |