[Bf-blender-cvs] [e0a4a4a] master: Bevel: change 'adj' pattern to use subdivision.
Howard Trickey
noreply at git.blender.org
Mon Jan 6 16:54:16 CET 2014
Commit: e0a4a4afc31e101bac6238ac4e0785935610a862
Author: Howard Trickey
Date: Mon Jan 6 10:52:31 2014 -0500
https://developer.blender.org/rBe0a4a4afc31e101bac6238ac4e0785935610a862
Bevel: change 'adj' pattern to use subdivision.
This gets rid of a hacky way of setting the mesh
coordinates at corners, which created overlaps
in some cases.
Also, special case a cube-like corner with all
edges beveled, to snap to a sphere, giving more
expected curvature in that case.
===================================================================
M source/blender/bmesh/tools/bmesh_bevel.c
===================================================================
diff --git a/source/blender/bmesh/tools/bmesh_bevel.c b/source/blender/bmesh/tools/bmesh_bevel.c
index b10f162..35c3d10 100644
--- a/source/blender/bmesh/tools/bmesh_bevel.c
+++ b/source/blender/bmesh/tools/bmesh_bevel.c
@@ -47,8 +47,12 @@
#include "./intern/bmesh_private.h"
+/* use new way of doing ADJ pattern */
+#define USE_ADJ_SUBDIV
+
#define BEVEL_EPSILON_D 1e-6
#define BEVEL_EPSILON 1e-6f
+#define BEVEL_EPSILON_SQ 1e-12f
/* happens far too often, uncomment for development */
// #define BEVEL_ASSERT_PROJECT
@@ -84,6 +88,15 @@ typedef struct EdgeHalf {
// int _pad;
} EdgeHalf;
+/* Profile specification.
+ * For now, only have round profiles so only need midpoint.
+ * The start and end points of the profile are stored separately.
+ * TODO: generalize to superellipse profiles.
+ */
+typedef struct Profile {
+ float midco[3]; /* mid control point for profile */
+} Profile;
+
/* An element in a cyclic boundary of a Vertex Mesh (VMesh) */
typedef struct BoundVert {
struct BoundVert *next, *prev; /* in CCW order */
@@ -92,6 +105,7 @@ typedef struct BoundVert {
EdgeHalf *elast;
EdgeHalf *ebev; /* beveled edge whose left side is attached here, if any */
int index; /* used for vmesh indexing */
+ Profile profile; /* edge profile between this and next BoundVert */
bool any_seam; /* are any of the edges attached here seams? */
// int _pad;
} BoundVert;
@@ -753,7 +767,7 @@ static void offset_in_two_planes(BevelParams *bp, EdgeHalf *e1, EdgeHalf *e2, Ed
}
else if (iret == 2) {
/* lines are not coplanar and don't meet; meetco and isect2 are nearest to first and second lines */
- if (len_v3v3(meetco, isect2) > 100.0f * BEVEL_EPSILON) {
+ if (len_squared_v3v3(meetco, isect2) > 100.0f * BEVEL_EPSILON_SQ) {
/* offset lines don't meet so can't preserve widths */
offset_on_edge_between(bp, e1, e2, emid, v, meetco);
}
@@ -806,6 +820,37 @@ static void project_to_edge(BMEdge *e, const float co_a[3], const float co_b[3],
}
}
+/* If there is a bndv->ebev edge, find the mid control point if necessary.
+ * It is the closest point on the beveled edge to the line segment between
+ * bndv and bndv->next. */
+static void set_profile_params(BoundVert *bndv)
+{
+ EdgeHalf *e;
+
+ e = bndv->ebev;
+ if (e) {
+ project_to_edge(e->e, bndv->nv.co, bndv->next->nv.co,
+ bndv->profile.midco);
+ }
+}
+
+/* project co along the direction of edir to the plane containing
+ * e1 and e2 (which share a vert) */
+static void project_to_edges_plane(float co[3], EdgeHalf *edir, EdgeHalf *e1, EdgeHalf *e2)
+{
+ float dir[3], co2[3], d1[3], d2[3], no[3], snap[3];
+
+ sub_v3_v3v3(dir, edir->e->v1->co, edir->e->v2->co);
+ sub_v3_v3v3(d1, e1->e->v1->co, e1->e->v2->co);
+ sub_v3_v3v3(d2, e2->e->v1->co, e2->e->v2->co);
+ cross_v3_v3v3(no, d1, d2);
+ if (len_squared_v3(no) < BEVEL_EPSILON_SQ)
+ return; /* e1 and e2 parallel so plane undefined -- don't snap */
+ add_v3_v3v3(co2, co, dir);
+ if (isect_line_plane_v3(snap, co, co2, e1->e->v1->co, no))
+ copy_v3_v3(co, snap);
+}
+
/* return 1 if a and b are in CCW order on the normal side of f,
* and -1 if they are reversed, and 0 if there is no shared face f */
static int bev_ccw_test(BMEdge *a, BMEdge *b, BMFace *f)
@@ -823,7 +868,7 @@ static int bev_ccw_test(BMEdge *a, BMEdge *b, BMFace *f)
/* Fill matrix r_mat so that a point in the sheared parallelogram with corners
* va, vmid, vb (and the 4th that is implied by it being a parallelogram)
- * is transformed to the unit square by multiplication with r_mat.
+ * is the result of transforming the unit square by multiplication with r_mat.
* If it can't be done because the parallelogram is degenerate, return FALSE
* else return TRUE.
* Method:
@@ -875,10 +920,55 @@ static int make_unit_square_map(const float va[3], const float vmid[3], const fl
return FALSE;
}
+/* Like make_unit_square_map, but this one makes a matrix that transforms the
+ * (1,1,1) corner of a unit cube into an arbitrary corner with corner vert d
+ * and verts around it a, b, c (in ccw order, viewed from d normal dir).
+ * The matrix mat is calculated to map:
+ * (1,0,0) -> va
+ * (0,1,0) -> vb
+ * (0,0,1) -> vc
+ * (1,1,1) -> vd
+ * We want M to make M*A=B where A has the left side above, as columns
+ * and B has the right side as columns - both extended into homogeneous coords.
+ * So M = B*(Ainverse). Doing Ainverse by hand gives the code below.
+ * The cols of M are 1/2{va-vb+vc-vd}, 1/2{-va+vb-vc+vd}, 1/2{-va-vb+vc+vd},
+ * and 1/2{va+vb+vc-vd}
+ * and Blender matrices have cols at m[i][*].
+ */
+static void make_unit_cube_map(const float va[3], const float vb[3], const float vc[3],
+ const float vd[3], float r_mat[4][4])
+{
+ copy_v3_v3(r_mat[0], va);
+ sub_v3_v3(r_mat[0], vb);
+ sub_v3_v3(r_mat[0], vc);
+ add_v3_v3(r_mat[0], vd);
+ mul_v3_fl(r_mat[0], 0.5f);
+ r_mat[0][3] = 0.0f;
+ copy_v3_v3(r_mat[1], vb);
+ sub_v3_v3(r_mat[1], va);
+ sub_v3_v3(r_mat[1], vc);
+ add_v3_v3(r_mat[1], vd);
+ mul_v3_fl(r_mat[1], 0.5f);
+ r_mat[1][3] = 0.0f;
+ copy_v3_v3(r_mat[2], vc);
+ sub_v3_v3(r_mat[2], va);
+ sub_v3_v3(r_mat[2], vb);
+ add_v3_v3(r_mat[2], vd);
+ mul_v3_fl(r_mat[2], 0.5f);
+ r_mat[2][3] = 0.0f;
+ copy_v3_v3(r_mat[3], va);
+ add_v3_v3(r_mat[3], vb);
+ add_v3_v3(r_mat[3], vc);
+ sub_v3_v3(r_mat[3], vd);
+ mul_v3_fl(r_mat[3], 0.5f);
+ r_mat[3][3] = 1.0f;
+}
+
+#ifndef USE_ADJ_SUBDIV
/*
* Find the point (/n) of the way around the round profile for e,
* where start point is va, midarc point is vmid, and end point is vb.
- * Return the answer in profileco.
+ * Return the answer in r_co.
* If va -- vmid -- vb is approximately a straight line, just
* interpolate along the line.
*/
@@ -903,7 +993,37 @@ static void get_point_on_round_edge(EdgeHalf *e, int k,
interp_v3_v3v3(r_co, va, vb, (float)k / (float)n);
}
}
+#endif
+
+/* Find the point on given profile at parameter u which goes from 0 to 2 as
+ * the profile is moved from va to vb. */
+static void get_profile_point(const Profile *pro, const float va[3], const float vb[3], float u, float r_co[3])
+ {
+ float p[3], angle;
+ float m[4][4];
+ if (u <= 0.0f)
+ copy_v3_v3(r_co, va);
+ else if (u >= 2.0f)
+ copy_v3_v3(r_co, vb);
+ else if (!make_unit_square_map(va, pro->midco, vb, m)) {
+ interp_v3_v3v3(r_co, va, vb, u / 2.0f);
+ }
+ else {
+ angle = u * (float)M_PI / 4.0f; /* angle from y axis */
+ p[0] = sinf(angle);
+ p[1] = cosf(angle);
+ p[2] = 0.0f;
+ mul_v3_m4v3(r_co, m, p);
+ }
+}
+
+BLI_INLINE void get_bndv_profile_point(const BoundVert *bndv, const float u, float r_co[3])
+{
+ get_profile_point(&bndv->profile, bndv->nv.co, bndv->next->nv.co, u, r_co);
+}
+
+#ifndef USE_ADJ_SUBDIV
/* Calculate a snapped point to the transformed profile of edge e, extended as
* in a cylinder-like surface in the direction of e.
* co is the point to snap and is modified in place.
@@ -940,6 +1060,47 @@ static void snap_to_edge_profile(EdgeHalf *e, const float va[3], const float vb[
copy_v3_v3(co, p);
}
}
+#endif
+
+/* Snapping a direction co to a unit radius sphere is just normalizing co.
+ * TODO: generalize to superellipsoid */
+static void snap_to_sphere(float co[3])
+{
+ normalize_v3(co);
+}
+
+static void snap_to_profile(BoundVert *bndv, EdgeHalf *e, float co[3])
+{
+ float va[3], vb[3], edir[3], va0[3], vb0[3], vmid0[3];
+ float plane[4], m[4][4], minv[4][4], p[3], snap[3];
+
+ copy_v3_v3(va, bndv->nv.co);
+ copy_v3_v3(vb, bndv->next->nv.co);
+
+ sub_v3_v3v3(edir, e->e->v1->co, e->e->v2->co);
+
+ plane_from_point_normal_v3(plane, co, edir);
+ closest_to_plane_v3(va0, plane, va);
+ closest_to_plane_v3(vb0, plane, vb);
+ closest_to_plane_v3(vmid0, plane, bndv->profile.midco);
+ if (make_unit_square_map(va0, vmid0, vb0, m)) {
+ /* Transform co and project it onto sphere */
+ if (!invert_m4_m4(minv, m)) {
+ /* shouldn't happen */
+ BLI_assert(!"failed inverse during profile snap");
+ return;
+ }
+ mul_v3_m4v3(p, minv, co);
+ snap_to_sphere(p);
+ mul_v3_m4v3(snap, m, p);
+ copy_v3_v3(co, snap);
+ }
+ else {
+ /* planar case: just snap to line va--vb */
+ closest_to_line_segment_v3(p, co, va, vb);
+ copy_v3_v3(co, p);
+ }
+}
/* Set the any_seam property for a BevVert and all its BoundVerts */
static void set_bound_vert_seams(BevVert *bv)
@@ -1043,6 +1204,7 @@ static void build_boundary(BevelParams *bp, BevVert *bv, bool construct)
else {
adjust_bound_vert(e->next->leftv, co);
}
+ set_profile_params(vm->boundstart);
return;
}
@@ -1144,6 +1306,18 @@ static void build_boundary(BevelParams *bp, BevVert *bv, bool construct)
}
} while ((e = e->next) != efirst);
+ v = vm->boundstart;
+ do {
+ set_profile_params(v);
+ } while ((v = v->next) != vm->boundstart);
+
+ if (bv->selcount == 1 && bv->edgecount == 3) {
+ /* special case: snap profile to third face */
+ v = vm->boundstart;
+ BLI_assert(v->ebev != NULL);
+ project_to_edges_plane(v->profile.midco, v->ebev, v->efirst, v->next->elast);
+ }
+
if (construct) {
set_bound_vert_seams(bv);
@@ -1171,7 +1345,11 @@ static void build_boundary(BevelParams *bp, BevVert *bv, bool construct)
}
}
else {
+#ifdef USE_ADJ_SUBDIV
+ vm->mesh_kind = M_ADJ_SUBDIV;
+#else
vm->mesh_kind = M_ADJ;
+#endif
}
}
}
@@ -1252,29 +1430,13 @@ static void adjust_offsets(BevelParams *bp)
BLI_gsqueue_free(q);
}
-/*
- * Given that the boundary is built and the boundary BMVerts have been made,
- * calculate the positions of the interior mesh points for the M_ADJ pattern,
- * then make the BMVerts and the new faces. */
-static void bevel_build_rings(BMesh *bm, BevVert *bv)
+/* Do the edges at bv form a "pipe"?
+ * Current definition: at least three beveled edges,
+ * two in line, and sharing a face. */
+static EdgeHalf *pipe_test(BevVert *bv)
{
- int k, ring, i, n, ns, ns2, nn, odd;
- VMesh *vm = bv->vmesh;
- BoundVert *v, *vprev, *vnext;
- NewVert *nv, *nvprev, *nvnext;
EdgeHalf *e1, *e2, *epipe;
- BMVert *bmv, *bmv1, *bmv2, *bmv3, *bmv4;
- BMFace *f, *f2, *f23;
- float co[3], coa[3], cob[3], midco[3];
- float va_pipe[3], vb_pipe[3];
- n = vm->count;
- ns = vm->seg;
- ns2 = ns / 2;
- odd = (ns % 2)
@@ Diff output truncated at 10240 characters. @@
More information about the Bf-blender-cvs
mailing list