Skip to content

Commit

Permalink
Merge pull request #53 from valeriyvan/fix/get-rid-of-arrays-of-singl…
Browse files Browse the repository at this point in the history
…e-element

Get rid of arrays of the only element
  • Loading branch information
valeriyvan authored Oct 4, 2023
2 parents 95f665f + 448c45d commit ed19451
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
10 changes: 5 additions & 5 deletions Sources/geometrize-cli/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,25 +110,25 @@ while shapeData.count <= shapeCount {
if options.verbose {
print("Step \(counter)", terminator: "")
}
let shapes = runner.step(
let shapeResult = runner.step(
options: runnerOptions,
shapeCreator: nil,
energyFunction: defaultEnergyFunction,
addShapePrecondition: defaultAddShapePrecondition
)
if shapes.isEmpty {
if let shapeResult {
shapeData.append(shapeResult)
if options.verbose {
print(", no shapes added.", terminator: "")
print(", \(shapeResult.shape.description) added.", terminator: "")
}
} else {
if options.verbose {
print(", \(shapes.map(\.shape).map(\.description).joined(separator: ", ")) added.", terminator: "")
print(", no shapes added.", terminator: "")
}
}
if options.verbose {
print(" Total count of shapes \(shapeData.count ).")
}
shapeData.append(contentsOf: shapes)
counter += 1
}

Expand Down
13 changes: 6 additions & 7 deletions Sources/geometrize/GeometrizeModelHillClimb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class GeometrizeModelHillClimb: GeometrizeModelBase {
return [state]
}

/// Steps the primitive optimization/fitting algorithm.
/// Concurrently runs several optimization sessions tying improving image geometrization by adding a shape to it
/// and returns result of the best optimization or nil if improvement of image wasn't found.
/// - Parameters:
/// - shapeCreator: A function that will produce the shapes.
/// - alpha: The alpha of the shape.
Expand All @@ -49,8 +50,7 @@ class GeometrizeModelHillClimb: GeometrizeModelBase {
/// - maxThreads: The maximum number of threads to use during this step.
/// - energyFunction: A function to calculate the energy.
/// - addShapePrecondition: A function to determine whether to accept a shape.
/// - Returns: A vector containing data about the shapes added to the model in this step.
/// This may be empty if no shape that improved the image could be found.
/// - Returns: Returns `ShapeResult` representing a shape added to improve image or nil if improvement wasn't found.
func step( // swiftlint:disable:this function_parameter_count
shapeCreator: () -> any Shape,
alpha: UInt8,
Expand All @@ -59,7 +59,7 @@ class GeometrizeModelHillClimb: GeometrizeModelBase {
maxThreads: Int,
energyFunction: @escaping EnergyFunction,
addShapePrecondition: @escaping ShapeAcceptancePreconditionFunction = defaultAddShapePrecondition
) -> [ShapeResult] {
) -> ShapeResult? {

let states: [State] = getHillClimbState(
shapeCreator: shapeCreator,
Expand Down Expand Up @@ -90,14 +90,13 @@ class GeometrizeModelHillClimb: GeometrizeModelBase {
let newScore: Double = differencePartial(target: targetBitmap, before: before, after: currentBitmap, score: lastScore, lines: lines)
guard addShapePrecondition(lastScore, newScore, shape, lines, color, before, currentBitmap, targetBitmap) else {
currentBitmap = before
return []
return nil
}

// Improvement - set new baseline and return the new shape
lastScore = newScore

let result: ShapeResult = ShapeResult(score: lastScore, color: color, shape: shape)
return [result]
return ShapeResult(score: lastScore, color: color, shape: shape)
}

/// Sets the seed that the random number generators of this model use.
Expand Down
11 changes: 5 additions & 6 deletions Sources/geometrize/ImageRunner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,28 @@ public struct ImageRunner {
model = GeometrizeModelHillClimb(target: targetBitmap, initial: initialBitmap)
}

/// Updates the internal model once.
/// Makes one step of geometrization trying to add a shape to image improving its geometrization.
/// - Parameters:
/// - options: Various configurable settings for doing the step e.g. the shape types to consider.
/// - shapeCreator: An optional function for creating and mutating shapes.
/// - energyFunction: A function to calculate the energy.
/// - addShapePrecondition: A function to determine whether to accept a shape.
/// - Returns: A vector containing data about the shapes just added to the internal model.
/// - Returns: a ShapeResult representing a shape added to image or nil if a shape improving image
/// geometrization wasn't found.
public mutating func step(
options: ImageRunnerOptions,
shapeCreator: (() -> any Shape)? = nil,
energyFunction: @escaping EnergyFunction,
addShapePrecondition: @escaping ShapeAcceptancePreconditionFunction
) -> [ShapeResult] {
) -> ShapeResult? {
let (xMin, yMin, xMax, yMax) = mapShapeBoundsToImage(options: options.shapeBounds, image: model.getTarget())
let types = options.shapeTypes

let shapeCreator: () -> any Shape = shapeCreator ?? createDefaultShapeCreator(types: types, canvasBounds: Bounds(xMin: xMin, xMax: xMax, yMin: yMin, yMax: yMax))

model.setSeed(options.seed)

let result: [ShapeResult] = model.step(
return model.step(
shapeCreator: shapeCreator,
alpha: options.alpha,
shapeCount: options.shapeCount,
Expand All @@ -111,8 +112,6 @@ public struct ImageRunner {
energyFunction: energyFunction,
addShapePrecondition: addShapePrecondition
)

return result
}

public var currentBitmap: Bitmap {
Expand Down
8 changes: 5 additions & 3 deletions Tests/geometrizeTests/ImageRunnerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ final class ImageRunnerTests: XCTestCase {
var counter = 0
while shapeData.count <= 1000 /* Here set count of shapes final image should have. Remember background is the first shape. */ {
print("Step \(counter)", terminator: "")
let shapes = runner.step(options: options, shapeCreator: nil, energyFunction: defaultEnergyFunction, addShapePrecondition: defaultAddShapePrecondition)
print(", \(shapes.isEmpty ? "no" : String(shapes.count)) shape(s) was/were added. Total count of shapes \(shapeData.count ).")
shapeData.append(contentsOf: shapes)
let shapeResult = runner.step(options: options, shapeCreator: nil, energyFunction: defaultEnergyFunction, addShapePrecondition: defaultAddShapePrecondition)
if let shapeResult {
shapeData.append(shapeResult)
}
print(", \(shapeResult == nil ? "no" : "1") shape was added. Total count of shapes \(shapeData.count ).")
counter += 1
}

Expand Down

0 comments on commit ed19451

Please sign in to comment.