Blender V5.0
lasso_2d.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_vec_types.h"
10
11#include "BLI_math_base.h"
12#include "BLI_math_geom.h"
13#include "BLI_sys_types.h"
14
15#include "BLI_lasso_2d.hh" /* own include */
16
17#include "BLI_strict_flags.h" /* IWYU pragma: keep. Keep last. */
18
19using blender::int2;
20using blender::Span;
21
22void BLI_lasso_boundbox(rcti *rect, const Span<int2> mcoords)
23{
24 rect->xmin = rect->xmax = mcoords[0][0];
25 rect->ymin = rect->ymax = mcoords[0][1];
26
27 for (const int64_t a : mcoords.index_range().drop_front(1)) {
28 if (mcoords[a][0] < rect->xmin) {
29 rect->xmin = mcoords[a][0];
30 }
31 else if (mcoords[a][0] > rect->xmax) {
32 rect->xmax = mcoords[a][0];
33 }
34 if (mcoords[a][1] < rect->ymin) {
35 rect->ymin = mcoords[a][1];
36 }
37 else if (mcoords[a][1] > rect->ymax) {
38 rect->ymax = mcoords[a][1];
39 }
40 }
41}
42
44 const int sx,
45 const int sy,
46 const int error_value)
47{
48 if (sx == error_value || mcoords.is_empty()) {
49 return false;
50 }
51
52 const int pt[2] = {sx, sy};
54 pt, reinterpret_cast<const int (*)[2]>(mcoords.data()), uint(mcoords.size()));
55}
56
58 const Span<int2> mcoords, int x0, int y0, int x1, int y1, const int error_value)
59{
60
61 if (x0 == error_value || x1 == error_value || mcoords.is_empty()) {
62 return false;
63 }
64
65 const int v1[2] = {x0, y0}, v2[2] = {x1, y1};
66
67 /* check points in lasso */
68 if (BLI_lasso_is_point_inside(mcoords, v1[0], v1[1], error_value)) {
69 return true;
70 }
71 if (BLI_lasso_is_point_inside(mcoords, v2[0], v2[1], error_value)) {
72 return true;
73 }
74
75 /* no points in lasso, so we have to intersect with lasso edge */
76
77 if (isect_seg_seg_v2_int(mcoords[0], mcoords.last(), v1, v2) > 0) {
78 return true;
79 }
80 for (const int64_t i : mcoords.index_range().drop_back(1)) {
81 if (isect_seg_seg_v2_int(mcoords[i], mcoords[i + 1], v1, v2) > 0) {
82 return true;
83 }
84 }
85
86 return false;
87}
bool isect_point_poly_v2_int(const int pt[2], const int verts[][2], unsigned int nr)
int isect_seg_seg_v2_int(const int v1[2], const int v2[2], const int v3[2], const int v4[2])
unsigned int uint
ATTR_WARN_UNUSED_RESULT const BMVert * v2
long long int int64_t
constexpr IndexRange drop_back(int64_t n) const
constexpr IndexRange drop_front(int64_t n) const
constexpr const T * data() const
Definition BLI_span.hh:215
constexpr int64_t size() const
Definition BLI_span.hh:252
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:325
constexpr IndexRange index_range() const
Definition BLI_span.hh:401
constexpr bool is_empty() const
Definition BLI_span.hh:260
void BLI_lasso_boundbox(rcti *rect, const Span< int2 > mcoords)
Definition lasso_2d.cc:22
bool BLI_lasso_is_point_inside(const Span< int2 > mcoords, const int sx, const int sy, const int error_value)
Definition lasso_2d.cc:43
bool BLI_lasso_is_edge_inside(const Span< int2 > mcoords, int x0, int y0, int x1, int y1, const int error_value)
Definition lasso_2d.cc:57
VecBase< int32_t, 2 > int2
int ymin
int ymax
int xmin
int xmax
i
Definition text_draw.cc:230