[Bf-blender-cvs] [7d5c16c] master: Add a reusable dial mechanism to get rotations around a center and an initial position. The system supports arbitrarily big angles.

Antony Riakiotakis noreply at git.blender.org
Wed Aug 27 17:29:26 CEST 2014


Commit: 7d5c16c085a6059262b5d25cd1833609b643c649
Author: Antony Riakiotakis
Date:   Wed Aug 27 17:29:15 2014 +0200
Branches: master
https://developer.blender.org/rB7d5c16c085a6059262b5d25cd1833609b643c649

Add a reusable dial mechanism to get rotations around a center and an
initial position. The system supports arbitrarily big angles.

===================================================================

A	source/blender/blenlib/BLI_dial.h
M	source/blender/blenlib/CMakeLists.txt
A	source/blender/blenlib/intern/BLI_dial.c

===================================================================

diff --git a/source/blender/blenlib/BLI_dial.h b/source/blender/blenlib/BLI_dial.h
new file mode 100644
index 0000000..61420f4
--- /dev/null
+++ b/source/blender/blenlib/BLI_dial.h
@@ -0,0 +1,59 @@
+/*
+ * ***** 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.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#ifndef __BLI_DIAL_H__
+#define __BLI_DIAL_H__
+
+/** \file BLI_dial.h
+ *  \ingroup bli
+ *
+ * \note dials act similar to old rotation based phones and output an angle.
+ * 
+ * They just are initialized with the center of the dial and a threshold value as input.
+ * 
+ * When the distance of the current position of the dial from the center 
+ * exceeds the threshold, this position is used to calculate the initial direction. 
+ * After that, the angle from the initial direction is calculated based on
+ * current and previous directions of the digit, and returned to the user.
+ * 
+ * Usage examples:
+ * \code
+ * float start_position[2] = {0.0f, 0.0f};
+ * float current_position[2];
+ * float threshold = 0.5f;
+ * float angle;
+ * Dial *dial;
+ * 
+ * dial = BLI_dial_initialize(start_position, threshold);
+ *
+ * angle = BLI_dial_angle(dial, curent_position);
+ * 
+ * MEM_freeN(dial);
+ *
+ * \endcode
+ */
+
+typedef struct Dial Dial;
+
+Dial * BLI_dial_initialize(float start_position[2], float threshold);
+
+float BLI_dial_angle(Dial *dial, float current_position[2]);
+
+#endif /* __BLI_DIAL_H__ */
\ No newline at end of file
diff --git a/source/blender/blenlib/CMakeLists.txt b/source/blender/blenlib/CMakeLists.txt
index 0c1c5f1..dd02bcf 100644
--- a/source/blender/blenlib/CMakeLists.txt
+++ b/source/blender/blenlib/CMakeLists.txt
@@ -42,6 +42,7 @@ set(INC_SYS
 set(SRC
 	intern/BLI_args.c
 	intern/BLI_array.c
+	intern/BLI_dial.c
 	intern/BLI_dynstr.c
 	intern/BLI_ghash.c
 	intern/BLI_heap.c
@@ -118,6 +119,7 @@ set(SRC
 	BLI_compiler_attrs.h
 	BLI_compiler_compat.h
 	BLI_convexhull2d.h
+	BLI_dial.h
 	BLI_dlrbTree.h
 	BLI_dynlib.h
 	BLI_dynstr.h
diff --git a/source/blender/blenlib/intern/BLI_dial.c b/source/blender/blenlib/intern/BLI_dial.c
new file mode 100644
index 0000000..943e0b2
--- /dev/null
+++ b/source/blender/blenlib/intern/BLI_dial.c
@@ -0,0 +1,99 @@
+/*
+ * ***** 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.
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+#include "BLI_dial.h"
+#include "BLI_math.h"
+
+#include "MEM_guardedalloc.h"
+
+struct Dial {
+	/* center of the dial */
+	float center[2];
+	
+	/* threshold of the dial. Distance of current position has to be greater 
+	 * than the threshold to be used in any calculations */
+	float threshold_squared;
+	
+	/* the direction of the first dial position exceeding the threshold. This
+	 * is later used as the basis against which rotation angle is calculated */
+	float initial_direction[2];
+
+	/* cache the last angle to detect rotations bigger than -/+ PI */
+	float last_angle;
+	
+	/* number of full rotations */
+	int rotations;
+	
+	/* has initial_direction been initialized */
+	bool initialized;
+};
+
+
+Dial * BLI_dial_initialize(float start_position[2], float threshold)
+{
+	Dial *dial = MEM_callocN(sizeof(Dial), "dial");
+	
+	copy_v2_v2(dial->center, start_position);
+	dial->threshold_squared = threshold * threshold;
+	
+	return dial;
+}
+
+float BLI_dial_angle(Dial *dial, float current_position[2])
+{
+	float current_direction[2];
+	
+	sub_v2_v2v2(current_direction, current_position, dial->center);
+
+	/* only update when we have enough precision, by having the mouse adequately away from center */
+	if (len_squared_v2(current_direction) > dial->threshold_squared) {
+		float angle;
+		float cosval, sinval;
+
+		normalize_v2(current_direction);
+
+		if (!dial->initialized) {
+			copy_v2_v2(dial->initial_direction, current_direction);
+			dial->initialized = true;
+		}
+		
+		/* calculate mouse angle between initial and final mouse position */
+		cosval = dot_v2v2(current_direction, dial->initial_direction);
+		sinval = cross_v2v2(current_direction, dial->initial_direction);
+		
+		/* clamp to avoid nans in acos */
+		angle = atan2(sinval, cosval);
+		
+		/* change of sign, we passed the 180 degree threshold. This means we need to add a turn.
+		 * to distinguish between transition from 0 to -1 and -PI to +PI, use comparison with PI/2 */
+		if ((angle * dial->last_angle < 0.0f) && (fabsf(dial->last_angle) > (float)M_PI_2))
+		{
+			if (dial->last_angle < 0)
+				dial->rotations--;
+			else
+				dial->rotations++;
+		}
+		dial->last_angle = angle;
+		
+		return angle + 2.0f * (float)M_PI * dial->rotations;
+	}
+	
+	return dial->last_angle;
+}




More information about the Bf-blender-cvs mailing list