2018-07-13 04:52:21 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2024-01-12 17:04:05 +00:00
|
|
|
* Modifications Copyright (C) 2018-2024 KiCad Developers
|
2018-07-13 04:52:21 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* Based on Uniform Plane Subdivision algorithm from Lamot, Marko, and Borut Žalik.
|
|
|
|
* "A fast polygon triangulation algorithm based on uniform plane subdivision."
|
|
|
|
* Computers & graphics 27, no. 2 (2003): 239-253.
|
|
|
|
*
|
|
|
|
* Code derived from:
|
|
|
|
* K-3D which is Copyright (c) 2005-2006, Romain Behar, GPL-2, license above
|
|
|
|
* earcut which is Copyright (c) 2016, Mapbox, ISC
|
|
|
|
*
|
|
|
|
* ISC License:
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any purpose
|
|
|
|
* with or without fee is hereby granted, provided that the above copyright notice
|
|
|
|
* and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
* FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
|
|
|
|
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
|
|
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
|
|
|
|
* THIS SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __POLYGON_TRIANGULATION_H
|
|
|
|
#define __POLYGON_TRIANGULATION_H
|
|
|
|
|
|
|
|
#include <algorithm>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <deque>
|
2018-07-13 04:52:21 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
2024-03-21 01:00:42 +00:00
|
|
|
#include <advanced_config.h>
|
2020-01-07 17:12:59 +00:00
|
|
|
#include <clipper.hpp>
|
|
|
|
#include <geometry/shape_line_chain.h>
|
|
|
|
#include <geometry/shape_poly_set.h>
|
|
|
|
#include <math/box2.h>
|
|
|
|
#include <math/vector2d.h>
|
2018-09-12 10:19:11 +00:00
|
|
|
|
2024-03-20 22:47:44 +00:00
|
|
|
#include <wx/log.h>
|
|
|
|
|
2024-02-12 23:19:01 +00:00
|
|
|
#define TRIANGULATE_TRACE "triangulate"
|
2024-01-12 17:09:52 +00:00
|
|
|
class POLYGON_TRIANGULATION
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
|
|
|
public:
|
2024-01-12 17:09:52 +00:00
|
|
|
POLYGON_TRIANGULATION( SHAPE_POLY_SET::TRIANGULATED_POLYGON& aResult ) :
|
2024-04-02 18:19:18 +00:00
|
|
|
m_vertices_original_size( 0 ), m_result( aResult )
|
2018-07-13 04:52:21 +00:00
|
|
|
{};
|
|
|
|
|
2024-01-12 17:04:05 +00:00
|
|
|
bool TesselatePolygon( const SHAPE_LINE_CHAIN& aPoly,
|
|
|
|
SHAPE_POLY_SET::TRIANGULATED_POLYGON* aHintData )
|
2020-12-12 03:43:16 +00:00
|
|
|
{
|
|
|
|
m_bbox = aPoly.BBox();
|
|
|
|
m_result.Clear();
|
|
|
|
|
|
|
|
if( !m_bbox.GetWidth() || !m_bbox.GetHeight() )
|
2024-04-01 17:45:26 +00:00
|
|
|
return true;
|
2020-12-12 03:43:16 +00:00
|
|
|
|
|
|
|
/// Place the polygon Vertices into a circular linked list
|
|
|
|
/// and check for lists that have only 0, 1 or 2 elements and
|
|
|
|
/// therefore cannot be polygons
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* firstVertex = createList( aPoly );
|
|
|
|
|
2020-12-12 03:43:16 +00:00
|
|
|
if( !firstVertex || firstVertex->prev == firstVertex->next )
|
2024-04-01 17:45:26 +00:00
|
|
|
return true;
|
2020-12-12 03:43:16 +00:00
|
|
|
|
2024-03-29 23:46:08 +00:00
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "Created list with %f area", firstVertex->area() );
|
|
|
|
|
2024-04-02 18:19:18 +00:00
|
|
|
m_vertices_original_size = m_vertices.size();
|
2020-12-12 03:43:16 +00:00
|
|
|
firstVertex->updateList();
|
|
|
|
|
2024-03-31 21:57:54 +00:00
|
|
|
/**
|
|
|
|
* If we have a hint data, we can skip the tesselation process as long as the
|
|
|
|
* the hint source did not need to subdivide the polygon.
|
|
|
|
*/
|
|
|
|
if( aHintData && aHintData->Vertices().size() == m_vertices.size() )
|
2024-01-12 17:04:05 +00:00
|
|
|
{
|
2024-03-31 21:57:54 +00:00
|
|
|
m_result.SetTriangles( aHintData->Triangles() );
|
2024-01-12 17:04:05 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
auto retval = earcutList( firstVertex );
|
2024-03-21 01:00:42 +00:00
|
|
|
|
|
|
|
if( !retval )
|
2024-03-26 00:24:50 +00:00
|
|
|
{
|
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "Tesselation failed, logging remaining vertices" );
|
2024-03-21 01:00:42 +00:00
|
|
|
logRemaining();
|
2024-03-26 00:24:50 +00:00
|
|
|
}
|
2024-03-21 01:00:42 +00:00
|
|
|
|
2024-01-12 17:04:05 +00:00
|
|
|
m_vertices.clear();
|
|
|
|
return retval;
|
|
|
|
}
|
2020-12-12 03:43:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
private:
|
2024-01-12 17:09:52 +00:00
|
|
|
struct VERTEX
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX( size_t aIndex, double aX, double aY, POLYGON_TRIANGULATION* aParent ) :
|
2020-12-12 03:43:16 +00:00
|
|
|
i( aIndex ),
|
|
|
|
x( aX ),
|
|
|
|
y( aY ),
|
|
|
|
parent( aParent )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
|
|
|
}
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX& operator=( const VERTEX& ) = delete;
|
|
|
|
VERTEX& operator=( VERTEX&& ) = delete;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
2024-01-12 17:09:52 +00:00
|
|
|
bool operator==( const VERTEX& rhs ) const
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
|
|
|
return this->x == rhs.x && this->y == rhs.y;
|
|
|
|
}
|
2024-01-12 17:09:52 +00:00
|
|
|
bool operator!=( const VERTEX& rhs ) const { return !( *this == rhs ); }
|
2018-07-13 04:52:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Split the referenced polygon between the reference point and
|
2018-07-13 04:52:21 +00:00
|
|
|
* vertex b, assuming they are in the same polygon. Notes that while we
|
|
|
|
* create a new vertex pointer for the linked list, we maintain the same
|
|
|
|
* vertex index value from the original polygon. In this way, we have
|
|
|
|
* two polygons that both share the same vertices.
|
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return the newly created vertex in the polygon that does not include the
|
|
|
|
* reference vertex.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* split( VERTEX* b )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
|
|
|
parent->m_vertices.emplace_back( i, x, y, parent );
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* a2 = &parent->m_vertices.back();
|
2018-07-13 04:52:21 +00:00
|
|
|
parent->m_vertices.emplace_back( b->i, b->x, b->y, parent );
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* b2 = &parent->m_vertices.back();
|
|
|
|
VERTEX* an = next;
|
|
|
|
VERTEX* bp = b->prev;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
|
|
|
next = b;
|
|
|
|
b->prev = this;
|
|
|
|
|
|
|
|
a2->next = an;
|
|
|
|
an->prev = a2;
|
|
|
|
|
|
|
|
b2->next = a2;
|
|
|
|
a2->prev = b2;
|
|
|
|
|
|
|
|
bp->next = b2;
|
|
|
|
b2->prev = bp;
|
|
|
|
|
|
|
|
return b2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Remove the node from the linked list and z-ordered linked list.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
|
|
|
void remove()
|
|
|
|
{
|
|
|
|
next->prev = prev;
|
|
|
|
prev->next = next;
|
|
|
|
|
|
|
|
if( prevZ )
|
|
|
|
prevZ->nextZ = nextZ;
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
if( nextZ )
|
|
|
|
nextZ->prevZ = prevZ;
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
next = nullptr;
|
|
|
|
prev = nullptr;
|
|
|
|
nextZ = nullptr;
|
|
|
|
prevZ = nullptr;
|
2018-07-13 04:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void updateOrder()
|
|
|
|
{
|
|
|
|
if( !z )
|
|
|
|
z = parent->zOrder( x, y );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* After inserting or changing nodes, this function should be called to
|
2021-07-01 17:46:17 +00:00
|
|
|
* remove duplicate vertices and ensure z-ordering is correct.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
|
|
|
void updateList()
|
|
|
|
{
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* p = next;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
|
|
|
while( p != this )
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Remove duplicates
|
|
|
|
*/
|
|
|
|
if( *p == *p->next )
|
|
|
|
{
|
|
|
|
p = p->prev;
|
|
|
|
p->next->remove();
|
|
|
|
|
|
|
|
if( p == p->next )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->updateOrder();
|
|
|
|
p = p->next;
|
|
|
|
};
|
|
|
|
|
|
|
|
updateOrder();
|
|
|
|
zSort();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Sort all vertices in this vertex's list by their Morton code.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
|
|
|
void zSort()
|
|
|
|
{
|
2024-01-12 17:09:52 +00:00
|
|
|
std::deque<VERTEX*> queue;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
|
|
|
queue.push_back( this );
|
|
|
|
|
|
|
|
for( auto p = next; p && p != this; p = p->next )
|
|
|
|
queue.push_back( p );
|
|
|
|
|
2024-01-12 17:09:52 +00:00
|
|
|
std::sort( queue.begin(), queue.end(), []( const VERTEX* a, const VERTEX* b )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2023-02-13 19:18:04 +00:00
|
|
|
if( a->z != b->z )
|
|
|
|
return a->z < b->z;
|
|
|
|
|
|
|
|
if( a->x != b->x )
|
|
|
|
return a->x < b->x;
|
|
|
|
|
|
|
|
if( a->y != b->y )
|
|
|
|
return a->y < b->y;
|
|
|
|
|
|
|
|
return a->i < b->i;
|
2018-07-13 04:52:21 +00:00
|
|
|
} );
|
|
|
|
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* prev_elem = nullptr;
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
for( auto elem : queue )
|
|
|
|
{
|
|
|
|
if( prev_elem )
|
|
|
|
prev_elem->nextZ = elem;
|
|
|
|
|
|
|
|
elem->prevZ = prev_elem;
|
|
|
|
prev_elem = elem;
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_elem->nextZ = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check to see if triangle surrounds our current vertex
|
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
bool inTriangle( const VERTEX& a, const VERTEX& b, const VERTEX& c )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
|
|
|
return ( c.x - x ) * ( a.y - y ) - ( a.x - x ) * ( c.y - y ) >= 0
|
|
|
|
&& ( a.x - x ) * ( b.y - y ) - ( b.x - x ) * ( a.y - y ) >= 0
|
|
|
|
&& ( b.x - x ) * ( c.y - y ) - ( c.x - x ) * ( b.y - y ) >= 0;
|
|
|
|
}
|
|
|
|
|
2024-03-20 22:47:44 +00:00
|
|
|
/**
|
2024-03-26 00:24:50 +00:00
|
|
|
* Returns the signed area of the polygon connected to the current vertex,
|
|
|
|
* optionally ending at a specified vertex.
|
|
|
|
*/
|
|
|
|
double area( const VERTEX* aEnd = nullptr ) const
|
2024-03-20 22:47:44 +00:00
|
|
|
{
|
|
|
|
const VERTEX* p = this;
|
|
|
|
double a = 0.0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2024-03-26 00:24:50 +00:00
|
|
|
a += ( p->x + p->next->x ) * ( p->next->y - p->y );
|
2024-03-20 22:47:44 +00:00
|
|
|
p = p->next;
|
2024-03-26 00:24:50 +00:00
|
|
|
} while( p != this && p != aEnd );
|
|
|
|
|
|
|
|
if( p != this )
|
|
|
|
a += ( p->x + aEnd->x ) * ( aEnd->y - p->y );
|
2024-03-20 22:47:44 +00:00
|
|
|
|
|
|
|
return a / 2;
|
|
|
|
}
|
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
const size_t i;
|
2024-03-26 00:24:50 +00:00
|
|
|
double x;
|
|
|
|
double y;
|
2024-01-12 17:09:52 +00:00
|
|
|
POLYGON_TRIANGULATION* parent;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
|
|
|
// previous and next vertices nodes in a polygon ring
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* prev = nullptr;
|
|
|
|
VERTEX* next = nullptr;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
|
|
|
// z-order curve value
|
|
|
|
int32_t z = 0;
|
|
|
|
|
|
|
|
// previous and next nodes in z-order
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* prevZ = nullptr;
|
|
|
|
VERTEX* nextZ = nullptr;
|
2018-07-13 04:52:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculate the Morton code of the Vertex
|
|
|
|
* http://www.graphics.stanford.edu/~seander/bithacks.html#InterleaveBMN
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
int32_t zOrder( const double aX, const double aY ) const
|
|
|
|
{
|
2024-01-18 16:34:11 +00:00
|
|
|
int32_t x = static_cast<int32_t>( 32767.0 * ( aX - m_bbox.GetX() ) / m_bbox.GetWidth() );
|
|
|
|
int32_t y = static_cast<int32_t>( 32767.0 * ( aY - m_bbox.GetY() ) / m_bbox.GetHeight() );
|
2018-07-13 04:52:21 +00:00
|
|
|
|
|
|
|
x = ( x | ( x << 8 ) ) & 0x00FF00FF;
|
|
|
|
x = ( x | ( x << 4 ) ) & 0x0F0F0F0F;
|
|
|
|
x = ( x | ( x << 2 ) ) & 0x33333333;
|
|
|
|
x = ( x | ( x << 1 ) ) & 0x55555555;
|
|
|
|
|
|
|
|
y = ( y | ( y << 8 ) ) & 0x00FF00FF;
|
|
|
|
y = ( y | ( y << 4 ) ) & 0x0F0F0F0F;
|
|
|
|
y = ( y | ( y << 2 ) ) & 0x33333333;
|
|
|
|
y = ( y | ( y << 1 ) ) & 0x55555555;
|
|
|
|
|
|
|
|
return x | ( y << 1 );
|
|
|
|
}
|
|
|
|
|
2024-03-20 22:47:44 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Outputs a list of vertices that have not yet been triangulated.
|
|
|
|
*/
|
|
|
|
void logRemaining()
|
|
|
|
{
|
|
|
|
std::set<VERTEX*> seen;
|
2024-03-21 01:00:42 +00:00
|
|
|
wxLog::EnableLogging();
|
2024-03-20 22:47:44 +00:00
|
|
|
for( VERTEX& p : m_vertices )
|
|
|
|
{
|
|
|
|
if( !p.next || p.next == &p || seen.find( &p ) != seen.end() )
|
|
|
|
continue;
|
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
logVertices( &p, &seen );
|
|
|
|
}
|
|
|
|
}
|
2024-03-20 22:47:44 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
void logVertices( VERTEX* aStart, std::set<VERTEX*>* aSeen )
|
|
|
|
{
|
|
|
|
if( aSeen && aSeen->count( aStart ) )
|
|
|
|
return;
|
2024-03-21 01:00:42 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
if( aSeen )
|
|
|
|
aSeen->insert( aStart );
|
2024-03-20 22:47:44 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
int count = 1;
|
|
|
|
VERTEX* p = aStart->next;
|
|
|
|
wxString msg = wxString::Format( "Vertices: %d,%d,", static_cast<int>( aStart->x ),
|
|
|
|
static_cast<int>( aStart->y ) );
|
2024-03-20 22:47:44 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
do
|
|
|
|
{
|
|
|
|
msg += wxString::Format( "%d,%d,", static_cast<int>( p->x ), static_cast<int>( p->y ) );
|
|
|
|
|
|
|
|
if( aSeen )
|
|
|
|
aSeen->insert( p );
|
|
|
|
|
|
|
|
p = p->next;
|
|
|
|
count++;
|
|
|
|
} while( p != aStart );
|
|
|
|
|
|
|
|
if( count < 3 ) // Don't log anything that only has 2 or fewer points
|
|
|
|
return;
|
|
|
|
|
|
|
|
msg.RemoveLast();
|
|
|
|
wxLogTrace( TRIANGULATE_TRACE, msg );
|
2024-03-20 22:47:44 +00:00
|
|
|
}
|
2024-03-26 00:24:50 +00:00
|
|
|
|
2024-04-01 17:45:26 +00:00
|
|
|
/**
|
|
|
|
* Simplify the line chain by removing points that are too close to each other.
|
|
|
|
* If no points are removed, it returns nullptr.
|
|
|
|
*/
|
|
|
|
VERTEX* simplifyList( VERTEX* aStart )
|
|
|
|
{
|
|
|
|
if( !aStart || aStart->next == aStart->prev )
|
|
|
|
return aStart;
|
|
|
|
|
|
|
|
VERTEX* p = aStart;
|
|
|
|
VERTEX* next = p->next;
|
|
|
|
VERTEX* retval = aStart;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
double sq_dist = ADVANCED_CFG::GetCfg().m_TriangulateSimplificationLevel;
|
|
|
|
sq_dist *= sq_dist;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
VECTOR2D diff = VECTOR2D( next->x - p->x, next->y - p->y );
|
|
|
|
|
|
|
|
if( diff.SquaredEuclideanNorm() < sq_dist )
|
|
|
|
{
|
|
|
|
if( next == aStart )
|
|
|
|
{
|
|
|
|
retval = p;
|
|
|
|
aStart->remove();
|
|
|
|
count++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
next = next->next;
|
|
|
|
p->next->remove();
|
|
|
|
count++;
|
|
|
|
retval = p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p = next;
|
|
|
|
next = next->next;
|
|
|
|
}
|
|
|
|
} while( p != aStart && next && p );
|
|
|
|
|
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "Removed %d points in simplifyList", count );
|
|
|
|
|
|
|
|
if( count )
|
|
|
|
return retval;
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Iterate through the list to remove NULL triangles if they exist.
|
|
|
|
*
|
2018-07-13 04:52:21 +00:00
|
|
|
* This should only be called as a last resort when tesselation fails
|
|
|
|
* as the NULL triangles are inserted as Steiner points to improve the
|
|
|
|
* triangulation regularity of polygons
|
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* removeNullTriangles( VERTEX* aStart )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* retval = nullptr;
|
2024-03-26 00:24:50 +00:00
|
|
|
size_t count = 0;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
2024-04-01 17:45:26 +00:00
|
|
|
if( ( retval = simplifyList( aStart ) ) )
|
|
|
|
aStart = retval;
|
|
|
|
|
|
|
|
wxASSERT( aStart->next && aStart->prev );
|
|
|
|
|
|
|
|
VERTEX* p = aStart->next;
|
|
|
|
|
|
|
|
while( p != aStart && p->next && p->prev )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-03-26 00:24:50 +00:00
|
|
|
// We make a dummy triangle that is actually part of the existing line segment
|
|
|
|
// and measure its area. This will not be exactly zero due to floating point
|
|
|
|
// errors. We then look for areas that are less than 4 times the area of the
|
|
|
|
// dummy triangle. For small triangles, this is a small number
|
|
|
|
VERTEX tmp( 0, 0.5 * ( p->prev->x + p->next->x ), 0.5 * ( p->prev->y + p->next->y ), this );
|
|
|
|
double null_area = 4.0 * std::abs( area( p->prev, &tmp, p->next ) );
|
|
|
|
|
|
|
|
if( *p == *( p->next ) || std::abs( area( p->prev, p, p->next ) ) <= null_area )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-03-21 01:00:42 +00:00
|
|
|
// This is a spike, remove it, leaving only one point
|
|
|
|
if( *( p->next ) == *( p->prev ) )
|
|
|
|
p->next->remove();
|
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
p = p->prev;
|
|
|
|
p->next->remove();
|
2024-04-01 17:45:26 +00:00
|
|
|
retval = p;
|
2024-03-26 00:24:50 +00:00
|
|
|
++count;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
|
|
|
if( p == p->next )
|
|
|
|
break;
|
2024-03-21 01:00:42 +00:00
|
|
|
|
2024-04-01 17:45:26 +00:00
|
|
|
// aStart was removed above, so we need to reset it
|
|
|
|
if( !aStart->next )
|
|
|
|
aStart = p->prev;
|
|
|
|
|
2024-03-21 01:00:42 +00:00
|
|
|
continue;
|
2018-07-13 04:52:21 +00:00
|
|
|
}
|
2024-01-18 19:05:45 +00:00
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
p = p->next;
|
|
|
|
};
|
|
|
|
|
2024-04-01 17:45:26 +00:00
|
|
|
/// We've removed all possible triangles
|
|
|
|
if( !p->next || p->next == p || p->next == p->prev )
|
|
|
|
return p;
|
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
// We needed an end point above that wouldn't be removed, so
|
|
|
|
// here we do the final check for this as a Steiner point
|
2024-04-01 17:45:26 +00:00
|
|
|
VERTEX tmp( 0, 0.5 * ( p->prev->x + p->next->x ),
|
|
|
|
0.5 * ( p->prev->y + p->next->y ), this );
|
|
|
|
double null_area = 4.0 * std::abs( area( p->prev, &tmp, p->next ) );
|
2024-03-26 00:24:50 +00:00
|
|
|
|
2024-04-01 17:45:26 +00:00
|
|
|
if( std::abs( area( p->prev, p, p->next ) ) <= null_area )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2019-01-18 15:13:50 +00:00
|
|
|
retval = p->next;
|
2018-07-13 04:52:21 +00:00
|
|
|
p->remove();
|
2024-03-26 00:24:50 +00:00
|
|
|
++count;
|
2018-07-13 04:52:21 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "Removed %zu NULL triangles", count );
|
2018-09-12 10:19:11 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
return retval;
|
2018-09-12 10:19:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Take a #SHAPE_LINE_CHAIN and links each point into a circular, doubly-linked list.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* createList( const SHAPE_LINE_CHAIN& points )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-03-26 00:24:50 +00:00
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "Creating list from %d points", points.PointCount() );
|
|
|
|
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* tail = nullptr;
|
2018-12-17 13:34:56 +00:00
|
|
|
double sum = 0.0;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
2018-12-17 13:34:56 +00:00
|
|
|
// Check for winding order
|
2018-07-13 04:52:21 +00:00
|
|
|
for( int i = 0; i < points.PointCount(); i++ )
|
2018-12-17 13:34:56 +00:00
|
|
|
{
|
|
|
|
VECTOR2D p1 = points.CPoint( i );
|
|
|
|
VECTOR2D p2 = points.CPoint( i + 1 );
|
|
|
|
|
2018-12-18 01:13:13 +00:00
|
|
|
sum += ( ( p2.x - p1.x ) * ( p2.y + p1.y ) );
|
2018-12-17 13:34:56 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
VECTOR2I last_pt{ std::numeric_limits<int>::max(), std::numeric_limits<int>::max() };
|
|
|
|
VECTOR2I::extended_type sq_dist = ADVANCED_CFG::GetCfg().m_TriangulateSimplificationLevel;
|
|
|
|
sq_dist *= sq_dist;
|
|
|
|
|
|
|
|
auto addVertex = [&]( int i )
|
|
|
|
{
|
|
|
|
const VECTOR2I& pt = points.CPoint( i );
|
|
|
|
VECTOR2I diff = pt - last_pt;
|
|
|
|
if( diff.SquaredEuclideanNorm() > sq_dist )
|
|
|
|
{
|
|
|
|
tail = insertVertex( pt, tail );
|
|
|
|
last_pt = pt;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-17 13:34:56 +00:00
|
|
|
if( sum > 0.0 )
|
2024-03-26 00:24:50 +00:00
|
|
|
{
|
|
|
|
for( int i = points.PointCount() - 1; i >= 0; i-- )
|
|
|
|
addVertex( i );
|
|
|
|
}
|
2018-12-17 13:34:56 +00:00
|
|
|
else
|
2024-03-26 00:24:50 +00:00
|
|
|
{
|
2018-12-17 13:34:56 +00:00
|
|
|
for( int i = 0; i < points.PointCount(); i++ )
|
2024-03-26 00:24:50 +00:00
|
|
|
addVertex( i );
|
|
|
|
}
|
2018-07-13 04:52:21 +00:00
|
|
|
|
|
|
|
if( tail && ( *tail == *tail->next ) )
|
|
|
|
tail->next->remove();
|
|
|
|
|
|
|
|
return tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Walk through a circular linked list starting at \a aPoint.
|
|
|
|
*
|
|
|
|
* For each point, test to see if the adjacent points form a triangle that is completely
|
|
|
|
* enclosed by the remaining polygon (an "ear" sticking off the polygon). If the three
|
|
|
|
* points form an ear, we log the ear's location and remove the center point from the
|
|
|
|
* linked list.
|
2018-07-13 04:52:21 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* This function can be called recursively in the case of difficult polygons. In cases
|
|
|
|
* where there is an intersection (not technically allowed by KiCad, but could exist in
|
|
|
|
* an edited file), we create a single triangle and remove both vertices before attempting
|
|
|
|
* to.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
bool earcutList( VERTEX* aPoint, int pass = 0 )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-03-26 00:24:50 +00:00
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "earcutList starting at %p for pass %d", aPoint, pass );
|
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
if( !aPoint )
|
2018-09-12 10:19:11 +00:00
|
|
|
return true;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* stop = aPoint;
|
|
|
|
VERTEX* prev;
|
|
|
|
VERTEX* next;
|
2024-03-26 00:24:50 +00:00
|
|
|
int internal_pass = 1;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
|
|
|
while( aPoint->prev != aPoint->next )
|
|
|
|
{
|
|
|
|
prev = aPoint->prev;
|
|
|
|
next = aPoint->next;
|
|
|
|
|
|
|
|
if( isEar( aPoint ) )
|
|
|
|
{
|
2024-03-21 16:54:50 +00:00
|
|
|
// Tiny ears cannot be seen on the screen
|
|
|
|
if( !isTooSmall( aPoint ) )
|
2024-03-26 00:24:50 +00:00
|
|
|
{
|
2024-03-21 16:54:50 +00:00
|
|
|
m_result.AddTriangle( prev->i, aPoint->i, next->i );
|
2024-03-26 00:24:50 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "Ignoring tiny ear with area %f",
|
|
|
|
area( prev, aPoint, next ) );
|
|
|
|
}
|
2024-03-21 16:54:50 +00:00
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
aPoint->remove();
|
|
|
|
|
|
|
|
// Skip one vertex as the triangle will account for the prev node
|
|
|
|
aPoint = next->next;
|
|
|
|
stop = next->next;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* nextNext = next->next;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
2024-01-18 16:34:11 +00:00
|
|
|
if( *prev != *nextNext && intersects( prev, aPoint, next, nextNext ) &&
|
|
|
|
locallyInside( prev, nextNext ) &&
|
|
|
|
locallyInside( nextNext, prev ) )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-03-26 00:24:50 +00:00
|
|
|
wxLogTrace( TRIANGULATE_TRACE,
|
|
|
|
"Local intersection detected. Merging minor triangle with area %f",
|
|
|
|
area( prev, aPoint, nextNext ) );
|
2024-01-18 16:34:11 +00:00
|
|
|
m_result.AddTriangle( prev->i, aPoint->i, nextNext->i );
|
2018-07-13 04:52:21 +00:00
|
|
|
|
2024-01-18 16:34:11 +00:00
|
|
|
// remove two nodes involved
|
|
|
|
next->remove();
|
|
|
|
aPoint->remove();
|
2018-07-13 04:52:21 +00:00
|
|
|
|
2024-01-18 16:34:11 +00:00
|
|
|
aPoint = nextNext;
|
|
|
|
stop = nextNext;
|
2023-12-13 21:35:10 +00:00
|
|
|
|
2024-01-18 16:34:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
2018-07-13 04:52:21 +00:00
|
|
|
|
|
|
|
aPoint = next;
|
|
|
|
|
2021-07-01 17:46:17 +00:00
|
|
|
/*
|
|
|
|
* We've searched the entire polygon for available ears and there are still
|
|
|
|
* un-sliced nodes remaining.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
2024-04-01 17:45:26 +00:00
|
|
|
if( aPoint == stop && aPoint->prev != aPoint->next )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-03-26 00:24:50 +00:00
|
|
|
VERTEX* newPoint;
|
|
|
|
|
|
|
|
// Removing null triangles will remove steiner points as well as colinear points
|
|
|
|
// that are three in a row. Because our next step is to subdivide the polygon,
|
|
|
|
// we need to allow it to add the subdivided points first. This is why we only
|
|
|
|
// run the RemoveNullTriangles function after the first pass.
|
|
|
|
if( ( internal_pass == 2 ) && ( newPoint = removeNullTriangles( aPoint ) ) )
|
2019-01-18 15:13:50 +00:00
|
|
|
{
|
2024-04-01 17:45:26 +00:00
|
|
|
// There are no remaining triangles in the list
|
|
|
|
if( newPoint->next == newPoint->prev )
|
|
|
|
break;
|
|
|
|
|
2019-01-18 15:13:50 +00:00
|
|
|
aPoint = newPoint;
|
|
|
|
stop = newPoint;
|
2018-07-13 04:52:21 +00:00
|
|
|
continue;
|
2019-01-18 15:13:50 +00:00
|
|
|
}
|
2018-07-13 04:52:21 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
++internal_pass;
|
|
|
|
|
|
|
|
// This will subdivide the polygon 2 times. The first pass will add enough points
|
|
|
|
// such that each edge is less than the average edge length. If this doesn't work
|
|
|
|
// The next pass will remove the null triangles (above) and subdivide the polygon
|
|
|
|
// again, this time adding one point to each long edge (and thereby changing the locations)
|
|
|
|
if( internal_pass < 4 )
|
|
|
|
{
|
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "Subdividing polygon" );
|
|
|
|
subdividePolygon( aPoint, internal_pass );
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
// If we don't have any NULL triangles left, cut the polygon in two and try again
|
2024-03-26 00:24:50 +00:00
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "Splitting polygon" );
|
|
|
|
|
2024-01-18 19:05:45 +00:00
|
|
|
if( !splitPolygon( aPoint ) )
|
|
|
|
return false;
|
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-12 19:13:14 +00:00
|
|
|
// Check to see if we are left with only three points in the polygon
|
|
|
|
if( aPoint->next && aPoint->prev == aPoint->next->next )
|
|
|
|
{
|
|
|
|
// Three concave points will never be able to be triangulated because they were
|
|
|
|
// created by an intersecting polygon, so just drop them.
|
|
|
|
if( area( aPoint->prev, aPoint, aPoint->next ) >= 0 )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-07-01 17:46:17 +00:00
|
|
|
/*
|
|
|
|
* At this point, our polygon should be fully tessellated.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
2024-03-21 01:00:42 +00:00
|
|
|
if( aPoint->prev != aPoint->next )
|
2024-03-22 18:08:38 +00:00
|
|
|
return std::abs( aPoint->area() ) > ADVANCED_CFG::GetCfg().m_TriangulateMinimumArea;
|
2024-03-21 01:00:42 +00:00
|
|
|
|
|
|
|
return true;
|
2018-07-13 04:52:21 +00:00
|
|
|
}
|
|
|
|
|
2024-03-21 16:54:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check whether a given vertex is too small to matter.
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool isTooSmall( const VERTEX* aPoint ) const
|
|
|
|
{
|
|
|
|
double min_area = ADVANCED_CFG::GetCfg().m_TriangulateMinimumArea;
|
|
|
|
double prev_sq_len = ( aPoint->prev->x - aPoint->x ) * ( aPoint->prev->x - aPoint->x ) +
|
|
|
|
( aPoint->prev->y - aPoint->y ) * ( aPoint->prev->y - aPoint->y );
|
|
|
|
double next_sq_len = ( aPoint->next->x - aPoint->x ) * ( aPoint->next->x - aPoint->x ) +
|
|
|
|
( aPoint->next->y - aPoint->y ) * ( aPoint->next->y - aPoint->y );
|
|
|
|
double opp_sq_len = ( aPoint->next->x - aPoint->prev->x ) * ( aPoint->next->x - aPoint->prev->x ) +
|
|
|
|
( aPoint->next->y - aPoint->prev->y ) * ( aPoint->next->y - aPoint->prev->y );
|
|
|
|
|
|
|
|
return ( prev_sq_len < min_area || next_sq_len < min_area || opp_sq_len < min_area );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Check whether the given vertex is in the middle of an ear.
|
|
|
|
*
|
|
|
|
* This works by walking forward and backward in zOrder to the limits of the minimal
|
|
|
|
* bounding box formed around the triangle, checking whether any points are located
|
|
|
|
* inside the given triangle.
|
2018-07-13 04:52:21 +00:00
|
|
|
*
|
2021-07-01 17:46:17 +00:00
|
|
|
* @return true if aEar is the apex point of a ear in the polygon.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
bool isEar( VERTEX* aEar ) const
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-01-12 17:09:52 +00:00
|
|
|
const VERTEX* a = aEar->prev;
|
|
|
|
const VERTEX* b = aEar;
|
|
|
|
const VERTEX* c = aEar->next;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
|
|
|
// If the area >=0, then the three points for a concave sequence
|
|
|
|
// with b as the reflex point
|
|
|
|
if( area( a, b, c ) >= 0 )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// triangle bbox
|
|
|
|
const double minTX = std::min( a->x, std::min( b->x, c->x ) );
|
|
|
|
const double minTY = std::min( a->y, std::min( b->y, c->y ) );
|
|
|
|
const double maxTX = std::max( a->x, std::max( b->x, c->x ) );
|
|
|
|
const double maxTY = std::max( a->y, std::max( b->y, c->y ) );
|
|
|
|
|
|
|
|
// z-order range for the current triangle bounding box
|
|
|
|
const int32_t minZ = zOrder( minTX, minTY );
|
|
|
|
const int32_t maxZ = zOrder( maxTX, maxTY );
|
|
|
|
|
|
|
|
// first look for points inside the triangle in increasing z-order
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* p = aEar->nextZ;
|
2018-07-13 04:52:21 +00:00
|
|
|
|
|
|
|
while( p && p->z <= maxZ )
|
|
|
|
{
|
|
|
|
if( p != a && p != c
|
|
|
|
&& p->inTriangle( *a, *b, *c )
|
|
|
|
&& area( p->prev, p, p->next ) >= 0 )
|
|
|
|
return false;
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
p = p->nextZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
// then look for points in decreasing z-order
|
|
|
|
p = aEar->prevZ;
|
|
|
|
|
|
|
|
while( p && p->z >= minZ )
|
|
|
|
{
|
|
|
|
if( p != a && p != c
|
|
|
|
&& p->inTriangle( *a, *b, *c )
|
|
|
|
&& area( p->prev, p, p->next ) >= 0 )
|
|
|
|
return false;
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
p = p->prevZ;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
/**
|
|
|
|
* Inserts a new vertex halfway between each existing pair of vertices.
|
|
|
|
*/
|
|
|
|
void subdividePolygon( VERTEX* aStart, int pass = 0 )
|
|
|
|
{
|
|
|
|
VERTEX* p = aStart;
|
|
|
|
|
|
|
|
struct VertexComparator {
|
|
|
|
bool operator()(const std::pair<VERTEX*,double>& a, const std::pair<VERTEX*,double>& b) const {
|
|
|
|
return a.second > b.second;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::set<std::pair<VERTEX*,double>, VertexComparator> longest;
|
|
|
|
double avg = 0.0;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
double len = ( p->x - p->next->x ) * ( p->x - p->next->x ) +
|
|
|
|
( p->y - p->next->y ) * ( p->y - p->next->y );
|
|
|
|
longest.emplace( p, len );
|
|
|
|
|
|
|
|
avg += len;
|
|
|
|
p = p->next;
|
|
|
|
} while (p != aStart);
|
|
|
|
|
|
|
|
avg /= longest.size();
|
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "Average length: %f", avg );
|
|
|
|
|
|
|
|
for( auto it = longest.begin(); it != longest.end() && it->second > avg; ++it )
|
|
|
|
{
|
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "Subdividing edge with length %f", it->second );
|
|
|
|
VERTEX* a = it->first;
|
|
|
|
VERTEX* b = a->next;
|
|
|
|
VERTEX* last = a;
|
|
|
|
|
|
|
|
// We adjust the number of divisions based on the pass in order to progressively
|
|
|
|
// subdivide the polygon when triangulation fails
|
|
|
|
int divisions = avg / it->second + 2 + pass;
|
|
|
|
double step = 1.0 / divisions;
|
|
|
|
|
|
|
|
for( int i = 1; i < divisions; i++ )
|
|
|
|
{
|
|
|
|
double x = a->x * ( 1.0 - step * i ) + b->x * ( step * i );
|
|
|
|
double y = a->y * ( 1.0 - step * i ) + b->y * ( step * i );
|
|
|
|
last = insertVertex( VECTOR2I( x, y ), last );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// update z-order of the vertices
|
|
|
|
aStart->updateList();
|
|
|
|
}
|
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
/**
|
|
|
|
* If we cannot find an ear to slice in the current polygon list, we
|
|
|
|
* use this to split the polygon into two separate lists and slice them each
|
|
|
|
* independently. This is assured to generate at least one new ear if the
|
|
|
|
* split is successful
|
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
bool splitPolygon( VERTEX* start )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* origPoly = start;
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
// If we have fewer than 4 points, we cannot split the polygon
|
|
|
|
if( !start || !start->next || start->next == start->prev
|
|
|
|
|| start->next->next == start->prev )
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-02-12 23:19:01 +00:00
|
|
|
// Our first attempts to split the polygon will be at overlapping points.
|
|
|
|
// These are natural split points and we only need to switch the loop directions
|
|
|
|
// to generate two new loops. Since they are overlapping, we are do not
|
|
|
|
// need to create a new segment to disconnect the two loops.
|
|
|
|
do
|
|
|
|
{
|
2024-03-26 00:24:50 +00:00
|
|
|
std::vector<VERTEX*> overlapPoints;
|
|
|
|
VERTEX* z_pt = origPoly;
|
2024-02-12 23:19:01 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
while ( z_pt->prevZ && *z_pt->prevZ == *origPoly )
|
|
|
|
z_pt = z_pt->prevZ;
|
2024-02-12 23:19:01 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
overlapPoints.push_back( z_pt );
|
2024-02-12 23:19:01 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
while( z_pt->nextZ && *z_pt->nextZ == *origPoly )
|
|
|
|
{
|
|
|
|
z_pt = z_pt->nextZ;
|
|
|
|
overlapPoints.push_back( z_pt );
|
|
|
|
}
|
2024-02-12 23:19:01 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
if( overlapPoints.size() != 2 || overlapPoints[0]->next == overlapPoints[1]
|
|
|
|
|| overlapPoints[0]->prev == overlapPoints[1] )
|
2024-02-12 23:19:01 +00:00
|
|
|
{
|
2024-03-26 00:24:50 +00:00
|
|
|
origPoly = origPoly->next;
|
|
|
|
continue;
|
|
|
|
}
|
2024-02-12 23:19:01 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
if( overlapPoints[0]->area( overlapPoints[1] ) < 0 || overlapPoints[1]->area( overlapPoints[0] ) < 0 )
|
|
|
|
{
|
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "Split generated a hole, skipping" );
|
|
|
|
origPoly = origPoly->next;
|
|
|
|
continue;
|
2024-02-12 23:19:01 +00:00
|
|
|
}
|
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "Splitting at overlap point %f, %f", overlapPoints[0]->x, overlapPoints[0]->y );
|
|
|
|
std::swap( overlapPoints[0]->next, overlapPoints[1]->next );
|
|
|
|
overlapPoints[0]->next->prev = overlapPoints[0];
|
|
|
|
overlapPoints[1]->next->prev = overlapPoints[1];
|
|
|
|
|
|
|
|
overlapPoints[0]->updateList();
|
|
|
|
overlapPoints[1]->updateList();
|
|
|
|
logVertices( overlapPoints[0], nullptr );
|
|
|
|
logVertices( overlapPoints[1], nullptr );
|
|
|
|
bool retval = earcutList( overlapPoints[0] ) && earcutList( overlapPoints[1] );
|
|
|
|
|
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "%s at first overlap split", retval ? "Success" : "Failed" );
|
|
|
|
return retval;
|
|
|
|
|
2024-02-12 23:19:01 +00:00
|
|
|
|
|
|
|
} while ( origPoly != start );
|
|
|
|
|
|
|
|
// If we've made it through the split algorithm and we still haven't found a
|
|
|
|
// set of overlapping points, we need to create a new segment to split the polygon
|
|
|
|
// into two separate polygons. We do this by finding the two vertices that form
|
|
|
|
// a valid line (does not cross the existing polygon)
|
2018-07-13 04:52:21 +00:00
|
|
|
do
|
|
|
|
{
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* marker = origPoly->next->next;
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
while( marker != origPoly->prev )
|
|
|
|
{
|
|
|
|
// Find a diagonal line that is wholly enclosed by the polygon interior
|
2024-03-26 00:24:50 +00:00
|
|
|
if( origPoly->next && origPoly->i != marker->i && goodSplit( origPoly, marker ) )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* newPoly = origPoly->split( marker );
|
2018-07-13 04:52:21 +00:00
|
|
|
|
2024-01-18 16:34:11 +00:00
|
|
|
origPoly->updateList();
|
|
|
|
newPoly->updateList();
|
2018-07-13 04:52:21 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
bool retval = earcutList( origPoly ) && earcutList( newPoly );
|
|
|
|
|
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "%s at split", retval ? "Success" : "Failed" );
|
|
|
|
return retval;
|
2018-07-13 04:52:21 +00:00
|
|
|
}
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
marker = marker->next;
|
|
|
|
}
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
origPoly = origPoly->next;
|
|
|
|
} while( origPoly != start );
|
2024-01-18 19:05:45 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
wxLogTrace( TRIANGULATE_TRACE, "Could not find a valid split point" );
|
2024-01-18 19:05:45 +00:00
|
|
|
return false;
|
2018-07-13 04:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a segment joining two vertices lies fully inside the polygon.
|
|
|
|
* To do this, we first ensure that the line isn't along the polygon edge.
|
|
|
|
* Next, we know that if the line doesn't intersect the polygon, then it is
|
2024-01-18 19:05:45 +00:00
|
|
|
* either fully inside or fully outside the polygon. Next, we ensure that
|
|
|
|
* the proposed split is inside the local area of the polygon at both ends
|
|
|
|
* and the midpoint. Finally, we check to split creates two new polygons,
|
|
|
|
* each with positive area.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
bool goodSplit( const VERTEX* a, const VERTEX* b ) const
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-01-18 19:05:45 +00:00
|
|
|
bool a_on_edge = ( a->nextZ && *a == *a->nextZ ) || ( a->prevZ && *a == *a->prevZ );
|
|
|
|
bool b_on_edge = ( b->nextZ && *b == *b->nextZ ) || ( b->prevZ && *b == *b->prevZ );
|
|
|
|
bool no_intersect = a->next->i != b->i && a->prev->i != b->i && !intersectsPolygon( a, b );
|
|
|
|
bool local_split = locallyInside( a, b ) && locallyInside( b, a ) && middleInside( a, b );
|
|
|
|
bool same_dir = area( a->prev, a, b->prev ) != 0.0 || area( a, b->prev, b ) != 0.0;
|
|
|
|
bool has_len = ( *a == *b ) && area( a->prev, a, a->next ) > 0 && area( b->prev, b, b->next ) > 0;
|
2024-03-26 00:24:50 +00:00
|
|
|
bool pos_area = a->area( b ) > 0 && b->area( a ) > 0;
|
2024-01-18 19:05:45 +00:00
|
|
|
|
2024-03-26 00:24:50 +00:00
|
|
|
return no_intersect && local_split && ( same_dir || has_len ) && !a_on_edge && !b_on_edge && pos_area;
|
2024-01-18 19:05:45 +00:00
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Return the twice the signed area of the triangle formed by vertices p, q, and r.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
double area( const VERTEX* p, const VERTEX* q, const VERTEX* r ) const
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
|
|
|
return ( q->y - p->y ) * ( r->x - q->x ) - ( q->x - p->x ) * ( r->y - q->y );
|
|
|
|
}
|
|
|
|
|
2024-01-18 19:05:45 +00:00
|
|
|
|
|
|
|
constexpr int sign( double aVal ) const
|
|
|
|
{
|
|
|
|
return ( aVal > 0 ) - ( aVal < 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If p, q, and r are collinear and r lies between p and q, then return true.
|
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
constexpr bool overlapping( const VERTEX* p, const VERTEX* q, const VERTEX* r ) const
|
2024-01-18 19:05:45 +00:00
|
|
|
{
|
|
|
|
return q->x <= std::max( p->x, r->x ) &&
|
|
|
|
q->x >= std::min( p->x, r->x ) &&
|
|
|
|
q->y <= std::max( p->y, r->y ) &&
|
|
|
|
q->y >= std::min( p->y, r->y );
|
|
|
|
}
|
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Check for intersection between two segments, end points included.
|
|
|
|
*
|
|
|
|
* @return true if p1-p2 intersects q1-q2.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
bool intersects( const VERTEX* p1, const VERTEX* q1, const VERTEX* p2, const VERTEX* q2 ) const
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-01-18 19:05:45 +00:00
|
|
|
int sign1 = sign( area( p1, q1, p2 ) );
|
|
|
|
int sign2 = sign( area( p1, q1, q2 ) );
|
|
|
|
int sign3 = sign( area( p2, q2, p1 ) );
|
|
|
|
int sign4 = sign( area( p2, q2, q1 ) );
|
|
|
|
|
|
|
|
if( sign1 != sign2 && sign3 != sign4 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( sign1 == 0 && overlapping( p1, p2, q1 ) )
|
2018-07-13 04:52:21 +00:00
|
|
|
return true;
|
|
|
|
|
2024-01-18 19:05:45 +00:00
|
|
|
if( sign2 == 0 && overlapping( p1, q2, q1 ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( sign3 == 0 && overlapping( p2, p1, q2 ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( sign4 == 0 && overlapping( p2, q1, q2 ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
|
|
return false;
|
2018-07-13 04:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Check whether the segment from vertex a -> vertex b crosses any of the segments
|
2018-07-13 04:52:21 +00:00
|
|
|
* of the polygon of which vertex a is a member.
|
2021-07-01 17:46:17 +00:00
|
|
|
*
|
|
|
|
* @return true if the segment intersects the edge of the polygon.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
bool intersectsPolygon( const VERTEX* a, const VERTEX* b ) const
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-04-02 18:19:18 +00:00
|
|
|
for( size_t ii = 0; ii < m_vertices_original_size; ii++ )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
2024-04-02 18:19:18 +00:00
|
|
|
const VERTEX* p = &m_vertices[ii];
|
|
|
|
const VERTEX* q = &m_vertices[( ii + 1 ) % m_vertices_original_size];
|
2024-03-26 00:24:50 +00:00
|
|
|
|
|
|
|
if( p->i == a->i || p->i == b->i || q->i == a->i || q->i == b->i )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if( intersects( p, q, a, b ) )
|
2018-07-13 04:52:21 +00:00
|
|
|
return true;
|
2024-03-26 00:24:50 +00:00
|
|
|
}
|
2018-07-13 04:52:21 +00:00
|
|
|
|
2024-04-02 18:19:18 +00:00
|
|
|
return false;
|
2018-07-13 04:52:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Check whether the segment from vertex a -> vertex b is inside the polygon
|
|
|
|
* around the immediate area of vertex a.
|
|
|
|
*
|
|
|
|
* We don't define the exact area over which the segment is inside but it is guaranteed to
|
|
|
|
* be inside the polygon immediately adjacent to vertex a.
|
|
|
|
*
|
|
|
|
* @return true if the segment from a->b is inside a's polygon next to vertex a.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
bool locallyInside( const VERTEX* a, const VERTEX* b ) const
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
|
|
|
if( area( a->prev, a, a->next ) < 0 )
|
|
|
|
return area( a, b, a->next ) >= 0 && area( a, a->prev, b ) >= 0;
|
|
|
|
else
|
|
|
|
return area( a, b, a->prev ) < 0 || area( a, a->next, b ) < 0;
|
|
|
|
}
|
|
|
|
|
2024-01-18 19:05:45 +00:00
|
|
|
/**
|
|
|
|
* Check to see if the segment halfway point between a and b is inside the polygon
|
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
bool middleInside( const VERTEX* a, const VERTEX* b ) const
|
2024-01-18 19:05:45 +00:00
|
|
|
{
|
2024-01-12 17:09:52 +00:00
|
|
|
const VERTEX* p = a;
|
2024-01-18 19:05:45 +00:00
|
|
|
bool inside = false;
|
|
|
|
double px = ( a->x + b->x ) / 2;
|
|
|
|
double py = ( a->y + b->y ) / 2;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( ( ( p->y > py ) != ( p->next->y > py ) )
|
|
|
|
&& ( px < ( p->next->x - p->x ) * ( py - p->y ) / ( p->next->y - p->y ) + p->x ) )
|
|
|
|
inside = !inside;
|
|
|
|
|
|
|
|
p = p->next;
|
|
|
|
} while( p != a );
|
|
|
|
|
|
|
|
return inside;
|
|
|
|
}
|
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
/**
|
2021-07-01 17:46:17 +00:00
|
|
|
* Create an entry in the vertices lookup and optionally inserts the newly created vertex
|
|
|
|
* into an existing linked list.
|
|
|
|
*
|
|
|
|
* @return a pointer to the newly created vertex.
|
2018-07-13 04:52:21 +00:00
|
|
|
*/
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* insertVertex( const VECTOR2I& pt, VERTEX* last )
|
2018-07-13 04:52:21 +00:00
|
|
|
{
|
|
|
|
m_result.AddVertex( pt );
|
|
|
|
m_vertices.emplace_back( m_result.GetVertexCount() - 1, pt.x, pt.y, this );
|
|
|
|
|
2024-01-12 17:09:52 +00:00
|
|
|
VERTEX* p = &m_vertices.back();
|
2021-07-01 17:46:17 +00:00
|
|
|
|
2018-07-13 04:52:21 +00:00
|
|
|
if( !last )
|
|
|
|
{
|
|
|
|
p->prev = p;
|
|
|
|
p->next = p;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
p->next = last->next;
|
|
|
|
p->prev = last;
|
|
|
|
last->next->prev = p;
|
|
|
|
last->next = p;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2020-12-12 03:43:16 +00:00
|
|
|
private:
|
|
|
|
BOX2I m_bbox;
|
2024-01-12 17:09:52 +00:00
|
|
|
std::deque<VERTEX> m_vertices;
|
2024-04-02 18:19:18 +00:00
|
|
|
size_t m_vertices_original_size;
|
2020-12-12 03:43:16 +00:00
|
|
|
SHAPE_POLY_SET::TRIANGULATED_POLYGON& m_result;
|
2018-07-13 04:52:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif //__POLYGON_TRIANGULATION_H
|