Blender  V3.3
obj_export_mesh.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #pragma once
8 
9 #include <optional>
10 
11 #include "BLI_math_vec_types.hh"
12 #include "BLI_utility_mixins.hh"
13 #include "BLI_vector.hh"
14 
15 #include "bmesh.h"
16 #include "bmesh_tools.h"
17 
18 #include "DNA_material_types.h"
19 #include "DNA_mesh_types.h"
20 #include "DNA_meshdata_types.h"
21 #include "DNA_object_types.h"
22 
23 #include "IO_wavefront_obj.h"
24 
25 namespace blender::io::obj {
27 const int NOT_FOUND = -1;
29 const int NEGATIVE_INIT = -10;
30 
35  void operator()(BMesh *bmesh)
36  {
37  if (bmesh) {
38  BM_mesh_free(bmesh);
39  }
40  }
41 };
42 
43 using unique_bmesh_ptr = std::unique_ptr<BMesh, CustomBMeshDeleter>;
44 
46  private:
51  Object export_object_eval_;
52  Mesh *export_mesh_eval_;
56  bool mesh_eval_needs_free_ = false;
61  float world_and_axes_transform_[4][4];
62  float world_and_axes_normal_transform_[3][3];
63  bool mirrored_transform_;
64 
68  int tot_uv_vertices_ = 0;
72  Vector<Vector<int>> uv_indices_;
73  /*
74  * UV vertices.
75  */
76  Vector<float2> uv_coords_;
80  Vector<int> loop_to_normal_index_;
81  /*
82  * Normal coords.
83  */
84  Vector<float3> normal_coords_;
85  /*
86  * Total number of normal indices (maximum entry, plus 1, in
87  * the loop_to_norm_index_ vector).
88  */
89  int tot_normal_indices_ = 0;
93  int tot_smooth_groups_ = NEGATIVE_INIT;
97  int *poly_smooth_groups_ = nullptr;
101  Vector<int> poly_order_;
102 
103  public:
108  OBJMesh(Depsgraph *depsgraph, const OBJExportParams &export_params, Object *mesh_object);
109  ~OBJMesh();
110 
111  /* Clear various arrays to release potentially large memory allocations. */
112  void clear();
113 
114  int tot_vertices() const;
115  int tot_polygons() const;
116  int tot_uv_vertices() const;
117  int tot_normal_indices() const;
118  int tot_edges() const;
119  int tot_deform_groups() const;
121  {
122  return mirrored_transform_;
123  }
124 
128  int16_t tot_materials() const;
132  const Material *get_object_material(int16_t mat_nr) const;
137  int16_t ith_poly_matnr(int poly_index) const;
138 
139  void ensure_mesh_normals() const;
140  void ensure_mesh_edges() const;
141 
146  void calc_smooth_groups(bool use_bitflags);
150  int ith_smooth_group(int poly_index) const;
151  bool is_ith_poly_smooth(int poly_index) const;
152 
156  const char *get_object_name() const;
160  const char *get_object_mesh_name() const;
164  const char *get_object_material_name(int16_t mat_nr) const;
165 
169  float3 calc_vertex_coords(int vert_index, float scaling_factor) const;
173  Vector<int> calc_poly_vertex_indices(int poly_index) const;
179  /* Get UV coordinates computed by store_uv_coords_and_indices. */
181  {
182  return uv_coords_;
183  }
184  Span<int> calc_poly_uv_indices(int poly_index) const;
190  float3 calc_poly_normal(int poly_index) const;
196  /* Get normals calculate by store_normal_coords_and_indices. */
198  {
199  return normal_coords_;
200  }
206  Vector<int> calc_poly_normal_indices(int poly_index) const;
216  int16_t get_poly_deform_group_index(int poly_index, MutableSpan<float> group_weights) const;
221  const char *get_poly_deform_group_name(int16_t def_group_index) const;
222 
226  std::optional<std::array<int, 2>> calc_loose_edge_vert_indices(int edge_index) const;
227 
232  void calc_poly_order();
233 
239  int remap_poly_index(int i) const
240  {
241  return i < 0 || i >= poly_order_.size() ? i : poly_order_[i];
242  }
243 
244  Mesh *get_mesh() const
245  {
246  return export_mesh_eval_;
247  }
248 
249  private:
253  void free_mesh_if_needed();
260  std::pair<Mesh *, bool> triangulate_mesh_eval();
264  void set_world_axes_transform(eIOAxis forward, eIOAxis up);
265 };
266 } // namespace blender::io::obj
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
Object is a sort of wrapper for general info.
eIOAxis
Definition: IO_orientation.h:7
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
Definition: bmesh_mesh.cc:258
int64_t size() const
Definition: BLI_vector.hh:694
const char * get_object_mesh_name() const
const char * get_object_name() const
const Vector< float2 > & get_uv_coords() const
int ith_smooth_group(int poly_index) const
void calc_smooth_groups(bool use_bitflags)
Span< int > calc_poly_uv_indices(int poly_index) const
const char * get_poly_deform_group_name(int16_t def_group_index) const
float3 calc_vertex_coords(int vert_index, float scaling_factor) const
const Material * get_object_material(int16_t mat_nr) const
int16_t get_poly_deform_group_index(int poly_index, MutableSpan< float > group_weights) const
float3 calc_poly_normal(int poly_index) const
int16_t ith_poly_matnr(int poly_index) const
const Vector< float3 > & get_normal_coords() const
int remap_poly_index(int i) const
const char * get_object_material_name(int16_t mat_nr) const
Vector< int > calc_poly_vertex_indices(int poly_index) const
std::optional< std::array< int, 2 > > calc_loose_edge_vert_indices(int edge_index) const
Vector< int > calc_poly_normal_indices(int poly_index) const
bool is_ith_poly_smooth(int poly_index) const
OBJMesh(Depsgraph *depsgraph, const OBJExportParams &export_params, Object *mesh_object)
const Depsgraph * depsgraph
const int NEGATIVE_INIT
std::unique_ptr< BMesh, CustomBMeshDeleter > unique_bmesh_ptr
signed short int16_t
Definition: stdint.h:76