libatomprobe
Library for Atom Probe Tomography (APT) computation
voxels.cpp
Go to the documentation of this file.
1 /*
2  * voxels.cpp - Voxelised data manipulation class
3  * Copyright (C) 2018, 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  */
19 
20 #include <utility>
21 
22 #include <gsl/gsl_matrix.h>
23 #include <gsl/gsl_vector.h>
24 #include <gsl/gsl_blas.h>
25 
26 using std::vector;
27 using std::pair;
28 using std::numeric_limits;
29 
30 
31 
32 namespace AtomProbe{
33 
34 
35 #ifdef DEBUG
36 #include <algorithm>
37 
38 const float FLOAT_SMALL=
39  sqrt(numeric_limits<float>::epsilon());
40 
41 bool simpleMath()
42 {
43  Voxels<float> a,b,c;
44  a.resize(3,3,3);
45  a.fill(2.0f);
46 
47  float f;
48  f=a.getSum();
49  TEST(fabs(f-3.0*3.0*3.0*2.0 )< FLOAT_SMALL,"getsum test");
50  TEST(fabs(a.count(1.0f)- 3.0*3.0*3.0) < FLOAT_SMALL,"Count test");
51 
52  return true;
53 }
54 
55 bool basicTests()
56 {
57  Voxels<float> f;
58  f.resize(3,3,3);
59 
60  size_t xs,ys,zs;
61  f.getSize(xs,ys,zs);
62  TEST(xs == ys && ys == zs && zs == 3,"resize tests");
63 
64 
65 
66  f.fill(0);
67  f.setData(1,1,1,1.0f);
68 
69  TEST(fabs(f.max() - 1.0f) < FLOAT_SMALL,"Fill and data set");
70 
71  f.resizeKeepData(2,2,2);
72  f.getSize(xs,ys,zs);
73 
74  TEST(xs == ys && ys == zs && zs == 2, "resizeKeepData");
75  TEST(f.max() == 1.0f,"resize keep data");
76 
77  //Test slice functions
78  //--
79  Voxels<float> v;
80  v.resize(2,2,2);
81  for(size_t ui=0;ui<8;ui++)
82  v.setData(ui&1, (ui & 2) >> 1, (ui &4)>>2, ui);
83 
84  auto slice = new float[4];
85  //Test Z slice
86  v.getSlice(2,0,slice);
87  for(size_t ui=0;ui<4;ui++)
88  {
89  ASSERT(slice[ui] == ui);
90  }
91 
92  //Expected results
93  float expResults[4];
94  //Test X slice
95  expResults[0]=0; expResults[1]=2;expResults[2]=4; expResults[3]=6;
96  v.getSlice(0,0,slice);
97  for(size_t ui=0;ui<4;ui++)
98  {
99  ASSERT(slice[ui] == expResults[ui]);
100  }
101 
102  //Test Y slice
103  v.getSlice(1,1,slice);
104  expResults[0]=2; expResults[1]=3;expResults[2]=6; expResults[3]=7;
105  for(size_t ui=0;ui<4;ui++)
106  {
107  ASSERT(slice[ui] == expResults[ui]);
108  }
109 
110  delete[] slice;
111 
112  //-- try again with nonuniform voxels
113  v.resize(4,3,2);
114  for(size_t ui=0;ui<24;ui++)
115  v.setData(ui, ui);
116 
117  slice = new float[12];
118  //Test Z slice
119  v.getSlice(2,1,slice);
120  for(size_t ui=0;ui<12;ui++)
121  {
122  ASSERT( slice[ui] >=12);
123  }
124 
125  delete[] slice;
126  //--
127 
128  return true;
129 }
130 
131 
132 bool interpTests()
133 {
134  Voxels<float> v;
135  v.resize(3,3,4);
136  v.setBounds(Point3D(0,0,0),Point3D(3,3,4));
137  for(unsigned int ui=0;ui<3;ui++)
138  {
139  for(unsigned int uj=0;uj<3;uj++)
140  {
141  v.setData(ui,uj,0,1);
142  v.setData(ui,uj,1,0);
143  v.setData(ui,uj,2,-2);
144  v.setData(ui,uj,3,-1);
145  }
146  }
147 
148 
149  Point3D p(1.5,1.5,0);
150  const unsigned int NSTEP=30;
151  for(unsigned int ui=0;ui<NSTEP;ui++)
152  {
153  p[2] = (float)ui/(float)NSTEP*4.0f;
154  float interpV;
155  v.getInterpolatedData(p,interpV);
156 
157  TEST(interpV <= 1 && interpV >= -2, "interp test");
158  }
159 
160  return true;
161 }
162 
163 
164 bool runVoxelTests()
165 {
166  TEST(basicTests(),"basic voxel tests");
167  TEST(simpleMath(), "voxel simple maths");
168  TEST(interpTests(), "voxel simple maths");
169 
170  return true;
171 }
172 
173 #endif
174 }
#define TEST(f, g)
Definition: aptAssert.h:49
#define ASSERT(f)