JuliaGeometry / GeometryBasics.jl

Basic Geometry Types
MIT License
164 stars 54 forks source link

Polygon with BigFloats #132

Open zekeriyasari opened 3 years ago

zekeriyasari commented 3 years ago

The construction of a polygon with Float64 points such as

hexagon = Polygon(
    [Point(cos(θ), sin(θ)) for θ in 0 : π / 3 : 2π - π / 3]
)

does not throw an error, while the one with BigFloat points such as

hexagon = Polygon(
    [Point(BigFloat(cos(θ)), BigFloat(sin(θ))) for θ in 0 : π / 3 : 2π - π / 3]
)

does throw an error. The error thrown is

ERROR: ArgumentError: cannot reinterpret `Tuple{Point2{BigFloat}, Point2{BigFloat}}` as `Line{2, BigFloat}`, type `Line{2, BigFloat}` is not a bits type
Stacktrace:
 [1] (::Base.var"#throwbits#243")(S::Type, T::Type, U::Type)
   @ Base ./reinterpretarray.jl:16
 [2] reinterpret(#unused#::Type{Line{2, BigFloat}}, a::TupleView{Tuple{Point2{BigFloat}, Point2{BigFloat}}, 2, 1, Vector{Point2{BigFloat}}})
   @ Base ./reinterpretarray.jl:36
 [3] connect
   @ ~/.julia/packages/GeometryBasics/l4gkj/src/viewtypes.jl:77 [inlined]
 [4] LineString(points::Vector{Point2{BigFloat}}, skip::Int64)
   @ GeometryBasics ~/.julia/packages/GeometryBasics/l4gkj/src/basic_types.jl:209
 [5] Polygon(exterior::Vector{Point2{BigFloat}}, skip::Int64)
   @ GeometryBasics ~/.julia/packages/GeometryBasics/l4gkj/src/basic_types.jl:275
 [6] top-level scope
   @ REPL[559]:1

Is there a possible workaround for this error?

zekeriyasari commented 3 years ago

The aim was to construct a hexagon. Calling the constructor directly, I think, solves the problem.

using StaticArrays 
using GeometryBasics 

hexagon = GeometryBasics.Ngon(@SVector [Point(BigFloat(cos(θ)), BigFloat(sin(θ))) for θ in 0 : π / 3 : 2π - π / 3])
SimonDanisch commented 3 years ago

Oh yeah, there is some branch missing to handle non isbits element types... https://github.com/JuliaGeometry/GeometryBasics.jl/blob/master/src/viewtypes.jl#L77 This needs to check for isbits, and if not isbits copy instead of reinterprete

visr commented 3 years ago

@zekeriyasari why did you close this? I think https://github.com/JuliaGeometry/GeometryBasics.jl/issues/132#issuecomment-841818308 would need to be changed in this package to fix this issue.