1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
#ifndef GFXGEOM3D_INCLUDED
#define GFXGEOM3D_INCLUDED
#if !defined(__GNUC__)
# pragma once
#endif
/************************************************************************
Handy 3D geometrical primitives
$Id: geom3d.h 432 2004-11-02 22:55:41Z garland $
************************************************************************/
#include "vec3.h"
namespace gfx
{
//
// Computing properties of triangles
//
template<class Vec>
inline Vec triangle_raw_normal(const Vec& v1, const Vec& v2, const Vec& v3)
{
return cross(v2-v1, v3-v1);
}
template<class Vec>
inline typename Vec::value_type
triangle_area(const Vec& v1,const Vec& v2,const Vec& v3)
{
return 0.5 * norm(triangle_raw_normal(v1, v2, v3));
}
template<class Vec>
inline Vec triangle_normal(const Vec& v1, const Vec& v2, const Vec& v3)
{
Vec n = triangle_raw_normal(v1, v2, v3);
unitize(n);
return n;
}
template<class Vec, class Plane>
inline Plane triangle_plane(const Vec& v1, const Vec& v2, const Vec& v3)
{
Vec n = triangle_normal(v1, v2, v3);
return Plane(n, -(n*v1));
}
template<class Vec, class Plane>
inline Plane triangle_raw_plane(const Vec& v1, const Vec& v2, const Vec& v3)
{
Vec n = triangle_raw_normal(v1, v2, v3);
return Plane(n, -(n*v1));
}
template< class Vec>
inline typename Vec::value_type
triangle_compactness(const Vec& v1, const Vec& v2, const Vec& v3)
{
const double FOUR_ROOT3 = 6.928203230275509;
return FOUR_ROOT3 * triangle_area(v1, v2, v3) /
( norm2(v2 - v1) + norm2(v3 - v2) + norm2(v1 - v3) );
}
//
// Operations with axis-aligned bounding boxes
//
template<class Vec, class List>
void update_bbox(Vec& min, Vec& max, const List& items)
{
typedef typename List::const_iterator iterator;
for(iterator i=items.begin(); i!=items.end(); i++)
{
const Vec& v = *i;
for(int j=0; j<Vec::dim(); j++)
{
if( v[j] < min[j] ) min[j] = v[j];
if( v[j] > max[j] ) max[j] = v[j];
}
}
}
template<class Vec, class List>
void compute_bbox(Vec& min, Vec& max, const List& items)
{
if( items.size()==0 ) min = max = 0;
else min = max = items[0];
update_bbox(min, max, items);
}
template<class Vec>
bool is_inside_bbox(const Vec& p, const Vec& min, Vec& max)
{
for(int i=0; i<Vec::dim(); i++)
if( p[i]<min[i] || p[i]>max[i] )
return false;
return true;
}
template<class Vec>
Vec clamp_to_bbox(Vec p, const Vec& min, const Vec& max)
{
for(int i=0; i<Vec::dim(); i++)
{
if (p[i]<min[i]) p[i]=min[i];
else if (p[i]>max[i]) p[i]=max[i];
}
return p;
}
//
// Computing properties of tetrahedra
//
extern double tetrahedron_determinant(const Vec3& v0, const Vec3& v1,
const Vec3& v2, const Vec3& v3);
extern double tetrahedron_volume(const Vec3& v0, const Vec3& v1,
const Vec3& v2, const Vec3& v3);
} // namespace gfx
// GFXGEOM3D_INCLUDED
#endif
|