utils

package
v0.2.11 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2023 License: AGPL-3.0 Imports: 26 Imported by: 22

Documentation

Overview

Package utils contains all utility functions that currently have no better home than here. Consider moving them to go.viam.com/utils.

Index

Constants

View Source
const (
	// CredentialsTypeRobotSecret is for credentials used against the cloud managing this robot.
	CredentialsTypeRobotSecret = "robot-secret"

	// CredentialsTypeRobotLocationSecret is for credentials used against the cloud managing this robot's location.
	CredentialsTypeRobotLocationSecret = "robot-location-secret"
)
View Source
const (
	CLFMessageTypeParam            = CLFMessageType("PARAM")
	CLFMessageTypeComment          = CLFMessageType("COMMENT")
	CLFMessageTypeSync             = CLFMessageType("SYNC")
	CLFMessageTypeOdometry         = CLFMessageType("ODOM")
	CLFMessageTypeTruePos          = CLFMessageType("TRUEPOS")
	CLFMessageTypeRawLaser1        = CLFMessageType("RAWLASER1")
	CLFMessageTypeRawLaser2        = CLFMessageType("RAWLASER2")
	CLFMessageTypeRawLaser3        = CLFMessageType("RAWLASER3")
	CLFMessageTypeRawLaser4        = CLFMessageType("RAWLASER4")
	CLFMessageTypePositionLaser    = CLFMessageType("POSITIONLASER")
	CLFMessageTypeRobotLaser1      = CLFMessageType("ROBOTLASER1")
	CLFMessageTypeRobotLaser2      = CLFMessageType("ROBOTLASER2")
	CLFMessageTypeNMEAGGA          = CLFMessageType("NMEAGGA")
	CLFMessageTypeNMEARMC          = CLFMessageType("NMEARMC")
	CLFMessageTypeSonar            = CLFMessageType("SONAR")
	CLFMessageTypeBumper           = CLFMessageType("BUMPER")
	CLFMessageTypeScanMark         = CLFMessageType("SCANMARK")
	CLFMessageTypeIMU              = CLFMessageType("IMU")
	CLFMessageTypeVectorMove       = CLFMessageType("VECTORMOVE")
	CLFMessageTypeRobotVelocity    = CLFMessageType("ROBOTVELOCITY")
	CLFMessageTypeFollowTrajectory = CLFMessageType("FOLLOWTRAJECTORY")
	CLFMessageTypeBaseVelocity     = CLFMessageType("BASEVELOCITY")
	CLFMessageTypeOld              = CLFMessageType("OLD")
	CLFMessageTypeFrontLaser       = CLFMessageType("FLASER")
	CLFMessageTypeRearLaser        = CLFMessageType("RLASER")
	CLFMessageTypeLaser3           = CLFMessageType("LASER3")
	CLFMessageTypeLaser4           = CLFMessageType("LASER4")
	CLFMessageTypeRemissionFLaser  = CLFMessageType("REMISSIONFLASER")
	CLFMessageTypeRemissionRLaser  = CLFMessageType("REMISSIONRLASER")
	CLFMessageTypeRemissionLaser3  = CLFMessageType("REMISSIONLASER3")
	CLFMessageTypeRemissionLaser4  = CLFMessageType("REMISSIONLASER4")
)

known message types.

View Source
const (
	// MimeTypeSuffixLazy is used to indicate a lazy loading of data.
	MimeTypeSuffixLazy = "lazy"

	// MimeTypeRawRGBA is for go's internal image.NRGBA. This uses the custom header as
	// explained in the comments for rimage.DecodeImage and rimage.EncodeImage.
	MimeTypeRawRGBA = "image/vnd.viam.rgba"

	// MimeTypeRawRGBALazy is a lazy MimeTypeRawRGBA.
	MimeTypeRawRGBALazy = MimeTypeRawRGBA + "+" + MimeTypeSuffixLazy

	// MimeTypeRawDepth is for depth images.
	MimeTypeRawDepth = "image/vnd.viam.dep"

	// MimeTypeJPEG is regular jpgs.
	MimeTypeJPEG = "image/jpeg"

	// MimeTypePNG is regular pngs.
	MimeTypePNG = "image/png"

	// MimeTypePCD is for .pcd pountcloud files.
	MimeTypePCD = "pointcloud/pcd"

	// MimeTypeQOI is for .qoi "Quite OK Image" for lossless, fast encoding/decoding.
	MimeTypeQOI = "image/qoi"

	// MimeTypeTabular used to indicate tabular data, this is used mainly for filtering data.
	MimeTypeTabular = "x-application/tabular"

	// MimeTypeDefault used if mimetype cannot be inferred.
	MimeTypeDefault = "application/octet-stream"
)

Variables

View Source
var ParallelFactor = runtime.GOMAXPROCS(0)

ParallelFactor controls the max level of parallelization. This might be useful to set in tests where too much parallelism actually slows tests down in aggregate.

Functions

func AbsInt

func AbsInt(n int) int

AbsInt returns the absolute value of the given value.

func AbsInt64

func AbsInt64(n int64) int64

AbsInt64 returns the absolute value of the given value.

func AngleDiffDeg

func AngleDiffDeg(a1, a2 float64) float64

AngleDiffDeg returns the closest difference from the two given angles. The arguments are commutative.

func AntiCWDeg

func AntiCWDeg(deg float64) float64

AntiCWDeg flips the given degrees as if you were to start at 0 and go counter-clockwise or vice versa.

func BytesFromFloat32BE

func BytesFromFloat32BE(v float32) []byte

BytesFromFloat32BE converts a float32 to an array of bytes ordered in big-endian.

func BytesFromFloat32LE

func BytesFromFloat32LE(v float32) []byte

BytesFromFloat32LE converts a float32 to an array of bytes ordered in little-endian.

func BytesFromFloat64BE

func BytesFromFloat64BE(v float64) []byte

BytesFromFloat64BE converts a float64 to an array of bytes ordered in big-endian.

func BytesFromFloat64LE

func BytesFromFloat64LE(v float64) []byte

BytesFromFloat64LE converts a float64 to an array of bytes ordered in little-endian.

func BytesFromUint32BE

func BytesFromUint32BE(v uint32) []byte

BytesFromUint32BE converts a uint32 to an array of bytes ordered in big-endian.

func BytesFromUint32LE

func BytesFromUint32LE(v uint32) []byte

BytesFromUint32LE converts a uint32 to an array of bytes ordered in little-endian.

func CheckLazyMIMEType added in v0.0.8

func CheckLazyMIMEType(mimeType string) (string, bool)

CheckLazyMIMEType checks the lazy suffix of a MIME.

func Clamp

func Clamp(value, min, max float64) float64

Clamp returns min if value is lesser than min, max if value is greater them max or value if the input value is between min and max.

func ComputeDistance

func ComputeDistance(p1, p2 []float64, distType DistanceType) (float64, error)

ComputeDistance computes the distance between two vectors stored in a slice of floats.

func CubeRoot

func CubeRoot(x float64) float64

CubeRoot returns the cube root of the given value.

func CycleIntSliceByN

func CycleIntSliceByN(s []int, n int) []int

CycleIntSliceByN cycles the list to the right by n steps.

func DegToRad

func DegToRad(degrees float64) float64

DegToRad converts degrees to radians.

func DependencyNotFoundError

func DependencyNotFoundError(name string) error

DependencyNotFoundError is used when a resource is not found in a dependencies.

func DependencyTypeError

func DependencyTypeError(name string, expected, actual interface{}) error

DependencyTypeError is used when a resource doesn't implement the expected interface.

func EuclideanDistance

func EuclideanDistance(p1, p2 []float64) (float64, error)

EuclideanDistance computes the euclidean distance between 2 vectors.

func Float32FromBytesBE

func Float32FromBytesBE(bytes []byte) float32

Float32FromBytesBE converts an array of byte ordered in big-endian to a float32.

func Float32FromBytesLE

func Float32FromBytesLE(bytes []byte) float32

Float32FromBytesLE converts an array of byte ordered in little-endian to a float32.

func Float64AlmostEqual

func Float64AlmostEqual(a, b, epsilon float64) bool

Float64AlmostEqual compares two float64s and returns if the difference between them is less than epsilon.

func Float64FromBytesBE

func Float64FromBytesBE(bytes []byte) float64

Float64FromBytesBE converts an array of byte ordered in big-endian to a float64.

func Float64FromBytesLE

func Float64FromBytesLE(bytes []byte) float64

Float64FromBytesLE converts an array of byte ordered in little-endian to a float64.

func GetArgMinDistancesPerRow

func GetArgMinDistancesPerRow(distances *mat.Dense) []int

GetArgMinDistancesPerRow returns in a slice of int the index of the point with minimum distance for each row.

func GetArgMinDistancesPerRowInt added in v0.0.9

func GetArgMinDistancesPerRowInt(distances [][]int) []int

GetArgMinDistancesPerRowInt returns in a slice of int the index of the point with minimum distance for each row.

func GroupWorkParallel

func GroupWorkParallel(ctx context.Context, totalSize int, before BeforeParallelGroupWorkFunc, groupWork GroupWorkFunc) error

GroupWorkParallel parallelizes the given size of work over multiple workers.

func HammingDistance

func HammingDistance(p1, p2 []float64) (float64, error)

HammingDistance computes the hamming distance between two vectors that only contain zeros and ones.

func Int16FromBytesBE added in v0.2.5

func Int16FromBytesBE(bytes []byte) int16

Int16FromBytesBE converts an array of bytes ordered in big-endian to a (signed) int16.

func Int16FromBytesLE added in v0.2.5

func Int16FromBytesLE(bytes []byte) int16

Int16FromBytesLE converts an array of bytes ordered in little-endian to a (signed) int16.

func MaxInt

func MaxInt(a, b int) int

MaxInt returns the maximum of two values.

func MaxUint8

func MaxUint8(a, b uint8) uint8

MaxUint8 returns the maximum of two values.

func Median

func Median(values ...float64) float64

Median returns the median value of the given values. If there are no values, NaN is returned.

func MinInt

func MinInt(a, b int) int

MinInt returns the minimum of two values.

func MinUint8

func MinUint8(a, b uint8) uint8

MinUint8 returns the minimum of two values.

func ModAngDeg

func ModAngDeg(ang float64) float64

ModAngDeg returns the given angle modulus 360 and resolves any negativity.

func NewRemoteResourceClashError

func NewRemoteResourceClashError(name string) error

NewRemoteResourceClashError is used when you are more than one resource with the same name exist.

func NewResourceNotAvailableError added in v0.1.6

func NewResourceNotAvailableError(name resource.Name, err error) error

NewResourceNotAvailableError is used when a resource is not available because of some error.

func NewResourceNotFoundError

func NewResourceNotFoundError(name resource.Name) error

NewResourceNotFoundError is used when a resource is not found.

func NewUnexpectedTypeError

func NewUnexpectedTypeError(expected, actual interface{}) error

NewUnexpectedTypeError is used when there is a type mismatch.

func NewUnimplementedInterfaceError

func NewUnimplementedInterfaceError(expected, actual interface{}) error

NewUnimplementedInterfaceError is used when there is a failed interface check. Future: This should also tell you that expected is not even an interface.

func PairwiseDistance

func PairwiseDistance(pts1, pts2 [][]float64, distType DistanceType) (*mat.Dense, error)

PairwiseDistance computes the pairwise distances between 2 sets of points.

func ParallelForEachPixel

func ParallelForEachPixel(size image.Point, f func(x, y int))

ParallelForEachPixel loops through the image and calls f functions for each [x, y] position. The image is divided into N * N blocks, where N is the number of available processor threads. For each block a parallel Goroutine is started.

func RadToDeg

func RadToDeg(radians float64) float64

RadToDeg converts radians to degrees.

func ResolveFile

func ResolveFile(fn string) string

ResolveFile returns the path of the given file relative to the root of the codebase. For example, if this file currently lives in utils/file.go and ./foo/bar/baz is given, then the result is foo/bar/baz. This is helpful when you don't want to relatively refer to files when you're not sure where the caller actually lives in relation to the target file.

func RunInParallel

func RunInParallel(ctx context.Context, fs []SimpleFunc) (time.Duration, error)

RunInParallel runs all functions in parallel, return is elapsed time and n error.

func SampleNIntegersNormal

func SampleNIntegersNormal(n int, vMin, vMax float64) []int

SampleNIntegersNormal samples n integers from normal distribution centered around (vMax+vMin) / 2 and in range [vMin, vMax].

func SampleNIntegersUniform

func SampleNIntegersUniform(n int, vMin, vMax float64) []int

SampleNIntegersUniform samples n integers uniformly in [vMin, vMax].

func SampleNRegularlySpaced

func SampleNRegularlySpaced(n int, vMin, vMax float64) []int

SampleNRegularlySpaced returns the same set of evenly divided numbers every time, and is mostly used for testing purposes.

func SampleRandomIntRange

func SampleRandomIntRange(min, max int, r *rand.Rand) int

SampleRandomIntRange samples a random integer within a range given by [min, max] using the given rand.Rand.

func ScaleByPct

func ScaleByPct(n int, pct float64) int

ScaleByPct scales a max number by a floating point percentage between two bounds [0, n].

func SelectNIndicesWithoutReplacement

func SelectNIndicesWithoutReplacement(nSamples, nMax int) ([]int, error)

SelectNIndicesWithoutReplacement select N random indices from [0,nMax] without replacement (no duplicate indices).

func Single

func Single(dim int, x []float64) [][]float64

Single generates an n-dimensional Grid using a single set of values. dim specifies the number of dimensions, the entries in x specify the gridded values.

func Square

func Square(n float64) float64

Square returns the square of the given value. Math.pow( x, 2 ) is slow, this is faster.

func SquareInt

func SquareInt(n int) int

SquareInt returns the square of the given value. Math.pow( x, 2 ) is slow, this is faster.

func SubFor

func SubFor(sub []int, idx int, dims []int) []int

SubFor constructs the multi-dimensional subscript for the input linear index. dims specify the maximum size in each dimension. If sub is non-nil the result is stored in-place into sub. If it is nil a new slice of the appropriate length is allocated.

func Transpose added in v0.0.9

func Transpose(slice [][]int) [][]int

Transpose transposes the slice of slice of ints.

func Uint32FromBytesBE

func Uint32FromBytesBE(bytes []byte) uint32

Uint32FromBytesBE converts an array of bytes ordered in big-endian to a uint32.

func Uint32FromBytesLE

func Uint32FromBytesLE(bytes []byte) uint32

Uint32FromBytesLE converts an array of bytes ordered in little-endian to a uint32.

func UnwrapProxy

func UnwrapProxy(v interface{}) interface{}

UnwrapProxy unwraps a proxy as far as possible.

func Walk

func Walk(middleX, middleY, maxRadius int, f WalkCallback) error

Walk starts at the given middle point and walks around increasingly bigger squares based on the given radius growing outwards.

func WithLazyMIMEType added in v0.0.8

func WithLazyMIMEType(mimeType string) string

WithLazyMIMEType attaches the lazy suffix to a MIME.

Types

type BeforeParallelGroupWorkFunc

type BeforeParallelGroupWorkFunc func(groupSize int)

BeforeParallelGroupWorkFunc executes before any work starts with the calculated group size.

type CLFBaseMessage

type CLFBaseMessage struct {
	MessageType     CLFMessageType
	IPCTimestamp    float64
	IPCHostname     string
	LoggerTimestamp float64
}

CLFBaseMessage is used by all messages and contains basic information about the message.

func (CLFBaseMessage) Base

func (b CLFBaseMessage) Base() CLFBaseMessage

Base returns the base part of the message.

func (CLFBaseMessage) Type

func (b CLFBaseMessage) Type() CLFMessageType

Type returns the type of message this is.

type CLFMessage

type CLFMessage interface {
	Base() CLFBaseMessage
	Type() CLFMessageType
}

CLFMessage is a specific type of CLF message that always has a base message.

type CLFMessageType

type CLFMessageType string

CLFMessageType describes a specific type of message.

type CLFOdometryMessage

type CLFOdometryMessage struct {
	CLFMessage
	X                     float64
	Y                     float64
	Theta                 float64
	TranslationalVelocity float64
	RotationalVelocity    float64
	Acceleration          float64
}

CLFOdometryMessage represents odometry data.

type CLFOldLaserMessage

type CLFOldLaserMessage struct {
	CLFMessage
	RangeReadings []float64
	X             float64
	Y             float64
	Theta         float64
	OdomX         float64
	OdomY         float64
	OdomTheta     float64
}

CLFOldLaserMessage represents legacy lidar scan data.

type CLFParamMessage

type CLFParamMessage struct {
	CLFMessage
	Name, Value string
}

CLFParamMessage conveys parameters being set for the whole CLF.

type CLFReader

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

A CLFReader can read in CARMEN Logfiles.

func NewCLFReader

func NewCLFReader(reader io.Reader) *CLFReader

NewCLFReader returns a CLF reader based on the given reader.

func (*CLFReader) Process

func (r *CLFReader) Process(f func(message CLFMessage) error) error

Process reads over all messages and calls the given function for each message. If the function returns an error, execution stops with that error returned to the caller.

type DistanceType

type DistanceType int

DistanceType defines the type of distance used in a function.

const (
	// Euclidean is DistanceType 0.
	Euclidean DistanceType = iota
	// Hamming is DistanceType 1.
	Hamming
)

type GroupWorkDoneFunc

type GroupWorkDoneFunc func()

GroupWorkDoneFunc runs when a single group's work is done; helpful for merge stages.

type GroupWorkFunc

type GroupWorkFunc func(groupNum, groupSize, from, to int) (MemberWorkFunc, GroupWorkDoneFunc)

GroupWorkFunc runs to determine what work members should do, if any.

type MemberWorkFunc

type MemberWorkFunc func(memberNum, workNum int)

MemberWorkFunc runs for each work item (member) of a group.

type ProxyType

type ProxyType interface {
	ProxyFor() interface{}
}

A ProxyType is a type that proxies behavior on behalf of some other type. This uses empty interfaces in lieu of generics existing and not wanting to duplicate much code. Ideally this never needs to be used but in cases where a concrete type must be reached and it's interface cannot be used, this is useful.

type RollingAverage

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

RollingAverage computes an average in a moving window of a certain size. It is goroutine-safe but should be used for statistical purposes only due to the use of atomics and not mutexes.

func NewRollingAverage

func NewRollingAverage(windowSize int) *RollingAverage

NewRollingAverage returns a rolling average computed on the given window size.

func (*RollingAverage) Add

func (ra *RollingAverage) Add(x int)

Add adds the given value to the samples.

func (*RollingAverage) Average

func (ra *RollingAverage) Average() int

Average recomputes and returns the current rolling average.

func (*RollingAverage) NumSamples

func (ra *RollingAverage) NumSamples() int

NumSamples returns the number of samples currently collected.

type SimpleFunc

type SimpleFunc func(ctx context.Context) error

SimpleFunc is for RunInParallel.

type TypedName

type TypedName struct {
	Name string
	Type string
}

A TypedName stores both the name and type of the variable.

func JSONTags

func JSONTags(s interface{}) []TypedName

JSONTags returns a slice of strings of the variable names in the JSON tags of a struct.

type Vec2Fs

type Vec2Fs [][]float64

Vec2Fs is a series of two dimensional vectors that are float based.

func (Vec2Fs) Len

func (vs Vec2Fs) Len() int

Len returns the number of vectors.

func (Vec2Fs) Less

func (vs Vec2Fs) Less(i, j int) bool

Less returns which vector compares less by first checking if the X component is less, then if the X component is more, and finally if the Y component is less.

func (Vec2Fs) Swap

func (vs Vec2Fs) Swap(i, j int)

Swap swaps two vectors by positionally.

type Vec2Matrix

type Vec2Matrix mat.Dense

Vec2Matrix is a wrapper around a mat.Dense intended for a series of 2D vectors.

func (*Vec2Matrix) DistanceMSETo

func (v2m *Vec2Matrix) DistanceMSETo(to *Vec2Matrix) float64

DistanceMSETo returns the mean squared error between this matrix and the given matrix. This is helpful for calculating how "far off" two matrices are.

func (*Vec2Matrix) RotateMatrixAbout

func (v2m *Vec2Matrix) RotateMatrixAbout(x, y, theta float64) *Vec2Matrix

RotateMatrixAbout rotates every vector by the given theta about a given point.

type WalkCallback

type WalkCallback func(x, y int) error

WalkCallback is to be called for each point visited by Walk.

Jump to

Keyboard shortcuts

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