draw2d/curve/curve_test.go

149 lines
3.8 KiB
Go
Raw Normal View History

2012-01-13 09:14:12 +00:00
package curve
import (
2015-04-23 08:05:48 +00:00
"bufio"
2012-01-13 09:14:12 +00:00
"fmt"
"image"
"image/color"
"image/draw"
2015-04-23 08:05:48 +00:00
"image/png"
2012-01-13 09:14:12 +00:00
"log"
"os"
"testing"
2015-04-16 09:51:13 +00:00
2015-04-19 15:14:42 +00:00
"github.com/llgcode/draw2d/raster"
2012-01-13 09:14:12 +00:00
)
var (
flattening_threshold float64 = 0.5
2015-04-23 08:05:48 +00:00
testsCubicFloat64 = []float64{
100, 100, 200, 100, 100, 200, 200, 200,
100, 100, 300, 200, 200, 200, 300, 100,
100, 100, 0, 300, 200, 0, 300, 300,
150, 290, 10, 10, 290, 10, 150, 290,
10, 290, 10, 10, 290, 10, 290, 290,
100, 290, 290, 10, 10, 10, 200, 290,
2012-01-13 09:14:12 +00:00
}
2015-04-23 08:05:48 +00:00
testsQuadFloat64 = []float64{
100, 100, 200, 100, 200, 200,
100, 100, 290, 200, 290, 100,
100, 100, 0, 290, 200, 290,
150, 290, 10, 10, 290, 290,
10, 290, 10, 10, 290, 290,
100, 290, 290, 10, 120, 290,
2012-01-13 09:14:12 +00:00
}
)
type Path struct {
points []float64
}
func (p *Path) MoveTo(x, y float64) {
p.points = append(p.points, x, y)
}
func (p *Path) LineTo(x, y float64) {
2015-04-23 08:05:48 +00:00
p.points = append(p.points, x, y)
2012-01-13 09:14:12 +00:00
}
func init() {
2015-04-22 17:07:03 +00:00
os.Mkdir("test_results", 0666)
f, err := os.Create("test_results/_test.html")
2012-01-13 09:14:12 +00:00
if err != nil {
log.Println(err)
os.Exit(1)
}
defer f.Close()
log.Printf("Create html viewer")
f.Write([]byte("<html><body>"))
2015-04-23 08:05:48 +00:00
for i := 0; i < len(testsCubicFloat64)/8; i++ {
2015-04-22 17:07:03 +00:00
f.Write([]byte(fmt.Sprintf("<div><img src='_test%d.png'/></div>\n", i)))
2012-01-13 09:14:12 +00:00
}
for i := 0; i < len(testsQuadFloat64); i++ {
f.Write([]byte(fmt.Sprintf("<div><img src='_testQuad%d.png'/>\n</div>\n", i)))
}
f.Write([]byte("</body></html>"))
}
func drawPoints(img draw.Image, c color.Color, s ...float64) image.Image {
2015-04-22 17:07:03 +00:00
for i := 0; i < len(s); i += 2 {
2015-04-16 09:51:13 +00:00
x, y := int(s[i]+0.5), int(s[i+1]+0.5)
img.Set(x, y, c)
img.Set(x, y+1, c)
img.Set(x, y-1, c)
img.Set(x+1, y, c)
img.Set(x+1, y+1, c)
img.Set(x+1, y-1, c)
img.Set(x-1, y, c)
img.Set(x-1, y+1, c)
img.Set(x-1, y-1, c)
2012-01-13 09:14:12 +00:00
}
2015-04-22 17:07:03 +00:00
return img
2012-01-13 09:14:12 +00:00
}
func TestCubicCurve(t *testing.T) {
2015-04-23 08:05:48 +00:00
for i := 0; i < len(testsCubicFloat64); i += 8 {
2012-01-13 09:14:12 +00:00
var p Path
p.MoveTo(testsCubicFloat64[i], testsCubicFloat64[i+1])
2015-04-23 08:05:48 +00:00
TraceCubic(&p, testsCubicFloat64[i:], flattening_threshold)
2012-01-13 09:14:12 +00:00
img := image.NewNRGBA(image.Rect(0, 0, 300, 300))
2015-04-23 08:05:48 +00:00
raster.PolylineBresenham(img, color.NRGBA{0xff, 0, 0, 0xff}, testsCubicFloat64[i:i+8]...)
2012-01-13 09:14:12 +00:00
raster.PolylineBresenham(img, image.Black, p.points...)
2015-04-16 09:51:13 +00:00
//drawPoints(img, image.NRGBAColor{0, 0, 0, 0xff}, curve[:]...)
2012-01-13 09:14:12 +00:00
drawPoints(img, color.NRGBA{0, 0, 0, 0xff}, p.points...)
2015-04-23 08:05:48 +00:00
SaveToPngFile(fmt.Sprintf("test_results/_test%d.png", i/8), img)
2012-01-13 09:14:12 +00:00
log.Printf("Num of points: %d\n", len(p.points))
}
fmt.Println()
}
func TestQuadCurve(t *testing.T) {
2015-04-23 08:05:48 +00:00
for i := 0; i < len(testsQuadFloat64); i += 6 {
2012-01-13 09:14:12 +00:00
var p Path
p.MoveTo(testsQuadFloat64[i], testsQuadFloat64[i+1])
2015-04-23 08:05:48 +00:00
TraceQuad(&p, testsQuadFloat64[i:], flattening_threshold)
2012-01-13 09:14:12 +00:00
img := image.NewNRGBA(image.Rect(0, 0, 300, 300))
2015-04-23 08:05:48 +00:00
raster.PolylineBresenham(img, color.NRGBA{0xff, 0, 0, 0xff}, testsQuadFloat64[i:i+6]...)
2012-01-13 09:14:12 +00:00
raster.PolylineBresenham(img, image.Black, p.points...)
2015-04-16 09:51:13 +00:00
//drawPoints(img, image.NRGBAColor{0, 0, 0, 0xff}, curve[:]...)
2012-01-13 09:14:12 +00:00
drawPoints(img, color.NRGBA{0, 0, 0, 0xff}, p.points...)
2015-04-23 08:05:48 +00:00
SaveToPngFile(fmt.Sprintf("test_results/_testQuad%d.png", i), img)
2012-01-13 09:14:12 +00:00
log.Printf("Num of points: %d\n", len(p.points))
}
fmt.Println()
}
func BenchmarkCubicCurve(b *testing.B) {
for i := 0; i < b.N; i++ {
2015-04-23 08:05:48 +00:00
for i := 0; i < len(testsCubicFloat64); i += 8 {
var p Path
p.MoveTo(testsCubicFloat64[i], testsCubicFloat64[i+1])
2015-04-23 08:05:48 +00:00
TraceCubic(&p, testsCubicFloat64[i:], flattening_threshold)
2012-01-13 09:14:12 +00:00
}
}
}
2015-04-23 08:05:48 +00:00
// SaveToPngFile create and save an image to a file using PNG format
func SaveToPngFile(filePath string, m image.Image) error {
// Create the file
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
// Create Writer from file
b := bufio.NewWriter(f)
// Write the image into the buffer
err = png.Encode(b, m)
if err != nil {
return err
}
err = b.Flush()
if err != nil {
return err
}
return nil
}