Statismo  0.10.1
 All Classes Namespaces Functions Typedefs
StatismoUtils.h
1 /*
2  * This file is part of the statismo library.
3  *
4  * Author: Marcel Luethi (marcel.luethi@unibas.ch)
5  *
6  * Copyright (c) 2011 University of Basel
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * Redistributions of source code must retain the above copyright notice,
14  * this list of conditions and the following disclaimer.
15  *
16  * Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in the
18  * documentation and/or other materials provided with the distribution.
19  *
20  * Neither the name of the project's author nor the names of its
21  * contributors may be used to endorse or promote products derived from
22  * this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
30  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35  *
36  */
37 
38 
39 #ifndef __UTILS_H_
40 #define __UTILS_H_
41 
42 #include <cstdlib>
43 #include <ctime>
44 
45 #include <algorithm>
46 #include <fstream>
47 #include <iostream>
48 #include <iterator>
49 
50 #ifdef _WIN32
51 #define NOMINMAX // avoid including the min and max macro
52 #include <windows.h>
53 #include <tchar.h>
54 #endif
55 
56 
57 #include <boost/random.hpp>
58 
59 #include "CommonTypes.h"
60 #include "Exceptions.h"
61 
62 namespace statismo {
63 
69 #ifdef _MSC_VER
70 #define is_deprecated __declspec(deprecated)
71 #elif defined(__GNUC__)
72 #define is_deprecated __attribute__((deprecated))
73 #else
74 #define is_deprecated //uncommon compiler, don't bother
75 #endif
76 
77 
78 class Utils {
79  public:
80 
81 
82 
86  template <class T>
87  static std::string toString(T t) {
88  std::ostringstream os;
89  os << t;
90  return os.str();
91  }
92 
93 
95  static VectorType generateNormalVector(unsigned n) {
96  // we would like to use tr1 here as well, but on some versions of visual studio it hangs forever.
97  // therefore we use the functionality from boost
98 
99  // we make the random generate static, to ensure that the seed is only set once, and not with
100  // every call
101  static boost::minstd_rand randgen(static_cast<unsigned>(time(0)));
102  static boost::normal_distribution<> dist(0, 1);
103  static boost::variate_generator<boost::minstd_rand, boost::normal_distribution<> > r(randgen, dist);
104 
105  VectorType v = VectorType::Zero(n);
106  for (unsigned i=0; i < n; i++) {
107  v(i) = r();
108  }
109  return v;
110  }
111 
112 
113  static VectorType ReadVectorFromTxtFile(const char *name) {
114  typedef std::list<statismo::ScalarType> ListType;
115  std::list<statismo::ScalarType> values;
116  std::ifstream inFile(name, std::ios::in);
117  if (inFile.good()) {
118  std::copy(std::istream_iterator<statismo::ScalarType>(inFile), std::istream_iterator<statismo::ScalarType>(), std::back_insert_iterator<ListType >(values));
119  inFile.close();
120  } else {
121  throw StatisticalModelException((std::string("Could not read text file ") + name).c_str());
122  }
123 
124  VectorType v = VectorType::Zero(values.size());
125  unsigned i = 0;
126  for (ListType::const_iterator it = values.begin(); it != values.end(); ++it) {
127  v(i) = *it;
128  i++;
129  }
130  return v;
131  }
132 
133 
134  static std::string CreateTmpName(const std::string& extension) {
135 #ifdef _WIN32
136  std::string tmpDirectoryName;
137  TCHAR szTempFileName[MAX_PATH];
138  DWORD dwRetVal = 0;
139  // Gets the temp path env string (no guarantee it's a valid path).
140  dwRetVal = GetTempPath(MAX_PATH, // length of the buffer
141  szTempFileName); // buffer for path
142  tmpDirectoryName.assign(szTempFileName);
143  std::string tmpfilename = tmpDirectoryName + "/" + tmpnam(0) +extension;
144  return tmpfilename;
145 #else
146  std::string tmpfilename = tmpnam(0);
147  tmpfilename += extension;
148  return tmpfilename;
149 #endif
150 
151  }
152 
153 };
154 
155 } // namespace statismo
156 
157 #endif /* __UTILS_H_ */