martinez_go

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2024 License: MIT Imports: 2 Imported by: 0

README

martinez-go

Martinez-Rueda polygon clipping algorithm for golang.

Follows https://github.com/w8r/martinez and https://github.com/21re/rust-geo-booleanop as reference.

Usage

package main

import (
	"fmt"
	"github.com/akayami/martinez-go"
)

func main() {

	type Point = martinez_go.Point

	subject := [][][]Point{{{
		Point{0, 0},
		Point{0, 10},
		Point{10, 10},
		Point{10, 0},
		Point{0, 0},
	}}}

	clipping := [][][]Point{{{
		Point{5, 5},
		Point{5, 15},
		Point{15, 15},
		Point{15, 5},
		Point{5, 5},
	}}}

	res := martinez_go.Compute(subject, clipping, martinez_go.Intersection)

	fmt.Println(res)

}
go run ./main.go 
[[[{5 5} {10 5} {10 10} {5 10} {5 5}]]]

Authors

Based on

Documentation

Index

Constants

View Source
const (
	Intersection = iota
	Union
	Difference
	XOR
)

Operation types

View Source
const (
	Normal = iota
	SameTransition
	DifferentTransition
	NonContributing
)

Edge types

Variables

View Source
var EMPTY = [][][]Point{}

EMPTY slice to represent an empty result

Functions

func CompareBBoxes

func CompareBBoxes(subject, clipping [][][]Point, sbbox, cbbox [4]float64, operation int) [][][]Point

CompareBBoxes compares bounding boxes for trivial solutions.

func CompareEvents

func CompareEvents(e1, e2 *SweepEvent) int

func CompareSegments

func CompareSegments(le1, le2 *SweepEvent) int

CompareSegments compares two SweepEvent instances and returns an integer indicating their order.

func Compute

func Compute(subject, clipping [][][]Point, operation int) [][][]Point

// Compute performs the Compute operation on two polygon sets.

func ComputeFields

func ComputeFields(event, prev *SweepEvent, operation int)

ComputeFields computes various fields for a SweepEvent.

func DetermineResultTransition

func DetermineResultTransition(event *SweepEvent, operation int) int

DetermineResultTransition determines the result transition for a SweepEvent.

func DivideSegment

func DivideSegment(se *SweepEvent, p Point, queue TinyQueue)

DivideSegment divides a SweepEvent at a point and updates the queue.

func Equals

func Equals(p1, p2 Point) bool

Equals checks if two points are equal.

func InResult

func InResult(event *SweepEvent, operation int) bool

InResult determines if a SweepEvent is in the result of the Boolean operation.

func NextPos

func NextPos(pos int, resultEvents []*SweepEvent, processed map[int]bool, origPos int) int

func OrbPolygonToPolygon

func OrbPolygonToPolygon(polygon orb.Polygon) [][]Point

func Orient2d

func Orient2d(p0, p1, p2 Point) float64

Orient2d calculates the orientation of three points.

func PossibleIntersection

func PossibleIntersection(se1, se2 *SweepEvent, queue TinyQueue) int

PossibleIntersection checks for a possible intersection between two sweep events.

func SignedArea

func SignedArea(p0, p1, p2 Point) int

SignedArea determines the orientation of the triangle formed by three points. It returns -1 for counterclockwise, 1 for clockwise, and 0 for collinear points.

func SpecialCases

func SpecialCases(e1, e2 *SweepEvent, p1, p2 Point) int

func TrivialOperation

func TrivialOperation(subject, clipping [][][]Point, operation int) [][][]Point

TrivialOperation checks for trivial cases based on the operation type.

Types

type Comparator

type Comparator func(a, b *SweepEvent) int

Comparator function type

type CompareSeg

type CompareSeg func(le1, le2 *SweepEvent) int

type Contour

type Contour struct {
	Points  []Point
	HoleIds []int
	HoleOf  *Contour
	Depth   int
	Id      int
}

Contour represents a contour with a series of points, holes, and other properties.

func ConnectEdges

func ConnectEdges(sortedEvents []*SweepEvent) []*Contour

ConnectEdges connects edges to form contours.

func InitializeContourFromContext

func InitializeContourFromContext(event *SweepEvent, contours []*Contour, contourId int) *Contour

func NewContour

func NewContour(id int) *Contour

NewContour creates and initializes a new Contour instance.

func (*Contour) IsExterior

func (c *Contour) IsExterior() bool

IsExterior checks if the contour is an exterior contour.

type Key

type Key *SweepEvent // Assuming Key is of type int for simplicity; adjust as needed.

Key Define basic types for Key and Value

type Node

type Node struct {
	Key   *SweepEvent
	Value any
	Left  *Node
	Right *Node
	Next  *Node
}

Node structure

func NewSplayTreeNode

func NewSplayTreeNode(key *SweepEvent) *Node

type NodePrinter

type NodePrinter func(node *Node) string

NodePrinter function type for printing nodes

type Point

type Point struct {
	X, Y float64
}

Point represents a 2D point with X and Y coordinates.

func OrbPointToPoint

func OrbPointToPoint(point orb.Point) Point

func SegmentIntersection

func SegmentIntersection(a1, a2, b1, b2 Point, noEndpointTouch bool) []Point

func (*Point) Equals

func (f *Point) Equals(p Point) bool

type RangeVisitor

type RangeVisitor func(node *Node) bool

type SplayTree

type SplayTree struct {
	Root       *Node
	Size       int
	Comparator Comparator
}

SplayTree structure

func NewSplayTree

func NewSplayTree(comparator Comparator) *SplayTree

NewTree creates a new instance of SplayTree with a given comparator

func (*SplayTree) Add

func (tree *SplayTree) Add(key Key, value Value) *Node

Add inserts a key and its associated data into the tree if the key does not already exist. It returns the new root of the tree after insertion.

func (*SplayTree) At

func (tree *SplayTree) At(index int) *Node

At returns the node at the given index in an in-order traversal of the tree. If the index is out of bounds, it returns nil.

func (*SplayTree) Clear

func (tree *SplayTree) Clear()

Clear removes all nodes from the tree.

func (*SplayTree) Contains

func (tree *SplayTree) Contains(key Key) bool

Contains checks if the tree contains a node with the specified key.

func (*SplayTree) Find

func (tree *SplayTree) Find(key Key) *Node

Find searches for a node by key and splays the tree. If the node is found, it becomes the new root of the tree. If the node is not found, the last accessed node becomes the new root.

func (*SplayTree) FindStatic

func (tree *SplayTree) FindStatic(key Key) (*Node, bool)

FindStatic searches for a node by key without splaying.

func (*SplayTree) ForEach

func (tree *SplayTree) ForEach(visitor Visitor)

ForEach traverses the tree in-order and applies the visitor function to each node.

func (*SplayTree) Insert

func (tree *SplayTree) Insert(node *Node) *Node

Insert Inserts a key, allows duplicates

func (*SplayTree) Keys

func (tree *SplayTree) Keys() []Key

Keys returns a slice of all keys in the tree.

func (*SplayTree) Max

func (tree *SplayTree) Max() (Key, bool)

Max returns the largest key in the tree or zero value if the tree is empty.

func (*SplayTree) MaxNode

func (tree *SplayTree) MaxNode(t *Node) *Node

MaxNode returns the node with the largest key starting from the given node.

func (*SplayTree) Min

func (tree *SplayTree) Min() (Key, bool)

Min returns the smallest key in the tree or zero value if the tree is empty.

func (*SplayTree) MinNode

func (tree *SplayTree) MinNode(t *Node) *Node

MinNode returns the node with the smallest key starting from the given node.

func (*SplayTree) Next

func (tree *SplayTree) Next(d *Node) *Node

Next returns the successor of the given node in the tree.

func (*SplayTree) Pop

func (tree *SplayTree) Pop() (Key, Value, bool)

Pop removes and returns the node with the smallest key.

func (*SplayTree) Prev

func (tree *SplayTree) Prev(d *Node) *Node

Prev returns the predecessor of the given node in the tree.

func (*SplayTree) Range

func (tree *SplayTree) Range(low, high Key, fn RangeVisitor)

Range performs an in-order traversal of the tree within a key range from low to high. If the visitor function returns true, the traversal stops.

func (*SplayTree) Remove

func (tree *SplayTree) Remove(key Key)

Remove is a public method that removes a node with the given key from the tree, if it exists.

func (*SplayTree) ToList

func (tree *SplayTree) ToList() []*Node

ToList converts the tree to a list.

func (*SplayTree) Values

func (tree *SplayTree) Values() []Value

Values returns a slice of all data in the tree nodes.

type SweepEvent

type SweepEvent struct {
	Point            Point
	Left             bool
	OtherEvent       *SweepEvent
	IsSubject        bool
	Type             int
	InOut            bool
	OtherInOut       bool
	PrevInResult     *SweepEvent
	ResultTransition int
	OtherPos         int
	OutputContourId  int
	IsExteriorRing   bool
	ContourId        int
}

func NewSweepEvent

func NewSweepEvent(point Point, left bool, otherEvent *SweepEvent, isSubject bool, edgeType int) *SweepEvent

func OrderEvents

func OrderEvents(sortedEvents []*SweepEvent) []*SweepEvent

func SubdivideSegments

func SubdivideSegments(eventQueue TinyQueue, subject, clipping [][][]Point, sbbox, cbbox *[4]float64, operation int) []*SweepEvent

Subdivide processes the event queue for the sweep line algorithm.

func (*SweepEvent) Clone

func (e *SweepEvent) Clone() *SweepEvent

func (*SweepEvent) InResult

func (e *SweepEvent) InResult() bool

func (*SweepEvent) IsAbove

func (e *SweepEvent) IsAbove(p Point) bool

func (*SweepEvent) IsBelow

func (e *SweepEvent) IsBelow(p Point) bool

func (*SweepEvent) IsVertical

func (e *SweepEvent) IsVertical() bool

type SweepEventComparator

type SweepEventComparator func(e1, e2 *SweepEvent) int

type TinyQueue

type TinyQueue interface {
	Len() int
	Push(*SweepEvent)
	Pop() *SweepEvent
	Peek() *SweepEvent
}

TinyQueueDefault represents a priority queue with an underlying slice.

func FillQueue

func FillQueue(subject, clipping [][][]Point, sbbox, cbbox *[4]float64, operation int) TinyQueue

type TinyQueueAI

type TinyQueueAI struct {
	// contains filtered or unexported fields
}

func NewTinyQueueDefault

func NewTinyQueueDefault(data []*SweepEvent, compare func(a, b *SweepEvent) int) *TinyQueueAI

func (*TinyQueueAI) Len

func (tq *TinyQueueAI) Len() int

func (*TinyQueueAI) Peek

func (tq *TinyQueueAI) Peek() *SweepEvent

func (*TinyQueueAI) Pop

func (tq *TinyQueueAI) Pop() *SweepEvent

func (*TinyQueueAI) Push

func (tq *TinyQueueAI) Push(item *SweepEvent)

type TinyQueueDefault

type TinyQueueDefault struct {
	// contains filtered or unexported fields
}

TinyQueueDefault represents a priority queue with an underlying slice.

func NewTinyQueueDefault2

func NewTinyQueueDefault2(data []*SweepEvent, compare SweepEventComparator) *TinyQueueDefault

NewTinyQueueDefault creates a new instance of TinyQueueDefault It takes an optional slice of data and a compare function.

func (*TinyQueueDefault) Len

func (tq *TinyQueueDefault) Len() int

func (*TinyQueueDefault) Peek

func (tq *TinyQueueDefault) Peek() *SweepEvent

Peek returns the top item from the queue without removing it.

func (*TinyQueueDefault) Pop

func (tq *TinyQueueDefault) Pop() *SweepEvent

Pop removes and returns the top item from the queue.

func (*TinyQueueDefault) Push

func (tq *TinyQueueDefault) Push(item *SweepEvent)

Push adds an item to the queue.

type Value

type Value any

type Visitor

type Visitor func(node *Node)

Visitor function type for traversing nodes

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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