libatomprobe
Library for Atom Probe Tomography (APT) computation
reconstruct.cpp
Go to the documentation of this file.
1 /* reconstruct.cpp : Sample reconstruction program
2  * Copyright (C) 2015 Daniel Haley
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 #include <iostream>
18 
19 #include "atomprobe/atomprobe.h"
20 
21 using namespace std;
22 using namespace AtomProbe;
23 
24 
25 int main( int argc, char *argv[])
26 {
27  string filename;
28  if(argc ==1)
29  {
30  cout << "Enter file name" << endl;
31 
32  cin >> filename;
33  }
34  else if(argc ==2)
35  filename=argv[1];
36  else
37  {
38  cerr<< "USAGE: " << argv[0] << " [FILENAME]" << endl;
39  return 1;
40  }
41 
42  vector<vector<float> > data;
43  vector<string> headers;
44  cerr << "Loading :" << filename << endl;
45  if(loadTextData(filename.c_str(),data,headers,"\t",true,true,1))
46  {
47  cerr << " Failed to load text data" << endl;
48  return 1;
49  }
50 
51  //Find the header that is det_x, det_y
52  vector<string> search = { "det_x","det_y"};
53  vector<unsigned int> offset;
54  offset.resize(search.size(),(unsigned int)-1);
55  for(auto ui=0u; ui<headers.size();ui++)
56  {
57  auto s = std::find(search.begin(),search.end(),
58  stripWhite(headers[ui]));
59 
60  if(s != search.end())
61  offset[distance(search.begin(),s)] = ui;
62  }
63 
64  for(auto h : headers)
65  cerr << "Header :" << h << endl;
66 
67  if(find(offset.begin(),offset.end(),-1) != offset.end())
68  {
69  cerr << "Unable to locate det_x or det_y field" << endl;
70  return 1;
71  }
72 
73 
74  vector<float> detX,detY;
75  detX.swap(data[offset[0]]);
76  detY.swap(data[offset[1]]);
77  data.clear();
78 
79  cerr << "Loaded :" << detX.size() << " lines." << endl;
80  cerr << "Loaded :" << detY.size() << " lines." << endl;
81  float detBounds[2][2];
82  detBounds[0][0]=*std::min_element(detX.begin(),detX.end());
83  detBounds[1][0]=*std::min_element(detY.begin(),detY.end());
84  detBounds[0][1]=*std::max_element(detX.begin(),detX.end());
85  detBounds[1][1]=*std::max_element(detY.begin(),detY.end());
86 
87  cerr << "Pre-cut" <<endl;
88  cerr << detBounds[0][0]<< "<->" << detBounds[0][1] << endl;
89  cerr << detBounds[1][0]<< "<->" << detBounds[1][1] << endl;
90 
91 
92 
93  const float DET_RADIUS=0.04; //meters
94  cerr << "Trimming to radius :" << DET_RADIUS << endl;
95  vector<float> trimX,trimY;
96  for(auto ui=0u;ui<detX.size();ui++)
97  {
98  if(detX[ui]*detX[ui] + detY[ui]*detY[ui] < DET_RADIUS*DET_RADIUS)
99  {
100  trimX.push_back(detX[ui]);
101  trimY.push_back(detY[ui]);
102  }
103  }
104 
105  detX.swap(trimX);
106  detY.swap(trimY);
107 
110 
111  std::vector<float> ionV,tof; //Ion volumes
112 
113  //Create initial parameters
114  r.setInitialRadius(35);
115  r.setProjModel(&model); //Use the Modified Stereographic model
116  r.setReconFOV(atan(DET_RADIUS/0.12)); //Set the 1/2 FOV
117  r.setFlightPath(0.12); //Set flight path
118  r.setShankAngle(15.2*M_PI/180.0f); //Angle as measured from recon image
119  r.setDetectorEfficiency(1); //Set efficiency
120 
121  //make space for ion volumes & TOF
122  ionV.resize(detX.size());
123  tof.resize(detX.size());
124 
125  //OK, so now we know which column we want
126  using AtomProbe::IonHit;
127 
128  std::vector<IonHit> pts;
129 
130  bool errorRes;
131  errorRes = r.reconstruct(detX,detY,tof,ionV,pts);
132 
133  if(!errorRes)
134  {
135  cerr << "Reconstruction failed for reasons" << endl;
136  }
137  else
138  {
139  cerr << "Successfully reconstructed!" << endl;
140  if(savePosFile(pts,"reconstructed.pos"))
141  {
142  cerr << "Error writing reconstruction to file" << endl;
143  }
144  }
145 }
void setFlightPath(float detEff)
Set flight path. Units must match detector units (eg mm)
void setReconFOV(float angle)
Set the angle (between 0 and pi radiaans). This is.
int main(int argc, char *argv[])
Definition: reconstruct.cpp:25
STL namespace.
std::string stripWhite(const std::string &str)
Strip whitespace, (eg tab,space) from either side of a string.
unsigned int loadTextData(const char *cpFilename, std::vector< std::vector< float > > &dataVec, std::vector< std::string > &headerVec, const char *delim="\", bool allowNan=true, bool allowConvFails=false, unsigned int headerCount=-1)
Load a CSV, TSV or similar text file. Assumes "C" Locale for input (ie "." as decimal separator)...
Definition: dataFiles.cpp:1504
unsigned int savePosFile(const std::vector< Point3D > &points, float mass, const char *name, bool append=false)
Save a vector of Point3Ds into a pos file, using a fixed mass, return nonzero on error.
Definition: dataFiles.cpp:499
#define M_PI
Definition: kd-example.cpp:34
bool reconstruct(const std::vector< float > &detectorX, const std::vector< float > &detectorY, const std::vector< float > &timeOfFlight, const std::vector< float > &ionVolume, std::vector< IonHit > &outputPts) const
Using the current reconstruction model, reconstruct a detector sequence.
This is a data holding class for POS file ions, from.
Definition: ionHit.h:36
void setInitialRadius(float rInit)
Set the initial tip radius.
void setShankAngle(float angle)
Set initial angle, in radiians.
void setDetectorEfficiency(float detEff)
Set detector efficiency in range (0,1].
void setProjModel(AtomProbe::SphericPlaneProjection *model)
Set a given projection model.