Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module ru.nsk.sampler.math.vector;
- //math stuff (sqrt, cos, sin)
- import std.math;
- //'zip', 'repeat', 'take' lie here
- import std.range;
- //'map' and 'reduce' lie here
- import std.algorithm;
- //to make char upper case + 'format' - a sprintf-like function
- import std.string;
- public static immutable X = 0;
- public static immutable Y = 1;
- public static immutable Z = 2;
- public static immutable W = 3;
- public struct Vector4
- {
- public
- {
- static immutable DIMENSION = 4;
- this(Range)(Range coords) pure
- {
- auto i = 0u;
- foreach (immutable coord; coords)
- {
- this.coords[i] = coord;
- i++;
- }
- }
- this(in float x, in float y, in float z, in float w) pure nothrow
- {
- coords[X] = x;
- coords[Y] = y;
- coords[Z] = z;
- coords[W] = w;
- }
- this(in float x, in float y, in float z) pure nothrow
- {
- coords[X] = x;
- coords[Y] = y;
- coords[Z] = z;
- }
- @property float[] get() pure nothrow
- {
- return coords;
- }
- @property const (float[]) get() const pure nothrow
- {
- return coords;
- }
- alias get this;
- @property auto opDispatch(const string swizzling)() const pure
- if (swizzling.length == DIMENSION || swizzling.length == DIMENSION - 1)
- {
- immutable expression = "coords".repeat().take(swizzling.length).zip(swizzling)
- .map!( a => format("%s[%c]", a[0], a[1].toUpper()) ).reduce!((a,b) => a ~ "," ~ b);
- mixin( "return Vector4 (" ~ expression ~ ");" );
- }
- auto opEquals(in Vector4 other) const pure nothrow
- {
- return this.coords == other.coords;
- }
- }
- private
- {
- float[DIMENSION] coords = [0.0f, 0.0f, 0.0f, 1.0f,];
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement