[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [55089] trunk/blender/source/blender/ editors/space_view3d: ruler/protractor for 3d viewport ( apart of 3d printing features).

Campbell Barton ideasman42 at gmail.com
Thu Mar 7 12:53:12 CET 2013


Revision: 55089
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=55089
Author:   campbellbarton
Date:     2013-03-07 11:53:11 +0000 (Thu, 07 Mar 2013)
Log Message:
-----------
ruler/protractor for 3d viewport (apart of 3d printing features). work-in-progress.

- activate from spacebar search (3D Ruler) 
- ctrl-click adds new rulers
- clicking in the middle of a ruler, turns into protractor, dragging out of view snaps back to ruler.

Modified Paths:
--------------
    trunk/blender/source/blender/editors/space_view3d/CMakeLists.txt
    trunk/blender/source/blender/editors/space_view3d/view3d_intern.h
    trunk/blender/source/blender/editors/space_view3d/view3d_ops.c

Added Paths:
-----------
    trunk/blender/source/blender/editors/space_view3d/view3d_ruler.c

Modified: trunk/blender/source/blender/editors/space_view3d/CMakeLists.txt
===================================================================
--- trunk/blender/source/blender/editors/space_view3d/CMakeLists.txt	2013-03-07 10:26:04 UTC (rev 55088)
+++ trunk/blender/source/blender/editors/space_view3d/CMakeLists.txt	2013-03-07 11:53:11 UTC (rev 55089)
@@ -54,6 +54,7 @@
 	view3d_iterators.c
 	view3d_ops.c
 	view3d_project.c
+	view3d_ruler.c
 	view3d_select.c
 	view3d_snap.c
 	view3d_toolbar.c

Modified: trunk/blender/source/blender/editors/space_view3d/view3d_intern.h
===================================================================
--- trunk/blender/source/blender/editors/space_view3d/view3d_intern.h	2013-03-07 10:26:04 UTC (rev 55088)
+++ trunk/blender/source/blender/editors/space_view3d/view3d_intern.h	2013-03-07 11:53:11 UTC (rev 55089)
@@ -108,6 +108,9 @@
 void view3d_keymap(struct wmKeyConfig *keyconf);
 void VIEW3D_OT_fly(struct wmOperatorType *ot);
 
+/* view3d_ruler.c */
+void VIEW3D_OT_ruler(struct wmOperatorType *ot);
+
 /* drawanim.c */
 void draw_motion_paths_init(View3D *v3d, struct ARegion *ar);
 void draw_motion_path_instance(Scene *scene,

Modified: trunk/blender/source/blender/editors/space_view3d/view3d_ops.c
===================================================================
--- trunk/blender/source/blender/editors/space_view3d/view3d_ops.c	2013-03-07 10:26:04 UTC (rev 55088)
+++ trunk/blender/source/blender/editors/space_view3d/view3d_ops.c	2013-03-07 11:53:11 UTC (rev 55089)
@@ -166,6 +166,7 @@
 	WM_operatortype_append(VIEW3D_OT_localview);
 	WM_operatortype_append(VIEW3D_OT_game_start);
 	WM_operatortype_append(VIEW3D_OT_fly);
+	WM_operatortype_append(VIEW3D_OT_ruler);
 	WM_operatortype_append(VIEW3D_OT_layers);
 	WM_operatortype_append(VIEW3D_OT_copybuffer);
 	WM_operatortype_append(VIEW3D_OT_pastebuffer);

Added: trunk/blender/source/blender/editors/space_view3d/view3d_ruler.c
===================================================================
--- trunk/blender/source/blender/editors/space_view3d/view3d_ruler.c	                        (rev 0)
+++ trunk/blender/source/blender/editors/space_view3d/view3d_ruler.c	2013-03-07 11:53:11 UTC (rev 55089)
@@ -0,0 +1,704 @@
+/*
+ * ***** 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.
+ *
+ * Contributor(s): Campbell Barton
+ *
+ * ***** END GPL LICENSE BLOCK *****
+ */
+
+/** \file blender/editors/space_view3d/view3d_ruler.c
+ *  \ingroup spview3d
+ */
+
+/* defines VIEW3D_OT_ruler modal operator */
+
+#include "DNA_scene_types.h"
+
+#include "MEM_guardedalloc.h"
+
+#include "BLI_math.h"
+#include "BLI_blenlib.h"
+
+#include "BKE_context.h"
+#include "BKE_unit.h"
+
+#include "BIF_gl.h"
+
+#include "WM_api.h"
+#include "WM_types.h"
+
+#include "ED_screen.h"
+#include "ED_space_api.h"
+
+#include "BLF_api.h"
+#include "BIF_glutil.h"
+
+#include "UI_resources.h"
+#include "UI_interface.h"
+
+#include "view3d_intern.h"  /* own include */
+
+/* -------------------------------------------------------------------- */
+/* Ruler Item (we can have many) */
+enum {
+	RULERITEM_USE_ANGLE = (1 << 0),  /* use protractor */
+	RULERITEM_USE_RAYCAST = (1 << 1)
+};
+
+enum {
+	RULERITEM_DIRECTION_IN = 0,
+	RULERITEM_DIRECTION_OUT
+};
+
+#define RULER_PICK_DIST 75.0f
+#define RULER_PICK_DIST_SQ (RULER_PICK_DIST * RULER_PICK_DIST)
+
+typedef struct RulerItem {
+	struct RulerItem *next, *prev;
+
+	/* worldspace coords, middle being optional */
+	float co[3][3];
+
+	/* selected coord */
+	char  co_index; /* 0 -> 2*/
+
+	int   flag;
+	int   raycast_dir;  /* RULER_DIRECTION_* */
+} RulerItem;
+
+enum {
+	RULER_STATE_NORMAL = 0,
+	RULER_STATE_DRAG
+};
+
+
+/* -------------------------------------------------------------------- */
+/* Ruler Info (one per session) */
+
+typedef struct RulerInfo {
+	ListBase items;
+	int      item_active;
+	int flag;
+	int snap_flag;
+	int state;
+
+	/* --- */
+	ARegion *ar;
+	void *draw_handle_pixel;
+} RulerInfo;
+
+/* -------------------------------------------------------------------- */
+/* local functions */
+static RulerItem *ruler_item_add(RulerInfo *ruler_info)
+{
+	RulerItem *ruler_item = MEM_callocN(sizeof(RulerItem), "RulerItem");
+	BLI_addtail(&ruler_info->items, ruler_item);
+	return ruler_item;
+}
+
+#if 0
+static void ruler_item_remove(RulerInfo *ruler_info, RulerItem *ruler_item)
+{
+	BLI_remlink(&ruler_info->items, ruler_item);
+	MEM_freeN(ruler_item);
+}
+#endif
+
+static RulerItem *ruler_item_active_get(RulerInfo *ruler_info)
+{
+	return BLI_findlink(&ruler_info->items, ruler_info->item_active);
+}
+
+static void ruler_item_active_set(RulerInfo *ruler_info, RulerItem *ruler_item)
+{
+	ruler_info->item_active = BLI_findindex(&ruler_info->items, ruler_item);
+}
+
+static bool view3d_ruler_pick(RulerInfo *ruler_info, const float mval[2],
+                              RulerItem **r_ruler_item, int *r_co_index)
+{
+	ARegion *ar = ruler_info->ar;
+	RulerItem *ruler_item;
+
+	float dist_best = RULER_PICK_DIST_SQ;
+	RulerItem *ruler_item_best = NULL;
+	int co_index_best = -1;
+
+	for (ruler_item = ruler_info->items.first; ruler_item; ruler_item = ruler_item->next) {
+		float co_ss[3][2];
+		float dist;
+		int j;
+
+		/* should these be checked? - ok for now not to */
+		for (j = 0; j < 3; j++) {
+			ED_view3d_project_float_global(ar, ruler_item->co[j], co_ss[j], V3D_PROJ_TEST_NOP);
+		}
+
+		if (ruler_item->flag & RULERITEM_USE_ANGLE) {
+			dist = min_ff(dist_squared_to_line_segment_v2(mval, co_ss[0], co_ss[1]),
+			              dist_squared_to_line_segment_v2(mval, co_ss[1], co_ss[2]));
+			if (dist < dist_best) {
+				dist_best = dist;
+				ruler_item_best = ruler_item;
+
+				{
+					float dist_points[3] = {len_squared_v2v2(co_ss[0], mval),
+					                        len_squared_v2v2(co_ss[1], mval),
+					                        len_squared_v2v2(co_ss[2], mval)};
+					if (min_fff(UNPACK3(dist_points)) < RULER_PICK_DIST_SQ) {
+						co_index_best = min_axis_v3(dist_points);
+					}
+					else {
+						co_index_best = -1;
+					}
+				}
+			}
+		}
+		else {
+			dist = dist_squared_to_line_segment_v2(mval, co_ss[0], co_ss[2]);
+			if (dist < dist_best) {
+				dist_best = dist;
+				ruler_item_best = ruler_item;
+
+				{
+					float dist_points[2] = {len_squared_v2v2(co_ss[0], mval),
+					                        len_squared_v2v2(co_ss[2], mval)};
+					if (min_ff(UNPACK2(dist_points)) < RULER_PICK_DIST_SQ) {
+						co_index_best = (dist_points[0] < dist_points[1]) ? 0 : 2;
+					}
+					else {
+						co_index_best = -1;
+					}
+				}
+			}
+		}
+	}
+
+	if (ruler_item_best) {
+		*r_ruler_item = ruler_item_best;
+		*r_co_index = co_index_best;
+		return true;
+	}
+	else {
+		*r_ruler_item = NULL;
+		*r_co_index = -1;
+		return false;
+	}
+}
+
+/* -------------------------------------------------------------------- */
+/* local callbacks */
+
+static void ruler_info_draw_pixel(const struct bContext *C, ARegion *ar, void *arg)
+{
+	Scene *scene = CTX_data_scene(C);
+	UnitSettings *unit = &scene->unit;
+	const int do_split = unit->flag & USER_UNIT_OPT_SPLIT;
+	RulerItem *ruler_item;
+	RulerInfo *ruler_info = arg;
+	RegionView3D *rv3d = ruler_info->ar->regiondata;
+//	ARegion *ar = ruler_info->ar;
+	const float cap_size = 4.0f;
+	const float bg_margin = 4.0f * U.pixelsize;
+	const float bg_radius = 4.0f * U.pixelsize;
+	const float arc_size = 64.0f * U.pixelsize;
+#define ARC_STEPS 24
+	const int arc_steps = ARC_STEPS;
+	int i;
+	//unsigned int color_act = 0x666600;
+	unsigned int color_act = 0xffffff;
+	unsigned int color_base = 0x0;
+	unsigned char color_back[4] = {0xff, 0xff, 0xff, 0x80};
+	unsigned char color_text[3];
+	unsigned char color_wire[3];
+
+	/* anti-aliased lines for more consistent appearance */
+	glEnable(GL_LINE_SMOOTH);
+
+	BLF_enable(blf_mono_font, BLF_ROTATION);
+	BLF_size(blf_mono_font, 14 * U.pixelsize, U.dpi);
+
+	UI_GetThemeColor3ubv(TH_TEXT, color_text);
+	UI_GetThemeColor3ubv(TH_WIRE, color_wire);
+
+	for (ruler_item = ruler_info->items.first, i = 0; ruler_item; ruler_item = ruler_item->next, i++) {
+		const bool is_act = (i == ruler_info->item_active);
+		float dir_ruler[2];
+		float co_ss[3][2];
+		int j;
+
+		/* should these be checked? - ok for now not to */
+		for (j = 0; j < 3; j++) {
+			ED_view3d_project_float_global(ar, ruler_item->co[j], co_ss[j], V3D_PROJ_TEST_NOP);
+		}
+
+		glEnable(GL_BLEND);
+
+		cpack(is_act ? color_act : color_base);
+
+		if (ruler_item->flag & RULERITEM_USE_ANGLE) {
+			const float ruler_angle = angle_v3v3v3(ruler_item->co[0],
+			                                       ruler_item->co[1],
+			                                       ruler_item->co[2]);
+			glBegin(GL_LINE_STRIP);
+			for (j = 0; j < 3; j++) {
+				glVertex2fv(co_ss[j]);
+			}
+			glEnd();
+			cpack(0xaaaaaa);
+			setlinestyle(3);
+			glBegin(GL_LINE_STRIP);
+			for (j = 0; j < 3; j++) {
+				glVertex2fv(co_ss[j]);
+			}
+			glEnd();
+			setlinestyle(0);
+
+			/* arc */
+			{
+				float dir_tmp[3];
+				float co_tmp[3];
+				float arc_ss_coords[ARC_STEPS + 1][2];
+
+				float dir_a[3];
+				float dir_b[3];
+				float quat[4];
+				float axis[3];
+				float angle;
+				int j;
+				const float px_scale = ED_view3d_pixel_size(rv3d, ruler_item->co[1]) * arc_size;
+
+				sub_v3_v3v3(dir_a, ruler_item->co[0], ruler_item->co[1]);
+				sub_v3_v3v3(dir_b, ruler_item->co[2], ruler_item->co[1]);
+				normalize_v3(dir_a);
+				normalize_v3(dir_b);
+
+				cross_v3_v3v3(axis, dir_a, dir_b);
+				angle = angle_normalized_v3v3(dir_a, dir_b);
+
+				axis_angle_to_quat(quat, axis, angle / arc_steps);
+
+				copy_v3_v3(dir_tmp, dir_a);
+
+				glColor3ubv(color_wire);
+
+				for(j = 0; j <= arc_steps; j++) {
+					madd_v3_v3v3fl(co_tmp, ruler_item->co[1], dir_tmp, px_scale);
+					ED_view3d_project_float_global(ar, co_tmp, arc_ss_coords[j], V3D_PROJ_TEST_NOP);
+					mul_qt_v3(quat, dir_tmp);

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list