1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
use crate::*; const PI: Rad<f64> = Rad(std::f64::consts::PI); /// Creates and returns a vertex by a three dimensional point. /// # Examples /// ``` /// use truck_modeling::*; /// /// // put a vertex /// let vertex = builder::vertex(Point3::new(1.0, 2.0, 3.0)); /// # assert_eq!(*vertex.lock_point().unwrap(), Point3::new(1.0, 2.0, 3.0)); /// ``` #[inline(always)] pub fn vertex(pt: Point3) -> Vertex { Vertex::new(pt) } /// Returns a line from `vertex0` to `vertex1`. /// # Examples /// ``` /// use truck_modeling::*; /// /// // draw a line /// let vertex0 = builder::vertex(Point3::new(1.0, 2.0, 3.0)); /// let vertex1 = builder::vertex(Point3::new(6.0, 5.0, 4.0)); /// let line = builder::line(&vertex0, &vertex1); /// # let curve = line.oriented_curve(); /// # let pt0 = Point3::new(1.0, 2.0, 3.0); /// # let pt1 = Point3::new(6.0, 5.0, 4.0); /// # const N: usize = 10; /// # for i in 0..=N { /// # let t = i as f64 / N as f64; /// # assert!(curve.subs(t).near2(&(pt0 + t * (pt1 - pt0)))); /// # } /// ``` #[inline(always)] pub fn line(vertex0: &Vertex, vertex1: &Vertex) -> Edge { let curve = geom_impls::line( (*vertex0.lock_point().unwrap()).to_homogeneous(), (*vertex1.lock_point().unwrap()).to_homogeneous(), ); Edge::new(vertex0, vertex1, NURBSCurve::new(curve)) } /// Returns a circle arc from `vertex0` to `vertex1` via `transit`. /// # Examples /// ``` /// use truck_modeling::*; /// /// // draw the unit upper semicircle /// let vertex0 = builder::vertex(Point3::new(1.0, 0.0, 0.0)); /// let vertex1 = builder::vertex(Point3::new(-1.0, 0.0, 0.0)); /// let semi_circle = builder::circle_arc(&vertex0, &vertex1, Point3::new(0.0, 1.0, 0.0)); /// # let curve = semi_circle.oriented_curve(); /// # const N: usize = 10; /// # for i in 0..=N { /// # let t = curve.knot_vec()[0] + curve.knot_vec().range_length() * i as f64 / N as f64; /// # assert!(curve.subs(t).to_vec().magnitude().near(&1.0)); /// # } /// ``` #[inline(always)] pub fn circle_arc(vertex0: &Vertex, vertex1: &Vertex, transit: Point3) -> Edge { let curve = geom_impls::circle_arc_by_three_points( (*vertex0.lock_point().unwrap()).to_homogeneous(), (*vertex1.lock_point().unwrap()).to_homogeneous(), transit, ); Edge::new(vertex0, vertex1, NURBSCurve::new(curve)) } /// Returns a Bezier curve from `vertex0` to `vertex1` with inter control points `inter_points`. /// # Examples /// ``` /// use truck_modeling::*; /// /// // draw a Bezier curve /// let vertex0 = builder::vertex(Point3::origin()); /// let vertex1 = builder::vertex(Point3::new(3.0, 0.0, 0.0)); /// let inter_points = vec![Point3::new(1.0, 1.0, 0.0), Point3::new(2.0, -1.0, 0.0)]; /// let bezier = builder::bezier(&vertex0, &vertex1, inter_points); /// # let curve = bezier.oriented_curve(); /// # const N: usize = 10; /// # for i in 0..=N { /// # let t = i as f64 / N as f64; /// # let pt = Point3::new(t * 3.0, 6.0 * t * t * t - 9.0 * t * t + 3.0 * t, 0.0); /// # assert!(curve.subs(t).near(&pt)); /// # } /// ``` #[inline(always)] pub fn bezier(vertex0: &Vertex, vertex1: &Vertex, mut inter_points: Vec<Point3>) -> Edge { let pt0 = *vertex0.lock_point().unwrap(); let pt1 = *vertex1.lock_point().unwrap(); let mut pre_ctrl_pts = vec![pt0]; pre_ctrl_pts.append(&mut inter_points); pre_ctrl_pts.push(pt1); let ctrl_pts: Vec<_> = pre_ctrl_pts .into_iter() .map(|pt| pt.to_homogeneous()) .collect(); let knot_vec = KnotVec::bezier_knot(ctrl_pts.len() - 1); let curve = BSplineCurve::new(knot_vec, ctrl_pts); Edge::new(vertex0, vertex1, NURBSCurve::new(curve)) } /// Returns a homotopic face from `edge0` to `edge1`. /// # Examples /// ``` /// use truck_modeling::*; /// /// // homotopy between skew lines /// let v0 = builder::vertex(Point3::new(0.0, 0.0, 0.0)); /// let v1 = builder::vertex(Point3::new(1.0, 0.0, 0.0)); /// let v2 = builder::vertex(Point3::new(0.0, 1.0, 0.0)); /// let v3 = builder::vertex(Point3::new(0.0, 1.0, 1.0)); /// let line0 = builder::line(&v0, &v1); /// let line1 = builder::line(&v2, &v3); /// let homotopy = builder::homotopy(&line0, &line1); /// # let surface = homotopy.oriented_surface(); /// # const N: usize = 10; /// # for i in 0..=N { /// # for j in 0..=N { /// # let s = i as f64 / N as f64; /// # let t = j as f64 / N as f64; /// # let pt = Point3::new(s * (1.0 - t), t, s * t); /// # assert!(surface.subs(s, t).near(&pt)); /// # } /// # } /// ``` #[inline(always)] pub fn homotopy(edge0: &Edge, edge1: &Edge) -> Face { let wire: Wire = vec![ edge0.clone(), line(edge0.back(), edge1.back()), edge1.inverse(), line(edge1.front(), edge0.front()), ] .into(); let curve0 = edge0.oriented_curve().into_non_rationalized(); let curve1 = edge1.oriented_curve().into_non_rationalized(); let surface = BSplineSurface::homotopy(curve0, curve1); Face::new(vec![wire], NURBSSurface::new(surface)) } /// Try attatiching a plane whose boundary is `wire`. /// Todo: Define the crate error and make return value `Result<Face>`! /// # Examples /// ``` /// use truck_modeling::*; /// /// // make a disk by attaching a plane into circle /// let vertex = builder::vertex(Point3::new(1.0, 0.0, 0.0)); /// let circle = builder::rsweep(&vertex, Point3::origin(), Vector3::unit_y(), Rad(7.0)); /// let disk = builder::try_attach_plane(&vec![circle]).expect("failed to attach plane."); /// # let surface = disk.oriented_surface(); /// # let normal = surface.normal(0.5, 0.5); /// # assert!(normal.near(&Vector3::unit_y())); /// ``` /// # Remarks /// If wires are not closed or not in one plane, then return `None`. /// ``` /// use truck_modeling::*; /// /// let v0 = builder::vertex(Point3::new(0.0, 0.0, 0.0)); /// let v1 = builder::vertex(Point3::new(1.0, 0.0, 0.0)); /// let v2 = builder::vertex(Point3::new(0.0, 1.0, 0.0)); /// let v3 = builder::vertex(Point3::new(0.0, 0.0, 1.0)); /// let wire: Wire = vec![ /// builder::line(&v0, &v1), /// builder::line(&v1, &v2), /// ] /// .into(); /// let mut wires = vec![wire]; /// // failed to attach plane, because wire is not closed. /// assert!(builder::try_attach_plane(&wires).is_none()); /// /// wires[0].push_back(builder::line(&v2, &v3)); /// wires[0].push_back(builder::line(&v3, &v0)); /// // failed to attach plane, because wire is not in the plane. /// assert!(builder::try_attach_plane(&wires).is_none()); /// /// wires[0].pop_back(); /// wires[0].pop_back(); /// wires[0].push_back(builder::line(&v2, &v0)); /// // sucess in attaching plane! /// assert!(builder::try_attach_plane(&wires).is_some()); /// ``` #[inline(always)] pub fn try_attach_plane(wires: &Vec<Wire>) -> Option<Face> { let pts = wires .iter() .flatten() .flat_map(|edge| { edge.oriented_curve() .control_points() .clone() .into_iter() .map(|pt| pt.to_point()) }) .collect::<Vec<_>>(); let surface = NURBSSurface::new(geom_impls::attach_plane(pts)?); Face::try_new(wires.clone(), surface).ok() } /// Returns another topology whose points, curves, and surfaces are cloned. #[inline(always)] pub fn clone<T: Mapped<Point3, NURBSCurve, NURBSSurface>>(elem: &T) -> T { elem.topological_clone() } /// Returns a transformed vertex, edge, wire, face, shell or solid. #[inline(always)] pub fn transformed<T: Mapped<Point3, NURBSCurve, NURBSSurface>>(elem: &T, mat: Matrix4) -> T { elem.mapped( &move |pt: &Point3| mat.transform_point(*pt), &move |curve: &NURBSCurve| NURBSCurve::new(mat * curve.non_rationalized()), &move |surface: &NURBSSurface| NURBSSurface::new(mat * surface.non_rationalized()), ) } /// Returns a translated vertex, edge, wire, face, shell or solid. #[inline(always)] pub fn translated<T: Mapped<Point3, NURBSCurve, NURBSSurface>>(elem: &T, vector: Vector3) -> T { transformed(elem, Matrix4::from_translation(vector)) } /// Returns a rotated vertex, edge, wire, face, shell or solid. #[inline(always)] pub fn rotated<T: Mapped<Point3, NURBSCurve, NURBSSurface>>( elem: &T, origin: Point3, axis: Vector3, angle: Rad<f64>, ) -> T { let mat0 = Matrix4::from_translation(-origin.to_vec()); let mat1 = Matrix4::from_axis_angle(axis, angle); let mat2 = Matrix4::from_translation(origin.to_vec()); transformed(elem, mat2 * mat1 * mat0) } /// Returns a scaled vertex, edge, wire, face, shell or solid. #[inline(always)] pub fn scaled<T: Mapped<Point3, NURBSCurve, NURBSSurface>>( elem: &T, origin: Point3, scalars: Vector3, ) -> T { let mat0 = Matrix4::from_translation(-origin.to_vec()); let mat1 = Matrix4::from_nonuniform_scale(scalars[0], scalars[1], scalars[2]); let mat2 = Matrix4::from_translation(origin.to_vec()); transformed(elem, mat2 * mat1 * mat0) } /// Sweeps a vertex, an edge, a wire, a face, or a shell by a vector. /// # Examples /// ``` /// use truck_modeling::*; /// let vertex: Vertex = builder::vertex(Point3::new(0.0, 0.0, 0.0)); /// let line: Edge = builder::tsweep(&vertex, Vector3::unit_x()); /// let square: Face = builder::tsweep(&line, Vector3::unit_y()); /// let cube: Solid = builder::tsweep(&square, Vector3::unit_z()); /// # /// # let b_shell = &cube.boundaries()[0]; /// # assert_eq!(b_shell.len(), 6); // This solid is a cube! /// # assert!(cube.is_geometric_consistent()); /// # /// # let b_loop = &b_shell[0].boundaries()[0]; /// # let mut loop_iter = b_loop.vertex_iter(); /// # assert_eq!(*loop_iter.next().unwrap().lock_point().unwrap(), Point3::new(0.0, 0.0, 0.0)); /// # assert_eq!(*loop_iter.next().unwrap().lock_point().unwrap(), Point3::new(0.0, 1.0, 0.0)); /// # assert_eq!(*loop_iter.next().unwrap().lock_point().unwrap(), Point3::new(1.0, 1.0, 0.0)); /// # assert_eq!(*loop_iter.next().unwrap().lock_point().unwrap(), Point3::new(1.0, 0.0, 0.0)); /// # assert_eq!(loop_iter.next(), None); /// # /// # let b_loop = &b_shell[3].boundaries()[0]; /// # let mut loop_iter = b_loop.vertex_iter(); /// # assert_eq!(*loop_iter.next().unwrap().lock_point().unwrap(), Point3::new(1.0, 1.0, 0.0)); /// # assert_eq!(*loop_iter.next().unwrap().lock_point().unwrap(), Point3::new(0.0, 1.0, 0.0)); /// # assert_eq!(*loop_iter.next().unwrap().lock_point().unwrap(), Point3::new(0.0, 1.0, 1.0)); /// # assert_eq!(*loop_iter.next().unwrap().lock_point().unwrap(), Point3::new(1.0, 1.0, 1.0)); /// # assert_eq!(loop_iter.next(), None); /// # /// # let b_loop = &b_shell[5].boundaries()[0]; /// # let mut loop_iter = b_loop.vertex_iter(); /// # assert_eq!(*loop_iter.next().unwrap().lock_point().unwrap(), Point3::new(0.0, 0.0, 1.0)); /// # assert_eq!(*loop_iter.next().unwrap().lock_point().unwrap(), Point3::new(1.0, 0.0, 1.0)); /// # assert_eq!(*loop_iter.next().unwrap().lock_point().unwrap(), Point3::new(1.0, 1.0, 1.0)); /// # assert_eq!(*loop_iter.next().unwrap().lock_point().unwrap(), Point3::new(0.0, 1.0, 1.0)); /// # assert_eq!(loop_iter.next(), None); /// ``` pub fn tsweep<T: Sweep<Point3, NURBSCurve, NURBSSurface>>(elem: &T, vector: Vector3) -> T::Swept { let trsl = Matrix4::from_translation(vector); elem.sweep( &move |pt| trsl.transform_point(*pt), &move |curve| NURBSCurve::new(trsl * curve.non_rationalized()), &move |surface| NURBSSurface::new(trsl * surface.non_rationalized()), &move |pt0, pt1| { NURBSCurve::new(geom_impls::line(pt0.to_homogeneous(), pt1.to_homogeneous())) }, &move |curve0, curve1| { NURBSSurface::new(BSplineSurface::homotopy( curve0.clone().into_non_rationalized(), curve1.clone().into_non_rationalized(), )) }, ) } /// Sweeps a vertex, an edge, a wire, a face, or a shell by the rotation. /// # Details /// If the absolute value of `angle` is more than 2π rad, then the result is closed shape. /// For example, the result of sweeping a disk is a bent cylinder if `angle` is less than 2π rad /// and a solid torus if `angle` is more than 2π rad. /// # Examples /// ``` /// // Torus /// use truck_modeling::*; /// const PI: Rad<f64> = Rad(std::f64::consts::PI); /// /// let v: Vertex = builder::vertex(Point3::new(3.0, 0.0, 0.0)); /// let circle: Wire = builder::rsweep(&v, Point3::new(2.0, 0.0, 0.0), Vector3::unit_z(), PI * 2.0); /// let torus: Shell = builder::rsweep(&circle, Point3::origin(), Vector3::unit_y(), PI * 2.0); /// let solid: Solid = Solid::new(vec![torus]); /// # /// # assert!(solid.is_geometric_consistent()); /// # const N: usize = 100; /// # let shell = &solid.boundaries()[0]; /// # for face in shell.iter() { /// # let surface = face.lock_surface().unwrap().clone(); /// # for i in 0..=N { /// # for j in 0..=N { /// # let u = i as f64 / N as f64; /// # let v = j as f64 / N as f64; /// # let pt = surface.subs(u, v); /// # /// # // this surface is a part of torus. /// # let tmp = f64::sqrt(pt[0] * pt[0] + pt[2] * pt[2]) - 2.0; /// # let res = tmp * tmp + pt[1] * pt[1]; /// # assert!(Tolerance::near(&res, &1.0)); /// # } /// # } /// # } /// ``` /// ``` /// // Modeling a pipe. /// use truck_modeling::*; /// const PI: Rad<f64> = Rad(std::f64::consts::PI); /// /// // Creates the base circle /// let v: Vertex = builder::vertex(Point3::new(1.0, 0.0, 4.0)); /// let circle: Wire = builder::rsweep(&v, Point3::new(2.0, 0.0, 4.0), -Vector3::unit_z(), PI * 2.0); /// /// // the result shell of the pipe. /// let mut pipe: Shell = Shell::new(); /// /// // Draw the first line pipe /// let mut first_line_part: Shell = builder::tsweep(&circle, Vector3::new(0.0, 0.0, -4.0)); /// pipe.append(&mut first_line_part); /// /// // Get the new wire /// let boundaries: Vec<Wire> = pipe.extract_boundaries(); /// let another_circle: Wire = boundaries.into_iter().find(|wire| wire != &circle).unwrap().inverse(); /// /// // Draw the bent part /// let mut bend_part: Shell = builder::rsweep( /// &another_circle, /// Point3::origin(), /// Vector3::unit_y(), /// PI / 2.0, /// ); /// # let surface = bend_part[0].lock_surface().unwrap().clone(); /// pipe.append(&mut bend_part); /// /// // Get the new wire /// let boundaries: Vec<Wire> = pipe.extract_boundaries(); /// let another_circle: Wire = boundaries.into_iter().find(|wire| wire != &circle).unwrap().inverse(); /// /// // Draw the second line pipe /// let mut second_line_part: Shell = builder::tsweep(&another_circle, Vector3::new(-4.0, 0.0, 0.0)); /// pipe.append(&mut second_line_part); /// /// assert_eq!(pipe.shell_condition(), ShellCondition::Oriented); /// # assert!(pipe.is_geometric_consistent()); /// # const N: usize = 100; /// # for i in 0..=N { /// # for j in 0..=N { /// # let u = i as f64 / N as f64; /// # let v = j as f64 / N as f64; /// # let pt = surface.subs(u, v); /// # /// # // the y coordinate is positive. /// # //assert!(pt[1] >= 0.0); /// # /// # // this surface is a part of torus. /// # let tmp = f64::sqrt(pt[0] * pt[0] + pt[2] * pt[2]) - 2.0; /// # let res = tmp * tmp + pt[1] * pt[1]; /// # assert!(Tolerance::near(&res, &1.0)); /// # } /// # } /// ``` #[inline(always)] pub fn rsweep<T: ClosedSweep<Point3, NURBSCurve, NURBSSurface>>( elem: &T, origin: Point3, axis: Vector3, angle: Rad<f64>, ) -> T::Swept { if angle.0.abs() < 2.0 * PI.0 { partial_rsweep(elem, origin, axis, angle) } else if angle.0 > 0.0 { whole_rsweep(elem, origin, axis) } else { whole_rsweep(elem, origin, -axis) } } fn partial_rsweep<T: MultiSweep<Point3, NURBSCurve, NURBSSurface>>( elem: &T, origin: Point3, axis: Vector3, angle: Rad<f64>, ) -> T::Swept { let division = if angle.0.abs() < PI.0 { 1 } else { 2 }; let mat0 = Matrix4::from_translation(-origin.to_vec()); let mat1 = Matrix4::from_axis_angle(axis, angle / division as f64); let mat2 = Matrix4::from_translation(origin.to_vec()); let trsl = mat2 * mat1 * mat0; elem.multi_sweep( &move |pt| trsl.transform_point(*pt), &move |curve| NURBSCurve::new(trsl * curve.non_rationalized()), &move |surface| NURBSSurface::new(trsl * surface.non_rationalized()), &move |pt, _| { NURBSCurve::new(geom_impls::circle_arc( pt.to_homogeneous(), origin, axis, angle / division as f64, )) }, &move |curve, _| { NURBSSurface::new(geom_impls::rsweep_surface( curve.non_rationalized(), origin, axis, angle / division as f64, )) }, division, ) } fn whole_rsweep<T: ClosedSweep<Point3, NURBSCurve, NURBSSurface>>( elem: &T, origin: Point3, axis: Vector3, ) -> T::Swept { let mat0 = Matrix4::from_translation(-origin.to_vec()); let mat1 = Matrix4::from_axis_angle(axis, PI); let mat2 = Matrix4::from_translation(origin.to_vec()); let trsl = mat2 * mat1 * mat0; elem.closed_sweep( &move |pt| trsl.transform_point(*pt), &move |curve| NURBSCurve::new(trsl * curve.non_rationalized()), &move |surface| NURBSSurface::new(trsl * surface.non_rationalized()), &move |pt, _| { NURBSCurve::new(geom_impls::circle_arc( pt.to_homogeneous(), origin, axis, PI, )) }, &move |curve, _| { NURBSSurface::new(geom_impls::rsweep_surface( curve.non_rationalized(), origin, axis, PI, )) }, 2, ) } #[test] fn partial_torus() { let v = vertex(Point3::new(0.5, 0.0, 0.0)); let w = rsweep(&v, Point3::new(0.75, 0.0, 0.0), Vector3::unit_y(), Rad(7.0)); let face = try_attach_plane(&vec![w]).unwrap(); let torus = rsweep(&face, Point3::origin(), Vector3::unit_z(), Rad(2.0)); assert!(torus.is_geometric_consistent()); let torus = rsweep(&face, Point3::origin(), Vector3::unit_z(), Rad(5.0)); assert!(torus.is_geometric_consistent()); let torus = rsweep(&face, Point3::origin(), Vector3::unit_z(), Rad(-2.0)); assert!(torus.is_geometric_consistent()); let torus = rsweep(&face, Point3::origin(), Vector3::unit_z(), Rad(-5.0)); assert!(torus.is_geometric_consistent()); }