1147
static int snapEdge(ARegion *ar, float v1co[3], short v1no[3], float v2co[3], short v2no[3], float obmat[4][4], float timat[3][3],
1148
const float ray_start[3], const float ray_start_local[3], const float ray_normal_local[3], const float mval[2],
1149
float r_loc[3], float r_no[3], int *r_dist, float *r_depth)
1150
static bool snapEdge(ARegion *ar, float v1co[3], short v1no[3], float v2co[3], short v2no[3], float obmat[4][4], float timat[3][3],
1151
const float ray_start[3], const float ray_start_local[3], const float ray_normal_local[3], const float mval_fl[2],
1152
float r_loc[3], float r_no[3], float *r_dist_px, float *r_depth)
1151
1154
float intersect[3] = {0, 0, 0}, ray_end[3], dvec[3];
1156
bool retval = false;
1155
1158
copy_v3_v3(ray_end, ray_normal_local);
1156
1159
mul_v3_fl(ray_end, 2000);
1304
1307
if ((eBone->flag & (BONE_HIDDEN_A | BONE_ROOTSEL | BONE_TIPSEL)) == 0) {
1305
1308
switch (snap_mode) {
1306
1309
case SCE_SNAP_MODE_VERTEX:
1307
retval |= snapVertex(ar, eBone->head, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist, r_depth);
1308
retval |= snapVertex(ar, eBone->tail, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist, r_depth);
1310
retval |= snapVertex(ar, eBone->head, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1311
retval |= snapVertex(ar, eBone->tail, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1310
1313
case SCE_SNAP_MODE_EDGE:
1311
retval |= snapEdge(ar, eBone->head, NULL, eBone->tail, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist, r_depth);
1314
retval |= snapEdge(ar, eBone->head, NULL, eBone->tail, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1329
1332
switch (snap_mode) {
1330
1333
case SCE_SNAP_MODE_VERTEX:
1331
retval |= snapVertex(ar, head_vec, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist, r_depth);
1332
retval |= snapVertex(ar, tail_vec, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist, r_depth);
1334
retval |= snapVertex(ar, head_vec, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1335
retval |= snapVertex(ar, tail_vec, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1334
1337
case SCE_SNAP_MODE_EDGE:
1335
retval |= snapEdge(ar, head_vec, NULL, tail_vec, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist, r_depth);
1338
retval |= snapEdge(ar, head_vec, NULL, tail_vec, NULL, obmat, NULL, ray_start, ray_start_local, ray_normal_local, mval, r_loc, NULL, r_dist_px, r_depth);
1517
static int snapObject(Scene *scene, ARegion *ar, Object *ob, int editobject, float obmat[4][4],
1518
const float ray_start[3], const float ray_normal[3], const float mval[2],
1519
float r_loc[3], float r_no[3], int *r_dist, float *r_depth)
1521
static bool snapObject(Scene *scene, short snap_mode, ARegion *ar, Object *ob, float obmat[4][4], bool use_obedit,
1522
Object **r_ob, float r_obmat[4][4],
1523
const float ray_start[3], const float ray_normal[3], const float mval[2],
1524
float r_loc[3], float r_no[3], float *r_dist_px, float *r_depth)
1521
ToolSettings *ts = scene->toolsettings;
1526
bool retval = false;
1524
1528
if (ob->type == OB_MESH) {
1525
1529
BMEditMesh *em;
1526
1530
DerivedMesh *dm;
1529
em = BMEdit_FromObject(ob);
1530
/* dm = editbmesh_get_derived_cage(scene, ob, em, CD_MASK_BAREMESH); */
1531
dm = editbmesh_get_derived_base(ob, em); /* limitation, em & dm MUST have the same number of faces */
1533
em = BKE_editmesh_from_object(ob);
1534
dm = editbmesh_get_derived_cage(scene, ob, em, CD_MASK_BAREMESH);
1535
1538
dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
1538
retval = snapDerivedMesh(ts->snap_mode, ar, ob, dm, em, obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist, r_depth);
1541
retval = snapDerivedMesh(snap_mode, ar, ob, dm, em, obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist_px, r_depth);
1540
1543
dm->release(dm);
1542
1545
else if (ob->type == OB_ARMATURE) {
1543
retval = snapArmature(ts->snap_mode, ar, ob, ob->data, obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist, r_depth);
1546
retval = snapArmature(snap_mode, ar, ob, ob->data, obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist_px, r_depth);
1552
copy_m4_m4(r_obmat, obmat);
1549
static int snapObjects(Scene *scene, View3D *v3d, ARegion *ar, Object *obedit, const float mval[2],
1550
int *r_dist, float r_loc[3], float r_no[3], SnapMode mode)
1559
static bool snapObjectsRay(Scene *scene, short snap_mode, Base *base_act, View3D *v3d, ARegion *ar, Object *obedit,
1560
Object **r_ob, float r_obmat[4][4],
1561
const float ray_start[3], const float ray_normal[3], float *r_ray_dist,
1562
const float mval[2], float *r_dist_px, float r_loc[3], float r_no[3], SnapMode mode)
1553
float depth = FLT_MAX;
1555
float ray_start[3], ray_normal[3];
1557
ED_view3d_win_to_ray(ar, v3d, mval, ray_start, ray_normal);
1565
bool retval = false;
1559
1567
if (mode == SNAP_ALL && obedit) {
1560
1568
Object *ob = obedit;
1562
retval |= snapObject(scene, ar, ob, 1, ob->obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist, &depth);
1570
retval |= snapObject(scene, snap_mode, ar, ob, ob->obmat, true,
1572
ray_start, ray_normal, mval, r_loc, r_no, r_dist_px, r_ray_dist);
1565
1575
/* Need an exception for particle edit because the base is flagged with BA_HAS_RECALC_DATA
1568
1578
* To solve that problem, we do it first as an exception.
1571
1581
if (base && base->object && base->object->mode & OB_MODE_PARTICLE_EDIT) {
1572
1582
Object *ob = base->object;
1573
retval |= snapObject(scene, ar, ob, 0, ob->obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist, &depth);
1583
retval |= snapObject(scene, snap_mode, ar, ob, ob->obmat, false,
1585
ray_start, ray_normal, mval, r_loc, r_no, r_dist_px, r_ray_dist);
1576
1588
for (base = FIRSTBASE; base != NULL; base = base->next) {
1577
if ((BASE_VISIBLE(v3d, base)) &&
1589
if ((BASE_VISIBLE_BGMODE(v3d, scene, base)) &&
1578
1590
(base->flag & (BA_HAS_RECALC_OB | BA_HAS_RECALC_DATA)) == 0 &&
1580
1592
((mode == SNAP_NOT_SELECTED && (base->flag & (SELECT | BA_WAS_SEL)) == 0) ||
1581
(ELEM(mode, SNAP_ALL, SNAP_NOT_OBEDIT) && base != BASACT)))
1593
(ELEM(mode, SNAP_ALL, SNAP_NOT_OBEDIT) && base != base_act)))
1583
1595
Object *ob = base->object;
1587
1599
ListBase *lb = object_duplilist(scene, ob, FALSE);
1589
1601
for (dupli_ob = lb->first; dupli_ob; dupli_ob = dupli_ob->next) {
1590
Object *dob = dupli_ob->ob;
1592
retval |= snapObject(scene, ar, dob, 0, dupli_ob->mat, ray_start, ray_normal, mval, r_loc, r_no, r_dist, &depth);
1602
retval |= snapObject(scene, snap_mode, ar, dupli_ob->ob, dupli_ob->mat, false,
1604
ray_start, ray_normal, mval, r_loc, r_no, r_dist_px, r_ray_dist);
1595
1607
free_object_duplilist(lb);
1598
retval |= snapObject(scene, ar, ob, 0, ob->obmat, ray_start, ray_normal, mval, r_loc, r_no, r_dist, &depth);
1610
retval |= snapObject(scene, snap_mode, ar, ob, ob->obmat, false,
1612
ray_start, ray_normal, mval, r_loc, r_no, r_dist_px, r_ray_dist);
1605
int snapObjectsTransform(TransInfo *t, const float mval[2], int *r_dist, float r_loc[3], float r_no[3], SnapMode mode)
1607
return snapObjects(t->scene, t->view, t->ar, t->obedit, mval, r_dist, r_loc, r_no, mode);
1610
int snapObjectsContext(bContext *C, const float mval[2], int *r_dist, float r_loc[3], float r_no[3], SnapMode mode)
1618
static bool snapObjects(Scene *scene, short snap_mode, Base *base_act, View3D *v3d, ARegion *ar, Object *obedit,
1619
const float mval[2], float *r_dist_px,
1620
float r_loc[3], float r_no[3], float *r_ray_dist, SnapMode mode)
1622
float ray_start[3], ray_normal[3];
1624
if (ED_view3d_win_to_ray(ar, v3d, mval, ray_start, ray_normal, true) == false) {
1628
return snapObjectsRay(scene, snap_mode, base_act, v3d, ar, obedit,
1630
ray_start, ray_normal, r_ray_dist,
1631
mval, r_dist_px, r_loc, r_no, mode);
1634
bool snapObjectsTransform(TransInfo *t, const float mval[2], float *r_dist_px, float r_loc[3], float r_no[3], SnapMode mode)
1636
float ray_dist = TRANSFORM_DIST_MAX_RAY;
1637
return snapObjects(t->scene, t->scene->toolsettings->snap_mode, t->scene->basact, t->view, t->ar, t->obedit,
1638
mval, r_dist_px, r_loc, r_no, &ray_dist, mode);
1641
bool snapObjectsContext(bContext *C, const float mval[2], float *r_dist_px, float r_loc[3], float r_no[3], SnapMode mode)
1612
1643
ScrArea *sa = CTX_wm_area(C);
1613
1644
View3D *v3d = sa->spacedata.first;
1615
return snapObjects(CTX_data_scene(C), v3d, CTX_wm_region(C), CTX_data_edit_object(C), mval, r_dist, r_loc, r_no, mode);
1645
Scene *scene = CTX_data_scene(C);
1646
ARegion *ar = CTX_wm_region(C);
1647
Object *obedit = CTX_data_edit_object(C);
1648
float ray_dist = TRANSFORM_DIST_MAX_RAY;
1650
return snapObjects(scene, scene->toolsettings->snap_mode, scene->basact, v3d, ar, obedit,
1651
mval, r_dist_px, r_loc, r_no, &ray_dist, mode);
1654
bool snapObjectsEx(Scene *scene, Base *base_act, View3D *v3d, ARegion *ar, Object *obedit, short snap_mode,
1655
const float mval[2], float *r_dist_px,
1656
float r_loc[3], float r_no[3], float *r_ray_dist, SnapMode mode)
1658
return snapObjects(scene, snap_mode, base_act, v3d, ar, obedit,
1660
r_loc, r_no, r_ray_dist, mode);
1662
bool snapObjectsRayEx(Scene *scene, Base *base_act, View3D *v3d, ARegion *ar, Object *obedit, short snap_mode,
1663
Object **r_ob, float r_obmat[4][4],
1664
const float ray_start[3], const float ray_normal[3], float *r_ray_dist,
1665
const float mval[2], float *r_dist_px, float r_loc[3], float r_no[3], SnapMode mode)
1667
return snapObjectsRay(scene, snap_mode, base_act, v3d, ar, obedit,
1669
ray_start, ray_normal, r_ray_dist,
1670
mval, r_dist_px, r_loc, r_no, mode);
1618
1673
/******************** PEELING *********************************/
1853
int peelObjectsTransForm(TransInfo *t, ListBase *depth_peels, const float mval[2], SnapMode mode)
1911
bool peelObjectsTransForm(TransInfo *t, ListBase *depth_peels, const float mval[2], SnapMode mode)
1855
1913
return peelObjects(t->scene, t->view, t->ar, t->obedit, depth_peels, mval, mode);
1858
int peelObjectsContext(bContext *C, ListBase *depth_peels, const float mval[2], SnapMode mode)
1916
bool peelObjectsContext(bContext *C, ListBase *depth_peels, const float mval[2], SnapMode mode)
1918
Scene *scene = CTX_data_scene(C);
1860
1919
ScrArea *sa = CTX_wm_area(C);
1861
1920
View3D *v3d = sa->spacedata.first;
1921
ARegion *ar = CTX_wm_region(C);
1922
Object *obedit = CTX_data_edit_object(C);
1863
return peelObjects(CTX_data_scene(C), v3d, CTX_wm_region(C), CTX_data_edit_object(C), depth_peels, mval, mode);
1924
return peelObjects(scene, v3d, ar, obedit, depth_peels, mval, mode);
1866
1927
/******************** NODES ***********************************/
1868
static int snapNodeTest(View2D *v2d, bNode *node, SnapMode mode)
1929
static bool snapNodeTest(View2D *v2d, bNode *node, SnapMode mode)
1870
1931
/* node is use for snapping only if a) snap mode matches and b) node is inside the view */
1871
1932
return ((mode == SNAP_NOT_SELECTED && !(node->flag & NODE_SELECT)) ||
1902
1963
if (border & NODE_LEFT) {
1903
1964
new_dist = abs(totr.xmin - mval[0]);
1904
if (new_dist < *r_dist) {
1965
if (new_dist < *r_dist_px) {
1905
1966
UI_view2d_region_to_view(v2d, totr.xmin, mval[1], &r_loc[0], &r_loc[1]);
1967
*r_dist_px = new_dist;
1907
1968
*r_node_border = NODE_LEFT;
1912
1973
if (border & NODE_RIGHT) {
1913
1974
new_dist = abs(totr.xmax - mval[0]);
1914
if (new_dist < *r_dist) {
1975
if (new_dist < *r_dist_px) {
1915
1976
UI_view2d_region_to_view(v2d, totr.xmax, mval[1], &r_loc[0], &r_loc[1]);
1977
*r_dist_px = new_dist;
1917
1978
*r_node_border = NODE_RIGHT;
1922
1983
if (border & NODE_BOTTOM) {
1923
1984
new_dist = abs(totr.ymin - mval[1]);
1924
if (new_dist < *r_dist) {
1985
if (new_dist < *r_dist_px) {
1925
1986
UI_view2d_region_to_view(v2d, mval[0], totr.ymin, &r_loc[0], &r_loc[1]);
1987
*r_dist_px = new_dist;
1927
1988
*r_node_border = NODE_BOTTOM;
1932
1993
if (border & NODE_TOP) {
1933
1994
new_dist = abs(totr.ymax - mval[1]);
1934
if (new_dist < *r_dist) {
1995
if (new_dist < *r_dist_px) {
1935
1996
UI_view2d_region_to_view(v2d, mval[0], totr.ymax, &r_loc[0], &r_loc[1]);
1997
*r_dist_px = new_dist;
1937
1998
*r_node_border = NODE_TOP;
1945
static int snapNodes(ToolSettings *ts, SpaceNode *snode, ARegion *ar, const int mval[2],
1946
int *r_dist, float r_loc[2], char *r_node_border, SnapMode mode)
2006
static bool snapNodes(ToolSettings *ts, SpaceNode *snode, ARegion *ar, const int mval[2],
2007
float *r_dist_px, float r_loc[2], char *r_node_border, SnapMode mode)
1948
2009
bNodeTree *ntree = snode->edittree;
2011
bool retval = false;
1952
2013
*r_node_border = 0;
1954
2015
for (node = ntree->nodes.first; node; node = node->next) {
1955
2016
if (snapNodeTest(&ar->v2d, node, mode))
1956
retval |= snapNode(ts, snode, ar, node, mval, r_loc, r_dist, r_node_border);
2017
retval |= snapNode(ts, snode, ar, node, mval, r_loc, r_dist_px, r_node_border);
1962
int snapNodesTransform(TransInfo *t, const int mval[2], int *r_dist, float r_loc[2], char *r_node_border, SnapMode mode)
2023
bool snapNodesTransform(TransInfo *t, const int mval[2], float *r_dist_px, float r_loc[2], char *r_node_border, SnapMode mode)
1964
return snapNodes(t->settings, t->sa->spacedata.first, t->ar, mval, r_dist, r_loc, r_node_border, mode);
2025
return snapNodes(t->settings, t->sa->spacedata.first, t->ar, mval, r_dist_px, r_loc, r_node_border, mode);
1967
int snapNodesContext(bContext *C, const int mval[2], int *r_dist, float r_loc[2], char *r_node_border, SnapMode mode)
2028
bool snapNodesContext(bContext *C, const int mval[2], float *r_dist_px, float r_loc[2], char *r_node_border, SnapMode mode)
1969
2030
Scene *scene = CTX_data_scene(C);
1970
return snapNodes(scene->toolsettings, CTX_wm_space_node(C), CTX_wm_region(C), mval, r_dist, r_loc, r_node_border, mode);
2031
ARegion *ar = CTX_wm_region(C);
2032
return snapNodes(scene->toolsettings, CTX_wm_space_node(C), ar, mval, r_dist_px, r_loc, r_node_border, mode);
1973
2035
/*================================================================*/