forked from btgraham/SparseConvNet-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnlineHandwritingPicture.cpp
More file actions
198 lines (194 loc) · 6.18 KB
/
OnlineHandwritingPicture.cpp
File metadata and controls
198 lines (194 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include "OnlineHandwritingPicture.h"
#include "signature.h"
OnlineHandwritingPicture::OnlineHandwritingPicture(int renderSize, OnlineHandwritingEncoding enc, int label, float penSpeed3d) :
enc(enc), renderSize(renderSize), Picture(label), offset3d(0), penSpeed3d(penSpeed3d) {
}
OnlineHandwritingPicture::~OnlineHandwritingPicture() {
}
void OnlineHandwritingPicture::normalize() { //Fit centrally in the cube [-renderSize/2,renderSize/2]^2
arma::mat pointsm(ops.size(),2);
arma::mat pointsM(ops.size(),2);
for (int i=0;i<ops.size();++i) {
pointsm.row(i)=arma::min(ops[i],0);
pointsM.row(i)=arma::max(ops[i],0);
}
pointsm=arma::min(pointsm,0);
pointsM=arma::max(pointsM,0);
float scale = arma::mat(pointsM-pointsm).max();
assert(scale>0);
for (int i=0;i<ops.size();++i) {
ops[i]=ops[i]-arma::repmat(0.5*(pointsm+pointsM),ops[i].n_rows,1);
ops[i]*=renderSize/scale;
}
}
arma::mat constantSpeed
(arma::mat &stroke,
float density,
int multiplier = 1) {
std::vector<float> lengths(stroke.n_rows);
lengths[0]=0;
for (int i=1;i<stroke.n_rows;i++) {
lengths[i]=lengths[i-1]+pow(pow(stroke(i,0)-stroke(i-1,0),2)+
pow(stroke(i,1)-stroke(i-1,1),2),0.5);
}
float lTotal=lengths[stroke.n_rows-1];
int n=(int)(0.5+lTotal/density);
n*=multiplier;
arma::mat r(n+1,2);
int j=0;
float alpha;
r(0,0)=stroke(0,0);
r(0,1)=stroke(0,1);
for (int i=1;i<=n;i++) {
while(n*lengths[j+1]<i*lTotal) j++;
alpha=(lengths[j+1]-i*lTotal/n)/(lengths[j+1]-lengths[j]);
r(i,0)=stroke(j,0)*alpha+stroke(j+1,0)*(1-alpha);
r(i,1)=stroke(j,1)*alpha+stroke(j+1,1)*(1-alpha);
}
return r;
}
int mapToGrid(float coord, int scale_N) {
return std::max(0,std::min(scale_N-1,(int)(coord+0.5*scale_N)));
}
int maxLength=0;
void OnlineHandwritingPicture::codifyInputData(SparseGrid &grid, std::vector<float> &features, int &nSpatialSites, int spatialSize) {
int nInputFeatures=OnlineHandwritingEncodingSize[enc];
//Background feature
grid.backgroundCol=nSpatialSites++;
features.resize(nInputFeatures*nSpatialSites,0);
//Render character
switch(enc) {
case Simple:
for (int i=0; i<ops.size(); i++) {
arma::mat csp=constantSpeed(ops[i],0.1);
for (int j=0;j<csp.n_rows;++j) {
int
a0=csp(j,0)+spatialSize/2.0,
a1=csp(j,1)+spatialSize/2.0;
if (a0>=0 and a1>=0 and a0<spatialSize and a1<spatialSize) {
int n= a0*spatialSize + a1;
if (grid.mp.find(n)==grid.mp.end()) {
grid.mp[n]=nSpatialSites++;
features.resize(nInputFeatures*nSpatialSites,0);
}
++features[grid.mp[n]];
// std::cout <<"."<<std::flush;
} //else std::cout <<"x"<<std::flush;
}
}
break;
case Octogram: {//todo: Test
float piBy4=3.1415926536/4;
for (int i=0; i<ops.size(); i++) {
arma::mat csp=constantSpeed(ops[i],0.1);
for (int j=0;j<csp.n_rows-1;++j) {
int
a0=csp(j,0)+spatialSize/2.0,
a1=csp(j,1)+spatialSize/2.0;
if (a0>=0 and a1>=0 and a0<spatialSize and a1<spatialSize) {
int n= a0*spatialSize + a1;
if (grid.mp.find(n)==grid.mp.end()) {
grid.mp[n]=nSpatialSites++;
features.resize(nInputFeatures*nSpatialSites,0);
}
int mpnnIF=nInputFeatures*grid.mp[n];
features[mpnnIF]++;
float dx=(csp(j+1,0)-csp(j,0));
float dy=(csp(j+1,1)-csp(j,1));
float m=powf(dx*dx+dy*dy,0.5);
dx/=m;
dy/=m;
for (int k=0;k<8;k++) {
float alpha=1-acosf(cosf(k*piBy4)*dx+sinf(k*piBy4)*dy)/piBy4;
if (alpha>0)
features[mpnnIF+k+1]+=alpha;
}
}
}
}
break;
}
case LogSignature1:
//Todo
break;
case LogSignature2:
//Todo
break;
case LogSignature3:
//Todo
break;
case LogSignature4:
//Todo
break;
case SpaceTime3d: {
std::vector<arma::mat> csp;
int cspLengths=0;
for (int i=0; i<ops.size(); i++) {
csp.push_back(constantSpeed(ops[i],0.1));
cspLengths+=csp[i].n_rows;
}
// if (cspLengths>maxLength) {
// std::cout <<cspLengths*penSpeed3d << " " << std::flush;
// maxLength=cspLengths;
// }
int z=0;
for (int i=0; i<ops.size(); i++) {
for (int j=0;j<csp[i].n_rows;++j) {
int
a0=csp[i](j,0)+spatialSize/2.0,
a1=csp[i](j,1)+spatialSize/2.0,
a2=renderSize*((z-cspLengths/2)*penSpeed3d+offset3d)+spatialSize/2;
z++;
if (a0>=0 and a1>=0 and a2>=0 and a0<spatialSize and a1<spatialSize and a2<spatialSize) {
int n= a0*spatialSize*spatialSize + a1*spatialSize + a2;
if (grid.mp.find(n)==grid.mp.end()) {
grid.mp[n]=nSpatialSites++;
features.resize(nInputFeatures*nSpatialSites,0);
}
++features[grid.mp[n]];
}
}
}
break;
}
case VectorSpaceTime3d: {
std::vector<arma::mat> csp;
int cspLengths=0;
for (int i=0; i<ops.size(); i++) {
csp.push_back(constantSpeed(ops[i],0.1));
cspLengths+=csp[i].n_rows;
}
// if (cspLengths>maxLength) {
// std::cout << cspLengths*penSpeed3d << " " << std::flush;
// maxLength=cspLengths;
// }
int z=0;
for (int i=0; i<ops.size(); i++) {
for (int j=0;j<csp[i].n_rows-1;++j) {
int
a0=csp[i](j,0)+spatialSize/2.0,
a1=csp[i](j,1)+spatialSize/2.0,
a2=renderSize*((z-cspLengths/2)*penSpeed3d+offset3d)+spatialSize/2;
z++;
if (a0>=0 and a1>=0 and a2>=0 and a0<spatialSize and a1<spatialSize and a2<spatialSize) {
int n= a0*spatialSize*spatialSize + a1*spatialSize + a2;
if (grid.mp.find(n)==grid.mp.end()) {
grid.mp[n]=nSpatialSites++;
features.resize(nInputFeatures*nSpatialSites,0);
}
int mpnnIF=nInputFeatures*grid.mp[n];
features[mpnnIF]++;
float dx=(csp[i](j+1,0)-csp[i](j,0));
float dy=(csp[i](j+1,1)-csp[i](j,1));
float m=powf(dx*dx+dy*dy,0.5);
dx/=m;
dy/=m;
features[mpnnIF+1]+=dx;
features[mpnnIF+2]+=dy;
}
}
}
break;
}
}
}