various improvements to speed up surface meshing of BRL-CAD geometries
[engrid.git] / src / libengrid / triangularcadinterface.h
blobbbcb5debc3bb14642cb3919ccb93a485d9f5b8b5
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008-2013 enGits GmbH +
7 // + +
8 // + enGrid is free software: you can redistribute it and/or modify +
9 // + it under the terms of the GNU General Public License as published by +
10 // + the Free Software Foundation, either version 3 of the License, or +
11 // + (at your option) any later version. +
12 // + +
13 // + enGrid is distributed in the hope that it will be useful, +
14 // + but WITHOUT ANY WARRANTY; without even the implied warranty of +
15 // + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +
16 // + GNU General Public License for more details. +
17 // + +
18 // + You should have received a copy of the GNU General Public License +
19 // + along with enGrid. If not, see <http://www.gnu.org/licenses/>. +
20 // + +
21 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
22 //
23 #ifndef TRIANGULARCADINTERFACE_H
24 #define TRIANGULARCADINTERFACE_H
26 #include "cadinterface.h"
27 #include "triangle.h"
28 #include "facefinder.h"
29 #include "surfacealgorithm.h"
31 class TriangularCadInterface : public CadInterface, public SurfaceAlgorithm
34 private: // methods
36 template <class C>
37 void setBackgroundGrid_setupGrid(vtkUnstructuredGrid* grid, const C& cells); ///< copy the cells from grid to m_BGrid
39 void updateBackgroundGridInfo(); ///< Set up the background grid (triangles, bezier triangles, etc)
40 void computeSurfaceCurvature();
42 protected: // attributes
44 vtkUnstructuredGrid* m_BGrid; ///< the background grid defining the geometry
45 MeshPartition m_BPart;
46 QVector<vtkIdType> m_Cells;
47 QVector<vtkIdType> m_Nodes;
48 QVector<vec3_t> m_NodeNormals; ///< The surface normal at each node of m_BGrid
49 QVector<Triangle> m_Triangles; ///< All triangles of m_BGrid. One for each triangle cell of m_BGrid.
50 QVector<double> m_Radius; ///< Surface radius for mesh resolution.
51 QVector<QVector<int> > m_N2N;
52 double m_CritDistance;
53 FaceFinder m_FaceFinder;
56 protected: // methods
58 void searchNewTriangle(vec3_t xp, vtkIdType &id_tri, vec3_t &x_proj, vec3_t &r_proj, bool neigh_mode, bool &on_triangle);
59 virtual vec3_t correctCurvature(vtkIdType proj_triangle, vec3_t x);
62 public:
64 TriangularCadInterface();
66 virtual vec3_t snap(vec3_t x, bool correct_curvature = false);
67 virtual vec3_t correctCurvature(vec3_t x);
69 template <class C> void setBackgroundGrid(vtkUnstructuredGrid* grid, const C& cells); ///< Set the background grid to use + set it up
73 template <class C>
74 void TriangularCadInterface::setBackgroundGrid(vtkUnstructuredGrid* grid, const C& cells)
76 QVector<vtkIdType> nodes;
77 getNodesFromCells(cells, nodes, grid);
78 int num_new_cells = cells.size();
79 foreach (vtkIdType id_cell, cells) {
80 vtkIdType type_cell = grid->GetCellType(id_cell);
81 if (type_cell == VTK_QUAD) {
82 ++num_new_cells;
85 allocateGrid(m_BGrid, num_new_cells, nodes.size());
87 QVector<vtkIdType> _nodes(grid->GetNumberOfPoints());
88 vtkIdType id_new_node = 0;
89 foreach (vtkIdType id_node, nodes) {
90 vec3_t x;
91 grid->GetPoints()->GetPoint(id_node, x.data());
92 m_BGrid->GetPoints()->SetPoint(id_new_node, x.data());
93 copyNodeData(grid,id_node,m_BGrid,id_new_node);
94 _nodes[id_node] = id_new_node;
95 ++id_new_node;
97 foreach (vtkIdType id_cell, cells) {
98 vtkIdType N_pts, *pts;
99 vtkIdType type_cell = grid->GetCellType(id_cell);
100 grid->GetCellPoints(id_cell, N_pts, pts);
101 QVector<vtkIdType> new_pts(3);
102 if (type_cell == VTK_TRIANGLE) {
103 new_pts[0] = _nodes[pts[0]];
104 new_pts[1] = _nodes[pts[1]];
105 new_pts[2] = _nodes[pts[2]];
106 vtkIdType id_new_cell = m_BGrid->InsertNextCell(VTK_TRIANGLE, 3, new_pts.data());
107 copyCellData(grid, id_cell, m_BGrid, id_new_cell);
108 } else if (type_cell == VTK_QUAD) {
109 new_pts[0] = _nodes[pts[0]];
110 new_pts[1] = _nodes[pts[1]];
111 new_pts[2] = _nodes[pts[2]];
112 vtkIdType id_new_cell1 = m_BGrid->InsertNextCell(VTK_TRIANGLE, 3, new_pts.data());
113 copyCellData(grid, id_cell, m_BGrid, id_new_cell1);
114 new_pts[0] = _nodes[pts[2]];
115 new_pts[1] = _nodes[pts[3]];
116 new_pts[2] = _nodes[pts[0]];
117 vtkIdType id_new_cell2 = m_BGrid->InsertNextCell(VTK_TRIANGLE, 3, new_pts.data());
118 copyCellData(grid, id_cell, m_BGrid, id_new_cell2);
119 } else {
120 EG_BUG;
123 updateBackgroundGridInfo();
124 m_FaceFinder.setMaxNumFaces(10);
125 m_FaceFinder.setGrid(m_BGrid);
128 #endif // TRIANGULARCADINTERFACE_H