src/utility.h

changeset 206
654661eab7f3
parent 205
1a4342d80de7
child 207
5315358a7d91
equal deleted inserted replaced
205:1a4342d80de7 206:654661eab7f3
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013 - 2020 Teemu Piippo
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (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
13 * GNU 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, see <http://www.gnu.org/licenses/>.
17 */
18
19 #pragma once
20 #include "basics.h"
21
22 // http://stackoverflow.com/a/18204188/3629665
23 template<typename T>
24 inline T rotl10(T x)
25 {
26 return (x << 10) | ((x >> 22) & 0x000000ff);
27 }
28
29 template<typename T>
30 inline T rotl20(T x)
31 {
32 return (x << 20) | ((x >> 12) & 0x000000ff);
33 }
34
35 inline QString format(const QString& format_string)
36 {
37 return format_string;
38 }
39
40 template<typename T, typename... Rest>
41 QString format(const QString& format_string, T&& arg, Rest&&... rest)
42 {
43 return format(format_string.arg(arg), std::forward<Rest>(rest)...);
44 }
45
46 inline QString quoted(QString string)
47 {
48 if (string.contains("'"))
49 {
50 string.replace("\"", "\\\"");
51 string = "\"" + string + "\"";
52 }
53 else
54 {
55 string = "'" + string + "'";
56 }
57 return string;
58 }
59
60 /**
61 * @brief Converts the specified vertex to a simple string
62 * @param vertex vertex to convert
63 * @return "x y z"-formatted string
64 */
65 inline QString vertexToString(const glm::vec3& vertex)
66 {
67 return format("%1 %2 %3", vertex.x, vertex.y, vertex.z);
68 }
69
70 inline QString vertexToStringParens(const glm::vec3& vertex)
71 {
72 return format("(%1, %2, %3)", vertex.x, vertex.y, vertex.z);
73 }
74
75 inline QString transformToString(const glm::mat4& matrix)
76 {
77 return format(
78 "%1 %2 %3 %4 %5 %6 %7 %8 %9 %10 %11 %12",
79 matrix[3][0],
80 matrix[3][1],
81 matrix[3][2],
82 matrix[0][0],
83 matrix[1][0],
84 matrix[2][0],
85 matrix[0][1],
86 matrix[1][1],
87 matrix[2][1],
88 matrix[0][2],
89 matrix[1][2],
90 matrix[2][2]);
91 }
92
93 template<typename T, glm::qualifier Q>
94 constexpr unsigned int qHash(const glm::vec<3, T, Q>& key)
95 {
96 return qHash(key.x) ^ rotl10(qHash(key.y)) ^ rotl20(qHash(key.z));
97 }

mercurial