libatomprobe
Library for Atom Probe Tomography (APT) computation
proxigram.cpp
Go to the documentation of this file.
1 #include "atomprobe/atomprobe.h"
2 
3 
4 using namespace std;
5 using namespace AtomProbe;
6 
7 int main( int argc, char *argv[])
8 {
10 
11  const unsigned int VOXEL_COUNT=30;
12  v.resize(VOXEL_COUNT,VOXEL_COUNT,VOXEL_COUNT);
13 
14  const float VOX_SIZE=2;
15  v.setBounds(Point3D(-VOX_SIZE,-VOX_SIZE,-VOX_SIZE),
16  Point3D(VOX_SIZE,VOX_SIZE,VOX_SIZE) );
17  v.fill(0);
18 
19  //Consider two spheres at pCentreA, and pCentreB
20  // of given radius SPHERE_RAD
21  const float SPHERE_RAD=1.0f;
22  const float SPHERE_RAD_SQR=SPHERE_RAD*SPHERE_RAD;
23  const Point3D pCentreA(-0.5f,-0.5f,-0.5f);
24  const Point3D pCentreB(0.5f,0.5f,0.5f);
25 
26 
27  cerr << "Creating synthetic voxel field...";
28  for(auto ui=0;ui<VOXEL_COUNT;ui++)
29  {
30  for(auto uj=0;uj<VOXEL_COUNT;uj++)
31  {
32  for(auto uk=0;uk<VOXEL_COUNT;uk++)
33  {
34  Point3D p;
35  p=v.getPoint(ui,uj,uk);
36 
37  if(p.sqrDist(pCentreA) < SPHERE_RAD_SQR
38  || p.sqrDist(pCentreB) < SPHERE_RAD_SQR)
39  {
40  v.setData(ui,uj,uk,1);
41  }
42  }
43  }
44 
45  }
46  cerr << "Done" << endl;
47 
48  cerr << "Extracting isosurface (triangle soup)..." ;
49  vector<TriangleWithVertexNorm> surfTris;
50  marchingCubes(v,0.5,surfTris);
51  cerr << "Done (" << surfTris.size() << " triangles)" << endl;
52 
53  //FIXME: This is not needed strictly speaking
54  // but it has got advantages if we used a different algorithm
55  //Convert to mesh object
56  cerr << "Constructing mesh (deduplicating soup)...";
57  Mesh m;
58  m.nodes.resize(surfTris.size()*3);
59  m.triangles.resize(surfTris.size());
60 
61  for(auto ui=0;ui<surfTris.size();ui++)
62  {
63  for(auto uj=0;uj<3;uj++)
64  {
65  m.nodes[3*ui+ uj]=surfTris[ui].p[uj];
66  m.triangles[ui].p[uj]=3*ui+uj;
67  }
68  }
69  //Remove surf triangles, as we will now work with mesh
70  surfTris.clear();
71 
72  //Remove duplciated vertices, and fuse into a single mesh object
74  2.0f*sqrt(std::numeric_limits<float>::epsilon()));
75  cerr << "Done" << endl;
76 
77  //TODO: Optimise : split mesh into disconnected parts
78 
79  cerr << "Generating synthetic points...";
80  const unsigned int NUM_POINTS=1000;
81  vector<Point3D> randomPoints;
82  randomPoints.resize(NUM_POINTS);
83  BoundCube bc;
84  v.getBounds(bc);
85  gsl_rng *r = randGen.getRng();
86  for(auto ui=0;ui<NUM_POINTS;ui++)
87  {
88  float x,y,z;
89  x=2.0*VOX_SIZE*gsl_rng_uniform(r) -VOX_SIZE;
90  y=2.0*VOX_SIZE*gsl_rng_uniform(r) -VOX_SIZE;
91  z=2.0*VOX_SIZE*gsl_rng_uniform(r) -VOX_SIZE;
92  randomPoints[ui]=Point3D(x,y,z);
93 
94  }
95  cerr << "Done" << endl;
96 
97  cerr << "Running signed-distance computation...";
98  //Run points-inside algorithm
99  vector<bool> inside;
100 
101  //TODO: Thread me, as w/o threading, progress is not useful
102  unsigned int progress;
103  m.pointsInside(randomPoints,inside,progress);
104 
105  vector<float> distances;
106  distances.resize(randomPoints.size());
107 
108 #pragma omp parallel for
109  for(auto ui=0;ui<randomPoints.size();ui++)
110  {
111 
112  m.getNearestTri(randomPoints[ui],
113  distances[ui]);
114  if(inside[ui])
115  distances[ui]=-fabs(distances[ui]);
116  }
117 
118  const char *OUTPUT_FILE="proxigram.txt";
119  ofstream fOut(OUTPUT_FILE);
120 
121  if(!fOut)
122  {
123  cerr << "failed to open output file" << endl;
124  return 1;
125  }
126 
127  vector<float> hist;
128  const unsigned int NBINS=50;
129  float dMin,dMax;
130  dMin=*std::min_element(distances.begin(),distances.end());
131  dMax=*std::max_element(distances.begin(),distances.end());
132  float delta = (dMax-dMin)/NBINS;
133  linearHistogram(distances,dMin,dMax,delta,hist);
134 
135  for(auto ui=0;ui<hist.size();ui++)
136  fOut << ui*delta+dMin << "\t" << hist[ui] << endl;
137 
138  cerr << "Done. Output written to :" << OUTPUT_FILE << endl;
139 }
float sqrDist(const Point3D &pt) const
Returns the square of distance to another Point3D.
Definition: point3D.cpp:272
size_t resize(size_t newX, size_t newY, size_t newZ, const AtomProbe::Point3D &newMinBound=AtomProbe::Point3D(0.0f, 0.0f, 0.0f), const AtomProbe::Point3D &newMaxBound=AtomProbe::Point3D(1.0f, 1.0f, 1.0f))
Resize the data field.
Definition: voxels.h:835
void getBounds(AtomProbe::Point3D &pMin, AtomProbe::Point3D &pMax) const
Get the bounding size.
Definition: voxels.h:252
STL namespace.
Template class that stores 3D voxel data.
Definition: voxels.h:106
Simple mesh class for storing and manipulating meshes consisting of 2 and 3D simplexes (triangles or ...
Definition: mesh.h:78
size_t getNearestTri(const Point3D &p, float &distance) const
Find the nearest triangle to a particular point.
Definition: mesh.cpp:2126
void fill(const T &val)
Fill all voxels with a given value.
Definition: voxels.h:1536
A 3D point data class storage.
Definition: point3D.h:39
AtomProbe::Point3D getPoint(size_t x, size_t y, size_t z) const
Get the position associated with an XYZ voxel.
Definition: voxels.h:470
std::vector< Point3D > nodes
Point storage for 3D Data (nodes/coords/vertices..)
Definition: mesh.h:110
void setBounds(const AtomProbe::Point3D &pMin, const AtomProbe::Point3D &pMax)
Set the bounding size.
Definition: voxels.h:961
void linearHistogram(const std::vector< T > &data, T start, T end, T step, std::vector< T > &histVals)
Definition: histogram.h:75
int main(int argc, char *argv[])
Definition: proxigram.cpp:7
Helper class to define a bounding cube.
Definition: boundcube.h:29
void setData(size_t x, size_t y, size_t z, const T &val)
Retrieve a reference to the data ata given position.
Definition: voxels.h:435
void mergeDuplicateVertices(float tolerance)
Definition: mesh.cpp:738
unsigned int progress
Definition: kd-example.cpp:26
void pointsInside(const std::vector< Point3D > &p, std::vector< bool > &meshResults, unsigned int &prog) const
Find the points that lie inside a this mesh.
Definition: mesh.cpp:1989
gsl_rng * getRng() const
Obtain a GSL random number generator.
Definition: atomprobe.h:115
std::vector< TRIANGLE > triangles
Storage for node connectivity in triangle form (take in groups of 3)
Definition: mesh.h:121
RandNumGen randGen
Definition: atomprobe.cpp:29
void marchingCubes(const Voxels< float > &v, float isoValue, std::vector< TriangleWithVertexNorm > &tVec)
Definition: isoSurface.cpp:494