Cleanup: Use C++ math types in OBJ export

This commit is contained in:
Hans Goudey
2024-01-25 13:10:10 -05:00
parent 9f0c6e0685
commit a08e636bbc
2 changed files with 29 additions and 34 deletions

View File

@@ -14,12 +14,11 @@
#include "BKE_mesh.hh"
#include "BKE_mesh_mapping.hh"
#include "BKE_object.hh"
#include "BLI_math_matrix.h"
#include "BLI_math_rotation.h"
#include "BLI_math_vector.h"
#include "BLI_listbase.h"
#include "BLI_map.hh"
#include "BLI_math_matrix.hh"
#include "BLI_math_rotation.h"
#include "BLI_sort.hh"
#include "DEG_depsgraph_query.hh"
@@ -148,21 +147,21 @@ void OBJMesh::set_world_axes_transform(const Object &obj_eval,
const eIOAxis forward,
const eIOAxis up)
{
float axes_transform[3][3];
unit_m3(axes_transform);
float3x3 axes_transform;
/* +Y-forward and +Z-up are the default Blender axis settings. */
mat3_from_axis_conversion(forward, up, IO_AXIS_Y, IO_AXIS_Z, axes_transform);
mul_m4_m3m4(world_and_axes_transform_, axes_transform, obj_eval.object_to_world);
/* mul_m4_m3m4 does not transform last row of obmat, i.e. location data. */
mul_v3_m3v3(world_and_axes_transform_[3], axes_transform, obj_eval.object_to_world[3]);
world_and_axes_transform_[3][3] = obj_eval.object_to_world[3][3];
mat3_from_axis_conversion(forward, up, IO_AXIS_Y, IO_AXIS_Z, axes_transform.ptr());
const float4x4 object_to_world(obj_eval.object_to_world);
const float3x3 transform = axes_transform * float3x3(object_to_world);
world_and_axes_transform_ = float4x4(transform);
world_and_axes_transform_.location() = axes_transform * object_to_world.location();
world_and_axes_transform_[3][3] = object_to_world[3][3];
/* Normals need inverse transpose of the regular matrix to handle non-uniform scale. */
float normal_matrix[3][3];
copy_m3_m4(normal_matrix, world_and_axes_transform_);
invert_m3_m3(world_and_axes_normal_transform_, normal_matrix);
transpose_m3(world_and_axes_normal_transform_);
mirrored_transform_ = is_negative_m3(world_and_axes_normal_transform_);
world_and_axes_normal_transform_ = math::transpose(math::invert(transform));
mirrored_transform_ = math::is_negative(world_and_axes_normal_transform_);
}
int OBJMesh::tot_vertices() const
@@ -260,10 +259,9 @@ const char *OBJMesh::get_object_mesh_name() const
float3 OBJMesh::calc_vertex_coords(const int vert_index, const float global_scale) const
{
float3 r_coords = mesh_positions_[vert_index];
mul_m4_v3(world_and_axes_transform_, r_coords);
mul_v3_fl(r_coords, global_scale);
return r_coords;
const float3 coords = math::transform_point(world_and_axes_transform_,
mesh_positions_[vert_index]);
return coords * global_scale;
}
Span<int> OBJMesh::calc_poly_vertex_indices(const int face_index) const
@@ -317,11 +315,9 @@ Span<int> OBJMesh::calc_poly_uv_indices(const int face_index) const
float3 OBJMesh::calc_poly_normal(const int face_index) const
{
float3 r_poly_normal = bke::mesh::face_normal_calc(
mesh_positions_, mesh_corner_verts_.slice(mesh_faces_[face_index]));
mul_m3_v3(world_and_axes_normal_transform_, r_poly_normal);
normalize_v3(r_poly_normal);
return r_poly_normal;
const Span<int> face_verts = mesh_corner_verts_.slice(mesh_faces_[face_index]);
const float3 normal = bke::mesh::face_normal_calc(mesh_positions_, face_verts);
return math::normalize(world_and_axes_normal_transform_ * normal);
}
/** Round \a f to \a round_digits decimal digits. */
@@ -368,17 +364,15 @@ void OBJMesh::store_normal_coords_and_indices()
bool need_per_loop_normals = !corner_normals.is_empty() || !(sharp_faces_[face_index]);
if (need_per_loop_normals) {
for (const int corner : face) {
float3 loop_normal;
BLI_assert(corner < export_mesh_->corners_num);
copy_v3_v3(loop_normal, corner_normals[corner]);
mul_m3_v3(world_and_axes_normal_transform_, loop_normal);
normalize_v3(loop_normal);
float3 rounded_loop_normal = round_float3_to_n_digits(loop_normal, round_digits);
int loop_norm_index = normal_to_index.lookup_default(rounded_loop_normal, -1);
const float3 normal = math::normalize(world_and_axes_normal_transform_ *
corner_normals[corner]);
const float3 rounded = round_float3_to_n_digits(normal, round_digits);
int loop_norm_index = normal_to_index.lookup_default(rounded, -1);
if (loop_norm_index == -1) {
loop_norm_index = cur_normal_index++;
normal_to_index.add(rounded_loop_normal, loop_norm_index);
normal_coords_.append(rounded_loop_normal);
normal_to_index.add(rounded, loop_norm_index);
normal_coords_.append(rounded);
}
loop_to_normal_index_[corner] = loop_norm_index;
}

View File

@@ -10,6 +10,7 @@
#include <optional>
#include "BLI_math_matrix_types.hh"
#include "BLI_math_vector_types.hh"
#include "BLI_offset_indices.hh"
#include "BLI_utility_mixins.hh"
@@ -44,8 +45,8 @@ class OBJMesh : NonCopyable {
* Final transform of an object obtained from export settings (up_axis, forward_axis) and the
* object's world transform matrix.
*/
float world_and_axes_transform_[4][4];
float world_and_axes_normal_transform_[3][3];
float4x4 world_and_axes_transform_;
float3x3 world_and_axes_normal_transform_;
bool mirrored_transform_;
/**