2014-09-09 15:22:29 +02:00
|
|
|
// Copyright 2014 by Sascha L. Teichmann
|
|
|
|
// Use of this source code is governed by the MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
2014-09-07 21:46:55 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2014-09-11 17:35:03 +02:00
|
|
|
"image"
|
2014-09-10 18:58:12 +02:00
|
|
|
"image/color"
|
2014-09-07 21:46:55 +02:00
|
|
|
"log"
|
2014-10-28 11:51:45 +01:00
|
|
|
"os"
|
|
|
|
"runtime/pprof"
|
2014-09-07 21:46:55 +02:00
|
|
|
|
2014-10-03 12:07:53 +02:00
|
|
|
"bitbucket.org/s_l_teichmann/mtsatellite/common"
|
2014-09-07 21:46:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
|
|
|
port int
|
|
|
|
host string
|
|
|
|
x, y, z int
|
|
|
|
width, height, depth int
|
2014-09-09 15:01:14 +02:00
|
|
|
colorsfile string
|
2014-09-10 18:58:12 +02:00
|
|
|
outfile string
|
2014-09-11 17:35:03 +02:00
|
|
|
shaded bool
|
2014-10-26 18:36:47 +01:00
|
|
|
transparent bool
|
2014-10-28 11:51:45 +01:00
|
|
|
cpuProfile string
|
2014-09-07 21:46:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
flag.IntVar(&port, "port", 6379, "port to of mtredisalize server")
|
2014-09-10 18:58:12 +02:00
|
|
|
flag.IntVar(&port, "p", 6379, "port to of mtredisalize server (shorthand)")
|
2014-09-07 21:46:55 +02:00
|
|
|
flag.StringVar(&host, "host", "localhost", "host to mtredisalize server")
|
2014-09-12 17:10:05 +02:00
|
|
|
flag.IntVar(&x, "x", 0, "x of query cuboid")
|
2014-09-07 21:46:55 +02:00
|
|
|
flag.IntVar(&y, "y", -75, "y of query cuboid")
|
2014-09-12 17:10:05 +02:00
|
|
|
flag.IntVar(&z, "z", 0, "z of query cuboid")
|
|
|
|
flag.IntVar(&width, "width", 16, "width of query cuboid")
|
|
|
|
flag.IntVar(&height, "height", 16, "height of query cuboid")
|
2014-09-07 21:46:55 +02:00
|
|
|
flag.IntVar(&depth, "depth", 150, "depth of query cuboid")
|
2014-09-12 17:10:05 +02:00
|
|
|
flag.IntVar(&width, "w", 16, "width of query cuboid (shorthand)")
|
|
|
|
flag.IntVar(&height, "h", 16, "height of query cuboid (shorthand)")
|
2014-09-07 21:46:55 +02:00
|
|
|
flag.IntVar(&depth, "d", 150, "depth of query cuboid (shorthand)")
|
2014-09-09 15:01:14 +02:00
|
|
|
flag.StringVar(&colorsfile, "colors", "colors.txt", "definition of colors")
|
2014-09-10 18:58:12 +02:00
|
|
|
flag.StringVar(&outfile, "output", "out.png", "image file of result")
|
|
|
|
flag.StringVar(&outfile, "o", "out.png", "image file of result (shorthand)")
|
2014-09-11 17:35:03 +02:00
|
|
|
flag.BoolVar(&shaded, "shaded", true, "draw relief")
|
2014-10-26 18:36:47 +01:00
|
|
|
flag.BoolVar(&transparent, "transparent", false, "render transparent blocks")
|
2014-10-28 11:51:45 +01:00
|
|
|
flag.StringVar(&cpuProfile, "cpuprofile", "", "write cpu profile to file")
|
2014-09-07 21:46:55 +02:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2014-10-28 11:51:45 +01:00
|
|
|
if cpuProfile != "" {
|
|
|
|
f, err := os.Create(cpuProfile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
}
|
|
|
|
|
2014-09-09 15:01:14 +02:00
|
|
|
var err error
|
2014-09-14 00:02:04 +02:00
|
|
|
var colors *common.Colors
|
2014-09-09 15:01:14 +02:00
|
|
|
|
2014-09-14 00:02:04 +02:00
|
|
|
if colors, err = common.ParseColors(colorsfile); err != nil {
|
2014-09-09 15:01:14 +02:00
|
|
|
log.Fatalf("Cannot open color file: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-09-07 21:46:55 +02:00
|
|
|
address := fmt.Sprintf("%s:%d", host, port)
|
|
|
|
|
2014-09-13 19:18:12 +02:00
|
|
|
var client *common.RedisClient
|
2014-09-07 21:46:55 +02:00
|
|
|
|
2014-09-13 19:18:12 +02:00
|
|
|
if client, err = common.NewRedisClient("tcp", address); err != nil {
|
2014-09-07 21:46:55 +02:00
|
|
|
log.Fatalf("Cannot connect to '%s': %s", address, err)
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
2014-09-12 17:10:05 +02:00
|
|
|
if shaded {
|
|
|
|
width += 2
|
|
|
|
height += 2
|
|
|
|
x--
|
|
|
|
z--
|
|
|
|
}
|
|
|
|
|
2014-09-07 21:46:55 +02:00
|
|
|
q1x, q1y, q1z := int16(x), int16(y), int16(z)
|
|
|
|
q2x, q2y, q2z := q1x+int16(width)-1, q1y+int16(depth)-1, q1z+int16(height)-1
|
|
|
|
|
2014-10-26 18:36:47 +01:00
|
|
|
renderer := common.NewRenderer(width, height, transparent)
|
2014-09-14 14:57:49 +02:00
|
|
|
renderer.SetPos(q1x, q1z)
|
2014-09-12 20:22:34 +02:00
|
|
|
yOrder := common.NewYOrder(renderer, 512)
|
2014-09-09 17:48:39 +02:00
|
|
|
|
2014-09-11 00:22:36 +02:00
|
|
|
numBlocks := 0
|
2014-09-10 18:58:12 +02:00
|
|
|
drawBlock := func(block *common.Block) {
|
2014-10-19 21:05:38 +02:00
|
|
|
if err := yOrder.RenderBlock(block, colors); err != nil {
|
2015-07-20 14:19:41 +02:00
|
|
|
log.Printf("WARN: rendering block failed: %s\n", err)
|
2014-09-09 17:48:39 +02:00
|
|
|
}
|
2014-09-11 00:22:36 +02:00
|
|
|
numBlocks++
|
2014-09-07 21:46:55 +02:00
|
|
|
}
|
|
|
|
|
2014-09-11 00:22:36 +02:00
|
|
|
c1 := common.Coord{X: q1x, Z: q1z}
|
|
|
|
c2 := common.Coord{X: q2x, Z: q2z}
|
2014-09-11 02:28:09 +02:00
|
|
|
for c2.Y = q2y; c2.Y > q1y; c2.Y -= 8 {
|
|
|
|
c1.Y = c2.Y - 7
|
2014-09-11 00:22:36 +02:00
|
|
|
if c1.Y < q1y {
|
|
|
|
c1.Y = q1y
|
|
|
|
}
|
|
|
|
cuboid := common.Cuboid{P1: common.MinCoord(c1, c2), P2: common.MaxCoord(c1, c2)}
|
|
|
|
if err = client.QueryCuboid(cuboid, drawBlock); err != nil {
|
|
|
|
log.Fatalf("query failed: %s", err)
|
|
|
|
}
|
2014-10-19 21:05:38 +02:00
|
|
|
if err = yOrder.Drain(colors); err != nil {
|
2015-07-20 14:19:41 +02:00
|
|
|
log.Printf("WARN: rendering block failed: %s\n", err)
|
2014-09-12 12:37:27 +02:00
|
|
|
}
|
2014-09-11 02:28:09 +02:00
|
|
|
if renderer.IsFilled() {
|
|
|
|
break
|
|
|
|
}
|
2014-09-07 21:46:55 +02:00
|
|
|
}
|
|
|
|
|
2014-09-11 17:35:03 +02:00
|
|
|
var image image.Image
|
|
|
|
|
|
|
|
if shaded {
|
|
|
|
image = renderer.CreateShadedImage(
|
2014-09-12 17:10:05 +02:00
|
|
|
16, 16, (width-2)*16, (height-2)*16,
|
2014-10-26 18:36:47 +01:00
|
|
|
colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff})
|
2014-09-11 17:35:03 +02:00
|
|
|
} else {
|
|
|
|
image = renderer.CreateImage(
|
|
|
|
colors.Colors, color.RGBA{R: 0xff, G: 0xff, B: 0xff, A: 0xff})
|
|
|
|
}
|
2014-09-09 17:48:39 +02:00
|
|
|
|
2014-09-14 00:31:28 +02:00
|
|
|
if err = common.SaveAsPNG(outfile, image); err != nil {
|
2014-09-10 18:58:12 +02:00
|
|
|
log.Fatalf("writing image failed: %s", err)
|
2014-09-09 17:48:39 +02:00
|
|
|
}
|
2014-09-10 23:49:27 +02:00
|
|
|
|
2014-09-12 20:22:34 +02:00
|
|
|
log.Printf("num blocks: %d\n", numBlocks)
|
2014-10-26 17:09:23 +01:00
|
|
|
log.Printf("rejected blocks: %d\n", renderer.RejectedBlocks)
|
|
|
|
log.Printf("transparent blocks: %d\n", renderer.TransparentBlocks)
|
|
|
|
log.Printf("solid blocks: %d\n", renderer.SolidBlocks)
|
2014-09-07 21:46:55 +02:00
|
|
|
}
|