Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
AlgorithmsAngleT.cc
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 
53 //=============================================================================
54 //
55 // IMPLEMENTATION
56 //
57 //=============================================================================
58 
59 #define ALGORITHMSANGLE_C
60 
61 //== INCLUDES =================================================================
62 
63 #include "AlgorithmsAngleT.hh"
64 
65 #include <OpenMesh/Core/Geometry/MathDefs.hh>
66 
67 #include <cmath>
68 #include <iostream>
69 
70 
71 //== NAMESPACES ===============================================================
72 
73 namespace ACG {
74 namespace Geometry {
75 
76 //== IMPLEMENTATION ==========================================================
77 
79 inline bool isNan(double x) {
80  return (x != x);
81 }
82 
83 
84 template < typename VectorT , typename ValueT >
85 ValueT
86 getFullangle( VectorT _vector1 , VectorT _vector2 , const VectorT& _normal , bool& _skip )
87 {
88  //Project vectors into tangent plane defined by _normal
89  _vector1 = _vector1 - _normal * ( _vector1 | _normal );
90  _vector2 = _vector2 - _normal * ( _vector2 | _normal );
91  _vector1.normalize();
92  _vector2.normalize();
93 
94  //calculate projection onto right Vector (used to decide if vector2 is left or right of vector1
95  const double right = ( ( _normal % _vector1 ) | _vector2 ) ;
96 
97  double sp = ( _vector1 | _vector2 );
98 
99  //Catch some errors with scalar product and the following acos
100  if (sp < -1.0) {
101  sp = -1.0;
102  }
103 
104  if (sp > 1.0) {
105  sp = 1.0;
106  }
107 
108  double angle = acos(sp);
109 
110  // catch some possible nans
111  _skip = ( isNan(right) || isNan(angle) ) ;
112 
113  if ( right < 0 ) {
114  angle = 2 * M_PI - angle;
115  }
116 
117  return angle;
118 }
119 
120 template < typename ValueT >
121 inline
122 ValueT
123 angleDist( const ValueT& angle0 , const ValueT& angle1 ) {
124  ValueT dist = fabs( angle1 - angle0 );
125  return ( std::min( dist , 2 * M_PI - dist) );
126 }
127 
128 template < typename ValueT >
129 inline
130 ValueT
131 getAngle( const ValueT& _cos ,
132  const ValueT& _sin )
133 {
134  const double angle_asin = asin( OpenMesh::sane_aarg(_sin) );
135  const double angle_acos = acos( OpenMesh::sane_aarg(_cos) );
136 
137  if ( angle_asin >= 0 ) { //Quadrant 1,2
138  if ( angle_acos >= 0 ) { // Quadrant 1
139  return angle_asin;
140  } else { //Quadrant 2
141  return (M_PI - angle_asin);
142  }
143  } else { //Quadrant 3,4
144  if ( angle_acos >= 0 ) { // Quadrant 4
145  return (2 * M_PI + angle_asin);
146  } else { //Quadrant 3
147  return (M_PI - angle_asin);
148  }
149  }
150 }
151 
152 template < typename ValueT >
153 inline
154 ValueT
155 radToDeg( const ValueT& _angle ) {
156  return ( _angle / M_PI * 180);
157 }
158 
159 template < typename ValueT >
160 inline
161 ValueT
162 degToRad( const ValueT& _angle ) {
163  return ( _angle / 180 * M_PI );
164 }
165 
166 
167 
168 //=============================================================================
169 } // Geometry Namespace
170 } // ACG Namespace
171 //=============================================================================
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
ValueT getAngle(const ValueT &_cos, const ValueT &_sin)
bool isNan(double x)
Return false if x is not a number.
ValueT degToRad(const ValueT &_angle)
Functions for geometric operations related to angles.
ValueT radToDeg(const ValueT &_angle)
ValueT angleDist(const ValueT &angle0, const ValueT &angle1)
T sane_aarg(T _aarg)
Trigonometry/angles - related.
Definition: MathDefs.hh:127
ValueT getFullangle(VectorT _vector1, VectorT _vector2, const VectorT &_normal, bool &_skip)