[Bf-blender-cvs] [bc125b415d5] greasepencil-object: Cleanup: Reshuffling order of code in file

Joshua Leung noreply at git.blender.org
Wed Jan 31 04:11:14 CET 2018


Commit: bc125b415d5549e7a0c5c9aca9037cf2cccfe48a
Author: Joshua Leung
Date:   Wed Jan 31 15:50:04 2018 +1300
Branches: greasepencil-object
https://developer.blender.org/rBbc125b415d5549e7a0c5c9aca9037cf2cccfe48a

Cleanup: Reshuffling order of code in file

In future: Keep related code together, instead of just dumping
it "wherever" (aka at the end of the file, since it's most
convenient right now)

(In the process, I've ended up flagging a few pieces of code
for further investigation. They likely duplicate some code
elsewhere in other modules)

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

M	source/blender/editors/gpencil/gpencil_utils.c

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

diff --git a/source/blender/editors/gpencil/gpencil_utils.c b/source/blender/editors/gpencil/gpencil_utils.c
index 51879fde5ac..e29d40ebb39 100644
--- a/source/blender/editors/gpencil/gpencil_utils.c
+++ b/source/blender/editors/gpencil/gpencil_utils.c
@@ -357,6 +357,117 @@ const EnumPropertyItem *ED_gpencil_layers_with_new_enum_itemf(
 	return item;
 }
 
+/* Dynamic Enums of GP Brushes */
+// XXX: Duplicates rna_GPencilBrush_enum_itemf()
+const EnumPropertyItem *ED_gpencil_brushes_enum_itemf(
+        bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop),
+        bool *r_free)
+{
+	ToolSettings *ts = CTX_data_tool_settings(C);
+	bGPDbrush *brush;
+	EnumPropertyItem *item = NULL, item_tmp = { 0 };
+	int totitem = 0;
+	int i = 0;
+
+	if (ELEM(NULL, C, ts)) {
+		return DummyRNA_DEFAULT_items;
+	}
+
+	/* Existing brushes */
+	for (brush = ts->gp_brushes.first; brush; brush = brush->next, i++) {
+		item_tmp.identifier = brush->info;
+		item_tmp.name = brush->info;
+		item_tmp.value = i;
+
+		if (brush->flag & GP_BRUSH_ACTIVE)
+			if (brush->flag & GP_BRUSH_FILL_ONLY) {
+				item_tmp.icon = ICON_GPBRUSH_FILL;
+			}
+			else {
+				item_tmp.icon = ED_gpencil_get_brush_icon(brush->icon);
+			}
+		else
+			item_tmp.icon = ICON_NONE;
+
+		RNA_enum_item_add(&item, &totitem, &item_tmp);
+	}
+
+	RNA_enum_item_end(&item, &totitem);
+	*r_free = true;
+
+	return item;
+}
+
+/* Dynamic Enums of GP Palettes */
+// XXX: Deprecated
+const EnumPropertyItem *ED_gpencil_palettes_enum_itemf(
+        bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop),
+        bool *r_free)
+{
+	bGPdata *gpd = CTX_data_gpencil_data(C);
+	bGPDpalette *palette;
+	EnumPropertyItem *item = NULL, item_tmp = { 0 };
+	int totitem = 0;
+	int i = 0;
+
+	if (ELEM(NULL, C, gpd)) {
+		return DummyRNA_DEFAULT_items;
+	}
+
+	/* Existing palettes */
+	for (palette = gpd->palettes.first; palette; palette = palette->next, i++) {
+		item_tmp.identifier = palette->info;
+		item_tmp.name = palette->info;
+		item_tmp.value = i;
+
+		if (palette->flag & PL_PALETTE_ACTIVE)
+			item_tmp.icon = ICON_COLOR;
+		else
+			item_tmp.icon = ICON_NONE;
+
+		RNA_enum_item_add(&item, &totitem, &item_tmp);
+	}
+
+	RNA_enum_item_end(&item, &totitem);
+	*r_free = true;
+
+	return item;
+}
+
+/* helper to get brush icon */
+// XXX: Remove redundant breaks
+int ED_gpencil_get_brush_icon(int type)
+{
+	switch (type) {
+		case GPBRUSH_CUSTOM:
+			return ICON_GPBRUSH_CUSTOM;
+			break;
+		case GPBRUSH_PENCIL:
+			return ICON_GPBRUSH_PENCIL;
+			break;
+		case GPBRUSH_PEN:
+			return ICON_GPBRUSH_PEN;
+			break;
+		case GPBRUSH_INK:
+			return ICON_GPBRUSH_INK;
+			break;
+		case GPBRUSH_INKNOISE:
+			return ICON_GPBRUSH_INKNOISE;
+			break;
+		case GPBRUSH_BLOCK:
+			return ICON_GPBRUSH_BLOCK;
+			break;
+		case GPBRUSH_MARKER:
+			return ICON_GPBRUSH_MARKER;
+			break;
+		case GPBRUSH_FILL:
+			return ICON_GPBRUSH_FILL;
+			break;
+		default:
+			return ICON_GPBRUSH_CUSTOM;
+			break;
+	}
+}
 
 
 /* ******************************************************** */
@@ -700,108 +811,283 @@ bool gp_point_xy_to_3d(GP_SpaceConversion *gsc, Scene *scene, const float screen
 }
 
 /**
- * Subdivide a stroke once, by adding a point half way between each pair of existing points
- * \param gps           Stroke data
- * \param sublevel      Number of times to subdivide
+ * Convert tGPspoint (temporary 2D/screenspace point data used by GP modal operators)
+ * to 3D coordinates.
+ *
+ * \param point2D: The screenspace 2D point data to convert
+ * \param[out] r_out: The resulting 2D point data
  */
-void gp_subdivide_stroke(bGPDstroke *gps, const int sublevel)
+void gp_stroke_convertcoords_tpoint(Scene *scene, ARegion *ar, View3D *v3d, 
+								struct Object *ob, bGPDlayer *gpl, 
+								const tGPspoint *point2D, float *depth, float r_out[3])
 {
-	bGPDspoint *temp_points;
-	int totnewpoints, oldtotpoints;
-	int i2;
+	ToolSettings *ts = scene->toolsettings;
+	const int mval[2] = { point2D->x, point2D->y };
+	float mval_f[2];
+	ARRAY_SET_ITEMS(mval_f, point2D->x, point2D->y);
+	float mval_prj[2];
+	float rvec[3], dvec[3];
+	float zfac;
 
-	/* loop as many times as levels */
-	for (int s = 0; s < sublevel; s++) {
-		totnewpoints = gps->totpoints - 1;
-		/* duplicate points in a temp area */
-		temp_points = MEM_dupallocN(gps->points);
-		oldtotpoints = gps->totpoints;
+	if ((depth != NULL) && (ED_view3d_autodist_simple(ar, mval, r_out, 0, depth))) {
+		/* projecting onto 3D-Geometry
+		*	- nothing more needs to be done here, since view_autodist_simple() has already done it
+		*/
+	}
+	else {
+		/* Current method just converts each point in screen-coordinates to
+		* 3D-coordinates using the 3D-cursor as reference.
+		*/
+		ED_gp_get_drawing_reference(v3d, scene, ob, gpl, ts->gpencil_v3d_align, rvec);
+		zfac = ED_view3d_calc_zfac(ar->regiondata, rvec, NULL);
 
-		/* resize the points arrys */
-		gps->totpoints += totnewpoints;
-		gps->points = MEM_recallocN(gps->points, sizeof(*gps->points) * gps->totpoints);
-		gps->flag |= GP_STROKE_RECALC_CACHES;
+		if (ED_view3d_project_float_global(ar, rvec, mval_prj, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
+			sub_v2_v2v2(mval_f, mval_prj, mval_f);
+			ED_view3d_win_to_delta(ar, mval_f, dvec, zfac);
+			sub_v3_v3v3(r_out, rvec, dvec);
+		}
+		else {
+			zero_v3(r_out);
+		}
+	}
+}
 
-		/* move points from last to first to new place */
-		i2 = gps->totpoints - 1;
-		for (int i = oldtotpoints - 1; i > 0; i--) {
-			bGPDspoint *pt = &temp_points[i];
-			bGPDspoint *pt_final = &gps->points[i2];
 
-			copy_v3_v3(&pt_final->x, &pt->x);
-			pt_final->pressure = pt->pressure;
-			pt_final->strength = pt->strength;
-			pt_final->time = pt->time;
-			pt_final->flag = pt->flag;
-			pt_final->totweight = pt->totweight;
-			pt_final->weights = pt->weights;
-			i2 -= 2;
-		}
-		/* interpolate mid points */
-		i2 = 1;
-		for (int i = 0; i < oldtotpoints - 1; i++) {
-			bGPDspoint *pt = &temp_points[i];
-			bGPDspoint *next = &temp_points[i + 1];
-			bGPDspoint *pt_final = &gps->points[i2];
+/**
+ * Reproject the points of the stroke to a plane locked to axis to avoid stroke offset
+ */
+void ED_gp_project_stroke_to_plane(Object *ob, RegionView3D *rv3d, bGPDstroke *gps, const float origin[3], const int axis, char UNUSED(type))
+{
+	float plane_normal[3];
+	float vn[3];
 
-			/* add a half way point */
-			interp_v3_v3v3(&pt_final->x, &pt->x, &next->x, 0.5f);
-			pt_final->pressure = interpf(pt->pressure, next->pressure, 0.5f);
-			pt_final->strength = interpf(pt->strength, next->strength, 0.5f);
-			CLAMP(pt_final->strength, GPENCIL_STRENGTH_MIN, 1.0f);
-			pt_final->time = interpf(pt->time, next->time, 0.5f);
-			pt_final->totweight = 0;
-			pt_final->weights = NULL;
-			i2 += 2;
+	float ray[3];
+	float rpoint[3];
+
+	/* normal vector for a plane locked to axis */
+	zero_v3(plane_normal);
+	if (axis < 0) {
+		/* if the axis is not locked, need a vector to the view direction
+		* in order to get the right size of the stroke.
+		*/
+		ED_view3d_global_to_vector(rv3d, origin, plane_normal);
+	}
+	else {
+		plane_normal[axis] = 1.0f;
+		/* if object, apply object rotation */
+		if (ob && (ob->type == OB_GPENCIL)) {
+			mul_mat3_m4_v3(ob->obmat, plane_normal);
 		}
+	}
 
-		MEM_SAFE_FREE(temp_points);
+	/* Reproject the points in the plane */
+	for (int i = 0; i < gps->totpoints; i++) {
+		bGPDspoint *pt = &gps->points[i];
 
-		/* move points to smooth stroke */
-		/* duplicate points in a temp area with the new subdivide data */
-		temp_points = MEM_dupallocN(gps->points);
+		/* get a vector from the point with the current view direction of the viewport */
+		ED_view3d_global_to_vector(rv3d, &pt->x, vn);
 
-		/* extreme points are not changed */
-		for (int i = 0; i < gps->totpoints - 2; i++) {
-			bGPDspoint *pt = &temp_points[i];
-			bGPDspoint *next = &temp_points[i + 1];
-			bGPDspoint *pt_final = &gps->points[i + 1];
+		/* calculate line extrem point to create a ray that cross the plane */
+		mul_v3_fl(vn, -50.0f);
+		add_v3_v3v3(ray, &pt->x, vn);
 
-			/* move point */
-			interp_v3_v3v3(&pt_final->x, &pt->x, &next->x, 0.5f);
+		/* if the line never intersect, the point is not changed */
+		if (isect_line_plane_v3(rpoint, &pt->x, ray, origin, plane_normal)) {
+			copy_v3_v3(&pt->x, rpoint);
 		}
-		/* free temp memory */
-		MEM_SAFE_FREE(temp_points);
 	}
 }
 
 /**
- * Add randomness to stroke
- * \param gps           Stroke data
- * \param brush         Brush data
+ * Reproject one points to a plane locked to axis to avoid stroke offset
  */
-void gp_randomize_stroke(bGPDstroke *gps, bGPDbrush *brush)
+void ED_gp_project_point_to_plane(Object *ob, RegionView3D *rv3d, const float origin[3], const int axis, char UNUSED(type), bGPDspoint *pt)
 {
-	bGPDspoint *pt1, *pt2, *pt3;
-	float v1[3];
-	float v2[3];
-	if (gps->totpoints < 3) {
-		return;
-	}
-
-	/* get two vectors using 3 points */
-	pt1 = &gps->points[0];
-	pt2 = &gps->points[1];
-	pt3 = &gps->points[(int)(gps->totpoints * 0.75)];
+	float plane_normal[3];
+	float vn[3];
 
-	sub_v3_v3v3(v1, &pt2->x, &pt1->x);
-	sub_v3_v3v3(v2, &pt3->x, &pt2->x);
-	normalize_v3(v1);
-	normalize_v3(v2);
+	float ray[3];
+	float rpoint[3];
 
-	/* get normal vector to plane created by two vectors */
-	float normal[3];
-	cross_v3_v3v3(normal, v1, v2);
+	/* normal vector for a plane locked to axis */
+	zero_v3(plane_normal);
+	if (axis < 0) {
+		/* if the axis is not locked, need a vector to the view direction 
+		 * in order to get the right size of the stroke.
+		 */
+		ED_view3d_global_to_vector(rv3d, origin, plane_normal);
+	}
+	else {
+		plane_normal[axis] = 1.0f;
+		/* if object, apply object rotation */
+		if (ob && (ob->type == OB_GPENCIL)) {
+			mul_mat3_m4_v3(ob->obmat, plane_normal);
+		}
+	}
+
+
+	/* Reproject the points in the plane */
+	/* get a vector from the point with the current view direction of the viewport */
+	ED_view3d_global_to_vector(rv3d, &pt->x, vn);
+
+	/* calculate line extrem point to create a ray that cross the plane */
+	mul_v3_fl(vn, -50.0f);
+	add_v3_v3v3(ray, &pt->x, vn);
+
+	/* if the line never intersect, the point is not changed */
+	if (isect_line_plane_v3(rpoint, &pt->x, ray, origin, plane_normal)) {
+		copy_v3_v3(&pt->x, rp

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list