-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move convexity into an environment modifier
- Loading branch information
Showing
18 changed files
with
179 additions
and
107 deletions.
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
46 changes: 46 additions & 0 deletions
46
Sources/SwiftSCAD/Environment/Values/PreviewConvexity.swift
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,46 @@ | ||
import Foundation | ||
|
||
public extension Environment { | ||
private static let key = ValueKey("SwiftSCAD.PreviewConvexity") | ||
|
||
/// The preview convexity currently set in the environment. | ||
/// | ||
/// The `previewConvexity` parameter specifies the maximum number of front or back faces a ray might intersect in the geometry during preview rendering. This parameter is only relevant when using OpenCSG preview mode in OpenSCAD and has no effect on the final mesh output or rendering. | ||
/// | ||
/// - Important: In OpenCSG preview mode, `previewConvexity` helps determine the correct visibility of complex shapes (e.g., tori or nested geometries). Adjust this parameter for accurate previews of intricate models but know that it is not necessary for final output generation. | ||
/// | ||
/// - Returns: The preview convexity value as an `Int`, or `nil` if not set. | ||
var previewConvexity: Int? { | ||
self[Self.key] as? Int | ||
} | ||
|
||
/// Returns a new environment with a modified preview convexity value. | ||
/// | ||
/// Use this method to apply a `previewConvexity` value to the environment, influencing the OpenCSG preview rendering in OpenSCAD. | ||
/// | ||
/// - Parameter convexity: An optional `Int` specifying the maximum number of front or back faces a ray might intersect. Pass `nil` to remove the existing convexity setting. | ||
/// - Returns: A new `Environment` with the specified preview convexity. | ||
/// | ||
/// ### Example | ||
/// ```swift | ||
/// let environmentWithConvexity = environment.withPreviewConvexity(2) | ||
/// ``` | ||
func withPreviewConvexity(_ convexity: Int?) -> Environment { | ||
setting(key: Self.key, value: convexity) | ||
} | ||
} | ||
|
||
public extension Geometry3D { | ||
/// Applies a preview convexity setting to the geometry. | ||
/// | ||
/// This method sets a `previewConvexity` value, specifying the maximum number of front or back faces a ray might intersect in the geometry during preview rendering. While SwiftSCAD does not use this value directly for geometry creation, OpenSCAD’s OpenCSG preview mode relies on it for accurate previews. | ||
/// | ||
/// - Parameter convexity: An `Int` specifying the preview convexity. | ||
/// - Returns: A modified geometry with the specified preview convexity. | ||
/// | ||
func withPreviewConvexity(_ convexity: Int) -> any Geometry3D { | ||
withEnvironment { enviroment in | ||
enviroment.withPreviewConvexity(convexity) | ||
} | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
Sources/SwiftSCAD/Geometry/Protocols/ExtrusionGeometry.swift
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,39 @@ | ||
import Foundation | ||
|
||
internal protocol ExtrusionGeometry: Geometry3D { | ||
var body: any Geometry2D { get } | ||
var moduleName: String { get } | ||
var moduleParameters: CodeFragment.Parameters { get } | ||
func boundary(for boundary2D: Boundary2D, facets: Environment.Facets) -> Boundary3D | ||
} | ||
|
||
extension ExtrusionGeometry { | ||
func bodyEnvironment(for environment: Environment) -> Environment { | ||
environment.withPreviewConvexity(nil) | ||
} | ||
|
||
func codeFragment(in environment: Environment) -> CodeFragment { | ||
|
||
var parameters = moduleParameters | ||
if let previewConvexity = environment.previewConvexity { | ||
parameters["convexity"] = previewConvexity | ||
} | ||
|
||
return CodeFragment( | ||
module: moduleName, | ||
parameters: parameters, | ||
body: [body.codeFragment(in: bodyEnvironment(for: environment))] | ||
) | ||
} | ||
|
||
func boundary(in environment: Environment) -> Bounds { | ||
boundary( | ||
for: body.boundary(in: bodyEnvironment(for: environment)), | ||
facets: environment.facets | ||
) | ||
} | ||
|
||
func elements(in environment: Environment) -> [ObjectIdentifier: any ResultElement] { | ||
body.elements(in: bodyEnvironment(for: environment)) | ||
} | ||
} |
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
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
Oops, something went wrong.