plane_3d.hpp 4.4 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-2012, 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.unistra.fr/                                           *
Pierre Kraemer's avatar
Pierre Kraemer committed
21
22
23
24
25
26
27
28
29
30
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

namespace CGoGN
{

namespace Geom
{

31
32
33
34
35
36
37
38
39
40
41
template <typename T>
std::string Plane3D<T>::CGoGNnameOfType()
{
	std::stringstream ss ;
	ss << "Geom::Plane3D<" ;
	ss << nameOfType(T()) ;
	ss << ">" ;

	return ss.str() ;
}

Pierre Kraemer's avatar
Pierre Kraemer committed
42
43
44
45
46
/**********************************************/
/*                CONSTRUCTORS                */
/**********************************************/

template <typename T>
47
Plane3D<T>::Plane3D(int d) : m_normal(0), m_d(d)
48
{ }
Pierre Kraemer's avatar
Pierre Kraemer committed
49
50
51
52
53
54
55
56
57

template <typename T>
Plane3D<T>::Plane3D(const Plane3D<T>& p)
{
	m_normal = p.m_normal ;
	m_d = p.m_d ;
}

template <typename T>
58
Plane3D<T>::Plane3D(const Vector<3,T>& n, T d) : m_normal(n), m_d(d)
Pierre Kraemer's avatar
Pierre Kraemer committed
59
60
61
62
63
{
	m_normal.normalize();
}

template <typename T>
64
Plane3D<T>::Plane3D(const Vector<3,T>& n, const Vector<3,T>& p) : m_normal(n), m_d(-(p*n))
Pierre Kraemer's avatar
Pierre Kraemer committed
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
{
	m_normal.normalize();
}

template <typename T>
Plane3D<T>::Plane3D(const Vector<3,T>& p1, const Vector<3,T>& p2, const Vector<3,T>& p3)
{
	m_normal = (p2-p1) ^ (p3-p1) ;
	m_normal.normalize() ;
	m_d = -(p1 * m_normal) ;
}

/**********************************************/
/*                 ACCESSORS                  */
/**********************************************/
template <typename T>
Vector<3,T>& Plane3D<T>::normal()
{
	return m_normal ;
}

template <typename T>
const Vector<3,T>& Plane3D<T>::normal() const
{
	return m_normal ;
}

template <typename T>
T& Plane3D<T>::d()
{
	return m_d ;
}

template <typename T>
const T& Plane3D<T>::d() const
{
	return m_d ;
}

/**********************************************/
/*             UTILITY FUNCTIONS              */
/**********************************************/

template <typename T>
T Plane3D<T>::distance(const Vector<3,T>& p) const
{
	T k = m_normal * p ;
	return k + m_d ;
}

template <typename T>
void Plane3D<T>::project(Vector<3,T>& p) const
{
118
#define PRECISION 1e-10
Pierre Kraemer's avatar
Pierre Kraemer committed
119
	T d = -distance(p) ;
120
	if(std::abs(d) > PRECISION)
Pierre Kraemer's avatar
Pierre Kraemer committed
121
	{
122
		Vector<3,T> v = m_normal * d ;
Pierre Kraemer's avatar
Pierre Kraemer committed
123
124
125
126
127
128
129
130
		p += v ;
	}
#undef PRECISION
}

template <typename T>
Orientation3D Plane3D<T>::orient(const Vector<3,T>& p) const
{
131
#define PRECISION 1e-10
Pierre Kraemer's avatar
Pierre Kraemer committed
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	T dist = distance(p) ;
	if(dist < -PRECISION)
		return UNDER ;
	if(dist > PRECISION)
		return OVER ;
	return ON ;
#undef PRECISION
}

/**********************************************/
/*             STREAM OPERATORS               */
/**********************************************/

template <typename T>
std::ostream& operator<<(std::ostream& out, const Plane3D<T>& p)
{
	out << p.normal() << " " << p.d() ;
	return out ;
}

template <typename T>
std::istream& operator>>(std::istream& in, Plane3D<T>& p)
{
	in >> p.normal() >> p.d() ;
	return in ;
}

} // namespace Geom

} // namespace CGoGN