javafeature-extractionhilbert-curve

Indexing order based on Hilbert curve in Java


After two days of searching and trying I couldn't implement Hilbert Curve in java.

I need to fill (for example) 64-pixels into an square-8x8-image based on Hilbert curve. I did the same based on Zig-Zog curve, but (as I suppose to do some feature extraction) the result is not satisfying.

Result:

result

Can anybody help me out?

EDIT:

for example if I gonna write a function to return indexing of square-matrix based on Zig-Zog curve, it would be like this:

public class Source {
public static void main(String[] args) {
    int matrixWidth = 4;
    for (int col = 0; col < matrixWidth; col++){
        for (int row = 0; row < matrixWidth; row++){
            int[] temp = zigZog(row, col, matrixWidth);
            System.out.println(temp[0] + ", " + temp[1]);
        }
    }
}

// >>> zig-zog indexing
public static int[] zigZog(int row, int col, int matrixWidth){
    if (col%2 == 1){
        int[] temp = {((matrixWidth -1) - row), col};
        return temp;
    } else {
        int[] temp = {row, col};
        return temp;
    }
}
}

Solution

  • Well, it seems that nobody was interested in my Question, I have to do it on my own.

    This code blew may return the indices order, based on Hilbert-Curve which I convert it from a python code, which was provided in here (frankly it takes me a while to convert it to Java [no useful auto-converter exists out there], python is really Hard to understand, it seem like it has no structure):

    public class Source {
    public static void main(String[] args) {
        int current_square_num = 4;
        int row_num = 2;
        int col_num = 2;
        int arg_num = 2;
        char[][][][] hilbert_map = new char[current_square_num][row_num][col_num][arg_num];
    
        // >>> 'a': {(0, 0): (0, 'd'), (0, 1): (1, 'a'), (1, 0): (3, 'b'), (1, 1): (2, 'a')}
        hilbert_map[0][0][0][0] = '0';
        hilbert_map[0][0][0][1] = 'd';
    
        hilbert_map[0][0][1][0] = '1';
        hilbert_map[0][0][1][1] = 'a';
    
        hilbert_map[0][1][0][0] = '3';
        hilbert_map[0][1][0][1] = 'b';
    
        hilbert_map[0][1][1][0] = '2';
        hilbert_map[0][1][1][1] = 'a';
    
        // >>> 'b': {(0, 0): (2, 'b'), (0, 1): (1, 'b'), (1, 0): (3, 'a'), (1, 1): (0, 'c')}
        hilbert_map[1][0][0][0] = '2';
        hilbert_map[1][0][0][1] = 'b';
    
        hilbert_map[1][0][1][0] = '1';
        hilbert_map[1][0][1][1] = 'b';
    
        hilbert_map[1][1][0][0] = '3';
        hilbert_map[1][1][0][1] = 'a';
    
        hilbert_map[1][1][1][0] = '0';
        hilbert_map[1][1][1][1] = 'c';
    
        // >>> 'c': {(0, 0): (2, 'c'), (0, 1): (3, 'd'), (1, 0): (1, 'c'), (1, 1): (0, 'b')}
        hilbert_map[2][0][0][0] = '2';
        hilbert_map[2][0][0][1] = 'c';
    
        hilbert_map[2][0][1][0] = '3';
        hilbert_map[2][0][1][1] = 'd';
    
        hilbert_map[2][1][0][0] = '1';
        hilbert_map[2][1][0][1] = 'c';
    
        hilbert_map[2][1][1][0] = '0';
        hilbert_map[2][1][1][1] = 'b';
    
        // >>> 'd': {(0, 0): (0, 'a'), (0, 1): (3, 'c'), (1, 0): (1, 'd'), (1, 1): (2, 'd')}
        hilbert_map[3][0][0][0] = '0';
        hilbert_map[3][0][0][1] = 'a';
    
        hilbert_map[3][0][1][0] = '3';
        hilbert_map[3][0][1][1] = 'c';
    
        hilbert_map[3][0][1][0] = '1';
        hilbert_map[3][0][1][1] = 'd';
    
        hilbert_map[3][1][1][0] = '2';
        hilbert_map[3][1][1][1] = 'd';
    
        double order = 3;
    
        for (int col = 0; col < Math.pow(2, order); col++) {
            for (int row = 0; row < Math.pow(2, order); row++) {
                System.out.println("(" + row + ", " + col + "): " + point_to_hilbert(row, col, (int) order, hilbert_map));
            }
        }
    
    }
    
    public static int point_to_hilbert(int x, int y, int order, char[][][][] hilbert_map) {
        int position = 0;
        int current_square = 0 /* 'a' */;
    
        for (int i = 0; i < order; i++) {
            position = position << 2; // >>> position <<= 2
    
            int quad_x;
            int quad_y;
    
            if ((x & (1 << i)) != 0) { // >>> quad_x = 1 if x & (1 << i) else 0
                quad_x = 1;
            } else {
                quad_x = 0;
            }
    
            if ((y & (1 << i)) != 0) { // >>> quad_y = 1 if y & (1 << i) else 0
                quad_y = 1;
            } else {
                quad_y = 0;
            }
    
            current_square = 0;
            // >>> quad_position, current_square = hilbert_map[current_square][(quad_x, quad_y)]
            char quad_position = hilbert_map[current_square][quad_x][quad_y][0];
            current_square = Character.getNumericValue(hilbert_map[current_square][quad_x][quad_y][1]);
            // >>> position |= quad_position
            position = position | Character.getNumericValue(quad_position);
        }
    
        return position;
    }
    
    }
    

    Enjoy!