File: cpolygon4pts2d.cpp

package info (click to toggle)
kicad 5.0.2%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 234,592 kB
  • sloc: cpp: 505,330; ansic: 57,038; python: 4,886; sh: 879; awk: 294; makefile: 253; xml: 103; perl: 5
file content (212 lines) | stat: -rw-r--r-- 5,749 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
 * Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

/**
 * @file  cpolygon4pts2d.cpp
 * @brief
 */

#include "cpolygon4pts2d.h"
#include <wx/debug.h>


CPOLYGON4PTS2D::CPOLYGON4PTS2D( const SFVEC2F &v1,
                                const SFVEC2F &v2,
                                const SFVEC2F &v3,
                                const SFVEC2F &v4,
                                const BOARD_ITEM &aBoardItem ) :
                COBJECT2D( OBJ2D_POLYGON4PT, aBoardItem )
{/*
    if( (v1.x > v2.x) || (v1.y < v2.y) )
    {
        m_segments[0] = v4;
        m_segments[1] = v3;
        m_segments[2] = v2;
        m_segments[3] = v1;
    }
    else
    {*/
        m_segments[0] = v1;
        m_segments[1] = v4;
        m_segments[2] = v3;
        m_segments[3] = v2;
   // }

    unsigned int i;
    unsigned int j = 4 - 1;

    for( i = 0; i < 4; j = i++ )
    {
        SFVEC2F slope = m_segments[j] - m_segments[i];
        m_precalc_slope[i] = slope;
        m_seg_normal[i] = glm::normalize( SFVEC2F( -slope.y, +slope.x ) );
    }

    m_bbox.Reset();
    m_bbox.Union( v1 );
    m_bbox.Union( v2 );
    m_bbox.Union( v3 );
    m_bbox.Union( v4 );
    m_bbox.ScaleNextUp();
    m_bbox.ScaleNextUp();
    m_bbox.ScaleNextUp();
    m_bbox.ScaleNextUp();
    m_bbox.ScaleNextUp();
    m_centroid = m_bbox.GetCenter();

    wxASSERT( m_bbox.IsInitialized() );
}


bool CPOLYGON4PTS2D::Intersects( const CBBOX2D &aBBox ) const
{
    return m_bbox.Intersects( aBBox );

    // This source code is not working OK.
    /*
    if( !m_bbox.Intersects( aBBox ) )
        return false;

    // Check if the bouding box complety have inside the small bouding box
    if( (aBBox.Max().x > m_bbox.Max().x) &&
        (aBBox.Max().y > m_bbox.Max().x) &&
        (aBBox.Min().x < m_bbox.Min().x) &&
        (aBBox.Min().y < m_bbox.Min().y)
        )
        return true;

    SFVEC2F v[4];

    v[0] = aBBox.Min();
    v[1] = SFVEC2F( aBBox.Min().x, aBBox.Max().y );
    v[2] = aBBox.Max();
    v[3] = SFVEC2F( aBBox.Max().x, aBBox.Min().y );

    for( unsigned int i = 0; i < 4; i++ )
    {
        if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[0], v[1] - v[0] ) )
            return true;
        if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[1], v[2] - v[1] ) )
            return true;
        if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[2], v[3] - v[2] ) )
            return true;
        if( IntersectSegment( m_segments[i], m_precalc_slope[i], v[3], v[0] - v[3] ) )
            return true;
    }

    if( IsPointInside( v[0] ) )
        return true;
    if( IsPointInside( v[1] ) )
        return true;
    if( IsPointInside( v[2] ) )
        return true;
    if( IsPointInside( v[3] ) )
        return true;

    return false;*/
}


bool CPOLYGON4PTS2D::Overlaps( const CBBOX2D &aBBox ) const
{
    // NOT IMPLEMENTED
    return true;
}


bool CPOLYGON4PTS2D::Intersect( const RAYSEG2D &aSegRay,
                                float *aOutT,
                                SFVEC2F *aNormalOut ) const
{
    wxASSERT( aOutT );
    wxASSERT( aNormalOut );

    bool  hited = false;
    unsigned int hitIndex;
    float bestHitT;

    for( unsigned int i = 0; i < 4; i++ )
    {
        float t;

        if( aSegRay.IntersectSegment( m_segments[i], m_precalc_slope[i], &t ) )
            if( (hited == false) || ( t < bestHitT) )
            {
                hited = true;
                hitIndex = i;
                bestHitT = t;
            }
    }

    if( hited )
    {
        wxASSERT( (bestHitT >= 0.0f) && (bestHitT <= 1.0f) );

        *aOutT = bestHitT;
        *aNormalOut = m_seg_normal[hitIndex];

        return true;
    }

    return false;
}


INTERSECTION_RESULT CPOLYGON4PTS2D::IsBBoxInside( const CBBOX2D &aBBox ) const
{
    // !TODO:

    return INTR_MISSES;
}


bool CPOLYGON4PTS2D::IsPointInside( const SFVEC2F &aPoint ) const
{
    unsigned int i;
    unsigned int j = 4 - 1;
    bool  oddNodes = false;

    for( i = 0; i < 4; j = i++ )
    {
        const float polyJY = m_segments[j].y;
        const float polyIY = m_segments[i].y;

        if( ((polyIY <= aPoint.y) && (polyJY >= aPoint.y)) ||
            ((polyJY <= aPoint.y) && (polyIY >= aPoint.y))
          )
        {
            const float polyJX = m_segments[j].x;
            const float polyIX = m_segments[i].x;

            if( (polyIX <= aPoint.x) || (polyJX <= aPoint.x) )
            {
                oddNodes ^= ( ( polyIX +
                                ( ( aPoint.y - polyIY ) / ( polyJY - polyIY ) ) *
                                ( polyJX - polyIX ) ) < aPoint.x );
            }
        }
    }

    return oddNodes;
}