00001 #include "../headers/testQuadtrees.h"
00002
00003
00004
00005
00006
00007
00008 #include <iostream>
00009 using namespace std;
00010
00011 #include "../headers/Order2StatisticalConcept.h"
00012 #include "../headers/NaiveQuadtree.h"
00013 #include "../headers/AreaSumTableQuadtree.h"
00014 #include "../headers/SimpleQuadtree.h"
00015 #include "../headers/AugmentedAreaSumTableQuadtree.h"
00016
00017 #include "../headers/dmath.h"
00018
00019 using namespace za_co_codespot::datastructures;
00020
00021 DECLARE_CLOCK(clock1)
00022
00023 typedef int TestType;
00024
00025
00026 void testQuadtreeArchetypes()
00027 {
00028 Order2StatisticalArchetype<TestType> ar[32*32];
00029 Order2StatisticalArchetype<TestType> threshold;
00030
00031 NaiveQuadtree <Order2StatisticalArchetype<TestType>>(ar, 32, 32, threshold);
00032 }
00033
00034
00035
00036
00037 void testReflectionInvariance()
00038 {
00039 const unsigned int width(64);
00040 const unsigned int height(128);
00041 const TestType MAX_VAL(255);
00042 TestType threshold(20);
00043
00044 TestType * ar1 = new TestType[width*height];
00045 TestType * ar2 = new TestType[width*height];
00046 TestType * ar3 = new TestType[width*height];
00047 TestType * ar4 = new TestType[width*height];
00048
00049 for (unsigned int x = 0; x < width; x++)
00050 {
00051 for(unsigned int y = 0; y < height; y++)
00052 {
00053 TestType val = (TestType)(MAX_VAL * ((TestType) rand()) / RAND_MAX);
00054
00055 ar1[y*width + x] = val;
00056 ar2[x*height + y] = val;
00057 ar3[y*width + (width - x - 1)] = val;
00058 ar4[(height - y - 1) * width + x] = val;
00059 }
00060 }
00061
00062 TestType sum1 = 0;
00063 TestType sum2 = 0;
00064 TestType sum3 = 0;
00065
00066 NaiveQuadtree<TestType> quadtree1(ar1, width, height, threshold);
00067 NaiveQuadtree<TestType> quadtree2(ar2, height, width, threshold);
00068 NaiveQuadtree<TestType> quadtree3(ar3, width, height, threshold);
00069 NaiveQuadtree<TestType> quadtree4(ar4, width, height, threshold);
00070
00071
00072 for (unsigned int x = 0; x < width; x++)
00073 {
00074 for(unsigned int y = 0; y < height; y++)
00075 {
00076 sum1 += abs(quadtree1(x, y) - quadtree2(y, x));
00077 sum2 += abs(quadtree1(x, y) - quadtree3(width - x - 1, y));
00078
00079 if (sum2 > 0)
00080 {
00081 sum3 = 0;
00082 }
00083
00084 sum3 += abs(quadtree1(x, y) - quadtree4(x, height - y - 1));
00085 }
00086 }
00087
00088 double error1((double) sum1 / (width * height));
00089 double error2((double) sum2 / (width * height));
00090 double error3((double) sum3 / (width * height));
00091
00092 cout << "Error per pixel: " << error1 << " [XY Reflection]" << endl;
00093 cout << "Error per pixel: " << error2 << " [X Reflection]" << endl;
00094 cout << "Error per pixel: " << error3 << " [Y Reflection]" << endl;
00095
00096 cout << endl;
00097 }
00098
00099
00100 void testQuadtreeEquivalence()
00101 {
00102 const unsigned int width(100);
00103 const unsigned int height(100);
00104 const TestType MAX_VAL(255);
00105 TestType threshold(0);
00106
00107 TestType * ar = new TestType[width*height];
00108
00109 for (unsigned int i = 0; i < width*height; i++)
00110 {
00111 ar[i] = (TestType)(MAX_VAL * ((TestType) rand()) / RAND_MAX);
00112 }
00113
00114 NaiveQuadtree<TestType> quadtree1(ar, width, height, threshold);
00115 SimpleQuadtree<TestType> quadtree2(ar, width, height, threshold);
00116 AreaSumTableQuadtree<TestType> quadtree3(ar, width, height, threshold);
00117 AugmentedAreaSumTableQuadtree<TestType> quadtree4(ar, width, height, threshold);
00118
00119 cout << "Error per pixel: " << error(quadtree1, quadtree1) << " Naive - Naive" << endl;
00120 cout << "Error per pixel: " << error(quadtree1, quadtree2) << " Naive - Simple" << endl;
00121 cout << "Error per pixel: " << error(quadtree1, quadtree3) << " Naive - Area Sum Table" << endl;
00122 cout << "Error per pixel: " << error(quadtree1, quadtree4) << " Naive - Augmented Area Sum Table" << endl;
00123
00124 cout << endl;
00125 }
00126
00127
00128
00129 void testQuadtreeSpeed()
00130 {
00131 const unsigned int step(400);
00132 const unsigned int start(32);
00133 const unsigned int stop((1 << 10) + 1);
00134 const TestType MAX_VAL((1 << 5) - 1);
00135
00136 TestType threshold(0);
00137
00138 cout << "Simple" << endl;
00139 srand(100);
00140
00141 for (int k = start; k < stop; k*=2)
00142 {
00143 const unsigned int width(k);
00144 const unsigned int height(k);
00145
00146 TestType * ar = new TestType[width*height];
00147
00148 for (unsigned int i = 0; i < width*height; i++)
00149 ar[i] = (TestType)(MAX_VAL * ((TestType) rand()) / RAND_MAX);
00150
00151 START_CLOCK(clock1, 10)
00152 SimpleQuadtree<TestType> simpleQuadtree(ar, width, height, threshold);
00153 STOP_CLOCK(clock1)
00154
00155
00156
00157 delete [] ar;
00158 }
00159
00160 cout << endl;
00161
00162 cout << "Naive" << endl;
00163 srand(100);
00164
00165 for (int k = start; k < stop; k*=2)
00166 {
00167 const unsigned int width(k);
00168 const unsigned int height(k);
00169
00170
00171 TestType * ar = new TestType[width*height];
00172
00173 for (unsigned int i = 0; i < width*height; i++)
00174 ar[i] = (TestType)(MAX_VAL * ((TestType) rand()) / RAND_MAX);
00175
00176
00177 START_CLOCK(clock1, 10)
00178 NaiveQuadtree<TestType> quadtree(ar, width, height, threshold);
00179 STOP_CLOCK(clock1)
00180
00181
00182 delete [] ar;
00183 }
00184
00185 cout << endl;
00186
00187 cout << "Area Sum Table" << endl;
00188 srand(100);
00189
00190 for (int k = start; k < stop; k*=2)
00191 {
00192 const unsigned int width(k);
00193 const unsigned int height(k);
00194
00195 TestType * ar = new TestType[width*height];
00196
00197 for (unsigned int i = 0; i < width*height; i++)
00198 ar[i] = (TestType)(MAX_VAL * ((TestType) rand()) / RAND_MAX);
00199
00200 START_CLOCK(clock1, 10)
00201 AreaSumTableQuadtree<TestType> fastQuadtree(ar, width, height, threshold);
00202 STOP_CLOCK(clock1)
00203
00204
00205 delete [] ar;
00206 }
00207
00208 cout << endl;
00209
00210 cout << "Augmented Area Sum Table" << endl;
00211 srand(100);
00212
00213
00214 for (int k = start; k < stop; k*=2)
00215 {
00216 const unsigned int width(k);
00217 const unsigned int height(k);
00218
00219 TestType * ar = new TestType[width*height];
00220
00221 for (unsigned int i = 0; i < width*height; i++)
00222 ar[i] = (TestType)(MAX_VAL * ((TestType) rand()) / RAND_MAX);
00223
00224 START_CLOCK(clock1, 10)
00225 AugmentedAreaSumTableQuadtree<TestType> fastQuadtree2(ar, width, height, threshold);
00226 STOP_CLOCK(clock1)
00227
00228
00229 delete [] ar;
00230 }
00231 }
00232
00233 void testQuadtrees()
00234 {
00235 testReflectionInvariance();
00236 testQuadtreeArchetypes();
00237 testQuadtreeEquivalence();
00238 testQuadtreeSpeed();
00239 }