replaced abort() with EG_BUG
[engrid.git] / src / octree.h
blobef7b3a718f0debf441a68ed3eb41abee5702dc29
1 //
2 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 // + +
4 // + This file is part of enGrid. +
5 // + +
6 // + Copyright 2008,2009 Oliver Gloth +
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 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
23 #ifndef OCTREE_H
24 #define OCTREE_H
26 class Octree;
28 #include "egvtkobject.h"
30 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32 class OctreeNode
35 friend class Octree;
37 vec3_t m_Position;
39 public:
41 vec3_t& getPosition() { return m_Position; }
42 void setPosition(const vec3_t& x) { m_Position = x; }
46 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
48 class OctreeCell
51 friend class Octree;
53 int m_Node[8];
54 int m_Child[8];
55 int m_Neighbour[6];
56 int m_Parent;
57 int m_Level;
59 public:
61 OctreeCell();
63 int getNode (int i) { return m_Node[i]; }
64 int getNeighbour(int i) { return m_Neighbour[i]; }
65 bool hasChildren () { return m_Child[0] != -1; }
67 int getEdgeNode(Octree* octree, int n1, int n2, int f);
68 void getFaceNodes(int i, Octree* octree, QVector<int>& face_nodes, bool reverse = false);
69 void getFaceNodes(int i, Octree* octree, QVector<QVector<int> >& face_nodes, bool reverse = false);
74 // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
76 class Octree : public EgVtkObject
79 friend class OctreeCell;
81 private: // attributes
83 vec3_t m_Origin; ///< origin of internal coordinate system
84 mat3_t m_Base; ///< base vectors of internal coordinate system
85 mat3_t m_InvBase; ///< inverted base of internal coordiante system
86 vec3_t m_Corner1; ///< first corner of extend box of the whole domain (in internal coordinates)
87 vec3_t m_Corner2; ///< second corner of extend box of the whole domain (in internal coordinates)
88 double m_Dx; ///< extend in x direction
89 double m_Dy; ///< extend in y direction
90 double m_Dz; ///< extend in z direction
92 bool m_SmoothTransition;
94 QVector<OctreeNode> m_Nodes;
95 QVector<OctreeCell> m_Cells;
96 QVector<bool> m_ToRefine;
97 QVector<int> m_SameNodes;
98 int m_MaxCells;
100 private: // methods
102 void mergeNodes_identifyDuplicates();
103 void mergeNodes_compactNodes();
104 void mergeNodes_updateCells();
105 void mergeNodes();
106 void checkNeighbours();
108 int opposingFace(int i);
110 void toVtkGrid_HangingNodes(vtkUnstructuredGrid *grid, bool create_fields);
111 void toVtkGrid_Conforming(vtkUnstructuredGrid *grid, bool create_fields);
114 public: // methods
116 Octree();
118 void setOrigin(vec3_t x0);
119 void setBase(vec3_t g1, vec3_t g2, vec3_t g3);
120 void setBounds(vec3_t corner1, vec3_t corner2);
122 //int getNeighbour(int cell, int neigh) { return m_Cells[cell].m_Neighbour[neigh]; }
124 void markToRefine(int cell) { m_ToRefine[cell] = true; }
125 bool markedForRefine(int cell) { return m_ToRefine[cell]; }
126 int refineAll();
127 void resetRefineMarks();
128 void setSmoothTransitionOn() { m_SmoothTransition = true; }
129 void setSmoothTransitionOff() { m_SmoothTransition = false; }
131 vec3_t getCellCentre(int cell);
132 vec3_t getFaceCentre(int i_cells, int i_faces);
133 vec3_t getNodePosition(int cell, int node) { return m_Nodes[m_Cells[cell].m_Node[node]].m_Position; }
134 vec3_t getNodePosition(int node) { return m_Nodes[node].m_Position; }
135 int getNode(int cell, int node) { return m_Cells[cell].m_Node[node]; }
136 int getNumCells() { return m_Cells.size(); }
137 int getNumNodes() { return m_Nodes.size(); }
138 void getEdges(int cell, QVector<SortedPair<int> >& edges);
139 int getLevel(int cell) { return m_Cells[cell].m_Level; }
140 double getDx(int cell);
141 double getDy(int cell);
142 double getDz(int cell);
143 double getDx(const OctreeCell& cell);
144 double getDy(const OctreeCell& cell);
145 double getDz(const OctreeCell& cell);
146 bool hasChildren(int i_cells) { return m_Cells[i_cells].m_Child[0] != -1; }
147 int findCell(vec3_t x);
148 bool intersectsFace(int cell, int face, vec3_t x1, vec3_t x2, double &k, double tol = 1e-4);
149 void setMaxCells(int n) { m_MaxCells = n; }
151 void toVtkGrid(vtkUnstructuredGrid *grid, bool hanging_nodes = true, bool create_fields = false);
156 inline double Octree::getDx(int cell)
158 double dx = m_Dx;
159 for (int i = 0; i < m_Cells[cell].m_Level; ++i) {
160 dx *= 0.5;
162 return dx;
165 inline double Octree::getDx(const OctreeCell& cell)
167 double dx = m_Dx;
168 for (int i = 0; i < cell.m_Level; ++i) {
169 dx *= 0.5;
171 return dx;
174 inline double Octree::getDy(int cell)
176 double dy = m_Dy;
177 for (int i = 0; i < m_Cells[cell].m_Level; ++i) {
178 dy *= 0.5;
180 return dy;
183 inline double Octree::getDy(const OctreeCell& cell)
185 double dy = m_Dy;
186 for (int i = 0; i < cell.m_Level; ++i) {
187 dy *= 0.5;
189 return dy;
192 inline double Octree::getDz(int cell)
194 double dz = m_Dz;
195 for (int i = 0; i < m_Cells[cell].m_Level; ++i) {
196 dz *= 0.5;
198 return dz;
201 inline double Octree::getDz(const OctreeCell& cell)
203 double dz = m_Dz;
204 for (int i = 0; i < cell.m_Level; ++i) {
205 dz *= 0.5;
207 return dz;
210 #endif // OCTREE_H