void ED_view3d_operator_properties_viewmat_set(bContext *C, wmOperator *op) { ARegion *ar = CTX_wm_region(C); RegionView3D *rv3d = ED_view3d_context_rv3d(C); if (!RNA_struct_property_is_set(op->ptr, "region_width")) RNA_int_set(op->ptr, "region_width", ar->winx); if (!RNA_struct_property_is_set(op->ptr, "region_height")) RNA_int_set(op->ptr, "region_height", ar->winy); if (!RNA_struct_property_is_set(op->ptr, "perspective_matrix")) RNA_float_set_array(op->ptr, "perspective_matrix", (float *)rv3d->persmat); }
Nurb *ED_curve_add_nurbs_primitive(bContext *C, Object *obedit, float mat[4][4], int type, int newob) { static int xzproj = 0; /* this function calls itself... */ ListBase *editnurb = object_editcurve_get(obedit); RegionView3D *rv3d = ED_view3d_context_rv3d(C); Nurb *nu = NULL; BezTriple *bezt; BPoint *bp; Curve *cu = (Curve *)obedit->data; float vec[3], zvec[3] = {0.0f, 0.0f, 1.0f}; float umat[4][4], viewmat[4][4]; float fac; int a, b; const float grid = 1.0f; const int cutype = (type & CU_TYPE); // poly, bezier, nurbs, etc const int stype = (type & CU_PRIMITIVE); unit_m4(umat); unit_m4(viewmat); if (rv3d) { copy_m4_m4(viewmat, rv3d->viewmat); copy_v3_v3(zvec, rv3d->viewinv[2]); } BKE_nurbList_flag_set(editnurb, 0); /* these types call this function to return a Nurb */ if (stype != CU_PRIM_TUBE && stype != CU_PRIM_DONUT) { nu = (Nurb *)MEM_callocN(sizeof(Nurb), "addNurbprim"); nu->type = cutype; nu->resolu = cu->resolu; nu->resolv = cu->resolv; } switch (stype) { case CU_PRIM_CURVE: /* curve */ nu->resolu = cu->resolu; if (cutype == CU_BEZIER) { nu->pntsu = 2; nu->bezt = (BezTriple *)MEM_callocN(2 * sizeof(BezTriple), "addNurbprim1"); bezt = nu->bezt; bezt->h1 = bezt->h2 = HD_ALIGN; bezt->f1 = bezt->f2 = bezt->f3 = SELECT; bezt->radius = 1.0; bezt->vec[1][0] += -grid; bezt->vec[0][0] += -1.5f * grid; bezt->vec[0][1] += -0.5f * grid; bezt->vec[2][0] += -0.5f * grid; bezt->vec[2][1] += 0.5f * grid; for (a = 0; a < 3; a++) mul_m4_v3(mat, bezt->vec[a]); bezt++; bezt->h1 = bezt->h2 = HD_ALIGN; bezt->f1 = bezt->f2 = bezt->f3 = SELECT; bezt->radius = bezt->weight = 1.0; bezt->vec[0][0] = 0; bezt->vec[0][1] = 0; bezt->vec[1][0] = grid; bezt->vec[1][1] = 0; bezt->vec[2][0] = grid * 2; bezt->vec[2][1] = 0; for (a = 0; a < 3; a++) mul_m4_v3(mat, bezt->vec[a]); BKE_nurb_handles_calc(nu); } else { nu->pntsu = 4; nu->pntsv = 1; nu->orderu = 4; nu->bp = (BPoint *)MEM_callocN(sizeof(BPoint) * 4, "addNurbprim3"); bp = nu->bp; for (a = 0; a < 4; a++, bp++) { bp->vec[3] = 1.0; bp->f1 = SELECT; bp->radius = bp->weight = 1.0; } bp = nu->bp; bp->vec[0] += -1.5f * grid; bp++; bp->vec[0] += -grid; bp->vec[1] += grid; bp++; bp->vec[0] += grid; bp->vec[1] += grid; bp++; bp->vec[0] += 1.5f * grid; bp = nu->bp; for (a = 0; a < 4; a++, bp++) mul_m4_v3(mat, bp->vec); if (cutype == CU_NURBS) { nu->knotsu = NULL; /* nurbs_knot_calc_u allocates */ BKE_nurb_knot_calc_u(nu); } } break; case CU_PRIM_PATH: /* 5 point path */ nu->pntsu = 5; nu->pntsv = 1; nu->orderu = 5; nu->flagu = CU_NURB_ENDPOINT; /* endpoint */ nu->resolu = cu->resolu; nu->bp = (BPoint *)MEM_callocN(sizeof(BPoint) * 5, "addNurbprim3"); bp = nu->bp; for (a = 0; a < 5; a++, bp++) { bp->vec[3] = 1.0; bp->f1 = SELECT; bp->radius = bp->weight = 1.0; } bp = nu->bp; bp->vec[0] += -2.0f * grid; bp++; bp->vec[0] += -grid; bp++; bp++; bp->vec[0] += grid; bp++; bp->vec[0] += 2.0f * grid; bp = nu->bp; for (a = 0; a < 5; a++, bp++) mul_m4_v3(mat, bp->vec); if (cutype == CU_NURBS) { nu->knotsu = NULL; /* nurbs_knot_calc_u allocates */ BKE_nurb_knot_calc_u(nu); } break; case CU_PRIM_CIRCLE: /* circle */ nu->resolu = cu->resolu; if (cutype == CU_BEZIER) { nu->pntsu = 4; nu->bezt = (BezTriple *)MEM_callocN(sizeof(BezTriple) * 4, "addNurbprim1"); nu->flagu = CU_NURB_CYCLIC; bezt = nu->bezt; bezt->h1 = bezt->h2 = HD_AUTO; bezt->f1 = bezt->f2 = bezt->f3 = SELECT; bezt->vec[1][0] += -grid; for (a = 0; a < 3; a++) mul_m4_v3(mat, bezt->vec[a]); bezt->radius = bezt->weight = 1.0; bezt++; bezt->h1 = bezt->h2 = HD_AUTO; bezt->f1 = bezt->f2 = bezt->f3 = SELECT; bezt->vec[1][1] += grid; for (a = 0; a < 3; a++) mul_m4_v3(mat, bezt->vec[a]); bezt->radius = bezt->weight = 1.0; bezt++; bezt->h1 = bezt->h2 = HD_AUTO; bezt->f1 = bezt->f2 = bezt->f3 = SELECT; bezt->vec[1][0] += grid; for (a = 0; a < 3; a++) mul_m4_v3(mat, bezt->vec[a]); bezt->radius = bezt->weight = 1.0; bezt++; bezt->h1 = bezt->h2 = HD_AUTO; bezt->f1 = bezt->f2 = bezt->f3 = SELECT; bezt->vec[1][1] += -grid; for (a = 0; a < 3; a++) mul_m4_v3(mat, bezt->vec[a]); bezt->radius = bezt->weight = 1.0; BKE_nurb_handles_calc(nu); } else if (cutype == CU_NURBS) { /* nurb */ nu->pntsu = 8; nu->pntsv = 1; nu->orderu = 4; nu->bp = (BPoint *)MEM_callocN(sizeof(BPoint) * 8, "addNurbprim6"); nu->flagu = CU_NURB_CYCLIC; bp = nu->bp; for (a = 0; a < 8; a++) { bp->f1 = SELECT; if (xzproj == 0) { bp->vec[0] += nurbcircle[a][0] * grid; bp->vec[1] += nurbcircle[a][1] * grid; } else { bp->vec[0] += 0.25f * nurbcircle[a][0] * grid - 0.75f * grid; bp->vec[2] += 0.25f * nurbcircle[a][1] * grid; } if (a & 1) bp->vec[3] = 0.25 * M_SQRT2; else bp->vec[3] = 1.0; mul_m4_v3(mat, bp->vec); bp->radius = bp->weight = 1.0; bp++; } BKE_nurb_knot_calc_u(nu); } break; case CU_PRIM_PATCH: /* 4x4 patch */ if (cutype == CU_NURBS) { /* nurb */ nu->pntsu = 4; nu->pntsv = 4; nu->orderu = 4; nu->orderv = 4; nu->flag = CU_SMOOTH; nu->bp = (BPoint *)MEM_callocN(sizeof(BPoint) * (4 * 4), "addNurbprim6"); nu->flagu = 0; nu->flagv = 0; bp = nu->bp; for (a = 0; a < 4; a++) { for (b = 0; b < 4; b++) { bp->f1 = SELECT; fac = (float)a - 1.5f; bp->vec[0] += fac * grid; fac = (float)b - 1.5f; bp->vec[1] += fac * grid; if ((a == 1 || a == 2) && (b == 1 || b == 2)) { bp->vec[2] += grid; } mul_m4_v3(mat, bp->vec); bp->vec[3] = 1.0; bp++; } } BKE_nurb_knot_calc_u(nu); BKE_nurb_knot_calc_v(nu); } break; case CU_PRIM_TUBE: /* Cylinder */ if (cutype == CU_NURBS) { nu = ED_curve_add_nurbs_primitive(C, obedit, mat, CU_NURBS | CU_PRIM_CIRCLE, 0); /* circle */ nu->resolu = cu->resolu; nu->flag = CU_SMOOTH; BLI_addtail(editnurb, nu); /* temporal for extrude and translate */ vec[0] = vec[1] = 0.0; vec[2] = -grid; mul_mat3_m4_v3(mat, vec); ed_editnurb_translate_flag(editnurb, SELECT, vec); ed_editnurb_extrude_flag(cu->editnurb, SELECT); mul_v3_fl(vec, -2.0f); ed_editnurb_translate_flag(editnurb, SELECT, vec); BLI_remlink(editnurb, nu); a = nu->pntsu * nu->pntsv; bp = nu->bp; while (a-- > 0) { bp->f1 |= SELECT; bp++; } } break; case CU_PRIM_SPHERE: /* sphere */ if (cutype == CU_NURBS) { float tmp_cent[3] = {0.f, 0.f, 0.f}; float tmp_vec[3] = {0.f, 0.f, 1.f}; nu->pntsu = 5; nu->pntsv = 1; nu->orderu = 3; nu->resolu = cu->resolu; nu->resolv = cu->resolv; nu->flag = CU_SMOOTH; nu->bp = (BPoint *)MEM_callocN(sizeof(BPoint) * 5, "addNurbprim6"); nu->flagu = 0; bp = nu->bp; for (a = 0; a < 5; a++) { bp->f1 = SELECT; bp->vec[0] += nurbcircle[a][0] * grid; bp->vec[2] += nurbcircle[a][1] * grid; if (a & 1) bp->vec[3] = 0.5 * M_SQRT2; else bp->vec[3] = 1.0; mul_m4_v3(mat, bp->vec); bp++; } nu->flagu = CU_NURB_BEZIER; BKE_nurb_knot_calc_u(nu); BLI_addtail(editnurb, nu); /* temporal for spin */ if (newob && (U.flag & USER_ADD_VIEWALIGNED) == 0) ed_editnurb_spin(umat, obedit, tmp_vec, tmp_cent); else if ((U.flag & USER_ADD_VIEWALIGNED)) ed_editnurb_spin(viewmat, obedit, zvec, mat[3]); else ed_editnurb_spin(umat, obedit, tmp_vec, mat[3]); BKE_nurb_knot_calc_v(nu); a = nu->pntsu * nu->pntsv; bp = nu->bp; while (a-- > 0) { bp->f1 |= SELECT; bp++; } BLI_remlink(editnurb, nu); } break; case CU_PRIM_DONUT: /* torus */ if (cutype == CU_NURBS) { float tmp_cent[3] = {0.f, 0.f, 0.f}; float tmp_vec[3] = {0.f, 0.f, 1.f}; xzproj = 1; nu = ED_curve_add_nurbs_primitive(C, obedit, mat, CU_NURBS | CU_PRIM_CIRCLE, 0); /* circle */ xzproj = 0; nu->resolu = cu->resolu; nu->resolv = cu->resolv; nu->flag = CU_SMOOTH; BLI_addtail(editnurb, nu); /* temporal for spin */ /* same as above */ if (newob && (U.flag & USER_ADD_VIEWALIGNED) == 0) ed_editnurb_spin(umat, obedit, tmp_vec, tmp_cent); else if ((U.flag & USER_ADD_VIEWALIGNED)) ed_editnurb_spin(viewmat, obedit, zvec, mat[3]); else ed_editnurb_spin(umat, obedit, tmp_vec, mat[3]); BLI_remlink(editnurb, nu); a = nu->pntsu * nu->pntsv; bp = nu->bp; while (a-- > 0) { bp->f1 |= SELECT; bp++; } } break; default: /* should never happen */ BLI_assert(!"invalid nurbs type"); return NULL; } BLI_assert(nu != NULL); if (nu) { /* should always be set */ if ((obedit->type != OB_SURF) && ((cu->flag & CU_3D) == 0)) { nu->flag |= CU_2D; } nu->flag |= CU_SMOOTH; cu->actnu = BLI_listbase_count(editnurb); cu->actvert = CU_ACT_NONE; BKE_nurb_test2D(nu); } return nu; }
static int mesh_bisect_exec(bContext *C, wmOperator *op) { Scene *scene = CTX_data_scene(C); /* both can be NULL, fallbacks values are used */ View3D *v3d = CTX_wm_view3d(C); RegionView3D *rv3d = ED_view3d_context_rv3d(C); Object *obedit = CTX_data_edit_object(C); BMEditMesh *em = BKE_editmesh_from_object(obedit); BMesh *bm; BMOperator bmop; float plane_co[3]; float plane_no[3]; float imat[4][4]; const float thresh = RNA_float_get(op->ptr, "threshold"); const bool use_fill = RNA_boolean_get(op->ptr, "use_fill"); const bool clear_inner = RNA_boolean_get(op->ptr, "clear_inner"); const bool clear_outer = RNA_boolean_get(op->ptr, "clear_outer"); PropertyRNA *prop_plane_co; PropertyRNA *prop_plane_no; prop_plane_co = RNA_struct_find_property(op->ptr, "plane_co"); if (RNA_property_is_set(op->ptr, prop_plane_co)) { RNA_property_float_get_array(op->ptr, prop_plane_co, plane_co); } else { copy_v3_v3(plane_co, ED_view3d_cursor3d_get(scene, v3d)); RNA_property_float_set_array(op->ptr, prop_plane_co, plane_co); } prop_plane_no = RNA_struct_find_property(op->ptr, "plane_no"); if (RNA_property_is_set(op->ptr, prop_plane_no)) { RNA_property_float_get_array(op->ptr, prop_plane_no, plane_no); } else { if (rv3d) { copy_v3_v3(plane_no, rv3d->viewinv[1]); } else { /* fallback... */ plane_no[0] = plane_no[1] = 0.0f; plane_no[2] = 1.0f; } RNA_property_float_set_array(op->ptr, prop_plane_no, plane_no); } /* -------------------------------------------------------------------- */ /* Modal support */ /* Note: keep this isolated, exec can work wihout this */ if ((op->customdata != NULL) && mesh_bisect_interactive_calc(C, op, em, plane_co, plane_no)) { /* write back to the props */ RNA_property_float_set_array(op->ptr, prop_plane_no, plane_no); RNA_property_float_set_array(op->ptr, prop_plane_co, plane_co); } /* End Modal */ /* -------------------------------------------------------------------- */ bm = em->bm; invert_m4_m4(imat, obedit->obmat); mul_m4_v3(imat, plane_co); mul_mat3_m4_v3(imat, plane_no); EDBM_op_init(em, &bmop, op, "bisect_plane geom=%hvef plane_co=%v plane_no=%v dist=%f clear_inner=%b clear_outer=%b", BM_ELEM_SELECT, plane_co, plane_no, thresh, clear_inner, clear_outer); BMO_op_exec(bm, &bmop); EDBM_flag_disable_all(em, BM_ELEM_SELECT); if (use_fill) { float normal_fill[3]; BMOperator bmop_fill; BMOperator bmop_attr; normalize_v3_v3(normal_fill, plane_no); if (clear_outer == true && clear_inner == false) { negate_v3(normal_fill); } /* Fill */ BMO_op_initf( bm, &bmop_fill, op->flag, "triangle_fill edges=%S normal=%v use_dissolve=%b", &bmop, "geom_cut.out", normal_fill, true); BMO_op_exec(bm, &bmop_fill); /* Copy Attributes */ BMO_op_initf(bm, &bmop_attr, op->flag, "face_attribute_fill faces=%S use_normals=%b use_data=%b", &bmop_fill, "geom.out", false, true); BMO_op_exec(bm, &bmop_attr); BMO_slot_buffer_hflag_enable(bm, bmop_fill.slots_out, "geom.out", BM_FACE, BM_ELEM_SELECT, true); BMO_op_finish(bm, &bmop_attr); BMO_op_finish(bm, &bmop_fill); } BMO_slot_buffer_hflag_enable(bm, bmop.slots_out, "geom_cut.out", BM_VERT | BM_EDGE, BM_ELEM_SELECT, true); if (!EDBM_op_finish(em, &bmop, op, true)) { return OPERATOR_CANCELLED; } else { EDBM_update_generic(em, true, true); EDBM_selectmode_flush(em); return OPERATOR_FINISHED; } }