Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
L
libSDL
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PocketInsanity
libSDL
Commits
311a3e94
Commit
311a3e94
authored
Nov 08, 2011
by
Ryan C. Gordon
Browse files
Options
Browse Files
Download
Plain Diff
Merge from
https://bitbucket.org/keestux/sdl
... SDL_gesture code cleanup.
parents
a61775dd
8d5ba0eb
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
517 additions
and
506 deletions
+517
-506
SDL_gesture.c
src/events/SDL_gesture.c
+517
-506
No files found.
src/events/SDL_gesture.c
View file @
311a3e94
...
@@ -63,7 +63,7 @@ typedef struct {
...
@@ -63,7 +63,7 @@ typedef struct {
}
SDL_DollarTemplate
;
}
SDL_DollarTemplate
;
typedef
struct
{
typedef
struct
{
SDL_Gesture
ID
id
;
SDL_Touch
ID
id
;
SDL_FloatPoint
res
;
SDL_FloatPoint
res
;
SDL_FloatPoint
centroid
;
SDL_FloatPoint
centroid
;
SDL_DollarPath
dollarPath
;
SDL_DollarPath
dollarPath
;
...
@@ -80,33 +80,36 @@ int SDL_numGestureTouches = 0;
...
@@ -80,33 +80,36 @@ int SDL_numGestureTouches = 0;
SDL_bool
recordAll
;
SDL_bool
recordAll
;
#if 0
#if 0
static void PrintPath(SDL_FloatPoint *path) {
static void PrintPath(SDL_FloatPoint *path)
{
int i;
int i;
printf("Path:");
printf("Path:");
for(i=0;i<DOLLARNPOINTS;
i++) {
for (i=0; i<DOLLARNPOINTS;
i++) {
printf(" (%f,%f)",path[i].x,path[i].y);
printf(" (%f,%f)",path[i].x,path[i].y);
}
}
printf("\n");
printf("\n");
}
}
#endif
#endif
int
SDL_RecordGesture
(
SDL_TouchID
touchId
)
{
int
SDL_RecordGesture
(
SDL_TouchID
touchId
)
{
int
i
;
int
i
;
if
(
touchId
<
0
)
recordAll
=
SDL_TRUE
;
if
(
touchId
<
0
)
recordAll
=
SDL_TRUE
;
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
{
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
{
if
((
touchId
<
0
)
||
(
SDL_gestureTouch
[
i
].
id
==
touchId
))
{
if
((
touchId
<
0
)
||
(
SDL_gestureTouch
[
i
].
id
==
touchId
))
{
SDL_gestureTouch
[
i
].
recording
=
SDL_TRUE
;
SDL_gestureTouch
[
i
].
recording
=
SDL_TRUE
;
if
(
touchId
>=
0
)
if
(
touchId
>=
0
)
return
1
;
return
1
;
}
}
}
}
return
(
touchId
<
0
);
return
(
touchId
<
0
);
}
}
static
unsigned
long
SDL_HashDollar
(
SDL_FloatPoint
*
points
)
{
static
unsigned
long
SDL_HashDollar
(
SDL_FloatPoint
*
points
)
{
unsigned
long
hash
=
5381
;
unsigned
long
hash
=
5381
;
int
i
;
int
i
;
for
(
i
=
0
;
i
<
DOLLARNPOINTS
;
i
++
)
{
for
(
i
=
0
;
i
<
DOLLARNPOINTS
;
i
++
)
{
hash
=
((
hash
<<
5
)
+
hash
)
+
(
unsigned
long
)
points
[
i
].
x
;
hash
=
((
hash
<<
5
)
+
hash
)
+
(
unsigned
long
)
points
[
i
].
x
;
hash
=
((
hash
<<
5
)
+
hash
)
+
(
unsigned
long
)
points
[
i
].
y
;
hash
=
((
hash
<<
5
)
+
hash
)
+
(
unsigned
long
)
points
[
i
].
y
;
}
}
...
@@ -114,14 +117,15 @@ static unsigned long SDL_HashDollar(SDL_FloatPoint* points) {
...
@@ -114,14 +117,15 @@ static unsigned long SDL_HashDollar(SDL_FloatPoint* points) {
}
}
static
int
SaveTemplate
(
SDL_DollarTemplate
*
templ
,
SDL_RWops
*
src
)
{
static
int
SaveTemplate
(
SDL_DollarTemplate
*
templ
,
SDL_RWops
*
src
)
if
(
src
==
NULL
)
return
0
;
{
if
(
src
==
NULL
)
return
0
;
//No Longer storing the Hash, rehash on load
//No Longer storing the Hash, rehash on load
//if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0;
//if(SDL_RWops.write(src,&(templ->hash),sizeof(templ->hash),1) != 1) return 0;
if
(
SDL_RWwrite
(
src
,
templ
->
path
,
if
(
SDL_RWwrite
(
src
,
templ
->
path
,
sizeof
(
templ
->
path
[
0
]),
DOLLARNPOINTS
)
!=
DOLLARNPOINTS
)
sizeof
(
templ
->
path
[
0
]),
DOLLARNPOINTS
)
!=
DOLLARNPOINTS
)
return
0
;
return
0
;
...
@@ -129,23 +133,25 @@ static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) {
...
@@ -129,23 +133,25 @@ static int SaveTemplate(SDL_DollarTemplate *templ, SDL_RWops * src) {
}
}
int
SDL_SaveAllDollarTemplates
(
SDL_RWops
*
src
)
{
int
SDL_SaveAllDollarTemplates
(
SDL_RWops
*
src
)
{
int
i
,
j
,
rtrn
=
0
;
int
i
,
j
,
rtrn
=
0
;
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
{
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
{
SDL_GestureTouch
*
touch
=
&
SDL_gestureTouch
[
i
];
SDL_GestureTouch
*
touch
=
&
SDL_gestureTouch
[
i
];
for
(
j
=
0
;
j
<
touch
->
numDollarTemplates
;
j
++
)
{
for
(
j
=
0
;
j
<
touch
->
numDollarTemplates
;
j
++
)
{
rtrn
+=
SaveTemplate
(
&
touch
->
dollarTemplate
[
i
],
src
);
rtrn
+=
SaveTemplate
(
&
touch
->
dollarTemplate
[
i
],
src
);
}
}
}
}
return
rtrn
;
return
rtrn
;
}
}
int
SDL_SaveDollarTemplate
(
SDL_GestureID
gestureId
,
SDL_RWops
*
src
)
{
int
SDL_SaveDollarTemplate
(
SDL_GestureID
gestureId
,
SDL_RWops
*
src
)
{
int
i
,
j
;
int
i
,
j
;
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
{
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
{
SDL_GestureTouch
*
touch
=
&
SDL_gestureTouch
[
i
];
SDL_GestureTouch
*
touch
=
&
SDL_gestureTouch
[
i
];
for
(
j
=
0
;
j
<
touch
->
numDollarTemplates
;
j
++
)
{
for
(
j
=
0
;
j
<
touch
->
numDollarTemplates
;
j
++
)
{
if
(
touch
->
dollarTemplate
[
i
].
hash
==
gestureId
)
{
if
(
touch
->
dollarTemplate
[
i
].
hash
==
gestureId
)
{
return
SaveTemplate
(
&
touch
->
dollarTemplate
[
i
],
src
);
return
SaveTemplate
(
&
touch
->
dollarTemplate
[
i
],
src
);
}
}
}
}
...
@@ -156,79 +162,77 @@ int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) {
...
@@ -156,79 +162,77 @@ int SDL_SaveDollarTemplate(SDL_GestureID gestureId, SDL_RWops *src) {
//path is an already sampled set of points
//path is an already sampled set of points
//Returns the index of the gesture on success, or -1
//Returns the index of the gesture on success, or -1
static
int
SDL_AddDollarGesture
(
SDL_GestureTouch
*
inTouch
,
SDL_FloatPoint
*
path
)
{
static
int
SDL_AddDollarGesture_one
(
SDL_GestureTouch
*
inTouch
,
SDL_FloatPoint
*
path
)
{
SDL_DollarTemplate
*
dollarTemplate
;
SDL_DollarTemplate
*
dollarTemplate
;
SDL_DollarTemplate
*
templ
;
SDL_DollarTemplate
*
templ
;
int
i
=
0
;
int
index
;
if
(
inTouch
==
NULL
)
{
if
(
SDL_numGestureTouches
==
0
)
return
-
1
;
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
{
inTouch
=
&
SDL_gestureTouch
[
i
];
index
=
inTouch
->
numDollarTemplates
;
dollarTemplate
=
dollarTemplate
=
(
SDL_DollarTemplate
*
)
SDL_realloc
(
inTouch
->
dollarTemplate
,
(
SDL_DollarTemplate
*
)
SDL_realloc
(
inTouch
->
dollarTemplate
,
(
inTouch
->
numDollarTemplates
+
1
)
*
(
index
+
1
)
*
sizeof
(
SDL_DollarTemplate
));
sizeof
(
SDL_DollarTemplate
));
if
(
!
dollarTemplate
)
{
if
(
!
dollarTemplate
)
{
SDL_OutOfMemory
();
SDL_OutOfMemory
();
return
-
1
;
return
-
1
;
}
}
inTouch
->
dollarTemplate
=
dollarTemplate
;
inTouch
->
dollarTemplate
=
dollarTemplate
;
templ
=
templ
=
&
inTouch
->
dollarTemplate
[
index
];
&
inTouch
->
dollarTemplate
[
inTouch
->
numDollarTemplates
];
SDL_memcpy
(
templ
->
path
,
path
,
DOLLARNPOINTS
*
sizeof
(
SDL_FloatPoint
));
SDL_memcpy
(
templ
->
path
,
path
,
DOLLARNPOINTS
*
sizeof
(
SDL_FloatPoint
));
templ
->
hash
=
SDL_HashDollar
(
templ
->
path
);
templ
->
hash
=
SDL_HashDollar
(
templ
->
path
);
inTouch
->
numDollarTemplates
++
;
inTouch
->
numDollarTemplates
++
;
}
return
inTouch
->
numDollarTemplates
-
1
;
}
else
{
SDL_DollarTemplate
*
dollarTemplate
=
(
SDL_DollarTemplate
*
)
SDL_realloc
(
inTouch
->
dollarTemplate
,
(
inTouch
->
numDollarTemplates
+
1
)
*
sizeof
(
SDL_DollarTemplate
));
if
(
!
dollarTemplate
)
{
SDL_OutOfMemory
();
return
-
1
;
}
inTouch
->
dollarTemplate
=
dollarTemplate
;
return
index
;
}
templ
=
static
int
SDL_AddDollarGesture
(
SDL_GestureTouch
*
inTouch
,
SDL_FloatPoint
*
path
)
&
inTouch
->
dollarTemplate
[
inTouch
->
numDollarTemplates
];
{
SDL_memcpy
(
templ
->
path
,
path
,
DOLLARNPOINTS
*
sizeof
(
SDL_FloatPoint
));
int
index
;
templ
->
hash
=
SDL_HashDollar
(
templ
->
path
);
int
i
=
0
;
inTouch
->
numDollarTemplates
++
;
if
(
inTouch
==
NULL
)
{
return
inTouch
->
numDollarTemplates
-
1
;
if
(
SDL_numGestureTouches
==
0
)
return
-
1
;
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
{
inTouch
=
&
SDL_gestureTouch
[
i
];
index
=
SDL_AddDollarGesture_one
(
inTouch
,
path
);
if
(
index
<
0
)
return
-
1
;
}
// Use the index of the last one added.
return
index
;
}
else
{
return
SDL_AddDollarGesture_one
(
inTouch
,
path
);
}
}
return
-
1
;
return
-
1
;
}
}
int
SDL_LoadDollarTemplates
(
SDL_TouchID
touchId
,
SDL_RWops
*
src
)
{
int
SDL_LoadDollarTemplates
(
SDL_TouchID
touchId
,
SDL_RWops
*
src
)
{
int
i
,
loaded
=
0
;
int
i
,
loaded
=
0
;
SDL_GestureTouch
*
touch
=
NULL
;
SDL_GestureTouch
*
touch
=
NULL
;
if
(
src
==
NULL
)
return
0
;
if
(
src
==
NULL
)
return
0
;
if
(
touchId
>=
0
)
{
if
(
touchId
>=
0
)
{
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
if
(
SDL_gestureTouch
[
i
].
id
==
touchId
)
if
(
SDL_gestureTouch
[
i
].
id
==
touchId
)
touch
=
&
SDL_gestureTouch
[
i
];
touch
=
&
SDL_gestureTouch
[
i
];
if
(
touch
==
NULL
)
return
-
1
;
if
(
touch
==
NULL
)
return
-
1
;
}
}
while
(
1
)
{
while
(
1
)
{
SDL_DollarTemplate
templ
;
SDL_DollarTemplate
templ
;
if
(
SDL_RWread
(
src
,
templ
.
path
,
sizeof
(
templ
.
path
[
0
]),
DOLLARNPOINTS
)
<
if
(
SDL_RWread
(
src
,
templ
.
path
,
sizeof
(
templ
.
path
[
0
]),
DOLLARNPOINTS
)
<
DOLLARNPOINTS
)
break
;
DOLLARNPOINTS
)
break
;
if
(
touchId
>=
0
)
{
if
(
touchId
>=
0
)
{
//printf("Adding loaded gesture to 1 touch\n");
//printf("Adding loaded gesture to 1 touch\n");
if
(
SDL_AddDollarGesture
(
touch
,
templ
.
path
))
loaded
++
;
if
(
SDL_AddDollarGesture
(
touch
,
templ
.
path
)
>=
0
)
loaded
++
;
}
}
else
{
else
{
//printf("Adding to: %i touches\n",SDL_numGestureTouches);
//printf("Adding to: %i touches\n",SDL_numGestureTouches);
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
{
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
{
touch
=
&
SDL_gestureTouch
[
i
];
touch
=
&
SDL_gestureTouch
[
i
];
//printf("Adding loaded gesture to + touches\n");
//printf("Adding loaded gesture to + touches\n");
//TODO: What if this fails?
//TODO: What if this fails?
...
@@ -242,12 +246,13 @@ int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) {
...
@@ -242,12 +246,13 @@ int SDL_LoadDollarTemplates(SDL_TouchID touchId, SDL_RWops *src) {
}
}
static
float
dollarDifference
(
SDL_FloatPoint
*
points
,
SDL_FloatPoint
*
templ
,
float
ang
)
{
static
float
dollarDifference
(
SDL_FloatPoint
*
points
,
SDL_FloatPoint
*
templ
,
float
ang
)
{
// SDL_FloatPoint p[DOLLARNPOINTS];
// SDL_FloatPoint p[DOLLARNPOINTS];
float
dist
=
0
;
float
dist
=
0
;
SDL_FloatPoint
p
;
SDL_FloatPoint
p
;
int
i
;
int
i
;
for
(
i
=
0
;
i
<
DOLLARNPOINTS
;
i
++
)
{
for
(
i
=
0
;
i
<
DOLLARNPOINTS
;
i
++
)
{
p
.
x
=
(
float
)(
points
[
i
].
x
*
SDL_cos
(
ang
)
-
points
[
i
].
y
*
SDL_sin
(
ang
));
p
.
x
=
(
float
)(
points
[
i
].
x
*
SDL_cos
(
ang
)
-
points
[
i
].
y
*
SDL_sin
(
ang
));
p
.
y
=
(
float
)(
points
[
i
].
x
*
SDL_sin
(
ang
)
+
points
[
i
].
y
*
SDL_cos
(
ang
));
p
.
y
=
(
float
)(
points
[
i
].
x
*
SDL_sin
(
ang
)
+
points
[
i
].
y
*
SDL_cos
(
ang
));
dist
+=
(
float
)(
SDL_sqrt
((
p
.
x
-
templ
[
i
].
x
)
*
(
p
.
x
-
templ
[
i
].
x
)
+
dist
+=
(
float
)(
SDL_sqrt
((
p
.
x
-
templ
[
i
].
x
)
*
(
p
.
x
-
templ
[
i
].
x
)
+
...
@@ -257,7 +262,8 @@ static float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float
...
@@ -257,7 +262,8 @@ static float dollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ,float
}
}
static
float
bestDollarDifference
(
SDL_FloatPoint
*
points
,
SDL_FloatPoint
*
templ
)
{
static
float
bestDollarDifference
(
SDL_FloatPoint
*
points
,
SDL_FloatPoint
*
templ
)
{
//------------BEGIN DOLLAR BLACKBOX----------------//
//------------BEGIN DOLLAR BLACKBOX----------------//
//-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
//-TRANSLATED DIRECTLY FROM PSUDEO-CODE AVAILABLE AT-//
//-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
//-"http://depts.washington.edu/aimgroup/proj/dollar/"-//
...
@@ -268,8 +274,8 @@ static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ)
...
@@ -268,8 +274,8 @@ static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ)
float
f1
=
dollarDifference
(
points
,
templ
,
x1
);
float
f1
=
dollarDifference
(
points
,
templ
,
x1
);
float
x2
=
(
float
)((
1
-
PHI
)
*
ta
+
PHI
*
tb
);
float
x2
=
(
float
)((
1
-
PHI
)
*
ta
+
PHI
*
tb
);
float
f2
=
dollarDifference
(
points
,
templ
,
x2
);
float
f2
=
dollarDifference
(
points
,
templ
,
x2
);
while
(
SDL_fabs
(
ta
-
tb
)
>
dt
)
{
while
(
SDL_fabs
(
ta
-
tb
)
>
dt
)
{
if
(
f1
<
f2
)
{
if
(
f1
<
f2
)
{
tb
=
x2
;
tb
=
x2
;
x2
=
x1
;
x2
=
x1
;
f2
=
f1
;
f2
=
f1
;
...
@@ -285,16 +291,17 @@ static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ)
...
@@ -285,16 +291,17 @@ static float bestDollarDifference(SDL_FloatPoint* points,SDL_FloatPoint* templ)
}
}
}
}
/*
/*
if
(f1 <= f2)
if
(f1 <= f2)
printf("Min angle (x1): %f\n",x1);
printf("Min angle (x1): %f\n",x1);
else if
(f1 > f2)
else if
(f1 > f2)
printf("Min angle (x2): %f\n",x2);
printf("Min angle (x2): %f\n",x2);
*/
*/
return
SDL_min
(
f1
,
f2
);
return
SDL_min
(
f1
,
f2
);
}
}
//DollarPath contains raw points, plus (possibly) the calculated length
//DollarPath contains raw points, plus (possibly) the calculated length
static
int
dollarNormalize
(
const
SDL_DollarPath
*
path
,
SDL_FloatPoint
*
points
)
{
static
int
dollarNormalize
(
const
SDL_DollarPath
*
path
,
SDL_FloatPoint
*
points
)
{
int
i
;
int
i
;
float
interval
;
float
interval
;
float
dist
;
float
dist
;
...
@@ -306,12 +313,10 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
...
@@ -306,12 +313,10 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
float
length
=
path
->
length
;
float
length
=
path
->
length
;
//Calculate length if it hasn't already been done
//Calculate length if it hasn't already been done
if
(
length
<=
0
)
{
if
(
length
<=
0
)
{
for
(
i
=
1
;
i
<
path
->
numPoints
;
i
++
)
{
for
(
i
=
1
;
i
<
path
->
numPoints
;
i
++
)
{
float
dx
=
path
->
p
[
i
].
x
-
float
dx
=
path
->
p
[
i
].
x
-
path
->
p
[
i
-
1
].
x
;
path
->
p
[
i
-
1
].
x
;
float
dy
=
path
->
p
[
i
].
y
-
path
->
p
[
i
-
1
].
y
;
float
dy
=
path
->
p
[
i
].
y
-
path
->
p
[
i
-
1
].
y
;
length
+=
(
float
)(
SDL_sqrt
(
dx
*
dx
+
dy
*
dy
));
length
+=
(
float
)(
SDL_sqrt
(
dx
*
dx
+
dy
*
dy
));
}
}
}
}
...
@@ -323,11 +328,11 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
...
@@ -323,11 +328,11 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
centroid
.
x
=
0
;
centroid
.
y
=
0
;
centroid
.
x
=
0
;
centroid
.
y
=
0
;
//printf("(%f,%f)\n",path->p[path->numPoints-1].x,path->p[path->numPoints-1].y);
//printf("(%f,%f)\n",path->p[path->numPoints-1].x,path->p[path->numPoints-1].y);
for
(
i
=
1
;
i
<
path
->
numPoints
;
i
++
)
{
for
(
i
=
1
;
i
<
path
->
numPoints
;
i
++
)
{
float
d
=
(
float
)(
SDL_sqrt
((
path
->
p
[
i
-
1
].
x
-
path
->
p
[
i
].
x
)
*
(
path
->
p
[
i
-
1
].
x
-
path
->
p
[
i
].
x
)
+
float
d
=
(
float
)(
SDL_sqrt
((
path
->
p
[
i
-
1
].
x
-
path
->
p
[
i
].
x
)
*
(
path
->
p
[
i
-
1
].
x
-
path
->
p
[
i
].
x
)
+
(
path
->
p
[
i
-
1
].
y
-
path
->
p
[
i
].
y
)
*
(
path
->
p
[
i
-
1
].
y
-
path
->
p
[
i
].
y
)));
(
path
->
p
[
i
-
1
].
y
-
path
->
p
[
i
].
y
)
*
(
path
->
p
[
i
-
1
].
y
-
path
->
p
[
i
].
y
)));
//printf("d = %f dist = %f/%f\n",d,dist,interval);
//printf("d = %f dist = %f/%f\n",d,dist,interval);
while
(
dist
+
d
>
interval
)
{
while
(
dist
+
d
>
interval
)
{
points
[
numPoints
].
x
=
path
->
p
[
i
-
1
].
x
+
points
[
numPoints
].
x
=
path
->
p
[
i
-
1
].
x
+
((
interval
-
dist
)
/
d
)
*
(
path
->
p
[
i
].
x
-
path
->
p
[
i
-
1
].
x
);
((
interval
-
dist
)
/
d
)
*
(
path
->
p
[
i
].
x
-
path
->
p
[
i
-
1
].
x
);
points
[
numPoints
].
y
=
path
->
p
[
i
-
1
].
y
+
points
[
numPoints
].
y
=
path
->
p
[
i
-
1
].
y
+
...
@@ -340,7 +345,7 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
...
@@ -340,7 +345,7 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
}
}
dist
+=
d
;
dist
+=
d
;
}
}
if
(
numPoints
<
DOLLARNPOINTS
-
1
)
{
if
(
numPoints
<
DOLLARNPOINTS
-
1
)
{
SDL_SetError
(
"ERROR: NumPoints = %i
\n
"
,
numPoints
);
SDL_SetError
(
"ERROR: NumPoints = %i
\n
"
,
numPoints
);
return
0
;
return
0
;
}
}
...
@@ -361,7 +366,7 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
...
@@ -361,7 +366,7 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
ang
=
(
float
)(
SDL_atan2
(
centroid
.
y
-
points
[
0
].
y
,
ang
=
(
float
)(
SDL_atan2
(
centroid
.
y
-
points
[
0
].
y
,
centroid
.
x
-
points
[
0
].
x
));
centroid
.
x
-
points
[
0
].
x
));
for
(
i
=
0
;
i
<
numPoints
;
i
++
)
{
for
(
i
=
0
;
i
<
numPoints
;
i
++
)
{
float
px
=
points
[
i
].
x
;
float
px
=
points
[
i
].
x
;
float
py
=
points
[
i
].
y
;
float
py
=
points
[
i
].
y
;
points
[
i
].
x
=
(
float
)((
px
-
centroid
.
x
)
*
SDL_cos
(
ang
)
-
points
[
i
].
x
=
(
float
)((
px
-
centroid
.
x
)
*
SDL_cos
(
ang
)
-
...
@@ -370,24 +375,25 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
...
@@ -370,24 +375,25 @@ static int dollarNormalize(const SDL_DollarPath *path,SDL_FloatPoint *points) {
(
py
-
centroid
.
y
)
*
SDL_cos
(
ang
)
+
centroid
.
y
);
(
py
-
centroid
.
y
)
*
SDL_cos
(
ang
)
+
centroid
.
y
);
if
(
points
[
i
].
x
<
xmin
)
xmin
=
points
[
i
].
x
;
if
(
points
[
i
].
x
<
xmin
)
xmin
=
points
[
i
].
x
;
if
(
points
[
i
].
x
>
xmax
)
xmax
=
points
[
i
].
x
;
if
(
points
[
i
].
x
>
xmax
)
xmax
=
points
[
i
].
x
;
if
(
points
[
i
].
y
<
ymin
)
ymin
=
points
[
i
].
y
;
if
(
points
[
i
].
y
<
ymin
)
ymin
=
points
[
i
].
y
;
if
(
points
[
i
].
y
>
ymax
)
ymax
=
points
[
i
].
y
;
if
(
points
[
i
].
y
>
ymax
)
ymax
=
points
[
i
].
y
;
}
}
//Scale points to DOLLARSIZE, and translate to the origin
//Scale points to DOLLARSIZE, and translate to the origin
w
=
xmax
-
xmin
;
w
=
xmax
-
xmin
;
h
=
ymax
-
ymin
;
h
=
ymax
-
ymin
;
for
(
i
=
0
;
i
<
numPoints
;
i
++
)
{
for
(
i
=
0
;
i
<
numPoints
;
i
++
)
{
points
[
i
].
x
=
(
points
[
i
].
x
-
centroid
.
x
)
*
DOLLARSIZE
/
w
;
points
[
i
].
x
=
(
points
[
i
].
x
-
centroid
.
x
)
*
DOLLARSIZE
/
w
;
points
[
i
].
y
=
(
points
[
i
].
y
-
centroid
.
y
)
*
DOLLARSIZE
/
h
;
points
[
i
].
y
=
(
points
[
i
].
y
-
centroid
.
y
)
*
DOLLARSIZE
/
h
;
}
}
return
numPoints
;
return
numPoints
;
}
}
static
float
dollarRecognize
(
const
SDL_DollarPath
*
path
,
int
*
bestTempl
,
SDL_GestureTouch
*
touch
)
{
static
float
dollarRecognize
(
const
SDL_DollarPath
*
path
,
int
*
bestTempl
,
SDL_GestureTouch
*
touch
)
{
SDL_FloatPoint
points
[
DOLLARNPOINTS
];
SDL_FloatPoint
points
[
DOLLARNPOINTS
];
int
i
;
int
i
;
...
@@ -397,19 +403,20 @@ static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_Gestu
...
@@ -397,19 +403,20 @@ static float dollarRecognize(const SDL_DollarPath *path,int *bestTempl,SDL_Gestu
//PrintPath(points);
//PrintPath(points);
*
bestTempl
=
-
1
;
*
bestTempl
=
-
1
;
for
(
i
=
0
;
i
<
touch
->
numDollarTemplates
;
i
++
)
{
for
(
i
=
0
;
i
<
touch
->
numDollarTemplates
;
i
++
)
{
float
diff
=
bestDollarDifference
(
points
,
touch
->
dollarTemplate
[
i
].
path
);
float
diff
=
bestDollarDifference
(
points
,
touch
->
dollarTemplate
[
i
].
path
);
if
(
diff
<
bestDiff
)
{
bestDiff
=
diff
;
*
bestTempl
=
i
;}
if
(
diff
<
bestDiff
)
{
bestDiff
=
diff
;
*
bestTempl
=
i
;}
}
}
return
bestDiff
;
return
bestDiff
;
}
}
int
SDL_GestureAddTouch
(
SDL_Touch
*
touch
)
{
int
SDL_GestureAddTouch
(
SDL_Touch
*
touch
)
{
SDL_GestureTouch
*
gestureTouch
=
(
SDL_GestureTouch
*
)
SDL_realloc
(
SDL_gestureTouch
,
SDL_GestureTouch
*
gestureTouch
=
(
SDL_GestureTouch
*
)
SDL_realloc
(
SDL_gestureTouch
,
(
SDL_numGestureTouches
+
1
)
*
(
SDL_numGestureTouches
+
1
)
*
sizeof
(
SDL_GestureTouch
));
sizeof
(
SDL_GestureTouch
));
if
(
!
gestureTouch
)
{
if
(
!
gestureTouch
)
{
SDL_OutOfMemory
();
SDL_OutOfMemory
();
return
-
1
;
return
-
1
;
}
}
...
@@ -431,16 +438,19 @@ int SDL_GestureAddTouch(SDL_Touch* touch) {
...
@@ -431,16 +438,19 @@ int SDL_GestureAddTouch(SDL_Touch* touch) {
return
0
;
return
0
;
}
}
static
SDL_GestureTouch
*
SDL_GetGestureTouch
(
SDL_TouchID
id
)
{
static
SDL_GestureTouch
*
SDL_GetGestureTouch
(
SDL_TouchID
id
)
{
int
i
;
int
i
;
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
{
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
{
//printf("%i ?= %i\n",SDL_gestureTouch[i].id,id);
//printf("%i ?= %i\n",SDL_gestureTouch[i].id,id);
if
(
SDL_gestureTouch
[
i
].
id
==
id
)
return
&
SDL_gestureTouch
[
i
];
if
(
SDL_gestureTouch
[
i
].
id
==
id
)
return
&
SDL_gestureTouch
[
i
];
}
}
return
NULL
;
return
NULL
;
}
}
static
int
SDL_SendGestureMulti
(
SDL_GestureTouch
*
touch
,
float
dTheta
,
float
dDist
)
{
int
SDL_SendGestureMulti
(
SDL_GestureTouch
*
touch
,
float
dTheta
,
float
dDist
)
{
SDL_Event
event
;
SDL_Event
event
;
event
.
mgesture
.
type
=
SDL_MULTIGESTURE
;
event
.
mgesture
.
type
=
SDL_MULTIGESTURE
;
event
.
mgesture
.
touchId
=
touch
->
id
;
event
.
mgesture
.
touchId
=
touch
->
id
;
...
@@ -453,7 +463,8 @@ static int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist
...
@@ -453,7 +463,8 @@ static int SDL_SendGestureMulti(SDL_GestureTouch* touch,float dTheta,float dDist
}
}
static
int
SDL_SendGestureDollar
(
SDL_GestureTouch
*
touch
,
static
int
SDL_SendGestureDollar
(
SDL_GestureTouch
*
touch
,
SDL_GestureID
gestureId
,
float
error
)
{
SDL_GestureID
gestureId
,
float
error
)
{
SDL_Event
event
;
SDL_Event
event
;
event
.
dgesture
.
type
=
SDL_DOLLARGESTURE
;
event
.
dgesture
.
type
=
SDL_DOLLARGESTURE
;
event
.
dgesture
.
touchId
=
touch
->
id
;
event
.
dgesture
.
touchId
=
touch
->
id
;
...
@@ -470,7 +481,8 @@ static int SDL_SendGestureDollar(SDL_GestureTouch* touch,
...
@@ -470,7 +481,8 @@ static int SDL_SendGestureDollar(SDL_GestureTouch* touch,
}
}
static
int
SDL_SendDollarRecord
(
SDL_GestureTouch
*
touch
,
SDL_GestureID
gestureId
)
{
static
int
SDL_SendDollarRecord
(
SDL_GestureTouch
*
touch
,
SDL_GestureID
gestureId
)
{
SDL_Event
event
;
SDL_Event
event
;
event
.
dgesture
.
type
=
SDL_DOLLARRECORD
;
event
.
dgesture
.
type
=
SDL_DOLLARRECORD
;
event
.
dgesture
.
touchId
=
touch
->
id
;
event
.
dgesture
.
touchId
=
touch
->
id
;
...
@@ -493,13 +505,13 @@ void SDL_GestureProcessEvent(SDL_Event* event)
...
@@ -493,13 +505,13 @@ void SDL_GestureProcessEvent(SDL_Event* event)
float
dtheta
;
float
dtheta
;
float
dDist
;
float
dDist
;
if
(
event
->
type
==
SDL_FINGERMOTION
||
if
(
event
->
type
==
SDL_FINGERMOTION
||
event
->
type
==
SDL_FINGERDOWN
||
event
->
type
==
SDL_FINGERDOWN
||
event
->
type
==
SDL_FINGERUP
)
{
event
->
type
==
SDL_FINGERUP
)
{
SDL_GestureTouch
*
inTouch
=
SDL_GetGestureTouch
(
event
->
tfinger
.
touchId
);
SDL_GestureTouch
*
inTouch
=
SDL_GetGestureTouch
(
event
->
tfinger
.
touchId
);
//Shouldn't be possible
//Shouldn't be possible
if
(
inTouch
==
NULL
)
return
;
if
(
inTouch
==
NULL
)
return
;
//printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x,
//printf("@ (%i,%i) with res: (%i,%i)\n",(int)event->tfinger.x,
// (int)event->tfinger.y,
// (int)event->tfinger.y,
...
@@ -511,24 +523,24 @@ void SDL_GestureProcessEvent(SDL_Event* event)
...
@@ -511,24 +523,24 @@ void SDL_GestureProcessEvent(SDL_Event* event)
//Finger Up
//Finger Up
if
(
event
->
type
==
SDL_FINGERUP
)
{
if
(
event
->
type
==
SDL_FINGERUP
)
{
inTouch
->
numDownFingers
--
;
inTouch
->
numDownFingers
--
;
#ifdef ENABLE_DOLLAR
#ifdef ENABLE_DOLLAR
if
(
inTouch
->
recording
)
{
if
(
inTouch
->
recording
)
{
inTouch
->
recording
=
SDL_FALSE
;
inTouch
->
recording
=
SDL_FALSE
;
dollarNormalize
(
&
inTouch
->
dollarPath
,
path
);
dollarNormalize
(
&
inTouch
->
dollarPath
,
path
);
//PrintPath(path);
//PrintPath(path);
if
(
recordAll
)
{
if
(
recordAll
)
{
index
=
SDL_AddDollarGesture
(
NULL
,
path
);
index
=
SDL_AddDollarGesture
(
NULL
,
path
);
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
for
(
i
=
0
;
i
<
SDL_numGestureTouches
;
i
++
)
SDL_gestureTouch
[
i
].
recording
=
SDL_FALSE
;
SDL_gestureTouch
[
i
].
recording
=
SDL_FALSE
;
}
}
else
{
else
{
index
=
SDL_AddDollarGesture
(
inTouch
,
path
);
index
=
SDL_AddDollarGesture
(
inTouch
,
path
);
}
}
if
(
index
>=
0
)
{
if
(
index
>=
0
)
{
SDL_SendDollarRecord
(
inTouch
,
inTouch
->
dollarTemplate
[
index
].
hash
);
SDL_SendDollarRecord
(
inTouch
,
inTouch
->
dollarTemplate
[
index
].
hash
);
}
}
else
{
else
{
...
@@ -540,7 +552,7 @@ void SDL_GestureProcessEvent(SDL_Event* event)
...
@@ -540,7 +552,7 @@ void SDL_GestureProcessEvent(SDL_Event* event)
float
error
;
float
error
;
error
=
dollarRecognize
(
&
inTouch
->
dollarPath
,
error
=
dollarRecognize
(
&
inTouch
->
dollarPath
,
&
bestTempl
,
inTouch
);
&
bestTempl
,
inTouch
);
if
(
bestTempl
>=
0
){
if
(
bestTempl
>=
0
){
//Send Event
//Send Event
unsigned
long
gestureId
=
inTouch
->
dollarTemplate
[
bestTempl
].
hash
;
unsigned
long
gestureId
=
inTouch
->
dollarTemplate
[
bestTempl
].
hash
;
SDL_SendGestureDollar
(
inTouch
,
gestureId
,
error
);
SDL_SendGestureDollar
(
inTouch
,
gestureId
,
error
);
...
@@ -549,20 +561,20 @@ void SDL_GestureProcessEvent(SDL_Event* event)
...
@@ -549,20 +561,20 @@ void SDL_GestureProcessEvent(SDL_Event* event)
}
}
#endif
#endif
//inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
//inTouch->gestureLast[j] = inTouch->gestureLast[inTouch->numDownFingers];
if
(
inTouch
->
numDownFingers
>
0
)
{
if
(
inTouch
->
numDownFingers
>
0
)
{
inTouch
->
centroid
.
x
=
(
inTouch
->
centroid
.
x
*
(
inTouch
->
numDownFingers
+
1
)
-
inTouch
->
centroid
.
x
=
(
inTouch
->
centroid
.
x
*
(
inTouch
->
numDownFingers
+
1
)
-
x
)
/
inTouch
->
numDownFingers
;
x
)
/
inTouch
->
numDownFingers
;
inTouch
->
centroid
.
y
=
(
inTouch
->
centroid
.
y
*
(
inTouch
->
numDownFingers
+
1
)
-
inTouch
->
centroid
.
y
=
(
inTouch
->
centroid
.
y
*
(
inTouch
->
numDownFingers
+
1
)
-
y
)
/
inTouch
->
numDownFingers
;
y
)
/
inTouch
->
numDownFingers
;
}
}
}
}
else
if
(
event
->
type
==
SDL_FINGERMOTION
)
{
else
if
(
event
->
type
==
SDL_FINGERMOTION
)
{
float
dx
=
((
float
)
event
->
tfinger
.
dx
)
/
(
float
)
inTouch
->
res
.
x
;
float
dx
=
((
float
)
event
->
tfinger
.
dx
)
/
(
float
)
inTouch
->
res
.
x
;
float
dy
=
((
float
)
event
->
tfinger
.
dy
)
/
(
float
)
inTouch
->
res
.
y
;
float
dy
=
((
float
)
event
->
tfinger
.
dy
)
/
(
float
)
inTouch
->
res
.
y
;
//printf("dx,dy: (%f,%f)\n",dx,dy);
//printf("dx,dy: (%f,%f)\n",dx,dy);
#ifdef ENABLE_DOLLAR
#ifdef ENABLE_DOLLAR
SDL_DollarPath
*
path
=
&
inTouch
->
dollarPath
;
SDL_DollarPath
*
path
=
&
inTouch
->
dollarPath
;
if
(
path
->
numPoints
<
MAXPATHSIZE
)
{
if
(
path
->
numPoints
<
MAXPATHSIZE
)
{
path
->
p
[
path
->
numPoints
].
x
=
inTouch
->
centroid
.
x
;
path
->
p
[
path
->
numPoints
].
x
=
inTouch
->
centroid
.
x
;
path
->
p
[
path
->
numPoints
].
y
=
inTouch
->
centroid
.
y
;
path
->
p
[
path
->
numPoints
].
y
=
inTouch
->
centroid
.
y
;
pathDx
=
pathDx
=
...
@@ -580,7 +592,7 @@ void SDL_GestureProcessEvent(SDL_Event* event)
...
@@ -580,7 +592,7 @@ void SDL_GestureProcessEvent(SDL_Event* event)
inTouch
->
centroid
.
x
+=
dx
/
inTouch
->
numDownFingers
;
inTouch
->
centroid
.
x
+=
dx
/
inTouch
->
numDownFingers
;
inTouch
->
centroid
.
y
+=
dy
/
inTouch
->
numDownFingers
;
inTouch
->
centroid
.
y
+=
dy
/
inTouch
->
numDownFingers
;
//printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y);
//printf("Centrid : (%f,%f)\n",inTouch->centroid.x,inTouch->centroid.y);
if
(
inTouch
->
numDownFingers
>
1
)
{
if
(
inTouch
->
numDownFingers
>
1
)
{
SDL_FloatPoint
lv
;
//Vector from centroid to last x,y position
SDL_FloatPoint
lv
;
//Vector from centroid to last x,y position
SDL_FloatPoint
v
;
//Vector from centroid to current x,y position
SDL_FloatPoint
v
;
//Vector from centroid to current x,y position
//lv = inTouch->gestureLast[j].cv;
//lv = inTouch->gestureLast[j].cv;
...
@@ -602,7 +614,7 @@ void SDL_GestureProcessEvent(SDL_Event* event)
...
@@ -602,7 +614,7 @@ void SDL_GestureProcessEvent(SDL_Event* event)
dtheta
=
(
float
)
SDL_atan2
(
lv
.
x
*
v
.
y
-
lv
.
y
*
v
.
x
,
lv
.
x
*
v
.
x
+
lv
.
y
*
v
.
y
);
dtheta
=
(
float
)
SDL_atan2
(
lv
.
x
*
v
.
y
-
lv
.
y
*
v
.
x
,
lv
.
x
*
v
.
x
+
lv
.
y
*
v
.
y
);
dDist
=
(
Dist
-
lDist
);
dDist
=
(
Dist
-
lDist
);
if
(
lDist
==
0
)
{
dDist
=
0
;
dtheta
=
0
;}
//To avoid impossible values
if
(
lDist
==
0
)
{
dDist
=
0
;
dtheta
=
0
;}
//To avoid impossible values
//inTouch->gestureLast[j].dDist = dDist;
//inTouch->gestureLast[j].dDist = dDist;
//inTouch->gestureLast[j].dtheta = dtheta;
//inTouch->gestureLast[j].dtheta = dtheta;
...
@@ -628,7 +640,7 @@ void SDL_GestureProcessEvent(SDL_Event* event)
...
@@ -628,7 +640,7 @@ void SDL_GestureProcessEvent(SDL_Event* event)
//pressure?
//pressure?
}
}
if
(
event
->
type
==
SDL_FINGERDOWN
)
{
if
(
event
->
type
==
SDL_FINGERDOWN
)
{
inTouch
->
numDownFingers
++
;
inTouch
->
numDownFingers
++
;
inTouch
->
centroid
.
x
=
(
inTouch
->
centroid
.
x
*
(
inTouch
->
numDownFingers
-
1
)
+
inTouch
->
centroid
.
x
=
(
inTouch
->
centroid
.
x
*
(
inTouch
->
numDownFingers
-
1
)
+
...
@@ -648,5 +660,4 @@ void SDL_GestureProcessEvent(SDL_Event* event)
...
@@ -648,5 +660,4 @@ void SDL_GestureProcessEvent(SDL_Event* event)
}
}
}
}
/* vi: set ts=4 sw=4 expandtab: */
/* vi: set ts=4 sw=4 expandtab: */
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment