[Bf-blender-cvs] [bb182d1] hair_system: Change of the hair DNA data to use an array of arrays, instead of flattening all the points into a single array.

Lukas Tönne noreply at git.blender.org
Fri Jul 25 19:35:00 CEST 2014


Commit: bb182d11647762e6db7a59e9630d7aad17336d80
Author: Lukas Tönne
Date:   Fri Jul 25 19:31:08 2014 +0200
Branches: hair_system
https://developer.blender.org/rBbb182d11647762e6db7a59e9630d7aad17336d80

Change of the hair DNA data to use an array of arrays, instead of
flattening all the points into a single array.

The latter would be better for some algorithms and memory conherence,
but for frequent access we would copy to acceleration structures anyway.
Using double arrays is much nicer for access and modification by tools.

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

M	source/blender/blenkernel/BKE_hair.h
M	source/blender/blenkernel/intern/hair.c
M	source/blender/makesdna/DNA_hair_types.h

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

diff --git a/source/blender/blenkernel/BKE_hair.h b/source/blender/blenkernel/BKE_hair.h
index 2a3c5e8..ae0b8b7 100644
--- a/source/blender/blenkernel/BKE_hair.h
+++ b/source/blender/blenkernel/BKE_hair.h
@@ -32,11 +32,22 @@
  */
 
 struct HairSystem;
+struct HairCurve;
+struct HairPoint;
 
 struct HairSystem *BKE_hairsys_new(void);
 void BKE_hairsys_free(struct HairSystem *hsys);
 struct HairSystem *BKE_hairsys_copy(struct HairSystem *hsys);
 
-void BKE_hair_calc_curve_offsets(HairSystem *hsys);
+struct HairCurve *BKE_hair_curve_add(struct HairSystem *hsys);
+struct HairCurve *BKE_hair_curve_add_multi(struct HairSystem *hsys, int num);
+void BKE_hair_curve_remove(struct HairSystem *hsys, struct HairCurve *hair);
+
+struct HairPoint *BKE_hair_point_append(struct HairSystem *hsys, struct HairCurve *hair);
+struct HairPoint *BKE_hair_point_append_multi(struct HairSystem *hsys, struct HairCurve *hair, int num);
+struct HairPoint *BKE_hair_point_insert(struct HairSystem *hsys, struct HairCurve *hair, int pos);
+struct HairPoint *BKE_hair_point_insert_multi(struct HairSystem *hsys, struct HairCurve *hair, int pos, int num);
+void BKE_hair_point_remove(struct HairSystem *hsys, struct HairCurve *hair, struct HairPoint *point);
+void BKE_hair_point_remove_position(struct HairSystem *hsys, struct HairCurve *hair, int pos);
 
 #endif
diff --git a/source/blender/blenkernel/intern/hair.c b/source/blender/blenkernel/intern/hair.c
index 76cab0a..e9bcdf7 100644
--- a/source/blender/blenkernel/intern/hair.c
+++ b/source/blender/blenkernel/intern/hair.c
@@ -28,44 +28,156 @@
  *  \ingroup bke
  */
 
+#include <stdlib.h>
+#include <string.h>
+
 #include "MEM_guardedalloc.h"
 
+#include "BLI_utildefines.h"
+
 #include "DNA_hair_types.h"
 
 #include "BKE_hair.h"
 
-struct HairSystem *BKE_hairsys_new(void)
+HairSystem *BKE_hairsys_new(void)
 {
 	HairSystem *hsys = MEM_callocN(sizeof(HairSystem), "hair system");
 	return hsys;
 }
 
-void BKE_hairsys_free(struct HairSystem *hsys)
+void BKE_hairsys_free(HairSystem *hsys)
 {
-	if (hsys->curves)
+	int totcurves = hsys->totcurves, i;
+	if (hsys->curves) {
+		for (i = 0; i < totcurves; ++i)
+			MEM_freeN(hsys->curves[i].points);
 		MEM_freeN(hsys->curves);
-	if (hsys->points)
-		MEM_freeN(hsys->points);
+	}
 	MEM_freeN(hsys);
 }
 
-struct HairSystem *BKE_hairsys_copy(struct HairSystem *hsys)
+HairSystem *BKE_hairsys_copy(HairSystem *hsys)
 {
+	int totcurves = hsys->totcurves, i;
 	HairSystem *thsys = MEM_dupallocN(hsys);
-	thsys->points = MEM_dupallocN(hsys->points);
 	thsys->curves = MEM_dupallocN(hsys->curves);
+	for (i = 0; i < totcurves; ++i)
+		thsys->curves[i].points = MEM_dupallocN(hsys->curves[i].points);
 	
 	return thsys;
 }
 
-void BKE_hair_calc_curve_offsets(HairSystem *hsys)
+
+HairCurve *BKE_hair_curve_add(HairSystem *hsys)
+{
+	return BKE_hair_curve_add_multi(hsys, 1);
+}
+
+HairCurve *BKE_hair_curve_add_multi(HairSystem *hsys, int num)
 {
-	HairCurve *hair;
-	int a;
-	int offset = 0;
+	if (num <= 0)
+		return NULL;
+	
+	hsys->totcurves += num;
+	hsys->curves = MEM_recallocN_id(hsys->curves, sizeof(HairCurve) * hsys->totcurves, "hair system curve data");
+	
+	return &hsys->curves[hsys->totcurves-num];
+}
+
+void BKE_hair_curve_remove(HairSystem *hsys, HairCurve *hair)
+{
+	HairCurve *ncurves;
+	int pos, ntotcurves;
+	
+	pos = (int)(hair - hsys->curves);
+	BLI_assert(pos >= 0 && pos < hsys->totcurves);
 	
-	for (a = 0, hair = hsys->curves; a < hsys->totcurves; ++a, ++hair) {
-		hair->offset = offset;
-		offset += hair->numpoints;
+	ntotcurves = hsys->totcurves - 1;
+	ncurves = ntotcurves > 0 ? MEM_mallocN(sizeof(HairCurve) * ntotcurves, "hair system curve data") : NULL;
+	
+	if (pos >= 1) {
+		memcpy(ncurves, hsys->curves, sizeof(HairCurve) * pos);
+	}
+	if (pos < hsys->totcurves - 1) {
+		memcpy(ncurves + pos, hsys->curves + (pos + 1), hsys->totcurves - (pos + 1));
 	}
+	
+	MEM_freeN(hair->points);
+	MEM_freeN(hsys->curves);
+	hsys->curves = ncurves;
+	hsys->totcurves = ntotcurves;
+}
+
+HairPoint *BKE_hair_point_append(HairSystem *hsys, HairCurve *hair)
+{
+	return BKE_hair_point_append_multi(hsys, hair, 1);
+}
+
+HairPoint *BKE_hair_point_append_multi(HairSystem *UNUSED(hsys), HairCurve *hair, int num)
+{
+	if (num <= 0)
+		return NULL;
+	
+	hair->totpoints += num;
+	hair->points = MEM_recallocN_id(hair->points, sizeof(HairPoint) * hair->totpoints, "hair point data");
+	
+	return &hair->points[hair->totpoints-num];
+}
+
+HairPoint *BKE_hair_point_insert(HairSystem *hsys, HairCurve *hair, int pos)
+{
+	return BKE_hair_point_insert_multi(hsys, hair, pos, 1);
+}
+
+HairPoint *BKE_hair_point_insert_multi(HairSystem *UNUSED(hsys), HairCurve *hair, int pos, int num)
+{
+	HairPoint *npoints;
+	int ntotpoints;
+	
+	if (num <= 0)
+		return NULL;
+	
+	ntotpoints = hair->totpoints + num;
+	npoints = ntotpoints > 0 ? MEM_callocN(sizeof(HairPoint) * ntotpoints, "hair point data") : NULL;
+	
+	CLAMP(pos, 0, ntotpoints-1);
+	if (pos >= 1) {
+		memcpy(npoints, hair->points, sizeof(HairPoint) * pos);
+	}
+	if (pos < hair->totpoints - num) {
+		memcpy(npoints + (pos + num), hair->points + pos, hair->totpoints - pos);
+	}
+	
+	MEM_freeN(hair->points);
+	hair->points = npoints;
+	hair->totpoints = ntotpoints;
+	
+	return &hair->points[pos];
+}
+
+void BKE_hair_point_remove(HairSystem *hsys, HairCurve *hair, HairPoint *point)
+{
+	BKE_hair_point_remove_position(hsys, hair, (int)(point - hair->points));
+}
+
+void BKE_hair_point_remove_position(HairSystem *UNUSED(hsys), HairCurve *hair, int pos)
+{
+	HairPoint *npoints;
+	int ntotpoints;
+	
+	BLI_assert(pos >= 0 && pos < hair->totpoints);
+	
+	ntotpoints = hair->totpoints - 1;
+	npoints = ntotpoints > 0 ? MEM_mallocN(sizeof(HairPoint) * ntotpoints, "hair point data") : NULL;
+	
+	if (pos >= 1) {
+		memcpy(npoints, hair->points, sizeof(HairPoint) * pos);
+	}
+	if (pos < hair->totpoints - 1) {
+		memcpy(npoints + pos, hair->points + (pos + 1), hair->totpoints - (pos + 1));
+	}
+	
+	MEM_freeN(hair->points);
+	hair->points = npoints;
+	hair->totpoints = ntotpoints;
 }
diff --git a/source/blender/makesdna/DNA_hair_types.h b/source/blender/makesdna/DNA_hair_types.h
index a7fbf48..b80e533 100644
--- a/source/blender/makesdna/DNA_hair_types.h
+++ b/source/blender/makesdna/DNA_hair_types.h
@@ -35,19 +35,19 @@
 
 typedef struct HairPoint {
 	float co[3];                /* location in object space */
+	int pad;
 } HairPoint;
 
 typedef struct HairCurve {
-	int numpoints;              /* number of points in the curve */
-	
-	int offset;                 /* utility variable to keep track of points offset */
+	HairPoint *points;          /* point data */
+	int totpoints;              /* number of points in the curve */
+	int pad;
 } HairCurve;
 
 typedef struct HairSystem {
-	HairPoint *points;          /* point data */
 	HairCurve *curves;          /* curve data */
-	
-	int totcurves, totpoints;   /* number of curves and points */
+	int totcurves;              /* number of curves */
+	int pad;
 } HairSystem;
 
 #endif




More information about the Bf-blender-cvs mailing list