1 /*
2 * Copyright (C) 2005 Andreas Volz
3 * Copyright (C) 2006-2007 MakeHuman Project
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 3 of
8 * the License, or (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 GNU
13 * 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, write to the Free Software Foun-
17 * dation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 *
20 * File: util.h
21 * Project: MakeHuman <info@makehuman.org>, http://www.makehuman.org/
22 * Library: ANIMORPH
23 *
24 * For individual developers look into the AUTHORS file.
25 *
26 */
27
28 #ifndef UTIL_H
29 #define UTIL_H 1
30
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 #include <string>
36 #include <sstream>
37 #include <iomanip>
38 #include <vector>
39 #include <iostream>
40 #include <cstdlib> // for atoi()
41 #include "Vector3.h"
42 #include "Vertex.h"
43 #include "VertexVector.h"
44
45 /// some system specific defines
46 #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
47 # define PATH_SEPARATOR string("\\")
48 #else
49 # define PATH_SEPARATOR string("/")
50 #endif
51
52 using std::string;
53 using std::vector;
54
55 namespace Animorph {
56
57 enum RotateAxis
58 {
59 X_AXIS,
60 Y_AXIS,
61 Z_AXIS
62 };
63
64 // delete all following 'characters' in 'str'
65 void UtilStringDelFollow (std::string &str, const std::string &characters);
66
67 // delete all leading 'characters' in 'str'
68 void UtilStringDelLead (std::string &str, const std::string &characters);
69
70 // delete all surrounding 'characters' in 'str'
71 void UtilStringDelSurround (std::string &str, const std::string &characters);
72
73 // some functions that are very usefull for writing files
74 bool hasFileEnding (const std::string &filename, const std::string &ending);
75 std::string cutFileEnding (std::string filename, const std::string &ending = "");
76
77 // some generic template functions for delete algorithms
78 template <typename T>
delete_one(T * t)79 void delete_one (T *t)
80 {
81 delete t;
82 t = NULL;
83 }
84
85 template <typename T>
delete_array(T * t)86 void delete_array (T *t)
87 {
88 delete [] t;
89 t = NULL;
90 }
91
92 // '<<' operator for vector class
93 /*template <typename T>
94 std::ostream &operator << (std::ostream &s, std::vector<T> iv)
95 {
96 s << "[";
97 for (unsigned i = 0; i < iv.size (); i++)
98 {
99 s << iv[i];
100 if (i < iv.size ()-1)
101 s << ",";
102 }
103 s << "]" << std::endl;
104
105 return s;
106 }*/
107
108 /// print Vector on std::cout
109 template <typename T>
printVector(std::vector<T> iv)110 void printVector (std::vector<T> iv)
111 {
112 std::cout << "[";
113 for (unsigned i = 0; i < iv.size (); i++)
114 {
115 std::cout << iv[i];
116 if (i < iv.size ()-1)
117 std::cout << ",";
118 }
119 std::cout << "]" << std::endl;
120 }
121
122 /// create std::string from any number
123 template <typename T>
124 std::string toString (const T &thing, int w = 0, int p = 0)
125 {
126 std::ostringstream os;
127 os << std::setw(w) << std::setprecision(p) << thing;
128 return os.str();
129
130 }
131
132 /// line - to extract tokens from
133 /// seperator - token separator characters
134 /// result - sequential string container
135 template <typename T>
StringToken(const std::string & line,const std::string & separator,T & result)136 void StringToken (const std::string& line, const std::string &separator, T& result)
137 {
138 std::string::size_type start = line.find_first_not_of(separator);
139 if (std::string::npos == start) return; // nothing found
140
141 do
142 {
143 std::string::size_type end = line.find_first_of(separator,start);
144 if (std::string::npos == end)
145 {
146 result.push_back(line.substr(start));
147 start = std::string::npos; // finish loop
148 }
149 else
150 {
151 result.push_back(line.substr(start, end - start));
152 start = line.find_first_not_of(separator, end);
153 }
154 } while (std::string::npos != start);
155
156 }
157
158
159 template <typename T>
stringTokeni(const string & line,const std::string & separator,T & result)160 void stringTokeni(const string& line, const std::string& separator, T& result)
161 {
162 std::string::size_type start = line.find_first_not_of(separator);
163 if (std::string::npos == start) return; // nothing found
164
165 do
166 {
167 std::string::size_type end = line.find_first_of(separator,start);
168 if (std::string::npos == end)
169 {
170 result.push_back(std::atoi(line.substr(start).c_str()));
171 start = std::string::npos; // finish loop
172 }
173 else
174 {
175 result.push_back(std::atoi(line.substr(start, end - start).c_str()));
176 start = line.find_first_not_of(separator, end);
177 }
178 } while (std::string::npos != start);
179 }
180
181 int replaceString (const string& match, const string& replace, string& str, unsigned int maxReplace = 0);
182
183 /*! \brief Returns the location of the center of gravity
184 * \param vertexNumbers a vector of indices into vertexvector
185 * \param vertexvector a vector of vertices, from which only the ones indicated by vertexNumbers are used
186 * \return location of the center of gravity
187 */
188 Vector3f calcCenteroid(const vector<int>& vertexNumbers, const VertexVector& vertexvector);
189
190 Vector3f calcAverageNormalLength(const vector<int> vertexNumbers, const VertexVector& vertexvector);
191
192 } // end namespace
193
194
195 #endif // UTIL_H
196