1submodule(
fplot_core) fplot_triangulations_delaunay_2d
5 module subroutine d2d_init(this, x, y, err)
7 class(delaunay_tri_2d),
intent(inout) :: this
8 real(real64),
intent(in),
dimension(:) :: x, y
9 class(errors),
intent(inout),
target,
optional :: err
12 class(errors),
pointer :: errmgr
13 type(errors),
target :: deferr
14 character(len = 256) :: errmsg
15 integer(int32) :: i, npts, ntri, flag
16 real(real64),
allocatable,
dimension(:,:) :: nodexy
17 integer(int32),
allocatable,
dimension(:,:) :: trinode, trinbr
20 if (
present(err))
then
28 if (
size(y) /= npts)
then
30 "Expected the y-coordinate array to have ", npts, &
31 " elements, but found ",
size(y),
" instead."
32 call errmgr%report_error(
"d2d_init", trim(errmsg), &
33 plot_array_size_mismatch_error)
38 if (
allocated(this%m_x))
deallocate(this%m_x)
39 if (
allocated(this%m_y))
deallocate(this%m_y)
40 if (
allocated(this%m_indices))
deallocate(this%m_indices)
43 allocate(nodexy(2, npts), stat = flag)
44 if (flag == 0)
allocate(trinode(3, 2 * npts), stat = flag)
45 if (flag == 0)
allocate(trinbr(3, 2 * npts), stat = flag)
46 if (flag /= 0)
go to 100
55 call r8tris2(npts, nodexy, ntri, trinode, trinbr)
58 allocate(this%m_x(npts), stat = flag)
59 if (flag == 0)
allocate(this%m_y(npts), stat = flag)
60 if (flag == 0)
allocate(this%m_indices(ntri, 3), stat = flag)
63 this%m_x(i) = nodexy(1,i)
64 this%m_y(i) = nodexy(2, i)
68 this%m_indices(i,:) = trinode(:,i)
76 call errmgr%report_error(
"d2d_init",
"Insufficient memory available.", &
77 plot_out_of_memory_error)
80200
format(a, i0, a, i0, a)
84 pure module function d2d_get_pt_count(this) result(rst)
85 class(delaunay_tri_2d),
intent(in) :: this
87 if (
allocated(this%m_x))
then
95 pure module function d2d_get_tri_count(this) result(rst)
96 class(delaunay_tri_2d),
intent(in) :: this
98 if (
allocated(this%m_indices))
then
99 rst =
size(this%m_indices, 1)
106 pure module function d2d_get_x_pts(this) result(rst)
107 class(delaunay_tri_2d),
intent(in) :: this
108 real(real64),
allocatable,
dimension(:) :: rst
109 if (
allocated(this%m_x))
then
117 pure module function d2d_get_y_pts(this) result(rst)
118 class(delaunay_tri_2d),
intent(in) :: this
119 real(real64),
allocatable,
dimension(:) :: rst
120 if (
allocated(this%m_y))
then
128 pure module function d2d_get_tris(this) result(rst)
129 class(delaunay_tri_2d),
intent(in) :: this
130 integer(int32),
allocatable,
dimension(:,:) :: rst
131 if (
allocated(this%m_indices))
then
139 pure module function d2d_get_tri_with_pt(this, x, y) result(rst)
141 class(delaunay_tri_2d),
intent(in) :: this
142 real(real64),
intent(in) :: x, y
143 integer(int32) :: rst
146 integer(int32) :: i, j
147 real(real64) :: x1, y1, x2, y2, x3, y3
154 do i = 1, this%get_triangle_count()
155 j = this%m_indices(i, 1)
159 j = this%m_indices(i, 2)
163 j = this%m_indices(i, 3)
167 check = point_inside_triangle(x1, y1, x2, y2, x3, y3, x, y)
178 pure elemental function point_inside_triangle(x1, y1, x2, y2, x3, y3, &
181 real(real64),
intent(in) :: x1, y1, x2, y2, x3, y3, x, y
185 real(real64) :: lambda1, lambda2, dT
188 dt = (y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3)
189 lambda1 = ((y2 - y3) * (x - x3) + (x3 - x2) * (y - y3)) / dt
190 lambda2 = ((y3 - y1) * (x - x3) + (x1 - x3) * (y - y3)) / dt
196 rst = (lambda1 <= 1.0d0 .and. lambda1 >= 0.0d0) .and. &
197 (lambda2 <= 1.0d0 .and. lambda2 >= 0.0d0) .and. &
198 (lambda1 + lambda2 >= 0.0d0 .and. lambda1 + lambda2 <= 1.0d0)