Blender V5.3
BLI_dial_2d.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_dial_2d.hh"
10
11#include "MEM_guardedalloc.h"
12
13#include "BLI_math_vector_c.hh"
14
15namespace blender {
16
17struct Dial {
18 /* center of the dial */
19 float center[2];
20
21 /* threshold of the dial. Distance of current position has to be greater
22 * than the threshold to be used in any calculations */
24
25 /* the direction of the first dial position exceeding the threshold. This
26 * is later used as the basis against which rotation angle is calculated */
28
29 /* cache the last angle to detect rotations bigger than -/+ PI */
31
32 /* number of full rotations */
34
35 /* has initial_direction been initialized */
37};
38
39Dial *BLI_dial_init(const float start_position[2], float threshold)
40{
41 Dial *dial = MEM_new_zeroed<Dial>("dial");
42
43 copy_v2_v2(dial->center, start_position);
44 dial->threshold_squared = threshold * threshold;
45
46 return dial;
47}
48
50{
51 MEM_delete(dial);
52}
53
54float BLI_dial_angle(Dial *dial, const float current_position[2])
55{
56 float current_direction[2];
57
58 sub_v2_v2v2(current_direction, current_position, dial->center);
59
60 /* only update when we have enough precision,
61 * by having the mouse adequately away from center */
62 if (len_squared_v2(current_direction) > dial->threshold_squared) {
63 float angle;
64 float cosval, sinval;
65
66 normalize_v2(current_direction);
67
68 if (!dial->initialized) {
69 copy_v2_v2(dial->initial_direction, current_direction);
70 dial->initialized = true;
71 }
72
73 /* calculate mouse angle between initial and final mouse position */
74 cosval = dot_v2v2(current_direction, dial->initial_direction);
75 sinval = cross_v2v2(current_direction, dial->initial_direction);
76
77 /* Clamp to avoid NAN's in #acos */
78 angle = atan2f(sinval, cosval);
79
80 /* change of sign, we passed the 180 degree threshold. This means we need to add a turn.
81 * to distinguish between transition from 0 to -1 and -PI to +PI,
82 * use comparison with PI/2 */
83 if ((angle * dial->last_angle < 0.0f) && (fabsf(dial->last_angle) > float(M_PI_2))) {
84 if (dial->last_angle < 0.0f) {
85 dial->rotations--;
86 }
87 else {
88 dial->rotations++;
89 }
90 }
91 dial->last_angle = angle;
92
93 return angle + 2.0f * float(M_PI) * dial->rotations;
94 }
95
96 return dial->last_angle;
97}
98
99} // namespace blender
#define M_PI_2
#define M_PI
Read Blender memory allocation and freeing.
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
BMesh Iterators.
MINLINE float cross_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE void copy_v2_v2(float r[2], const float a[2])
MINLINE void sub_v2_v2v2(float r[2], const float a[2], const float b[2])
MINLINE float normalize_v2(float n[2])
void * MEM_new_zeroed(size_t len, const char *str) ATTR_WARN_UNUSED_RESULT ATTR_ALLOC_SIZE(1) ATTR_NONNULL(2)
MINLINE float dot_v2v2(const float a[2], const float b[2]) ATTR_WARN_UNUSED_RESULT
MINLINE float len_squared_v2(const float v[2]) ATTR_WARN_UNUSED_RESULT
void BLI_dial_free(Dial *dial)
Dial * BLI_dial_init(const float start_position[2], float threshold)
float BLI_dial_angle(Dial *dial, const float current_position[2])
#define fabsf
#define atan2f
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
float threshold_squared
float center[2]
float initial_direction[2]