This commit is contained in:
Laurent Le Goff 2011-05-18 23:23:31 +02:00
parent 713b1fa1f6
commit b7d000715e
6 changed files with 56 additions and 72 deletions

View file

@ -53,7 +53,7 @@ func reshape(w, h int) {
gl.Translatef(0, float32(-h), 0) /* Shift origin up to upper-left corner. */
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
gl.Disable(gl.DEPTH_TEST);
gl.Disable(gl.DEPTH_TEST)
width, height = w, h
}

View file

@ -27,29 +27,29 @@ func (c *CubicCurveFloat64) ArbitraryPoint(mu float64) (x, y float64) {
}
func (c *CubicCurveFloat64) SubdivideAt(c1, c2 *CubicCurveFloat64, t float64) {
inv_t := (1 - t)
c1.X1, c1.Y1 = c.X1, c.Y1
c2.X4, c2.Y4 = c.X4, c.Y4
c1.X2 = inv_t * c.X1 + t * c.X2
c1.Y2 = inv_t * c.Y1 + t * c.Y2
x23 := inv_t * c.X2 + t * c.X3
y23 := inv_t * c.Y2 + t * c.Y3
c2.X3 = inv_t * c.X3 + t * c.X4
c2.Y3 = inv_t * c.Y3 + t * c.Y4
inv_t := (1 - t)
c1.X1, c1.Y1 = c.X1, c.Y1
c2.X4, c2.Y4 = c.X4, c.Y4
c1.X3 = inv_t * c1.X2 + t * x23
c1.Y3 = inv_t * c1.Y2 + t * y23
c2.X2 = inv_t * x23 + t * c2.X3
c2.Y2 = inv_t * y23 + t * c2.Y3
c1.X4 = inv_t * c1.X3 + t * c2.X2
c1.Y4 = inv_t * c1.Y3 + t * c2.Y2
c2.X1, c2.Y1 = c1.X4, c1.Y4
c1.X2 = inv_t*c.X1 + t*c.X2
c1.Y2 = inv_t*c.Y1 + t*c.Y2
x23 := inv_t*c.X2 + t*c.X3
y23 := inv_t*c.Y2 + t*c.Y3
c2.X3 = inv_t*c.X3 + t*c.X4
c2.Y3 = inv_t*c.Y3 + t*c.Y4
c1.X3 = inv_t*c1.X2 + t*x23
c1.Y3 = inv_t*c1.Y2 + t*y23
c2.X2 = inv_t*x23 + t*c2.X3
c2.Y2 = inv_t*y23 + t*c2.Y3
c1.X4 = inv_t*c1.X3 + t*c2.X2
c1.Y4 = inv_t*c1.Y3 + t*c2.Y2
c2.X1, c2.Y1 = c1.X4, c1.Y4
}
func (c *CubicCurveFloat64) Subdivide(c1, c2 *CubicCurveFloat64) {
@ -121,12 +121,12 @@ func (c *CubicCurveFloat64) segmentRec(segments []float64) []float64 {
return segments
}
func (curve *CubicCurveFloat64) Segment(segments []float64) ([]float64) {
func (curve *CubicCurveFloat64) Segment(segments []float64) []float64 {
// Add the first point
segments = segments[0 : len(segments)+2]
segments[len(segments)-2] = curve.X1
segments[len(segments)-1] = curve.Y1
var curves [32]CubicCurveFloat64
curves[0] = *curve
i := 0
@ -137,21 +137,20 @@ func (curve *CubicCurveFloat64) Segment(segments []float64) ([]float64) {
c = &curves[i]
dx = c.X4 - c.X1
dy = c.Y4 - c.Y1
d2 = math.Fabs(((c.X2-c.X4)*dy - (c.Y2-c.Y4)*dx))
d3 = math.Fabs(((c.X3-c.X4)*dy - (c.Y3-c.Y4)*dx))
if (d2+d3)*(d2+d3) < flattening_threshold*(dx*dx+dy*dy) || i == len(curves) - 1 {
segments = segments[0 : len(segments)+2]
if (d2+d3)*(d2+d3) < flattening_threshold*(dx*dx+dy*dy) || i == len(curves)-1 {
segments = segments[0 : len(segments)+2]
segments[len(segments)-2] = c.X4
segments[len(segments)-1] = c.Y4
i--;
} else {
// second half of bezier go lower onto the stack
c.Subdivide(&curves[i+1], &curves[i])
i++;
}
}
return segments
i--
} else {
// second half of bezier go lower onto the stack
c.Subdivide(&curves[i+1], &curves[i])
i++
}
}
return segments
}

View file

@ -8,16 +8,15 @@ import (
"bufio"
"image"
"image/png"
"exp/draw"
"draw2d.googlecode.com/hg/draw2d/raster"
)
var (
testsFloat64 = []CubicCurveFloat64 {
testsFloat64 = []CubicCurveFloat64{
CubicCurveFloat64{100, 100, 200, 100, 100, 200, 200, 200},
CubicCurveFloat64{100, 100, 300, 200, 200, 200, 200, 100},
}
}
)
func init() {
@ -31,18 +30,9 @@ func init() {
f.Write([]byte("<html><body>"))
for i := 0; i < len(testsFloat64); i++ {
f.Write([]byte(fmt.Sprintf("<div><img src='_testRec%d.png'/><img src='_test%d.png'/></div>", i, i)))
}
f.Write([]byte("</body></html>"))
}
func rasterPolyline(img draw.Image, c image.Color, s ...float64) image.Image {
for i := 2; i < len(s); i+=2 {
raster.Bresenham(img, c, int(s[i-2]+0.5), int(s[i-1]+0.5), int(s[i]+0.5), int(s[i+1]+0.5))
}
return img
f.Write([]byte("</body></html>"))
}
func savepng(filePath string, m image.Image) {
@ -75,8 +65,8 @@ func TestCubicCurveCasteljauRec(t *testing.T) {
s := make([]float64, 0, numSegments)
s = curve.SegmentRec(s)
img := image.NewNRGBA(300, 300)
rasterPolyline(img, image.NRGBAColor{0xff, 0, 0, 0xff}, curve.X1, curve.Y1, curve.X2, curve.Y2, curve.X3, curve.Y3, curve.X4, curve.Y4)
savepng(fmt.Sprintf("_testRec%d.png", i), rasterPolyline(img, image.Black, s...))
raster.PolylineBresenham(img, image.NRGBAColor{0xff, 0, 0, 0xff}, curve.X1, curve.Y1, curve.X2, curve.Y2, curve.X3, curve.Y3, curve.X4, curve.Y4)
savepng(fmt.Sprintf("_testRec%d.png", i), raster.PolylineBresenham(img, image.Black, s...))
log.Printf("Num of points: %d\n", len(s))
}
}
@ -90,8 +80,8 @@ func TestCubicCurveCasteljau(t *testing.T) {
s := make([]float64, 0, numSegments)
s = curve.Segment(s)
img := image.NewNRGBA(300, 300)
rasterPolyline(img, image.NRGBAColor{0xff, 0, 0, 0xff}, curve.X1, curve.Y1, curve.X2, curve.Y2, curve.X3, curve.Y3, curve.X4, curve.Y4)
savepng(fmt.Sprintf("_test%d.png", i), rasterPolyline(img, image.Black, s...))
raster.PolylineBresenham(img, image.NRGBAColor{0xff, 0, 0, 0xff}, curve.X1, curve.Y1, curve.X2, curve.Y2, curve.X3, curve.Y3, curve.X4, curve.Y4)
savepng(fmt.Sprintf("_test%d.png", i), raster.PolylineBresenham(img, image.Black, s...))
log.Printf("Num of points: %d\n", len(s))
}
}
@ -118,13 +108,3 @@ func BenchmarkCubicCurveCasteljau(b *testing.B) {
}
}
}

View file

@ -9,8 +9,8 @@ import (
type VertexAdder struct {
command VertexCommand
adder raster.Adder
command VertexCommand
adder raster.Adder
}
func NewVertexAdder(adder raster.Adder) *VertexAdder {
@ -34,7 +34,7 @@ func (vertexAdder *VertexAdder) Vertex(x, y float64) {
type PathAdder struct {
adder raster.Adder
firstPoint raster.Point
firstPoint raster.Point
ApproximationScale float64
}
@ -71,5 +71,3 @@ func (pathAdder *PathAdder) Convert(paths ...*PathStorage) {
}
}
}

View file

@ -12,6 +12,13 @@ func abs(i int) int {
return i
}
func PolylineBresenham(img draw.Image, c image.Color, s ...float64) image.Image {
for i := 2; i < len(s); i += 2 {
Bresenham(img, c, int(s[i-2]+0.5), int(s[i-1]+0.5), int(s[i]+0.5), int(s[i+1]+0.5))
}
return img
}
func Bresenham(img draw.Image, color image.Color, x0, y0, x1, y1 int) {
dx := abs(x1 - x0)
dy := abs(y1 - y0)

View file

@ -40,8 +40,8 @@ func (p *GLPainter) Paint(ss []raster.Span, done bool) {
p.vertices = p.vertices[0 : vi+vlenrequired]
p.colors = p.colors[0 : ci+clenrequired]
var (
colors []uint8
vertices []int32
colors []uint8
vertices []int32
)
for _, s := range ss {
ma := s.A >> 16