func normalize(f *data.Slice) { a := f.Vectors() maxnorm := 0. for i := range a[0] { for j := range a[0][i] { for k := range a[0][i][j] { x, y, z := a[0][i][j][k], a[1][i][j][k], a[2][i][j][k] norm := math.Sqrt(float64(x*x + y*y + z*z)) if norm > maxnorm { maxnorm = norm } } } } factor := float32(1 / maxnorm) for i := range a[0] { for j := range a[0][i] { for k := range a[0][i][j] { a[0][i][j][k] *= factor a[1][i][j][k] *= factor a[2][i][j][k] *= factor } } } }
// Render on existing image buffer. Resize it if needed func On(img *image.RGBA, f *data.Slice, fmin, fmax string, arrowSize int, colormap ...color.RGBA) { dim := f.NComp() switch dim { default: log.Fatalf("unsupported number of components: %v", dim) case 3: drawVectors(img, f.Vectors(), arrowSize) case 1: min, max := extrema(f.Host()[0]) if fmin != "auto" { m, err := strconv.ParseFloat(fmin, 32) if err != nil { util.Fatal("draw: scale:", err) } min = float32(m) } if fmax != "auto" { m, err := strconv.ParseFloat(fmax, 32) if err != nil { util.Fatal("draw: scale:", err) } max = float32(m) } if min == max { min -= 1 max += 1 // make it gray instead of black } drawFloats(img, f.Scalars(), min, max, colormap...) } }
func scale(f *data.Slice, factor float32) { a := f.Vectors() for i := range a[0] { for j := range a[0][i] { for k := range a[0][i][j] { a[0][i][j][k] *= factor a[1][i][j][k] *= factor a[2][i][j][k] *= factor } } } }
func normpeak(f *data.Slice) { a := f.Vectors() maxnorm := 0. for i := range a[0] { for j := range a[0][i] { for k := range a[0][i][j] { x, y, z := a[0][i][j][k], a[1][i][j][k], a[2][i][j][k] norm := math.Sqrt(float64(x*x + y*y + z*z)) if norm > maxnorm { maxnorm = norm } } } } scale(f, float32(1/maxnorm)) }
// normalize vector data to given length func normalize(f *data.Slice, length float64) { a := f.Vectors() for i := range a[0] { for j := range a[0][i] { for k := range a[0][i][j] { x, y, z := a[0][i][j][k], a[1][i][j][k], a[2][i][j][k] norm := math.Sqrt(float64(x*x + y*y + z*z)) invnorm := float32(1) if norm != 0 { invnorm = float32(length / norm) } a[0][i][j][k] *= invnorm a[1][i][j][k] *= invnorm a[2][i][j][k] *= invnorm } } } }
func renderSVGZ(f *data.Slice, info data.Meta, out io.Writer) { out2 := gzip.NewWriter(out) defer out2.Close() draw.SVG(out2, f.Vectors()) }
func renderSVG(f *data.Slice, info data.Meta, out io.Writer) { draw.SVG(out, f.Vectors()) }