Cleanup: Move some math C-API usage to C++ API

Mostly part of an attempt to remove some of the lesser-used
parts of the C math API.
This commit is contained in:
Hans Goudey
2024-12-20 12:07:05 -05:00
committed by Hans Goudey
parent 081b0258ef
commit 3d0988a719
21 changed files with 66 additions and 69 deletions

View File

@@ -6,6 +6,7 @@
#include "BLI_listbase.h"
#include "BLI_math_matrix.h"
#include "BLI_math_matrix.hh"
#include "BLI_math_rotation.h"
#include "BLI_math_vector.h"
#include "BLI_string.h"
@@ -29,13 +30,13 @@ static double EXPECT_M3_ORTHOGONAL(const float mat[3][3],
double epsilon_ortho)
{
/* Do the checks in double precision to avoid precision issues in the checks themselves. */
double dmat[3][3];
copy_m3d_m3(dmat, mat);
double3x3 dmat;
copy_m3d_m3(dmat.ptr(), mat);
/* Check individual axis scaling. */
EXPECT_NEAR(len_v3_db(dmat[0]), 1.0, epsilon_scale);
EXPECT_NEAR(len_v3_db(dmat[1]), 1.0, epsilon_scale);
EXPECT_NEAR(len_v3_db(dmat[2]), 1.0, epsilon_scale);
EXPECT_NEAR(math::length(dmat[0]), 1.0, epsilon_scale);
EXPECT_NEAR(math::length(dmat[1]), 1.0, epsilon_scale);
EXPECT_NEAR(math::length(dmat[2]), 1.0, epsilon_scale);
/* Check orthogonality. */
EXPECT_NEAR(dot_v3v3_db(dmat[0], dmat[1]), 0.0, epsilon_ortho);
@@ -43,7 +44,7 @@ static double EXPECT_M3_ORTHOGONAL(const float mat[3][3],
EXPECT_NEAR(dot_v3v3_db(dmat[1], dmat[2]), 0.0, epsilon_ortho);
/* Check determinant to detect flipping and as a secondary volume change check. */
double determinant = determinant_m3_array_db(dmat);
double determinant = math::determinant(dmat);
EXPECT_NEAR(determinant, 1.0, epsilon_ortho);

View File

@@ -4588,12 +4588,14 @@ static void damptrack_evaluate(bConstraint *con, bConstraintOb *cob, ListBase *t
static void damptrack_do_transform(float matrix[4][4], const float tarvec_in[3], int track_axis)
{
using namespace blender;
/* find the (unit) direction vector going from the owner to the target */
float tarvec[3];
float3 tarvec;
if (normalize_v3_v3(tarvec, tarvec_in) != 0.0f) {
float obvec[3], obloc[3];
float raxis[3], rangle;
float3 obvec, obloc;
float3 raxis;
float rangle;
float rmat[3][3], tmat[4][4];
/* find the (unit) direction that the axis we're interested in currently points
@@ -4619,7 +4621,7 @@ static void damptrack_do_transform(float matrix[4][4], const float tarvec_in[3],
* - the min/max wrappers around (obvec . tarvec) result (stored temporarily in rangle)
* are used to ensure that the smallest angle is chosen
*/
cross_v3_v3v3_hi_prec(raxis, obvec, tarvec);
raxis = math::cross_high_precision(obvec, tarvec);
rangle = dot_v3v3(obvec, tarvec);
rangle = acosf(max_ff(-1.0f, min_ff(1.0f, rangle)));

View File

@@ -893,14 +893,10 @@ static void update_velocities(FluidEffectorSettings *fes,
mul_v3_fl(hit_vel, fes->vel_multi);
/* Absolute representation of new object velocity. */
float abs_hit_vel[3];
copy_v3_v3(abs_hit_vel, hit_vel);
abs_v3(abs_hit_vel);
blender::float3 abs_hit_vel = blender::math::abs(blender::float3(hit_vel));
/* Absolute representation of current object velocity. */
float abs_vel[3];
copy_v3_v3(abs_vel, &velocity_map[index * 3]);
abs_v3(abs_vel);
blender::float3 abs_vel = blender::math::abs(blender::float3(&velocity_map[index * 3]));
switch (fes->guide_mode) {
case FLUID_EFFECTOR_GUIDE_AVERAGED:

View File

@@ -321,7 +321,7 @@ static void apply_watertight_check(Tree &pbvh, Image &image, ImageUser &image_us
}
if (image_buffer->byte_buffer.data) {
uint8_t *dest = &image_buffer->byte_buffer.data[pixel_offset * 4];
copy_v4_uchar(dest, 255);
dest[0] = dest[1] = dest[2] = dest[3] = 255;
}
pixel_offset += 1;
}

View File

@@ -1179,7 +1179,7 @@ void BM_face_triangulate(BMesh *bm,
void BM_face_splits_check_legal(BMesh *bm, BMFace *f, BMLoop *(*loops)[2], int len)
{
float out[2] = {-FLT_MAX, -FLT_MAX};
blender::float2 out = {-FLT_MAX, -FLT_MAX};
float center[2] = {0.0f, 0.0f};
float axis_mat[3][3];
float(*projverts)[2] = BLI_array_alloca(projverts, f->len);
@@ -1215,7 +1215,7 @@ void BM_face_splits_check_legal(BMesh *bm, BMFace *f, BMLoop *(*loops)[2], int l
bm->elem_index_dirty |= BM_LOOP;
/* ensure we are well outside the face bounds (value is arbitrary) */
add_v2_fl(out, 1.0f);
out += 1.0f;
for (i = 0; i < len; i++) {
edgeverts[i][0] = projverts[BM_elem_index_get(loops[i][0])];

View File

@@ -172,7 +172,7 @@ static GPUTexture *create_volume_texture(const int dim[3],
const void *data)
{
GPUTexture *tex = nullptr;
int final_dim[3] = {UNPACK3(dim)};
blender::int3 final_dim = {UNPACK3(dim)};
if (data == nullptr) {
return nullptr;
@@ -199,7 +199,7 @@ static GPUTexture *create_volume_texture(const int dim[3],
printf("Error: Could not create 3D texture.\n");
tex = GPU_texture_create_error(3, false);
}
else if (equals_v3v3_int(dim, final_dim)) {
else if (blender::int3(dim) == final_dim) {
/* No need to resize, just upload the data. */
GPU_texture_update_sub(tex, data_format, data, 0, 0, 0, UNPACK3(final_dim));
}

View File

@@ -33,7 +33,7 @@ struct DRWViewData {
bool from_viewport = false;
/** Common size for texture in the engines texture list.
* We free all texture lists if it changes. */
int texture_list_size[2] = {0, 0};
int2 texture_list_size = {0, 0};
double cache_time = 0.0;
@@ -181,7 +181,7 @@ void DRW_view_data_free(DRWViewData *view_data)
void DRW_view_data_texture_list_size_validate(DRWViewData *view_data, const int size[2])
{
if (!equals_v2v2_int(view_data->texture_list_size, size)) {
if (view_data->texture_list_size != int2(size)) {
draw_view_data_clear(view_data, false);
copy_v2_v2_int(view_data->texture_list_size, size);
}

View File

@@ -1369,8 +1369,7 @@ static void move_adjacent_handle(const ViewContext *vc, const wmEvent *event, Li
}
adj_bezt->h1 = adj_bezt->h2 = HD_FREE;
int displacement[2];
sub_v2_v2v2_int(displacement, event->xy, event->prev_xy);
blender::int2 displacement = blender::int2(event->xy) - blender::int2(event->prev_xy);
const float disp_fl[2] = {float(displacement[0]), float(displacement[1])};
move_bezt_handle_or_vertex_by_displacement(
vc, adj_bezt, bezt_idx, disp_fl, 0.0f, false, false);

View File

@@ -300,7 +300,7 @@ static bool annotation_stroke_filtermval(tGPsdata *p, const float mval[2], const
/* convert screen-coordinates to buffer-coordinates */
static void annotation_stroke_convertcoords(tGPsdata *p,
const float mval[2],
const blender::float2 mval,
float out[3],
const float *depth)
{
@@ -311,8 +311,7 @@ static void annotation_stroke_convertcoords(tGPsdata *p,
/* in 3d-space - pt->x/y/z are 3 side-by-side floats */
if (gpd->runtime.sbuffer_sflag & GP_STROKE_3DSPACE) {
int mval_i[2];
round_v2i_v2fl(mval_i, mval);
blender::int2 mval_i = blender::int2(mval);
if (annotation_project_check(p) && ED_view3d_autodist_simple(p->region, mval_i, out, 0, depth))
{
/* projecting onto 3D-Geometry
@@ -966,7 +965,7 @@ static void annotation_stroke_newfrombuffer(tGPsdata *p)
/* get an array of depths, far depths are blended */
if (annotation_project_check(p)) {
int mval_i[2], mval_prev[2] = {0};
blender::int2 mval_i, mval_prev = {0, 0};
int interp_depth = 0;
int found_depth = 0;
@@ -978,8 +977,7 @@ static void annotation_stroke_newfrombuffer(tGPsdata *p)
i < gpd->runtime.sbuffer_used;
i++, ptc++, pt++)
{
round_v2i_v2fl(mval_i, ptc->m_xy);
mval_i = blender::int2(ptc->m_xy);
if ((ED_view3d_depth_read_cached(depths, mval_i, depth_margin, depth_arr + i) == 0) &&
(i && (ED_view3d_depth_read_cached_seg(
depths, mval_i, mval_prev, depth_margin + 1, depth_arr + i) == 0)))
@@ -1121,8 +1119,7 @@ static void annotation_stroke_eraser_dostroke(tGPsdata *p,
bGPDspoint *pt1, *pt2;
int pc1[2] = {0};
int pc2[2] = {0};
int mval_i[2];
round_v2i_v2fl(mval_i, mval);
blender::int2 mval_i = blender::int2(mval);
if (gps->totpoints == 0) {
/* just free stroke */

View File

@@ -10679,12 +10679,11 @@ static int ui_handle_menu_event(bContext *C,
else {
if (event->type == MOUSEMOVE) {
WM_cursor_set(win, PopupTitleDragCursor);
int mdiff[2];
blender::int2 mdiff = blender::int2(event->xy) - blender::int2(menu->grab_xy_prev);
sub_v2_v2v2_int(mdiff, event->xy, menu->grab_xy_prev);
copy_v2_v2_int(menu->grab_xy_prev, event->xy);
add_v2_v2v2_int(menu->popup_create_vars.event_xy, menu->popup_create_vars.event_xy, mdiff);
menu->popup_create_vars.event_xy -= mdiff;
ui_popup_translate(region, mdiff);
}

View File

@@ -865,7 +865,7 @@ struct uiKeyNavLock {
/** Set when we're using keyboard-input. */
bool is_keynav;
/** Only used to check if we've moved the cursor. */
int event_xy[2];
blender::int2 event_xy;
};
using uiBlockHandleCreateFunc = uiBlock *(*)(bContext *C, uiPopupBlockHandle *handle, void *arg1);
@@ -876,7 +876,7 @@ struct uiPopupBlockCreate {
void *arg;
uiFreeArgFunc arg_free;
int event_xy[2];
blender::int2 event_xy;
/** Set when popup is initialized from a button. */
ARegion *butregion;

View File

@@ -11,6 +11,7 @@
#include "BLI_listbase.h"
#include "BLI_math_matrix.h"
#include "BLI_math_vector.h"
#include "BLI_math_vector.hh"
#include "BKE_context.hh"
#include "BKE_mask.h"
@@ -671,7 +672,7 @@ static int slide_point_modal(bContext *C, wmOperator *op, const wmEvent *event)
case MOUSEMOVE: {
ScrArea *area = CTX_wm_area(C);
ARegion *region = CTX_wm_region(C);
float delta[2];
blender::float2 delta;
ED_mask_mouse_pos(area, region, event->mval, co);
sub_v2_v2v2(delta, co, data->prev_mouse_coord);
@@ -708,7 +709,7 @@ static int slide_point_modal(bContext *C, wmOperator *op, const wmEvent *event)
/* flip last point */
if (data->point != &data->spline->points[0]) {
negate_v2(delta);
delta *= -1.0f;
}
}
}

View File

@@ -279,11 +279,11 @@ static void color_filter_task(const Depsgraph &depsgraph,
bool copy_alpha = colors[i][3] == average_colors[i][3];
if (factors[i] < 0.0f) {
float delta_color[4];
float4 delta_color;
/* Unsharp mask. */
copy_v4_v4(delta_color, ss.filter_cache->pre_smoothed_color[vert]);
sub_v4_v4(delta_color, average_colors[i]);
delta_color -= average_colors[i];
copy_v4_v4(new_colors[i], colors[i]);
madd_v4_v4fl(new_colors[i], delta_color, factors[i]);

View File

@@ -354,8 +354,7 @@ void ViewOpsData::init_navigation(bContext *C,
ED_view3d_win_to_vector(region, mval, this->init.mousevec);
{
int event_xy_offset[2];
add_v2_v2v2_int(event_xy_offset, event->xy, this->init.event_xy_offset);
int2 event_xy_offset = int2(event->xy) - this->init.event_xy_offset;
/* For rotation with trackball rotation. */
calctrackballvec(&region->winrct, event_xy_offset, this->init.trackvec);

View File

@@ -126,10 +126,10 @@ struct ViewOpsData {
/** The ones below are unrelated to the state of the 3D view. */
/** #wmEvent.xy. */
int event_xy[2];
blender::int2 event_xy;
/* Offset used when "use_cursor_init" is false to simulate pressing in the middle of the
* region. */
int event_xy_offset[2];
blender::int2 event_xy_offset;
/** #wmEvent.type that triggered the operator. */
int event_type;

View File

@@ -606,8 +606,8 @@ struct MouseInput {
*/
bool use_virtual_mval;
struct {
double prev[2];
double accum[2];
blender::double2 prev;
blender::double2 accum;
} virtual_mval;
};

View File

@@ -536,12 +536,12 @@ void transform_input_update(TransInfo *t, const float fac)
if (mi->use_virtual_mval) {
/* Update accumulator. */
double mval_delta[2];
double2 mval_delta;
sub_v2_v2v2_db(mval_delta, mi->virtual_mval.accum, mi->virtual_mval.prev);
mval_delta[0] *= fac;
mval_delta[1] *= fac;
copy_v2_v2_db(mi->virtual_mval.accum, mi->virtual_mval.prev);
add_v2_v2_db(mi->virtual_mval.accum, mval_delta);
mi->virtual_mval.accum += mval_delta;
}
if (ELEM(mi->apply, InputAngle, InputAngleSpring)) {

View File

@@ -231,7 +231,7 @@ static void bm_loop_calc_uv_angle_from_dir(BMLoop *l,
int *r_edge_index)
{
/* Calculate 3 directions, return the shortest angle. */
float dir_test[3][2];
blender::float2 dir_test[3];
const float *luv = BM_ELEM_CD_GET_FLOAT_P(l, cd_loop_uv_offset);
const float *luv_prev = BM_ELEM_CD_GET_FLOAT_P(l->prev, cd_loop_uv_offset);
const float *luv_next = BM_ELEM_CD_GET_FLOAT_P(l->next, cd_loop_uv_offset);
@@ -253,7 +253,7 @@ static void bm_loop_calc_uv_angle_from_dir(BMLoop *l,
dir_test[1][1] *= -1.0f;
if (BM_face_uv_calc_cross(l->f, cd_loop_uv_offset) > 0.0f) {
negate_v2(dir_test[1]);
dir_test[1] *= -1.0f;
}
const float angles[3] = {

View File

@@ -11,6 +11,7 @@
#include <cstring>
#include "BLI_math_vector.h"
#include "BLI_math_vector_types.hh"
#include "BLI_rect.h"
#include "BKE_colortools.hh"
@@ -50,7 +51,7 @@ static struct {
} g_viewport = {{0}};
struct GPUViewport {
int size[2];
blender::int2 size;
int flag;
/* Set the active view (for stereoscopic viewport rendering). */
@@ -190,14 +191,14 @@ static void gpu_viewport_textures_free(GPUViewport *viewport)
void GPU_viewport_bind(GPUViewport *viewport, int view, const rcti *rect)
{
int rect_size[2];
blender::int2 rect_size;
/* add one pixel because of scissor test */
rect_size[0] = BLI_rcti_size_x(rect) + 1;
rect_size[1] = BLI_rcti_size_y(rect) + 1;
DRW_gpu_context_enable();
if (!equals_v2v2_int(viewport->size, rect_size)) {
if (viewport->size != rect_size) {
copy_v2_v2_int(viewport->size, rect_size);
gpu_viewport_textures_free(viewport);
gpu_viewport_textures_create(viewport);

View File

@@ -15,6 +15,7 @@
#include "BLI_bitmap.h"
#include "BLI_math_matrix.h"
#include "BLI_math_vector.h"
#include "BLI_math_vector.hh"
#include "BLT_translation.hh"
@@ -45,7 +46,7 @@ static void generate_vert_coordinates(Mesh *mesh,
const float offset[3],
const int verts_num,
float (*r_cos)[3],
float r_size[3])
blender::float3 *r_size)
{
using namespace blender;
float min_co[3], max_co[3];
@@ -71,21 +72,21 @@ static void generate_vert_coordinates(Mesh *mesh,
/* Not we are not interested in signs here - they are even troublesome actually,
* due to security clamping! */
abs_v3_v3(r_size, ob_center->scale);
*r_size = blender::math::abs(blender::float3(ob_center->scale));
}
else {
/* Set size. */
sub_v3_v3v3(r_size, max_co, min_co);
sub_v3_v3v3(*r_size, max_co, min_co);
}
/* Error checks - we do not want one or more of our sizes to be null! */
if (is_zero_v3(r_size)) {
r_size[0] = r_size[1] = r_size[2] = 1.0f;
if (is_zero_v3(*r_size)) {
*r_size = float3(1.0f);
}
else {
CLAMP_MIN(r_size[0], FLT_EPSILON);
CLAMP_MIN(r_size[1], FLT_EPSILON);
CLAMP_MIN(r_size[2], FLT_EPSILON);
CLAMP_MIN((*r_size)[0], FLT_EPSILON);
CLAMP_MIN((*r_size)[1], FLT_EPSILON);
CLAMP_MIN((*r_size)[2], FLT_EPSILON);
}
}
@@ -236,11 +237,11 @@ static void normalEditModifier_do_radial(NormalEditModifierData *enmd,
float(*cos)[3] = static_cast<float(*)[3]>(
MEM_malloc_arrayN(size_t(vert_positions.size()), sizeof(*cos), __func__));
blender::Array<blender::float3> nos(corner_verts.size());
float size[3];
float3 size;
BLI_bitmap *done_verts = BLI_BITMAP_NEW(size_t(vert_positions.size()), __func__);
generate_vert_coordinates(mesh, ob, ob_target, enmd->offset, vert_positions.size(), cos, size);
generate_vert_coordinates(mesh, ob, ob_target, enmd->offset, vert_positions.size(), cos, &size);
/**
* size gives us our spheroid coefficients `(A, B, C)`.

View File

@@ -16,6 +16,7 @@
#include "BLI_map.hh"
#include "BLI_math_geom.h"
#include "BLI_math_vector.h"
#include "BLI_math_vector_types.hh"
#include "BLI_span.hh"
#include "BLI_vector.hh"
@@ -207,7 +208,7 @@ static void seq_retiming_line_segments_tangent_circle(const SeqRetimingKey *star
double r_center[2],
double *radius)
{
double s1_1[2], s1_2[2], s2_1[2], s2_2[2], p1_2[2];
blender::double2 s1_1, s1_2, s2_1, s2_2, p1_2;
/* Get 2 segments. */
seq_retiming_segment_as_line_segment(start_key - 1, s1_1, s1_2);
@@ -215,7 +216,7 @@ static void seq_retiming_line_segments_tangent_circle(const SeqRetimingKey *star
/* Backup first segment end point - needed to calculate arc radius. */
copy_v2_v2_db(p1_2, s1_2);
/* Convert segments to vectors. */
double v1[2], v2[2];
blender::double2 v1, v2;
sub_v2_v2v2_db(v1, s1_1, s1_2);
sub_v2_v2v2_db(v2, s2_1, s2_2);
/* Rotate segments by 90 degrees around seg. 1 end and seg. 2 start point. */
@@ -224,9 +225,9 @@ static void seq_retiming_line_segments_tangent_circle(const SeqRetimingKey *star
v1[0] *= -1;
v2[0] *= -1;
copy_v2_v2_db(s1_1, s1_2);
add_v2_v2_db(s1_2, v1);
s1_2 += v1;
copy_v2_v2_db(s2_2, s2_1);
add_v2_v2_db(s2_2, v2);
s2_2 += v2;
/* Get center and radius of arc segment between 2 linear segments. */
double lambda, mu;
isect_seg_seg_v2_lambda_mu_db(s1_1, s1_2, s2_1, s2_2, &lambda, &mu);