Coupure prévue mardi 3 Août au matin pour maintenance du serveur. Nous faisons au mieux pour que celle-ci soit la plus brève possible.

basic.h 4.95 KB
Newer Older
Pierre Kraemer's avatar
Pierre Kraemer committed
1
2
3
/*******************************************************************************
 * CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
 * version 0.1                                                                  *
4
 * Copyright (C) 2009-2011, IGG Team, LSIIT, University of Strasbourg           *
Pierre Kraemer's avatar
Pierre Kraemer committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *                                                                              *
 * This library is free software; you can redistribute it and/or modify it      *
 * under the terms of the GNU Lesser General Public License as published by the *
 * Free Software Foundation; either version 2.1 of the License, or (at your     *
 * option) any later version.                                                   *
 *                                                                              *
 * This library is distributed in the hope that it will be useful, but WITHOUT  *
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
 * for more details.                                                            *
 *                                                                              *
 * You should have received a copy of the GNU Lesser General Public License     *
 * along with this library; if not, write to the Free Software Foundation,      *
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
 *                                                                              *
20
 * Web site: http://cgogn.u-strasbg.fr/                                         *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
 * Contact information: cgogn@unistra.fr                                        *
 *                                                                              *
 *******************************************************************************/

#ifndef __GEOMETRY__
#define __GEOMETRY__

#include "Geometry/vector_gen.h"
#include "Geometry/plane_3d.h"

namespace CGoGN
{

namespace Geom
{

// linear interpolation between 2 points
template <typename VEC>
VEC lerp(const VEC& v1, const VEC& v2, typename VEC::DATA_TYPE s)
{
	return (1.0 - s) * v1 + s * v2 ;
}

// weighted barycenter of 2 points
template <unsigned int DIM, typename T>
Vector<DIM,T> barycenter(const Vector<DIM,T>& v1, const Vector<DIM,T>& v2, T a, T b)
{
	return a * v1 + b * v2 ;
}

// isobarycenter of 2 points
template <unsigned int DIM, typename T>
Vector<DIM,T> isobarycenter(const Vector<DIM,T>& v1, const Vector<DIM,T>& v2)
{
	return lerp(v1, v2, 0.5) ;
}

// weighted barycenter of 3 points
template <unsigned int DIM, typename T>
Vector<DIM,T> barycenter(const Vector<DIM,T>& v1, const Vector<DIM,T>& v2, const Vector<DIM,T>& v3, T a, T b, T c)
{
	return a * v1 + b * v2 + c * v3 ;
}

// isobarycenter of 3 points
template <unsigned int DIM, typename T>
Vector<DIM,T> isobarycenter(const Vector<DIM,T>& v1, const Vector<DIM,T>& v2, const Vector<DIM,T>& v3)
{
	Vector<DIM,T> v ;
	for(unsigned int i = 0; i < DIM; ++i)
		v[i] = (v1[i] + v2[i] + v3[i]) / T(3) ;
	return v ;
}

// cosinus of the angle formed by 2 vectors
template <typename VEC>
typename VEC::DATA_TYPE cos_angle(const VEC& a, const VEC& b)
{
	typename VEC::DATA_TYPE na2 = a.norm2() ;
	typename VEC::DATA_TYPE nb2 = b.norm2() ;
	return (a * b) / sqrt(na2 * nb2) ;
}

// angle formed by 2 vectors
template <typename VEC>
typename VEC::DATA_TYPE angle(const VEC& a, const VEC& b)
{
	return acos(cos_angle(a,b)) ;
}

// area of the triangle formed by 3 points in 3D
template <typename VEC3>
typename VEC3::DATA_TYPE triangleArea(const VEC3& p1, const VEC3& p2, const VEC3& p3)
{
	return 0.5 * ((p2 - p1) ^ (p3 - p1)).norm() ;
}

98
// normal of the plane spanned by 3 points in 3D
Pierre Kraemer's avatar
Pierre Kraemer committed
99
100
101
102
103
104
template <typename VEC3>
VEC3 triangleNormal(const VEC3& p1, const VEC3& p2, const VEC3& p3)
{
	return (p2 - p1) ^ (p3 - p1) ;
}

105
106
107
108
109
110
111
112
113
114
115
116
117
// return true if the triangle formed by 3 points in 3D is obtuse, false otherwise
template <typename VEC3>
bool isTriangleObtuse(const VEC3& p1, const VEC3& p2, const VEC3& p3)
{
	typename VEC3::DATA_TYPE a1 = angle(p2-p1, p3-p1) ;
	if(a1 > M_PI / 2)
		return true ;
	typename VEC3::DATA_TYPE a2 = angle(p3-p2, p1-p2) ;
	if(a2 > M_PI / 2 || a1 + a2 < M_PI / 2)
		return true ;
	return false ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// signed volume of the tetrahedron formed by 4 points in 3D
template <typename VEC3>
typename VEC3::DATA_TYPE tetraSignedVolume(const VEC3& p1, const VEC3& p2, const VEC3& p3, const VEC3& p4)
{
	return tripleProduct(p2 - p1, p3 - p1, p4 - p1) / typename VEC3::DATA_TYPE(6) ;
}

// volume of the tetrahedron formed by 4 points in 3D
template <typename VEC3>
typename VEC3::DATA_TYPE tetraVolume(const VEC3& p1, const VEC3& p2, const VEC3& p3, const VEC3& p4)
{
	return fabs(tetraSignedVolume(p1,p2,p3,p4)) ;
}

// volume of the parallelepiped spanned by three 3D vectors
template <typename VEC3>
typename VEC3::DATA_TYPE parallelepipedVolume(const VEC3& v1, const VEC3& v2, const VEC3& v3)
{
	return tripleProduct(v1, v2, v3) ;
}

} // namespace Geom

} // namespace CGoGN

#endif