binary

package
v0.12.1 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2019 License: BSD-2-Clause Imports: 5 Imported by: 2

Documentation

Overview

Package binary implements simple translation between numbers and byte sequences.

Numbers are translated by reading and writing fixed-size values. A fixed-size value is either a fixed-size arithmetic type (bool, int8, uint8, int16, float32, complex64, ...) or an array or struct containing only fixed-size values.

Even though the defined structs don't implement a common interface, they all follow the same `generic` interface, and the only difference is the type of the value:

type Component interface {
  Scalar([]byte) interface{}
  Vec2([]byte) [2]interface{}
  Vec3([]byte) [2]interface{}
  Vec4([]byte) [2]interface{}
  Mat2([]byte) [2][2]interface{}
  Mat3([]byte) [3][3]interface{}
  Mat4([]byte) [4][4]interface{}
  PutScalar([]byte, interface{})
  PutVec2([]byte, [2]interface{})
  PutVec3([]byte, [3]interface{})
  PutVec4([]byte, [4]interface{})
  PutMat2([]byte, [2][2]interface{})
  PutMat3([]byte, [3][3]interface{})
  PutMat4([]byte, [4][4]interface{})
}

This package favors simplicity and compliance over efficiency, but it is still an order of magnitude more performant than using the built-int `binary` package, as it implements fast paths for basic glTF types and slices.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Byte byteComponent

Byte is the byte implementation of Component.

View Source
var Float floatComponent

Float is the float implementation of Component.

View Source
var Short shortComponent

Short is the byte short of Component.

View Source
var UnsignedByte ubyteComponent

UnsignedByte is the usnigned byte implementation of Component.

View Source
var UnsignedInt uintComponent

UnsignedInt is the unsigned int implementation of Component.

View Source
var UnsignedShort ushortComponent

UnsignedShort is the usnigned short implementation of Component.

Functions

func ComponentsOf

func ComponentsOf(t gltf.AccessorType) int

ComponentsOf returns the number of components of an accessor type.

func Read

func Read(b []byte, data interface{}) error

Read reads structured binary data from r into data. Data should be a slice of glTF predefined fixed-size types, else it fallbacks to `encoding/binary.Read`.

If data length is greater than the length of b, Read returns io.ErrShortBuffer.

Example
package main

import (
	"fmt"
	"github.com/qmuntal/gltf"
	"github.com/qmuntal/gltf/binary"
)

func main() {
	// Allocate data
	indices := make([]uint8, 18)
	vertices := make([][3]float32, 5)

	// Define buffer
	b := []byte{0, 1, 2, 3, 1, 0, 0, 2, 3, 1, 4, 2, 4, 3, 2, 4, 1, 3, 0, 0, 44, 66, 0, 0, 44, 66, 0, 0, 0, 0, 0, 0, 166, 66, 0, 0, 44, 66, 0, 0, 0, 0, 0, 0, 124, 66, 0, 0, 124, 66, 0, 0, 32, 66, 0, 0, 44, 66, 0, 0, 166, 66, 0, 0, 0, 0, 0, 0, 166, 66, 0, 0, 166, 66, 0, 0, 0, 0}
	sizeIndices := len(indices) * binary.SizeOfElement(gltf.UnsignedByte, gltf.Scalar)

	// Write
	binary.Read(b, indices)
	binary.Read(b[sizeIndices:], vertices)

	fmt.Println(indices)
	fmt.Println(vertices)
}
Output:

[0 1 2 3 1 0 0 2 3 1 4 2 4 3 2 4 1 3]
[[43 43 0] [83 43 0] [63 63 40] [43 83 0] [83 83 0]]

func SizeOf

func SizeOf(c gltf.ComponentType) int

SizeOf returns the size, in bytes, of a component type.

func SizeOfElement

func SizeOfElement(c gltf.ComponentType, t gltf.AccessorType) int

SizeOfElement returns the size, in bytes, of an element.

func Write

func Write(b []byte, data interface{}) error

Write writes the binary representation of data into b. Data must be a slice of glTF predefined fixed-size types, else it fallbacks to `encoding/binary.Write`.

Example
package main

import (
	"fmt"
	"github.com/qmuntal/gltf"
	"github.com/qmuntal/gltf/binary"
)

func main() {
	// Define data
	indices := []uint8{0, 1, 2, 3, 1, 0, 0, 2, 3, 1, 4, 2, 4, 3, 2, 4, 1, 3}
	vertices := [][3]float32{{43, 43, 0}, {83, 43, 0}, {63, 63, 40}, {43, 83, 0}, {83, 83, 0}}

	// Allocate buffer
	sizeIndices := len(indices) * binary.SizeOfElement(gltf.UnsignedByte, gltf.Scalar)
	sizeVertices := len(vertices) * binary.SizeOfElement(gltf.Float, gltf.Vec3)
	b := make([]byte, sizeIndices+sizeVertices)

	// Write
	binary.Write(b, indices)
	binary.Write(b[sizeIndices:], vertices)

	fmt.Print(b)
}
Output:

[0 1 2 3 1 0 0 2 3 1 4 2 4 3 2 4 1 3 0 0 44 66 0 0 44 66 0 0 0 0 0 0 166 66 0 0 44 66 0 0 0 0 0 0 124 66 0 0 124 66 0 0 32 66 0 0 44 66 0 0 166 66 0 0 0 0 0 0 166 66 0 0 166 66 0 0 0 0]

Types

This section is empty.

Jump to

Keyboard shortcuts

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