code cleanup: use const events for modal and invoke operators.
[blender.git] / source / blender / editors / sculpt_paint / paint_utils.c
index 89a46272a8f80feefa42188c0bec55da2ec22638..19c061996e733d1e2f5f2a41a045c68a5526ecb0 100644 (file)
@@ -13,7 +13,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
@@ -41,6 +41,7 @@
 
 #include "BLI_math.h"
 #include "BLI_utildefines.h"
+#include "BLI_rect.h"
 
 #include "BKE_brush.h"
 #include "BKE_context.h"
@@ -80,8 +81,7 @@ int paint_convert_bb_to_rect(rcti *rect,
        float projection_mat[4][4];
        int i, j, k;
 
-       rect->xmin = rect->ymin = INT_MAX;
-       rect->xmax = rect->ymax = INT_MIN;
+       BLI_rcti_init_minmax(rect);
 
        /* return zero if the bounding box has non-positive volume */
        if (bb_min[0] > bb_max[0] || bb_min[1] > bb_max[1] || bb_min[2] > bb_max[2])
@@ -93,16 +93,19 @@ int paint_convert_bb_to_rect(rcti *rect,
                for (j = 0; j < 2; ++j) {
                        for (k = 0; k < 2; ++k) {
                                float vec[3], proj[2];
+                               int proj_i[2];
                                vec[0] = i ? bb_min[0] : bb_max[0];
                                vec[1] = j ? bb_min[1] : bb_max[1];
                                vec[2] = k ? bb_min[2] : bb_max[2];
                                /* convert corner to screen space */
-                               ED_view3d_project_float_v2(ar, vec, proj, projection_mat);
+                               ED_view3d_project_float_v2_m4(ar, vec, proj, projection_mat);
                                /* expand 2D rectangle */
-                               rect->xmin = MIN2(rect->xmin, proj[0]);
-                               rect->xmax = MAX2(rect->xmax, proj[0]);
-                               rect->ymin = MIN2(rect->ymin, proj[1]);
-                               rect->ymax = MAX2(rect->ymax, proj[1]);
+
+                               /* we could project directly to int? */
+                               proj_i[0] = proj[0];
+                               proj_i[1] = proj[1];
+
+                               BLI_rcti_do_minmax_v(rect, proj_i);
                        }
                }
        }
@@ -134,7 +137,7 @@ void paint_calc_redraw_planes(float planes[4][4],
        rect.ymin -= 2;
        rect.ymax += 2;
 
-       ED_view3d_calc_clipping(&bb, planes, &mats, &rect);
+       ED_view3d_clipping_calc(&bb, planes, &mats, &rect);
        mul_m4_fl(planes, -1.0f);
 }
 
@@ -156,15 +159,13 @@ float paint_calc_object_space_radius(ViewContext *vc, const float center[3],
 {
        Object *ob = vc->obact;
        float delta[3], scale, loc[3];
-       float mval_f[2];
+       const float mval_f[2] = {pixel_radius, 0.0f};
+       float zfac;
 
        mul_v3_m4v3(loc, ob->obmat, center);
 
-       initgrabz(vc->rv3d, loc[0], loc[1], loc[2]);
-
-       mval_f[0] = pixel_radius;
-       mval_f[1] = 0.0f;
-       ED_view3d_win_to_delta(vc->ar, mval_f, delta);
+       zfac = ED_view3d_calc_zfac(vc->rv3d, loc, NULL);
+       ED_view3d_win_to_delta(vc->ar, mval_f, delta, zfac);
 
        scale = fabsf(mat4_to_scale(ob->obmat));
        scale = (scale == 0.0f) ? 1.0f : scale;
@@ -172,28 +173,23 @@ float paint_calc_object_space_radius(ViewContext *vc, const float center[3],
        return len_v3(delta) / scale;
 }
 
-float paint_get_tex_pixel(Brush *br, float u, float v)
+float paint_get_tex_pixel(Brush *br, float u, float v, struct ImagePool *pool)
 {
-       TexResult texres;
-       float co[3];
+       TexResult texres = {0};
+       float co[3] = {u, v, 0.0f};
        int hasrgb;
 
-       co[0] = u;
-       co[1] = v;
-       co[2] = 0;
-
-       memset(&texres, 0, sizeof(TexResult));
-       hasrgb = multitex_ext(br->mtex.tex, co, NULL, NULL, 0, &texres);
+       hasrgb = multitex_ext(br->mtex.tex, co, NULL, NULL, 0, &texres, pool);
 
        if (hasrgb & TEX_RGB)
-               texres.tin = (0.35f * texres.tr + 0.45f * texres.tg + 0.2f * texres.tb) * texres.ta;
+               texres.tin = rgb_to_grayscale(&texres.tr) * texres.ta;
 
        return texres.tin;
 }
 
 /* 3D Paint */
 
-static void imapaint_project(Object *ob, float model[][4], float proj[][4], const float co[3], float pco[4])
+static void imapaint_project(Object *ob, float model[4][4], float proj[4][4], const float co[3], float pco[4])
 {
        copy_v3_v3(pco, co);
        pco[3] = 1.0f;
@@ -205,7 +201,7 @@ static void imapaint_project(Object *ob, float model[][4], float proj[][4], cons
 
 static void imapaint_tri_weights(Object *ob,
                                  const float v1[3], const float v2[3], const float v3[3],
-                                 const float co[3], float w[3])
+                                 const float co[2], float w[3])
 {
        float pv1[4], pv2[4], pv3[4], h[3], divw;
        float model[4][4], proj[4][4], wmat[3][3], invwmat[3][3];
@@ -252,19 +248,25 @@ static void imapaint_tri_weights(Object *ob,
 void imapaint_pick_uv(Scene *scene, Object *ob, unsigned int faceindex, const int xy[2], float uv[2])
 {
        DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
-       const int *index = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
        MTFace *tface = dm->getTessFaceDataArray(dm, CD_MTFACE), *tf;
        int numfaces = dm->getNumTessFaces(dm), a, findex;
        float p[2], w[3], absw, minabsw;
        MFace mf;
        MVert mv[4];
 
+       /* double lookup */
+       const int *index_mf_to_mpoly = dm->getTessFaceDataArray(dm, CD_ORIGINDEX);
+       const int *index_mp_to_orig  = dm->getPolyDataArray(dm, CD_ORIGINDEX);
+       if (index_mf_to_mpoly == NULL) {
+               index_mp_to_orig = NULL;
+       }
+
        minabsw = 1e10;
        uv[0] = uv[1] = 0.0;
 
        /* test all faces in the derivedmesh with the original index of the picked face */
        for (a = 0; a < numfaces; a++) {
-               findex = index ? index[a] : a;
+               findex = index_mf_to_mpoly ? DM_origindex_mface_mpoly(index_mf_to_mpoly, index_mp_to_orig, a) : a;
 
                if (findex == faceindex) {
                        dm->getTessFace(dm, a, &mf);
@@ -284,7 +286,7 @@ void imapaint_pick_uv(Scene *scene, Object *ob, unsigned int faceindex, const in
                                /* the triangle with the largest absolute values is the one
                                 * with the most negative weights */
                                imapaint_tri_weights(ob, mv[0].co, mv[1].co, mv[3].co, p, w);
-                               absw = fabs(w[0]) + fabs(w[1]) + fabs(w[2]);
+                               absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]);
                                if (absw < minabsw) {
                                        uv[0] = tf->uv[0][0] * w[0] + tf->uv[1][0] * w[1] + tf->uv[3][0] * w[2];
                                        uv[1] = tf->uv[0][1] * w[0] + tf->uv[1][1] * w[1] + tf->uv[3][1] * w[2];
@@ -292,7 +294,7 @@ void imapaint_pick_uv(Scene *scene, Object *ob, unsigned int faceindex, const in
                                }
 
                                imapaint_tri_weights(ob, mv[1].co, mv[2].co, mv[3].co, p, w);
-                               absw = fabs(w[0]) + fabs(w[1]) + fabs(w[2]);
+                               absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]);
                                if (absw < minabsw) {
                                        uv[0] = tf->uv[1][0] * w[0] + tf->uv[2][0] * w[1] + tf->uv[3][0] * w[2];
                                        uv[1] = tf->uv[1][1] * w[0] + tf->uv[2][1] * w[1] + tf->uv[3][1] * w[2];
@@ -301,7 +303,7 @@ void imapaint_pick_uv(Scene *scene, Object *ob, unsigned int faceindex, const in
                        }
                        else {
                                imapaint_tri_weights(ob, mv[0].co, mv[1].co, mv[2].co, p, w);
-                               absw = fabs(w[0]) + fabs(w[1]) + fabs(w[2]);
+                               absw = fabsf(w[0]) + fabsf(w[1]) + fabsf(w[2]);
                                if (absw < minabsw) {
                                        uv[0] = tf->uv[0][0] * w[0] + tf->uv[1][0] * w[1] + tf->uv[2][0] * w[2];
                                        uv[1] = tf->uv[0][1] * w[0] + tf->uv[1][1] * w[1] + tf->uv[2][1] * w[2];
@@ -333,9 +335,9 @@ int imapaint_pick_face(ViewContext *vc, const int mval[2], unsigned int *index,
 }
 
 /* used for both 3d view and image window */
-void paint_sample_color(Scene *scene, ARegion *ar, int x, int y)    /* frontbuf */
+void paint_sample_color(const bContext *C, ARegion *ar, int x, int y)    /* frontbuf */
 {
-       Brush *br = paint_brush(paint_get_active(scene));
+       Brush *br = paint_brush(paint_get_active_from_context(C));
        unsigned int col;
        char *cp;
 
@@ -357,15 +359,15 @@ void paint_sample_color(Scene *scene, ARegion *ar, int x, int y)    /* frontbuf
 
 static int brush_curve_preset_exec(bContext *C, wmOperator *op)
 {
-       Brush *br = paint_brush(paint_get_active(CTX_data_scene(C)));
-       brush_curve_preset(br, RNA_enum_get(op->ptr, "shape"));
+       Brush *br = paint_brush(paint_get_active_from_context(C));
+       BKE_brush_curve_preset(br, RNA_enum_get(op->ptr, "shape"));
 
        return OPERATOR_FINISHED;
 }
 
 static int brush_curve_preset_poll(bContext *C)
 {
-       Brush *br = paint_brush(paint_get_active(CTX_data_scene(C)));
+       Brush *br = paint_brush(paint_get_active_from_context(C));
 
        return br && br->curve;
 }
@@ -412,7 +414,7 @@ void PAINT_OT_face_select_linked(wmOperatorType *ot)
        ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
 }
 
-static int paint_select_linked_pick_invoke(bContext *C, wmOperator *op, wmEvent *event)
+static int paint_select_linked_pick_invoke(bContext *C, wmOperator *op, const wmEvent *event)
 {
        int mode = RNA_boolean_get(op->ptr, "extend") ? 1 : 0;
        paintface_select_linked(C, CTX_data_active_object(C), event->mval, mode);
@@ -446,7 +448,7 @@ static int face_select_all_exec(bContext *C, wmOperator *op)
 
 void PAINT_OT_face_select_all(wmOperatorType *ot)
 {
-       ot->name = "Face Selection";
+       ot->name = "(De)select All";
        ot->description = "Change selection for all faces";
        ot->idname = "PAINT_OT_face_select_all";
 
@@ -470,7 +472,7 @@ static int vert_select_all_exec(bContext *C, wmOperator *op)
 
 void PAINT_OT_vert_select_all(wmOperatorType *ot)
 {
-       ot->name = "Vertex Selection";
+       ot->name = "(De)select All";
        ot->description = "Change selection for all vertices";
        ot->idname = "PAINT_OT_vert_select_all";
 
@@ -482,46 +484,6 @@ void PAINT_OT_vert_select_all(wmOperatorType *ot)
        WM_operator_properties_select_all(ot);
 }
 
-static int vert_select_inverse_exec(bContext *C, wmOperator *UNUSED(op))
-{
-       Object *ob = CTX_data_active_object(C);
-       paintvert_deselect_all_visible(ob, SEL_INVERT, TRUE);
-       ED_region_tag_redraw(CTX_wm_region(C));
-       return OPERATOR_FINISHED;
-}
-
-void PAINT_OT_vert_select_inverse(wmOperatorType *ot)
-{
-       ot->name = "Vertex Select Invert";
-       ot->description = "Invert selection of vertices";
-       ot->idname = "PAINT_OT_vert_select_inverse";
-
-       ot->exec = vert_select_inverse_exec;
-       ot->poll = vert_paint_poll;
-
-       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
-}
-static int face_select_inverse_exec(bContext *C, wmOperator *UNUSED(op))
-{
-       Object *ob = CTX_data_active_object(C);
-       paintface_deselect_all_visible(ob, SEL_INVERT, TRUE);
-       ED_region_tag_redraw(CTX_wm_region(C));
-       return OPERATOR_FINISHED;
-}
-
-
-void PAINT_OT_face_select_inverse(wmOperatorType *ot)
-{
-       ot->name = "Face Select Invert";
-       ot->description = "Invert selection of faces";
-       ot->idname = "PAINT_OT_face_select_inverse";
-
-       ot->exec = face_select_inverse_exec;
-       ot->poll = facemask_paint_poll;
-
-       ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
-}
-
 static int face_select_hide_exec(bContext *C, wmOperator *op)
 {
        const int unselected = RNA_boolean_get(op->ptr, "unselected");