Commit Graph

385 Commits

Author SHA1 Message Date
Seth Hillbrand bcf6b620a8 Large rework of BEZIER_POLY
Add direct handling of quadratic beziers to save compute time and number
of points.  Update cubic interpolation to reduce number of points
generated for a given smoothness

Cache data on open and used cached data to avoid multiple re-calcs

Remove minimum line length and number of segments and replace with
standard max error level.  Allows us to specify the tolerance of bezier
interpolation
2024-06-18 17:55:41 -07:00
Seth Hillbrand e3231e6996 Rework VECTOR2D promotion rules
VECTOR + scalar = vector type (KiROUND when VECTOR is integral and
scalar is float)
VECTOR + VECTOR = common type vector

Implemented with concepts in template rules - SWIG compatible
2024-06-15 13:41:07 -07:00
Seth Hillbrand f7450e5b37 Fix SWIG compile
SWIG doesn't like a lot of the new C++20 elements, so fall back to basic
cast that should get almost every case
2024-06-14 17:41:05 -07:00
Seth Hillbrand 6df16d053e Add specialization for unsigned scalar add/sub
Adding/subtracting an unsigned scalar to a signed vector should maintain
the same signed vector

Fixes https://gitlab.com/kicad/code/kicad/-/issues/18212
2024-06-14 17:27:59 -07:00
Alex Shvartzkop 9d0ee029bc Add a upper >= lower check to Clamp.
Similar to std::clamp.
2024-06-12 23:05:33 +03:00
Alex Shvartzkop e49d233923 Move BOX2I_MINMAX to separate header. 2024-06-12 23:05:33 +03:00
Seth Hillbrand 9d9028c979 Handle unsigned VECTOR2 casts
The previous overflow handling code casted the int_min value into an
unsigned, meaning that the min value and max value were almost the same,
clamping the output to unreasonable values.

Updated code handles floating points first, then does integer casting
through int64_t
2024-06-10 12:40:01 -07:00
Seth Hillbrand 31a0652c58 Force VECTOR2 templates to use standard promotion rules
This forces the operators -, +, * to use standard promotion rules when
operating on vectors of two different types.  Previously, this depended
on the order in which the operator was called, so subtracting a VECTOR2D
from a VECTOR2I could have a different result than negating the result
of subtracting the same vectors in the opposite order
2024-06-07 15:32:07 -07:00
Seth Hillbrand 590bd6237d Fix off-by-one error in distance check for arcs 2024-06-03 16:29:06 -07:00
Seth Hillbrand e8c96a8a20 Fixups for a9ae86ee
Correcting a couple mistakes and clarifying rounding
2024-05-31 17:31:50 -07:00
Seth Hillbrand 382477c622 Fix sin/cos fixed values 2024-05-31 17:10:36 -07:00
Seth Hillbrand 2f9b10e379 Revert "Add additional 45° shortcuts for sin/cos"
Needs more testing

This reverts commit e83ca5e91d.
2024-05-31 14:40:10 -07:00
Seth Hillbrand e83ca5e91d Add additional 45° shortcuts for sin/cos 2024-05-31 13:27:52 -07:00
Seth Hillbrand a9ae86eefd Cleanup geometry functions
Added Distance(VECTOR2) function that returns a double.  Removed
superfluous EuclideanNorm, GetLineLength, integer constructor for
EDA_ANGLE (this promotes to double in the CTOR), DistanceLinePoint and
HitTestPoints

Also extended the size for arc calculations that get distances to center
points to avoid overflow
2024-05-31 12:26:37 -07:00
Seth Hillbrand 75ab3d9e8b Handle arcs dp coupling
Calculate coupled arcs and spacing

Fixes https://gitlab.com/kicad/code/kicad/-/issues/17967
2024-05-30 16:11:12 -07:00
Seth Hillbrand 429625e311 Unify SHAPE_ARC clockwise calc.
We don't really need the extra angle calculations
2024-05-30 14:19:57 -07:00
Seth Hillbrand 57c0953c39 Return closest point on line-circle intersection
Don't only look for the segment endpoints when asking the closest point.
If we have a collision, we need to get the collision point
2024-05-21 09:34:45 -07:00
Seth Hillbrand 5e5741edd3 Minor optimizations
Most of the lines in KiCad are on 45° angles.  We can avoid sqrts by
recognizing that this implies the x/y values are the same.  Therefore,
the length of the hypotenuse is sqrt(2) * x.
2024-05-16 11:50:48 -07:00
John Beard fa7e842c8c Rework item distribution
This splits the tool into two separate tools: by center and
by even gaps. Previously, this was automatically decided, based on
if the items could have any gaps between them. This was unintuitive
as it would appear to arrange by centre point sometimes but not others.
When items aren't all the same width, the results can then be very
different, based only on the starting positions.

The new behaviour is to have a dedicated tool for each, which echos
how graphical programs like Inkscape manage this.

The by-gaps method is then extended to work for overlapping items
(when items overlap, the overlaps are made equal). The logic is
centralised in kimath/geometry, and some QA is added. This should
make it easier to extend to eeschema, for example.

This also (attempts to) address some rounding issues which could
cause minor, but compounding, errors to build up along the list
of items.

Also, fix bugs in the collection filtering - previously items
like markers were filtered out only after the selection size
was used to compute the gaps between items.
2024-05-05 03:35:09 +08:00
Jeff Young c59ed0bbb7 Flip symbol editor's Y axis to match other editors. 2024-04-27 13:47:56 +01:00
Alex Shvartzkop cf6d30706b More clang build fixes. 2024-04-23 21:50:22 +03:00
Alex Shvartzkop 124b511037 Fix build on clang. 2024-04-23 21:44:55 +03:00
Alex Shvartzkop 865e3a9f3c Improvements when working closer to 32-bit integer limits.
- Changes BOX2 size to extended coordinates.
- Adds BOX2ISafe to construct a BOX2I that will fit in int32.
- Adds more checks.
- Safer computations.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/17670
2024-04-23 16:34:07 +00:00
Alex Shvartzkop 5e70883ef6 Add self-intersection check algo for arcs in SHAPE_LINE_CHAIN. 2024-04-23 19:25:23 +03:00
Alex Shvartzkop fcdaa4a59e Cache center point and radius along with BBox in SHAPE_ARC. 2024-04-23 19:25:23 +03:00
Jeff Young e8be782687 Mostly clean-up, but also bug fixes with bounding boxes. 2024-04-22 11:16:14 +01:00
Seth Hillbrand 59f99d9a6a Re-add routine to remove duplicate points
The router needs to elimitate duplicate points but keep colinear
segments.  This re-creates the partial routine from the old Simplify()
function

Fixes https://gitlab.com/kicad/code/kicad/-/issues/17582
2024-04-10 17:25:58 -07:00
Seth Hillbrand 8260c174e9 Prevent OOB access in polygon split
Check for vertex placement against original size.  Since we are
potentially adding vertices that are out of order with the original
polygon, we only want to check against the first set of vertices when
checking for polygon collision

Fixes https://gitlab.com/kicad/code/kicad/-/issues/17664
2024-04-02 11:22:59 -07:00
Seth Hillbrand 553e4a25bf Tidy up some remaining triangulation cases
Found and fixed a few corner cases where triangulation point removal
could reset expected points
2024-04-01 11:48:12 -07:00
Seth Hillbrand 06b199fd41 Do not use cache when we modify vertices
When we add vertices to the tesselation routines, we cannot reuse these
without the original vertex points.

It may be possible to copy and modify the vertices from the hint data so
that they are properly positioned but naive attempts (moving based on
first point) did not work, so for now, we disable the hint cache when
the vertex sizes do not match as this prevents OOB access

Fixes https://gitlab.com/kicad/code/kicad/-/issues/17621
2024-03-31 15:00:46 -07:00
Seth Hillbrand 6c05f2c67c Move logging message after vertex check
When creating the list, we may have fewer than needed vertices, which
prevents calculating the area
2024-03-29 16:47:07 -07:00
Seth Hillbrand c3f6a84d66 Update triangulation to handle poly-intersection
Polygon intersections happen against the original outline, not against
the currently remaining polygon.  This avoids pathalogical cases

Adds new simplification system to avoid duplicated points
Adds new edge-splitting algorithm to provide additional fall-back
Verifies that polygon cuts do not swap holes for outlines (negative
area)

Fixes https://gitlab.com/kicad/code/kicad/-/issues/17559
2024-03-28 10:16:24 -07:00
Seth Hillbrand a793fa8b17 Fix typo in triangulation return 2024-03-22 11:08:38 -07:00
Seth Hillbrand a58e7b37ff Skip small triangles
The minor triangles cost the same amount of compute time as larger ones
but do not have a material effect on the zone display.  This skips these
minor triangles when earcutting
2024-03-21 13:57:57 -07:00
Seth Hillbrand 7e7fec69f6 Add logging and area check to tesselation
Logging is useful when we find an area that cannot be triangulated.
This will be used to generated test cases.

Skipping minor untesselated areas means that the polygon will still be
considered fully tesselated (and not sent back again and again) even if
the tesselation misses an area less than the configured limit.
Currently, this is 31^2nm.
2024-03-21 13:57:57 -07:00
Seth Hillbrand 4f03bb2fb6 Update triangulation 2024-03-21 13:57:57 -07:00
StefanBruens e07b4ce8e4 Fix triangulationValid check race for zone fill
The m_triangulationValid flag is used in several places without holding
the mutex, thus it should only ever be set when the triangulation is
guaranteed to be valid.

This can either be done by protecting both data and flag by the same
mutex, or updating the flag only after the triangulation has finished.

Also fix the case when the triangulation actually fails, the flag should
not be set in this case.

While at it, simplify the recalculation check. Only if both the
triangulation is valid, and the data hash is unchanged the recalculation
can be skipped - this is typically the case when two threads try to
update the cache concurrently, the second one will block at the mutex,
and will see the valid data after the first thread has finished.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/17180
2024-03-14 22:26:23 +00:00
Seth Hillbrand 81cb6d0c3f Fix race condition in zone fill
When checking collisions, the SHAPE_POLY_SET::Collide() routine is not
const because it will regenerate the triangulation cache if it is out of
date (using a const_cast, grrr).  This sidesteps the issue by assigning
a mutex to the triangulation caching

Fixes https://gitlab.com/kicad/code/kicad/-/issues/17180
2024-03-13 10:35:40 -07:00
Alex Shvartzkop 4d66a8ebdb Fix rendering/plotting of arcs with tiny angle and huge radius.
Fixes https://gitlab.com/kicad/code/kicad/-/issues/17343
2024-03-09 02:07:24 +03:00
Roberto Fernandez Bautista 0d340d0610 SHAPE_LINE_CHAIN: Move .h definitions to .cpp
No functional changes, just allow faster rebuilds
2024-03-02 12:09:09 +00:00
Roberto Fernandez Bautista 8c22f8df31 SHAPE_LINE_CHAIN: Fix/unify constructors 2024-03-02 12:09:09 +00:00
Jeff Young ffa6adec96 Add a cache for TrueType contours and triangulation data.
Also returns minimumSegmentLength to its former value as
it doesn't appear to be required to fix
https://gitlab.com/kicad/code/kicad/-/issues/11463.

Fixes: https://gitlab.com/kicad/code/kicad/-/issues/16568
(cherry picked from commit 162e8962f7)
2024-02-23 16:53:29 +01:00
Seth Hillbrand 745c52c2b7 Update triangulation
Allow for overlapping points in input polygon.  This is relatively
common on a grid and should be used as potential split points to shave
additional ears

Fixes https://gitlab.com/kicad/code/kicad/-/issues/16933
2024-02-12 15:20:08 -08:00
Alex Shvartzkop 1122d80388 Better support for arcs in tuning patterns. 2024-01-31 23:38:05 +03:00
Roberto Fernandez Bautista e950475bc3 Add assert to SHAPE_LINE_CHAIN::Segment + Deduplicate code
CSegment should have the exact same logic as Segment

Also, moving definition to .cpp file to reduce need to rebuild every time
2024-01-31 19:17:46 +03:00
Roberto Fernandez Bautista e9fbb36538 SHAPE_LINE_CHAIN: Fix and simplify NextShape() + remove PrevShape()
NextShape() function was too complex (trying to go back and forwards).
We only used PrevShape() in one place, so removed that usage.

Added QA tests
2024-01-31 19:17:46 +03:00
Roberto Fernandez Bautista 079478f989 EDA_ANGLE: use constexpr for constant angles
Remove all statics in the class and instead use constexpr to
ensure correct ordering
2024-01-31 19:17:46 +03:00
Jeff Young 8410a5e685 Coding conventions and cleanup. 2024-01-23 16:03:25 +00:00
Seth Hillbrand f1dacf7d92 Updated revision of polygon triangulation from 0a67dd3fdd
This maintains the primary goal of ensuring that bad splits do not allow
the triangulation process to continue.  Instead, we boot this back up to
the top-level where we will simplify the polygon before triangulating
again.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/16624
2024-01-18 11:07:40 -08:00
Seth Hillbrand e4a0b9c7ed Revert "Revise triangulation"
This reverts commit 0a67dd3fdd.
2024-01-18 11:07:40 -08:00