Skip to content

Commit

Permalink
Add new predefined NURBS shapes.
Browse files Browse the repository at this point in the history
  • Loading branch information
gha3mi committed Apr 18, 2024
1 parent f716a75 commit 084c9b1
Show file tree
Hide file tree
Showing 12 changed files with 552 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ ForCAD supports **B-Spline**, **NURBS**, **Bezier**, and **Rational Bezier** cur
- Obtain visualized elements connectivity and coordinates for geometry and control geometry.
- Mesh insertion into a NURBS object.
- Export NURBS objects to VTK files for visualization.
- Includes predefined NURBS shapes: Circle, Tetragon, Hexahedron, Ring (2D), Ring (3D).
- Includes predefined NURBS shapes: Circle, Half Circle, Tetragon, Hexahedron, 2D Ring, Half 2D Ring, 3D Ring, Half 3D Ring, C-shapes.
- Rotate and translate NURBS objects.
- Visualization using provided python PyVista scripts.

Expand Down
43 changes: 43 additions & 0 deletions example/shape_C_1d.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
program shape_C_1d

use forcad, only: rk, nurbs_curve

implicit none
type(nurbs_curve) :: shape

!-----------------------------------------------------------------------------
! Setting up NURBS C-shape
!-----------------------------------------------------------------------------

!> Set a C-shape with radius 2.0 and center at [0.0, 0.0, 0.0]
call shape%set_C(center = [0.0_rk, 0.0_rk, 0.0_rk], radius = 2.0_rk)

!> Export control points to a VTK file
call shape%export_Xc('vtk/shape_C_1d_Xc.vtk')

!-----------------------------------------------------------------------------
! Creating C-shape
!-----------------------------------------------------------------------------

!> Generate the NURBS C-shape with a resolution of 100
call shape%create(res = 100)

!> Export the generated cirlce to a VTK file
call shape%export_Xg('vtk/shape_C_1d_Xg.vtk')

!-----------------------------------------------------------------------------
! Visualization using PyVista
! Note: PyVista is required for visualization. Install it using `pip install pyvista`
!-----------------------------------------------------------------------------

!> Show the control geometry and geometry using PyVista
call shape%show('vtk/shape_C_1d_Xc.vtk','vtk/shape_C_1d_Xg.vtk')

!-----------------------------------------------------------------------------
! Finalizing
!-----------------------------------------------------------------------------

!> Finalize the NURBS curve object
call shape%finalize()

end program
43 changes: 43 additions & 0 deletions example/shape_C_2d.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
program shape_C_2d

use forcad, only: rk, nurbs_surface

implicit none
type(nurbs_surface) :: shape

!-----------------------------------------------------------------------------
! Setting up NURBS C-shape
!-----------------------------------------------------------------------------

!> Set a C-shape with radius 2.0 and center at [0.0, 0.0, 0.0]
call shape%set_C(center = [0.0_rk, 0.0_rk, 0.0_rk], radius1 = 1.0_rk, radius2 = 2.0_rk)

!> Export control points to a VTK file
call shape%export_Xc('vtk/shape_C_2d_Xc.vtk')

!-----------------------------------------------------------------------------
! Creating C-shape
!-----------------------------------------------------------------------------

!> Generate the NURBS C-shape with a resolution of 100
call shape%create(100, 60)

!> Export the generated cirlce to a VTK file
call shape%export_Xg('vtk/shape_C_2d_Xg.vtk')

!-----------------------------------------------------------------------------
! Visualization using PyVista
! Note: PyVista is required for visualization. Install it using `pip install pyvista`
!-----------------------------------------------------------------------------

!> Show the control geometry and geometry using PyVista
call shape%show('vtk/shape_C_2d_Xc.vtk','vtk/shape_C_2d_Xg.vtk')

!-----------------------------------------------------------------------------
! Finalizing
!-----------------------------------------------------------------------------

!> Finalize the NURBS curve object
call shape%finalize()

end program
43 changes: 43 additions & 0 deletions example/shape_C_3d.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
program shape_C_3d

use forcad, only: rk, nurbs_volume

implicit none
type(nurbs_volume) :: shape

!-----------------------------------------------------------------------------
! Setting up NURBS C-shape
!-----------------------------------------------------------------------------

!> Set a C-shape with radius 2.0 and center at [0.0, 0.0, 0.0]
call shape%set_C(center = [0.0_rk, 0.0_rk, 0.0_rk], radius1 = 1.0_rk, radius2 = 2.0_rk, length = 2.0_rk)

!> Export control points to a VTK file
call shape%export_Xc('vtk/shape_C_3d_Xc.vtk')

!-----------------------------------------------------------------------------
! Creating C-shape
!-----------------------------------------------------------------------------

!> Generate the NURBS C-shape with a resolution of 100
call shape%create(100, 60, 10)

!> Export the generated cirlce to a VTK file
call shape%export_Xg('vtk/shape_C_3d_Xg.vtk')

!-----------------------------------------------------------------------------
! Visualization using PyVista
! Note: PyVista is required for visualization. Install it using `pip install pyvista`
!-----------------------------------------------------------------------------

!> Show the control geometry and geometry using PyVista
call shape%show('vtk/shape_C_3d_Xc.vtk','vtk/shape_C_3d_Xg.vtk')

!-----------------------------------------------------------------------------
! Finalizing
!-----------------------------------------------------------------------------

!> Finalize the NURBS curve object
call shape%finalize()

end program
32 changes: 32 additions & 0 deletions example/shape_half_circle.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
program shape_half_circle

use forcad, only: rk, nurbs_curve

implicit none
type(nurbs_curve) :: shape


!> Set up a half circle shape centered at the 0,0,0 with a radius of 1
call shape%set_half_circle([0.0_rk, 0.0_rk, 0.0_rk], 1.0_rk)

!> Export the control points to a VTK file for visualization.
call shape%export_Xc('vtk/shape_half_circle_Xc.vtk')

!> Create the shape using the specified number of elements in each direction.
call shape%create(60)

!> Export the geometry to a VTK file for visualization.
call shape%export_Xg('vtk/shape_half_circle_Xg.vtk')

!-----------------------------------------------------------------------------
! Visualization using PyVista
! Note: PyVista is required for visualization. Install it using `pip install pyvista`
!-----------------------------------------------------------------------------

!> Show the control geometry and geometry using PyVista
call shape%show('vtk/shape_half_circle_Xc.vtk','vtk/shape_half_circle_Xg.vtk')

!> Finalize and clean up the shape object.
call shape%finalize()

end program
32 changes: 32 additions & 0 deletions example/shape_half_ring_2d.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
program shape_half_ring_2d

use forcad, only: rk, nurbs_surface

implicit none
type(nurbs_surface) :: shape


!> Set up a half ring shape centered at 0,0,0 with inner radius 1 and outer radius 2.
call shape%set_half_ring([0.0_rk, 0.0_rk, 0.0_rk], 1.0_rk, 2.0_rk)

!> Export the control points to a VTK file for visualization.
call shape%export_Xc('vtk/shape_half_ring_2d_Xc.vtk')

!> Create the shape using the specified number of elements in each direction.
call shape%create(60, 15)

!> Export the geometry to a VTK file for visualization.
call shape%export_Xg('vtk/shape_half_ring_2d_Xg.vtk')

!-----------------------------------------------------------------------------
! Visualization using PyVista
! Note: PyVista is required for visualization. Install it using `pip install pyvista`
!-----------------------------------------------------------------------------

!> Show the control geometry and geometry using PyVista
call shape%show('vtk/shape_half_ring_2d_Xc.vtk','vtk/shape_half_ring_2d_Xg.vtk')

!> Finalize and clean up the shape object.
call shape%finalize()

end program
32 changes: 32 additions & 0 deletions example/shape_half_ring_3d.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
program shape_half_ring_3d

use forcad, only: rk, nurbs_volume

implicit none
type(nurbs_volume) :: shape


!> Set up a half ring centered at 0,0,0 with inner radius 1, outer radius 2, and length 1.
call shape%set_half_ring([0.0_rk, 0.0_rk, 0.0_rk], 1.0_rk, 2.0_rk, 1.0_rk)

!> Export the control points to a VTK file for visualization.
call shape%export_Xc('vtk/shape_half_ring_3d_Xc.vtk')

!> Create the shape using the specified number of elements in each direction.
call shape%create(60, 15, 10)

!> Export the geometry to a VTK file for visualization.
call shape%export_Xg('vtk/shape_half_ring_3d_Xg.vtk')

!-----------------------------------------------------------------------------
! Visualization using PyVista
! Note: PyVista is required for visualization. Install it using `pip install pyvista`
!-----------------------------------------------------------------------------

!> Show the control geometry and geometry using PyVista
call shape%show('vtk/shape_half_ring_3d_Xc.vtk','vtk/shape_half_ring_3d_Xg.vtk')

!> Finalize and clean up the shape object.
call shape%finalize()

end program
2 changes: 1 addition & 1 deletion example/shape_ring_3d.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ program shape_ring_3d
type(nurbs_volume) :: shape


!> Set up a ring shape with inner radius 1.0, outer radius 2.0, and thickness 1.0.
!> Set up a ring shape centered at 0,0,0 with inner radius 1, outer radius 2, and length 1.
call shape%set_ring([0.0_rk, 0.0_rk, 0.0_rk], 1.0_rk, 2.0_rk, 1.0_rk)

!> Export the control points to a VTK file for visualization.
Expand Down
30 changes: 30 additions & 0 deletions fpm.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ name = "shape_circle"
source-dir = "example"
main = "shape_circle.f90"

[[example]]
name = "shape_half_circle"
source-dir = "example"
main = "shape_half_circle.f90"

[[example]]
name = "shape_C_1d"
source-dir = "example"
main = "shape_C_1d.f90"

[[example]]
name = "shape_C_2d"
source-dir = "example"
main = "shape_C_2d.f90"

[[example]]
name = "shape_C_3d"
source-dir = "example"
main = "shape_C_3d.f90"

[[example]]
name = "shape_tetragon"
source-dir = "example"
Expand All @@ -76,6 +96,16 @@ name = "shape_ring_3d"
source-dir = "example"
main = "shape_ring_3d.f90"

[[example]]
name = "shape_half_ring_2d"
source-dir = "example"
main = "shape_half_ring_2d.f90"

[[example]]
name = "shape_half_ring_3d"
source-dir = "example"
main = "shape_half_ring_3d.f90"

[[example]]
name = "put_to_nurbs"
source-dir = "example"
Expand Down
73 changes: 73 additions & 0 deletions src/forcad_nurbs_curve.f90
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ module forcad_nurbs_curve

! Shapes
procedure :: set_circle !!> Set a circle
procedure :: set_half_circle !!> Set a half circle
procedure :: set_C !!> Set a C-shape
end type
!===============================================================================

Expand Down Expand Up @@ -1168,6 +1170,41 @@ pure subroutine set_circle(this, center, radius)
!===============================================================================


!===============================================================================
!> author: Seyed Ali Ghasemi
!> license: BSD 3-Clause
pure subroutine set_C(this, center, radius)
class(nurbs_curve), intent(inout) :: this
real(rk), intent(in), contiguous :: center(:)
real(rk), intent(in) :: radius
real(rk), allocatable :: Xc(:,:), Wc(:), knot(:)
integer :: i

! Define control points for C-shape
allocate(Xc(5, 3))
Xc(1,:)= [ 1.0_rk, 0.0_rk, 0.0_rk]
Xc(2,:)= [ 1.0_rk, sqrt(3.0_rk), 0.0_rk]
Xc(3,:)= [-0.5_rk, sqrt(3.0_rk)/2.0_rk, 0.0_rk]
Xc(4,:)= [-2.0_rk, 0.0_rk, 0.0_rk]
Xc(5,:)= [-0.5_rk, -sqrt(3.0_rk)/2.0_rk, 0.0_rk]

! Scale and translate the control points
do i = 1, size(Xc, 1)
Xc(i,:) = center + Xc(i,:) * radius
end do

! Define weights for the control points
Wc = [1.0_rk, 0.5_rk, 1.0_rk, 0.5_rk, 1.0_rk]

! Define knot vector
knot = [0.0_rk, 0.0_rk, 0.0_rk, 1.0_rk/2.0_rk, 1.0_rk/2.0_rk, 1.0_rk, 1.0_rk, 1.0_rk]

! Set knot vector, control points, and weights
call this%set(knot, Xc, Wc)
end subroutine
!===============================================================================


!===============================================================================
!> author: Seyed Ali Ghasemi
!> license: BSD 3-Clause
Expand Down Expand Up @@ -1360,4 +1397,40 @@ impure subroutine show(this, vtkfile_Xc, vtkfile_Xg)
end subroutine
!===============================================================================


!===============================================================================
!> author: Seyed Ali Ghasemi
!> license: BSD 3-Clause
pure subroutine set_half_circle(this, center, radius)
class(nurbs_curve), intent(inout) :: this
real(rk), intent(in), contiguous :: center(:)
real(rk), intent(in) :: radius
real(rk), allocatable :: Xc(:,:), Wc(:), knot(:)
integer :: i

! Define control points for half circle
allocate(Xc(5, 3))
Xc(1,:) = [ 0.5_rk, 0.0_rk, 0.0_rk]
Xc(2,:) = [ 0.5_rk, 0.5_rk, 0.0_rk]
Xc(3,:) = [ 0.0_rk, 0.5_rk, 0.0_rk]
Xc(4,:) = [-0.5_rk, 0.5_rk, 0.0_rk]
Xc(5,:) = [-0.5_rk, 0.0_rk, 0.0_rk]

! Scale and translate the control points
do i = 1, size(Xc, 1)
Xc(i,:) = center + Xc(i,:) * radius
end do

! Define weights for the control points
Wc = [1.0_rk, 1.0_rk/sqrt(2.0_rk), 1.0_rk, 1.0_rk/sqrt(2.0_rk), 1.0_rk]

! Define knot vector
knot = [0.0_rk, 0.0_rk, 0.0_rk, 1.0_rk/2.0_rk, &
1.0_rk/2.0_rk, 1.0_rk, 1.0_rk, 1.0_rk]

! Set knot vector, control points, and weights
call this%set(knot, Xc, Wc)
end subroutine
!===============================================================================

end module forcad_nurbs_curve
Loading

0 comments on commit 084c9b1

Please sign in to comment.