simpletrace

package module
v0.0.0-...-55a093e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2022 License: MIT Imports: 6 Imported by: 0

README

simpletrace

Note: This is in a prototype stage and I've pushed it for use in my own projects. I don't recommend depending on it yet. It has inadequate tests, and needs significant refactoring.

simpletrace is a Go package for converting images into polygons. It is similar to tools like potrace, but produces simple polygons, rather than curves, which makes it easier to work with, for example, in applications where you need to produce polygon meshes.

Tracing is achieved by using marching squares to partition the field, and then simplifying the contours to reduce the line count. Filled regions turn into counterclockwise-wound, simple polygons, and holes are turned into clockwise-wound simple polygons.

simpletrace only supports bitmap tracing, and cannot be extended easily to handle other colors. It does, however, support converting images to bitmap through the IsColorFilledFunc callback.

Documentation

Index

Constants

View Source
const (
	CornerStateTopLeft = CornerStates(1 << iota)
	CornerStateTopRight
	CornerStateBottomLeft
	CornerStateBottomRight
)
View Source
const (
	DirectionUp = Direction(iota)
	DirectionRight
	DirectionDown
	DirectionLeft
	DirectionInvalid
)
View Source
const CornerStateNone = CornerStates(0)

Variables

View Source
var Redirections [16][4]Direction

Lookup table for the marching squares

Functions

func SignedAreaOfPolygon

func SignedAreaOfPolygon(polygon []Point) float64

func TraceImage

func TraceImage(img image.Image, isColorFilledFunc IsColorFilledFunc) [][]Point

Types

type CornerStates

type CornerStates uint8

Whether each corner of the square is set in the original bitmap

func CornerStateForOffset

func CornerStateForOffset(x, y int) CornerStates

func (CornerStates) IsSaddle

func (cs CornerStates) IsSaddle() bool

type Direction

type Direction uint8

func (Direction) IsVertical

func (dir Direction) IsVertical() bool

func (Direction) Reverse

func (dir Direction) Reverse() Direction

func (Direction) String

func (dir Direction) String() string

type IPoint

type IPoint struct {
	X int
	Y int
}

func (IPoint) ApplyDirection

func (p IPoint) ApplyDirection(dir Direction) IPoint

type IsColorFilledFunc

type IsColorFilledFunc func(color.Color) bool

Callback for determining if a pixel is filled

var DarkColorFilledFunc IsColorFilledFunc = func(c color.Color) bool {
	if !OpacityColorFilledFunc(c) {
		return false
	}
	y := yValueFromColor(c)
	return y < 0x80
}

Convert to bitmap by 50% lightness threshold, where black is filled

var LightColorFilledFunc IsColorFilledFunc = func(c color.Color) bool {
	if !OpacityColorFilledFunc(c) {
		return false
	}
	y := yValueFromColor(c)
	return y > 0x80
}

Convert to a bitmap by 50% lightness threshold, where white is filled

var OpacityColorFilledFunc IsColorFilledFunc = func(c color.Color) bool {
	_, _, _, alpha := c.RGBA()
	return alpha > 0xffff/2
}

Convert to bitmap by 50% alpha threshold

type Point

type Point struct {
	X float64
	Y float64
}

func PointFromInts

func PointFromInts(x, y int) Point

func (Point) UnitVectorTo

func (p Point) UnitVectorTo(other Point) Point

type RotationMatrix

type RotationMatrix [4][4]float64

type Square

type Square struct {
	Point   IPoint
	Corners CornerStates
}

A single square for the marching squares algorithm.

func (Square) CornerPointsInDirection

func (s Square) CornerPointsInDirection(dir Direction) (Point, Point)

func (Square) DirectionFor

func (s Square) DirectionFor(from Direction) Direction

func (Square) DirectionForNeighbor

func (s Square) DirectionForNeighbor(neighbor Square) Direction

func (*Square) Inspect

func (s *Square) Inspect() string

func (*Square) RemovePathForOutgoingDirection

func (s *Square) RemovePathForOutgoingDirection(outgoingDirection Direction)

For an outgoing direction, remove the path by updating the corner states. This allows saddle points to be handled correctly, since they have two paths. If a square loses all its paths, it gets garbage collected

type SquareMap

type SquareMap map[IPoint]*Square

func (SquareMap) GetSquare

func (s SquareMap) GetSquare() *Square

Get any square from the map.

func (SquareMap) Inspect

func (sm SquareMap) Inspect() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL