26 #include <boost/functional/hash.hpp>
27 #include <boost/lexical_cast.hpp>
28 #include <boost/uuid/uuid.hpp>
29 #include <boost/uuid/uuid_generators.hpp>
30 #include <boost/uuid/uuid_io.hpp>
34 #include "../../lib/repo_log.h"
38 typedef boost::uuids::uuid repoUUID;
42 std::size_t operator()(
const repoUUID& uid)
const
44 return boost::hash<boost::uuids::uuid>()(uid);
49 std::vector<float> ambient;
50 std::vector<float> diffuse;
51 std::vector<float> specular;
52 std::vector<float> emissive;
55 float shininessStrength;
78 typedef std::vector<uint32_t> repo_face_t;
92 static boost::uuids::random_generator gen;
94 static repoUUID generateUUID(){
112 static repoUUID stringToUUID(
113 const std::string &text,
114 const std::string &suffix = std::string())
116 boost::uuids::uuid uuid;
118 uuid = generateUUID();
123 boost::uuids::string_generator gen;
124 if (text.substr(0, 1) !=
"{")
125 uuid = gen(
"{" + text +
"}");
129 catch (std::runtime_error e)
132 boost::hash<std::string> string_hash;
133 std::string hashedUUID;
134 std::stringstream str;
135 str << string_hash(text);
140 while (hashedUUID.size() < 32 - suffix.size())
141 hashedUUID.append(
"0");
142 hashedUUID.append(suffix);
143 uuid = stringToUUID(hashedUUID, suffix);
154 static std::string UUIDtoString(
const repoUUID &
id)
156 return boost::lexical_cast<std::string>(id);
159 static std::string toString(
const repo_face_t &f)
162 unsigned int mNumIndices = f.size();
165 for (
unsigned int i = 0; i < mNumIndices; i++)
167 str += std::to_string(f[i]);
168 if (i != mNumIndices - 1)
177 std::stringstream sstr;
178 sstr <<
"[" << color.r <<
", " << color.g <<
", " << color.b <<
", " << color.a <<
"]";
184 std::stringstream sstr;
185 sstr <<
"[" << vec.x <<
", " << vec.y <<
", " << vec.z <<
"]";
191 std::stringstream sstr;
192 sstr <<
"[" << vec.x <<
", " << vec.y <<
"]";
205 static std::string vectorToString(
const std::vector<T> &vec)
211 str +=
"[" + toString(vec.at(0));
213 str +=
", ..., " + toString(vec.at(vec.size() - 1));
222 return a.x*b.x + a.y*b.y + a.z*b.z;
228 product.x = (a.y * b.z) - (a.z * b.y);
229 product.y = (a.z * b.x) - (a.x * b.z);
230 product.z = (a.x * b.y) - (a.y * b.x);
235 static std::string printMat(
const std::vector<float> &mat)
237 std::stringstream ss;
238 for (
int i = 0; i < mat.size(); ++i)
252 std::stringstream ss;
253 ss <<
"[ " << vec.x <<
", " << vec.y <<
" ," << vec.z <<
" ]";
268 if (mat.size() != 16)
270 repoError <<
"Trying to perform a matrix x vector multiplation with unexpected matrix size(" << mat.size() <<
")";
280 result.x = mat[0] * vec.x + mat[1] * vec.y + mat[2] * vec.z + mat[3];
281 result.y = mat[4] * vec.x + mat[5] * vec.y + mat[6] * vec.z + mat[7];
282 result.z = mat[8] * vec.x + mat[9] * vec.y + mat[10] * vec.z + mat[11];
286 if (fabs(mat[12]) > sig || fabs(mat[13]) > sig || fabs(mat[14]) > sig || fabs(mat[15] - 1) > sig)
288 repoWarning <<
"Potentially incorrect transformation : does not expect the last row to have values!";
306 if (mat.size() != 16)
308 repoError <<
"Trying to perform a matrix x vector multiplation with unexpected matrix size(" << mat.size() <<
")";
318 result.x = mat[0] * vec.x + mat[1] * vec.y + mat[2] * vec.z;
319 result.y = mat[4] * vec.x + mat[5] * vec.y + mat[6] * vec.z;
320 result.z = mat[8] * vec.x + mat[9] * vec.y + mat[10] * vec.z;
326 static float calculateDeterminant(std::vector<float> mat)
335 float a1 = mat[0], a2 = mat[1], a3 = mat[2], a4 = mat[3];
336 float b1 = mat[4], b2 = mat[5], b3 = mat[6], b4 = mat[7];
337 float c1 = mat[8], c2 = mat[9], c3 = mat[10], c4 = mat[11];
338 float d1 = mat[12], d2 = mat[13], d3 = mat[14], d4 = mat[15];
340 float a1b2 = (a1 * b2) *(c3 * d4 - c4 * d3);
341 float a1b3 = (a1 * b3) *(c4 * d2 - c2 * d4);
342 float a1b4 = (a1 * b4) *(c2 * d3 - c3 * d2);
344 float a2b1 = -(a2 * b1) *(c3 * d4 - c4 * d3);
345 float a2b3 = -(a2 * b3) *(c4 * d1 - c1 * d4);
346 float a2b4 = -(a2 * b4) *(c1 * d3 - c3 * d1);
348 float a3b1 = (a3 * b1) *(c2 * d4 - c4 * d2);
349 float a3b2 = (a3 * b2) *(c4 * d1 - c1 * d4);
350 float a3b4 = (a3 * b4) *(c1 * d2 - c2 * d1);
352 float a4b1 = -(a4 * b1) *(c2 * d3 - c3 * d2);
353 float a4b2 = -(a4 * b2) *(c3 * d1 - c1 * d3);
354 float a4b3 = -(a4 * b3) *(c1 * d2 - c2 * d1);
356 return a1b2 + a1b3 + a1b4
359 + a4b1 + a4b2 + a4b3;
362 static std::vector<float> invertMat(
const std::vector<float> &mat)
364 std::vector<float> result;
367 if (mat.size() != 16)
369 repoError <<
"Unsupported vector size (" << mat.size() <<
")!";
373 const float det = calculateDeterminant(mat);
376 repoError <<
"Trying to invert a matrix with determinant = 0!";
380 const float inv_det = 1. / det;
382 float a1 = mat[0], a2 = mat[1], a3 = mat[2], a4 = mat[3];
383 float b1 = mat[4], b2 = mat[5], b3 = mat[6], b4 = mat[7];
384 float c1 = mat[8], c2 = mat[9], c3 = mat[10], c4 = mat[11];
385 float d1 = mat[12], d2 = mat[13], d3 = mat[14], d4 = mat[15];
387 result[0] = inv_det * (b2 * (c3 * d4 - c4 * d3) + b3 * (c4 * d2 - c2 * d4) + b4 * (c2 * d3 - c3 * d2));
388 result[1] = -inv_det * (a2 * (c3 * d4 - c4 * d3) + a3 * (c4 * d2 - c2 * d4) + a4 * (c2 * d3 - c3 * d2));
389 result[2] = inv_det * (a2 * (b3 * d4 - b4 * d3) + a3 * (b4 * d2 - b2 * d4) + a4 * (b2 * d3 - b3 * d2));
390 result[3] = -inv_det * (a2 * (b3 * c4 - b4 * c3) + a3 * (b4 * c2 - b2 * c4) + a4 * (b2 * c3 - b3 * c2));
392 result[4] = -inv_det * (b1 * (c3 * d4 - c4 * d3) + b3 * (c4 * d1 - c1 * d4) + b4 * (c1 * d3 - c3 * d1));
393 result[5] = inv_det * (a1 * (c3 * d4 - c4 * d3) + a3 * (c4 * d1 - c1 * d4) + a4 * (c1 * d3 - c3 * d1));
394 result[6] = -inv_det * (a1 * (b3 * d4 - b4 * d3) + a3 * (b4 * d1 - b1 * d4) + a4 * (b1 * d3 - b3 * d1));
395 result[7] = inv_det * (a1 * (b3 * c4 - b4 * c3) + a3 * (b4 * c1 - b1 * c4) + a4 * (b1 * c3 - b3 * c1));
397 result[8] = inv_det * (b1 * (c2 * d4 - c4 * d2) + b2 * (c4 * d1 - c1 * d4) + b4 * (c1 * d2 - c2 * d1));
398 result[9] = -inv_det * (a1 * (c2 * d4 - c4 * d2) + a2 * (c4 * d1 - c1 * d4) + a4 * (c1 * d2 - c2 * d1));
399 result[10] = inv_det * (a1 * (b2 * d4 - b4 * d2) + a2 * (b4 * d1 - b1 * d4) + a4 * (b1 * d2 - b2 * d1));
400 result[11] = -inv_det * (a1 * (b2 * c4 - b4 * c2) + a2 * (b4 * c1 - b1 * c4) + a4 * (b1 * c2 - b2 * c1));
402 result[12] = -inv_det * (b1 * (c2 * d3 - c3 * d2) + b2 * (c3 * d1 - c1 * d3) + b3 * (c1 * d2 - c2 * d1));
403 result[13] = inv_det * (a1 * (c2 * d3 - c3 * d2) + a2 * (c3 * d1 - c1 * d3) + a3 * (c1 * d2 - c2 * d1));
404 result[14] = -inv_det * (a1 * (b2 * d3 - b3 * d2) + a2 * (b3 * d1 - b1 * d3) + a3 * (b1 * d2 - b2 * d1));
405 result[15] = inv_det * (a1 * (b2 * c3 - b3 * c2) + a2 * (b3 * c1 - b1 * c3) + a3 * (b1 * c2 - b2 * c1));
412 static std::vector<float> matMult(
const std::vector<float> &mat1,
const std::vector<float> &mat2)
414 std::vector<float> result;
415 if (mat1.size() != mat2.size() != 16)
419 for (
int i = 0; i < 4; ++i)
421 for (
int j = 0; j < 4; ++j)
423 size_t resultIdx = i * 4 + j;
424 result[resultIdx] = 0;
425 for (
int k = 0; k < 4; ++k)
427 result[resultIdx] += mat1[i * 4 + k] * mat2[k * 4 + j];
434 repoError <<
"We currently only support 4x4 matrix multiplications";
440 static std::vector<float> transposeMat(
const std::vector<float> &mat)
442 std::vector<float> result(mat.begin(), mat.end());
444 if (mat.size() != 16)
446 repoError <<
"Unsupported vector size (" << mat.size() <<
")!";
467 result[11] = mat[14];
468 result[14] = mat[11];
476 float length = std::sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
486 static bool nameCheck(
const char &c)
488 return c ==
' ' || c ==
'$' || c ==
'.';
491 static bool dbNameCheck(
const char &c)
493 return c ==
'/' || c ==
'\\' || c ==
'.' || c ==
' '
494 || c ==
'\"' || c ==
'$' || c ==
'*' || c ==
'<'
495 || c ==
'>' || c ==
':' || c ==
'?';
498 static bool extNameCheck(
const char &c)
500 return c ==
' ' || c ==
'$';
503 static std::string sanitizeExt(
const std::string& name)
506 std::string newName(name);
507 std::replace_if(newName.begin(), newName.end(), extNameCheck,
'_');
508 auto strPos = newName.find(
"system.");
509 if (strPos != std::string::npos)
511 newName.replace(strPos,
sizeof(
"system."),
"");
516 static std::string sanitizeName(
const std::string& name)
519 std::string newName(name);
520 std::replace_if(newName.begin(), newName.end(), nameCheck,
'_');
521 auto strPos = newName.find(
"system.");
522 if (strPos != std::string::npos)
524 newName.replace(strPos,
sizeof(
"system."),
"");
529 static std::string sanitizeDatabaseName(
const std::string& name)
534 std::string newName(name);
535 std::replace_if(newName.begin(), newName.end(), dbNameCheck,
'_');
536 auto strPos = newName.find(
"system.");
537 if (strPos != std::string::npos)
539 newName.replace(strPos,
sizeof(
"system."),
"");
Definition: repo_node_utils.h:60
Definition: repo_node_utils.h:73
Definition: repo_node_utils.h:67
Definition: repo_node_utils.h:81
Definition: repo_node_utils.h:40
Definition: repo_node_utils.h:48