src/utility.h

Fri, 17 Sep 2021 22:38:53 +0300

author
Teemu Piippo <teemu@hecknology.net>
date
Fri, 17 Sep 2021 22:38:53 +0300
changeset 130
f2d78b30f2a9
parent 33
4c41bfe2ec6e
child 141
185eb297dc1e
permissions
-rw-r--r--

Add missing changes to canvas.h

24
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
1 /*
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
2 * LDForge: LDraw parts authoring CAD
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
3 * Copyright (C) 2013 - 2020 Teemu Piippo
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
4 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
5 * This program is free software: you can redistribute it and/or modify
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
6 * it under the terms of the GNU General Public License as published by
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
7 * the Free Software Foundation, either version 3 of the License, or
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
8 * (at your option) any later version.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
9 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
10 * This program is distributed in the hope that it will be useful,
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
13 * GNU General Public License for more details.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
14 *
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
15 * You should have received a copy of the GNU General Public License
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
17 */
1a0faaaceb84 added license
Teemu Piippo <teemu@hecknology.net>
parents: 8
diff changeset
18
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
19 #pragma once
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
20 #include "basics.h"
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
21
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
22 namespace utility
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
23 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
24 template<typename T, std::size_t N>
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
25 constexpr std::size_t countof(T(&)[N])
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
26 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
27 return N;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
28 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
29
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
30 // http://stackoverflow.com/a/18204188/3629665
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
31 template<typename T>
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
32 inline T rotl10(T x)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
33 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
34 return (x << 10) | ((x >> 22) & 0x000000ff);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
35 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
36
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
37 template<typename T>
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
38 inline T rotl20(T x)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
39 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
40 return (x << 20) | ((x >> 12) & 0x000000ff);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
41 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
42
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
43 inline QString format(const QString& format_string)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
44 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
45 return format_string;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
46 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
47
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
48 template<typename T, typename... Rest>
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
49 QString format(const QString& format_string, T&& arg, Rest&&... rest)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
50 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
51 return format(format_string.arg(arg), std::forward<Rest>(rest)...);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
52 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
53
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
54 inline QString quoted(QString string)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
55 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
56 if (string.contains("'"))
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
57 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
58 string.replace("\"", "\\\"");
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
59 string = "\"" + string + "\"";
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
60 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
61 else
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
62 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
63 string = "'" + string + "'";
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
64 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
65 return string;
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
66 }
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
67
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
68 template<typename T, typename R>
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
69 bool contains(T&& container, R&& value)
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
70 {
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
71 return std::find(std::begin(container), std::end(container), value) != std::end(container);
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
72 }
33
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
73
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
74 inline QString vertexToStringParens(const glm::vec3& vertex)
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
75 {
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
76 return utility::format("(%1, %2, %3)", vertex.x, vertex.y, vertex.z);
4c41bfe2ec6e replaced matrix and vertex classes with glm
Teemu Piippo <teemu@hecknology.net>
parents: 24
diff changeset
77 }
8
44679e468ba9 major update with many things
Teemu Piippo <teemu@hecknology.net>
parents:
diff changeset
78 }

mercurial