~ubuntu-branches/ubuntu/saucy/3depict/saucy

« back to all changes in this revision

Viewing changes to src/common/voxels.cpp

  • Committer: Package Import Robot
  • Author(s): D Haley
  • Date: 2013-05-17 00:52:39 UTC
  • mfrom: (3.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20130517005239-7bl4mnhkvrhc2ba6
Tags: 0.0.13-1
Upload to unstable 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * voxels.cpp - Voxelised data manipulation class 
 
3
 * Copyright (C) 2013  D. Haley
 
4
 * 
 
5
 * This program is free software: you can redistribute it and/or modify
 
6
 * it under the terms of the GNU General Public License as published by
 
7
 * the Free Software Foundation, either version 3 of the License, or
 
8
 * (at your option) any later version.
 
9
 * 
 
10
 * This program is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 * GNU General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU General Public License
 
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
17
 */
 
18
#include "voxels.h"
 
19
 
 
20
using std::numeric_limits;
 
21
const float FLOAT_SMALL=
 
22
        sqrt(numeric_limits<float>::epsilon());
 
23
 
 
24
bool testConvolve()
 
25
{
 
26
        {
 
27
        Voxels<unsigned int> data,kernel,result;
 
28
        data.setCallbackMethod(dummyCallback);
 
29
        kernel.setCallbackMethod(dummyCallback);
 
30
 
 
31
        const size_t NUM_SIZES=4;
 
32
        const size_t TEST_SIZES[]= { 1, 3, 4, 7};
 
33
 
 
34
        for(size_t ui=0;ui<BOUND_ENUM_END; ui++)
 
35
        {
 
36
                //Convolve several kernel sizes
 
37
                //---
 
38
                for(unsigned int ui=0;ui<NUM_SIZES;ui++)
 
39
                {
 
40
 
 
41
                        size_t curSize;
 
42
                        curSize=TEST_SIZES[ui];
 
43
 
 
44
                        data.resize(curSize,curSize,curSize);
 
45
                        kernel.resize(curSize,curSize,curSize);
 
46
 
 
47
                        //Test with a whole bunch of different fill values
 
48
                        for(size_t fillVal=0;fillVal<5;fillVal++)
 
49
                        {
 
50
                                data.fill(fillVal);
 
51
                                kernel.fill(fillVal);
 
52
 
 
53
                                data.convolve(kernel,result,BOUND_CLIP);
 
54
 
 
55
                                size_t nX,nY,nZ;
 
56
                                result.getSize(nX,nY,nZ);
 
57
 
 
58
                                TEST(nX == 1,"convolve dimensions");
 
59
                                TEST(nY == 1,"convolve dimensions");
 
60
                                TEST(nZ == 1,"convolve dimensions");
 
61
 
 
62
                                TEST(result.max() == curSize*curSize*curSize*(fillVal*fillVal),
 
63
                                                "Convolve maxima");
 
64
                        }
 
65
                }
 
66
                //--
 
67
        
 
68
                kernel.resize(1,1,1);
 
69
                //Convolve several kernel sizes
 
70
                for(unsigned int ui=0;ui<NUM_SIZES;ui++)
 
71
                {
 
72
 
 
73
                        size_t curSize;
 
74
                        curSize=TEST_SIZES[ui];
 
75
 
 
76
                        data.resize(curSize,curSize,curSize);
 
77
                        data.fill(1);
 
78
                        kernel.fill(1);
 
79
 
 
80
                        data.convolve(kernel,result,BOUND_CLIP);
 
81
                        TEST(result == data, "convolve identity");
 
82
                }
 
83
                kernel.resize(3,3,3);
 
84
        }
 
85
        }
 
86
 
 
87
        //Test integral stuff
 
88
        {
 
89
                Voxels<float> data;
 
90
                data.resize(3,3,3);
 
91
                data.fill(1);
 
92
                TEST(fabs(data.trapezIntegral() - 1.0f )< FLOAT_SMALL,"Trapezoid test");
 
93
 
 
94
                data.resize(5,5,5);
 
95
                data.fill(1);
 
96
                data.setBounds(Point3D(0,0,0),Point3D(1,1,1));
 
97
                TEST(fabs(data.trapezIntegral() - 1.0f) < FLOAT_SMALL,"Trapezoid test");
 
98
                data.setBounds(Point3D(0,0,0),Point3D(5,5,5));
 
99
                TEST(fabs(data.trapezIntegral() - 125.0f) < FLOAT_SMALL,"Trapezoid test");
 
100
        }
 
101
 
 
102
        //Test convolution stuff
 
103
        {
 
104
        Voxels<float> data,kernel,result;
 
105
        data.setCallbackMethod(dummyCallback);
 
106
        kernel.setCallbackMethod(dummyCallback);
 
107
        //Check that convolving with an impulse with 
 
108
        //a Gaussian  gives us a Gaussian 
 
109
        //back, roughly speaking
 
110
        kernel.setGaussianKernelCube(1.0f,10.0f,10);
 
111
 
 
112
        float trapz = kernel.trapezIntegral();
 
113
        TEST(trapz < 1.5f && trapz > 0.5f, "Trapezoidal kernel integral test");
 
114
        
 
115
 
 
116
        data.resize(20,20,20);
 
117
        data.fill(0.0f);
 
118
        data.setData(10,10,10,1.0f);
 
119
        data.convolve(kernel,result,BOUND_CLIP);
 
120
 
 
121
        TEST(result.max() >  0 && result.max() < 1.0f,"result should be nonzero, and less than the original input (convolve only squeezes maxima/minima)");
 
122
        //Gaussian @ x=0, stdev 1
 
123
        TEST(fabs(result.max() - kernel.max())  < FLOAT_SMALL
 
124
                ,"Gaussian kernel test- maxima of convolved and kernel should be the same");
 
125
        }
 
126
 
 
127
        return true;
 
128
}
 
129
 
 
130
bool simpleMath()
 
131
{
 
132
        Voxels<float> a,b,c;
 
133
        a.resize(3,3,3);
 
134
        a.fill(2.0f);
 
135
 
 
136
        float f;
 
137
        f=a.getSum();
 
138
        TEST(fabs(f-3.0*3.0*3.0*2.0 )< FLOAT_SMALL,"getsum test");
 
139
        TEST(fabs(a.count(1.0f)- 3.0*3.0*3.0) < FLOAT_SMALL,"Count test"); 
 
140
 
 
141
        return true;
 
142
}
 
143
 
 
144
bool basicTests()
 
145
{
 
146
        Voxels<float> f;
 
147
        f.setCallbackMethod(dummyCallback);
 
148
        f.resize(3,3,3);
 
149
        
 
150
        size_t xs,ys,zs;
 
151
        f.getSize(xs,ys,zs);
 
152
        TEST(xs == ys && ys == zs && zs == 3,"resize tests");
 
153
        
 
154
 
 
155
 
 
156
        f.fill(0);
 
157
        f.setData(1,1,1,1.0f);
 
158
 
 
159
        TEST(fabs(f.max() - 1.0f) < FLOAT_SMALL,"Fill and data set");
 
160
 
 
161
        f.resizeKeepData(2,2,2);
 
162
        f.getSize(xs,ys,zs);
 
163
 
 
164
        TEST(xs == ys && ys == zs && zs == 2, "resizeKeepData");
 
165
        TEST(f.max() == 1.0f,"resize keep data");
 
166
        
 
167
        return true;
 
168
}
 
169
 
 
170
 
 
171
bool runVoxelTests()
 
172
{
 
173
        TEST(basicTests(),"basic voxel tests");
 
174
        TEST(testConvolve()," voxel convolve");
 
175
        TEST(simpleMath(), "voxel simple maths");       
 
176
        return true;    
 
177
}