1 #include "../include/animorph/util.h"
2
3 using namespace std;
4 using namespace Animorph;
5
UtilStringDelFollow(std::string & str,const std::string & characters)6 void Animorph::UtilStringDelFollow (std::string &str, const std::string &characters)
7 {
8 string::size_type pos = str.find_last_not_of(characters);
9 if (string::npos != pos)
10 str = str.substr(0, pos+1);
11 else
12 {
13 // it is still possible that 'str' contains only 'characters':
14 if (string::npos != str.find_first_of(characters))
15 str.erase();
16 }
17
18 }
19
UtilStringDelLead(std::string & str,const std::string & characters)20 void Animorph::UtilStringDelLead (std::string &str, const std::string &characters)
21 {
22 string::size_type pos = str.find_first_not_of(characters);
23 if (string::npos != pos)
24 str = str.substr(pos);
25 else
26 {
27 // it is still possible that 'str' contains only 'characters':
28 if (string::npos != str.find_first_of(characters))
29 str.erase();
30 }
31
32 }
33
UtilStringDelSurround(std::string & str,const std::string & characters)34 void Animorph::UtilStringDelSurround (std::string &str, const std::string &characters)
35 {
36 string::size_type pos = str.find_first_not_of(characters);
37 if (string::npos != pos)
38 str = str.substr(pos);
39
40 pos = str.find_last_not_of(characters);
41 if (string::npos != pos)
42 str = str.substr(0, pos+1);
43 else
44 {
45 // it is still possible that 'str' contains only 'characters':
46 if (string::npos != str.find_first_of(characters))
47 str.erase();
48 }
49 }
50
hasFileEnding(const std::string & filename,const std::string & ending)51 bool Animorph::hasFileEnding (const std::string &filename, const std::string &ending)
52 {
53 unsigned int loc = filename.find (ending, filename.length () - ending.length ());
54
55 if (loc != string::npos )
56 {
57 return true;
58 }
59
60 return false;
61 }
62
cutFileEnding(std::string filename,const std::string & ending)63 std::string Animorph::cutFileEnding (std::string filename, const std::string &ending)
64 {
65 if (ending == "")
66 {
67 unsigned int loc = filename.find_last_of('.', filename.length ());
68
69 if (loc != string::npos )
70 {
71 filename.erase (loc);
72 return filename;
73 }
74 }
75 else
76 {
77 unsigned int loc = filename.find (ending, filename.length () - ending.length ());
78
79 if (loc != string::npos )
80 {
81 filename.erase (loc);
82 return filename;
83 }
84 }
85
86 return filename;
87 }
88
89 /*!
90 * An function to replace occurences of substrings in a bigger string. Very basic function without regex.
91 * @param match This is the string that is matched to replace.
92 * @param replace The string that replaces all found match strings.
93 * @param str The string that is matched and replaced.
94 * @param maxReplace Give a int to limit the replace matches. If 0 is given there's no limit.
95 */
replaceString(const string & match,const string & replace,string & str,unsigned int maxReplace)96 int Animorph::replaceString (const string& match, const string& replace, string& str, unsigned int maxReplace)
97 {
98 int start = 0;
99 unsigned int i = 0;
100
101 if (maxReplace == 0)
102 maxReplace = str.length ();
103
104 for (; i < maxReplace; i++)
105 {
106 string::size_type loc = str.find (match, start);
107
108 if (loc != string::npos)
109 {
110 str.replace (loc, match.length(), replace);
111 }
112 else
113 {
114 return i;
115 }
116
117 start = loc;
118 }
119
120 return i;
121 }
122
calcCenteroid(const vector<int> & vertexNumbers,const VertexVector & vertexvector)123 Vector3f Animorph::calcCenteroid(const vector<int>& vertexNumbers, const VertexVector& vertexvector)
124 {
125 Vector3f center;
126 center.zero();
127
128 for (vector<int>::const_iterator v_it = vertexNumbers.begin ();
129 v_it != vertexNumbers.end ();
130 v_it++)
131 {
132 int vn = *v_it;
133
134 const Vertex &vertex(vertexvector[vn]);
135 center += vertex.co;
136 }
137
138 if (vertexNumbers.size ())
139 {
140 center /= vertexNumbers.size ();
141 }
142
143 return center;
144 }
145
calcAverageNormalLength(const vector<int> vertexNumbers,const VertexVector & vertexvector)146 Vector3f Animorph::calcAverageNormalLength(const vector<int> vertexNumbers, const VertexVector& vertexvector)
147 {
148 Vector3f averageNormal;
149 averageNormal.zero();
150
151 for (vector<int>::const_iterator v_it = vertexNumbers.begin ();
152 v_it != vertexNumbers.end ();
153 v_it++)
154 {
155 int vn = *v_it;
156
157 const Vertex &vertex(vertexvector[vn]);
158 averageNormal += vertex.no;
159 }
160
161 if (vertexNumbers.size ())
162 {
163 averageNormal /= vertexNumbers.size ();
164 }
165
166 return averageNormal;
167 }
168
169