From 7054ba622aa6629d3ecbb11986491969f4cc4bb2 Mon Sep 17 00:00:00 2001 From: Sebastien Binet Date: Thu, 27 Aug 2020 08:27:58 +0000 Subject: [PATCH] op/clip: implement arc path Signed-off-by: Sebastien Binet --- internal/rendertest/clip_test.go | 30 +++++ internal/rendertest/refs/TestPaintArc.png | Bin 0 -> 1888 bytes op/clip/clip.go | 148 ++++++++++++++++++++++ 3 files changed, 178 insertions(+) create mode 100644 internal/rendertest/refs/TestPaintArc.png diff --git a/internal/rendertest/clip_test.go b/internal/rendertest/clip_test.go index 6d205f72..9debf7fa 100644 --- a/internal/rendertest/clip_test.go +++ b/internal/rendertest/clip_test.go @@ -1,6 +1,7 @@ package rendertest import ( + "math" "testing" "gioui.org/f32" @@ -49,6 +50,35 @@ func TestPaintClippedCirle(t *testing.T) { }) } +func TestPaintArc(t *testing.T) { + run(t, func(o *op.Ops) { + p := new(clip.Path) + p.Begin(o) + p.Move(f32.Pt(0, 20)) + p.Line(f32.Pt(10, 0)) + p.Arc(f32.Pt(10, 0), f32.Pt(40, 0), math.Pi) + p.Line(f32.Pt(30, 0)) + p.Line(f32.Pt(0, 25)) + p.Arc(f32.Pt(-10, 5), f32.Pt(10, 15), -math.Pi) + p.Line(f32.Pt(0, 25)) + p.Line(f32.Pt(-10, 0)) + p.Arc(f32.Pt(-10, 0), f32.Pt(-40, 0), -math.Pi) + p.Line(f32.Pt(-10, 0)) + p.Line(f32.Pt(0, -10)) + p.Arc(f32.Pt(-10, -20), f32.Pt(10, -5), math.Pi) + p.Line(f32.Pt(0, -10)) + p.Line(f32.Pt(-50, 0)) + p.End().Add(o) + + paint.ColorOp{Color: colornames.Red}.Add(o) + paint.PaintOp{Rect: f32.Rect(0, 0, 128, 128)}.Add(o) + }, func(r result) { + r.expect(0, 0, colornames.White) + r.expect(0, 25, colornames.Red) + r.expect(0, 15, colornames.White) + }) +} + func TestPaintTexture(t *testing.T) { run(t, func(o *op.Ops) { squares.Add(o) diff --git a/internal/rendertest/refs/TestPaintArc.png b/internal/rendertest/refs/TestPaintArc.png new file mode 100644 index 0000000000000000000000000000000000000000..d900cc32eabbfa73473c95f256bcab07d25d5aa3 GIT binary patch literal 1888 zcmbVN`9IW&8~@ChaSemH43lFft(&nku1OlkH8~=bs}dPjwPspk9F052l9VLpNRh2b zt`{3)4B6H=axcZI6bi$LZNu9C;QQSlp67YJ-p3Em5AWy6ba!)BvZ)Qk(@^I%ZUW`Zpoh0?u>Hu zm1ss;)Ye}_gXFg*tNo$9KMf2Nk%lv*m*$zgHKKTn7HuBC2S0d_NQR`O)JN(HUUWBv z%#%#f{H;QobM|tCyrgI`GVAzF`XhszWpFk- z@F~?#^^UMM&DSn)Eq*8K{{7Y;kl+ffOeinP1S=ryUHqQyb8a&{UM&6iBzoOIFBuuw zG{5tA71w5uIECO}hrrdycrvoe&f>eT9<=|Fi`PFdA8k8%;a)BOd76LqF_$SB7y zG|^I~C1ROnwEb?u6{}GV=YB5>8J(cfQq+CpUkw3KQKNXT-a?=nscto-n~f;5hK_Z1 z9O^0D-|NitSNn*bULZ4%l6rR=p_nYseBGARHs?$Yt#-4yMIus5sjl%dnu}~hmk8r1 zu&IZ;RI2Jq%QS{GtVxIMT%9o5Y0#;90ozyScwHo+FRMm5<}_uOzmOFVCd~ z8>^$l5?I6G1zQHH8=P?tB&Y|1d;k-JL@b6X2h&W4D*g}flB;)pMqhvGBLgS5^i~TZ zHjp9d;K;302)`i1hit`M>p0$r&6y{C*Sk@!?`w2MkPB~|e!g;JL}e5tDtBrF*VfKW znrP6LDl5gmZ*T|>RJP@Tqi$+^i5ng=tFsDIQ=@n*qzZU_UGuwiN@^&9Mh(}C0@vB4 z12;1%Nv8-jUir~cnO_kq;I_6JWK2jGL#OhiMsu^RKTiCGzilDt&DyKh)y=ytd0}En zndDUa3tn5QlGY&n+>|{&k^+T3JL1naDGrU}u2(7LH-^RXB_+&E7ysz;{<)B2v$#(Qn5wY4?07@jLnOb%$r+|_ojFQGtl zaz^n1EacFPoT(|ZZ6IJ zB!;lZcny)0ABGGUeT(lCqC;_;pJ+dV5s0wlkAu%b^UB~oJ!|h_TuF1#^7t@>JHO;T zSq~a0P5HwwzsTe@_W9OHC3If!s8na^>~re7%-&eg>j-C{s3T7-kOg@L+x= zyWY+aj6~b`xA2Qd5MM&R&R~Sc(xPbSz}F8N_0#7VlKqT3cK|x#u{qloPrqAw;aC~x z_k=gP5ym2xul7L+xJLTsGTzdcoUyl~926uV&?{%jCivTDJ1g38etvU*{dE7eLyFBs zJ0q`rL9UCdO=RPwvdTS{S@G_OXWUL@TU+cKA>g_yRbrKiMo*5tr!X%4$qK)6_=`B> zXn(&r;pkihcRd#?x3Qw%SJ!nzYqR6SsjO(wmBSe23r2J6Ar~XIVk^*aRO7!fuaegX zWms6}+S~idv|d$fZ%?S&h^}IsDkQ2}gU?H)rRl*YpW|>iwAu_W{iT^Hw+CSaR%Bg9 ztiJJf<>lZk%pQKNJ=3$I6Z$zfWiMWVM%B~21B z5eiL$)jUEGb_jjLU*S^LY=i^&-N@2TT>oz(f2yyq=3+7@fWL@>^Bog^7N!e@G0WpK z{vjBQyBBIMPR5&kriXz9@sIIl-!_C7M1WLOEGVjhlqPkos(NqfVVWnE8BBZ88(-%c zAje|a6d7U{+2vvWaJ$vO-;w{vY;*&7lih9MMVAGk7&O0u7#m;Jcv>`NgZra=cvjo{^H6hoq zhqOJlnfCkUlVPnQo=-z6B}v~;)~6$&J4Y&g%B7TtHr`5Amh`B$M0*)Hy}2cnbht;s wd1WR-h)YT;*B_=6|G&fW-_#2n5CH7$30qH4i@ErQ{m%)|?cMC2*ajy51HGO#cmMzZ literal 0 HcmV?d00001 diff --git a/op/clip/clip.go b/op/clip/clip.go index 9f6ced4b..04471349 100644 --- a/op/clip/clip.go +++ b/op/clip/clip.go @@ -5,6 +5,7 @@ package clip import ( "encoding/binary" "image" + "math" "gioui.org/f32" "gioui.org/internal/opconst" @@ -104,6 +105,153 @@ func (p *Path) quadTo(ctrl, to f32.Point) { p.pen = to } +// Arc adds an elliptical arc to the path. The implied ellipse is defined +// by its focus points f1 and f2. +// The arc starts in the current point and ends angle radians along the ellipse boundary. +// The sign of angle determines the direction; positive being counter-clockwise, +// negative clockwise. +func (p *Path) Arc(f1, f2 f32.Point, angle float32) { + f1 = f1.Add(p.pen) + f2 = f2.Add(p.pen) + c, rx, ry, beg, alpha := arcFrom(f1, f2, p.pen) + p.arc(alpha, c, rx, ry, beg, float64(angle)) +} + +func dist(p1, p2 f32.Point) float64 { + var ( + x1 = float64(p1.X) + y1 = float64(p1.Y) + x2 = float64(p2.X) + y2 = float64(p2.Y) + dx = x2 - x1 + dy = y2 - y1 + ) + return math.Hypot(dx, dy) +} + +func arcFrom(f1, f2, p f32.Point) (c f32.Point, rx, ry, start, alpha float64) { + c = f32.Point{ + X: 0.5 * (f1.X + f2.X), + Y: 0.5 * (f1.Y + f2.Y), + } + + // semi-major axis: 2a = |PF1| + |PF2| + a := 0.5 * (dist(f1, p) + dist(f2, p)) + + // semi-minor axis: c^2 = a^2+b^2 (c: focal distance) + f := dist(f1, c) + b := math.Sqrt(a*a - f*f) + + switch { + case a > b: + rx = a + ry = b + default: + rx = b + ry = a + } + + var x float64 + switch { + case f1 == c || f2 == c: + // degenerate case of a circle. + alpha = 0 + default: + switch { + case f1.X > c.X: + x = float64(f1.X - c.X) + alpha = math.Acos(x / f) + case f1.X < c.X: + x = float64(f2.X - c.X) + alpha = math.Acos(x / f) + case f1.X == c.X: + // special case of a "vertical" ellipse. + alpha = math.Pi / 2 + if f1.Y < c.Y { + alpha = -alpha + } + } + } + + start = math.Acos(float64(p.X-c.X) / dist(c, p)) + if c.Y > p.Y { + start = -start + } + start -= alpha + + return c, rx, ry, start, alpha +} + +// arc records an elliptical arc centered at c, with radii rx and ry, +// starting at angle beg and stopping at end, in radians. +// +// The math is extracted from the following paper: +// "Drawing an elliptical arc using polylines, quadratic or +// cubic Bezier curves", L. Maisonobe +// An electronic version may be found at: +// http://spaceroots.org/documents/ellipse/elliptical-arc.pdf +func (p *Path) arc(alpha float64, c f32.Point, rx, ry, beg, delta float64) { + var ( + n = math.Round(20 * math.Pi / math.Abs(delta)) + θ = delta / n + + sinθ64, cosθ64 = math.Sincos(θ) + sinθ, cosθ = float32(sinθ64), float32(cosθ64) + b = (cosθ - 1) / sinθ + ) + + var ( + ref f32.Affine2D // transform from absolute frame to ellipse-based one + rot f32.Affine2D // rotation matrix for each segment + inv f32.Affine2D // transform from ellipse-based frame to absolute one + ) + ref = ref.Offset(f32.Point{}.Sub(c)) + ref = ref.Rotate(f32.Point{}, float32(-alpha)) + ref = ref.Scale(f32.Point{}, f32.Point{ + X: float32(1 / rx), + Y: float32(1 / ry), + }) + inv = ref.Invert() + rot = rot.Rotate(f32.Point{}, float32(θ)) + + // Instead of invoking math.Sincos for every segment, compute a rotation + // matrix once and apply for each segment. + // Before applying the rotation matrix rot, transform the coordinates + // to a frame centered to the ellipse (and warped into a unit circle), then rotate. + // Finally, transform back into the original frame. + // + // Also compute the control point C according to + // https://pomax.github.io/bezierinfo/#circles. + // If S is the starting point, S' is the orthogonal + // tangent, θ is clockwise: + // + // C = S + b*S', b = (cos θ - 1)/sin θ + // + // We apply the same original <-> ellipse frame transformation to the + // control point as well. + rotate := func(p f32.Point) (end, ctl f32.Point) { + q := ref.Transform(p) + t := f32.Pt(-q.Y, q.X) + + end = rot.Transform(q) + ctl = q.Add(t.Mul(b)) + + end = inv.Transform(end) + ctl = inv.Transform(ctl) + + return end, ctl + } + + var ( + ctl f32.Point + end = p.pen + ) + for i := 0; i < int(n); i++ { + end, ctl = rotate(p.pen) + p.quadTo(ctl, end) + } +} + // Cube records a cubic Bézier from the pen through // two control points ending in to. func (p *Path) Cube(ctrl0, ctrl1, to f32.Point) {