|
4 | 4 | import Clipper from '@doodle3d/clipper-js'; |
5 | 5 |
|
6 | 6 | import { OpenCVTypes } from '../opencv/interfaces'; |
7 | | -import { Point, Polygon } from '../shared/interfaces'; |
| 7 | +import { Point, Polygon, RegionOfInterest } from '../shared/interfaces'; |
| 8 | +import { BoundingBox, clampBetween } from './math'; |
8 | 9 |
|
9 | 10 | export const formatContourToPoints = ( |
10 | 11 | mask: OpenCVTypes.Mat, |
@@ -151,3 +152,162 @@ export const getMatFromPoints = (CV: OpenCVTypes.cv, points: Point[], offset = { |
151 | 152 |
|
152 | 153 | return pointsMat; |
153 | 154 | }; |
| 155 | + |
| 156 | +interface getBoundingBoxResizePointsProps { |
| 157 | + gap: number; |
| 158 | + boundingBox: BoundingBox; |
| 159 | + onResized: (boundingBox: BoundingBox) => void; |
| 160 | +} |
| 161 | + |
| 162 | +export const getClampedBoundingBox = (point: Point, boundingBox: RegionOfInterest, roi: RegionOfInterest) => { |
| 163 | + const roiX = roi.width + roi.x; |
| 164 | + const roiY = roi.height + roi.y; |
| 165 | + const shapeX = boundingBox.width + boundingBox.x; |
| 166 | + const shapeY = boundingBox.height + boundingBox.y; |
| 167 | + |
| 168 | + const clampedTranslate = { |
| 169 | + x: clampBetween(shapeX - roiX, -point.x, boundingBox.x - roi.x), |
| 170 | + y: clampBetween(shapeY - roiY, -point.y, boundingBox.y - roi.y), |
| 171 | + }; |
| 172 | + |
| 173 | + return { |
| 174 | + ...boundingBox, |
| 175 | + x: boundingBox.x - clampedTranslate.x, |
| 176 | + y: boundingBox.y - clampedTranslate.y, |
| 177 | + }; |
| 178 | +}; |
| 179 | + |
| 180 | +export const getBoundingBoxInRoi = (boundingBox: BoundingBox, roi: RegionOfInterest) => { |
| 181 | + const x = Math.max(0, boundingBox.x); |
| 182 | + const y = Math.max(0, boundingBox.y); |
| 183 | + |
| 184 | + return { |
| 185 | + x, |
| 186 | + y, |
| 187 | + width: Math.min(roi.width - x, boundingBox.width), |
| 188 | + height: Math.min(roi.height - y, boundingBox.height), |
| 189 | + }; |
| 190 | +}; |
| 191 | + |
| 192 | +// Keep a gap between anchor points so that they don't overlap |
| 193 | +export const getBoundingBoxResizePoints = ({ boundingBox, gap, onResized }: getBoundingBoxResizePointsProps) => { |
| 194 | + return [ |
| 195 | + { |
| 196 | + x: boundingBox.x, |
| 197 | + y: boundingBox.y, |
| 198 | + moveAnchorTo: (x: number, y: number) => { |
| 199 | + const x1 = Math.max(0, Math.min(x, boundingBox.x + boundingBox.width - gap)); |
| 200 | + const y1 = Math.max(0, Math.min(y, boundingBox.y + boundingBox.height - gap)); |
| 201 | + |
| 202 | + onResized({ |
| 203 | + x: x1, |
| 204 | + width: Math.max(gap, boundingBox.width + boundingBox.x - x1), |
| 205 | + y: y1, |
| 206 | + height: Math.max(gap, boundingBox.height + boundingBox.y - y1), |
| 207 | + }); |
| 208 | + }, |
| 209 | + cursor: 'nw-resize', |
| 210 | + label: 'North west resize anchor', |
| 211 | + }, |
| 212 | + { |
| 213 | + x: boundingBox.x + boundingBox.width / 2, |
| 214 | + y: boundingBox.y, |
| 215 | + moveAnchorTo: (_x: number, y: number) => { |
| 216 | + const y1 = Math.max(0, Math.min(y, boundingBox.y + boundingBox.height - gap)); |
| 217 | + |
| 218 | + onResized({ |
| 219 | + ...boundingBox, |
| 220 | + y: y1, |
| 221 | + height: Math.max(gap, boundingBox.height + boundingBox.y - y1), |
| 222 | + }); |
| 223 | + }, |
| 224 | + cursor: 'n-resize', |
| 225 | + label: 'North resize anchor', |
| 226 | + }, |
| 227 | + { |
| 228 | + x: boundingBox.x + boundingBox.width, |
| 229 | + y: boundingBox.y, |
| 230 | + moveAnchorTo: (x: number, y: number) => { |
| 231 | + const y1 = Math.max(0, Math.min(y, boundingBox.y + boundingBox.height - gap)); |
| 232 | + |
| 233 | + onResized({ |
| 234 | + ...boundingBox, |
| 235 | + width: Math.max(gap, x - boundingBox.x), |
| 236 | + y: y1, |
| 237 | + height: Math.max(gap, boundingBox.height + boundingBox.y - y1), |
| 238 | + }); |
| 239 | + }, |
| 240 | + cursor: 'ne-resize', |
| 241 | + label: 'North east resize anchor', |
| 242 | + }, |
| 243 | + { |
| 244 | + x: boundingBox.x + boundingBox.width, |
| 245 | + y: boundingBox.y + boundingBox.height / 2, |
| 246 | + moveAnchorTo: (x: number) => { |
| 247 | + onResized({ ...boundingBox, width: Math.max(gap, x - boundingBox.x) }); |
| 248 | + }, |
| 249 | + cursor: 'e-resize', |
| 250 | + label: 'East resize anchor', |
| 251 | + }, |
| 252 | + { |
| 253 | + x: boundingBox.x + boundingBox.width, |
| 254 | + y: boundingBox.y + boundingBox.height, |
| 255 | + moveAnchorTo: (x: number, y: number) => { |
| 256 | + onResized({ |
| 257 | + x: boundingBox.x, |
| 258 | + width: Math.max(gap, x - boundingBox.x), |
| 259 | + |
| 260 | + y: boundingBox.y, |
| 261 | + height: Math.max(gap, y - boundingBox.y), |
| 262 | + }); |
| 263 | + }, |
| 264 | + cursor: 'se-resize', |
| 265 | + label: 'South east resize anchor', |
| 266 | + }, |
| 267 | + { |
| 268 | + x: boundingBox.x + boundingBox.width / 2, |
| 269 | + y: boundingBox.y + boundingBox.height, |
| 270 | + moveAnchorTo: (_x: number, y: number) => { |
| 271 | + onResized({ |
| 272 | + ...boundingBox, |
| 273 | + y: boundingBox.y, |
| 274 | + height: Math.max(gap, y - boundingBox.y), |
| 275 | + }); |
| 276 | + }, |
| 277 | + cursor: 's-resize', |
| 278 | + label: 'South resize anchor', |
| 279 | + }, |
| 280 | + { |
| 281 | + x: boundingBox.x, |
| 282 | + y: boundingBox.y + boundingBox.height, |
| 283 | + moveAnchorTo: (x: number, y: number) => { |
| 284 | + const x1 = Math.max(0, Math.min(x, boundingBox.x + boundingBox.width - gap)); |
| 285 | + |
| 286 | + onResized({ |
| 287 | + x: x1, |
| 288 | + width: Math.max(gap, boundingBox.width + boundingBox.x - x1), |
| 289 | + |
| 290 | + y: boundingBox.y, |
| 291 | + height: Math.max(gap, y - boundingBox.y), |
| 292 | + }); |
| 293 | + }, |
| 294 | + cursor: 'sw-resize', |
| 295 | + label: 'South west resize anchor', |
| 296 | + }, |
| 297 | + { |
| 298 | + x: boundingBox.x, |
| 299 | + y: boundingBox.y + boundingBox.height / 2, |
| 300 | + moveAnchorTo: (x: number, _y: number) => { |
| 301 | + const x1 = Math.max(0, Math.min(x, boundingBox.x + boundingBox.width - gap)); |
| 302 | + |
| 303 | + onResized({ |
| 304 | + ...boundingBox, |
| 305 | + x: x1, |
| 306 | + width: Math.max(gap, boundingBox.width + boundingBox.x - x1), |
| 307 | + }); |
| 308 | + }, |
| 309 | + cursor: 'w-resize', |
| 310 | + label: 'West resize anchor', |
| 311 | + }, |
| 312 | + ]; |
| 313 | +}; |
0 commit comments