Inlined Vector2 and SignedDistance for performance, minor coding style change

This commit is contained in:
Chlumsky 2023-10-11 23:59:32 +02:00
parent e69b7962d1
commit bd5f145672
33 changed files with 289 additions and 349 deletions

View File

@ -1,7 +1,7 @@
#pragma once
#include "Vector2.h"
#include "Vector2.hpp"
namespace msdfgen {

View File

@ -2,7 +2,7 @@
#pragma once
#include <vector>
#include "Vector2.h"
#include "Vector2.hpp"
#include "edge-selectors.h"
#include "contour-combiners.h"

View File

@ -1,29 +0,0 @@
#include "SignedDistance.h"
#include <cmath>
#include <cfloat>
namespace msdfgen {
SignedDistance::SignedDistance() : distance(-DBL_MAX), dot(1) { }
SignedDistance::SignedDistance(double dist, double d) : distance(dist), dot(d) { }
bool operator<(SignedDistance a, SignedDistance b) {
return fabs(a.distance) < fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot < b.dot);
}
bool operator>(SignedDistance a, SignedDistance b) {
return fabs(a.distance) > fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot > b.dot);
}
bool operator<=(SignedDistance a, SignedDistance b) {
return fabs(a.distance) < fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot <= b.dot);
}
bool operator>=(SignedDistance a, SignedDistance b) {
return fabs(a.distance) > fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot >= b.dot);
}
}

View File

@ -1,23 +0,0 @@
#pragma once
namespace msdfgen {
/// Represents a signed distance and alignment, which together can be compared to uniquely determine the closest edge segment.
class SignedDistance {
public:
double distance;
double dot;
SignedDistance();
SignedDistance(double dist, double d);
friend bool operator<(SignedDistance a, SignedDistance b);
friend bool operator>(SignedDistance a, SignedDistance b);
friend bool operator<=(SignedDistance a, SignedDistance b);
friend bool operator>=(SignedDistance a, SignedDistance b);
};
}

37
core/SignedDistance.hpp Normal file
View File

@ -0,0 +1,37 @@
#pragma once
#include <cmath>
#include <cfloat>
namespace msdfgen {
/// Represents a signed distance and alignment, which together can be compared to uniquely determine the closest edge segment.
class SignedDistance {
public:
double distance;
double dot;
inline SignedDistance() : distance(-DBL_MAX), dot(0) { }
inline SignedDistance(double dist, double d) : distance(dist), dot(d) { }
};
inline bool operator<(const SignedDistance a, const SignedDistance b) {
return fabs(a.distance) < fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot < b.dot);
}
inline bool operator>(const SignedDistance a, const SignedDistance b) {
return fabs(a.distance) > fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot > b.dot);
}
inline bool operator<=(const SignedDistance a, const SignedDistance b) {
return fabs(a.distance) < fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot <= b.dot);
}
inline bool operator>=(const SignedDistance a, const SignedDistance b) {
return fabs(a.distance) > fabs(b.distance) || (fabs(a.distance) == fabs(b.distance) && a.dot >= b.dot);
}
}

View File

@ -1,149 +0,0 @@
#include "Vector2.h"
#include <cstddef>
#include <cmath>
namespace msdfgen {
Vector2::Vector2(double val) : x(val), y(val) { }
Vector2::Vector2(double x, double y) : x(x), y(y) { }
void Vector2::reset() {
x = 0, y = 0;
}
void Vector2::set(double x, double y) {
Vector2::x = x, Vector2::y = y;
}
double Vector2::length() const {
return sqrt(x*x+y*y);
}
double Vector2::direction() const {
return atan2(y, x);
}
Vector2 Vector2::normalize(bool allowZero) const {
double len = length();
if (len == 0)
return Vector2(0, !allowZero);
return Vector2(x/len, y/len);
}
Vector2 Vector2::getOrthogonal(bool polarity) const {
return polarity ? Vector2(-y, x) : Vector2(y, -x);
}
Vector2 Vector2::getOrthonormal(bool polarity, bool allowZero) const {
double len = length();
if (len == 0)
return polarity ? Vector2(0, !allowZero) : Vector2(0, -!allowZero);
return polarity ? Vector2(-y/len, x/len) : Vector2(y/len, -x/len);
}
Vector2 Vector2::project(const Vector2 &vector, bool positive) const {
Vector2 n = normalize(true);
double t = dotProduct(vector, n);
if (positive && t <= 0)
return Vector2();
return t*n;
}
Vector2::operator const void*() const {
return x || y ? this : NULL;
}
bool Vector2::operator!() const {
return !x && !y;
}
bool Vector2::operator==(const Vector2 &other) const {
return x == other.x && y == other.y;
}
bool Vector2::operator!=(const Vector2 &other) const {
return x != other.x || y != other.y;
}
Vector2 Vector2::operator+() const {
return *this;
}
Vector2 Vector2::operator-() const {
return Vector2(-x, -y);
}
Vector2 Vector2::operator+(const Vector2 &other) const {
return Vector2(x+other.x, y+other.y);
}
Vector2 Vector2::operator-(const Vector2 &other) const {
return Vector2(x-other.x, y-other.y);
}
Vector2 Vector2::operator*(const Vector2 &other) const {
return Vector2(x*other.x, y*other.y);
}
Vector2 Vector2::operator/(const Vector2 &other) const {
return Vector2(x/other.x, y/other.y);
}
Vector2 Vector2::operator*(double value) const {
return Vector2(x*value, y*value);
}
Vector2 Vector2::operator/(double value) const {
return Vector2(x/value, y/value);
}
Vector2 & Vector2::operator+=(const Vector2 &other) {
x += other.x, y += other.y;
return *this;
}
Vector2 & Vector2::operator-=(const Vector2 &other) {
x -= other.x, y -= other.y;
return *this;
}
Vector2 & Vector2::operator*=(const Vector2 &other) {
x *= other.x, y *= other.y;
return *this;
}
Vector2 & Vector2::operator/=(const Vector2 &other) {
x /= other.x, y /= other.y;
return *this;
}
Vector2 & Vector2::operator*=(double value) {
x *= value, y *= value;
return *this;
}
Vector2 & Vector2::operator/=(double value) {
x /= value, y /= value;
return *this;
}
double dotProduct(const Vector2 &a, const Vector2 &b) {
return a.x*b.x+a.y*b.y;
}
double crossProduct(const Vector2 &a, const Vector2 &b) {
return a.x*b.y-a.y*b.x;
}
Vector2 operator*(double value, const Vector2 &vector) {
return Vector2(value*vector.x, value*vector.y);
}
Vector2 operator/(double value, const Vector2 &vector) {
return Vector2(value/vector.x, value/vector.y);
}
}

View File

@ -1,63 +0,0 @@
#pragma once
namespace msdfgen {
/**
* A 2-dimensional euclidean vector with double precision.
* Implementation based on the Vector2 template from Artery Engine.
* @author Viktor Chlumsky
*/
struct Vector2 {
double x, y;
Vector2(double val = 0);
Vector2(double x, double y);
/// Sets the vector to zero.
void reset();
/// Sets individual elements of the vector.
void set(double x, double y);
/// Returns the vector's length.
double length() const;
/// Returns the angle of the vector in radians (atan2).
double direction() const;
/// Returns the normalized vector - one that has the same direction but unit length.
Vector2 normalize(bool allowZero = false) const;
/// Returns a vector with the same length that is orthogonal to this one.
Vector2 getOrthogonal(bool polarity = true) const;
/// Returns a vector with unit length that is orthogonal to this one.
Vector2 getOrthonormal(bool polarity = true, bool allowZero = false) const;
/// Returns a vector projected along this one.
Vector2 project(const Vector2 &vector, bool positive = false) const;
operator const void *() const;
bool operator!() const;
bool operator==(const Vector2 &other) const;
bool operator!=(const Vector2 &other) const;
Vector2 operator+() const;
Vector2 operator-() const;
Vector2 operator+(const Vector2 &other) const;
Vector2 operator-(const Vector2 &other) const;
Vector2 operator*(const Vector2 &other) const;
Vector2 operator/(const Vector2 &other) const;
Vector2 operator*(double value) const;
Vector2 operator/(double value) const;
Vector2 & operator+=(const Vector2 &other);
Vector2 & operator-=(const Vector2 &other);
Vector2 & operator*=(const Vector2 &other);
Vector2 & operator/=(const Vector2 &other);
Vector2 & operator*=(double value);
Vector2 & operator/=(double value);
/// Dot product of two vectors.
friend double dotProduct(const Vector2 &a, const Vector2 &b);
/// A special version of the cross product for 2D vectors (returns scalar value).
friend double crossProduct(const Vector2 &a, const Vector2 &b);
friend Vector2 operator*(double value, const Vector2 &vector);
friend Vector2 operator/(double value, const Vector2 &vector);
};
/// A vector may also represent a point, which shall be differentiated semantically using the alias Point2.
typedef Vector2 Point2;
}

167
core/Vector2.hpp Normal file
View File

@ -0,0 +1,167 @@
#pragma once
#include <cstddef>
#include <cmath>
namespace msdfgen {
/**
* A 2-dimensional euclidean floating-point vector.
* @author Viktor Chlumsky
*/
struct Vector2 {
double x, y;
inline Vector2(double val = 0) : x(val), y(val) { }
inline Vector2(double x, double y) : x(x), y(y) { }
/// Sets the vector to zero.
inline void reset() {
x = 0, y = 0;
}
/// Sets individual elements of the vector.
inline void set(double x, double y) {
this->x = x, this->y = y;
}
/// Returns the vector's squared length.
inline double squaredLength() const {
return x*x+y*y;
}
/// Returns the vector's length.
inline double length() const {
return sqrt(x*x+y*y);
}
/// Returns the normalized vector - one that has the same direction but unit length.
inline Vector2 normalize(bool allowZero = false) const {
if (double len = length())
return Vector2(x/len, y/len);
return Vector2(0, !allowZero);
}
/// Returns a vector with the same length that is orthogonal to this one.
inline Vector2 getOrthogonal(bool polarity = true) const {
return polarity ? Vector2(-y, x) : Vector2(y, -x);
}
/// Returns a vector with unit length that is orthogonal to this one.
inline Vector2 getOrthonormal(bool polarity = true, bool allowZero = false) const {
if (double len = length())
return polarity ? Vector2(-y/len, x/len) : Vector2(y/len, -x/len);
return polarity ? Vector2(0, !allowZero) : Vector2(0, -!allowZero);
}
#ifdef MSDFGEN_USE_CPP11
inline explicit operator bool() const {
return x || y;
}
#else
inline operator const void *() const {
return x || y ? this : NULL;
}
#endif
inline Vector2 &operator+=(const Vector2 other) {
x += other.x, y += other.y;
return *this;
}
inline Vector2 &operator-=(const Vector2 other) {
x -= other.x, y -= other.y;
return *this;
}
inline Vector2 &operator*=(const Vector2 other) {
x *= other.x, y *= other.y;
return *this;
}
inline Vector2 &operator/=(const Vector2 other) {
x /= other.x, y /= other.y;
return *this;
}
inline Vector2 &operator*=(double value) {
x *= value, y *= value;
return *this;
}
inline Vector2 &operator/=(double value) {
x /= value, y /= value;
return *this;
}
};
/// A vector may also represent a point, which shall be differentiated semantically using the alias Point2.
typedef Vector2 Point2;
/// Dot product of two vectors.
inline double dotProduct(const Vector2 a, const Vector2 b) {
return a.x*b.x+a.y*b.y;
}
/// A special version of the cross product for 2D vectors (returns scalar value).
inline double crossProduct(const Vector2 a, const Vector2 b) {
return a.x*b.y-a.y*b.x;
}
inline bool operator==(const Vector2 a, const Vector2 b) {
return a.x == b.x && a.y == b.y;
}
inline bool operator!=(const Vector2 a, const Vector2 b) {
return a.x != b.x || a.y != b.y;
}
inline Vector2 operator+(const Vector2 v) {
return v;
}
inline Vector2 operator-(const Vector2 v) {
return Vector2(-v.x, -v.y);
}
inline bool operator!(const Vector2 v) {
return !v.x && !v.y;
}
inline Vector2 operator+(const Vector2 a, const Vector2 b) {
return Vector2(a.x+b.x, a.y+b.y);
}
inline Vector2 operator-(const Vector2 a, const Vector2 b) {
return Vector2(a.x-b.x, a.y-b.y);
}
inline Vector2 operator*(const Vector2 a, const Vector2 b) {
return Vector2(a.x*b.x, a.y*b.y);
}
inline Vector2 operator/(const Vector2 a, const Vector2 b) {
return Vector2(a.x/b.x, a.y/b.y);
}
inline Vector2 operator*(double a, const Vector2 b) {
return Vector2(a*b.x, a*b.y);
}
inline Vector2 operator/(double a, const Vector2 b) {
return Vector2(a/b.x, a/b.y);
}
inline Vector2 operator*(const Vector2 a, double b) {
return Vector2(a.x*b, a.y*b);
}
inline Vector2 operator/(const Vector2 a, double b) {
return Vector2(a.x/b, a.y/b);
}
}

View File

@ -2,7 +2,7 @@
#pragma once
#include "arithmetics.hpp"
#include "Vector2.h"
#include "Vector2.hpp"
#include "BitmapRef.hpp"
namespace msdfgen {

View File

@ -1,8 +1,8 @@
#pragma once
#include "Vector2.h"
#include "SignedDistance.h"
#include "Vector2.hpp"
#include "SignedDistance.hpp"
#include "EdgeColor.h"
namespace msdfgen {

View File

@ -1,8 +1,8 @@
#pragma once
#include "Vector2.h"
#include "SignedDistance.h"
#include "Vector2.hpp"
#include "SignedDistance.hpp"
#include "edge-segments.h"
namespace msdfgen {

View File

@ -1,7 +1,7 @@
#pragma once
#include "Vector2.h"
#include "Vector2.hpp"
#include "Projection.h"
#include "Shape.h"
#include "BitmapRef.hpp"

View File

@ -1,7 +1,7 @@
#pragma once
#include "Vector2.h"
#include "Vector2.hpp"
#include "Shape.h"
#include "Projection.h"
#include "Scanline.h"

View File

@ -1,7 +1,7 @@
#pragma once
#include "Vector2.h"
#include "Vector2.hpp"
#include "BitmapRef.hpp"
namespace msdfgen {

View File

@ -1,7 +1,7 @@
#pragma once
#include "Vector2.h"
#include "Vector2.hpp"
#include "Shape.h"
#include "Projection.h"
#include "Scanline.h"

View File

@ -5,7 +5,7 @@
#include <skia/core/SkPath.h>
#include <skia/pathops/SkPathOps.h>
#include "../core/Vector2.h"
#include "../core/Vector2.hpp"
#include "../core/edge-segments.h"
#include "../core/Contour.h"

View File

@ -16,7 +16,7 @@
*/
#include "core/arithmetics.hpp"
#include "core/Vector2.h"
#include "core/Vector2.hpp"
#include "core/Projection.h"
#include "core/Scanline.h"
#include "core/Shape.h"