KiCad PCB EDA Suite
Loading...
Searching...
No Matches
snap_manifold.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 3
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef SNAP_MANIFOLD_H
21#define SNAP_MANIFOLD_H
22
24#include <geometry/nearest.h>
25#include <math/vector2d.h>
26
27#include <cmath>
28#include <type_traits>
29#include <variant>
30
37inline double snapManifoldDistance( const INTERSECTABLE_GEOM& aGeometry, const VECTOR2I& aPoint )
38{
39 return std::visit(
40 [&]( const auto& aShape ) -> double
41 {
42 using MANIFOLD_TYPE = std::decay_t<decltype( aShape )>;
43
44 if constexpr( std::is_same_v<MANIFOLD_TYPE, LINE> )
45 return aShape.Distance( aPoint );
46 else if constexpr( std::is_same_v<MANIFOLD_TYPE, HALF_LINE> )
47 return aShape.NearestPoint( aPoint ).Distance( aPoint );
48 else if constexpr( std::is_same_v<MANIFOLD_TYPE, SEG> )
49 return aShape.Distance( aPoint );
50 else if constexpr( std::is_same_v<MANIFOLD_TYPE, CIRCLE> )
51 return std::abs( aShape.Center.Distance( aPoint ) - aShape.Radius );
52 else if constexpr( std::is_same_v<MANIFOLD_TYPE, SHAPE_ARC> )
53 return aShape.NearestPoint( aPoint ).Distance( aPoint );
54 else
55 return GetNearestPoint( NEARABLE_GEOM( aShape ), aPoint ).Distance( aPoint );
56 },
57 aGeometry );
58}
59
60#endif
double Distance(const VECTOR2< extended_type > &aVector) const
Compute the distance between two vectors.
Definition vector2d.h:549
std::variant< LINE, HALF_LINE, SEG, CIRCLE, SHAPE_ARC, SHAPE_ELLIPSE, BOX2I > INTERSECTABLE_GEOM
A variant type that can hold any of the supported geometry types for intersection calculations.
EDA_ANGLE abs(const EDA_ANGLE &aAngle)
Definition eda_angle.h:411
VECTOR2I GetNearestPoint(const NEARABLE_GEOM &aGeom, const VECTOR2I &aPt)
Get the nearest point on a geometry to a given point.
Definition nearest.cpp:54
std::variant< LINE, HALF_LINE, SEG, CIRCLE, SHAPE_ARC, SHAPE_ELLIPSE, BOX2I, VECTOR2I > NEARABLE_GEOM
A variant type that can hold any of the supported geometry types for nearest point calculations.
Definition nearest.h:40
double snapManifoldDistance(const INTERSECTABLE_GEOM &aGeometry, const VECTOR2I &aPoint)
Distance from a point to the nearest point of a snap manifold shape.
VECTOR2< int32_t > VECTOR2I
Definition vector2d.h:683