[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [58170] branches/soc-2013-depsgraph_mt/ source/blender: Move curve' s boundbox and texspace calculation out of modifier stack

Sergey Sharybin sergey.vfx at gmail.com
Thu Jul 11 12:45:02 CEST 2013


Revision: 58170
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=58170
Author:   nazgul
Date:     2013-07-11 10:45:01 +0000 (Thu, 11 Jul 2013)
Log Message:
-----------
Move curve's boundbox and texspace calculation out of modifier stack

There were several issues with how bounding box and texture space
are calculated:

- This was done at the same time as applying modifiers, meaning if
  several objects are sharing the same curve datablock, bounding
  box and texture space will be calculated multiple times.

  Further, allocating bounding box wasn't safe for threading.

- Bounding box and texture space were evaluated after pre-tessellation
  modifiers are applied. This means Curve-level data is actually
  depends on object data, and it's really bad because different
  objects could have different modifiers and this leads to
  conflicts (curve's data depends on object evaluation order)
  and doesn't behave in a predictable way.

This commit moves bounding box and texture space evaluation from
modifier stack to own utility functions, just like it's was done
for meshes.

This makes curve objects update thread-safe, but gives some
limitations as well. Namely, with such approach it's not so
clear how to preserve the same behavior of texture space:
before this change texture space and bounding box would match
bevelled curve as accurate as possible.

Old behavior was nice for quick texturing -- in most cases you
don;t need to modify texture space at all. But texture space
was depending on render/preview settings which could easily lead
to situations, when final result would be far different from
preview one.

Now we're using CV points coordinates and their radius to approximate
the bounding box. This doesn't give the same exact texture space,
but it helps a lot keeping texture space in a nice predictable way.

We could make approximation smarter in the future or add operator
like "match texture space to object's bounding box".

Modified Paths:
--------------
    branches/soc-2013-depsgraph_mt/source/blender/blenkernel/BKE_curve.h
    branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/curve.c
    branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/displist.c
    branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/object.c
    branches/soc-2013-depsgraph_mt/source/blender/editors/object/object_edit.c
    branches/soc-2013-depsgraph_mt/source/blender/editors/space_view3d/drawobject.c

Modified: branches/soc-2013-depsgraph_mt/source/blender/blenkernel/BKE_curve.h
===================================================================
--- branches/soc-2013-depsgraph_mt/source/blender/blenkernel/BKE_curve.h	2013-07-11 09:15:19 UTC (rev 58169)
+++ branches/soc-2013-depsgraph_mt/source/blender/blenkernel/BKE_curve.h	2013-07-11 10:45:01 UTC (rev 58170)
@@ -70,9 +70,13 @@
 short BKE_curve_type_get(struct Curve *cu);
 void BKE_curve_type_test(struct Object *ob);
 void BKE_curve_curve_dimension_update(struct Curve *cu);
+
+void BKE_curve_boundbox_calc(struct Curve *cu, float r_loc[3], float r_size[3]);
+struct BoundBox *BKE_curve_boundbox_get(struct Object *ob);
 void BKE_curve_texspace_calc(struct Curve *cu);
+void BKE_curve_texspace_get(struct Curve *cu, float r_loc[3], float r_rot[3], float r_size[3]);
 
-bool BKE_curve_minmax(struct Curve *cu, float min[3], float max[3]);
+bool BKE_curve_minmax(struct Curve *cu, bool use_radius, float min[3], float max[3]);
 bool BKE_curve_center_median(struct Curve *cu, float cent[3]);
 bool BKE_curve_center_bounds(struct Curve *cu, float cent[3]);
 void BKE_curve_translate(struct Curve *cu, float offset[3], int do_keys);
@@ -115,7 +119,7 @@
 struct Nurb *BKE_nurb_duplicate(struct Nurb *nu);
 
 void BKE_nurb_test2D(struct Nurb *nu);
-void BKE_nurb_minmax(struct Nurb *nu, float min[3], float max[3]);
+void BKE_nurb_minmax(struct Nurb *nu, bool use_radius, float min[3], float max[3]);
 
 void BKE_nurb_makeFaces(struct Nurb *nu, float *coord_array, int rowstride, int resolu, int resolv);
 void BKE_nurb_makeCurve(struct Nurb *nu, float *coord_array, float *tilt_array, float *radius_array, float *weight_array, int resolu, int stride);

Modified: branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/curve.c
===================================================================
--- branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/curve.c	2013-07-11 09:15:19 UTC (rev 58169)
+++ branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/curve.c	2013-07-11 10:45:01 UTC (rev 58170)
@@ -364,40 +364,81 @@
 		BKE_curve_curve_dimension_update((Curve *)ob->data);
 }
 
-void BKE_curve_texspace_calc(Curve *cu)
+void BKE_curve_boundbox_calc(Curve *cu, float r_loc[3], float r_size[3])
 {
-	BoundBox *bb = cu->bb;
+	BoundBox *bb;
 	float min[3], max[3];
+	float mloc[3], msize[3];
 
-	/* Curve's undeformed bounding box is calculated in displist.c,
-	 * as a part of display list calculation.
-	 */
-	copy_v3_v3(min, bb->vec[0]);
-	copy_v3_v3(max, bb->vec[6]);
+	if (cu->bb == NULL) cu->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
+	bb = cu->bb;
 
-	if (cu->texflag & CU_AUTOSPACE) {
-		mid_v3_v3v3(cu->loc, min, max);
-		cu->size[0] = (max[0] - min[0]) / 2.0f;
-		cu->size[1] = (max[1] - min[1]) / 2.0f;
-		cu->size[2] = (max[2] - min[2]) / 2.0f;
+	if (!r_loc) r_loc = mloc;
+	if (!r_size) r_size = msize;
 
-		zero_v3(cu->rot);
+	INIT_MINMAX(min, max);
+	/* TODO(sergey): makecode aware of radius and bevel somehow.. */
+	if (!BKE_curve_minmax(cu, true, min, max)) {
+		min[0] = min[1] = min[2] = -1.0f;
+		max[0] = max[1] = max[2] = 1.0f;
+	}
 
-		if (cu->size[0] == 0.0f) cu->size[0] = 1.0f;
-		else if (cu->size[0] > 0.0f && cu->size[0] < 0.00001f) cu->size[0] = 0.00001f;
-		else if (cu->size[0] < 0.0f && cu->size[0] > -0.00001f) cu->size[0] = -0.00001f;
+	mid_v3_v3v3(r_loc, min, max);
 
-		if (cu->size[1] == 0.0f) cu->size[1] = 1.0f;
-		else if (cu->size[1] > 0.0f && cu->size[1] < 0.00001f) cu->size[1] = 0.00001f;
-		else if (cu->size[1] < 0.0f && cu->size[1] > -0.00001f) cu->size[1] = -0.00001f;
+	r_size[0] = (max[0] - min[0]) / 2.0f;
+	r_size[1] = (max[1] - min[1]) / 2.0f;
+	r_size[2] = (max[2] - min[2]) / 2.0f;
 
-		if (cu->size[2] == 0.0f) cu->size[2] = 1.0f;
-		else if (cu->size[2] > 0.0f && cu->size[2] < 0.00001f) cu->size[2] = 0.00001f;
-		else if (cu->size[2] < 0.0f && cu->size[2] > -0.00001f) cu->size[2] = -0.00001f;
+	BKE_boundbox_init_from_minmax(bb, min, max);
 
+	bb->flag &= ~BOUNDBOX_INVALID;
+}
+
+BoundBox *BKE_curve_boundbox_get(Object *ob)
+{
+	Curve *cu = ob->data;
+
+	if (ob->bb)
+		return ob->bb;
+
+	if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_INVALID)) {
+		BKE_curve_texspace_calc(cu);
 	}
+
+	return cu->bb;
 }
 
+void BKE_curve_texspace_calc(Curve *cu)
+{
+	float loc[3], size[3];
+	int a;
+
+	BKE_curve_boundbox_calc(cu, loc, size);
+
+	if (cu->texflag & CU_AUTOSPACE) {
+		for (a = 0; a < 3; a++) {
+			if (size[a] == 0.0f) size[a] = 1.0f;
+			else if (size[a] > 0.0f && size[a] < 0.00001f) size[a] = 0.00001f;
+			else if (size[a] < 0.0f && size[a] > -0.00001f) size[a] = -0.00001f;
+		}
+
+		copy_v3_v3(cu->loc, loc);
+		copy_v3_v3(cu->size, size);
+		zero_v3(cu->rot);
+	}
+}
+
+void BKE_curve_texspace_get(Curve *cu, float r_loc[3], float r_rot[3], float r_size[3])
+{
+	if (cu->bb == NULL || (cu->bb->flag & BOUNDBOX_INVALID)) {
+		BKE_curve_texspace_calc(cu);
+	}
+
+	if (r_loc) copy_v3_v3(r_loc,  cu->loc);
+	if (r_rot) copy_v3_v3(r_rot,  cu->rot);
+	if (r_size) copy_v3_v3(r_size, cu->size);
+}
+
 int BKE_nurbList_index_get_co(ListBase *nurb, const int index, float r_co[3])
 {
 	Nurb *nu;
@@ -584,18 +625,34 @@
 	}
 }
 
-void BKE_nurb_minmax(Nurb *nu, float min[3], float max[3])
+/* if use_radius is truth, minmax will take points' radius into account,
+ * which will make boundbox closer to bevelled curve.
+ */
+void BKE_nurb_minmax(Nurb *nu, bool use_radius, float min[3], float max[3])
 {
 	BezTriple *bezt;
 	BPoint *bp;
 	int a;
+	float point[3];
 
 	if (nu->type == CU_BEZIER) {
 		a = nu->pntsu;
 		bezt = nu->bezt;
 		while (a--) {
+			if (use_radius) {
+				float radius_vector[3];
+				radius_vector[0] = radius_vector[1] = radius_vector[2] = bezt->radius;
+
+				add_v3_v3v3(point, bezt->vec[1], radius_vector);
+				minmax_v3v3_v3(min, max, point);
+
+				sub_v3_v3v3(point, bezt->vec[1], radius_vector);
+				minmax_v3v3_v3(min, max, point);
+			}
+			else {
+				minmax_v3v3_v3(min, max, bezt->vec[1]);
+			}
 			minmax_v3v3_v3(min, max, bezt->vec[0]);
-			minmax_v3v3_v3(min, max, bezt->vec[1]);
 			minmax_v3v3_v3(min, max, bezt->vec[2]);
 			bezt++;
 		}
@@ -604,7 +661,20 @@
 		a = nu->pntsu * nu->pntsv;
 		bp = nu->bp;
 		while (a--) {
-			minmax_v3v3_v3(min, max, bp->vec);
+			if (nu->pntsv == 1 && use_radius) {
+				float radius_vector[3];
+				radius_vector[0] = radius_vector[1] = radius_vector[2] = bp->radius;
+
+				add_v3_v3v3(point, bp->vec, radius_vector);
+				minmax_v3v3_v3(min, max, point);
+
+				sub_v3_v3v3(point, bp->vec, radius_vector);
+				minmax_v3v3_v3(min, max, point);
+			}
+			else {
+				/* Surfaces doesn't use bevel, so no need to take radius into account. */
+				minmax_v3v3_v3(min, max, bp->vec);
+			}
 			bp++;
 		}
 	}
@@ -3513,13 +3583,13 @@
 
 
 /* basic vertex data functions */
-bool BKE_curve_minmax(Curve *cu, float min[3], float max[3])
+bool BKE_curve_minmax(Curve *cu, bool use_radius, float min[3], float max[3])
 {
 	ListBase *nurb_lb = BKE_curve_nurbs_get(cu);
 	Nurb *nu;
 
 	for (nu = nurb_lb->first; nu; nu = nu->next)
-		BKE_nurb_minmax(nu, min, max);
+		BKE_nurb_minmax(nu, use_radius, min, max);
 
 	return (nurb_lb->first != NULL);
 }
@@ -3566,7 +3636,7 @@
 {
 	float min[3], max[3];
 	INIT_MINMAX(min, max);
-	if (BKE_curve_minmax(cu, min, max)) {
+	if (BKE_curve_minmax(cu, false, min, max)) {
 		mid_v3_v3v3(cent, min, max);
 		return true;
 	}

Modified: branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/displist.c
===================================================================
--- branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/displist.c	2013-07-11 09:15:19 UTC (rev 58169)
+++ branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/displist.c	2013-07-11 10:45:01 UTC (rev 58170)
@@ -1269,17 +1269,6 @@
 		}
 	}
 
-	/* Calculate curve's boundig box from non-modified display list. */
-	/* TODO(sergey): not thread-safe. */
-	if (cu->bb == NULL) {
-		cu->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
-	}
-	boundbox_dispbase(cu->bb, dispbase);
-
-	if (!forRender) {
-		BKE_curve_texspace_calc(cu);
-	}
-
 	if (!forOrco) {
 		curve_calc_modifiers_post(scene, ob, &nubase, dispbase, derivedFinal,
 		                          forRender, renderResolution);
@@ -1579,18 +1568,6 @@
 		if ((cu->flag & CU_PATH) && !forOrco)
 			calc_curvepath(ob, &nubase);
 
-		/* Calculate curve's boundig box from non-modified display list. */
-		/* TODO(sergey): not thread-safe. */
-		if (cu->bb == NULL) {
-			cu->bb = MEM_callocN(sizeof(BoundBox), "boundbox");
-		}
-
-		boundbox_dispbase(cu->bb, dispbase);
-
-		if (!forRender) {
-			BKE_curve_texspace_calc(cu);
-		}
-
 		if (!forOrco)
 			curve_calc_modifiers_post(scene, ob, &nubase, dispbase, derivedFinal, forRender, renderResolution);
 
@@ -1612,10 +1589,9 @@
 	if (!ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT))
 		return;
 
-	if (ob->curve_cache) {
-		BKE_displist_free(&(ob->curve_cache->disp));
-	}
-	else {
+	BKE_object_free_derived_caches(ob);
+
+	if (!ob->curve_cache) {
 		ob->curve_cache = MEM_callocN(sizeof(CurveCache), "CurveCache for curve types");
 	}
 

Modified: branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/object.c
===================================================================
--- branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/object.c	2013-07-11 09:15:19 UTC (rev 58169)
+++ branches/soc-2013-depsgraph_mt/source/blender/blenkernel/intern/object.c	2013-07-11 10:45:01 UTC (rev 58170)
@@ -265,7 +265,14 @@
 			me->bb->flag |= BOUNDBOX_INVALID;
 		}
 	}
+	else if (ELEM3(ob->type, OB_SURF, OB_CURVE, OB_FONT)) {
+		Curve *cu = ob->data;
 
+		if (cu->bb) {
+			cu->bb->flag |= BOUNDBOX_INVALID;
+		}
+	}
+
 	if (ob->bb) {
 		MEM_freeN(ob->bb);
 		ob->bb = NULL;
@@ -2292,7 +2299,7 @@
 		bb = BKE_mesh_boundbox_get(ob);
 	}
 	else if (ELEM3(ob->type, OB_CURVE, OB_SURF, OB_FONT)) {
-		bb = ob->bb ? ob->bb : ((Curve *)ob->data)->bb;
+		bb = BKE_curve_boundbox_get(ob);
 	}
 	else if (ob->type == OB_MBALL) {
 		bb = ob->bb;
@@ -2361,9 +2368,7 @@
 		case OB_FONT:
 		case OB_SURF:
 		{
-			/* Use the object bounding box so that modifier output
-			 * gets taken into account */
-			bb = *(ob->bb);
+			bb = *BKE_curve_boundbox_get(ob);
 
 			for (a = 0; a < 8; a++) {
 				mul_m4_v3(ob->obmat, bb.vec[a]);

Modified: branches/soc-2013-depsgraph_mt/source/blender/editors/object/object_edit.c
===================================================================

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list