]> mj.ucw.cz Git - libucw.git/blob - images/hilbert-test.c
configurable border bonuses for signature computation
[libucw.git] / images / hilbert-test.c
1 /* Tests for multidimensional Hilbert curves */
2
3 #define LOCAL_DEBUG
4
5 #include "lib/lib.h"
6 #include "lib/mempool.h"
7 #include "lib/math.h"
8 #include <stdlib.h>
9 #include <stdio.h>
10
11 static struct mempool *pool;
12
13 static uns dim;
14 static uns order;
15
16 static inline void
17 rand_vec(uns *vec)
18 {
19   for (uns i = 0; i < dim; i++)
20     vec[i] = (uns)rand() >> (32 - order);
21 }
22
23 static byte *
24 print_vec(uns *vec)
25 {
26   byte *s = mp_alloc(pool, dim * 16), *res = s;
27   *s++ = '(';
28   for (uns i = 0; i < dim; i++)
29     {
30       if (i)
31         *s++ = ' ';
32       s += sprintf(s, "%x", vec[i]);
33     }
34   *s++ = ')';
35   *s = 0;
36   return res;
37 }
38
39 static inline int
40 cmp_vec(uns *vec1, uns *vec2)
41 {
42   for (uns i = dim; i--; )
43     if (vec1[i] < vec2[i])
44       return -1;
45     else if (vec1[i] > vec2[i])
46       return 1;
47   return 0;
48 }
49
50 #if 0
51 static long double
52 param_dist(uns *vec1, uns *vec2)
53 {
54   long double d1 = 0, d2 = 0;
55   for (uns i = 0; i < dim; i++)
56     {
57       d1 = (d1 + vec1[i]) / ((u64)1 << order);
58       d2 = (d2 + vec2[i]) / ((u64)1 << order);
59     }
60   return fabsl(d1 - d2);
61 }
62
63 static long double
64 vec_dist(uns *vec1, uns *vec2)
65 {
66   long double d = 0;
67   for (uns i = 0; i < dim; i++)
68     {
69       long double x = fabsl(vec1[i] - vec2[i]) / ((u64)1 << order);
70       d += x * x;
71     }
72   return sqrtl(d);
73 }
74 #endif
75
76 #define HILBERT_PREFIX(x) test1_##x
77 #define HILBERT_DIM dim
78 #define HILBERT_ORDER order
79 #define HILBERT_WANT_DECODE
80 #define HILBERT_WANT_ENCODE
81 #include "images/hilbert.h"
82
83 static void
84 test1(void)
85 {
86   uns a[32], b[32], c[32];
87   for (dim = 2; dim <= 8; dim++)
88     for (order = 8; order <= 32; order++)
89       for (uns i = 0; i < 1000; i++)
90         {
91           rand_vec(a);
92           test1_encode(b, a);
93           test1_decode(c, b);
94           if (cmp_vec(a, c))
95             die("Error... dim=%d order=%d testnum=%d ... %s -> %s -> %s", 
96                 dim, order, i, print_vec(a), print_vec(b), print_vec(c));
97         }
98 }
99
100 #if 0
101 #include "images/hilbert-origin.h"
102 static void
103 test_origin(void)
104 {
105   Hcode code;
106   Point pt, pt2;
107   pt.hcode[0] = 0x12345678;
108   pt.hcode[1] = 0x654321;
109   pt.hcode[2] = 0x11122233;
110   code = H_encode(pt);
111   pt2 = H_decode(code);
112   DBG("origin: [%08x, %08x, %08x] --> [%08x, %08x %08x] --> [%08x, %08x %08x]", 
113     pt.hcode[0], pt.hcode[1], pt.hcode[2], code.hcode[0], code.hcode[1], code.hcode[2], pt2.hcode[0], pt2.hcode[1], pt2.hcode[2]);
114 }
115 #endif
116
117 int
118 main(int argc UNUSED, char **argv UNUSED)
119 {
120   pool = mp_new(1 << 16);
121   test1();
122   //test_origin();
123   return 0;
124 }