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

BLI_mesh_intersect.hh « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8ce26b6e53318b1d7d6355741d6443e83085402c (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef __BLI_MESH_INTERSECT_HH__
#define __BLI_MESH_INTERSECT_HH__

/** \file
 * \ingroup bli
 *
 *  Work in progress on mesh intersection library function.
 */

#include <iostream>

#include "BLI_array.hh"
#include "BLI_math_mpq.hh"
#include "BLI_mpq3.hh"
#include "BLI_vector.hh"

namespace blender {

namespace meshintersect {

/* The indices are for vertices in some external space of coordinates.
 * The "orig" component is used to track how a triangle originally came
 * from some other space of triangle indices. Which we usually need,
 * and it packs nicely into this structure, so keeping it here will save
 * memory.
 */
class IndexedTriangle {
 public:
  IndexedTriangle() = default;
  IndexedTriangle(int v0, int v1, int v2, int orig) : m_v{v0, v1, v2}, m_orig{orig}
  {
  }
  IndexedTriangle(const IndexedTriangle &other)
  {
    m_v[0] = other.m_v[0];
    m_v[1] = other.m_v[1];
    m_v[2] = other.m_v[2];
    m_orig = other.m_orig;
  }
  IndexedTriangle &operator=(const IndexedTriangle &other)
  {
    if (this != &other) {
      m_v[0] = other.m_v[0];
      m_v[1] = other.m_v[1];
      m_v[2] = other.m_v[2];
      m_orig = other.m_orig;
    }
    return *this;
  }

  int v0() const
  {
    return m_v[0];
  }
  int v1() const
  {
    return m_v[1];
  }
  int v2() const
  {
    return m_v[2];
  }
  int orig() const
  {
    return m_orig;
  }
  int operator[](int i) const
  {
    return m_v[i];
  }
  int &operator[](int i)
  {
    return m_v[i];
  }
  bool operator==(const IndexedTriangle &other)
  {
    /* Let equality happen with any cyclic ordering difference, but not orientation difference. */
    return (((m_v[0] == other.m_v[0] && m_v[1] == other.m_v[1] && m_v[2] == other.m_v[2]) ||
             (m_v[0] == other.m_v[1] && m_v[1] == other.m_v[2] && m_v[2] == other.m_v[0]) ||
             (m_v[0] == other.m_v[2] && m_v[1] == other.m_v[0] && m_v[2] == other.m_v[1])) &&
            m_orig == other.m_orig);
  }

 private:
  int m_v[3]{-1, -1, -1};
  int m_orig{-1};
};

struct TriMesh {
  Array<blender::mpq3> vert;
  Array<IndexedTriangle> tri;

  TriMesh() = default;
};

/* The output will have dup vertices merged and degenerate triangles ignored.
 * If the input has overlapping coplanar triangles, then there will be
 * as many duplicates as there are overlaps in each overlapping triangular region.
 * The orig field of each IndexedTriangle will give the orig index in the input TriMesh
 * that the output triangle was a part of (input can have -1 for that field and then
 * the index in tri[] will be used as the original index).
 */
TriMesh trimesh_self_intersect(const TriMesh &tm_in);

/* For debugging output. */
std::ostream &operator<<(std::ostream &os, const IndexedTriangle &tri);

std::ostream &operator<<(std::ostream &os, const TriMesh &tm);

void write_html_trimesh(const Array<mpq3> &vert,
                        const Array<IndexedTriangle> &tri,
                        const std::string &fname,
                        const std::string &label);

void write_obj_trimesh(const Array<mpq3> &vert,
                       const Array<IndexedTriangle> &tri,
                       const std::string &objname);

} /* namespace meshintersect */

} /* namespace blender */

#endif /* __BLI_MESH_INTERSECT_HH__ */