-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lineclipping.f90
129 lines (99 loc) · 2.86 KB
/
lineclipping.f90
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
module lineclip
use, intrinsic:: iso_c_binding, only: c_int
use, intrinsic:: iso_fortran_env, only : stderr=>error_unit
use, intrinsic:: ieee_arithmetic, only: ieee_value, ieee_quiet_nan, ieee_is_nan
use assert, only: wp
implicit none
integer,parameter :: inside=0,left=1,right=2,lower=4,upper=8
private
public:: wp,cohensutherland,Ccohensutherland
contains
subroutine Ccohensutherland(xmin,ymax,xmax,ymin,Np,x1,y1,x2,y2) bind(c)
! for C/C++/f2py that need constant length arrays
!
! INPUTS
! ------
! xmin,ymax,xmax,ymin: upper left and lower right corners of box (pixel coordinates)
! Np: number of points in vectors (number of elementts)
!
! INOUT
! -----
! x1,y1,x2,y2:
! in - endpoints of line
! out - intersection points with box. If no intersection, all NaN
integer(c_int), intent(in) :: Np
real(wp),intent(in) :: xmin,ymax,xmax,ymin
real(wp),intent(inout), dimension(Np):: x1,y1,x2,y2
call cohensutherland(xmin, ymax, xmax,ymin,x1,y1,x2,y2)
end subroutine Ccohensutherland
elemental subroutine cohensutherland(xmin,ymax,xmax,ymin, &
x1, y1, x2, y2)
! INPUTS
! ------
! xmin,ymax,xmax,ymin: upper left and lower right corners of box (pixel coordinates)
!
! INOUT
! -----
! x1,y1,x2,y2:
! in - endpoints of line
! out - intersection points with box. If no intersection, all NaN
real(wp), intent(in) :: xmin,ymax,xmax,ymin
real(wp), intent(out):: x1,y1,x2,y2
real(wp) :: nan
integer k1, k2, opt ! just plain integers
real(wp) :: x,y
nan = ieee_value(1.,ieee_quiet_nan)
y = nan
x = nan
! check for trivially outside lines
k1 = getclip(x1,y1,xmin,xmax,ymin,ymax)
k2 = getclip(x2,y2,xmin,xmax,ymin,ymax)
do while (ior(k1,k2) /= 0)
!trivially outside window, Reject
if (iand(k1,k2) /= 0) then
x1=nan; y1=nan; x2=nan; y2=nan
return
endif
opt = merge(k1,k2,k1 > 0)
if (iand(opt,UPPER) > 0) then
x = x1 + (x2 - x1) * (ymax - y1) / (y2 - y1)
y = ymax
elseif (iand(opt,LOWER) > 0) then
x = x1 + (x2 - x1) * (ymin - y1) / (y2 - y1)
y = ymin
elseif (iand(opt,RIGHT) > 0) then
y = y1 + (y2 - y1) * (xmax - x1) / (x2 - x1)
x = xmax
elseif (iand(opt,LEFT) > 0) then
y = y1 + (y2 - y1) * (xmin - x1) / (x2 - x1)
x = xmin
else
error stop 'undefined clipping state'
endif
if (opt == k1) then ! not case select
x1 = x; y1 = y
k1 = getclip(x1,y1,xmin,xmax,ymin,ymax)
elseif (opt == k2) then
x2 = x; y2 = y
k2 = getclip(x2,y2,xmin,xmax,ymin,ymax)
endif
end do
end subroutine cohensutherland
elemental function getclip(xa,ya,xmin,xmax,ymin,ymax) result(p) ! bit patterns
real(wp), intent(in) :: xa,ya,xmin,xmax,ymin,ymax
integer p
p = inside ! default
!consider x
if (xa < xmin) then
p = ior(p,left)
elseif (xa > xmax) then
p = ior(p,right)
endif
!consider y
if (ya < ymin) then
p = ior(p,lower)
elseif (ya > ymax) then
p = ior(p,upper)
endif
end function getclip
end module lineclip