-
Notifications
You must be signed in to change notification settings - Fork 364
/
bspline.py
279 lines (228 loc) · 10.4 KB
/
bspline.py
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
#Copyright 2018 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tensorflow.graphics B-spline interpolation module.
This module supports cardinal B-spline interpolation up to degree 4, with up
to C3 smoothness. It has functions to calculate basis functions, control point
weights, and the final interpolation.
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import enum
import tensorflow as tf
from tensorflow_graphics.util import asserts
from tensorflow_graphics.util import export_api
from tensorflow_graphics.util import shape
# TODO(b/131510643): remove when TF API is
def _mod(x, y):
return x - tf.cast((x // y) * y, dtype=x.dtype)
class Degree(enum.IntEnum):
"""Defines valid degrees for B-spline interpolation."""
CONSTANT = 0
LINEAR = 1
QUADRATIC = 2
CUBIC = 3
QUARTIC = 4
def _constant(position):
"""B-Spline basis function of degree 0 for positions in the range [0, 1]."""
# A piecewise constant spline is discontinuous at the knots.
return tf.expand_dims(tf.clip_by_value(1.0 + position, 1.0, 1.0), axis=-1)
def _linear(position):
"""B-Spline basis functions of degree 1 for positions in the range [0, 1]."""
# Piecewise linear splines are C0 smooth.
return tf.stack((1.0 - position, position), axis=-1)
def _quadratic(position):
"""B-Spline basis functions of degree 2 for positions in the range [0, 1]."""
# We pre-calculate the terms that are used multiple times.
pos_sq = tf.pow(position, 2.0)
# Piecewise quadratic splines are C1 smooth.
return tf.stack((tf.pow(1.0 - position, 2.0) / 2.0, -pos_sq + position + 0.5,
pos_sq / 2.0),
axis=-1)
def _cubic(position):
"""B-Spline basis functions of degree 3 for positions in the range [0, 1]."""
# We pre-calculate the terms that are used multiple times.
neg_pos = 1.0 - position
pos_sq = tf.pow(position, 2.0)
pos_cb = tf.pow(position, 3.0)
# Piecewise cubic splines are C2 smooth.
return tf.stack(
(tf.pow(neg_pos, 3.0) / 6.0, (3.0 * pos_cb - 6.0 * pos_sq + 4.0) / 6.0,
(-3.0 * pos_cb + 3.0 * pos_sq + 3.0 * position + 1.0) / 6.0,
pos_cb / 6.0),
axis=-1)
def _quartic(position):
"""B-Spline basis functions of degree 4 for positions in the range [0, 1]."""
# We pre-calculate the terms that are used multiple times.
neg_pos = 1.0 - position
pos_sq = tf.pow(position, 2.0)
pos_cb = tf.pow(position, 3.0)
pos_qt = tf.pow(position, 4.0)
# Piecewise quartic splines are C3 smooth.
return tf.stack(
(tf.pow(neg_pos, 4.0) / 24.0,
(-4.0 * tf.pow(neg_pos, 4.0) + 4.0 * tf.pow(neg_pos, 3.0) +
6.0 * tf.pow(neg_pos, 2.0) + 4.0 * neg_pos + 1.0) / 24.0,
(pos_qt - 2.0 * pos_cb - pos_sq + 2.0 * position) / 4.0 + 11.0 / 24.0,
(-4.0 * pos_qt + 4.0 * pos_cb + 6.0 * pos_sq + 4.0 * position + 1.0) /
24.0, pos_qt / 24.0),
axis=-1)
def knot_weights(positions,
num_knots,
degree,
cyclical,
sparse_mode=False,
name=None):
"""Function that converts cardinal B-spline positions to knot weights.
Note:
In the following, A1 to An are optional batch dimensions.
Args:
positions: A tensor with shape `[A1, .. An]`. Positions must be between
`[0, C - D)` for non-cyclical and `[0, C)` for cyclical splines, where `C`
is the number of knots and `D` is the spline degree.
num_knots: A strictly positive `int` describing the number of knots in the
spline.
degree: An `int` describing the degree of the spline, which must be smaller
than `num_knots`.
cyclical: A `bool` describing whether the spline is cyclical.
sparse_mode: A `bool` describing whether to return a result only for the
knots with nonzero weights. If set to True, the function returns the
weights of only the `degree` + 1 knots that are non-zero, as well as the
indices of the knots.
name: A name for this op. Defaults to "bsplines_knot_weights".
Returns:
A tensor with dense weights for each control point, with the shape
`[A1, ... An, C]` if `sparse_mode` is False.
Otherwise, returns a tensor of shape `[A1, ... An, D + 1]` that contains the
non-zero weights, and a tensor with the indices of the knots, with the type
tf.int32.
Raises:
ValueError: If degree is greater than 4 or num_knots - 1, or less than 0.
InvalidArgumentError: If positions are not in the right range.
"""
with tf.compat.v1.name_scope(name, "bsplines_knot_weights", [positions]):
positions = tf.convert_to_tensor(value=positions)
if degree > 4 or degree < 0:
raise ValueError("Degree should be between 0 and 4.")
if degree > num_knots - 1:
raise ValueError("Degree cannot be >= number of knots.")
if cyclical:
positions = asserts.assert_all_in_range(positions, 0.0, float(num_knots))
else:
positions = asserts.assert_all_in_range(positions, 0.0,
float(num_knots - degree))
all_basis_functions = {
# Maps valid degrees to functions.
Degree.CONSTANT: _constant,
Degree.LINEAR: _linear,
Degree.QUADRATIC: _quadratic,
Degree.CUBIC: _cubic,
Degree.QUARTIC: _quartic
}
basis_functions = all_basis_functions[degree]
if not cyclical and num_knots - degree == 1:
# In this case all weights are non-zero and we can just return them.
if not sparse_mode:
return basis_functions(positions)
else:
shift = tf.zeros_like(positions, dtype=tf.int32)
return basis_functions(positions), shift
# shape_batch = positions.shape.as_list()
shape_batch = tf.shape(input=positions)
positions = tf.reshape(positions, shape=(-1,))
# Calculate the nonzero weights from the decimal parts of positions.
shift = tf.floor(positions)
sparse_weights = basis_functions(positions - shift)
shift = tf.cast(shift, tf.int32)
if sparse_mode:
# Returns just the weights and the shift amounts, so that tf.gather_nd on
# the knots can be used to sparsely activate knots if needed.
shape_weights = tf.concat(
(shape_batch, tf.constant((degree + 1,), dtype=tf.int32)), axis=0)
sparse_weights = tf.reshape(sparse_weights, shape=shape_weights)
shift = tf.reshape(shift, shape=shape_batch)
return sparse_weights, shift
num_positions = tf.size(input=positions)
ind_row, ind_col = tf.meshgrid(
tf.range(num_positions, dtype=tf.int32),
tf.range(degree + 1, dtype=tf.int32),
indexing="ij")
tiled_shifts = tf.reshape(
tf.tile(tf.expand_dims(shift, axis=-1), multiples=(1, degree + 1)),
shape=(-1,))
ind_col = tf.reshape(ind_col, shape=(-1,)) + tiled_shifts
if cyclical:
ind_col = _mod(ind_col, num_knots)
indices = tf.stack((tf.reshape(ind_row, shape=(-1,)), ind_col), axis=-1)
shape_indices = tf.concat((tf.reshape(
num_positions, shape=(1,)), tf.constant(
(degree + 1, 2), dtype=tf.int32)),
axis=0)
indices = tf.reshape(indices, shape=shape_indices)
shape_scatter = tf.concat((tf.reshape(
num_positions, shape=(1,)), tf.constant((num_knots,), dtype=tf.int32)),
axis=0)
weights = tf.scatter_nd(indices, sparse_weights, shape_scatter)
shape_weights = tf.concat(
(shape_batch, tf.constant((num_knots,), dtype=tf.int32)), axis=0)
return tf.reshape(weights, shape=shape_weights)
def interpolate_with_weights(knots, weights, name=None):
"""Interpolates knots using knot weights.
Note:
In the following, A1 to An, and B1 to Bk are optional batch dimensions.
Args:
knots: A tensor with shape `[B1, ..., Bk, C]` containing knot values, where
`C` is the number of knots.
weights: A tensor with shape `[A1, ..., An, C]` containing dense weights for
the knots, where `C` is the number of knots.
name: A name for this op. Defaults to "bsplines_interpolate_with_weights".
Returns:
A tensor with shape `[A1, ..., An, B1, ..., Bk]`, which is the result of
spline interpolation.
Raises:
ValueError: If the last dimension of knots and weights is not equal.
"""
with tf.compat.v1.name_scope(name, "bsplines_interpolate_with_weights",
[knots, weights]):
knots = tf.convert_to_tensor(value=knots)
weights = tf.convert_to_tensor(value=weights)
shape.compare_dimensions(
tensors=(knots, weights), axes=-1, tensor_names=("knots", "weights"))
return tf.tensordot(weights, knots, (-1, -1))
def interpolate(knots, positions, degree, cyclical, name=None):
"""Applies B-spline interpolation to input control points (knots).
Note:
In the following, A1 to An, and B1 to Bk are optional batch dimensions.
Args:
knots: A tensor with shape `[B1, ..., Bk, C]` containing knot values, where
`C` is the number of knots.
positions: Tensor with shape `[A1, .. An]`. Positions must be between `[0, C
- D)` for non-cyclical and `[0, C)` for cyclical splines, where `C` is the
number of knots and `D` is the spline degree.
degree: An `int` between 0 and 4, or an enumerated constant from the Degree
class, which is the degree of the splines.
cyclical: A `bool`, whether the splines are cyclical.
name: A name for this op. Defaults to "bspline_interpolate".
Returns:
A tensor of shape `[A1, ... An, B1, ..., Bk]`, which is the result of spline
interpolation.
"""
with tf.compat.v1.name_scope(name, "bspline_interpolate", [knots, positions]):
knots = tf.convert_to_tensor(value=knots)
positions = tf.convert_to_tensor(value=positions)
num_knots = knots.get_shape().as_list()[-1]
weights = knot_weights(positions, num_knots, degree, cyclical, False, name)
return interpolate_with_weights(knots, weights)
# API contains all public functions and classes.
__all__ = export_api.get_functions_and_classes()