replaced abort() with EG_BUG
[engrid.git] / src / geometrytools.h
bloba606df7caabbb0ce885cca6d9e33600eba1e997d
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 geometrytools_H
24 #define geometrytools_H
26 #include "math/mathvector.h"
27 #include "math/smallsquarematrix.h"
29 #include <vtkUnstructuredGrid.h>
31 namespace GeometryTools
35 double rad2deg( double rad ); ///< Converts radians to degrees
36 double deg2rad( double deg ); ///< Converts degrees to radians
38 void rotate(vec3_t g1, vec3_t g2, vec3_t g3, vec3_t &b, double theta);
40 /** Rotates vector v around vector axis by an angle theta */
41 vec3_t rotate(vec3_t v, vec3_t axis, double theta);
43 /** Returns a normalized vector orthogonal to v */
44 vec3_t orthogonalVector(vec3_t v);
46 double intersection(vec3_t x_straight, vec3_t v_straight,
47 vec3_t x_plane, vec3_t n_plane);
49 double intersection(vec3_t x_straight, vec3_t v_straight,
50 vec3_t x_plane, vec3_t u_plane, vec3_t v_plane);
52 bool intersectEdgeAndTriangle(const vec3_t& a, const vec3_t& b, const vec3_t& c,
53 const vec3_t& x1, const vec3_t& x2, vec3_t& xi, vec3_t& ri, double tol = 1e-4);
55 bool intersection (double &k1, double &k2, vec2_t r1, vec2_t u1, vec2_t r2, vec2_t u2);
57 void sliceTriangle(const vector<vec3_t> &Tin, vec3_t x, vec3_t n, vector<vector<vec3_t> > &Tout);
59 double tetraVol(const vec3_t& x0, const vec3_t& x1, const vec3_t& x2, const vec3_t& x3, bool neg = false);
61 double pyraVol(vec3_t x1, vec3_t x2, vec3_t x3, vec3_t x4, vec3_t x5, bool neg = false);
63 double prismVol(vec3_t x1, vec3_t x2, vec3_t x3, vec3_t x4, vec3_t x5, vec3_t x6, bool neg = false);
65 double hexaVol(vec3_t x1, vec3_t x2, vec3_t x3, vec3_t x4, vec3_t x5, vec3_t x6, vec3_t x7, vec3_t x8, bool neg = false);
67 double triArea(vec3_t x1, vec3_t x2, vec3_t x3);
69 double quadArea(vec3_t x1, vec3_t x2, vec3_t x3, vec3_t x4);
71 vec3_t triNormal(vec3_t x1, vec3_t x2, vec3_t x3);
73 vec3_t quadNormal(vec3_t x1, vec3_t x2, vec3_t x3, vec3_t x4);
75 vec3_t triNormal(vtkUnstructuredGrid *grid, vtkIdType p1, vtkIdType p2, vtkIdType p3);
77 vec3_t quadNormal(vtkUnstructuredGrid *grid, vtkIdType p1, vtkIdType p2, vtkIdType p3, vtkIdType p4);
79 vec3_t cellNormal(vtkUnstructuredGrid *grid, vtkIdType i);
81 double cellVA(vtkUnstructuredGrid *grid, vtkIdType cellId, bool neg = false);
83 inline vec2_t turnRight(const vec2_t &v)
85 vec2_t u;
86 u[0] = v[1];
87 u[1] = -v[0];
88 return u;
91 inline vec2_t turnLeft(const vec2_t &v)
93 vec2_t u;
94 u[0] = -v[1];
95 u[1] = v[0];
96 return u;
99 //polygon must be numbered clockwise
100 inline bool IsConvex(vec3_t a,vec3_t b,vec3_t c,vec3_t d)
102 vec3_t u[4];
103 u[0]=b-a;
104 u[1]=c-b;
105 u[2]=d-c;
106 u[3]=a-d;
108 for(int i=0;i<4;i++) {
109 vec3_t n=u[i].cross(u[(i+1)%4]);
110 if(n[2]>0) return(false);
112 return(true);
115 inline bool IsConvex(vec2_t a_2D,vec2_t b_2D,vec2_t c_2D,vec2_t d_2D)
117 vec3_t a_3D(a_2D[0],a_2D[1]);
118 vec3_t b_3D(b_2D[0],b_2D[1]);
119 vec3_t c_3D(c_2D[0],c_2D[1]);
120 vec3_t d_3D(d_2D[0],d_2D[1]);
121 return(IsConvex(a_3D,b_3D,c_3D,d_3D));
124 /** return the angle w.r.t. another 3-vector */
125 double angle(const vec3_t & u, const vec3_t & v);
127 /** return the deviation p1->p2->p3 (angle(p2-p1,p3-p2)) */
128 double deviation(vtkUnstructuredGrid *grid, vtkIdType p1, vtkIdType p2, vtkIdType p3);
130 /** return the angle p1,p2,p3 (angle(p1-p2,p3-p2)) */
131 double angle(vtkUnstructuredGrid *grid, vtkIdType p1, vtkIdType p2, vtkIdType p3);
133 /** return the cosine of the angle between the normals of cell1 and cell2 */
134 double CosAngle(vtkUnstructuredGrid *grid, vtkIdType cell1, vtkIdType cell2);
136 /** Returns the center of mass of cellId + passes the minimal and maximal center to corner distances by reference */
137 vec3_t getCenter(vtkUnstructuredGrid *grid, vtkIdType cellId, double& Rmin, double& Rmax);
139 /** Returns the distance between id_node1 and id_node2 */
140 double distance(vtkUnstructuredGrid *grid, vtkIdType id_node1, vtkIdType id_node2);
142 /** Returns the distance squared between id_node1 and id_node2 */
143 double distance2(vtkUnstructuredGrid *grid, vtkIdType id_node1, vtkIdType id_node2);
145 /** area of the circumscribed circle of the triangle */
146 double areaOfCircumscribedCircle(vtkUnstructuredGrid *grid, vtkIdType id_cell);
150 #endif