A `Bbox` that is automatically transformed by a given transform. When either the child bounding box or transform changes, the bounds of this bbox will update accordingly.
| 1130 | |
| 1131 | |
| 1132 | class TransformedBbox(BboxBase): |
| 1133 | """ |
| 1134 | A `Bbox` that is automatically transformed by a given |
| 1135 | transform. When either the child bounding box or transform |
| 1136 | changes, the bounds of this bbox will update accordingly. |
| 1137 | """ |
| 1138 | |
| 1139 | def __init__(self, bbox, transform, **kwargs): |
| 1140 | """ |
| 1141 | Parameters |
| 1142 | ---------- |
| 1143 | bbox : `Bbox` |
| 1144 | transform : `Transform` |
| 1145 | """ |
| 1146 | _api.check_isinstance(BboxBase, bbox=bbox) |
| 1147 | _api.check_isinstance(Transform, transform=transform) |
| 1148 | if transform.input_dims != 2 or transform.output_dims != 2: |
| 1149 | raise ValueError( |
| 1150 | "The input and output dimensions of 'transform' must be 2") |
| 1151 | |
| 1152 | super().__init__(**kwargs) |
| 1153 | self._bbox = bbox |
| 1154 | self._transform = transform |
| 1155 | self.set_children(bbox, transform) |
| 1156 | self._points = None |
| 1157 | |
| 1158 | __str__ = _make_str_method("_bbox", "_transform") |
| 1159 | |
| 1160 | def get_points(self): |
| 1161 | # docstring inherited |
| 1162 | if self._invalid: |
| 1163 | p = self._bbox.get_points() |
| 1164 | # Transform all four points, then make a new bounding box |
| 1165 | # from the result, taking care to make the orientation the |
| 1166 | # same. |
| 1167 | points = self._transform.transform( |
| 1168 | [[p[0, 0], p[0, 1]], |
| 1169 | [p[1, 0], p[0, 1]], |
| 1170 | [p[0, 0], p[1, 1]], |
| 1171 | [p[1, 0], p[1, 1]]]) |
| 1172 | points = np.ma.filled(points, 0.0) |
| 1173 | |
| 1174 | xs = min(points[:, 0]), max(points[:, 0]) |
| 1175 | if p[0, 0] > p[1, 0]: |
| 1176 | xs = xs[::-1] |
| 1177 | |
| 1178 | ys = min(points[:, 1]), max(points[:, 1]) |
| 1179 | if p[0, 1] > p[1, 1]: |
| 1180 | ys = ys[::-1] |
| 1181 | |
| 1182 | self._points = np.array([ |
| 1183 | [xs[0], ys[0]], |
| 1184 | [xs[1], ys[1]] |
| 1185 | ]) |
| 1186 | |
| 1187 | self._invalid = 0 |
| 1188 | return self._points |
| 1189 |
searching dependent graphs…