Welcome to mirror list, hosted at ThFree Co, Russian Federation.

stl_import_mesh.hh « importer « stl « io « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a01e5be09a47eb24ae702b6aa25e9b25f1666b15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* SPDX-License-Identifier: GPL-2.0-or-later */

/** \file
 * \ingroup stl
 */

#pragma once

#include <cstdint>

#include "BLI_math_vec_types.hh"
#include "BLI_set.hh"
#include "BLI_vector.hh"
#include "BLI_vector_set.hh"

#include "DNA_mesh_types.h"

namespace blender::io::stl {
class Triangle {
 public:
  int v1, v2, v3;
  /* Based on an old version of Python's frozen-set hash
   * https://web.archive.org/web/20220520211017/https://stackoverflow.com/questions/20832279/python-frozenset-hashing-algorithm-implementation
   */
  uint64_t hash() const
  {
    uint64_t res = 1927868237UL;
    res *= 4;
    res ^= (v1 ^ (v1 << 16) ^ 89869747UL) * 3644798167UL;
    res ^= (v2 ^ (v2 << 16) ^ 89869747UL) * 3644798167UL;
    res ^= (v3 ^ (v3 << 16) ^ 89869747UL) * 3644798167UL;
    return res * 69069U + 907133923UL;
  }
  friend bool operator==(const Triangle &a, const Triangle &b)
  {
    bool i = (a.v1 == b.v1) && (a.v2 == b.v2) && (a.v3 == b.v3);
    bool j = (a.v1 == b.v1) && (a.v3 == b.v2) && (a.v2 == b.v3);
    bool k = (a.v2 == b.v1) && (a.v1 == b.v2) && (a.v3 == b.v3);

    bool l = (a.v2 == b.v1) && (a.v3 == b.v2) && (a.v1 == b.v3);
    bool m = (a.v3 == b.v1) && (a.v1 == b.v2) && (a.v2 == b.v3);
    bool n = (a.v3 == b.v1) && (a.v2 == b.v2) && (a.v1 == b.v3);

    return i || j || k || l || m || n;
  }
};

class STLMeshHelper {
 private:
  VectorSet<float3> m_verts;
  VectorSet<Triangle> m_tris;
  Vector<float3> m_loop_normals;
  int m_num_degenerate_tris;
  int m_num_duplicate_tris;
  const bool m_use_custom_normals;

 public:
  STLMeshHelper(int num_tris, bool use_custom_normals);

  /* Creates a new triangle from specified vertex locations,
   * duplicate vertices and triangles are merged.
   */
  bool add_triangle(const float3 &a, const float3 &b, const float3 &c);
  void add_triangle(const float3 &a,
                    const float3 &b,
                    const float3 &c,
                    const float3 &custom_normal);
  Mesh *to_mesh(Main *bmain, char *mesh_name);
};

}  // namespace blender::io::stl