Blender  V3.3
stl_import_binary_reader.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
7 #include <cstdint>
8 #include <cstdio>
9 
10 #include "BKE_main.h"
11 #include "BKE_mesh.h"
12 
13 #include "BLI_array.hh"
14 #include "BLI_memory_utils.hh"
15 
16 #include "DNA_mesh_types.h"
17 
18 #include "stl_import.hh"
20 #include "stl_import_mesh.hh"
21 
22 namespace blender::io::stl {
23 
24 #pragma pack(push, 1)
26  float normal[3];
27  float v1[3], v2[3], v3[3];
29 };
30 #pragma pack(pop)
31 
32 Mesh *read_stl_binary(FILE *file, Main *bmain, char *mesh_name, bool use_custom_normals)
33 {
34  const int chunk_size = 1024;
35  uint32_t num_tris = 0;
36  fseek(file, BINARY_HEADER_SIZE, SEEK_SET);
37  if (fread(&num_tris, sizeof(uint32_t), 1, file) != 1) {
39  return nullptr;
40  }
41 
42  if (num_tris == 0) {
43  return BKE_mesh_add(bmain, mesh_name);
44  }
45 
47  STLMeshHelper stl_mesh(num_tris, use_custom_normals);
48  size_t num_read_tris;
49  while ((num_read_tris = fread(tris_buf.data(), sizeof(STLBinaryTriangle), chunk_size, file))) {
50  for (size_t i = 0; i < num_read_tris; i++) {
51  if (use_custom_normals) {
52  stl_mesh.add_triangle(tris_buf[i].v1, tris_buf[i].v2, tris_buf[i].v3, tris_buf[i].normal);
53  }
54  else {
55  stl_mesh.add_triangle(tris_buf[i].v1, tris_buf[i].v2, tris_buf[i].v3);
56  }
57  }
58  }
59 
60  return stl_mesh.to_mesh(bmain, mesh_name);
61 }
62 
63 } // namespace blender::io::stl
struct Mesh * BKE_mesh_add(struct Main *bmain, const char *name)
Definition: mesh.cc:963
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble v1
ATTR_WARN_UNUSED_RESULT const BMVert * v2
const T * data() const
Definition: BLI_array.hh:300
Mesh * to_mesh(Main *bmain, char *mesh_name)
bool add_triangle(const float3 &a, const float3 &b, const float3 &c)
FILE * file
IconTextureDrawCall normal
static const int chunk_size
Mesh * read_stl_binary(FILE *file, Main *bmain, char *mesh_name, bool use_custom_normals)
void stl_import_report_error(FILE *file)
Definition: stl_import.cc:32
unsigned short uint16_t
Definition: stdint.h:79
unsigned int uint32_t
Definition: stdint.h:80
Definition: BKE_main.h:121