Blender  V3.3
stl_import_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 <cstdint>
10 
11 #include "BLI_math_vec_types.hh"
12 #include "BLI_set.hh"
13 #include "BLI_vector.hh"
14 #include "BLI_vector_set.hh"
15 
16 #include "DNA_mesh_types.h"
17 
18 namespace blender::io::stl {
19 class Triangle {
20  public:
21  int v1, v2, v3;
22  /* Based on an old version of Python's frozen-set hash
23  * https://web.archive.org/web/20220520211017/https://stackoverflow.com/questions/20832279/python-frozenset-hashing-algorithm-implementation
24  */
25  uint64_t hash() const
26  {
27  uint64_t res = 1927868237UL;
28  res *= 4;
29  res ^= (v1 ^ (v1 << 16) ^ 89869747UL) * 3644798167UL;
30  res ^= (v2 ^ (v2 << 16) ^ 89869747UL) * 3644798167UL;
31  res ^= (v3 ^ (v3 << 16) ^ 89869747UL) * 3644798167UL;
32  return res * 69069U + 907133923UL;
33  }
34  friend bool operator==(const Triangle &a, const Triangle &b)
35  {
36  bool i = (a.v1 == b.v1) && (a.v2 == b.v2) && (a.v3 == b.v3);
37  bool j = (a.v1 == b.v1) && (a.v3 == b.v2) && (a.v2 == b.v3);
38  bool k = (a.v2 == b.v1) && (a.v1 == b.v2) && (a.v3 == b.v3);
39 
40  bool l = (a.v2 == b.v1) && (a.v3 == b.v2) && (a.v1 == b.v3);
41  bool m = (a.v3 == b.v1) && (a.v1 == b.v2) && (a.v2 == b.v3);
42  bool n = (a.v3 == b.v1) && (a.v2 == b.v2) && (a.v1 == b.v3);
43 
44  return i || j || k || l || m || n;
45  }
46 };
47 
49  private:
50  VectorSet<float3> verts_;
51  VectorSet<Triangle> tris_;
52  Vector<float3> loop_normals_;
53  int degenerate_tris_num_;
54  int duplicate_tris_num_;
55  const bool use_custom_normals_;
56 
57  public:
58  STLMeshHelper(int tris_num, bool use_custom_normals);
59 
60  /* Creates a new triangle from specified vertex locations,
61  * duplicate vertices and triangles are merged.
62  */
63  bool add_triangle(const float3 &a, const float3 &b, const float3 &c);
64  void add_triangle(const float3 &a,
65  const float3 &b,
66  const float3 &c,
67  const float3 &custom_normal);
68  Mesh *to_mesh(Main *bmain, char *mesh_name);
69 };
70 
71 } // namespace blender::io::stl
ATTR_WARN_UNUSED_RESULT const BMLoop * l
Mesh * to_mesh(Main *bmain, char *mesh_name)
bool add_triangle(const float3 &a, const float3 &b, const float3 &c)
STLMeshHelper(int tris_num, bool use_custom_normals)
friend bool operator==(const Triangle &a, const Triangle &b)
static unsigned c
Definition: RandGen.cpp:83
static unsigned a[3]
Definition: RandGen.cpp:78
static const pxr::TfToken b("b", pxr::TfToken::Immortal)
unsigned __int64 uint64_t
Definition: stdint.h:90
Definition: BKE_main.h:121