Removed old vector stuff
This commit is contained in:
parent
a8ec1fbc10
commit
82b5b2e282
377
generate_vec.py
377
generate_vec.py
@ -1,377 +0,0 @@
|
|||||||
#!/bin/python3
|
|
||||||
"""
|
|
||||||
A python script to generate a c++ vector library for math
|
|
||||||
Should work with vectors with 2-9 components
|
|
||||||
Copyright © 2022 Matthias Quintern.
|
|
||||||
This software comes with no warranty.
|
|
||||||
This software is licensed under the GPL3
|
|
||||||
"""
|
|
||||||
|
|
||||||
from os import path
|
|
||||||
#
|
|
||||||
# SETTINGS
|
|
||||||
#
|
|
||||||
vectors = [2, 3, 4] # , 5, 6, 7, 8, 9]
|
|
||||||
letters_ = {
|
|
||||||
2: [ "x", "y" ],
|
|
||||||
3: [ "x", "y", "z" ],
|
|
||||||
4: [ "x", "y", "z", "w"],
|
|
||||||
}
|
|
||||||
# \t or x-spaces
|
|
||||||
tab = "\t"
|
|
||||||
# float, double, long double
|
|
||||||
float_type = "float"
|
|
||||||
# string or None
|
|
||||||
namespace = "gz"
|
|
||||||
types = {
|
|
||||||
"float": "f",
|
|
||||||
"double": "d",
|
|
||||||
"int": "i",
|
|
||||||
"unsigned int": "u",
|
|
||||||
}
|
|
||||||
filename = "vec.hpp"
|
|
||||||
# for genstring
|
|
||||||
templateStr = "@"
|
|
||||||
|
|
||||||
#
|
|
||||||
# HELPERS
|
|
||||||
#
|
|
||||||
def classname(n):
|
|
||||||
return "vec" + str(n)
|
|
||||||
|
|
||||||
def letters(n, i):
|
|
||||||
if n in letters_:
|
|
||||||
return letters_[n][i]
|
|
||||||
else:
|
|
||||||
return f"x{i}"
|
|
||||||
|
|
||||||
|
|
||||||
def genstring(n: int, template: str, sep=", ", offset=0):
|
|
||||||
"""
|
|
||||||
Generate a string from a template for each vector component
|
|
||||||
eg genstring(3, "@ + ") -> x + y + z
|
|
||||||
"""
|
|
||||||
s = ""
|
|
||||||
for i in range(n):
|
|
||||||
s += template.replace(templateStr, letters(n, i + offset)) + sep
|
|
||||||
s.removesuffix(sep)
|
|
||||||
return s[0:len(s)-len(sep)]
|
|
||||||
|
|
||||||
def transformString(s: str, depth: int):
|
|
||||||
"""Add tabs after all but the last line break and before the first char"""
|
|
||||||
return depth * tab + s.replace("\n", "\n" + depth * tab, s.count("\n") - 1)
|
|
||||||
|
|
||||||
def getPossiblities(i, a, depth=0):
|
|
||||||
"""
|
|
||||||
get all possibilites to get i by addition of numbers lower than i
|
|
||||||
eg i=3 -> 3, 2+1, 1+2, 1+1+1
|
|
||||||
"""
|
|
||||||
if i == 0:
|
|
||||||
return
|
|
||||||
if i == 1:
|
|
||||||
a.append(str(1))
|
|
||||||
return
|
|
||||||
for j in range(1, i):
|
|
||||||
b = []
|
|
||||||
# print("\t" * depth + "gp: i="+str(i)+" j="+str(j))
|
|
||||||
getPossiblities(i-j, b, depth+1)
|
|
||||||
|
|
||||||
for poss in b:
|
|
||||||
# print("\t"*depth + f"{i}-{j} returned", b)
|
|
||||||
a.append(str(j) + poss)
|
|
||||||
a.append(str(i))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#
|
|
||||||
# GENERATORS
|
|
||||||
#
|
|
||||||
def genConstructors(n):
|
|
||||||
constructors = []
|
|
||||||
s = "/// Default constructor\n"
|
|
||||||
s += classname(n) + "() : " + genstring(n, "@(0)") + " {};\n"
|
|
||||||
constructors.append(s)
|
|
||||||
|
|
||||||
s = "/// Create vector from scalar, all components will have value n\n"
|
|
||||||
s += "template<typename N>\n"
|
|
||||||
s += "(const N n) : " + genstring(n, "@(static_cast<T>(n))") + " {};\n";
|
|
||||||
|
|
||||||
a = []
|
|
||||||
getPossiblities(n, a)
|
|
||||||
for possiblity in a:
|
|
||||||
n_count = 0
|
|
||||||
v_count = 0
|
|
||||||
i = 0
|
|
||||||
comment = "/// Create a " + classname(n) + " from "
|
|
||||||
templates = "template<"
|
|
||||||
args = classname(n) + "("
|
|
||||||
init = " : "
|
|
||||||
|
|
||||||
for nstr in possiblity:
|
|
||||||
c = int(nstr)
|
|
||||||
if c == 1:
|
|
||||||
comment += "n "
|
|
||||||
templates += f"typename N{n_count}, "
|
|
||||||
args += f"N{n_count} n{n_count}, "
|
|
||||||
init += letters(n, i) + f"(static_cast<T>(n{n_count})), "
|
|
||||||
n_count += 1
|
|
||||||
i += 1
|
|
||||||
else:
|
|
||||||
comment += f"vec{c} "
|
|
||||||
args += "const " + classname(c) + f"<V{v_count}>& v{v_count}, "
|
|
||||||
templates += f"typename V{v_count}, "
|
|
||||||
for j in range(c):
|
|
||||||
init += letters(n, i) + "(static_cast<T>(v" + str(v_count) + "." + letters(n, j) + ")), "
|
|
||||||
i += 1
|
|
||||||
v_count += 1
|
|
||||||
templates = templates.removesuffix(", ") + ">"
|
|
||||||
args = args.removesuffix(", ") + ")"
|
|
||||||
init = init.removesuffix(", ") + " {};"
|
|
||||||
|
|
||||||
s = comment + "\n" + templates + "\n" + args + init + "\n"
|
|
||||||
constructors.append(s)
|
|
||||||
|
|
||||||
return constructors
|
|
||||||
|
|
||||||
|
|
||||||
def genValues(n):
|
|
||||||
s = genstring(n, "T @;\n", "")
|
|
||||||
return s
|
|
||||||
|
|
||||||
def genAssignment(n):
|
|
||||||
s = "/// component-wise assignment\n"
|
|
||||||
s += "template<typename V>\n"
|
|
||||||
s += "void operator=(const " + classname(n) + "<V>& other) {\n"
|
|
||||||
s += genstring(n, f"\t@ = static_cast<T>(other.@);\n", "") + "};\n\n"
|
|
||||||
s += "template<typename N>\n"
|
|
||||||
s += "void operator=(const N& other) {\n"
|
|
||||||
s += genstring(n, f"\t@ = static_cast<T>(other);\n", "") + "};\n\n"
|
|
||||||
|
|
||||||
return s
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def genArithmeticVectorial(n):
|
|
||||||
operators = []
|
|
||||||
for op in ["+", "-", "*", "/", "%"]:
|
|
||||||
s = "/// component-wise " + op + "\n"
|
|
||||||
s += "template<typename V>\n"
|
|
||||||
s += classname(n) + "<T> operator" + op + "(const " + classname(n) + "<V>& other) const { return "
|
|
||||||
s += classname(n) + "<T>(" + genstring(n, f"@ {op} static_cast<T>(other.@)") + "); };\n"
|
|
||||||
operators.append(s)
|
|
||||||
operators.append("\n")
|
|
||||||
|
|
||||||
for op in ["+=", "-=", "*=", "/=", "%="]:
|
|
||||||
s = "/// component-wise assignment" + op + "\n"
|
|
||||||
s += "template<typename V>\n"
|
|
||||||
s += "void operator" + op + "(const " + classname(n) + "<V>& other) {\n"
|
|
||||||
s += genstring(n, f"\t@ {op} static_cast<T>(other.@);\n", "") + "};\n"
|
|
||||||
operators.append(s)
|
|
||||||
operators.append("\n")
|
|
||||||
|
|
||||||
return operators
|
|
||||||
|
|
||||||
def genArithmeticScalar(n):
|
|
||||||
operators = []
|
|
||||||
for op in ["+", "-", "*", "/", "%"]:
|
|
||||||
s = "/// component-wise " + op + "\n"
|
|
||||||
s += "template<typename N>\n"
|
|
||||||
s += classname(n) + "<T> operator" + op + "(const N& other) const { return "
|
|
||||||
s += classname(n) + "<T>(" + genstring(n, f"@ {op} static_cast<T>(other.@)") + "); };\n"
|
|
||||||
operators.append(s)
|
|
||||||
operators.append("\n")
|
|
||||||
for op in ["+=", "-=", "*=", "/=", "%="]:
|
|
||||||
s = "/// component-wise assignment" + op + "\n"
|
|
||||||
s += "template<typename N>\n"
|
|
||||||
s += "void operator" + op + "(const N& other) {\n"
|
|
||||||
s += genstring(n, f"\t@ {op} static_cast<T>(other.@);\n", "") + "};\n"
|
|
||||||
operators.append(s)
|
|
||||||
operators.append("\n")
|
|
||||||
return operators
|
|
||||||
|
|
||||||
def genComparisonVectorial(n):
|
|
||||||
operators = []
|
|
||||||
for op in ["==", "<", ">"]:
|
|
||||||
s = "/// component-wise comparison " + op + " (and)\n"
|
|
||||||
s += "template<typename N>\n"
|
|
||||||
s += "bool operator" + op + "(const " + classname(n) + "<N>& other) const { return "
|
|
||||||
s += genstring(n, f"@ {op} other.@", " and ") + "; };\n"
|
|
||||||
operators.append(s)
|
|
||||||
operators.append("\n")
|
|
||||||
for op, antiop in { "!=": "==", "<=": ">", ">=": "<" }.items():
|
|
||||||
s = "/// component-wise comparison " + op + " (and)\n"
|
|
||||||
s += "template<typename N>\n"
|
|
||||||
s += "bool operator" + op + "(const " + classname(n) + "<N>& other) const { return "
|
|
||||||
s += genstring(n, f"@ {antiop} other.@", " and ") + "; };\n"
|
|
||||||
operators.append(s)
|
|
||||||
operators.append("\n")
|
|
||||||
return operators
|
|
||||||
|
|
||||||
def genComparisonScalar(n):
|
|
||||||
operators = []
|
|
||||||
for op in ["==", "<", ">"]:
|
|
||||||
s = "/// component-wise comparison " + op + " (and)\n"
|
|
||||||
s += "template<typename N>\n"
|
|
||||||
s += "bool operator" + op + "(const N& other) const { return "
|
|
||||||
s += genstring(n, f"@ {op} other.@", " and ") + "; };\n"
|
|
||||||
operators.append(s)
|
|
||||||
operators.append("\n")
|
|
||||||
for op, antiop in { "!=": "==", "<=": ">", ">=": "<" }.items():
|
|
||||||
s = "/// component-wise comparison " + op + " (and)\n"
|
|
||||||
s += "template<typename N>\n"
|
|
||||||
s += "bool operator" + op + "(const N& other) const { return "
|
|
||||||
s += genstring(n, f"@ {antiop} other.@", " and ") + "; };\n"
|
|
||||||
operators.append(s)
|
|
||||||
return operators
|
|
||||||
|
|
||||||
def genIterator(n):
|
|
||||||
s = """struct Iterator {
|
|
||||||
public:
|
|
||||||
using value_type = T;
|
|
||||||
Iterator() : ptr(nullptr) {};
|
|
||||||
Iterator(T* ptr) : ptr(ptr) {};
|
|
||||||
T& operator*() { return *ptr; };
|
|
||||||
Iterator& operator=(const Iterator& other) {
|
|
||||||
ptr = other.ptr;
|
|
||||||
return *this;
|
|
||||||
};
|
|
||||||
Iterator& operator++() { ptr += sizeof(T); return *this; };
|
|
||||||
Iterator operator++(int) { auto copy = *this; ptr += sizeof(T); return copy; };
|
|
||||||
friend int operator-(Iterator lhs, Iterator rhs) {
|
|
||||||
return lhs.ptr - rhs.ptr;
|
|
||||||
};
|
|
||||||
bool operator==(const Iterator& other) const { return ptr == other.ptr; };
|
|
||||||
// bool operator!=(const Iterator& other) const { return ptr != other.ptr; };
|
|
||||||
private:
|
|
||||||
T* ptr;
|
|
||||||
};
|
|
||||||
"""
|
|
||||||
s += "const Iterator cbegin() const { return Iterator(&" + letters(n, 0) + "); };\n"
|
|
||||||
s += "const Iterator cend() const { return Iterator(&" + letters(n, n-1) + "); };\n"
|
|
||||||
s += "const Iterator begin() const { return Iterator(&" + letters(n, 0) + "); };\n"
|
|
||||||
s += "const Iterator end() const { return Iterator(&" + letters(n, n-1) + "); };\n\n"
|
|
||||||
return s
|
|
||||||
|
|
||||||
|
|
||||||
def genFunctional(n):
|
|
||||||
s = "/// Returns the absolute value of the vector\n"
|
|
||||||
s += "inline " + float_type + " abs() const { return std::sqrt(" + genstring(n, f"static_cast<{float_type}>(@ * @)", " + ") + "); };"
|
|
||||||
|
|
||||||
if n == 2:
|
|
||||||
s += "/// Returns x/y\n"
|
|
||||||
s += "inline " + float_type + " ratio() const { return static_cast<" + float_type + ">(x) / y; };"
|
|
||||||
s += "/// Returns y/x\n"
|
|
||||||
s += "inline " + float_type + " inverseRatio() const { return static_cast<" + float_type + ">(y) / x; };"
|
|
||||||
|
|
||||||
s += "/// Returns the min of the components\n"
|
|
||||||
s += "inline T min() const { return std::min_element(cbegin(), cend()); };\n"
|
|
||||||
|
|
||||||
s += "/// Returns the max of the components\n"
|
|
||||||
s += "inline T max() const { return std::max_element(cbegin(), cend()); };\n"
|
|
||||||
|
|
||||||
s += "/// Scalar product\n"
|
|
||||||
s += "template<typename V>\n"
|
|
||||||
s += "inline " + classname(n) + "<T> dot(const " + classname(n) + "<V>& other) { return " + classname(n) + "<T>("
|
|
||||||
s += genstring(n, "@ * static_cast<T>(other.@)", " + ") + "); };\n"
|
|
||||||
|
|
||||||
return s
|
|
||||||
|
|
||||||
def genUtility(n):
|
|
||||||
s = "std::string to_string() const { return \"(\" + " + genstring(n, "std::to_string(@)", " + \", \" + ") + " + \")\"; };\n"
|
|
||||||
|
|
||||||
return s
|
|
||||||
|
|
||||||
def genUsing(n):
|
|
||||||
global types
|
|
||||||
s = ""
|
|
||||||
for t, c in types.items():
|
|
||||||
s += f"using {classname(n)}{c} = {classname(n)}<{t}>;\n"
|
|
||||||
return s
|
|
||||||
|
|
||||||
def generateFile(vectors):
|
|
||||||
depth = 0
|
|
||||||
s = "#pragma once\n\n#include <string>\n#include <cmath>\n#include <algorithm>\n\n"
|
|
||||||
if namespace:
|
|
||||||
s += "namespace " + namespace + " {\n"
|
|
||||||
depth = 1
|
|
||||||
for v in vectors:
|
|
||||||
s += generateVector(v, depth)
|
|
||||||
s += "\n"
|
|
||||||
for v in vectors:
|
|
||||||
s += transformString(genUsing(v), depth)
|
|
||||||
s += "\n"
|
|
||||||
if namespace:
|
|
||||||
depth -= 1
|
|
||||||
s += transformString("} // namespace " + namespace + "\n", depth)
|
|
||||||
|
|
||||||
for i in range(1, 5):
|
|
||||||
s = s.replace("\n" + i * tab + "\n", "\n\n")
|
|
||||||
|
|
||||||
return s
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def generateVector(n, depth):
|
|
||||||
s = transformString("""/**
|
|
||||||
* @brief Class containing $ numbers
|
|
||||||
*/
|
|
||||||
template<typename T>
|
|
||||||
class vec$ {
|
|
||||||
public:
|
|
||||||
""".replace("$", str(n)), depth)
|
|
||||||
|
|
||||||
depth += 1
|
|
||||||
|
|
||||||
s += transformString("// Constructors\n", depth)
|
|
||||||
for c in genConstructors(n):
|
|
||||||
s += transformString(c, depth + 1)
|
|
||||||
|
|
||||||
s += transformString("// Values\n", depth)
|
|
||||||
s += transformString(genValues(n), depth + 1)
|
|
||||||
|
|
||||||
s += transformString("// Assignment\n", depth)
|
|
||||||
s += transformString(genAssignment(n), depth + 1)
|
|
||||||
|
|
||||||
s += transformString("// Arithmetic\n", depth - 1)
|
|
||||||
s += transformString("// Vectorial\n", depth)
|
|
||||||
for o in genArithmeticVectorial(n):
|
|
||||||
s += transformString(o, depth + 1)
|
|
||||||
s += transformString("// Scalar\n", depth)
|
|
||||||
for o in genArithmeticScalar(n):
|
|
||||||
s += transformString(o, depth + 1)
|
|
||||||
|
|
||||||
s += transformString("// Comparison\n", depth - 1)
|
|
||||||
s += transformString("// Vectorial\n", depth)
|
|
||||||
for o in genComparisonVectorial(n):
|
|
||||||
s += transformString(o, depth + 1)
|
|
||||||
s += transformString("// Scalar\n", depth)
|
|
||||||
for o in genComparisonScalar(n):
|
|
||||||
s += transformString(o, depth + 1)
|
|
||||||
|
|
||||||
s += transformString("// Functional\n", depth - 1)
|
|
||||||
s += transformString(genFunctional(n), depth + 1)
|
|
||||||
|
|
||||||
s += transformString("// Utility\n", depth - 1)
|
|
||||||
s += transformString(genUtility(n), depth + 1)
|
|
||||||
s += transformString(genIterator(n), depth + 1)
|
|
||||||
|
|
||||||
s += transformString("}; // vec" + str(n) + "\n", depth - 1)
|
|
||||||
|
|
||||||
return s
|
|
||||||
|
|
||||||
|
|
||||||
def write_file(s):
|
|
||||||
global filename
|
|
||||||
if path.exists(filename):
|
|
||||||
answer = input("File " + filename + "already exists. Overwrite? (y/n): ")
|
|
||||||
if answer not in "yY":
|
|
||||||
return
|
|
||||||
|
|
||||||
with open(filename, "w") as file:
|
|
||||||
file.write(s)
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
write_file(generateFile(vectors))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
715
src/math/vec.hpp
715
src/math/vec.hpp
@ -1,715 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include<string>
|
|
||||||
#include <cmath>
|
|
||||||
#include <algorithm>
|
|
||||||
|
|
||||||
namespace gz {
|
|
||||||
/**
|
|
||||||
* @brief Class containing 2 numbers
|
|
||||||
*/
|
|
||||||
template<typename T>
|
|
||||||
class vec2 {
|
|
||||||
public:
|
|
||||||
// Constructors
|
|
||||||
/// Default constructor
|
|
||||||
vec2() : x(0), y(0) {};
|
|
||||||
/// Create a vec2 from n n
|
|
||||||
template<typename N0, typename N1>
|
|
||||||
vec2(N0 n0, N1 n1) : x(static_cast<T>(n0)), y(static_cast<T>(n1)) {};
|
|
||||||
/// Create a vec2 from vec2
|
|
||||||
template<typename V0>
|
|
||||||
vec2(const vec2<V0>& v0) : x(static_cast<T>(v0.x)), y(static_cast<T>(v0.y)) {};
|
|
||||||
// Values
|
|
||||||
T x;
|
|
||||||
T y;
|
|
||||||
// Assignment
|
|
||||||
/// component-wise assignment
|
|
||||||
template<typename V>
|
|
||||||
void operator=(const vec2<V>& other) {
|
|
||||||
x = static_cast<T>(other.x);
|
|
||||||
y = static_cast<T>(other.y);
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename N>
|
|
||||||
void operator=(const N& other) {
|
|
||||||
x = static_cast<T>(other);
|
|
||||||
y = static_cast<T>(other);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Arithmetic
|
|
||||||
// Vectorial
|
|
||||||
/// component-wise +
|
|
||||||
template<typename V>
|
|
||||||
vec2<T> operator+(const vec2<V>& other) const { return vec2<T>(x + static_cast<T>(other.x), y + static_cast<T>(other.y)); };
|
|
||||||
/// component-wise -
|
|
||||||
template<typename V>
|
|
||||||
vec2<T> operator-(const vec2<V>& other) const { return vec2<T>(x - static_cast<T>(other.x), y - static_cast<T>(other.y)); };
|
|
||||||
/// component-wise *
|
|
||||||
template<typename V>
|
|
||||||
vec2<T> operator*(const vec2<V>& other) const { return vec2<T>(x * static_cast<T>(other.x), y * static_cast<T>(other.y)); };
|
|
||||||
/// component-wise /
|
|
||||||
template<typename V>
|
|
||||||
vec2<T> operator/(const vec2<V>& other) const { return vec2<T>(x / static_cast<T>(other.x), y / static_cast<T>(other.y)); };
|
|
||||||
/// component-wise %
|
|
||||||
template<typename V>
|
|
||||||
vec2<T> operator%(const vec2<V>& other) const { return vec2<T>(x % static_cast<T>(other.x), y % static_cast<T>(other.y)); };
|
|
||||||
|
|
||||||
/// component-wise assignment+=
|
|
||||||
template<typename V>
|
|
||||||
void operator+=(const vec2<V>& other) {
|
|
||||||
x += static_cast<T>(other.x);
|
|
||||||
y += static_cast<T>(other.y);
|
|
||||||
};
|
|
||||||
/// component-wise assignment-=
|
|
||||||
template<typename V>
|
|
||||||
void operator-=(const vec2<V>& other) {
|
|
||||||
x -= static_cast<T>(other.x);
|
|
||||||
y -= static_cast<T>(other.y);
|
|
||||||
};
|
|
||||||
/// component-wise assignment*=
|
|
||||||
template<typename V>
|
|
||||||
void operator*=(const vec2<V>& other) {
|
|
||||||
x *= static_cast<T>(other.x);
|
|
||||||
y *= static_cast<T>(other.y);
|
|
||||||
};
|
|
||||||
/// component-wise assignment/=
|
|
||||||
template<typename V>
|
|
||||||
void operator/=(const vec2<V>& other) {
|
|
||||||
x /= static_cast<T>(other.x);
|
|
||||||
y /= static_cast<T>(other.y);
|
|
||||||
};
|
|
||||||
/// component-wise assignment%=
|
|
||||||
template<typename V>
|
|
||||||
void operator%=(const vec2<V>& other) {
|
|
||||||
x %= static_cast<T>(other.x);
|
|
||||||
y %= static_cast<T>(other.y);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Scalar
|
|
||||||
/// component-wise +
|
|
||||||
template<typename N>
|
|
||||||
vec2<T> operator+(const N& other) const { return vec2<T>(x + static_cast<T>(other.x), y + static_cast<T>(other.y)); };
|
|
||||||
/// component-wise -
|
|
||||||
template<typename N>
|
|
||||||
vec2<T> operator-(const N& other) const { return vec2<T>(x - static_cast<T>(other.x), y - static_cast<T>(other.y)); };
|
|
||||||
/// component-wise *
|
|
||||||
template<typename N>
|
|
||||||
vec2<T> operator*(const N& other) const { return vec2<T>(x * static_cast<T>(other.x), y * static_cast<T>(other.y)); };
|
|
||||||
/// component-wise /
|
|
||||||
template<typename N>
|
|
||||||
vec2<T> operator/(const N& other) const { return vec2<T>(x / static_cast<T>(other.x), y / static_cast<T>(other.y)); };
|
|
||||||
/// component-wise %
|
|
||||||
template<typename N>
|
|
||||||
vec2<T> operator%(const N& other) const { return vec2<T>(x % static_cast<T>(other.x), y % static_cast<T>(other.y)); };
|
|
||||||
|
|
||||||
/// component-wise assignment+=
|
|
||||||
template<typename N>
|
|
||||||
void operator+=(const N& other) {
|
|
||||||
x += static_cast<T>(other.x);
|
|
||||||
y += static_cast<T>(other.y);
|
|
||||||
};
|
|
||||||
/// component-wise assignment-=
|
|
||||||
template<typename N>
|
|
||||||
void operator-=(const N& other) {
|
|
||||||
x -= static_cast<T>(other.x);
|
|
||||||
y -= static_cast<T>(other.y);
|
|
||||||
};
|
|
||||||
/// component-wise assignment*=
|
|
||||||
template<typename N>
|
|
||||||
void operator*=(const N& other) {
|
|
||||||
x *= static_cast<T>(other.x);
|
|
||||||
y *= static_cast<T>(other.y);
|
|
||||||
};
|
|
||||||
/// component-wise assignment/=
|
|
||||||
template<typename N>
|
|
||||||
void operator/=(const N& other) {
|
|
||||||
x /= static_cast<T>(other.x);
|
|
||||||
y /= static_cast<T>(other.y);
|
|
||||||
};
|
|
||||||
/// component-wise assignment%=
|
|
||||||
template<typename N>
|
|
||||||
void operator%=(const N& other) {
|
|
||||||
x %= static_cast<T>(other.x);
|
|
||||||
y %= static_cast<T>(other.y);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Comparison
|
|
||||||
// Vectorial
|
|
||||||
/// component-wise comparison == (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator==(const vec2<N>& other) const { return x == other.x and y == other.y; };
|
|
||||||
/// component-wise comparison < (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator<(const vec2<N>& other) const { return x < other.x and y < other.y; };
|
|
||||||
/// component-wise comparison > (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator>(const vec2<N>& other) const { return x > other.x and y > other.y; };
|
|
||||||
|
|
||||||
/// component-wise comparison != (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator!=(const vec2<N>& other) const { return x == other.x and y == other.y; };
|
|
||||||
/// component-wise comparison <= (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator<=(const vec2<N>& other) const { return x > other.x and y > other.y; };
|
|
||||||
/// component-wise comparison >= (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator>=(const vec2<N>& other) const { return x < other.x and y < other.y; };
|
|
||||||
|
|
||||||
// Scalar
|
|
||||||
/// component-wise comparison == (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator==(const N& other) const { return x == other.x and y == other.y; };
|
|
||||||
/// component-wise comparison < (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator<(const N& other) const { return x < other.x and y < other.y; };
|
|
||||||
/// component-wise comparison > (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator>(const N& other) const { return x > other.x and y > other.y; };
|
|
||||||
|
|
||||||
/// component-wise comparison != (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator!=(const N& other) const { return x == other.x and y == other.y; };
|
|
||||||
/// component-wise comparison <= (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator<=(const N& other) const { return x > other.x and y > other.y; };
|
|
||||||
/// component-wise comparison >= (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator>=(const N& other) const { return x < other.x and y < other.y; };
|
|
||||||
// Functional
|
|
||||||
/// Returns the absolute value of the vector
|
|
||||||
inline float abs() const { return std::sqrt(static_cast<float>(x * x) + static_cast<float>(y * y)); };/// Returns x/y
|
|
||||||
inline float ratio() const { return static_cast<float>(x) / y; };/// Returns y/x
|
|
||||||
inline float inverseRatio() const { return static_cast<float>(y) / x; };/// Returns the min of the components
|
|
||||||
inline T min() const { return std::min_element(cbegin(), cend()); };
|
|
||||||
/// Returns the max of the components
|
|
||||||
inline T max() const { return std::max_element(cbegin(), cend()); };
|
|
||||||
/// Scalar product
|
|
||||||
template<typename V>
|
|
||||||
inline vec2<T> dot(const vec2<V>& other) { return vec2<T>(x * static_cast<T>(other.x) + y * static_cast<T>(other.y)); };
|
|
||||||
// Utility
|
|
||||||
std::string to_string() const { return "(" + std::to_string(x) + ", " + std::to_string(y) + ")"; };
|
|
||||||
struct Iterator {
|
|
||||||
public:
|
|
||||||
using value_type = T;
|
|
||||||
Iterator() : ptr(nullptr) {};
|
|
||||||
Iterator(T* ptr) : ptr(ptr) {};
|
|
||||||
T& operator*() { return *ptr; };
|
|
||||||
Iterator& operator=(const Iterator& other) {
|
|
||||||
ptr = other.ptr;
|
|
||||||
return *this;
|
|
||||||
};
|
|
||||||
Iterator& operator++() { ptr += sizeof(T); return *this; };
|
|
||||||
Iterator operator++(int) { auto copy = *this; ptr += sizeof(T); return copy; };
|
|
||||||
friend int operator-(Iterator lhs, Iterator rhs) {
|
|
||||||
return lhs.ptr - rhs.ptr;
|
|
||||||
};
|
|
||||||
bool operator==(const Iterator& other) const { return ptr == other.ptr; };
|
|
||||||
// bool operator!=(const Iterator& other) const { return ptr != other.ptr; };
|
|
||||||
private:
|
|
||||||
T* ptr;
|
|
||||||
};
|
|
||||||
const Iterator cbegin() const { return Iterator(&x); };
|
|
||||||
const Iterator cend() const { return Iterator(&y); };
|
|
||||||
const Iterator begin() const { return Iterator(&x); };
|
|
||||||
const Iterator end() const { return Iterator(&y); };
|
|
||||||
|
|
||||||
}; // vec2
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Class containing 3 numbers
|
|
||||||
*/
|
|
||||||
template<typename T>
|
|
||||||
class vec3 {
|
|
||||||
public:
|
|
||||||
// Constructors
|
|
||||||
/// Default constructor
|
|
||||||
vec3() : x(0), y(0), z(0) {};
|
|
||||||
/// Create a vec3 from n n n
|
|
||||||
template<typename N0, typename N1, typename N2>
|
|
||||||
vec3(N0 n0, N1 n1, N2 n2) : x(static_cast<T>(n0)), y(static_cast<T>(n1)), z(static_cast<T>(n2)) {};
|
|
||||||
/// Create a vec3 from n vec2
|
|
||||||
template<typename N0, typename V0>
|
|
||||||
vec3(N0 n0, const vec2<V0>& v0) : x(static_cast<T>(n0)), y(static_cast<T>(v0.x)), z(static_cast<T>(v0.y)) {};
|
|
||||||
/// Create a vec3 from vec2 n
|
|
||||||
template<typename V0, typename N0>
|
|
||||||
vec3(const vec2<V0>& v0, N0 n0) : x(static_cast<T>(v0.x)), y(static_cast<T>(v0.y)), z(static_cast<T>(n0)) {};
|
|
||||||
/// Create a vec3 from vec3
|
|
||||||
template<typename V0>
|
|
||||||
vec3(const vec3<V0>& v0) : x(static_cast<T>(v0.x)), y(static_cast<T>(v0.y)), z(static_cast<T>(v0.z)) {};
|
|
||||||
// Values
|
|
||||||
T x;
|
|
||||||
T y;
|
|
||||||
T z;
|
|
||||||
// Assignment
|
|
||||||
/// component-wise assignment
|
|
||||||
template<typename V>
|
|
||||||
void operator=(const vec3<V>& other) {
|
|
||||||
x = static_cast<T>(other.x);
|
|
||||||
y = static_cast<T>(other.y);
|
|
||||||
z = static_cast<T>(other.z);
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename N>
|
|
||||||
void operator=(const N& other) {
|
|
||||||
x = static_cast<T>(other);
|
|
||||||
y = static_cast<T>(other);
|
|
||||||
z = static_cast<T>(other);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Arithmetic
|
|
||||||
// Vectorial
|
|
||||||
/// component-wise +
|
|
||||||
template<typename V>
|
|
||||||
vec3<T> operator+(const vec3<V>& other) const { return vec3<T>(x + static_cast<T>(other.x), y + static_cast<T>(other.y), z + static_cast<T>(other.z)); };
|
|
||||||
/// component-wise -
|
|
||||||
template<typename V>
|
|
||||||
vec3<T> operator-(const vec3<V>& other) const { return vec3<T>(x - static_cast<T>(other.x), y - static_cast<T>(other.y), z - static_cast<T>(other.z)); };
|
|
||||||
/// component-wise *
|
|
||||||
template<typename V>
|
|
||||||
vec3<T> operator*(const vec3<V>& other) const { return vec3<T>(x * static_cast<T>(other.x), y * static_cast<T>(other.y), z * static_cast<T>(other.z)); };
|
|
||||||
/// component-wise /
|
|
||||||
template<typename V>
|
|
||||||
vec3<T> operator/(const vec3<V>& other) const { return vec3<T>(x / static_cast<T>(other.x), y / static_cast<T>(other.y), z / static_cast<T>(other.z)); };
|
|
||||||
/// component-wise %
|
|
||||||
template<typename V>
|
|
||||||
vec3<T> operator%(const vec3<V>& other) const { return vec3<T>(x % static_cast<T>(other.x), y % static_cast<T>(other.y), z % static_cast<T>(other.z)); };
|
|
||||||
|
|
||||||
/// component-wise assignment+=
|
|
||||||
template<typename V>
|
|
||||||
void operator+=(const vec3<V>& other) {
|
|
||||||
x += static_cast<T>(other.x);
|
|
||||||
y += static_cast<T>(other.y);
|
|
||||||
z += static_cast<T>(other.z);
|
|
||||||
};
|
|
||||||
/// component-wise assignment-=
|
|
||||||
template<typename V>
|
|
||||||
void operator-=(const vec3<V>& other) {
|
|
||||||
x -= static_cast<T>(other.x);
|
|
||||||
y -= static_cast<T>(other.y);
|
|
||||||
z -= static_cast<T>(other.z);
|
|
||||||
};
|
|
||||||
/// component-wise assignment*=
|
|
||||||
template<typename V>
|
|
||||||
void operator*=(const vec3<V>& other) {
|
|
||||||
x *= static_cast<T>(other.x);
|
|
||||||
y *= static_cast<T>(other.y);
|
|
||||||
z *= static_cast<T>(other.z);
|
|
||||||
};
|
|
||||||
/// component-wise assignment/=
|
|
||||||
template<typename V>
|
|
||||||
void operator/=(const vec3<V>& other) {
|
|
||||||
x /= static_cast<T>(other.x);
|
|
||||||
y /= static_cast<T>(other.y);
|
|
||||||
z /= static_cast<T>(other.z);
|
|
||||||
};
|
|
||||||
/// component-wise assignment%=
|
|
||||||
template<typename V>
|
|
||||||
void operator%=(const vec3<V>& other) {
|
|
||||||
x %= static_cast<T>(other.x);
|
|
||||||
y %= static_cast<T>(other.y);
|
|
||||||
z %= static_cast<T>(other.z);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Scalar
|
|
||||||
/// component-wise +
|
|
||||||
template<typename N>
|
|
||||||
vec3<T> operator+(const N& other) const { return vec3<T>(x + static_cast<T>(other.x), y + static_cast<T>(other.y), z + static_cast<T>(other.z)); };
|
|
||||||
/// component-wise -
|
|
||||||
template<typename N>
|
|
||||||
vec3<T> operator-(const N& other) const { return vec3<T>(x - static_cast<T>(other.x), y - static_cast<T>(other.y), z - static_cast<T>(other.z)); };
|
|
||||||
/// component-wise *
|
|
||||||
template<typename N>
|
|
||||||
vec3<T> operator*(const N& other) const { return vec3<T>(x * static_cast<T>(other.x), y * static_cast<T>(other.y), z * static_cast<T>(other.z)); };
|
|
||||||
/// component-wise /
|
|
||||||
template<typename N>
|
|
||||||
vec3<T> operator/(const N& other) const { return vec3<T>(x / static_cast<T>(other.x), y / static_cast<T>(other.y), z / static_cast<T>(other.z)); };
|
|
||||||
/// component-wise %
|
|
||||||
template<typename N>
|
|
||||||
vec3<T> operator%(const N& other) const { return vec3<T>(x % static_cast<T>(other.x), y % static_cast<T>(other.y), z % static_cast<T>(other.z)); };
|
|
||||||
|
|
||||||
/// component-wise assignment+=
|
|
||||||
template<typename N>
|
|
||||||
void operator+=(const N& other) {
|
|
||||||
x += static_cast<T>(other.x);
|
|
||||||
y += static_cast<T>(other.y);
|
|
||||||
z += static_cast<T>(other.z);
|
|
||||||
};
|
|
||||||
/// component-wise assignment-=
|
|
||||||
template<typename N>
|
|
||||||
void operator-=(const N& other) {
|
|
||||||
x -= static_cast<T>(other.x);
|
|
||||||
y -= static_cast<T>(other.y);
|
|
||||||
z -= static_cast<T>(other.z);
|
|
||||||
};
|
|
||||||
/// component-wise assignment*=
|
|
||||||
template<typename N>
|
|
||||||
void operator*=(const N& other) {
|
|
||||||
x *= static_cast<T>(other.x);
|
|
||||||
y *= static_cast<T>(other.y);
|
|
||||||
z *= static_cast<T>(other.z);
|
|
||||||
};
|
|
||||||
/// component-wise assignment/=
|
|
||||||
template<typename N>
|
|
||||||
void operator/=(const N& other) {
|
|
||||||
x /= static_cast<T>(other.x);
|
|
||||||
y /= static_cast<T>(other.y);
|
|
||||||
z /= static_cast<T>(other.z);
|
|
||||||
};
|
|
||||||
/// component-wise assignment%=
|
|
||||||
template<typename N>
|
|
||||||
void operator%=(const N& other) {
|
|
||||||
x %= static_cast<T>(other.x);
|
|
||||||
y %= static_cast<T>(other.y);
|
|
||||||
z %= static_cast<T>(other.z);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Comparison
|
|
||||||
// Vectorial
|
|
||||||
/// component-wise comparison == (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator==(const vec3<N>& other) const { return x == other.x and y == other.y and z == other.z; };
|
|
||||||
/// component-wise comparison < (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator<(const vec3<N>& other) const { return x < other.x and y < other.y and z < other.z; };
|
|
||||||
/// component-wise comparison > (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator>(const vec3<N>& other) const { return x > other.x and y > other.y and z > other.z; };
|
|
||||||
|
|
||||||
/// component-wise comparison != (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator!=(const vec3<N>& other) const { return x == other.x and y == other.y and z == other.z; };
|
|
||||||
/// component-wise comparison <= (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator<=(const vec3<N>& other) const { return x > other.x and y > other.y and z > other.z; };
|
|
||||||
/// component-wise comparison >= (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator>=(const vec3<N>& other) const { return x < other.x and y < other.y and z < other.z; };
|
|
||||||
|
|
||||||
// Scalar
|
|
||||||
/// component-wise comparison == (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator==(const N& other) const { return x == other.x and y == other.y and z == other.z; };
|
|
||||||
/// component-wise comparison < (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator<(const N& other) const { return x < other.x and y < other.y and z < other.z; };
|
|
||||||
/// component-wise comparison > (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator>(const N& other) const { return x > other.x and y > other.y and z > other.z; };
|
|
||||||
|
|
||||||
/// component-wise comparison != (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator!=(const N& other) const { return x == other.x and y == other.y and z == other.z; };
|
|
||||||
/// component-wise comparison <= (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator<=(const N& other) const { return x > other.x and y > other.y and z > other.z; };
|
|
||||||
/// component-wise comparison >= (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator>=(const N& other) const { return x < other.x and y < other.y and z < other.z; };
|
|
||||||
// Functional
|
|
||||||
/// Returns the absolute value of the vector
|
|
||||||
inline float abs() const { return std::sqrt(static_cast<float>(x * x) + static_cast<float>(y * y) + static_cast<float>(z * z)); };/// Returns the min of the components
|
|
||||||
inline T min() const { return std::min_element(cbegin(), cend()); };
|
|
||||||
/// Returns the max of the components
|
|
||||||
inline T max() const { return std::max_element(cbegin(), cend()); };
|
|
||||||
/// Scalar product
|
|
||||||
template<typename V>
|
|
||||||
inline vec3<T> dot(const vec3<V>& other) { return vec3<T>(x * static_cast<T>(other.x) + y * static_cast<T>(other.y) + z * static_cast<T>(other.z)); };
|
|
||||||
// Utility
|
|
||||||
std::string to_string() const { return "(" + std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(z) + ")"; };
|
|
||||||
struct Iterator {
|
|
||||||
public:
|
|
||||||
using value_type = T;
|
|
||||||
Iterator() : ptr(nullptr) {};
|
|
||||||
Iterator(T* ptr) : ptr(ptr) {};
|
|
||||||
T& operator*() { return *ptr; };
|
|
||||||
Iterator& operator=(const Iterator& other) {
|
|
||||||
ptr = other.ptr;
|
|
||||||
return *this;
|
|
||||||
};
|
|
||||||
Iterator& operator++() { ptr += sizeof(T); return *this; };
|
|
||||||
Iterator operator++(int) { auto copy = *this; ptr += sizeof(T); return copy; };
|
|
||||||
friend int operator-(Iterator lhs, Iterator rhs) {
|
|
||||||
return lhs.ptr - rhs.ptr;
|
|
||||||
};
|
|
||||||
bool operator==(const Iterator& other) const { return ptr == other.ptr; };
|
|
||||||
// bool operator!=(const Iterator& other) const { return ptr != other.ptr; };
|
|
||||||
private:
|
|
||||||
T* ptr;
|
|
||||||
};
|
|
||||||
const Iterator cbegin() const { return Iterator(&x); };
|
|
||||||
const Iterator cend() const { return Iterator(&z); };
|
|
||||||
const Iterator begin() const { return Iterator(&x); };
|
|
||||||
const Iterator end() const { return Iterator(&z); };
|
|
||||||
|
|
||||||
}; // vec3
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Class containing 4 numbers
|
|
||||||
*/
|
|
||||||
template<typename T>
|
|
||||||
class vec4 {
|
|
||||||
public:
|
|
||||||
// Constructors
|
|
||||||
/// Default constructor
|
|
||||||
vec4() : x(0), y(0), z(0), w(0) {};
|
|
||||||
/// Create a vec4 from n n n n
|
|
||||||
template<typename N0, typename N1, typename N2, typename N3>
|
|
||||||
vec4(N0 n0, N1 n1, N2 n2, N3 n3) : x(static_cast<T>(n0)), y(static_cast<T>(n1)), z(static_cast<T>(n2)), w(static_cast<T>(n3)) {};
|
|
||||||
/// Create a vec4 from n n vec2
|
|
||||||
template<typename N0, typename N1, typename V0>
|
|
||||||
vec4(N0 n0, N1 n1, const vec2<V0>& v0) : x(static_cast<T>(n0)), y(static_cast<T>(n1)), z(static_cast<T>(v0.x)), w(static_cast<T>(v0.y)) {};
|
|
||||||
/// Create a vec4 from n vec2 n
|
|
||||||
template<typename N0, typename V0, typename N1>
|
|
||||||
vec4(N0 n0, const vec2<V0>& v0, N1 n1) : x(static_cast<T>(n0)), y(static_cast<T>(v0.x)), z(static_cast<T>(v0.y)), w(static_cast<T>(n1)) {};
|
|
||||||
/// Create a vec4 from n vec3
|
|
||||||
template<typename N0, typename V0>
|
|
||||||
vec4(N0 n0, const vec3<V0>& v0) : x(static_cast<T>(n0)), y(static_cast<T>(v0.x)), z(static_cast<T>(v0.y)), w(static_cast<T>(v0.z)) {};
|
|
||||||
/// Create a vec4 from vec2 n n
|
|
||||||
template<typename V0, typename N0, typename N1>
|
|
||||||
vec4(const vec2<V0>& v0, N0 n0, N1 n1) : x(static_cast<T>(v0.x)), y(static_cast<T>(v0.y)), z(static_cast<T>(n0)), w(static_cast<T>(n1)) {};
|
|
||||||
/// Create a vec4 from vec2 vec2
|
|
||||||
template<typename V0, typename V1>
|
|
||||||
vec4(const vec2<V0>& v0, const vec2<V1>& v1) : x(static_cast<T>(v0.x)), y(static_cast<T>(v0.y)), z(static_cast<T>(v1.x)), w(static_cast<T>(v1.y)) {};
|
|
||||||
/// Create a vec4 from vec3 n
|
|
||||||
template<typename V0, typename N0>
|
|
||||||
vec4(const vec3<V0>& v0, N0 n0) : x(static_cast<T>(v0.x)), y(static_cast<T>(v0.y)), z(static_cast<T>(v0.z)), w(static_cast<T>(n0)) {};
|
|
||||||
/// Create a vec4 from vec4
|
|
||||||
template<typename V0>
|
|
||||||
vec4(const vec4<V0>& v0) : x(static_cast<T>(v0.x)), y(static_cast<T>(v0.y)), z(static_cast<T>(v0.z)), w(static_cast<T>(v0.w)) {};
|
|
||||||
// Values
|
|
||||||
T x;
|
|
||||||
T y;
|
|
||||||
T z;
|
|
||||||
T w;
|
|
||||||
// Assignment
|
|
||||||
/// component-wise assignment
|
|
||||||
template<typename V>
|
|
||||||
void operator=(const vec4<V>& other) {
|
|
||||||
x = static_cast<T>(other.x);
|
|
||||||
y = static_cast<T>(other.y);
|
|
||||||
z = static_cast<T>(other.z);
|
|
||||||
w = static_cast<T>(other.w);
|
|
||||||
};
|
|
||||||
|
|
||||||
template<typename N>
|
|
||||||
void operator=(const N& other) {
|
|
||||||
x = static_cast<T>(other);
|
|
||||||
y = static_cast<T>(other);
|
|
||||||
z = static_cast<T>(other);
|
|
||||||
w = static_cast<T>(other);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Arithmetic
|
|
||||||
// Vectorial
|
|
||||||
/// component-wise +
|
|
||||||
template<typename V>
|
|
||||||
vec4<T> operator+(const vec4<V>& other) const { return vec4<T>(x + static_cast<T>(other.x), y + static_cast<T>(other.y), z + static_cast<T>(other.z), w + static_cast<T>(other.w)); };
|
|
||||||
/// component-wise -
|
|
||||||
template<typename V>
|
|
||||||
vec4<T> operator-(const vec4<V>& other) const { return vec4<T>(x - static_cast<T>(other.x), y - static_cast<T>(other.y), z - static_cast<T>(other.z), w - static_cast<T>(other.w)); };
|
|
||||||
/// component-wise *
|
|
||||||
template<typename V>
|
|
||||||
vec4<T> operator*(const vec4<V>& other) const { return vec4<T>(x * static_cast<T>(other.x), y * static_cast<T>(other.y), z * static_cast<T>(other.z), w * static_cast<T>(other.w)); };
|
|
||||||
/// component-wise /
|
|
||||||
template<typename V>
|
|
||||||
vec4<T> operator/(const vec4<V>& other) const { return vec4<T>(x / static_cast<T>(other.x), y / static_cast<T>(other.y), z / static_cast<T>(other.z), w / static_cast<T>(other.w)); };
|
|
||||||
/// component-wise %
|
|
||||||
template<typename V>
|
|
||||||
vec4<T> operator%(const vec4<V>& other) const { return vec4<T>(x % static_cast<T>(other.x), y % static_cast<T>(other.y), z % static_cast<T>(other.z), w % static_cast<T>(other.w)); };
|
|
||||||
|
|
||||||
/// component-wise assignment+=
|
|
||||||
template<typename V>
|
|
||||||
void operator+=(const vec4<V>& other) {
|
|
||||||
x += static_cast<T>(other.x);
|
|
||||||
y += static_cast<T>(other.y);
|
|
||||||
z += static_cast<T>(other.z);
|
|
||||||
w += static_cast<T>(other.w);
|
|
||||||
};
|
|
||||||
/// component-wise assignment-=
|
|
||||||
template<typename V>
|
|
||||||
void operator-=(const vec4<V>& other) {
|
|
||||||
x -= static_cast<T>(other.x);
|
|
||||||
y -= static_cast<T>(other.y);
|
|
||||||
z -= static_cast<T>(other.z);
|
|
||||||
w -= static_cast<T>(other.w);
|
|
||||||
};
|
|
||||||
/// component-wise assignment*=
|
|
||||||
template<typename V>
|
|
||||||
void operator*=(const vec4<V>& other) {
|
|
||||||
x *= static_cast<T>(other.x);
|
|
||||||
y *= static_cast<T>(other.y);
|
|
||||||
z *= static_cast<T>(other.z);
|
|
||||||
w *= static_cast<T>(other.w);
|
|
||||||
};
|
|
||||||
/// component-wise assignment/=
|
|
||||||
template<typename V>
|
|
||||||
void operator/=(const vec4<V>& other) {
|
|
||||||
x /= static_cast<T>(other.x);
|
|
||||||
y /= static_cast<T>(other.y);
|
|
||||||
z /= static_cast<T>(other.z);
|
|
||||||
w /= static_cast<T>(other.w);
|
|
||||||
};
|
|
||||||
/// component-wise assignment%=
|
|
||||||
template<typename V>
|
|
||||||
void operator%=(const vec4<V>& other) {
|
|
||||||
x %= static_cast<T>(other.x);
|
|
||||||
y %= static_cast<T>(other.y);
|
|
||||||
z %= static_cast<T>(other.z);
|
|
||||||
w %= static_cast<T>(other.w);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Scalar
|
|
||||||
/// component-wise +
|
|
||||||
template<typename N>
|
|
||||||
vec4<T> operator+(const N& other) const { return vec4<T>(x + static_cast<T>(other.x), y + static_cast<T>(other.y), z + static_cast<T>(other.z), w + static_cast<T>(other.w)); };
|
|
||||||
/// component-wise -
|
|
||||||
template<typename N>
|
|
||||||
vec4<T> operator-(const N& other) const { return vec4<T>(x - static_cast<T>(other.x), y - static_cast<T>(other.y), z - static_cast<T>(other.z), w - static_cast<T>(other.w)); };
|
|
||||||
/// component-wise *
|
|
||||||
template<typename N>
|
|
||||||
vec4<T> operator*(const N& other) const { return vec4<T>(x * static_cast<T>(other.x), y * static_cast<T>(other.y), z * static_cast<T>(other.z), w * static_cast<T>(other.w)); };
|
|
||||||
/// component-wise /
|
|
||||||
template<typename N>
|
|
||||||
vec4<T> operator/(const N& other) const { return vec4<T>(x / static_cast<T>(other.x), y / static_cast<T>(other.y), z / static_cast<T>(other.z), w / static_cast<T>(other.w)); };
|
|
||||||
/// component-wise %
|
|
||||||
template<typename N>
|
|
||||||
vec4<T> operator%(const N& other) const { return vec4<T>(x % static_cast<T>(other.x), y % static_cast<T>(other.y), z % static_cast<T>(other.z), w % static_cast<T>(other.w)); };
|
|
||||||
|
|
||||||
/// component-wise assignment+=
|
|
||||||
template<typename N>
|
|
||||||
void operator+=(const N& other) {
|
|
||||||
x += static_cast<T>(other.x);
|
|
||||||
y += static_cast<T>(other.y);
|
|
||||||
z += static_cast<T>(other.z);
|
|
||||||
w += static_cast<T>(other.w);
|
|
||||||
};
|
|
||||||
/// component-wise assignment-=
|
|
||||||
template<typename N>
|
|
||||||
void operator-=(const N& other) {
|
|
||||||
x -= static_cast<T>(other.x);
|
|
||||||
y -= static_cast<T>(other.y);
|
|
||||||
z -= static_cast<T>(other.z);
|
|
||||||
w -= static_cast<T>(other.w);
|
|
||||||
};
|
|
||||||
/// component-wise assignment*=
|
|
||||||
template<typename N>
|
|
||||||
void operator*=(const N& other) {
|
|
||||||
x *= static_cast<T>(other.x);
|
|
||||||
y *= static_cast<T>(other.y);
|
|
||||||
z *= static_cast<T>(other.z);
|
|
||||||
w *= static_cast<T>(other.w);
|
|
||||||
};
|
|
||||||
/// component-wise assignment/=
|
|
||||||
template<typename N>
|
|
||||||
void operator/=(const N& other) {
|
|
||||||
x /= static_cast<T>(other.x);
|
|
||||||
y /= static_cast<T>(other.y);
|
|
||||||
z /= static_cast<T>(other.z);
|
|
||||||
w /= static_cast<T>(other.w);
|
|
||||||
};
|
|
||||||
/// component-wise assignment%=
|
|
||||||
template<typename N>
|
|
||||||
void operator%=(const N& other) {
|
|
||||||
x %= static_cast<T>(other.x);
|
|
||||||
y %= static_cast<T>(other.y);
|
|
||||||
z %= static_cast<T>(other.z);
|
|
||||||
w %= static_cast<T>(other.w);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Comparison
|
|
||||||
// Vectorial
|
|
||||||
/// component-wise comparison == (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator==(const vec4<N>& other) const { return x == other.x and y == other.y and z == other.z and w == other.w; };
|
|
||||||
/// component-wise comparison < (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator<(const vec4<N>& other) const { return x < other.x and y < other.y and z < other.z and w < other.w; };
|
|
||||||
/// component-wise comparison > (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator>(const vec4<N>& other) const { return x > other.x and y > other.y and z > other.z and w > other.w; };
|
|
||||||
|
|
||||||
/// component-wise comparison != (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator!=(const vec4<N>& other) const { return x == other.x and y == other.y and z == other.z and w == other.w; };
|
|
||||||
/// component-wise comparison <= (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator<=(const vec4<N>& other) const { return x > other.x and y > other.y and z > other.z and w > other.w; };
|
|
||||||
/// component-wise comparison >= (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator>=(const vec4<N>& other) const { return x < other.x and y < other.y and z < other.z and w < other.w; };
|
|
||||||
|
|
||||||
// Scalar
|
|
||||||
/// component-wise comparison == (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator==(const N& other) const { return x == other.x and y == other.y and z == other.z and w == other.w; };
|
|
||||||
/// component-wise comparison < (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator<(const N& other) const { return x < other.x and y < other.y and z < other.z and w < other.w; };
|
|
||||||
/// component-wise comparison > (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator>(const N& other) const { return x > other.x and y > other.y and z > other.z and w > other.w; };
|
|
||||||
|
|
||||||
/// component-wise comparison != (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator!=(const N& other) const { return x == other.x and y == other.y and z == other.z and w == other.w; };
|
|
||||||
/// component-wise comparison <= (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator<=(const N& other) const { return x > other.x and y > other.y and z > other.z and w > other.w; };
|
|
||||||
/// component-wise comparison >= (and)
|
|
||||||
template<typename N>
|
|
||||||
bool operator>=(const N& other) const { return x < other.x and y < other.y and z < other.z and w < other.w; };
|
|
||||||
// Functional
|
|
||||||
/// Returns the absolute value of the vector
|
|
||||||
inline float abs() const { return std::sqrt(static_cast<float>(x * x) + static_cast<float>(y * y) + static_cast<float>(z * z) + static_cast<float>(w * w)); };/// Returns the min of the components
|
|
||||||
inline T min() const { return std::min_element(cbegin(), cend()); };
|
|
||||||
/// Returns the max of the components
|
|
||||||
inline T max() const { return std::max_element(cbegin(), cend()); };
|
|
||||||
/// Scalar product
|
|
||||||
template<typename V>
|
|
||||||
inline vec4<T> dot(const vec4<V>& other) { return vec4<T>(x * static_cast<T>(other.x) + y * static_cast<T>(other.y) + z * static_cast<T>(other.z) + w * static_cast<T>(other.w)); };
|
|
||||||
// Utility
|
|
||||||
std::string to_string() const { return "(" + std::to_string(x) + ", " + std::to_string(y) + ", " + std::to_string(z) + ", " + std::to_string(w) + ")"; };
|
|
||||||
struct Iterator {
|
|
||||||
public:
|
|
||||||
using value_type = T;
|
|
||||||
Iterator() : ptr(nullptr) {};
|
|
||||||
Iterator(T* ptr) : ptr(ptr) {};
|
|
||||||
T& operator*() { return *ptr; };
|
|
||||||
Iterator& operator=(const Iterator& other) {
|
|
||||||
ptr = other.ptr;
|
|
||||||
return *this;
|
|
||||||
};
|
|
||||||
Iterator& operator++() { ptr += sizeof(T); return *this; };
|
|
||||||
Iterator operator++(int) { auto copy = *this; ptr += sizeof(T); return copy; };
|
|
||||||
friend int operator-(Iterator lhs, Iterator rhs) {
|
|
||||||
return lhs.ptr - rhs.ptr;
|
|
||||||
};
|
|
||||||
bool operator==(const Iterator& other) const { return ptr == other.ptr; };
|
|
||||||
// bool operator!=(const Iterator& other) const { return ptr != other.ptr; };
|
|
||||||
private:
|
|
||||||
T* ptr;
|
|
||||||
};
|
|
||||||
const Iterator cbegin() const { return Iterator(&x); };
|
|
||||||
const Iterator cend() const { return Iterator(&w); };
|
|
||||||
const Iterator begin() const { return Iterator(&x); };
|
|
||||||
const Iterator end() const { return Iterator(&w); };
|
|
||||||
|
|
||||||
}; // vec4
|
|
||||||
|
|
||||||
using vec2f = vec2<float>;
|
|
||||||
using vec2d = vec2<double>;
|
|
||||||
using vec2i = vec2<int>;
|
|
||||||
using vec2u = vec2<unsigned int>;
|
|
||||||
|
|
||||||
using vec3f = vec3<float>;
|
|
||||||
using vec3d = vec3<double>;
|
|
||||||
using vec3i = vec3<int>;
|
|
||||||
using vec3u = vec3<unsigned int>;
|
|
||||||
|
|
||||||
using vec4f = vec4<float>;
|
|
||||||
using vec4d = vec4<double>;
|
|
||||||
using vec4i = vec4<int>;
|
|
||||||
using vec4u = vec4<unsigned int>;
|
|
||||||
|
|
||||||
} // namespace gz
|
|
Loading…
Reference in New Issue
Block a user