[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [16987] trunk/blender/source/blender: Grease Pencil:

Joshua Leung aligorith at gmail.com
Thu Oct 9 01:42:00 CEST 2008


Revision: 16987
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=16987
Author:   aligorith
Date:     2008-10-09 01:42:00 +0200 (Thu, 09 Oct 2008)

Log Message:
-----------
Grease Pencil:

Added comments a few + stroke simplification code (hidden behind rt and userpref, adapted from theeth's etch-a-ton code). This shouldn't have any other adverse effects.

Modified Paths:
--------------
    trunk/blender/source/blender/makesdna/DNA_userdef_types.h
    trunk/blender/source/blender/src/gpencil.c
    trunk/blender/source/blender/src/space.c

Modified: trunk/blender/source/blender/makesdna/DNA_userdef_types.h
===================================================================
--- trunk/blender/source/blender/makesdna/DNA_userdef_types.h	2008-10-08 23:17:02 UTC (rev 16986)
+++ trunk/blender/source/blender/makesdna/DNA_userdef_types.h	2008-10-08 23:42:00 UTC (rev 16987)
@@ -327,7 +327,8 @@
 /* tw_flag (transform widget) */
 
 /* gp_settings (Grease Pencil Settings) */
-#define GP_PAINT_DOSMOOTH	(1<<0)
+#define GP_PAINT_DOSMOOTH		(1<<0)
+#define GP_PAINT_DOSIMPLIFY		(1<<1)
 
 
 #endif

Modified: trunk/blender/source/blender/src/gpencil.c
===================================================================
--- trunk/blender/source/blender/src/gpencil.c	2008-10-08 23:17:02 UTC (rev 16986)
+++ trunk/blender/source/blender/src/gpencil.c	2008-10-08 23:42:00 UTC (rev 16987)
@@ -1186,6 +1186,16 @@
 		float *fp= give_cursor();
 		float dvec[3];
 		
+		/* Current method just converts each point in screen-coordinates to 
+		 * 3D-coordinates using the 3D-cursor as reference. In general, this 
+		 * works OK, but it could of course be improved.
+		 *
+		 * TODO:
+		 *	- investigate using nearest point(s) on a previous stroke as
+		 *	  reference point instead or as offset, for easier stroke matching
+		 *	- investigate projection onto geometry (ala retopo)
+		 */
+		
 		/* method taken from editview.c - mouse_cursor() */
 		project_short_noclip(fp, mval);
 		window_to_3d(dvec, mval[0]-mx, mval[1]-my);
@@ -1203,8 +1213,7 @@
 	}
 	
 	/* 2d - on image 'canvas' (assume that p->v2d is set) */
-	else if ( (gpd->sbuffer_sflag & GP_STROKE_2DIMAGE) && (p->v2d) ) 
-	{
+	else if ((gpd->sbuffer_sflag & GP_STROKE_2DIMAGE) && (p->v2d)) {
 		/* for now - space specific */
 		switch (p->sa->spacetype) {
 			case SPACE_SEQ: /* sequencer */
@@ -1285,7 +1294,7 @@
 	int i=0, cmx=gpd->sbuffer_size;
 	
 	/* only smooth if smoothing is enabled, and we're not doing a straight line */
-	if ( !(U.gp_settings & GP_PAINT_DOSMOOTH) || GP_BUFFER2STROKE_ENDPOINTS)
+	if (!(U.gp_settings & GP_PAINT_DOSMOOTH) || GP_BUFFER2STROKE_ENDPOINTS)
 		return;
 	
 	/* don't try if less than 2 points in buffer */
@@ -1305,7 +1314,78 @@
 	}
 }
 
+/* simplify a stroke (in buffer) before storing it 
+ *	- applies a reverse Chaikin filter
+ *	- code adapted from etch-a-ton branch (editarmature_sketch.c)
+ */
+static void gp_stroke_simplify (tGPsdata *p)
+{
+	bGPdata *gpd= p->gpd;
+	tGPspoint *old_points= (tGPspoint *)gpd->sbuffer;
+	short num_points= gpd->sbuffer_size;
+	short flag= gpd->sbuffer_sflag;
+	short i, j;
+	
+	/* only simplify if simlification is enabled, and we're not doing a straight line */
+	if (!(U.gp_settings & GP_PAINT_DOSIMPLIFY) || GP_BUFFER2STROKE_ENDPOINTS)
+		return;
+	
+	/* don't simplify if less than 4 points in buffer */
+	if ((num_points <= 2) || (old_points == NULL))
+		return;
+		
+	/* clear buffer (but don't free mem yet) so that we can write to it 
+	 *	- firstly set sbuffer to NULL, so a new one is allocated
+	 *	- secondly, reset flag after, as it gets cleared auto
+	 */
+	gpd->sbuffer= NULL;
+	gp_session_validatebuffer(p);
+	gpd->sbuffer_sflag = flag;
+	
+/* macro used in loop to get position of new point
+ *	- used due to the mixture of datatypes in use here
+ */
+#define GP_SIMPLIFY_AVPOINT(offs, sfac) \
+	{ \
+		co[0] += (float)(old_points[offs].x * sfac); \
+		co[1] += (float)(old_points[offs].y * sfac); \
+		pressure += old_points[offs].pressure * sfac; \
+	}
+	
+	for (i = 0, j = 0; i < num_points; i++)
+	{
+		if (i - j == 3)
+		{
+			float co[2], pressure;
+			short mco[2];
+			
+			/* initialise values */
+			co[0]= 0;
+			co[1]= 0;
+			pressure = 0;
+			
+			/* using macro, calculate new point */
+			GP_SIMPLIFY_AVPOINT(j, -0.25);
+			GP_SIMPLIFY_AVPOINT(j+1, 0.75);
+			GP_SIMPLIFY_AVPOINT(j+2, 0.75);
+			GP_SIMPLIFY_AVPOINT(j+3, -0.25);
+			
+			/* set values for adding */
+			mco[0]= (short)co[0];
+			mco[1]= (short)co[1];
+			
+			/* ignore return values on this... assume to be ok for now */
+			gp_stroke_addpoint(p, mco, pressure);
+			
+			j += 2;
+		}
+	} 
+	
+	/* free old buffer */
+	MEM_freeN(old_points);
+}
 
+
 /* make a new stroke from the buffer data */
 static void gp_stroke_newfrombuffer (tGPsdata *p)
 {
@@ -1804,6 +1884,9 @@
 		/* smooth stroke before transferring? */
 		gp_stroke_smooth(p);
 		
+		/* simplify stroke before transferring? */
+		gp_stroke_simplify(p);
+		
 		/* transfer stroke to frame */
 		gp_stroke_newfrombuffer(p);
 	}

Modified: trunk/blender/source/blender/src/space.c
===================================================================
--- trunk/blender/source/blender/src/space.c	2008-10-08 23:17:02 UTC (rev 16986)
+++ trunk/blender/source/blender/src/space.c	2008-10-08 23:42:00 UTC (rev 16987)
@@ -4120,14 +4120,22 @@
 			(xpos+(5*midsp)+(3*mpref)+(spref*2.5)),y5,(spref*1.5),buth,
 			&(U.gp_euclideandist), 0, 100, 0, 0, "Distance moved by mouse when drawing stroke (in pixels) to include");
 		
+		uiDefButBitS(block, TOG, GP_PAINT_DOSMOOTH, 0,"Smooth Stroke",
+			(xpos+(4*midsp)+(3*mpref)+spref),y4,(spref*1.5),buth,
+			&(U.gp_settings), 0, 100, 0, 0, "Smooth the final stroke");
+			
+		// currently hidden behind G.rt, as it is not that useful yet
+		if (G.rt) {
+			uiDefButBitS(block, TOG, GP_PAINT_DOSIMPLIFY, 0,"Simplify Stroke",
+				(xpos+(5*midsp)+(3*mpref)+(spref*2.5)),y4,(spref*1.5),buth,
+				&(U.gp_settings), 0, 100, 0, 0, "Simplify the final stroke");
+		}
+		uiBlockEndAlign(block);
 		
 		uiDefButS(block, NUM, 0, "Eraser Radius:",
-			(xpos+(7*midsp)+(3*mpref)+(3.75*spref)),y5,spref*1.5,buth,
+			(xpos+(7*midsp)+(3*mpref)+(4*spref)),y5,mpref,buth,
 			&(U.gp_eraser), 0, 100, 0, 0, "Radius of eraser 'brush'");
-		uiDefButBitS(block, TOG, GP_PAINT_DOSMOOTH, 0,"Smooth Stroke",
-			(xpos+(8*midsp)+(3*mpref)+(5*spref)),y5,spref,buth,
-			&(U.gp_settings), 0, 100, 0, 0, "Smooth the final stroke");
-		uiBlockEndAlign(block);
+		
 	
 	} else if(U.userpref == 2) { /* language & colors */
 





More information about the Bf-blender-cvs mailing list