-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Voronoi diagrams #692
Merged
Merged
Voronoi diagrams #692
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
f0b9f96
Initial implementation
Lehonti 543f89a
Added it to `CoreEffectsExtension`
Lehonti 08dbde2
Assigned distance
Lehonti c9da51b
Reduced number of points
Lehonti 3a0a12d
Merge branch 'PintaProject:master' into feature/voronoi
Lehonti 4d3f34c
Using immutable structures
Lehonti e146763
Added Chebyshev method of distance calculation
Lehonti 4cd5095
Added color sorting
Lehonti 83c9136
Added labels to sorting options
Lehonti c33fd4d
Added notes for translators, and added option to invert sorting
Lehonti e000330
Removed empty comment for translators
Lehonti b7d6b14
stored values in more variables
Lehonti 368f2cb
Created new `record` type for settings
Lehonti 2dfe1a8
Added option to show points
Lehonti bd4babb
Local refactoring and reformatting
Lehonti c00bec6
Ordering of points
Lehonti 1b452dc
Merge branch 'PintaProject:master' into feature/voronoi
Lehonti 3fa9d8f
Changed naming and added TODO comment
Lehonti 4955709
Added tests
Lehonti 53f5bcb
Removed property assignment
Lehonti 1aab816
Constructor now gets a service manager
Lehonti 38ca138
Created `Utility.GeneratePixelOffsets`
Lehonti 73946e6
Creating pixels in parallel
Lehonti 4fa83a6
Removed intermediate dictionary structure and added check
Lehonti d85079e
Revert "Removed intermediate dictionary structure and added check"
Lehonti 63ed0cd
With the dictionary it seems to work faster for some reason
Lehonti 44b3fba
Tried parallel (and removing intermediate structure) in some other way
Lehonti c1a2ce9
A few adjustments
Lehonti b8ca36a
Solved merge conflicts
Lehonti 313f7ee
Added chrome service to `VoronoiDiagramEffect`
Lehonti 0f772d2
Reduced number of sortings
Lehonti aa26e5a
Removed a test
Lehonti c91da94
Merge branch 'PintaProject:master' into feature/voronoi
Lehonti e34c9b1
Changed comments for translators
Lehonti d354929
Merge branch 'PintaProject:master' into feature/voronoi
Lehonti 9b44f4e
Added two more tests
Lehonti File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.ComponentModel; | ||
using System.Linq; | ||
using Cairo; | ||
using Pinta.Core; | ||
using Pinta.Gui.Widgets; | ||
|
||
namespace Pinta.Effects; | ||
|
||
public sealed class VoronoiDiagramEffect : BaseEffect | ||
{ | ||
// TODO: Icon | ||
|
||
public override bool IsTileable => false; | ||
|
||
public override string Name => Translations.GetString ("Voronoi Diagram"); | ||
|
||
public override bool IsConfigurable => true; | ||
|
||
public override string EffectMenuCategory => Translations.GetString ("Render"); | ||
|
||
public VoronoiDiagramData Data => (VoronoiDiagramData) EffectData!; // NRT - Set in constructor | ||
|
||
private readonly IChromeService chrome; | ||
|
||
public VoronoiDiagramEffect (IServiceManager services) | ||
{ | ||
chrome = services.GetService<IChromeService> (); | ||
EffectData = new VoronoiDiagramData (); | ||
} | ||
|
||
public override void LaunchConfiguration () | ||
=> chrome.LaunchSimpleEffectDialog (this); | ||
|
||
private sealed record VoronoiSettings ( | ||
Size size, | ||
bool showPoints, | ||
ImmutableArray<PointI> points, | ||
ImmutableArray<ColorBgra> colors, | ||
Func<PointI, PointI, double> distanceCalculator); | ||
|
||
private VoronoiSettings CreateSettings (ImageSurface dst, RectangleI roi) | ||
{ | ||
ColorSorting colorSorting = Data.ColorSorting; | ||
|
||
IEnumerable<PointI> basePoints = CreatePoints (roi, Data.NumberOfCells, Data.RandomPointLocations); | ||
ImmutableArray<PointI> points = SortPoints (basePoints, colorSorting).ToImmutableArray (); | ||
|
||
IEnumerable<ColorBgra> baseColors = CreateColors (points.Length, Data.RandomColors); | ||
IEnumerable<ColorBgra> positionSortedColors = SortColors (baseColors, colorSorting); | ||
IEnumerable<ColorBgra> reversedSortingColors = Data.ReverseColorSorting ? positionSortedColors.Reverse () : positionSortedColors; | ||
|
||
return new ( | ||
size: dst.GetSize (), | ||
showPoints: Data.ShowPoints, | ||
points: points, | ||
colors: reversedSortingColors.ToImmutableArray (), | ||
distanceCalculator: GetDistanceCalculator (Data.DistanceMetric) | ||
); | ||
} | ||
|
||
protected override void Render (ImageSurface src, ImageSurface dst, RectangleI roi) | ||
{ | ||
VoronoiSettings settings = CreateSettings (dst, roi); | ||
Span<ColorBgra> dst_data = dst.GetPixelData (); | ||
foreach (var kvp in roi.GeneratePixelOffsets (settings.size).AsParallel ().Select (CreateColor)) | ||
dst_data[kvp.Key] = kvp.Value; | ||
|
||
KeyValuePair<int, ColorBgra> CreateColor (Utility.PixelOffset pixel) | ||
{ | ||
double shortestDistance = double.MaxValue; | ||
int closestIndex = 0; | ||
|
||
for (var i = 0; i < settings.points.Length; i++) { | ||
// TODO: Acceleration structure that limits the search | ||
// to a relevant subset of points, for better performance. | ||
// Some ideas to consider: quadtree, spatial hashing | ||
var point = settings.points[i]; | ||
double distance = settings.distanceCalculator (point, pixel.coordinates); | ||
if (distance > shortestDistance) continue; | ||
shortestDistance = distance; | ||
closestIndex = i; | ||
} | ||
|
||
ColorBgra finalColor = | ||
settings.showPoints && shortestDistance == 0 | ||
? ColorBgra.Black | ||
: settings.colors[closestIndex]; | ||
|
||
return KeyValuePair.Create (pixel.memoryOffset, finalColor); | ||
} | ||
} | ||
|
||
private static IEnumerable<ColorBgra> SortColors (IEnumerable<ColorBgra> baseColors, ColorSorting colorSorting) | ||
|
||
=> colorSorting switch { | ||
|
||
ColorSorting.Random => baseColors, | ||
|
||
ColorSorting.HorizontalB or ColorSorting.VerticalB => baseColors.OrderBy (p => p.B), | ||
ColorSorting.HorizontalG or ColorSorting.VerticalG => baseColors.OrderBy (p => p.G), | ||
ColorSorting.HorizontalR or ColorSorting.VerticalR => baseColors.OrderBy (p => p.R), | ||
|
||
_ => throw new InvalidEnumArgumentException ( | ||
nameof (baseColors), | ||
(int) colorSorting, | ||
typeof (ColorSorting)), | ||
}; | ||
|
||
private static IEnumerable<PointI> SortPoints (IEnumerable<PointI> basePoints, ColorSorting colorSorting) | ||
|
||
=> colorSorting switch { | ||
|
||
ColorSorting.Random => basePoints, | ||
|
||
ColorSorting.HorizontalB | ||
or ColorSorting.HorizontalG | ||
or ColorSorting.HorizontalR => basePoints.OrderBy (p => p.X).ThenBy (p => p.Y), | ||
|
||
ColorSorting.VerticalB | ||
or ColorSorting.VerticalG | ||
or ColorSorting.VerticalR => basePoints.OrderBy (p => p.Y).ThenBy (p => p.X), | ||
|
||
_ => throw new InvalidEnumArgumentException ( | ||
nameof (colorSorting), | ||
(int) colorSorting, | ||
typeof (ColorSorting)), | ||
}; | ||
|
||
private static Func<PointI, PointI, double> GetDistanceCalculator (DistanceMetric distanceCalculationMethod) | ||
{ | ||
return distanceCalculationMethod switch { | ||
DistanceMetric.Euclidean => Euclidean, | ||
DistanceMetric.Manhattan => Manhattan, | ||
DistanceMetric.Chebyshev => Chebyshev, | ||
_ => throw new InvalidEnumArgumentException ( | ||
nameof (distanceCalculationMethod), | ||
(int) distanceCalculationMethod, | ||
typeof (DistanceMetric)), | ||
}; | ||
|
||
static double Euclidean (PointI targetPoint, PointI pixelLocation) | ||
{ | ||
PointI difference = pixelLocation - targetPoint; | ||
return difference.Magnitude (); | ||
} | ||
|
||
static double Manhattan (PointI targetPoint, PointI pixelLocation) | ||
{ | ||
PointI difference = pixelLocation - targetPoint; | ||
return Math.Abs (difference.X) + Math.Abs (difference.Y); | ||
} | ||
|
||
static double Chebyshev (PointI targetPoint, PointI pixelLocation) | ||
{ | ||
PointI difference = pixelLocation - targetPoint; | ||
return Math.Max (Math.Abs (difference.X), Math.Abs (difference.Y)); | ||
} | ||
} | ||
|
||
private static ImmutableHashSet<PointI> CreatePoints (RectangleI roi, int pointCount, RandomSeed pointLocationsSeed) | ||
{ | ||
int effectivePointCount = Math.Min (pointCount, roi.Width * roi.Height); | ||
|
||
Random randomPositioner = new (pointLocationsSeed.Value); | ||
var result = ImmutableHashSet.CreateBuilder<PointI> (); // Ensures points' uniqueness | ||
|
||
while (result.Count < effectivePointCount) { | ||
|
||
PointI point = new ( | ||
X: randomPositioner.Next (roi.Left, roi.Right + 1), | ||
Y: randomPositioner.Next (roi.Top, roi.Bottom + 1) | ||
); | ||
|
||
result.Add (point); | ||
} | ||
|
||
return result.ToImmutable (); | ||
} | ||
|
||
private static IEnumerable<ColorBgra> CreateColors (int colorCount, RandomSeed colorsSeed) | ||
{ | ||
Random randomColorizer = new (colorsSeed.Value); | ||
HashSet<ColorBgra> uniquenessTracker = new (); | ||
while (uniquenessTracker.Count < colorCount) { | ||
ColorBgra candidateColor = randomColorizer.RandomColorBgra (); | ||
if (uniquenessTracker.Contains (candidateColor)) continue; | ||
uniquenessTracker.Add (candidateColor); | ||
yield return candidateColor; | ||
} | ||
} | ||
|
||
public sealed class VoronoiDiagramData : EffectData | ||
{ | ||
[Caption ("Distance Metric")] | ||
public DistanceMetric DistanceMetric { get; set; } = DistanceMetric.Euclidean; | ||
|
||
[Caption ("Number of Cells"), MinimumValue (1), MaximumValue (1024)] | ||
public int NumberOfCells { get; set; } = 100; | ||
|
||
// Translators: The user can choose whether or not to render the points used in the calculation of a Voronoi diagram | ||
[Caption ("Show Points")] | ||
public bool ShowPoints { get; set; } = false; | ||
|
||
[Caption ("Color Sorting")] | ||
public ColorSorting ColorSorting { get; set; } = ColorSorting.Random; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might need some more thought on how to present it to the user. The effect is neat, but the huge menu of combinations seems intimidating |
||
|
||
// Translators: In this context, "reverse" is a verb, and the user can choose whether or not they want to reverse the color sorting | ||
[Caption ("Reverse Color Sorting")] | ||
public bool ReverseColorSorting { get; set; } = false; | ||
|
||
[Caption ("Random Colors")] | ||
public RandomSeed RandomColors { get; set; } = new (0); | ||
|
||
[Caption ("Random Point Locations")] | ||
public RandomSeed RandomPointLocations { get; set; } = new (0); | ||
} | ||
|
||
public enum DistanceMetric | ||
{ | ||
Euclidean, | ||
Manhattan, | ||
Chebyshev, | ||
} | ||
|
||
public enum ColorSorting | ||
{ | ||
[Caption ("Random")] Random, | ||
|
||
// Translators: Horizontal color sorting with blue (B) as the leading term | ||
[Caption ("Horizontal blue (B)")] HorizontalB, | ||
|
||
// Translators: Horizontal color sorting with green (G) as the leading term | ||
[Caption ("Horizontal green (G)")] HorizontalG, | ||
|
||
// Translators: Horizontal color sorting with red (R) as the leading term | ||
[Caption ("Horizontal red (R)")] HorizontalR, | ||
|
||
|
||
// Translators: Vertical color sorting with blue (B) as the leading term | ||
[Caption ("Vertical blue (B)")] VerticalB, | ||
|
||
// Translators: Vertical color sorting with green (G) as the leading term | ||
[Caption ("Vertical green (G)")] VerticalG, | ||
|
||
// Translators: Vertical color sorting with red (R) as the leading term | ||
[Caption ("Vertical red (R)")] VerticalR, | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are there good use cases for this to be exposed to users? Seems more like a debugging option to me at first glance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be an educational tool, so that they see that the cells are centered around the points, and then it piques their curiosity and then they look it up