|
| 1 | +/* |
| 2 | + * This file is part of the GreasePad distribution (https://github.com/FraunhoferIOSB/GreasePad). |
| 3 | + * Copyright (c) 2022-2026 Jochen Meidow, Fraunhofer IOSB |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#ifndef ACUTE_H |
| 20 | +#define ACUTE_H |
| 21 | + |
| 22 | + |
| 23 | +#include <Eigen/Core> |
| 24 | + |
| 25 | +#include <cassert> |
| 26 | +#include <cfloat> |
| 27 | +#include <cmath> |
| 28 | +#include <cstdlib> |
| 29 | + |
| 30 | + |
| 31 | +namespace Geometry { |
| 32 | + |
| 33 | +using Eigen::Vector2d; |
| 34 | +using Eigen::Vector3d; |
| 35 | + |
| 36 | + |
| 37 | +//! acute angle between two straight lines in radians |
| 38 | +static inline double acute( const Vector3d l, const Vector3d m) |
| 39 | +{ |
| 40 | + constexpr double pi = 3.141592653589793; |
| 41 | + constexpr double pi_2 = 3.141592653589793/2; |
| 42 | + |
| 43 | + Vector2d lh = l.head(2); |
| 44 | + Vector2d mh = m.head(2); |
| 45 | + assert( lh.norm() > FLT_EPSILON ); |
| 46 | + assert( mh.norm() > FLT_EPSILON ); |
| 47 | + lh.normalize(); |
| 48 | + mh.normalize(); |
| 49 | + |
| 50 | + assert( std::fabs(lh.dot(mh)) <= 1.0 ); // a'*b in [-1,+1] |
| 51 | + double alpha = std::acos( lh.dot(mh) ); // [0,pi] |
| 52 | + if ( alpha > pi_2) { |
| 53 | + alpha = pi -alpha; |
| 54 | + } |
| 55 | + assert( alpha >= 0.0 ); |
| 56 | + assert( alpha <= pi_2 ); |
| 57 | + |
| 58 | + return alpha; |
| 59 | +} |
| 60 | + |
| 61 | +} // namespace Geometry |
| 62 | + |
| 63 | +#endif // ACUTE_H |
0 commit comments