Developer Documentation
AlgorithmsAngleT_impl.hh
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 //
45 // IMPLEMENTATION
46 //
47 //=============================================================================
48 
49 #define ALGORITHMSANGLE_C
50 
51 //== INCLUDES =================================================================
52 
53 #include "AlgorithmsAngleT.hh"
54 
55 #include <OpenMesh/Core/Geometry/MathDefs.hh>
56 
57 #include <cmath>
58 #include <iostream>
59 
60 
61 //== NAMESPACES ===============================================================
62 
63 namespace ACG {
64 namespace Geometry {
65 
66 //== IMPLEMENTATION ==========================================================
67 
69 inline bool isNan(double x) {
70  return (x != x);
71 }
72 
73 
74 template < typename VectorT , typename ValueT >
75 ValueT
76 getFullangle( VectorT _vector1 , VectorT _vector2 , const VectorT& _normal , bool& _skip )
77 {
78  //Project vectors into tangent plane defined by _normal
79  _vector1 = _vector1 - _normal * ( _vector1 | _normal );
80  _vector2 = _vector2 - _normal * ( _vector2 | _normal );
81  _vector1.normalize();
82  _vector2.normalize();
83 
84  //calculate projection onto right Vector (used to decide if vector2 is left or right of vector1
85  const double right = ( ( _normal % _vector1 ) | _vector2 ) ;
86 
87  double sp = ( _vector1 | _vector2 );
88 
89  //Catch some errors with scalar product and the following acos
90  if (sp < -1.0) {
91  sp = -1.0;
92  }
93 
94  if (sp > 1.0) {
95  sp = 1.0;
96  }
97 
98  double angle = acos(sp);
99 
100  // catch some possible nans
101  _skip = ( isNan(right) || isNan(angle) ) ;
102 
103  if ( right < 0 ) {
104  angle = 2 * M_PI - angle;
105  }
106 
107  return angle;
108 }
109 
110 template < typename ValueT >
111 inline
112 ValueT
113 angleDist( const ValueT& angle0 , const ValueT& angle1 ) {
114  ValueT dist = fabs( angle1 - angle0 );
115  return ( std::min( dist , 2 * M_PI - dist) );
116 }
117 
118 template < typename ValueT >
119 inline
120 ValueT
121 getAngle( const ValueT& _cos ,
122  const ValueT& _sin )
123 {
124  const double angle_asin = asin( OpenMesh::sane_aarg(_sin) );
125  const double angle_acos = acos( OpenMesh::sane_aarg(_cos) );
126 
127  if ( angle_asin >= 0 ) { //Quadrant 1,2
128  if ( angle_acos >= 0 ) { // Quadrant 1
129  return angle_asin;
130  } else { //Quadrant 2
131  return (M_PI - angle_asin);
132  }
133  } else { //Quadrant 3,4
134  if ( angle_acos >= 0 ) { // Quadrant 4
135  return (2 * M_PI + angle_asin);
136  } else { //Quadrant 3
137  return (M_PI - angle_asin);
138  }
139  }
140 }
141 
142 template < typename ValueT >
143 inline
144 ValueT
145 radToDeg( const ValueT& _angle ) {
146  return ( _angle / M_PI * 180);
147 }
148 
149 template < typename ValueT >
150 inline
151 ValueT
152 degToRad( const ValueT& _angle ) {
153  return ( _angle / 180 * M_PI );
154 }
155 
156 
157 
158 //=============================================================================
159 } // Geometry Namespace
160 } // ACG Namespace
161 //=============================================================================
ValueT degToRad(const ValueT &_angle)
ValueT angleDist(const ValueT &angle0, const ValueT &angle1)
Namespace providing different geometric functions concerning angles.
T sane_aarg(T _aarg)
Trigonometry/angles - related.
Definition: MathDefs.hh:122
Functions for geometric operations related to angles.
ValueT radToDeg(const ValueT &_angle)
ValueT getFullangle(VectorT _vector1, VectorT _vector2, const VectorT &_normal, bool &_skip)
bool isNan(double x)
Return false if x is not a number.
ValueT getAngle(const ValueT &_cos, const ValueT &_sin)
auto normalize() -> decltype(*this/=std::declval< VectorT< S, DIM >>().norm())
Definition: Vector11T.hh:434