]> git.lizzy.rs Git - minetest.git/blobdiff - src/util/numeric.h
Change BS constant from implicit double to float (#6286)
[minetest.git] / src / util / numeric.h
index e742ab24aa2a5ab7340ec83333d4e2881eb24d9e..f09c4e9f5f95dc44a5561cc3d5c4879abdd70a1d 100644 (file)
@@ -17,53 +17,27 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef UTIL_NUMERIC_HEADER
-#define UTIL_NUMERIC_HEADER
+#pragma once
 
 #include "basic_macros.h"
 #include "../irrlichttypes.h"
 #include "../irr_v2d.h"
 #include "../irr_v3d.h"
 #include "../irr_aabb3d.h"
-#include "../threading/mutex.h"
-#include <list>
-#include <map>
-#include <vector>
 
+#define rangelim(d, min, max) ((d) < (min) ? (min) : ((d) > (max) ? (max) : (d)))
+#define myfloor(x) ((x) < 0.0 ? (int)(x) - 1 : (int)(x))
+// The naive swap performs better than the xor version
+#define SWAP(t, x, y) do { \
+       t temp = x; \
+       x = y; \
+       y = temp; \
+} while (0)
 
-/*
- * This class permits to cache getFacePosition call results
- * This reduces CPU usage and vector calls
- */
-class FacePositionCache
-{
-public:
-       static std::vector<v3s16> getFacePositions(u16 d);
-private:
-       static void generateFacePosition(u16 d);
-       static std::map<u16, std::vector<v3s16> > m_cache;
-       static Mutex m_cache_mutex;
-};
-
-class IndentationRaiser
-{
-public:
-       IndentationRaiser(u16 *indentation)
-       {
-               m_indentation = indentation;
-               (*m_indentation)++;
-       }
-       ~IndentationRaiser()
-       {
-               (*m_indentation)--;
-       }
-private:
-       u16 *m_indentation;
-};
 
 inline s16 getContainerPos(s16 p, s16 d)
 {
-       return (p>=0 ? p : p-d+1) / d;
+       return (p >= 0 ? p : p - d + 1) / d;
 }
 
 inline v2s16 getContainerPos(v2s16 p, s16 d)
@@ -146,33 +120,6 @@ inline bool isInArea(v3s16 p, v3s16 d)
        );
 }
 
-#define rangelim(d, min, max) ((d) < (min) ? (min) : ((d)>(max)?(max):(d)))
-#define myfloor(x) ((x) > 0.0 ? (int)(x) : (int)(x) - 1)
-
-inline v3s16 arealim(v3s16 p, s16 d)
-{
-       if(p.X < 0)
-               p.X = 0;
-       if(p.Y < 0)
-               p.Y = 0;
-       if(p.Z < 0)
-               p.Z = 0;
-       if(p.X > d-1)
-               p.X = d-1;
-       if(p.Y > d-1)
-               p.Y = d-1;
-       if(p.Z > d-1)
-               p.Z = d-1;
-       return p;
-}
-
-// The naive swap performs better than the xor version
-#define SWAP(t, x, y) do { \
-       t temp = x;            \
-       x = y;                 \
-       y = temp;              \
-} while (0)
-
 inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
        if (p1.X > p2.X)
                SWAP(s16, p1.X, p2.X);
@@ -182,6 +129,16 @@ inline void sortBoxVerticies(v3s16 &p1, v3s16 &p2) {
                SWAP(s16, p1.Z, p2.Z);
 }
 
+inline v3s16 componentwise_min(const v3s16 &a, const v3s16 &b)
+{
+       return v3s16(MYMIN(a.X, b.X), MYMIN(a.Y, b.Y), MYMIN(a.Z, b.Z));
+}
+
+inline v3s16 componentwise_max(const v3s16 &a, const v3s16 &b)
+{
+       return v3s16(MYMAX(a.X, b.X), MYMAX(a.Y, b.Y), MYMAX(a.Z, b.Z));
+}
+
 
 /** Returns \p f wrapped to the range [-360, 360]
  *
@@ -289,11 +246,10 @@ inline s32 myround(f32 f)
 */
 inline v3s16 floatToInt(v3f p, f32 d)
 {
-       v3s16 p2(
-               (p.X + (p.X>0 ? d/2 : -d/2))/d,
-               (p.Y + (p.Y>0 ? d/2 : -d/2))/d,
-               (p.Z + (p.Z>0 ? d/2 : -d/2))/d);
-       return p2;
+       return v3s16(
+               (p.X + (p.X > 0 ? d / 2 : -d / 2)) / d,
+               (p.Y + (p.Y > 0 ? d / 2 : -d / 2)) / d,
+               (p.Z + (p.Z > 0 ? d / 2 : -d / 2)) / d);
 }
 
 /*
@@ -301,34 +257,32 @@ inline v3s16 floatToInt(v3f p, f32 d)
 */
 inline v3f intToFloat(v3s16 p, f32 d)
 {
-       v3f p2(
+       return v3f(
                (f32)p.X * d,
                (f32)p.Y * d,
                (f32)p.Z * d
        );
-       return p2;
 }
 
 // Random helper. Usually d=BS
-inline core::aabbox3d<f32> getNodeBox(v3s16 p, float d)
+inline aabb3f getNodeBox(v3s16 p, float d)
 {
-       return core::aabbox3d<f32>(
-               (float)p.X * d - 0.5*d,
-               (float)p.Y * d - 0.5*d,
-               (float)p.Z * d - 0.5*d,
-               (float)p.X * d + 0.5*d,
-               (float)p.Y * d + 0.5*d,
-               (float)p.Z * d + 0.5*d
+       return aabb3f(
+               (float)p.X * d - 0.5f * d,
+               (float)p.Y * d - 0.5f * d,
+               (float)p.Z * d - 0.5f * d,
+               (float)p.X * d + 0.5f * d,
+               (float)p.Y * d + 0.5f * d,
+               (float)p.Z * d + 0.5f * d
        );
 }
 
+
 class IntervalLimiter
 {
 public:
-       IntervalLimiter():
-               m_accumulator(0)
-       {
-       }
+       IntervalLimiter() = default;
+
        /*
                dtime: time from last call to this method
                wanted_interval: interval wanted
@@ -339,15 +293,17 @@ class IntervalLimiter
        bool step(float dtime, float wanted_interval)
        {
                m_accumulator += dtime;
-               if(m_accumulator < wanted_interval)
+               if (m_accumulator < wanted_interval)
                        return false;
                m_accumulator -= wanted_interval;
                return true;
        }
-protected:
-       float m_accumulator;
+
+private:
+       float m_accumulator = 0.0f;
 };
 
+
 /*
        Splits a list into "pages". For example, the list [1,2,3,4,5] split
        into two pages would be [1,2,3],[4,5]. This function computes the
@@ -363,29 +319,21 @@ class IntervalLimiter
 */
 inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxindex)
 {
-       if(length < 1 || pagecount < 1 || page < 1 || page > pagecount)
-       {
+       if (length < 1 || pagecount < 1 || page < 1 || page > pagecount) {
                // Special cases or invalid parameters
                minindex = maxindex = 0;
-       }
-       else if(pagecount <= length)
-       {
+       } else if(pagecount <= length) {
                // Less pages than entries in the list:
                // Each page contains at least one entry
                minindex = (length * (page-1) + (pagecount-1)) / pagecount;
                maxindex = (length * page + (pagecount-1)) / pagecount;
-       }
-       else
-       {
+       } else {
                // More pages than entries in the list:
                // Make sure the empty pages are at the end
-               if(page < length)
-               {
+               if (page < length) {
                        minindex = page-1;
                        maxindex = page;
-               }
-               else
-               {
+               } else {
                        minindex = 0;
                        maxindex = 0;
                }
@@ -394,14 +342,14 @@ inline void paging(u32 length, u32 page, u32 pagecount, u32 &minindex, u32 &maxi
 
 inline float cycle_shift(float value, float by = 0, float max = 1)
 {
-    if (value + by < 0) return max + by + value;
+    if (value + by < 0)   return value + by + max;
     if (value + by > max) return value + by - max;
     return value + by;
 }
 
 inline bool is_power_of_two(u32 n)
 {
-       return n != 0 && (n & (n-1)) == 0;
+       return n != 0 && (n & (n - 1)) == 0;
 }
 
 // Compute next-higher power of 2 efficiently, e.g. for power-of-2 texture sizes.
@@ -415,5 +363,3 @@ inline u32 npot2(u32 orig) {
        orig |= orig >> 16;
        return orig + 1;
 }
-
-#endif