Welcome to mirror list, hosted at ThFree Co, Russian Federation.

lasso.c « intern « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7df4da80e164228686b8a27f67cd6ee3551ea349 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 *
 */

/** \file blender/blenlib/intern/lasso.c
 *  \ingroup bli
 */

#include "DNA_vec_types.h"

#include "BLI_math.h"
#include "BLI_rect.h"

#include "BLI_lasso.h" /* own include */

void BLI_lasso_boundbox(rcti *rect, int mcords[][2], short moves)
{
	short a;

	rect->xmin = rect->xmax = mcords[0][0];
	rect->ymin = rect->ymax = mcords[0][1];

	for (a = 1; a < moves; a++) {
		if      (mcords[a][0] < rect->xmin) rect->xmin = mcords[a][0];
		else if (mcords[a][0] > rect->xmax) rect->xmax = mcords[a][0];
		if      (mcords[a][1] < rect->ymin) rect->ymin = mcords[a][1];
		else if (mcords[a][1] > rect->ymax) rect->ymax = mcords[a][1];
	}
}


int BLI_lasso_is_point_inside(int mcords[][2], short moves,
                              const int sx, const int sy,
                              const int error_value)
{
	/* we do the angle rule, define that all added angles should be about zero or (2 * PI) */
	float angletot = 0.0, dot, ang, cross, fp1[2], fp2[2];
	int a;
	int *p1, *p2;

	if (sx == error_value) {
		return 0;
	}

	p1 = mcords[moves - 1];
	p2 = mcords[0];

	/* first vector */
	fp1[0] = (float)(p1[0] - sx);
	fp1[1] = (float)(p1[1] - sy);
	normalize_v2(fp1);

	for (a = 0; a < moves; a++) {
		/* second vector */
		fp2[0] = (float)(p2[0] - sx);
		fp2[1] = (float)(p2[1] - sy);
		normalize_v2(fp2);

		/* dot and angle and cross */
		dot = fp1[0] * fp2[0] + fp1[1] * fp2[1];
		ang = fabs(saacos(dot));

		cross = (float)((p1[1] - p2[1]) * (p1[0] - sx) + (p2[0] - p1[0]) * (p1[1] - sy));

		if (cross < 0.0f) angletot -= ang;
		else angletot += ang;

		/* circulate */
		fp1[0] = fp2[0]; fp1[1] = fp2[1];
		p1 = p2;
		p2 = mcords[a + 1];
	}

	if (fabsf(angletot) > 4.0f) return 1;
	return 0;
}

/* edge version for lasso select. we assume boundbox check was done */
int BLI_lasso_is_edge_inside(int mcords[][2], short moves,
                             int x0, int y0, int x1, int y1,
                             const int error_value)
{
	int v1[2], v2[2];
	int a;

	if (x0 == error_value || x1 == error_value) {
		return 0;
	}

	v1[0] = x0, v1[1] = y0;
	v2[0] = x1, v2[1] = y1;

	/* check points in lasso */
	if (BLI_lasso_is_point_inside(mcords, moves, v1[0], v1[1], error_value)) return 1;
	if (BLI_lasso_is_point_inside(mcords, moves, v2[0], v2[1], error_value)) return 1;

	/* no points in lasso, so we have to intersect with lasso edge */

	if (isect_line_line_v2_int(mcords[0], mcords[moves - 1], v1, v2) > 0) return 1;
	for (a = 0; a < moves - 1; a++) {
		if (isect_line_line_v2_int(mcords[a], mcords[a + 1], v1, v2) > 0) return 1;
	}

	return 0;
}