Newer
Older
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
overlapping.polygons<-function(listpoly) {
stopifnot(unlist(lapply(listpoly,is.poly)))
nbpoly<-length(listpoly)
res<-rep(FALSE,(nbpoly-1))
for(i in 1:(nbpoly-1)) {
for(j in (i+1):nbpoly) {
if(abs(overlap.poly(listpoly[[i]],listpoly[[j]]))>.Machine$double.eps^0.5)
res[i]<-TRUE
}
}
return(res)
}
#from area.xypolygon{spatstat}
#return area>0 when (xp,yp) vertices are ranked anticlockwise
#or area<0 when (xp,yp) vertices are ranked clockwise
area.poly<-function(xp,yp) {
nedges <- length(xp)
yp <- yp - min(yp)
nxt <- c(2:nedges, 1)
dx <- xp[nxt] - xp
ym <- (yp + yp[nxt])/2
-sum(dx * ym)
}
in.circle<-function(x,y,x0,y0,r0,bdry=TRUE) {
stopifnot(length(x)==length(y))
l<-length(x)
inside<-vector(mode="logical",length=l)
for(i in 1:l) {
if(bdry) {
if(((x[i]-x0)^2+(y[i]-y0)^2)<=(r0^2))
inside[i]<-TRUE
}
else {
if(((x[i]-x0)^2+(y[i]-y0)^2)<(r0^2))
inside[i]<-TRUE
}
}
return(inside)
}
in.rectangle<-function(x,y,xmin,ymin,xmax,ymax,bdry=TRUE) {
stopifnot(length(x)==length(y))
stopifnot((xmax-xmin)>0)
stopifnot((ymax-ymin)>0)
rect<-list(x=c(xmin,xmax,xmax,xmin),y=c(ymin,ymin,ymax,ymax))
return(in.poly(x,y,rect,bdry))
}
in.triangle<-function(x,y,ax,ay,bx,by,cx,cy,bdry=TRUE) {
stopifnot(length(x)==length(y))
tri<-list(x=c(ax,bx,cx),y=c(ay,by,cy))
return(in.poly(x,y,tri,bdry))
}
#modified from plot.ppp{spatstat}
adjust.marks.size<-function(marks,window,maxsize=NULL) {
if(is.null(maxsize)) {
if("rectangle"%in%window$type)
diam<-sqrt((window$xmin-window$xmax)^2+(window$ymin-window$ymax)^2)
else if("circle"%in%window$type)
diam<-2*window$r0
maxsize<-min(1.4/sqrt(pi*length(marks)/area.swin(window)),diam*0.07)
}
mr<-range(c(0,marks))
maxabs<-max(abs(mr))
if(diff(mr)<4*.Machine$double.eps||maxabs<4*.Machine$double.eps) {
ms<-rep(0.5*maxsize,length(marks))
mp.value<-mr[1]
mp.plotted<-0.5*maxsize
}
else {
scal<-maxsize/maxabs
ms<-marks*scal
mp.value<-pretty(mr)
mp.plotted<-mp.value*scal
}
return(ms)
}
#modified from verify.xypolygon{spatstat}
is.poly<-function (p) {
stopifnot(is.list(p))
stopifnot(length(p)==2,length(names(p))==2)
stopifnot(identical(sort(names(p)),c("x","y")))
stopifnot(!is.null(p$x),!is.null(p$y))
stopifnot(is.numeric(p$x),is.numeric(p$y))
stopifnot(length(p$x)==length(p$y))
return(TRUE)
}
testInteger<-function(i) {
if(as.integer(i)!=i) {
warning(paste(substitute(i),"=",i," has been converted to integer : ",as.integer(i),sep=""),call.=FALSE)
i<-as.integer(i)
}
return(i)
}
testIC<-function(nbSimu,lev) {
if(lev*(nbSimu+1)<5) {
warning(paste(
"Low validity test: a*(n+1) < 5\n Significance level: a = ",lev,
"\n Number of simulations: n = ",nbSimu,"\n",sep=""))
}
}
#from spatstat
overlap.poly <- function(P, Q) {
# compute area of overlap of two simple closed polygons
# verify.xypolygon(P)
#verify.xypolygon(Q)
xp <- P$x
yp <- P$y
np <- length(xp)
nextp <- c(2:np, 1)
xq <- Q$x
yq <- Q$y
nq <- length(xq)
nextq <- c(2:nq, 1)
# adjust y coordinates so all are nonnegative
ylow <- min(c(yp,yq))
yp <- yp - ylow
yq <- yq - ylow
area <- 0
for(i in 1:np) {
ii <- c(i, nextp[i])
xpii <- xp[ii]
ypii <- yp[ii]
for(j in 1:nq) {
jj <- c(j, nextq[j])
area <- area +
overlap.trapez(xpii, ypii, xq[jj], yq[jj])
}
}
return(area)
}
#from spatstat
overlap.trapez <- function(xa, ya, xb, yb, verb=FALSE) {
# compute area of overlap of two trapezia
# which have same baseline y = 0
#
# first trapezium has vertices
# (xa[1], 0), (xa[1], ya[1]), (xa[2], ya[2]), (xa[2], 0).
# Similarly for second trapezium
# Test for vertical edges
dxa <- diff(xa)
dxb <- diff(xb)
if(dxa == 0 || dxb == 0)
return(0)
# Order x coordinates, x0 < x1
if(dxa > 0) {
signa <- 1
lefta <- 1
righta <- 2
if(verb) cat("A is positive\n")
} else {
signa <- -1
lefta <- 2
righta <- 1
if(verb) cat("A is negative\n")
}
if(dxb > 0) {
signb <- 1
leftb <- 1
rightb <- 2
if(verb) cat("B is positive\n")
} else {
signb <- -1
leftb <- 2
rightb <- 1
if(verb) cat("B is negative\n")
}
signfactor <- signa * signb # actually (-signa) * (-signb)
if(verb) cat(paste("sign factor =", signfactor, "\n"))
# Intersect x ranges
x0 <- max(xa[lefta], xb[leftb])
x1 <- min(xa[righta], xb[rightb])
if(x0 >= x1)
return(0)
if(verb) {
cat(paste("Intersection of x ranges: [", x0, ",", x1, "]\n"))
abline(v=x0, lty=3)
abline(v=x1, lty=3)
}
# Compute associated y coordinates
slopea <- diff(ya)/diff(xa)
y0a <- ya[lefta] + slopea * (x0-xa[lefta])
y1a <- ya[lefta] + slopea * (x1-xa[lefta])
slopeb <- diff(yb)/diff(xb)
y0b <- yb[leftb] + slopeb * (x0-xb[leftb])
y1b <- yb[leftb] + slopeb * (x1-xb[leftb])
# Determine whether upper edges intersect
# if not, intersection is a single trapezium
# if so, intersection is a union of two trapezia
yd0 <- y0b - y0a
yd1 <- y1b - y1a
if(yd0 * yd1 >= 0) {
# edges do not intersect
areaT <- (x1 - x0) * (min(y1a,y1b) + min(y0a,y0b))/2
if(verb) cat(paste("Edges do not intersect\n"))
} else {
# edges do intersect
# find intersection
xint <- x0 + (x1-x0) * abs(yd0/(yd1 - yd0))
yint <- y0a + slopea * (xint - x0)
if(verb) {
cat(paste("Edges intersect at (", xint, ",", yint, ")\n"))
points(xint, yint, cex=2, pch="O")
}
# evaluate left trapezium
left <- (xint - x0) * (min(y0a, y0b) + yint)/2
# evaluate right trapezium
right <- (x1 - xint) * (min(y1a, y1b) + yint)/2
areaT <- left + right
if(verb)
cat(paste("Left area = ", left, ", right=", right, "\n"))
}
# return area of intersection multiplied by signs
return(signfactor * areaT)
}
#TRUE: les points sur la bordure sont = inside
in.poly<-function(x,y,poly,bdry=TRUE) {
stopifnot(is.poly(poly))
xp <- poly$x
yp <- poly$y
npts <- length(x)
nedges <- length(xp) # sic
score <- rep(0, npts)
on.boundary <- rep(FALSE, npts)
temp <- .Fortran(
"inpoly",
x=as.double(x),
y=as.double(y),
xp=as.double(xp),
yp=as.double(yp),
npts=as.integer(npts),
nedges=as.integer(nedges),
score=as.double(score),
onbndry=as.logical(on.boundary),
PACKAGE="ads"
)
score <- temp$score
on.boundary <- temp$onbndry
score[on.boundary] <- 1
res<-rep(FALSE,npts)
res[score==(-1)]<-TRUE
if(bdry)
res[score==1]<-TRUE
return(res)
}
####################
convert<-function(x) {
r<-alist()
x<-as.matrix(x)
for(i in 1:dim(x)[1])
r[[i]]<-data.frame(x=c(x[i,1],x[i,5],x[i,3]),y=c(x[i,2],x[i,6],x[i,4]))
return(r)
}
convert2<-function(x){ # liste de liste vers df 6 var
x<-unlist(x)
mat<-matrix(x,ncol=6,byrow=TRUE,dimnames=list(NULL,c("ax","bx","cx","ay","by","cy")))
#mat<-cbind(tmp$ax,tmp$ay,tmp$bx,tmp$by,tmp$cx,tmp$cy)
r<-data.frame(ax=mat[,1],ay=mat[,4],bx=mat[,2],by=mat[,5],cx=mat[,3],cy=mat[,6])
return(r)
}
read.tri<-function(X) {
res<-NULL
tabtri<-read.table(X)
n<-length(tabtri[,1])
for(i in 1:n) {
tri<-list(x=c(tabtri[,1][i],tabtri[,3][i],tabtri[,5][i]),
y=c(tabtri[,2][i],tabtri[,4][i],tabtri[,6][i]))
#if(area.xypolygon(tri)>0){
if(area.poly(tri$x,tri$y)>0){
tri<-list(x=c(tabtri[,5][i],tabtri[,3][i],tabtri[,1][i]),
y=c(tabtri[,6][i],tabtri[,4][i],tabtri[,2][i]))
}
res<-c(res,list(tri))
}
if(length(res)==1) {
res<-unlist(res,recursive=FALSE)
}
return(res)
}
##############
#subsetting dist objects
#sub is a logical vector of True/False
subsetdist<-function(dis,sub) {
mat<-as.matrix(dis)
k<-dimnames(mat)[[1]]%in%sub
submat<-mat[k,k]
return(as.dist(submat))
}
#ordering dist objetcs on ind
sortmat<-function(dis,ind) {
mat<-as.matrix(dis)[,ind]
mat<-mat[ind,]
return(as.dist(mat))
}