#ifndef NNLAYER2_CONV_INC_CPP
#define NNLAYER2_CONV_INC_CPP
#include "nnlayer2.inc.cpp"
Layer* createConv(Layer &prev, int psx, int psy, int psz, int sx, int sy, int sz, int lsize) {
struct Point {
int x, y, r;
inline bool operator<(const Point &b) const { return r < b.r; }
};
static const int hs = 256;
static const int s = hs*2 + 1;
static const int rnd[] = { 9, 12, 4, 6, 0, 15, 13, 8, 2, 3, 10, 1, 5, 11, 14, 7 };
static std::vector<Point> points;
if (points.empty()) {
points.resize(s*s);
Point *p = points.data();
for(int y = -hs, r = 0; y <= hs; ++y)
for(int x = -hs; x <= hs; ++x, ++r, ++p)
{ p->x = x, p->y = y, p->r = (x*x + y*y)*16 + rnd[r%16]; }
std::sort(points.begin(), points.end());
}
Layer &pl = prev.back();
assert(psx > 0 && psy > 0 && psz > 0);
assert(sx > 0 && sy > 0 && sz > 0);
assert(psx*psy*psz == pl.size);
assert(lsize > 0 && lsize <= psx*psy);
Layer &cl = *new Layer(&pl, sx*sy*sz, lsize*psz);
assert(cl.size && cl.lsize);
const Point *pb = points.data();
int *order = new int[lsize];
for(int y = 0; y < sy; ++y) {
for(int x = 0; x < sx; ++x) {
int cx = (int)((x + 0.5)/(sx + 1)*(psx + 1));
int cy = (int)((y + 0.5)/(sy + 1)*(psy + 1));
const Point *p = pb;
for(int l = 0; l < lsize; ++l) {
int px, py;
do { assert(p < pb + points.size()); px = cx + p->x; py = cy + p->y; ++p; }
while(px < 0 || py < 0 || px >= psx || py >= psy);
order[l] = py*psx + px;
}
std::sort(order, order + lsize);
for(int z = 0; z < sz; ++z) {
for(int l = 0; l < lsize; ++l) {
for(int pz = 0; pz < psz; ++pz) {
int i = (((y*sx + x)*sz + z)*lsize + l)*psz + pz;
int pi = order[l]*psz + pz;
assert(i >= 0 && i < cl.size*cl.lsize);
assert(pi >= 0 && pi < pl.size);
cl.links[i].nprev = pi;
}
}
}
}
}
delete[] order;
cl.prepareBackLinks();
return &cl;
}
#endif