1 /*
2  *  OGF/Graphite: Geometry and Graphics Programming Library + Utilities
3  *  Copyright (C) 2000-2015 INRIA - Project ALICE
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  *  If you modify this software, you should include a notice giving the
20  *  name of the person performing the modification, the date of modification,
21  *  and the reason for such modification.
22  *
23  *  Contact for Graphite: Bruno Levy - Bruno.Levy@inria.fr
24  *  Contact for this Plugin: Nicolas Ray - nicolas.ray@inria.fr
25  *
26  *     Project ALICE
27  *     LORIA, INRIA Lorraine,
28  *     Campus Scientifique, BP 239
29  *     54506 VANDOEUVRE LES NANCY CEDEX
30  *     FRANCE
31  *
32  *  Note that the GNU General Public License does not permit incorporating
33  *  the Software into proprietary programs.
34  *
35  * As an exception to the GPL, Graphite can be linked with the following
36  * (non-GPL) libraries:
37  *     Qt, tetgen, SuperLU, WildMagic and CGAL
38  */
39 
40 #ifndef H_HEXDOM_ALGO_GEOMETRY_H
41 #define H_HEXDOM_ALGO_GEOMETRY_H
42 
43 #include <exploragram/basic/common.h>
44 #include <exploragram/hexdom/basic.h>
45 #include <geogram/basic/geometry.h>
46 
47 namespace GEO {
48 
49     struct EXPLORAGRAM_API CoTan3D{
50 	double w[6];
51 	double tetvol;
52 
53 	CoTan3D(vec3 P[4], double* anisotropy_as_xx_yy_zz_xy_yz_xz = nullptr);
54 
55 	index_t org(index_t e);
56 	index_t dest(index_t e);
57 	double coeff(index_t e);
58 
59 	void check_for_grad(vec3 P[4], vec3 grad);
60     };
61 
62     /*******************************************************************************/
63 
64     class EXPLORAGRAM_API TrglGradient {
65     public:
66 	TrglGradient(const vec3& p0, const vec3& p1, const vec3& p2);
67 
68 	/** Creates an uninitialized TrglGradient */
69 	TrglGradient();
70 
71 	void initialize(const vec3& p0, const vec3& p1, const vec3& p2);
72 
73 	/**
74 	 * Returns the ith vertex of the triangle.
75 	 * @param i is the index of the vertex,
76 	 *        which can be one of 0,1,2.
77 	 */
78 	const vec3& vertex(index_t i) const;
79 
80 	/**
81 	 * Returns the orthonormal basis in which gradient computation are
82 	 * performed.
83 	 */
84 	void basis(vec3& origin, vec3& X, vec3& Y, vec3& Z) const;
85 
86 	/**
87 	 * Returns the coefficients determining the gradient in this triangle.
88 	 *
89 	 * grad_X = sum_i { TX(i) * vertex(i)-> embedding(prop) }
90 	 * Note: TX(2) == 0
91 	 */
92 	double TX(index_t i) const;
93 
94 	/**
95 	 * Returns the coefficients determining the gradient in this triangle.
96 	 *
97 	 * grad_Y = sum_i { TY(i) * vertex(i)-> embedding(prop) }
98 	 */
99 	double TY(index_t i) const;
is_flat()100 	bool is_flat() const { return is_flat_; }
101 
102 	vec3 gradient_3d(double value0, double value1, double value2) const;
103 
104     private:
105 	double TX_[3];
106 	double TY_[3];
107 	vec3 vertex_[3];
108 	bool is_flat_;
109     };
110 
111     /*******************************************************************************/
112 
113     struct Basis3d {
Basis3dBasis3d114 	Basis3d(vec3 z) { // TODO DOCUMENT THIS!
115 	    v[2] = normalize(z);
116 	    if (std::fabs(v[2].z) < .8)
117 		v[0] = cross(v[2], vec3(0, 0, 1));
118 	    else v[0] = cross(v[2], vec3(1, 0, 0));
119 	    v[0] = normalize(v[0]);
120 	    v[1] = cross(v[2], v[0]);
121 	    geo_assert(std::abs(v[1].length2() - 1) < .001);
122 	}
project_xyBasis3d123 	vec2 project_xy(vec3 in){
124 	    return vec2(dot(in, v[0]), dot(in, v[1]));
125 	}
un_project_xyBasis3d126 	vec3 un_project_xy(vec2 in){
127 	    return in[0] * v[0] + in[1] * v[1];
128 	}
129 	vec3 v[3];
130     };
131 
132     /*******************************************************************************/
133 
134     /*     _____   _____            ____  _____                                _                    _                    _
135      *    |  __ \ / ____|   /\     |___ \|  __ \                              | |                  | |                  | |
136      *    | |__) | |       /  \      __) | |  | |  ______ ______   _ __   ___ | |_    ___ ___ _ __ | |_ ___ _ __ ___  __| |
137      *    |  ___/| |      / /\ \    |__ <| |  | | |______|______| | '_ \ / _ \| __|  / __/ _ \ '_ \| __/ _ \ '__/ _ \/ _` |
138      *    | |    | |____ / ____ \   ___) | |__| |                 | | | | (_) | |_  | (_|  __/ | | | ||  __/ | |  __/ (_| |
139      *    |_|     \_____/_/    \_\ |____/|_____/                  |_| |_|\___/ \__|  \___\___|_| |_|\__\___|_|  \___|\__,_|
140      *
141      */
142 
143     struct EXPLORAGRAM_API UncenteredPCA3D {
144     public:
145 	void begin_points();
146 	void end_points();
147 	void point(const vec3& p, double weight = 1.0);
148 	vec3 axis[3];
149 	double eigen_value[3];
150     private:
151 	double M_[6];
152 	int nb_points_;
153 	double sum_weights_;
154     };
155 
156 
157     /*******************************************************************************/
158 
159 
160     /* Triangle/triangle intersection test routine,
161      * by Tomas Moller, 1997.
162      * See article "A Fast Triangle-Triangle Intersection Test",
163      * Journal of Graphics Tools, 2(2), 1997
164      *
165      * Updated June 1999: removed the divisions -- a little faster now!
166      * Updated October 1999: added {} to CROSS and SUB macros
167      *
168      * int NoDivTriTriIsect(double V0[3],double V1[3],double V2[3],
169      *                      double U0[3],double U1[3],double U2[3])
170      *
171      * parameters: vertices of triangle 1: V0,V1,V2
172      *             vertices of triangle 2: U0,U1,U2
173      * result    : returns 1 if the triangles intersect, otherwise 0
174      *
175      */
176     int EXPLORAGRAM_API NoDivTriTriIsect(
177         double V0[3], double V1[3], double V2[3],
178 	double U0[3], double U1[3], double U2[3]
179     );
180 
181     /********************************************************/
182     /* AABB-triangle overlap test code                      */
183     /* by Tomas Akenine-Möller                              */
184     /* Function: int triBoxOverlap(float boxcenter[3],      */
185     /*          float boxhalfsize[3],float triverts[3][3]); */
186     /* History:                                             */
187     /*   2001-03-05: released the code in its first version */
188     /*   2001-06-18: changed the order of the tests, faster */
189     /*                                                      */
190     /* Acknowledgement: Many thanks to Pierre Terdiman for  */
191     /* suggestions and discussions on how to optimize code. */
192     /* Thanks to David Hunt for finding a ">="-bug!         */
193     /********************************************************/
194     int EXPLORAGRAM_API triBoxOverlap(
195 	float boxcenter[3], float boxhalfsize[3], float triverts[3][3]
196     );
197 
198     /*******************************************************************************/
199 
200 }
201 
202 #endif
203