[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [22404] branches/blender2.5/blender/source /blender: when units are enabled use them for 3d viewport grid drawing, display the dimension for the smallest grid cell.

Campbell Barton ideasman42 at gmail.com
Wed Aug 12 16:11:55 CEST 2009


Revision: 22404
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=22404
Author:   campbellbarton
Date:     2009-08-12 16:11:53 +0200 (Wed, 12 Aug 2009)

Log Message:
-----------
when units are enabled use them for 3d viewport grid drawing, display the dimension for the smallest grid cell.

still need to make this work with grid snapping.

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/blenkernel/BKE_unit.h
    branches/blender2.5/blender/source/blender/blenkernel/intern/unit.c
    branches/blender2.5/blender/source/blender/editors/interface/interface.c
    branches/blender2.5/blender/source/blender/editors/space_view3d/view3d_draw.c

Modified: branches/blender2.5/blender/source/blender/blenkernel/BKE_unit.h
===================================================================
--- branches/blender2.5/blender/source/blender/blenkernel/BKE_unit.h	2009-08-12 13:07:12 UTC (rev 22403)
+++ branches/blender2.5/blender/source/blender/blenkernel/BKE_unit.h	2009-08-12 14:11:53 UTC (rev 22404)
@@ -37,8 +37,27 @@
 int		bUnit_ReplaceString(char *str, char *str_orig, char *str_prev, double scale_pref, int system, int type);
 
 /* the size of the unit used for this value (used for calculating the ckickstep) */
-double bUnit_Size(double value, int system, int type);
+double bUnit_ClosestScalar(double value, int system, int type);
 
+/* loop over scales, coudl add names later */
+//double bUnit_Iter(void **unit, char **name, int system, int type);
+
+void	bUnit_GetSystem(void **usys_pt, int *len, int system, int type);
+char*	bUnit_GetName(void *usys_pt, int index);
+char*	bUnit_GetNamePlural(void *usys_pt, int index);
+double	bUnit_GetScaler(void *usys_pt, int index);
+
+/* aligned with PropertyUnit */
+#define		B_UNIT_NONE 0
+#define 	B_UNIT_LENGTH 1
+#define 	B_UNIT_AREA 2
+#define 	B_UNIT_VOLUME 3
+#define 	B_UNIT_MASS 4
+#define 	B_UNIT_ROTATION 5
+#define 	B_UNIT_TIME 6
+#define 	B_UNIT_VELOCITY 7
+#define 	B_UNIT_ACCELERATION 8
+
 #ifdef __cplusplus
 }
 #endif

Modified: branches/blender2.5/blender/source/blender/blenkernel/intern/unit.c
===================================================================
--- branches/blender2.5/blender/source/blender/blenkernel/intern/unit.c	2009-08-12 13:07:12 UTC (rev 22403)
+++ branches/blender2.5/blender/source/blender/blenkernel/intern/unit.c	2009-08-12 14:11:53 UTC (rev 22404)
@@ -30,19 +30,24 @@
 /* define a single unit */
 typedef struct bUnitDef {
 	char *name;
-	char *name_plural;	/* can be NULL */
+	char *name_plural;	/* abused a bit for the display name */
 	char *name_short;	/* this is used for display*/
 	char *name_alt;		/* can be NULL */
 	
-	double mul;
+	double scalar;
 	double bias;		/* not used yet, needed for converting temperature */
+	int flag;
 } bUnitDef;
 
+#define B_UNIT_DEF_NONE 0
+#define B_UNIT_DEF_SUPPRESS 1 /* Use for units that are not used enough to be translated into for common use */
+
 /* define a single unit */
 typedef struct bUnitCollection {
 	struct bUnitDef *units;
 	int base_unit;				/* use for 0.0, or none given */
 	int flag;					/* options for this system */
+	int length;					/* to quickly find the last item */
 } bUnitCollection;
 
 /* Dummy */
@@ -50,45 +55,49 @@
 	{"", NULL, "", NULL,	1.0, 0.0},
 	{NULL, NULL, NULL,	NULL, 0.0, 0.0}
 };
-static struct bUnitCollection buDummyCollecton = {buDummyDef, 0, 0};
+static struct bUnitCollection buDummyCollecton = {buDummyDef, 0, 0, sizeof(buDummyDef)};
 
 
 /* Lengths */
 static struct bUnitDef buMetricLenDef[] = {
-	{"kilometer", "kilometers",		"km", NULL,	1000.0, 0.0},
-	{"meter", "meters",				"m",  NULL,	1.0, 0.0}, /* base unit */
-	{"centimeter", "centimeters",	"cm", NULL,	0.01, 0.0},
-	{"millimeter", "millimeters",	"mm", NULL,	0.001, 0.0},
-	{"micrometer", "micrometers",	"um", "µm",	0.000001, 0.0}, // micron too?
-	{"nanometer", "nanometers",		"nm", NULL,	0.000000001, 0.0},
-	{"picometer", "picometers",		"pm", NULL,	0.000000000001, 0.0},
+	{"kilometer", "Kilometers",		"km", NULL,	1000.0, 0.0,		B_UNIT_DEF_NONE},
+	{"hectometer", "100 Meters",	"hm", NULL,	100.0, 0.0,			B_UNIT_DEF_SUPPRESS},
+	{"dekameter", "10 Meters",		"dkm",NULL,	10.0, 0.0,			B_UNIT_DEF_SUPPRESS},
+	{"meter", "Meters",				"m",  NULL,	1.0, 0.0, 			B_UNIT_DEF_NONE}, /* base unit */
+	{"decimetre", "10 Centimeters",	"dm", NULL,	0.1, 0.0,			B_UNIT_DEF_SUPPRESS},
+	{"centimeter", "Centimeters",	"cm", NULL,	0.01, 0.0,			B_UNIT_DEF_NONE},
+	{"millimeter", "Millimeters",	"mm", NULL,	0.001, 0.0,			B_UNIT_DEF_NONE},
+	{"micrometer", "Micrometers",	"um", "µm",	0.000001, 0.0,		B_UNIT_DEF_NONE}, // micron too?
+	{"nanometer", "Nanometers",		"nm", NULL,	0.000000001, 0.0,	B_UNIT_DEF_NONE},
+	{"picometer", "Picometers",		"pm", NULL,	0.000000000001, 0.0,B_UNIT_DEF_NONE},
 	{NULL, NULL, NULL,	NULL, 0.0, 0.0}
 };
-static struct bUnitCollection buMetricLenCollecton = {buMetricLenDef, 1, 0};
+static struct bUnitCollection buMetricLenCollecton = {buMetricLenDef, 1, 0, sizeof(buMetricLenDef)/sizeof(bUnitDef)};
 
 static struct bUnitDef buImperialLenDef[] = {
-	{"mile", "miles",				"mi", "m",	1609.344, 0.0},
-	{"yard", "yards",				"yd", NULL,	0.9144, 0.0},
-	{"foot", "feet",				"'", "ft",	0.3048, 0.0},
-	{"inch", "inches",				"\"", "in",	0.0254, 0.0}, /* base unit */
-	{"thou", "thous",				"mil", NULL,0.0000254, 0.0},
+	{"mile", "Miles",				"mi", "m",	1609.344, 0.0,	B_UNIT_DEF_NONE},
+	{"furlong", "Furlongs",			"fur", NULL,201.168, 0.0,	B_UNIT_DEF_SUPPRESS},
+	{"yard", "Yards",				"yd", NULL,	0.9144, 0.0,	B_UNIT_DEF_NONE},
+	{"foot", "Feet",				"'", "ft",	0.3048, 0.0,	B_UNIT_DEF_NONE},
+	{"inch", "Inches",				"\"", "in",	0.0254, 0.0,	B_UNIT_DEF_NONE}, /* base unit */
+	{"thou", "Thous",				"mil", NULL,0.0000254, 0.0,	B_UNIT_DEF_NONE},
 	{NULL, NULL, NULL, NULL, 0.0, 0.0}
 };
-static struct bUnitCollection buImperialLenCollecton = {buImperialLenDef, 2, 0};
+static struct bUnitCollection buImperialLenCollecton = {buImperialLenDef, 2, 0, sizeof(buImperialLenDef)/sizeof(bUnitDef)};
 
 
 /* Time */
 static struct bUnitDef buNaturalTimeDef[] = {
 	/* weeks? - probably not needed for blender */
-	{"day", "days",					"d", NULL,	90000.0, 0.0},
-	{"hour", "hours",				"hr", "h",	3600.0, 0.0},
-	{"minute", "minutes",			"min", "m",	60.0, 0.0},
-	{"second", "seconds",			"sec", "s",	1.0, 0.0}, /* base unit */
-	{"millisecond", "milliseconds",	"ms", NULL,	0.001, 0.0},
-	{"microsecond", "microseconds",	"us", NULL,	0.000001, 0.0},
+	{"day", "Days",					"d", NULL,	90000.0, 0.0,	B_UNIT_DEF_NONE},
+	{"hour", "Hours",				"hr", "h",	3600.0, 0.0,	B_UNIT_DEF_NONE},
+	{"minute", "Minutes",			"min", "m",	60.0, 0.0,		B_UNIT_DEF_NONE},
+	{"second", "Seconds",			"sec", "s",	1.0, 0.0,		B_UNIT_DEF_NONE}, /* base unit */
+	{"millisecond", "Milliseconds",	"ms", NULL,	0.001, 0.0	,	B_UNIT_DEF_NONE},
+	{"microsecond", "Microseconds",	"us", NULL,	0.000001, 0.0,	B_UNIT_DEF_NONE},
 	{NULL, NULL, NULL, NULL, 0.0, 0.0}
 };
-static struct bUnitCollection buNaturalTimeCollecton = {buNaturalTimeDef, 3, 0};
+static struct bUnitCollection buNaturalTimeCollecton = {buNaturalTimeDef, 3, 0, sizeof(buNaturalTimeDef)/sizeof(bUnitDef)};
 
 #define UNIT_SYSTEM_MAX 3
 static struct bUnitCollection *bUnitSystems[][8] = {
@@ -109,14 +118,19 @@
 	return &usys->units[usys->base_unit];
 }
 
-static bUnitDef *unit_best_fit(double value, bUnitCollection *usys, bUnitDef *unit_start)
+static bUnitDef *unit_best_fit(double value, bUnitCollection *usys, bUnitDef *unit_start, int suppress)
 {
 	bUnitDef *unit;
 	double value_abs= value>0.0?value:-value;
 
-	for(unit= unit_start ? unit_start:usys->units; unit->name; unit++)
-		if (value_abs >= unit->mul*0.9999) /* scale down mul so 1cm doesnt convert to 10mm because of float error */
+	for(unit= unit_start ? unit_start:usys->units; unit->name; unit++) {
+
+		if(suppress && (unit->flag & B_UNIT_DEF_SUPPRESS))
+			continue;
+
+		if (value_abs >= unit->scalar*0.9999) /* scale down scalar so 1cm doesnt convert to 10mm because of float error */
 			return unit;
+	}
 
 	return unit_default(usys);
 }
@@ -127,13 +141,13 @@
 static void unit_dual_convert(double value, bUnitCollection *usys,
 		bUnitDef **unit_a, bUnitDef **unit_b, double *value_a, double *value_b)
 {
-	bUnitDef *unit= unit_best_fit(value, usys, NULL);
+	bUnitDef *unit= unit_best_fit(value, usys, NULL, 1);
 
-	*value_a= floor(value/unit->mul) * unit->mul;
+	*value_a= floor(value/unit->scalar) * unit->scalar;
 	*value_b= value - (*value_a);
 
 	*unit_a=	unit;
-	*unit_b=	unit_best_fit(*value_b, usys, *unit_a);
+	*unit_b=	unit_best_fit(*value_b, usys, *unit_a, 1);
 }
 
 static int unit_as_string(char *str, double value, int prec, bUnitCollection *usys,
@@ -151,10 +165,10 @@
 		unit= unit_default(usys);
 	}
 	else {
-		unit= unit_best_fit(value, usys, NULL);
+		unit= unit_best_fit(value, usys, NULL, 1);
 	}
 
-	value_conv= value/unit->mul;
+	value_conv= value/unit->scalar;
 
 	/* Convert to a string */
 	{
@@ -252,7 +266,7 @@
 			/* next char cannot be alphanum */
 			if (!isalpha(*(str_found+len_name))) {
 				int len= strlen(str);
-				int len_num= sprintf(str_tmp, "*%g", unit->mul/scale_pref);
+				int len_num= sprintf(str_tmp, "*%g", unit->scalar/scale_pref);
 				memmove(str_found+len_num, str_found+len_name, (len+1)-(int)((str_found+len_name)-str)); /* may grow or shrink the string, 1+ to copy the string terminator */
 				memcpy(str_found, str_tmp, len_num); /* without the string terminator */
 				change= 1;
@@ -299,6 +313,10 @@
 	}
 	
 	for(unit= usys->units; unit->name; unit++) {
+
+		if(unit->flag & B_UNIT_DEF_SUPPRESS)
+			continue;
+
 		/* incase there are multiple instances */
 		while(unit_replace(str, str_tmp, scale_pref, unit))
 			change= 1;
@@ -314,6 +332,10 @@
 			if (system_iter != system) {
 				usys_iter= unit_get_system(system_iter, type);
 				for(unit= usys_iter->units; unit->name; unit++) {
+
+					if(unit->flag & B_UNIT_DEF_SUPPRESS)
+						continue;
+
 					/* incase there are multiple instances */
 					while(unit_replace(str, str_tmp, scale_pref, unit))
 						change= 1;
@@ -329,6 +351,10 @@
 			/* see which units the original value had */
 			strcpy(str, str_prev); /* temp overwrite */
 			for(unit= usys->units; unit->name; unit++) {
+
+				if(unit->flag & B_UNIT_DEF_SUPPRESS)
+					continue;
+
 				if (unit_replace(str, str_tmp, scale_pref, unit))
 					break;
 			}
@@ -349,7 +375,7 @@
 }
 
 
-double bUnit_Size(double value, int system, int type)
+double bUnit_ClosestScalar(double value, int system, int type)
 {
 	bUnitCollection *usys = unit_get_system(system, type);
 	bUnitDef *unit;
@@ -357,9 +383,37 @@
 	if(usys==NULL)
 		return -1;
 
-	unit= unit_best_fit(value, usys, NULL);
+	unit= unit_best_fit(value, usys, NULL, 1);
 	if(unit==NULL)
 		return -1;
 
-	return unit->mul;
+	return unit->scalar;
 }
+
+/* external access */
+void bUnit_GetSystem(void **usys_pt, int *len, int system, int type)
+{
+	bUnitCollection *usys = unit_get_system(system, type);
+	*usys_pt= usys;
+
+	if(usys==NULL) {
+		*len= 0;
+		return;
+	}
+
+	*len= usys->length;
+}
+
+char *bUnit_GetName(void *usys_pt, int index)
+{
+	return ((bUnitCollection *)usys_pt)->units[index].name;
+}
+char *bUnit_GetNamePlural(void *usys_pt, int index)
+{

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list