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