Debian Codegraph @

avogadro/qtgui/meshgenerator.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_QTGUI_MESHGENERATOR_H
7 #define AVOGADRO_QTGUI_MESHGENERATOR_H
8
9 #include "avogadroqtguiexport.h"
10
11 #include <avogadro/core/array.h>
12 #include <avogadro/core/vector.h>
13
14 #include <QtCore/QThread>
15
16 namespace Avogadro {
17
18 namespace Core {
19 class Cube;
20 class Mesh;
21 } // namespace Core
22
23 namespace QtGui {
24
25 /**
26 * @class MeshGenerator meshgenerator.h <avogadro/qtgui/meshgenerator.h>
27 * @brief Class that can generate Mesh objects from Cube objects.
28 * @author Marcus D. Hanwell
29 * @author Perminder Singh
30 *
31 * This class implements a method of generating an isosurface Mesh from
32 * volumetric data using the marching cubes algorithm. In the case of the
33 * MeshGenerator class it expects a Cube as an input and an isosurface value.
34 * The tables and the basic code is taken from the public domain code written
35 * by Cory Bloyd (marchingsource.cpp) and available at,
36 * http://local.wasp.uwa.edu.au/~pbourke/geometry/polygonise/
37 *
38 * You must first initialize the class and then call run() to actually
39 * polygonize the isosurface. Connect to the classes finished() signal to
40 * do something once the polygonization is complete.
41 */
42
43 class AVOGADROQTGUI_EXPORT MeshGenerator : public QThread
44 {
45 Q_OBJECT
46 public:
47 /**
48 * Constructor.
49 */
50 explicit MeshGenerator(QObject* parent = nullptr);
51
52 /**
53 * Constructor. Can be used to initialize the MeshGenerator.
54 * @param cube The source Cube with the volumetric data.
55 * @param mesh The Mesh that will hold the isosurface.
56 * @param iso The iso value of the surface.
57 * @param passes Number of smoothing passes to perform.
58 */
59 MeshGenerator(const Core::Cube* cube, Core::Mesh* mesh, float iso,
60 int passes = 6, bool reverse = false,
61 QObject* parent = nullptr);
62
63 /**
64 * Destructor.
65 */
66 ~MeshGenerator() override;
67
68 /**
69 * Initialization function, set up the MeshGenerator ready to find an
70 * isosurface of the supplied Cube.
71 * @param cube The source Cube with the volumetric data.
72 * @param mesh The Mesh that will hold the isosurface.
73 * @param iso The iso value of the surface.
74 * @param passes Number of smoothing passes to perform.
75 */
76 bool initialize(const Core::Cube* cube, Core::Mesh* mesh, float iso,
77 int passes = 6, bool reverse = false);
78
79 /**
80 * Use this function to begin Mesh generation. Uses an asynchronous thread,
81 * and so avoids locking the user interface while the isosurface is found.
82 */
83 void run() override;
84
85 /**
86 * It holds the range and starting offsets of isosurface-intersected
87 * edges along the x, y, and z axes for each grid cell.
88 */
89 struct gridEdge
90 {
91 gridEdge() : xl(0), xr(0), xstart(0), ystart(0), zstart(0) {}
92
93 // trim values
94 // set on pass 1
95 int xl;
96 int xr;
97
98 // modified on pass 2
99 // set on pass 3
100 int xstart;
101 int ystart;
102 int zstart;
103 };
104
105 /**
106 * Handles duplicate vertices (Not implemented). Placeholder for future
107 * functionality.
108 */
109 unsigned long duplicate(const Vector3i& c, const Vector3f& pos);
110
111 /**
112 * @name Flying Edges
113 * Methods to implement the "flying edges" method for isosurface mesh
114 * generation. Flying edges: A high-performance scalable isocontouring
115 * algorithm Schroeder; Maynard; Geveci; 2015 IEEE 5th Symposium on Large Data
116 * Analysis and Visualization (LDAV)
117 * [10.1109/LDAV.2015.7348069](https://doi.org/10.1109/LDAV.2015.7348069)
118 * Alternate (non-VTK) implementation at
119 * https://github.com/sandialabs/miniIsosurface/blob/master/flyingEdges/
120 * @{
121 */
122
123 /**
124 * Pass 1 for flying edges. Pass1 detects and records
125 * where the isosurface intersects each row of grid edges
126 * along the x-axis.
127 */
128 void FlyingEdgesAlgorithmPass1();
129
130 /**
131 * Pass2 assigns case identifiers to each grid cell based on
132 * intersected edges and tallies the number of triangles needed
133 * for mesh construction.
134 */
135 void FlyingEdgesAlgorithmPass2();
136
137 /**
138 * Pass3 computes cumulative offsets for triangles
139 * and vertices and allocates memory for the mesh structures.
140 */
141 void FlyingEdgesAlgorithmPass3();
142
143 /**
144 * Calculates normals, triangles and vertices.
145 */
146 void FlyingEdgesAlgorithmPass4();
147
148 /**@}*/
149
150 /**
151 * @return The Cube being used by the class.
152 */
153 const Core::Cube* cube() const { return m_cube; }
154
155 /**
156 * Determines the x-range (xl to xr) where isosurface intersections
157 * occur, optimizing calculations within this range.
158 */
159 inline void calcTrimValues(int& xl, int& xr, int const& j,
160 int const& k) const;
161
162 /**
163 * Indicates which edges intersects the isosurface.
164 */
165 inline unsigned char calcCubeCase(unsigned char const& ec0,
166 unsigned char const& ec1,
167 unsigned char const& ec2,
168 unsigned char const& ec3) const;
169
170 /**
171 * @return The Mesh being generated by the class.
172 */
173 Core::Mesh* mesh() const { return m_mesh; }
174
175 /**
176 * Clears the contents of the MeshGenerator.
177 */
178 void clear();
179
180 /**
181 * @return The minimum value of the progress value.
182 */
183 int progressMinimum() { return m_progmin; }
184
185 /**
186 * @return The maximum value of the progress value.
187 */
188 int progressMaximum() { return m_progmax; }
189
190 signals:
191
192 /**
193 * The current value of the calculation's progress.
194 */
195 void progressValueChanged(int);
196
197 protected:
198 /**
199 * isCutEdge checks whether the grid edge at position (i, j, k) is
200 * intersected by the isosurface based on edge case conditions.
201 * @return Boolean if it's intersected or not.
202 */
203 bool isCutEdge(int const& i, int const& j, int const& k) const;
204
205 /**
206 * It computes the 3D intersection point on a cube edge via interpolation.
207 */
208 inline std::array<float, 3> interpolateOnCube(
209 std::array<std::array<float, 3>, 8> const& pts,
210 std::array<float, 8> const& isovals, unsigned char const& edge) const;
211
212 /**
213 * It linearly interpolates between two 3D points, a and b, using
214 * the given weight to determine the intermediate position.
215 */
216 inline std::array<float, 3> interpolate(std::array<float, 3> const& a,
217 std::array<float, 3> const& b,
218 float const& weight) const;
219
220 /**
221 * calcCaseEdge determines an edge case code (0–3) based on two boolean edge
222 * comparisons.
223 */
224 inline unsigned char calcCaseEdge(bool const& prevEdge,
225 bool const& currEdge) const;
226
227 float m_iso; /** The value of the isosurface. */
228 int m_passes; /** Number of smoothing passes to perform. */
229 bool m_reverseWinding; /** Whether the winding and normals are reversed. */
230 const Core::Cube* m_cube; /** The cube that we are generating a Mesh from. */
231 Core::Mesh* m_mesh; /** The mesh that is being generated. */
232 Vector3f m_stepSize; /** The step size vector for cube. */
233 Vector3f m_min; /** The minimum point in the cube. */
234 Vector3i m_dim; /** The dimensions of the cube. */
235
236 Core::Array<Vector3f> m_normals, m_vertices;
237 std::vector<gridEdge> gridEdges; // size (m_dim.y() * m_dim.z())
238 std::vector<unsigned char>
239 cubeCases; // size ((m_dim.x() - 1) * (m_dim.y() - 1) * (m_dim.z() - 1))
240 std::vector<int> triCounter; // size ((m_dim.y() - 1) * (m_dim.z() - 1))
241 std::vector<unsigned char>
242 edgeCases; // size ((m_dim.x() - 1) * (m_dim.y()) * (m_dim.z()))
243 Core::Array<Vector3f> m_triangles; // triangles of a mesh
244 int m_progmin;
245 int m_progmax;
246
247 /**
248 * These are the lookup tables for flying edges.
249 * Reference :
250 * https://github.com/sandialabs/miniIsosurface/blob/master/flyingEdges/util/MarchingCubesTables.h
251 */
252 static const unsigned char m_numTris[256];
253 static const bool m_isCut[256][12];
254 static const signed char m_caseTriangles[256][16];
255 static const unsigned char m_edgeVertices[12][2];
256 };
257
258 } // End namespace QtGui
259 } // End namespace Avogadro
260
261 #endif // AVOGADRO_QTGUI_MESHGENERATOR_H