-
Notifications
You must be signed in to change notification settings - Fork 13
/
point.R
311 lines (294 loc) · 8.5 KB
/
point.R
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
#' Vector of points
#'
#' Points are the fundamental unit in geometry, from which other geometric
#' primitives can be constructed. Points support less than and greater than
#' operations which are handled lexicographically. This means that point vectors
#' can also be sorted and ranked. Subtracking a point from a point gives a
#' vector. Adding and subtracting a vector to a point translates the point by
#' the vector.
#'
#' @param ... Various input. See the Constructor section.
#' @param default_dim The dimensionality when constructing an empty vector
#' @param x A point vector or an object to convert to it
#'
#' @return a `euclid_point` vector
#'
#' @section Constructors:
#' **2 dimensional points**
#' - Providing two numeric vector will construct points with those x and y
#' coordinate.
#' - Providing a vector will construct a point at the location the vector
#' points to.
#' - Providing a weighted point vector will create a points at the same
#' locations without weights.
#'
#' **3 dimensional points**
#' - Providing three numeric vector will construct points with those x, y, and z
#' coordinate.
#' - Providing a vector will construct a point at the location the vector
#' points to.
#' - Providing a weighted point vector will create a points at the same
#' locations without weights.
#'
#' @export
#'
#' @family Geometries
#' @family Locations
#'
#' @examples
#' num1 <- exact_numeric(runif(5))
#' num2 <- exact_numeric(runif(5))
#' num3 <- exact_numeric(runif(5))
#'
#' # 2 dimensions
#' p <- point(num1, num2)
#' p
#'
#' plot(p)
#'
#' # 3 dimensions
#' point(num1, num2, num3)
#'
#' # Standard R vectors are automatically converted to exact_numeric vectors
#' point(runif(5), runif(5))
#'
#' # Convert points to vectors
#' as_vec(p)
#'
#' # Arithmetic
#' # Translate by adding vector
#' p + vec(3, 7)
#'
#' # Create vector by subtracting points
#' p[1:2] - p[3:4]
#'
#' # Sorting etc.
#' sort(p)
#' min(p)
#' cummax(p)
#' rank(p)
#' p[1:2] < p[3:4]
#'
point <- function(..., default_dim = 2) {
inputs <- validate_constructor_input(...)
if (length(inputs) == 0) {
return(new_point_empty(default_dim))
}
vectors <- inputs[vapply(inputs, is_vec, logical(1))]
numbers <- inputs[vapply(inputs, is_exact_numeric, logical(1))]
w_points <- inputs[vapply(inputs, is_weighted_point, logical(1))]
if (length(vectors) == 1) {
new_point_from_vec(vectors[[1]])
} else if (length(w_points) == 1) {
new_point_from_wp(w_points[[1]])
} else if (length(numbers) == 2) {
new_point_from_xy(numbers[[1]], numbers[[2]])
} else if (length(numbers) == 3) {
new_point_from_xyz(numbers[[1]], numbers[[2]], numbers[[3]])
} else {
cli_abort("Can't construct a {.cls euclid_point} vector from the given input")
}
}
#' @rdname point
#' @export
is_point <- function(x) inherits(x, "euclid_point")
# Conversion --------------------------------------------------------------
#' @rdname point
#' @export
as_point <- function(x) {
UseMethod("as_point")
}
#' @export
as_point.default <- function(x) {
cli_abort("Can't convert the input to a {.cls euclid_point} vector")
}
#' @export
as_point.euclid_point <- function(x) x
#' @export
as_vec.euclid_point <- function(x) {
vec(x)
}
# Operators ---------------------------------------------------------------
#' @export
geometry_op_plus.euclid_point <- function(e1, e2) {
if (!is_vec(e2)) {
cli_abort("{.code +} is only defined between points and vectors")
}
if (dim(e1) != dim(e2)) {
cli_abort("{.arg e1} and {.arg e2} must have the same number of dimensions")
}
if (length(e1) == 0 || length(e2) == 0) {
return(new_point_empty(dim(e1)))
}
if (dim(e1) == 2) {
restore_euclid_vector(point_2_add_vector(get_ptr(e1), get_ptr(e2)), e1)
} else {
restore_euclid_vector(point_3_add_vector(get_ptr(e1), get_ptr(e2)), e1)
}
}
#' @export
geometry_op_minus.euclid_point <- function(e1, e2) {
if (!is_vec(e2) && !is_point(e2)) {
cli_abort("{.code -} is only defined between points and vectors or points and points")
}
if (dim(e1) != dim(e2)) {
cli_abort("{.arg e1} and {.arg e2} must have the same number of dimensions")
}
if (length(e1) == 0 || length(e2) == 0) {
if (is_point(e2)) {
return(new_vector_empty(dim(e1)))
} else {
return(new_point_empty(dim(e1)))
}
}
if (dim(e1) == 2) {
if (is_point(e2)) {
new_geometry_vector(point_2_sub_point(get_ptr(e1), get_ptr(e2)))
} else {
restore_euclid_vector(point_2_sub_vector(get_ptr(e1), get_ptr(e2)), e1)
}
} else {
if (is_point(e2)) {
new_geometry_vector(point_3_sub_point(get_ptr(e1), get_ptr(e2)))
} else {
restore_euclid_vector(point_3_sub_vector(get_ptr(e1), get_ptr(e2)), e1)
}
}
}
#' @export
geometry_op_less.euclid_point <- function(e1, e2) {
if (!is_point(e2)) {
cli_abort("{.code <} is only defined for points and points")
}
if (dim(e1) != dim(e2)) {
cli_abort("{.arg e1} and {.arg e2} must have the same number of dimensions")
}
if (length(e1) == 0 || length(e2) == 0) {
return(logical(0))
}
if (dim(e1) == 2) {
point_2_less(get_ptr(e1), get_ptr(e2))
} else {
point_3_less(get_ptr(e1), get_ptr(e2))
}
}
#' @export
geometry_op_greater.euclid_point <- function(e1, e2) {
if (!is_point(e2)) {
cli_abort("{.code >} is only defined for points and points")
}
if (dim(e1) != dim(e2)) {
cli_abort("{.arg e1} and {.arg e2} must have the same number of dimensions")
}
if (length(e1) == 0 || length(e2) == 0) {
return(logical(0))
}
if (dim(e1) == 2) {
point_2_greater(get_ptr(e1), get_ptr(e2))
} else {
point_3_greater(get_ptr(e1), get_ptr(e2))
}
}
# Summaries ---------------------------------------------------------------
#' @export
geometry_summary_min.euclid_point <- function(x, na_rm) {
if (dim(x) == 2) {
point_2_min(get_ptr(x), na_rm)
} else {
point_3_min(get_ptr(x), na_rm)
}
}
#' @export
geometry_summary_max.euclid_point <- function(x, na_rm) {
if (dim(x) == 2) {
point_2_max(get_ptr(x), na_rm)
} else {
point_3_max(get_ptr(x), na_rm)
}
}
# Math --------------------------------------------------------------------
#' @export
geometry_math_cummin.euclid_point <- function(x) {
if (dim(x) == 2) {
point_2_cummin(get_ptr(x))
} else {
point_3_cummin(get_ptr(x))
}
}
#' @export
geometry_math_cummax.euclid_point <- function(x) {
if (dim(x) == 2) {
point_2_cummax(get_ptr(x))
} else {
point_3_cummax(get_ptr(x))
}
}
# Miscellaneous vector support --------------------------------------------
#' @export
sort.euclid_point <- function(x, decreasing = FALSE, na.last = NA, ...) {
if (dim(x) == 2) {
restore_euclid_vector(point_2_sort(get_ptr(x), decreasing, na.last), x)
} else {
restore_euclid_vector(point_3_sort(get_ptr(x), decreasing, na.last), x)
}
}
#' @export
xtfrm.euclid_point <- function(x) {
if (dim(x) == 2) {
point_2_rank(get_ptr(x))
} else {
point_3_rank(get_ptr(x))
}
}
#' @export
range.euclid_point <- function(..., na.rm = FALSE) {
input <- do.call(c, list(...))
c(min(input, na.rm = na.rm), max(input, na.rm = na.rm))
}
#' @export
seq.euclid_point <- function(from, to, length.out = NULL, along.with = NULL, ...) {
if (dim(from) != dim(to)) {
cli_abort("{.arg from} and {.arg to} must have the same number of dimensions")
}
if (!is.null(along.with)) {
length.out = length(along.with)
}
if (is.null(length.out)) {
cli_abort("Either {.arg length.out} or {.arg along.with} must be provided")
}
x <- seq(def(from, "x"), def(to, "x"), length.out = length.out)
y <- seq(def(from, "y"), def(to, "y"), length.out = length.out)
if (dim(from) == 2) {
return(point(x, y))
}
z <- seq(def(from, "z"), def(to, "z"), length.out = length.out)
point(x, y, z)
}
# Internal constructors ---------------------------------------------------
new_point_empty <- function(dim) {
if (dim == 2) {
new_geometry_vector(create_point_2_empty())
} else {
new_geometry_vector(create_point_3_empty())
}
}
new_point_from_xy <- function(x, y) {
new_geometry_vector(create_point_2_x_y(get_ptr(x), get_ptr(y)))
}
new_point_from_xyz <- function(x, y, z) {
new_geometry_vector(create_point_3_x_y_z(get_ptr(x), get_ptr(y), get_ptr(z)))
}
new_point_from_vec <- function(p) {
if (dim(p) == 2) {
new_geometry_vector(create_point_2_vec(get_ptr(p)))
} else {
new_geometry_vector(create_point_3_vec(get_ptr(p)))
}
}
new_point_from_wp <- function(p) {
if (dim(p) == 2) {
new_geometry_vector(create_point_2_wp(get_ptr(p)))
} else {
new_geometry_vector(create_point_3_wp(get_ptr(p)))
}
}