YaDICs  V04.14.a
Yet another Digital Image Correlation software: platform dedicated to 2/3D Fluid and Solid kinematics field measurements.
 All Classes Files Functions Variables Pages
Cpeak_factory.h
1 /**********************************************************************
2  * Copyright (C) 2012, The YaDICs Project Developers.
3  * See the COPYRIGHT file at the top-level directory of this distribution ./COPYRIGHT.
4  * See ./COPYING file for copying and redistribution conditions.
5  *
6  * This file is part of YaDICs.
7  * YaDICs is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * YaDICs is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with YaDICs. If not, see <http://www.gnu.org/licenses/>.
19  *
20  * Information about how to use the software are provided at http://yadic.univ-lille1.fr/
21  **********************************************************************/
22 
23 
24 #ifndef CPEAK_FACTORY
25 #define CPEAK_FACTORY
26 
27 
28 
29 //-----------------------------------------------------------------------
30 #include "Cpeak.h"
31  #include "Cpeak_max.h"
32  #include "Cpeak_subPix.h"
33 // #include "Cpeak_fit.h"
34 // #include "Cpeak_fitP.h"
35 // #include "Cpeak_fitG.h"
36  #include "Cpeak_interpBiLin.h"
37  #include "Cpeak_barycenter.h"
38  #include "Cpeak_interp.h"
39  #include "Cpeak_interpP.h"
40  #include "Cpeak_interpG.h"
41 
42 //-----------------------------------------------------------------------
43 
44 
45 template<typename T>
47 {
48 
49  public:
50 
51  //attributes
52  std::string class_name, m_peak_name, m_peak_type;
53  std::vector<std::string> m_infos;
54 
55  const char* m_paramPath;
56  bool m_verbose;
57 
58 
59  Cpeak_factory(const std::string &peak_name)
60  {
61 
62  m_peak_name = peak_name;
63  class_name="Cpeak_factory";
64 
65  m_infos.push_back("max");
66  m_infos.push_back("interpP");
67  m_infos.push_back("interpG");
68  m_infos.push_back("interpBiLin");
69  m_infos.push_back("barycenter");
70 
71  }//constructor
72 
73  Cpeak<T>* create(int type)
74  {
75 
76  Cpeak<T>* pPeak;
77 
78  switch(type)
79  {
80  case 0: pPeak = new Cpeak_max<T>; break;
81  case 1: pPeak = new Cpeak_interpP<T>; break;
82  case 2: pPeak = new Cpeak_interpG<T>; break;
83  case 3: pPeak = new Cpeak_interpBiLin<T>; break;
84  case 4: pPeak = new Cpeak_barycenter<T>; break;
85  default:
86  {
87  std::cerr<<class_name<<"::"<<__func__<<": error: serial type="<<type<<" is unknown\n"<<std::flush;
88  return NULL; break;
89  }
90  }
91 
92 // if (m_verbose){printf("%s -> %s class loaded\n",class_name.c_str(), m_peak_type.c_str());}
93 
94  pPeak->m_verbose = m_verbose;// active or not the verbose mode
95 
96  return pPeak;// return pointer to specific correlation method
97 
98  }//create
99 
100  Cpeak<T>* create(std::string type_name)
101  {
102  #if cimg_debug>1
103  std::cerr<<class_name<<"::"<<__func__<<"(\""<<type_name<<"\")\n"<<std::flush;
104  #endif
105  if(type_name==m_infos[0]) return create(0);
106  else if(type_name==m_infos[1]) return create(1);
107  else if(type_name==m_infos[2]) return create(2);
108  else if(type_name==m_infos[3]) return create(3);
109  else if(type_name==m_infos[4]) return create(4);
110  else
111  {
112  std::cerr<<class_name<<"::"<<__func__<<": error: grab type="<<type_name<<" not handled.\n"<<std::flush;
113  return NULL;
114  }
115  }//create
116 
117  Cpeak<T>* create(CParameterNetCDF &paramFile)
118  {//CDL e.g. parameter.nc
119 
120  //variable and attribute name
121  std::string attribute;
122  std::string peak_type="type";
123  load_attribute(paramFile, peak_type, attribute);
124 
125  if (m_peak_type != "none")
126  {
127  attribute = m_peak_type;
128  };
129 
130  m_peak_type = attribute;
131 
132  return create(attribute);
133 
134  }//create CParameterNetCDF
135 
136  Cpeak<T>* create(int argc, char *argv[])
137  {
138 
139  //read command line argument
140  commandLine(argc,argv);
141 
142  // pointer toward any mesh
143  Cpeak<T> *ppeak;
144  int error;
145 
146  if(m_paramPath!="true")
147  {// parameter.nc or mesh.nc location is provided
148 
149  // open the file and keep the object in file variable
150  NcFile file(m_paramPath,NcFile::ReadOnly);
151  if(m_verbose){std::cout<<" --correlator file : "<<m_paramPath<<"\n";}
152 
153  // create a CParameterNetCDF object associated to already opened file
154  CParameterNetCDF paramFile;
155  if ( error = paramFile.setFile(&file) !=1 )
156  {
157  std::cerr<< "Error while loading"<<m_paramPath<<" parameter file"<<std::endl;
158  return NULL;
159  }
160 
161  // associate pmesh pointer to the appropriate one following parameter.nc attribute type
162  ppeak = create(paramFile);
163 
164  if (ppeak == NULL){return NULL;}
165 
166  return ppeak;
167 
168  }
169  else
170  {// mesh_option = true
171 
172  std::cout<<" --mesh option : Command line\n";
173  std::cout<<" --building!!\n";
174 
175  return NULL;
176 
177  }
178 
179 
180 
181  }//create (argv,argc)
182 
183  //other methods
184  template<typename Tatt>
185  int load_attribute(CParameterNetCDF &paramFile, std::string &attribute_name, Tatt &attribute)
186  {
187 
188  //load attribute
189  int error = paramFile.loadAttribute(attribute_name,attribute, &m_peak_name);
190 
191  return error;
192 
193  }//load_attribute_name from parameter.nc file
194 
195  bool is_parameter_file(NcFile &file)
196  {
197 
198  NcVar* pVar;
199  pVar = file.get_var(m_peak_name.c_str());
200 
201  return ((pVar->num_dims())==0);
202 
203  }
204 
205  void commandLine(int argc, char *argv[])
206  {
207 
208  //set parameter file path
209  m_paramPath = cimg_option("-p","./parameters.nc","parameter, mesh or true");
210  //set verbose mode
211  m_verbose = cimg_option("-v",false,0);
212 
213  if(m_verbose)
214  {
215  printf("Mode Verbose\n");
216  printf("%s : %s\n",class_name.c_str(),m_paramPath);
217  }
218 
219  }
220 
221  void stringSplit(std::string manyString, std::vector<std::string> value)
222  {
223 
224  char *cstr,*ptr;
225  char *split=(char*)(" ,;");
226 
227  cstr = new char [manyString.size()+1];
228  strcpy(cstr,manyString.c_str());
229  ptr=strtok(cstr,split);
230  while (ptr!=NULL)
231  {
232  value.push_back(ptr);
233  ptr=strtok(NULL,split);
234  }
235  delete[] cstr;
236 
237  }
238 
239  void info()
240  {// print the whole set of available classes within the factory
241 
242  printf("\nclass %s\n",class_name.c_str());
243  for (int i=0; i<m_infos.size(); i++)
244  {
245 
246  printf("\t(%i) mode : %s\n",i,m_infos[i].c_str());
247 
248  }
249 
250  }
251 
252 
253 };//Cpeak_factory class
254 
255 
256 #endif //FACTORY
Definition: Cpeak_interpG.h:42
Definition: Cpeak_interpP.h:42
This class localizes the most important peak within an image, removes it and provide peak statistics...
Definition: Cpeak.h:47
Definition: Cpeak_factory.h:46
Definition: Cpeak_max.h:41
Definition: Cpeak_barycenter.h:43
Definition: Cpeak_interpBiLin.h:43