Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 11ecb634 authored by Romain Guy's avatar Romain Guy
Browse files

Add various vector functions

- map()
- Bool vectors
- any(), all(), comparison functions

Example:

all(lessThan(abs(v1 - v0), epsilon))

Bug: 32984164
Test: vec_test
Change-Id: I7d030387f5a280a499ea480015b69138cef38459
parent 2a03c32a
Loading
Loading
Loading
Loading
+98 −14
Original line number Diff line number Diff line
@@ -321,6 +321,66 @@ public:
    constexpr bool PURE operator >=(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
        return !(lv < rv);
    }

    template<typename RT>
    friend inline
    constexpr VECTOR<bool> PURE equal(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
        VECTOR<bool> r;
        for (size_t i = 0; i < lv.size(); i++) {
            r[i] = lv[i] == rv[i];
        }
        return r;
    }

    template<typename RT>
    friend inline
    constexpr VECTOR<bool> PURE notEqual(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
        VECTOR<bool> r;
        for (size_t i = 0; i < lv.size(); i++) {
            r[i] = lv[i] != rv[i];
        }
        return r;
    }

    template<typename RT>
    friend inline
    constexpr VECTOR<bool> PURE lessThan(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
        VECTOR<bool> r;
        for (size_t i = 0; i < lv.size(); i++) {
            r[i] = lv[i] < rv[i];
        }
        return r;
    }

    template<typename RT>
    friend inline
    constexpr VECTOR<bool> PURE lessThanEqual(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
        VECTOR<bool> r;
        for (size_t i = 0; i < lv.size(); i++) {
            r[i] = lv[i] <= rv[i];
        }
        return r;
    }

    template<typename RT>
    friend inline
    constexpr VECTOR<bool> PURE greaterThan(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
        VECTOR<bool> r;
        for (size_t i = 0; i < lv.size(); i++) {
            r[i] = lv[i] > rv[i];
        }
        return r;
    }

    template<typename RT>
    friend inline
    constexpr VECTOR<bool> PURE greaterThanEqual(const VECTOR<T>& lv, const VECTOR<RT>& rv) {
        VECTOR<bool> r;
        for (size_t i = 0; i < lv.size(); i++) {
            r[i] = lv[i] >= rv[i];
        }
        return r;
    }
};

/*
@@ -488,6 +548,30 @@ public:
        }
        return v;
    }

    friend inline CONSTEXPR bool PURE any(const VECTOR<T>& v) {
        for (size_t i = 0; i < v.size(); i++) {
            if (v[i] != T(0)) return true;
        }
        return false;
    }

    friend inline CONSTEXPR bool PURE all(const VECTOR<T>& v) {
        bool result = true;
        for (size_t i = 0; i < v.size(); i++) {
            result &= (v[i] != T(0));
        }
        return result;
    }

    template<typename R>
    friend inline CONSTEXPR VECTOR<R> PURE map(VECTOR<T> v, const std::function<R(T)>& f) {
        VECTOR<R> result;
        for (size_t i = 0; i < v.size(); i++) {
            result[i] = f(v[i]);
        }
        return result;
    }
};

/*
+1 −0
Original line number Diff line number Diff line
@@ -118,6 +118,7 @@ typedef details::TVec2<int16_t> short2;
typedef details::TVec2<uint16_t> ushort2;
typedef details::TVec2<int8_t> byte2;
typedef details::TVec2<uint8_t> ubyte2;
typedef details::TVec2<bool> bool2;

// ----------------------------------------------------------------------------------------
}  // namespace android
+1 −0
Original line number Diff line number Diff line
@@ -124,6 +124,7 @@ typedef details::TVec3<int16_t> short3;
typedef details::TVec3<uint16_t> ushort3;
typedef details::TVec3<int8_t> byte3;
typedef details::TVec3<uint8_t> ubyte3;
typedef details::TVec3<bool> bool3;

// ----------------------------------------------------------------------------------------
}  // namespace android
+1 −0
Original line number Diff line number Diff line
@@ -121,6 +121,7 @@ typedef details::TVec4<int16_t> short4;
typedef details::TVec4<uint16_t> ushort4;
typedef details::TVec4<int8_t> byte4;
typedef details::TVec4<uint8_t> ubyte4;
typedef details::TVec4<bool> bool4;

// ----------------------------------------------------------------------------------------
}  // namespace android
+32 −0
Original line number Diff line number Diff line
@@ -187,6 +187,23 @@ TEST_F(VecTest, ComparisonOps) {
    EXPECT_FALSE(v0 == v1);
}

TEST_F(VecTest, ComparisonFunctions) {
    vec4 v0(1, 2, 3, 4);
    vec4 v1(10, 20, 30, 40);

    EXPECT_TRUE(all(equal(v0, v0)));
    EXPECT_TRUE(all(notEqual(v0, v1)));
    EXPECT_FALSE(any(notEqual(v0, v0)));
    EXPECT_FALSE(any(equal(v0, v1)));

    EXPECT_FALSE(all(lessThan(v0, v0)));
    EXPECT_TRUE(all(lessThanEqual(v0, v0)));
    EXPECT_FALSE(all(greaterThan(v0, v0)));
    EXPECT_TRUE(all(greaterThanEqual(v0, v0)));
    EXPECT_TRUE(all(lessThan(v0, v1)));
    EXPECT_TRUE(all(greaterThan(v1, v0)));
}

TEST_F(VecTest, ArithmeticOps) {
    vec4 v0(1, 2, 3, 4);
    vec4 v1(10, 20, 30, 40);
@@ -233,6 +250,21 @@ TEST_F(VecTest, ArithmeticFunc) {

    float3 vf(east);
    EXPECT_EQ(length(vf), 1);

    EXPECT_TRUE(any(vec3(0, 0, 1)));
    EXPECT_FALSE(any(vec3(0, 0, 0)));

    EXPECT_TRUE(all(vec3(1, 1, 1)));
    EXPECT_FALSE(all(vec3(0, 0, 1)));

    EXPECT_TRUE(any(bool3(false, false, true)));
    EXPECT_FALSE(any(bool3(false)));

    EXPECT_TRUE(all(bool3(true)));
    EXPECT_FALSE(all(bool3(false, false, true)));

    std::function<bool(float)> p = [](auto v) -> bool { return v > 0.0f; };
    EXPECT_TRUE(all(map(vec3(1, 2, 3), p)));
}

}; // namespace android