repo
stringclasses 679
values | path
stringlengths 6
122
| func_name
stringlengths 2
76
| original_string
stringlengths 87
70.9k
| language
stringclasses 1
value | code
stringlengths 87
70.9k
| code_tokens
sequencelengths 20
6.91k
| docstring
stringlengths 1
21.7k
| docstring_tokens
sequencelengths 1
1.6k
| sha
stringclasses 679
values | url
stringlengths 92
213
| partition
stringclasses 1
value |
---|---|---|---|---|---|---|---|---|---|---|---|
aleju/imgaug | imgaug/augmentables/batches.py | UnnormalizedBatch.fill_from_augmented_normalized_batch | def fill_from_augmented_normalized_batch(self, batch_aug_norm):
"""
Fill this batch with (normalized) augmentation results.
This method receives a (normalized) Batch instance, takes all
``*_aug`` attributes out if it and assigns them to this
batch *in unnormalized form*. Hence, the datatypes of all ``*_aug``
attributes will match the datatypes of the ``*_unaug`` attributes.
Parameters
----------
batch_aug_norm: imgaug.augmentables.batches.Batch
Batch after normalization and augmentation.
Returns
-------
imgaug.augmentables.batches.UnnormalizedBatch
New UnnormalizedBatch instance. All ``*_unaug`` attributes are
taken from the old UnnormalizedBatch (without deepcopying them)
and all ``*_aug`` attributes are taken from `batch_normalized`
converted to unnormalized form.
"""
# we take here the .data from the normalized batch instead of from
# self for the rare case where one has decided to somehow change it
# during augmentation
batch = UnnormalizedBatch(
images=self.images_unaug,
heatmaps=self.heatmaps_unaug,
segmentation_maps=self.segmentation_maps_unaug,
keypoints=self.keypoints_unaug,
bounding_boxes=self.bounding_boxes_unaug,
polygons=self.polygons_unaug,
line_strings=self.line_strings_unaug,
data=batch_aug_norm.data
)
batch.images_aug = nlib.invert_normalize_images(
batch_aug_norm.images_aug, self.images_unaug)
batch.heatmaps_aug = nlib.invert_normalize_heatmaps(
batch_aug_norm.heatmaps_aug, self.heatmaps_unaug)
batch.segmentation_maps_aug = nlib.invert_normalize_segmentation_maps(
batch_aug_norm.segmentation_maps_aug, self.segmentation_maps_unaug)
batch.keypoints_aug = nlib.invert_normalize_keypoints(
batch_aug_norm.keypoints_aug, self.keypoints_unaug)
batch.bounding_boxes_aug = nlib.invert_normalize_bounding_boxes(
batch_aug_norm.bounding_boxes_aug, self.bounding_boxes_unaug)
batch.polygons_aug = nlib.invert_normalize_polygons(
batch_aug_norm.polygons_aug, self.polygons_unaug)
batch.line_strings_aug = nlib.invert_normalize_line_strings(
batch_aug_norm.line_strings_aug, self.line_strings_unaug)
return batch | python | def fill_from_augmented_normalized_batch(self, batch_aug_norm):
"""
Fill this batch with (normalized) augmentation results.
This method receives a (normalized) Batch instance, takes all
``*_aug`` attributes out if it and assigns them to this
batch *in unnormalized form*. Hence, the datatypes of all ``*_aug``
attributes will match the datatypes of the ``*_unaug`` attributes.
Parameters
----------
batch_aug_norm: imgaug.augmentables.batches.Batch
Batch after normalization and augmentation.
Returns
-------
imgaug.augmentables.batches.UnnormalizedBatch
New UnnormalizedBatch instance. All ``*_unaug`` attributes are
taken from the old UnnormalizedBatch (without deepcopying them)
and all ``*_aug`` attributes are taken from `batch_normalized`
converted to unnormalized form.
"""
# we take here the .data from the normalized batch instead of from
# self for the rare case where one has decided to somehow change it
# during augmentation
batch = UnnormalizedBatch(
images=self.images_unaug,
heatmaps=self.heatmaps_unaug,
segmentation_maps=self.segmentation_maps_unaug,
keypoints=self.keypoints_unaug,
bounding_boxes=self.bounding_boxes_unaug,
polygons=self.polygons_unaug,
line_strings=self.line_strings_unaug,
data=batch_aug_norm.data
)
batch.images_aug = nlib.invert_normalize_images(
batch_aug_norm.images_aug, self.images_unaug)
batch.heatmaps_aug = nlib.invert_normalize_heatmaps(
batch_aug_norm.heatmaps_aug, self.heatmaps_unaug)
batch.segmentation_maps_aug = nlib.invert_normalize_segmentation_maps(
batch_aug_norm.segmentation_maps_aug, self.segmentation_maps_unaug)
batch.keypoints_aug = nlib.invert_normalize_keypoints(
batch_aug_norm.keypoints_aug, self.keypoints_unaug)
batch.bounding_boxes_aug = nlib.invert_normalize_bounding_boxes(
batch_aug_norm.bounding_boxes_aug, self.bounding_boxes_unaug)
batch.polygons_aug = nlib.invert_normalize_polygons(
batch_aug_norm.polygons_aug, self.polygons_unaug)
batch.line_strings_aug = nlib.invert_normalize_line_strings(
batch_aug_norm.line_strings_aug, self.line_strings_unaug)
return batch | [
"def",
"fill_from_augmented_normalized_batch",
"(",
"self",
",",
"batch_aug_norm",
")",
":",
"# we take here the .data from the normalized batch instead of from",
"# self for the rare case where one has decided to somehow change it",
"# during augmentation",
"batch",
"=",
"UnnormalizedBatch",
"(",
"images",
"=",
"self",
".",
"images_unaug",
",",
"heatmaps",
"=",
"self",
".",
"heatmaps_unaug",
",",
"segmentation_maps",
"=",
"self",
".",
"segmentation_maps_unaug",
",",
"keypoints",
"=",
"self",
".",
"keypoints_unaug",
",",
"bounding_boxes",
"=",
"self",
".",
"bounding_boxes_unaug",
",",
"polygons",
"=",
"self",
".",
"polygons_unaug",
",",
"line_strings",
"=",
"self",
".",
"line_strings_unaug",
",",
"data",
"=",
"batch_aug_norm",
".",
"data",
")",
"batch",
".",
"images_aug",
"=",
"nlib",
".",
"invert_normalize_images",
"(",
"batch_aug_norm",
".",
"images_aug",
",",
"self",
".",
"images_unaug",
")",
"batch",
".",
"heatmaps_aug",
"=",
"nlib",
".",
"invert_normalize_heatmaps",
"(",
"batch_aug_norm",
".",
"heatmaps_aug",
",",
"self",
".",
"heatmaps_unaug",
")",
"batch",
".",
"segmentation_maps_aug",
"=",
"nlib",
".",
"invert_normalize_segmentation_maps",
"(",
"batch_aug_norm",
".",
"segmentation_maps_aug",
",",
"self",
".",
"segmentation_maps_unaug",
")",
"batch",
".",
"keypoints_aug",
"=",
"nlib",
".",
"invert_normalize_keypoints",
"(",
"batch_aug_norm",
".",
"keypoints_aug",
",",
"self",
".",
"keypoints_unaug",
")",
"batch",
".",
"bounding_boxes_aug",
"=",
"nlib",
".",
"invert_normalize_bounding_boxes",
"(",
"batch_aug_norm",
".",
"bounding_boxes_aug",
",",
"self",
".",
"bounding_boxes_unaug",
")",
"batch",
".",
"polygons_aug",
"=",
"nlib",
".",
"invert_normalize_polygons",
"(",
"batch_aug_norm",
".",
"polygons_aug",
",",
"self",
".",
"polygons_unaug",
")",
"batch",
".",
"line_strings_aug",
"=",
"nlib",
".",
"invert_normalize_line_strings",
"(",
"batch_aug_norm",
".",
"line_strings_aug",
",",
"self",
".",
"line_strings_unaug",
")",
"return",
"batch"
] | Fill this batch with (normalized) augmentation results.
This method receives a (normalized) Batch instance, takes all
``*_aug`` attributes out if it and assigns them to this
batch *in unnormalized form*. Hence, the datatypes of all ``*_aug``
attributes will match the datatypes of the ``*_unaug`` attributes.
Parameters
----------
batch_aug_norm: imgaug.augmentables.batches.Batch
Batch after normalization and augmentation.
Returns
-------
imgaug.augmentables.batches.UnnormalizedBatch
New UnnormalizedBatch instance. All ``*_unaug`` attributes are
taken from the old UnnormalizedBatch (without deepcopying them)
and all ``*_aug`` attributes are taken from `batch_normalized`
converted to unnormalized form. | [
"Fill",
"this",
"batch",
"with",
"(",
"normalized",
")",
"augmentation",
"results",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/batches.py#L225-L277 | valid |
aleju/imgaug | imgaug/parameters.py | Positive | def Positive(other_param, mode="invert", reroll_count_max=2):
"""
Converts another parameter's results to positive values.
Parameters
----------
other_param : imgaug.parameters.StochasticParameter
Other parameter which's sampled values are to be
modified.
mode : {'invert', 'reroll'}, optional
How to change the signs. Valid values are ``invert`` and ``reroll``.
``invert`` means that wrong signs are simply flipped.
``reroll`` means that all samples with wrong signs are sampled again,
optionally many times, until they randomly end up having the correct
sign.
reroll_count_max : int, optional
If `mode` is set to ``reroll``, this determines how often values may
be rerolled before giving up and simply flipping the sign (as in
``mode="invert"``). This shouldn't be set too high, as rerolling is
expensive.
Examples
--------
>>> param = Positive(Normal(0, 1), mode="reroll")
Generates a normal distribution that has only positive values.
"""
return ForceSign(
other_param=other_param,
positive=True,
mode=mode,
reroll_count_max=reroll_count_max
) | python | def Positive(other_param, mode="invert", reroll_count_max=2):
"""
Converts another parameter's results to positive values.
Parameters
----------
other_param : imgaug.parameters.StochasticParameter
Other parameter which's sampled values are to be
modified.
mode : {'invert', 'reroll'}, optional
How to change the signs. Valid values are ``invert`` and ``reroll``.
``invert`` means that wrong signs are simply flipped.
``reroll`` means that all samples with wrong signs are sampled again,
optionally many times, until they randomly end up having the correct
sign.
reroll_count_max : int, optional
If `mode` is set to ``reroll``, this determines how often values may
be rerolled before giving up and simply flipping the sign (as in
``mode="invert"``). This shouldn't be set too high, as rerolling is
expensive.
Examples
--------
>>> param = Positive(Normal(0, 1), mode="reroll")
Generates a normal distribution that has only positive values.
"""
return ForceSign(
other_param=other_param,
positive=True,
mode=mode,
reroll_count_max=reroll_count_max
) | [
"def",
"Positive",
"(",
"other_param",
",",
"mode",
"=",
"\"invert\"",
",",
"reroll_count_max",
"=",
"2",
")",
":",
"return",
"ForceSign",
"(",
"other_param",
"=",
"other_param",
",",
"positive",
"=",
"True",
",",
"mode",
"=",
"mode",
",",
"reroll_count_max",
"=",
"reroll_count_max",
")"
] | Converts another parameter's results to positive values.
Parameters
----------
other_param : imgaug.parameters.StochasticParameter
Other parameter which's sampled values are to be
modified.
mode : {'invert', 'reroll'}, optional
How to change the signs. Valid values are ``invert`` and ``reroll``.
``invert`` means that wrong signs are simply flipped.
``reroll`` means that all samples with wrong signs are sampled again,
optionally many times, until they randomly end up having the correct
sign.
reroll_count_max : int, optional
If `mode` is set to ``reroll``, this determines how often values may
be rerolled before giving up and simply flipping the sign (as in
``mode="invert"``). This shouldn't be set too high, as rerolling is
expensive.
Examples
--------
>>> param = Positive(Normal(0, 1), mode="reroll")
Generates a normal distribution that has only positive values. | [
"Converts",
"another",
"parameter",
"s",
"results",
"to",
"positive",
"values",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/parameters.py#L1919-L1954 | valid |
aleju/imgaug | imgaug/parameters.py | Negative | def Negative(other_param, mode="invert", reroll_count_max=2):
"""
Converts another parameter's results to negative values.
Parameters
----------
other_param : imgaug.parameters.StochasticParameter
Other parameter which's sampled values are to be
modified.
mode : {'invert', 'reroll'}, optional
How to change the signs. Valid values are ``invert`` and ``reroll``.
``invert`` means that wrong signs are simply flipped.
``reroll`` means that all samples with wrong signs are sampled again,
optionally many times, until they randomly end up having the correct
sign.
reroll_count_max : int, optional
If `mode` is set to ``reroll``, this determines how often values may
be rerolled before giving up and simply flipping the sign (as in
``mode="invert"``). This shouldn't be set too high, as rerolling is
expensive.
Examples
--------
>>> param = Negative(Normal(0, 1), mode="reroll")
Generates a normal distribution that has only negative values.
"""
return ForceSign(
other_param=other_param,
positive=False,
mode=mode,
reroll_count_max=reroll_count_max
) | python | def Negative(other_param, mode="invert", reroll_count_max=2):
"""
Converts another parameter's results to negative values.
Parameters
----------
other_param : imgaug.parameters.StochasticParameter
Other parameter which's sampled values are to be
modified.
mode : {'invert', 'reroll'}, optional
How to change the signs. Valid values are ``invert`` and ``reroll``.
``invert`` means that wrong signs are simply flipped.
``reroll`` means that all samples with wrong signs are sampled again,
optionally many times, until they randomly end up having the correct
sign.
reroll_count_max : int, optional
If `mode` is set to ``reroll``, this determines how often values may
be rerolled before giving up and simply flipping the sign (as in
``mode="invert"``). This shouldn't be set too high, as rerolling is
expensive.
Examples
--------
>>> param = Negative(Normal(0, 1), mode="reroll")
Generates a normal distribution that has only negative values.
"""
return ForceSign(
other_param=other_param,
positive=False,
mode=mode,
reroll_count_max=reroll_count_max
) | [
"def",
"Negative",
"(",
"other_param",
",",
"mode",
"=",
"\"invert\"",
",",
"reroll_count_max",
"=",
"2",
")",
":",
"return",
"ForceSign",
"(",
"other_param",
"=",
"other_param",
",",
"positive",
"=",
"False",
",",
"mode",
"=",
"mode",
",",
"reroll_count_max",
"=",
"reroll_count_max",
")"
] | Converts another parameter's results to negative values.
Parameters
----------
other_param : imgaug.parameters.StochasticParameter
Other parameter which's sampled values are to be
modified.
mode : {'invert', 'reroll'}, optional
How to change the signs. Valid values are ``invert`` and ``reroll``.
``invert`` means that wrong signs are simply flipped.
``reroll`` means that all samples with wrong signs are sampled again,
optionally many times, until they randomly end up having the correct
sign.
reroll_count_max : int, optional
If `mode` is set to ``reroll``, this determines how often values may
be rerolled before giving up and simply flipping the sign (as in
``mode="invert"``). This shouldn't be set too high, as rerolling is
expensive.
Examples
--------
>>> param = Negative(Normal(0, 1), mode="reroll")
Generates a normal distribution that has only negative values. | [
"Converts",
"another",
"parameter",
"s",
"results",
"to",
"negative",
"values",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/parameters.py#L1957-L1992 | valid |
aleju/imgaug | imgaug/parameters.py | Sigmoid.create_for_noise | def create_for_noise(other_param, threshold=(-10, 10), activated=True):
"""
Creates a Sigmoid that is adjusted to be used with noise parameters,
i.e. with parameters which's output values are in the range [0.0, 1.0].
Parameters
----------
other_param : imgaug.parameters.StochasticParameter
See :func:`imgaug.parameters.Sigmoid.__init__`.
threshold : number or tuple of number or iterable of number or imgaug.parameters.StochasticParameter,\
optional
See :func:`imgaug.parameters.Sigmoid.__init__`.
activated : bool or number, optional
See :func:`imgaug.parameters.Sigmoid.__init__`.
Returns
-------
Sigmoid
A sigmoid adjusted to be used with noise.
"""
return Sigmoid(other_param, threshold, activated, mul=20, add=-10) | python | def create_for_noise(other_param, threshold=(-10, 10), activated=True):
"""
Creates a Sigmoid that is adjusted to be used with noise parameters,
i.e. with parameters which's output values are in the range [0.0, 1.0].
Parameters
----------
other_param : imgaug.parameters.StochasticParameter
See :func:`imgaug.parameters.Sigmoid.__init__`.
threshold : number or tuple of number or iterable of number or imgaug.parameters.StochasticParameter,\
optional
See :func:`imgaug.parameters.Sigmoid.__init__`.
activated : bool or number, optional
See :func:`imgaug.parameters.Sigmoid.__init__`.
Returns
-------
Sigmoid
A sigmoid adjusted to be used with noise.
"""
return Sigmoid(other_param, threshold, activated, mul=20, add=-10) | [
"def",
"create_for_noise",
"(",
"other_param",
",",
"threshold",
"=",
"(",
"-",
"10",
",",
"10",
")",
",",
"activated",
"=",
"True",
")",
":",
"return",
"Sigmoid",
"(",
"other_param",
",",
"threshold",
",",
"activated",
",",
"mul",
"=",
"20",
",",
"add",
"=",
"-",
"10",
")"
] | Creates a Sigmoid that is adjusted to be used with noise parameters,
i.e. with parameters which's output values are in the range [0.0, 1.0].
Parameters
----------
other_param : imgaug.parameters.StochasticParameter
See :func:`imgaug.parameters.Sigmoid.__init__`.
threshold : number or tuple of number or iterable of number or imgaug.parameters.StochasticParameter,\
optional
See :func:`imgaug.parameters.Sigmoid.__init__`.
activated : bool or number, optional
See :func:`imgaug.parameters.Sigmoid.__init__`.
Returns
-------
Sigmoid
A sigmoid adjusted to be used with noise. | [
"Creates",
"a",
"Sigmoid",
"that",
"is",
"adjusted",
"to",
"be",
"used",
"with",
"noise",
"parameters",
"i",
".",
"e",
".",
"with",
"parameters",
"which",
"s",
"output",
"values",
"are",
"in",
"the",
"range",
"[",
"0",
".",
"0",
"1",
".",
"0",
"]",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/parameters.py#L2175-L2198 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.area | def area(self):
"""
Estimate the area of the polygon.
Returns
-------
number
Area of the polygon.
"""
if len(self.exterior) < 3:
raise Exception("Cannot compute the polygon's area because it contains less than three points.")
poly = self.to_shapely_polygon()
return poly.area | python | def area(self):
"""
Estimate the area of the polygon.
Returns
-------
number
Area of the polygon.
"""
if len(self.exterior) < 3:
raise Exception("Cannot compute the polygon's area because it contains less than three points.")
poly = self.to_shapely_polygon()
return poly.area | [
"def",
"area",
"(",
"self",
")",
":",
"if",
"len",
"(",
"self",
".",
"exterior",
")",
"<",
"3",
":",
"raise",
"Exception",
"(",
"\"Cannot compute the polygon's area because it contains less than three points.\"",
")",
"poly",
"=",
"self",
".",
"to_shapely_polygon",
"(",
")",
"return",
"poly",
".",
"area"
] | Estimate the area of the polygon.
Returns
-------
number
Area of the polygon. | [
"Estimate",
"the",
"area",
"of",
"the",
"polygon",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L148-L161 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.project | def project(self, from_shape, to_shape):
"""
Project the polygon onto an image with different shape.
The relative coordinates of all points remain the same.
E.g. a point at (x=20, y=20) on an image (width=100, height=200) will be
projected on a new image (width=200, height=100) to (x=40, y=10).
This is intended for cases where the original image is resized.
It cannot be used for more complex changes (e.g. padding, cropping).
Parameters
----------
from_shape : tuple of int
Shape of the original image. (Before resize.)
to_shape : tuple of int
Shape of the new image. (After resize.)
Returns
-------
imgaug.Polygon
Polygon object with new coordinates.
"""
if from_shape[0:2] == to_shape[0:2]:
return self.copy()
ls_proj = self.to_line_string(closed=False).project(
from_shape, to_shape)
return self.copy(exterior=ls_proj.coords) | python | def project(self, from_shape, to_shape):
"""
Project the polygon onto an image with different shape.
The relative coordinates of all points remain the same.
E.g. a point at (x=20, y=20) on an image (width=100, height=200) will be
projected on a new image (width=200, height=100) to (x=40, y=10).
This is intended for cases where the original image is resized.
It cannot be used for more complex changes (e.g. padding, cropping).
Parameters
----------
from_shape : tuple of int
Shape of the original image. (Before resize.)
to_shape : tuple of int
Shape of the new image. (After resize.)
Returns
-------
imgaug.Polygon
Polygon object with new coordinates.
"""
if from_shape[0:2] == to_shape[0:2]:
return self.copy()
ls_proj = self.to_line_string(closed=False).project(
from_shape, to_shape)
return self.copy(exterior=ls_proj.coords) | [
"def",
"project",
"(",
"self",
",",
"from_shape",
",",
"to_shape",
")",
":",
"if",
"from_shape",
"[",
"0",
":",
"2",
"]",
"==",
"to_shape",
"[",
"0",
":",
"2",
"]",
":",
"return",
"self",
".",
"copy",
"(",
")",
"ls_proj",
"=",
"self",
".",
"to_line_string",
"(",
"closed",
"=",
"False",
")",
".",
"project",
"(",
"from_shape",
",",
"to_shape",
")",
"return",
"self",
".",
"copy",
"(",
"exterior",
"=",
"ls_proj",
".",
"coords",
")"
] | Project the polygon onto an image with different shape.
The relative coordinates of all points remain the same.
E.g. a point at (x=20, y=20) on an image (width=100, height=200) will be
projected on a new image (width=200, height=100) to (x=40, y=10).
This is intended for cases where the original image is resized.
It cannot be used for more complex changes (e.g. padding, cropping).
Parameters
----------
from_shape : tuple of int
Shape of the original image. (Before resize.)
to_shape : tuple of int
Shape of the new image. (After resize.)
Returns
-------
imgaug.Polygon
Polygon object with new coordinates. | [
"Project",
"the",
"polygon",
"onto",
"an",
"image",
"with",
"different",
"shape",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L191-L220 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.find_closest_point_index | def find_closest_point_index(self, x, y, return_distance=False):
"""
Find the index of the point within the exterior that is closest to the given coordinates.
"Closeness" is here defined based on euclidean distance.
This method will raise an AssertionError if the exterior contains no points.
Parameters
----------
x : number
X-coordinate around which to search for close points.
y : number
Y-coordinate around which to search for close points.
return_distance : bool, optional
Whether to also return the distance of the closest point.
Returns
-------
int
Index of the closest point.
number
Euclidean distance to the closest point.
This value is only returned if `return_distance` was set to True.
"""
ia.do_assert(len(self.exterior) > 0)
distances = []
for x2, y2 in self.exterior:
d = (x2 - x) ** 2 + (y2 - y) ** 2
distances.append(d)
distances = np.sqrt(distances)
closest_idx = np.argmin(distances)
if return_distance:
return closest_idx, distances[closest_idx]
return closest_idx | python | def find_closest_point_index(self, x, y, return_distance=False):
"""
Find the index of the point within the exterior that is closest to the given coordinates.
"Closeness" is here defined based on euclidean distance.
This method will raise an AssertionError if the exterior contains no points.
Parameters
----------
x : number
X-coordinate around which to search for close points.
y : number
Y-coordinate around which to search for close points.
return_distance : bool, optional
Whether to also return the distance of the closest point.
Returns
-------
int
Index of the closest point.
number
Euclidean distance to the closest point.
This value is only returned if `return_distance` was set to True.
"""
ia.do_assert(len(self.exterior) > 0)
distances = []
for x2, y2 in self.exterior:
d = (x2 - x) ** 2 + (y2 - y) ** 2
distances.append(d)
distances = np.sqrt(distances)
closest_idx = np.argmin(distances)
if return_distance:
return closest_idx, distances[closest_idx]
return closest_idx | [
"def",
"find_closest_point_index",
"(",
"self",
",",
"x",
",",
"y",
",",
"return_distance",
"=",
"False",
")",
":",
"ia",
".",
"do_assert",
"(",
"len",
"(",
"self",
".",
"exterior",
")",
">",
"0",
")",
"distances",
"=",
"[",
"]",
"for",
"x2",
",",
"y2",
"in",
"self",
".",
"exterior",
":",
"d",
"=",
"(",
"x2",
"-",
"x",
")",
"**",
"2",
"+",
"(",
"y2",
"-",
"y",
")",
"**",
"2",
"distances",
".",
"append",
"(",
"d",
")",
"distances",
"=",
"np",
".",
"sqrt",
"(",
"distances",
")",
"closest_idx",
"=",
"np",
".",
"argmin",
"(",
"distances",
")",
"if",
"return_distance",
":",
"return",
"closest_idx",
",",
"distances",
"[",
"closest_idx",
"]",
"return",
"closest_idx"
] | Find the index of the point within the exterior that is closest to the given coordinates.
"Closeness" is here defined based on euclidean distance.
This method will raise an AssertionError if the exterior contains no points.
Parameters
----------
x : number
X-coordinate around which to search for close points.
y : number
Y-coordinate around which to search for close points.
return_distance : bool, optional
Whether to also return the distance of the closest point.
Returns
-------
int
Index of the closest point.
number
Euclidean distance to the closest point.
This value is only returned if `return_distance` was set to True. | [
"Find",
"the",
"index",
"of",
"the",
"point",
"within",
"the",
"exterior",
"that",
"is",
"closest",
"to",
"the",
"given",
"coordinates",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L222-L259 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.is_fully_within_image | def is_fully_within_image(self, image):
"""
Estimate whether the polygon is fully inside the image area.
Parameters
----------
image : (H,W,...) ndarray or tuple of int
Image dimensions to use.
If an ndarray, its shape will be used.
If a tuple, it is assumed to represent the image shape and must contain at least two integers.
Returns
-------
bool
True if the polygon is fully inside the image area.
False otherwise.
"""
return not self.is_out_of_image(image, fully=True, partly=True) | python | def is_fully_within_image(self, image):
"""
Estimate whether the polygon is fully inside the image area.
Parameters
----------
image : (H,W,...) ndarray or tuple of int
Image dimensions to use.
If an ndarray, its shape will be used.
If a tuple, it is assumed to represent the image shape and must contain at least two integers.
Returns
-------
bool
True if the polygon is fully inside the image area.
False otherwise.
"""
return not self.is_out_of_image(image, fully=True, partly=True) | [
"def",
"is_fully_within_image",
"(",
"self",
",",
"image",
")",
":",
"return",
"not",
"self",
".",
"is_out_of_image",
"(",
"image",
",",
"fully",
"=",
"True",
",",
"partly",
"=",
"True",
")"
] | Estimate whether the polygon is fully inside the image area.
Parameters
----------
image : (H,W,...) ndarray or tuple of int
Image dimensions to use.
If an ndarray, its shape will be used.
If a tuple, it is assumed to represent the image shape and must contain at least two integers.
Returns
-------
bool
True if the polygon is fully inside the image area.
False otherwise. | [
"Estimate",
"whether",
"the",
"polygon",
"is",
"fully",
"inside",
"the",
"image",
"area",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L262-L280 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.is_partly_within_image | def is_partly_within_image(self, image):
"""
Estimate whether the polygon is at least partially inside the image area.
Parameters
----------
image : (H,W,...) ndarray or tuple of int
Image dimensions to use.
If an ndarray, its shape will be used.
If a tuple, it is assumed to represent the image shape and must contain at least two integers.
Returns
-------
bool
True if the polygon is at least partially inside the image area.
False otherwise.
"""
return not self.is_out_of_image(image, fully=True, partly=False) | python | def is_partly_within_image(self, image):
"""
Estimate whether the polygon is at least partially inside the image area.
Parameters
----------
image : (H,W,...) ndarray or tuple of int
Image dimensions to use.
If an ndarray, its shape will be used.
If a tuple, it is assumed to represent the image shape and must contain at least two integers.
Returns
-------
bool
True if the polygon is at least partially inside the image area.
False otherwise.
"""
return not self.is_out_of_image(image, fully=True, partly=False) | [
"def",
"is_partly_within_image",
"(",
"self",
",",
"image",
")",
":",
"return",
"not",
"self",
".",
"is_out_of_image",
"(",
"image",
",",
"fully",
"=",
"True",
",",
"partly",
"=",
"False",
")"
] | Estimate whether the polygon is at least partially inside the image area.
Parameters
----------
image : (H,W,...) ndarray or tuple of int
Image dimensions to use.
If an ndarray, its shape will be used.
If a tuple, it is assumed to represent the image shape and must contain at least two integers.
Returns
-------
bool
True if the polygon is at least partially inside the image area.
False otherwise. | [
"Estimate",
"whether",
"the",
"polygon",
"is",
"at",
"least",
"partially",
"inside",
"the",
"image",
"area",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L283-L301 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.is_out_of_image | def is_out_of_image(self, image, fully=True, partly=False):
"""
Estimate whether the polygon is partially or fully outside of the image area.
Parameters
----------
image : (H,W,...) ndarray or tuple of int
Image dimensions to use.
If an ndarray, its shape will be used.
If a tuple, it is assumed to represent the image shape and must contain at least two integers.
fully : bool, optional
Whether to return True if the polygon is fully outside of the image area.
partly : bool, optional
Whether to return True if the polygon is at least partially outside fo the image area.
Returns
-------
bool
True if the polygon is partially/fully outside of the image area, depending
on defined parameters. False otherwise.
"""
# TODO this is inconsistent with line strings, which return a default
# value in these cases
if len(self.exterior) == 0:
raise Exception("Cannot determine whether the polygon is inside the image, because it contains no points.")
ls = self.to_line_string()
return ls.is_out_of_image(image, fully=fully, partly=partly) | python | def is_out_of_image(self, image, fully=True, partly=False):
"""
Estimate whether the polygon is partially or fully outside of the image area.
Parameters
----------
image : (H,W,...) ndarray or tuple of int
Image dimensions to use.
If an ndarray, its shape will be used.
If a tuple, it is assumed to represent the image shape and must contain at least two integers.
fully : bool, optional
Whether to return True if the polygon is fully outside of the image area.
partly : bool, optional
Whether to return True if the polygon is at least partially outside fo the image area.
Returns
-------
bool
True if the polygon is partially/fully outside of the image area, depending
on defined parameters. False otherwise.
"""
# TODO this is inconsistent with line strings, which return a default
# value in these cases
if len(self.exterior) == 0:
raise Exception("Cannot determine whether the polygon is inside the image, because it contains no points.")
ls = self.to_line_string()
return ls.is_out_of_image(image, fully=fully, partly=partly) | [
"def",
"is_out_of_image",
"(",
"self",
",",
"image",
",",
"fully",
"=",
"True",
",",
"partly",
"=",
"False",
")",
":",
"# TODO this is inconsistent with line strings, which return a default",
"# value in these cases",
"if",
"len",
"(",
"self",
".",
"exterior",
")",
"==",
"0",
":",
"raise",
"Exception",
"(",
"\"Cannot determine whether the polygon is inside the image, because it contains no points.\"",
")",
"ls",
"=",
"self",
".",
"to_line_string",
"(",
")",
"return",
"ls",
".",
"is_out_of_image",
"(",
"image",
",",
"fully",
"=",
"fully",
",",
"partly",
"=",
"partly",
")"
] | Estimate whether the polygon is partially or fully outside of the image area.
Parameters
----------
image : (H,W,...) ndarray or tuple of int
Image dimensions to use.
If an ndarray, its shape will be used.
If a tuple, it is assumed to represent the image shape and must contain at least two integers.
fully : bool, optional
Whether to return True if the polygon is fully outside of the image area.
partly : bool, optional
Whether to return True if the polygon is at least partially outside fo the image area.
Returns
-------
bool
True if the polygon is partially/fully outside of the image area, depending
on defined parameters. False otherwise. | [
"Estimate",
"whether",
"the",
"polygon",
"is",
"partially",
"or",
"fully",
"outside",
"of",
"the",
"image",
"area",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L303-L332 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.clip_out_of_image | def clip_out_of_image(self, image):
"""
Cut off all parts of the polygon that are outside of the image.
This operation may lead to new points being created.
As a single polygon may be split into multiple new polygons, the result
is always a list, which may contain more than one output polygon.
This operation will return an empty list if the polygon is completely
outside of the image plane.
Parameters
----------
image : (H,W,...) ndarray or tuple of int
Image dimensions to use for the clipping of the polygon.
If an ndarray, its shape will be used.
If a tuple, it is assumed to represent the image shape and must
contain at least two integers.
Returns
-------
list of imgaug.Polygon
Polygon, clipped to fall within the image dimensions.
Returned as a list, because the clipping can split the polygon into
multiple parts. The list may also be empty, if the polygon was
fully outside of the image plane.
"""
# load shapely lazily, which makes the dependency more optional
import shapely.geometry
# if fully out of image, clip everything away, nothing remaining
if self.is_out_of_image(image, fully=True, partly=False):
return []
h, w = image.shape[0:2] if ia.is_np_array(image) else image[0:2]
poly_shapely = self.to_shapely_polygon()
poly_image = shapely.geometry.Polygon([(0, 0), (w, 0), (w, h), (0, h)])
multipoly_inter_shapely = poly_shapely.intersection(poly_image)
if not isinstance(multipoly_inter_shapely, shapely.geometry.MultiPolygon):
ia.do_assert(isinstance(multipoly_inter_shapely, shapely.geometry.Polygon))
multipoly_inter_shapely = shapely.geometry.MultiPolygon([multipoly_inter_shapely])
polygons = []
for poly_inter_shapely in multipoly_inter_shapely.geoms:
polygons.append(Polygon.from_shapely(poly_inter_shapely, label=self.label))
# shapely changes the order of points, we try here to preserve it as
# much as possible
polygons_reordered = []
for polygon in polygons:
found = False
for x, y in self.exterior:
closest_idx, dist = polygon.find_closest_point_index(x=x, y=y, return_distance=True)
if dist < 1e-6:
polygon_reordered = polygon.change_first_point_by_index(closest_idx)
polygons_reordered.append(polygon_reordered)
found = True
break
ia.do_assert(found) # could only not find closest points if new polys are empty
return polygons_reordered | python | def clip_out_of_image(self, image):
"""
Cut off all parts of the polygon that are outside of the image.
This operation may lead to new points being created.
As a single polygon may be split into multiple new polygons, the result
is always a list, which may contain more than one output polygon.
This operation will return an empty list if the polygon is completely
outside of the image plane.
Parameters
----------
image : (H,W,...) ndarray or tuple of int
Image dimensions to use for the clipping of the polygon.
If an ndarray, its shape will be used.
If a tuple, it is assumed to represent the image shape and must
contain at least two integers.
Returns
-------
list of imgaug.Polygon
Polygon, clipped to fall within the image dimensions.
Returned as a list, because the clipping can split the polygon into
multiple parts. The list may also be empty, if the polygon was
fully outside of the image plane.
"""
# load shapely lazily, which makes the dependency more optional
import shapely.geometry
# if fully out of image, clip everything away, nothing remaining
if self.is_out_of_image(image, fully=True, partly=False):
return []
h, w = image.shape[0:2] if ia.is_np_array(image) else image[0:2]
poly_shapely = self.to_shapely_polygon()
poly_image = shapely.geometry.Polygon([(0, 0), (w, 0), (w, h), (0, h)])
multipoly_inter_shapely = poly_shapely.intersection(poly_image)
if not isinstance(multipoly_inter_shapely, shapely.geometry.MultiPolygon):
ia.do_assert(isinstance(multipoly_inter_shapely, shapely.geometry.Polygon))
multipoly_inter_shapely = shapely.geometry.MultiPolygon([multipoly_inter_shapely])
polygons = []
for poly_inter_shapely in multipoly_inter_shapely.geoms:
polygons.append(Polygon.from_shapely(poly_inter_shapely, label=self.label))
# shapely changes the order of points, we try here to preserve it as
# much as possible
polygons_reordered = []
for polygon in polygons:
found = False
for x, y in self.exterior:
closest_idx, dist = polygon.find_closest_point_index(x=x, y=y, return_distance=True)
if dist < 1e-6:
polygon_reordered = polygon.change_first_point_by_index(closest_idx)
polygons_reordered.append(polygon_reordered)
found = True
break
ia.do_assert(found) # could only not find closest points if new polys are empty
return polygons_reordered | [
"def",
"clip_out_of_image",
"(",
"self",
",",
"image",
")",
":",
"# load shapely lazily, which makes the dependency more optional",
"import",
"shapely",
".",
"geometry",
"# if fully out of image, clip everything away, nothing remaining",
"if",
"self",
".",
"is_out_of_image",
"(",
"image",
",",
"fully",
"=",
"True",
",",
"partly",
"=",
"False",
")",
":",
"return",
"[",
"]",
"h",
",",
"w",
"=",
"image",
".",
"shape",
"[",
"0",
":",
"2",
"]",
"if",
"ia",
".",
"is_np_array",
"(",
"image",
")",
"else",
"image",
"[",
"0",
":",
"2",
"]",
"poly_shapely",
"=",
"self",
".",
"to_shapely_polygon",
"(",
")",
"poly_image",
"=",
"shapely",
".",
"geometry",
".",
"Polygon",
"(",
"[",
"(",
"0",
",",
"0",
")",
",",
"(",
"w",
",",
"0",
")",
",",
"(",
"w",
",",
"h",
")",
",",
"(",
"0",
",",
"h",
")",
"]",
")",
"multipoly_inter_shapely",
"=",
"poly_shapely",
".",
"intersection",
"(",
"poly_image",
")",
"if",
"not",
"isinstance",
"(",
"multipoly_inter_shapely",
",",
"shapely",
".",
"geometry",
".",
"MultiPolygon",
")",
":",
"ia",
".",
"do_assert",
"(",
"isinstance",
"(",
"multipoly_inter_shapely",
",",
"shapely",
".",
"geometry",
".",
"Polygon",
")",
")",
"multipoly_inter_shapely",
"=",
"shapely",
".",
"geometry",
".",
"MultiPolygon",
"(",
"[",
"multipoly_inter_shapely",
"]",
")",
"polygons",
"=",
"[",
"]",
"for",
"poly_inter_shapely",
"in",
"multipoly_inter_shapely",
".",
"geoms",
":",
"polygons",
".",
"append",
"(",
"Polygon",
".",
"from_shapely",
"(",
"poly_inter_shapely",
",",
"label",
"=",
"self",
".",
"label",
")",
")",
"# shapely changes the order of points, we try here to preserve it as",
"# much as possible",
"polygons_reordered",
"=",
"[",
"]",
"for",
"polygon",
"in",
"polygons",
":",
"found",
"=",
"False",
"for",
"x",
",",
"y",
"in",
"self",
".",
"exterior",
":",
"closest_idx",
",",
"dist",
"=",
"polygon",
".",
"find_closest_point_index",
"(",
"x",
"=",
"x",
",",
"y",
"=",
"y",
",",
"return_distance",
"=",
"True",
")",
"if",
"dist",
"<",
"1e-6",
":",
"polygon_reordered",
"=",
"polygon",
".",
"change_first_point_by_index",
"(",
"closest_idx",
")",
"polygons_reordered",
".",
"append",
"(",
"polygon_reordered",
")",
"found",
"=",
"True",
"break",
"ia",
".",
"do_assert",
"(",
"found",
")",
"# could only not find closest points if new polys are empty",
"return",
"polygons_reordered"
] | Cut off all parts of the polygon that are outside of the image.
This operation may lead to new points being created.
As a single polygon may be split into multiple new polygons, the result
is always a list, which may contain more than one output polygon.
This operation will return an empty list if the polygon is completely
outside of the image plane.
Parameters
----------
image : (H,W,...) ndarray or tuple of int
Image dimensions to use for the clipping of the polygon.
If an ndarray, its shape will be used.
If a tuple, it is assumed to represent the image shape and must
contain at least two integers.
Returns
-------
list of imgaug.Polygon
Polygon, clipped to fall within the image dimensions.
Returned as a list, because the clipping can split the polygon into
multiple parts. The list may also be empty, if the polygon was
fully outside of the image plane. | [
"Cut",
"off",
"all",
"parts",
"of",
"the",
"polygon",
"that",
"are",
"outside",
"of",
"the",
"image",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L342-L403 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.shift | def shift(self, top=None, right=None, bottom=None, left=None):
"""
Shift the polygon from one or more image sides, i.e. move it on the x/y-axis.
Parameters
----------
top : None or int, optional
Amount of pixels by which to shift the polygon from the top.
right : None or int, optional
Amount of pixels by which to shift the polygon from the right.
bottom : None or int, optional
Amount of pixels by which to shift the polygon from the bottom.
left : None or int, optional
Amount of pixels by which to shift the polygon from the left.
Returns
-------
imgaug.Polygon
Shifted polygon.
"""
ls_shifted = self.to_line_string(closed=False).shift(
top=top, right=right, bottom=bottom, left=left)
return self.copy(exterior=ls_shifted.coords) | python | def shift(self, top=None, right=None, bottom=None, left=None):
"""
Shift the polygon from one or more image sides, i.e. move it on the x/y-axis.
Parameters
----------
top : None or int, optional
Amount of pixels by which to shift the polygon from the top.
right : None or int, optional
Amount of pixels by which to shift the polygon from the right.
bottom : None or int, optional
Amount of pixels by which to shift the polygon from the bottom.
left : None or int, optional
Amount of pixels by which to shift the polygon from the left.
Returns
-------
imgaug.Polygon
Shifted polygon.
"""
ls_shifted = self.to_line_string(closed=False).shift(
top=top, right=right, bottom=bottom, left=left)
return self.copy(exterior=ls_shifted.coords) | [
"def",
"shift",
"(",
"self",
",",
"top",
"=",
"None",
",",
"right",
"=",
"None",
",",
"bottom",
"=",
"None",
",",
"left",
"=",
"None",
")",
":",
"ls_shifted",
"=",
"self",
".",
"to_line_string",
"(",
"closed",
"=",
"False",
")",
".",
"shift",
"(",
"top",
"=",
"top",
",",
"right",
"=",
"right",
",",
"bottom",
"=",
"bottom",
",",
"left",
"=",
"left",
")",
"return",
"self",
".",
"copy",
"(",
"exterior",
"=",
"ls_shifted",
".",
"coords",
")"
] | Shift the polygon from one or more image sides, i.e. move it on the x/y-axis.
Parameters
----------
top : None or int, optional
Amount of pixels by which to shift the polygon from the top.
right : None or int, optional
Amount of pixels by which to shift the polygon from the right.
bottom : None or int, optional
Amount of pixels by which to shift the polygon from the bottom.
left : None or int, optional
Amount of pixels by which to shift the polygon from the left.
Returns
-------
imgaug.Polygon
Shifted polygon. | [
"Shift",
"the",
"polygon",
"from",
"one",
"or",
"more",
"image",
"sides",
"i",
".",
"e",
".",
"move",
"it",
"on",
"the",
"x",
"/",
"y",
"-",
"axis",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L405-L431 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.draw_on_image | def draw_on_image(self,
image,
color=(0, 255, 0), color_face=None,
color_lines=None, color_points=None,
alpha=1.0, alpha_face=None,
alpha_lines=None, alpha_points=None,
size=1, size_lines=None, size_points=None,
raise_if_out_of_image=False):
"""
Draw the polygon on an image.
Parameters
----------
image : (H,W,C) ndarray
The image onto which to draw the polygon. Usually expected to be
of dtype ``uint8``, though other dtypes are also handled.
color : iterable of int, optional
The color to use for the whole polygon.
Must correspond to the channel layout of the image. Usually RGB.
The values for `color_face`, `color_lines` and `color_points`
will be derived from this color if they are set to ``None``.
This argument has no effect if `color_face`, `color_lines`
and `color_points` are all set anything other than ``None``.
color_face : None or iterable of int, optional
The color to use for the inner polygon area (excluding perimeter).
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 1.0``.
color_lines : None or iterable of int, optional
The color to use for the line (aka perimeter/border) of the polygon.
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 0.5``.
color_points : None or iterable of int, optional
The color to use for the corner points of the polygon.
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 0.5``.
alpha : float, optional
The opacity of the whole polygon, where ``1.0`` denotes a completely
visible polygon and ``0.0`` an invisible one.
The values for `alpha_face`, `alpha_lines` and `alpha_points`
will be derived from this alpha value if they are set to ``None``.
This argument has no effect if `alpha_face`, `alpha_lines`
and `alpha_points` are all set anything other than ``None``.
alpha_face : None or number, optional
The opacity of the polygon's inner area (excluding the perimeter),
where ``1.0`` denotes a completely visible inner area and ``0.0``
an invisible one.
If this is ``None``, it will be derived from ``alpha * 0.5``.
alpha_lines : None or number, optional
The opacity of the polygon's line (aka perimeter/border),
where ``1.0`` denotes a completely visible line and ``0.0`` an
invisible one.
If this is ``None``, it will be derived from ``alpha * 1.0``.
alpha_points : None or number, optional
The opacity of the polygon's corner points, where ``1.0`` denotes
completely visible corners and ``0.0`` invisible ones.
If this is ``None``, it will be derived from ``alpha * 1.0``.
size : int, optional
Size of the polygon.
The sizes of the line and points are derived from this value,
unless they are set.
size_lines : None or int, optional
Thickness of the polygon's line (aka perimeter/border).
If ``None``, this value is derived from `size`.
size_points : int, optional
Size of the points in pixels.
If ``None``, this value is derived from ``3 * size``.
raise_if_out_of_image : bool, optional
Whether to raise an error if the polygon is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
result : (H,W,C) ndarray
Image with polygon drawn on it. Result dtype is the same as the input dtype.
"""
assert color is not None
assert alpha is not None
assert size is not None
color_face = color_face if color_face is not None else np.array(color)
color_lines = color_lines if color_lines is not None else np.array(color) * 0.5
color_points = color_points if color_points is not None else np.array(color) * 0.5
alpha_face = alpha_face if alpha_face is not None else alpha * 0.5
alpha_lines = alpha_lines if alpha_lines is not None else alpha
alpha_points = alpha_points if alpha_points is not None else alpha
size_lines = size_lines if size_lines is not None else size
size_points = size_points if size_points is not None else size * 3
if image.ndim == 2:
assert ia.is_single_number(color_face), (
"Got a 2D image. Expected then 'color_face' to be a single "
"number, but got %s." % (str(color_face),))
color_face = [color_face]
elif image.ndim == 3 and ia.is_single_number(color_face):
color_face = [color_face] * image.shape[-1]
if alpha_face < 0.01:
alpha_face = 0
elif alpha_face > 0.99:
alpha_face = 1
if raise_if_out_of_image and self.is_out_of_image(image):
raise Exception("Cannot draw polygon %s on image with shape %s." % (
str(self), image.shape
))
# TODO np.clip to image plane if is_fully_within_image(), similar to how it is done for bounding boxes
# TODO improve efficiency by only drawing in rectangle that covers poly instead of drawing in the whole image
# TODO for a rectangular polygon, the face coordinates include the top/left boundary but not the right/bottom
# boundary. This may be unintuitive when not drawing the boundary. Maybe somehow remove the boundary
# coordinates from the face coordinates after generating both?
input_dtype = image.dtype
result = image.astype(np.float32)
rr, cc = skimage.draw.polygon(self.yy_int, self.xx_int, shape=image.shape)
if len(rr) > 0:
if alpha_face == 1:
result[rr, cc] = np.float32(color_face)
elif alpha_face == 0:
pass
else:
result[rr, cc] = (
(1 - alpha_face) * result[rr, cc, :]
+ alpha_face * np.float32(color_face)
)
ls_open = self.to_line_string(closed=False)
ls_closed = self.to_line_string(closed=True)
result = ls_closed.draw_lines_on_image(
result, color=color_lines, alpha=alpha_lines,
size=size_lines, raise_if_out_of_image=raise_if_out_of_image)
result = ls_open.draw_points_on_image(
result, color=color_points, alpha=alpha_points,
size=size_points, raise_if_out_of_image=raise_if_out_of_image)
if input_dtype.type == np.uint8:
result = np.clip(np.round(result), 0, 255).astype(input_dtype) # TODO make clipping more flexible
else:
result = result.astype(input_dtype)
return result | python | def draw_on_image(self,
image,
color=(0, 255, 0), color_face=None,
color_lines=None, color_points=None,
alpha=1.0, alpha_face=None,
alpha_lines=None, alpha_points=None,
size=1, size_lines=None, size_points=None,
raise_if_out_of_image=False):
"""
Draw the polygon on an image.
Parameters
----------
image : (H,W,C) ndarray
The image onto which to draw the polygon. Usually expected to be
of dtype ``uint8``, though other dtypes are also handled.
color : iterable of int, optional
The color to use for the whole polygon.
Must correspond to the channel layout of the image. Usually RGB.
The values for `color_face`, `color_lines` and `color_points`
will be derived from this color if they are set to ``None``.
This argument has no effect if `color_face`, `color_lines`
and `color_points` are all set anything other than ``None``.
color_face : None or iterable of int, optional
The color to use for the inner polygon area (excluding perimeter).
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 1.0``.
color_lines : None or iterable of int, optional
The color to use for the line (aka perimeter/border) of the polygon.
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 0.5``.
color_points : None or iterable of int, optional
The color to use for the corner points of the polygon.
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 0.5``.
alpha : float, optional
The opacity of the whole polygon, where ``1.0`` denotes a completely
visible polygon and ``0.0`` an invisible one.
The values for `alpha_face`, `alpha_lines` and `alpha_points`
will be derived from this alpha value if they are set to ``None``.
This argument has no effect if `alpha_face`, `alpha_lines`
and `alpha_points` are all set anything other than ``None``.
alpha_face : None or number, optional
The opacity of the polygon's inner area (excluding the perimeter),
where ``1.0`` denotes a completely visible inner area and ``0.0``
an invisible one.
If this is ``None``, it will be derived from ``alpha * 0.5``.
alpha_lines : None or number, optional
The opacity of the polygon's line (aka perimeter/border),
where ``1.0`` denotes a completely visible line and ``0.0`` an
invisible one.
If this is ``None``, it will be derived from ``alpha * 1.0``.
alpha_points : None or number, optional
The opacity of the polygon's corner points, where ``1.0`` denotes
completely visible corners and ``0.0`` invisible ones.
If this is ``None``, it will be derived from ``alpha * 1.0``.
size : int, optional
Size of the polygon.
The sizes of the line and points are derived from this value,
unless they are set.
size_lines : None or int, optional
Thickness of the polygon's line (aka perimeter/border).
If ``None``, this value is derived from `size`.
size_points : int, optional
Size of the points in pixels.
If ``None``, this value is derived from ``3 * size``.
raise_if_out_of_image : bool, optional
Whether to raise an error if the polygon is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
result : (H,W,C) ndarray
Image with polygon drawn on it. Result dtype is the same as the input dtype.
"""
assert color is not None
assert alpha is not None
assert size is not None
color_face = color_face if color_face is not None else np.array(color)
color_lines = color_lines if color_lines is not None else np.array(color) * 0.5
color_points = color_points if color_points is not None else np.array(color) * 0.5
alpha_face = alpha_face if alpha_face is not None else alpha * 0.5
alpha_lines = alpha_lines if alpha_lines is not None else alpha
alpha_points = alpha_points if alpha_points is not None else alpha
size_lines = size_lines if size_lines is not None else size
size_points = size_points if size_points is not None else size * 3
if image.ndim == 2:
assert ia.is_single_number(color_face), (
"Got a 2D image. Expected then 'color_face' to be a single "
"number, but got %s." % (str(color_face),))
color_face = [color_face]
elif image.ndim == 3 and ia.is_single_number(color_face):
color_face = [color_face] * image.shape[-1]
if alpha_face < 0.01:
alpha_face = 0
elif alpha_face > 0.99:
alpha_face = 1
if raise_if_out_of_image and self.is_out_of_image(image):
raise Exception("Cannot draw polygon %s on image with shape %s." % (
str(self), image.shape
))
# TODO np.clip to image plane if is_fully_within_image(), similar to how it is done for bounding boxes
# TODO improve efficiency by only drawing in rectangle that covers poly instead of drawing in the whole image
# TODO for a rectangular polygon, the face coordinates include the top/left boundary but not the right/bottom
# boundary. This may be unintuitive when not drawing the boundary. Maybe somehow remove the boundary
# coordinates from the face coordinates after generating both?
input_dtype = image.dtype
result = image.astype(np.float32)
rr, cc = skimage.draw.polygon(self.yy_int, self.xx_int, shape=image.shape)
if len(rr) > 0:
if alpha_face == 1:
result[rr, cc] = np.float32(color_face)
elif alpha_face == 0:
pass
else:
result[rr, cc] = (
(1 - alpha_face) * result[rr, cc, :]
+ alpha_face * np.float32(color_face)
)
ls_open = self.to_line_string(closed=False)
ls_closed = self.to_line_string(closed=True)
result = ls_closed.draw_lines_on_image(
result, color=color_lines, alpha=alpha_lines,
size=size_lines, raise_if_out_of_image=raise_if_out_of_image)
result = ls_open.draw_points_on_image(
result, color=color_points, alpha=alpha_points,
size=size_points, raise_if_out_of_image=raise_if_out_of_image)
if input_dtype.type == np.uint8:
result = np.clip(np.round(result), 0, 255).astype(input_dtype) # TODO make clipping more flexible
else:
result = result.astype(input_dtype)
return result | [
"def",
"draw_on_image",
"(",
"self",
",",
"image",
",",
"color",
"=",
"(",
"0",
",",
"255",
",",
"0",
")",
",",
"color_face",
"=",
"None",
",",
"color_lines",
"=",
"None",
",",
"color_points",
"=",
"None",
",",
"alpha",
"=",
"1.0",
",",
"alpha_face",
"=",
"None",
",",
"alpha_lines",
"=",
"None",
",",
"alpha_points",
"=",
"None",
",",
"size",
"=",
"1",
",",
"size_lines",
"=",
"None",
",",
"size_points",
"=",
"None",
",",
"raise_if_out_of_image",
"=",
"False",
")",
":",
"assert",
"color",
"is",
"not",
"None",
"assert",
"alpha",
"is",
"not",
"None",
"assert",
"size",
"is",
"not",
"None",
"color_face",
"=",
"color_face",
"if",
"color_face",
"is",
"not",
"None",
"else",
"np",
".",
"array",
"(",
"color",
")",
"color_lines",
"=",
"color_lines",
"if",
"color_lines",
"is",
"not",
"None",
"else",
"np",
".",
"array",
"(",
"color",
")",
"*",
"0.5",
"color_points",
"=",
"color_points",
"if",
"color_points",
"is",
"not",
"None",
"else",
"np",
".",
"array",
"(",
"color",
")",
"*",
"0.5",
"alpha_face",
"=",
"alpha_face",
"if",
"alpha_face",
"is",
"not",
"None",
"else",
"alpha",
"*",
"0.5",
"alpha_lines",
"=",
"alpha_lines",
"if",
"alpha_lines",
"is",
"not",
"None",
"else",
"alpha",
"alpha_points",
"=",
"alpha_points",
"if",
"alpha_points",
"is",
"not",
"None",
"else",
"alpha",
"size_lines",
"=",
"size_lines",
"if",
"size_lines",
"is",
"not",
"None",
"else",
"size",
"size_points",
"=",
"size_points",
"if",
"size_points",
"is",
"not",
"None",
"else",
"size",
"*",
"3",
"if",
"image",
".",
"ndim",
"==",
"2",
":",
"assert",
"ia",
".",
"is_single_number",
"(",
"color_face",
")",
",",
"(",
"\"Got a 2D image. Expected then 'color_face' to be a single \"",
"\"number, but got %s.\"",
"%",
"(",
"str",
"(",
"color_face",
")",
",",
")",
")",
"color_face",
"=",
"[",
"color_face",
"]",
"elif",
"image",
".",
"ndim",
"==",
"3",
"and",
"ia",
".",
"is_single_number",
"(",
"color_face",
")",
":",
"color_face",
"=",
"[",
"color_face",
"]",
"*",
"image",
".",
"shape",
"[",
"-",
"1",
"]",
"if",
"alpha_face",
"<",
"0.01",
":",
"alpha_face",
"=",
"0",
"elif",
"alpha_face",
">",
"0.99",
":",
"alpha_face",
"=",
"1",
"if",
"raise_if_out_of_image",
"and",
"self",
".",
"is_out_of_image",
"(",
"image",
")",
":",
"raise",
"Exception",
"(",
"\"Cannot draw polygon %s on image with shape %s.\"",
"%",
"(",
"str",
"(",
"self",
")",
",",
"image",
".",
"shape",
")",
")",
"# TODO np.clip to image plane if is_fully_within_image(), similar to how it is done for bounding boxes",
"# TODO improve efficiency by only drawing in rectangle that covers poly instead of drawing in the whole image",
"# TODO for a rectangular polygon, the face coordinates include the top/left boundary but not the right/bottom",
"# boundary. This may be unintuitive when not drawing the boundary. Maybe somehow remove the boundary",
"# coordinates from the face coordinates after generating both?",
"input_dtype",
"=",
"image",
".",
"dtype",
"result",
"=",
"image",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"rr",
",",
"cc",
"=",
"skimage",
".",
"draw",
".",
"polygon",
"(",
"self",
".",
"yy_int",
",",
"self",
".",
"xx_int",
",",
"shape",
"=",
"image",
".",
"shape",
")",
"if",
"len",
"(",
"rr",
")",
">",
"0",
":",
"if",
"alpha_face",
"==",
"1",
":",
"result",
"[",
"rr",
",",
"cc",
"]",
"=",
"np",
".",
"float32",
"(",
"color_face",
")",
"elif",
"alpha_face",
"==",
"0",
":",
"pass",
"else",
":",
"result",
"[",
"rr",
",",
"cc",
"]",
"=",
"(",
"(",
"1",
"-",
"alpha_face",
")",
"*",
"result",
"[",
"rr",
",",
"cc",
",",
":",
"]",
"+",
"alpha_face",
"*",
"np",
".",
"float32",
"(",
"color_face",
")",
")",
"ls_open",
"=",
"self",
".",
"to_line_string",
"(",
"closed",
"=",
"False",
")",
"ls_closed",
"=",
"self",
".",
"to_line_string",
"(",
"closed",
"=",
"True",
")",
"result",
"=",
"ls_closed",
".",
"draw_lines_on_image",
"(",
"result",
",",
"color",
"=",
"color_lines",
",",
"alpha",
"=",
"alpha_lines",
",",
"size",
"=",
"size_lines",
",",
"raise_if_out_of_image",
"=",
"raise_if_out_of_image",
")",
"result",
"=",
"ls_open",
".",
"draw_points_on_image",
"(",
"result",
",",
"color",
"=",
"color_points",
",",
"alpha",
"=",
"alpha_points",
",",
"size",
"=",
"size_points",
",",
"raise_if_out_of_image",
"=",
"raise_if_out_of_image",
")",
"if",
"input_dtype",
".",
"type",
"==",
"np",
".",
"uint8",
":",
"result",
"=",
"np",
".",
"clip",
"(",
"np",
".",
"round",
"(",
"result",
")",
",",
"0",
",",
"255",
")",
".",
"astype",
"(",
"input_dtype",
")",
"# TODO make clipping more flexible",
"else",
":",
"result",
"=",
"result",
".",
"astype",
"(",
"input_dtype",
")",
"return",
"result"
] | Draw the polygon on an image.
Parameters
----------
image : (H,W,C) ndarray
The image onto which to draw the polygon. Usually expected to be
of dtype ``uint8``, though other dtypes are also handled.
color : iterable of int, optional
The color to use for the whole polygon.
Must correspond to the channel layout of the image. Usually RGB.
The values for `color_face`, `color_lines` and `color_points`
will be derived from this color if they are set to ``None``.
This argument has no effect if `color_face`, `color_lines`
and `color_points` are all set anything other than ``None``.
color_face : None or iterable of int, optional
The color to use for the inner polygon area (excluding perimeter).
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 1.0``.
color_lines : None or iterable of int, optional
The color to use for the line (aka perimeter/border) of the polygon.
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 0.5``.
color_points : None or iterable of int, optional
The color to use for the corner points of the polygon.
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 0.5``.
alpha : float, optional
The opacity of the whole polygon, where ``1.0`` denotes a completely
visible polygon and ``0.0`` an invisible one.
The values for `alpha_face`, `alpha_lines` and `alpha_points`
will be derived from this alpha value if they are set to ``None``.
This argument has no effect if `alpha_face`, `alpha_lines`
and `alpha_points` are all set anything other than ``None``.
alpha_face : None or number, optional
The opacity of the polygon's inner area (excluding the perimeter),
where ``1.0`` denotes a completely visible inner area and ``0.0``
an invisible one.
If this is ``None``, it will be derived from ``alpha * 0.5``.
alpha_lines : None or number, optional
The opacity of the polygon's line (aka perimeter/border),
where ``1.0`` denotes a completely visible line and ``0.0`` an
invisible one.
If this is ``None``, it will be derived from ``alpha * 1.0``.
alpha_points : None or number, optional
The opacity of the polygon's corner points, where ``1.0`` denotes
completely visible corners and ``0.0`` invisible ones.
If this is ``None``, it will be derived from ``alpha * 1.0``.
size : int, optional
Size of the polygon.
The sizes of the line and points are derived from this value,
unless they are set.
size_lines : None or int, optional
Thickness of the polygon's line (aka perimeter/border).
If ``None``, this value is derived from `size`.
size_points : int, optional
Size of the points in pixels.
If ``None``, this value is derived from ``3 * size``.
raise_if_out_of_image : bool, optional
Whether to raise an error if the polygon is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
result : (H,W,C) ndarray
Image with polygon drawn on it. Result dtype is the same as the input dtype. | [
"Draw",
"the",
"polygon",
"on",
"an",
"image",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L435-L591 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.extract_from_image | def extract_from_image(self, image):
"""
Extract the image pixels within the polygon.
This function will zero-pad the image if the polygon is partially/fully outside of
the image.
Parameters
----------
image : (H,W) ndarray or (H,W,C) ndarray
The image from which to extract the pixels within the polygon.
Returns
-------
result : (H',W') ndarray or (H',W',C) ndarray
Pixels within the polygon. Zero-padded if the polygon is partially/fully
outside of the image.
"""
ia.do_assert(image.ndim in [2, 3])
if len(self.exterior) <= 2:
raise Exception("Polygon must be made up of at least 3 points to extract its area from an image.")
bb = self.to_bounding_box()
bb_area = bb.extract_from_image(image)
if self.is_out_of_image(image, fully=True, partly=False):
return bb_area
xx = self.xx_int
yy = self.yy_int
xx_mask = xx - np.min(xx)
yy_mask = yy - np.min(yy)
height_mask = np.max(yy_mask)
width_mask = np.max(xx_mask)
rr_face, cc_face = skimage.draw.polygon(yy_mask, xx_mask, shape=(height_mask, width_mask))
mask = np.zeros((height_mask, width_mask), dtype=np.bool)
mask[rr_face, cc_face] = True
if image.ndim == 3:
mask = np.tile(mask[:, :, np.newaxis], (1, 1, image.shape[2]))
return bb_area * mask | python | def extract_from_image(self, image):
"""
Extract the image pixels within the polygon.
This function will zero-pad the image if the polygon is partially/fully outside of
the image.
Parameters
----------
image : (H,W) ndarray or (H,W,C) ndarray
The image from which to extract the pixels within the polygon.
Returns
-------
result : (H',W') ndarray or (H',W',C) ndarray
Pixels within the polygon. Zero-padded if the polygon is partially/fully
outside of the image.
"""
ia.do_assert(image.ndim in [2, 3])
if len(self.exterior) <= 2:
raise Exception("Polygon must be made up of at least 3 points to extract its area from an image.")
bb = self.to_bounding_box()
bb_area = bb.extract_from_image(image)
if self.is_out_of_image(image, fully=True, partly=False):
return bb_area
xx = self.xx_int
yy = self.yy_int
xx_mask = xx - np.min(xx)
yy_mask = yy - np.min(yy)
height_mask = np.max(yy_mask)
width_mask = np.max(xx_mask)
rr_face, cc_face = skimage.draw.polygon(yy_mask, xx_mask, shape=(height_mask, width_mask))
mask = np.zeros((height_mask, width_mask), dtype=np.bool)
mask[rr_face, cc_face] = True
if image.ndim == 3:
mask = np.tile(mask[:, :, np.newaxis], (1, 1, image.shape[2]))
return bb_area * mask | [
"def",
"extract_from_image",
"(",
"self",
",",
"image",
")",
":",
"ia",
".",
"do_assert",
"(",
"image",
".",
"ndim",
"in",
"[",
"2",
",",
"3",
"]",
")",
"if",
"len",
"(",
"self",
".",
"exterior",
")",
"<=",
"2",
":",
"raise",
"Exception",
"(",
"\"Polygon must be made up of at least 3 points to extract its area from an image.\"",
")",
"bb",
"=",
"self",
".",
"to_bounding_box",
"(",
")",
"bb_area",
"=",
"bb",
".",
"extract_from_image",
"(",
"image",
")",
"if",
"self",
".",
"is_out_of_image",
"(",
"image",
",",
"fully",
"=",
"True",
",",
"partly",
"=",
"False",
")",
":",
"return",
"bb_area",
"xx",
"=",
"self",
".",
"xx_int",
"yy",
"=",
"self",
".",
"yy_int",
"xx_mask",
"=",
"xx",
"-",
"np",
".",
"min",
"(",
"xx",
")",
"yy_mask",
"=",
"yy",
"-",
"np",
".",
"min",
"(",
"yy",
")",
"height_mask",
"=",
"np",
".",
"max",
"(",
"yy_mask",
")",
"width_mask",
"=",
"np",
".",
"max",
"(",
"xx_mask",
")",
"rr_face",
",",
"cc_face",
"=",
"skimage",
".",
"draw",
".",
"polygon",
"(",
"yy_mask",
",",
"xx_mask",
",",
"shape",
"=",
"(",
"height_mask",
",",
"width_mask",
")",
")",
"mask",
"=",
"np",
".",
"zeros",
"(",
"(",
"height_mask",
",",
"width_mask",
")",
",",
"dtype",
"=",
"np",
".",
"bool",
")",
"mask",
"[",
"rr_face",
",",
"cc_face",
"]",
"=",
"True",
"if",
"image",
".",
"ndim",
"==",
"3",
":",
"mask",
"=",
"np",
".",
"tile",
"(",
"mask",
"[",
":",
",",
":",
",",
"np",
".",
"newaxis",
"]",
",",
"(",
"1",
",",
"1",
",",
"image",
".",
"shape",
"[",
"2",
"]",
")",
")",
"return",
"bb_area",
"*",
"mask"
] | Extract the image pixels within the polygon.
This function will zero-pad the image if the polygon is partially/fully outside of
the image.
Parameters
----------
image : (H,W) ndarray or (H,W,C) ndarray
The image from which to extract the pixels within the polygon.
Returns
-------
result : (H',W') ndarray or (H',W',C) ndarray
Pixels within the polygon. Zero-padded if the polygon is partially/fully
outside of the image. | [
"Extract",
"the",
"image",
"pixels",
"within",
"the",
"polygon",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L593-L636 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.change_first_point_by_coords | def change_first_point_by_coords(self, x, y, max_distance=1e-4,
raise_if_too_far_away=True):
"""
Set the first point of the exterior to the given point based on its coordinates.
If multiple points are found, the closest one will be picked.
If no matching points are found, an exception is raised.
Note: This method does *not* work in-place.
Parameters
----------
x : number
X-coordinate of the point.
y : number
Y-coordinate of the point.
max_distance : None or number, optional
Maximum distance past which possible matches are ignored.
If ``None`` the distance limit is deactivated.
raise_if_too_far_away : bool, optional
Whether to raise an exception if the closest found point is too
far away (``True``) or simply return an unchanged copy if this
object (``False``).
Returns
-------
imgaug.Polygon
Copy of this polygon with the new point order.
"""
if len(self.exterior) == 0:
raise Exception("Cannot reorder polygon points, because it contains no points.")
closest_idx, closest_dist = self.find_closest_point_index(x=x, y=y, return_distance=True)
if max_distance is not None and closest_dist > max_distance:
if not raise_if_too_far_away:
return self.deepcopy()
closest_point = self.exterior[closest_idx, :]
raise Exception(
"Closest found point (%.9f, %.9f) exceeds max_distance of %.9f exceeded" % (
closest_point[0], closest_point[1], closest_dist)
)
return self.change_first_point_by_index(closest_idx) | python | def change_first_point_by_coords(self, x, y, max_distance=1e-4,
raise_if_too_far_away=True):
"""
Set the first point of the exterior to the given point based on its coordinates.
If multiple points are found, the closest one will be picked.
If no matching points are found, an exception is raised.
Note: This method does *not* work in-place.
Parameters
----------
x : number
X-coordinate of the point.
y : number
Y-coordinate of the point.
max_distance : None or number, optional
Maximum distance past which possible matches are ignored.
If ``None`` the distance limit is deactivated.
raise_if_too_far_away : bool, optional
Whether to raise an exception if the closest found point is too
far away (``True``) or simply return an unchanged copy if this
object (``False``).
Returns
-------
imgaug.Polygon
Copy of this polygon with the new point order.
"""
if len(self.exterior) == 0:
raise Exception("Cannot reorder polygon points, because it contains no points.")
closest_idx, closest_dist = self.find_closest_point_index(x=x, y=y, return_distance=True)
if max_distance is not None and closest_dist > max_distance:
if not raise_if_too_far_away:
return self.deepcopy()
closest_point = self.exterior[closest_idx, :]
raise Exception(
"Closest found point (%.9f, %.9f) exceeds max_distance of %.9f exceeded" % (
closest_point[0], closest_point[1], closest_dist)
)
return self.change_first_point_by_index(closest_idx) | [
"def",
"change_first_point_by_coords",
"(",
"self",
",",
"x",
",",
"y",
",",
"max_distance",
"=",
"1e-4",
",",
"raise_if_too_far_away",
"=",
"True",
")",
":",
"if",
"len",
"(",
"self",
".",
"exterior",
")",
"==",
"0",
":",
"raise",
"Exception",
"(",
"\"Cannot reorder polygon points, because it contains no points.\"",
")",
"closest_idx",
",",
"closest_dist",
"=",
"self",
".",
"find_closest_point_index",
"(",
"x",
"=",
"x",
",",
"y",
"=",
"y",
",",
"return_distance",
"=",
"True",
")",
"if",
"max_distance",
"is",
"not",
"None",
"and",
"closest_dist",
">",
"max_distance",
":",
"if",
"not",
"raise_if_too_far_away",
":",
"return",
"self",
".",
"deepcopy",
"(",
")",
"closest_point",
"=",
"self",
".",
"exterior",
"[",
"closest_idx",
",",
":",
"]",
"raise",
"Exception",
"(",
"\"Closest found point (%.9f, %.9f) exceeds max_distance of %.9f exceeded\"",
"%",
"(",
"closest_point",
"[",
"0",
"]",
",",
"closest_point",
"[",
"1",
"]",
",",
"closest_dist",
")",
")",
"return",
"self",
".",
"change_first_point_by_index",
"(",
"closest_idx",
")"
] | Set the first point of the exterior to the given point based on its coordinates.
If multiple points are found, the closest one will be picked.
If no matching points are found, an exception is raised.
Note: This method does *not* work in-place.
Parameters
----------
x : number
X-coordinate of the point.
y : number
Y-coordinate of the point.
max_distance : None or number, optional
Maximum distance past which possible matches are ignored.
If ``None`` the distance limit is deactivated.
raise_if_too_far_away : bool, optional
Whether to raise an exception if the closest found point is too
far away (``True``) or simply return an unchanged copy if this
object (``False``).
Returns
-------
imgaug.Polygon
Copy of this polygon with the new point order. | [
"Set",
"the",
"first",
"point",
"of",
"the",
"exterior",
"to",
"the",
"given",
"point",
"based",
"on",
"its",
"coordinates",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L638-L684 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.change_first_point_by_index | def change_first_point_by_index(self, point_idx):
"""
Set the first point of the exterior to the given point based on its index.
Note: This method does *not* work in-place.
Parameters
----------
point_idx : int
Index of the desired starting point.
Returns
-------
imgaug.Polygon
Copy of this polygon with the new point order.
"""
ia.do_assert(0 <= point_idx < len(self.exterior))
if point_idx == 0:
return self.deepcopy()
exterior = np.concatenate(
(self.exterior[point_idx:, :], self.exterior[:point_idx, :]),
axis=0
)
return self.deepcopy(exterior=exterior) | python | def change_first_point_by_index(self, point_idx):
"""
Set the first point of the exterior to the given point based on its index.
Note: This method does *not* work in-place.
Parameters
----------
point_idx : int
Index of the desired starting point.
Returns
-------
imgaug.Polygon
Copy of this polygon with the new point order.
"""
ia.do_assert(0 <= point_idx < len(self.exterior))
if point_idx == 0:
return self.deepcopy()
exterior = np.concatenate(
(self.exterior[point_idx:, :], self.exterior[:point_idx, :]),
axis=0
)
return self.deepcopy(exterior=exterior) | [
"def",
"change_first_point_by_index",
"(",
"self",
",",
"point_idx",
")",
":",
"ia",
".",
"do_assert",
"(",
"0",
"<=",
"point_idx",
"<",
"len",
"(",
"self",
".",
"exterior",
")",
")",
"if",
"point_idx",
"==",
"0",
":",
"return",
"self",
".",
"deepcopy",
"(",
")",
"exterior",
"=",
"np",
".",
"concatenate",
"(",
"(",
"self",
".",
"exterior",
"[",
"point_idx",
":",
",",
":",
"]",
",",
"self",
".",
"exterior",
"[",
":",
"point_idx",
",",
":",
"]",
")",
",",
"axis",
"=",
"0",
")",
"return",
"self",
".",
"deepcopy",
"(",
"exterior",
"=",
"exterior",
")"
] | Set the first point of the exterior to the given point based on its index.
Note: This method does *not* work in-place.
Parameters
----------
point_idx : int
Index of the desired starting point.
Returns
-------
imgaug.Polygon
Copy of this polygon with the new point order. | [
"Set",
"the",
"first",
"point",
"of",
"the",
"exterior",
"to",
"the",
"given",
"point",
"based",
"on",
"its",
"index",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L686-L710 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.to_shapely_polygon | def to_shapely_polygon(self):
"""
Convert this polygon to a Shapely polygon.
Returns
-------
shapely.geometry.Polygon
The Shapely polygon matching this polygon's exterior.
"""
# load shapely lazily, which makes the dependency more optional
import shapely.geometry
return shapely.geometry.Polygon([(point[0], point[1]) for point in self.exterior]) | python | def to_shapely_polygon(self):
"""
Convert this polygon to a Shapely polygon.
Returns
-------
shapely.geometry.Polygon
The Shapely polygon matching this polygon's exterior.
"""
# load shapely lazily, which makes the dependency more optional
import shapely.geometry
return shapely.geometry.Polygon([(point[0], point[1]) for point in self.exterior]) | [
"def",
"to_shapely_polygon",
"(",
"self",
")",
":",
"# load shapely lazily, which makes the dependency more optional",
"import",
"shapely",
".",
"geometry",
"return",
"shapely",
".",
"geometry",
".",
"Polygon",
"(",
"[",
"(",
"point",
"[",
"0",
"]",
",",
"point",
"[",
"1",
"]",
")",
"for",
"point",
"in",
"self",
".",
"exterior",
"]",
")"
] | Convert this polygon to a Shapely polygon.
Returns
-------
shapely.geometry.Polygon
The Shapely polygon matching this polygon's exterior. | [
"Convert",
"this",
"polygon",
"to",
"a",
"Shapely",
"polygon",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L712-L725 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.to_shapely_line_string | def to_shapely_line_string(self, closed=False, interpolate=0):
"""
Convert this polygon to a Shapely LineString object.
Parameters
----------
closed : bool, optional
Whether to return the line string with the last point being identical to the first point.
interpolate : int, optional
Number of points to interpolate between any pair of two consecutive points. These points are added
to the final line string.
Returns
-------
shapely.geometry.LineString
The Shapely LineString matching the polygon's exterior.
"""
return _convert_points_to_shapely_line_string(self.exterior, closed=closed, interpolate=interpolate) | python | def to_shapely_line_string(self, closed=False, interpolate=0):
"""
Convert this polygon to a Shapely LineString object.
Parameters
----------
closed : bool, optional
Whether to return the line string with the last point being identical to the first point.
interpolate : int, optional
Number of points to interpolate between any pair of two consecutive points. These points are added
to the final line string.
Returns
-------
shapely.geometry.LineString
The Shapely LineString matching the polygon's exterior.
"""
return _convert_points_to_shapely_line_string(self.exterior, closed=closed, interpolate=interpolate) | [
"def",
"to_shapely_line_string",
"(",
"self",
",",
"closed",
"=",
"False",
",",
"interpolate",
"=",
"0",
")",
":",
"return",
"_convert_points_to_shapely_line_string",
"(",
"self",
".",
"exterior",
",",
"closed",
"=",
"closed",
",",
"interpolate",
"=",
"interpolate",
")"
] | Convert this polygon to a Shapely LineString object.
Parameters
----------
closed : bool, optional
Whether to return the line string with the last point being identical to the first point.
interpolate : int, optional
Number of points to interpolate between any pair of two consecutive points. These points are added
to the final line string.
Returns
-------
shapely.geometry.LineString
The Shapely LineString matching the polygon's exterior. | [
"Convert",
"this",
"polygon",
"to",
"a",
"Shapely",
"LineString",
"object",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L727-L746 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.to_bounding_box | def to_bounding_box(self):
"""
Convert this polygon to a bounding box tightly containing the whole polygon.
Returns
-------
imgaug.BoundingBox
Tight bounding box around the polygon.
"""
# TODO get rid of this deferred import
from imgaug.augmentables.bbs import BoundingBox
xx = self.xx
yy = self.yy
return BoundingBox(x1=min(xx), x2=max(xx), y1=min(yy), y2=max(yy), label=self.label) | python | def to_bounding_box(self):
"""
Convert this polygon to a bounding box tightly containing the whole polygon.
Returns
-------
imgaug.BoundingBox
Tight bounding box around the polygon.
"""
# TODO get rid of this deferred import
from imgaug.augmentables.bbs import BoundingBox
xx = self.xx
yy = self.yy
return BoundingBox(x1=min(xx), x2=max(xx), y1=min(yy), y2=max(yy), label=self.label) | [
"def",
"to_bounding_box",
"(",
"self",
")",
":",
"# TODO get rid of this deferred import",
"from",
"imgaug",
".",
"augmentables",
".",
"bbs",
"import",
"BoundingBox",
"xx",
"=",
"self",
".",
"xx",
"yy",
"=",
"self",
".",
"yy",
"return",
"BoundingBox",
"(",
"x1",
"=",
"min",
"(",
"xx",
")",
",",
"x2",
"=",
"max",
"(",
"xx",
")",
",",
"y1",
"=",
"min",
"(",
"yy",
")",
",",
"y2",
"=",
"max",
"(",
"yy",
")",
",",
"label",
"=",
"self",
".",
"label",
")"
] | Convert this polygon to a bounding box tightly containing the whole polygon.
Returns
-------
imgaug.BoundingBox
Tight bounding box around the polygon. | [
"Convert",
"this",
"polygon",
"to",
"a",
"bounding",
"box",
"tightly",
"containing",
"the",
"whole",
"polygon",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L748-L763 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.to_keypoints | def to_keypoints(self):
"""
Convert this polygon's `exterior` to ``Keypoint`` instances.
Returns
-------
list of imgaug.Keypoint
Exterior vertices as ``Keypoint`` instances.
"""
# TODO get rid of this deferred import
from imgaug.augmentables.kps import Keypoint
return [Keypoint(x=point[0], y=point[1]) for point in self.exterior] | python | def to_keypoints(self):
"""
Convert this polygon's `exterior` to ``Keypoint`` instances.
Returns
-------
list of imgaug.Keypoint
Exterior vertices as ``Keypoint`` instances.
"""
# TODO get rid of this deferred import
from imgaug.augmentables.kps import Keypoint
return [Keypoint(x=point[0], y=point[1]) for point in self.exterior] | [
"def",
"to_keypoints",
"(",
"self",
")",
":",
"# TODO get rid of this deferred import",
"from",
"imgaug",
".",
"augmentables",
".",
"kps",
"import",
"Keypoint",
"return",
"[",
"Keypoint",
"(",
"x",
"=",
"point",
"[",
"0",
"]",
",",
"y",
"=",
"point",
"[",
"1",
"]",
")",
"for",
"point",
"in",
"self",
".",
"exterior",
"]"
] | Convert this polygon's `exterior` to ``Keypoint`` instances.
Returns
-------
list of imgaug.Keypoint
Exterior vertices as ``Keypoint`` instances. | [
"Convert",
"this",
"polygon",
"s",
"exterior",
"to",
"Keypoint",
"instances",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L765-L778 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.to_line_string | def to_line_string(self, closed=True):
"""
Convert this polygon's `exterior` to a ``LineString`` instance.
Parameters
----------
closed : bool, optional
Whether to close the line string, i.e. to add the first point of
the `exterior` also as the last point at the end of the line string.
This has no effect if the polygon has a single point or zero
points.
Returns
-------
imgaug.augmentables.lines.LineString
Exterior of the polygon as a line string.
"""
from imgaug.augmentables.lines import LineString
if not closed or len(self.exterior) <= 1:
return LineString(self.exterior, label=self.label)
return LineString(
np.concatenate([self.exterior, self.exterior[0:1, :]], axis=0),
label=self.label) | python | def to_line_string(self, closed=True):
"""
Convert this polygon's `exterior` to a ``LineString`` instance.
Parameters
----------
closed : bool, optional
Whether to close the line string, i.e. to add the first point of
the `exterior` also as the last point at the end of the line string.
This has no effect if the polygon has a single point or zero
points.
Returns
-------
imgaug.augmentables.lines.LineString
Exterior of the polygon as a line string.
"""
from imgaug.augmentables.lines import LineString
if not closed or len(self.exterior) <= 1:
return LineString(self.exterior, label=self.label)
return LineString(
np.concatenate([self.exterior, self.exterior[0:1, :]], axis=0),
label=self.label) | [
"def",
"to_line_string",
"(",
"self",
",",
"closed",
"=",
"True",
")",
":",
"from",
"imgaug",
".",
"augmentables",
".",
"lines",
"import",
"LineString",
"if",
"not",
"closed",
"or",
"len",
"(",
"self",
".",
"exterior",
")",
"<=",
"1",
":",
"return",
"LineString",
"(",
"self",
".",
"exterior",
",",
"label",
"=",
"self",
".",
"label",
")",
"return",
"LineString",
"(",
"np",
".",
"concatenate",
"(",
"[",
"self",
".",
"exterior",
",",
"self",
".",
"exterior",
"[",
"0",
":",
"1",
",",
":",
"]",
"]",
",",
"axis",
"=",
"0",
")",
",",
"label",
"=",
"self",
".",
"label",
")"
] | Convert this polygon's `exterior` to a ``LineString`` instance.
Parameters
----------
closed : bool, optional
Whether to close the line string, i.e. to add the first point of
the `exterior` also as the last point at the end of the line string.
This has no effect if the polygon has a single point or zero
points.
Returns
-------
imgaug.augmentables.lines.LineString
Exterior of the polygon as a line string. | [
"Convert",
"this",
"polygon",
"s",
"exterior",
"to",
"a",
"LineString",
"instance",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L780-L803 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.from_shapely | def from_shapely(polygon_shapely, label=None):
"""
Create a polygon from a Shapely polygon.
Note: This will remove any holes in the Shapely polygon.
Parameters
----------
polygon_shapely : shapely.geometry.Polygon
The shapely polygon.
label : None or str, optional
The label of the new polygon.
Returns
-------
imgaug.Polygon
A polygon with the same exterior as the Shapely polygon.
"""
# load shapely lazily, which makes the dependency more optional
import shapely.geometry
ia.do_assert(isinstance(polygon_shapely, shapely.geometry.Polygon))
# polygon_shapely.exterior can be None if the polygon was instantiated without points
if polygon_shapely.exterior is None or len(polygon_shapely.exterior.coords) == 0:
return Polygon([], label=label)
exterior = np.float32([[x, y] for (x, y) in polygon_shapely.exterior.coords])
return Polygon(exterior, label=label) | python | def from_shapely(polygon_shapely, label=None):
"""
Create a polygon from a Shapely polygon.
Note: This will remove any holes in the Shapely polygon.
Parameters
----------
polygon_shapely : shapely.geometry.Polygon
The shapely polygon.
label : None or str, optional
The label of the new polygon.
Returns
-------
imgaug.Polygon
A polygon with the same exterior as the Shapely polygon.
"""
# load shapely lazily, which makes the dependency more optional
import shapely.geometry
ia.do_assert(isinstance(polygon_shapely, shapely.geometry.Polygon))
# polygon_shapely.exterior can be None if the polygon was instantiated without points
if polygon_shapely.exterior is None or len(polygon_shapely.exterior.coords) == 0:
return Polygon([], label=label)
exterior = np.float32([[x, y] for (x, y) in polygon_shapely.exterior.coords])
return Polygon(exterior, label=label) | [
"def",
"from_shapely",
"(",
"polygon_shapely",
",",
"label",
"=",
"None",
")",
":",
"# load shapely lazily, which makes the dependency more optional",
"import",
"shapely",
".",
"geometry",
"ia",
".",
"do_assert",
"(",
"isinstance",
"(",
"polygon_shapely",
",",
"shapely",
".",
"geometry",
".",
"Polygon",
")",
")",
"# polygon_shapely.exterior can be None if the polygon was instantiated without points",
"if",
"polygon_shapely",
".",
"exterior",
"is",
"None",
"or",
"len",
"(",
"polygon_shapely",
".",
"exterior",
".",
"coords",
")",
"==",
"0",
":",
"return",
"Polygon",
"(",
"[",
"]",
",",
"label",
"=",
"label",
")",
"exterior",
"=",
"np",
".",
"float32",
"(",
"[",
"[",
"x",
",",
"y",
"]",
"for",
"(",
"x",
",",
"y",
")",
"in",
"polygon_shapely",
".",
"exterior",
".",
"coords",
"]",
")",
"return",
"Polygon",
"(",
"exterior",
",",
"label",
"=",
"label",
")"
] | Create a polygon from a Shapely polygon.
Note: This will remove any holes in the Shapely polygon.
Parameters
----------
polygon_shapely : shapely.geometry.Polygon
The shapely polygon.
label : None or str, optional
The label of the new polygon.
Returns
-------
imgaug.Polygon
A polygon with the same exterior as the Shapely polygon. | [
"Create",
"a",
"polygon",
"from",
"a",
"Shapely",
"polygon",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L806-L834 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.exterior_almost_equals | def exterior_almost_equals(self, other, max_distance=1e-6, points_per_edge=8):
"""
Estimate if this and other polygon's exterior are almost identical.
The two exteriors can have different numbers of points, but any point
randomly sampled on the exterior of one polygon should be close to the
closest point on the exterior of the other polygon.
Note that this method works approximately. One can come up with
polygons with fairly different shapes that will still be estimated as
equal by this method. In practice however this should be unlikely to be
the case. The probability for something like that goes down as the
interpolation parameter is increased.
Parameters
----------
other : imgaug.Polygon or (N,2) ndarray or list of tuple
The other polygon with which to compare the exterior.
If this is an ndarray, it is assumed to represent an exterior.
It must then have dtype ``float32`` and shape ``(N,2)`` with the
second dimension denoting xy-coordinates.
If this is a list of tuples, it is assumed to represent an exterior.
Each tuple then must contain exactly two numbers, denoting
xy-coordinates.
max_distance : number, optional
The maximum euclidean distance between a point on one polygon and
the closest point on the other polygon. If the distance is exceeded
for any such pair, the two exteriors are not viewed as equal. The
points are other the points contained in the polygon's exterior
ndarray or interpolated points between these.
points_per_edge : int, optional
How many points to interpolate on each edge.
Returns
-------
bool
Whether the two polygon's exteriors can be viewed as equal
(approximate test).
"""
if isinstance(other, list):
other = Polygon(np.float32(other))
elif ia.is_np_array(other):
other = Polygon(other)
else:
assert isinstance(other, Polygon)
other = other
return self.to_line_string(closed=True).coords_almost_equals(
other.to_line_string(closed=True),
max_distance=max_distance,
points_per_edge=points_per_edge
) | python | def exterior_almost_equals(self, other, max_distance=1e-6, points_per_edge=8):
"""
Estimate if this and other polygon's exterior are almost identical.
The two exteriors can have different numbers of points, but any point
randomly sampled on the exterior of one polygon should be close to the
closest point on the exterior of the other polygon.
Note that this method works approximately. One can come up with
polygons with fairly different shapes that will still be estimated as
equal by this method. In practice however this should be unlikely to be
the case. The probability for something like that goes down as the
interpolation parameter is increased.
Parameters
----------
other : imgaug.Polygon or (N,2) ndarray or list of tuple
The other polygon with which to compare the exterior.
If this is an ndarray, it is assumed to represent an exterior.
It must then have dtype ``float32`` and shape ``(N,2)`` with the
second dimension denoting xy-coordinates.
If this is a list of tuples, it is assumed to represent an exterior.
Each tuple then must contain exactly two numbers, denoting
xy-coordinates.
max_distance : number, optional
The maximum euclidean distance between a point on one polygon and
the closest point on the other polygon. If the distance is exceeded
for any such pair, the two exteriors are not viewed as equal. The
points are other the points contained in the polygon's exterior
ndarray or interpolated points between these.
points_per_edge : int, optional
How many points to interpolate on each edge.
Returns
-------
bool
Whether the two polygon's exteriors can be viewed as equal
(approximate test).
"""
if isinstance(other, list):
other = Polygon(np.float32(other))
elif ia.is_np_array(other):
other = Polygon(other)
else:
assert isinstance(other, Polygon)
other = other
return self.to_line_string(closed=True).coords_almost_equals(
other.to_line_string(closed=True),
max_distance=max_distance,
points_per_edge=points_per_edge
) | [
"def",
"exterior_almost_equals",
"(",
"self",
",",
"other",
",",
"max_distance",
"=",
"1e-6",
",",
"points_per_edge",
"=",
"8",
")",
":",
"if",
"isinstance",
"(",
"other",
",",
"list",
")",
":",
"other",
"=",
"Polygon",
"(",
"np",
".",
"float32",
"(",
"other",
")",
")",
"elif",
"ia",
".",
"is_np_array",
"(",
"other",
")",
":",
"other",
"=",
"Polygon",
"(",
"other",
")",
"else",
":",
"assert",
"isinstance",
"(",
"other",
",",
"Polygon",
")",
"other",
"=",
"other",
"return",
"self",
".",
"to_line_string",
"(",
"closed",
"=",
"True",
")",
".",
"coords_almost_equals",
"(",
"other",
".",
"to_line_string",
"(",
"closed",
"=",
"True",
")",
",",
"max_distance",
"=",
"max_distance",
",",
"points_per_edge",
"=",
"points_per_edge",
")"
] | Estimate if this and other polygon's exterior are almost identical.
The two exteriors can have different numbers of points, but any point
randomly sampled on the exterior of one polygon should be close to the
closest point on the exterior of the other polygon.
Note that this method works approximately. One can come up with
polygons with fairly different shapes that will still be estimated as
equal by this method. In practice however this should be unlikely to be
the case. The probability for something like that goes down as the
interpolation parameter is increased.
Parameters
----------
other : imgaug.Polygon or (N,2) ndarray or list of tuple
The other polygon with which to compare the exterior.
If this is an ndarray, it is assumed to represent an exterior.
It must then have dtype ``float32`` and shape ``(N,2)`` with the
second dimension denoting xy-coordinates.
If this is a list of tuples, it is assumed to represent an exterior.
Each tuple then must contain exactly two numbers, denoting
xy-coordinates.
max_distance : number, optional
The maximum euclidean distance between a point on one polygon and
the closest point on the other polygon. If the distance is exceeded
for any such pair, the two exteriors are not viewed as equal. The
points are other the points contained in the polygon's exterior
ndarray or interpolated points between these.
points_per_edge : int, optional
How many points to interpolate on each edge.
Returns
-------
bool
Whether the two polygon's exteriors can be viewed as equal
(approximate test). | [
"Estimate",
"if",
"this",
"and",
"other",
"polygon",
"s",
"exterior",
"are",
"almost",
"identical",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L836-L890 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.almost_equals | def almost_equals(self, other, max_distance=1e-6, points_per_edge=8):
"""
Estimate if this polygon's and another's geometry/labels are similar.
This is the same as :func:`imgaug.Polygon.exterior_almost_equals` but
additionally compares the labels.
Parameters
----------
other
The object to compare against. If not a Polygon, then False will
be returned.
max_distance : float, optional
See :func:`imgaug.augmentables.polys.Polygon.exterior_almost_equals`.
points_per_edge : int, optional
See :func:`imgaug.augmentables.polys.Polygon.exterior_almost_equals`.
Returns
-------
bool
Whether the two polygons can be viewed as equal. In the case of
the exteriors this is an approximate test.
"""
if not isinstance(other, Polygon):
return False
if self.label is not None or other.label is not None:
if self.label is None:
return False
if other.label is None:
return False
if self.label != other.label:
return False
return self.exterior_almost_equals(
other, max_distance=max_distance, points_per_edge=points_per_edge) | python | def almost_equals(self, other, max_distance=1e-6, points_per_edge=8):
"""
Estimate if this polygon's and another's geometry/labels are similar.
This is the same as :func:`imgaug.Polygon.exterior_almost_equals` but
additionally compares the labels.
Parameters
----------
other
The object to compare against. If not a Polygon, then False will
be returned.
max_distance : float, optional
See :func:`imgaug.augmentables.polys.Polygon.exterior_almost_equals`.
points_per_edge : int, optional
See :func:`imgaug.augmentables.polys.Polygon.exterior_almost_equals`.
Returns
-------
bool
Whether the two polygons can be viewed as equal. In the case of
the exteriors this is an approximate test.
"""
if not isinstance(other, Polygon):
return False
if self.label is not None or other.label is not None:
if self.label is None:
return False
if other.label is None:
return False
if self.label != other.label:
return False
return self.exterior_almost_equals(
other, max_distance=max_distance, points_per_edge=points_per_edge) | [
"def",
"almost_equals",
"(",
"self",
",",
"other",
",",
"max_distance",
"=",
"1e-6",
",",
"points_per_edge",
"=",
"8",
")",
":",
"if",
"not",
"isinstance",
"(",
"other",
",",
"Polygon",
")",
":",
"return",
"False",
"if",
"self",
".",
"label",
"is",
"not",
"None",
"or",
"other",
".",
"label",
"is",
"not",
"None",
":",
"if",
"self",
".",
"label",
"is",
"None",
":",
"return",
"False",
"if",
"other",
".",
"label",
"is",
"None",
":",
"return",
"False",
"if",
"self",
".",
"label",
"!=",
"other",
".",
"label",
":",
"return",
"False",
"return",
"self",
".",
"exterior_almost_equals",
"(",
"other",
",",
"max_distance",
"=",
"max_distance",
",",
"points_per_edge",
"=",
"points_per_edge",
")"
] | Estimate if this polygon's and another's geometry/labels are similar.
This is the same as :func:`imgaug.Polygon.exterior_almost_equals` but
additionally compares the labels.
Parameters
----------
other
The object to compare against. If not a Polygon, then False will
be returned.
max_distance : float, optional
See :func:`imgaug.augmentables.polys.Polygon.exterior_almost_equals`.
points_per_edge : int, optional
See :func:`imgaug.augmentables.polys.Polygon.exterior_almost_equals`.
Returns
-------
bool
Whether the two polygons can be viewed as equal. In the case of
the exteriors this is an approximate test. | [
"Estimate",
"if",
"this",
"polygon",
"s",
"and",
"another",
"s",
"geometry",
"/",
"labels",
"are",
"similar",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L892-L928 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.copy | def copy(self, exterior=None, label=None):
"""
Create a shallow copy of the Polygon object.
Parameters
----------
exterior : list of imgaug.Keypoint or list of tuple or (N,2) ndarray, optional
List of points defining the polygon. See :func:`imgaug.Polygon.__init__` for details.
label : None or str, optional
If not None, then the label of the copied object will be set to this value.
Returns
-------
imgaug.Polygon
Shallow copy.
"""
return self.deepcopy(exterior=exterior, label=label) | python | def copy(self, exterior=None, label=None):
"""
Create a shallow copy of the Polygon object.
Parameters
----------
exterior : list of imgaug.Keypoint or list of tuple or (N,2) ndarray, optional
List of points defining the polygon. See :func:`imgaug.Polygon.__init__` for details.
label : None or str, optional
If not None, then the label of the copied object will be set to this value.
Returns
-------
imgaug.Polygon
Shallow copy.
"""
return self.deepcopy(exterior=exterior, label=label) | [
"def",
"copy",
"(",
"self",
",",
"exterior",
"=",
"None",
",",
"label",
"=",
"None",
")",
":",
"return",
"self",
".",
"deepcopy",
"(",
"exterior",
"=",
"exterior",
",",
"label",
"=",
"label",
")"
] | Create a shallow copy of the Polygon object.
Parameters
----------
exterior : list of imgaug.Keypoint or list of tuple or (N,2) ndarray, optional
List of points defining the polygon. See :func:`imgaug.Polygon.__init__` for details.
label : None or str, optional
If not None, then the label of the copied object will be set to this value.
Returns
-------
imgaug.Polygon
Shallow copy. | [
"Create",
"a",
"shallow",
"copy",
"of",
"the",
"Polygon",
"object",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L930-L948 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | Polygon.deepcopy | def deepcopy(self, exterior=None, label=None):
"""
Create a deep copy of the Polygon object.
Parameters
----------
exterior : list of Keypoint or list of tuple or (N,2) ndarray, optional
List of points defining the polygon. See `imgaug.Polygon.__init__` for details.
label : None or str
If not None, then the label of the copied object will be set to this value.
Returns
-------
imgaug.Polygon
Deep copy.
"""
return Polygon(
exterior=np.copy(self.exterior) if exterior is None else exterior,
label=self.label if label is None else label
) | python | def deepcopy(self, exterior=None, label=None):
"""
Create a deep copy of the Polygon object.
Parameters
----------
exterior : list of Keypoint or list of tuple or (N,2) ndarray, optional
List of points defining the polygon. See `imgaug.Polygon.__init__` for details.
label : None or str
If not None, then the label of the copied object will be set to this value.
Returns
-------
imgaug.Polygon
Deep copy.
"""
return Polygon(
exterior=np.copy(self.exterior) if exterior is None else exterior,
label=self.label if label is None else label
) | [
"def",
"deepcopy",
"(",
"self",
",",
"exterior",
"=",
"None",
",",
"label",
"=",
"None",
")",
":",
"return",
"Polygon",
"(",
"exterior",
"=",
"np",
".",
"copy",
"(",
"self",
".",
"exterior",
")",
"if",
"exterior",
"is",
"None",
"else",
"exterior",
",",
"label",
"=",
"self",
".",
"label",
"if",
"label",
"is",
"None",
"else",
"label",
")"
] | Create a deep copy of the Polygon object.
Parameters
----------
exterior : list of Keypoint or list of tuple or (N,2) ndarray, optional
List of points defining the polygon. See `imgaug.Polygon.__init__` for details.
label : None or str
If not None, then the label of the copied object will be set to this value.
Returns
-------
imgaug.Polygon
Deep copy. | [
"Create",
"a",
"deep",
"copy",
"of",
"the",
"Polygon",
"object",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L950-L971 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | PolygonsOnImage.on | def on(self, image):
"""
Project polygons from one image to a new one.
Parameters
----------
image : ndarray or tuple of int
New image onto which the polygons are to be projected.
May also simply be that new image's shape tuple.
Returns
-------
imgaug.PolygonsOnImage
Object containing all projected polygons.
"""
shape = normalize_shape(image)
if shape[0:2] == self.shape[0:2]:
return self.deepcopy()
polygons = [poly.project(self.shape, shape) for poly in self.polygons]
# TODO use deepcopy() here
return PolygonsOnImage(polygons, shape) | python | def on(self, image):
"""
Project polygons from one image to a new one.
Parameters
----------
image : ndarray or tuple of int
New image onto which the polygons are to be projected.
May also simply be that new image's shape tuple.
Returns
-------
imgaug.PolygonsOnImage
Object containing all projected polygons.
"""
shape = normalize_shape(image)
if shape[0:2] == self.shape[0:2]:
return self.deepcopy()
polygons = [poly.project(self.shape, shape) for poly in self.polygons]
# TODO use deepcopy() here
return PolygonsOnImage(polygons, shape) | [
"def",
"on",
"(",
"self",
",",
"image",
")",
":",
"shape",
"=",
"normalize_shape",
"(",
"image",
")",
"if",
"shape",
"[",
"0",
":",
"2",
"]",
"==",
"self",
".",
"shape",
"[",
"0",
":",
"2",
"]",
":",
"return",
"self",
".",
"deepcopy",
"(",
")",
"polygons",
"=",
"[",
"poly",
".",
"project",
"(",
"self",
".",
"shape",
",",
"shape",
")",
"for",
"poly",
"in",
"self",
".",
"polygons",
"]",
"# TODO use deepcopy() here",
"return",
"PolygonsOnImage",
"(",
"polygons",
",",
"shape",
")"
] | Project polygons from one image to a new one.
Parameters
----------
image : ndarray or tuple of int
New image onto which the polygons are to be projected.
May also simply be that new image's shape tuple.
Returns
-------
imgaug.PolygonsOnImage
Object containing all projected polygons. | [
"Project",
"polygons",
"from",
"one",
"image",
"to",
"a",
"new",
"one",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L1024-L1045 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | PolygonsOnImage.draw_on_image | def draw_on_image(self,
image,
color=(0, 255, 0), color_face=None,
color_lines=None, color_points=None,
alpha=1.0, alpha_face=None,
alpha_lines=None, alpha_points=None,
size=1, size_lines=None, size_points=None,
raise_if_out_of_image=False):
"""
Draw all polygons onto a given image.
Parameters
----------
image : (H,W,C) ndarray
The image onto which to draw the bounding boxes.
This image should usually have the same shape as set in
``PolygonsOnImage.shape``.
color : iterable of int, optional
The color to use for the whole polygons.
Must correspond to the channel layout of the image. Usually RGB.
The values for `color_face`, `color_lines` and `color_points`
will be derived from this color if they are set to ``None``.
This argument has no effect if `color_face`, `color_lines`
and `color_points` are all set anything other than ``None``.
color_face : None or iterable of int, optional
The color to use for the inner polygon areas (excluding perimeters).
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 1.0``.
color_lines : None or iterable of int, optional
The color to use for the lines (aka perimeters/borders) of the
polygons. Must correspond to the channel layout of the image.
Usually RGB. If this is ``None``, it will be derived
from ``color * 0.5``.
color_points : None or iterable of int, optional
The color to use for the corner points of the polygons.
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 0.5``.
alpha : float, optional
The opacity of the whole polygons, where ``1.0`` denotes
completely visible polygons and ``0.0`` invisible ones.
The values for `alpha_face`, `alpha_lines` and `alpha_points`
will be derived from this alpha value if they are set to ``None``.
This argument has no effect if `alpha_face`, `alpha_lines`
and `alpha_points` are all set anything other than ``None``.
alpha_face : None or number, optional
The opacity of the polygon's inner areas (excluding the perimeters),
where ``1.0`` denotes completely visible inner areas and ``0.0``
invisible ones.
If this is ``None``, it will be derived from ``alpha * 0.5``.
alpha_lines : None or number, optional
The opacity of the polygon's lines (aka perimeters/borders),
where ``1.0`` denotes completely visible perimeters and ``0.0``
invisible ones.
If this is ``None``, it will be derived from ``alpha * 1.0``.
alpha_points : None or number, optional
The opacity of the polygon's corner points, where ``1.0`` denotes
completely visible corners and ``0.0`` invisible ones.
Currently this is an on/off choice, i.e. only ``0.0`` or ``1.0``
are allowed.
If this is ``None``, it will be derived from ``alpha * 1.0``.
size : int, optional
Size of the polygons.
The sizes of the line and points are derived from this value,
unless they are set.
size_lines : None or int, optional
Thickness of the polygon lines (aka perimeter/border).
If ``None``, this value is derived from `size`.
size_points : int, optional
The size of all corner points. If set to ``C``, each corner point
will be drawn as a square of size ``C x C``.
raise_if_out_of_image : bool, optional
Whether to raise an error if any polygon is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
image : (H,W,C) ndarray
Image with drawn polygons.
"""
for poly in self.polygons:
image = poly.draw_on_image(
image,
color=color,
color_face=color_face,
color_lines=color_lines,
color_points=color_points,
alpha=alpha,
alpha_face=alpha_face,
alpha_lines=alpha_lines,
alpha_points=alpha_points,
size=size,
size_lines=size_lines,
size_points=size_points,
raise_if_out_of_image=raise_if_out_of_image
)
return image | python | def draw_on_image(self,
image,
color=(0, 255, 0), color_face=None,
color_lines=None, color_points=None,
alpha=1.0, alpha_face=None,
alpha_lines=None, alpha_points=None,
size=1, size_lines=None, size_points=None,
raise_if_out_of_image=False):
"""
Draw all polygons onto a given image.
Parameters
----------
image : (H,W,C) ndarray
The image onto which to draw the bounding boxes.
This image should usually have the same shape as set in
``PolygonsOnImage.shape``.
color : iterable of int, optional
The color to use for the whole polygons.
Must correspond to the channel layout of the image. Usually RGB.
The values for `color_face`, `color_lines` and `color_points`
will be derived from this color if they are set to ``None``.
This argument has no effect if `color_face`, `color_lines`
and `color_points` are all set anything other than ``None``.
color_face : None or iterable of int, optional
The color to use for the inner polygon areas (excluding perimeters).
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 1.0``.
color_lines : None or iterable of int, optional
The color to use for the lines (aka perimeters/borders) of the
polygons. Must correspond to the channel layout of the image.
Usually RGB. If this is ``None``, it will be derived
from ``color * 0.5``.
color_points : None or iterable of int, optional
The color to use for the corner points of the polygons.
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 0.5``.
alpha : float, optional
The opacity of the whole polygons, where ``1.0`` denotes
completely visible polygons and ``0.0`` invisible ones.
The values for `alpha_face`, `alpha_lines` and `alpha_points`
will be derived from this alpha value if they are set to ``None``.
This argument has no effect if `alpha_face`, `alpha_lines`
and `alpha_points` are all set anything other than ``None``.
alpha_face : None or number, optional
The opacity of the polygon's inner areas (excluding the perimeters),
where ``1.0`` denotes completely visible inner areas and ``0.0``
invisible ones.
If this is ``None``, it will be derived from ``alpha * 0.5``.
alpha_lines : None or number, optional
The opacity of the polygon's lines (aka perimeters/borders),
where ``1.0`` denotes completely visible perimeters and ``0.0``
invisible ones.
If this is ``None``, it will be derived from ``alpha * 1.0``.
alpha_points : None or number, optional
The opacity of the polygon's corner points, where ``1.0`` denotes
completely visible corners and ``0.0`` invisible ones.
Currently this is an on/off choice, i.e. only ``0.0`` or ``1.0``
are allowed.
If this is ``None``, it will be derived from ``alpha * 1.0``.
size : int, optional
Size of the polygons.
The sizes of the line and points are derived from this value,
unless they are set.
size_lines : None or int, optional
Thickness of the polygon lines (aka perimeter/border).
If ``None``, this value is derived from `size`.
size_points : int, optional
The size of all corner points. If set to ``C``, each corner point
will be drawn as a square of size ``C x C``.
raise_if_out_of_image : bool, optional
Whether to raise an error if any polygon is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
image : (H,W,C) ndarray
Image with drawn polygons.
"""
for poly in self.polygons:
image = poly.draw_on_image(
image,
color=color,
color_face=color_face,
color_lines=color_lines,
color_points=color_points,
alpha=alpha,
alpha_face=alpha_face,
alpha_lines=alpha_lines,
alpha_points=alpha_points,
size=size,
size_lines=size_lines,
size_points=size_points,
raise_if_out_of_image=raise_if_out_of_image
)
return image | [
"def",
"draw_on_image",
"(",
"self",
",",
"image",
",",
"color",
"=",
"(",
"0",
",",
"255",
",",
"0",
")",
",",
"color_face",
"=",
"None",
",",
"color_lines",
"=",
"None",
",",
"color_points",
"=",
"None",
",",
"alpha",
"=",
"1.0",
",",
"alpha_face",
"=",
"None",
",",
"alpha_lines",
"=",
"None",
",",
"alpha_points",
"=",
"None",
",",
"size",
"=",
"1",
",",
"size_lines",
"=",
"None",
",",
"size_points",
"=",
"None",
",",
"raise_if_out_of_image",
"=",
"False",
")",
":",
"for",
"poly",
"in",
"self",
".",
"polygons",
":",
"image",
"=",
"poly",
".",
"draw_on_image",
"(",
"image",
",",
"color",
"=",
"color",
",",
"color_face",
"=",
"color_face",
",",
"color_lines",
"=",
"color_lines",
",",
"color_points",
"=",
"color_points",
",",
"alpha",
"=",
"alpha",
",",
"alpha_face",
"=",
"alpha_face",
",",
"alpha_lines",
"=",
"alpha_lines",
",",
"alpha_points",
"=",
"alpha_points",
",",
"size",
"=",
"size",
",",
"size_lines",
"=",
"size_lines",
",",
"size_points",
"=",
"size_points",
",",
"raise_if_out_of_image",
"=",
"raise_if_out_of_image",
")",
"return",
"image"
] | Draw all polygons onto a given image.
Parameters
----------
image : (H,W,C) ndarray
The image onto which to draw the bounding boxes.
This image should usually have the same shape as set in
``PolygonsOnImage.shape``.
color : iterable of int, optional
The color to use for the whole polygons.
Must correspond to the channel layout of the image. Usually RGB.
The values for `color_face`, `color_lines` and `color_points`
will be derived from this color if they are set to ``None``.
This argument has no effect if `color_face`, `color_lines`
and `color_points` are all set anything other than ``None``.
color_face : None or iterable of int, optional
The color to use for the inner polygon areas (excluding perimeters).
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 1.0``.
color_lines : None or iterable of int, optional
The color to use for the lines (aka perimeters/borders) of the
polygons. Must correspond to the channel layout of the image.
Usually RGB. If this is ``None``, it will be derived
from ``color * 0.5``.
color_points : None or iterable of int, optional
The color to use for the corner points of the polygons.
Must correspond to the channel layout of the image. Usually RGB.
If this is ``None``, it will be derived from ``color * 0.5``.
alpha : float, optional
The opacity of the whole polygons, where ``1.0`` denotes
completely visible polygons and ``0.0`` invisible ones.
The values for `alpha_face`, `alpha_lines` and `alpha_points`
will be derived from this alpha value if they are set to ``None``.
This argument has no effect if `alpha_face`, `alpha_lines`
and `alpha_points` are all set anything other than ``None``.
alpha_face : None or number, optional
The opacity of the polygon's inner areas (excluding the perimeters),
where ``1.0`` denotes completely visible inner areas and ``0.0``
invisible ones.
If this is ``None``, it will be derived from ``alpha * 0.5``.
alpha_lines : None or number, optional
The opacity of the polygon's lines (aka perimeters/borders),
where ``1.0`` denotes completely visible perimeters and ``0.0``
invisible ones.
If this is ``None``, it will be derived from ``alpha * 1.0``.
alpha_points : None or number, optional
The opacity of the polygon's corner points, where ``1.0`` denotes
completely visible corners and ``0.0`` invisible ones.
Currently this is an on/off choice, i.e. only ``0.0`` or ``1.0``
are allowed.
If this is ``None``, it will be derived from ``alpha * 1.0``.
size : int, optional
Size of the polygons.
The sizes of the line and points are derived from this value,
unless they are set.
size_lines : None or int, optional
Thickness of the polygon lines (aka perimeter/border).
If ``None``, this value is derived from `size`.
size_points : int, optional
The size of all corner points. If set to ``C``, each corner point
will be drawn as a square of size ``C x C``.
raise_if_out_of_image : bool, optional
Whether to raise an error if any polygon is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
image : (H,W,C) ndarray
Image with drawn polygons. | [
"Draw",
"all",
"polygons",
"onto",
"a",
"given",
"image",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L1047-L1156 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | PolygonsOnImage.remove_out_of_image | def remove_out_of_image(self, fully=True, partly=False):
"""
Remove all polygons that are fully or partially outside of the image.
Parameters
----------
fully : bool, optional
Whether to remove polygons that are fully outside of the image.
partly : bool, optional
Whether to remove polygons that are partially outside of the image.
Returns
-------
imgaug.PolygonsOnImage
Reduced set of polygons, with those that were fully/partially
outside of the image removed.
"""
polys_clean = [
poly for poly in self.polygons
if not poly.is_out_of_image(self.shape, fully=fully, partly=partly)
]
# TODO use deepcopy() here
return PolygonsOnImage(polys_clean, shape=self.shape) | python | def remove_out_of_image(self, fully=True, partly=False):
"""
Remove all polygons that are fully or partially outside of the image.
Parameters
----------
fully : bool, optional
Whether to remove polygons that are fully outside of the image.
partly : bool, optional
Whether to remove polygons that are partially outside of the image.
Returns
-------
imgaug.PolygonsOnImage
Reduced set of polygons, with those that were fully/partially
outside of the image removed.
"""
polys_clean = [
poly for poly in self.polygons
if not poly.is_out_of_image(self.shape, fully=fully, partly=partly)
]
# TODO use deepcopy() here
return PolygonsOnImage(polys_clean, shape=self.shape) | [
"def",
"remove_out_of_image",
"(",
"self",
",",
"fully",
"=",
"True",
",",
"partly",
"=",
"False",
")",
":",
"polys_clean",
"=",
"[",
"poly",
"for",
"poly",
"in",
"self",
".",
"polygons",
"if",
"not",
"poly",
".",
"is_out_of_image",
"(",
"self",
".",
"shape",
",",
"fully",
"=",
"fully",
",",
"partly",
"=",
"partly",
")",
"]",
"# TODO use deepcopy() here",
"return",
"PolygonsOnImage",
"(",
"polys_clean",
",",
"shape",
"=",
"self",
".",
"shape",
")"
] | Remove all polygons that are fully or partially outside of the image.
Parameters
----------
fully : bool, optional
Whether to remove polygons that are fully outside of the image.
partly : bool, optional
Whether to remove polygons that are partially outside of the image.
Returns
-------
imgaug.PolygonsOnImage
Reduced set of polygons, with those that were fully/partially
outside of the image removed. | [
"Remove",
"all",
"polygons",
"that",
"are",
"fully",
"or",
"partially",
"outside",
"of",
"the",
"image",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L1158-L1182 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | PolygonsOnImage.clip_out_of_image | def clip_out_of_image(self):
"""
Clip off all parts from all polygons that are outside of the image.
NOTE: The result can contain less polygons than the input did. That
happens when a polygon is fully outside of the image plane.
NOTE: The result can also contain *more* polygons than the input
did. That happens when distinct parts of a polygon are only
connected by areas that are outside of the image plane and hence will
be clipped off, resulting in two or more unconnected polygon parts that
are left in the image plane.
Returns
-------
imgaug.PolygonsOnImage
Polygons, clipped to fall within the image dimensions. Count of
output polygons may differ from the input count.
"""
polys_cut = [
poly.clip_out_of_image(self.shape)
for poly
in self.polygons
if poly.is_partly_within_image(self.shape)
]
polys_cut_flat = [poly for poly_lst in polys_cut for poly in poly_lst]
# TODO use deepcopy() here
return PolygonsOnImage(polys_cut_flat, shape=self.shape) | python | def clip_out_of_image(self):
"""
Clip off all parts from all polygons that are outside of the image.
NOTE: The result can contain less polygons than the input did. That
happens when a polygon is fully outside of the image plane.
NOTE: The result can also contain *more* polygons than the input
did. That happens when distinct parts of a polygon are only
connected by areas that are outside of the image plane and hence will
be clipped off, resulting in two or more unconnected polygon parts that
are left in the image plane.
Returns
-------
imgaug.PolygonsOnImage
Polygons, clipped to fall within the image dimensions. Count of
output polygons may differ from the input count.
"""
polys_cut = [
poly.clip_out_of_image(self.shape)
for poly
in self.polygons
if poly.is_partly_within_image(self.shape)
]
polys_cut_flat = [poly for poly_lst in polys_cut for poly in poly_lst]
# TODO use deepcopy() here
return PolygonsOnImage(polys_cut_flat, shape=self.shape) | [
"def",
"clip_out_of_image",
"(",
"self",
")",
":",
"polys_cut",
"=",
"[",
"poly",
".",
"clip_out_of_image",
"(",
"self",
".",
"shape",
")",
"for",
"poly",
"in",
"self",
".",
"polygons",
"if",
"poly",
".",
"is_partly_within_image",
"(",
"self",
".",
"shape",
")",
"]",
"polys_cut_flat",
"=",
"[",
"poly",
"for",
"poly_lst",
"in",
"polys_cut",
"for",
"poly",
"in",
"poly_lst",
"]",
"# TODO use deepcopy() here",
"return",
"PolygonsOnImage",
"(",
"polys_cut_flat",
",",
"shape",
"=",
"self",
".",
"shape",
")"
] | Clip off all parts from all polygons that are outside of the image.
NOTE: The result can contain less polygons than the input did. That
happens when a polygon is fully outside of the image plane.
NOTE: The result can also contain *more* polygons than the input
did. That happens when distinct parts of a polygon are only
connected by areas that are outside of the image plane and hence will
be clipped off, resulting in two or more unconnected polygon parts that
are left in the image plane.
Returns
-------
imgaug.PolygonsOnImage
Polygons, clipped to fall within the image dimensions. Count of
output polygons may differ from the input count. | [
"Clip",
"off",
"all",
"parts",
"from",
"all",
"polygons",
"that",
"are",
"outside",
"of",
"the",
"image",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L1184-L1212 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | PolygonsOnImage.shift | def shift(self, top=None, right=None, bottom=None, left=None):
"""
Shift all polygons from one or more image sides, i.e. move them on the x/y-axis.
Parameters
----------
top : None or int, optional
Amount of pixels by which to shift all polygons from the top.
right : None or int, optional
Amount of pixels by which to shift all polygons from the right.
bottom : None or int, optional
Amount of pixels by which to shift all polygons from the bottom.
left : None or int, optional
Amount of pixels by which to shift all polygons from the left.
Returns
-------
imgaug.PolygonsOnImage
Shifted polygons.
"""
polys_new = [
poly.shift(top=top, right=right, bottom=bottom, left=left)
for poly
in self.polygons
]
return PolygonsOnImage(polys_new, shape=self.shape) | python | def shift(self, top=None, right=None, bottom=None, left=None):
"""
Shift all polygons from one or more image sides, i.e. move them on the x/y-axis.
Parameters
----------
top : None or int, optional
Amount of pixels by which to shift all polygons from the top.
right : None or int, optional
Amount of pixels by which to shift all polygons from the right.
bottom : None or int, optional
Amount of pixels by which to shift all polygons from the bottom.
left : None or int, optional
Amount of pixels by which to shift all polygons from the left.
Returns
-------
imgaug.PolygonsOnImage
Shifted polygons.
"""
polys_new = [
poly.shift(top=top, right=right, bottom=bottom, left=left)
for poly
in self.polygons
]
return PolygonsOnImage(polys_new, shape=self.shape) | [
"def",
"shift",
"(",
"self",
",",
"top",
"=",
"None",
",",
"right",
"=",
"None",
",",
"bottom",
"=",
"None",
",",
"left",
"=",
"None",
")",
":",
"polys_new",
"=",
"[",
"poly",
".",
"shift",
"(",
"top",
"=",
"top",
",",
"right",
"=",
"right",
",",
"bottom",
"=",
"bottom",
",",
"left",
"=",
"left",
")",
"for",
"poly",
"in",
"self",
".",
"polygons",
"]",
"return",
"PolygonsOnImage",
"(",
"polys_new",
",",
"shape",
"=",
"self",
".",
"shape",
")"
] | Shift all polygons from one or more image sides, i.e. move them on the x/y-axis.
Parameters
----------
top : None or int, optional
Amount of pixels by which to shift all polygons from the top.
right : None or int, optional
Amount of pixels by which to shift all polygons from the right.
bottom : None or int, optional
Amount of pixels by which to shift all polygons from the bottom.
left : None or int, optional
Amount of pixels by which to shift all polygons from the left.
Returns
-------
imgaug.PolygonsOnImage
Shifted polygons. | [
"Shift",
"all",
"polygons",
"from",
"one",
"or",
"more",
"image",
"sides",
"i",
".",
"e",
".",
"move",
"them",
"on",
"the",
"x",
"/",
"y",
"-",
"axis",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L1214-L1243 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | PolygonsOnImage.deepcopy | def deepcopy(self):
"""
Create a deep copy of the PolygonsOnImage object.
Returns
-------
imgaug.PolygonsOnImage
Deep copy.
"""
# Manual copy is far faster than deepcopy for PolygonsOnImage,
# so use manual copy here too
polys = [poly.deepcopy() for poly in self.polygons]
return PolygonsOnImage(polys, tuple(self.shape)) | python | def deepcopy(self):
"""
Create a deep copy of the PolygonsOnImage object.
Returns
-------
imgaug.PolygonsOnImage
Deep copy.
"""
# Manual copy is far faster than deepcopy for PolygonsOnImage,
# so use manual copy here too
polys = [poly.deepcopy() for poly in self.polygons]
return PolygonsOnImage(polys, tuple(self.shape)) | [
"def",
"deepcopy",
"(",
"self",
")",
":",
"# Manual copy is far faster than deepcopy for PolygonsOnImage,",
"# so use manual copy here too",
"polys",
"=",
"[",
"poly",
".",
"deepcopy",
"(",
")",
"for",
"poly",
"in",
"self",
".",
"polygons",
"]",
"return",
"PolygonsOnImage",
"(",
"polys",
",",
"tuple",
"(",
"self",
".",
"shape",
")",
")"
] | Create a deep copy of the PolygonsOnImage object.
Returns
-------
imgaug.PolygonsOnImage
Deep copy. | [
"Create",
"a",
"deep",
"copy",
"of",
"the",
"PolygonsOnImage",
"object",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L1257-L1270 | valid |
aleju/imgaug | imgaug/augmentables/polys.py | MultiPolygon.from_shapely | def from_shapely(geometry, label=None):
"""
Create a MultiPolygon from a Shapely MultiPolygon, a Shapely Polygon or a Shapely GeometryCollection.
This also creates all necessary Polygons contained by this MultiPolygon.
Parameters
----------
geometry : shapely.geometry.MultiPolygon or shapely.geometry.Polygon\
or shapely.geometry.collection.GeometryCollection
The object to convert to a MultiPolygon.
label : None or str, optional
A label assigned to all Polygons within the MultiPolygon.
Returns
-------
imgaug.MultiPolygon
The derived MultiPolygon.
"""
# load shapely lazily, which makes the dependency more optional
import shapely.geometry
if isinstance(geometry, shapely.geometry.MultiPolygon):
return MultiPolygon([Polygon.from_shapely(poly, label=label) for poly in geometry.geoms])
elif isinstance(geometry, shapely.geometry.Polygon):
return MultiPolygon([Polygon.from_shapely(geometry, label=label)])
elif isinstance(geometry, shapely.geometry.collection.GeometryCollection):
ia.do_assert(all([isinstance(poly, shapely.geometry.Polygon) for poly in geometry.geoms]))
return MultiPolygon([Polygon.from_shapely(poly, label=label) for poly in geometry.geoms])
else:
raise Exception("Unknown datatype '%s'. Expected shapely.geometry.Polygon or "
"shapely.geometry.MultiPolygon or "
"shapely.geometry.collections.GeometryCollection." % (type(geometry),)) | python | def from_shapely(geometry, label=None):
"""
Create a MultiPolygon from a Shapely MultiPolygon, a Shapely Polygon or a Shapely GeometryCollection.
This also creates all necessary Polygons contained by this MultiPolygon.
Parameters
----------
geometry : shapely.geometry.MultiPolygon or shapely.geometry.Polygon\
or shapely.geometry.collection.GeometryCollection
The object to convert to a MultiPolygon.
label : None or str, optional
A label assigned to all Polygons within the MultiPolygon.
Returns
-------
imgaug.MultiPolygon
The derived MultiPolygon.
"""
# load shapely lazily, which makes the dependency more optional
import shapely.geometry
if isinstance(geometry, shapely.geometry.MultiPolygon):
return MultiPolygon([Polygon.from_shapely(poly, label=label) for poly in geometry.geoms])
elif isinstance(geometry, shapely.geometry.Polygon):
return MultiPolygon([Polygon.from_shapely(geometry, label=label)])
elif isinstance(geometry, shapely.geometry.collection.GeometryCollection):
ia.do_assert(all([isinstance(poly, shapely.geometry.Polygon) for poly in geometry.geoms]))
return MultiPolygon([Polygon.from_shapely(poly, label=label) for poly in geometry.geoms])
else:
raise Exception("Unknown datatype '%s'. Expected shapely.geometry.Polygon or "
"shapely.geometry.MultiPolygon or "
"shapely.geometry.collections.GeometryCollection." % (type(geometry),)) | [
"def",
"from_shapely",
"(",
"geometry",
",",
"label",
"=",
"None",
")",
":",
"# load shapely lazily, which makes the dependency more optional",
"import",
"shapely",
".",
"geometry",
"if",
"isinstance",
"(",
"geometry",
",",
"shapely",
".",
"geometry",
".",
"MultiPolygon",
")",
":",
"return",
"MultiPolygon",
"(",
"[",
"Polygon",
".",
"from_shapely",
"(",
"poly",
",",
"label",
"=",
"label",
")",
"for",
"poly",
"in",
"geometry",
".",
"geoms",
"]",
")",
"elif",
"isinstance",
"(",
"geometry",
",",
"shapely",
".",
"geometry",
".",
"Polygon",
")",
":",
"return",
"MultiPolygon",
"(",
"[",
"Polygon",
".",
"from_shapely",
"(",
"geometry",
",",
"label",
"=",
"label",
")",
"]",
")",
"elif",
"isinstance",
"(",
"geometry",
",",
"shapely",
".",
"geometry",
".",
"collection",
".",
"GeometryCollection",
")",
":",
"ia",
".",
"do_assert",
"(",
"all",
"(",
"[",
"isinstance",
"(",
"poly",
",",
"shapely",
".",
"geometry",
".",
"Polygon",
")",
"for",
"poly",
"in",
"geometry",
".",
"geoms",
"]",
")",
")",
"return",
"MultiPolygon",
"(",
"[",
"Polygon",
".",
"from_shapely",
"(",
"poly",
",",
"label",
"=",
"label",
")",
"for",
"poly",
"in",
"geometry",
".",
"geoms",
"]",
")",
"else",
":",
"raise",
"Exception",
"(",
"\"Unknown datatype '%s'. Expected shapely.geometry.Polygon or \"",
"\"shapely.geometry.MultiPolygon or \"",
"\"shapely.geometry.collections.GeometryCollection.\"",
"%",
"(",
"type",
"(",
"geometry",
")",
",",
")",
")"
] | Create a MultiPolygon from a Shapely MultiPolygon, a Shapely Polygon or a Shapely GeometryCollection.
This also creates all necessary Polygons contained by this MultiPolygon.
Parameters
----------
geometry : shapely.geometry.MultiPolygon or shapely.geometry.Polygon\
or shapely.geometry.collection.GeometryCollection
The object to convert to a MultiPolygon.
label : None or str, optional
A label assigned to all Polygons within the MultiPolygon.
Returns
-------
imgaug.MultiPolygon
The derived MultiPolygon. | [
"Create",
"a",
"MultiPolygon",
"from",
"a",
"Shapely",
"MultiPolygon",
"a",
"Shapely",
"Polygon",
"or",
"a",
"Shapely",
"GeometryCollection",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/polys.py#L1776-L1810 | valid |
aleju/imgaug | imgaug/augmenters/size.py | Pad | def Pad(px=None, percent=None, pad_mode="constant", pad_cval=0, keep_size=True, sample_independently=True,
name=None, deterministic=False, random_state=None):
"""
Augmenter that pads images, i.e. adds columns/rows to them.
dtype support::
See ``imgaug.augmenters.size.CropAndPad``.
Parameters
----------
px : None or int or imgaug.parameters.StochasticParameter or tuple, optional
The number of pixels to pad on each side of the image.
Either this or the parameter `percent` may be set, not both at the same
time.
* If None, then pixel-based padding will not be used.
* If int, then that exact number of pixels will always be padded.
* If StochasticParameter, then that parameter will be used for each
image. Four samples will be drawn per image (top, right, bottom,
left).
* If a tuple of two ints with values a and b, then each side will
be padded by a random amount in the range ``a <= x <= b``.
``x`` is sampled per image side.
* If a tuple of four entries, then the entries represent top, right,
bottom, left. Each entry may be a single integer (always pad by
exactly that value), a tuple of two ints ``a`` and ``b`` (pad by
an amount ``a <= x <= b``), a list of ints (pad by a random value
that is contained in the list) or a StochasticParameter (sample
the amount to pad from that parameter).
percent : None or int or float or imgaug.parameters.StochasticParameter \
or tuple, optional
The number of pixels to pad on each side of the image given
*in percent* of the image height/width.
E.g. if this is set to 0.1, the augmenter will always add 10 percent
of the image's height to the top, 10 percent of the width to the right,
10 percent of the height at the bottom and 10 percent of the width to
the left. Either this or the parameter `px` may be set, not both at the
same time.
* If None, then percent-based padding will not be used.
* If int, then expected to be 0 (no padding).
* If float, then that percentage will always be padded.
* If StochasticParameter, then that parameter will be used for each
image. Four samples will be drawn per image (top, right, bottom,
left).
* If a tuple of two floats with values a and b, then each side will
be padded by a random percentage in the range ``a <= x <= b``.
``x`` is sampled per image side.
* If a tuple of four entries, then the entries represent top, right,
bottom, left. Each entry may be a single float (always pad by
exactly that percent value), a tuple of two floats ``a`` and ``b``
(pad by a percentage ``a <= x <= b``), a list of floats (pad by a
random value that is contained in the list) or a
StochasticParameter (sample the percentage to pad from that
parameter).
pad_mode : imgaug.ALL or str or list of str or \
imgaug.parameters.StochasticParameter, optional
Padding mode to use. The available modes match the numpy padding modes,
i.e. ``constant``, ``edge``, ``linear_ramp``, ``maximum``, ``median``,
``minimum``, ``reflect``, ``symmetric``, ``wrap``. The modes
``constant`` and ``linear_ramp`` use extra values, which are provided
by ``pad_cval`` when necessary. See :func:`imgaug.imgaug.pad` for
more details.
* If ``imgaug.ALL``, then a random mode from all available modes
will be sampled per image.
* If a string, it will be used as the pad mode for all images.
* If a list of strings, a random one of these will be sampled per
image and used as the mode.
* If StochasticParameter, a random mode will be sampled from this
parameter per image.
pad_cval : number or tuple of number list of number or \
imgaug.parameters.StochasticParameter, optional
The constant value to use if the pad mode is ``constant`` or the end
value to use if the mode is ``linear_ramp``.
See :func:`imgaug.imgaug.pad` for more details.
* If number, then that value will be used.
* If a tuple of two numbers and at least one of them is a float,
then a random number will be sampled from the continuous range
``a <= x <= b`` and used as the value. If both numbers are
integers, the range is discrete.
* If a list of number, then a random value will be chosen from the
elements of the list and used as the value.
* If StochasticParameter, a random value will be sampled from that
parameter per image.
keep_size : bool, optional
After padding, the result image will usually have a different
height/width compared to the original input image. If this parameter is
set to True, then the padded image will be resized to the input image's
size, i.e. the augmenter's output shape is always identical to the
input shape.
sample_independently : bool, optional
If False AND the values for `px`/`percent` result in exactly one
probability distribution for the amount to pad, only one single value
will be sampled from that probability distribution and used for all
sides. I.e. the pad amount then is the same for all sides.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Examples
--------
>>> aug = iaa.Pad(px=(0, 10))
pads each side by a random value from the range 0px to 10px (the value
is sampled per side). The added rows/columns are filled with black pixels.
>>> aug = iaa.Pad(px=(0, 10), sample_independently=False)
samples one value v from the discrete range ``[0..10]`` and pads all sides
by ``v`` pixels.
>>> aug = iaa.Pad(px=(0, 10), keep_size=False)
pads each side by a random value from the range 0px to 10px (the value
is sampled per side). After padding, the images are NOT resized to their
original size (i.e. the images may end up having different heights/widths).
>>> aug = iaa.Pad(px=((0, 10), (0, 5), (0, 10), (0, 5)))
pads the top and bottom by a random value from the range 0px to 10px
and the left and right by a random value in the range 0px to 5px.
>>> aug = iaa.Pad(percent=(0, 0.1))
pads each side by a random value from the range 0 percent to
10 percent. (Percent with respect to the side's size, e.g. for the
top side it uses the image's height.)
>>> aug = iaa.Pad(percent=([0.05, 0.1], [0.05, 0.1], [0.05, 0.1], [0.05, 0.1]))
pads each side by either 5 percent or 10 percent.
>>> aug = iaa.Pad(px=(0, 10), pad_mode="edge")
pads each side by a random value from the range 0px to 10px (the values
are sampled per side). The padding uses the ``edge`` mode from numpy's
pad function.
>>> aug = iaa.Pad(px=(0, 10), pad_mode=["constant", "edge"])
pads each side by a random value from the range 0px to 10px (the values
are sampled per side). The padding uses randomly either the ``constant``
or ``edge`` mode from numpy's pad function.
>>> aug = iaa.Pad(px=(0, 10), pad_mode=ia.ALL, pad_cval=(0, 255))
pads each side by a random value from the range 0px to 10px (the values
are sampled per side). It uses a random mode for numpy's pad function.
If the mode is ``constant`` or ``linear_ramp``, it samples a random value
``v`` from the range ``[0, 255]`` and uses that as the constant
value (``mode=constant``) or end value (``mode=linear_ramp``).
"""
def recursive_validate(v):
if v is None:
return v
elif ia.is_single_number(v):
ia.do_assert(v >= 0)
return v
elif isinstance(v, iap.StochasticParameter):
return v
elif isinstance(v, tuple):
return tuple([recursive_validate(v_) for v_ in v])
elif isinstance(v, list):
return [recursive_validate(v_) for v_ in v]
else:
raise Exception("Expected None or int or float or StochasticParameter or list or tuple, got %s." % (
type(v),))
px = recursive_validate(px)
percent = recursive_validate(percent)
if name is None:
name = "Unnamed%s" % (ia.caller_name(),)
aug = CropAndPad(
px=px, percent=percent,
pad_mode=pad_mode, pad_cval=pad_cval,
keep_size=keep_size, sample_independently=sample_independently,
name=name, deterministic=deterministic, random_state=random_state
)
return aug | python | def Pad(px=None, percent=None, pad_mode="constant", pad_cval=0, keep_size=True, sample_independently=True,
name=None, deterministic=False, random_state=None):
"""
Augmenter that pads images, i.e. adds columns/rows to them.
dtype support::
See ``imgaug.augmenters.size.CropAndPad``.
Parameters
----------
px : None or int or imgaug.parameters.StochasticParameter or tuple, optional
The number of pixels to pad on each side of the image.
Either this or the parameter `percent` may be set, not both at the same
time.
* If None, then pixel-based padding will not be used.
* If int, then that exact number of pixels will always be padded.
* If StochasticParameter, then that parameter will be used for each
image. Four samples will be drawn per image (top, right, bottom,
left).
* If a tuple of two ints with values a and b, then each side will
be padded by a random amount in the range ``a <= x <= b``.
``x`` is sampled per image side.
* If a tuple of four entries, then the entries represent top, right,
bottom, left. Each entry may be a single integer (always pad by
exactly that value), a tuple of two ints ``a`` and ``b`` (pad by
an amount ``a <= x <= b``), a list of ints (pad by a random value
that is contained in the list) or a StochasticParameter (sample
the amount to pad from that parameter).
percent : None or int or float or imgaug.parameters.StochasticParameter \
or tuple, optional
The number of pixels to pad on each side of the image given
*in percent* of the image height/width.
E.g. if this is set to 0.1, the augmenter will always add 10 percent
of the image's height to the top, 10 percent of the width to the right,
10 percent of the height at the bottom and 10 percent of the width to
the left. Either this or the parameter `px` may be set, not both at the
same time.
* If None, then percent-based padding will not be used.
* If int, then expected to be 0 (no padding).
* If float, then that percentage will always be padded.
* If StochasticParameter, then that parameter will be used for each
image. Four samples will be drawn per image (top, right, bottom,
left).
* If a tuple of two floats with values a and b, then each side will
be padded by a random percentage in the range ``a <= x <= b``.
``x`` is sampled per image side.
* If a tuple of four entries, then the entries represent top, right,
bottom, left. Each entry may be a single float (always pad by
exactly that percent value), a tuple of two floats ``a`` and ``b``
(pad by a percentage ``a <= x <= b``), a list of floats (pad by a
random value that is contained in the list) or a
StochasticParameter (sample the percentage to pad from that
parameter).
pad_mode : imgaug.ALL or str or list of str or \
imgaug.parameters.StochasticParameter, optional
Padding mode to use. The available modes match the numpy padding modes,
i.e. ``constant``, ``edge``, ``linear_ramp``, ``maximum``, ``median``,
``minimum``, ``reflect``, ``symmetric``, ``wrap``. The modes
``constant`` and ``linear_ramp`` use extra values, which are provided
by ``pad_cval`` when necessary. See :func:`imgaug.imgaug.pad` for
more details.
* If ``imgaug.ALL``, then a random mode from all available modes
will be sampled per image.
* If a string, it will be used as the pad mode for all images.
* If a list of strings, a random one of these will be sampled per
image and used as the mode.
* If StochasticParameter, a random mode will be sampled from this
parameter per image.
pad_cval : number or tuple of number list of number or \
imgaug.parameters.StochasticParameter, optional
The constant value to use if the pad mode is ``constant`` or the end
value to use if the mode is ``linear_ramp``.
See :func:`imgaug.imgaug.pad` for more details.
* If number, then that value will be used.
* If a tuple of two numbers and at least one of them is a float,
then a random number will be sampled from the continuous range
``a <= x <= b`` and used as the value. If both numbers are
integers, the range is discrete.
* If a list of number, then a random value will be chosen from the
elements of the list and used as the value.
* If StochasticParameter, a random value will be sampled from that
parameter per image.
keep_size : bool, optional
After padding, the result image will usually have a different
height/width compared to the original input image. If this parameter is
set to True, then the padded image will be resized to the input image's
size, i.e. the augmenter's output shape is always identical to the
input shape.
sample_independently : bool, optional
If False AND the values for `px`/`percent` result in exactly one
probability distribution for the amount to pad, only one single value
will be sampled from that probability distribution and used for all
sides. I.e. the pad amount then is the same for all sides.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Examples
--------
>>> aug = iaa.Pad(px=(0, 10))
pads each side by a random value from the range 0px to 10px (the value
is sampled per side). The added rows/columns are filled with black pixels.
>>> aug = iaa.Pad(px=(0, 10), sample_independently=False)
samples one value v from the discrete range ``[0..10]`` and pads all sides
by ``v`` pixels.
>>> aug = iaa.Pad(px=(0, 10), keep_size=False)
pads each side by a random value from the range 0px to 10px (the value
is sampled per side). After padding, the images are NOT resized to their
original size (i.e. the images may end up having different heights/widths).
>>> aug = iaa.Pad(px=((0, 10), (0, 5), (0, 10), (0, 5)))
pads the top and bottom by a random value from the range 0px to 10px
and the left and right by a random value in the range 0px to 5px.
>>> aug = iaa.Pad(percent=(0, 0.1))
pads each side by a random value from the range 0 percent to
10 percent. (Percent with respect to the side's size, e.g. for the
top side it uses the image's height.)
>>> aug = iaa.Pad(percent=([0.05, 0.1], [0.05, 0.1], [0.05, 0.1], [0.05, 0.1]))
pads each side by either 5 percent or 10 percent.
>>> aug = iaa.Pad(px=(0, 10), pad_mode="edge")
pads each side by a random value from the range 0px to 10px (the values
are sampled per side). The padding uses the ``edge`` mode from numpy's
pad function.
>>> aug = iaa.Pad(px=(0, 10), pad_mode=["constant", "edge"])
pads each side by a random value from the range 0px to 10px (the values
are sampled per side). The padding uses randomly either the ``constant``
or ``edge`` mode from numpy's pad function.
>>> aug = iaa.Pad(px=(0, 10), pad_mode=ia.ALL, pad_cval=(0, 255))
pads each side by a random value from the range 0px to 10px (the values
are sampled per side). It uses a random mode for numpy's pad function.
If the mode is ``constant`` or ``linear_ramp``, it samples a random value
``v`` from the range ``[0, 255]`` and uses that as the constant
value (``mode=constant``) or end value (``mode=linear_ramp``).
"""
def recursive_validate(v):
if v is None:
return v
elif ia.is_single_number(v):
ia.do_assert(v >= 0)
return v
elif isinstance(v, iap.StochasticParameter):
return v
elif isinstance(v, tuple):
return tuple([recursive_validate(v_) for v_ in v])
elif isinstance(v, list):
return [recursive_validate(v_) for v_ in v]
else:
raise Exception("Expected None or int or float or StochasticParameter or list or tuple, got %s." % (
type(v),))
px = recursive_validate(px)
percent = recursive_validate(percent)
if name is None:
name = "Unnamed%s" % (ia.caller_name(),)
aug = CropAndPad(
px=px, percent=percent,
pad_mode=pad_mode, pad_cval=pad_cval,
keep_size=keep_size, sample_independently=sample_independently,
name=name, deterministic=deterministic, random_state=random_state
)
return aug | [
"def",
"Pad",
"(",
"px",
"=",
"None",
",",
"percent",
"=",
"None",
",",
"pad_mode",
"=",
"\"constant\"",
",",
"pad_cval",
"=",
"0",
",",
"keep_size",
"=",
"True",
",",
"sample_independently",
"=",
"True",
",",
"name",
"=",
"None",
",",
"deterministic",
"=",
"False",
",",
"random_state",
"=",
"None",
")",
":",
"def",
"recursive_validate",
"(",
"v",
")",
":",
"if",
"v",
"is",
"None",
":",
"return",
"v",
"elif",
"ia",
".",
"is_single_number",
"(",
"v",
")",
":",
"ia",
".",
"do_assert",
"(",
"v",
">=",
"0",
")",
"return",
"v",
"elif",
"isinstance",
"(",
"v",
",",
"iap",
".",
"StochasticParameter",
")",
":",
"return",
"v",
"elif",
"isinstance",
"(",
"v",
",",
"tuple",
")",
":",
"return",
"tuple",
"(",
"[",
"recursive_validate",
"(",
"v_",
")",
"for",
"v_",
"in",
"v",
"]",
")",
"elif",
"isinstance",
"(",
"v",
",",
"list",
")",
":",
"return",
"[",
"recursive_validate",
"(",
"v_",
")",
"for",
"v_",
"in",
"v",
"]",
"else",
":",
"raise",
"Exception",
"(",
"\"Expected None or int or float or StochasticParameter or list or tuple, got %s.\"",
"%",
"(",
"type",
"(",
"v",
")",
",",
")",
")",
"px",
"=",
"recursive_validate",
"(",
"px",
")",
"percent",
"=",
"recursive_validate",
"(",
"percent",
")",
"if",
"name",
"is",
"None",
":",
"name",
"=",
"\"Unnamed%s\"",
"%",
"(",
"ia",
".",
"caller_name",
"(",
")",
",",
")",
"aug",
"=",
"CropAndPad",
"(",
"px",
"=",
"px",
",",
"percent",
"=",
"percent",
",",
"pad_mode",
"=",
"pad_mode",
",",
"pad_cval",
"=",
"pad_cval",
",",
"keep_size",
"=",
"keep_size",
",",
"sample_independently",
"=",
"sample_independently",
",",
"name",
"=",
"name",
",",
"deterministic",
"=",
"deterministic",
",",
"random_state",
"=",
"random_state",
")",
"return",
"aug"
] | Augmenter that pads images, i.e. adds columns/rows to them.
dtype support::
See ``imgaug.augmenters.size.CropAndPad``.
Parameters
----------
px : None or int or imgaug.parameters.StochasticParameter or tuple, optional
The number of pixels to pad on each side of the image.
Either this or the parameter `percent` may be set, not both at the same
time.
* If None, then pixel-based padding will not be used.
* If int, then that exact number of pixels will always be padded.
* If StochasticParameter, then that parameter will be used for each
image. Four samples will be drawn per image (top, right, bottom,
left).
* If a tuple of two ints with values a and b, then each side will
be padded by a random amount in the range ``a <= x <= b``.
``x`` is sampled per image side.
* If a tuple of four entries, then the entries represent top, right,
bottom, left. Each entry may be a single integer (always pad by
exactly that value), a tuple of two ints ``a`` and ``b`` (pad by
an amount ``a <= x <= b``), a list of ints (pad by a random value
that is contained in the list) or a StochasticParameter (sample
the amount to pad from that parameter).
percent : None or int or float or imgaug.parameters.StochasticParameter \
or tuple, optional
The number of pixels to pad on each side of the image given
*in percent* of the image height/width.
E.g. if this is set to 0.1, the augmenter will always add 10 percent
of the image's height to the top, 10 percent of the width to the right,
10 percent of the height at the bottom and 10 percent of the width to
the left. Either this or the parameter `px` may be set, not both at the
same time.
* If None, then percent-based padding will not be used.
* If int, then expected to be 0 (no padding).
* If float, then that percentage will always be padded.
* If StochasticParameter, then that parameter will be used for each
image. Four samples will be drawn per image (top, right, bottom,
left).
* If a tuple of two floats with values a and b, then each side will
be padded by a random percentage in the range ``a <= x <= b``.
``x`` is sampled per image side.
* If a tuple of four entries, then the entries represent top, right,
bottom, left. Each entry may be a single float (always pad by
exactly that percent value), a tuple of two floats ``a`` and ``b``
(pad by a percentage ``a <= x <= b``), a list of floats (pad by a
random value that is contained in the list) or a
StochasticParameter (sample the percentage to pad from that
parameter).
pad_mode : imgaug.ALL or str or list of str or \
imgaug.parameters.StochasticParameter, optional
Padding mode to use. The available modes match the numpy padding modes,
i.e. ``constant``, ``edge``, ``linear_ramp``, ``maximum``, ``median``,
``minimum``, ``reflect``, ``symmetric``, ``wrap``. The modes
``constant`` and ``linear_ramp`` use extra values, which are provided
by ``pad_cval`` when necessary. See :func:`imgaug.imgaug.pad` for
more details.
* If ``imgaug.ALL``, then a random mode from all available modes
will be sampled per image.
* If a string, it will be used as the pad mode for all images.
* If a list of strings, a random one of these will be sampled per
image and used as the mode.
* If StochasticParameter, a random mode will be sampled from this
parameter per image.
pad_cval : number or tuple of number list of number or \
imgaug.parameters.StochasticParameter, optional
The constant value to use if the pad mode is ``constant`` or the end
value to use if the mode is ``linear_ramp``.
See :func:`imgaug.imgaug.pad` for more details.
* If number, then that value will be used.
* If a tuple of two numbers and at least one of them is a float,
then a random number will be sampled from the continuous range
``a <= x <= b`` and used as the value. If both numbers are
integers, the range is discrete.
* If a list of number, then a random value will be chosen from the
elements of the list and used as the value.
* If StochasticParameter, a random value will be sampled from that
parameter per image.
keep_size : bool, optional
After padding, the result image will usually have a different
height/width compared to the original input image. If this parameter is
set to True, then the padded image will be resized to the input image's
size, i.e. the augmenter's output shape is always identical to the
input shape.
sample_independently : bool, optional
If False AND the values for `px`/`percent` result in exactly one
probability distribution for the amount to pad, only one single value
will be sampled from that probability distribution and used for all
sides. I.e. the pad amount then is the same for all sides.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Examples
--------
>>> aug = iaa.Pad(px=(0, 10))
pads each side by a random value from the range 0px to 10px (the value
is sampled per side). The added rows/columns are filled with black pixels.
>>> aug = iaa.Pad(px=(0, 10), sample_independently=False)
samples one value v from the discrete range ``[0..10]`` and pads all sides
by ``v`` pixels.
>>> aug = iaa.Pad(px=(0, 10), keep_size=False)
pads each side by a random value from the range 0px to 10px (the value
is sampled per side). After padding, the images are NOT resized to their
original size (i.e. the images may end up having different heights/widths).
>>> aug = iaa.Pad(px=((0, 10), (0, 5), (0, 10), (0, 5)))
pads the top and bottom by a random value from the range 0px to 10px
and the left and right by a random value in the range 0px to 5px.
>>> aug = iaa.Pad(percent=(0, 0.1))
pads each side by a random value from the range 0 percent to
10 percent. (Percent with respect to the side's size, e.g. for the
top side it uses the image's height.)
>>> aug = iaa.Pad(percent=([0.05, 0.1], [0.05, 0.1], [0.05, 0.1], [0.05, 0.1]))
pads each side by either 5 percent or 10 percent.
>>> aug = iaa.Pad(px=(0, 10), pad_mode="edge")
pads each side by a random value from the range 0px to 10px (the values
are sampled per side). The padding uses the ``edge`` mode from numpy's
pad function.
>>> aug = iaa.Pad(px=(0, 10), pad_mode=["constant", "edge"])
pads each side by a random value from the range 0px to 10px (the values
are sampled per side). The padding uses randomly either the ``constant``
or ``edge`` mode from numpy's pad function.
>>> aug = iaa.Pad(px=(0, 10), pad_mode=ia.ALL, pad_cval=(0, 255))
pads each side by a random value from the range 0px to 10px (the values
are sampled per side). It uses a random mode for numpy's pad function.
If the mode is ``constant`` or ``linear_ramp``, it samples a random value
``v`` from the range ``[0, 255]`` and uses that as the constant
value (``mode=constant``) or end value (``mode=linear_ramp``). | [
"Augmenter",
"that",
"pads",
"images",
"i",
".",
"e",
".",
"adds",
"columns",
"/",
"rows",
"to",
"them",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmenters/size.py#L958-L1154 | valid |
aleju/imgaug | imgaug/augmenters/size.py | Crop | def Crop(px=None, percent=None, keep_size=True, sample_independently=True,
name=None, deterministic=False, random_state=None):
"""
Augmenter that crops/cuts away pixels at the sides of the image.
That allows to cut out subimages from given (full) input images.
The number of pixels to cut off may be defined in absolute values or
percent of the image sizes.
dtype support::
See ``imgaug.augmenters.size.CropAndPad``.
Parameters
----------
px : None or int or imgaug.parameters.StochasticParameter or tuple, optional
The number of pixels to crop away (cut off) on each side of the image.
Either this or the parameter `percent` may be set, not both at the same
time.
* If None, then pixel-based cropping will not be used.
* If int, then that exact number of pixels will always be cropped.
* If StochasticParameter, then that parameter will be used for each
image. Four samples will be drawn per image (top, right, bottom,
left).
* If a tuple of two ints with values ``a`` and ``b``, then each
side will be cropped by a random amount in the range
``a <= x <= b``. ``x`` is sampled per image side.
* If a tuple of four entries, then the entries represent top, right,
bottom, left. Each entry may be a single integer (always crop by
exactly that value), a tuple of two ints ``a`` and ``b`` (crop by
an amount ``a <= x <= b``), a list of ints (crop by a random
value that is contained in the list) or a StochasticParameter
(sample the amount to crop from that parameter).
percent : None or int or float or imgaug.parameters.StochasticParameter \
or tuple, optional
The number of pixels to crop away (cut off) on each side of the image
given *in percent* of the image height/width.
E.g. if this is set to 0.1, the augmenter will always crop away
10 percent of the image's height at the top, 10 percent of the width
on the right, 10 percent of the height at the bottom and 10 percent
of the width on the left.
Either this or the parameter `px` may be set, not both at the same time.
* If None, then percent-based cropping will not be used.
* If int, then expected to be 0 (no cropping).
* If float, then that percentage will always be cropped away.
* If StochasticParameter, then that parameter will be used for each
image. Four samples will be drawn per image (top, right, bottom,
left).
* If a tuple of two floats with values ``a`` and ``b``, then each
side will be cropped by a random percentage in the range
``a <= x <= b``. ``x`` is sampled per image side.
* If a tuple of four entries, then the entries represent top, right,
bottom, left. Each entry may be a single float (always crop by
exactly that percent value), a tuple of two floats a and ``b``
(crop by a percentage ``a <= x <= b``), a list of floats (crop by
a random value that is contained in the list) or a
StochasticParameter (sample the percentage to crop from that
parameter).
keep_size : bool, optional
After cropping, the result image has a different height/width than
the input image. If this parameter is set to True, then the cropped
image will be resized to the input image's size, i.e. the image size
is then not changed by the augmenter.
sample_independently : bool, optional
If False AND the values for `px`/`percent` result in exactly one
probability distribution for the amount to crop, only one
single value will be sampled from that probability distribution
and used for all sides. I.e. the crop amount then is the same
for all sides.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Examples
--------
>>> aug = iaa.Crop(px=(0, 10))
crops each side by a random value from the range 0px to 10px (the value
is sampled per side).
>>> aug = iaa.Crop(px=(0, 10), sample_independently=False)
samples one value ``v`` from the discrete range ``[0..10]`` and crops all
sides by ``v`` pixels.
>>> aug = iaa.Crop(px=(0, 10), keep_size=False)
crops each side by a random value from the range 0px to 10px (the value
is sampled per side). After cropping, the images are NOT resized to their
original size (i.e. the images may end up having different heights/widths).
>>> aug = iaa.Crop(px=((0, 10), (0, 5), (0, 10), (0, 5)))
crops the top and bottom by a random value from the range 0px to 10px
and the left and right by a random value in the range 0px to 5px.
>>> aug = iaa.Crop(percent=(0, 0.1))
crops each side by a random value from the range 0 percent to
10 percent. (Percent with respect to the side's size, e.g. for the
top side it uses the image's height.)
>>> aug = iaa.Crop(percent=([0.05, 0.1], [0.05, 0.1], [0.05, 0.1], [0.05, 0.1]))
crops each side by either 5 percent or 10 percent.
"""
def recursive_negate(v):
if v is None:
return v
elif ia.is_single_number(v):
ia.do_assert(v >= 0)
return -v
elif isinstance(v, iap.StochasticParameter):
return iap.Multiply(v, -1)
elif isinstance(v, tuple):
return tuple([recursive_negate(v_) for v_ in v])
elif isinstance(v, list):
return [recursive_negate(v_) for v_ in v]
else:
raise Exception("Expected None or int or float or StochasticParameter or list or tuple, got %s." % (
type(v),))
px = recursive_negate(px)
percent = recursive_negate(percent)
if name is None:
name = "Unnamed%s" % (ia.caller_name(),)
aug = CropAndPad(
px=px, percent=percent,
keep_size=keep_size, sample_independently=sample_independently,
name=name, deterministic=deterministic, random_state=random_state
)
return aug | python | def Crop(px=None, percent=None, keep_size=True, sample_independently=True,
name=None, deterministic=False, random_state=None):
"""
Augmenter that crops/cuts away pixels at the sides of the image.
That allows to cut out subimages from given (full) input images.
The number of pixels to cut off may be defined in absolute values or
percent of the image sizes.
dtype support::
See ``imgaug.augmenters.size.CropAndPad``.
Parameters
----------
px : None or int or imgaug.parameters.StochasticParameter or tuple, optional
The number of pixels to crop away (cut off) on each side of the image.
Either this or the parameter `percent` may be set, not both at the same
time.
* If None, then pixel-based cropping will not be used.
* If int, then that exact number of pixels will always be cropped.
* If StochasticParameter, then that parameter will be used for each
image. Four samples will be drawn per image (top, right, bottom,
left).
* If a tuple of two ints with values ``a`` and ``b``, then each
side will be cropped by a random amount in the range
``a <= x <= b``. ``x`` is sampled per image side.
* If a tuple of four entries, then the entries represent top, right,
bottom, left. Each entry may be a single integer (always crop by
exactly that value), a tuple of two ints ``a`` and ``b`` (crop by
an amount ``a <= x <= b``), a list of ints (crop by a random
value that is contained in the list) or a StochasticParameter
(sample the amount to crop from that parameter).
percent : None or int or float or imgaug.parameters.StochasticParameter \
or tuple, optional
The number of pixels to crop away (cut off) on each side of the image
given *in percent* of the image height/width.
E.g. if this is set to 0.1, the augmenter will always crop away
10 percent of the image's height at the top, 10 percent of the width
on the right, 10 percent of the height at the bottom and 10 percent
of the width on the left.
Either this or the parameter `px` may be set, not both at the same time.
* If None, then percent-based cropping will not be used.
* If int, then expected to be 0 (no cropping).
* If float, then that percentage will always be cropped away.
* If StochasticParameter, then that parameter will be used for each
image. Four samples will be drawn per image (top, right, bottom,
left).
* If a tuple of two floats with values ``a`` and ``b``, then each
side will be cropped by a random percentage in the range
``a <= x <= b``. ``x`` is sampled per image side.
* If a tuple of four entries, then the entries represent top, right,
bottom, left. Each entry may be a single float (always crop by
exactly that percent value), a tuple of two floats a and ``b``
(crop by a percentage ``a <= x <= b``), a list of floats (crop by
a random value that is contained in the list) or a
StochasticParameter (sample the percentage to crop from that
parameter).
keep_size : bool, optional
After cropping, the result image has a different height/width than
the input image. If this parameter is set to True, then the cropped
image will be resized to the input image's size, i.e. the image size
is then not changed by the augmenter.
sample_independently : bool, optional
If False AND the values for `px`/`percent` result in exactly one
probability distribution for the amount to crop, only one
single value will be sampled from that probability distribution
and used for all sides. I.e. the crop amount then is the same
for all sides.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Examples
--------
>>> aug = iaa.Crop(px=(0, 10))
crops each side by a random value from the range 0px to 10px (the value
is sampled per side).
>>> aug = iaa.Crop(px=(0, 10), sample_independently=False)
samples one value ``v`` from the discrete range ``[0..10]`` and crops all
sides by ``v`` pixels.
>>> aug = iaa.Crop(px=(0, 10), keep_size=False)
crops each side by a random value from the range 0px to 10px (the value
is sampled per side). After cropping, the images are NOT resized to their
original size (i.e. the images may end up having different heights/widths).
>>> aug = iaa.Crop(px=((0, 10), (0, 5), (0, 10), (0, 5)))
crops the top and bottom by a random value from the range 0px to 10px
and the left and right by a random value in the range 0px to 5px.
>>> aug = iaa.Crop(percent=(0, 0.1))
crops each side by a random value from the range 0 percent to
10 percent. (Percent with respect to the side's size, e.g. for the
top side it uses the image's height.)
>>> aug = iaa.Crop(percent=([0.05, 0.1], [0.05, 0.1], [0.05, 0.1], [0.05, 0.1]))
crops each side by either 5 percent or 10 percent.
"""
def recursive_negate(v):
if v is None:
return v
elif ia.is_single_number(v):
ia.do_assert(v >= 0)
return -v
elif isinstance(v, iap.StochasticParameter):
return iap.Multiply(v, -1)
elif isinstance(v, tuple):
return tuple([recursive_negate(v_) for v_ in v])
elif isinstance(v, list):
return [recursive_negate(v_) for v_ in v]
else:
raise Exception("Expected None or int or float or StochasticParameter or list or tuple, got %s." % (
type(v),))
px = recursive_negate(px)
percent = recursive_negate(percent)
if name is None:
name = "Unnamed%s" % (ia.caller_name(),)
aug = CropAndPad(
px=px, percent=percent,
keep_size=keep_size, sample_independently=sample_independently,
name=name, deterministic=deterministic, random_state=random_state
)
return aug | [
"def",
"Crop",
"(",
"px",
"=",
"None",
",",
"percent",
"=",
"None",
",",
"keep_size",
"=",
"True",
",",
"sample_independently",
"=",
"True",
",",
"name",
"=",
"None",
",",
"deterministic",
"=",
"False",
",",
"random_state",
"=",
"None",
")",
":",
"def",
"recursive_negate",
"(",
"v",
")",
":",
"if",
"v",
"is",
"None",
":",
"return",
"v",
"elif",
"ia",
".",
"is_single_number",
"(",
"v",
")",
":",
"ia",
".",
"do_assert",
"(",
"v",
">=",
"0",
")",
"return",
"-",
"v",
"elif",
"isinstance",
"(",
"v",
",",
"iap",
".",
"StochasticParameter",
")",
":",
"return",
"iap",
".",
"Multiply",
"(",
"v",
",",
"-",
"1",
")",
"elif",
"isinstance",
"(",
"v",
",",
"tuple",
")",
":",
"return",
"tuple",
"(",
"[",
"recursive_negate",
"(",
"v_",
")",
"for",
"v_",
"in",
"v",
"]",
")",
"elif",
"isinstance",
"(",
"v",
",",
"list",
")",
":",
"return",
"[",
"recursive_negate",
"(",
"v_",
")",
"for",
"v_",
"in",
"v",
"]",
"else",
":",
"raise",
"Exception",
"(",
"\"Expected None or int or float or StochasticParameter or list or tuple, got %s.\"",
"%",
"(",
"type",
"(",
"v",
")",
",",
")",
")",
"px",
"=",
"recursive_negate",
"(",
"px",
")",
"percent",
"=",
"recursive_negate",
"(",
"percent",
")",
"if",
"name",
"is",
"None",
":",
"name",
"=",
"\"Unnamed%s\"",
"%",
"(",
"ia",
".",
"caller_name",
"(",
")",
",",
")",
"aug",
"=",
"CropAndPad",
"(",
"px",
"=",
"px",
",",
"percent",
"=",
"percent",
",",
"keep_size",
"=",
"keep_size",
",",
"sample_independently",
"=",
"sample_independently",
",",
"name",
"=",
"name",
",",
"deterministic",
"=",
"deterministic",
",",
"random_state",
"=",
"random_state",
")",
"return",
"aug"
] | Augmenter that crops/cuts away pixels at the sides of the image.
That allows to cut out subimages from given (full) input images.
The number of pixels to cut off may be defined in absolute values or
percent of the image sizes.
dtype support::
See ``imgaug.augmenters.size.CropAndPad``.
Parameters
----------
px : None or int or imgaug.parameters.StochasticParameter or tuple, optional
The number of pixels to crop away (cut off) on each side of the image.
Either this or the parameter `percent` may be set, not both at the same
time.
* If None, then pixel-based cropping will not be used.
* If int, then that exact number of pixels will always be cropped.
* If StochasticParameter, then that parameter will be used for each
image. Four samples will be drawn per image (top, right, bottom,
left).
* If a tuple of two ints with values ``a`` and ``b``, then each
side will be cropped by a random amount in the range
``a <= x <= b``. ``x`` is sampled per image side.
* If a tuple of four entries, then the entries represent top, right,
bottom, left. Each entry may be a single integer (always crop by
exactly that value), a tuple of two ints ``a`` and ``b`` (crop by
an amount ``a <= x <= b``), a list of ints (crop by a random
value that is contained in the list) or a StochasticParameter
(sample the amount to crop from that parameter).
percent : None or int or float or imgaug.parameters.StochasticParameter \
or tuple, optional
The number of pixels to crop away (cut off) on each side of the image
given *in percent* of the image height/width.
E.g. if this is set to 0.1, the augmenter will always crop away
10 percent of the image's height at the top, 10 percent of the width
on the right, 10 percent of the height at the bottom and 10 percent
of the width on the left.
Either this or the parameter `px` may be set, not both at the same time.
* If None, then percent-based cropping will not be used.
* If int, then expected to be 0 (no cropping).
* If float, then that percentage will always be cropped away.
* If StochasticParameter, then that parameter will be used for each
image. Four samples will be drawn per image (top, right, bottom,
left).
* If a tuple of two floats with values ``a`` and ``b``, then each
side will be cropped by a random percentage in the range
``a <= x <= b``. ``x`` is sampled per image side.
* If a tuple of four entries, then the entries represent top, right,
bottom, left. Each entry may be a single float (always crop by
exactly that percent value), a tuple of two floats a and ``b``
(crop by a percentage ``a <= x <= b``), a list of floats (crop by
a random value that is contained in the list) or a
StochasticParameter (sample the percentage to crop from that
parameter).
keep_size : bool, optional
After cropping, the result image has a different height/width than
the input image. If this parameter is set to True, then the cropped
image will be resized to the input image's size, i.e. the image size
is then not changed by the augmenter.
sample_independently : bool, optional
If False AND the values for `px`/`percent` result in exactly one
probability distribution for the amount to crop, only one
single value will be sampled from that probability distribution
and used for all sides. I.e. the crop amount then is the same
for all sides.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Examples
--------
>>> aug = iaa.Crop(px=(0, 10))
crops each side by a random value from the range 0px to 10px (the value
is sampled per side).
>>> aug = iaa.Crop(px=(0, 10), sample_independently=False)
samples one value ``v`` from the discrete range ``[0..10]`` and crops all
sides by ``v`` pixels.
>>> aug = iaa.Crop(px=(0, 10), keep_size=False)
crops each side by a random value from the range 0px to 10px (the value
is sampled per side). After cropping, the images are NOT resized to their
original size (i.e. the images may end up having different heights/widths).
>>> aug = iaa.Crop(px=((0, 10), (0, 5), (0, 10), (0, 5)))
crops the top and bottom by a random value from the range 0px to 10px
and the left and right by a random value in the range 0px to 5px.
>>> aug = iaa.Crop(percent=(0, 0.1))
crops each side by a random value from the range 0 percent to
10 percent. (Percent with respect to the side's size, e.g. for the
top side it uses the image's height.)
>>> aug = iaa.Crop(percent=([0.05, 0.1], [0.05, 0.1], [0.05, 0.1], [0.05, 0.1]))
crops each side by either 5 percent or 10 percent. | [
"Augmenter",
"that",
"crops",
"/",
"cuts",
"away",
"pixels",
"at",
"the",
"sides",
"of",
"the",
"image",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmenters/size.py#L1157-L1303 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | isect_segments__naive | def isect_segments__naive(segments):
"""
Brute force O(n2) version of ``isect_segments`` for test validation.
"""
isect = []
# order points left -> right
if Real is float:
segments = [
(s[0], s[1]) if s[0][X] <= s[1][X] else
(s[1], s[0])
for s in segments]
else:
segments = [
(
(Real(s[0][0]), Real(s[0][1])),
(Real(s[1][0]), Real(s[1][1])),
) if (s[0] <= s[1]) else
(
(Real(s[1][0]), Real(s[1][1])),
(Real(s[0][0]), Real(s[0][1])),
)
for s in segments]
n = len(segments)
for i in range(n):
a0, a1 = segments[i]
for j in range(i + 1, n):
b0, b1 = segments[j]
if a0 not in (b0, b1) and a1 not in (b0, b1):
ix = isect_seg_seg_v2_point(a0, a1, b0, b1)
if ix is not None:
# USE_IGNORE_SEGMENT_ENDINGS handled already
isect.append(ix)
return isect | python | def isect_segments__naive(segments):
"""
Brute force O(n2) version of ``isect_segments`` for test validation.
"""
isect = []
# order points left -> right
if Real is float:
segments = [
(s[0], s[1]) if s[0][X] <= s[1][X] else
(s[1], s[0])
for s in segments]
else:
segments = [
(
(Real(s[0][0]), Real(s[0][1])),
(Real(s[1][0]), Real(s[1][1])),
) if (s[0] <= s[1]) else
(
(Real(s[1][0]), Real(s[1][1])),
(Real(s[0][0]), Real(s[0][1])),
)
for s in segments]
n = len(segments)
for i in range(n):
a0, a1 = segments[i]
for j in range(i + 1, n):
b0, b1 = segments[j]
if a0 not in (b0, b1) and a1 not in (b0, b1):
ix = isect_seg_seg_v2_point(a0, a1, b0, b1)
if ix is not None:
# USE_IGNORE_SEGMENT_ENDINGS handled already
isect.append(ix)
return isect | [
"def",
"isect_segments__naive",
"(",
"segments",
")",
":",
"isect",
"=",
"[",
"]",
"# order points left -> right",
"if",
"Real",
"is",
"float",
":",
"segments",
"=",
"[",
"(",
"s",
"[",
"0",
"]",
",",
"s",
"[",
"1",
"]",
")",
"if",
"s",
"[",
"0",
"]",
"[",
"X",
"]",
"<=",
"s",
"[",
"1",
"]",
"[",
"X",
"]",
"else",
"(",
"s",
"[",
"1",
"]",
",",
"s",
"[",
"0",
"]",
")",
"for",
"s",
"in",
"segments",
"]",
"else",
":",
"segments",
"=",
"[",
"(",
"(",
"Real",
"(",
"s",
"[",
"0",
"]",
"[",
"0",
"]",
")",
",",
"Real",
"(",
"s",
"[",
"0",
"]",
"[",
"1",
"]",
")",
")",
",",
"(",
"Real",
"(",
"s",
"[",
"1",
"]",
"[",
"0",
"]",
")",
",",
"Real",
"(",
"s",
"[",
"1",
"]",
"[",
"1",
"]",
")",
")",
",",
")",
"if",
"(",
"s",
"[",
"0",
"]",
"<=",
"s",
"[",
"1",
"]",
")",
"else",
"(",
"(",
"Real",
"(",
"s",
"[",
"1",
"]",
"[",
"0",
"]",
")",
",",
"Real",
"(",
"s",
"[",
"1",
"]",
"[",
"1",
"]",
")",
")",
",",
"(",
"Real",
"(",
"s",
"[",
"0",
"]",
"[",
"0",
"]",
")",
",",
"Real",
"(",
"s",
"[",
"0",
"]",
"[",
"1",
"]",
")",
")",
",",
")",
"for",
"s",
"in",
"segments",
"]",
"n",
"=",
"len",
"(",
"segments",
")",
"for",
"i",
"in",
"range",
"(",
"n",
")",
":",
"a0",
",",
"a1",
"=",
"segments",
"[",
"i",
"]",
"for",
"j",
"in",
"range",
"(",
"i",
"+",
"1",
",",
"n",
")",
":",
"b0",
",",
"b1",
"=",
"segments",
"[",
"j",
"]",
"if",
"a0",
"not",
"in",
"(",
"b0",
",",
"b1",
")",
"and",
"a1",
"not",
"in",
"(",
"b0",
",",
"b1",
")",
":",
"ix",
"=",
"isect_seg_seg_v2_point",
"(",
"a0",
",",
"a1",
",",
"b0",
",",
"b1",
")",
"if",
"ix",
"is",
"not",
"None",
":",
"# USE_IGNORE_SEGMENT_ENDINGS handled already",
"isect",
".",
"append",
"(",
"ix",
")",
"return",
"isect"
] | Brute force O(n2) version of ``isect_segments`` for test validation. | [
"Brute",
"force",
"O",
"(",
"n2",
")",
"version",
"of",
"isect_segments",
"for",
"test",
"validation",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L685-L721 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | isect_polygon__naive | def isect_polygon__naive(points):
"""
Brute force O(n2) version of ``isect_polygon`` for test validation.
"""
isect = []
n = len(points)
if Real is float:
pass
else:
points = [(Real(p[0]), Real(p[1])) for p in points]
for i in range(n):
a0, a1 = points[i], points[(i + 1) % n]
for j in range(i + 1, n):
b0, b1 = points[j], points[(j + 1) % n]
if a0 not in (b0, b1) and a1 not in (b0, b1):
ix = isect_seg_seg_v2_point(a0, a1, b0, b1)
if ix is not None:
if USE_IGNORE_SEGMENT_ENDINGS:
if ((len_squared_v2v2(ix, a0) < NUM_EPS_SQ or
len_squared_v2v2(ix, a1) < NUM_EPS_SQ) and
(len_squared_v2v2(ix, b0) < NUM_EPS_SQ or
len_squared_v2v2(ix, b1) < NUM_EPS_SQ)):
continue
isect.append(ix)
return isect | python | def isect_polygon__naive(points):
"""
Brute force O(n2) version of ``isect_polygon`` for test validation.
"""
isect = []
n = len(points)
if Real is float:
pass
else:
points = [(Real(p[0]), Real(p[1])) for p in points]
for i in range(n):
a0, a1 = points[i], points[(i + 1) % n]
for j in range(i + 1, n):
b0, b1 = points[j], points[(j + 1) % n]
if a0 not in (b0, b1) and a1 not in (b0, b1):
ix = isect_seg_seg_v2_point(a0, a1, b0, b1)
if ix is not None:
if USE_IGNORE_SEGMENT_ENDINGS:
if ((len_squared_v2v2(ix, a0) < NUM_EPS_SQ or
len_squared_v2v2(ix, a1) < NUM_EPS_SQ) and
(len_squared_v2v2(ix, b0) < NUM_EPS_SQ or
len_squared_v2v2(ix, b1) < NUM_EPS_SQ)):
continue
isect.append(ix)
return isect | [
"def",
"isect_polygon__naive",
"(",
"points",
")",
":",
"isect",
"=",
"[",
"]",
"n",
"=",
"len",
"(",
"points",
")",
"if",
"Real",
"is",
"float",
":",
"pass",
"else",
":",
"points",
"=",
"[",
"(",
"Real",
"(",
"p",
"[",
"0",
"]",
")",
",",
"Real",
"(",
"p",
"[",
"1",
"]",
")",
")",
"for",
"p",
"in",
"points",
"]",
"for",
"i",
"in",
"range",
"(",
"n",
")",
":",
"a0",
",",
"a1",
"=",
"points",
"[",
"i",
"]",
",",
"points",
"[",
"(",
"i",
"+",
"1",
")",
"%",
"n",
"]",
"for",
"j",
"in",
"range",
"(",
"i",
"+",
"1",
",",
"n",
")",
":",
"b0",
",",
"b1",
"=",
"points",
"[",
"j",
"]",
",",
"points",
"[",
"(",
"j",
"+",
"1",
")",
"%",
"n",
"]",
"if",
"a0",
"not",
"in",
"(",
"b0",
",",
"b1",
")",
"and",
"a1",
"not",
"in",
"(",
"b0",
",",
"b1",
")",
":",
"ix",
"=",
"isect_seg_seg_v2_point",
"(",
"a0",
",",
"a1",
",",
"b0",
",",
"b1",
")",
"if",
"ix",
"is",
"not",
"None",
":",
"if",
"USE_IGNORE_SEGMENT_ENDINGS",
":",
"if",
"(",
"(",
"len_squared_v2v2",
"(",
"ix",
",",
"a0",
")",
"<",
"NUM_EPS_SQ",
"or",
"len_squared_v2v2",
"(",
"ix",
",",
"a1",
")",
"<",
"NUM_EPS_SQ",
")",
"and",
"(",
"len_squared_v2v2",
"(",
"ix",
",",
"b0",
")",
"<",
"NUM_EPS_SQ",
"or",
"len_squared_v2v2",
"(",
"ix",
",",
"b1",
")",
"<",
"NUM_EPS_SQ",
")",
")",
":",
"continue",
"isect",
".",
"append",
"(",
"ix",
")",
"return",
"isect"
] | Brute force O(n2) version of ``isect_polygon`` for test validation. | [
"Brute",
"force",
"O",
"(",
"n2",
")",
"version",
"of",
"isect_polygon",
"for",
"test",
"validation",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L724-L755 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | SweepLine.get_intersections | def get_intersections(self):
"""
Return a list of unordered intersection points.
"""
if Real is float:
return list(self.intersections.keys())
else:
return [(float(p[0]), float(p[1])) for p in self.intersections.keys()] | python | def get_intersections(self):
"""
Return a list of unordered intersection points.
"""
if Real is float:
return list(self.intersections.keys())
else:
return [(float(p[0]), float(p[1])) for p in self.intersections.keys()] | [
"def",
"get_intersections",
"(",
"self",
")",
":",
"if",
"Real",
"is",
"float",
":",
"return",
"list",
"(",
"self",
".",
"intersections",
".",
"keys",
"(",
")",
")",
"else",
":",
"return",
"[",
"(",
"float",
"(",
"p",
"[",
"0",
"]",
")",
",",
"float",
"(",
"p",
"[",
"1",
"]",
")",
")",
"for",
"p",
"in",
"self",
".",
"intersections",
".",
"keys",
"(",
")",
"]"
] | Return a list of unordered intersection points. | [
"Return",
"a",
"list",
"of",
"unordered",
"intersection",
"points",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L237-L244 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | SweepLine.get_intersections_with_segments | def get_intersections_with_segments(self):
"""
Return a list of unordered intersection '(point, segment)' pairs,
where segments may contain 2 or more values.
"""
if Real is float:
return [
(p, [event.segment for event in event_set])
for p, event_set in self.intersections.items()
]
else:
return [
(
(float(p[0]), float(p[1])),
[((float(event.segment[0][0]), float(event.segment[0][1])),
(float(event.segment[1][0]), float(event.segment[1][1])))
for event in event_set],
)
for p, event_set in self.intersections.items()
] | python | def get_intersections_with_segments(self):
"""
Return a list of unordered intersection '(point, segment)' pairs,
where segments may contain 2 or more values.
"""
if Real is float:
return [
(p, [event.segment for event in event_set])
for p, event_set in self.intersections.items()
]
else:
return [
(
(float(p[0]), float(p[1])),
[((float(event.segment[0][0]), float(event.segment[0][1])),
(float(event.segment[1][0]), float(event.segment[1][1])))
for event in event_set],
)
for p, event_set in self.intersections.items()
] | [
"def",
"get_intersections_with_segments",
"(",
"self",
")",
":",
"if",
"Real",
"is",
"float",
":",
"return",
"[",
"(",
"p",
",",
"[",
"event",
".",
"segment",
"for",
"event",
"in",
"event_set",
"]",
")",
"for",
"p",
",",
"event_set",
"in",
"self",
".",
"intersections",
".",
"items",
"(",
")",
"]",
"else",
":",
"return",
"[",
"(",
"(",
"float",
"(",
"p",
"[",
"0",
"]",
")",
",",
"float",
"(",
"p",
"[",
"1",
"]",
")",
")",
",",
"[",
"(",
"(",
"float",
"(",
"event",
".",
"segment",
"[",
"0",
"]",
"[",
"0",
"]",
")",
",",
"float",
"(",
"event",
".",
"segment",
"[",
"0",
"]",
"[",
"1",
"]",
")",
")",
",",
"(",
"float",
"(",
"event",
".",
"segment",
"[",
"1",
"]",
"[",
"0",
"]",
")",
",",
"float",
"(",
"event",
".",
"segment",
"[",
"1",
"]",
"[",
"1",
"]",
")",
")",
")",
"for",
"event",
"in",
"event_set",
"]",
",",
")",
"for",
"p",
",",
"event_set",
"in",
"self",
".",
"intersections",
".",
"items",
"(",
")",
"]"
] | Return a list of unordered intersection '(point, segment)' pairs,
where segments may contain 2 or more values. | [
"Return",
"a",
"list",
"of",
"unordered",
"intersection",
"(",
"point",
"segment",
")",
"pairs",
"where",
"segments",
"may",
"contain",
"2",
"or",
"more",
"values",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L247-L266 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | EventQueue.poll | def poll(self):
"""
Get, and remove, the first (lowest) item from this queue.
:return: the first (lowest) item from this queue.
:rtype: Point, Event pair.
"""
assert(len(self.events_scan) != 0)
p, events_current = self.events_scan.pop_min()
return p, events_current | python | def poll(self):
"""
Get, and remove, the first (lowest) item from this queue.
:return: the first (lowest) item from this queue.
:rtype: Point, Event pair.
"""
assert(len(self.events_scan) != 0)
p, events_current = self.events_scan.pop_min()
return p, events_current | [
"def",
"poll",
"(",
"self",
")",
":",
"assert",
"(",
"len",
"(",
"self",
".",
"events_scan",
")",
"!=",
"0",
")",
"p",
",",
"events_current",
"=",
"self",
".",
"events_scan",
".",
"pop_min",
"(",
")",
"return",
"p",
",",
"events_current"
] | Get, and remove, the first (lowest) item from this queue.
:return: the first (lowest) item from this queue.
:rtype: Point, Event pair. | [
"Get",
"and",
"remove",
"the",
"first",
"(",
"lowest",
")",
"item",
"from",
"this",
"queue",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L532-L541 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | _ABCTree.clear | def clear(self):
"""T.clear() -> None. Remove all items from T."""
def _clear(node):
if node is not None:
_clear(node.left)
_clear(node.right)
node.free()
_clear(self._root)
self._count = 0
self._root = None | python | def clear(self):
"""T.clear() -> None. Remove all items from T."""
def _clear(node):
if node is not None:
_clear(node.left)
_clear(node.right)
node.free()
_clear(self._root)
self._count = 0
self._root = None | [
"def",
"clear",
"(",
"self",
")",
":",
"def",
"_clear",
"(",
"node",
")",
":",
"if",
"node",
"is",
"not",
"None",
":",
"_clear",
"(",
"node",
".",
"left",
")",
"_clear",
"(",
"node",
".",
"right",
")",
"node",
".",
"free",
"(",
")",
"_clear",
"(",
"self",
".",
"_root",
")",
"self",
".",
"_count",
"=",
"0",
"self",
".",
"_root",
"=",
"None"
] | T.clear() -> None. Remove all items from T. | [
"T",
".",
"clear",
"()",
"-",
">",
"None",
".",
"Remove",
"all",
"items",
"from",
"T",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L796-L805 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | _ABCTree.pop_item | def pop_item(self):
"""T.pop_item() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if T is empty.
"""
if self.is_empty():
raise KeyError("pop_item(): tree is empty")
node = self._root
while True:
if node.left is not None:
node = node.left
elif node.right is not None:
node = node.right
else:
break
key = node.key
value = node.value
self.remove(key)
return key, value | python | def pop_item(self):
"""T.pop_item() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if T is empty.
"""
if self.is_empty():
raise KeyError("pop_item(): tree is empty")
node = self._root
while True:
if node.left is not None:
node = node.left
elif node.right is not None:
node = node.right
else:
break
key = node.key
value = node.value
self.remove(key)
return key, value | [
"def",
"pop_item",
"(",
"self",
")",
":",
"if",
"self",
".",
"is_empty",
"(",
")",
":",
"raise",
"KeyError",
"(",
"\"pop_item(): tree is empty\"",
")",
"node",
"=",
"self",
".",
"_root",
"while",
"True",
":",
"if",
"node",
".",
"left",
"is",
"not",
"None",
":",
"node",
"=",
"node",
".",
"left",
"elif",
"node",
".",
"right",
"is",
"not",
"None",
":",
"node",
"=",
"node",
".",
"right",
"else",
":",
"break",
"key",
"=",
"node",
".",
"key",
"value",
"=",
"node",
".",
"value",
"self",
".",
"remove",
"(",
"key",
")",
"return",
"key",
",",
"value"
] | T.pop_item() -> (k, v), remove and return some (key, value) pair as a
2-tuple; but raise KeyError if T is empty. | [
"T",
".",
"pop_item",
"()",
"-",
">",
"(",
"k",
"v",
")",
"remove",
"and",
"return",
"some",
"(",
"key",
"value",
")",
"pair",
"as",
"a",
"2",
"-",
"tuple",
";",
"but",
"raise",
"KeyError",
"if",
"T",
"is",
"empty",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L824-L841 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | _ABCTree.min_item | def min_item(self):
"""Get item with min key of tree, raises ValueError if tree is empty."""
if self.is_empty():
raise ValueError("Tree is empty")
node = self._root
while node.left is not None:
node = node.left
return node.key, node.value | python | def min_item(self):
"""Get item with min key of tree, raises ValueError if tree is empty."""
if self.is_empty():
raise ValueError("Tree is empty")
node = self._root
while node.left is not None:
node = node.left
return node.key, node.value | [
"def",
"min_item",
"(",
"self",
")",
":",
"if",
"self",
".",
"is_empty",
"(",
")",
":",
"raise",
"ValueError",
"(",
"\"Tree is empty\"",
")",
"node",
"=",
"self",
".",
"_root",
"while",
"node",
".",
"left",
"is",
"not",
"None",
":",
"node",
"=",
"node",
".",
"left",
"return",
"node",
".",
"key",
",",
"node",
".",
"value"
] | Get item with min key of tree, raises ValueError if tree is empty. | [
"Get",
"item",
"with",
"min",
"key",
"of",
"tree",
"raises",
"ValueError",
"if",
"tree",
"is",
"empty",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L844-L851 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | _ABCTree.max_item | def max_item(self):
"""Get item with max key of tree, raises ValueError if tree is empty."""
if self.is_empty():
raise ValueError("Tree is empty")
node = self._root
while node.right is not None:
node = node.right
return node.key, node.value | python | def max_item(self):
"""Get item with max key of tree, raises ValueError if tree is empty."""
if self.is_empty():
raise ValueError("Tree is empty")
node = self._root
while node.right is not None:
node = node.right
return node.key, node.value | [
"def",
"max_item",
"(",
"self",
")",
":",
"if",
"self",
".",
"is_empty",
"(",
")",
":",
"raise",
"ValueError",
"(",
"\"Tree is empty\"",
")",
"node",
"=",
"self",
".",
"_root",
"while",
"node",
".",
"right",
"is",
"not",
"None",
":",
"node",
"=",
"node",
".",
"right",
"return",
"node",
".",
"key",
",",
"node",
".",
"value"
] | Get item with max key of tree, raises ValueError if tree is empty. | [
"Get",
"item",
"with",
"max",
"key",
"of",
"tree",
"raises",
"ValueError",
"if",
"tree",
"is",
"empty",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L853-L860 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | _ABCTree.succ_item | def succ_item(self, key, default=_sentinel):
"""Get successor (k,v) pair of key, raises KeyError if key is max key
or key does not exist. optimized for pypy.
"""
# removed graingets version, because it was little slower on CPython and much slower on pypy
# this version runs about 4x faster with pypy than the Cython version
# Note: Code sharing of succ_item() and ceiling_item() is possible, but has always a speed penalty.
node = self._root
succ_node = None
while node is not None:
cmp = self._cmp(self._cmp_data, key, node.key)
if cmp == 0:
break
elif cmp < 0:
if (succ_node is None) or self._cmp(self._cmp_data, node.key, succ_node.key) < 0:
succ_node = node
node = node.left
else:
node = node.right
if node is None: # stay at dead end
if default is _sentinel:
raise KeyError(str(key))
return default
# found node of key
if node.right is not None:
# find smallest node of right subtree
node = node.right
while node.left is not None:
node = node.left
if succ_node is None:
succ_node = node
elif self._cmp(self._cmp_data, node.key, succ_node.key) < 0:
succ_node = node
elif succ_node is None: # given key is biggest in tree
if default is _sentinel:
raise KeyError(str(key))
return default
return succ_node.key, succ_node.value | python | def succ_item(self, key, default=_sentinel):
"""Get successor (k,v) pair of key, raises KeyError if key is max key
or key does not exist. optimized for pypy.
"""
# removed graingets version, because it was little slower on CPython and much slower on pypy
# this version runs about 4x faster with pypy than the Cython version
# Note: Code sharing of succ_item() and ceiling_item() is possible, but has always a speed penalty.
node = self._root
succ_node = None
while node is not None:
cmp = self._cmp(self._cmp_data, key, node.key)
if cmp == 0:
break
elif cmp < 0:
if (succ_node is None) or self._cmp(self._cmp_data, node.key, succ_node.key) < 0:
succ_node = node
node = node.left
else:
node = node.right
if node is None: # stay at dead end
if default is _sentinel:
raise KeyError(str(key))
return default
# found node of key
if node.right is not None:
# find smallest node of right subtree
node = node.right
while node.left is not None:
node = node.left
if succ_node is None:
succ_node = node
elif self._cmp(self._cmp_data, node.key, succ_node.key) < 0:
succ_node = node
elif succ_node is None: # given key is biggest in tree
if default is _sentinel:
raise KeyError(str(key))
return default
return succ_node.key, succ_node.value | [
"def",
"succ_item",
"(",
"self",
",",
"key",
",",
"default",
"=",
"_sentinel",
")",
":",
"# removed graingets version, because it was little slower on CPython and much slower on pypy",
"# this version runs about 4x faster with pypy than the Cython version",
"# Note: Code sharing of succ_item() and ceiling_item() is possible, but has always a speed penalty.",
"node",
"=",
"self",
".",
"_root",
"succ_node",
"=",
"None",
"while",
"node",
"is",
"not",
"None",
":",
"cmp",
"=",
"self",
".",
"_cmp",
"(",
"self",
".",
"_cmp_data",
",",
"key",
",",
"node",
".",
"key",
")",
"if",
"cmp",
"==",
"0",
":",
"break",
"elif",
"cmp",
"<",
"0",
":",
"if",
"(",
"succ_node",
"is",
"None",
")",
"or",
"self",
".",
"_cmp",
"(",
"self",
".",
"_cmp_data",
",",
"node",
".",
"key",
",",
"succ_node",
".",
"key",
")",
"<",
"0",
":",
"succ_node",
"=",
"node",
"node",
"=",
"node",
".",
"left",
"else",
":",
"node",
"=",
"node",
".",
"right",
"if",
"node",
"is",
"None",
":",
"# stay at dead end",
"if",
"default",
"is",
"_sentinel",
":",
"raise",
"KeyError",
"(",
"str",
"(",
"key",
")",
")",
"return",
"default",
"# found node of key",
"if",
"node",
".",
"right",
"is",
"not",
"None",
":",
"# find smallest node of right subtree",
"node",
"=",
"node",
".",
"right",
"while",
"node",
".",
"left",
"is",
"not",
"None",
":",
"node",
"=",
"node",
".",
"left",
"if",
"succ_node",
"is",
"None",
":",
"succ_node",
"=",
"node",
"elif",
"self",
".",
"_cmp",
"(",
"self",
".",
"_cmp_data",
",",
"node",
".",
"key",
",",
"succ_node",
".",
"key",
")",
"<",
"0",
":",
"succ_node",
"=",
"node",
"elif",
"succ_node",
"is",
"None",
":",
"# given key is biggest in tree",
"if",
"default",
"is",
"_sentinel",
":",
"raise",
"KeyError",
"(",
"str",
"(",
"key",
")",
")",
"return",
"default",
"return",
"succ_node",
".",
"key",
",",
"succ_node",
".",
"value"
] | Get successor (k,v) pair of key, raises KeyError if key is max key
or key does not exist. optimized for pypy. | [
"Get",
"successor",
"(",
"k",
"v",
")",
"pair",
"of",
"key",
"raises",
"KeyError",
"if",
"key",
"is",
"max",
"key",
"or",
"key",
"does",
"not",
"exist",
".",
"optimized",
"for",
"pypy",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L862-L900 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | _ABCTree.prev_item | def prev_item(self, key, default=_sentinel):
"""Get predecessor (k,v) pair of key, raises KeyError if key is min key
or key does not exist. optimized for pypy.
"""
# removed graingets version, because it was little slower on CPython and much slower on pypy
# this version runs about 4x faster with pypy than the Cython version
# Note: Code sharing of prev_item() and floor_item() is possible, but has always a speed penalty.
node = self._root
prev_node = None
while node is not None:
cmp = self._cmp(self._cmp_data, key, node.key)
if cmp == 0:
break
elif cmp < 0:
node = node.left
else:
if (prev_node is None) or self._cmp(self._cmp_data, prev_node.key, node.key) < 0:
prev_node = node
node = node.right
if node is None: # stay at dead end (None)
if default is _sentinel:
raise KeyError(str(key))
return default
# found node of key
if node.left is not None:
# find biggest node of left subtree
node = node.left
while node.right is not None:
node = node.right
if prev_node is None:
prev_node = node
elif self._cmp(self._cmp_data, prev_node.key, node.key) < 0:
prev_node = node
elif prev_node is None: # given key is smallest in tree
if default is _sentinel:
raise KeyError(str(key))
return default
return prev_node.key, prev_node.value | python | def prev_item(self, key, default=_sentinel):
"""Get predecessor (k,v) pair of key, raises KeyError if key is min key
or key does not exist. optimized for pypy.
"""
# removed graingets version, because it was little slower on CPython and much slower on pypy
# this version runs about 4x faster with pypy than the Cython version
# Note: Code sharing of prev_item() and floor_item() is possible, but has always a speed penalty.
node = self._root
prev_node = None
while node is not None:
cmp = self._cmp(self._cmp_data, key, node.key)
if cmp == 0:
break
elif cmp < 0:
node = node.left
else:
if (prev_node is None) or self._cmp(self._cmp_data, prev_node.key, node.key) < 0:
prev_node = node
node = node.right
if node is None: # stay at dead end (None)
if default is _sentinel:
raise KeyError(str(key))
return default
# found node of key
if node.left is not None:
# find biggest node of left subtree
node = node.left
while node.right is not None:
node = node.right
if prev_node is None:
prev_node = node
elif self._cmp(self._cmp_data, prev_node.key, node.key) < 0:
prev_node = node
elif prev_node is None: # given key is smallest in tree
if default is _sentinel:
raise KeyError(str(key))
return default
return prev_node.key, prev_node.value | [
"def",
"prev_item",
"(",
"self",
",",
"key",
",",
"default",
"=",
"_sentinel",
")",
":",
"# removed graingets version, because it was little slower on CPython and much slower on pypy",
"# this version runs about 4x faster with pypy than the Cython version",
"# Note: Code sharing of prev_item() and floor_item() is possible, but has always a speed penalty.",
"node",
"=",
"self",
".",
"_root",
"prev_node",
"=",
"None",
"while",
"node",
"is",
"not",
"None",
":",
"cmp",
"=",
"self",
".",
"_cmp",
"(",
"self",
".",
"_cmp_data",
",",
"key",
",",
"node",
".",
"key",
")",
"if",
"cmp",
"==",
"0",
":",
"break",
"elif",
"cmp",
"<",
"0",
":",
"node",
"=",
"node",
".",
"left",
"else",
":",
"if",
"(",
"prev_node",
"is",
"None",
")",
"or",
"self",
".",
"_cmp",
"(",
"self",
".",
"_cmp_data",
",",
"prev_node",
".",
"key",
",",
"node",
".",
"key",
")",
"<",
"0",
":",
"prev_node",
"=",
"node",
"node",
"=",
"node",
".",
"right",
"if",
"node",
"is",
"None",
":",
"# stay at dead end (None)",
"if",
"default",
"is",
"_sentinel",
":",
"raise",
"KeyError",
"(",
"str",
"(",
"key",
")",
")",
"return",
"default",
"# found node of key",
"if",
"node",
".",
"left",
"is",
"not",
"None",
":",
"# find biggest node of left subtree",
"node",
"=",
"node",
".",
"left",
"while",
"node",
".",
"right",
"is",
"not",
"None",
":",
"node",
"=",
"node",
".",
"right",
"if",
"prev_node",
"is",
"None",
":",
"prev_node",
"=",
"node",
"elif",
"self",
".",
"_cmp",
"(",
"self",
".",
"_cmp_data",
",",
"prev_node",
".",
"key",
",",
"node",
".",
"key",
")",
"<",
"0",
":",
"prev_node",
"=",
"node",
"elif",
"prev_node",
"is",
"None",
":",
"# given key is smallest in tree",
"if",
"default",
"is",
"_sentinel",
":",
"raise",
"KeyError",
"(",
"str",
"(",
"key",
")",
")",
"return",
"default",
"return",
"prev_node",
".",
"key",
",",
"prev_node",
".",
"value"
] | Get predecessor (k,v) pair of key, raises KeyError if key is min key
or key does not exist. optimized for pypy. | [
"Get",
"predecessor",
"(",
"k",
"v",
")",
"pair",
"of",
"key",
"raises",
"KeyError",
"if",
"key",
"is",
"min",
"key",
"or",
"key",
"does",
"not",
"exist",
".",
"optimized",
"for",
"pypy",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L902-L941 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | _ABCTree.set_default | def set_default(self, key, default=None):
"""T.set_default(k[,d]) -> T.get(k,d), also set T[k]=d if k not in T"""
try:
return self.get_value(key)
except KeyError:
self.insert(key, default)
return default | python | def set_default(self, key, default=None):
"""T.set_default(k[,d]) -> T.get(k,d), also set T[k]=d if k not in T"""
try:
return self.get_value(key)
except KeyError:
self.insert(key, default)
return default | [
"def",
"set_default",
"(",
"self",
",",
"key",
",",
"default",
"=",
"None",
")",
":",
"try",
":",
"return",
"self",
".",
"get_value",
"(",
"key",
")",
"except",
"KeyError",
":",
"self",
".",
"insert",
"(",
"key",
",",
"default",
")",
"return",
"default"
] | T.set_default(k[,d]) -> T.get(k,d), also set T[k]=d if k not in T | [
"T",
".",
"set_default",
"(",
"k",
"[",
"d",
"]",
")",
"-",
">",
"T",
".",
"get",
"(",
"k",
"d",
")",
"also",
"set",
"T",
"[",
"k",
"]",
"=",
"d",
"if",
"k",
"not",
"in",
"T"
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L964-L970 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | _ABCTree.pop | def pop(self, key, *args):
"""T.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
"""
if len(args) > 1:
raise TypeError("pop expected at most 2 arguments, got %d" % (1 + len(args)))
try:
value = self.get_value(key)
self.remove(key)
return value
except KeyError:
if len(args) == 0:
raise
else:
return args[0] | python | def pop(self, key, *args):
"""T.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised
"""
if len(args) > 1:
raise TypeError("pop expected at most 2 arguments, got %d" % (1 + len(args)))
try:
value = self.get_value(key)
self.remove(key)
return value
except KeyError:
if len(args) == 0:
raise
else:
return args[0] | [
"def",
"pop",
"(",
"self",
",",
"key",
",",
"*",
"args",
")",
":",
"if",
"len",
"(",
"args",
")",
">",
"1",
":",
"raise",
"TypeError",
"(",
"\"pop expected at most 2 arguments, got %d\"",
"%",
"(",
"1",
"+",
"len",
"(",
"args",
")",
")",
")",
"try",
":",
"value",
"=",
"self",
".",
"get_value",
"(",
"key",
")",
"self",
".",
"remove",
"(",
"key",
")",
"return",
"value",
"except",
"KeyError",
":",
"if",
"len",
"(",
"args",
")",
"==",
"0",
":",
"raise",
"else",
":",
"return",
"args",
"[",
"0",
"]"
] | T.pop(k[,d]) -> v, remove specified key and return the corresponding value.
If key is not found, d is returned if given, otherwise KeyError is raised | [
"T",
".",
"pop",
"(",
"k",
"[",
"d",
"]",
")",
"-",
">",
"v",
"remove",
"specified",
"key",
"and",
"return",
"the",
"corresponding",
"value",
".",
"If",
"key",
"is",
"not",
"found",
"d",
"is",
"returned",
"if",
"given",
"otherwise",
"KeyError",
"is",
"raised"
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L980-L994 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | _ABCTree.prev_key | def prev_key(self, key, default=_sentinel):
"""Get predecessor to key, raises KeyError if key is min key
or key does not exist.
"""
item = self.prev_item(key, default)
return default if item is default else item[0] | python | def prev_key(self, key, default=_sentinel):
"""Get predecessor to key, raises KeyError if key is min key
or key does not exist.
"""
item = self.prev_item(key, default)
return default if item is default else item[0] | [
"def",
"prev_key",
"(",
"self",
",",
"key",
",",
"default",
"=",
"_sentinel",
")",
":",
"item",
"=",
"self",
".",
"prev_item",
"(",
"key",
",",
"default",
")",
"return",
"default",
"if",
"item",
"is",
"default",
"else",
"item",
"[",
"0",
"]"
] | Get predecessor to key, raises KeyError if key is min key
or key does not exist. | [
"Get",
"predecessor",
"to",
"key",
"raises",
"KeyError",
"if",
"key",
"is",
"min",
"key",
"or",
"key",
"does",
"not",
"exist",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L996-L1001 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | _ABCTree.succ_key | def succ_key(self, key, default=_sentinel):
"""Get successor to key, raises KeyError if key is max key
or key does not exist.
"""
item = self.succ_item(key, default)
return default if item is default else item[0] | python | def succ_key(self, key, default=_sentinel):
"""Get successor to key, raises KeyError if key is max key
or key does not exist.
"""
item = self.succ_item(key, default)
return default if item is default else item[0] | [
"def",
"succ_key",
"(",
"self",
",",
"key",
",",
"default",
"=",
"_sentinel",
")",
":",
"item",
"=",
"self",
".",
"succ_item",
"(",
"key",
",",
"default",
")",
"return",
"default",
"if",
"item",
"is",
"default",
"else",
"item",
"[",
"0",
"]"
] | Get successor to key, raises KeyError if key is max key
or key does not exist. | [
"Get",
"successor",
"to",
"key",
"raises",
"KeyError",
"if",
"key",
"is",
"max",
"key",
"or",
"key",
"does",
"not",
"exist",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L1003-L1008 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | _ABCTree.key_slice | def key_slice(self, start_key, end_key, reverse=False):
"""T.key_slice(start_key, end_key) -> key iterator:
start_key <= key < end_key.
Yields keys in ascending order if reverse is False else in descending order.
"""
return (k for k, v in self.iter_items(start_key, end_key, reverse=reverse)) | python | def key_slice(self, start_key, end_key, reverse=False):
"""T.key_slice(start_key, end_key) -> key iterator:
start_key <= key < end_key.
Yields keys in ascending order if reverse is False else in descending order.
"""
return (k for k, v in self.iter_items(start_key, end_key, reverse=reverse)) | [
"def",
"key_slice",
"(",
"self",
",",
"start_key",
",",
"end_key",
",",
"reverse",
"=",
"False",
")",
":",
"return",
"(",
"k",
"for",
"k",
",",
"v",
"in",
"self",
".",
"iter_items",
"(",
"start_key",
",",
"end_key",
",",
"reverse",
"=",
"reverse",
")",
")"
] | T.key_slice(start_key, end_key) -> key iterator:
start_key <= key < end_key.
Yields keys in ascending order if reverse is False else in descending order. | [
"T",
".",
"key_slice",
"(",
"start_key",
"end_key",
")",
"-",
">",
"key",
"iterator",
":",
"start_key",
"<",
"=",
"key",
"<",
"end_key",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L1034-L1040 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | _ABCTree.iter_items | def iter_items(self, start_key=None, end_key=None, reverse=False):
"""Iterates over the (key, value) items of the associated tree,
in ascending order if reverse is True, iterate in descending order,
reverse defaults to False"""
# optimized iterator (reduced method calls) - faster on CPython but slower on pypy
if self.is_empty():
return []
if reverse:
return self._iter_items_backward(start_key, end_key)
else:
return self._iter_items_forward(start_key, end_key) | python | def iter_items(self, start_key=None, end_key=None, reverse=False):
"""Iterates over the (key, value) items of the associated tree,
in ascending order if reverse is True, iterate in descending order,
reverse defaults to False"""
# optimized iterator (reduced method calls) - faster on CPython but slower on pypy
if self.is_empty():
return []
if reverse:
return self._iter_items_backward(start_key, end_key)
else:
return self._iter_items_forward(start_key, end_key) | [
"def",
"iter_items",
"(",
"self",
",",
"start_key",
"=",
"None",
",",
"end_key",
"=",
"None",
",",
"reverse",
"=",
"False",
")",
":",
"# optimized iterator (reduced method calls) - faster on CPython but slower on pypy",
"if",
"self",
".",
"is_empty",
"(",
")",
":",
"return",
"[",
"]",
"if",
"reverse",
":",
"return",
"self",
".",
"_iter_items_backward",
"(",
"start_key",
",",
"end_key",
")",
"else",
":",
"return",
"self",
".",
"_iter_items_forward",
"(",
"start_key",
",",
"end_key",
")"
] | Iterates over the (key, value) items of the associated tree,
in ascending order if reverse is True, iterate in descending order,
reverse defaults to False | [
"Iterates",
"over",
"the",
"(",
"key",
"value",
")",
"items",
"of",
"the",
"associated",
"tree",
"in",
"ascending",
"order",
"if",
"reverse",
"is",
"True",
"iterate",
"in",
"descending",
"order",
"reverse",
"defaults",
"to",
"False"
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L1042-L1053 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | RBTree.insert | def insert(self, key, value):
"""T.insert(key, value) <==> T[key] = value, insert key, value into tree."""
if self._root is None: # Empty tree case
self._root = self._new_node(key, value)
self._root.red = False # make root black
return
head = Node() # False tree root
grand_parent = None
grand_grand_parent = head
parent = None # parent
direction = 0
last = 0
# Set up helpers
grand_grand_parent.right = self._root
node = grand_grand_parent.right
# Search down the tree
while True:
if node is None: # Insert new node at the bottom
node = self._new_node(key, value)
parent[direction] = node
elif RBTree.is_red(node.left) and RBTree.is_red(node.right): # Color flip
node.red = True
node.left.red = False
node.right.red = False
# Fix red violation
if RBTree.is_red(node) and RBTree.is_red(parent):
direction2 = 1 if grand_grand_parent.right is grand_parent else 0
if node is parent[last]:
grand_grand_parent[direction2] = RBTree.jsw_single(grand_parent, 1 - last)
else:
grand_grand_parent[direction2] = RBTree.jsw_double(grand_parent, 1 - last)
# Stop if found
if self._cmp(self._cmp_data, key, node.key) == 0:
node.value = value # set new value for key
break
last = direction
direction = 0 if (self._cmp(self._cmp_data, key, node.key) < 0) else 1
# Update helpers
if grand_parent is not None:
grand_grand_parent = grand_parent
grand_parent = parent
parent = node
node = node[direction]
self._root = head.right # Update root
self._root.red = False | python | def insert(self, key, value):
"""T.insert(key, value) <==> T[key] = value, insert key, value into tree."""
if self._root is None: # Empty tree case
self._root = self._new_node(key, value)
self._root.red = False # make root black
return
head = Node() # False tree root
grand_parent = None
grand_grand_parent = head
parent = None # parent
direction = 0
last = 0
# Set up helpers
grand_grand_parent.right = self._root
node = grand_grand_parent.right
# Search down the tree
while True:
if node is None: # Insert new node at the bottom
node = self._new_node(key, value)
parent[direction] = node
elif RBTree.is_red(node.left) and RBTree.is_red(node.right): # Color flip
node.red = True
node.left.red = False
node.right.red = False
# Fix red violation
if RBTree.is_red(node) and RBTree.is_red(parent):
direction2 = 1 if grand_grand_parent.right is grand_parent else 0
if node is parent[last]:
grand_grand_parent[direction2] = RBTree.jsw_single(grand_parent, 1 - last)
else:
grand_grand_parent[direction2] = RBTree.jsw_double(grand_parent, 1 - last)
# Stop if found
if self._cmp(self._cmp_data, key, node.key) == 0:
node.value = value # set new value for key
break
last = direction
direction = 0 if (self._cmp(self._cmp_data, key, node.key) < 0) else 1
# Update helpers
if grand_parent is not None:
grand_grand_parent = grand_parent
grand_parent = parent
parent = node
node = node[direction]
self._root = head.right # Update root
self._root.red = False | [
"def",
"insert",
"(",
"self",
",",
"key",
",",
"value",
")",
":",
"if",
"self",
".",
"_root",
"is",
"None",
":",
"# Empty tree case",
"self",
".",
"_root",
"=",
"self",
".",
"_new_node",
"(",
"key",
",",
"value",
")",
"self",
".",
"_root",
".",
"red",
"=",
"False",
"# make root black",
"return",
"head",
"=",
"Node",
"(",
")",
"# False tree root",
"grand_parent",
"=",
"None",
"grand_grand_parent",
"=",
"head",
"parent",
"=",
"None",
"# parent",
"direction",
"=",
"0",
"last",
"=",
"0",
"# Set up helpers",
"grand_grand_parent",
".",
"right",
"=",
"self",
".",
"_root",
"node",
"=",
"grand_grand_parent",
".",
"right",
"# Search down the tree",
"while",
"True",
":",
"if",
"node",
"is",
"None",
":",
"# Insert new node at the bottom",
"node",
"=",
"self",
".",
"_new_node",
"(",
"key",
",",
"value",
")",
"parent",
"[",
"direction",
"]",
"=",
"node",
"elif",
"RBTree",
".",
"is_red",
"(",
"node",
".",
"left",
")",
"and",
"RBTree",
".",
"is_red",
"(",
"node",
".",
"right",
")",
":",
"# Color flip",
"node",
".",
"red",
"=",
"True",
"node",
".",
"left",
".",
"red",
"=",
"False",
"node",
".",
"right",
".",
"red",
"=",
"False",
"# Fix red violation",
"if",
"RBTree",
".",
"is_red",
"(",
"node",
")",
"and",
"RBTree",
".",
"is_red",
"(",
"parent",
")",
":",
"direction2",
"=",
"1",
"if",
"grand_grand_parent",
".",
"right",
"is",
"grand_parent",
"else",
"0",
"if",
"node",
"is",
"parent",
"[",
"last",
"]",
":",
"grand_grand_parent",
"[",
"direction2",
"]",
"=",
"RBTree",
".",
"jsw_single",
"(",
"grand_parent",
",",
"1",
"-",
"last",
")",
"else",
":",
"grand_grand_parent",
"[",
"direction2",
"]",
"=",
"RBTree",
".",
"jsw_double",
"(",
"grand_parent",
",",
"1",
"-",
"last",
")",
"# Stop if found",
"if",
"self",
".",
"_cmp",
"(",
"self",
".",
"_cmp_data",
",",
"key",
",",
"node",
".",
"key",
")",
"==",
"0",
":",
"node",
".",
"value",
"=",
"value",
"# set new value for key",
"break",
"last",
"=",
"direction",
"direction",
"=",
"0",
"if",
"(",
"self",
".",
"_cmp",
"(",
"self",
".",
"_cmp_data",
",",
"key",
",",
"node",
".",
"key",
")",
"<",
"0",
")",
"else",
"1",
"# Update helpers",
"if",
"grand_parent",
"is",
"not",
"None",
":",
"grand_grand_parent",
"=",
"grand_parent",
"grand_parent",
"=",
"parent",
"parent",
"=",
"node",
"node",
"=",
"node",
"[",
"direction",
"]",
"self",
".",
"_root",
"=",
"head",
".",
"right",
"# Update root",
"self",
".",
"_root",
".",
"red",
"=",
"False"
] | T.insert(key, value) <==> T[key] = value, insert key, value into tree. | [
"T",
".",
"insert",
"(",
"key",
"value",
")",
"<",
"==",
">",
"T",
"[",
"key",
"]",
"=",
"value",
"insert",
"key",
"value",
"into",
"tree",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L1166-L1216 | valid |
aleju/imgaug | imgaug/external/poly_point_isect_py2py3.py | RBTree.remove | def remove(self, key):
"""T.remove(key) <==> del T[key], remove item <key> from tree."""
if self._root is None:
raise KeyError(str(key))
head = Node() # False tree root
node = head
node.right = self._root
parent = None
grand_parent = None
found = None # Found item
direction = 1
# Search and push a red down
while node[direction] is not None:
last = direction
# Update helpers
grand_parent = parent
parent = node
node = node[direction]
direction = 1 if (self._cmp(self._cmp_data, node.key, key) < 0) else 0
# Save found node
if self._cmp(self._cmp_data, key, node.key) == 0:
found = node
# Push the red node down
if not RBTree.is_red(node) and not RBTree.is_red(node[direction]):
if RBTree.is_red(node[1 - direction]):
parent[last] = RBTree.jsw_single(node, direction)
parent = parent[last]
elif not RBTree.is_red(node[1 - direction]):
sibling = parent[1 - last]
if sibling is not None:
if (not RBTree.is_red(sibling[1 - last])) and (not RBTree.is_red(sibling[last])):
# Color flip
parent.red = False
sibling.red = True
node.red = True
else:
direction2 = 1 if grand_parent.right is parent else 0
if RBTree.is_red(sibling[last]):
grand_parent[direction2] = RBTree.jsw_double(parent, last)
elif RBTree.is_red(sibling[1-last]):
grand_parent[direction2] = RBTree.jsw_single(parent, last)
# Ensure correct coloring
grand_parent[direction2].red = True
node.red = True
grand_parent[direction2].left.red = False
grand_parent[direction2].right.red = False
# Replace and remove if found
if found is not None:
found.key = node.key
found.value = node.value
parent[int(parent.right is node)] = node[int(node.left is None)]
node.free()
self._count -= 1
# Update root and make it black
self._root = head.right
if self._root is not None:
self._root.red = False
if not found:
raise KeyError(str(key)) | python | def remove(self, key):
"""T.remove(key) <==> del T[key], remove item <key> from tree."""
if self._root is None:
raise KeyError(str(key))
head = Node() # False tree root
node = head
node.right = self._root
parent = None
grand_parent = None
found = None # Found item
direction = 1
# Search and push a red down
while node[direction] is not None:
last = direction
# Update helpers
grand_parent = parent
parent = node
node = node[direction]
direction = 1 if (self._cmp(self._cmp_data, node.key, key) < 0) else 0
# Save found node
if self._cmp(self._cmp_data, key, node.key) == 0:
found = node
# Push the red node down
if not RBTree.is_red(node) and not RBTree.is_red(node[direction]):
if RBTree.is_red(node[1 - direction]):
parent[last] = RBTree.jsw_single(node, direction)
parent = parent[last]
elif not RBTree.is_red(node[1 - direction]):
sibling = parent[1 - last]
if sibling is not None:
if (not RBTree.is_red(sibling[1 - last])) and (not RBTree.is_red(sibling[last])):
# Color flip
parent.red = False
sibling.red = True
node.red = True
else:
direction2 = 1 if grand_parent.right is parent else 0
if RBTree.is_red(sibling[last]):
grand_parent[direction2] = RBTree.jsw_double(parent, last)
elif RBTree.is_red(sibling[1-last]):
grand_parent[direction2] = RBTree.jsw_single(parent, last)
# Ensure correct coloring
grand_parent[direction2].red = True
node.red = True
grand_parent[direction2].left.red = False
grand_parent[direction2].right.red = False
# Replace and remove if found
if found is not None:
found.key = node.key
found.value = node.value
parent[int(parent.right is node)] = node[int(node.left is None)]
node.free()
self._count -= 1
# Update root and make it black
self._root = head.right
if self._root is not None:
self._root.red = False
if not found:
raise KeyError(str(key)) | [
"def",
"remove",
"(",
"self",
",",
"key",
")",
":",
"if",
"self",
".",
"_root",
"is",
"None",
":",
"raise",
"KeyError",
"(",
"str",
"(",
"key",
")",
")",
"head",
"=",
"Node",
"(",
")",
"# False tree root",
"node",
"=",
"head",
"node",
".",
"right",
"=",
"self",
".",
"_root",
"parent",
"=",
"None",
"grand_parent",
"=",
"None",
"found",
"=",
"None",
"# Found item",
"direction",
"=",
"1",
"# Search and push a red down",
"while",
"node",
"[",
"direction",
"]",
"is",
"not",
"None",
":",
"last",
"=",
"direction",
"# Update helpers",
"grand_parent",
"=",
"parent",
"parent",
"=",
"node",
"node",
"=",
"node",
"[",
"direction",
"]",
"direction",
"=",
"1",
"if",
"(",
"self",
".",
"_cmp",
"(",
"self",
".",
"_cmp_data",
",",
"node",
".",
"key",
",",
"key",
")",
"<",
"0",
")",
"else",
"0",
"# Save found node",
"if",
"self",
".",
"_cmp",
"(",
"self",
".",
"_cmp_data",
",",
"key",
",",
"node",
".",
"key",
")",
"==",
"0",
":",
"found",
"=",
"node",
"# Push the red node down",
"if",
"not",
"RBTree",
".",
"is_red",
"(",
"node",
")",
"and",
"not",
"RBTree",
".",
"is_red",
"(",
"node",
"[",
"direction",
"]",
")",
":",
"if",
"RBTree",
".",
"is_red",
"(",
"node",
"[",
"1",
"-",
"direction",
"]",
")",
":",
"parent",
"[",
"last",
"]",
"=",
"RBTree",
".",
"jsw_single",
"(",
"node",
",",
"direction",
")",
"parent",
"=",
"parent",
"[",
"last",
"]",
"elif",
"not",
"RBTree",
".",
"is_red",
"(",
"node",
"[",
"1",
"-",
"direction",
"]",
")",
":",
"sibling",
"=",
"parent",
"[",
"1",
"-",
"last",
"]",
"if",
"sibling",
"is",
"not",
"None",
":",
"if",
"(",
"not",
"RBTree",
".",
"is_red",
"(",
"sibling",
"[",
"1",
"-",
"last",
"]",
")",
")",
"and",
"(",
"not",
"RBTree",
".",
"is_red",
"(",
"sibling",
"[",
"last",
"]",
")",
")",
":",
"# Color flip",
"parent",
".",
"red",
"=",
"False",
"sibling",
".",
"red",
"=",
"True",
"node",
".",
"red",
"=",
"True",
"else",
":",
"direction2",
"=",
"1",
"if",
"grand_parent",
".",
"right",
"is",
"parent",
"else",
"0",
"if",
"RBTree",
".",
"is_red",
"(",
"sibling",
"[",
"last",
"]",
")",
":",
"grand_parent",
"[",
"direction2",
"]",
"=",
"RBTree",
".",
"jsw_double",
"(",
"parent",
",",
"last",
")",
"elif",
"RBTree",
".",
"is_red",
"(",
"sibling",
"[",
"1",
"-",
"last",
"]",
")",
":",
"grand_parent",
"[",
"direction2",
"]",
"=",
"RBTree",
".",
"jsw_single",
"(",
"parent",
",",
"last",
")",
"# Ensure correct coloring",
"grand_parent",
"[",
"direction2",
"]",
".",
"red",
"=",
"True",
"node",
".",
"red",
"=",
"True",
"grand_parent",
"[",
"direction2",
"]",
".",
"left",
".",
"red",
"=",
"False",
"grand_parent",
"[",
"direction2",
"]",
".",
"right",
".",
"red",
"=",
"False",
"# Replace and remove if found",
"if",
"found",
"is",
"not",
"None",
":",
"found",
".",
"key",
"=",
"node",
".",
"key",
"found",
".",
"value",
"=",
"node",
".",
"value",
"parent",
"[",
"int",
"(",
"parent",
".",
"right",
"is",
"node",
")",
"]",
"=",
"node",
"[",
"int",
"(",
"node",
".",
"left",
"is",
"None",
")",
"]",
"node",
".",
"free",
"(",
")",
"self",
".",
"_count",
"-=",
"1",
"# Update root and make it black",
"self",
".",
"_root",
"=",
"head",
".",
"right",
"if",
"self",
".",
"_root",
"is",
"not",
"None",
":",
"self",
".",
"_root",
".",
"red",
"=",
"False",
"if",
"not",
"found",
":",
"raise",
"KeyError",
"(",
"str",
"(",
"key",
")",
")"
] | T.remove(key) <==> del T[key], remove item <key> from tree. | [
"T",
".",
"remove",
"(",
"key",
")",
"<",
"==",
">",
"del",
"T",
"[",
"key",
"]",
"remove",
"item",
"<key",
">",
"from",
"tree",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/poly_point_isect_py2py3.py#L1218-L1283 | valid |
aleju/imgaug | imgaug/external/opensimplex.py | OpenSimplex.noise2d | def noise2d(self, x, y):
"""
Generate 2D OpenSimplex noise from X,Y coordinates.
"""
# Place input coordinates onto grid.
stretch_offset = (x + y) * STRETCH_CONSTANT_2D
xs = x + stretch_offset
ys = y + stretch_offset
# Floor to get grid coordinates of rhombus (stretched square) super-cell origin.
xsb = floor(xs)
ysb = floor(ys)
# Skew out to get actual coordinates of rhombus origin. We'll need these later.
squish_offset = (xsb + ysb) * SQUISH_CONSTANT_2D
xb = xsb + squish_offset
yb = ysb + squish_offset
# Compute grid coordinates relative to rhombus origin.
xins = xs - xsb
yins = ys - ysb
# Sum those together to get a value that determines which region we're in.
in_sum = xins + yins
# Positions relative to origin point.
dx0 = x - xb
dy0 = y - yb
value = 0
# Contribution (1,0)
dx1 = dx0 - 1 - SQUISH_CONSTANT_2D
dy1 = dy0 - 0 - SQUISH_CONSTANT_2D
attn1 = 2 - dx1 * dx1 - dy1 * dy1
extrapolate = self._extrapolate2d
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, dx1, dy1)
# Contribution (0,1)
dx2 = dx0 - 0 - SQUISH_CONSTANT_2D
dy2 = dy0 - 1 - SQUISH_CONSTANT_2D
attn2 = 2 - dx2 * dx2 - dy2 * dy2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, dx2, dy2)
if in_sum <= 1: # We're inside the triangle (2-Simplex) at (0,0)
zins = 1 - in_sum
if zins > xins or zins > yins: # (0,0) is one of the closest two triangular vertices
if xins > yins:
xsv_ext = xsb + 1
ysv_ext = ysb - 1
dx_ext = dx0 - 1
dy_ext = dy0 + 1
else:
xsv_ext = xsb - 1
ysv_ext = ysb + 1
dx_ext = dx0 + 1
dy_ext = dy0 - 1
else: # (1,0) and (0,1) are the closest two vertices.
xsv_ext = xsb + 1
ysv_ext = ysb + 1
dx_ext = dx0 - 1 - 2 * SQUISH_CONSTANT_2D
dy_ext = dy0 - 1 - 2 * SQUISH_CONSTANT_2D
else: # We're inside the triangle (2-Simplex) at (1,1)
zins = 2 - in_sum
if zins < xins or zins < yins: # (0,0) is one of the closest two triangular vertices
if xins > yins:
xsv_ext = xsb + 2
ysv_ext = ysb + 0
dx_ext = dx0 - 2 - 2 * SQUISH_CONSTANT_2D
dy_ext = dy0 + 0 - 2 * SQUISH_CONSTANT_2D
else:
xsv_ext = xsb + 0
ysv_ext = ysb + 2
dx_ext = dx0 + 0 - 2 * SQUISH_CONSTANT_2D
dy_ext = dy0 - 2 - 2 * SQUISH_CONSTANT_2D
else: # (1,0) and (0,1) are the closest two vertices.
dx_ext = dx0
dy_ext = dy0
xsv_ext = xsb
ysv_ext = ysb
xsb += 1
ysb += 1
dx0 = dx0 - 1 - 2 * SQUISH_CONSTANT_2D
dy0 = dy0 - 1 - 2 * SQUISH_CONSTANT_2D
# Contribution (0,0) or (1,1)
attn0 = 2 - dx0 * dx0 - dy0 * dy0
if attn0 > 0:
attn0 *= attn0
value += attn0 * attn0 * extrapolate(xsb, ysb, dx0, dy0)
# Extra Vertex
attn_ext = 2 - dx_ext * dx_ext - dy_ext * dy_ext
if attn_ext > 0:
attn_ext *= attn_ext
value += attn_ext * attn_ext * extrapolate(xsv_ext, ysv_ext, dx_ext, dy_ext)
return value / NORM_CONSTANT_2D | python | def noise2d(self, x, y):
"""
Generate 2D OpenSimplex noise from X,Y coordinates.
"""
# Place input coordinates onto grid.
stretch_offset = (x + y) * STRETCH_CONSTANT_2D
xs = x + stretch_offset
ys = y + stretch_offset
# Floor to get grid coordinates of rhombus (stretched square) super-cell origin.
xsb = floor(xs)
ysb = floor(ys)
# Skew out to get actual coordinates of rhombus origin. We'll need these later.
squish_offset = (xsb + ysb) * SQUISH_CONSTANT_2D
xb = xsb + squish_offset
yb = ysb + squish_offset
# Compute grid coordinates relative to rhombus origin.
xins = xs - xsb
yins = ys - ysb
# Sum those together to get a value that determines which region we're in.
in_sum = xins + yins
# Positions relative to origin point.
dx0 = x - xb
dy0 = y - yb
value = 0
# Contribution (1,0)
dx1 = dx0 - 1 - SQUISH_CONSTANT_2D
dy1 = dy0 - 0 - SQUISH_CONSTANT_2D
attn1 = 2 - dx1 * dx1 - dy1 * dy1
extrapolate = self._extrapolate2d
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, dx1, dy1)
# Contribution (0,1)
dx2 = dx0 - 0 - SQUISH_CONSTANT_2D
dy2 = dy0 - 1 - SQUISH_CONSTANT_2D
attn2 = 2 - dx2 * dx2 - dy2 * dy2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, dx2, dy2)
if in_sum <= 1: # We're inside the triangle (2-Simplex) at (0,0)
zins = 1 - in_sum
if zins > xins or zins > yins: # (0,0) is one of the closest two triangular vertices
if xins > yins:
xsv_ext = xsb + 1
ysv_ext = ysb - 1
dx_ext = dx0 - 1
dy_ext = dy0 + 1
else:
xsv_ext = xsb - 1
ysv_ext = ysb + 1
dx_ext = dx0 + 1
dy_ext = dy0 - 1
else: # (1,0) and (0,1) are the closest two vertices.
xsv_ext = xsb + 1
ysv_ext = ysb + 1
dx_ext = dx0 - 1 - 2 * SQUISH_CONSTANT_2D
dy_ext = dy0 - 1 - 2 * SQUISH_CONSTANT_2D
else: # We're inside the triangle (2-Simplex) at (1,1)
zins = 2 - in_sum
if zins < xins or zins < yins: # (0,0) is one of the closest two triangular vertices
if xins > yins:
xsv_ext = xsb + 2
ysv_ext = ysb + 0
dx_ext = dx0 - 2 - 2 * SQUISH_CONSTANT_2D
dy_ext = dy0 + 0 - 2 * SQUISH_CONSTANT_2D
else:
xsv_ext = xsb + 0
ysv_ext = ysb + 2
dx_ext = dx0 + 0 - 2 * SQUISH_CONSTANT_2D
dy_ext = dy0 - 2 - 2 * SQUISH_CONSTANT_2D
else: # (1,0) and (0,1) are the closest two vertices.
dx_ext = dx0
dy_ext = dy0
xsv_ext = xsb
ysv_ext = ysb
xsb += 1
ysb += 1
dx0 = dx0 - 1 - 2 * SQUISH_CONSTANT_2D
dy0 = dy0 - 1 - 2 * SQUISH_CONSTANT_2D
# Contribution (0,0) or (1,1)
attn0 = 2 - dx0 * dx0 - dy0 * dy0
if attn0 > 0:
attn0 *= attn0
value += attn0 * attn0 * extrapolate(xsb, ysb, dx0, dy0)
# Extra Vertex
attn_ext = 2 - dx_ext * dx_ext - dy_ext * dy_ext
if attn_ext > 0:
attn_ext *= attn_ext
value += attn_ext * attn_ext * extrapolate(xsv_ext, ysv_ext, dx_ext, dy_ext)
return value / NORM_CONSTANT_2D | [
"def",
"noise2d",
"(",
"self",
",",
"x",
",",
"y",
")",
":",
"# Place input coordinates onto grid.",
"stretch_offset",
"=",
"(",
"x",
"+",
"y",
")",
"*",
"STRETCH_CONSTANT_2D",
"xs",
"=",
"x",
"+",
"stretch_offset",
"ys",
"=",
"y",
"+",
"stretch_offset",
"# Floor to get grid coordinates of rhombus (stretched square) super-cell origin.",
"xsb",
"=",
"floor",
"(",
"xs",
")",
"ysb",
"=",
"floor",
"(",
"ys",
")",
"# Skew out to get actual coordinates of rhombus origin. We'll need these later.",
"squish_offset",
"=",
"(",
"xsb",
"+",
"ysb",
")",
"*",
"SQUISH_CONSTANT_2D",
"xb",
"=",
"xsb",
"+",
"squish_offset",
"yb",
"=",
"ysb",
"+",
"squish_offset",
"# Compute grid coordinates relative to rhombus origin.",
"xins",
"=",
"xs",
"-",
"xsb",
"yins",
"=",
"ys",
"-",
"ysb",
"# Sum those together to get a value that determines which region we're in.",
"in_sum",
"=",
"xins",
"+",
"yins",
"# Positions relative to origin point.",
"dx0",
"=",
"x",
"-",
"xb",
"dy0",
"=",
"y",
"-",
"yb",
"value",
"=",
"0",
"# Contribution (1,0)",
"dx1",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_2D",
"dy1",
"=",
"dy0",
"-",
"0",
"-",
"SQUISH_CONSTANT_2D",
"attn1",
"=",
"2",
"-",
"dx1",
"*",
"dx1",
"-",
"dy1",
"*",
"dy1",
"extrapolate",
"=",
"self",
".",
"_extrapolate2d",
"if",
"attn1",
">",
"0",
":",
"attn1",
"*=",
"attn1",
"value",
"+=",
"attn1",
"*",
"attn1",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"0",
",",
"dx1",
",",
"dy1",
")",
"# Contribution (0,1)",
"dx2",
"=",
"dx0",
"-",
"0",
"-",
"SQUISH_CONSTANT_2D",
"dy2",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_2D",
"attn2",
"=",
"2",
"-",
"dx2",
"*",
"dx2",
"-",
"dy2",
"*",
"dy2",
"if",
"attn2",
">",
"0",
":",
"attn2",
"*=",
"attn2",
"value",
"+=",
"attn2",
"*",
"attn2",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"1",
",",
"dx2",
",",
"dy2",
")",
"if",
"in_sum",
"<=",
"1",
":",
"# We're inside the triangle (2-Simplex) at (0,0)",
"zins",
"=",
"1",
"-",
"in_sum",
"if",
"zins",
">",
"xins",
"or",
"zins",
">",
"yins",
":",
"# (0,0) is one of the closest two triangular vertices",
"if",
"xins",
">",
"yins",
":",
"xsv_ext",
"=",
"xsb",
"+",
"1",
"ysv_ext",
"=",
"ysb",
"-",
"1",
"dx_ext",
"=",
"dx0",
"-",
"1",
"dy_ext",
"=",
"dy0",
"+",
"1",
"else",
":",
"xsv_ext",
"=",
"xsb",
"-",
"1",
"ysv_ext",
"=",
"ysb",
"+",
"1",
"dx_ext",
"=",
"dx0",
"+",
"1",
"dy_ext",
"=",
"dy0",
"-",
"1",
"else",
":",
"# (1,0) and (0,1) are the closest two vertices.",
"xsv_ext",
"=",
"xsb",
"+",
"1",
"ysv_ext",
"=",
"ysb",
"+",
"1",
"dx_ext",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_2D",
"dy_ext",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_2D",
"else",
":",
"# We're inside the triangle (2-Simplex) at (1,1)",
"zins",
"=",
"2",
"-",
"in_sum",
"if",
"zins",
"<",
"xins",
"or",
"zins",
"<",
"yins",
":",
"# (0,0) is one of the closest two triangular vertices",
"if",
"xins",
">",
"yins",
":",
"xsv_ext",
"=",
"xsb",
"+",
"2",
"ysv_ext",
"=",
"ysb",
"+",
"0",
"dx_ext",
"=",
"dx0",
"-",
"2",
"-",
"2",
"*",
"SQUISH_CONSTANT_2D",
"dy_ext",
"=",
"dy0",
"+",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_2D",
"else",
":",
"xsv_ext",
"=",
"xsb",
"+",
"0",
"ysv_ext",
"=",
"ysb",
"+",
"2",
"dx_ext",
"=",
"dx0",
"+",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_2D",
"dy_ext",
"=",
"dy0",
"-",
"2",
"-",
"2",
"*",
"SQUISH_CONSTANT_2D",
"else",
":",
"# (1,0) and (0,1) are the closest two vertices.",
"dx_ext",
"=",
"dx0",
"dy_ext",
"=",
"dy0",
"xsv_ext",
"=",
"xsb",
"ysv_ext",
"=",
"ysb",
"xsb",
"+=",
"1",
"ysb",
"+=",
"1",
"dx0",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_2D",
"dy0",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_2D",
"# Contribution (0,0) or (1,1)",
"attn0",
"=",
"2",
"-",
"dx0",
"*",
"dx0",
"-",
"dy0",
"*",
"dy0",
"if",
"attn0",
">",
"0",
":",
"attn0",
"*=",
"attn0",
"value",
"+=",
"attn0",
"*",
"attn0",
"*",
"extrapolate",
"(",
"xsb",
",",
"ysb",
",",
"dx0",
",",
"dy0",
")",
"# Extra Vertex",
"attn_ext",
"=",
"2",
"-",
"dx_ext",
"*",
"dx_ext",
"-",
"dy_ext",
"*",
"dy_ext",
"if",
"attn_ext",
">",
"0",
":",
"attn_ext",
"*=",
"attn_ext",
"value",
"+=",
"attn_ext",
"*",
"attn_ext",
"*",
"extrapolate",
"(",
"xsv_ext",
",",
"ysv_ext",
",",
"dx_ext",
",",
"dy_ext",
")",
"return",
"value",
"/",
"NORM_CONSTANT_2D"
] | Generate 2D OpenSimplex noise from X,Y coordinates. | [
"Generate",
"2D",
"OpenSimplex",
"noise",
"from",
"X",
"Y",
"coordinates",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/opensimplex.py#L143-L244 | valid |
aleju/imgaug | imgaug/external/opensimplex.py | OpenSimplex.noise3d | def noise3d(self, x, y, z):
"""
Generate 3D OpenSimplex noise from X,Y,Z coordinates.
"""
# Place input coordinates on simplectic honeycomb.
stretch_offset = (x + y + z) * STRETCH_CONSTANT_3D
xs = x + stretch_offset
ys = y + stretch_offset
zs = z + stretch_offset
# Floor to get simplectic honeycomb coordinates of rhombohedron (stretched cube) super-cell origin.
xsb = floor(xs)
ysb = floor(ys)
zsb = floor(zs)
# Skew out to get actual coordinates of rhombohedron origin. We'll need these later.
squish_offset = (xsb + ysb + zsb) * SQUISH_CONSTANT_3D
xb = xsb + squish_offset
yb = ysb + squish_offset
zb = zsb + squish_offset
# Compute simplectic honeycomb coordinates relative to rhombohedral origin.
xins = xs - xsb
yins = ys - ysb
zins = zs - zsb
# Sum those together to get a value that determines which region we're in.
in_sum = xins + yins + zins
# Positions relative to origin point.
dx0 = x - xb
dy0 = y - yb
dz0 = z - zb
value = 0
extrapolate = self._extrapolate3d
if in_sum <= 1: # We're inside the tetrahedron (3-Simplex) at (0,0,0)
# Determine which two of (0,0,1), (0,1,0), (1,0,0) are closest.
a_point = 0x01
a_score = xins
b_point = 0x02
b_score = yins
if a_score >= b_score and zins > b_score:
b_score = zins
b_point = 0x04
elif a_score < b_score and zins > a_score:
a_score = zins
a_point = 0x04
# Now we determine the two lattice points not part of the tetrahedron that may contribute.
# This depends on the closest two tetrahedral vertices, including (0,0,0)
wins = 1 - in_sum
if wins > a_score or wins > b_score: # (0,0,0) is one of the closest two tetrahedral vertices.
c = b_point if (b_score > a_score) else a_point # Our other closest vertex is the closest out of a and b.
if (c & 0x01) == 0:
xsv_ext0 = xsb - 1
xsv_ext1 = xsb
dx_ext0 = dx0 + 1
dx_ext1 = dx0
else:
xsv_ext0 = xsv_ext1 = xsb + 1
dx_ext0 = dx_ext1 = dx0 - 1
if (c & 0x02) == 0:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy_ext1 = dy0
if (c & 0x01) == 0:
ysv_ext1 -= 1
dy_ext1 += 1
else:
ysv_ext0 -= 1
dy_ext0 += 1
else:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy_ext1 = dy0 - 1
if (c & 0x04) == 0:
zsv_ext0 = zsb
zsv_ext1 = zsb - 1
dz_ext0 = dz0
dz_ext1 = dz0 + 1
else:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz_ext1 = dz0 - 1
else: # (0,0,0) is not one of the closest two tetrahedral vertices.
c = (a_point | b_point) # Our two extra vertices are determined by the closest two.
if (c & 0x01) == 0:
xsv_ext0 = xsb
xsv_ext1 = xsb - 1
dx_ext0 = dx0 - 2 * SQUISH_CONSTANT_3D
dx_ext1 = dx0 + 1 - SQUISH_CONSTANT_3D
else:
xsv_ext0 = xsv_ext1 = xsb + 1
dx_ext0 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D
dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D
if (c & 0x02) == 0:
ysv_ext0 = ysb
ysv_ext1 = ysb - 1
dy_ext0 = dy0 - 2 * SQUISH_CONSTANT_3D
dy_ext1 = dy0 + 1 - SQUISH_CONSTANT_3D
else:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D
if (c & 0x04) == 0:
zsv_ext0 = zsb
zsv_ext1 = zsb - 1
dz_ext0 = dz0 - 2 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 + 1 - SQUISH_CONSTANT_3D
else:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D
# Contribution (0,0,0)
attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0
if attn0 > 0:
attn0 *= attn0
value += attn0 * attn0 * extrapolate(xsb + 0, ysb + 0, zsb + 0, dx0, dy0, dz0)
# Contribution (1,0,0)
dx1 = dx0 - 1 - SQUISH_CONSTANT_3D
dy1 = dy0 - 0 - SQUISH_CONSTANT_3D
dz1 = dz0 - 0 - SQUISH_CONSTANT_3D
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, zsb + 0, dx1, dy1, dz1)
# Contribution (0,1,0)
dx2 = dx0 - 0 - SQUISH_CONSTANT_3D
dy2 = dy0 - 1 - SQUISH_CONSTANT_3D
dz2 = dz1
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, dx2, dy2, dz2)
# Contribution (0,0,1)
dx3 = dx2
dy3 = dy1
dz3 = dz0 - 1 - SQUISH_CONSTANT_3D
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, dx3, dy3, dz3)
elif in_sum >= 2: # We're inside the tetrahedron (3-Simplex) at (1,1,1)
# Determine which two tetrahedral vertices are the closest, out of (1,1,0), (1,0,1), (0,1,1) but not (1,1,1).
a_point = 0x06
a_score = xins
b_point = 0x05
b_score = yins
if a_score <= b_score and zins < b_score:
b_score = zins
b_point = 0x03
elif a_score > b_score and zins < a_score:
a_score = zins
a_point = 0x03
# Now we determine the two lattice points not part of the tetrahedron that may contribute.
# This depends on the closest two tetrahedral vertices, including (1,1,1)
wins = 3 - in_sum
if wins < a_score or wins < b_score: # (1,1,1) is one of the closest two tetrahedral vertices.
c = b_point if (b_score < a_score) else a_point # Our other closest vertex is the closest out of a and b.
if (c & 0x01) != 0:
xsv_ext0 = xsb + 2
xsv_ext1 = xsb + 1
dx_ext0 = dx0 - 2 - 3 * SQUISH_CONSTANT_3D
dx_ext1 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D
else:
xsv_ext0 = xsv_ext1 = xsb
dx_ext0 = dx_ext1 = dx0 - 3 * SQUISH_CONSTANT_3D
if (c & 0x02) != 0:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy_ext1 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D
if (c & 0x01) != 0:
ysv_ext1 += 1
dy_ext1 -= 1
else:
ysv_ext0 += 1
dy_ext0 -= 1
else:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy_ext1 = dy0 - 3 * SQUISH_CONSTANT_3D
if (c & 0x04) != 0:
zsv_ext0 = zsb + 1
zsv_ext1 = zsb + 2
dz_ext0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 - 3 * SQUISH_CONSTANT_3D
else:
zsv_ext0 = zsv_ext1 = zsb
dz_ext0 = dz_ext1 = dz0 - 3 * SQUISH_CONSTANT_3D
else: # (1,1,1) is not one of the closest two tetrahedral vertices.
c = (a_point & b_point) # Our two extra vertices are determined by the closest two.
if (c & 0x01) != 0:
xsv_ext0 = xsb + 1
xsv_ext1 = xsb + 2
dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D
dx_ext1 = dx0 - 2 - 2 * SQUISH_CONSTANT_3D
else:
xsv_ext0 = xsv_ext1 = xsb
dx_ext0 = dx0 - SQUISH_CONSTANT_3D
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D
if (c & 0x02) != 0:
ysv_ext0 = ysb + 1
ysv_ext1 = ysb + 2
dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 2 - 2 * SQUISH_CONSTANT_3D
else:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy0 - SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D
if (c & 0x04) != 0:
zsv_ext0 = zsb + 1
zsv_ext1 = zsb + 2
dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 - 2 * SQUISH_CONSTANT_3D
else:
zsv_ext0 = zsv_ext1 = zsb
dz_ext0 = dz0 - SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D
# Contribution (1,1,0)
dx3 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D
dy3 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D
dz3 = dz0 - 0 - 2 * SQUISH_CONSTANT_3D
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 1, ysb + 1, zsb + 0, dx3, dy3, dz3)
# Contribution (1,0,1)
dx2 = dx3
dy2 = dy0 - 0 - 2 * SQUISH_CONSTANT_3D
dz2 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 1, ysb + 0, zsb + 1, dx2, dy2, dz2)
# Contribution (0,1,1)
dx1 = dx0 - 0 - 2 * SQUISH_CONSTANT_3D
dy1 = dy3
dz1 = dz2
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 0, ysb + 1, zsb + 1, dx1, dy1, dz1)
# Contribution (1,1,1)
dx0 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D
dy0 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D
dz0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D
attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0
if attn0 > 0:
attn0 *= attn0
value += attn0 * attn0 * extrapolate(xsb + 1, ysb + 1, zsb + 1, dx0, dy0, dz0)
else: # We're inside the octahedron (Rectified 3-Simplex) in between.
# Decide between point (0,0,1) and (1,1,0) as closest
p1 = xins + yins
if p1 > 1:
a_score = p1 - 1
a_point = 0x03
a_is_further_side = True
else:
a_score = 1 - p1
a_point = 0x04
a_is_further_side = False
# Decide between point (0,1,0) and (1,0,1) as closest
p2 = xins + zins
if p2 > 1:
b_score = p2 - 1
b_point = 0x05
b_is_further_side = True
else:
b_score = 1 - p2
b_point = 0x02
b_is_further_side = False
# The closest out of the two (1,0,0) and (0,1,1) will replace the furthest out of the two decided above, if closer.
p3 = yins + zins
if p3 > 1:
score = p3 - 1
if a_score <= b_score and a_score < score:
a_point = 0x06
a_is_further_side = True
elif a_score > b_score and b_score < score:
b_point = 0x06
b_is_further_side = True
else:
score = 1 - p3
if a_score <= b_score and a_score < score:
a_point = 0x01
a_is_further_side = False
elif a_score > b_score and b_score < score:
b_point = 0x01
b_is_further_side = False
# Where each of the two closest points are determines how the extra two vertices are calculated.
if a_is_further_side == b_is_further_side:
if a_is_further_side: # Both closest points on (1,1,1) side
# One of the two extra points is (1,1,1)
dx_ext0 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D
dy_ext0 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D
dz_ext0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D
xsv_ext0 = xsb + 1
ysv_ext0 = ysb + 1
zsv_ext0 = zsb + 1
# Other extra point is based on the shared axis.
c = (a_point & b_point)
if (c & 0x01) != 0:
dx_ext1 = dx0 - 2 - 2 * SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D
xsv_ext1 = xsb + 2
ysv_ext1 = ysb
zsv_ext1 = zsb
elif (c & 0x02) != 0:
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 2 - 2 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D
xsv_ext1 = xsb
ysv_ext1 = ysb + 2
zsv_ext1 = zsb
else:
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 - 2 * SQUISH_CONSTANT_3D
xsv_ext1 = xsb
ysv_ext1 = ysb
zsv_ext1 = zsb + 2
else:# Both closest points on (0,0,0) side
# One of the two extra points is (0,0,0)
dx_ext0 = dx0
dy_ext0 = dy0
dz_ext0 = dz0
xsv_ext0 = xsb
ysv_ext0 = ysb
zsv_ext0 = zsb
# Other extra point is based on the omitted axis.
c = (a_point | b_point)
if (c & 0x01) == 0:
dx_ext1 = dx0 + 1 - SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D
xsv_ext1 = xsb - 1
ysv_ext1 = ysb + 1
zsv_ext1 = zsb + 1
elif (c & 0x02) == 0:
dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D
dy_ext1 = dy0 + 1 - SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D
xsv_ext1 = xsb + 1
ysv_ext1 = ysb - 1
zsv_ext1 = zsb + 1
else:
dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D
dz_ext1 = dz0 + 1 - SQUISH_CONSTANT_3D
xsv_ext1 = xsb + 1
ysv_ext1 = ysb + 1
zsv_ext1 = zsb - 1
else: # One point on (0,0,0) side, one point on (1,1,1) side
if a_is_further_side:
c1 = a_point
c2 = b_point
else:
c1 = b_point
c2 = a_point
# One contribution is a _permutation of (1,1,-1)
if (c1 & 0x01) == 0:
dx_ext0 = dx0 + 1 - SQUISH_CONSTANT_3D
dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D
dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D
xsv_ext0 = xsb - 1
ysv_ext0 = ysb + 1
zsv_ext0 = zsb + 1
elif (c1 & 0x02) == 0:
dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D
dy_ext0 = dy0 + 1 - SQUISH_CONSTANT_3D
dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D
xsv_ext0 = xsb + 1
ysv_ext0 = ysb - 1
zsv_ext0 = zsb + 1
else:
dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D
dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D
dz_ext0 = dz0 + 1 - SQUISH_CONSTANT_3D
xsv_ext0 = xsb + 1
ysv_ext0 = ysb + 1
zsv_ext0 = zsb - 1
# One contribution is a _permutation of (0,0,2)
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D
xsv_ext1 = xsb
ysv_ext1 = ysb
zsv_ext1 = zsb
if (c2 & 0x01) != 0:
dx_ext1 -= 2
xsv_ext1 += 2
elif (c2 & 0x02) != 0:
dy_ext1 -= 2
ysv_ext1 += 2
else:
dz_ext1 -= 2
zsv_ext1 += 2
# Contribution (1,0,0)
dx1 = dx0 - 1 - SQUISH_CONSTANT_3D
dy1 = dy0 - 0 - SQUISH_CONSTANT_3D
dz1 = dz0 - 0 - SQUISH_CONSTANT_3D
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, zsb + 0, dx1, dy1, dz1)
# Contribution (0,1,0)
dx2 = dx0 - 0 - SQUISH_CONSTANT_3D
dy2 = dy0 - 1 - SQUISH_CONSTANT_3D
dz2 = dz1
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, dx2, dy2, dz2)
# Contribution (0,0,1)
dx3 = dx2
dy3 = dy1
dz3 = dz0 - 1 - SQUISH_CONSTANT_3D
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, dx3, dy3, dz3)
# Contribution (1,1,0)
dx4 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D
dy4 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D
dz4 = dz0 - 0 - 2 * SQUISH_CONSTANT_3D
attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4
if attn4 > 0:
attn4 *= attn4
value += attn4 * attn4 * extrapolate(xsb + 1, ysb + 1, zsb + 0, dx4, dy4, dz4)
# Contribution (1,0,1)
dx5 = dx4
dy5 = dy0 - 0 - 2 * SQUISH_CONSTANT_3D
dz5 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D
attn5 = 2 - dx5 * dx5 - dy5 * dy5 - dz5 * dz5
if attn5 > 0:
attn5 *= attn5
value += attn5 * attn5 * extrapolate(xsb + 1, ysb + 0, zsb + 1, dx5, dy5, dz5)
# Contribution (0,1,1)
dx6 = dx0 - 0 - 2 * SQUISH_CONSTANT_3D
dy6 = dy4
dz6 = dz5
attn6 = 2 - dx6 * dx6 - dy6 * dy6 - dz6 * dz6
if attn6 > 0:
attn6 *= attn6
value += attn6 * attn6 * extrapolate(xsb + 0, ysb + 1, zsb + 1, dx6, dy6, dz6)
# First extra vertex
attn_ext0 = 2 - dx_ext0 * dx_ext0 - dy_ext0 * dy_ext0 - dz_ext0 * dz_ext0
if attn_ext0 > 0:
attn_ext0 *= attn_ext0
value += attn_ext0 * attn_ext0 * extrapolate(xsv_ext0, ysv_ext0, zsv_ext0, dx_ext0, dy_ext0, dz_ext0)
# Second extra vertex
attn_ext1 = 2 - dx_ext1 * dx_ext1 - dy_ext1 * dy_ext1 - dz_ext1 * dz_ext1
if attn_ext1 > 0:
attn_ext1 *= attn_ext1
value += attn_ext1 * attn_ext1 * extrapolate(xsv_ext1, ysv_ext1, zsv_ext1, dx_ext1, dy_ext1, dz_ext1)
return value / NORM_CONSTANT_3D | python | def noise3d(self, x, y, z):
"""
Generate 3D OpenSimplex noise from X,Y,Z coordinates.
"""
# Place input coordinates on simplectic honeycomb.
stretch_offset = (x + y + z) * STRETCH_CONSTANT_3D
xs = x + stretch_offset
ys = y + stretch_offset
zs = z + stretch_offset
# Floor to get simplectic honeycomb coordinates of rhombohedron (stretched cube) super-cell origin.
xsb = floor(xs)
ysb = floor(ys)
zsb = floor(zs)
# Skew out to get actual coordinates of rhombohedron origin. We'll need these later.
squish_offset = (xsb + ysb + zsb) * SQUISH_CONSTANT_3D
xb = xsb + squish_offset
yb = ysb + squish_offset
zb = zsb + squish_offset
# Compute simplectic honeycomb coordinates relative to rhombohedral origin.
xins = xs - xsb
yins = ys - ysb
zins = zs - zsb
# Sum those together to get a value that determines which region we're in.
in_sum = xins + yins + zins
# Positions relative to origin point.
dx0 = x - xb
dy0 = y - yb
dz0 = z - zb
value = 0
extrapolate = self._extrapolate3d
if in_sum <= 1: # We're inside the tetrahedron (3-Simplex) at (0,0,0)
# Determine which two of (0,0,1), (0,1,0), (1,0,0) are closest.
a_point = 0x01
a_score = xins
b_point = 0x02
b_score = yins
if a_score >= b_score and zins > b_score:
b_score = zins
b_point = 0x04
elif a_score < b_score and zins > a_score:
a_score = zins
a_point = 0x04
# Now we determine the two lattice points not part of the tetrahedron that may contribute.
# This depends on the closest two tetrahedral vertices, including (0,0,0)
wins = 1 - in_sum
if wins > a_score or wins > b_score: # (0,0,0) is one of the closest two tetrahedral vertices.
c = b_point if (b_score > a_score) else a_point # Our other closest vertex is the closest out of a and b.
if (c & 0x01) == 0:
xsv_ext0 = xsb - 1
xsv_ext1 = xsb
dx_ext0 = dx0 + 1
dx_ext1 = dx0
else:
xsv_ext0 = xsv_ext1 = xsb + 1
dx_ext0 = dx_ext1 = dx0 - 1
if (c & 0x02) == 0:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy_ext1 = dy0
if (c & 0x01) == 0:
ysv_ext1 -= 1
dy_ext1 += 1
else:
ysv_ext0 -= 1
dy_ext0 += 1
else:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy_ext1 = dy0 - 1
if (c & 0x04) == 0:
zsv_ext0 = zsb
zsv_ext1 = zsb - 1
dz_ext0 = dz0
dz_ext1 = dz0 + 1
else:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz_ext1 = dz0 - 1
else: # (0,0,0) is not one of the closest two tetrahedral vertices.
c = (a_point | b_point) # Our two extra vertices are determined by the closest two.
if (c & 0x01) == 0:
xsv_ext0 = xsb
xsv_ext1 = xsb - 1
dx_ext0 = dx0 - 2 * SQUISH_CONSTANT_3D
dx_ext1 = dx0 + 1 - SQUISH_CONSTANT_3D
else:
xsv_ext0 = xsv_ext1 = xsb + 1
dx_ext0 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D
dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D
if (c & 0x02) == 0:
ysv_ext0 = ysb
ysv_ext1 = ysb - 1
dy_ext0 = dy0 - 2 * SQUISH_CONSTANT_3D
dy_ext1 = dy0 + 1 - SQUISH_CONSTANT_3D
else:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D
if (c & 0x04) == 0:
zsv_ext0 = zsb
zsv_ext1 = zsb - 1
dz_ext0 = dz0 - 2 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 + 1 - SQUISH_CONSTANT_3D
else:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D
# Contribution (0,0,0)
attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0
if attn0 > 0:
attn0 *= attn0
value += attn0 * attn0 * extrapolate(xsb + 0, ysb + 0, zsb + 0, dx0, dy0, dz0)
# Contribution (1,0,0)
dx1 = dx0 - 1 - SQUISH_CONSTANT_3D
dy1 = dy0 - 0 - SQUISH_CONSTANT_3D
dz1 = dz0 - 0 - SQUISH_CONSTANT_3D
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, zsb + 0, dx1, dy1, dz1)
# Contribution (0,1,0)
dx2 = dx0 - 0 - SQUISH_CONSTANT_3D
dy2 = dy0 - 1 - SQUISH_CONSTANT_3D
dz2 = dz1
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, dx2, dy2, dz2)
# Contribution (0,0,1)
dx3 = dx2
dy3 = dy1
dz3 = dz0 - 1 - SQUISH_CONSTANT_3D
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, dx3, dy3, dz3)
elif in_sum >= 2: # We're inside the tetrahedron (3-Simplex) at (1,1,1)
# Determine which two tetrahedral vertices are the closest, out of (1,1,0), (1,0,1), (0,1,1) but not (1,1,1).
a_point = 0x06
a_score = xins
b_point = 0x05
b_score = yins
if a_score <= b_score and zins < b_score:
b_score = zins
b_point = 0x03
elif a_score > b_score and zins < a_score:
a_score = zins
a_point = 0x03
# Now we determine the two lattice points not part of the tetrahedron that may contribute.
# This depends on the closest two tetrahedral vertices, including (1,1,1)
wins = 3 - in_sum
if wins < a_score or wins < b_score: # (1,1,1) is one of the closest two tetrahedral vertices.
c = b_point if (b_score < a_score) else a_point # Our other closest vertex is the closest out of a and b.
if (c & 0x01) != 0:
xsv_ext0 = xsb + 2
xsv_ext1 = xsb + 1
dx_ext0 = dx0 - 2 - 3 * SQUISH_CONSTANT_3D
dx_ext1 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D
else:
xsv_ext0 = xsv_ext1 = xsb
dx_ext0 = dx_ext1 = dx0 - 3 * SQUISH_CONSTANT_3D
if (c & 0x02) != 0:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy_ext1 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D
if (c & 0x01) != 0:
ysv_ext1 += 1
dy_ext1 -= 1
else:
ysv_ext0 += 1
dy_ext0 -= 1
else:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy_ext1 = dy0 - 3 * SQUISH_CONSTANT_3D
if (c & 0x04) != 0:
zsv_ext0 = zsb + 1
zsv_ext1 = zsb + 2
dz_ext0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 - 3 * SQUISH_CONSTANT_3D
else:
zsv_ext0 = zsv_ext1 = zsb
dz_ext0 = dz_ext1 = dz0 - 3 * SQUISH_CONSTANT_3D
else: # (1,1,1) is not one of the closest two tetrahedral vertices.
c = (a_point & b_point) # Our two extra vertices are determined by the closest two.
if (c & 0x01) != 0:
xsv_ext0 = xsb + 1
xsv_ext1 = xsb + 2
dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D
dx_ext1 = dx0 - 2 - 2 * SQUISH_CONSTANT_3D
else:
xsv_ext0 = xsv_ext1 = xsb
dx_ext0 = dx0 - SQUISH_CONSTANT_3D
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D
if (c & 0x02) != 0:
ysv_ext0 = ysb + 1
ysv_ext1 = ysb + 2
dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 2 - 2 * SQUISH_CONSTANT_3D
else:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy0 - SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D
if (c & 0x04) != 0:
zsv_ext0 = zsb + 1
zsv_ext1 = zsb + 2
dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 - 2 * SQUISH_CONSTANT_3D
else:
zsv_ext0 = zsv_ext1 = zsb
dz_ext0 = dz0 - SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D
# Contribution (1,1,0)
dx3 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D
dy3 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D
dz3 = dz0 - 0 - 2 * SQUISH_CONSTANT_3D
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 1, ysb + 1, zsb + 0, dx3, dy3, dz3)
# Contribution (1,0,1)
dx2 = dx3
dy2 = dy0 - 0 - 2 * SQUISH_CONSTANT_3D
dz2 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 1, ysb + 0, zsb + 1, dx2, dy2, dz2)
# Contribution (0,1,1)
dx1 = dx0 - 0 - 2 * SQUISH_CONSTANT_3D
dy1 = dy3
dz1 = dz2
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 0, ysb + 1, zsb + 1, dx1, dy1, dz1)
# Contribution (1,1,1)
dx0 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D
dy0 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D
dz0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D
attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0
if attn0 > 0:
attn0 *= attn0
value += attn0 * attn0 * extrapolate(xsb + 1, ysb + 1, zsb + 1, dx0, dy0, dz0)
else: # We're inside the octahedron (Rectified 3-Simplex) in between.
# Decide between point (0,0,1) and (1,1,0) as closest
p1 = xins + yins
if p1 > 1:
a_score = p1 - 1
a_point = 0x03
a_is_further_side = True
else:
a_score = 1 - p1
a_point = 0x04
a_is_further_side = False
# Decide between point (0,1,0) and (1,0,1) as closest
p2 = xins + zins
if p2 > 1:
b_score = p2 - 1
b_point = 0x05
b_is_further_side = True
else:
b_score = 1 - p2
b_point = 0x02
b_is_further_side = False
# The closest out of the two (1,0,0) and (0,1,1) will replace the furthest out of the two decided above, if closer.
p3 = yins + zins
if p3 > 1:
score = p3 - 1
if a_score <= b_score and a_score < score:
a_point = 0x06
a_is_further_side = True
elif a_score > b_score and b_score < score:
b_point = 0x06
b_is_further_side = True
else:
score = 1 - p3
if a_score <= b_score and a_score < score:
a_point = 0x01
a_is_further_side = False
elif a_score > b_score and b_score < score:
b_point = 0x01
b_is_further_side = False
# Where each of the two closest points are determines how the extra two vertices are calculated.
if a_is_further_side == b_is_further_side:
if a_is_further_side: # Both closest points on (1,1,1) side
# One of the two extra points is (1,1,1)
dx_ext0 = dx0 - 1 - 3 * SQUISH_CONSTANT_3D
dy_ext0 = dy0 - 1 - 3 * SQUISH_CONSTANT_3D
dz_ext0 = dz0 - 1 - 3 * SQUISH_CONSTANT_3D
xsv_ext0 = xsb + 1
ysv_ext0 = ysb + 1
zsv_ext0 = zsb + 1
# Other extra point is based on the shared axis.
c = (a_point & b_point)
if (c & 0x01) != 0:
dx_ext1 = dx0 - 2 - 2 * SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D
xsv_ext1 = xsb + 2
ysv_ext1 = ysb
zsv_ext1 = zsb
elif (c & 0x02) != 0:
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 2 - 2 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D
xsv_ext1 = xsb
ysv_ext1 = ysb + 2
zsv_ext1 = zsb
else:
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 - 2 * SQUISH_CONSTANT_3D
xsv_ext1 = xsb
ysv_ext1 = ysb
zsv_ext1 = zsb + 2
else:# Both closest points on (0,0,0) side
# One of the two extra points is (0,0,0)
dx_ext0 = dx0
dy_ext0 = dy0
dz_ext0 = dz0
xsv_ext0 = xsb
ysv_ext0 = ysb
zsv_ext0 = zsb
# Other extra point is based on the omitted axis.
c = (a_point | b_point)
if (c & 0x01) == 0:
dx_ext1 = dx0 + 1 - SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D
xsv_ext1 = xsb - 1
ysv_ext1 = ysb + 1
zsv_ext1 = zsb + 1
elif (c & 0x02) == 0:
dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D
dy_ext1 = dy0 + 1 - SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_3D
xsv_ext1 = xsb + 1
ysv_ext1 = ysb - 1
zsv_ext1 = zsb + 1
else:
dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_3D
dz_ext1 = dz0 + 1 - SQUISH_CONSTANT_3D
xsv_ext1 = xsb + 1
ysv_ext1 = ysb + 1
zsv_ext1 = zsb - 1
else: # One point on (0,0,0) side, one point on (1,1,1) side
if a_is_further_side:
c1 = a_point
c2 = b_point
else:
c1 = b_point
c2 = a_point
# One contribution is a _permutation of (1,1,-1)
if (c1 & 0x01) == 0:
dx_ext0 = dx0 + 1 - SQUISH_CONSTANT_3D
dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D
dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D
xsv_ext0 = xsb - 1
ysv_ext0 = ysb + 1
zsv_ext0 = zsb + 1
elif (c1 & 0x02) == 0:
dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D
dy_ext0 = dy0 + 1 - SQUISH_CONSTANT_3D
dz_ext0 = dz0 - 1 - SQUISH_CONSTANT_3D
xsv_ext0 = xsb + 1
ysv_ext0 = ysb - 1
zsv_ext0 = zsb + 1
else:
dx_ext0 = dx0 - 1 - SQUISH_CONSTANT_3D
dy_ext0 = dy0 - 1 - SQUISH_CONSTANT_3D
dz_ext0 = dz0 + 1 - SQUISH_CONSTANT_3D
xsv_ext0 = xsb + 1
ysv_ext0 = ysb + 1
zsv_ext0 = zsb - 1
# One contribution is a _permutation of (0,0,2)
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_3D
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_3D
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_3D
xsv_ext1 = xsb
ysv_ext1 = ysb
zsv_ext1 = zsb
if (c2 & 0x01) != 0:
dx_ext1 -= 2
xsv_ext1 += 2
elif (c2 & 0x02) != 0:
dy_ext1 -= 2
ysv_ext1 += 2
else:
dz_ext1 -= 2
zsv_ext1 += 2
# Contribution (1,0,0)
dx1 = dx0 - 1 - SQUISH_CONSTANT_3D
dy1 = dy0 - 0 - SQUISH_CONSTANT_3D
dz1 = dz0 - 0 - SQUISH_CONSTANT_3D
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, zsb + 0, dx1, dy1, dz1)
# Contribution (0,1,0)
dx2 = dx0 - 0 - SQUISH_CONSTANT_3D
dy2 = dy0 - 1 - SQUISH_CONSTANT_3D
dz2 = dz1
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, dx2, dy2, dz2)
# Contribution (0,0,1)
dx3 = dx2
dy3 = dy1
dz3 = dz0 - 1 - SQUISH_CONSTANT_3D
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, dx3, dy3, dz3)
# Contribution (1,1,0)
dx4 = dx0 - 1 - 2 * SQUISH_CONSTANT_3D
dy4 = dy0 - 1 - 2 * SQUISH_CONSTANT_3D
dz4 = dz0 - 0 - 2 * SQUISH_CONSTANT_3D
attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4
if attn4 > 0:
attn4 *= attn4
value += attn4 * attn4 * extrapolate(xsb + 1, ysb + 1, zsb + 0, dx4, dy4, dz4)
# Contribution (1,0,1)
dx5 = dx4
dy5 = dy0 - 0 - 2 * SQUISH_CONSTANT_3D
dz5 = dz0 - 1 - 2 * SQUISH_CONSTANT_3D
attn5 = 2 - dx5 * dx5 - dy5 * dy5 - dz5 * dz5
if attn5 > 0:
attn5 *= attn5
value += attn5 * attn5 * extrapolate(xsb + 1, ysb + 0, zsb + 1, dx5, dy5, dz5)
# Contribution (0,1,1)
dx6 = dx0 - 0 - 2 * SQUISH_CONSTANT_3D
dy6 = dy4
dz6 = dz5
attn6 = 2 - dx6 * dx6 - dy6 * dy6 - dz6 * dz6
if attn6 > 0:
attn6 *= attn6
value += attn6 * attn6 * extrapolate(xsb + 0, ysb + 1, zsb + 1, dx6, dy6, dz6)
# First extra vertex
attn_ext0 = 2 - dx_ext0 * dx_ext0 - dy_ext0 * dy_ext0 - dz_ext0 * dz_ext0
if attn_ext0 > 0:
attn_ext0 *= attn_ext0
value += attn_ext0 * attn_ext0 * extrapolate(xsv_ext0, ysv_ext0, zsv_ext0, dx_ext0, dy_ext0, dz_ext0)
# Second extra vertex
attn_ext1 = 2 - dx_ext1 * dx_ext1 - dy_ext1 * dy_ext1 - dz_ext1 * dz_ext1
if attn_ext1 > 0:
attn_ext1 *= attn_ext1
value += attn_ext1 * attn_ext1 * extrapolate(xsv_ext1, ysv_ext1, zsv_ext1, dx_ext1, dy_ext1, dz_ext1)
return value / NORM_CONSTANT_3D | [
"def",
"noise3d",
"(",
"self",
",",
"x",
",",
"y",
",",
"z",
")",
":",
"# Place input coordinates on simplectic honeycomb.",
"stretch_offset",
"=",
"(",
"x",
"+",
"y",
"+",
"z",
")",
"*",
"STRETCH_CONSTANT_3D",
"xs",
"=",
"x",
"+",
"stretch_offset",
"ys",
"=",
"y",
"+",
"stretch_offset",
"zs",
"=",
"z",
"+",
"stretch_offset",
"# Floor to get simplectic honeycomb coordinates of rhombohedron (stretched cube) super-cell origin.",
"xsb",
"=",
"floor",
"(",
"xs",
")",
"ysb",
"=",
"floor",
"(",
"ys",
")",
"zsb",
"=",
"floor",
"(",
"zs",
")",
"# Skew out to get actual coordinates of rhombohedron origin. We'll need these later.",
"squish_offset",
"=",
"(",
"xsb",
"+",
"ysb",
"+",
"zsb",
")",
"*",
"SQUISH_CONSTANT_3D",
"xb",
"=",
"xsb",
"+",
"squish_offset",
"yb",
"=",
"ysb",
"+",
"squish_offset",
"zb",
"=",
"zsb",
"+",
"squish_offset",
"# Compute simplectic honeycomb coordinates relative to rhombohedral origin.",
"xins",
"=",
"xs",
"-",
"xsb",
"yins",
"=",
"ys",
"-",
"ysb",
"zins",
"=",
"zs",
"-",
"zsb",
"# Sum those together to get a value that determines which region we're in.",
"in_sum",
"=",
"xins",
"+",
"yins",
"+",
"zins",
"# Positions relative to origin point.",
"dx0",
"=",
"x",
"-",
"xb",
"dy0",
"=",
"y",
"-",
"yb",
"dz0",
"=",
"z",
"-",
"zb",
"value",
"=",
"0",
"extrapolate",
"=",
"self",
".",
"_extrapolate3d",
"if",
"in_sum",
"<=",
"1",
":",
"# We're inside the tetrahedron (3-Simplex) at (0,0,0)",
"# Determine which two of (0,0,1), (0,1,0), (1,0,0) are closest.",
"a_point",
"=",
"0x01",
"a_score",
"=",
"xins",
"b_point",
"=",
"0x02",
"b_score",
"=",
"yins",
"if",
"a_score",
">=",
"b_score",
"and",
"zins",
">",
"b_score",
":",
"b_score",
"=",
"zins",
"b_point",
"=",
"0x04",
"elif",
"a_score",
"<",
"b_score",
"and",
"zins",
">",
"a_score",
":",
"a_score",
"=",
"zins",
"a_point",
"=",
"0x04",
"# Now we determine the two lattice points not part of the tetrahedron that may contribute.",
"# This depends on the closest two tetrahedral vertices, including (0,0,0)",
"wins",
"=",
"1",
"-",
"in_sum",
"if",
"wins",
">",
"a_score",
"or",
"wins",
">",
"b_score",
":",
"# (0,0,0) is one of the closest two tetrahedral vertices.",
"c",
"=",
"b_point",
"if",
"(",
"b_score",
">",
"a_score",
")",
"else",
"a_point",
"# Our other closest vertex is the closest out of a and b.",
"if",
"(",
"c",
"&",
"0x01",
")",
"==",
"0",
":",
"xsv_ext0",
"=",
"xsb",
"-",
"1",
"xsv_ext1",
"=",
"xsb",
"dx_ext0",
"=",
"dx0",
"+",
"1",
"dx_ext1",
"=",
"dx0",
"else",
":",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsb",
"+",
"1",
"dx_ext0",
"=",
"dx_ext1",
"=",
"dx0",
"-",
"1",
"if",
"(",
"c",
"&",
"0x02",
")",
"==",
"0",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy0",
"if",
"(",
"c",
"&",
"0x01",
")",
"==",
"0",
":",
"ysv_ext1",
"-=",
"1",
"dy_ext1",
"+=",
"1",
"else",
":",
"ysv_ext0",
"-=",
"1",
"dy_ext0",
"+=",
"1",
"else",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"+",
"1",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy0",
"-",
"1",
"if",
"(",
"c",
"&",
"0x04",
")",
"==",
"0",
":",
"zsv_ext0",
"=",
"zsb",
"zsv_ext1",
"=",
"zsb",
"-",
"1",
"dz_ext0",
"=",
"dz0",
"dz_ext1",
"=",
"dz0",
"+",
"1",
"else",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"+",
"1",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz0",
"-",
"1",
"else",
":",
"# (0,0,0) is not one of the closest two tetrahedral vertices.",
"c",
"=",
"(",
"a_point",
"|",
"b_point",
")",
"# Our two extra vertices are determined by the closest two.",
"if",
"(",
"c",
"&",
"0x01",
")",
"==",
"0",
":",
"xsv_ext0",
"=",
"xsb",
"xsv_ext1",
"=",
"xsb",
"-",
"1",
"dx_ext0",
"=",
"dx0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dx_ext1",
"=",
"dx0",
"+",
"1",
"-",
"SQUISH_CONSTANT_3D",
"else",
":",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsb",
"+",
"1",
"dx_ext0",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dx_ext1",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"if",
"(",
"c",
"&",
"0x02",
")",
"==",
"0",
":",
"ysv_ext0",
"=",
"ysb",
"ysv_ext1",
"=",
"ysb",
"-",
"1",
"dy_ext0",
"=",
"dy0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dy_ext1",
"=",
"dy0",
"+",
"1",
"-",
"SQUISH_CONSTANT_3D",
"else",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"+",
"1",
"dy_ext0",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dy_ext1",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"if",
"(",
"c",
"&",
"0x04",
")",
"==",
"0",
":",
"zsv_ext0",
"=",
"zsb",
"zsv_ext1",
"=",
"zsb",
"-",
"1",
"dz_ext0",
"=",
"dz0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dz_ext1",
"=",
"dz0",
"+",
"1",
"-",
"SQUISH_CONSTANT_3D",
"else",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"+",
"1",
"dz_ext0",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dz_ext1",
"=",
"dz0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"# Contribution (0,0,0)",
"attn0",
"=",
"2",
"-",
"dx0",
"*",
"dx0",
"-",
"dy0",
"*",
"dy0",
"-",
"dz0",
"*",
"dz0",
"if",
"attn0",
">",
"0",
":",
"attn0",
"*=",
"attn0",
"value",
"+=",
"attn0",
"*",
"attn0",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"0",
",",
"dx0",
",",
"dy0",
",",
"dz0",
")",
"# Contribution (1,0,0)",
"dx1",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dy1",
"=",
"dy0",
"-",
"0",
"-",
"SQUISH_CONSTANT_3D",
"dz1",
"=",
"dz0",
"-",
"0",
"-",
"SQUISH_CONSTANT_3D",
"attn1",
"=",
"2",
"-",
"dx1",
"*",
"dx1",
"-",
"dy1",
"*",
"dy1",
"-",
"dz1",
"*",
"dz1",
"if",
"attn1",
">",
"0",
":",
"attn1",
"*=",
"attn1",
"value",
"+=",
"attn1",
"*",
"attn1",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"0",
",",
"dx1",
",",
"dy1",
",",
"dz1",
")",
"# Contribution (0,1,0)",
"dx2",
"=",
"dx0",
"-",
"0",
"-",
"SQUISH_CONSTANT_3D",
"dy2",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dz2",
"=",
"dz1",
"attn2",
"=",
"2",
"-",
"dx2",
"*",
"dx2",
"-",
"dy2",
"*",
"dy2",
"-",
"dz2",
"*",
"dz2",
"if",
"attn2",
">",
"0",
":",
"attn2",
"*=",
"attn2",
"value",
"+=",
"attn2",
"*",
"attn2",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"0",
",",
"dx2",
",",
"dy2",
",",
"dz2",
")",
"# Contribution (0,0,1)",
"dx3",
"=",
"dx2",
"dy3",
"=",
"dy1",
"dz3",
"=",
"dz0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"attn3",
"=",
"2",
"-",
"dx3",
"*",
"dx3",
"-",
"dy3",
"*",
"dy3",
"-",
"dz3",
"*",
"dz3",
"if",
"attn3",
">",
"0",
":",
"attn3",
"*=",
"attn3",
"value",
"+=",
"attn3",
"*",
"attn3",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"1",
",",
"dx3",
",",
"dy3",
",",
"dz3",
")",
"elif",
"in_sum",
">=",
"2",
":",
"# We're inside the tetrahedron (3-Simplex) at (1,1,1)",
"# Determine which two tetrahedral vertices are the closest, out of (1,1,0), (1,0,1), (0,1,1) but not (1,1,1).",
"a_point",
"=",
"0x06",
"a_score",
"=",
"xins",
"b_point",
"=",
"0x05",
"b_score",
"=",
"yins",
"if",
"a_score",
"<=",
"b_score",
"and",
"zins",
"<",
"b_score",
":",
"b_score",
"=",
"zins",
"b_point",
"=",
"0x03",
"elif",
"a_score",
">",
"b_score",
"and",
"zins",
"<",
"a_score",
":",
"a_score",
"=",
"zins",
"a_point",
"=",
"0x03",
"# Now we determine the two lattice points not part of the tetrahedron that may contribute.",
"# This depends on the closest two tetrahedral vertices, including (1,1,1)",
"wins",
"=",
"3",
"-",
"in_sum",
"if",
"wins",
"<",
"a_score",
"or",
"wins",
"<",
"b_score",
":",
"# (1,1,1) is one of the closest two tetrahedral vertices.",
"c",
"=",
"b_point",
"if",
"(",
"b_score",
"<",
"a_score",
")",
"else",
"a_point",
"# Our other closest vertex is the closest out of a and b.",
"if",
"(",
"c",
"&",
"0x01",
")",
"!=",
"0",
":",
"xsv_ext0",
"=",
"xsb",
"+",
"2",
"xsv_ext1",
"=",
"xsb",
"+",
"1",
"dx_ext0",
"=",
"dx0",
"-",
"2",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"dx_ext1",
"=",
"dx0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"else",
":",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsb",
"dx_ext0",
"=",
"dx_ext1",
"=",
"dx0",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"if",
"(",
"c",
"&",
"0x02",
")",
"!=",
"0",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"+",
"1",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"if",
"(",
"c",
"&",
"0x01",
")",
"!=",
"0",
":",
"ysv_ext1",
"+=",
"1",
"dy_ext1",
"-=",
"1",
"else",
":",
"ysv_ext0",
"+=",
"1",
"dy_ext0",
"-=",
"1",
"else",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy0",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"if",
"(",
"c",
"&",
"0x04",
")",
"!=",
"0",
":",
"zsv_ext0",
"=",
"zsb",
"+",
"1",
"zsv_ext1",
"=",
"zsb",
"+",
"2",
"dz_ext0",
"=",
"dz0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"dz_ext1",
"=",
"dz0",
"-",
"2",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"else",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz0",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"else",
":",
"# (1,1,1) is not one of the closest two tetrahedral vertices.",
"c",
"=",
"(",
"a_point",
"&",
"b_point",
")",
"# Our two extra vertices are determined by the closest two.",
"if",
"(",
"c",
"&",
"0x01",
")",
"!=",
"0",
":",
"xsv_ext0",
"=",
"xsb",
"+",
"1",
"xsv_ext1",
"=",
"xsb",
"+",
"2",
"dx_ext0",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dx_ext1",
"=",
"dx0",
"-",
"2",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"else",
":",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsb",
"dx_ext0",
"=",
"dx0",
"-",
"SQUISH_CONSTANT_3D",
"dx_ext1",
"=",
"dx0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"if",
"(",
"c",
"&",
"0x02",
")",
"!=",
"0",
":",
"ysv_ext0",
"=",
"ysb",
"+",
"1",
"ysv_ext1",
"=",
"ysb",
"+",
"2",
"dy_ext0",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dy_ext1",
"=",
"dy0",
"-",
"2",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"else",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"dy_ext0",
"=",
"dy0",
"-",
"SQUISH_CONSTANT_3D",
"dy_ext1",
"=",
"dy0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"if",
"(",
"c",
"&",
"0x04",
")",
"!=",
"0",
":",
"zsv_ext0",
"=",
"zsb",
"+",
"1",
"zsv_ext1",
"=",
"zsb",
"+",
"2",
"dz_ext0",
"=",
"dz0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dz_ext1",
"=",
"dz0",
"-",
"2",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"else",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"dz_ext0",
"=",
"dz0",
"-",
"SQUISH_CONSTANT_3D",
"dz_ext1",
"=",
"dz0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"# Contribution (1,1,0)",
"dx3",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dy3",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dz3",
"=",
"dz0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"attn3",
"=",
"2",
"-",
"dx3",
"*",
"dx3",
"-",
"dy3",
"*",
"dy3",
"-",
"dz3",
"*",
"dz3",
"if",
"attn3",
">",
"0",
":",
"attn3",
"*=",
"attn3",
"value",
"+=",
"attn3",
"*",
"attn3",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"0",
",",
"dx3",
",",
"dy3",
",",
"dz3",
")",
"# Contribution (1,0,1)",
"dx2",
"=",
"dx3",
"dy2",
"=",
"dy0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dz2",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"attn2",
"=",
"2",
"-",
"dx2",
"*",
"dx2",
"-",
"dy2",
"*",
"dy2",
"-",
"dz2",
"*",
"dz2",
"if",
"attn2",
">",
"0",
":",
"attn2",
"*=",
"attn2",
"value",
"+=",
"attn2",
"*",
"attn2",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"1",
",",
"dx2",
",",
"dy2",
",",
"dz2",
")",
"# Contribution (0,1,1)",
"dx1",
"=",
"dx0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dy1",
"=",
"dy3",
"dz1",
"=",
"dz2",
"attn1",
"=",
"2",
"-",
"dx1",
"*",
"dx1",
"-",
"dy1",
"*",
"dy1",
"-",
"dz1",
"*",
"dz1",
"if",
"attn1",
">",
"0",
":",
"attn1",
"*=",
"attn1",
"value",
"+=",
"attn1",
"*",
"attn1",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"1",
",",
"dx1",
",",
"dy1",
",",
"dz1",
")",
"# Contribution (1,1,1)",
"dx0",
"=",
"dx0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"dy0",
"=",
"dy0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"dz0",
"=",
"dz0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"attn0",
"=",
"2",
"-",
"dx0",
"*",
"dx0",
"-",
"dy0",
"*",
"dy0",
"-",
"dz0",
"*",
"dz0",
"if",
"attn0",
">",
"0",
":",
"attn0",
"*=",
"attn0",
"value",
"+=",
"attn0",
"*",
"attn0",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"1",
",",
"dx0",
",",
"dy0",
",",
"dz0",
")",
"else",
":",
"# We're inside the octahedron (Rectified 3-Simplex) in between.",
"# Decide between point (0,0,1) and (1,1,0) as closest",
"p1",
"=",
"xins",
"+",
"yins",
"if",
"p1",
">",
"1",
":",
"a_score",
"=",
"p1",
"-",
"1",
"a_point",
"=",
"0x03",
"a_is_further_side",
"=",
"True",
"else",
":",
"a_score",
"=",
"1",
"-",
"p1",
"a_point",
"=",
"0x04",
"a_is_further_side",
"=",
"False",
"# Decide between point (0,1,0) and (1,0,1) as closest",
"p2",
"=",
"xins",
"+",
"zins",
"if",
"p2",
">",
"1",
":",
"b_score",
"=",
"p2",
"-",
"1",
"b_point",
"=",
"0x05",
"b_is_further_side",
"=",
"True",
"else",
":",
"b_score",
"=",
"1",
"-",
"p2",
"b_point",
"=",
"0x02",
"b_is_further_side",
"=",
"False",
"# The closest out of the two (1,0,0) and (0,1,1) will replace the furthest out of the two decided above, if closer.",
"p3",
"=",
"yins",
"+",
"zins",
"if",
"p3",
">",
"1",
":",
"score",
"=",
"p3",
"-",
"1",
"if",
"a_score",
"<=",
"b_score",
"and",
"a_score",
"<",
"score",
":",
"a_point",
"=",
"0x06",
"a_is_further_side",
"=",
"True",
"elif",
"a_score",
">",
"b_score",
"and",
"b_score",
"<",
"score",
":",
"b_point",
"=",
"0x06",
"b_is_further_side",
"=",
"True",
"else",
":",
"score",
"=",
"1",
"-",
"p3",
"if",
"a_score",
"<=",
"b_score",
"and",
"a_score",
"<",
"score",
":",
"a_point",
"=",
"0x01",
"a_is_further_side",
"=",
"False",
"elif",
"a_score",
">",
"b_score",
"and",
"b_score",
"<",
"score",
":",
"b_point",
"=",
"0x01",
"b_is_further_side",
"=",
"False",
"# Where each of the two closest points are determines how the extra two vertices are calculated.",
"if",
"a_is_further_side",
"==",
"b_is_further_side",
":",
"if",
"a_is_further_side",
":",
"# Both closest points on (1,1,1) side",
"# One of the two extra points is (1,1,1)",
"dx_ext0",
"=",
"dx0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"dy_ext0",
"=",
"dy0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"dz_ext0",
"=",
"dz0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_3D",
"xsv_ext0",
"=",
"xsb",
"+",
"1",
"ysv_ext0",
"=",
"ysb",
"+",
"1",
"zsv_ext0",
"=",
"zsb",
"+",
"1",
"# Other extra point is based on the shared axis.",
"c",
"=",
"(",
"a_point",
"&",
"b_point",
")",
"if",
"(",
"c",
"&",
"0x01",
")",
"!=",
"0",
":",
"dx_ext1",
"=",
"dx0",
"-",
"2",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dy_ext1",
"=",
"dy0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dz_ext1",
"=",
"dz0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"xsv_ext1",
"=",
"xsb",
"+",
"2",
"ysv_ext1",
"=",
"ysb",
"zsv_ext1",
"=",
"zsb",
"elif",
"(",
"c",
"&",
"0x02",
")",
"!=",
"0",
":",
"dx_ext1",
"=",
"dx0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dy_ext1",
"=",
"dy0",
"-",
"2",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dz_ext1",
"=",
"dz0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"xsv_ext1",
"=",
"xsb",
"ysv_ext1",
"=",
"ysb",
"+",
"2",
"zsv_ext1",
"=",
"zsb",
"else",
":",
"dx_ext1",
"=",
"dx0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dy_ext1",
"=",
"dy0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dz_ext1",
"=",
"dz0",
"-",
"2",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"xsv_ext1",
"=",
"xsb",
"ysv_ext1",
"=",
"ysb",
"zsv_ext1",
"=",
"zsb",
"+",
"2",
"else",
":",
"# Both closest points on (0,0,0) side",
"# One of the two extra points is (0,0,0)",
"dx_ext0",
"=",
"dx0",
"dy_ext0",
"=",
"dy0",
"dz_ext0",
"=",
"dz0",
"xsv_ext0",
"=",
"xsb",
"ysv_ext0",
"=",
"ysb",
"zsv_ext0",
"=",
"zsb",
"# Other extra point is based on the omitted axis.",
"c",
"=",
"(",
"a_point",
"|",
"b_point",
")",
"if",
"(",
"c",
"&",
"0x01",
")",
"==",
"0",
":",
"dx_ext1",
"=",
"dx0",
"+",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dy_ext1",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dz_ext1",
"=",
"dz0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"xsv_ext1",
"=",
"xsb",
"-",
"1",
"ysv_ext1",
"=",
"ysb",
"+",
"1",
"zsv_ext1",
"=",
"zsb",
"+",
"1",
"elif",
"(",
"c",
"&",
"0x02",
")",
"==",
"0",
":",
"dx_ext1",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dy_ext1",
"=",
"dy0",
"+",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dz_ext1",
"=",
"dz0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"xsv_ext1",
"=",
"xsb",
"+",
"1",
"ysv_ext1",
"=",
"ysb",
"-",
"1",
"zsv_ext1",
"=",
"zsb",
"+",
"1",
"else",
":",
"dx_ext1",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dy_ext1",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dz_ext1",
"=",
"dz0",
"+",
"1",
"-",
"SQUISH_CONSTANT_3D",
"xsv_ext1",
"=",
"xsb",
"+",
"1",
"ysv_ext1",
"=",
"ysb",
"+",
"1",
"zsv_ext1",
"=",
"zsb",
"-",
"1",
"else",
":",
"# One point on (0,0,0) side, one point on (1,1,1) side",
"if",
"a_is_further_side",
":",
"c1",
"=",
"a_point",
"c2",
"=",
"b_point",
"else",
":",
"c1",
"=",
"b_point",
"c2",
"=",
"a_point",
"# One contribution is a _permutation of (1,1,-1)",
"if",
"(",
"c1",
"&",
"0x01",
")",
"==",
"0",
":",
"dx_ext0",
"=",
"dx0",
"+",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dy_ext0",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dz_ext0",
"=",
"dz0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"xsv_ext0",
"=",
"xsb",
"-",
"1",
"ysv_ext0",
"=",
"ysb",
"+",
"1",
"zsv_ext0",
"=",
"zsb",
"+",
"1",
"elif",
"(",
"c1",
"&",
"0x02",
")",
"==",
"0",
":",
"dx_ext0",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dy_ext0",
"=",
"dy0",
"+",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dz_ext0",
"=",
"dz0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"xsv_ext0",
"=",
"xsb",
"+",
"1",
"ysv_ext0",
"=",
"ysb",
"-",
"1",
"zsv_ext0",
"=",
"zsb",
"+",
"1",
"else",
":",
"dx_ext0",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dy_ext0",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dz_ext0",
"=",
"dz0",
"+",
"1",
"-",
"SQUISH_CONSTANT_3D",
"xsv_ext0",
"=",
"xsb",
"+",
"1",
"ysv_ext0",
"=",
"ysb",
"+",
"1",
"zsv_ext0",
"=",
"zsb",
"-",
"1",
"# One contribution is a _permutation of (0,0,2)",
"dx_ext1",
"=",
"dx0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dy_ext1",
"=",
"dy0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dz_ext1",
"=",
"dz0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"xsv_ext1",
"=",
"xsb",
"ysv_ext1",
"=",
"ysb",
"zsv_ext1",
"=",
"zsb",
"if",
"(",
"c2",
"&",
"0x01",
")",
"!=",
"0",
":",
"dx_ext1",
"-=",
"2",
"xsv_ext1",
"+=",
"2",
"elif",
"(",
"c2",
"&",
"0x02",
")",
"!=",
"0",
":",
"dy_ext1",
"-=",
"2",
"ysv_ext1",
"+=",
"2",
"else",
":",
"dz_ext1",
"-=",
"2",
"zsv_ext1",
"+=",
"2",
"# Contribution (1,0,0)",
"dx1",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dy1",
"=",
"dy0",
"-",
"0",
"-",
"SQUISH_CONSTANT_3D",
"dz1",
"=",
"dz0",
"-",
"0",
"-",
"SQUISH_CONSTANT_3D",
"attn1",
"=",
"2",
"-",
"dx1",
"*",
"dx1",
"-",
"dy1",
"*",
"dy1",
"-",
"dz1",
"*",
"dz1",
"if",
"attn1",
">",
"0",
":",
"attn1",
"*=",
"attn1",
"value",
"+=",
"attn1",
"*",
"attn1",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"0",
",",
"dx1",
",",
"dy1",
",",
"dz1",
")",
"# Contribution (0,1,0)",
"dx2",
"=",
"dx0",
"-",
"0",
"-",
"SQUISH_CONSTANT_3D",
"dy2",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"dz2",
"=",
"dz1",
"attn2",
"=",
"2",
"-",
"dx2",
"*",
"dx2",
"-",
"dy2",
"*",
"dy2",
"-",
"dz2",
"*",
"dz2",
"if",
"attn2",
">",
"0",
":",
"attn2",
"*=",
"attn2",
"value",
"+=",
"attn2",
"*",
"attn2",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"0",
",",
"dx2",
",",
"dy2",
",",
"dz2",
")",
"# Contribution (0,0,1)",
"dx3",
"=",
"dx2",
"dy3",
"=",
"dy1",
"dz3",
"=",
"dz0",
"-",
"1",
"-",
"SQUISH_CONSTANT_3D",
"attn3",
"=",
"2",
"-",
"dx3",
"*",
"dx3",
"-",
"dy3",
"*",
"dy3",
"-",
"dz3",
"*",
"dz3",
"if",
"attn3",
">",
"0",
":",
"attn3",
"*=",
"attn3",
"value",
"+=",
"attn3",
"*",
"attn3",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"1",
",",
"dx3",
",",
"dy3",
",",
"dz3",
")",
"# Contribution (1,1,0)",
"dx4",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dy4",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dz4",
"=",
"dz0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"attn4",
"=",
"2",
"-",
"dx4",
"*",
"dx4",
"-",
"dy4",
"*",
"dy4",
"-",
"dz4",
"*",
"dz4",
"if",
"attn4",
">",
"0",
":",
"attn4",
"*=",
"attn4",
"value",
"+=",
"attn4",
"*",
"attn4",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"0",
",",
"dx4",
",",
"dy4",
",",
"dz4",
")",
"# Contribution (1,0,1)",
"dx5",
"=",
"dx4",
"dy5",
"=",
"dy0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dz5",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"attn5",
"=",
"2",
"-",
"dx5",
"*",
"dx5",
"-",
"dy5",
"*",
"dy5",
"-",
"dz5",
"*",
"dz5",
"if",
"attn5",
">",
"0",
":",
"attn5",
"*=",
"attn5",
"value",
"+=",
"attn5",
"*",
"attn5",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"1",
",",
"dx5",
",",
"dy5",
",",
"dz5",
")",
"# Contribution (0,1,1)",
"dx6",
"=",
"dx0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_3D",
"dy6",
"=",
"dy4",
"dz6",
"=",
"dz5",
"attn6",
"=",
"2",
"-",
"dx6",
"*",
"dx6",
"-",
"dy6",
"*",
"dy6",
"-",
"dz6",
"*",
"dz6",
"if",
"attn6",
">",
"0",
":",
"attn6",
"*=",
"attn6",
"value",
"+=",
"attn6",
"*",
"attn6",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"1",
",",
"dx6",
",",
"dy6",
",",
"dz6",
")",
"# First extra vertex",
"attn_ext0",
"=",
"2",
"-",
"dx_ext0",
"*",
"dx_ext0",
"-",
"dy_ext0",
"*",
"dy_ext0",
"-",
"dz_ext0",
"*",
"dz_ext0",
"if",
"attn_ext0",
">",
"0",
":",
"attn_ext0",
"*=",
"attn_ext0",
"value",
"+=",
"attn_ext0",
"*",
"attn_ext0",
"*",
"extrapolate",
"(",
"xsv_ext0",
",",
"ysv_ext0",
",",
"zsv_ext0",
",",
"dx_ext0",
",",
"dy_ext0",
",",
"dz_ext0",
")",
"# Second extra vertex",
"attn_ext1",
"=",
"2",
"-",
"dx_ext1",
"*",
"dx_ext1",
"-",
"dy_ext1",
"*",
"dy_ext1",
"-",
"dz_ext1",
"*",
"dz_ext1",
"if",
"attn_ext1",
">",
"0",
":",
"attn_ext1",
"*=",
"attn_ext1",
"value",
"+=",
"attn_ext1",
"*",
"attn_ext1",
"*",
"extrapolate",
"(",
"xsv_ext1",
",",
"ysv_ext1",
",",
"zsv_ext1",
",",
"dx_ext1",
",",
"dy_ext1",
",",
"dz_ext1",
")",
"return",
"value",
"/",
"NORM_CONSTANT_3D"
] | Generate 3D OpenSimplex noise from X,Y,Z coordinates. | [
"Generate",
"3D",
"OpenSimplex",
"noise",
"from",
"X",
"Y",
"Z",
"coordinates",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/opensimplex.py#L247-L740 | valid |
aleju/imgaug | imgaug/external/opensimplex.py | OpenSimplex.noise4d | def noise4d(self, x, y, z, w):
"""
Generate 4D OpenSimplex noise from X,Y,Z,W coordinates.
"""
# Place input coordinates on simplectic honeycomb.
stretch_offset = (x + y + z + w) * STRETCH_CONSTANT_4D
xs = x + stretch_offset
ys = y + stretch_offset
zs = z + stretch_offset
ws = w + stretch_offset
# Floor to get simplectic honeycomb coordinates of rhombo-hypercube super-cell origin.
xsb = floor(xs)
ysb = floor(ys)
zsb = floor(zs)
wsb = floor(ws)
# Skew out to get actual coordinates of stretched rhombo-hypercube origin. We'll need these later.
squish_offset = (xsb + ysb + zsb + wsb) * SQUISH_CONSTANT_4D
xb = xsb + squish_offset
yb = ysb + squish_offset
zb = zsb + squish_offset
wb = wsb + squish_offset
# Compute simplectic honeycomb coordinates relative to rhombo-hypercube origin.
xins = xs - xsb
yins = ys - ysb
zins = zs - zsb
wins = ws - wsb
# Sum those together to get a value that determines which region we're in.
in_sum = xins + yins + zins + wins
# Positions relative to origin po.
dx0 = x - xb
dy0 = y - yb
dz0 = z - zb
dw0 = w - wb
value = 0
extrapolate = self._extrapolate4d
if in_sum <= 1: # We're inside the pentachoron (4-Simplex) at (0,0,0,0)
# Determine which two of (0,0,0,1), (0,0,1,0), (0,1,0,0), (1,0,0,0) are closest.
a_po = 0x01
a_score = xins
b_po = 0x02
b_score = yins
if a_score >= b_score and zins > b_score:
b_score = zins
b_po = 0x04
elif a_score < b_score and zins > a_score:
a_score = zins
a_po = 0x04
if a_score >= b_score and wins > b_score:
b_score = wins
b_po = 0x08
elif a_score < b_score and wins > a_score:
a_score = wins
a_po = 0x08
# Now we determine the three lattice pos not part of the pentachoron that may contribute.
# This depends on the closest two pentachoron vertices, including (0,0,0,0)
uins = 1 - in_sum
if uins > a_score or uins > b_score: # (0,0,0,0) is one of the closest two pentachoron vertices.
c = b_po if (b_score > a_score) else a_po # Our other closest vertex is the closest out of a and b.
if (c & 0x01) == 0:
xsv_ext0 = xsb - 1
xsv_ext1 = xsv_ext2 = xsb
dx_ext0 = dx0 + 1
dx_ext1 = dx_ext2 = dx0
else:
xsv_ext0 = xsv_ext1 = xsv_ext2 = xsb + 1
dx_ext0 = dx_ext1 = dx_ext2 = dx0 - 1
if (c & 0x02) == 0:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb
dy_ext0 = dy_ext1 = dy_ext2 = dy0
if (c & 0x01) == 0x01:
ysv_ext0 -= 1
dy_ext0 += 1
else:
ysv_ext1 -= 1
dy_ext1 += 1
else:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb + 1
dy_ext0 = dy_ext1 = dy_ext2 = dy0 - 1
if (c & 0x04) == 0:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb
dz_ext0 = dz_ext1 = dz_ext2 = dz0
if (c & 0x03) != 0:
if (c & 0x03) == 0x03:
zsv_ext0 -= 1
dz_ext0 += 1
else:
zsv_ext1 -= 1
dz_ext1 += 1
else:
zsv_ext2 -= 1
dz_ext2 += 1
else:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb + 1
dz_ext0 = dz_ext1 = dz_ext2 = dz0 - 1
if (c & 0x08) == 0:
wsv_ext0 = wsv_ext1 = wsb
wsv_ext2 = wsb - 1
dw_ext0 = dw_ext1 = dw0
dw_ext2 = dw0 + 1
else:
wsv_ext0 = wsv_ext1 = wsv_ext2 = wsb + 1
dw_ext0 = dw_ext1 = dw_ext2 = dw0 - 1
else: # (0,0,0,0) is not one of the closest two pentachoron vertices.
c = (a_po | b_po) # Our three extra vertices are determined by the closest two.
if (c & 0x01) == 0:
xsv_ext0 = xsv_ext2 = xsb
xsv_ext1 = xsb - 1
dx_ext0 = dx0 - 2 * SQUISH_CONSTANT_4D
dx_ext1 = dx0 + 1 - SQUISH_CONSTANT_4D
dx_ext2 = dx0 - SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsv_ext2 = xsb + 1
dx_ext0 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dx_ext1 = dx_ext2 = dx0 - 1 - SQUISH_CONSTANT_4D
if (c & 0x02) == 0:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb
dy_ext0 = dy0 - 2 * SQUISH_CONSTANT_4D
dy_ext1 = dy_ext2 = dy0 - SQUISH_CONSTANT_4D
if (c & 0x01) == 0x01:
ysv_ext1 -= 1
dy_ext1 += 1
else:
ysv_ext2 -= 1
dy_ext2 += 1
else:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb + 1
dy_ext0 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dy_ext1 = dy_ext2 = dy0 - 1 - SQUISH_CONSTANT_4D
if (c & 0x04) == 0:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb
dz_ext0 = dz0 - 2 * SQUISH_CONSTANT_4D
dz_ext1 = dz_ext2 = dz0 - SQUISH_CONSTANT_4D
if (c & 0x03) == 0x03:
zsv_ext1 -= 1
dz_ext1 += 1
else:
zsv_ext2 -= 1
dz_ext2 += 1
else:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb + 1
dz_ext0 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dz_ext1 = dz_ext2 = dz0 - 1 - SQUISH_CONSTANT_4D
if (c & 0x08) == 0:
wsv_ext0 = wsv_ext1 = wsb
wsv_ext2 = wsb - 1
dw_ext0 = dw0 - 2 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 - SQUISH_CONSTANT_4D
dw_ext2 = dw0 + 1 - SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsv_ext2 = wsb + 1
dw_ext0 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
dw_ext1 = dw_ext2 = dw0 - 1 - SQUISH_CONSTANT_4D
# Contribution (0,0,0,0)
attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0 - dw0 * dw0
if attn0 > 0:
attn0 *= attn0
value += attn0 * attn0 * extrapolate(xsb + 0, ysb + 0, zsb + 0, wsb + 0, dx0, dy0, dz0, dw0)
# Contribution (1,0,0,0)
dx1 = dx0 - 1 - SQUISH_CONSTANT_4D
dy1 = dy0 - 0 - SQUISH_CONSTANT_4D
dz1 = dz0 - 0 - SQUISH_CONSTANT_4D
dw1 = dw0 - 0 - SQUISH_CONSTANT_4D
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1 - dw1 * dw1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, zsb + 0, wsb + 0, dx1, dy1, dz1, dw1)
# Contribution (0,1,0,0)
dx2 = dx0 - 0 - SQUISH_CONSTANT_4D
dy2 = dy0 - 1 - SQUISH_CONSTANT_4D
dz2 = dz1
dw2 = dw1
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2 - dw2 * dw2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, wsb + 0, dx2, dy2, dz2, dw2)
# Contribution (0,0,1,0)
dx3 = dx2
dy3 = dy1
dz3 = dz0 - 1 - SQUISH_CONSTANT_4D
dw3 = dw1
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3 - dw3 * dw3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, wsb + 0, dx3, dy3, dz3, dw3)
# Contribution (0,0,0,1)
dx4 = dx2
dy4 = dy1
dz4 = dz1
dw4 = dw0 - 1 - SQUISH_CONSTANT_4D
attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4 - dw4 * dw4
if attn4 > 0:
attn4 *= attn4
value += attn4 * attn4 * extrapolate(xsb + 0, ysb + 0, zsb + 0, wsb + 1, dx4, dy4, dz4, dw4)
elif in_sum >= 3: # We're inside the pentachoron (4-Simplex) at (1,1,1,1)
# Determine which two of (1,1,1,0), (1,1,0,1), (1,0,1,1), (0,1,1,1) are closest.
a_po = 0x0E
a_score = xins
b_po = 0x0D
b_score = yins
if a_score <= b_score and zins < b_score:
b_score = zins
b_po = 0x0B
elif a_score > b_score and zins < a_score:
a_score = zins
a_po = 0x0B
if a_score <= b_score and wins < b_score:
b_score = wins
b_po = 0x07
elif a_score > b_score and wins < a_score:
a_score = wins
a_po = 0x07
# Now we determine the three lattice pos not part of the pentachoron that may contribute.
# This depends on the closest two pentachoron vertices, including (0,0,0,0)
uins = 4 - in_sum
if uins < a_score or uins < b_score: # (1,1,1,1) is one of the closest two pentachoron vertices.
c = b_po if (b_score < a_score) else a_po # Our other closest vertex is the closest out of a and b.
if (c & 0x01) != 0:
xsv_ext0 = xsb + 2
xsv_ext1 = xsv_ext2 = xsb + 1
dx_ext0 = dx0 - 2 - 4 * SQUISH_CONSTANT_4D
dx_ext1 = dx_ext2 = dx0 - 1 - 4 * SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsv_ext2 = xsb
dx_ext0 = dx_ext1 = dx_ext2 = dx0 - 4 * SQUISH_CONSTANT_4D
if (c & 0x02) != 0:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb + 1
dy_ext0 = dy_ext1 = dy_ext2 = dy0 - 1 - 4 * SQUISH_CONSTANT_4D
if (c & 0x01) != 0:
ysv_ext1 += 1
dy_ext1 -= 1
else:
ysv_ext0 += 1
dy_ext0 -= 1
else:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb
dy_ext0 = dy_ext1 = dy_ext2 = dy0 - 4 * SQUISH_CONSTANT_4D
if (c & 0x04) != 0:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb + 1
dz_ext0 = dz_ext1 = dz_ext2 = dz0 - 1 - 4 * SQUISH_CONSTANT_4D
if (c & 0x03) != 0x03:
if (c & 0x03) == 0:
zsv_ext0 += 1
dz_ext0 -= 1
else:
zsv_ext1 += 1
dz_ext1 -= 1
else:
zsv_ext2 += 1
dz_ext2 -= 1
else:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb
dz_ext0 = dz_ext1 = dz_ext2 = dz0 - 4 * SQUISH_CONSTANT_4D
if (c & 0x08) != 0:
wsv_ext0 = wsv_ext1 = wsb + 1
wsv_ext2 = wsb + 2
dw_ext0 = dw_ext1 = dw0 - 1 - 4 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 2 - 4 * SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsv_ext2 = wsb
dw_ext0 = dw_ext1 = dw_ext2 = dw0 - 4 * SQUISH_CONSTANT_4D
else: # (1,1,1,1) is not one of the closest two pentachoron vertices.
c = (a_po & b_po) # Our three extra vertices are determined by the closest two.
if (c & 0x01) != 0:
xsv_ext0 = xsv_ext2 = xsb + 1
xsv_ext1 = xsb + 2
dx_ext0 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dx_ext1 = dx0 - 2 - 3 * SQUISH_CONSTANT_4D
dx_ext2 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsv_ext2 = xsb
dx_ext0 = dx0 - 2 * SQUISH_CONSTANT_4D
dx_ext1 = dx_ext2 = dx0 - 3 * SQUISH_CONSTANT_4D
if (c & 0x02) != 0:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb + 1
dy_ext0 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dy_ext1 = dy_ext2 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D
if (c & 0x01) != 0:
ysv_ext2 += 1
dy_ext2 -= 1
else:
ysv_ext1 += 1
dy_ext1 -= 1
else:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb
dy_ext0 = dy0 - 2 * SQUISH_CONSTANT_4D
dy_ext1 = dy_ext2 = dy0 - 3 * SQUISH_CONSTANT_4D
if (c & 0x04) != 0:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb + 1
dz_ext0 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dz_ext1 = dz_ext2 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D
if (c & 0x03) != 0:
zsv_ext2 += 1
dz_ext2 -= 1
else:
zsv_ext1 += 1
dz_ext1 -= 1
else:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb
dz_ext0 = dz0 - 2 * SQUISH_CONSTANT_4D
dz_ext1 = dz_ext2 = dz0 - 3 * SQUISH_CONSTANT_4D
if (c & 0x08) != 0:
wsv_ext0 = wsv_ext1 = wsb + 1
wsv_ext2 = wsb + 2
dw_ext0 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 2 - 3 * SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsv_ext2 = wsb
dw_ext0 = dw0 - 2 * SQUISH_CONSTANT_4D
dw_ext1 = dw_ext2 = dw0 - 3 * SQUISH_CONSTANT_4D
# Contribution (1,1,1,0)
dx4 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D
dy4 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D
dz4 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D
dw4 = dw0 - 3 * SQUISH_CONSTANT_4D
attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4 - dw4 * dw4
if attn4 > 0:
attn4 *= attn4
value += attn4 * attn4 * extrapolate(xsb + 1, ysb + 1, zsb + 1, wsb + 0, dx4, dy4, dz4, dw4)
# Contribution (1,1,0,1)
dx3 = dx4
dy3 = dy4
dz3 = dz0 - 3 * SQUISH_CONSTANT_4D
dw3 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3 - dw3 * dw3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 1, ysb + 1, zsb + 0, wsb + 1, dx3, dy3, dz3, dw3)
# Contribution (1,0,1,1)
dx2 = dx4
dy2 = dy0 - 3 * SQUISH_CONSTANT_4D
dz2 = dz4
dw2 = dw3
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2 - dw2 * dw2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 1, ysb + 0, zsb + 1, wsb + 1, dx2, dy2, dz2, dw2)
# Contribution (0,1,1,1)
dx1 = dx0 - 3 * SQUISH_CONSTANT_4D
dz1 = dz4
dy1 = dy4
dw1 = dw3
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1 - dw1 * dw1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 0, ysb + 1, zsb + 1, wsb + 1, dx1, dy1, dz1, dw1)
# Contribution (1,1,1,1)
dx0 = dx0 - 1 - 4 * SQUISH_CONSTANT_4D
dy0 = dy0 - 1 - 4 * SQUISH_CONSTANT_4D
dz0 = dz0 - 1 - 4 * SQUISH_CONSTANT_4D
dw0 = dw0 - 1 - 4 * SQUISH_CONSTANT_4D
attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0 - dw0 * dw0
if attn0 > 0:
attn0 *= attn0
value += attn0 * attn0 * extrapolate(xsb + 1, ysb + 1, zsb + 1, wsb + 1, dx0, dy0, dz0, dw0)
elif in_sum <= 2: # We're inside the first dispentachoron (Rectified 4-Simplex)
a_is_bigger_side = True
b_is_bigger_side = True
# Decide between (1,1,0,0) and (0,0,1,1)
if xins + yins > zins + wins:
a_score = xins + yins
a_po = 0x03
else:
a_score = zins + wins
a_po = 0x0C
# Decide between (1,0,1,0) and (0,1,0,1)
if xins + zins > yins + wins:
b_score = xins + zins
b_po = 0x05
else:
b_score = yins + wins
b_po = 0x0A
# Closer between (1,0,0,1) and (0,1,1,0) will replace the further of a and b, if closer.
if xins + wins > yins + zins:
score = xins + wins
if a_score >= b_score and score > b_score:
b_score = score
b_po = 0x09
elif a_score < b_score and score > a_score:
a_score = score
a_po = 0x09
else:
score = yins + zins
if a_score >= b_score and score > b_score:
b_score = score
b_po = 0x06
elif a_score < b_score and score > a_score:
a_score = score
a_po = 0x06
# Decide if (1,0,0,0) is closer.
p1 = 2 - in_sum + xins
if a_score >= b_score and p1 > b_score:
b_score = p1
b_po = 0x01
b_is_bigger_side = False
elif a_score < b_score and p1 > a_score:
a_score = p1
a_po = 0x01
a_is_bigger_side = False
# Decide if (0,1,0,0) is closer.
p2 = 2 - in_sum + yins
if a_score >= b_score and p2 > b_score:
b_score = p2
b_po = 0x02
b_is_bigger_side = False
elif a_score < b_score and p2 > a_score:
a_score = p2
a_po = 0x02
a_is_bigger_side = False
# Decide if (0,0,1,0) is closer.
p3 = 2 - in_sum + zins
if a_score >= b_score and p3 > b_score:
b_score = p3
b_po = 0x04
b_is_bigger_side = False
elif a_score < b_score and p3 > a_score:
a_score = p3
a_po = 0x04
a_is_bigger_side = False
# Decide if (0,0,0,1) is closer.
p4 = 2 - in_sum + wins
if a_score >= b_score and p4 > b_score:
b_po = 0x08
b_is_bigger_side = False
elif a_score < b_score and p4 > a_score:
a_po = 0x08
a_is_bigger_side = False
# Where each of the two closest pos are determines how the extra three vertices are calculated.
if a_is_bigger_side == b_is_bigger_side:
if a_is_bigger_side: # Both closest pos on the bigger side
c1 = (a_po | b_po)
c2 = (a_po & b_po)
if (c1 & 0x01) == 0:
xsv_ext0 = xsb
xsv_ext1 = xsb - 1
dx_ext0 = dx0 - 3 * SQUISH_CONSTANT_4D
dx_ext1 = dx0 + 1 - 2 * SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsb + 1
dx_ext0 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D
dx_ext1 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
if (c1 & 0x02) == 0:
ysv_ext0 = ysb
ysv_ext1 = ysb - 1
dy_ext0 = dy0 - 3 * SQUISH_CONSTANT_4D
dy_ext1 = dy0 + 1 - 2 * SQUISH_CONSTANT_4D
else:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D
dy_ext1 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
if (c1 & 0x04) == 0:
zsv_ext0 = zsb
zsv_ext1 = zsb - 1
dz_ext0 = dz0 - 3 * SQUISH_CONSTANT_4D
dz_ext1 = dz0 + 1 - 2 * SQUISH_CONSTANT_4D
else:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D
dz_ext1 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
if (c1 & 0x08) == 0:
wsv_ext0 = wsb
wsv_ext1 = wsb - 1
dw_ext0 = dw0 - 3 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 + 1 - 2 * SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsb + 1
dw_ext0 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
# One combination is a _permutation of (0,0,0,2) based on c2
xsv_ext2 = xsb
ysv_ext2 = ysb
zsv_ext2 = zsb
wsv_ext2 = wsb
dx_ext2 = dx0 - 2 * SQUISH_CONSTANT_4D
dy_ext2 = dy0 - 2 * SQUISH_CONSTANT_4D
dz_ext2 = dz0 - 2 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 2 * SQUISH_CONSTANT_4D
if (c2 & 0x01) != 0:
xsv_ext2 += 2
dx_ext2 -= 2
elif (c2 & 0x02) != 0:
ysv_ext2 += 2
dy_ext2 -= 2
elif (c2 & 0x04) != 0:
zsv_ext2 += 2
dz_ext2 -= 2
else:
wsv_ext2 += 2
dw_ext2 -= 2
else: # Both closest pos on the smaller side
# One of the two extra pos is (0,0,0,0)
xsv_ext2 = xsb
ysv_ext2 = ysb
zsv_ext2 = zsb
wsv_ext2 = wsb
dx_ext2 = dx0
dy_ext2 = dy0
dz_ext2 = dz0
dw_ext2 = dw0
# Other two pos are based on the omitted axes.
c = (a_po | b_po)
if (c & 0x01) == 0:
xsv_ext0 = xsb - 1
xsv_ext1 = xsb
dx_ext0 = dx0 + 1 - SQUISH_CONSTANT_4D
dx_ext1 = dx0 - SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsb + 1
dx_ext0 = dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_4D
if (c & 0x02) == 0:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy_ext1 = dy0 - SQUISH_CONSTANT_4D
if (c & 0x01) == 0x01:
ysv_ext0 -= 1
dy_ext0 += 1
else:
ysv_ext1 -= 1
dy_ext1 += 1
else:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_4D
if (c & 0x04) == 0:
zsv_ext0 = zsv_ext1 = zsb
dz_ext0 = dz_ext1 = dz0 - SQUISH_CONSTANT_4D
if (c & 0x03) == 0x03:
zsv_ext0 -= 1
dz_ext0 += 1
else:
zsv_ext1 -= 1
dz_ext1 += 1
else:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_4D
if (c & 0x08) == 0:
wsv_ext0 = wsb
wsv_ext1 = wsb - 1
dw_ext0 = dw0 - SQUISH_CONSTANT_4D
dw_ext1 = dw0 + 1 - SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsb + 1
dw_ext0 = dw_ext1 = dw0 - 1 - SQUISH_CONSTANT_4D
else: # One po on each "side"
if a_is_bigger_side:
c1 = a_po
c2 = b_po
else:
c1 = b_po
c2 = a_po
# Two contributions are the bigger-sided po with each 0 replaced with -1.
if (c1 & 0x01) == 0:
xsv_ext0 = xsb - 1
xsv_ext1 = xsb
dx_ext0 = dx0 + 1 - SQUISH_CONSTANT_4D
dx_ext1 = dx0 - SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsb + 1
dx_ext0 = dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_4D
if (c1 & 0x02) == 0:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy_ext1 = dy0 - SQUISH_CONSTANT_4D
if (c1 & 0x01) == 0x01:
ysv_ext0 -= 1
dy_ext0 += 1
else:
ysv_ext1 -= 1
dy_ext1 += 1
else:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_4D
if (c1 & 0x04) == 0:
zsv_ext0 = zsv_ext1 = zsb
dz_ext0 = dz_ext1 = dz0 - SQUISH_CONSTANT_4D
if (c1 & 0x03) == 0x03:
zsv_ext0 -= 1
dz_ext0 += 1
else:
zsv_ext1 -= 1
dz_ext1 += 1
else:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_4D
if (c1 & 0x08) == 0:
wsv_ext0 = wsb
wsv_ext1 = wsb - 1
dw_ext0 = dw0 - SQUISH_CONSTANT_4D
dw_ext1 = dw0 + 1 - SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsb + 1
dw_ext0 = dw_ext1 = dw0 - 1 - SQUISH_CONSTANT_4D
# One contribution is a _permutation of (0,0,0,2) based on the smaller-sided po
xsv_ext2 = xsb
ysv_ext2 = ysb
zsv_ext2 = zsb
wsv_ext2 = wsb
dx_ext2 = dx0 - 2 * SQUISH_CONSTANT_4D
dy_ext2 = dy0 - 2 * SQUISH_CONSTANT_4D
dz_ext2 = dz0 - 2 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 2 * SQUISH_CONSTANT_4D
if (c2 & 0x01) != 0:
xsv_ext2 += 2
dx_ext2 -= 2
elif (c2 & 0x02) != 0:
ysv_ext2 += 2
dy_ext2 -= 2
elif (c2 & 0x04) != 0:
zsv_ext2 += 2
dz_ext2 -= 2
else:
wsv_ext2 += 2
dw_ext2 -= 2
# Contribution (1,0,0,0)
dx1 = dx0 - 1 - SQUISH_CONSTANT_4D
dy1 = dy0 - 0 - SQUISH_CONSTANT_4D
dz1 = dz0 - 0 - SQUISH_CONSTANT_4D
dw1 = dw0 - 0 - SQUISH_CONSTANT_4D
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1 - dw1 * dw1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, zsb + 0, wsb + 0, dx1, dy1, dz1, dw1)
# Contribution (0,1,0,0)
dx2 = dx0 - 0 - SQUISH_CONSTANT_4D
dy2 = dy0 - 1 - SQUISH_CONSTANT_4D
dz2 = dz1
dw2 = dw1
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2 - dw2 * dw2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, wsb + 0, dx2, dy2, dz2, dw2)
# Contribution (0,0,1,0)
dx3 = dx2
dy3 = dy1
dz3 = dz0 - 1 - SQUISH_CONSTANT_4D
dw3 = dw1
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3 - dw3 * dw3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, wsb + 0, dx3, dy3, dz3, dw3)
# Contribution (0,0,0,1)
dx4 = dx2
dy4 = dy1
dz4 = dz1
dw4 = dw0 - 1 - SQUISH_CONSTANT_4D
attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4 - dw4 * dw4
if attn4 > 0:
attn4 *= attn4
value += attn4 * attn4 * extrapolate(xsb + 0, ysb + 0, zsb + 0, wsb + 1, dx4, dy4, dz4, dw4)
# Contribution (1,1,0,0)
dx5 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy5 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz5 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D
dw5 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D
attn5 = 2 - dx5 * dx5 - dy5 * dy5 - dz5 * dz5 - dw5 * dw5
if attn5 > 0:
attn5 *= attn5
value += attn5 * attn5 * extrapolate(xsb + 1, ysb + 1, zsb + 0, wsb + 0, dx5, dy5, dz5, dw5)
# Contribution (1,0,1,0)
dx6 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy6 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D
dz6 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw6 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D
attn6 = 2 - dx6 * dx6 - dy6 * dy6 - dz6 * dz6 - dw6 * dw6
if attn6 > 0:
attn6 *= attn6
value += attn6 * attn6 * extrapolate(xsb + 1, ysb + 0, zsb + 1, wsb + 0, dx6, dy6, dz6, dw6)
# Contribution (1,0,0,1)
dx7 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy7 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D
dz7 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D
dw7 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
attn7 = 2 - dx7 * dx7 - dy7 * dy7 - dz7 * dz7 - dw7 * dw7
if attn7 > 0:
attn7 *= attn7
value += attn7 * attn7 * extrapolate(xsb + 1, ysb + 0, zsb + 0, wsb + 1, dx7, dy7, dz7, dw7)
# Contribution (0,1,1,0)
dx8 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D
dy8 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz8 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw8 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D
attn8 = 2 - dx8 * dx8 - dy8 * dy8 - dz8 * dz8 - dw8 * dw8
if attn8 > 0:
attn8 *= attn8
value += attn8 * attn8 * extrapolate(xsb + 0, ysb + 1, zsb + 1, wsb + 0, dx8, dy8, dz8, dw8)
# Contribution (0,1,0,1)
dx9 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D
dy9 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz9 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D
dw9 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
attn9 = 2 - dx9 * dx9 - dy9 * dy9 - dz9 * dz9 - dw9 * dw9
if attn9 > 0:
attn9 *= attn9
value += attn9 * attn9 * extrapolate(xsb + 0, ysb + 1, zsb + 0, wsb + 1, dx9, dy9, dz9, dw9)
# Contribution (0,0,1,1)
dx10 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D
dy10 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D
dz10 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw10 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
attn10 = 2 - dx10 * dx10 - dy10 * dy10 - dz10 * dz10 - dw10 * dw10
if attn10 > 0:
attn10 *= attn10
value += attn10 * attn10 * extrapolate(xsb + 0, ysb + 0, zsb + 1, wsb + 1, dx10, dy10, dz10, dw10)
else: # We're inside the second dispentachoron (Rectified 4-Simplex)
a_is_bigger_side = True
b_is_bigger_side = True
# Decide between (0,0,1,1) and (1,1,0,0)
if xins + yins < zins + wins:
a_score = xins + yins
a_po = 0x0C
else:
a_score = zins + wins
a_po = 0x03
# Decide between (0,1,0,1) and (1,0,1,0)
if xins + zins < yins + wins:
b_score = xins + zins
b_po = 0x0A
else:
b_score = yins + wins
b_po = 0x05
# Closer between (0,1,1,0) and (1,0,0,1) will replace the further of a and b, if closer.
if xins + wins < yins + zins:
score = xins + wins
if a_score <= b_score and score < b_score:
b_score = score
b_po = 0x06
elif a_score > b_score and score < a_score:
a_score = score
a_po = 0x06
else:
score = yins + zins
if a_score <= b_score and score < b_score:
b_score = score
b_po = 0x09
elif a_score > b_score and score < a_score:
a_score = score
a_po = 0x09
# Decide if (0,1,1,1) is closer.
p1 = 3 - in_sum + xins
if a_score <= b_score and p1 < b_score:
b_score = p1
b_po = 0x0E
b_is_bigger_side = False
elif a_score > b_score and p1 < a_score:
a_score = p1
a_po = 0x0E
a_is_bigger_side = False
# Decide if (1,0,1,1) is closer.
p2 = 3 - in_sum + yins
if a_score <= b_score and p2 < b_score:
b_score = p2
b_po = 0x0D
b_is_bigger_side = False
elif a_score > b_score and p2 < a_score:
a_score = p2
a_po = 0x0D
a_is_bigger_side = False
# Decide if (1,1,0,1) is closer.
p3 = 3 - in_sum + zins
if a_score <= b_score and p3 < b_score:
b_score = p3
b_po = 0x0B
b_is_bigger_side = False
elif a_score > b_score and p3 < a_score:
a_score = p3
a_po = 0x0B
a_is_bigger_side = False
# Decide if (1,1,1,0) is closer.
p4 = 3 - in_sum + wins
if a_score <= b_score and p4 < b_score:
b_po = 0x07
b_is_bigger_side = False
elif a_score > b_score and p4 < a_score:
a_po = 0x07
a_is_bigger_side = False
# Where each of the two closest pos are determines how the extra three vertices are calculated.
if a_is_bigger_side == b_is_bigger_side:
if a_is_bigger_side: # Both closest pos on the bigger side
c1 = (a_po & b_po)
c2 = (a_po | b_po)
# Two contributions are _permutations of (0,0,0,1) and (0,0,0,2) based on c1
xsv_ext0 = xsv_ext1 = xsb
ysv_ext0 = ysv_ext1 = ysb
zsv_ext0 = zsv_ext1 = zsb
wsv_ext0 = wsv_ext1 = wsb
dx_ext0 = dx0 - SQUISH_CONSTANT_4D
dy_ext0 = dy0 - SQUISH_CONSTANT_4D
dz_ext0 = dz0 - SQUISH_CONSTANT_4D
dw_ext0 = dw0 - SQUISH_CONSTANT_4D
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_4D
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_4D
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 - 2 * SQUISH_CONSTANT_4D
if (c1 & 0x01) != 0:
xsv_ext0 += 1
dx_ext0 -= 1
xsv_ext1 += 2
dx_ext1 -= 2
elif (c1 & 0x02) != 0:
ysv_ext0 += 1
dy_ext0 -= 1
ysv_ext1 += 2
dy_ext1 -= 2
elif (c1 & 0x04) != 0:
zsv_ext0 += 1
dz_ext0 -= 1
zsv_ext1 += 2
dz_ext1 -= 2
else:
wsv_ext0 += 1
dw_ext0 -= 1
wsv_ext1 += 2
dw_ext1 -= 2
# One contribution is a _permutation of (1,1,1,-1) based on c2
xsv_ext2 = xsb + 1
ysv_ext2 = ysb + 1
zsv_ext2 = zsb + 1
wsv_ext2 = wsb + 1
dx_ext2 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy_ext2 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz_ext2 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
if (c2 & 0x01) == 0:
xsv_ext2 -= 2
dx_ext2 += 2
elif (c2 & 0x02) == 0:
ysv_ext2 -= 2
dy_ext2 += 2
elif (c2 & 0x04) == 0:
zsv_ext2 -= 2
dz_ext2 += 2
else:
wsv_ext2 -= 2
dw_ext2 += 2
else: # Both closest pos on the smaller side
# One of the two extra pos is (1,1,1,1)
xsv_ext2 = xsb + 1
ysv_ext2 = ysb + 1
zsv_ext2 = zsb + 1
wsv_ext2 = wsb + 1
dx_ext2 = dx0 - 1 - 4 * SQUISH_CONSTANT_4D
dy_ext2 = dy0 - 1 - 4 * SQUISH_CONSTANT_4D
dz_ext2 = dz0 - 1 - 4 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 1 - 4 * SQUISH_CONSTANT_4D
# Other two pos are based on the shared axes.
c = (a_po & b_po)
if (c & 0x01) != 0:
xsv_ext0 = xsb + 2
xsv_ext1 = xsb + 1
dx_ext0 = dx0 - 2 - 3 * SQUISH_CONSTANT_4D
dx_ext1 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsb
dx_ext0 = dx_ext1 = dx0 - 3 * SQUISH_CONSTANT_4D
if (c & 0x02) != 0:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy_ext1 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D
if (c & 0x01) == 0:
ysv_ext0 += 1
dy_ext0 -= 1
else:
ysv_ext1 += 1
dy_ext1 -= 1
else:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy_ext1 = dy0 - 3 * SQUISH_CONSTANT_4D
if (c & 0x04) != 0:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz_ext1 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D
if (c & 0x03) == 0:
zsv_ext0 += 1
dz_ext0 -= 1
else:
zsv_ext1 += 1
dz_ext1 -= 1
else:
zsv_ext0 = zsv_ext1 = zsb
dz_ext0 = dz_ext1 = dz0 - 3 * SQUISH_CONSTANT_4D
if (c & 0x08) != 0:
wsv_ext0 = wsb + 1
wsv_ext1 = wsb + 2
dw_ext0 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 - 2 - 3 * SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsb
dw_ext0 = dw_ext1 = dw0 - 3 * SQUISH_CONSTANT_4D
else: # One po on each "side"
if a_is_bigger_side:
c1 = a_po
c2 = b_po
else:
c1 = b_po
c2 = a_po
# Two contributions are the bigger-sided po with each 1 replaced with 2.
if (c1 & 0x01) != 0:
xsv_ext0 = xsb + 2
xsv_ext1 = xsb + 1
dx_ext0 = dx0 - 2 - 3 * SQUISH_CONSTANT_4D
dx_ext1 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsb
dx_ext0 = dx_ext1 = dx0 - 3 * SQUISH_CONSTANT_4D
if (c1 & 0x02) != 0:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy_ext1 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D
if (c1 & 0x01) == 0:
ysv_ext0 += 1
dy_ext0 -= 1
else:
ysv_ext1 += 1
dy_ext1 -= 1
else:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy_ext1 = dy0 - 3 * SQUISH_CONSTANT_4D
if (c1 & 0x04) != 0:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz_ext1 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D
if (c1 & 0x03) == 0:
zsv_ext0 += 1
dz_ext0 -= 1
else:
zsv_ext1 += 1
dz_ext1 -= 1
else:
zsv_ext0 = zsv_ext1 = zsb
dz_ext0 = dz_ext1 = dz0 - 3 * SQUISH_CONSTANT_4D
if (c1 & 0x08) != 0:
wsv_ext0 = wsb + 1
wsv_ext1 = wsb + 2
dw_ext0 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 - 2 - 3 * SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsb
dw_ext0 = dw_ext1 = dw0 - 3 * SQUISH_CONSTANT_4D
# One contribution is a _permutation of (1,1,1,-1) based on the smaller-sided po
xsv_ext2 = xsb + 1
ysv_ext2 = ysb + 1
zsv_ext2 = zsb + 1
wsv_ext2 = wsb + 1
dx_ext2 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy_ext2 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz_ext2 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
if (c2 & 0x01) == 0:
xsv_ext2 -= 2
dx_ext2 += 2
elif (c2 & 0x02) == 0:
ysv_ext2 -= 2
dy_ext2 += 2
elif (c2 & 0x04) == 0:
zsv_ext2 -= 2
dz_ext2 += 2
else:
wsv_ext2 -= 2
dw_ext2 += 2
# Contribution (1,1,1,0)
dx4 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D
dy4 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D
dz4 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D
dw4 = dw0 - 3 * SQUISH_CONSTANT_4D
attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4 - dw4 * dw4
if attn4 > 0:
attn4 *= attn4
value += attn4 * attn4 * extrapolate(xsb + 1, ysb + 1, zsb + 1, wsb + 0, dx4, dy4, dz4, dw4)
# Contribution (1,1,0,1)
dx3 = dx4
dy3 = dy4
dz3 = dz0 - 3 * SQUISH_CONSTANT_4D
dw3 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3 - dw3 * dw3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 1, ysb + 1, zsb + 0, wsb + 1, dx3, dy3, dz3, dw3)
# Contribution (1,0,1,1)
dx2 = dx4
dy2 = dy0 - 3 * SQUISH_CONSTANT_4D
dz2 = dz4
dw2 = dw3
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2 - dw2 * dw2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 1, ysb + 0, zsb + 1, wsb + 1, dx2, dy2, dz2, dw2)
# Contribution (0,1,1,1)
dx1 = dx0 - 3 * SQUISH_CONSTANT_4D
dz1 = dz4
dy1 = dy4
dw1 = dw3
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1 - dw1 * dw1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 0, ysb + 1, zsb + 1, wsb + 1, dx1, dy1, dz1, dw1)
# Contribution (1,1,0,0)
dx5 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy5 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz5 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D
dw5 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D
attn5 = 2 - dx5 * dx5 - dy5 * dy5 - dz5 * dz5 - dw5 * dw5
if attn5 > 0:
attn5 *= attn5
value += attn5 * attn5 * extrapolate(xsb + 1, ysb + 1, zsb + 0, wsb + 0, dx5, dy5, dz5, dw5)
# Contribution (1,0,1,0)
dx6 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy6 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D
dz6 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw6 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D
attn6 = 2 - dx6 * dx6 - dy6 * dy6 - dz6 * dz6 - dw6 * dw6
if attn6 > 0:
attn6 *= attn6
value += attn6 * attn6 * extrapolate(xsb + 1, ysb + 0, zsb + 1, wsb + 0, dx6, dy6, dz6, dw6)
# Contribution (1,0,0,1)
dx7 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy7 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D
dz7 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D
dw7 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
attn7 = 2 - dx7 * dx7 - dy7 * dy7 - dz7 * dz7 - dw7 * dw7
if attn7 > 0:
attn7 *= attn7
value += attn7 * attn7 * extrapolate(xsb + 1, ysb + 0, zsb + 0, wsb + 1, dx7, dy7, dz7, dw7)
# Contribution (0,1,1,0)
dx8 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D
dy8 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz8 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw8 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D
attn8 = 2 - dx8 * dx8 - dy8 * dy8 - dz8 * dz8 - dw8 * dw8
if attn8 > 0:
attn8 *= attn8
value += attn8 * attn8 * extrapolate(xsb + 0, ysb + 1, zsb + 1, wsb + 0, dx8, dy8, dz8, dw8)
# Contribution (0,1,0,1)
dx9 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D
dy9 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz9 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D
dw9 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
attn9 = 2 - dx9 * dx9 - dy9 * dy9 - dz9 * dz9 - dw9 * dw9
if attn9 > 0:
attn9 *= attn9
value += attn9 * attn9 * extrapolate(xsb + 0, ysb + 1, zsb + 0, wsb + 1, dx9, dy9, dz9, dw9)
# Contribution (0,0,1,1)
dx10 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D
dy10 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D
dz10 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw10 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
attn10 = 2 - dx10 * dx10 - dy10 * dy10 - dz10 * dz10 - dw10 * dw10
if attn10 > 0:
attn10 *= attn10
value += attn10 * attn10 * extrapolate(xsb + 0, ysb + 0, zsb + 1, wsb + 1, dx10, dy10, dz10, dw10)
# First extra vertex
attn_ext0 = 2 - dx_ext0 * dx_ext0 - dy_ext0 * dy_ext0 - dz_ext0 * dz_ext0 - dw_ext0 * dw_ext0
if attn_ext0 > 0:
attn_ext0 *= attn_ext0
value += attn_ext0 * attn_ext0 * extrapolate(xsv_ext0, ysv_ext0, zsv_ext0, wsv_ext0, dx_ext0, dy_ext0, dz_ext0, dw_ext0)
# Second extra vertex
attn_ext1 = 2 - dx_ext1 * dx_ext1 - dy_ext1 * dy_ext1 - dz_ext1 * dz_ext1 - dw_ext1 * dw_ext1
if attn_ext1 > 0:
attn_ext1 *= attn_ext1
value += attn_ext1 * attn_ext1 * extrapolate(xsv_ext1, ysv_ext1, zsv_ext1, wsv_ext1, dx_ext1, dy_ext1, dz_ext1, dw_ext1)
# Third extra vertex
attn_ext2 = 2 - dx_ext2 * dx_ext2 - dy_ext2 * dy_ext2 - dz_ext2 * dz_ext2 - dw_ext2 * dw_ext2
if attn_ext2 > 0:
attn_ext2 *= attn_ext2
value += attn_ext2 * attn_ext2 * extrapolate(xsv_ext2, ysv_ext2, zsv_ext2, wsv_ext2, dx_ext2, dy_ext2, dz_ext2, dw_ext2)
return value / NORM_CONSTANT_4D | python | def noise4d(self, x, y, z, w):
"""
Generate 4D OpenSimplex noise from X,Y,Z,W coordinates.
"""
# Place input coordinates on simplectic honeycomb.
stretch_offset = (x + y + z + w) * STRETCH_CONSTANT_4D
xs = x + stretch_offset
ys = y + stretch_offset
zs = z + stretch_offset
ws = w + stretch_offset
# Floor to get simplectic honeycomb coordinates of rhombo-hypercube super-cell origin.
xsb = floor(xs)
ysb = floor(ys)
zsb = floor(zs)
wsb = floor(ws)
# Skew out to get actual coordinates of stretched rhombo-hypercube origin. We'll need these later.
squish_offset = (xsb + ysb + zsb + wsb) * SQUISH_CONSTANT_4D
xb = xsb + squish_offset
yb = ysb + squish_offset
zb = zsb + squish_offset
wb = wsb + squish_offset
# Compute simplectic honeycomb coordinates relative to rhombo-hypercube origin.
xins = xs - xsb
yins = ys - ysb
zins = zs - zsb
wins = ws - wsb
# Sum those together to get a value that determines which region we're in.
in_sum = xins + yins + zins + wins
# Positions relative to origin po.
dx0 = x - xb
dy0 = y - yb
dz0 = z - zb
dw0 = w - wb
value = 0
extrapolate = self._extrapolate4d
if in_sum <= 1: # We're inside the pentachoron (4-Simplex) at (0,0,0,0)
# Determine which two of (0,0,0,1), (0,0,1,0), (0,1,0,0), (1,0,0,0) are closest.
a_po = 0x01
a_score = xins
b_po = 0x02
b_score = yins
if a_score >= b_score and zins > b_score:
b_score = zins
b_po = 0x04
elif a_score < b_score and zins > a_score:
a_score = zins
a_po = 0x04
if a_score >= b_score and wins > b_score:
b_score = wins
b_po = 0x08
elif a_score < b_score and wins > a_score:
a_score = wins
a_po = 0x08
# Now we determine the three lattice pos not part of the pentachoron that may contribute.
# This depends on the closest two pentachoron vertices, including (0,0,0,0)
uins = 1 - in_sum
if uins > a_score or uins > b_score: # (0,0,0,0) is one of the closest two pentachoron vertices.
c = b_po if (b_score > a_score) else a_po # Our other closest vertex is the closest out of a and b.
if (c & 0x01) == 0:
xsv_ext0 = xsb - 1
xsv_ext1 = xsv_ext2 = xsb
dx_ext0 = dx0 + 1
dx_ext1 = dx_ext2 = dx0
else:
xsv_ext0 = xsv_ext1 = xsv_ext2 = xsb + 1
dx_ext0 = dx_ext1 = dx_ext2 = dx0 - 1
if (c & 0x02) == 0:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb
dy_ext0 = dy_ext1 = dy_ext2 = dy0
if (c & 0x01) == 0x01:
ysv_ext0 -= 1
dy_ext0 += 1
else:
ysv_ext1 -= 1
dy_ext1 += 1
else:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb + 1
dy_ext0 = dy_ext1 = dy_ext2 = dy0 - 1
if (c & 0x04) == 0:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb
dz_ext0 = dz_ext1 = dz_ext2 = dz0
if (c & 0x03) != 0:
if (c & 0x03) == 0x03:
zsv_ext0 -= 1
dz_ext0 += 1
else:
zsv_ext1 -= 1
dz_ext1 += 1
else:
zsv_ext2 -= 1
dz_ext2 += 1
else:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb + 1
dz_ext0 = dz_ext1 = dz_ext2 = dz0 - 1
if (c & 0x08) == 0:
wsv_ext0 = wsv_ext1 = wsb
wsv_ext2 = wsb - 1
dw_ext0 = dw_ext1 = dw0
dw_ext2 = dw0 + 1
else:
wsv_ext0 = wsv_ext1 = wsv_ext2 = wsb + 1
dw_ext0 = dw_ext1 = dw_ext2 = dw0 - 1
else: # (0,0,0,0) is not one of the closest two pentachoron vertices.
c = (a_po | b_po) # Our three extra vertices are determined by the closest two.
if (c & 0x01) == 0:
xsv_ext0 = xsv_ext2 = xsb
xsv_ext1 = xsb - 1
dx_ext0 = dx0 - 2 * SQUISH_CONSTANT_4D
dx_ext1 = dx0 + 1 - SQUISH_CONSTANT_4D
dx_ext2 = dx0 - SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsv_ext2 = xsb + 1
dx_ext0 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dx_ext1 = dx_ext2 = dx0 - 1 - SQUISH_CONSTANT_4D
if (c & 0x02) == 0:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb
dy_ext0 = dy0 - 2 * SQUISH_CONSTANT_4D
dy_ext1 = dy_ext2 = dy0 - SQUISH_CONSTANT_4D
if (c & 0x01) == 0x01:
ysv_ext1 -= 1
dy_ext1 += 1
else:
ysv_ext2 -= 1
dy_ext2 += 1
else:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb + 1
dy_ext0 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dy_ext1 = dy_ext2 = dy0 - 1 - SQUISH_CONSTANT_4D
if (c & 0x04) == 0:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb
dz_ext0 = dz0 - 2 * SQUISH_CONSTANT_4D
dz_ext1 = dz_ext2 = dz0 - SQUISH_CONSTANT_4D
if (c & 0x03) == 0x03:
zsv_ext1 -= 1
dz_ext1 += 1
else:
zsv_ext2 -= 1
dz_ext2 += 1
else:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb + 1
dz_ext0 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dz_ext1 = dz_ext2 = dz0 - 1 - SQUISH_CONSTANT_4D
if (c & 0x08) == 0:
wsv_ext0 = wsv_ext1 = wsb
wsv_ext2 = wsb - 1
dw_ext0 = dw0 - 2 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 - SQUISH_CONSTANT_4D
dw_ext2 = dw0 + 1 - SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsv_ext2 = wsb + 1
dw_ext0 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
dw_ext1 = dw_ext2 = dw0 - 1 - SQUISH_CONSTANT_4D
# Contribution (0,0,0,0)
attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0 - dw0 * dw0
if attn0 > 0:
attn0 *= attn0
value += attn0 * attn0 * extrapolate(xsb + 0, ysb + 0, zsb + 0, wsb + 0, dx0, dy0, dz0, dw0)
# Contribution (1,0,0,0)
dx1 = dx0 - 1 - SQUISH_CONSTANT_4D
dy1 = dy0 - 0 - SQUISH_CONSTANT_4D
dz1 = dz0 - 0 - SQUISH_CONSTANT_4D
dw1 = dw0 - 0 - SQUISH_CONSTANT_4D
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1 - dw1 * dw1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, zsb + 0, wsb + 0, dx1, dy1, dz1, dw1)
# Contribution (0,1,0,0)
dx2 = dx0 - 0 - SQUISH_CONSTANT_4D
dy2 = dy0 - 1 - SQUISH_CONSTANT_4D
dz2 = dz1
dw2 = dw1
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2 - dw2 * dw2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, wsb + 0, dx2, dy2, dz2, dw2)
# Contribution (0,0,1,0)
dx3 = dx2
dy3 = dy1
dz3 = dz0 - 1 - SQUISH_CONSTANT_4D
dw3 = dw1
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3 - dw3 * dw3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, wsb + 0, dx3, dy3, dz3, dw3)
# Contribution (0,0,0,1)
dx4 = dx2
dy4 = dy1
dz4 = dz1
dw4 = dw0 - 1 - SQUISH_CONSTANT_4D
attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4 - dw4 * dw4
if attn4 > 0:
attn4 *= attn4
value += attn4 * attn4 * extrapolate(xsb + 0, ysb + 0, zsb + 0, wsb + 1, dx4, dy4, dz4, dw4)
elif in_sum >= 3: # We're inside the pentachoron (4-Simplex) at (1,1,1,1)
# Determine which two of (1,1,1,0), (1,1,0,1), (1,0,1,1), (0,1,1,1) are closest.
a_po = 0x0E
a_score = xins
b_po = 0x0D
b_score = yins
if a_score <= b_score and zins < b_score:
b_score = zins
b_po = 0x0B
elif a_score > b_score and zins < a_score:
a_score = zins
a_po = 0x0B
if a_score <= b_score and wins < b_score:
b_score = wins
b_po = 0x07
elif a_score > b_score and wins < a_score:
a_score = wins
a_po = 0x07
# Now we determine the three lattice pos not part of the pentachoron that may contribute.
# This depends on the closest two pentachoron vertices, including (0,0,0,0)
uins = 4 - in_sum
if uins < a_score or uins < b_score: # (1,1,1,1) is one of the closest two pentachoron vertices.
c = b_po if (b_score < a_score) else a_po # Our other closest vertex is the closest out of a and b.
if (c & 0x01) != 0:
xsv_ext0 = xsb + 2
xsv_ext1 = xsv_ext2 = xsb + 1
dx_ext0 = dx0 - 2 - 4 * SQUISH_CONSTANT_4D
dx_ext1 = dx_ext2 = dx0 - 1 - 4 * SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsv_ext2 = xsb
dx_ext0 = dx_ext1 = dx_ext2 = dx0 - 4 * SQUISH_CONSTANT_4D
if (c & 0x02) != 0:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb + 1
dy_ext0 = dy_ext1 = dy_ext2 = dy0 - 1 - 4 * SQUISH_CONSTANT_4D
if (c & 0x01) != 0:
ysv_ext1 += 1
dy_ext1 -= 1
else:
ysv_ext0 += 1
dy_ext0 -= 1
else:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb
dy_ext0 = dy_ext1 = dy_ext2 = dy0 - 4 * SQUISH_CONSTANT_4D
if (c & 0x04) != 0:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb + 1
dz_ext0 = dz_ext1 = dz_ext2 = dz0 - 1 - 4 * SQUISH_CONSTANT_4D
if (c & 0x03) != 0x03:
if (c & 0x03) == 0:
zsv_ext0 += 1
dz_ext0 -= 1
else:
zsv_ext1 += 1
dz_ext1 -= 1
else:
zsv_ext2 += 1
dz_ext2 -= 1
else:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb
dz_ext0 = dz_ext1 = dz_ext2 = dz0 - 4 * SQUISH_CONSTANT_4D
if (c & 0x08) != 0:
wsv_ext0 = wsv_ext1 = wsb + 1
wsv_ext2 = wsb + 2
dw_ext0 = dw_ext1 = dw0 - 1 - 4 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 2 - 4 * SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsv_ext2 = wsb
dw_ext0 = dw_ext1 = dw_ext2 = dw0 - 4 * SQUISH_CONSTANT_4D
else: # (1,1,1,1) is not one of the closest two pentachoron vertices.
c = (a_po & b_po) # Our three extra vertices are determined by the closest two.
if (c & 0x01) != 0:
xsv_ext0 = xsv_ext2 = xsb + 1
xsv_ext1 = xsb + 2
dx_ext0 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dx_ext1 = dx0 - 2 - 3 * SQUISH_CONSTANT_4D
dx_ext2 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsv_ext2 = xsb
dx_ext0 = dx0 - 2 * SQUISH_CONSTANT_4D
dx_ext1 = dx_ext2 = dx0 - 3 * SQUISH_CONSTANT_4D
if (c & 0x02) != 0:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb + 1
dy_ext0 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dy_ext1 = dy_ext2 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D
if (c & 0x01) != 0:
ysv_ext2 += 1
dy_ext2 -= 1
else:
ysv_ext1 += 1
dy_ext1 -= 1
else:
ysv_ext0 = ysv_ext1 = ysv_ext2 = ysb
dy_ext0 = dy0 - 2 * SQUISH_CONSTANT_4D
dy_ext1 = dy_ext2 = dy0 - 3 * SQUISH_CONSTANT_4D
if (c & 0x04) != 0:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb + 1
dz_ext0 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dz_ext1 = dz_ext2 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D
if (c & 0x03) != 0:
zsv_ext2 += 1
dz_ext2 -= 1
else:
zsv_ext1 += 1
dz_ext1 -= 1
else:
zsv_ext0 = zsv_ext1 = zsv_ext2 = zsb
dz_ext0 = dz0 - 2 * SQUISH_CONSTANT_4D
dz_ext1 = dz_ext2 = dz0 - 3 * SQUISH_CONSTANT_4D
if (c & 0x08) != 0:
wsv_ext0 = wsv_ext1 = wsb + 1
wsv_ext2 = wsb + 2
dw_ext0 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 2 - 3 * SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsv_ext2 = wsb
dw_ext0 = dw0 - 2 * SQUISH_CONSTANT_4D
dw_ext1 = dw_ext2 = dw0 - 3 * SQUISH_CONSTANT_4D
# Contribution (1,1,1,0)
dx4 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D
dy4 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D
dz4 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D
dw4 = dw0 - 3 * SQUISH_CONSTANT_4D
attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4 - dw4 * dw4
if attn4 > 0:
attn4 *= attn4
value += attn4 * attn4 * extrapolate(xsb + 1, ysb + 1, zsb + 1, wsb + 0, dx4, dy4, dz4, dw4)
# Contribution (1,1,0,1)
dx3 = dx4
dy3 = dy4
dz3 = dz0 - 3 * SQUISH_CONSTANT_4D
dw3 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3 - dw3 * dw3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 1, ysb + 1, zsb + 0, wsb + 1, dx3, dy3, dz3, dw3)
# Contribution (1,0,1,1)
dx2 = dx4
dy2 = dy0 - 3 * SQUISH_CONSTANT_4D
dz2 = dz4
dw2 = dw3
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2 - dw2 * dw2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 1, ysb + 0, zsb + 1, wsb + 1, dx2, dy2, dz2, dw2)
# Contribution (0,1,1,1)
dx1 = dx0 - 3 * SQUISH_CONSTANT_4D
dz1 = dz4
dy1 = dy4
dw1 = dw3
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1 - dw1 * dw1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 0, ysb + 1, zsb + 1, wsb + 1, dx1, dy1, dz1, dw1)
# Contribution (1,1,1,1)
dx0 = dx0 - 1 - 4 * SQUISH_CONSTANT_4D
dy0 = dy0 - 1 - 4 * SQUISH_CONSTANT_4D
dz0 = dz0 - 1 - 4 * SQUISH_CONSTANT_4D
dw0 = dw0 - 1 - 4 * SQUISH_CONSTANT_4D
attn0 = 2 - dx0 * dx0 - dy0 * dy0 - dz0 * dz0 - dw0 * dw0
if attn0 > 0:
attn0 *= attn0
value += attn0 * attn0 * extrapolate(xsb + 1, ysb + 1, zsb + 1, wsb + 1, dx0, dy0, dz0, dw0)
elif in_sum <= 2: # We're inside the first dispentachoron (Rectified 4-Simplex)
a_is_bigger_side = True
b_is_bigger_side = True
# Decide between (1,1,0,0) and (0,0,1,1)
if xins + yins > zins + wins:
a_score = xins + yins
a_po = 0x03
else:
a_score = zins + wins
a_po = 0x0C
# Decide between (1,0,1,0) and (0,1,0,1)
if xins + zins > yins + wins:
b_score = xins + zins
b_po = 0x05
else:
b_score = yins + wins
b_po = 0x0A
# Closer between (1,0,0,1) and (0,1,1,0) will replace the further of a and b, if closer.
if xins + wins > yins + zins:
score = xins + wins
if a_score >= b_score and score > b_score:
b_score = score
b_po = 0x09
elif a_score < b_score and score > a_score:
a_score = score
a_po = 0x09
else:
score = yins + zins
if a_score >= b_score and score > b_score:
b_score = score
b_po = 0x06
elif a_score < b_score and score > a_score:
a_score = score
a_po = 0x06
# Decide if (1,0,0,0) is closer.
p1 = 2 - in_sum + xins
if a_score >= b_score and p1 > b_score:
b_score = p1
b_po = 0x01
b_is_bigger_side = False
elif a_score < b_score and p1 > a_score:
a_score = p1
a_po = 0x01
a_is_bigger_side = False
# Decide if (0,1,0,0) is closer.
p2 = 2 - in_sum + yins
if a_score >= b_score and p2 > b_score:
b_score = p2
b_po = 0x02
b_is_bigger_side = False
elif a_score < b_score and p2 > a_score:
a_score = p2
a_po = 0x02
a_is_bigger_side = False
# Decide if (0,0,1,0) is closer.
p3 = 2 - in_sum + zins
if a_score >= b_score and p3 > b_score:
b_score = p3
b_po = 0x04
b_is_bigger_side = False
elif a_score < b_score and p3 > a_score:
a_score = p3
a_po = 0x04
a_is_bigger_side = False
# Decide if (0,0,0,1) is closer.
p4 = 2 - in_sum + wins
if a_score >= b_score and p4 > b_score:
b_po = 0x08
b_is_bigger_side = False
elif a_score < b_score and p4 > a_score:
a_po = 0x08
a_is_bigger_side = False
# Where each of the two closest pos are determines how the extra three vertices are calculated.
if a_is_bigger_side == b_is_bigger_side:
if a_is_bigger_side: # Both closest pos on the bigger side
c1 = (a_po | b_po)
c2 = (a_po & b_po)
if (c1 & 0x01) == 0:
xsv_ext0 = xsb
xsv_ext1 = xsb - 1
dx_ext0 = dx0 - 3 * SQUISH_CONSTANT_4D
dx_ext1 = dx0 + 1 - 2 * SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsb + 1
dx_ext0 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D
dx_ext1 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
if (c1 & 0x02) == 0:
ysv_ext0 = ysb
ysv_ext1 = ysb - 1
dy_ext0 = dy0 - 3 * SQUISH_CONSTANT_4D
dy_ext1 = dy0 + 1 - 2 * SQUISH_CONSTANT_4D
else:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D
dy_ext1 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
if (c1 & 0x04) == 0:
zsv_ext0 = zsb
zsv_ext1 = zsb - 1
dz_ext0 = dz0 - 3 * SQUISH_CONSTANT_4D
dz_ext1 = dz0 + 1 - 2 * SQUISH_CONSTANT_4D
else:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D
dz_ext1 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
if (c1 & 0x08) == 0:
wsv_ext0 = wsb
wsv_ext1 = wsb - 1
dw_ext0 = dw0 - 3 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 + 1 - 2 * SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsb + 1
dw_ext0 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
# One combination is a _permutation of (0,0,0,2) based on c2
xsv_ext2 = xsb
ysv_ext2 = ysb
zsv_ext2 = zsb
wsv_ext2 = wsb
dx_ext2 = dx0 - 2 * SQUISH_CONSTANT_4D
dy_ext2 = dy0 - 2 * SQUISH_CONSTANT_4D
dz_ext2 = dz0 - 2 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 2 * SQUISH_CONSTANT_4D
if (c2 & 0x01) != 0:
xsv_ext2 += 2
dx_ext2 -= 2
elif (c2 & 0x02) != 0:
ysv_ext2 += 2
dy_ext2 -= 2
elif (c2 & 0x04) != 0:
zsv_ext2 += 2
dz_ext2 -= 2
else:
wsv_ext2 += 2
dw_ext2 -= 2
else: # Both closest pos on the smaller side
# One of the two extra pos is (0,0,0,0)
xsv_ext2 = xsb
ysv_ext2 = ysb
zsv_ext2 = zsb
wsv_ext2 = wsb
dx_ext2 = dx0
dy_ext2 = dy0
dz_ext2 = dz0
dw_ext2 = dw0
# Other two pos are based on the omitted axes.
c = (a_po | b_po)
if (c & 0x01) == 0:
xsv_ext0 = xsb - 1
xsv_ext1 = xsb
dx_ext0 = dx0 + 1 - SQUISH_CONSTANT_4D
dx_ext1 = dx0 - SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsb + 1
dx_ext0 = dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_4D
if (c & 0x02) == 0:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy_ext1 = dy0 - SQUISH_CONSTANT_4D
if (c & 0x01) == 0x01:
ysv_ext0 -= 1
dy_ext0 += 1
else:
ysv_ext1 -= 1
dy_ext1 += 1
else:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_4D
if (c & 0x04) == 0:
zsv_ext0 = zsv_ext1 = zsb
dz_ext0 = dz_ext1 = dz0 - SQUISH_CONSTANT_4D
if (c & 0x03) == 0x03:
zsv_ext0 -= 1
dz_ext0 += 1
else:
zsv_ext1 -= 1
dz_ext1 += 1
else:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_4D
if (c & 0x08) == 0:
wsv_ext0 = wsb
wsv_ext1 = wsb - 1
dw_ext0 = dw0 - SQUISH_CONSTANT_4D
dw_ext1 = dw0 + 1 - SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsb + 1
dw_ext0 = dw_ext1 = dw0 - 1 - SQUISH_CONSTANT_4D
else: # One po on each "side"
if a_is_bigger_side:
c1 = a_po
c2 = b_po
else:
c1 = b_po
c2 = a_po
# Two contributions are the bigger-sided po with each 0 replaced with -1.
if (c1 & 0x01) == 0:
xsv_ext0 = xsb - 1
xsv_ext1 = xsb
dx_ext0 = dx0 + 1 - SQUISH_CONSTANT_4D
dx_ext1 = dx0 - SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsb + 1
dx_ext0 = dx_ext1 = dx0 - 1 - SQUISH_CONSTANT_4D
if (c1 & 0x02) == 0:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy_ext1 = dy0 - SQUISH_CONSTANT_4D
if (c1 & 0x01) == 0x01:
ysv_ext0 -= 1
dy_ext0 += 1
else:
ysv_ext1 -= 1
dy_ext1 += 1
else:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy_ext1 = dy0 - 1 - SQUISH_CONSTANT_4D
if (c1 & 0x04) == 0:
zsv_ext0 = zsv_ext1 = zsb
dz_ext0 = dz_ext1 = dz0 - SQUISH_CONSTANT_4D
if (c1 & 0x03) == 0x03:
zsv_ext0 -= 1
dz_ext0 += 1
else:
zsv_ext1 -= 1
dz_ext1 += 1
else:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz_ext1 = dz0 - 1 - SQUISH_CONSTANT_4D
if (c1 & 0x08) == 0:
wsv_ext0 = wsb
wsv_ext1 = wsb - 1
dw_ext0 = dw0 - SQUISH_CONSTANT_4D
dw_ext1 = dw0 + 1 - SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsb + 1
dw_ext0 = dw_ext1 = dw0 - 1 - SQUISH_CONSTANT_4D
# One contribution is a _permutation of (0,0,0,2) based on the smaller-sided po
xsv_ext2 = xsb
ysv_ext2 = ysb
zsv_ext2 = zsb
wsv_ext2 = wsb
dx_ext2 = dx0 - 2 * SQUISH_CONSTANT_4D
dy_ext2 = dy0 - 2 * SQUISH_CONSTANT_4D
dz_ext2 = dz0 - 2 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 2 * SQUISH_CONSTANT_4D
if (c2 & 0x01) != 0:
xsv_ext2 += 2
dx_ext2 -= 2
elif (c2 & 0x02) != 0:
ysv_ext2 += 2
dy_ext2 -= 2
elif (c2 & 0x04) != 0:
zsv_ext2 += 2
dz_ext2 -= 2
else:
wsv_ext2 += 2
dw_ext2 -= 2
# Contribution (1,0,0,0)
dx1 = dx0 - 1 - SQUISH_CONSTANT_4D
dy1 = dy0 - 0 - SQUISH_CONSTANT_4D
dz1 = dz0 - 0 - SQUISH_CONSTANT_4D
dw1 = dw0 - 0 - SQUISH_CONSTANT_4D
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1 - dw1 * dw1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 1, ysb + 0, zsb + 0, wsb + 0, dx1, dy1, dz1, dw1)
# Contribution (0,1,0,0)
dx2 = dx0 - 0 - SQUISH_CONSTANT_4D
dy2 = dy0 - 1 - SQUISH_CONSTANT_4D
dz2 = dz1
dw2 = dw1
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2 - dw2 * dw2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 0, ysb + 1, zsb + 0, wsb + 0, dx2, dy2, dz2, dw2)
# Contribution (0,0,1,0)
dx3 = dx2
dy3 = dy1
dz3 = dz0 - 1 - SQUISH_CONSTANT_4D
dw3 = dw1
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3 - dw3 * dw3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 0, ysb + 0, zsb + 1, wsb + 0, dx3, dy3, dz3, dw3)
# Contribution (0,0,0,1)
dx4 = dx2
dy4 = dy1
dz4 = dz1
dw4 = dw0 - 1 - SQUISH_CONSTANT_4D
attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4 - dw4 * dw4
if attn4 > 0:
attn4 *= attn4
value += attn4 * attn4 * extrapolate(xsb + 0, ysb + 0, zsb + 0, wsb + 1, dx4, dy4, dz4, dw4)
# Contribution (1,1,0,0)
dx5 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy5 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz5 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D
dw5 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D
attn5 = 2 - dx5 * dx5 - dy5 * dy5 - dz5 * dz5 - dw5 * dw5
if attn5 > 0:
attn5 *= attn5
value += attn5 * attn5 * extrapolate(xsb + 1, ysb + 1, zsb + 0, wsb + 0, dx5, dy5, dz5, dw5)
# Contribution (1,0,1,0)
dx6 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy6 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D
dz6 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw6 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D
attn6 = 2 - dx6 * dx6 - dy6 * dy6 - dz6 * dz6 - dw6 * dw6
if attn6 > 0:
attn6 *= attn6
value += attn6 * attn6 * extrapolate(xsb + 1, ysb + 0, zsb + 1, wsb + 0, dx6, dy6, dz6, dw6)
# Contribution (1,0,0,1)
dx7 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy7 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D
dz7 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D
dw7 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
attn7 = 2 - dx7 * dx7 - dy7 * dy7 - dz7 * dz7 - dw7 * dw7
if attn7 > 0:
attn7 *= attn7
value += attn7 * attn7 * extrapolate(xsb + 1, ysb + 0, zsb + 0, wsb + 1, dx7, dy7, dz7, dw7)
# Contribution (0,1,1,0)
dx8 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D
dy8 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz8 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw8 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D
attn8 = 2 - dx8 * dx8 - dy8 * dy8 - dz8 * dz8 - dw8 * dw8
if attn8 > 0:
attn8 *= attn8
value += attn8 * attn8 * extrapolate(xsb + 0, ysb + 1, zsb + 1, wsb + 0, dx8, dy8, dz8, dw8)
# Contribution (0,1,0,1)
dx9 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D
dy9 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz9 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D
dw9 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
attn9 = 2 - dx9 * dx9 - dy9 * dy9 - dz9 * dz9 - dw9 * dw9
if attn9 > 0:
attn9 *= attn9
value += attn9 * attn9 * extrapolate(xsb + 0, ysb + 1, zsb + 0, wsb + 1, dx9, dy9, dz9, dw9)
# Contribution (0,0,1,1)
dx10 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D
dy10 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D
dz10 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw10 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
attn10 = 2 - dx10 * dx10 - dy10 * dy10 - dz10 * dz10 - dw10 * dw10
if attn10 > 0:
attn10 *= attn10
value += attn10 * attn10 * extrapolate(xsb + 0, ysb + 0, zsb + 1, wsb + 1, dx10, dy10, dz10, dw10)
else: # We're inside the second dispentachoron (Rectified 4-Simplex)
a_is_bigger_side = True
b_is_bigger_side = True
# Decide between (0,0,1,1) and (1,1,0,0)
if xins + yins < zins + wins:
a_score = xins + yins
a_po = 0x0C
else:
a_score = zins + wins
a_po = 0x03
# Decide between (0,1,0,1) and (1,0,1,0)
if xins + zins < yins + wins:
b_score = xins + zins
b_po = 0x0A
else:
b_score = yins + wins
b_po = 0x05
# Closer between (0,1,1,0) and (1,0,0,1) will replace the further of a and b, if closer.
if xins + wins < yins + zins:
score = xins + wins
if a_score <= b_score and score < b_score:
b_score = score
b_po = 0x06
elif a_score > b_score and score < a_score:
a_score = score
a_po = 0x06
else:
score = yins + zins
if a_score <= b_score and score < b_score:
b_score = score
b_po = 0x09
elif a_score > b_score and score < a_score:
a_score = score
a_po = 0x09
# Decide if (0,1,1,1) is closer.
p1 = 3 - in_sum + xins
if a_score <= b_score and p1 < b_score:
b_score = p1
b_po = 0x0E
b_is_bigger_side = False
elif a_score > b_score and p1 < a_score:
a_score = p1
a_po = 0x0E
a_is_bigger_side = False
# Decide if (1,0,1,1) is closer.
p2 = 3 - in_sum + yins
if a_score <= b_score and p2 < b_score:
b_score = p2
b_po = 0x0D
b_is_bigger_side = False
elif a_score > b_score and p2 < a_score:
a_score = p2
a_po = 0x0D
a_is_bigger_side = False
# Decide if (1,1,0,1) is closer.
p3 = 3 - in_sum + zins
if a_score <= b_score and p3 < b_score:
b_score = p3
b_po = 0x0B
b_is_bigger_side = False
elif a_score > b_score and p3 < a_score:
a_score = p3
a_po = 0x0B
a_is_bigger_side = False
# Decide if (1,1,1,0) is closer.
p4 = 3 - in_sum + wins
if a_score <= b_score and p4 < b_score:
b_po = 0x07
b_is_bigger_side = False
elif a_score > b_score and p4 < a_score:
a_po = 0x07
a_is_bigger_side = False
# Where each of the two closest pos are determines how the extra three vertices are calculated.
if a_is_bigger_side == b_is_bigger_side:
if a_is_bigger_side: # Both closest pos on the bigger side
c1 = (a_po & b_po)
c2 = (a_po | b_po)
# Two contributions are _permutations of (0,0,0,1) and (0,0,0,2) based on c1
xsv_ext0 = xsv_ext1 = xsb
ysv_ext0 = ysv_ext1 = ysb
zsv_ext0 = zsv_ext1 = zsb
wsv_ext0 = wsv_ext1 = wsb
dx_ext0 = dx0 - SQUISH_CONSTANT_4D
dy_ext0 = dy0 - SQUISH_CONSTANT_4D
dz_ext0 = dz0 - SQUISH_CONSTANT_4D
dw_ext0 = dw0 - SQUISH_CONSTANT_4D
dx_ext1 = dx0 - 2 * SQUISH_CONSTANT_4D
dy_ext1 = dy0 - 2 * SQUISH_CONSTANT_4D
dz_ext1 = dz0 - 2 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 - 2 * SQUISH_CONSTANT_4D
if (c1 & 0x01) != 0:
xsv_ext0 += 1
dx_ext0 -= 1
xsv_ext1 += 2
dx_ext1 -= 2
elif (c1 & 0x02) != 0:
ysv_ext0 += 1
dy_ext0 -= 1
ysv_ext1 += 2
dy_ext1 -= 2
elif (c1 & 0x04) != 0:
zsv_ext0 += 1
dz_ext0 -= 1
zsv_ext1 += 2
dz_ext1 -= 2
else:
wsv_ext0 += 1
dw_ext0 -= 1
wsv_ext1 += 2
dw_ext1 -= 2
# One contribution is a _permutation of (1,1,1,-1) based on c2
xsv_ext2 = xsb + 1
ysv_ext2 = ysb + 1
zsv_ext2 = zsb + 1
wsv_ext2 = wsb + 1
dx_ext2 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy_ext2 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz_ext2 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
if (c2 & 0x01) == 0:
xsv_ext2 -= 2
dx_ext2 += 2
elif (c2 & 0x02) == 0:
ysv_ext2 -= 2
dy_ext2 += 2
elif (c2 & 0x04) == 0:
zsv_ext2 -= 2
dz_ext2 += 2
else:
wsv_ext2 -= 2
dw_ext2 += 2
else: # Both closest pos on the smaller side
# One of the two extra pos is (1,1,1,1)
xsv_ext2 = xsb + 1
ysv_ext2 = ysb + 1
zsv_ext2 = zsb + 1
wsv_ext2 = wsb + 1
dx_ext2 = dx0 - 1 - 4 * SQUISH_CONSTANT_4D
dy_ext2 = dy0 - 1 - 4 * SQUISH_CONSTANT_4D
dz_ext2 = dz0 - 1 - 4 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 1 - 4 * SQUISH_CONSTANT_4D
# Other two pos are based on the shared axes.
c = (a_po & b_po)
if (c & 0x01) != 0:
xsv_ext0 = xsb + 2
xsv_ext1 = xsb + 1
dx_ext0 = dx0 - 2 - 3 * SQUISH_CONSTANT_4D
dx_ext1 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsb
dx_ext0 = dx_ext1 = dx0 - 3 * SQUISH_CONSTANT_4D
if (c & 0x02) != 0:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy_ext1 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D
if (c & 0x01) == 0:
ysv_ext0 += 1
dy_ext0 -= 1
else:
ysv_ext1 += 1
dy_ext1 -= 1
else:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy_ext1 = dy0 - 3 * SQUISH_CONSTANT_4D
if (c & 0x04) != 0:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz_ext1 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D
if (c & 0x03) == 0:
zsv_ext0 += 1
dz_ext0 -= 1
else:
zsv_ext1 += 1
dz_ext1 -= 1
else:
zsv_ext0 = zsv_ext1 = zsb
dz_ext0 = dz_ext1 = dz0 - 3 * SQUISH_CONSTANT_4D
if (c & 0x08) != 0:
wsv_ext0 = wsb + 1
wsv_ext1 = wsb + 2
dw_ext0 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 - 2 - 3 * SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsb
dw_ext0 = dw_ext1 = dw0 - 3 * SQUISH_CONSTANT_4D
else: # One po on each "side"
if a_is_bigger_side:
c1 = a_po
c2 = b_po
else:
c1 = b_po
c2 = a_po
# Two contributions are the bigger-sided po with each 1 replaced with 2.
if (c1 & 0x01) != 0:
xsv_ext0 = xsb + 2
xsv_ext1 = xsb + 1
dx_ext0 = dx0 - 2 - 3 * SQUISH_CONSTANT_4D
dx_ext1 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D
else:
xsv_ext0 = xsv_ext1 = xsb
dx_ext0 = dx_ext1 = dx0 - 3 * SQUISH_CONSTANT_4D
if (c1 & 0x02) != 0:
ysv_ext0 = ysv_ext1 = ysb + 1
dy_ext0 = dy_ext1 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D
if (c1 & 0x01) == 0:
ysv_ext0 += 1
dy_ext0 -= 1
else:
ysv_ext1 += 1
dy_ext1 -= 1
else:
ysv_ext0 = ysv_ext1 = ysb
dy_ext0 = dy_ext1 = dy0 - 3 * SQUISH_CONSTANT_4D
if (c1 & 0x04) != 0:
zsv_ext0 = zsv_ext1 = zsb + 1
dz_ext0 = dz_ext1 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D
if (c1 & 0x03) == 0:
zsv_ext0 += 1
dz_ext0 -= 1
else:
zsv_ext1 += 1
dz_ext1 -= 1
else:
zsv_ext0 = zsv_ext1 = zsb
dz_ext0 = dz_ext1 = dz0 - 3 * SQUISH_CONSTANT_4D
if (c1 & 0x08) != 0:
wsv_ext0 = wsb + 1
wsv_ext1 = wsb + 2
dw_ext0 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D
dw_ext1 = dw0 - 2 - 3 * SQUISH_CONSTANT_4D
else:
wsv_ext0 = wsv_ext1 = wsb
dw_ext0 = dw_ext1 = dw0 - 3 * SQUISH_CONSTANT_4D
# One contribution is a _permutation of (1,1,1,-1) based on the smaller-sided po
xsv_ext2 = xsb + 1
ysv_ext2 = ysb + 1
zsv_ext2 = zsb + 1
wsv_ext2 = wsb + 1
dx_ext2 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy_ext2 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz_ext2 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw_ext2 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
if (c2 & 0x01) == 0:
xsv_ext2 -= 2
dx_ext2 += 2
elif (c2 & 0x02) == 0:
ysv_ext2 -= 2
dy_ext2 += 2
elif (c2 & 0x04) == 0:
zsv_ext2 -= 2
dz_ext2 += 2
else:
wsv_ext2 -= 2
dw_ext2 += 2
# Contribution (1,1,1,0)
dx4 = dx0 - 1 - 3 * SQUISH_CONSTANT_4D
dy4 = dy0 - 1 - 3 * SQUISH_CONSTANT_4D
dz4 = dz0 - 1 - 3 * SQUISH_CONSTANT_4D
dw4 = dw0 - 3 * SQUISH_CONSTANT_4D
attn4 = 2 - dx4 * dx4 - dy4 * dy4 - dz4 * dz4 - dw4 * dw4
if attn4 > 0:
attn4 *= attn4
value += attn4 * attn4 * extrapolate(xsb + 1, ysb + 1, zsb + 1, wsb + 0, dx4, dy4, dz4, dw4)
# Contribution (1,1,0,1)
dx3 = dx4
dy3 = dy4
dz3 = dz0 - 3 * SQUISH_CONSTANT_4D
dw3 = dw0 - 1 - 3 * SQUISH_CONSTANT_4D
attn3 = 2 - dx3 * dx3 - dy3 * dy3 - dz3 * dz3 - dw3 * dw3
if attn3 > 0:
attn3 *= attn3
value += attn3 * attn3 * extrapolate(xsb + 1, ysb + 1, zsb + 0, wsb + 1, dx3, dy3, dz3, dw3)
# Contribution (1,0,1,1)
dx2 = dx4
dy2 = dy0 - 3 * SQUISH_CONSTANT_4D
dz2 = dz4
dw2 = dw3
attn2 = 2 - dx2 * dx2 - dy2 * dy2 - dz2 * dz2 - dw2 * dw2
if attn2 > 0:
attn2 *= attn2
value += attn2 * attn2 * extrapolate(xsb + 1, ysb + 0, zsb + 1, wsb + 1, dx2, dy2, dz2, dw2)
# Contribution (0,1,1,1)
dx1 = dx0 - 3 * SQUISH_CONSTANT_4D
dz1 = dz4
dy1 = dy4
dw1 = dw3
attn1 = 2 - dx1 * dx1 - dy1 * dy1 - dz1 * dz1 - dw1 * dw1
if attn1 > 0:
attn1 *= attn1
value += attn1 * attn1 * extrapolate(xsb + 0, ysb + 1, zsb + 1, wsb + 1, dx1, dy1, dz1, dw1)
# Contribution (1,1,0,0)
dx5 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy5 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz5 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D
dw5 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D
attn5 = 2 - dx5 * dx5 - dy5 * dy5 - dz5 * dz5 - dw5 * dw5
if attn5 > 0:
attn5 *= attn5
value += attn5 * attn5 * extrapolate(xsb + 1, ysb + 1, zsb + 0, wsb + 0, dx5, dy5, dz5, dw5)
# Contribution (1,0,1,0)
dx6 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy6 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D
dz6 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw6 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D
attn6 = 2 - dx6 * dx6 - dy6 * dy6 - dz6 * dz6 - dw6 * dw6
if attn6 > 0:
attn6 *= attn6
value += attn6 * attn6 * extrapolate(xsb + 1, ysb + 0, zsb + 1, wsb + 0, dx6, dy6, dz6, dw6)
# Contribution (1,0,0,1)
dx7 = dx0 - 1 - 2 * SQUISH_CONSTANT_4D
dy7 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D
dz7 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D
dw7 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
attn7 = 2 - dx7 * dx7 - dy7 * dy7 - dz7 * dz7 - dw7 * dw7
if attn7 > 0:
attn7 *= attn7
value += attn7 * attn7 * extrapolate(xsb + 1, ysb + 0, zsb + 0, wsb + 1, dx7, dy7, dz7, dw7)
# Contribution (0,1,1,0)
dx8 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D
dy8 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz8 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw8 = dw0 - 0 - 2 * SQUISH_CONSTANT_4D
attn8 = 2 - dx8 * dx8 - dy8 * dy8 - dz8 * dz8 - dw8 * dw8
if attn8 > 0:
attn8 *= attn8
value += attn8 * attn8 * extrapolate(xsb + 0, ysb + 1, zsb + 1, wsb + 0, dx8, dy8, dz8, dw8)
# Contribution (0,1,0,1)
dx9 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D
dy9 = dy0 - 1 - 2 * SQUISH_CONSTANT_4D
dz9 = dz0 - 0 - 2 * SQUISH_CONSTANT_4D
dw9 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
attn9 = 2 - dx9 * dx9 - dy9 * dy9 - dz9 * dz9 - dw9 * dw9
if attn9 > 0:
attn9 *= attn9
value += attn9 * attn9 * extrapolate(xsb + 0, ysb + 1, zsb + 0, wsb + 1, dx9, dy9, dz9, dw9)
# Contribution (0,0,1,1)
dx10 = dx0 - 0 - 2 * SQUISH_CONSTANT_4D
dy10 = dy0 - 0 - 2 * SQUISH_CONSTANT_4D
dz10 = dz0 - 1 - 2 * SQUISH_CONSTANT_4D
dw10 = dw0 - 1 - 2 * SQUISH_CONSTANT_4D
attn10 = 2 - dx10 * dx10 - dy10 * dy10 - dz10 * dz10 - dw10 * dw10
if attn10 > 0:
attn10 *= attn10
value += attn10 * attn10 * extrapolate(xsb + 0, ysb + 0, zsb + 1, wsb + 1, dx10, dy10, dz10, dw10)
# First extra vertex
attn_ext0 = 2 - dx_ext0 * dx_ext0 - dy_ext0 * dy_ext0 - dz_ext0 * dz_ext0 - dw_ext0 * dw_ext0
if attn_ext0 > 0:
attn_ext0 *= attn_ext0
value += attn_ext0 * attn_ext0 * extrapolate(xsv_ext0, ysv_ext0, zsv_ext0, wsv_ext0, dx_ext0, dy_ext0, dz_ext0, dw_ext0)
# Second extra vertex
attn_ext1 = 2 - dx_ext1 * dx_ext1 - dy_ext1 * dy_ext1 - dz_ext1 * dz_ext1 - dw_ext1 * dw_ext1
if attn_ext1 > 0:
attn_ext1 *= attn_ext1
value += attn_ext1 * attn_ext1 * extrapolate(xsv_ext1, ysv_ext1, zsv_ext1, wsv_ext1, dx_ext1, dy_ext1, dz_ext1, dw_ext1)
# Third extra vertex
attn_ext2 = 2 - dx_ext2 * dx_ext2 - dy_ext2 * dy_ext2 - dz_ext2 * dz_ext2 - dw_ext2 * dw_ext2
if attn_ext2 > 0:
attn_ext2 *= attn_ext2
value += attn_ext2 * attn_ext2 * extrapolate(xsv_ext2, ysv_ext2, zsv_ext2, wsv_ext2, dx_ext2, dy_ext2, dz_ext2, dw_ext2)
return value / NORM_CONSTANT_4D | [
"def",
"noise4d",
"(",
"self",
",",
"x",
",",
"y",
",",
"z",
",",
"w",
")",
":",
"# Place input coordinates on simplectic honeycomb.",
"stretch_offset",
"=",
"(",
"x",
"+",
"y",
"+",
"z",
"+",
"w",
")",
"*",
"STRETCH_CONSTANT_4D",
"xs",
"=",
"x",
"+",
"stretch_offset",
"ys",
"=",
"y",
"+",
"stretch_offset",
"zs",
"=",
"z",
"+",
"stretch_offset",
"ws",
"=",
"w",
"+",
"stretch_offset",
"# Floor to get simplectic honeycomb coordinates of rhombo-hypercube super-cell origin.",
"xsb",
"=",
"floor",
"(",
"xs",
")",
"ysb",
"=",
"floor",
"(",
"ys",
")",
"zsb",
"=",
"floor",
"(",
"zs",
")",
"wsb",
"=",
"floor",
"(",
"ws",
")",
"# Skew out to get actual coordinates of stretched rhombo-hypercube origin. We'll need these later.",
"squish_offset",
"=",
"(",
"xsb",
"+",
"ysb",
"+",
"zsb",
"+",
"wsb",
")",
"*",
"SQUISH_CONSTANT_4D",
"xb",
"=",
"xsb",
"+",
"squish_offset",
"yb",
"=",
"ysb",
"+",
"squish_offset",
"zb",
"=",
"zsb",
"+",
"squish_offset",
"wb",
"=",
"wsb",
"+",
"squish_offset",
"# Compute simplectic honeycomb coordinates relative to rhombo-hypercube origin.",
"xins",
"=",
"xs",
"-",
"xsb",
"yins",
"=",
"ys",
"-",
"ysb",
"zins",
"=",
"zs",
"-",
"zsb",
"wins",
"=",
"ws",
"-",
"wsb",
"# Sum those together to get a value that determines which region we're in.",
"in_sum",
"=",
"xins",
"+",
"yins",
"+",
"zins",
"+",
"wins",
"# Positions relative to origin po.",
"dx0",
"=",
"x",
"-",
"xb",
"dy0",
"=",
"y",
"-",
"yb",
"dz0",
"=",
"z",
"-",
"zb",
"dw0",
"=",
"w",
"-",
"wb",
"value",
"=",
"0",
"extrapolate",
"=",
"self",
".",
"_extrapolate4d",
"if",
"in_sum",
"<=",
"1",
":",
"# We're inside the pentachoron (4-Simplex) at (0,0,0,0)",
"# Determine which two of (0,0,0,1), (0,0,1,0), (0,1,0,0), (1,0,0,0) are closest.",
"a_po",
"=",
"0x01",
"a_score",
"=",
"xins",
"b_po",
"=",
"0x02",
"b_score",
"=",
"yins",
"if",
"a_score",
">=",
"b_score",
"and",
"zins",
">",
"b_score",
":",
"b_score",
"=",
"zins",
"b_po",
"=",
"0x04",
"elif",
"a_score",
"<",
"b_score",
"and",
"zins",
">",
"a_score",
":",
"a_score",
"=",
"zins",
"a_po",
"=",
"0x04",
"if",
"a_score",
">=",
"b_score",
"and",
"wins",
">",
"b_score",
":",
"b_score",
"=",
"wins",
"b_po",
"=",
"0x08",
"elif",
"a_score",
"<",
"b_score",
"and",
"wins",
">",
"a_score",
":",
"a_score",
"=",
"wins",
"a_po",
"=",
"0x08",
"# Now we determine the three lattice pos not part of the pentachoron that may contribute.",
"# This depends on the closest two pentachoron vertices, including (0,0,0,0)",
"uins",
"=",
"1",
"-",
"in_sum",
"if",
"uins",
">",
"a_score",
"or",
"uins",
">",
"b_score",
":",
"# (0,0,0,0) is one of the closest two pentachoron vertices.",
"c",
"=",
"b_po",
"if",
"(",
"b_score",
">",
"a_score",
")",
"else",
"a_po",
"# Our other closest vertex is the closest out of a and b.",
"if",
"(",
"c",
"&",
"0x01",
")",
"==",
"0",
":",
"xsv_ext0",
"=",
"xsb",
"-",
"1",
"xsv_ext1",
"=",
"xsv_ext2",
"=",
"xsb",
"dx_ext0",
"=",
"dx0",
"+",
"1",
"dx_ext1",
"=",
"dx_ext2",
"=",
"dx0",
"else",
":",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsv_ext2",
"=",
"xsb",
"+",
"1",
"dx_ext0",
"=",
"dx_ext1",
"=",
"dx_ext2",
"=",
"dx0",
"-",
"1",
"if",
"(",
"c",
"&",
"0x02",
")",
"==",
"0",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysv_ext2",
"=",
"ysb",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy_ext2",
"=",
"dy0",
"if",
"(",
"c",
"&",
"0x01",
")",
"==",
"0x01",
":",
"ysv_ext0",
"-=",
"1",
"dy_ext0",
"+=",
"1",
"else",
":",
"ysv_ext1",
"-=",
"1",
"dy_ext1",
"+=",
"1",
"else",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysv_ext2",
"=",
"ysb",
"+",
"1",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy_ext2",
"=",
"dy0",
"-",
"1",
"if",
"(",
"c",
"&",
"0x04",
")",
"==",
"0",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsv_ext2",
"=",
"zsb",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz_ext2",
"=",
"dz0",
"if",
"(",
"c",
"&",
"0x03",
")",
"!=",
"0",
":",
"if",
"(",
"c",
"&",
"0x03",
")",
"==",
"0x03",
":",
"zsv_ext0",
"-=",
"1",
"dz_ext0",
"+=",
"1",
"else",
":",
"zsv_ext1",
"-=",
"1",
"dz_ext1",
"+=",
"1",
"else",
":",
"zsv_ext2",
"-=",
"1",
"dz_ext2",
"+=",
"1",
"else",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsv_ext2",
"=",
"zsb",
"+",
"1",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz_ext2",
"=",
"dz0",
"-",
"1",
"if",
"(",
"c",
"&",
"0x08",
")",
"==",
"0",
":",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsb",
"wsv_ext2",
"=",
"wsb",
"-",
"1",
"dw_ext0",
"=",
"dw_ext1",
"=",
"dw0",
"dw_ext2",
"=",
"dw0",
"+",
"1",
"else",
":",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsv_ext2",
"=",
"wsb",
"+",
"1",
"dw_ext0",
"=",
"dw_ext1",
"=",
"dw_ext2",
"=",
"dw0",
"-",
"1",
"else",
":",
"# (0,0,0,0) is not one of the closest two pentachoron vertices.",
"c",
"=",
"(",
"a_po",
"|",
"b_po",
")",
"# Our three extra vertices are determined by the closest two.",
"if",
"(",
"c",
"&",
"0x01",
")",
"==",
"0",
":",
"xsv_ext0",
"=",
"xsv_ext2",
"=",
"xsb",
"xsv_ext1",
"=",
"xsb",
"-",
"1",
"dx_ext0",
"=",
"dx0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dx_ext1",
"=",
"dx0",
"+",
"1",
"-",
"SQUISH_CONSTANT_4D",
"dx_ext2",
"=",
"dx0",
"-",
"SQUISH_CONSTANT_4D",
"else",
":",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsv_ext2",
"=",
"xsb",
"+",
"1",
"dx_ext0",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dx_ext1",
"=",
"dx_ext2",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x02",
")",
"==",
"0",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysv_ext2",
"=",
"ysb",
"dy_ext0",
"=",
"dy0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy_ext1",
"=",
"dy_ext2",
"=",
"dy0",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x01",
")",
"==",
"0x01",
":",
"ysv_ext1",
"-=",
"1",
"dy_ext1",
"+=",
"1",
"else",
":",
"ysv_ext2",
"-=",
"1",
"dy_ext2",
"+=",
"1",
"else",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysv_ext2",
"=",
"ysb",
"+",
"1",
"dy_ext0",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy_ext1",
"=",
"dy_ext2",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x04",
")",
"==",
"0",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsv_ext2",
"=",
"zsb",
"dz_ext0",
"=",
"dz0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz_ext1",
"=",
"dz_ext2",
"=",
"dz0",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x03",
")",
"==",
"0x03",
":",
"zsv_ext1",
"-=",
"1",
"dz_ext1",
"+=",
"1",
"else",
":",
"zsv_ext2",
"-=",
"1",
"dz_ext2",
"+=",
"1",
"else",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsv_ext2",
"=",
"zsb",
"+",
"1",
"dz_ext0",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz_ext1",
"=",
"dz_ext2",
"=",
"dz0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x08",
")",
"==",
"0",
":",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsb",
"wsv_ext2",
"=",
"wsb",
"-",
"1",
"dw_ext0",
"=",
"dw0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext1",
"=",
"dw0",
"-",
"SQUISH_CONSTANT_4D",
"dw_ext2",
"=",
"dw0",
"+",
"1",
"-",
"SQUISH_CONSTANT_4D",
"else",
":",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsv_ext2",
"=",
"wsb",
"+",
"1",
"dw_ext0",
"=",
"dw0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext1",
"=",
"dw_ext2",
"=",
"dw0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"# Contribution (0,0,0,0)",
"attn0",
"=",
"2",
"-",
"dx0",
"*",
"dx0",
"-",
"dy0",
"*",
"dy0",
"-",
"dz0",
"*",
"dz0",
"-",
"dw0",
"*",
"dw0",
"if",
"attn0",
">",
"0",
":",
"attn0",
"*=",
"attn0",
"value",
"+=",
"attn0",
"*",
"attn0",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"0",
",",
"dx0",
",",
"dy0",
",",
"dz0",
",",
"dw0",
")",
"# Contribution (1,0,0,0)",
"dx1",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"dy1",
"=",
"dy0",
"-",
"0",
"-",
"SQUISH_CONSTANT_4D",
"dz1",
"=",
"dz0",
"-",
"0",
"-",
"SQUISH_CONSTANT_4D",
"dw1",
"=",
"dw0",
"-",
"0",
"-",
"SQUISH_CONSTANT_4D",
"attn1",
"=",
"2",
"-",
"dx1",
"*",
"dx1",
"-",
"dy1",
"*",
"dy1",
"-",
"dz1",
"*",
"dz1",
"-",
"dw1",
"*",
"dw1",
"if",
"attn1",
">",
"0",
":",
"attn1",
"*=",
"attn1",
"value",
"+=",
"attn1",
"*",
"attn1",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"0",
",",
"dx1",
",",
"dy1",
",",
"dz1",
",",
"dw1",
")",
"# Contribution (0,1,0,0)",
"dx2",
"=",
"dx0",
"-",
"0",
"-",
"SQUISH_CONSTANT_4D",
"dy2",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"dz2",
"=",
"dz1",
"dw2",
"=",
"dw1",
"attn2",
"=",
"2",
"-",
"dx2",
"*",
"dx2",
"-",
"dy2",
"*",
"dy2",
"-",
"dz2",
"*",
"dz2",
"-",
"dw2",
"*",
"dw2",
"if",
"attn2",
">",
"0",
":",
"attn2",
"*=",
"attn2",
"value",
"+=",
"attn2",
"*",
"attn2",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"0",
",",
"dx2",
",",
"dy2",
",",
"dz2",
",",
"dw2",
")",
"# Contribution (0,0,1,0)",
"dx3",
"=",
"dx2",
"dy3",
"=",
"dy1",
"dz3",
"=",
"dz0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"dw3",
"=",
"dw1",
"attn3",
"=",
"2",
"-",
"dx3",
"*",
"dx3",
"-",
"dy3",
"*",
"dy3",
"-",
"dz3",
"*",
"dz3",
"-",
"dw3",
"*",
"dw3",
"if",
"attn3",
">",
"0",
":",
"attn3",
"*=",
"attn3",
"value",
"+=",
"attn3",
"*",
"attn3",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"0",
",",
"dx3",
",",
"dy3",
",",
"dz3",
",",
"dw3",
")",
"# Contribution (0,0,0,1)",
"dx4",
"=",
"dx2",
"dy4",
"=",
"dy1",
"dz4",
"=",
"dz1",
"dw4",
"=",
"dw0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"attn4",
"=",
"2",
"-",
"dx4",
"*",
"dx4",
"-",
"dy4",
"*",
"dy4",
"-",
"dz4",
"*",
"dz4",
"-",
"dw4",
"*",
"dw4",
"if",
"attn4",
">",
"0",
":",
"attn4",
"*=",
"attn4",
"value",
"+=",
"attn4",
"*",
"attn4",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"1",
",",
"dx4",
",",
"dy4",
",",
"dz4",
",",
"dw4",
")",
"elif",
"in_sum",
">=",
"3",
":",
"# We're inside the pentachoron (4-Simplex) at (1,1,1,1)",
"# Determine which two of (1,1,1,0), (1,1,0,1), (1,0,1,1), (0,1,1,1) are closest.",
"a_po",
"=",
"0x0E",
"a_score",
"=",
"xins",
"b_po",
"=",
"0x0D",
"b_score",
"=",
"yins",
"if",
"a_score",
"<=",
"b_score",
"and",
"zins",
"<",
"b_score",
":",
"b_score",
"=",
"zins",
"b_po",
"=",
"0x0B",
"elif",
"a_score",
">",
"b_score",
"and",
"zins",
"<",
"a_score",
":",
"a_score",
"=",
"zins",
"a_po",
"=",
"0x0B",
"if",
"a_score",
"<=",
"b_score",
"and",
"wins",
"<",
"b_score",
":",
"b_score",
"=",
"wins",
"b_po",
"=",
"0x07",
"elif",
"a_score",
">",
"b_score",
"and",
"wins",
"<",
"a_score",
":",
"a_score",
"=",
"wins",
"a_po",
"=",
"0x07",
"# Now we determine the three lattice pos not part of the pentachoron that may contribute.",
"# This depends on the closest two pentachoron vertices, including (0,0,0,0)",
"uins",
"=",
"4",
"-",
"in_sum",
"if",
"uins",
"<",
"a_score",
"or",
"uins",
"<",
"b_score",
":",
"# (1,1,1,1) is one of the closest two pentachoron vertices.",
"c",
"=",
"b_po",
"if",
"(",
"b_score",
"<",
"a_score",
")",
"else",
"a_po",
"# Our other closest vertex is the closest out of a and b.",
"if",
"(",
"c",
"&",
"0x01",
")",
"!=",
"0",
":",
"xsv_ext0",
"=",
"xsb",
"+",
"2",
"xsv_ext1",
"=",
"xsv_ext2",
"=",
"xsb",
"+",
"1",
"dx_ext0",
"=",
"dx0",
"-",
"2",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"dx_ext1",
"=",
"dx_ext2",
"=",
"dx0",
"-",
"1",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsv_ext2",
"=",
"xsb",
"dx_ext0",
"=",
"dx_ext1",
"=",
"dx_ext2",
"=",
"dx0",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x02",
")",
"!=",
"0",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysv_ext2",
"=",
"ysb",
"+",
"1",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy_ext2",
"=",
"dy0",
"-",
"1",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x01",
")",
"!=",
"0",
":",
"ysv_ext1",
"+=",
"1",
"dy_ext1",
"-=",
"1",
"else",
":",
"ysv_ext0",
"+=",
"1",
"dy_ext0",
"-=",
"1",
"else",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysv_ext2",
"=",
"ysb",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy_ext2",
"=",
"dy0",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x04",
")",
"!=",
"0",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsv_ext2",
"=",
"zsb",
"+",
"1",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz_ext2",
"=",
"dz0",
"-",
"1",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x03",
")",
"!=",
"0x03",
":",
"if",
"(",
"c",
"&",
"0x03",
")",
"==",
"0",
":",
"zsv_ext0",
"+=",
"1",
"dz_ext0",
"-=",
"1",
"else",
":",
"zsv_ext1",
"+=",
"1",
"dz_ext1",
"-=",
"1",
"else",
":",
"zsv_ext2",
"+=",
"1",
"dz_ext2",
"-=",
"1",
"else",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsv_ext2",
"=",
"zsb",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz_ext2",
"=",
"dz0",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x08",
")",
"!=",
"0",
":",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsb",
"+",
"1",
"wsv_ext2",
"=",
"wsb",
"+",
"2",
"dw_ext0",
"=",
"dw_ext1",
"=",
"dw0",
"-",
"1",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext2",
"=",
"dw0",
"-",
"2",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsv_ext2",
"=",
"wsb",
"dw_ext0",
"=",
"dw_ext1",
"=",
"dw_ext2",
"=",
"dw0",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"# (1,1,1,1) is not one of the closest two pentachoron vertices.",
"c",
"=",
"(",
"a_po",
"&",
"b_po",
")",
"# Our three extra vertices are determined by the closest two.",
"if",
"(",
"c",
"&",
"0x01",
")",
"!=",
"0",
":",
"xsv_ext0",
"=",
"xsv_ext2",
"=",
"xsb",
"+",
"1",
"xsv_ext1",
"=",
"xsb",
"+",
"2",
"dx_ext0",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dx_ext1",
"=",
"dx0",
"-",
"2",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dx_ext2",
"=",
"dx0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsv_ext2",
"=",
"xsb",
"dx_ext0",
"=",
"dx0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dx_ext1",
"=",
"dx_ext2",
"=",
"dx0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x02",
")",
"!=",
"0",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysv_ext2",
"=",
"ysb",
"+",
"1",
"dy_ext0",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy_ext1",
"=",
"dy_ext2",
"=",
"dy0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x01",
")",
"!=",
"0",
":",
"ysv_ext2",
"+=",
"1",
"dy_ext2",
"-=",
"1",
"else",
":",
"ysv_ext1",
"+=",
"1",
"dy_ext1",
"-=",
"1",
"else",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysv_ext2",
"=",
"ysb",
"dy_ext0",
"=",
"dy0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy_ext1",
"=",
"dy_ext2",
"=",
"dy0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x04",
")",
"!=",
"0",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsv_ext2",
"=",
"zsb",
"+",
"1",
"dz_ext0",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz_ext1",
"=",
"dz_ext2",
"=",
"dz0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x03",
")",
"!=",
"0",
":",
"zsv_ext2",
"+=",
"1",
"dz_ext2",
"-=",
"1",
"else",
":",
"zsv_ext1",
"+=",
"1",
"dz_ext1",
"-=",
"1",
"else",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsv_ext2",
"=",
"zsb",
"dz_ext0",
"=",
"dz0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz_ext1",
"=",
"dz_ext2",
"=",
"dz0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x08",
")",
"!=",
"0",
":",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsb",
"+",
"1",
"wsv_ext2",
"=",
"wsb",
"+",
"2",
"dw_ext0",
"=",
"dw0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext1",
"=",
"dw0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext2",
"=",
"dw0",
"-",
"2",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsv_ext2",
"=",
"wsb",
"dw_ext0",
"=",
"dw0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext1",
"=",
"dw_ext2",
"=",
"dw0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"# Contribution (1,1,1,0)",
"dx4",
"=",
"dx0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dy4",
"=",
"dy0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dz4",
"=",
"dz0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dw4",
"=",
"dw0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"attn4",
"=",
"2",
"-",
"dx4",
"*",
"dx4",
"-",
"dy4",
"*",
"dy4",
"-",
"dz4",
"*",
"dz4",
"-",
"dw4",
"*",
"dw4",
"if",
"attn4",
">",
"0",
":",
"attn4",
"*=",
"attn4",
"value",
"+=",
"attn4",
"*",
"attn4",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"0",
",",
"dx4",
",",
"dy4",
",",
"dz4",
",",
"dw4",
")",
"# Contribution (1,1,0,1)",
"dx3",
"=",
"dx4",
"dy3",
"=",
"dy4",
"dz3",
"=",
"dz0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dw3",
"=",
"dw0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"attn3",
"=",
"2",
"-",
"dx3",
"*",
"dx3",
"-",
"dy3",
"*",
"dy3",
"-",
"dz3",
"*",
"dz3",
"-",
"dw3",
"*",
"dw3",
"if",
"attn3",
">",
"0",
":",
"attn3",
"*=",
"attn3",
"value",
"+=",
"attn3",
"*",
"attn3",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"1",
",",
"dx3",
",",
"dy3",
",",
"dz3",
",",
"dw3",
")",
"# Contribution (1,0,1,1)",
"dx2",
"=",
"dx4",
"dy2",
"=",
"dy0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dz2",
"=",
"dz4",
"dw2",
"=",
"dw3",
"attn2",
"=",
"2",
"-",
"dx2",
"*",
"dx2",
"-",
"dy2",
"*",
"dy2",
"-",
"dz2",
"*",
"dz2",
"-",
"dw2",
"*",
"dw2",
"if",
"attn2",
">",
"0",
":",
"attn2",
"*=",
"attn2",
"value",
"+=",
"attn2",
"*",
"attn2",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"1",
",",
"dx2",
",",
"dy2",
",",
"dz2",
",",
"dw2",
")",
"# Contribution (0,1,1,1)",
"dx1",
"=",
"dx0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dz1",
"=",
"dz4",
"dy1",
"=",
"dy4",
"dw1",
"=",
"dw3",
"attn1",
"=",
"2",
"-",
"dx1",
"*",
"dx1",
"-",
"dy1",
"*",
"dy1",
"-",
"dz1",
"*",
"dz1",
"-",
"dw1",
"*",
"dw1",
"if",
"attn1",
">",
"0",
":",
"attn1",
"*=",
"attn1",
"value",
"+=",
"attn1",
"*",
"attn1",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"1",
",",
"dx1",
",",
"dy1",
",",
"dz1",
",",
"dw1",
")",
"# Contribution (1,1,1,1)",
"dx0",
"=",
"dx0",
"-",
"1",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"dy0",
"=",
"dy0",
"-",
"1",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"dz0",
"=",
"dz0",
"-",
"1",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"dw0",
"=",
"dw0",
"-",
"1",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"attn0",
"=",
"2",
"-",
"dx0",
"*",
"dx0",
"-",
"dy0",
"*",
"dy0",
"-",
"dz0",
"*",
"dz0",
"-",
"dw0",
"*",
"dw0",
"if",
"attn0",
">",
"0",
":",
"attn0",
"*=",
"attn0",
"value",
"+=",
"attn0",
"*",
"attn0",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"1",
",",
"dx0",
",",
"dy0",
",",
"dz0",
",",
"dw0",
")",
"elif",
"in_sum",
"<=",
"2",
":",
"# We're inside the first dispentachoron (Rectified 4-Simplex)",
"a_is_bigger_side",
"=",
"True",
"b_is_bigger_side",
"=",
"True",
"# Decide between (1,1,0,0) and (0,0,1,1)",
"if",
"xins",
"+",
"yins",
">",
"zins",
"+",
"wins",
":",
"a_score",
"=",
"xins",
"+",
"yins",
"a_po",
"=",
"0x03",
"else",
":",
"a_score",
"=",
"zins",
"+",
"wins",
"a_po",
"=",
"0x0C",
"# Decide between (1,0,1,0) and (0,1,0,1)",
"if",
"xins",
"+",
"zins",
">",
"yins",
"+",
"wins",
":",
"b_score",
"=",
"xins",
"+",
"zins",
"b_po",
"=",
"0x05",
"else",
":",
"b_score",
"=",
"yins",
"+",
"wins",
"b_po",
"=",
"0x0A",
"# Closer between (1,0,0,1) and (0,1,1,0) will replace the further of a and b, if closer.",
"if",
"xins",
"+",
"wins",
">",
"yins",
"+",
"zins",
":",
"score",
"=",
"xins",
"+",
"wins",
"if",
"a_score",
">=",
"b_score",
"and",
"score",
">",
"b_score",
":",
"b_score",
"=",
"score",
"b_po",
"=",
"0x09",
"elif",
"a_score",
"<",
"b_score",
"and",
"score",
">",
"a_score",
":",
"a_score",
"=",
"score",
"a_po",
"=",
"0x09",
"else",
":",
"score",
"=",
"yins",
"+",
"zins",
"if",
"a_score",
">=",
"b_score",
"and",
"score",
">",
"b_score",
":",
"b_score",
"=",
"score",
"b_po",
"=",
"0x06",
"elif",
"a_score",
"<",
"b_score",
"and",
"score",
">",
"a_score",
":",
"a_score",
"=",
"score",
"a_po",
"=",
"0x06",
"# Decide if (1,0,0,0) is closer.",
"p1",
"=",
"2",
"-",
"in_sum",
"+",
"xins",
"if",
"a_score",
">=",
"b_score",
"and",
"p1",
">",
"b_score",
":",
"b_score",
"=",
"p1",
"b_po",
"=",
"0x01",
"b_is_bigger_side",
"=",
"False",
"elif",
"a_score",
"<",
"b_score",
"and",
"p1",
">",
"a_score",
":",
"a_score",
"=",
"p1",
"a_po",
"=",
"0x01",
"a_is_bigger_side",
"=",
"False",
"# Decide if (0,1,0,0) is closer.",
"p2",
"=",
"2",
"-",
"in_sum",
"+",
"yins",
"if",
"a_score",
">=",
"b_score",
"and",
"p2",
">",
"b_score",
":",
"b_score",
"=",
"p2",
"b_po",
"=",
"0x02",
"b_is_bigger_side",
"=",
"False",
"elif",
"a_score",
"<",
"b_score",
"and",
"p2",
">",
"a_score",
":",
"a_score",
"=",
"p2",
"a_po",
"=",
"0x02",
"a_is_bigger_side",
"=",
"False",
"# Decide if (0,0,1,0) is closer.",
"p3",
"=",
"2",
"-",
"in_sum",
"+",
"zins",
"if",
"a_score",
">=",
"b_score",
"and",
"p3",
">",
"b_score",
":",
"b_score",
"=",
"p3",
"b_po",
"=",
"0x04",
"b_is_bigger_side",
"=",
"False",
"elif",
"a_score",
"<",
"b_score",
"and",
"p3",
">",
"a_score",
":",
"a_score",
"=",
"p3",
"a_po",
"=",
"0x04",
"a_is_bigger_side",
"=",
"False",
"# Decide if (0,0,0,1) is closer.",
"p4",
"=",
"2",
"-",
"in_sum",
"+",
"wins",
"if",
"a_score",
">=",
"b_score",
"and",
"p4",
">",
"b_score",
":",
"b_po",
"=",
"0x08",
"b_is_bigger_side",
"=",
"False",
"elif",
"a_score",
"<",
"b_score",
"and",
"p4",
">",
"a_score",
":",
"a_po",
"=",
"0x08",
"a_is_bigger_side",
"=",
"False",
"# Where each of the two closest pos are determines how the extra three vertices are calculated.",
"if",
"a_is_bigger_side",
"==",
"b_is_bigger_side",
":",
"if",
"a_is_bigger_side",
":",
"# Both closest pos on the bigger side",
"c1",
"=",
"(",
"a_po",
"|",
"b_po",
")",
"c2",
"=",
"(",
"a_po",
"&",
"b_po",
")",
"if",
"(",
"c1",
"&",
"0x01",
")",
"==",
"0",
":",
"xsv_ext0",
"=",
"xsb",
"xsv_ext1",
"=",
"xsb",
"-",
"1",
"dx_ext0",
"=",
"dx0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dx_ext1",
"=",
"dx0",
"+",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsb",
"+",
"1",
"dx_ext0",
"=",
"dx0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dx_ext1",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x02",
")",
"==",
"0",
":",
"ysv_ext0",
"=",
"ysb",
"ysv_ext1",
"=",
"ysb",
"-",
"1",
"dy_ext0",
"=",
"dy0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dy_ext1",
"=",
"dy0",
"+",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"+",
"1",
"dy_ext0",
"=",
"dy0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dy_ext1",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x04",
")",
"==",
"0",
":",
"zsv_ext0",
"=",
"zsb",
"zsv_ext1",
"=",
"zsb",
"-",
"1",
"dz_ext0",
"=",
"dz0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dz_ext1",
"=",
"dz0",
"+",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"+",
"1",
"dz_ext0",
"=",
"dz0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dz_ext1",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x08",
")",
"==",
"0",
":",
"wsv_ext0",
"=",
"wsb",
"wsv_ext1",
"=",
"wsb",
"-",
"1",
"dw_ext0",
"=",
"dw0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext1",
"=",
"dw0",
"+",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsb",
"+",
"1",
"dw_ext0",
"=",
"dw0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext1",
"=",
"dw0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"# One combination is a _permutation of (0,0,0,2) based on c2",
"xsv_ext2",
"=",
"xsb",
"ysv_ext2",
"=",
"ysb",
"zsv_ext2",
"=",
"zsb",
"wsv_ext2",
"=",
"wsb",
"dx_ext2",
"=",
"dx0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy_ext2",
"=",
"dy0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz_ext2",
"=",
"dz0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext2",
"=",
"dw0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c2",
"&",
"0x01",
")",
"!=",
"0",
":",
"xsv_ext2",
"+=",
"2",
"dx_ext2",
"-=",
"2",
"elif",
"(",
"c2",
"&",
"0x02",
")",
"!=",
"0",
":",
"ysv_ext2",
"+=",
"2",
"dy_ext2",
"-=",
"2",
"elif",
"(",
"c2",
"&",
"0x04",
")",
"!=",
"0",
":",
"zsv_ext2",
"+=",
"2",
"dz_ext2",
"-=",
"2",
"else",
":",
"wsv_ext2",
"+=",
"2",
"dw_ext2",
"-=",
"2",
"else",
":",
"# Both closest pos on the smaller side",
"# One of the two extra pos is (0,0,0,0)",
"xsv_ext2",
"=",
"xsb",
"ysv_ext2",
"=",
"ysb",
"zsv_ext2",
"=",
"zsb",
"wsv_ext2",
"=",
"wsb",
"dx_ext2",
"=",
"dx0",
"dy_ext2",
"=",
"dy0",
"dz_ext2",
"=",
"dz0",
"dw_ext2",
"=",
"dw0",
"# Other two pos are based on the omitted axes.",
"c",
"=",
"(",
"a_po",
"|",
"b_po",
")",
"if",
"(",
"c",
"&",
"0x01",
")",
"==",
"0",
":",
"xsv_ext0",
"=",
"xsb",
"-",
"1",
"xsv_ext1",
"=",
"xsb",
"dx_ext0",
"=",
"dx0",
"+",
"1",
"-",
"SQUISH_CONSTANT_4D",
"dx_ext1",
"=",
"dx0",
"-",
"SQUISH_CONSTANT_4D",
"else",
":",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsb",
"+",
"1",
"dx_ext0",
"=",
"dx_ext1",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x02",
")",
"==",
"0",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy0",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x01",
")",
"==",
"0x01",
":",
"ysv_ext0",
"-=",
"1",
"dy_ext0",
"+=",
"1",
"else",
":",
"ysv_ext1",
"-=",
"1",
"dy_ext1",
"+=",
"1",
"else",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"+",
"1",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x04",
")",
"==",
"0",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz0",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x03",
")",
"==",
"0x03",
":",
"zsv_ext0",
"-=",
"1",
"dz_ext0",
"+=",
"1",
"else",
":",
"zsv_ext1",
"-=",
"1",
"dz_ext1",
"+=",
"1",
"else",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"+",
"1",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x08",
")",
"==",
"0",
":",
"wsv_ext0",
"=",
"wsb",
"wsv_ext1",
"=",
"wsb",
"-",
"1",
"dw_ext0",
"=",
"dw0",
"-",
"SQUISH_CONSTANT_4D",
"dw_ext1",
"=",
"dw0",
"+",
"1",
"-",
"SQUISH_CONSTANT_4D",
"else",
":",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsb",
"+",
"1",
"dw_ext0",
"=",
"dw_ext1",
"=",
"dw0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"else",
":",
"# One po on each \"side\"",
"if",
"a_is_bigger_side",
":",
"c1",
"=",
"a_po",
"c2",
"=",
"b_po",
"else",
":",
"c1",
"=",
"b_po",
"c2",
"=",
"a_po",
"# Two contributions are the bigger-sided po with each 0 replaced with -1.",
"if",
"(",
"c1",
"&",
"0x01",
")",
"==",
"0",
":",
"xsv_ext0",
"=",
"xsb",
"-",
"1",
"xsv_ext1",
"=",
"xsb",
"dx_ext0",
"=",
"dx0",
"+",
"1",
"-",
"SQUISH_CONSTANT_4D",
"dx_ext1",
"=",
"dx0",
"-",
"SQUISH_CONSTANT_4D",
"else",
":",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsb",
"+",
"1",
"dx_ext0",
"=",
"dx_ext1",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x02",
")",
"==",
"0",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy0",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x01",
")",
"==",
"0x01",
":",
"ysv_ext0",
"-=",
"1",
"dy_ext0",
"+=",
"1",
"else",
":",
"ysv_ext1",
"-=",
"1",
"dy_ext1",
"+=",
"1",
"else",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"+",
"1",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x04",
")",
"==",
"0",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz0",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x03",
")",
"==",
"0x03",
":",
"zsv_ext0",
"-=",
"1",
"dz_ext0",
"+=",
"1",
"else",
":",
"zsv_ext1",
"-=",
"1",
"dz_ext1",
"+=",
"1",
"else",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"+",
"1",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x08",
")",
"==",
"0",
":",
"wsv_ext0",
"=",
"wsb",
"wsv_ext1",
"=",
"wsb",
"-",
"1",
"dw_ext0",
"=",
"dw0",
"-",
"SQUISH_CONSTANT_4D",
"dw_ext1",
"=",
"dw0",
"+",
"1",
"-",
"SQUISH_CONSTANT_4D",
"else",
":",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsb",
"+",
"1",
"dw_ext0",
"=",
"dw_ext1",
"=",
"dw0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"# One contribution is a _permutation of (0,0,0,2) based on the smaller-sided po",
"xsv_ext2",
"=",
"xsb",
"ysv_ext2",
"=",
"ysb",
"zsv_ext2",
"=",
"zsb",
"wsv_ext2",
"=",
"wsb",
"dx_ext2",
"=",
"dx0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy_ext2",
"=",
"dy0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz_ext2",
"=",
"dz0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext2",
"=",
"dw0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c2",
"&",
"0x01",
")",
"!=",
"0",
":",
"xsv_ext2",
"+=",
"2",
"dx_ext2",
"-=",
"2",
"elif",
"(",
"c2",
"&",
"0x02",
")",
"!=",
"0",
":",
"ysv_ext2",
"+=",
"2",
"dy_ext2",
"-=",
"2",
"elif",
"(",
"c2",
"&",
"0x04",
")",
"!=",
"0",
":",
"zsv_ext2",
"+=",
"2",
"dz_ext2",
"-=",
"2",
"else",
":",
"wsv_ext2",
"+=",
"2",
"dw_ext2",
"-=",
"2",
"# Contribution (1,0,0,0)",
"dx1",
"=",
"dx0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"dy1",
"=",
"dy0",
"-",
"0",
"-",
"SQUISH_CONSTANT_4D",
"dz1",
"=",
"dz0",
"-",
"0",
"-",
"SQUISH_CONSTANT_4D",
"dw1",
"=",
"dw0",
"-",
"0",
"-",
"SQUISH_CONSTANT_4D",
"attn1",
"=",
"2",
"-",
"dx1",
"*",
"dx1",
"-",
"dy1",
"*",
"dy1",
"-",
"dz1",
"*",
"dz1",
"-",
"dw1",
"*",
"dw1",
"if",
"attn1",
">",
"0",
":",
"attn1",
"*=",
"attn1",
"value",
"+=",
"attn1",
"*",
"attn1",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"0",
",",
"dx1",
",",
"dy1",
",",
"dz1",
",",
"dw1",
")",
"# Contribution (0,1,0,0)",
"dx2",
"=",
"dx0",
"-",
"0",
"-",
"SQUISH_CONSTANT_4D",
"dy2",
"=",
"dy0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"dz2",
"=",
"dz1",
"dw2",
"=",
"dw1",
"attn2",
"=",
"2",
"-",
"dx2",
"*",
"dx2",
"-",
"dy2",
"*",
"dy2",
"-",
"dz2",
"*",
"dz2",
"-",
"dw2",
"*",
"dw2",
"if",
"attn2",
">",
"0",
":",
"attn2",
"*=",
"attn2",
"value",
"+=",
"attn2",
"*",
"attn2",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"0",
",",
"dx2",
",",
"dy2",
",",
"dz2",
",",
"dw2",
")",
"# Contribution (0,0,1,0)",
"dx3",
"=",
"dx2",
"dy3",
"=",
"dy1",
"dz3",
"=",
"dz0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"dw3",
"=",
"dw1",
"attn3",
"=",
"2",
"-",
"dx3",
"*",
"dx3",
"-",
"dy3",
"*",
"dy3",
"-",
"dz3",
"*",
"dz3",
"-",
"dw3",
"*",
"dw3",
"if",
"attn3",
">",
"0",
":",
"attn3",
"*=",
"attn3",
"value",
"+=",
"attn3",
"*",
"attn3",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"0",
",",
"dx3",
",",
"dy3",
",",
"dz3",
",",
"dw3",
")",
"# Contribution (0,0,0,1)",
"dx4",
"=",
"dx2",
"dy4",
"=",
"dy1",
"dz4",
"=",
"dz1",
"dw4",
"=",
"dw0",
"-",
"1",
"-",
"SQUISH_CONSTANT_4D",
"attn4",
"=",
"2",
"-",
"dx4",
"*",
"dx4",
"-",
"dy4",
"*",
"dy4",
"-",
"dz4",
"*",
"dz4",
"-",
"dw4",
"*",
"dw4",
"if",
"attn4",
">",
"0",
":",
"attn4",
"*=",
"attn4",
"value",
"+=",
"attn4",
"*",
"attn4",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"1",
",",
"dx4",
",",
"dy4",
",",
"dz4",
",",
"dw4",
")",
"# Contribution (1,1,0,0)",
"dx5",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy5",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz5",
"=",
"dz0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw5",
"=",
"dw0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"attn5",
"=",
"2",
"-",
"dx5",
"*",
"dx5",
"-",
"dy5",
"*",
"dy5",
"-",
"dz5",
"*",
"dz5",
"-",
"dw5",
"*",
"dw5",
"if",
"attn5",
">",
"0",
":",
"attn5",
"*=",
"attn5",
"value",
"+=",
"attn5",
"*",
"attn5",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"0",
",",
"dx5",
",",
"dy5",
",",
"dz5",
",",
"dw5",
")",
"# Contribution (1,0,1,0)",
"dx6",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy6",
"=",
"dy0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz6",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw6",
"=",
"dw0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"attn6",
"=",
"2",
"-",
"dx6",
"*",
"dx6",
"-",
"dy6",
"*",
"dy6",
"-",
"dz6",
"*",
"dz6",
"-",
"dw6",
"*",
"dw6",
"if",
"attn6",
">",
"0",
":",
"attn6",
"*=",
"attn6",
"value",
"+=",
"attn6",
"*",
"attn6",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"0",
",",
"dx6",
",",
"dy6",
",",
"dz6",
",",
"dw6",
")",
"# Contribution (1,0,0,1)",
"dx7",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy7",
"=",
"dy0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz7",
"=",
"dz0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw7",
"=",
"dw0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"attn7",
"=",
"2",
"-",
"dx7",
"*",
"dx7",
"-",
"dy7",
"*",
"dy7",
"-",
"dz7",
"*",
"dz7",
"-",
"dw7",
"*",
"dw7",
"if",
"attn7",
">",
"0",
":",
"attn7",
"*=",
"attn7",
"value",
"+=",
"attn7",
"*",
"attn7",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"1",
",",
"dx7",
",",
"dy7",
",",
"dz7",
",",
"dw7",
")",
"# Contribution (0,1,1,0)",
"dx8",
"=",
"dx0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy8",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz8",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw8",
"=",
"dw0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"attn8",
"=",
"2",
"-",
"dx8",
"*",
"dx8",
"-",
"dy8",
"*",
"dy8",
"-",
"dz8",
"*",
"dz8",
"-",
"dw8",
"*",
"dw8",
"if",
"attn8",
">",
"0",
":",
"attn8",
"*=",
"attn8",
"value",
"+=",
"attn8",
"*",
"attn8",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"0",
",",
"dx8",
",",
"dy8",
",",
"dz8",
",",
"dw8",
")",
"# Contribution (0,1,0,1)",
"dx9",
"=",
"dx0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy9",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz9",
"=",
"dz0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw9",
"=",
"dw0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"attn9",
"=",
"2",
"-",
"dx9",
"*",
"dx9",
"-",
"dy9",
"*",
"dy9",
"-",
"dz9",
"*",
"dz9",
"-",
"dw9",
"*",
"dw9",
"if",
"attn9",
">",
"0",
":",
"attn9",
"*=",
"attn9",
"value",
"+=",
"attn9",
"*",
"attn9",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"1",
",",
"dx9",
",",
"dy9",
",",
"dz9",
",",
"dw9",
")",
"# Contribution (0,0,1,1)",
"dx10",
"=",
"dx0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy10",
"=",
"dy0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz10",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw10",
"=",
"dw0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"attn10",
"=",
"2",
"-",
"dx10",
"*",
"dx10",
"-",
"dy10",
"*",
"dy10",
"-",
"dz10",
"*",
"dz10",
"-",
"dw10",
"*",
"dw10",
"if",
"attn10",
">",
"0",
":",
"attn10",
"*=",
"attn10",
"value",
"+=",
"attn10",
"*",
"attn10",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"1",
",",
"dx10",
",",
"dy10",
",",
"dz10",
",",
"dw10",
")",
"else",
":",
"# We're inside the second dispentachoron (Rectified 4-Simplex)",
"a_is_bigger_side",
"=",
"True",
"b_is_bigger_side",
"=",
"True",
"# Decide between (0,0,1,1) and (1,1,0,0)",
"if",
"xins",
"+",
"yins",
"<",
"zins",
"+",
"wins",
":",
"a_score",
"=",
"xins",
"+",
"yins",
"a_po",
"=",
"0x0C",
"else",
":",
"a_score",
"=",
"zins",
"+",
"wins",
"a_po",
"=",
"0x03",
"# Decide between (0,1,0,1) and (1,0,1,0)",
"if",
"xins",
"+",
"zins",
"<",
"yins",
"+",
"wins",
":",
"b_score",
"=",
"xins",
"+",
"zins",
"b_po",
"=",
"0x0A",
"else",
":",
"b_score",
"=",
"yins",
"+",
"wins",
"b_po",
"=",
"0x05",
"# Closer between (0,1,1,0) and (1,0,0,1) will replace the further of a and b, if closer.",
"if",
"xins",
"+",
"wins",
"<",
"yins",
"+",
"zins",
":",
"score",
"=",
"xins",
"+",
"wins",
"if",
"a_score",
"<=",
"b_score",
"and",
"score",
"<",
"b_score",
":",
"b_score",
"=",
"score",
"b_po",
"=",
"0x06",
"elif",
"a_score",
">",
"b_score",
"and",
"score",
"<",
"a_score",
":",
"a_score",
"=",
"score",
"a_po",
"=",
"0x06",
"else",
":",
"score",
"=",
"yins",
"+",
"zins",
"if",
"a_score",
"<=",
"b_score",
"and",
"score",
"<",
"b_score",
":",
"b_score",
"=",
"score",
"b_po",
"=",
"0x09",
"elif",
"a_score",
">",
"b_score",
"and",
"score",
"<",
"a_score",
":",
"a_score",
"=",
"score",
"a_po",
"=",
"0x09",
"# Decide if (0,1,1,1) is closer.",
"p1",
"=",
"3",
"-",
"in_sum",
"+",
"xins",
"if",
"a_score",
"<=",
"b_score",
"and",
"p1",
"<",
"b_score",
":",
"b_score",
"=",
"p1",
"b_po",
"=",
"0x0E",
"b_is_bigger_side",
"=",
"False",
"elif",
"a_score",
">",
"b_score",
"and",
"p1",
"<",
"a_score",
":",
"a_score",
"=",
"p1",
"a_po",
"=",
"0x0E",
"a_is_bigger_side",
"=",
"False",
"# Decide if (1,0,1,1) is closer.",
"p2",
"=",
"3",
"-",
"in_sum",
"+",
"yins",
"if",
"a_score",
"<=",
"b_score",
"and",
"p2",
"<",
"b_score",
":",
"b_score",
"=",
"p2",
"b_po",
"=",
"0x0D",
"b_is_bigger_side",
"=",
"False",
"elif",
"a_score",
">",
"b_score",
"and",
"p2",
"<",
"a_score",
":",
"a_score",
"=",
"p2",
"a_po",
"=",
"0x0D",
"a_is_bigger_side",
"=",
"False",
"# Decide if (1,1,0,1) is closer.",
"p3",
"=",
"3",
"-",
"in_sum",
"+",
"zins",
"if",
"a_score",
"<=",
"b_score",
"and",
"p3",
"<",
"b_score",
":",
"b_score",
"=",
"p3",
"b_po",
"=",
"0x0B",
"b_is_bigger_side",
"=",
"False",
"elif",
"a_score",
">",
"b_score",
"and",
"p3",
"<",
"a_score",
":",
"a_score",
"=",
"p3",
"a_po",
"=",
"0x0B",
"a_is_bigger_side",
"=",
"False",
"# Decide if (1,1,1,0) is closer.",
"p4",
"=",
"3",
"-",
"in_sum",
"+",
"wins",
"if",
"a_score",
"<=",
"b_score",
"and",
"p4",
"<",
"b_score",
":",
"b_po",
"=",
"0x07",
"b_is_bigger_side",
"=",
"False",
"elif",
"a_score",
">",
"b_score",
"and",
"p4",
"<",
"a_score",
":",
"a_po",
"=",
"0x07",
"a_is_bigger_side",
"=",
"False",
"# Where each of the two closest pos are determines how the extra three vertices are calculated.",
"if",
"a_is_bigger_side",
"==",
"b_is_bigger_side",
":",
"if",
"a_is_bigger_side",
":",
"# Both closest pos on the bigger side",
"c1",
"=",
"(",
"a_po",
"&",
"b_po",
")",
"c2",
"=",
"(",
"a_po",
"|",
"b_po",
")",
"# Two contributions are _permutations of (0,0,0,1) and (0,0,0,2) based on c1",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsb",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsb",
"dx_ext0",
"=",
"dx0",
"-",
"SQUISH_CONSTANT_4D",
"dy_ext0",
"=",
"dy0",
"-",
"SQUISH_CONSTANT_4D",
"dz_ext0",
"=",
"dz0",
"-",
"SQUISH_CONSTANT_4D",
"dw_ext0",
"=",
"dw0",
"-",
"SQUISH_CONSTANT_4D",
"dx_ext1",
"=",
"dx0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy_ext1",
"=",
"dy0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz_ext1",
"=",
"dz0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext1",
"=",
"dw0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x01",
")",
"!=",
"0",
":",
"xsv_ext0",
"+=",
"1",
"dx_ext0",
"-=",
"1",
"xsv_ext1",
"+=",
"2",
"dx_ext1",
"-=",
"2",
"elif",
"(",
"c1",
"&",
"0x02",
")",
"!=",
"0",
":",
"ysv_ext0",
"+=",
"1",
"dy_ext0",
"-=",
"1",
"ysv_ext1",
"+=",
"2",
"dy_ext1",
"-=",
"2",
"elif",
"(",
"c1",
"&",
"0x04",
")",
"!=",
"0",
":",
"zsv_ext0",
"+=",
"1",
"dz_ext0",
"-=",
"1",
"zsv_ext1",
"+=",
"2",
"dz_ext1",
"-=",
"2",
"else",
":",
"wsv_ext0",
"+=",
"1",
"dw_ext0",
"-=",
"1",
"wsv_ext1",
"+=",
"2",
"dw_ext1",
"-=",
"2",
"# One contribution is a _permutation of (1,1,1,-1) based on c2",
"xsv_ext2",
"=",
"xsb",
"+",
"1",
"ysv_ext2",
"=",
"ysb",
"+",
"1",
"zsv_ext2",
"=",
"zsb",
"+",
"1",
"wsv_ext2",
"=",
"wsb",
"+",
"1",
"dx_ext2",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy_ext2",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz_ext2",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext2",
"=",
"dw0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c2",
"&",
"0x01",
")",
"==",
"0",
":",
"xsv_ext2",
"-=",
"2",
"dx_ext2",
"+=",
"2",
"elif",
"(",
"c2",
"&",
"0x02",
")",
"==",
"0",
":",
"ysv_ext2",
"-=",
"2",
"dy_ext2",
"+=",
"2",
"elif",
"(",
"c2",
"&",
"0x04",
")",
"==",
"0",
":",
"zsv_ext2",
"-=",
"2",
"dz_ext2",
"+=",
"2",
"else",
":",
"wsv_ext2",
"-=",
"2",
"dw_ext2",
"+=",
"2",
"else",
":",
"# Both closest pos on the smaller side",
"# One of the two extra pos is (1,1,1,1)",
"xsv_ext2",
"=",
"xsb",
"+",
"1",
"ysv_ext2",
"=",
"ysb",
"+",
"1",
"zsv_ext2",
"=",
"zsb",
"+",
"1",
"wsv_ext2",
"=",
"wsb",
"+",
"1",
"dx_ext2",
"=",
"dx0",
"-",
"1",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"dy_ext2",
"=",
"dy0",
"-",
"1",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"dz_ext2",
"=",
"dz0",
"-",
"1",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext2",
"=",
"dw0",
"-",
"1",
"-",
"4",
"*",
"SQUISH_CONSTANT_4D",
"# Other two pos are based on the shared axes.",
"c",
"=",
"(",
"a_po",
"&",
"b_po",
")",
"if",
"(",
"c",
"&",
"0x01",
")",
"!=",
"0",
":",
"xsv_ext0",
"=",
"xsb",
"+",
"2",
"xsv_ext1",
"=",
"xsb",
"+",
"1",
"dx_ext0",
"=",
"dx0",
"-",
"2",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dx_ext1",
"=",
"dx0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsb",
"dx_ext0",
"=",
"dx_ext1",
"=",
"dx0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x02",
")",
"!=",
"0",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"+",
"1",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x01",
")",
"==",
"0",
":",
"ysv_ext0",
"+=",
"1",
"dy_ext0",
"-=",
"1",
"else",
":",
"ysv_ext1",
"+=",
"1",
"dy_ext1",
"-=",
"1",
"else",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x04",
")",
"!=",
"0",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"+",
"1",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x03",
")",
"==",
"0",
":",
"zsv_ext0",
"+=",
"1",
"dz_ext0",
"-=",
"1",
"else",
":",
"zsv_ext1",
"+=",
"1",
"dz_ext1",
"-=",
"1",
"else",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c",
"&",
"0x08",
")",
"!=",
"0",
":",
"wsv_ext0",
"=",
"wsb",
"+",
"1",
"wsv_ext1",
"=",
"wsb",
"+",
"2",
"dw_ext0",
"=",
"dw0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext1",
"=",
"dw0",
"-",
"2",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsb",
"dw_ext0",
"=",
"dw_ext1",
"=",
"dw0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"# One po on each \"side\"",
"if",
"a_is_bigger_side",
":",
"c1",
"=",
"a_po",
"c2",
"=",
"b_po",
"else",
":",
"c1",
"=",
"b_po",
"c2",
"=",
"a_po",
"# Two contributions are the bigger-sided po with each 1 replaced with 2.",
"if",
"(",
"c1",
"&",
"0x01",
")",
"!=",
"0",
":",
"xsv_ext0",
"=",
"xsb",
"+",
"2",
"xsv_ext1",
"=",
"xsb",
"+",
"1",
"dx_ext0",
"=",
"dx0",
"-",
"2",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dx_ext1",
"=",
"dx0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"xsv_ext0",
"=",
"xsv_ext1",
"=",
"xsb",
"dx_ext0",
"=",
"dx_ext1",
"=",
"dx0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x02",
")",
"!=",
"0",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"+",
"1",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x01",
")",
"==",
"0",
":",
"ysv_ext0",
"+=",
"1",
"dy_ext0",
"-=",
"1",
"else",
":",
"ysv_ext1",
"+=",
"1",
"dy_ext1",
"-=",
"1",
"else",
":",
"ysv_ext0",
"=",
"ysv_ext1",
"=",
"ysb",
"dy_ext0",
"=",
"dy_ext1",
"=",
"dy0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x04",
")",
"!=",
"0",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"+",
"1",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x03",
")",
"==",
"0",
":",
"zsv_ext0",
"+=",
"1",
"dz_ext0",
"-=",
"1",
"else",
":",
"zsv_ext1",
"+=",
"1",
"dz_ext1",
"-=",
"1",
"else",
":",
"zsv_ext0",
"=",
"zsv_ext1",
"=",
"zsb",
"dz_ext0",
"=",
"dz_ext1",
"=",
"dz0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c1",
"&",
"0x08",
")",
"!=",
"0",
":",
"wsv_ext0",
"=",
"wsb",
"+",
"1",
"wsv_ext1",
"=",
"wsb",
"+",
"2",
"dw_ext0",
"=",
"dw0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext1",
"=",
"dw0",
"-",
"2",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"else",
":",
"wsv_ext0",
"=",
"wsv_ext1",
"=",
"wsb",
"dw_ext0",
"=",
"dw_ext1",
"=",
"dw0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"# One contribution is a _permutation of (1,1,1,-1) based on the smaller-sided po",
"xsv_ext2",
"=",
"xsb",
"+",
"1",
"ysv_ext2",
"=",
"ysb",
"+",
"1",
"zsv_ext2",
"=",
"zsb",
"+",
"1",
"wsv_ext2",
"=",
"wsb",
"+",
"1",
"dx_ext2",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy_ext2",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz_ext2",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw_ext2",
"=",
"dw0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"if",
"(",
"c2",
"&",
"0x01",
")",
"==",
"0",
":",
"xsv_ext2",
"-=",
"2",
"dx_ext2",
"+=",
"2",
"elif",
"(",
"c2",
"&",
"0x02",
")",
"==",
"0",
":",
"ysv_ext2",
"-=",
"2",
"dy_ext2",
"+=",
"2",
"elif",
"(",
"c2",
"&",
"0x04",
")",
"==",
"0",
":",
"zsv_ext2",
"-=",
"2",
"dz_ext2",
"+=",
"2",
"else",
":",
"wsv_ext2",
"-=",
"2",
"dw_ext2",
"+=",
"2",
"# Contribution (1,1,1,0)",
"dx4",
"=",
"dx0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dy4",
"=",
"dy0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dz4",
"=",
"dz0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dw4",
"=",
"dw0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"attn4",
"=",
"2",
"-",
"dx4",
"*",
"dx4",
"-",
"dy4",
"*",
"dy4",
"-",
"dz4",
"*",
"dz4",
"-",
"dw4",
"*",
"dw4",
"if",
"attn4",
">",
"0",
":",
"attn4",
"*=",
"attn4",
"value",
"+=",
"attn4",
"*",
"attn4",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"0",
",",
"dx4",
",",
"dy4",
",",
"dz4",
",",
"dw4",
")",
"# Contribution (1,1,0,1)",
"dx3",
"=",
"dx4",
"dy3",
"=",
"dy4",
"dz3",
"=",
"dz0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dw3",
"=",
"dw0",
"-",
"1",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"attn3",
"=",
"2",
"-",
"dx3",
"*",
"dx3",
"-",
"dy3",
"*",
"dy3",
"-",
"dz3",
"*",
"dz3",
"-",
"dw3",
"*",
"dw3",
"if",
"attn3",
">",
"0",
":",
"attn3",
"*=",
"attn3",
"value",
"+=",
"attn3",
"*",
"attn3",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"1",
",",
"dx3",
",",
"dy3",
",",
"dz3",
",",
"dw3",
")",
"# Contribution (1,0,1,1)",
"dx2",
"=",
"dx4",
"dy2",
"=",
"dy0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dz2",
"=",
"dz4",
"dw2",
"=",
"dw3",
"attn2",
"=",
"2",
"-",
"dx2",
"*",
"dx2",
"-",
"dy2",
"*",
"dy2",
"-",
"dz2",
"*",
"dz2",
"-",
"dw2",
"*",
"dw2",
"if",
"attn2",
">",
"0",
":",
"attn2",
"*=",
"attn2",
"value",
"+=",
"attn2",
"*",
"attn2",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"1",
",",
"dx2",
",",
"dy2",
",",
"dz2",
",",
"dw2",
")",
"# Contribution (0,1,1,1)",
"dx1",
"=",
"dx0",
"-",
"3",
"*",
"SQUISH_CONSTANT_4D",
"dz1",
"=",
"dz4",
"dy1",
"=",
"dy4",
"dw1",
"=",
"dw3",
"attn1",
"=",
"2",
"-",
"dx1",
"*",
"dx1",
"-",
"dy1",
"*",
"dy1",
"-",
"dz1",
"*",
"dz1",
"-",
"dw1",
"*",
"dw1",
"if",
"attn1",
">",
"0",
":",
"attn1",
"*=",
"attn1",
"value",
"+=",
"attn1",
"*",
"attn1",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"1",
",",
"dx1",
",",
"dy1",
",",
"dz1",
",",
"dw1",
")",
"# Contribution (1,1,0,0)",
"dx5",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy5",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz5",
"=",
"dz0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw5",
"=",
"dw0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"attn5",
"=",
"2",
"-",
"dx5",
"*",
"dx5",
"-",
"dy5",
"*",
"dy5",
"-",
"dz5",
"*",
"dz5",
"-",
"dw5",
"*",
"dw5",
"if",
"attn5",
">",
"0",
":",
"attn5",
"*=",
"attn5",
"value",
"+=",
"attn5",
"*",
"attn5",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"0",
",",
"dx5",
",",
"dy5",
",",
"dz5",
",",
"dw5",
")",
"# Contribution (1,0,1,0)",
"dx6",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy6",
"=",
"dy0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz6",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw6",
"=",
"dw0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"attn6",
"=",
"2",
"-",
"dx6",
"*",
"dx6",
"-",
"dy6",
"*",
"dy6",
"-",
"dz6",
"*",
"dz6",
"-",
"dw6",
"*",
"dw6",
"if",
"attn6",
">",
"0",
":",
"attn6",
"*=",
"attn6",
"value",
"+=",
"attn6",
"*",
"attn6",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"0",
",",
"dx6",
",",
"dy6",
",",
"dz6",
",",
"dw6",
")",
"# Contribution (1,0,0,1)",
"dx7",
"=",
"dx0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy7",
"=",
"dy0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz7",
"=",
"dz0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw7",
"=",
"dw0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"attn7",
"=",
"2",
"-",
"dx7",
"*",
"dx7",
"-",
"dy7",
"*",
"dy7",
"-",
"dz7",
"*",
"dz7",
"-",
"dw7",
"*",
"dw7",
"if",
"attn7",
">",
"0",
":",
"attn7",
"*=",
"attn7",
"value",
"+=",
"attn7",
"*",
"attn7",
"*",
"extrapolate",
"(",
"xsb",
"+",
"1",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"1",
",",
"dx7",
",",
"dy7",
",",
"dz7",
",",
"dw7",
")",
"# Contribution (0,1,1,0)",
"dx8",
"=",
"dx0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy8",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz8",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw8",
"=",
"dw0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"attn8",
"=",
"2",
"-",
"dx8",
"*",
"dx8",
"-",
"dy8",
"*",
"dy8",
"-",
"dz8",
"*",
"dz8",
"-",
"dw8",
"*",
"dw8",
"if",
"attn8",
">",
"0",
":",
"attn8",
"*=",
"attn8",
"value",
"+=",
"attn8",
"*",
"attn8",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"0",
",",
"dx8",
",",
"dy8",
",",
"dz8",
",",
"dw8",
")",
"# Contribution (0,1,0,1)",
"dx9",
"=",
"dx0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy9",
"=",
"dy0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz9",
"=",
"dz0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw9",
"=",
"dw0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"attn9",
"=",
"2",
"-",
"dx9",
"*",
"dx9",
"-",
"dy9",
"*",
"dy9",
"-",
"dz9",
"*",
"dz9",
"-",
"dw9",
"*",
"dw9",
"if",
"attn9",
">",
"0",
":",
"attn9",
"*=",
"attn9",
"value",
"+=",
"attn9",
"*",
"attn9",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"1",
",",
"zsb",
"+",
"0",
",",
"wsb",
"+",
"1",
",",
"dx9",
",",
"dy9",
",",
"dz9",
",",
"dw9",
")",
"# Contribution (0,0,1,1)",
"dx10",
"=",
"dx0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dy10",
"=",
"dy0",
"-",
"0",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dz10",
"=",
"dz0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"dw10",
"=",
"dw0",
"-",
"1",
"-",
"2",
"*",
"SQUISH_CONSTANT_4D",
"attn10",
"=",
"2",
"-",
"dx10",
"*",
"dx10",
"-",
"dy10",
"*",
"dy10",
"-",
"dz10",
"*",
"dz10",
"-",
"dw10",
"*",
"dw10",
"if",
"attn10",
">",
"0",
":",
"attn10",
"*=",
"attn10",
"value",
"+=",
"attn10",
"*",
"attn10",
"*",
"extrapolate",
"(",
"xsb",
"+",
"0",
",",
"ysb",
"+",
"0",
",",
"zsb",
"+",
"1",
",",
"wsb",
"+",
"1",
",",
"dx10",
",",
"dy10",
",",
"dz10",
",",
"dw10",
")",
"# First extra vertex",
"attn_ext0",
"=",
"2",
"-",
"dx_ext0",
"*",
"dx_ext0",
"-",
"dy_ext0",
"*",
"dy_ext0",
"-",
"dz_ext0",
"*",
"dz_ext0",
"-",
"dw_ext0",
"*",
"dw_ext0",
"if",
"attn_ext0",
">",
"0",
":",
"attn_ext0",
"*=",
"attn_ext0",
"value",
"+=",
"attn_ext0",
"*",
"attn_ext0",
"*",
"extrapolate",
"(",
"xsv_ext0",
",",
"ysv_ext0",
",",
"zsv_ext0",
",",
"wsv_ext0",
",",
"dx_ext0",
",",
"dy_ext0",
",",
"dz_ext0",
",",
"dw_ext0",
")",
"# Second extra vertex",
"attn_ext1",
"=",
"2",
"-",
"dx_ext1",
"*",
"dx_ext1",
"-",
"dy_ext1",
"*",
"dy_ext1",
"-",
"dz_ext1",
"*",
"dz_ext1",
"-",
"dw_ext1",
"*",
"dw_ext1",
"if",
"attn_ext1",
">",
"0",
":",
"attn_ext1",
"*=",
"attn_ext1",
"value",
"+=",
"attn_ext1",
"*",
"attn_ext1",
"*",
"extrapolate",
"(",
"xsv_ext1",
",",
"ysv_ext1",
",",
"zsv_ext1",
",",
"wsv_ext1",
",",
"dx_ext1",
",",
"dy_ext1",
",",
"dz_ext1",
",",
"dw_ext1",
")",
"# Third extra vertex",
"attn_ext2",
"=",
"2",
"-",
"dx_ext2",
"*",
"dx_ext2",
"-",
"dy_ext2",
"*",
"dy_ext2",
"-",
"dz_ext2",
"*",
"dz_ext2",
"-",
"dw_ext2",
"*",
"dw_ext2",
"if",
"attn_ext2",
">",
"0",
":",
"attn_ext2",
"*=",
"attn_ext2",
"value",
"+=",
"attn_ext2",
"*",
"attn_ext2",
"*",
"extrapolate",
"(",
"xsv_ext2",
",",
"ysv_ext2",
",",
"zsv_ext2",
",",
"wsv_ext2",
",",
"dx_ext2",
",",
"dy_ext2",
",",
"dz_ext2",
",",
"dw_ext2",
")",
"return",
"value",
"/",
"NORM_CONSTANT_4D"
] | Generate 4D OpenSimplex noise from X,Y,Z,W coordinates. | [
"Generate",
"4D",
"OpenSimplex",
"noise",
"from",
"X",
"Y",
"Z",
"W",
"coordinates",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/external/opensimplex.py#L743-L1934 | valid |
aleju/imgaug | imgaug/augmenters/contrast.py | adjust_contrast_gamma | def adjust_contrast_gamma(arr, gamma):
"""
Adjust contrast by scaling each pixel value to ``255 * ((I_ij/255)**gamma)``.
dtype support::
* ``uint8``: yes; fully tested (1) (2) (3)
* ``uint16``: yes; tested (2) (3)
* ``uint32``: yes; tested (2) (3)
* ``uint64``: yes; tested (2) (3) (4)
* ``int8``: limited; tested (2) (3) (5)
* ``int16``: limited; tested (2) (3) (5)
* ``int32``: limited; tested (2) (3) (5)
* ``int64``: limited; tested (2) (3) (4) (5)
* ``float16``: limited; tested (5)
* ``float32``: limited; tested (5)
* ``float64``: limited; tested (5)
* ``float128``: no (6)
* ``bool``: no (7)
- (1) Handled by ``cv2``. Other dtypes are handled by ``skimage``.
- (2) Normalization is done as ``I_ij/max``, where ``max`` is the maximum value of the
dtype, e.g. 255 for ``uint8``. The normalization is reversed afterwards,
e.g. ``result*255`` for ``uint8``.
- (3) Integer-like values are not rounded after applying the contrast adjustment equation
(before inverting the normalization to 0.0-1.0 space), i.e. projection from continuous
space to discrete happens according to floor function.
- (4) Note that scikit-image doc says that integers are converted to ``float64`` values before
applying the contrast normalization method. This might lead to inaccuracies for large
64bit integer values. Tests showed no indication of that happening though.
- (5) Must not contain negative values. Values >=0 are fully supported.
- (6) Leads to error in scikit-image.
- (7) Does not make sense for contrast adjustments.
Parameters
----------
arr : numpy.ndarray
Array for which to adjust the contrast. Dtype ``uint8`` is fastest.
gamma : number
Exponent for the contrast adjustment. Higher values darken the image.
Returns
-------
numpy.ndarray
Array with adjusted contrast.
"""
# int8 is also possible according to docs
# https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html#cv2.LUT , but here it seemed
# like `d` was 0 for CV_8S, causing that to fail
if arr.dtype.name == "uint8":
min_value, _center_value, max_value = iadt.get_value_range_of_dtype(arr.dtype)
dynamic_range = max_value - min_value
value_range = np.linspace(0, 1.0, num=dynamic_range+1, dtype=np.float32)
# 255 * ((I_ij/255)**gamma)
# using np.float32(.) here still works when the input is a numpy array of size 1
table = (min_value + (value_range ** np.float32(gamma)) * dynamic_range)
arr_aug = cv2.LUT(arr, np.clip(table, min_value, max_value).astype(arr.dtype))
if arr.ndim == 3 and arr_aug.ndim == 2:
return arr_aug[..., np.newaxis]
return arr_aug
else:
return ski_exposure.adjust_gamma(arr, gamma) | python | def adjust_contrast_gamma(arr, gamma):
"""
Adjust contrast by scaling each pixel value to ``255 * ((I_ij/255)**gamma)``.
dtype support::
* ``uint8``: yes; fully tested (1) (2) (3)
* ``uint16``: yes; tested (2) (3)
* ``uint32``: yes; tested (2) (3)
* ``uint64``: yes; tested (2) (3) (4)
* ``int8``: limited; tested (2) (3) (5)
* ``int16``: limited; tested (2) (3) (5)
* ``int32``: limited; tested (2) (3) (5)
* ``int64``: limited; tested (2) (3) (4) (5)
* ``float16``: limited; tested (5)
* ``float32``: limited; tested (5)
* ``float64``: limited; tested (5)
* ``float128``: no (6)
* ``bool``: no (7)
- (1) Handled by ``cv2``. Other dtypes are handled by ``skimage``.
- (2) Normalization is done as ``I_ij/max``, where ``max`` is the maximum value of the
dtype, e.g. 255 for ``uint8``. The normalization is reversed afterwards,
e.g. ``result*255`` for ``uint8``.
- (3) Integer-like values are not rounded after applying the contrast adjustment equation
(before inverting the normalization to 0.0-1.0 space), i.e. projection from continuous
space to discrete happens according to floor function.
- (4) Note that scikit-image doc says that integers are converted to ``float64`` values before
applying the contrast normalization method. This might lead to inaccuracies for large
64bit integer values. Tests showed no indication of that happening though.
- (5) Must not contain negative values. Values >=0 are fully supported.
- (6) Leads to error in scikit-image.
- (7) Does not make sense for contrast adjustments.
Parameters
----------
arr : numpy.ndarray
Array for which to adjust the contrast. Dtype ``uint8`` is fastest.
gamma : number
Exponent for the contrast adjustment. Higher values darken the image.
Returns
-------
numpy.ndarray
Array with adjusted contrast.
"""
# int8 is also possible according to docs
# https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html#cv2.LUT , but here it seemed
# like `d` was 0 for CV_8S, causing that to fail
if arr.dtype.name == "uint8":
min_value, _center_value, max_value = iadt.get_value_range_of_dtype(arr.dtype)
dynamic_range = max_value - min_value
value_range = np.linspace(0, 1.0, num=dynamic_range+1, dtype=np.float32)
# 255 * ((I_ij/255)**gamma)
# using np.float32(.) here still works when the input is a numpy array of size 1
table = (min_value + (value_range ** np.float32(gamma)) * dynamic_range)
arr_aug = cv2.LUT(arr, np.clip(table, min_value, max_value).astype(arr.dtype))
if arr.ndim == 3 and arr_aug.ndim == 2:
return arr_aug[..., np.newaxis]
return arr_aug
else:
return ski_exposure.adjust_gamma(arr, gamma) | [
"def",
"adjust_contrast_gamma",
"(",
"arr",
",",
"gamma",
")",
":",
"# int8 is also possible according to docs",
"# https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html#cv2.LUT , but here it seemed",
"# like `d` was 0 for CV_8S, causing that to fail",
"if",
"arr",
".",
"dtype",
".",
"name",
"==",
"\"uint8\"",
":",
"min_value",
",",
"_center_value",
",",
"max_value",
"=",
"iadt",
".",
"get_value_range_of_dtype",
"(",
"arr",
".",
"dtype",
")",
"dynamic_range",
"=",
"max_value",
"-",
"min_value",
"value_range",
"=",
"np",
".",
"linspace",
"(",
"0",
",",
"1.0",
",",
"num",
"=",
"dynamic_range",
"+",
"1",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
"# 255 * ((I_ij/255)**gamma)",
"# using np.float32(.) here still works when the input is a numpy array of size 1",
"table",
"=",
"(",
"min_value",
"+",
"(",
"value_range",
"**",
"np",
".",
"float32",
"(",
"gamma",
")",
")",
"*",
"dynamic_range",
")",
"arr_aug",
"=",
"cv2",
".",
"LUT",
"(",
"arr",
",",
"np",
".",
"clip",
"(",
"table",
",",
"min_value",
",",
"max_value",
")",
".",
"astype",
"(",
"arr",
".",
"dtype",
")",
")",
"if",
"arr",
".",
"ndim",
"==",
"3",
"and",
"arr_aug",
".",
"ndim",
"==",
"2",
":",
"return",
"arr_aug",
"[",
"...",
",",
"np",
".",
"newaxis",
"]",
"return",
"arr_aug",
"else",
":",
"return",
"ski_exposure",
".",
"adjust_gamma",
"(",
"arr",
",",
"gamma",
")"
] | Adjust contrast by scaling each pixel value to ``255 * ((I_ij/255)**gamma)``.
dtype support::
* ``uint8``: yes; fully tested (1) (2) (3)
* ``uint16``: yes; tested (2) (3)
* ``uint32``: yes; tested (2) (3)
* ``uint64``: yes; tested (2) (3) (4)
* ``int8``: limited; tested (2) (3) (5)
* ``int16``: limited; tested (2) (3) (5)
* ``int32``: limited; tested (2) (3) (5)
* ``int64``: limited; tested (2) (3) (4) (5)
* ``float16``: limited; tested (5)
* ``float32``: limited; tested (5)
* ``float64``: limited; tested (5)
* ``float128``: no (6)
* ``bool``: no (7)
- (1) Handled by ``cv2``. Other dtypes are handled by ``skimage``.
- (2) Normalization is done as ``I_ij/max``, where ``max`` is the maximum value of the
dtype, e.g. 255 for ``uint8``. The normalization is reversed afterwards,
e.g. ``result*255`` for ``uint8``.
- (3) Integer-like values are not rounded after applying the contrast adjustment equation
(before inverting the normalization to 0.0-1.0 space), i.e. projection from continuous
space to discrete happens according to floor function.
- (4) Note that scikit-image doc says that integers are converted to ``float64`` values before
applying the contrast normalization method. This might lead to inaccuracies for large
64bit integer values. Tests showed no indication of that happening though.
- (5) Must not contain negative values. Values >=0 are fully supported.
- (6) Leads to error in scikit-image.
- (7) Does not make sense for contrast adjustments.
Parameters
----------
arr : numpy.ndarray
Array for which to adjust the contrast. Dtype ``uint8`` is fastest.
gamma : number
Exponent for the contrast adjustment. Higher values darken the image.
Returns
-------
numpy.ndarray
Array with adjusted contrast. | [
"Adjust",
"contrast",
"by",
"scaling",
"each",
"pixel",
"value",
"to",
"255",
"*",
"((",
"I_ij",
"/",
"255",
")",
"**",
"gamma",
")",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmenters/contrast.py#L41-L105 | valid |
aleju/imgaug | imgaug/augmenters/contrast.py | adjust_contrast_sigmoid | def adjust_contrast_sigmoid(arr, gain, cutoff):
"""
Adjust contrast by scaling each pixel value to ``255 * 1/(1 + exp(gain*(cutoff - I_ij/255)))``.
dtype support::
* ``uint8``: yes; fully tested (1) (2) (3)
* ``uint16``: yes; tested (2) (3)
* ``uint32``: yes; tested (2) (3)
* ``uint64``: yes; tested (2) (3) (4)
* ``int8``: limited; tested (2) (3) (5)
* ``int16``: limited; tested (2) (3) (5)
* ``int32``: limited; tested (2) (3) (5)
* ``int64``: limited; tested (2) (3) (4) (5)
* ``float16``: limited; tested (5)
* ``float32``: limited; tested (5)
* ``float64``: limited; tested (5)
* ``float128``: no (6)
* ``bool``: no (7)
- (1) Handled by ``cv2``. Other dtypes are handled by ``skimage``.
- (2) Normalization is done as ``I_ij/max``, where ``max`` is the maximum value of the
dtype, e.g. 255 for ``uint8``. The normalization is reversed afterwards,
e.g. ``result*255`` for ``uint8``.
- (3) Integer-like values are not rounded after applying the contrast adjustment equation
(before inverting the normalization to 0.0-1.0 space), i.e. projection from continuous
space to discrete happens according to floor function.
- (4) Note that scikit-image doc says that integers are converted to ``float64`` values before
applying the contrast normalization method. This might lead to inaccuracies for large
64bit integer values. Tests showed no indication of that happening though.
- (5) Must not contain negative values. Values >=0 are fully supported.
- (6) Leads to error in scikit-image.
- (7) Does not make sense for contrast adjustments.
Parameters
----------
arr : numpy.ndarray
Array for which to adjust the contrast. Dtype ``uint8`` is fastest.
gain : number
Multiplier for the sigmoid function's output.
Higher values lead to quicker changes from dark to light pixels.
cutoff : number
Cutoff that shifts the sigmoid function in horizontal direction.
Higher values mean that the switch from dark to light pixels happens later, i.e.
the pixels will remain darker.
Returns
-------
numpy.ndarray
Array with adjusted contrast.
"""
# int8 is also possible according to docs
# https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html#cv2.LUT , but here it seemed
# like `d` was 0 for CV_8S, causing that to fail
if arr.dtype.name == "uint8":
min_value, _center_value, max_value = iadt.get_value_range_of_dtype(arr.dtype)
dynamic_range = max_value - min_value
value_range = np.linspace(0, 1.0, num=dynamic_range+1, dtype=np.float32)
# 255 * 1/(1 + exp(gain*(cutoff - I_ij/255)))
# using np.float32(.) here still works when the input is a numpy array of size 1
gain = np.float32(gain)
cutoff = np.float32(cutoff)
table = min_value + dynamic_range * 1/(1 + np.exp(gain * (cutoff - value_range)))
arr_aug = cv2.LUT(arr, np.clip(table, min_value, max_value).astype(arr.dtype))
if arr.ndim == 3 and arr_aug.ndim == 2:
return arr_aug[..., np.newaxis]
return arr_aug
else:
return ski_exposure.adjust_sigmoid(arr, cutoff=cutoff, gain=gain) | python | def adjust_contrast_sigmoid(arr, gain, cutoff):
"""
Adjust contrast by scaling each pixel value to ``255 * 1/(1 + exp(gain*(cutoff - I_ij/255)))``.
dtype support::
* ``uint8``: yes; fully tested (1) (2) (3)
* ``uint16``: yes; tested (2) (3)
* ``uint32``: yes; tested (2) (3)
* ``uint64``: yes; tested (2) (3) (4)
* ``int8``: limited; tested (2) (3) (5)
* ``int16``: limited; tested (2) (3) (5)
* ``int32``: limited; tested (2) (3) (5)
* ``int64``: limited; tested (2) (3) (4) (5)
* ``float16``: limited; tested (5)
* ``float32``: limited; tested (5)
* ``float64``: limited; tested (5)
* ``float128``: no (6)
* ``bool``: no (7)
- (1) Handled by ``cv2``. Other dtypes are handled by ``skimage``.
- (2) Normalization is done as ``I_ij/max``, where ``max`` is the maximum value of the
dtype, e.g. 255 for ``uint8``. The normalization is reversed afterwards,
e.g. ``result*255`` for ``uint8``.
- (3) Integer-like values are not rounded after applying the contrast adjustment equation
(before inverting the normalization to 0.0-1.0 space), i.e. projection from continuous
space to discrete happens according to floor function.
- (4) Note that scikit-image doc says that integers are converted to ``float64`` values before
applying the contrast normalization method. This might lead to inaccuracies for large
64bit integer values. Tests showed no indication of that happening though.
- (5) Must not contain negative values. Values >=0 are fully supported.
- (6) Leads to error in scikit-image.
- (7) Does not make sense for contrast adjustments.
Parameters
----------
arr : numpy.ndarray
Array for which to adjust the contrast. Dtype ``uint8`` is fastest.
gain : number
Multiplier for the sigmoid function's output.
Higher values lead to quicker changes from dark to light pixels.
cutoff : number
Cutoff that shifts the sigmoid function in horizontal direction.
Higher values mean that the switch from dark to light pixels happens later, i.e.
the pixels will remain darker.
Returns
-------
numpy.ndarray
Array with adjusted contrast.
"""
# int8 is also possible according to docs
# https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html#cv2.LUT , but here it seemed
# like `d` was 0 for CV_8S, causing that to fail
if arr.dtype.name == "uint8":
min_value, _center_value, max_value = iadt.get_value_range_of_dtype(arr.dtype)
dynamic_range = max_value - min_value
value_range = np.linspace(0, 1.0, num=dynamic_range+1, dtype=np.float32)
# 255 * 1/(1 + exp(gain*(cutoff - I_ij/255)))
# using np.float32(.) here still works when the input is a numpy array of size 1
gain = np.float32(gain)
cutoff = np.float32(cutoff)
table = min_value + dynamic_range * 1/(1 + np.exp(gain * (cutoff - value_range)))
arr_aug = cv2.LUT(arr, np.clip(table, min_value, max_value).astype(arr.dtype))
if arr.ndim == 3 and arr_aug.ndim == 2:
return arr_aug[..., np.newaxis]
return arr_aug
else:
return ski_exposure.adjust_sigmoid(arr, cutoff=cutoff, gain=gain) | [
"def",
"adjust_contrast_sigmoid",
"(",
"arr",
",",
"gain",
",",
"cutoff",
")",
":",
"# int8 is also possible according to docs",
"# https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html#cv2.LUT , but here it seemed",
"# like `d` was 0 for CV_8S, causing that to fail",
"if",
"arr",
".",
"dtype",
".",
"name",
"==",
"\"uint8\"",
":",
"min_value",
",",
"_center_value",
",",
"max_value",
"=",
"iadt",
".",
"get_value_range_of_dtype",
"(",
"arr",
".",
"dtype",
")",
"dynamic_range",
"=",
"max_value",
"-",
"min_value",
"value_range",
"=",
"np",
".",
"linspace",
"(",
"0",
",",
"1.0",
",",
"num",
"=",
"dynamic_range",
"+",
"1",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
"# 255 * 1/(1 + exp(gain*(cutoff - I_ij/255)))",
"# using np.float32(.) here still works when the input is a numpy array of size 1",
"gain",
"=",
"np",
".",
"float32",
"(",
"gain",
")",
"cutoff",
"=",
"np",
".",
"float32",
"(",
"cutoff",
")",
"table",
"=",
"min_value",
"+",
"dynamic_range",
"*",
"1",
"/",
"(",
"1",
"+",
"np",
".",
"exp",
"(",
"gain",
"*",
"(",
"cutoff",
"-",
"value_range",
")",
")",
")",
"arr_aug",
"=",
"cv2",
".",
"LUT",
"(",
"arr",
",",
"np",
".",
"clip",
"(",
"table",
",",
"min_value",
",",
"max_value",
")",
".",
"astype",
"(",
"arr",
".",
"dtype",
")",
")",
"if",
"arr",
".",
"ndim",
"==",
"3",
"and",
"arr_aug",
".",
"ndim",
"==",
"2",
":",
"return",
"arr_aug",
"[",
"...",
",",
"np",
".",
"newaxis",
"]",
"return",
"arr_aug",
"else",
":",
"return",
"ski_exposure",
".",
"adjust_sigmoid",
"(",
"arr",
",",
"cutoff",
"=",
"cutoff",
",",
"gain",
"=",
"gain",
")"
] | Adjust contrast by scaling each pixel value to ``255 * 1/(1 + exp(gain*(cutoff - I_ij/255)))``.
dtype support::
* ``uint8``: yes; fully tested (1) (2) (3)
* ``uint16``: yes; tested (2) (3)
* ``uint32``: yes; tested (2) (3)
* ``uint64``: yes; tested (2) (3) (4)
* ``int8``: limited; tested (2) (3) (5)
* ``int16``: limited; tested (2) (3) (5)
* ``int32``: limited; tested (2) (3) (5)
* ``int64``: limited; tested (2) (3) (4) (5)
* ``float16``: limited; tested (5)
* ``float32``: limited; tested (5)
* ``float64``: limited; tested (5)
* ``float128``: no (6)
* ``bool``: no (7)
- (1) Handled by ``cv2``. Other dtypes are handled by ``skimage``.
- (2) Normalization is done as ``I_ij/max``, where ``max`` is the maximum value of the
dtype, e.g. 255 for ``uint8``. The normalization is reversed afterwards,
e.g. ``result*255`` for ``uint8``.
- (3) Integer-like values are not rounded after applying the contrast adjustment equation
(before inverting the normalization to 0.0-1.0 space), i.e. projection from continuous
space to discrete happens according to floor function.
- (4) Note that scikit-image doc says that integers are converted to ``float64`` values before
applying the contrast normalization method. This might lead to inaccuracies for large
64bit integer values. Tests showed no indication of that happening though.
- (5) Must not contain negative values. Values >=0 are fully supported.
- (6) Leads to error in scikit-image.
- (7) Does not make sense for contrast adjustments.
Parameters
----------
arr : numpy.ndarray
Array for which to adjust the contrast. Dtype ``uint8`` is fastest.
gain : number
Multiplier for the sigmoid function's output.
Higher values lead to quicker changes from dark to light pixels.
cutoff : number
Cutoff that shifts the sigmoid function in horizontal direction.
Higher values mean that the switch from dark to light pixels happens later, i.e.
the pixels will remain darker.
Returns
-------
numpy.ndarray
Array with adjusted contrast. | [
"Adjust",
"contrast",
"by",
"scaling",
"each",
"pixel",
"value",
"to",
"255",
"*",
"1",
"/",
"(",
"1",
"+",
"exp",
"(",
"gain",
"*",
"(",
"cutoff",
"-",
"I_ij",
"/",
"255",
")))",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmenters/contrast.py#L109-L181 | valid |
aleju/imgaug | imgaug/augmenters/contrast.py | adjust_contrast_log | def adjust_contrast_log(arr, gain):
"""
Adjust contrast by scaling each pixel value to ``255 * gain * log_2(1 + I_ij/255)``.
dtype support::
* ``uint8``: yes; fully tested (1) (2) (3)
* ``uint16``: yes; tested (2) (3)
* ``uint32``: yes; tested (2) (3)
* ``uint64``: yes; tested (2) (3) (4)
* ``int8``: limited; tested (2) (3) (5)
* ``int16``: limited; tested (2) (3) (5)
* ``int32``: limited; tested (2) (3) (5)
* ``int64``: limited; tested (2) (3) (4) (5)
* ``float16``: limited; tested (5)
* ``float32``: limited; tested (5)
* ``float64``: limited; tested (5)
* ``float128``: no (6)
* ``bool``: no (7)
- (1) Handled by ``cv2``. Other dtypes are handled by ``skimage``.
- (2) Normalization is done as ``I_ij/max``, where ``max`` is the maximum value of the
dtype, e.g. 255 for ``uint8``. The normalization is reversed afterwards,
e.g. ``result*255`` for ``uint8``.
- (3) Integer-like values are not rounded after applying the contrast adjustment equation
(before inverting the normalization to 0.0-1.0 space), i.e. projection from continuous
space to discrete happens according to floor function.
- (4) Note that scikit-image doc says that integers are converted to ``float64`` values before
applying the contrast normalization method. This might lead to inaccuracies for large
64bit integer values. Tests showed no indication of that happening though.
- (5) Must not contain negative values. Values >=0 are fully supported.
- (6) Leads to error in scikit-image.
- (7) Does not make sense for contrast adjustments.
Parameters
----------
arr : numpy.ndarray
Array for which to adjust the contrast. Dtype ``uint8`` is fastest.
gain : number
Multiplier for the logarithm result. Values around 1.0 lead to a contrast-adjusted
images. Values above 1.0 quickly lead to partially broken images due to exceeding the
datatype's value range.
Returns
-------
numpy.ndarray
Array with adjusted contrast.
"""
# int8 is also possible according to docs
# https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html#cv2.LUT , but here it seemed
# like `d` was 0 for CV_8S, causing that to fail
if arr.dtype.name == "uint8":
min_value, _center_value, max_value = iadt.get_value_range_of_dtype(arr.dtype)
dynamic_range = max_value - min_value
value_range = np.linspace(0, 1.0, num=dynamic_range+1, dtype=np.float32)
# 255 * 1/(1 + exp(gain*(cutoff - I_ij/255)))
# using np.float32(.) here still works when the input is a numpy array of size 1
gain = np.float32(gain)
table = min_value + dynamic_range * gain * np.log2(1 + value_range)
arr_aug = cv2.LUT(arr, np.clip(table, min_value, max_value).astype(arr.dtype))
if arr.ndim == 3 and arr_aug.ndim == 2:
return arr_aug[..., np.newaxis]
return arr_aug
else:
return ski_exposure.adjust_log(arr, gain=gain) | python | def adjust_contrast_log(arr, gain):
"""
Adjust contrast by scaling each pixel value to ``255 * gain * log_2(1 + I_ij/255)``.
dtype support::
* ``uint8``: yes; fully tested (1) (2) (3)
* ``uint16``: yes; tested (2) (3)
* ``uint32``: yes; tested (2) (3)
* ``uint64``: yes; tested (2) (3) (4)
* ``int8``: limited; tested (2) (3) (5)
* ``int16``: limited; tested (2) (3) (5)
* ``int32``: limited; tested (2) (3) (5)
* ``int64``: limited; tested (2) (3) (4) (5)
* ``float16``: limited; tested (5)
* ``float32``: limited; tested (5)
* ``float64``: limited; tested (5)
* ``float128``: no (6)
* ``bool``: no (7)
- (1) Handled by ``cv2``. Other dtypes are handled by ``skimage``.
- (2) Normalization is done as ``I_ij/max``, where ``max`` is the maximum value of the
dtype, e.g. 255 for ``uint8``. The normalization is reversed afterwards,
e.g. ``result*255`` for ``uint8``.
- (3) Integer-like values are not rounded after applying the contrast adjustment equation
(before inverting the normalization to 0.0-1.0 space), i.e. projection from continuous
space to discrete happens according to floor function.
- (4) Note that scikit-image doc says that integers are converted to ``float64`` values before
applying the contrast normalization method. This might lead to inaccuracies for large
64bit integer values. Tests showed no indication of that happening though.
- (5) Must not contain negative values. Values >=0 are fully supported.
- (6) Leads to error in scikit-image.
- (7) Does not make sense for contrast adjustments.
Parameters
----------
arr : numpy.ndarray
Array for which to adjust the contrast. Dtype ``uint8`` is fastest.
gain : number
Multiplier for the logarithm result. Values around 1.0 lead to a contrast-adjusted
images. Values above 1.0 quickly lead to partially broken images due to exceeding the
datatype's value range.
Returns
-------
numpy.ndarray
Array with adjusted contrast.
"""
# int8 is also possible according to docs
# https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html#cv2.LUT , but here it seemed
# like `d` was 0 for CV_8S, causing that to fail
if arr.dtype.name == "uint8":
min_value, _center_value, max_value = iadt.get_value_range_of_dtype(arr.dtype)
dynamic_range = max_value - min_value
value_range = np.linspace(0, 1.0, num=dynamic_range+1, dtype=np.float32)
# 255 * 1/(1 + exp(gain*(cutoff - I_ij/255)))
# using np.float32(.) here still works when the input is a numpy array of size 1
gain = np.float32(gain)
table = min_value + dynamic_range * gain * np.log2(1 + value_range)
arr_aug = cv2.LUT(arr, np.clip(table, min_value, max_value).astype(arr.dtype))
if arr.ndim == 3 and arr_aug.ndim == 2:
return arr_aug[..., np.newaxis]
return arr_aug
else:
return ski_exposure.adjust_log(arr, gain=gain) | [
"def",
"adjust_contrast_log",
"(",
"arr",
",",
"gain",
")",
":",
"# int8 is also possible according to docs",
"# https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html#cv2.LUT , but here it seemed",
"# like `d` was 0 for CV_8S, causing that to fail",
"if",
"arr",
".",
"dtype",
".",
"name",
"==",
"\"uint8\"",
":",
"min_value",
",",
"_center_value",
",",
"max_value",
"=",
"iadt",
".",
"get_value_range_of_dtype",
"(",
"arr",
".",
"dtype",
")",
"dynamic_range",
"=",
"max_value",
"-",
"min_value",
"value_range",
"=",
"np",
".",
"linspace",
"(",
"0",
",",
"1.0",
",",
"num",
"=",
"dynamic_range",
"+",
"1",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
"# 255 * 1/(1 + exp(gain*(cutoff - I_ij/255)))",
"# using np.float32(.) here still works when the input is a numpy array of size 1",
"gain",
"=",
"np",
".",
"float32",
"(",
"gain",
")",
"table",
"=",
"min_value",
"+",
"dynamic_range",
"*",
"gain",
"*",
"np",
".",
"log2",
"(",
"1",
"+",
"value_range",
")",
"arr_aug",
"=",
"cv2",
".",
"LUT",
"(",
"arr",
",",
"np",
".",
"clip",
"(",
"table",
",",
"min_value",
",",
"max_value",
")",
".",
"astype",
"(",
"arr",
".",
"dtype",
")",
")",
"if",
"arr",
".",
"ndim",
"==",
"3",
"and",
"arr_aug",
".",
"ndim",
"==",
"2",
":",
"return",
"arr_aug",
"[",
"...",
",",
"np",
".",
"newaxis",
"]",
"return",
"arr_aug",
"else",
":",
"return",
"ski_exposure",
".",
"adjust_log",
"(",
"arr",
",",
"gain",
"=",
"gain",
")"
] | Adjust contrast by scaling each pixel value to ``255 * gain * log_2(1 + I_ij/255)``.
dtype support::
* ``uint8``: yes; fully tested (1) (2) (3)
* ``uint16``: yes; tested (2) (3)
* ``uint32``: yes; tested (2) (3)
* ``uint64``: yes; tested (2) (3) (4)
* ``int8``: limited; tested (2) (3) (5)
* ``int16``: limited; tested (2) (3) (5)
* ``int32``: limited; tested (2) (3) (5)
* ``int64``: limited; tested (2) (3) (4) (5)
* ``float16``: limited; tested (5)
* ``float32``: limited; tested (5)
* ``float64``: limited; tested (5)
* ``float128``: no (6)
* ``bool``: no (7)
- (1) Handled by ``cv2``. Other dtypes are handled by ``skimage``.
- (2) Normalization is done as ``I_ij/max``, where ``max`` is the maximum value of the
dtype, e.g. 255 for ``uint8``. The normalization is reversed afterwards,
e.g. ``result*255`` for ``uint8``.
- (3) Integer-like values are not rounded after applying the contrast adjustment equation
(before inverting the normalization to 0.0-1.0 space), i.e. projection from continuous
space to discrete happens according to floor function.
- (4) Note that scikit-image doc says that integers are converted to ``float64`` values before
applying the contrast normalization method. This might lead to inaccuracies for large
64bit integer values. Tests showed no indication of that happening though.
- (5) Must not contain negative values. Values >=0 are fully supported.
- (6) Leads to error in scikit-image.
- (7) Does not make sense for contrast adjustments.
Parameters
----------
arr : numpy.ndarray
Array for which to adjust the contrast. Dtype ``uint8`` is fastest.
gain : number
Multiplier for the logarithm result. Values around 1.0 lead to a contrast-adjusted
images. Values above 1.0 quickly lead to partially broken images due to exceeding the
datatype's value range.
Returns
-------
numpy.ndarray
Array with adjusted contrast. | [
"Adjust",
"contrast",
"by",
"scaling",
"each",
"pixel",
"value",
"to",
"255",
"*",
"gain",
"*",
"log_2",
"(",
"1",
"+",
"I_ij",
"/",
"255",
")",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmenters/contrast.py#L185-L252 | valid |
aleju/imgaug | imgaug/augmenters/contrast.py | adjust_contrast_linear | def adjust_contrast_linear(arr, alpha):
"""Adjust contrast by scaling each pixel value to ``127 + alpha*(I_ij-127)``.
dtype support::
* ``uint8``: yes; fully tested (1) (2)
* ``uint16``: yes; tested (2)
* ``uint32``: yes; tested (2)
* ``uint64``: no (3)
* ``int8``: yes; tested (2)
* ``int16``: yes; tested (2)
* ``int32``: yes; tested (2)
* ``int64``: no (2)
* ``float16``: yes; tested (2)
* ``float32``: yes; tested (2)
* ``float64``: yes; tested (2)
* ``float128``: no (2)
* ``bool``: no (4)
- (1) Handled by ``cv2``. Other dtypes are handled by raw ``numpy``.
- (2) Only tested for reasonable alphas with up to a value of around 100.
- (3) Conversion to ``float64`` is done during augmentation, hence ``uint64``, ``int64``,
and ``float128`` support cannot be guaranteed.
- (4) Does not make sense for contrast adjustments.
Parameters
----------
arr : numpy.ndarray
Array for which to adjust the contrast. Dtype ``uint8`` is fastest.
alpha : number
Multiplier to linearly pronounce (>1.0), dampen (0.0 to 1.0) or invert (<0.0) the
difference between each pixel value and the center value, e.g. ``127`` for ``uint8``.
Returns
-------
numpy.ndarray
Array with adjusted contrast.
"""
# int8 is also possible according to docs
# https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html#cv2.LUT , but here it seemed
# like `d` was 0 for CV_8S, causing that to fail
if arr.dtype.name == "uint8":
min_value, center_value, max_value = iadt.get_value_range_of_dtype(arr.dtype)
value_range = np.arange(0, 256, dtype=np.float32)
# 127 + alpha*(I_ij-127)
# using np.float32(.) here still works when the input is a numpy array of size 1
alpha = np.float32(alpha)
table = center_value + alpha * (value_range - center_value)
arr_aug = cv2.LUT(arr, np.clip(table, min_value, max_value).astype(arr.dtype))
if arr.ndim == 3 and arr_aug.ndim == 2:
return arr_aug[..., np.newaxis]
return arr_aug
else:
input_dtype = arr.dtype
_min_value, center_value, _max_value = iadt.get_value_range_of_dtype(input_dtype)
if input_dtype.kind in ["u", "i"]:
center_value = int(center_value)
image_aug = center_value + alpha * (arr.astype(np.float64)-center_value)
image_aug = iadt.restore_dtypes_(image_aug, input_dtype)
return image_aug | python | def adjust_contrast_linear(arr, alpha):
"""Adjust contrast by scaling each pixel value to ``127 + alpha*(I_ij-127)``.
dtype support::
* ``uint8``: yes; fully tested (1) (2)
* ``uint16``: yes; tested (2)
* ``uint32``: yes; tested (2)
* ``uint64``: no (3)
* ``int8``: yes; tested (2)
* ``int16``: yes; tested (2)
* ``int32``: yes; tested (2)
* ``int64``: no (2)
* ``float16``: yes; tested (2)
* ``float32``: yes; tested (2)
* ``float64``: yes; tested (2)
* ``float128``: no (2)
* ``bool``: no (4)
- (1) Handled by ``cv2``. Other dtypes are handled by raw ``numpy``.
- (2) Only tested for reasonable alphas with up to a value of around 100.
- (3) Conversion to ``float64`` is done during augmentation, hence ``uint64``, ``int64``,
and ``float128`` support cannot be guaranteed.
- (4) Does not make sense for contrast adjustments.
Parameters
----------
arr : numpy.ndarray
Array for which to adjust the contrast. Dtype ``uint8`` is fastest.
alpha : number
Multiplier to linearly pronounce (>1.0), dampen (0.0 to 1.0) or invert (<0.0) the
difference between each pixel value and the center value, e.g. ``127`` for ``uint8``.
Returns
-------
numpy.ndarray
Array with adjusted contrast.
"""
# int8 is also possible according to docs
# https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html#cv2.LUT , but here it seemed
# like `d` was 0 for CV_8S, causing that to fail
if arr.dtype.name == "uint8":
min_value, center_value, max_value = iadt.get_value_range_of_dtype(arr.dtype)
value_range = np.arange(0, 256, dtype=np.float32)
# 127 + alpha*(I_ij-127)
# using np.float32(.) here still works when the input is a numpy array of size 1
alpha = np.float32(alpha)
table = center_value + alpha * (value_range - center_value)
arr_aug = cv2.LUT(arr, np.clip(table, min_value, max_value).astype(arr.dtype))
if arr.ndim == 3 and arr_aug.ndim == 2:
return arr_aug[..., np.newaxis]
return arr_aug
else:
input_dtype = arr.dtype
_min_value, center_value, _max_value = iadt.get_value_range_of_dtype(input_dtype)
if input_dtype.kind in ["u", "i"]:
center_value = int(center_value)
image_aug = center_value + alpha * (arr.astype(np.float64)-center_value)
image_aug = iadt.restore_dtypes_(image_aug, input_dtype)
return image_aug | [
"def",
"adjust_contrast_linear",
"(",
"arr",
",",
"alpha",
")",
":",
"# int8 is also possible according to docs",
"# https://docs.opencv.org/3.0-beta/modules/core/doc/operations_on_arrays.html#cv2.LUT , but here it seemed",
"# like `d` was 0 for CV_8S, causing that to fail",
"if",
"arr",
".",
"dtype",
".",
"name",
"==",
"\"uint8\"",
":",
"min_value",
",",
"center_value",
",",
"max_value",
"=",
"iadt",
".",
"get_value_range_of_dtype",
"(",
"arr",
".",
"dtype",
")",
"value_range",
"=",
"np",
".",
"arange",
"(",
"0",
",",
"256",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
"# 127 + alpha*(I_ij-127)",
"# using np.float32(.) here still works when the input is a numpy array of size 1",
"alpha",
"=",
"np",
".",
"float32",
"(",
"alpha",
")",
"table",
"=",
"center_value",
"+",
"alpha",
"*",
"(",
"value_range",
"-",
"center_value",
")",
"arr_aug",
"=",
"cv2",
".",
"LUT",
"(",
"arr",
",",
"np",
".",
"clip",
"(",
"table",
",",
"min_value",
",",
"max_value",
")",
".",
"astype",
"(",
"arr",
".",
"dtype",
")",
")",
"if",
"arr",
".",
"ndim",
"==",
"3",
"and",
"arr_aug",
".",
"ndim",
"==",
"2",
":",
"return",
"arr_aug",
"[",
"...",
",",
"np",
".",
"newaxis",
"]",
"return",
"arr_aug",
"else",
":",
"input_dtype",
"=",
"arr",
".",
"dtype",
"_min_value",
",",
"center_value",
",",
"_max_value",
"=",
"iadt",
".",
"get_value_range_of_dtype",
"(",
"input_dtype",
")",
"if",
"input_dtype",
".",
"kind",
"in",
"[",
"\"u\"",
",",
"\"i\"",
"]",
":",
"center_value",
"=",
"int",
"(",
"center_value",
")",
"image_aug",
"=",
"center_value",
"+",
"alpha",
"*",
"(",
"arr",
".",
"astype",
"(",
"np",
".",
"float64",
")",
"-",
"center_value",
")",
"image_aug",
"=",
"iadt",
".",
"restore_dtypes_",
"(",
"image_aug",
",",
"input_dtype",
")",
"return",
"image_aug"
] | Adjust contrast by scaling each pixel value to ``127 + alpha*(I_ij-127)``.
dtype support::
* ``uint8``: yes; fully tested (1) (2)
* ``uint16``: yes; tested (2)
* ``uint32``: yes; tested (2)
* ``uint64``: no (3)
* ``int8``: yes; tested (2)
* ``int16``: yes; tested (2)
* ``int32``: yes; tested (2)
* ``int64``: no (2)
* ``float16``: yes; tested (2)
* ``float32``: yes; tested (2)
* ``float64``: yes; tested (2)
* ``float128``: no (2)
* ``bool``: no (4)
- (1) Handled by ``cv2``. Other dtypes are handled by raw ``numpy``.
- (2) Only tested for reasonable alphas with up to a value of around 100.
- (3) Conversion to ``float64`` is done during augmentation, hence ``uint64``, ``int64``,
and ``float128`` support cannot be guaranteed.
- (4) Does not make sense for contrast adjustments.
Parameters
----------
arr : numpy.ndarray
Array for which to adjust the contrast. Dtype ``uint8`` is fastest.
alpha : number
Multiplier to linearly pronounce (>1.0), dampen (0.0 to 1.0) or invert (<0.0) the
difference between each pixel value and the center value, e.g. ``127`` for ``uint8``.
Returns
-------
numpy.ndarray
Array with adjusted contrast. | [
"Adjust",
"contrast",
"by",
"scaling",
"each",
"pixel",
"value",
"to",
"127",
"+",
"alpha",
"*",
"(",
"I_ij",
"-",
"127",
")",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmenters/contrast.py#L256-L318 | valid |
aleju/imgaug | imgaug/augmenters/contrast.py | GammaContrast | def GammaContrast(gamma=1, per_channel=False, name=None, deterministic=False, random_state=None):
"""
Adjust contrast by scaling each pixel value to ``255 * ((I_ij/255)**gamma)``.
Values in the range ``gamma=(0.5, 2.0)`` seem to be sensible.
dtype support::
See :func:`imgaug.augmenters.contrast.adjust_contrast_gamma`.
Parameters
----------
gamma : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Exponent for the contrast adjustment. Higher values darken the image.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
per_channel : bool or float, optional
Whether to use the same value for all channels (False) or to sample a new value for each
channel (True). If this value is a float ``p``, then for ``p`` percent of all images `per_channel`
will be treated as True, otherwise as False.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Returns
-------
_ContrastFuncWrapper
Augmenter to perform gamma contrast adjustment.
"""
params1d = [iap.handle_continuous_param(gamma, "gamma", value_range=None, tuple_to_uniform=True,
list_to_choice=True)]
func = adjust_contrast_gamma
return _ContrastFuncWrapper(
func, params1d, per_channel,
dtypes_allowed=["uint8", "uint16", "uint32", "uint64",
"int8", "int16", "int32", "int64",
"float16", "float32", "float64"],
dtypes_disallowed=["float96", "float128", "float256", "bool"],
name=name if name is not None else ia.caller_name(),
deterministic=deterministic,
random_state=random_state
) | python | def GammaContrast(gamma=1, per_channel=False, name=None, deterministic=False, random_state=None):
"""
Adjust contrast by scaling each pixel value to ``255 * ((I_ij/255)**gamma)``.
Values in the range ``gamma=(0.5, 2.0)`` seem to be sensible.
dtype support::
See :func:`imgaug.augmenters.contrast.adjust_contrast_gamma`.
Parameters
----------
gamma : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Exponent for the contrast adjustment. Higher values darken the image.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
per_channel : bool or float, optional
Whether to use the same value for all channels (False) or to sample a new value for each
channel (True). If this value is a float ``p``, then for ``p`` percent of all images `per_channel`
will be treated as True, otherwise as False.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Returns
-------
_ContrastFuncWrapper
Augmenter to perform gamma contrast adjustment.
"""
params1d = [iap.handle_continuous_param(gamma, "gamma", value_range=None, tuple_to_uniform=True,
list_to_choice=True)]
func = adjust_contrast_gamma
return _ContrastFuncWrapper(
func, params1d, per_channel,
dtypes_allowed=["uint8", "uint16", "uint32", "uint64",
"int8", "int16", "int32", "int64",
"float16", "float32", "float64"],
dtypes_disallowed=["float96", "float128", "float256", "bool"],
name=name if name is not None else ia.caller_name(),
deterministic=deterministic,
random_state=random_state
) | [
"def",
"GammaContrast",
"(",
"gamma",
"=",
"1",
",",
"per_channel",
"=",
"False",
",",
"name",
"=",
"None",
",",
"deterministic",
"=",
"False",
",",
"random_state",
"=",
"None",
")",
":",
"params1d",
"=",
"[",
"iap",
".",
"handle_continuous_param",
"(",
"gamma",
",",
"\"gamma\"",
",",
"value_range",
"=",
"None",
",",
"tuple_to_uniform",
"=",
"True",
",",
"list_to_choice",
"=",
"True",
")",
"]",
"func",
"=",
"adjust_contrast_gamma",
"return",
"_ContrastFuncWrapper",
"(",
"func",
",",
"params1d",
",",
"per_channel",
",",
"dtypes_allowed",
"=",
"[",
"\"uint8\"",
",",
"\"uint16\"",
",",
"\"uint32\"",
",",
"\"uint64\"",
",",
"\"int8\"",
",",
"\"int16\"",
",",
"\"int32\"",
",",
"\"int64\"",
",",
"\"float16\"",
",",
"\"float32\"",
",",
"\"float64\"",
"]",
",",
"dtypes_disallowed",
"=",
"[",
"\"float96\"",
",",
"\"float128\"",
",",
"\"float256\"",
",",
"\"bool\"",
"]",
",",
"name",
"=",
"name",
"if",
"name",
"is",
"not",
"None",
"else",
"ia",
".",
"caller_name",
"(",
")",
",",
"deterministic",
"=",
"deterministic",
",",
"random_state",
"=",
"random_state",
")"
] | Adjust contrast by scaling each pixel value to ``255 * ((I_ij/255)**gamma)``.
Values in the range ``gamma=(0.5, 2.0)`` seem to be sensible.
dtype support::
See :func:`imgaug.augmenters.contrast.adjust_contrast_gamma`.
Parameters
----------
gamma : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Exponent for the contrast adjustment. Higher values darken the image.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
per_channel : bool or float, optional
Whether to use the same value for all channels (False) or to sample a new value for each
channel (True). If this value is a float ``p``, then for ``p`` percent of all images `per_channel`
will be treated as True, otherwise as False.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Returns
-------
_ContrastFuncWrapper
Augmenter to perform gamma contrast adjustment. | [
"Adjust",
"contrast",
"by",
"scaling",
"each",
"pixel",
"value",
"to",
"255",
"*",
"((",
"I_ij",
"/",
"255",
")",
"**",
"gamma",
")",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmenters/contrast.py#L321-L373 | valid |
aleju/imgaug | imgaug/augmenters/contrast.py | SigmoidContrast | def SigmoidContrast(gain=10, cutoff=0.5, per_channel=False, name=None, deterministic=False, random_state=None):
"""
Adjust contrast by scaling each pixel value to ``255 * 1/(1 + exp(gain*(cutoff - I_ij/255)))``.
Values in the range ``gain=(5, 20)`` and ``cutoff=(0.25, 0.75)`` seem to be sensible.
dtype support::
See :func:`imgaug.augmenters.contrast.adjust_contrast_sigmoid`.
Parameters
----------
gain : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Multiplier for the sigmoid function's output.
Higher values lead to quicker changes from dark to light pixels.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
cutoff : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Cutoff that shifts the sigmoid function in horizontal direction.
Higher values mean that the switch from dark to light pixels happens later, i.e.
the pixels will remain darker.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
per_channel : bool or float, optional
Whether to use the same value for all channels (False) or to sample a new value for each
channel (True). If this value is a float ``p``, then for ``p`` percent of all images `per_channel`
will be treated as True, otherwise as False.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Returns
-------
_ContrastFuncWrapper
Augmenter to perform sigmoid contrast adjustment.
"""
# TODO add inv parameter?
params1d = [
iap.handle_continuous_param(gain, "gain", value_range=(0, None), tuple_to_uniform=True, list_to_choice=True),
iap.handle_continuous_param(cutoff, "cutoff", value_range=(0, 1.0), tuple_to_uniform=True, list_to_choice=True)
]
func = adjust_contrast_sigmoid
return _ContrastFuncWrapper(
func, params1d, per_channel,
dtypes_allowed=["uint8", "uint16", "uint32", "uint64",
"int8", "int16", "int32", "int64",
"float16", "float32", "float64"],
dtypes_disallowed=["float96", "float128", "float256", "bool"],
name=name if name is not None else ia.caller_name(),
deterministic=deterministic,
random_state=random_state
) | python | def SigmoidContrast(gain=10, cutoff=0.5, per_channel=False, name=None, deterministic=False, random_state=None):
"""
Adjust contrast by scaling each pixel value to ``255 * 1/(1 + exp(gain*(cutoff - I_ij/255)))``.
Values in the range ``gain=(5, 20)`` and ``cutoff=(0.25, 0.75)`` seem to be sensible.
dtype support::
See :func:`imgaug.augmenters.contrast.adjust_contrast_sigmoid`.
Parameters
----------
gain : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Multiplier for the sigmoid function's output.
Higher values lead to quicker changes from dark to light pixels.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
cutoff : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Cutoff that shifts the sigmoid function in horizontal direction.
Higher values mean that the switch from dark to light pixels happens later, i.e.
the pixels will remain darker.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
per_channel : bool or float, optional
Whether to use the same value for all channels (False) or to sample a new value for each
channel (True). If this value is a float ``p``, then for ``p`` percent of all images `per_channel`
will be treated as True, otherwise as False.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Returns
-------
_ContrastFuncWrapper
Augmenter to perform sigmoid contrast adjustment.
"""
# TODO add inv parameter?
params1d = [
iap.handle_continuous_param(gain, "gain", value_range=(0, None), tuple_to_uniform=True, list_to_choice=True),
iap.handle_continuous_param(cutoff, "cutoff", value_range=(0, 1.0), tuple_to_uniform=True, list_to_choice=True)
]
func = adjust_contrast_sigmoid
return _ContrastFuncWrapper(
func, params1d, per_channel,
dtypes_allowed=["uint8", "uint16", "uint32", "uint64",
"int8", "int16", "int32", "int64",
"float16", "float32", "float64"],
dtypes_disallowed=["float96", "float128", "float256", "bool"],
name=name if name is not None else ia.caller_name(),
deterministic=deterministic,
random_state=random_state
) | [
"def",
"SigmoidContrast",
"(",
"gain",
"=",
"10",
",",
"cutoff",
"=",
"0.5",
",",
"per_channel",
"=",
"False",
",",
"name",
"=",
"None",
",",
"deterministic",
"=",
"False",
",",
"random_state",
"=",
"None",
")",
":",
"# TODO add inv parameter?",
"params1d",
"=",
"[",
"iap",
".",
"handle_continuous_param",
"(",
"gain",
",",
"\"gain\"",
",",
"value_range",
"=",
"(",
"0",
",",
"None",
")",
",",
"tuple_to_uniform",
"=",
"True",
",",
"list_to_choice",
"=",
"True",
")",
",",
"iap",
".",
"handle_continuous_param",
"(",
"cutoff",
",",
"\"cutoff\"",
",",
"value_range",
"=",
"(",
"0",
",",
"1.0",
")",
",",
"tuple_to_uniform",
"=",
"True",
",",
"list_to_choice",
"=",
"True",
")",
"]",
"func",
"=",
"adjust_contrast_sigmoid",
"return",
"_ContrastFuncWrapper",
"(",
"func",
",",
"params1d",
",",
"per_channel",
",",
"dtypes_allowed",
"=",
"[",
"\"uint8\"",
",",
"\"uint16\"",
",",
"\"uint32\"",
",",
"\"uint64\"",
",",
"\"int8\"",
",",
"\"int16\"",
",",
"\"int32\"",
",",
"\"int64\"",
",",
"\"float16\"",
",",
"\"float32\"",
",",
"\"float64\"",
"]",
",",
"dtypes_disallowed",
"=",
"[",
"\"float96\"",
",",
"\"float128\"",
",",
"\"float256\"",
",",
"\"bool\"",
"]",
",",
"name",
"=",
"name",
"if",
"name",
"is",
"not",
"None",
"else",
"ia",
".",
"caller_name",
"(",
")",
",",
"deterministic",
"=",
"deterministic",
",",
"random_state",
"=",
"random_state",
")"
] | Adjust contrast by scaling each pixel value to ``255 * 1/(1 + exp(gain*(cutoff - I_ij/255)))``.
Values in the range ``gain=(5, 20)`` and ``cutoff=(0.25, 0.75)`` seem to be sensible.
dtype support::
See :func:`imgaug.augmenters.contrast.adjust_contrast_sigmoid`.
Parameters
----------
gain : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Multiplier for the sigmoid function's output.
Higher values lead to quicker changes from dark to light pixels.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
cutoff : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Cutoff that shifts the sigmoid function in horizontal direction.
Higher values mean that the switch from dark to light pixels happens later, i.e.
the pixels will remain darker.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
per_channel : bool or float, optional
Whether to use the same value for all channels (False) or to sample a new value for each
channel (True). If this value is a float ``p``, then for ``p`` percent of all images `per_channel`
will be treated as True, otherwise as False.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Returns
-------
_ContrastFuncWrapper
Augmenter to perform sigmoid contrast adjustment. | [
"Adjust",
"contrast",
"by",
"scaling",
"each",
"pixel",
"value",
"to",
"255",
"*",
"1",
"/",
"(",
"1",
"+",
"exp",
"(",
"gain",
"*",
"(",
"cutoff",
"-",
"I_ij",
"/",
"255",
")))",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmenters/contrast.py#L376-L442 | valid |
aleju/imgaug | imgaug/augmenters/contrast.py | LogContrast | def LogContrast(gain=1, per_channel=False, name=None, deterministic=False, random_state=None):
"""
Adjust contrast by scaling each pixel value to ``255 * gain * log_2(1 + I_ij/255)``.
dtype support::
See :func:`imgaug.augmenters.contrast.adjust_contrast_log`.
Parameters
----------
gain : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Multiplier for the logarithm result. Values around 1.0 lead to a contrast-adjusted
images. Values above 1.0 quickly lead to partially broken images due to exceeding the
datatype's value range.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
per_channel : bool or float, optional
Whether to use the same value for all channels (False) or to sample a new value for each
channel (True). If this value is a float ``p``, then for ``p`` percent of all images `per_channel`
will be treated as True, otherwise as False.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Returns
-------
_ContrastFuncWrapper
Augmenter to perform logarithmic contrast adjustment.
"""
# TODO add inv parameter?
params1d = [iap.handle_continuous_param(gain, "gain", value_range=(0, None), tuple_to_uniform=True,
list_to_choice=True)]
func = adjust_contrast_log
return _ContrastFuncWrapper(
func, params1d, per_channel,
dtypes_allowed=["uint8", "uint16", "uint32", "uint64",
"int8", "int16", "int32", "int64",
"float16", "float32", "float64"],
dtypes_disallowed=["float96", "float128", "float256", "bool"],
name=name if name is not None else ia.caller_name(),
deterministic=deterministic,
random_state=random_state
) | python | def LogContrast(gain=1, per_channel=False, name=None, deterministic=False, random_state=None):
"""
Adjust contrast by scaling each pixel value to ``255 * gain * log_2(1 + I_ij/255)``.
dtype support::
See :func:`imgaug.augmenters.contrast.adjust_contrast_log`.
Parameters
----------
gain : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Multiplier for the logarithm result. Values around 1.0 lead to a contrast-adjusted
images. Values above 1.0 quickly lead to partially broken images due to exceeding the
datatype's value range.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
per_channel : bool or float, optional
Whether to use the same value for all channels (False) or to sample a new value for each
channel (True). If this value is a float ``p``, then for ``p`` percent of all images `per_channel`
will be treated as True, otherwise as False.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Returns
-------
_ContrastFuncWrapper
Augmenter to perform logarithmic contrast adjustment.
"""
# TODO add inv parameter?
params1d = [iap.handle_continuous_param(gain, "gain", value_range=(0, None), tuple_to_uniform=True,
list_to_choice=True)]
func = adjust_contrast_log
return _ContrastFuncWrapper(
func, params1d, per_channel,
dtypes_allowed=["uint8", "uint16", "uint32", "uint64",
"int8", "int16", "int32", "int64",
"float16", "float32", "float64"],
dtypes_disallowed=["float96", "float128", "float256", "bool"],
name=name if name is not None else ia.caller_name(),
deterministic=deterministic,
random_state=random_state
) | [
"def",
"LogContrast",
"(",
"gain",
"=",
"1",
",",
"per_channel",
"=",
"False",
",",
"name",
"=",
"None",
",",
"deterministic",
"=",
"False",
",",
"random_state",
"=",
"None",
")",
":",
"# TODO add inv parameter?",
"params1d",
"=",
"[",
"iap",
".",
"handle_continuous_param",
"(",
"gain",
",",
"\"gain\"",
",",
"value_range",
"=",
"(",
"0",
",",
"None",
")",
",",
"tuple_to_uniform",
"=",
"True",
",",
"list_to_choice",
"=",
"True",
")",
"]",
"func",
"=",
"adjust_contrast_log",
"return",
"_ContrastFuncWrapper",
"(",
"func",
",",
"params1d",
",",
"per_channel",
",",
"dtypes_allowed",
"=",
"[",
"\"uint8\"",
",",
"\"uint16\"",
",",
"\"uint32\"",
",",
"\"uint64\"",
",",
"\"int8\"",
",",
"\"int16\"",
",",
"\"int32\"",
",",
"\"int64\"",
",",
"\"float16\"",
",",
"\"float32\"",
",",
"\"float64\"",
"]",
",",
"dtypes_disallowed",
"=",
"[",
"\"float96\"",
",",
"\"float128\"",
",",
"\"float256\"",
",",
"\"bool\"",
"]",
",",
"name",
"=",
"name",
"if",
"name",
"is",
"not",
"None",
"else",
"ia",
".",
"caller_name",
"(",
")",
",",
"deterministic",
"=",
"deterministic",
",",
"random_state",
"=",
"random_state",
")"
] | Adjust contrast by scaling each pixel value to ``255 * gain * log_2(1 + I_ij/255)``.
dtype support::
See :func:`imgaug.augmenters.contrast.adjust_contrast_log`.
Parameters
----------
gain : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Multiplier for the logarithm result. Values around 1.0 lead to a contrast-adjusted
images. Values above 1.0 quickly lead to partially broken images due to exceeding the
datatype's value range.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
per_channel : bool or float, optional
Whether to use the same value for all channels (False) or to sample a new value for each
channel (True). If this value is a float ``p``, then for ``p`` percent of all images `per_channel`
will be treated as True, otherwise as False.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Returns
-------
_ContrastFuncWrapper
Augmenter to perform logarithmic contrast adjustment. | [
"Adjust",
"contrast",
"by",
"scaling",
"each",
"pixel",
"value",
"to",
"255",
"*",
"gain",
"*",
"log_2",
"(",
"1",
"+",
"I_ij",
"/",
"255",
")",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmenters/contrast.py#L445-L498 | valid |
aleju/imgaug | imgaug/augmenters/contrast.py | LinearContrast | def LinearContrast(alpha=1, per_channel=False, name=None, deterministic=False, random_state=None):
"""Adjust contrast by scaling each pixel value to ``127 + alpha*(I_ij-127)``.
dtype support::
See :func:`imgaug.augmenters.contrast.adjust_contrast_linear`.
Parameters
----------
alpha : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Multiplier to linearly pronounce (>1.0), dampen (0.0 to 1.0) or invert (<0.0) the
difference between each pixel value and the center value, e.g. ``127`` for ``uint8``.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
per_channel : bool or float, optional
Whether to use the same value for all channels (False) or to sample a new value for each
channel (True). If this value is a float ``p``, then for ``p`` percent of all images `per_channel`
will be treated as True, otherwise as False.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Returns
-------
_ContrastFuncWrapper
Augmenter to perform contrast adjustment by linearly scaling the distance to 128.
"""
params1d = [
iap.handle_continuous_param(alpha, "alpha", value_range=None, tuple_to_uniform=True, list_to_choice=True)
]
func = adjust_contrast_linear
return _ContrastFuncWrapper(
func, params1d, per_channel,
dtypes_allowed=["uint8", "uint16", "uint32",
"int8", "int16", "int32",
"float16", "float32", "float64"],
dtypes_disallowed=["uint64", "int64", "float96", "float128", "float256", "bool"],
name=name if name is not None else ia.caller_name(),
deterministic=deterministic,
random_state=random_state
) | python | def LinearContrast(alpha=1, per_channel=False, name=None, deterministic=False, random_state=None):
"""Adjust contrast by scaling each pixel value to ``127 + alpha*(I_ij-127)``.
dtype support::
See :func:`imgaug.augmenters.contrast.adjust_contrast_linear`.
Parameters
----------
alpha : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Multiplier to linearly pronounce (>1.0), dampen (0.0 to 1.0) or invert (<0.0) the
difference between each pixel value and the center value, e.g. ``127`` for ``uint8``.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
per_channel : bool or float, optional
Whether to use the same value for all channels (False) or to sample a new value for each
channel (True). If this value is a float ``p``, then for ``p`` percent of all images `per_channel`
will be treated as True, otherwise as False.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Returns
-------
_ContrastFuncWrapper
Augmenter to perform contrast adjustment by linearly scaling the distance to 128.
"""
params1d = [
iap.handle_continuous_param(alpha, "alpha", value_range=None, tuple_to_uniform=True, list_to_choice=True)
]
func = adjust_contrast_linear
return _ContrastFuncWrapper(
func, params1d, per_channel,
dtypes_allowed=["uint8", "uint16", "uint32",
"int8", "int16", "int32",
"float16", "float32", "float64"],
dtypes_disallowed=["uint64", "int64", "float96", "float128", "float256", "bool"],
name=name if name is not None else ia.caller_name(),
deterministic=deterministic,
random_state=random_state
) | [
"def",
"LinearContrast",
"(",
"alpha",
"=",
"1",
",",
"per_channel",
"=",
"False",
",",
"name",
"=",
"None",
",",
"deterministic",
"=",
"False",
",",
"random_state",
"=",
"None",
")",
":",
"params1d",
"=",
"[",
"iap",
".",
"handle_continuous_param",
"(",
"alpha",
",",
"\"alpha\"",
",",
"value_range",
"=",
"None",
",",
"tuple_to_uniform",
"=",
"True",
",",
"list_to_choice",
"=",
"True",
")",
"]",
"func",
"=",
"adjust_contrast_linear",
"return",
"_ContrastFuncWrapper",
"(",
"func",
",",
"params1d",
",",
"per_channel",
",",
"dtypes_allowed",
"=",
"[",
"\"uint8\"",
",",
"\"uint16\"",
",",
"\"uint32\"",
",",
"\"int8\"",
",",
"\"int16\"",
",",
"\"int32\"",
",",
"\"float16\"",
",",
"\"float32\"",
",",
"\"float64\"",
"]",
",",
"dtypes_disallowed",
"=",
"[",
"\"uint64\"",
",",
"\"int64\"",
",",
"\"float96\"",
",",
"\"float128\"",
",",
"\"float256\"",
",",
"\"bool\"",
"]",
",",
"name",
"=",
"name",
"if",
"name",
"is",
"not",
"None",
"else",
"ia",
".",
"caller_name",
"(",
")",
",",
"deterministic",
"=",
"deterministic",
",",
"random_state",
"=",
"random_state",
")"
] | Adjust contrast by scaling each pixel value to ``127 + alpha*(I_ij-127)``.
dtype support::
See :func:`imgaug.augmenters.contrast.adjust_contrast_linear`.
Parameters
----------
alpha : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
Multiplier to linearly pronounce (>1.0), dampen (0.0 to 1.0) or invert (<0.0) the
difference between each pixel value and the center value, e.g. ``127`` for ``uint8``.
* If a number, then that value will be used for all images.
* If a tuple ``(a, b)``, then a value from the range ``[a, b]`` will be used per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, then a value will be sampled per image from that parameter.
per_channel : bool or float, optional
Whether to use the same value for all channels (False) or to sample a new value for each
channel (True). If this value is a float ``p``, then for ``p`` percent of all images `per_channel`
will be treated as True, otherwise as False.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Returns
-------
_ContrastFuncWrapper
Augmenter to perform contrast adjustment by linearly scaling the distance to 128. | [
"Adjust",
"contrast",
"by",
"scaling",
"each",
"pixel",
"value",
"to",
"127",
"+",
"alpha",
"*",
"(",
"I_ij",
"-",
"127",
")",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmenters/contrast.py#L501-L552 | valid |
aleju/imgaug | imgaug/augmenters/color.py | InColorspace | def InColorspace(to_colorspace, from_colorspace="RGB", children=None, name=None, deterministic=False,
random_state=None):
"""Convert images to another colorspace."""
return WithColorspace(to_colorspace, from_colorspace, children, name, deterministic, random_state) | python | def InColorspace(to_colorspace, from_colorspace="RGB", children=None, name=None, deterministic=False,
random_state=None):
"""Convert images to another colorspace."""
return WithColorspace(to_colorspace, from_colorspace, children, name, deterministic, random_state) | [
"def",
"InColorspace",
"(",
"to_colorspace",
",",
"from_colorspace",
"=",
"\"RGB\"",
",",
"children",
"=",
"None",
",",
"name",
"=",
"None",
",",
"deterministic",
"=",
"False",
",",
"random_state",
"=",
"None",
")",
":",
"return",
"WithColorspace",
"(",
"to_colorspace",
",",
"from_colorspace",
",",
"children",
",",
"name",
",",
"deterministic",
",",
"random_state",
")"
] | Convert images to another colorspace. | [
"Convert",
"images",
"to",
"another",
"colorspace",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmenters/color.py#L39-L42 | valid |
aleju/imgaug | imgaug/augmenters/color.py | Grayscale | def Grayscale(alpha=0, from_colorspace="RGB", name=None, deterministic=False, random_state=None):
"""
Augmenter to convert images to their grayscale versions.
NOTE: Number of output channels is still 3, i.e. this augmenter just "removes" color.
TODO check dtype support
dtype support::
* ``uint8``: yes; fully tested
* ``uint16``: ?
* ``uint32``: ?
* ``uint64``: ?
* ``int8``: ?
* ``int16``: ?
* ``int32``: ?
* ``int64``: ?
* ``float16``: ?
* ``float32``: ?
* ``float64``: ?
* ``float128``: ?
* ``bool``: ?
Parameters
----------
alpha : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
The alpha value of the grayscale image when overlayed over the
old image. A value close to 1.0 means, that mostly the new grayscale
image is visible. A value close to 0.0 means, that mostly the
old image is visible.
* If a number, exactly that value will always be used.
* If a tuple ``(a, b)``, a random value from the range ``a <= x <= b`` will
be sampled per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, a value will be sampled from the
parameter per image.
from_colorspace : str, optional
The source colorspace (of the input images).
Allowed strings are: ``RGB``, ``BGR``, ``GRAY``, ``CIE``, ``YCrCb``, ``HSV``, ``HLS``, ``Lab``, ``Luv``.
See :func:`imgaug.augmenters.color.ChangeColorspace.__init__`.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Examples
--------
>>> aug = iaa.Grayscale(alpha=1.0)
creates an augmenter that turns images to their grayscale versions.
>>> aug = iaa.Grayscale(alpha=(0.0, 1.0))
creates an augmenter that turns images to their grayscale versions with
an alpha value in the range ``0 <= alpha <= 1``. An alpha value of 0.5 would
mean, that the output image is 50 percent of the input image and 50
percent of the grayscale image (i.e. 50 percent of color removed).
"""
if name is None:
name = "Unnamed%s" % (ia.caller_name(),)
return ChangeColorspace(to_colorspace=ChangeColorspace.GRAY, alpha=alpha, from_colorspace=from_colorspace,
name=name, deterministic=deterministic, random_state=random_state) | python | def Grayscale(alpha=0, from_colorspace="RGB", name=None, deterministic=False, random_state=None):
"""
Augmenter to convert images to their grayscale versions.
NOTE: Number of output channels is still 3, i.e. this augmenter just "removes" color.
TODO check dtype support
dtype support::
* ``uint8``: yes; fully tested
* ``uint16``: ?
* ``uint32``: ?
* ``uint64``: ?
* ``int8``: ?
* ``int16``: ?
* ``int32``: ?
* ``int64``: ?
* ``float16``: ?
* ``float32``: ?
* ``float64``: ?
* ``float128``: ?
* ``bool``: ?
Parameters
----------
alpha : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
The alpha value of the grayscale image when overlayed over the
old image. A value close to 1.0 means, that mostly the new grayscale
image is visible. A value close to 0.0 means, that mostly the
old image is visible.
* If a number, exactly that value will always be used.
* If a tuple ``(a, b)``, a random value from the range ``a <= x <= b`` will
be sampled per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, a value will be sampled from the
parameter per image.
from_colorspace : str, optional
The source colorspace (of the input images).
Allowed strings are: ``RGB``, ``BGR``, ``GRAY``, ``CIE``, ``YCrCb``, ``HSV``, ``HLS``, ``Lab``, ``Luv``.
See :func:`imgaug.augmenters.color.ChangeColorspace.__init__`.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Examples
--------
>>> aug = iaa.Grayscale(alpha=1.0)
creates an augmenter that turns images to their grayscale versions.
>>> aug = iaa.Grayscale(alpha=(0.0, 1.0))
creates an augmenter that turns images to their grayscale versions with
an alpha value in the range ``0 <= alpha <= 1``. An alpha value of 0.5 would
mean, that the output image is 50 percent of the input image and 50
percent of the grayscale image (i.e. 50 percent of color removed).
"""
if name is None:
name = "Unnamed%s" % (ia.caller_name(),)
return ChangeColorspace(to_colorspace=ChangeColorspace.GRAY, alpha=alpha, from_colorspace=from_colorspace,
name=name, deterministic=deterministic, random_state=random_state) | [
"def",
"Grayscale",
"(",
"alpha",
"=",
"0",
",",
"from_colorspace",
"=",
"\"RGB\"",
",",
"name",
"=",
"None",
",",
"deterministic",
"=",
"False",
",",
"random_state",
"=",
"None",
")",
":",
"if",
"name",
"is",
"None",
":",
"name",
"=",
"\"Unnamed%s\"",
"%",
"(",
"ia",
".",
"caller_name",
"(",
")",
",",
")",
"return",
"ChangeColorspace",
"(",
"to_colorspace",
"=",
"ChangeColorspace",
".",
"GRAY",
",",
"alpha",
"=",
"alpha",
",",
"from_colorspace",
"=",
"from_colorspace",
",",
"name",
"=",
"name",
",",
"deterministic",
"=",
"deterministic",
",",
"random_state",
"=",
"random_state",
")"
] | Augmenter to convert images to their grayscale versions.
NOTE: Number of output channels is still 3, i.e. this augmenter just "removes" color.
TODO check dtype support
dtype support::
* ``uint8``: yes; fully tested
* ``uint16``: ?
* ``uint32``: ?
* ``uint64``: ?
* ``int8``: ?
* ``int16``: ?
* ``int32``: ?
* ``int64``: ?
* ``float16``: ?
* ``float32``: ?
* ``float64``: ?
* ``float128``: ?
* ``bool``: ?
Parameters
----------
alpha : number or tuple of number or list of number or imgaug.parameters.StochasticParameter, optional
The alpha value of the grayscale image when overlayed over the
old image. A value close to 1.0 means, that mostly the new grayscale
image is visible. A value close to 0.0 means, that mostly the
old image is visible.
* If a number, exactly that value will always be used.
* If a tuple ``(a, b)``, a random value from the range ``a <= x <= b`` will
be sampled per image.
* If a list, then a random value will be sampled from that list per image.
* If a StochasticParameter, a value will be sampled from the
parameter per image.
from_colorspace : str, optional
The source colorspace (of the input images).
Allowed strings are: ``RGB``, ``BGR``, ``GRAY``, ``CIE``, ``YCrCb``, ``HSV``, ``HLS``, ``Lab``, ``Luv``.
See :func:`imgaug.augmenters.color.ChangeColorspace.__init__`.
name : None or str, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
deterministic : bool, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
random_state : None or int or numpy.random.RandomState, optional
See :func:`imgaug.augmenters.meta.Augmenter.__init__`.
Examples
--------
>>> aug = iaa.Grayscale(alpha=1.0)
creates an augmenter that turns images to their grayscale versions.
>>> aug = iaa.Grayscale(alpha=(0.0, 1.0))
creates an augmenter that turns images to their grayscale versions with
an alpha value in the range ``0 <= alpha <= 1``. An alpha value of 0.5 would
mean, that the output image is 50 percent of the input image and 50
percent of the grayscale image (i.e. 50 percent of color removed). | [
"Augmenter",
"to",
"convert",
"images",
"to",
"their",
"grayscale",
"versions",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmenters/color.py#L596-L667 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.height | def height(self):
"""Get the height of a bounding box encapsulating the line."""
if len(self.coords) <= 1:
return 0
return np.max(self.yy) - np.min(self.yy) | python | def height(self):
"""Get the height of a bounding box encapsulating the line."""
if len(self.coords) <= 1:
return 0
return np.max(self.yy) - np.min(self.yy) | [
"def",
"height",
"(",
"self",
")",
":",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"<=",
"1",
":",
"return",
"0",
"return",
"np",
".",
"max",
"(",
"self",
".",
"yy",
")",
"-",
"np",
".",
"min",
"(",
"self",
".",
"yy",
")"
] | Get the height of a bounding box encapsulating the line. | [
"Get",
"the",
"height",
"of",
"a",
"bounding",
"box",
"encapsulating",
"the",
"line",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L100-L104 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.width | def width(self):
"""Get the width of a bounding box encapsulating the line."""
if len(self.coords) <= 1:
return 0
return np.max(self.xx) - np.min(self.xx) | python | def width(self):
"""Get the width of a bounding box encapsulating the line."""
if len(self.coords) <= 1:
return 0
return np.max(self.xx) - np.min(self.xx) | [
"def",
"width",
"(",
"self",
")",
":",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"<=",
"1",
":",
"return",
"0",
"return",
"np",
".",
"max",
"(",
"self",
".",
"xx",
")",
"-",
"np",
".",
"min",
"(",
"self",
".",
"xx",
")"
] | Get the width of a bounding box encapsulating the line. | [
"Get",
"the",
"width",
"of",
"a",
"bounding",
"box",
"encapsulating",
"the",
"line",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L107-L111 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.get_pointwise_inside_image_mask | def get_pointwise_inside_image_mask(self, image):
"""
Get for each point whether it is inside of the given image plane.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
Returns
-------
ndarray
Boolean array with one value per point indicating whether it is
inside of the provided image plane (``True``) or not (``False``).
"""
if len(self.coords) == 0:
return np.zeros((0,), dtype=bool)
shape = normalize_shape(image)
height, width = shape[0:2]
x_within = np.logical_and(0 <= self.xx, self.xx < width)
y_within = np.logical_and(0 <= self.yy, self.yy < height)
return np.logical_and(x_within, y_within) | python | def get_pointwise_inside_image_mask(self, image):
"""
Get for each point whether it is inside of the given image plane.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
Returns
-------
ndarray
Boolean array with one value per point indicating whether it is
inside of the provided image plane (``True``) or not (``False``).
"""
if len(self.coords) == 0:
return np.zeros((0,), dtype=bool)
shape = normalize_shape(image)
height, width = shape[0:2]
x_within = np.logical_and(0 <= self.xx, self.xx < width)
y_within = np.logical_and(0 <= self.yy, self.yy < height)
return np.logical_and(x_within, y_within) | [
"def",
"get_pointwise_inside_image_mask",
"(",
"self",
",",
"image",
")",
":",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"==",
"0",
":",
"return",
"np",
".",
"zeros",
"(",
"(",
"0",
",",
")",
",",
"dtype",
"=",
"bool",
")",
"shape",
"=",
"normalize_shape",
"(",
"image",
")",
"height",
",",
"width",
"=",
"shape",
"[",
"0",
":",
"2",
"]",
"x_within",
"=",
"np",
".",
"logical_and",
"(",
"0",
"<=",
"self",
".",
"xx",
",",
"self",
".",
"xx",
"<",
"width",
")",
"y_within",
"=",
"np",
".",
"logical_and",
"(",
"0",
"<=",
"self",
".",
"yy",
",",
"self",
".",
"yy",
"<",
"height",
")",
"return",
"np",
".",
"logical_and",
"(",
"x_within",
",",
"y_within",
")"
] | Get for each point whether it is inside of the given image plane.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
Returns
-------
ndarray
Boolean array with one value per point indicating whether it is
inside of the provided image plane (``True``) or not (``False``). | [
"Get",
"for",
"each",
"point",
"whether",
"it",
"is",
"inside",
"of",
"the",
"given",
"image",
"plane",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L113-L136 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.compute_neighbour_distances | def compute_neighbour_distances(self):
"""
Get the euclidean distance between each two consecutive points.
Returns
-------
ndarray
Euclidean distances between point pairs.
Same order as in `coords`. For ``N`` points, ``N-1`` distances
are returned.
"""
if len(self.coords) <= 1:
return np.zeros((0,), dtype=np.float32)
return np.sqrt(
np.sum(
(self.coords[:-1, :] - self.coords[1:, :]) ** 2,
axis=1
)
) | python | def compute_neighbour_distances(self):
"""
Get the euclidean distance between each two consecutive points.
Returns
-------
ndarray
Euclidean distances between point pairs.
Same order as in `coords`. For ``N`` points, ``N-1`` distances
are returned.
"""
if len(self.coords) <= 1:
return np.zeros((0,), dtype=np.float32)
return np.sqrt(
np.sum(
(self.coords[:-1, :] - self.coords[1:, :]) ** 2,
axis=1
)
) | [
"def",
"compute_neighbour_distances",
"(",
"self",
")",
":",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"<=",
"1",
":",
"return",
"np",
".",
"zeros",
"(",
"(",
"0",
",",
")",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
"return",
"np",
".",
"sqrt",
"(",
"np",
".",
"sum",
"(",
"(",
"self",
".",
"coords",
"[",
":",
"-",
"1",
",",
":",
"]",
"-",
"self",
".",
"coords",
"[",
"1",
":",
",",
":",
"]",
")",
"**",
"2",
",",
"axis",
"=",
"1",
")",
")"
] | Get the euclidean distance between each two consecutive points.
Returns
-------
ndarray
Euclidean distances between point pairs.
Same order as in `coords`. For ``N`` points, ``N-1`` distances
are returned. | [
"Get",
"the",
"euclidean",
"distance",
"between",
"each",
"two",
"consecutive",
"points",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L139-L158 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.compute_pointwise_distances | def compute_pointwise_distances(self, other, default=None):
"""
Compute the minimal distance between each point on self and other.
Parameters
----------
other : tuple of number \
or imgaug.augmentables.kps.Keypoint \
or imgaug.augmentables.LineString
Other object to which to compute the distances.
default
Value to return if `other` contains no points.
Returns
-------
list of float
Distances to `other` or `default` if not distance could be computed.
"""
import shapely.geometry
from .kps import Keypoint
if isinstance(other, Keypoint):
other = shapely.geometry.Point((other.x, other.y))
elif isinstance(other, LineString):
if len(other.coords) == 0:
return default
elif len(other.coords) == 1:
other = shapely.geometry.Point(other.coords[0, :])
else:
other = shapely.geometry.LineString(other.coords)
elif isinstance(other, tuple):
assert len(other) == 2
other = shapely.geometry.Point(other)
else:
raise ValueError(
("Expected Keypoint or LineString or tuple (x,y), "
+ "got type %s.") % (type(other),))
return [shapely.geometry.Point(point).distance(other)
for point in self.coords] | python | def compute_pointwise_distances(self, other, default=None):
"""
Compute the minimal distance between each point on self and other.
Parameters
----------
other : tuple of number \
or imgaug.augmentables.kps.Keypoint \
or imgaug.augmentables.LineString
Other object to which to compute the distances.
default
Value to return if `other` contains no points.
Returns
-------
list of float
Distances to `other` or `default` if not distance could be computed.
"""
import shapely.geometry
from .kps import Keypoint
if isinstance(other, Keypoint):
other = shapely.geometry.Point((other.x, other.y))
elif isinstance(other, LineString):
if len(other.coords) == 0:
return default
elif len(other.coords) == 1:
other = shapely.geometry.Point(other.coords[0, :])
else:
other = shapely.geometry.LineString(other.coords)
elif isinstance(other, tuple):
assert len(other) == 2
other = shapely.geometry.Point(other)
else:
raise ValueError(
("Expected Keypoint or LineString or tuple (x,y), "
+ "got type %s.") % (type(other),))
return [shapely.geometry.Point(point).distance(other)
for point in self.coords] | [
"def",
"compute_pointwise_distances",
"(",
"self",
",",
"other",
",",
"default",
"=",
"None",
")",
":",
"import",
"shapely",
".",
"geometry",
"from",
".",
"kps",
"import",
"Keypoint",
"if",
"isinstance",
"(",
"other",
",",
"Keypoint",
")",
":",
"other",
"=",
"shapely",
".",
"geometry",
".",
"Point",
"(",
"(",
"other",
".",
"x",
",",
"other",
".",
"y",
")",
")",
"elif",
"isinstance",
"(",
"other",
",",
"LineString",
")",
":",
"if",
"len",
"(",
"other",
".",
"coords",
")",
"==",
"0",
":",
"return",
"default",
"elif",
"len",
"(",
"other",
".",
"coords",
")",
"==",
"1",
":",
"other",
"=",
"shapely",
".",
"geometry",
".",
"Point",
"(",
"other",
".",
"coords",
"[",
"0",
",",
":",
"]",
")",
"else",
":",
"other",
"=",
"shapely",
".",
"geometry",
".",
"LineString",
"(",
"other",
".",
"coords",
")",
"elif",
"isinstance",
"(",
"other",
",",
"tuple",
")",
":",
"assert",
"len",
"(",
"other",
")",
"==",
"2",
"other",
"=",
"shapely",
".",
"geometry",
".",
"Point",
"(",
"other",
")",
"else",
":",
"raise",
"ValueError",
"(",
"(",
"\"Expected Keypoint or LineString or tuple (x,y), \"",
"+",
"\"got type %s.\"",
")",
"%",
"(",
"type",
"(",
"other",
")",
",",
")",
")",
"return",
"[",
"shapely",
".",
"geometry",
".",
"Point",
"(",
"point",
")",
".",
"distance",
"(",
"other",
")",
"for",
"point",
"in",
"self",
".",
"coords",
"]"
] | Compute the minimal distance between each point on self and other.
Parameters
----------
other : tuple of number \
or imgaug.augmentables.kps.Keypoint \
or imgaug.augmentables.LineString
Other object to which to compute the distances.
default
Value to return if `other` contains no points.
Returns
-------
list of float
Distances to `other` or `default` if not distance could be computed. | [
"Compute",
"the",
"minimal",
"distance",
"between",
"each",
"point",
"on",
"self",
"and",
"other",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L160-L201 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.compute_distance | def compute_distance(self, other, default=None):
"""
Compute the minimal distance between the line string and `other`.
Parameters
----------
other : tuple of number \
or imgaug.augmentables.kps.Keypoint \
or imgaug.augmentables.LineString
Other object to which to compute the distance.
default
Value to return if this line string or `other` contain no points.
Returns
-------
float
Distance to `other` or `default` if not distance could be computed.
"""
# FIXME this computes distance pointwise, does not have to be identical
# with the actual min distance (e.g. edge center to other's point)
distances = self.compute_pointwise_distances(other, default=[])
if len(distances) == 0:
return default
return min(distances) | python | def compute_distance(self, other, default=None):
"""
Compute the minimal distance between the line string and `other`.
Parameters
----------
other : tuple of number \
or imgaug.augmentables.kps.Keypoint \
or imgaug.augmentables.LineString
Other object to which to compute the distance.
default
Value to return if this line string or `other` contain no points.
Returns
-------
float
Distance to `other` or `default` if not distance could be computed.
"""
# FIXME this computes distance pointwise, does not have to be identical
# with the actual min distance (e.g. edge center to other's point)
distances = self.compute_pointwise_distances(other, default=[])
if len(distances) == 0:
return default
return min(distances) | [
"def",
"compute_distance",
"(",
"self",
",",
"other",
",",
"default",
"=",
"None",
")",
":",
"# FIXME this computes distance pointwise, does not have to be identical",
"# with the actual min distance (e.g. edge center to other's point)",
"distances",
"=",
"self",
".",
"compute_pointwise_distances",
"(",
"other",
",",
"default",
"=",
"[",
"]",
")",
"if",
"len",
"(",
"distances",
")",
"==",
"0",
":",
"return",
"default",
"return",
"min",
"(",
"distances",
")"
] | Compute the minimal distance between the line string and `other`.
Parameters
----------
other : tuple of number \
or imgaug.augmentables.kps.Keypoint \
or imgaug.augmentables.LineString
Other object to which to compute the distance.
default
Value to return if this line string or `other` contain no points.
Returns
-------
float
Distance to `other` or `default` if not distance could be computed. | [
"Compute",
"the",
"minimal",
"distance",
"between",
"the",
"line",
"string",
"and",
"other",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L203-L228 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.contains | def contains(self, other, max_distance=1e-4):
"""
Estimate whether the bounding box contains a point.
Parameters
----------
other : tuple of number or imgaug.augmentables.kps.Keypoint
Point to check for.
max_distance : float
Maximum allowed euclidean distance between the point and the
closest point on the line. If the threshold is exceeded, the point
is not considered to be contained in the line.
Returns
-------
bool
True if the point is contained in the line string, False otherwise.
It is contained if its distance to the line or any of its points
is below a threshold.
"""
return self.compute_distance(other, default=np.inf) < max_distance | python | def contains(self, other, max_distance=1e-4):
"""
Estimate whether the bounding box contains a point.
Parameters
----------
other : tuple of number or imgaug.augmentables.kps.Keypoint
Point to check for.
max_distance : float
Maximum allowed euclidean distance between the point and the
closest point on the line. If the threshold is exceeded, the point
is not considered to be contained in the line.
Returns
-------
bool
True if the point is contained in the line string, False otherwise.
It is contained if its distance to the line or any of its points
is below a threshold.
"""
return self.compute_distance(other, default=np.inf) < max_distance | [
"def",
"contains",
"(",
"self",
",",
"other",
",",
"max_distance",
"=",
"1e-4",
")",
":",
"return",
"self",
".",
"compute_distance",
"(",
"other",
",",
"default",
"=",
"np",
".",
"inf",
")",
"<",
"max_distance"
] | Estimate whether the bounding box contains a point.
Parameters
----------
other : tuple of number or imgaug.augmentables.kps.Keypoint
Point to check for.
max_distance : float
Maximum allowed euclidean distance between the point and the
closest point on the line. If the threshold is exceeded, the point
is not considered to be contained in the line.
Returns
-------
bool
True if the point is contained in the line string, False otherwise.
It is contained if its distance to the line or any of its points
is below a threshold. | [
"Estimate",
"whether",
"the",
"bounding",
"box",
"contains",
"a",
"point",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L231-L253 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.project | def project(self, from_shape, to_shape):
"""
Project the line string onto a differently shaped image.
E.g. if a point of the line string is on its original image at
``x=(10 of 100 pixels)`` and ``y=(20 of 100 pixels)`` and is projected
onto a new image with size ``(width=200, height=200)``, its new
position will be ``(x=20, y=40)``.
This is intended for cases where the original image is resized.
It cannot be used for more complex changes (e.g. padding, cropping).
Parameters
----------
from_shape : tuple of int or ndarray
Shape of the original image. (Before resize.)
to_shape : tuple of int or ndarray
Shape of the new image. (After resize.)
Returns
-------
out : imgaug.augmentables.lines.LineString
Line string with new coordinates.
"""
coords_proj = project_coords(self.coords, from_shape, to_shape)
return self.copy(coords=coords_proj) | python | def project(self, from_shape, to_shape):
"""
Project the line string onto a differently shaped image.
E.g. if a point of the line string is on its original image at
``x=(10 of 100 pixels)`` and ``y=(20 of 100 pixels)`` and is projected
onto a new image with size ``(width=200, height=200)``, its new
position will be ``(x=20, y=40)``.
This is intended for cases where the original image is resized.
It cannot be used for more complex changes (e.g. padding, cropping).
Parameters
----------
from_shape : tuple of int or ndarray
Shape of the original image. (Before resize.)
to_shape : tuple of int or ndarray
Shape of the new image. (After resize.)
Returns
-------
out : imgaug.augmentables.lines.LineString
Line string with new coordinates.
"""
coords_proj = project_coords(self.coords, from_shape, to_shape)
return self.copy(coords=coords_proj) | [
"def",
"project",
"(",
"self",
",",
"from_shape",
",",
"to_shape",
")",
":",
"coords_proj",
"=",
"project_coords",
"(",
"self",
".",
"coords",
",",
"from_shape",
",",
"to_shape",
")",
"return",
"self",
".",
"copy",
"(",
"coords",
"=",
"coords_proj",
")"
] | Project the line string onto a differently shaped image.
E.g. if a point of the line string is on its original image at
``x=(10 of 100 pixels)`` and ``y=(20 of 100 pixels)`` and is projected
onto a new image with size ``(width=200, height=200)``, its new
position will be ``(x=20, y=40)``.
This is intended for cases where the original image is resized.
It cannot be used for more complex changes (e.g. padding, cropping).
Parameters
----------
from_shape : tuple of int or ndarray
Shape of the original image. (Before resize.)
to_shape : tuple of int or ndarray
Shape of the new image. (After resize.)
Returns
-------
out : imgaug.augmentables.lines.LineString
Line string with new coordinates. | [
"Project",
"the",
"line",
"string",
"onto",
"a",
"differently",
"shaped",
"image",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L255-L282 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.is_fully_within_image | def is_fully_within_image(self, image, default=False):
"""
Estimate whether the line string is fully inside the image area.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
default
Default value to return if the line string contains no points.
Returns
-------
bool
True if the line string is fully inside the image area.
False otherwise.
"""
if len(self.coords) == 0:
return default
return np.all(self.get_pointwise_inside_image_mask(image)) | python | def is_fully_within_image(self, image, default=False):
"""
Estimate whether the line string is fully inside the image area.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
default
Default value to return if the line string contains no points.
Returns
-------
bool
True if the line string is fully inside the image area.
False otherwise.
"""
if len(self.coords) == 0:
return default
return np.all(self.get_pointwise_inside_image_mask(image)) | [
"def",
"is_fully_within_image",
"(",
"self",
",",
"image",
",",
"default",
"=",
"False",
")",
":",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"==",
"0",
":",
"return",
"default",
"return",
"np",
".",
"all",
"(",
"self",
".",
"get_pointwise_inside_image_mask",
"(",
"image",
")",
")"
] | Estimate whether the line string is fully inside the image area.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
default
Default value to return if the line string contains no points.
Returns
-------
bool
True if the line string is fully inside the image area.
False otherwise. | [
"Estimate",
"whether",
"the",
"line",
"string",
"is",
"fully",
"inside",
"the",
"image",
"area",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L284-L306 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.is_partly_within_image | def is_partly_within_image(self, image, default=False):
"""
Estimate whether the line string is at least partially inside the image.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
default
Default value to return if the line string contains no points.
Returns
-------
bool
True if the line string is at least partially inside the image area.
False otherwise.
"""
if len(self.coords) == 0:
return default
# check mask first to avoid costly computation of intersection points
# whenever possible
mask = self.get_pointwise_inside_image_mask(image)
if np.any(mask):
return True
return len(self.clip_out_of_image(image)) > 0 | python | def is_partly_within_image(self, image, default=False):
"""
Estimate whether the line string is at least partially inside the image.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
default
Default value to return if the line string contains no points.
Returns
-------
bool
True if the line string is at least partially inside the image area.
False otherwise.
"""
if len(self.coords) == 0:
return default
# check mask first to avoid costly computation of intersection points
# whenever possible
mask = self.get_pointwise_inside_image_mask(image)
if np.any(mask):
return True
return len(self.clip_out_of_image(image)) > 0 | [
"def",
"is_partly_within_image",
"(",
"self",
",",
"image",
",",
"default",
"=",
"False",
")",
":",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"==",
"0",
":",
"return",
"default",
"# check mask first to avoid costly computation of intersection points",
"# whenever possible",
"mask",
"=",
"self",
".",
"get_pointwise_inside_image_mask",
"(",
"image",
")",
"if",
"np",
".",
"any",
"(",
"mask",
")",
":",
"return",
"True",
"return",
"len",
"(",
"self",
".",
"clip_out_of_image",
"(",
"image",
")",
")",
">",
"0"
] | Estimate whether the line string is at least partially inside the image.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
default
Default value to return if the line string contains no points.
Returns
-------
bool
True if the line string is at least partially inside the image area.
False otherwise. | [
"Estimate",
"whether",
"the",
"line",
"string",
"is",
"at",
"least",
"partially",
"inside",
"the",
"image",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L308-L335 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.is_out_of_image | def is_out_of_image(self, image, fully=True, partly=False, default=True):
"""
Estimate whether the line is partially/fully outside of the image area.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
fully : bool, optional
Whether to return True if the bounding box is fully outside fo the
image area.
partly : bool, optional
Whether to return True if the bounding box is at least partially
outside fo the image area.
default
Default value to return if the line string contains no points.
Returns
-------
bool
`default` if the line string has no points.
True if the line string is partially/fully outside of the image
area, depending on defined parameters.
False otherwise.
"""
if len(self.coords) == 0:
return default
if self.is_fully_within_image(image):
return False
elif self.is_partly_within_image(image):
return partly
else:
return fully | python | def is_out_of_image(self, image, fully=True, partly=False, default=True):
"""
Estimate whether the line is partially/fully outside of the image area.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
fully : bool, optional
Whether to return True if the bounding box is fully outside fo the
image area.
partly : bool, optional
Whether to return True if the bounding box is at least partially
outside fo the image area.
default
Default value to return if the line string contains no points.
Returns
-------
bool
`default` if the line string has no points.
True if the line string is partially/fully outside of the image
area, depending on defined parameters.
False otherwise.
"""
if len(self.coords) == 0:
return default
if self.is_fully_within_image(image):
return False
elif self.is_partly_within_image(image):
return partly
else:
return fully | [
"def",
"is_out_of_image",
"(",
"self",
",",
"image",
",",
"fully",
"=",
"True",
",",
"partly",
"=",
"False",
",",
"default",
"=",
"True",
")",
":",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"==",
"0",
":",
"return",
"default",
"if",
"self",
".",
"is_fully_within_image",
"(",
"image",
")",
":",
"return",
"False",
"elif",
"self",
".",
"is_partly_within_image",
"(",
"image",
")",
":",
"return",
"partly",
"else",
":",
"return",
"fully"
] | Estimate whether the line is partially/fully outside of the image area.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
fully : bool, optional
Whether to return True if the bounding box is fully outside fo the
image area.
partly : bool, optional
Whether to return True if the bounding box is at least partially
outside fo the image area.
default
Default value to return if the line string contains no points.
Returns
-------
bool
`default` if the line string has no points.
True if the line string is partially/fully outside of the image
area, depending on defined parameters.
False otherwise. | [
"Estimate",
"whether",
"the",
"line",
"is",
"partially",
"/",
"fully",
"outside",
"of",
"the",
"image",
"area",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L337-L375 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.clip_out_of_image | def clip_out_of_image(self, image):
"""
Clip off all parts of the line_string that are outside of the image.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
Returns
-------
list of imgaug.augmentables.lines.LineString
Line strings, clipped to the image shape.
The result may contain any number of line strins, including zero.
"""
if len(self.coords) == 0:
return []
inside_image_mask = self.get_pointwise_inside_image_mask(image)
ooi_mask = ~inside_image_mask
if len(self.coords) == 1:
if not np.any(inside_image_mask):
return []
return [self.copy()]
if np.all(inside_image_mask):
return [self.copy()]
# top, right, bottom, left image edges
# we subtract eps here, because intersection() works inclusively,
# i.e. not subtracting eps would be equivalent to 0<=x<=C for C being
# height or width
# don't set the eps too low, otherwise points at height/width seem
# to get rounded to height/width by shapely, which can cause problems
# when first clipping and then calling is_fully_within_image()
# returning false
height, width = normalize_shape(image)[0:2]
eps = 1e-3
edges = [
LineString([(0.0, 0.0), (width - eps, 0.0)]),
LineString([(width - eps, 0.0), (width - eps, height - eps)]),
LineString([(width - eps, height - eps), (0.0, height - eps)]),
LineString([(0.0, height - eps), (0.0, 0.0)])
]
intersections = self.find_intersections_with(edges)
points = []
gen = enumerate(zip(self.coords[:-1], self.coords[1:],
ooi_mask[:-1], ooi_mask[1:],
intersections))
for i, (line_start, line_end, ooi_start, ooi_end, inter_line) in gen:
points.append((line_start, False, ooi_start))
for p_inter in inter_line:
points.append((p_inter, True, False))
is_last = (i == len(self.coords) - 2)
if is_last and not ooi_end:
points.append((line_end, False, ooi_end))
lines = []
line = []
for i, (coord, was_added, ooi) in enumerate(points):
# remove any point that is outside of the image,
# also start a new line once such a point is detected
if ooi:
if len(line) > 0:
lines.append(line)
line = []
continue
if not was_added:
# add all points that were part of the original line string
# AND that are inside the image plane
line.append(coord)
else:
is_last_point = (i == len(points)-1)
# ooi is a numpy.bool_, hence the bool(.)
is_next_ooi = (not is_last_point
and bool(points[i+1][2]) is True)
# Add all points that were new (i.e. intersections), so
# long that they aren't essentially identical to other point.
# This prevents adding overlapping intersections multiple times.
# (E.g. when a line intersects with a corner of the image plane
# and also with one of its edges.)
p_prev = line[-1] if len(line) > 0 else None
# ignore next point if end reached or next point is out of image
p_next = None
if not is_last_point and not is_next_ooi:
p_next = points[i+1][0]
dist_prev = None
dist_next = None
if p_prev is not None:
dist_prev = np.linalg.norm(
np.float32(coord) - np.float32(p_prev))
if p_next is not None:
dist_next = np.linalg.norm(
np.float32(coord) - np.float32(p_next))
dist_prev_ok = (dist_prev is None or dist_prev > 1e-2)
dist_next_ok = (dist_next is None or dist_next > 1e-2)
if dist_prev_ok and dist_next_ok:
line.append(coord)
if len(line) > 0:
lines.append(line)
lines = [line for line in lines if len(line) > 0]
return [self.deepcopy(coords=line) for line in lines] | python | def clip_out_of_image(self, image):
"""
Clip off all parts of the line_string that are outside of the image.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
Returns
-------
list of imgaug.augmentables.lines.LineString
Line strings, clipped to the image shape.
The result may contain any number of line strins, including zero.
"""
if len(self.coords) == 0:
return []
inside_image_mask = self.get_pointwise_inside_image_mask(image)
ooi_mask = ~inside_image_mask
if len(self.coords) == 1:
if not np.any(inside_image_mask):
return []
return [self.copy()]
if np.all(inside_image_mask):
return [self.copy()]
# top, right, bottom, left image edges
# we subtract eps here, because intersection() works inclusively,
# i.e. not subtracting eps would be equivalent to 0<=x<=C for C being
# height or width
# don't set the eps too low, otherwise points at height/width seem
# to get rounded to height/width by shapely, which can cause problems
# when first clipping and then calling is_fully_within_image()
# returning false
height, width = normalize_shape(image)[0:2]
eps = 1e-3
edges = [
LineString([(0.0, 0.0), (width - eps, 0.0)]),
LineString([(width - eps, 0.0), (width - eps, height - eps)]),
LineString([(width - eps, height - eps), (0.0, height - eps)]),
LineString([(0.0, height - eps), (0.0, 0.0)])
]
intersections = self.find_intersections_with(edges)
points = []
gen = enumerate(zip(self.coords[:-1], self.coords[1:],
ooi_mask[:-1], ooi_mask[1:],
intersections))
for i, (line_start, line_end, ooi_start, ooi_end, inter_line) in gen:
points.append((line_start, False, ooi_start))
for p_inter in inter_line:
points.append((p_inter, True, False))
is_last = (i == len(self.coords) - 2)
if is_last and not ooi_end:
points.append((line_end, False, ooi_end))
lines = []
line = []
for i, (coord, was_added, ooi) in enumerate(points):
# remove any point that is outside of the image,
# also start a new line once such a point is detected
if ooi:
if len(line) > 0:
lines.append(line)
line = []
continue
if not was_added:
# add all points that were part of the original line string
# AND that are inside the image plane
line.append(coord)
else:
is_last_point = (i == len(points)-1)
# ooi is a numpy.bool_, hence the bool(.)
is_next_ooi = (not is_last_point
and bool(points[i+1][2]) is True)
# Add all points that were new (i.e. intersections), so
# long that they aren't essentially identical to other point.
# This prevents adding overlapping intersections multiple times.
# (E.g. when a line intersects with a corner of the image plane
# and also with one of its edges.)
p_prev = line[-1] if len(line) > 0 else None
# ignore next point if end reached or next point is out of image
p_next = None
if not is_last_point and not is_next_ooi:
p_next = points[i+1][0]
dist_prev = None
dist_next = None
if p_prev is not None:
dist_prev = np.linalg.norm(
np.float32(coord) - np.float32(p_prev))
if p_next is not None:
dist_next = np.linalg.norm(
np.float32(coord) - np.float32(p_next))
dist_prev_ok = (dist_prev is None or dist_prev > 1e-2)
dist_next_ok = (dist_next is None or dist_next > 1e-2)
if dist_prev_ok and dist_next_ok:
line.append(coord)
if len(line) > 0:
lines.append(line)
lines = [line for line in lines if len(line) > 0]
return [self.deepcopy(coords=line) for line in lines] | [
"def",
"clip_out_of_image",
"(",
"self",
",",
"image",
")",
":",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"==",
"0",
":",
"return",
"[",
"]",
"inside_image_mask",
"=",
"self",
".",
"get_pointwise_inside_image_mask",
"(",
"image",
")",
"ooi_mask",
"=",
"~",
"inside_image_mask",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"==",
"1",
":",
"if",
"not",
"np",
".",
"any",
"(",
"inside_image_mask",
")",
":",
"return",
"[",
"]",
"return",
"[",
"self",
".",
"copy",
"(",
")",
"]",
"if",
"np",
".",
"all",
"(",
"inside_image_mask",
")",
":",
"return",
"[",
"self",
".",
"copy",
"(",
")",
"]",
"# top, right, bottom, left image edges",
"# we subtract eps here, because intersection() works inclusively,",
"# i.e. not subtracting eps would be equivalent to 0<=x<=C for C being",
"# height or width",
"# don't set the eps too low, otherwise points at height/width seem",
"# to get rounded to height/width by shapely, which can cause problems",
"# when first clipping and then calling is_fully_within_image()",
"# returning false",
"height",
",",
"width",
"=",
"normalize_shape",
"(",
"image",
")",
"[",
"0",
":",
"2",
"]",
"eps",
"=",
"1e-3",
"edges",
"=",
"[",
"LineString",
"(",
"[",
"(",
"0.0",
",",
"0.0",
")",
",",
"(",
"width",
"-",
"eps",
",",
"0.0",
")",
"]",
")",
",",
"LineString",
"(",
"[",
"(",
"width",
"-",
"eps",
",",
"0.0",
")",
",",
"(",
"width",
"-",
"eps",
",",
"height",
"-",
"eps",
")",
"]",
")",
",",
"LineString",
"(",
"[",
"(",
"width",
"-",
"eps",
",",
"height",
"-",
"eps",
")",
",",
"(",
"0.0",
",",
"height",
"-",
"eps",
")",
"]",
")",
",",
"LineString",
"(",
"[",
"(",
"0.0",
",",
"height",
"-",
"eps",
")",
",",
"(",
"0.0",
",",
"0.0",
")",
"]",
")",
"]",
"intersections",
"=",
"self",
".",
"find_intersections_with",
"(",
"edges",
")",
"points",
"=",
"[",
"]",
"gen",
"=",
"enumerate",
"(",
"zip",
"(",
"self",
".",
"coords",
"[",
":",
"-",
"1",
"]",
",",
"self",
".",
"coords",
"[",
"1",
":",
"]",
",",
"ooi_mask",
"[",
":",
"-",
"1",
"]",
",",
"ooi_mask",
"[",
"1",
":",
"]",
",",
"intersections",
")",
")",
"for",
"i",
",",
"(",
"line_start",
",",
"line_end",
",",
"ooi_start",
",",
"ooi_end",
",",
"inter_line",
")",
"in",
"gen",
":",
"points",
".",
"append",
"(",
"(",
"line_start",
",",
"False",
",",
"ooi_start",
")",
")",
"for",
"p_inter",
"in",
"inter_line",
":",
"points",
".",
"append",
"(",
"(",
"p_inter",
",",
"True",
",",
"False",
")",
")",
"is_last",
"=",
"(",
"i",
"==",
"len",
"(",
"self",
".",
"coords",
")",
"-",
"2",
")",
"if",
"is_last",
"and",
"not",
"ooi_end",
":",
"points",
".",
"append",
"(",
"(",
"line_end",
",",
"False",
",",
"ooi_end",
")",
")",
"lines",
"=",
"[",
"]",
"line",
"=",
"[",
"]",
"for",
"i",
",",
"(",
"coord",
",",
"was_added",
",",
"ooi",
")",
"in",
"enumerate",
"(",
"points",
")",
":",
"# remove any point that is outside of the image,",
"# also start a new line once such a point is detected",
"if",
"ooi",
":",
"if",
"len",
"(",
"line",
")",
">",
"0",
":",
"lines",
".",
"append",
"(",
"line",
")",
"line",
"=",
"[",
"]",
"continue",
"if",
"not",
"was_added",
":",
"# add all points that were part of the original line string",
"# AND that are inside the image plane",
"line",
".",
"append",
"(",
"coord",
")",
"else",
":",
"is_last_point",
"=",
"(",
"i",
"==",
"len",
"(",
"points",
")",
"-",
"1",
")",
"# ooi is a numpy.bool_, hence the bool(.)",
"is_next_ooi",
"=",
"(",
"not",
"is_last_point",
"and",
"bool",
"(",
"points",
"[",
"i",
"+",
"1",
"]",
"[",
"2",
"]",
")",
"is",
"True",
")",
"# Add all points that were new (i.e. intersections), so",
"# long that they aren't essentially identical to other point.",
"# This prevents adding overlapping intersections multiple times.",
"# (E.g. when a line intersects with a corner of the image plane",
"# and also with one of its edges.)",
"p_prev",
"=",
"line",
"[",
"-",
"1",
"]",
"if",
"len",
"(",
"line",
")",
">",
"0",
"else",
"None",
"# ignore next point if end reached or next point is out of image",
"p_next",
"=",
"None",
"if",
"not",
"is_last_point",
"and",
"not",
"is_next_ooi",
":",
"p_next",
"=",
"points",
"[",
"i",
"+",
"1",
"]",
"[",
"0",
"]",
"dist_prev",
"=",
"None",
"dist_next",
"=",
"None",
"if",
"p_prev",
"is",
"not",
"None",
":",
"dist_prev",
"=",
"np",
".",
"linalg",
".",
"norm",
"(",
"np",
".",
"float32",
"(",
"coord",
")",
"-",
"np",
".",
"float32",
"(",
"p_prev",
")",
")",
"if",
"p_next",
"is",
"not",
"None",
":",
"dist_next",
"=",
"np",
".",
"linalg",
".",
"norm",
"(",
"np",
".",
"float32",
"(",
"coord",
")",
"-",
"np",
".",
"float32",
"(",
"p_next",
")",
")",
"dist_prev_ok",
"=",
"(",
"dist_prev",
"is",
"None",
"or",
"dist_prev",
">",
"1e-2",
")",
"dist_next_ok",
"=",
"(",
"dist_next",
"is",
"None",
"or",
"dist_next",
">",
"1e-2",
")",
"if",
"dist_prev_ok",
"and",
"dist_next_ok",
":",
"line",
".",
"append",
"(",
"coord",
")",
"if",
"len",
"(",
"line",
")",
">",
"0",
":",
"lines",
".",
"append",
"(",
"line",
")",
"lines",
"=",
"[",
"line",
"for",
"line",
"in",
"lines",
"if",
"len",
"(",
"line",
")",
">",
"0",
"]",
"return",
"[",
"self",
".",
"deepcopy",
"(",
"coords",
"=",
"line",
")",
"for",
"line",
"in",
"lines",
"]"
] | Clip off all parts of the line_string that are outside of the image.
Parameters
----------
image : ndarray or tuple of int
Either an image with shape ``(H,W,[C])`` or a tuple denoting
such an image shape.
Returns
-------
list of imgaug.augmentables.lines.LineString
Line strings, clipped to the image shape.
The result may contain any number of line strins, including zero. | [
"Clip",
"off",
"all",
"parts",
"of",
"the",
"line_string",
"that",
"are",
"outside",
"of",
"the",
"image",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L377-L488 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.find_intersections_with | def find_intersections_with(self, other):
"""
Find all intersection points between the line string and `other`.
Parameters
----------
other : tuple of number or list of tuple of number or \
list of LineString or LineString
The other geometry to use during intersection tests.
Returns
-------
list of list of tuple of number
All intersection points. One list per pair of consecutive start
and end point, i.e. `N-1` lists of `N` points. Each list may
be empty or may contain multiple points.
"""
import shapely.geometry
geom = _convert_var_to_shapely_geometry(other)
result = []
for p_start, p_end in zip(self.coords[:-1], self.coords[1:]):
ls = shapely.geometry.LineString([p_start, p_end])
intersections = ls.intersection(geom)
intersections = list(_flatten_shapely_collection(intersections))
intersections_points = []
for inter in intersections:
if isinstance(inter, shapely.geometry.linestring.LineString):
inter_start = (inter.coords[0][0], inter.coords[0][1])
inter_end = (inter.coords[-1][0], inter.coords[-1][1])
intersections_points.extend([inter_start, inter_end])
else:
assert isinstance(inter, shapely.geometry.point.Point), (
"Expected to find shapely.geometry.point.Point or "
"shapely.geometry.linestring.LineString intersection, "
"actually found %s." % (type(inter),))
intersections_points.append((inter.x, inter.y))
# sort by distance to start point, this makes it later on easier
# to remove duplicate points
inter_sorted = sorted(
intersections_points,
key=lambda p: np.linalg.norm(np.float32(p) - p_start)
)
result.append(inter_sorted)
return result | python | def find_intersections_with(self, other):
"""
Find all intersection points between the line string and `other`.
Parameters
----------
other : tuple of number or list of tuple of number or \
list of LineString or LineString
The other geometry to use during intersection tests.
Returns
-------
list of list of tuple of number
All intersection points. One list per pair of consecutive start
and end point, i.e. `N-1` lists of `N` points. Each list may
be empty or may contain multiple points.
"""
import shapely.geometry
geom = _convert_var_to_shapely_geometry(other)
result = []
for p_start, p_end in zip(self.coords[:-1], self.coords[1:]):
ls = shapely.geometry.LineString([p_start, p_end])
intersections = ls.intersection(geom)
intersections = list(_flatten_shapely_collection(intersections))
intersections_points = []
for inter in intersections:
if isinstance(inter, shapely.geometry.linestring.LineString):
inter_start = (inter.coords[0][0], inter.coords[0][1])
inter_end = (inter.coords[-1][0], inter.coords[-1][1])
intersections_points.extend([inter_start, inter_end])
else:
assert isinstance(inter, shapely.geometry.point.Point), (
"Expected to find shapely.geometry.point.Point or "
"shapely.geometry.linestring.LineString intersection, "
"actually found %s." % (type(inter),))
intersections_points.append((inter.x, inter.y))
# sort by distance to start point, this makes it later on easier
# to remove duplicate points
inter_sorted = sorted(
intersections_points,
key=lambda p: np.linalg.norm(np.float32(p) - p_start)
)
result.append(inter_sorted)
return result | [
"def",
"find_intersections_with",
"(",
"self",
",",
"other",
")",
":",
"import",
"shapely",
".",
"geometry",
"geom",
"=",
"_convert_var_to_shapely_geometry",
"(",
"other",
")",
"result",
"=",
"[",
"]",
"for",
"p_start",
",",
"p_end",
"in",
"zip",
"(",
"self",
".",
"coords",
"[",
":",
"-",
"1",
"]",
",",
"self",
".",
"coords",
"[",
"1",
":",
"]",
")",
":",
"ls",
"=",
"shapely",
".",
"geometry",
".",
"LineString",
"(",
"[",
"p_start",
",",
"p_end",
"]",
")",
"intersections",
"=",
"ls",
".",
"intersection",
"(",
"geom",
")",
"intersections",
"=",
"list",
"(",
"_flatten_shapely_collection",
"(",
"intersections",
")",
")",
"intersections_points",
"=",
"[",
"]",
"for",
"inter",
"in",
"intersections",
":",
"if",
"isinstance",
"(",
"inter",
",",
"shapely",
".",
"geometry",
".",
"linestring",
".",
"LineString",
")",
":",
"inter_start",
"=",
"(",
"inter",
".",
"coords",
"[",
"0",
"]",
"[",
"0",
"]",
",",
"inter",
".",
"coords",
"[",
"0",
"]",
"[",
"1",
"]",
")",
"inter_end",
"=",
"(",
"inter",
".",
"coords",
"[",
"-",
"1",
"]",
"[",
"0",
"]",
",",
"inter",
".",
"coords",
"[",
"-",
"1",
"]",
"[",
"1",
"]",
")",
"intersections_points",
".",
"extend",
"(",
"[",
"inter_start",
",",
"inter_end",
"]",
")",
"else",
":",
"assert",
"isinstance",
"(",
"inter",
",",
"shapely",
".",
"geometry",
".",
"point",
".",
"Point",
")",
",",
"(",
"\"Expected to find shapely.geometry.point.Point or \"",
"\"shapely.geometry.linestring.LineString intersection, \"",
"\"actually found %s.\"",
"%",
"(",
"type",
"(",
"inter",
")",
",",
")",
")",
"intersections_points",
".",
"append",
"(",
"(",
"inter",
".",
"x",
",",
"inter",
".",
"y",
")",
")",
"# sort by distance to start point, this makes it later on easier",
"# to remove duplicate points",
"inter_sorted",
"=",
"sorted",
"(",
"intersections_points",
",",
"key",
"=",
"lambda",
"p",
":",
"np",
".",
"linalg",
".",
"norm",
"(",
"np",
".",
"float32",
"(",
"p",
")",
"-",
"p_start",
")",
")",
"result",
".",
"append",
"(",
"inter_sorted",
")",
"return",
"result"
] | Find all intersection points between the line string and `other`.
Parameters
----------
other : tuple of number or list of tuple of number or \
list of LineString or LineString
The other geometry to use during intersection tests.
Returns
-------
list of list of tuple of number
All intersection points. One list per pair of consecutive start
and end point, i.e. `N-1` lists of `N` points. Each list may
be empty or may contain multiple points. | [
"Find",
"all",
"intersection",
"points",
"between",
"the",
"line",
"string",
"and",
"other",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L491-L540 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.shift | def shift(self, top=None, right=None, bottom=None, left=None):
"""
Shift/move the line string from one or more image sides.
Parameters
----------
top : None or int, optional
Amount of pixels by which to shift the bounding box from the
top.
right : None or int, optional
Amount of pixels by which to shift the bounding box from the
right.
bottom : None or int, optional
Amount of pixels by which to shift the bounding box from the
bottom.
left : None or int, optional
Amount of pixels by which to shift the bounding box from the
left.
Returns
-------
result : imgaug.augmentables.lines.LineString
Shifted line string.
"""
top = top if top is not None else 0
right = right if right is not None else 0
bottom = bottom if bottom is not None else 0
left = left if left is not None else 0
coords = np.copy(self.coords)
coords[:, 0] += left - right
coords[:, 1] += top - bottom
return self.copy(coords=coords) | python | def shift(self, top=None, right=None, bottom=None, left=None):
"""
Shift/move the line string from one or more image sides.
Parameters
----------
top : None or int, optional
Amount of pixels by which to shift the bounding box from the
top.
right : None or int, optional
Amount of pixels by which to shift the bounding box from the
right.
bottom : None or int, optional
Amount of pixels by which to shift the bounding box from the
bottom.
left : None or int, optional
Amount of pixels by which to shift the bounding box from the
left.
Returns
-------
result : imgaug.augmentables.lines.LineString
Shifted line string.
"""
top = top if top is not None else 0
right = right if right is not None else 0
bottom = bottom if bottom is not None else 0
left = left if left is not None else 0
coords = np.copy(self.coords)
coords[:, 0] += left - right
coords[:, 1] += top - bottom
return self.copy(coords=coords) | [
"def",
"shift",
"(",
"self",
",",
"top",
"=",
"None",
",",
"right",
"=",
"None",
",",
"bottom",
"=",
"None",
",",
"left",
"=",
"None",
")",
":",
"top",
"=",
"top",
"if",
"top",
"is",
"not",
"None",
"else",
"0",
"right",
"=",
"right",
"if",
"right",
"is",
"not",
"None",
"else",
"0",
"bottom",
"=",
"bottom",
"if",
"bottom",
"is",
"not",
"None",
"else",
"0",
"left",
"=",
"left",
"if",
"left",
"is",
"not",
"None",
"else",
"0",
"coords",
"=",
"np",
".",
"copy",
"(",
"self",
".",
"coords",
")",
"coords",
"[",
":",
",",
"0",
"]",
"+=",
"left",
"-",
"right",
"coords",
"[",
":",
",",
"1",
"]",
"+=",
"top",
"-",
"bottom",
"return",
"self",
".",
"copy",
"(",
"coords",
"=",
"coords",
")"
] | Shift/move the line string from one or more image sides.
Parameters
----------
top : None or int, optional
Amount of pixels by which to shift the bounding box from the
top.
right : None or int, optional
Amount of pixels by which to shift the bounding box from the
right.
bottom : None or int, optional
Amount of pixels by which to shift the bounding box from the
bottom.
left : None or int, optional
Amount of pixels by which to shift the bounding box from the
left.
Returns
-------
result : imgaug.augmentables.lines.LineString
Shifted line string. | [
"Shift",
"/",
"move",
"the",
"line",
"string",
"from",
"one",
"or",
"more",
"image",
"sides",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L543-L578 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.draw_mask | def draw_mask(self, image_shape, size_lines=1, size_points=0,
raise_if_out_of_image=False):
"""
Draw this line segment as a binary image mask.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
size_lines : int, optional
Thickness of the line segments.
size_points : int, optional
Size of the points in pixels.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Boolean line mask of shape `image_shape` (no channel axis).
"""
heatmap = self.draw_heatmap_array(
image_shape,
alpha_lines=1.0, alpha_points=1.0,
size_lines=size_lines, size_points=size_points,
antialiased=False,
raise_if_out_of_image=raise_if_out_of_image)
return heatmap > 0.5 | python | def draw_mask(self, image_shape, size_lines=1, size_points=0,
raise_if_out_of_image=False):
"""
Draw this line segment as a binary image mask.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
size_lines : int, optional
Thickness of the line segments.
size_points : int, optional
Size of the points in pixels.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Boolean line mask of shape `image_shape` (no channel axis).
"""
heatmap = self.draw_heatmap_array(
image_shape,
alpha_lines=1.0, alpha_points=1.0,
size_lines=size_lines, size_points=size_points,
antialiased=False,
raise_if_out_of_image=raise_if_out_of_image)
return heatmap > 0.5 | [
"def",
"draw_mask",
"(",
"self",
",",
"image_shape",
",",
"size_lines",
"=",
"1",
",",
"size_points",
"=",
"0",
",",
"raise_if_out_of_image",
"=",
"False",
")",
":",
"heatmap",
"=",
"self",
".",
"draw_heatmap_array",
"(",
"image_shape",
",",
"alpha_lines",
"=",
"1.0",
",",
"alpha_points",
"=",
"1.0",
",",
"size_lines",
"=",
"size_lines",
",",
"size_points",
"=",
"size_points",
",",
"antialiased",
"=",
"False",
",",
"raise_if_out_of_image",
"=",
"raise_if_out_of_image",
")",
"return",
"heatmap",
">",
"0.5"
] | Draw this line segment as a binary image mask.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
size_lines : int, optional
Thickness of the line segments.
size_points : int, optional
Size of the points in pixels.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Boolean line mask of shape `image_shape` (no channel axis). | [
"Draw",
"this",
"line",
"segment",
"as",
"a",
"binary",
"image",
"mask",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L580-L613 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.draw_lines_heatmap_array | def draw_lines_heatmap_array(self, image_shape, alpha=1.0,
size=1, antialiased=True,
raise_if_out_of_image=False):
"""
Draw the line segments of the line string as a heatmap array.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
alpha : float, optional
Opacity of the line string. Higher values denote a more visible
line string.
size : int, optional
Thickness of the line segments.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Float array of shape `image_shape` (no channel axis) with drawn
line string. All values are in the interval ``[0.0, 1.0]``.
"""
assert len(image_shape) == 2 or (
len(image_shape) == 3 and image_shape[-1] == 1), (
"Expected (H,W) or (H,W,1) as image_shape, got %s." % (
image_shape,))
arr = self.draw_lines_on_image(
np.zeros(image_shape, dtype=np.uint8),
color=255, alpha=alpha, size=size,
antialiased=antialiased,
raise_if_out_of_image=raise_if_out_of_image
)
return arr.astype(np.float32) / 255.0 | python | def draw_lines_heatmap_array(self, image_shape, alpha=1.0,
size=1, antialiased=True,
raise_if_out_of_image=False):
"""
Draw the line segments of the line string as a heatmap array.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
alpha : float, optional
Opacity of the line string. Higher values denote a more visible
line string.
size : int, optional
Thickness of the line segments.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Float array of shape `image_shape` (no channel axis) with drawn
line string. All values are in the interval ``[0.0, 1.0]``.
"""
assert len(image_shape) == 2 or (
len(image_shape) == 3 and image_shape[-1] == 1), (
"Expected (H,W) or (H,W,1) as image_shape, got %s." % (
image_shape,))
arr = self.draw_lines_on_image(
np.zeros(image_shape, dtype=np.uint8),
color=255, alpha=alpha, size=size,
antialiased=antialiased,
raise_if_out_of_image=raise_if_out_of_image
)
return arr.astype(np.float32) / 255.0 | [
"def",
"draw_lines_heatmap_array",
"(",
"self",
",",
"image_shape",
",",
"alpha",
"=",
"1.0",
",",
"size",
"=",
"1",
",",
"antialiased",
"=",
"True",
",",
"raise_if_out_of_image",
"=",
"False",
")",
":",
"assert",
"len",
"(",
"image_shape",
")",
"==",
"2",
"or",
"(",
"len",
"(",
"image_shape",
")",
"==",
"3",
"and",
"image_shape",
"[",
"-",
"1",
"]",
"==",
"1",
")",
",",
"(",
"\"Expected (H,W) or (H,W,1) as image_shape, got %s.\"",
"%",
"(",
"image_shape",
",",
")",
")",
"arr",
"=",
"self",
".",
"draw_lines_on_image",
"(",
"np",
".",
"zeros",
"(",
"image_shape",
",",
"dtype",
"=",
"np",
".",
"uint8",
")",
",",
"color",
"=",
"255",
",",
"alpha",
"=",
"alpha",
",",
"size",
"=",
"size",
",",
"antialiased",
"=",
"antialiased",
",",
"raise_if_out_of_image",
"=",
"raise_if_out_of_image",
")",
"return",
"arr",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"/",
"255.0"
] | Draw the line segments of the line string as a heatmap array.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
alpha : float, optional
Opacity of the line string. Higher values denote a more visible
line string.
size : int, optional
Thickness of the line segments.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Float array of shape `image_shape` (no channel axis) with drawn
line string. All values are in the interval ``[0.0, 1.0]``. | [
"Draw",
"the",
"line",
"segments",
"of",
"the",
"line",
"string",
"as",
"a",
"heatmap",
"array",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L615-L659 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.draw_points_heatmap_array | def draw_points_heatmap_array(self, image_shape, alpha=1.0,
size=1, raise_if_out_of_image=False):
"""
Draw the points of the line string as a heatmap array.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the point mask.
alpha : float, optional
Opacity of the line string points. Higher values denote a more
visible points.
size : int, optional
Size of the points in pixels.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Float array of shape `image_shape` (no channel axis) with drawn
line string points. All values are in the interval ``[0.0, 1.0]``.
"""
assert len(image_shape) == 2 or (
len(image_shape) == 3 and image_shape[-1] == 1), (
"Expected (H,W) or (H,W,1) as image_shape, got %s." % (
image_shape,))
arr = self.draw_points_on_image(
np.zeros(image_shape, dtype=np.uint8),
color=255, alpha=alpha, size=size,
raise_if_out_of_image=raise_if_out_of_image
)
return arr.astype(np.float32) / 255.0 | python | def draw_points_heatmap_array(self, image_shape, alpha=1.0,
size=1, raise_if_out_of_image=False):
"""
Draw the points of the line string as a heatmap array.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the point mask.
alpha : float, optional
Opacity of the line string points. Higher values denote a more
visible points.
size : int, optional
Size of the points in pixels.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Float array of shape `image_shape` (no channel axis) with drawn
line string points. All values are in the interval ``[0.0, 1.0]``.
"""
assert len(image_shape) == 2 or (
len(image_shape) == 3 and image_shape[-1] == 1), (
"Expected (H,W) or (H,W,1) as image_shape, got %s." % (
image_shape,))
arr = self.draw_points_on_image(
np.zeros(image_shape, dtype=np.uint8),
color=255, alpha=alpha, size=size,
raise_if_out_of_image=raise_if_out_of_image
)
return arr.astype(np.float32) / 255.0 | [
"def",
"draw_points_heatmap_array",
"(",
"self",
",",
"image_shape",
",",
"alpha",
"=",
"1.0",
",",
"size",
"=",
"1",
",",
"raise_if_out_of_image",
"=",
"False",
")",
":",
"assert",
"len",
"(",
"image_shape",
")",
"==",
"2",
"or",
"(",
"len",
"(",
"image_shape",
")",
"==",
"3",
"and",
"image_shape",
"[",
"-",
"1",
"]",
"==",
"1",
")",
",",
"(",
"\"Expected (H,W) or (H,W,1) as image_shape, got %s.\"",
"%",
"(",
"image_shape",
",",
")",
")",
"arr",
"=",
"self",
".",
"draw_points_on_image",
"(",
"np",
".",
"zeros",
"(",
"image_shape",
",",
"dtype",
"=",
"np",
".",
"uint8",
")",
",",
"color",
"=",
"255",
",",
"alpha",
"=",
"alpha",
",",
"size",
"=",
"size",
",",
"raise_if_out_of_image",
"=",
"raise_if_out_of_image",
")",
"return",
"arr",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"/",
"255.0"
] | Draw the points of the line string as a heatmap array.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the point mask.
alpha : float, optional
Opacity of the line string points. Higher values denote a more
visible points.
size : int, optional
Size of the points in pixels.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Float array of shape `image_shape` (no channel axis) with drawn
line string points. All values are in the interval ``[0.0, 1.0]``. | [
"Draw",
"the",
"points",
"of",
"the",
"line",
"string",
"as",
"a",
"heatmap",
"array",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L661-L700 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.draw_heatmap_array | def draw_heatmap_array(self, image_shape, alpha_lines=1.0, alpha_points=1.0,
size_lines=1, size_points=0, antialiased=True,
raise_if_out_of_image=False):
"""
Draw the line segments and points of the line string as a heatmap array.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
alpha_lines : float, optional
Opacity of the line string. Higher values denote a more visible
line string.
alpha_points : float, optional
Opacity of the line string points. Higher values denote a more
visible points.
size_lines : int, optional
Thickness of the line segments.
size_points : int, optional
Size of the points in pixels.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Float array of shape `image_shape` (no channel axis) with drawn
line segments and points. All values are in the
interval ``[0.0, 1.0]``.
"""
heatmap_lines = self.draw_lines_heatmap_array(
image_shape,
alpha=alpha_lines,
size=size_lines,
antialiased=antialiased,
raise_if_out_of_image=raise_if_out_of_image)
if size_points <= 0:
return heatmap_lines
heatmap_points = self.draw_points_heatmap_array(
image_shape,
alpha=alpha_points,
size=size_points,
raise_if_out_of_image=raise_if_out_of_image)
heatmap = np.dstack([heatmap_lines, heatmap_points])
return np.max(heatmap, axis=2) | python | def draw_heatmap_array(self, image_shape, alpha_lines=1.0, alpha_points=1.0,
size_lines=1, size_points=0, antialiased=True,
raise_if_out_of_image=False):
"""
Draw the line segments and points of the line string as a heatmap array.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
alpha_lines : float, optional
Opacity of the line string. Higher values denote a more visible
line string.
alpha_points : float, optional
Opacity of the line string points. Higher values denote a more
visible points.
size_lines : int, optional
Thickness of the line segments.
size_points : int, optional
Size of the points in pixels.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Float array of shape `image_shape` (no channel axis) with drawn
line segments and points. All values are in the
interval ``[0.0, 1.0]``.
"""
heatmap_lines = self.draw_lines_heatmap_array(
image_shape,
alpha=alpha_lines,
size=size_lines,
antialiased=antialiased,
raise_if_out_of_image=raise_if_out_of_image)
if size_points <= 0:
return heatmap_lines
heatmap_points = self.draw_points_heatmap_array(
image_shape,
alpha=alpha_points,
size=size_points,
raise_if_out_of_image=raise_if_out_of_image)
heatmap = np.dstack([heatmap_lines, heatmap_points])
return np.max(heatmap, axis=2) | [
"def",
"draw_heatmap_array",
"(",
"self",
",",
"image_shape",
",",
"alpha_lines",
"=",
"1.0",
",",
"alpha_points",
"=",
"1.0",
",",
"size_lines",
"=",
"1",
",",
"size_points",
"=",
"0",
",",
"antialiased",
"=",
"True",
",",
"raise_if_out_of_image",
"=",
"False",
")",
":",
"heatmap_lines",
"=",
"self",
".",
"draw_lines_heatmap_array",
"(",
"image_shape",
",",
"alpha",
"=",
"alpha_lines",
",",
"size",
"=",
"size_lines",
",",
"antialiased",
"=",
"antialiased",
",",
"raise_if_out_of_image",
"=",
"raise_if_out_of_image",
")",
"if",
"size_points",
"<=",
"0",
":",
"return",
"heatmap_lines",
"heatmap_points",
"=",
"self",
".",
"draw_points_heatmap_array",
"(",
"image_shape",
",",
"alpha",
"=",
"alpha_points",
",",
"size",
"=",
"size_points",
",",
"raise_if_out_of_image",
"=",
"raise_if_out_of_image",
")",
"heatmap",
"=",
"np",
".",
"dstack",
"(",
"[",
"heatmap_lines",
",",
"heatmap_points",
"]",
")",
"return",
"np",
".",
"max",
"(",
"heatmap",
",",
"axis",
"=",
"2",
")"
] | Draw the line segments and points of the line string as a heatmap array.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
alpha_lines : float, optional
Opacity of the line string. Higher values denote a more visible
line string.
alpha_points : float, optional
Opacity of the line string points. Higher values denote a more
visible points.
size_lines : int, optional
Thickness of the line segments.
size_points : int, optional
Size of the points in pixels.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Float array of shape `image_shape` (no channel axis) with drawn
line segments and points. All values are in the
interval ``[0.0, 1.0]``. | [
"Draw",
"the",
"line",
"segments",
"and",
"points",
"of",
"the",
"line",
"string",
"as",
"a",
"heatmap",
"array",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L702-L759 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.draw_lines_on_image | def draw_lines_on_image(self, image, color=(0, 255, 0),
alpha=1.0, size=3,
antialiased=True,
raise_if_out_of_image=False):
"""
Draw the line segments of the line string on a given image.
Parameters
----------
image : ndarray or tuple of int
The image onto which to draw.
Expected to be ``uint8`` and of shape ``(H, W, C)`` with ``C``
usually being ``3`` (other values are not tested).
If a tuple, expected to be ``(H, W, C)`` and will lead to a new
``uint8`` array of zeros being created.
color : int or iterable of int
Color to use as RGB, i.e. three values.
alpha : float, optional
Opacity of the line string. Higher values denote a more visible
line string.
size : int, optional
Thickness of the line segments.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
`image` with line drawn on it.
"""
from .. import dtypes as iadt
from ..augmenters import blend as blendlib
image_was_empty = False
if isinstance(image, tuple):
image_was_empty = True
image = np.zeros(image, dtype=np.uint8)
assert image.ndim in [2, 3], (
("Expected image or shape of form (H,W) or (H,W,C), "
+ "got shape %s.") % (image.shape,))
if len(self.coords) <= 1 or alpha < 0 + 1e-4 or size < 1:
return np.copy(image)
if raise_if_out_of_image \
and self.is_out_of_image(image, partly=False, fully=True):
raise Exception(
"Cannot draw line string '%s' on image with shape %s, because "
"it would be out of bounds." % (
self.__str__(), image.shape))
if image.ndim == 2:
assert ia.is_single_number(color), (
"Got a 2D image. Expected then 'color' to be a single number, "
"but got %s." % (str(color),))
color = [color]
elif image.ndim == 3 and ia.is_single_number(color):
color = [color] * image.shape[-1]
image = image.astype(np.float32)
height, width = image.shape[0:2]
# We can't trivially exclude lines outside of the image here, because
# even if start and end point are outside, there can still be parts of
# the line inside the image.
# TODO Do this with edge-wise intersection tests
lines = []
for line_start, line_end in zip(self.coords[:-1], self.coords[1:]):
# note that line() expects order (y1, x1, y2, x2), hence ([1], [0])
lines.append((line_start[1], line_start[0],
line_end[1], line_end[0]))
# skimage.draw.line can only handle integers
lines = np.round(np.float32(lines)).astype(np.int32)
# size == 0 is already covered above
# Note here that we have to be careful not to draw lines two times
# at their intersection points, e.g. for (p0, p1), (p1, 2) we could
# end up drawing at p1 twice, leading to higher values if alpha is used.
color = np.float32(color)
heatmap = np.zeros(image.shape[0:2], dtype=np.float32)
for line in lines:
if antialiased:
rr, cc, val = skimage.draw.line_aa(*line)
else:
rr, cc = skimage.draw.line(*line)
val = 1.0
# mask check here, because line() can generate coordinates
# outside of the image plane
rr_mask = np.logical_and(0 <= rr, rr < height)
cc_mask = np.logical_and(0 <= cc, cc < width)
mask = np.logical_and(rr_mask, cc_mask)
if np.any(mask):
rr = rr[mask]
cc = cc[mask]
val = val[mask] if not ia.is_single_number(val) else val
heatmap[rr, cc] = val * alpha
if size > 1:
kernel = np.ones((size, size), dtype=np.uint8)
heatmap = cv2.dilate(heatmap, kernel)
if image_was_empty:
image_blend = image + heatmap * color
else:
image_color_shape = image.shape[0:2]
if image.ndim == 3:
image_color_shape = image_color_shape + (1,)
image_color = np.tile(color, image_color_shape)
image_blend = blendlib.blend_alpha(image_color, image, heatmap)
image_blend = iadt.restore_dtypes_(image_blend, np.uint8)
return image_blend | python | def draw_lines_on_image(self, image, color=(0, 255, 0),
alpha=1.0, size=3,
antialiased=True,
raise_if_out_of_image=False):
"""
Draw the line segments of the line string on a given image.
Parameters
----------
image : ndarray or tuple of int
The image onto which to draw.
Expected to be ``uint8`` and of shape ``(H, W, C)`` with ``C``
usually being ``3`` (other values are not tested).
If a tuple, expected to be ``(H, W, C)`` and will lead to a new
``uint8`` array of zeros being created.
color : int or iterable of int
Color to use as RGB, i.e. three values.
alpha : float, optional
Opacity of the line string. Higher values denote a more visible
line string.
size : int, optional
Thickness of the line segments.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
`image` with line drawn on it.
"""
from .. import dtypes as iadt
from ..augmenters import blend as blendlib
image_was_empty = False
if isinstance(image, tuple):
image_was_empty = True
image = np.zeros(image, dtype=np.uint8)
assert image.ndim in [2, 3], (
("Expected image or shape of form (H,W) or (H,W,C), "
+ "got shape %s.") % (image.shape,))
if len(self.coords) <= 1 or alpha < 0 + 1e-4 or size < 1:
return np.copy(image)
if raise_if_out_of_image \
and self.is_out_of_image(image, partly=False, fully=True):
raise Exception(
"Cannot draw line string '%s' on image with shape %s, because "
"it would be out of bounds." % (
self.__str__(), image.shape))
if image.ndim == 2:
assert ia.is_single_number(color), (
"Got a 2D image. Expected then 'color' to be a single number, "
"but got %s." % (str(color),))
color = [color]
elif image.ndim == 3 and ia.is_single_number(color):
color = [color] * image.shape[-1]
image = image.astype(np.float32)
height, width = image.shape[0:2]
# We can't trivially exclude lines outside of the image here, because
# even if start and end point are outside, there can still be parts of
# the line inside the image.
# TODO Do this with edge-wise intersection tests
lines = []
for line_start, line_end in zip(self.coords[:-1], self.coords[1:]):
# note that line() expects order (y1, x1, y2, x2), hence ([1], [0])
lines.append((line_start[1], line_start[0],
line_end[1], line_end[0]))
# skimage.draw.line can only handle integers
lines = np.round(np.float32(lines)).astype(np.int32)
# size == 0 is already covered above
# Note here that we have to be careful not to draw lines two times
# at their intersection points, e.g. for (p0, p1), (p1, 2) we could
# end up drawing at p1 twice, leading to higher values if alpha is used.
color = np.float32(color)
heatmap = np.zeros(image.shape[0:2], dtype=np.float32)
for line in lines:
if antialiased:
rr, cc, val = skimage.draw.line_aa(*line)
else:
rr, cc = skimage.draw.line(*line)
val = 1.0
# mask check here, because line() can generate coordinates
# outside of the image plane
rr_mask = np.logical_and(0 <= rr, rr < height)
cc_mask = np.logical_and(0 <= cc, cc < width)
mask = np.logical_and(rr_mask, cc_mask)
if np.any(mask):
rr = rr[mask]
cc = cc[mask]
val = val[mask] if not ia.is_single_number(val) else val
heatmap[rr, cc] = val * alpha
if size > 1:
kernel = np.ones((size, size), dtype=np.uint8)
heatmap = cv2.dilate(heatmap, kernel)
if image_was_empty:
image_blend = image + heatmap * color
else:
image_color_shape = image.shape[0:2]
if image.ndim == 3:
image_color_shape = image_color_shape + (1,)
image_color = np.tile(color, image_color_shape)
image_blend = blendlib.blend_alpha(image_color, image, heatmap)
image_blend = iadt.restore_dtypes_(image_blend, np.uint8)
return image_blend | [
"def",
"draw_lines_on_image",
"(",
"self",
",",
"image",
",",
"color",
"=",
"(",
"0",
",",
"255",
",",
"0",
")",
",",
"alpha",
"=",
"1.0",
",",
"size",
"=",
"3",
",",
"antialiased",
"=",
"True",
",",
"raise_if_out_of_image",
"=",
"False",
")",
":",
"from",
".",
".",
"import",
"dtypes",
"as",
"iadt",
"from",
".",
".",
"augmenters",
"import",
"blend",
"as",
"blendlib",
"image_was_empty",
"=",
"False",
"if",
"isinstance",
"(",
"image",
",",
"tuple",
")",
":",
"image_was_empty",
"=",
"True",
"image",
"=",
"np",
".",
"zeros",
"(",
"image",
",",
"dtype",
"=",
"np",
".",
"uint8",
")",
"assert",
"image",
".",
"ndim",
"in",
"[",
"2",
",",
"3",
"]",
",",
"(",
"(",
"\"Expected image or shape of form (H,W) or (H,W,C), \"",
"+",
"\"got shape %s.\"",
")",
"%",
"(",
"image",
".",
"shape",
",",
")",
")",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"<=",
"1",
"or",
"alpha",
"<",
"0",
"+",
"1e-4",
"or",
"size",
"<",
"1",
":",
"return",
"np",
".",
"copy",
"(",
"image",
")",
"if",
"raise_if_out_of_image",
"and",
"self",
".",
"is_out_of_image",
"(",
"image",
",",
"partly",
"=",
"False",
",",
"fully",
"=",
"True",
")",
":",
"raise",
"Exception",
"(",
"\"Cannot draw line string '%s' on image with shape %s, because \"",
"\"it would be out of bounds.\"",
"%",
"(",
"self",
".",
"__str__",
"(",
")",
",",
"image",
".",
"shape",
")",
")",
"if",
"image",
".",
"ndim",
"==",
"2",
":",
"assert",
"ia",
".",
"is_single_number",
"(",
"color",
")",
",",
"(",
"\"Got a 2D image. Expected then 'color' to be a single number, \"",
"\"but got %s.\"",
"%",
"(",
"str",
"(",
"color",
")",
",",
")",
")",
"color",
"=",
"[",
"color",
"]",
"elif",
"image",
".",
"ndim",
"==",
"3",
"and",
"ia",
".",
"is_single_number",
"(",
"color",
")",
":",
"color",
"=",
"[",
"color",
"]",
"*",
"image",
".",
"shape",
"[",
"-",
"1",
"]",
"image",
"=",
"image",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"height",
",",
"width",
"=",
"image",
".",
"shape",
"[",
"0",
":",
"2",
"]",
"# We can't trivially exclude lines outside of the image here, because",
"# even if start and end point are outside, there can still be parts of",
"# the line inside the image.",
"# TODO Do this with edge-wise intersection tests",
"lines",
"=",
"[",
"]",
"for",
"line_start",
",",
"line_end",
"in",
"zip",
"(",
"self",
".",
"coords",
"[",
":",
"-",
"1",
"]",
",",
"self",
".",
"coords",
"[",
"1",
":",
"]",
")",
":",
"# note that line() expects order (y1, x1, y2, x2), hence ([1], [0])",
"lines",
".",
"append",
"(",
"(",
"line_start",
"[",
"1",
"]",
",",
"line_start",
"[",
"0",
"]",
",",
"line_end",
"[",
"1",
"]",
",",
"line_end",
"[",
"0",
"]",
")",
")",
"# skimage.draw.line can only handle integers",
"lines",
"=",
"np",
".",
"round",
"(",
"np",
".",
"float32",
"(",
"lines",
")",
")",
".",
"astype",
"(",
"np",
".",
"int32",
")",
"# size == 0 is already covered above",
"# Note here that we have to be careful not to draw lines two times",
"# at their intersection points, e.g. for (p0, p1), (p1, 2) we could",
"# end up drawing at p1 twice, leading to higher values if alpha is used.",
"color",
"=",
"np",
".",
"float32",
"(",
"color",
")",
"heatmap",
"=",
"np",
".",
"zeros",
"(",
"image",
".",
"shape",
"[",
"0",
":",
"2",
"]",
",",
"dtype",
"=",
"np",
".",
"float32",
")",
"for",
"line",
"in",
"lines",
":",
"if",
"antialiased",
":",
"rr",
",",
"cc",
",",
"val",
"=",
"skimage",
".",
"draw",
".",
"line_aa",
"(",
"*",
"line",
")",
"else",
":",
"rr",
",",
"cc",
"=",
"skimage",
".",
"draw",
".",
"line",
"(",
"*",
"line",
")",
"val",
"=",
"1.0",
"# mask check here, because line() can generate coordinates",
"# outside of the image plane",
"rr_mask",
"=",
"np",
".",
"logical_and",
"(",
"0",
"<=",
"rr",
",",
"rr",
"<",
"height",
")",
"cc_mask",
"=",
"np",
".",
"logical_and",
"(",
"0",
"<=",
"cc",
",",
"cc",
"<",
"width",
")",
"mask",
"=",
"np",
".",
"logical_and",
"(",
"rr_mask",
",",
"cc_mask",
")",
"if",
"np",
".",
"any",
"(",
"mask",
")",
":",
"rr",
"=",
"rr",
"[",
"mask",
"]",
"cc",
"=",
"cc",
"[",
"mask",
"]",
"val",
"=",
"val",
"[",
"mask",
"]",
"if",
"not",
"ia",
".",
"is_single_number",
"(",
"val",
")",
"else",
"val",
"heatmap",
"[",
"rr",
",",
"cc",
"]",
"=",
"val",
"*",
"alpha",
"if",
"size",
">",
"1",
":",
"kernel",
"=",
"np",
".",
"ones",
"(",
"(",
"size",
",",
"size",
")",
",",
"dtype",
"=",
"np",
".",
"uint8",
")",
"heatmap",
"=",
"cv2",
".",
"dilate",
"(",
"heatmap",
",",
"kernel",
")",
"if",
"image_was_empty",
":",
"image_blend",
"=",
"image",
"+",
"heatmap",
"*",
"color",
"else",
":",
"image_color_shape",
"=",
"image",
".",
"shape",
"[",
"0",
":",
"2",
"]",
"if",
"image",
".",
"ndim",
"==",
"3",
":",
"image_color_shape",
"=",
"image_color_shape",
"+",
"(",
"1",
",",
")",
"image_color",
"=",
"np",
".",
"tile",
"(",
"color",
",",
"image_color_shape",
")",
"image_blend",
"=",
"blendlib",
".",
"blend_alpha",
"(",
"image_color",
",",
"image",
",",
"heatmap",
")",
"image_blend",
"=",
"iadt",
".",
"restore_dtypes_",
"(",
"image_blend",
",",
"np",
".",
"uint8",
")",
"return",
"image_blend"
] | Draw the line segments of the line string on a given image.
Parameters
----------
image : ndarray or tuple of int
The image onto which to draw.
Expected to be ``uint8`` and of shape ``(H, W, C)`` with ``C``
usually being ``3`` (other values are not tested).
If a tuple, expected to be ``(H, W, C)`` and will lead to a new
``uint8`` array of zeros being created.
color : int or iterable of int
Color to use as RGB, i.e. three values.
alpha : float, optional
Opacity of the line string. Higher values denote a more visible
line string.
size : int, optional
Thickness of the line segments.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
`image` with line drawn on it. | [
"Draw",
"the",
"line",
"segments",
"of",
"the",
"line",
"string",
"on",
"a",
"given",
"image",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L763-L887 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.draw_points_on_image | def draw_points_on_image(self, image, color=(0, 128, 0),
alpha=1.0, size=3,
copy=True, raise_if_out_of_image=False):
"""
Draw the points of the line string on a given image.
Parameters
----------
image : ndarray or tuple of int
The image onto which to draw.
Expected to be ``uint8`` and of shape ``(H, W, C)`` with ``C``
usually being ``3`` (other values are not tested).
If a tuple, expected to be ``(H, W, C)`` and will lead to a new
``uint8`` array of zeros being created.
color : iterable of int
Color to use as RGB, i.e. three values.
alpha : float, optional
Opacity of the line string points. Higher values denote a more
visible points.
size : int, optional
Size of the points in pixels.
copy : bool, optional
Whether it is allowed to draw directly in the input
array (``False``) or it has to be copied (``True``).
The routine may still have to copy, even if ``copy=False`` was
used. Always use the return value.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Float array of shape `image_shape` (no channel axis) with drawn
line string points. All values are in the interval ``[0.0, 1.0]``.
"""
from .kps import KeypointsOnImage
kpsoi = KeypointsOnImage.from_xy_array(self.coords, shape=image.shape)
image = kpsoi.draw_on_image(
image, color=color, alpha=alpha,
size=size, copy=copy,
raise_if_out_of_image=raise_if_out_of_image)
return image | python | def draw_points_on_image(self, image, color=(0, 128, 0),
alpha=1.0, size=3,
copy=True, raise_if_out_of_image=False):
"""
Draw the points of the line string on a given image.
Parameters
----------
image : ndarray or tuple of int
The image onto which to draw.
Expected to be ``uint8`` and of shape ``(H, W, C)`` with ``C``
usually being ``3`` (other values are not tested).
If a tuple, expected to be ``(H, W, C)`` and will lead to a new
``uint8`` array of zeros being created.
color : iterable of int
Color to use as RGB, i.e. three values.
alpha : float, optional
Opacity of the line string points. Higher values denote a more
visible points.
size : int, optional
Size of the points in pixels.
copy : bool, optional
Whether it is allowed to draw directly in the input
array (``False``) or it has to be copied (``True``).
The routine may still have to copy, even if ``copy=False`` was
used. Always use the return value.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Float array of shape `image_shape` (no channel axis) with drawn
line string points. All values are in the interval ``[0.0, 1.0]``.
"""
from .kps import KeypointsOnImage
kpsoi = KeypointsOnImage.from_xy_array(self.coords, shape=image.shape)
image = kpsoi.draw_on_image(
image, color=color, alpha=alpha,
size=size, copy=copy,
raise_if_out_of_image=raise_if_out_of_image)
return image | [
"def",
"draw_points_on_image",
"(",
"self",
",",
"image",
",",
"color",
"=",
"(",
"0",
",",
"128",
",",
"0",
")",
",",
"alpha",
"=",
"1.0",
",",
"size",
"=",
"3",
",",
"copy",
"=",
"True",
",",
"raise_if_out_of_image",
"=",
"False",
")",
":",
"from",
".",
"kps",
"import",
"KeypointsOnImage",
"kpsoi",
"=",
"KeypointsOnImage",
".",
"from_xy_array",
"(",
"self",
".",
"coords",
",",
"shape",
"=",
"image",
".",
"shape",
")",
"image",
"=",
"kpsoi",
".",
"draw_on_image",
"(",
"image",
",",
"color",
"=",
"color",
",",
"alpha",
"=",
"alpha",
",",
"size",
"=",
"size",
",",
"copy",
"=",
"copy",
",",
"raise_if_out_of_image",
"=",
"raise_if_out_of_image",
")",
"return",
"image"
] | Draw the points of the line string on a given image.
Parameters
----------
image : ndarray or tuple of int
The image onto which to draw.
Expected to be ``uint8`` and of shape ``(H, W, C)`` with ``C``
usually being ``3`` (other values are not tested).
If a tuple, expected to be ``(H, W, C)`` and will lead to a new
``uint8`` array of zeros being created.
color : iterable of int
Color to use as RGB, i.e. three values.
alpha : float, optional
Opacity of the line string points. Higher values denote a more
visible points.
size : int, optional
Size of the points in pixels.
copy : bool, optional
Whether it is allowed to draw directly in the input
array (``False``) or it has to be copied (``True``).
The routine may still have to copy, even if ``copy=False`` was
used. Always use the return value.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Float array of shape `image_shape` (no channel axis) with drawn
line string points. All values are in the interval ``[0.0, 1.0]``. | [
"Draw",
"the",
"points",
"of",
"the",
"line",
"string",
"on",
"a",
"given",
"image",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L889-L939 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.draw_on_image | def draw_on_image(self, image,
color=(0, 255, 0), color_lines=None, color_points=None,
alpha=1.0, alpha_lines=None, alpha_points=None,
size=1, size_lines=None, size_points=None,
antialiased=True,
raise_if_out_of_image=False):
"""
Draw the line string on an image.
Parameters
----------
image : ndarray
The `(H,W,C)` `uint8` image onto which to draw the line string.
color : iterable of int, optional
Color to use as RGB, i.e. three values.
The color of the line and points are derived from this value,
unless they are set.
color_lines : None or iterable of int
Color to use for the line segments as RGB, i.e. three values.
If ``None``, this value is derived from `color`.
color_points : None or iterable of int
Color to use for the points as RGB, i.e. three values.
If ``None``, this value is derived from ``0.5 * color``.
alpha : float, optional
Opacity of the line string. Higher values denote more visible
points.
The alphas of the line and points are derived from this value,
unless they are set.
alpha_lines : None or float, optional
Opacity of the line string. Higher values denote more visible
line string.
If ``None``, this value is derived from `alpha`.
alpha_points : None or float, optional
Opacity of the line string points. Higher values denote more
visible points.
If ``None``, this value is derived from `alpha`.
size : int, optional
Size of the line string.
The sizes of the line and points are derived from this value,
unless they are set.
size_lines : None or int, optional
Thickness of the line segments.
If ``None``, this value is derived from `size`.
size_points : None or int, optional
Size of the points in pixels.
If ``None``, this value is derived from ``3 * size``.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
This does currently not affect the point drawing.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Image with line string drawn on it.
"""
assert color is not None
assert alpha is not None
assert size is not None
color_lines = color_lines if color_lines is not None \
else np.float32(color)
color_points = color_points if color_points is not None \
else np.float32(color) * 0.5
alpha_lines = alpha_lines if alpha_lines is not None \
else np.float32(alpha)
alpha_points = alpha_points if alpha_points is not None \
else np.float32(alpha)
size_lines = size_lines if size_lines is not None else size
size_points = size_points if size_points is not None else size * 3
image = self.draw_lines_on_image(
image, color=np.array(color_lines).astype(np.uint8),
alpha=alpha_lines, size=size_lines,
antialiased=antialiased,
raise_if_out_of_image=raise_if_out_of_image)
image = self.draw_points_on_image(
image, color=np.array(color_points).astype(np.uint8),
alpha=alpha_points, size=size_points,
copy=False,
raise_if_out_of_image=raise_if_out_of_image)
return image | python | def draw_on_image(self, image,
color=(0, 255, 0), color_lines=None, color_points=None,
alpha=1.0, alpha_lines=None, alpha_points=None,
size=1, size_lines=None, size_points=None,
antialiased=True,
raise_if_out_of_image=False):
"""
Draw the line string on an image.
Parameters
----------
image : ndarray
The `(H,W,C)` `uint8` image onto which to draw the line string.
color : iterable of int, optional
Color to use as RGB, i.e. three values.
The color of the line and points are derived from this value,
unless they are set.
color_lines : None or iterable of int
Color to use for the line segments as RGB, i.e. three values.
If ``None``, this value is derived from `color`.
color_points : None or iterable of int
Color to use for the points as RGB, i.e. three values.
If ``None``, this value is derived from ``0.5 * color``.
alpha : float, optional
Opacity of the line string. Higher values denote more visible
points.
The alphas of the line and points are derived from this value,
unless they are set.
alpha_lines : None or float, optional
Opacity of the line string. Higher values denote more visible
line string.
If ``None``, this value is derived from `alpha`.
alpha_points : None or float, optional
Opacity of the line string points. Higher values denote more
visible points.
If ``None``, this value is derived from `alpha`.
size : int, optional
Size of the line string.
The sizes of the line and points are derived from this value,
unless they are set.
size_lines : None or int, optional
Thickness of the line segments.
If ``None``, this value is derived from `size`.
size_points : None or int, optional
Size of the points in pixels.
If ``None``, this value is derived from ``3 * size``.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
This does currently not affect the point drawing.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Image with line string drawn on it.
"""
assert color is not None
assert alpha is not None
assert size is not None
color_lines = color_lines if color_lines is not None \
else np.float32(color)
color_points = color_points if color_points is not None \
else np.float32(color) * 0.5
alpha_lines = alpha_lines if alpha_lines is not None \
else np.float32(alpha)
alpha_points = alpha_points if alpha_points is not None \
else np.float32(alpha)
size_lines = size_lines if size_lines is not None else size
size_points = size_points if size_points is not None else size * 3
image = self.draw_lines_on_image(
image, color=np.array(color_lines).astype(np.uint8),
alpha=alpha_lines, size=size_lines,
antialiased=antialiased,
raise_if_out_of_image=raise_if_out_of_image)
image = self.draw_points_on_image(
image, color=np.array(color_points).astype(np.uint8),
alpha=alpha_points, size=size_points,
copy=False,
raise_if_out_of_image=raise_if_out_of_image)
return image | [
"def",
"draw_on_image",
"(",
"self",
",",
"image",
",",
"color",
"=",
"(",
"0",
",",
"255",
",",
"0",
")",
",",
"color_lines",
"=",
"None",
",",
"color_points",
"=",
"None",
",",
"alpha",
"=",
"1.0",
",",
"alpha_lines",
"=",
"None",
",",
"alpha_points",
"=",
"None",
",",
"size",
"=",
"1",
",",
"size_lines",
"=",
"None",
",",
"size_points",
"=",
"None",
",",
"antialiased",
"=",
"True",
",",
"raise_if_out_of_image",
"=",
"False",
")",
":",
"assert",
"color",
"is",
"not",
"None",
"assert",
"alpha",
"is",
"not",
"None",
"assert",
"size",
"is",
"not",
"None",
"color_lines",
"=",
"color_lines",
"if",
"color_lines",
"is",
"not",
"None",
"else",
"np",
".",
"float32",
"(",
"color",
")",
"color_points",
"=",
"color_points",
"if",
"color_points",
"is",
"not",
"None",
"else",
"np",
".",
"float32",
"(",
"color",
")",
"*",
"0.5",
"alpha_lines",
"=",
"alpha_lines",
"if",
"alpha_lines",
"is",
"not",
"None",
"else",
"np",
".",
"float32",
"(",
"alpha",
")",
"alpha_points",
"=",
"alpha_points",
"if",
"alpha_points",
"is",
"not",
"None",
"else",
"np",
".",
"float32",
"(",
"alpha",
")",
"size_lines",
"=",
"size_lines",
"if",
"size_lines",
"is",
"not",
"None",
"else",
"size",
"size_points",
"=",
"size_points",
"if",
"size_points",
"is",
"not",
"None",
"else",
"size",
"*",
"3",
"image",
"=",
"self",
".",
"draw_lines_on_image",
"(",
"image",
",",
"color",
"=",
"np",
".",
"array",
"(",
"color_lines",
")",
".",
"astype",
"(",
"np",
".",
"uint8",
")",
",",
"alpha",
"=",
"alpha_lines",
",",
"size",
"=",
"size_lines",
",",
"antialiased",
"=",
"antialiased",
",",
"raise_if_out_of_image",
"=",
"raise_if_out_of_image",
")",
"image",
"=",
"self",
".",
"draw_points_on_image",
"(",
"image",
",",
"color",
"=",
"np",
".",
"array",
"(",
"color_points",
")",
".",
"astype",
"(",
"np",
".",
"uint8",
")",
",",
"alpha",
"=",
"alpha_points",
",",
"size",
"=",
"size_points",
",",
"copy",
"=",
"False",
",",
"raise_if_out_of_image",
"=",
"raise_if_out_of_image",
")",
"return",
"image"
] | Draw the line string on an image.
Parameters
----------
image : ndarray
The `(H,W,C)` `uint8` image onto which to draw the line string.
color : iterable of int, optional
Color to use as RGB, i.e. three values.
The color of the line and points are derived from this value,
unless they are set.
color_lines : None or iterable of int
Color to use for the line segments as RGB, i.e. three values.
If ``None``, this value is derived from `color`.
color_points : None or iterable of int
Color to use for the points as RGB, i.e. three values.
If ``None``, this value is derived from ``0.5 * color``.
alpha : float, optional
Opacity of the line string. Higher values denote more visible
points.
The alphas of the line and points are derived from this value,
unless they are set.
alpha_lines : None or float, optional
Opacity of the line string. Higher values denote more visible
line string.
If ``None``, this value is derived from `alpha`.
alpha_points : None or float, optional
Opacity of the line string points. Higher values denote more
visible points.
If ``None``, this value is derived from `alpha`.
size : int, optional
Size of the line string.
The sizes of the line and points are derived from this value,
unless they are set.
size_lines : None or int, optional
Thickness of the line segments.
If ``None``, this value is derived from `size`.
size_points : None or int, optional
Size of the points in pixels.
If ``None``, this value is derived from ``3 * size``.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
This does currently not affect the point drawing.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
ndarray
Image with line string drawn on it. | [
"Draw",
"the",
"line",
"string",
"on",
"an",
"image",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L941-L1041 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.extract_from_image | def extract_from_image(self, image, size=1, pad=True, pad_max=None,
antialiased=True, prevent_zero_size=True):
"""
Extract the image pixels covered by the line string.
It will only extract pixels overlapped by the line string.
This function will by default zero-pad the image if the line string is
partially/fully outside of the image. This is for consistency with
the same implementations for bounding boxes and polygons.
Parameters
----------
image : ndarray
The image of shape `(H,W,[C])` from which to extract the pixels
within the line string.
size : int, optional
Thickness of the line.
pad : bool, optional
Whether to zero-pad the image if the object is partially/fully
outside of it.
pad_max : None or int, optional
The maximum number of pixels that may be zero-paded on any side,
i.e. if this has value ``N`` the total maximum of added pixels
is ``4*N``.
This option exists to prevent extremely large images as a result of
single points being moved very far away during augmentation.
antialiased : bool, optional
Whether to apply anti-aliasing to the line string.
prevent_zero_size : bool, optional
Whether to prevent height or width of the extracted image from
becoming zero. If this is set to True and height or width of the
line string is below 1, the height/width will be increased to 1.
This can be useful to prevent problems, e.g. with image saving or
plotting. If it is set to False, images will be returned as
``(H', W')`` or ``(H', W', 3)`` with ``H`` or ``W`` potentially
being 0.
Returns
-------
image : (H',W') ndarray or (H',W',C) ndarray
Pixels overlapping with the line string. Zero-padded if the
line string is partially/fully outside of the image and
``pad=True``. If `prevent_zero_size` is activated, it is
guarantueed that ``H'>0`` and ``W'>0``, otherwise only
``H'>=0`` and ``W'>=0``.
"""
from .bbs import BoundingBox
assert image.ndim in [2, 3], (
"Expected image of shape (H,W,[C]), "
"got shape %s." % (image.shape,))
if len(self.coords) == 0 or size <= 0:
if prevent_zero_size:
return np.zeros((1, 1) + image.shape[2:], dtype=image.dtype)
return np.zeros((0, 0) + image.shape[2:], dtype=image.dtype)
xx = self.xx_int
yy = self.yy_int
# this would probably work if drawing was subpixel-accurate
# x1 = np.min(self.coords[:, 0]) - (size / 2)
# y1 = np.min(self.coords[:, 1]) - (size / 2)
# x2 = np.max(self.coords[:, 0]) + (size / 2)
# y2 = np.max(self.coords[:, 1]) + (size / 2)
# this works currently with non-subpixel-accurate drawing
sizeh = (size - 1) / 2
x1 = np.min(xx) - sizeh
y1 = np.min(yy) - sizeh
x2 = np.max(xx) + 1 + sizeh
y2 = np.max(yy) + 1 + sizeh
bb = BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2)
if len(self.coords) == 1:
return bb.extract_from_image(image, pad=pad, pad_max=pad_max,
prevent_zero_size=prevent_zero_size)
heatmap = self.draw_lines_heatmap_array(
image.shape[0:2], alpha=1.0, size=size, antialiased=antialiased)
if image.ndim == 3:
heatmap = np.atleast_3d(heatmap)
image_masked = image.astype(np.float32) * heatmap
extract = bb.extract_from_image(image_masked, pad=pad, pad_max=pad_max,
prevent_zero_size=prevent_zero_size)
return np.clip(np.round(extract), 0, 255).astype(np.uint8) | python | def extract_from_image(self, image, size=1, pad=True, pad_max=None,
antialiased=True, prevent_zero_size=True):
"""
Extract the image pixels covered by the line string.
It will only extract pixels overlapped by the line string.
This function will by default zero-pad the image if the line string is
partially/fully outside of the image. This is for consistency with
the same implementations for bounding boxes and polygons.
Parameters
----------
image : ndarray
The image of shape `(H,W,[C])` from which to extract the pixels
within the line string.
size : int, optional
Thickness of the line.
pad : bool, optional
Whether to zero-pad the image if the object is partially/fully
outside of it.
pad_max : None or int, optional
The maximum number of pixels that may be zero-paded on any side,
i.e. if this has value ``N`` the total maximum of added pixels
is ``4*N``.
This option exists to prevent extremely large images as a result of
single points being moved very far away during augmentation.
antialiased : bool, optional
Whether to apply anti-aliasing to the line string.
prevent_zero_size : bool, optional
Whether to prevent height or width of the extracted image from
becoming zero. If this is set to True and height or width of the
line string is below 1, the height/width will be increased to 1.
This can be useful to prevent problems, e.g. with image saving or
plotting. If it is set to False, images will be returned as
``(H', W')`` or ``(H', W', 3)`` with ``H`` or ``W`` potentially
being 0.
Returns
-------
image : (H',W') ndarray or (H',W',C) ndarray
Pixels overlapping with the line string. Zero-padded if the
line string is partially/fully outside of the image and
``pad=True``. If `prevent_zero_size` is activated, it is
guarantueed that ``H'>0`` and ``W'>0``, otherwise only
``H'>=0`` and ``W'>=0``.
"""
from .bbs import BoundingBox
assert image.ndim in [2, 3], (
"Expected image of shape (H,W,[C]), "
"got shape %s." % (image.shape,))
if len(self.coords) == 0 or size <= 0:
if prevent_zero_size:
return np.zeros((1, 1) + image.shape[2:], dtype=image.dtype)
return np.zeros((0, 0) + image.shape[2:], dtype=image.dtype)
xx = self.xx_int
yy = self.yy_int
# this would probably work if drawing was subpixel-accurate
# x1 = np.min(self.coords[:, 0]) - (size / 2)
# y1 = np.min(self.coords[:, 1]) - (size / 2)
# x2 = np.max(self.coords[:, 0]) + (size / 2)
# y2 = np.max(self.coords[:, 1]) + (size / 2)
# this works currently with non-subpixel-accurate drawing
sizeh = (size - 1) / 2
x1 = np.min(xx) - sizeh
y1 = np.min(yy) - sizeh
x2 = np.max(xx) + 1 + sizeh
y2 = np.max(yy) + 1 + sizeh
bb = BoundingBox(x1=x1, y1=y1, x2=x2, y2=y2)
if len(self.coords) == 1:
return bb.extract_from_image(image, pad=pad, pad_max=pad_max,
prevent_zero_size=prevent_zero_size)
heatmap = self.draw_lines_heatmap_array(
image.shape[0:2], alpha=1.0, size=size, antialiased=antialiased)
if image.ndim == 3:
heatmap = np.atleast_3d(heatmap)
image_masked = image.astype(np.float32) * heatmap
extract = bb.extract_from_image(image_masked, pad=pad, pad_max=pad_max,
prevent_zero_size=prevent_zero_size)
return np.clip(np.round(extract), 0, 255).astype(np.uint8) | [
"def",
"extract_from_image",
"(",
"self",
",",
"image",
",",
"size",
"=",
"1",
",",
"pad",
"=",
"True",
",",
"pad_max",
"=",
"None",
",",
"antialiased",
"=",
"True",
",",
"prevent_zero_size",
"=",
"True",
")",
":",
"from",
".",
"bbs",
"import",
"BoundingBox",
"assert",
"image",
".",
"ndim",
"in",
"[",
"2",
",",
"3",
"]",
",",
"(",
"\"Expected image of shape (H,W,[C]), \"",
"\"got shape %s.\"",
"%",
"(",
"image",
".",
"shape",
",",
")",
")",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"==",
"0",
"or",
"size",
"<=",
"0",
":",
"if",
"prevent_zero_size",
":",
"return",
"np",
".",
"zeros",
"(",
"(",
"1",
",",
"1",
")",
"+",
"image",
".",
"shape",
"[",
"2",
":",
"]",
",",
"dtype",
"=",
"image",
".",
"dtype",
")",
"return",
"np",
".",
"zeros",
"(",
"(",
"0",
",",
"0",
")",
"+",
"image",
".",
"shape",
"[",
"2",
":",
"]",
",",
"dtype",
"=",
"image",
".",
"dtype",
")",
"xx",
"=",
"self",
".",
"xx_int",
"yy",
"=",
"self",
".",
"yy_int",
"# this would probably work if drawing was subpixel-accurate",
"# x1 = np.min(self.coords[:, 0]) - (size / 2)",
"# y1 = np.min(self.coords[:, 1]) - (size / 2)",
"# x2 = np.max(self.coords[:, 0]) + (size / 2)",
"# y2 = np.max(self.coords[:, 1]) + (size / 2)",
"# this works currently with non-subpixel-accurate drawing",
"sizeh",
"=",
"(",
"size",
"-",
"1",
")",
"/",
"2",
"x1",
"=",
"np",
".",
"min",
"(",
"xx",
")",
"-",
"sizeh",
"y1",
"=",
"np",
".",
"min",
"(",
"yy",
")",
"-",
"sizeh",
"x2",
"=",
"np",
".",
"max",
"(",
"xx",
")",
"+",
"1",
"+",
"sizeh",
"y2",
"=",
"np",
".",
"max",
"(",
"yy",
")",
"+",
"1",
"+",
"sizeh",
"bb",
"=",
"BoundingBox",
"(",
"x1",
"=",
"x1",
",",
"y1",
"=",
"y1",
",",
"x2",
"=",
"x2",
",",
"y2",
"=",
"y2",
")",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"==",
"1",
":",
"return",
"bb",
".",
"extract_from_image",
"(",
"image",
",",
"pad",
"=",
"pad",
",",
"pad_max",
"=",
"pad_max",
",",
"prevent_zero_size",
"=",
"prevent_zero_size",
")",
"heatmap",
"=",
"self",
".",
"draw_lines_heatmap_array",
"(",
"image",
".",
"shape",
"[",
"0",
":",
"2",
"]",
",",
"alpha",
"=",
"1.0",
",",
"size",
"=",
"size",
",",
"antialiased",
"=",
"antialiased",
")",
"if",
"image",
".",
"ndim",
"==",
"3",
":",
"heatmap",
"=",
"np",
".",
"atleast_3d",
"(",
"heatmap",
")",
"image_masked",
"=",
"image",
".",
"astype",
"(",
"np",
".",
"float32",
")",
"*",
"heatmap",
"extract",
"=",
"bb",
".",
"extract_from_image",
"(",
"image_masked",
",",
"pad",
"=",
"pad",
",",
"pad_max",
"=",
"pad_max",
",",
"prevent_zero_size",
"=",
"prevent_zero_size",
")",
"return",
"np",
".",
"clip",
"(",
"np",
".",
"round",
"(",
"extract",
")",
",",
"0",
",",
"255",
")",
".",
"astype",
"(",
"np",
".",
"uint8",
")"
] | Extract the image pixels covered by the line string.
It will only extract pixels overlapped by the line string.
This function will by default zero-pad the image if the line string is
partially/fully outside of the image. This is for consistency with
the same implementations for bounding boxes and polygons.
Parameters
----------
image : ndarray
The image of shape `(H,W,[C])` from which to extract the pixels
within the line string.
size : int, optional
Thickness of the line.
pad : bool, optional
Whether to zero-pad the image if the object is partially/fully
outside of it.
pad_max : None or int, optional
The maximum number of pixels that may be zero-paded on any side,
i.e. if this has value ``N`` the total maximum of added pixels
is ``4*N``.
This option exists to prevent extremely large images as a result of
single points being moved very far away during augmentation.
antialiased : bool, optional
Whether to apply anti-aliasing to the line string.
prevent_zero_size : bool, optional
Whether to prevent height or width of the extracted image from
becoming zero. If this is set to True and height or width of the
line string is below 1, the height/width will be increased to 1.
This can be useful to prevent problems, e.g. with image saving or
plotting. If it is set to False, images will be returned as
``(H', W')`` or ``(H', W', 3)`` with ``H`` or ``W`` potentially
being 0.
Returns
-------
image : (H',W') ndarray or (H',W',C) ndarray
Pixels overlapping with the line string. Zero-padded if the
line string is partially/fully outside of the image and
``pad=True``. If `prevent_zero_size` is activated, it is
guarantueed that ``H'>0`` and ``W'>0``, otherwise only
``H'>=0`` and ``W'>=0``. | [
"Extract",
"the",
"image",
"pixels",
"covered",
"by",
"the",
"line",
"string",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L1043-L1135 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.concatenate | def concatenate(self, other):
"""
Concatenate this line string with another one.
This will add a line segment between the end point of this line string
and the start point of `other`.
Parameters
----------
other : imgaug.augmentables.lines.LineString or ndarray \
or iterable of tuple of number
The points to add to this line string.
Returns
-------
imgaug.augmentables.lines.LineString
New line string with concatenated points.
The `label` of this line string will be kept.
"""
if not isinstance(other, LineString):
other = LineString(other)
return self.deepcopy(
coords=np.concatenate([self.coords, other.coords], axis=0)) | python | def concatenate(self, other):
"""
Concatenate this line string with another one.
This will add a line segment between the end point of this line string
and the start point of `other`.
Parameters
----------
other : imgaug.augmentables.lines.LineString or ndarray \
or iterable of tuple of number
The points to add to this line string.
Returns
-------
imgaug.augmentables.lines.LineString
New line string with concatenated points.
The `label` of this line string will be kept.
"""
if not isinstance(other, LineString):
other = LineString(other)
return self.deepcopy(
coords=np.concatenate([self.coords, other.coords], axis=0)) | [
"def",
"concatenate",
"(",
"self",
",",
"other",
")",
":",
"if",
"not",
"isinstance",
"(",
"other",
",",
"LineString",
")",
":",
"other",
"=",
"LineString",
"(",
"other",
")",
"return",
"self",
".",
"deepcopy",
"(",
"coords",
"=",
"np",
".",
"concatenate",
"(",
"[",
"self",
".",
"coords",
",",
"other",
".",
"coords",
"]",
",",
"axis",
"=",
"0",
")",
")"
] | Concatenate this line string with another one.
This will add a line segment between the end point of this line string
and the start point of `other`.
Parameters
----------
other : imgaug.augmentables.lines.LineString or ndarray \
or iterable of tuple of number
The points to add to this line string.
Returns
-------
imgaug.augmentables.lines.LineString
New line string with concatenated points.
The `label` of this line string will be kept. | [
"Concatenate",
"this",
"line",
"string",
"with",
"another",
"one",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L1137-L1160 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.subdivide | def subdivide(self, points_per_edge):
"""
Adds ``N`` interpolated points with uniform spacing to each edge.
For each edge between points ``A`` and ``B`` this adds points
at ``A + (i/(1+N)) * (B - A)``, where ``i`` is the index of the added
point and ``N`` is the number of points to add per edge.
Calling this method two times will split each edge at its center
and then again split each newly created edge at their center.
It is equivalent to calling `subdivide(3)`.
Parameters
----------
points_per_edge : int
Number of points to interpolate on each edge.
Returns
-------
LineString
Line string with subdivided edges.
"""
if len(self.coords) <= 1 or points_per_edge < 1:
return self.deepcopy()
coords = interpolate_points(self.coords, nb_steps=points_per_edge,
closed=False)
return self.deepcopy(coords=coords) | python | def subdivide(self, points_per_edge):
"""
Adds ``N`` interpolated points with uniform spacing to each edge.
For each edge between points ``A`` and ``B`` this adds points
at ``A + (i/(1+N)) * (B - A)``, where ``i`` is the index of the added
point and ``N`` is the number of points to add per edge.
Calling this method two times will split each edge at its center
and then again split each newly created edge at their center.
It is equivalent to calling `subdivide(3)`.
Parameters
----------
points_per_edge : int
Number of points to interpolate on each edge.
Returns
-------
LineString
Line string with subdivided edges.
"""
if len(self.coords) <= 1 or points_per_edge < 1:
return self.deepcopy()
coords = interpolate_points(self.coords, nb_steps=points_per_edge,
closed=False)
return self.deepcopy(coords=coords) | [
"def",
"subdivide",
"(",
"self",
",",
"points_per_edge",
")",
":",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"<=",
"1",
"or",
"points_per_edge",
"<",
"1",
":",
"return",
"self",
".",
"deepcopy",
"(",
")",
"coords",
"=",
"interpolate_points",
"(",
"self",
".",
"coords",
",",
"nb_steps",
"=",
"points_per_edge",
",",
"closed",
"=",
"False",
")",
"return",
"self",
".",
"deepcopy",
"(",
"coords",
"=",
"coords",
")"
] | Adds ``N`` interpolated points with uniform spacing to each edge.
For each edge between points ``A`` and ``B`` this adds points
at ``A + (i/(1+N)) * (B - A)``, where ``i`` is the index of the added
point and ``N`` is the number of points to add per edge.
Calling this method two times will split each edge at its center
and then again split each newly created edge at their center.
It is equivalent to calling `subdivide(3)`.
Parameters
----------
points_per_edge : int
Number of points to interpolate on each edge.
Returns
-------
LineString
Line string with subdivided edges. | [
"Adds",
"N",
"interpolated",
"points",
"with",
"uniform",
"spacing",
"to",
"each",
"edge",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L1163-L1190 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.to_keypoints | def to_keypoints(self):
"""
Convert the line string points to keypoints.
Returns
-------
list of imgaug.augmentables.kps.Keypoint
Points of the line string as keypoints.
"""
# TODO get rid of this deferred import
from imgaug.augmentables.kps import Keypoint
return [Keypoint(x=x, y=y) for (x, y) in self.coords] | python | def to_keypoints(self):
"""
Convert the line string points to keypoints.
Returns
-------
list of imgaug.augmentables.kps.Keypoint
Points of the line string as keypoints.
"""
# TODO get rid of this deferred import
from imgaug.augmentables.kps import Keypoint
return [Keypoint(x=x, y=y) for (x, y) in self.coords] | [
"def",
"to_keypoints",
"(",
"self",
")",
":",
"# TODO get rid of this deferred import",
"from",
"imgaug",
".",
"augmentables",
".",
"kps",
"import",
"Keypoint",
"return",
"[",
"Keypoint",
"(",
"x",
"=",
"x",
",",
"y",
"=",
"y",
")",
"for",
"(",
"x",
",",
"y",
")",
"in",
"self",
".",
"coords",
"]"
] | Convert the line string points to keypoints.
Returns
-------
list of imgaug.augmentables.kps.Keypoint
Points of the line string as keypoints. | [
"Convert",
"the",
"line",
"string",
"points",
"to",
"keypoints",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L1192-L1204 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.to_bounding_box | def to_bounding_box(self):
"""
Generate a bounding box encapsulating the line string.
Returns
-------
None or imgaug.augmentables.bbs.BoundingBox
Bounding box encapsulating the line string.
``None`` if the line string contained no points.
"""
from .bbs import BoundingBox
# we don't have to mind the case of len(.) == 1 here, because
# zero-sized BBs are considered valid
if len(self.coords) == 0:
return None
return BoundingBox(x1=np.min(self.xx), y1=np.min(self.yy),
x2=np.max(self.xx), y2=np.max(self.yy),
label=self.label) | python | def to_bounding_box(self):
"""
Generate a bounding box encapsulating the line string.
Returns
-------
None or imgaug.augmentables.bbs.BoundingBox
Bounding box encapsulating the line string.
``None`` if the line string contained no points.
"""
from .bbs import BoundingBox
# we don't have to mind the case of len(.) == 1 here, because
# zero-sized BBs are considered valid
if len(self.coords) == 0:
return None
return BoundingBox(x1=np.min(self.xx), y1=np.min(self.yy),
x2=np.max(self.xx), y2=np.max(self.yy),
label=self.label) | [
"def",
"to_bounding_box",
"(",
"self",
")",
":",
"from",
".",
"bbs",
"import",
"BoundingBox",
"# we don't have to mind the case of len(.) == 1 here, because",
"# zero-sized BBs are considered valid",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"==",
"0",
":",
"return",
"None",
"return",
"BoundingBox",
"(",
"x1",
"=",
"np",
".",
"min",
"(",
"self",
".",
"xx",
")",
",",
"y1",
"=",
"np",
".",
"min",
"(",
"self",
".",
"yy",
")",
",",
"x2",
"=",
"np",
".",
"max",
"(",
"self",
".",
"xx",
")",
",",
"y2",
"=",
"np",
".",
"max",
"(",
"self",
".",
"yy",
")",
",",
"label",
"=",
"self",
".",
"label",
")"
] | Generate a bounding box encapsulating the line string.
Returns
-------
None or imgaug.augmentables.bbs.BoundingBox
Bounding box encapsulating the line string.
``None`` if the line string contained no points. | [
"Generate",
"a",
"bounding",
"box",
"encapsulating",
"the",
"line",
"string",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L1206-L1224 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.to_polygon | def to_polygon(self):
"""
Generate a polygon from the line string points.
Returns
-------
imgaug.augmentables.polys.Polygon
Polygon with the same corner points as the line string.
Note that the polygon might be invalid, e.g. contain less than 3
points or have self-intersections.
"""
from .polys import Polygon
return Polygon(self.coords, label=self.label) | python | def to_polygon(self):
"""
Generate a polygon from the line string points.
Returns
-------
imgaug.augmentables.polys.Polygon
Polygon with the same corner points as the line string.
Note that the polygon might be invalid, e.g. contain less than 3
points or have self-intersections.
"""
from .polys import Polygon
return Polygon(self.coords, label=self.label) | [
"def",
"to_polygon",
"(",
"self",
")",
":",
"from",
".",
"polys",
"import",
"Polygon",
"return",
"Polygon",
"(",
"self",
".",
"coords",
",",
"label",
"=",
"self",
".",
"label",
")"
] | Generate a polygon from the line string points.
Returns
-------
imgaug.augmentables.polys.Polygon
Polygon with the same corner points as the line string.
Note that the polygon might be invalid, e.g. contain less than 3
points or have self-intersections. | [
"Generate",
"a",
"polygon",
"from",
"the",
"line",
"string",
"points",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L1226-L1239 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.to_heatmap | def to_heatmap(self, image_shape, size_lines=1, size_points=0,
antialiased=True, raise_if_out_of_image=False):
"""
Generate a heatmap object from the line string.
This is similar to
:func:`imgaug.augmentables.lines.LineString.draw_lines_heatmap_array`
executed with ``alpha=1.0``. The result is wrapped in a
``HeatmapsOnImage`` object instead of just an array.
No points are drawn.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
size_lines : int, optional
Thickness of the line.
size_points : int, optional
Size of the points in pixels.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
imgaug.augmentables.heatmaps.HeatmapOnImage
Heatmap object containing drawn line string.
"""
from .heatmaps import HeatmapsOnImage
return HeatmapsOnImage(
self.draw_heatmap_array(
image_shape, size_lines=size_lines, size_points=size_points,
antialiased=antialiased,
raise_if_out_of_image=raise_if_out_of_image),
shape=image_shape
) | python | def to_heatmap(self, image_shape, size_lines=1, size_points=0,
antialiased=True, raise_if_out_of_image=False):
"""
Generate a heatmap object from the line string.
This is similar to
:func:`imgaug.augmentables.lines.LineString.draw_lines_heatmap_array`
executed with ``alpha=1.0``. The result is wrapped in a
``HeatmapsOnImage`` object instead of just an array.
No points are drawn.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
size_lines : int, optional
Thickness of the line.
size_points : int, optional
Size of the points in pixels.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
imgaug.augmentables.heatmaps.HeatmapOnImage
Heatmap object containing drawn line string.
"""
from .heatmaps import HeatmapsOnImage
return HeatmapsOnImage(
self.draw_heatmap_array(
image_shape, size_lines=size_lines, size_points=size_points,
antialiased=antialiased,
raise_if_out_of_image=raise_if_out_of_image),
shape=image_shape
) | [
"def",
"to_heatmap",
"(",
"self",
",",
"image_shape",
",",
"size_lines",
"=",
"1",
",",
"size_points",
"=",
"0",
",",
"antialiased",
"=",
"True",
",",
"raise_if_out_of_image",
"=",
"False",
")",
":",
"from",
".",
"heatmaps",
"import",
"HeatmapsOnImage",
"return",
"HeatmapsOnImage",
"(",
"self",
".",
"draw_heatmap_array",
"(",
"image_shape",
",",
"size_lines",
"=",
"size_lines",
",",
"size_points",
"=",
"size_points",
",",
"antialiased",
"=",
"antialiased",
",",
"raise_if_out_of_image",
"=",
"raise_if_out_of_image",
")",
",",
"shape",
"=",
"image_shape",
")"
] | Generate a heatmap object from the line string.
This is similar to
:func:`imgaug.augmentables.lines.LineString.draw_lines_heatmap_array`
executed with ``alpha=1.0``. The result is wrapped in a
``HeatmapsOnImage`` object instead of just an array.
No points are drawn.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
size_lines : int, optional
Thickness of the line.
size_points : int, optional
Size of the points in pixels.
antialiased : bool, optional
Whether to draw the line with anti-aliasing activated.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
imgaug.augmentables.heatmaps.HeatmapOnImage
Heatmap object containing drawn line string. | [
"Generate",
"a",
"heatmap",
"object",
"from",
"the",
"line",
"string",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L1241-L1284 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.to_segmentation_map | def to_segmentation_map(self, image_shape, size_lines=1, size_points=0,
raise_if_out_of_image=False):
"""
Generate a segmentation map object from the line string.
This is similar to
:func:`imgaug.augmentables.lines.LineString.draw_mask`.
The result is wrapped in a ``SegmentationMapOnImage`` object
instead of just an array.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
size_lines : int, optional
Thickness of the line.
size_points : int, optional
Size of the points in pixels.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
imgaug.augmentables.segmaps.SegmentationMapOnImage
Segmentation map object containing drawn line string.
"""
from .segmaps import SegmentationMapOnImage
return SegmentationMapOnImage(
self.draw_mask(
image_shape, size_lines=size_lines, size_points=size_points,
raise_if_out_of_image=raise_if_out_of_image),
shape=image_shape
) | python | def to_segmentation_map(self, image_shape, size_lines=1, size_points=0,
raise_if_out_of_image=False):
"""
Generate a segmentation map object from the line string.
This is similar to
:func:`imgaug.augmentables.lines.LineString.draw_mask`.
The result is wrapped in a ``SegmentationMapOnImage`` object
instead of just an array.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
size_lines : int, optional
Thickness of the line.
size_points : int, optional
Size of the points in pixels.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
imgaug.augmentables.segmaps.SegmentationMapOnImage
Segmentation map object containing drawn line string.
"""
from .segmaps import SegmentationMapOnImage
return SegmentationMapOnImage(
self.draw_mask(
image_shape, size_lines=size_lines, size_points=size_points,
raise_if_out_of_image=raise_if_out_of_image),
shape=image_shape
) | [
"def",
"to_segmentation_map",
"(",
"self",
",",
"image_shape",
",",
"size_lines",
"=",
"1",
",",
"size_points",
"=",
"0",
",",
"raise_if_out_of_image",
"=",
"False",
")",
":",
"from",
".",
"segmaps",
"import",
"SegmentationMapOnImage",
"return",
"SegmentationMapOnImage",
"(",
"self",
".",
"draw_mask",
"(",
"image_shape",
",",
"size_lines",
"=",
"size_lines",
",",
"size_points",
"=",
"size_points",
",",
"raise_if_out_of_image",
"=",
"raise_if_out_of_image",
")",
",",
"shape",
"=",
"image_shape",
")"
] | Generate a segmentation map object from the line string.
This is similar to
:func:`imgaug.augmentables.lines.LineString.draw_mask`.
The result is wrapped in a ``SegmentationMapOnImage`` object
instead of just an array.
Parameters
----------
image_shape : tuple of int
The shape of the image onto which to draw the line mask.
size_lines : int, optional
Thickness of the line.
size_points : int, optional
Size of the points in pixels.
raise_if_out_of_image : bool, optional
Whether to raise an error if the line string is fully
outside of the image. If set to False, no error will be raised and
only the parts inside the image will be drawn.
Returns
-------
imgaug.augmentables.segmaps.SegmentationMapOnImage
Segmentation map object containing drawn line string. | [
"Generate",
"a",
"segmentation",
"map",
"object",
"from",
"the",
"line",
"string",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L1286-L1324 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.coords_almost_equals | def coords_almost_equals(self, other, max_distance=1e-6, points_per_edge=8):
"""
Compare this and another LineString's coordinates.
This is an approximate method based on pointwise distances and can
in rare corner cases produce wrong outputs.
Parameters
----------
other : imgaug.augmentables.lines.LineString \
or tuple of number \
or ndarray \
or list of ndarray \
or list of tuple of number
The other line string or its coordinates.
max_distance : float
Max distance of any point from the other line string before
the two line strings are evaluated to be unequal.
points_per_edge : int, optional
How many points to interpolate on each edge.
Returns
-------
bool
Whether the two LineString's coordinates are almost identical,
i.e. the max distance is below the threshold.
If both have no coordinates, ``True`` is returned.
If only one has no coordinates, ``False`` is returned.
Beyond that, the number of points is not evaluated.
"""
if isinstance(other, LineString):
pass
elif isinstance(other, tuple):
other = LineString([other])
else:
other = LineString(other)
if len(self.coords) == 0 and len(other.coords) == 0:
return True
elif 0 in [len(self.coords), len(other.coords)]:
# only one of the two line strings has no coords
return False
self_subd = self.subdivide(points_per_edge)
other_subd = other.subdivide(points_per_edge)
dist_self2other = self_subd.compute_pointwise_distances(other_subd)
dist_other2self = other_subd.compute_pointwise_distances(self_subd)
dist = max(np.max(dist_self2other), np.max(dist_other2self))
return dist < max_distance | python | def coords_almost_equals(self, other, max_distance=1e-6, points_per_edge=8):
"""
Compare this and another LineString's coordinates.
This is an approximate method based on pointwise distances and can
in rare corner cases produce wrong outputs.
Parameters
----------
other : imgaug.augmentables.lines.LineString \
or tuple of number \
or ndarray \
or list of ndarray \
or list of tuple of number
The other line string or its coordinates.
max_distance : float
Max distance of any point from the other line string before
the two line strings are evaluated to be unequal.
points_per_edge : int, optional
How many points to interpolate on each edge.
Returns
-------
bool
Whether the two LineString's coordinates are almost identical,
i.e. the max distance is below the threshold.
If both have no coordinates, ``True`` is returned.
If only one has no coordinates, ``False`` is returned.
Beyond that, the number of points is not evaluated.
"""
if isinstance(other, LineString):
pass
elif isinstance(other, tuple):
other = LineString([other])
else:
other = LineString(other)
if len(self.coords) == 0 and len(other.coords) == 0:
return True
elif 0 in [len(self.coords), len(other.coords)]:
# only one of the two line strings has no coords
return False
self_subd = self.subdivide(points_per_edge)
other_subd = other.subdivide(points_per_edge)
dist_self2other = self_subd.compute_pointwise_distances(other_subd)
dist_other2self = other_subd.compute_pointwise_distances(self_subd)
dist = max(np.max(dist_self2other), np.max(dist_other2self))
return dist < max_distance | [
"def",
"coords_almost_equals",
"(",
"self",
",",
"other",
",",
"max_distance",
"=",
"1e-6",
",",
"points_per_edge",
"=",
"8",
")",
":",
"if",
"isinstance",
"(",
"other",
",",
"LineString",
")",
":",
"pass",
"elif",
"isinstance",
"(",
"other",
",",
"tuple",
")",
":",
"other",
"=",
"LineString",
"(",
"[",
"other",
"]",
")",
"else",
":",
"other",
"=",
"LineString",
"(",
"other",
")",
"if",
"len",
"(",
"self",
".",
"coords",
")",
"==",
"0",
"and",
"len",
"(",
"other",
".",
"coords",
")",
"==",
"0",
":",
"return",
"True",
"elif",
"0",
"in",
"[",
"len",
"(",
"self",
".",
"coords",
")",
",",
"len",
"(",
"other",
".",
"coords",
")",
"]",
":",
"# only one of the two line strings has no coords",
"return",
"False",
"self_subd",
"=",
"self",
".",
"subdivide",
"(",
"points_per_edge",
")",
"other_subd",
"=",
"other",
".",
"subdivide",
"(",
"points_per_edge",
")",
"dist_self2other",
"=",
"self_subd",
".",
"compute_pointwise_distances",
"(",
"other_subd",
")",
"dist_other2self",
"=",
"other_subd",
".",
"compute_pointwise_distances",
"(",
"self_subd",
")",
"dist",
"=",
"max",
"(",
"np",
".",
"max",
"(",
"dist_self2other",
")",
",",
"np",
".",
"max",
"(",
"dist_other2self",
")",
")",
"return",
"dist",
"<",
"max_distance"
] | Compare this and another LineString's coordinates.
This is an approximate method based on pointwise distances and can
in rare corner cases produce wrong outputs.
Parameters
----------
other : imgaug.augmentables.lines.LineString \
or tuple of number \
or ndarray \
or list of ndarray \
or list of tuple of number
The other line string or its coordinates.
max_distance : float
Max distance of any point from the other line string before
the two line strings are evaluated to be unequal.
points_per_edge : int, optional
How many points to interpolate on each edge.
Returns
-------
bool
Whether the two LineString's coordinates are almost identical,
i.e. the max distance is below the threshold.
If both have no coordinates, ``True`` is returned.
If only one has no coordinates, ``False`` is returned.
Beyond that, the number of points is not evaluated. | [
"Compare",
"this",
"and",
"another",
"LineString",
"s",
"coordinates",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L1327-L1379 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.almost_equals | def almost_equals(self, other, max_distance=1e-4, points_per_edge=8):
"""
Compare this and another LineString.
Parameters
----------
other: imgaug.augmentables.lines.LineString
The other line string. Must be a LineString instance, not just
its coordinates.
max_distance : float, optional
See :func:`imgaug.augmentables.lines.LineString.coords_almost_equals`.
points_per_edge : int, optional
See :func:`imgaug.augmentables.lines.LineString.coords_almost_equals`.
Returns
-------
bool
``True`` if the coordinates are almost equal according to
:func:`imgaug.augmentables.lines.LineString.coords_almost_equals`
and additionally the labels are identical. Otherwise ``False``.
"""
if self.label != other.label:
return False
return self.coords_almost_equals(
other, max_distance=max_distance, points_per_edge=points_per_edge) | python | def almost_equals(self, other, max_distance=1e-4, points_per_edge=8):
"""
Compare this and another LineString.
Parameters
----------
other: imgaug.augmentables.lines.LineString
The other line string. Must be a LineString instance, not just
its coordinates.
max_distance : float, optional
See :func:`imgaug.augmentables.lines.LineString.coords_almost_equals`.
points_per_edge : int, optional
See :func:`imgaug.augmentables.lines.LineString.coords_almost_equals`.
Returns
-------
bool
``True`` if the coordinates are almost equal according to
:func:`imgaug.augmentables.lines.LineString.coords_almost_equals`
and additionally the labels are identical. Otherwise ``False``.
"""
if self.label != other.label:
return False
return self.coords_almost_equals(
other, max_distance=max_distance, points_per_edge=points_per_edge) | [
"def",
"almost_equals",
"(",
"self",
",",
"other",
",",
"max_distance",
"=",
"1e-4",
",",
"points_per_edge",
"=",
"8",
")",
":",
"if",
"self",
".",
"label",
"!=",
"other",
".",
"label",
":",
"return",
"False",
"return",
"self",
".",
"coords_almost_equals",
"(",
"other",
",",
"max_distance",
"=",
"max_distance",
",",
"points_per_edge",
"=",
"points_per_edge",
")"
] | Compare this and another LineString.
Parameters
----------
other: imgaug.augmentables.lines.LineString
The other line string. Must be a LineString instance, not just
its coordinates.
max_distance : float, optional
See :func:`imgaug.augmentables.lines.LineString.coords_almost_equals`.
points_per_edge : int, optional
See :func:`imgaug.augmentables.lines.LineString.coords_almost_equals`.
Returns
-------
bool
``True`` if the coordinates are almost equal according to
:func:`imgaug.augmentables.lines.LineString.coords_almost_equals`
and additionally the labels are identical. Otherwise ``False``. | [
"Compare",
"this",
"and",
"another",
"LineString",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L1381-L1408 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.copy | def copy(self, coords=None, label=None):
"""
Create a shallow copy of the LineString object.
Parameters
----------
coords : None or iterable of tuple of number or ndarray
If not ``None``, then the coords of the copied object will be set
to this value.
label : None or str
If not ``None``, then the label of the copied object will be set to
this value.
Returns
-------
imgaug.augmentables.lines.LineString
Shallow copy.
"""
return LineString(coords=self.coords if coords is None else coords,
label=self.label if label is None else label) | python | def copy(self, coords=None, label=None):
"""
Create a shallow copy of the LineString object.
Parameters
----------
coords : None or iterable of tuple of number or ndarray
If not ``None``, then the coords of the copied object will be set
to this value.
label : None or str
If not ``None``, then the label of the copied object will be set to
this value.
Returns
-------
imgaug.augmentables.lines.LineString
Shallow copy.
"""
return LineString(coords=self.coords if coords is None else coords,
label=self.label if label is None else label) | [
"def",
"copy",
"(",
"self",
",",
"coords",
"=",
"None",
",",
"label",
"=",
"None",
")",
":",
"return",
"LineString",
"(",
"coords",
"=",
"self",
".",
"coords",
"if",
"coords",
"is",
"None",
"else",
"coords",
",",
"label",
"=",
"self",
".",
"label",
"if",
"label",
"is",
"None",
"else",
"label",
")"
] | Create a shallow copy of the LineString object.
Parameters
----------
coords : None or iterable of tuple of number or ndarray
If not ``None``, then the coords of the copied object will be set
to this value.
label : None or str
If not ``None``, then the label of the copied object will be set to
this value.
Returns
-------
imgaug.augmentables.lines.LineString
Shallow copy. | [
"Create",
"a",
"shallow",
"copy",
"of",
"the",
"LineString",
"object",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L1410-L1431 | valid |
aleju/imgaug | imgaug/augmentables/lines.py | LineString.deepcopy | def deepcopy(self, coords=None, label=None):
"""
Create a deep copy of the BoundingBox object.
Parameters
----------
coords : None or iterable of tuple of number or ndarray
If not ``None``, then the coords of the copied object will be set
to this value.
label : None or str
If not ``None``, then the label of the copied object will be set to
this value.
Returns
-------
imgaug.augmentables.lines.LineString
Deep copy.
"""
return LineString(
coords=np.copy(self.coords) if coords is None else coords,
label=copylib.deepcopy(self.label) if label is None else label) | python | def deepcopy(self, coords=None, label=None):
"""
Create a deep copy of the BoundingBox object.
Parameters
----------
coords : None or iterable of tuple of number or ndarray
If not ``None``, then the coords of the copied object will be set
to this value.
label : None or str
If not ``None``, then the label of the copied object will be set to
this value.
Returns
-------
imgaug.augmentables.lines.LineString
Deep copy.
"""
return LineString(
coords=np.copy(self.coords) if coords is None else coords,
label=copylib.deepcopy(self.label) if label is None else label) | [
"def",
"deepcopy",
"(",
"self",
",",
"coords",
"=",
"None",
",",
"label",
"=",
"None",
")",
":",
"return",
"LineString",
"(",
"coords",
"=",
"np",
".",
"copy",
"(",
"self",
".",
"coords",
")",
"if",
"coords",
"is",
"None",
"else",
"coords",
",",
"label",
"=",
"copylib",
".",
"deepcopy",
"(",
"self",
".",
"label",
")",
"if",
"label",
"is",
"None",
"else",
"label",
")"
] | Create a deep copy of the BoundingBox object.
Parameters
----------
coords : None or iterable of tuple of number or ndarray
If not ``None``, then the coords of the copied object will be set
to this value.
label : None or str
If not ``None``, then the label of the copied object will be set to
this value.
Returns
-------
imgaug.augmentables.lines.LineString
Deep copy. | [
"Create",
"a",
"deep",
"copy",
"of",
"the",
"BoundingBox",
"object",
"."
] | 786be74aa855513840113ea523c5df495dc6a8af | https://github.com/aleju/imgaug/blob/786be74aa855513840113ea523c5df495dc6a8af/imgaug/augmentables/lines.py#L1433-L1455 | valid |