bezier_star_sweep/bezier_star_sweep.rs
1// MIT License
2//
3// Copyright (c) 2023 Michael H. Phillips
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21// SOFTWARE.
22//
23
24//! This example uses the BezierStar struct to create a profile and a path
25//! for the Polyhedron::sweep function. Both of the stars are the same shape
26//! but the path is ten times larger than the profile. The profile is swept
27//! along the path, spinning seven times along the way.
28//!
29//! The output_viewer boolean controls whether we output the view of the curves
30//! or the shape created by the sweep. Outputting the shape will most likely stall
31//! OpenSCAD for several minutes.
32//!
33//! We use two Viewer(s) in this example so we can have different sized points and
34//! lines.
35
36use scad_tree::prelude::*;
37
38fn main() {
39 let output_viewer = false;
40 let path = BezierStar::new(7, 20.0, 9.0, 40.0, 15.0, 12);
41 let profile = BezierStar::new(7, 2.0, 0.9, 4.0, 1.5, 12);
42
43 if output_viewer {
44 let mut viewer = Viewer::new(0.5, 0.25, 6);
45 viewer.add_bezier_star(&path);
46 let mut small_viewer = Viewer::new(0.05, 0.025, 6);
47 small_viewer.add_bezier_star(&profile);
48 scad_file!(32, "output/bezier_star_sweep.scad",
49 small_viewer.into_scad() + viewer.into_scad();
50 );
51 } else {
52 let path = Pt3s::from_pt3s(path.gen_points().iter().map(|p| p.as_pt3(0.0)).collect());
53 let profile = profile.gen_points();
54 let star_swept = Polyhedron::sweep(&profile, &path, 7.0 * 360.0, true);
55 scad_file!(32, "output/bezier_star_sweep.scad",
56 star_swept.into_scad();
57 );
58 }
59}