From 08d4e91f3bee4de233ae38b16f3439cd3f0c9331 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Thu, 10 Sep 2020 09:28:52 -0700 Subject: [PATCH] Change partitions to fixed size Partititioning small polygons causes excessive partitions when we use a fixed number of cells per side. Partitioning by size keeps the partition count limited and speeds the calculations. Also adds an option to not partition the grid for elements (like 3d raytracing) that do not need it. Fixes https://gitlab.com/kicad/code/kicad/issues/5579 --- .../shapes2D/ctriangle2d.cpp | 2 +- libs/kimath/include/geometry/shape_poly_set.h | 6 +++--- libs/kimath/src/geometry/shape_poly_set.cpp | 18 +++++++++++------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/3d-viewer/3d_rendering/3d_render_raytracing/shapes2D/ctriangle2d.cpp b/3d-viewer/3d_rendering/3d_render_raytracing/shapes2D/ctriangle2d.cpp index b744cad33b..a359c50a77 100644 --- a/3d-viewer/3d_rendering/3d_render_raytracing/shapes2D/ctriangle2d.cpp +++ b/3d-viewer/3d_rendering/3d_render_raytracing/shapes2D/ctriangle2d.cpp @@ -132,7 +132,7 @@ void Convert_shape_line_polygon_to_triangles( SHAPE_POLY_SET &aPolyList, const BOARD_ITEM &aBoardItem ) { - aPolyList.CacheTriangulation(); + aPolyList.CacheTriangulation( false ); const double conver_d = (double)aBiuTo3DunitsScale; for( unsigned int j = 0; j < aPolyList.TriangulatedPolyCount(); j++ ) diff --git a/libs/kimath/include/geometry/shape_poly_set.h b/libs/kimath/include/geometry/shape_poly_set.h index cba1ef8042..1f71dd43c8 100644 --- a/libs/kimath/include/geometry/shape_poly_set.h +++ b/libs/kimath/include/geometry/shape_poly_set.h @@ -99,7 +99,7 @@ class SHAPE_POLY_SET : public SHAPE return bbox; } - virtual const VECTOR2I GetPoint( int aIndex ) const override + virtual const VECTOR2I GetPoint( int aIndex ) const override { switch(aIndex) { @@ -165,7 +165,7 @@ class SHAPE_POLY_SET : public SHAPE return m_triangles.size(); } - size_t GetVertexCount() const + size_t GetVertexCount() const { return m_vertices.size(); } @@ -1365,7 +1365,7 @@ class SHAPE_POLY_SET : public SHAPE SHAPE_POLY_SET& operator=( const SHAPE_POLY_SET& ); - void CacheTriangulation(); + void CacheTriangulation( bool aPartition = true ); bool IsTriangulationUpToDate() const; MD5_HASH GetHash() const; diff --git a/libs/kimath/src/geometry/shape_poly_set.cpp b/libs/kimath/src/geometry/shape_poly_set.cpp index 52ca2102ee..0e6406dba9 100644 --- a/libs/kimath/src/geometry/shape_poly_set.cpp +++ b/libs/kimath/src/geometry/shape_poly_set.cpp @@ -1903,7 +1903,7 @@ bool SHAPE_POLY_SET::IsTriangulationUpToDate() const static void partitionPolyIntoRegularCellGrid( - const SHAPE_POLY_SET& aPoly, int aCells, SHAPE_POLY_SET& aOut ) + const SHAPE_POLY_SET& aPoly, int aSize, SHAPE_POLY_SET& aOut ) { BOX2I bb = aPoly.BBox(); @@ -1917,13 +1917,13 @@ static void partitionPolyIntoRegularCellGrid( if( w > h ) { - n_cells_x = aCells; - n_cells_y = (int) floor( h / w * (double) aCells ) + 1; + n_cells_x = w / aSize; + n_cells_y = floor( h / w * n_cells_x ) + 1; } else { - n_cells_x = (int) floor( w / h * (double) aCells ) + 1; - n_cells_y = aCells; + n_cells_y = h / aSize; + n_cells_x = floor( w / h * n_cells_y ) + 1; } SHAPE_POLY_SET ps1( aPoly ), ps2( aPoly ), maskSetOdd, maskSetEven; @@ -1968,7 +1968,7 @@ static void partitionPolyIntoRegularCellGrid( } -void SHAPE_POLY_SET::CacheTriangulation() +void SHAPE_POLY_SET::CacheTriangulation( bool aPartition ) { bool recalculate = !m_hash.IsValid(); MD5_HASH hash; @@ -1992,7 +1992,11 @@ void SHAPE_POLY_SET::CacheTriangulation() SHAPE_POLY_SET tmpSet; - partitionPolyIntoRegularCellGrid( *this, 20, tmpSet ); + if( aPartition ) + // This partitions into regularly-sized grids (1cm in pcbnew) + partitionPolyIntoRegularCellGrid( *this, 1e7, tmpSet ); + else + tmpSet = *this; m_triangulatedPolys.clear(); m_triangulationValid = true;