/* MIT License Copyright (c) 2024 - 2025 René Amthor (tobid7) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include #include #include #include #include const char* license_text = R"(/* MIT License Copyright (c) 2024 - 2025 René Amthor (tobid7) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ )"; const std::vector elems = {"x", "y", "z", "w"}; void MakeOperationFor(std::fstream& off, char op, int n) { off << " template \n"; off << " vec" << n << "& operator" << op << "=(T1 v) {\n"; for (int i = 0; i < n; i++) { off << " " << elems[i] << " " << op << "= (T)v;\n"; } off << " return *this;\n"; off << " }\n\n"; off << " template \n"; off << " vec" << n << "& operator" << op << "=(const vec" << n << "& v) {\n"; for (int i = 0; i < n; i++) { off << " " << elems[i] << " " << op << "= (T)v." << elems[i] << ";\n"; } off << " return *this;\n"; off << " }\n\n"; off << " template \n"; off << " vec" << n << " operator" << op << "(T1 v) const {\n"; off << " return vec" << n << "("; for (int i = 0; i < n; i++) { if (i > 0) off << ", "; off << elems[i] << " " << op << " (T)v"; } off << ");\n }\n\n"; off << " template \n"; off << " vec" << n << " operator" << op << "(const vec" << n << "& v) const {\n"; off << " return vec" << n << "("; for (int i = 0; i < n; i++) { if (i > 0) off << ", "; off << elems[i] << " " << op << " (T)v." << elems[i]; } off << ");\n }\n\n"; } void SwapHaxx(std::fstream& off, int n) { std::unordered_set done; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { std::string a = elems[i]; std::string b = elems[j]; /** Make sure we generate nothing twice */ if (a == b || done.count(b + a)) { continue; } off << " void Swap" << (char)toupper(a[0]) << (char)toupper(b[0]) << "() {\n"; off << " T t = " << a << ";\n " << a << " = " << b << ";\n"; off << " " << b << " = t;\n }\n"; done.insert(a + b); } } } void GenerateVec(int n) { if (n < 2 || n > 4) { std::cout << "Only 2 to 4 supported.\n"; return; } std::fstream off("vec" + std::to_string(n) + ".hpp", std::ios::out); off << "#pragma once\n" << std::endl; off << license_text << std::endl; off << "// This file is generated by lazyvec\n#include " "\n\n"; off << "namespace PD {" << std::endl; off << "template \nclass vec" << n << " {\npublic:\n"; for (int i = 0; i < n; i++) { off << " T " << elems[i] << ";\n"; } off << "\n"; off << " vec" << n << "(): "; for (int i = 0; i < n; i++) { if (i > 0) { off << ", "; } off << elems[i] << "(0)"; } off << " {}" << std::endl; // Magic Construtor (support for anytype vec) off << " template \n"; off << " explicit vec" << n << "(T1 v) {\n"; for (int i = 0; i < n; i++) { off << " " << elems[i] << " = (T)v;\n"; } off << " }\n\n"; // Magic Constructor 2 off << " template \n"; off << " explicit vec" << n << "(vec" << n << " v) {\n"; for (int i = 0; i < n; i++) { off << " " << elems[i] << " = (T)v. " << elems[i] << ";\n"; } off << " }\n\n"; off << " vec" << n << "("; for (int i = 0; i < n; i++) { if (i > 0) off << ", "; off << "T " << elems[i]; } off << ") : "; for (int i = 0; i < n; i++) { if (i > 0) off << ", "; off << elems[i] << "(" << elems[i] << ")"; } off << " {}\n\n"; MakeOperationFor(off, '+', n); MakeOperationFor(off, '-', n); MakeOperationFor(off, '*', n); MakeOperationFor(off, '/', n); off << " vec" << n << " operator-() const {return vec" << n << "("; for (int i = 0; i < n; i++) { off << "-" << elems[i]; if (i != n - 1) { off << ", "; } } off << ");}\n\n"; off << " bool operator==(const vec" << n << "& v) const { return "; for (int i = 0; i < n; i++) { off << elems[i] << " == v." << elems[i]; if (i != n - 1) { off << " && "; } } off << ";}\n"; off << " bool operator!=(const vec" << n << "&v) const { return !(*this == v); }\n\n"; off << " double Len() const {return std::sqrt(SqLen()); }\n"; off << " double SqLen() const { return "; for (int i = 0; i < n; i++) { off << elems[i] << " * " << elems[i]; if (i != n - 1) { off << " + "; } } off << "; }\n\n"; SwapHaxx(off, n); off << "};\n"; off << "using fvec" << n << " = vec" << n << ";\n"; off << "using dvec" << n << " = vec" << n << ";\n"; off << "using ivec" << n << " = vec" << n << ";\n"; off << "}\n"; off.close(); } /** * Yet another Stupid Code generation tool * Why ? * Cause this is written and tested faster then * manually writeup vec2 to vec4 */ int main(int argc, char* argv[]) { if (argc != 2) { std::cout << argv[0] << " " << std::endl; return 0; } GenerateVec(std::stoi(argv[1])); return 0; }