Skip to content

Commit

Permalink
Scale and translation arguments refactored as Projection, generator c…
Browse files Browse the repository at this point in the history
…onfiguration WIP
  • Loading branch information
Chlumsky committed Mar 30, 2021
1 parent 8332618 commit 37a1fff
Show file tree
Hide file tree
Showing 17 changed files with 335 additions and 122 deletions.
7 changes: 5 additions & 2 deletions Msdfgen.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,11 @@
<ClInclude Include="core\EdgeColor.h" />
<ClInclude Include="core\EdgeHolder.h" />
<ClInclude Include="core\equation-solver.h" />
<ClInclude Include="core\generator-config.h" />
<ClInclude Include="core\msdf-error-correction.h" />
<ClInclude Include="core\Projection.h" />
<ClInclude Include="core\sdf-error-estimation.h" />
<ClInclude Include="core\msdf-edge-artifact-patcher.h" />
<ClInclude Include="core\msdf-artifact-patcher.h" />
<ClInclude Include="core\pixel-conversion.hpp" />
<ClInclude Include="core\rasterization.h" />
<ClInclude Include="core\render-sdf.h" />
Expand Down Expand Up @@ -496,8 +498,9 @@
<ClCompile Include="core\EdgeHolder.cpp" />
<ClCompile Include="core\equation-solver.cpp" />
<ClCompile Include="core\msdf-error-correction.cpp" />
<ClCompile Include="core\Projection.cpp" />
<ClCompile Include="core\sdf-error-estimation.cpp" />
<ClCompile Include="core\msdf-edge-artifact-patcher.cpp" />
<ClCompile Include="core\msdf-artifact-patcher.cpp" />
<ClCompile Include="core\rasterization.cpp" />
<ClCompile Include="core\render-sdf.cpp" />
<ClCompile Include="core\save-bmp.cpp" />
Expand Down
21 changes: 15 additions & 6 deletions Msdfgen.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,18 @@
<ClInclude Include="core\msdf-error-correction.h">
<Filter>Core</Filter>
</ClInclude>
<ClInclude Include="core\msdf-edge-artifact-patcher.h">
<Filter>Core</Filter>
</ClInclude>
<ClInclude Include="ext\resolve-shape-geometry.h">
<Filter>Extensions</Filter>
</ClInclude>
<ClInclude Include="core\Projection.h">
<Filter>Core</Filter>
</ClInclude>
<ClInclude Include="core\msdf-artifact-patcher.h">
<Filter>Core</Filter>
</ClInclude>
<ClInclude Include="core\generator-config.h">
<Filter>Core</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
Expand Down Expand Up @@ -200,12 +206,15 @@
<ClCompile Include="core\msdf-error-correction.cpp">
<Filter>Core</Filter>
</ClCompile>
<ClCompile Include="core\msdf-edge-artifact-patcher.cpp">
<Filter>Core</Filter>
</ClCompile>
<ClCompile Include="ext\resolve-shape-geometry.cpp">
<Filter>Extensions</Filter>
</ClCompile>
<ClCompile Include="core\msdf-artifact-patcher.cpp">
<Filter>Core</Filter>
</ClCompile>
<ClCompile Include="core\Projection.cpp">
<Filter>Core</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="Msdfgen.rc">
Expand Down
42 changes: 42 additions & 0 deletions core/Projection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

#include "Projection.h"

namespace msdfgen {

Projection::Projection() : scale(1), translate(0) { }

Projection::Projection(const Vector2 &scale, const Vector2 &translate) : scale(scale), translate(translate) { }

Point2 Projection::project(const Point2 &coord) const {
return scale*(coord+translate);
}

Point2 Projection::unproject(const Point2 &coord) const {
return coord/scale-translate;
}

Vector2 Projection::projectVector(const Vector2 &vector) const {
return scale*vector;
}

Vector2 Projection::unprojectVector(const Vector2 &vector) const {
return vector/scale;
}

double Projection::projectX(double x) const {
return scale.x*(x+translate.x);
}

double Projection::projectY(double y) const {
return scale.y*(y+translate.y);
}

double Projection::unprojectX(double x) const {
return x/scale.x-translate.x;
}

double Projection::unprojectY(double y) const {
return y/scale.y-translate.y;
}

}
37 changes: 37 additions & 0 deletions core/Projection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

#pragma once

#include "Vector2.h"

namespace msdfgen {

/// A transformation from shape coordinates to pixel coordinates.
class Projection {

public:
Projection();
Projection(const Vector2 &scale, const Vector2 &translate);
/// Converts the shape coordinate to pixel coordinate.
Point2 project(const Point2 &coord) const;
/// Converts the pixel coordinate to shape coordinate.
Point2 unproject(const Point2 &coord) const;
/// Converts the vector to pixel coordinate space.
Vector2 projectVector(const Vector2 &vector) const;
/// Converts the vector from pixel coordinate space.
Vector2 unprojectVector(const Vector2 &vector) const;
/// Converts the X-coordinate from shape to pixel coordinate space.
double projectX(double x) const;
/// Converts the Y-coordinate from shape to pixel coordinate space.
double projectY(double y) const;
/// Converts the X-coordinate from pixel to shape coordinate space.
double unprojectX(double x) const;
/// Converts the Y-coordinate from pixel to shape coordinate space.
double unprojectY(double y) const;

private:
Vector2 scale;
Vector2 translate;

};

}
31 changes: 31 additions & 0 deletions core/generator-config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

#pragma once

#define MSDFGEN_DEFAULT_ARTIFACT_PATCHER_MIN_IMPROVE_RATIO 1.001

namespace msdfgen {

/// The configuration of the distance field generator algorithm.
struct GeneratorConfig {
/// Specifies whether to use the version of the algorithm that supports overlapping contours with the same winding. May be set to false to improve performance when no such contours are present.
bool overlapSupport;

inline explicit GeneratorConfig(bool overlapSupport = true) : overlapSupport(overlapSupport) { }
};

/// The configuration of the artifact patcher that processes the generated MSDF and fixes potential interpolation errors.
struct ArtifactPatcherConfig {
/// The mode of operation.
enum Mode {
DISABLED,
INDISCRIMINATE,
EDGE_PRIORITY,
EDGE_ONLY
} mode;
/// The minimum ratio of improvement required to patch a pixel. Must be greater than or equal to 1.
double minImproveRatio;

inline explicit ArtifactPatcherConfig(Mode mode = EDGE_PRIORITY, double minImproveRatio = MSDFGEN_DEFAULT_ARTIFACT_PATCHER_MIN_IMPROVE_RATIO) : mode(mode), minImproveRatio(minImproveRatio) { }
};

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#include "msdf-edge-artifact-patcher.h"
#include "msdf-artifact-patcher.h"

#include <cstring>
#include <vector>
Expand Down Expand Up @@ -118,14 +118,14 @@ void findHotspots(std::vector<Point2> &hotspots, const BitmapConstRef<float, N>
}

template <template <typename> class ContourCombiner, int N>
static void msdfPatchEdgeArtifactsInner(const BitmapRef<float, N> &sdf, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate) {
static void msdfPatchArtifactsInner(const BitmapRef<float, N> &sdf, const Shape &shape, const Projection &projection, double range) {
ShapeDistanceFinder<ContourCombiner<PseudoDistanceSelector> > distanceFinder(shape);
std::vector<Point2> hotspots;
findHotspots(hotspots, BitmapConstRef<float, N>(sdf));
std::vector<std::pair<int, int> > artifacts;
artifacts.reserve(hotspots.size());
for (std::vector<Point2>::const_iterator hotspot = hotspots.begin(); hotspot != hotspots.end(); ++hotspot) {
Point2 pos = *hotspot/scale-translate;
Point2 pos = projection.unproject(*hotspot);
double actualDistance = distanceFinder.distance(pos);
float sd = float(actualDistance/range+.5);

Expand Down Expand Up @@ -157,18 +157,18 @@ static void msdfPatchEdgeArtifactsInner(const BitmapRef<float, N> &sdf, const Sh
}
}

void msdfPatchEdgeArtifacts(const BitmapRef<float, 3> &sdf, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
if (overlapSupport)
msdfPatchEdgeArtifactsInner<OverlappingContourCombiner>(sdf, shape, range, scale, translate);
void msdfPatchArtifacts(const BitmapRef<float, 3> &sdf, const Shape &shape, const Projection &projection, double range, const GeneratorConfig &generatorConfig, const ArtifactPatcherConfig &config) {
if (generatorConfig.overlapSupport)
msdfPatchArtifactsInner<OverlappingContourCombiner>(sdf, shape, projection, range);
else
msdfPatchEdgeArtifactsInner<SimpleContourCombiner>(sdf, shape, range, scale, translate);
msdfPatchArtifactsInner<SimpleContourCombiner>(sdf, shape, projection, range);
}

void msdfPatchEdgeArtifacts(const BitmapRef<float, 4> &sdf, const Shape &shape, double range, const Vector2 &scale, const Vector2 &translate, bool overlapSupport) {
if (overlapSupport)
msdfPatchEdgeArtifactsInner<OverlappingContourCombiner>(sdf, shape, range, scale, translate);
void msdfPatchArtifacts(const BitmapRef<float, 4> &sdf, const Shape &shape, const Projection &projection, double range, const GeneratorConfig &generatorConfig, const ArtifactPatcherConfig &config) {
if (generatorConfig.overlapSupport)
msdfPatchArtifactsInner<OverlappingContourCombiner>(sdf, shape, projection, range);
else
msdfPatchEdgeArtifactsInner<SimpleContourCombiner>(sdf, shape, range, scale, translate);
msdfPatchArtifactsInner<SimpleContourCombiner>(sdf, shape, projection, range);
}

}
15 changes: 15 additions & 0 deletions core/msdf-artifact-patcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#pragma once

#include "Vector2.h"
#include "Shape.h"
#include "Projection.h"
#include "BitmapRef.hpp"
#include "generator-config.h"

namespace msdfgen {

void msdfPatchArtifacts(const BitmapRef<float, 3> &sdf, const Shape &shape, const Projection &projection, double range, const GeneratorConfig &generatorConfig = GeneratorConfig(), const ArtifactPatcherConfig &config = ArtifactPatcherConfig());
void msdfPatchArtifacts(const BitmapRef<float, 4> &sdf, const Shape &shape, const Projection &projection, double range, const GeneratorConfig &generatorConfig = GeneratorConfig(), const ArtifactPatcherConfig &config = ArtifactPatcherConfig());

}
13 changes: 0 additions & 13 deletions core/msdf-edge-artifact-patcher.h

This file was deleted.

7 changes: 7 additions & 0 deletions core/msdf-error-correction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,11 @@ void msdfErrorCorrection(const BitmapRef<float, 4> &output, const Vector2 &thres
msdfErrorCorrectionInner(output, threshold);
}

void msdfErrorCorrection(const BitmapRef<float, 3> &output, double threshold, const Projection &projection, double range) {
msdfErrorCorrectionInner(output, projection.unprojectVector(Vector2(threshold/range)));
}
void msdfErrorCorrection(const BitmapRef<float, 4> &output, double threshold, const Projection &projection, double range) {
msdfErrorCorrectionInner(output, projection.unprojectVector(Vector2(threshold/range)));
}

}
5 changes: 5 additions & 0 deletions core/msdf-error-correction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#pragma once

#include "Vector2.h"
#include "Projection.h"
#include "BitmapRef.hpp"

#define MSDFGEN_DEFAULT_ERROR_CORRECTION_THRESHOLD 1.001
Expand All @@ -12,4 +13,8 @@ namespace msdfgen {
void msdfErrorCorrection(const BitmapRef<float, 3> &output, const Vector2 &threshold);
void msdfErrorCorrection(const BitmapRef<float, 4> &output, const Vector2 &threshold);

// Alternate API - threshold specified in pixels
void msdfErrorCorrection(const BitmapRef<float, 3> &output, double threshold, const Projection &projection, double range);
void msdfErrorCorrection(const BitmapRef<float, 4> &output, double threshold, const Projection &projection, double range);

}
Loading

0 comments on commit 37a1fff

Please sign in to comment.