Modeling: Snap 3d cursor rotation to active #112718
|
@ -27,6 +27,7 @@ void unit_m2(float m[2][2]);
|
||||||
void unit_m3(float m[3][3]);
|
void unit_m3(float m[3][3]);
|
||||||
void unit_m4(float m[4][4]);
|
void unit_m4(float m[4][4]);
|
||||||
void unit_m4_db(double m[4][4]);
|
void unit_m4_db(double m[4][4]);
|
||||||
|
void m3_from_single_axis(float mat3[3][3], const float axis[3], const int axis_index);
|
||||||
|
|
||||||
void copy_m2_m2(float m1[2][2], const float m2[2][2]);
|
void copy_m2_m2(float m1[2][2], const float m2[2][2]);
|
||||||
void copy_m3_m3(float m1[3][3], const float m2[3][3]);
|
void copy_m3_m3(float m1[3][3], const float m2[3][3]);
|
||||||
|
@ -455,6 +456,12 @@ void rescale_m4(float mat[4][4], const float scale[3]);
|
||||||
*/
|
*/
|
||||||
void transform_pivot_set_m4(float mat[4][4], const float pivot[3]);
|
void transform_pivot_set_m4(float mat[4][4], const float pivot[3]);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \param rot: A 3x3 rotation matrix, skewness considered,
|
||||||
|
* one primary axis is conserved, normalized never negative.
|
||||||
|
*/
|
||||||
|
void remove_skew_m3_m3(float mat3[3][3], const float wmat[3][3], const int fixed_axis);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* \param rot: A 3x3 rotation matrix, normalized never negative.
|
* \param rot: A 3x3 rotation matrix, normalized never negative.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -69,6 +69,40 @@ void unit_m4_db(double m[4][4])
|
||||||
m[3][0] = m[3][1] = m[3][2] = 0.0f;
|
m[3][0] = m[3][1] = m[3][2] = 0.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void m3_from_single_axis(float mat3[3][3], const float axis[3], const int axis_index)
|
||||||
|
{
|
||||||
|
float len, vec[3], world_ax[3];
|
||||||
|
int ax_i = mod_i(axis_index, 3);
|
||||||
|
zero_v3(world_ax);
|
||||||
|
world_ax[ax_i] = 1;
|
||||||
|
// 1st axis
|
||||||
|
normalize_v3_v3(mat3[ax_i], axis);
|
||||||
|
if (compare_v3v3(mat3[ax_i], world_ax, FLT_EPSILON)) {
|
||||||
|
unit_m3(mat3);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// check not to generate zero axes
|
||||||
|
cross_v3_v3v3(vec, mat3[ax_i], world_ax);
|
||||||
|
len = len_v3(vec);
|
||||||
|
if (compare_ff(len, 0, 0.01f)) {
|
||||||
|
zero_v3(vec);
|
||||||
|
vec[mod_i((ax_i - 1), 3)] = 1;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
copy_v3_v3(vec, world_ax);
|
||||||
|
}
|
||||||
|
// remaining axes
|
||||||
|
for (int i = 0; i < 2; i++) {
|
||||||
|
ax_i = mod_i((ax_i - 1), 3);
|
||||||
|
cross_v3_v3v3(mat3[ax_i], vec, axis);
|
||||||
|
len = normalize_v3(mat3[ax_i]);
|
||||||
|
copy_v3_v3(vec, mat3[ax_i]);
|
||||||
|
}
|
||||||
|
if (UNLIKELY(is_negative_m3(mat3))) {
|
||||||
|
negate_m3(mat3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void copy_m2_m2(float m1[2][2], const float m2[2][2])
|
void copy_m2_m2(float m1[2][2], const float m2[2][2])
|
||||||
{
|
{
|
||||||
memcpy(m1, m2, sizeof(float[2][2]));
|
memcpy(m1, m2, sizeof(float[2][2]));
|
||||||
|
@ -2210,6 +2244,29 @@ void mat3_to_rot_size(float rot[3][3], float size[3], const float mat3[3][3])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Makes a skewed rotation matrices, keeps an axis fixed,
|
||||||
|
* and orthogonalizes the other two with respect to fixed axis
|
||||||
|
*/
|
||||||
|
void remove_skew_m3_m3(float mat3[3][3], const float wmat[3][3], const int fixed_axis)
|
||||||
|
{
|
||||||
|
int cur_axis, prev_axis, f_axis = mod_i(fixed_axis, 3);
|
||||||
|
float proj[3];
|
||||||
|
normalize_v3_v3(mat3[f_axis], wmat[f_axis]); // Keep fixed axis as is
|
||||||
|
for (int i = 1; i <= 2; i++) { // For the two remaining axes do orthogonalization
|
||||||
|
cur_axis = mod_i((f_axis + i), 3);
|
||||||
|
copy_v3_v3(mat3[cur_axis], wmat[cur_axis]);
|
||||||
|
for (int j = f_axis; j < f_axis + i; j++) { // Make it perpendicular to previous axes
|
||||||
|
prev_axis = mod_i(j, 3);
|
||||||
|
project_v3_v3v3(proj, mat3[cur_axis], mat3[prev_axis]);
|
||||||
|
sub_v3_v3(mat3[cur_axis], proj);
|
||||||
|
}
|
||||||
|
normalize_v3(mat3[cur_axis]);
|
||||||
|
}
|
||||||
|
if (UNLIKELY(is_negative_m3(mat3))) {
|
||||||
|
negate_m3(mat3);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void mat4_to_rot(float rot[3][3], const float wmat[4][4])
|
void mat4_to_rot(float rot[3][3], const float wmat[4][4])
|
||||||
{
|
{
|
||||||
normalize_v3_v3(rot[0], wmat[0]);
|
normalize_v3_v3(rot[0], wmat[0]);
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
#include "MEM_guardedalloc.h"
|
#include "MEM_guardedalloc.h"
|
||||||
|
|
||||||
#include "BLI_listbase.h"
|
#include "BLI_listbase.h"
|
||||||
|
#include "BLI_math_matrix.h"
|
||||||
#include "BLI_math_vector.h"
|
#include "BLI_math_vector.h"
|
||||||
|
|
||||||
#include "BKE_curve.h"
|
#include "BKE_curve.h"
|
||||||
|
@ -242,4 +243,23 @@ bool ED_curve_active_center(Curve *cu, float center[3])
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool ED_curve_active_rot(Curve *cu, float rot[3][3])
|
||||||
|
{
|
||||||
|
float _axis[3];
|
||||||
|
Nurb *nu = nullptr;
|
||||||
|
void *vert = nullptr;
|
||||||
|
if (!BKE_curve_nurb_vert_active_get(cu, &nu, &vert)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (nu->type == CU_BEZIER) {
|
||||||
|
BezTriple *bezt = (BezTriple *)vert;
|
||||||
|
sub_v3_v3v3(_axis, bezt->vec[2], bezt->vec[1]);
|
||||||
|
m3_from_single_axis(rot, _axis, 1);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
unit_m3(rot);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/** \} */
|
/** \} */
|
||||||
|
|
|
@ -100,6 +100,8 @@ int ED_curve_updateAnimPaths(Main *bmain, Curve *cu);
|
||||||
|
|
||||||
bool ED_curve_active_center(Curve *cu, float center[3]);
|
bool ED_curve_active_center(Curve *cu, float center[3]);
|
||||||
|
|
||||||
|
bool ED_curve_active_rot(Curve *cu, float rot[3][3]);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Text box selection.
|
* Text box selection.
|
||||||
*
|
*
|
||||||
|
|
|
@ -71,6 +71,12 @@ bool ED_object_calc_active_center_for_editmode(Object *obedit,
|
||||||
bool ED_object_calc_active_center_for_posemode(Object *ob, bool select_only, float r_center[3]);
|
bool ED_object_calc_active_center_for_posemode(Object *ob, bool select_only, float r_center[3]);
|
||||||
bool ED_object_calc_active_center(Object *ob, bool select_only, float r_center[3]);
|
bool ED_object_calc_active_center(Object *ob, bool select_only, float r_center[3]);
|
||||||
|
|
||||||
|
bool ED_object_calc_active_world_rot_for_editmode(Object *obedit,
|
||||||
|
bool select_only,
|
||||||
|
float r_rot[3][3]);
|
||||||
|
bool ED_object_calc_active_rot_for_posemode(Object *ob, bool select_only, float r_rot[3][3]);
|
||||||
|
bool ED_object_calc_active_rot(Object *ob, const bool select_only, float r_rot[3][3]);
|
||||||
|
|
||||||
/* Object Data Container helper API. */
|
/* Object Data Container helper API. */
|
||||||
struct XFormObjectData_Container;
|
struct XFormObjectData_Container;
|
||||||
XFormObjectData_Container *ED_object_data_xform_container_create();
|
XFormObjectData_Container *ED_object_data_xform_container_create();
|
||||||
|
|
|
@ -136,6 +136,110 @@ bool ED_object_calc_active_center(Object *ob, const bool select_only, float r_ce
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Functions for getting the rotation of active element
|
||||||
|
bool ED_object_calc_active_world_rot_for_editmode(Object *obedit,
|
||||||
|
bool select_only,
|
||||||
|
float r_rot[3][3])
|
||||||
|
{
|
||||||
|
switch (obedit->type) {
|
||||||
|
case OB_MESH: {
|
||||||
|
BMEditMesh *em = BKE_editmesh_from_object(obedit);
|
||||||
|
BMEditSelection ese;
|
||||||
|
|
||||||
|
if (BM_select_history_active_get(em->bm, &ese)) {
|
||||||
|
float _axis[3];
|
||||||
|
BM_editselection_normal(&ese, _axis);
|
||||||
|
m3_from_single_axis(r_rot, _axis, 2);
|
||||||
|
mul_m3_m4m3(r_rot, obedit->object_to_world, r_rot);
|
||||||
|
remove_skew_m3_m3(r_rot, r_rot, 0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OB_ARMATURE: {
|
||||||
|
bArmature *arm = static_cast<bArmature *>(obedit->data);
|
||||||
|
EditBone *ebo = arm->act_edbone;
|
||||||
|
|
||||||
|
if (ebo && (!select_only || (ebo->flag & (BONE_SELECTED | BONE_ROOTSEL)))) {
|
||||||
|
copy_m3_m4(r_rot, ebo->disp_mat);
|
||||||
|
mul_m3_m4m3(r_rot, obedit->object_to_world, r_rot);
|
||||||
|
remove_skew_m3_m3(r_rot, r_rot, 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OB_CURVES_LEGACY:
|
||||||
|
case OB_SURF: {
|
||||||
|
Curve *cu = static_cast<Curve *>(obedit->data);
|
||||||
|
|
||||||
|
if (ED_curve_active_rot(cu, r_rot)) {
|
||||||
|
mul_m3_m4m3(r_rot, obedit->object_to_world, r_rot);
|
||||||
|
remove_skew_m3_m3(r_rot, r_rot, 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OB_MBALL: {
|
||||||
|
MetaBall *mb = static_cast<MetaBall *>(obedit->data);
|
||||||
|
MetaElem *ml_act = mb->lastelem;
|
||||||
|
|
||||||
|
if (ml_act && (!select_only || (ml_act->flag & SELECT))) {
|
||||||
|
copy_m3_m4(r_rot, obedit->object_to_world);
|
||||||
|
remove_skew_m3_m3(r_rot, r_rot, 2);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case OB_LATTICE: {
|
||||||
|
BPoint *actbp = BKE_lattice_active_point_get(static_cast<Lattice *>(obedit->data));
|
||||||
|
|
||||||
|
if (actbp) {
|
||||||
|
copy_m3_m4(r_rot, obedit->object_to_world);
|
||||||
|
remove_skew_m3_m3(r_rot, r_rot, 2);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ED_object_calc_active_rot_for_posemode(Object *ob, bool select_only, float r_rot[3][3])
|
||||||
|
{
|
||||||
|
bPoseChannel *pchan = BKE_pose_channel_active_if_bonecoll_visible(ob);
|
||||||
|
if (pchan && (!select_only || (pchan->bone->flag & BONE_SELECTED))) {
|
||||||
|
copy_m3_m4(r_rot, pchan->pose_mat);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ED_object_calc_active_rot(Object *ob, const bool select_only, float r_rot[3][3])
|
||||||
|
{
|
||||||
|
if (ob->mode & OB_MODE_EDIT) {
|
||||||
|
if (ED_object_calc_active_world_rot_for_editmode(ob, select_only, r_rot)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (ob->mode & OB_MODE_POSE) {
|
||||||
|
if (ED_object_calc_active_rot_for_posemode(ob, select_only, r_rot)) {
|
||||||
|
mul_m3_m4m3(r_rot, ob->object_to_world, r_rot);
|
||||||
|
remove_skew_m3_m3(r_rot, r_rot, 1);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!select_only || (ob->base_flag & BASE_SELECTED)) {
|
||||||
|
copy_m3_m4(r_rot, ob->object_to_world);
|
||||||
|
remove_skew_m3_m3(r_rot, r_rot, 2);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/** \} */
|
/** \} */
|
||||||
|
|
||||||
/* -------------------------------------------------------------------- */
|
/* -------------------------------------------------------------------- */
|
||||||
|
|
|
@ -600,6 +600,19 @@ bool ED_view3d_snap_selected_to_location(bContext *C,
|
||||||
/** \name Snap Selection to Cursor Operator
|
/** \name Snap Selection to Cursor Operator
|
||||||
* \{ */
|
* \{ */
|
||||||
|
|
||||||
|
enum {
|
||||||
|
SNAP_LOC = 0,
|
||||||
|
SNAP_ROT,
|
||||||
|
SNAP_LOC_ROT,
|
||||||
|
};
|
||||||
|
|
||||||
|
static const EnumPropertyItem snap_transform_mode_items[] = {
|
||||||
|
{SNAP_LOC, "LOC", 0, "Location", ""},
|
||||||
|
{SNAP_ROT, "ROT", 0, "Rotation", ""},
|
||||||
|
{SNAP_LOC_ROT, "LOC_ROT", 0, "Location & Rotation", ""},
|
||||||
|
{0, nullptr, 0, nullptr, nullptr},
|
||||||
|
};
|
||||||
|
|
||||||
static int snap_selected_to_cursor_exec(bContext *C, wmOperator *op)
|
static int snap_selected_to_cursor_exec(bContext *C, wmOperator *op)
|
||||||
{
|
{
|
||||||
const bool use_offset = RNA_boolean_get(op->ptr, "use_offset");
|
const bool use_offset = RNA_boolean_get(op->ptr, "use_offset");
|
||||||
|
@ -930,11 +943,43 @@ static bool snap_calc_active_center(bContext *C, const bool select_only, float r
|
||||||
return ED_object_calc_active_center(ob, select_only, r_center);
|
return ED_object_calc_active_center(ob, select_only, r_center);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int snap_curs_to_active_exec(bContext *C, wmOperator * /*op*/)
|
static bool snap_calc_active_rot(bContext *C, const bool select_only, float r_rot[3][3])
|
||||||
|
{
|
||||||
|
Object *ob = CTX_data_active_object(C);
|
||||||
|
if (ob == nullptr) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return ED_object_calc_active_rot(ob, select_only, r_rot);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int snap_curs_to_active_exec(bContext *C, wmOperator *op)
|
||||||
{
|
{
|
||||||
Scene *scene = CTX_data_scene(C);
|
Scene *scene = CTX_data_scene(C);
|
||||||
|
const int snap_mode = RNA_enum_get(op->ptr, "snap_mode");
|
||||||
|
bool is_snap_done = false, is_loc_done = false, is_rot_done = false;
|
||||||
|
float r_center[3], r_rot[3][3];
|
||||||
|
switch (snap_mode) {
|
||||||
|
case SNAP_LOC:
|
||||||
|
is_loc_done = is_snap_done = snap_calc_active_center(C, false, r_center);
|
||||||
|
break;
|
||||||
|
case SNAP_ROT:
|
||||||
|
is_rot_done = is_snap_done = snap_calc_active_rot(C, false, r_rot);
|
||||||
|
break;
|
||||||
|
case SNAP_LOC_ROT:
|
||||||
|
is_loc_done = is_rot_done = is_snap_done = snap_calc_active_center(C, false, r_center) &
|
||||||
|
snap_calc_active_rot(C, false, r_rot);
|
||||||
|
break;
|
||||||
|
|
||||||
if (snap_calc_active_center(C, false, scene->cursor.location)) {
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (is_snap_done) {
|
||||||
|
if (is_loc_done) {
|
||||||
|
copy_v3_v3(scene->cursor.location, r_center);
|
||||||
|
}
|
||||||
|
if (is_rot_done) {
|
||||||
|
BKE_scene_cursor_mat3_to_rot(&scene->cursor, r_rot, false);
|
||||||
|
}
|
||||||
WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, nullptr);
|
WM_event_add_notifier(C, NC_SPACE | ND_SPACE_VIEW3D, nullptr);
|
||||||
DEG_id_tag_update(&scene->id, ID_RECALC_COPY_ON_WRITE);
|
DEG_id_tag_update(&scene->id, ID_RECALC_COPY_ON_WRITE);
|
||||||
|
|
||||||
|
@ -956,6 +1001,9 @@ void VIEW3D_OT_snap_cursor_to_active(wmOperatorType *ot)
|
||||||
|
|
||||||
/* flags */
|
/* flags */
|
||||||
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
|
||||||
|
|
||||||
|
/* properties */
|
||||||
|
ot->prop = RNA_def_enum(ot->srna, "snap_mode", snap_transform_mode_items, 0, "Snap Mode", "");
|
||||||
}
|
}
|
||||||
|
|
||||||
/** \} */
|
/** \} */
|
||||||
|
|
Loading…
Reference in New Issue