forked from spatial-go/geoos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
57 changed files
with
1,912 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ on: | |
push: | ||
branches: [ main ] | ||
pull_request: | ||
branches: [ develop ] | ||
branches: [ main ] | ||
|
||
jobs: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Package bytevalues ead and write primitive datatypes from/to byte | ||
package bytevalues | ||
|
||
import "math" | ||
|
||
// Byte order: big endian or little endian | ||
const ( | ||
BigEndian = 0 | ||
LittleEndian = 1 | ||
) | ||
|
||
// GetInt32 Returns int32 read primitive datatypes from/to byte | ||
// sequences, allowing the byte order to be specified | ||
func GetInt32(buf []byte, byteOrder int) uint32 { | ||
if byteOrder == BigEndian { | ||
return uint32((buf[0]&0xff))<<24 | uint32((buf[1]&0xff))<<16 | uint32((buf[2]&0xff))<<8 | uint32(buf[3]&0xff) | ||
} | ||
return uint32((buf[3]&0xff))<<24 | uint32((buf[2]&0xff))<<16 | uint32((buf[1]&0xff))<<8 | uint32(buf[0]&0xff) | ||
|
||
} | ||
|
||
// PutInt32 write primitive datatypes from/to byte | ||
// sequences, allowing the byte order to be specified | ||
func PutInt32(buf []byte, intValue int32, byteOrder int) { | ||
if byteOrder == BigEndian { | ||
buf[0] = byte(intValue >> 24) | ||
buf[1] = byte(intValue >> 16) | ||
buf[2] = byte(intValue >> 8) | ||
buf[3] = byte(intValue) | ||
} else { // LITTLE_ENDIAN | ||
buf[0] = byte(intValue) | ||
buf[1] = byte(intValue >> 8) | ||
buf[2] = byte(intValue >> 16) | ||
buf[3] = byte(intValue >> 24) | ||
} | ||
} | ||
|
||
// GetInt64 Returns int64 read primitive datatypes from/to byte | ||
// sequences, allowing the byte order to be specified | ||
func GetInt64(buf []byte, byteOrder int) uint64 { | ||
if byteOrder == BigEndian { | ||
return uint64((buf[0]&0xff))<<56 | uint64(buf[1]&0xff)<<48 | | ||
uint64(buf[2]&0xff)<<40 | uint64(buf[3]&0xff)<<32 | | ||
uint64(buf[4]&0xff)<<24 | uint64(buf[5]&0xff)<<16 | | ||
uint64(buf[6]&0xff)<<8 | uint64(buf[7]&0xff) | ||
} | ||
return uint64(buf[7]&0xff)<<56 | uint64(buf[6]&0xff)<<48 | | ||
uint64(buf[5]&0xff)<<40 | uint64(buf[4]&0xff)<<32 | | ||
uint64(buf[3]&0xff)<<24 | uint64(buf[2]&0xff)<<16 | | ||
uint64(buf[1]&0xff)<<8 | uint64(buf[0]&0xff) | ||
} | ||
|
||
// PutInt64 write primitive datatypes from/to byte | ||
// sequences, allowing the byte order to be specified | ||
func PutInt64(buf []byte, intValue int64, byteOrder int) { | ||
if byteOrder == BigEndian { | ||
buf[0] = byte(intValue >> 56) | ||
buf[1] = byte(intValue >> 48) | ||
buf[2] = byte(intValue >> 40) | ||
buf[3] = byte(intValue >> 32) | ||
buf[4] = byte(intValue >> 24) | ||
buf[5] = byte(intValue >> 16) | ||
buf[6] = byte(intValue >> 8) | ||
buf[7] = byte(intValue) | ||
} else { // LITTLE_ENDIAN | ||
buf[0] = byte(intValue) | ||
buf[1] = byte(intValue >> 8) | ||
buf[2] = byte(intValue >> 16) | ||
buf[3] = byte(intValue >> 24) | ||
buf[4] = byte(intValue >> 32) | ||
buf[5] = byte(intValue >> 40) | ||
buf[6] = byte(intValue >> 48) | ||
buf[7] = byte(intValue >> 56) | ||
} | ||
} | ||
|
||
// GetFloat32 Returns float32 read primitive datatypes from/to byte | ||
// sequences, allowing the byte order to be specified | ||
func GetFloat32(buf []byte, byteOrder int) float32 { | ||
longVal := GetInt32(buf, byteOrder) | ||
return math.Float32frombits(longVal) | ||
} | ||
|
||
// PutFloat32 write primitive datatypes from/to byte | ||
// sequences, allowing the byte order to be specified | ||
func PutFloat32(buf []byte, floatValue float32, byteOrder int) { | ||
longVal := math.Float32bits(floatValue) | ||
PutInt32(buf, int32(longVal), byteOrder) | ||
} | ||
|
||
// GetFloat64 Returns float64 read primitive datatypes from/to byte | ||
// sequences, allowing the byte order to be specified | ||
func GetFloat64(buf []byte, byteOrder int) float64 { | ||
longVal := GetInt64(buf, byteOrder) | ||
return math.Float64frombits(longVal) | ||
} | ||
|
||
// PutFloat64 write primitive datatypes from/to byte | ||
// sequences, allowing the byte order to be specified | ||
func PutFloat64(buf []byte, floatValue float64, byteOrder int) { | ||
longVal := math.Float64bits(floatValue) | ||
PutInt64(buf, int64(longVal), byteOrder) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Package bytevalues ead and write primitive datatypes from/to byte | ||
package bytevalues | ||
|
||
import "testing" | ||
|
||
func TestGetInt32(t *testing.T) { | ||
type args struct { | ||
buf []byte | ||
byteOrder int | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want uint32 | ||
}{ | ||
{"Getint32", args{[]byte{1, 0, 0, 32}, 1}, 1}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := GetInt32(tt.args.buf, tt.args.byteOrder); (got&0xffff)%1000 != tt.want { | ||
t.Errorf("GetInt32() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package sweepline | ||
|
||
// Sweepline const parameter. | ||
const ( | ||
InsertStatus = iota + 1 | ||
DeleteStatus | ||
) | ||
|
||
// Event ... | ||
type Event struct { | ||
xValue float64 | ||
eventType int | ||
InsertEvent *Event // null if this is an INSERT event | ||
DeleteEventIndex int | ||
|
||
SweepInt *Interval | ||
} | ||
|
||
// NewEvent create a default NewEvent. | ||
func NewEvent(x float64, insertEvent *Event, sweepInt *Interval) *Event { | ||
s := &Event{} | ||
s.xValue = x | ||
s.InsertEvent = insertEvent | ||
s.eventType = InsertStatus | ||
if s.InsertEvent != nil { | ||
s.eventType = DeleteStatus | ||
} | ||
s.SweepInt = sweepInt | ||
return s | ||
} | ||
|
||
// IsInsert ... | ||
func (s *Event) IsInsert() bool { | ||
return s.InsertEvent == nil | ||
} | ||
|
||
// IsDelete ... | ||
func (s *Event) IsDelete() bool { | ||
return s.InsertEvent != nil | ||
} | ||
|
||
// Compare ProjectionEvents are ordered first by their x-value, and then by their eventType. | ||
// It is important that Insert events are sorted before Delete events, so that | ||
// items whose Insert and Delete events occur at the same x-value will be correctly handled. | ||
func (s *Event) Compare(pe *Event) int { | ||
if s.xValue < pe.xValue { | ||
return -1 | ||
} | ||
if s.xValue > pe.xValue { | ||
return 1 | ||
} | ||
if s.eventType < pe.eventType { | ||
return -1 | ||
} | ||
if s.eventType > pe.eventType { | ||
return 1 | ||
} | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//Package sweepline Contains struct which implement a sweepline algorithm for scanning geometric data structures. | ||
package sweepline | ||
|
||
import ( | ||
"sort" | ||
) | ||
|
||
// Index A sweepline implements a sorted index on a set of intervals. | ||
// It is used to compute all overlaps between the interval in the index. | ||
type Index struct { | ||
events LineEvents | ||
indexBuilt bool | ||
nOverlaps int | ||
} | ||
|
||
// Add ... | ||
func (s *Index) Add(sweepInt *Interval) { | ||
insertEvent := NewEvent(sweepInt.Min, nil, sweepInt) | ||
s.events = append(s.events, insertEvent) | ||
s.events = append(s.events, NewEvent(sweepInt.Max, insertEvent, sweepInt)) | ||
} | ||
|
||
// buildIndex Because Delete Events have a link to their corresponding Insert event, | ||
// it is possible to compute exactly the range of events which must be | ||
// compared to a given Insert event object. | ||
func (s *Index) buildIndex() { | ||
if s.indexBuilt { | ||
return | ||
} | ||
sort.Sort(s.events) | ||
for i, ev := range s.events { | ||
if ev.IsDelete() { | ||
ev.InsertEvent.DeleteEventIndex = i | ||
} | ||
} | ||
s.indexBuilt = true | ||
} | ||
|
||
// ComputeOverlaps compute overlaps. | ||
func (s *Index) ComputeOverlaps(action OverlapAction) { | ||
s.nOverlaps = 0 | ||
s.buildIndex() | ||
|
||
for i, ev := range s.events { | ||
if ev.IsInsert() { | ||
s.processOverlaps(i, ev.DeleteEventIndex, ev.SweepInt, action) | ||
} | ||
} | ||
} | ||
|
||
func (s *Index) processOverlaps(start, end int, s0 *Interval, action OverlapAction) { | ||
/** | ||
* Since we might need to test for self-intersections, | ||
* include current insert event object in list of event objects to test. | ||
* Last index can be skipped, because it must be a Delete event. | ||
*/ | ||
for i := start; i < end; i++ { | ||
ev := s.events[i] | ||
if ev.IsInsert() { | ||
if action.Overlap(s0, ev.SweepInt) { | ||
s.nOverlaps++ | ||
} | ||
} | ||
} | ||
} | ||
|
||
// LineEvents ... | ||
type LineEvents []*Event | ||
|
||
// Len ... | ||
func (it LineEvents) Len() int { | ||
return len(it) | ||
} | ||
|
||
// Less ... | ||
func (it LineEvents) Less(i, j int) bool { | ||
return it[i].Compare(it[j]) == -1 | ||
} | ||
|
||
// Swap ... | ||
func (it LineEvents) Swap(i, j int) { | ||
it[i], it[j] = it[j], it[i] | ||
} |
Oops, something went wrong.