KiCad PCB EDA Suite
Loading...
Searching...
No Matches
box2_minmax.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 (C) 2024 Alex Shvartzkop <[email protected]>
5 * Copyright (C) 2024 Kicad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, you may find one here:
19 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20 * or you may search the http://www.gnu.org website for the version 2 license,
21 * or you may write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 */
24
25#ifndef __BOX2_MINMAX_H
26#define __BOX2_MINMAX_H
27
28#include <math/box2.h>
29#include <geometry/seg.h>
30#include <geometry/shape_arc.h>
31
32
37{
38 BOX2I_MINMAX() : m_Left( 0 ), m_Top( 0 ), m_Right( 0 ), m_Bottom( 0 ) {}
39
40 BOX2I_MINMAX( int aLeft, int aTop, int aRight, int aBottom ) :
41 m_Left( aLeft ), m_Top( aTop ), m_Right( aRight ), m_Bottom( aBottom )
42 {
43 }
44
45 BOX2I_MINMAX( const VECTOR2I& aPt ) : BOX2I_MINMAX( aPt.x, aPt.y ) {}
46
47 BOX2I_MINMAX( int aX, int aY ) : m_Left( aX ), m_Top( aY ), m_Right( aX ), m_Bottom( aY ) {}
48
49 BOX2I_MINMAX( const BOX2I& aBox ) :
50 m_Left( aBox.GetLeft() ), m_Top( aBox.GetTop() ), m_Right( aBox.GetRight() ),
51 m_Bottom( aBox.GetBottom() )
52 {
53 }
54
55 BOX2I_MINMAX( const VECTOR2I& aA, const VECTOR2I& aB )
56 {
57 m_Left = std::min( aA.x, aB.x );
58 m_Right = std::max( aA.x, aB.x );
59 m_Top = std::min( aA.y, aB.y );
60 m_Bottom = std::max( aA.y, aB.y );
61 }
62
63 operator BOX2I()
64 {
65 return BOX2ISafe( VECTOR2I( m_Left, m_Top ),
66 VECTOR2L( int64_t( m_Right ) - m_Left, int64_t( m_Bottom ) - m_Top ) );
67 }
68
69 BOX2I_MINMAX( const SHAPE_ARC& aArc ) : BOX2I_MINMAX( aArc.BBox() ) {}
70
71 BOX2I_MINMAX( const SEG& aSeg ) : BOX2I_MINMAX( aSeg.A, aSeg.B ) {}
72
73 inline bool Intersects( const BOX2I_MINMAX& aOther ) const
74 {
75 // calculate the left common area coordinate:
76 int left = std::max( m_Left, aOther.m_Left );
77 // calculate the right common area coordinate:
78 int right = std::min( m_Right, aOther.m_Right );
79 // calculate the upper common area coordinate:
80 int top = std::max( m_Top, aOther.m_Top );
81 // calculate the lower common area coordinate:
82 int bottom = std::min( m_Bottom, aOther.m_Bottom );
83
84 // if a common area exists, it must have a positive (null accepted) size
85 return left <= right && top <= bottom;
86 }
87
88 void Merge( const VECTOR2I& aPt )
89 {
90 m_Left = std::min( m_Left, aPt.x );
91 m_Right = std::max( m_Right, aPt.x );
92 m_Top = std::min( m_Top, aPt.y );
93 m_Bottom = std::max( m_Bottom, aPt.y );
94 }
95
97 {
98 int cx = ( (int64_t) m_Left + m_Right ) / 2;
99 int cy = ( (int64_t) m_Top + m_Bottom ) / 2;
100
101 return VECTOR2I( cx, cy );
102 }
103
104 double GetDiameter() const
105 {
106 VECTOR2L start( m_Left, m_Top );
107 VECTOR2L end( m_Right, m_Bottom );
108 VECTOR2L d = end - start;
109
110 return std::hypot( d.x, d.y );
111 }
112
114 int m_Top;
117};
118
119
120#endif
BOX2< VECTOR2I > BOX2I
Definition: box2.h:877
BOX2I BOX2ISafe(const BOX2D &aInput)
Definition: box2.h:883
Definition: seg.h:42
A min-max version of BOX2 for fast intersection checking.
Definition: box2_minmax.h:37
BOX2I_MINMAX(const VECTOR2I &aA, const VECTOR2I &aB)
Definition: box2_minmax.h:55
BOX2I_MINMAX(const SEG &aSeg)
Definition: box2_minmax.h:71
bool Intersects(const BOX2I_MINMAX &aOther) const
Definition: box2_minmax.h:73
BOX2I_MINMAX(int aX, int aY)
Definition: box2_minmax.h:47
double GetDiameter() const
Definition: box2_minmax.h:104
BOX2I_MINMAX(const SHAPE_ARC &aArc)
Definition: box2_minmax.h:69
void Merge(const VECTOR2I &aPt)
Definition: box2_minmax.h:88
BOX2I_MINMAX(const VECTOR2I &aPt)
Definition: box2_minmax.h:45
BOX2I_MINMAX(const BOX2I &aBox)
Definition: box2_minmax.h:49
VECTOR2I GetCenter() const
Definition: box2_minmax.h:96
BOX2I_MINMAX(int aLeft, int aTop, int aRight, int aBottom)
Definition: box2_minmax.h:40
VECTOR2< int32_t > VECTOR2I
Definition: vector2d.h:673
VECTOR2< int64_t > VECTOR2L
Definition: vector2d.h:674