From 58464ec91cb5a45352297b6e6b574899ba31b339 Mon Sep 17 00:00:00 2001 From: al1-ce Date: Sat, 11 May 2024 22:29:26 +0300 Subject: [PATCH] fixes --- core/sily/array.d | 5 +-- core/sily/file.d | 11 +++-- core/sily/path.d | 11 +++-- core/sily/random.d | 24 ++++++----- core/sily/vector.d | 105 ++++++++++++++++++++++++--------------------- test/vector.d | 5 +++ 6 files changed, 89 insertions(+), 72 deletions(-) diff --git a/core/sily/array.d b/core/sily/array.d index af2b85c..ede348f 100644 --- a/core/sily/array.d +++ b/core/sily/array.d @@ -23,15 +23,14 @@ bool isOneOf(T)(T val, T[] vals ...) { } /** -Fills array with values `val` up to `size` if it's not 0 +Fills array with values `val` Params: arr = Array to fill val = Values to fill with Returns: Filled array */ T[] fill(T)(T[] arr, T val){ - - arr = arr.dup; + T[] arr = new T[](arr.length); for (int i = 0; i < arr.length; i ++) { arr[i] = val; diff --git a/core/sily/file.d b/core/sily/file.d index 7d1cbd8..834b968 100644 --- a/core/sily/file.d +++ b/core/sily/file.d @@ -16,14 +16,14 @@ Performs chmod +x on file Params: name = Path to file */ -void chmodpx(string name) { - name = name.buildAbsolutePath(); +void chmodpx(string path) { + path = path.buildAbsolutePath(); - if (!name.exists) { + if (!path.exists) { return; } - name.setAttributes(name.getAttributes | octal!700); + path.setAttributes(path.getAttributes | octal!700); } /** @@ -36,8 +36,7 @@ string readFile(string path) { path = path.buildAbsolutePath(); if (!path.isFile) { - writefln("ERROR: Unable to find file at '%s'.", path); - throw new FileException("Unable to read file."); + throw new FileException("Unable to find file at '%s'.", path); } return readText(path); diff --git a/core/sily/path.d b/core/sily/path.d index c332995..1745b9c 100644 --- a/core/sily/path.d +++ b/core/sily/path.d @@ -29,17 +29,20 @@ string buildRelativePath(string p) { /** Returns array of files/dirs from path Params: - pathname = Path to dir + path = Path to dir Returns: */ -string[] listdir(string pathname) { +string[] listdir(string path, bool listDirs = true, bool listFiles = true) { import std.algorithm; import std.array; import std.file; import std.path; - return std.file.dirEntries(pathname, SpanMode.shallow) - .filter!(a => a.isFile) + path = buildAbsolutePath(path); + if (!exists(path) || !isDir(path)) return []; + + return std.file.dirEntries(path, SpanMode.shallow) + .filter!(a => listFiles ? true : a.isFile || listDirs ? true : a.isDir) .map!((return a) => baseName(a.name)) .array; } diff --git a/core/sily/random.d b/core/sily/random.d index 2a2db75..6b1cfe0 100644 --- a/core/sily/random.d +++ b/core/sily/random.d @@ -7,7 +7,7 @@ Random number generation utils module sily.random; import std.math; -import std.random; +import std.random: StdRandom = Random, unpredictableSeed; import std.algorithm.comparison; import std.conv: to; import std.datetime: Clock; @@ -34,15 +34,15 @@ randf(4, 20); rand!ulong(); --- +/ -struct RNG { +struct Random { private uint _seed = defaultSeed; - private Random _rnd; - private ulong _calls; + private StdRandom _rnd; + private size_t _calls; /// Creates RNG with set seed this(uint p_seed) { _seed = p_seed; - _rnd = Random(_seed); + _rnd = StdRandom(_seed); } /// Randomizes seed @@ -56,13 +56,14 @@ struct RNG { @property public void seed(uint p_seed) { _seed = p_seed; _rnd.seed(_seed); + _calls = 0; } /// Returns current seed @property public uint seed() { return _seed; } /// Alias to std.random default seed - public static alias defaultSeed = Random.defaultSeed; + public static alias defaultSeed = StdRandom.defaultSeed; /// Alias to std.random unpredictable seed public static alias randomSeed = unpredictableSeed; @@ -76,7 +77,7 @@ struct RNG { alias randi = rand!int; /// Ditto alias randl = rand!long; - + template rand(T) { static if(isFloatingPoint!T) { /// Returns random value between 0 and T.max or custom min and max @@ -109,9 +110,12 @@ struct RNG { } /// Skips current random value - public void skip() { - _rnd.popFront(); - _calls ++; + public void skip(size_t amount = 1) { + while (amount > 0) { + _rnd.popFront(); + ++_calls; + --amount; + } } /// Skips N amount of random values diff --git a/core/sily/vector.d b/core/sily/vector.d index 3f71615..1545a3a 100644 --- a/core/sily/vector.d +++ b/core/sily/vector.d @@ -90,6 +90,12 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { /// Alias to vector size enum size_t size = N; + static if (isFloatingPoint!T) { + alias F = T; + } else { + alias F = double; + } + /** Constructs Vector from components. If no components present vector will be filled with 0 @@ -192,8 +198,8 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { /// opCmp x [< > <= >=] y int opCmp(R)(in Vector!(R, N) b) const if ( isNumeric!R ) { // assert(/* this !is null && */ b !is null, "\nOP::ERROR nullptr Vector!" ~ size.to!string ~ "."); - double al = cast(double) length(); - double bl = cast(double) b.length(); + F al = cast(F) length(); + F bl = cast(F) b.length(); if (al == bl) return 0; if (al < bl) return -1; return 1; @@ -219,9 +225,9 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { } /// Ditto - dvec!N opUnary(string op)() if (op == "~" && !isFloatingPoint!T) { + Vector!(F, N) opUnary(string op)() if (op == "~" && !isFloatingPoint!T) { // assert(this !is null, "\nOP::ERROR nullptr Vector!" ~ size.to!string ~ "."); - dvec!N ret; + Vector!(F, N) ret; foreach (i; 0 .. size) { ret[i] = 1.0 / data[i]; } return ret; } @@ -367,9 +373,9 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { } static if(N == 3) { - static alias forward = () => VecType(0, 0, -1); + static alias forward = () => VecType(0, 0, 1); /// Ditto - static alias back = () => VecType(0, 0, 1); + static alias back = () => VecType(0, 0, -1); /// Ditto static alias left = () => VecType(-1, 0, 0); /// Ditto @@ -385,13 +391,13 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { /* -------------------------------------------------------------------------- */ /// Returns vector length - double length() const { - return sqrt(cast(double) lengthSquared); + F length() const { + return sqrt(cast(F) lengthSquared); } /// Returns squared vector length - T lengthSquared() const { - T l = 0; + F lengthSquared() const { + F l = 0; foreach (i; 0 .. size) { l += data[i] * data[i]; } return l; } @@ -402,8 +408,8 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { b = Vector to calculate distance to Returns: Distance */ - T distanceSquaredTo(VecType b) { - T dist = 0; + F distanceSquaredTo(VecType b) { + F dist = 0; foreach (i; 0 .. size) { dist += (data[i] - b.data[i]) * (data[i] - b.data[i]); } return dist; } @@ -414,8 +420,8 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { b = Vector Returns: Distance */ - double distanceTo(VecType b) { - return sqrt(cast(double) distanceSquaredTo(b)); + F distanceTo(VecType b) { + return sqrt(cast(F) distanceSquaredTo(b)); } /** @@ -432,10 +438,10 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { /// Normalises vector VecType normalized() { - T q = lengthSquared; + F q = lengthSquared; if (q == 0 || q.isClose(0, float.epsilon)) return this; VecType ret; - double l = sqrt(cast(double) lengthSquared); + F l = sqrt(cast(F) lengthSquared); foreach (i; 0 .. size) { ret[i] = cast(T) (data[i] / l); } return ret; } @@ -444,9 +450,9 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { /// Normalises vector in place VecType normalize() { - T q = lengthSquared; + F q = lengthSquared; if (q == 0 || q.isClose(0, float.epsilon)) return this; - double l = sqrt(cast(double) lengthSquared); + F l = sqrt(cast(F) lengthSquared); foreach (i; 0 .. size) { data[i] = cast(T) (data[i] / l); } return this; } @@ -466,9 +472,9 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { b = Vector Returns: dot product */ - double dot(VecType b) { - double d = 0; - foreach (i; 0 .. size) { d += cast(double) data[i] * cast(double) b.data[i]; } + F dot(VecType b) { + F d = 0; + foreach (i; 0 .. size) { d += cast(F) data[i] * cast(F) b.data[i]; } return d; } @@ -508,7 +514,7 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { to = Vector to interpolate to weight = Interpolation weight in range [0.0, 1.0] */ - VecType lerp(VecType to, double weight) { + VecType lerp(VecType to, F weight) { VecType ret; foreach (i; 0 .. size) { ret[i] = data[i] + (weight * (to.data[i] - data[i])); } return ret; @@ -643,7 +649,7 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { /// Calculates reflected vector to normal VecType reflect(VecType normal) { - double d = dot(normal); + F d = dot(normal); VecType ret; foreach (i; 0..size) { ret[i] = cast(T) (data[i] - (2.0 * normal.data[i]) * d); @@ -655,9 +661,9 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { Calculates direction of refracted ray where this is incoming ray, n is normal vector and r is refractive ratio +/ - VecType refract(VecType n, double r) { - double dt = dot(n); - double d = 1.0 - r * r * (1.0 - dt * dt); + VecType refract(VecType n, F r) { + F dt = dot(n); + F d = 1.0 - r * r * (1.0 - dt * dt); VecType ret = 0; if (d >= 0) { foreach (i; 0..size) { @@ -669,9 +675,9 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { /// Moves vector toward target - VecType moveTowards(VecType to, double maxDist) { - double[size] d; - double val = 0; + VecType moveTowards(VecType to, F maxDist) { + F[size] d; + F val = 0; foreach (i; 0..size) { d[i] = to[i] - data[i]; @@ -681,7 +687,7 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { if (val == 0 || (maxDist >= 0 && val <= maxDist * maxDist)) return to; VecType ret; - double dist = sqrt(val); + F dist = sqrt(val); foreach (i; 0..size) { ret[i] = cast(T) (data[i] + d[i] / dist * maxDist); @@ -691,22 +697,23 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { } /// Limits length of vector and returns resulting vector - VecType limitLength(double p_min, double p_max) { + /// Limiter of 0 is ignored + VecType limitLength(F p_min, F p_max) { VecType ret = VecType(data); - double len = length; + F len = lengthSquared; if (len > 0.0) { len = sqrt(len); - if (len < p_min) { - double scale = p_min / len; + if (len < p_min && p_min != 0) { + F scale = p_min / len; foreach (i; 0..size) { ret[i] = cast(T) (ret[i] * scale); } } - if (len > p_max) { - double scale = p_max / len; + if (len > p_max && p_max != 0) { + F scale = p_max / len; foreach (i; 0..size) { ret[i] = cast(T) (ret[i] * scale); } @@ -718,38 +725,38 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { static if (N == 2) { /// Calculates angle between this vector and v2 from [0, 0] - double angle(VecType v2) { + F angle(VecType v2) { static if (isFloatingPoint!T) { return atan2(v2.y - data[1], v2.x - data[0]); } else { - return atan2(cast(double) v2.y - data[1], cast(double) v2.x - data[0]); + return atan2(cast(F) v2.y - data[1], cast(F) v2.x - data[0]); } } /// Calculates angle between line this->to and X ordinate - double angleTo(VecType to) { + F angleTo(VecType to) { return acos(dot(to).clamp(-1.0, 1.0)); } /// Calculates cross product of this and b, assumes that Z = 0 - double cross(VecType b) { + F cross(VecType b) { return data[0] * b.y - data[1] * b.x; } /// Rotates vector by angle - VecType rotate(double angle) { - double sina = sin(angle); - double cosa = cos(angle); + VecType rotate(F angle) { + F sina = sin(angle); + F cosa = cos(angle); return VecType( cast(T) (data[0] * cosa - data[1] * sina), cast(T) (data[0] * sina + data[1] * cosa) ); } } static if (N == 3) { - /// Calculates angle between this vector and v2 from [0, 0] - double angle(VecType v2) { + /// Calculates angle between this vector and v2 from [0, 0, 0] + F angle(VecType v2) { VecType cr = cross(v2); - double len = cr.length(); - double dot = dot(v2); + F len = cr.length(); + F dot = dot(v2); return atan2(len, dot); } @@ -771,11 +778,11 @@ struct Vector(T, size_t N) if (isNumeric!T && N > 0) { /// Returns vector perpendicular to this VecType perpendicular() { - double p_min = cast(double) data[0].abs; + F p_min = cast(F) data[0].abs; VecType cardinal = VecType(1, 0, 0); if (data[1].abs < p_min) { - p_min = cast(double) data[1].abs; + p_min = cast(F) data[1].abs; cardinal = VecType(0, 1, 0); } diff --git a/test/vector.d b/test/vector.d index 9b2f290..f39d053 100644 --- a/test/vector.d +++ b/test/vector.d @@ -153,5 +153,10 @@ void main() { writeln(col(1, 2, 3, 4).rgaabargb); + writeln("limitlen(vec, 0, 2)", vec3(1, 12, 2).limitLength(0, 2)); + writeln("limitlen(vec, 0, 2)", vec3(1, 12, 2).limitLength(0, 2).length()); + writeln("limitlen(vec, 16, 9999)", vec3(1, 12, 2).limitLength(2, 0)); + writeln("limitlen(vec, 16, 9999)", vec3(1, 16, 2).limitLength(2, 0).length()); + writeln("Completed"); }