Add integer and rotation data types
This commit is contained in:
parent
565480ca8e
commit
5965b3205c
3 changed files with 198 additions and 1 deletions
120
lib/omv/types/integer.dart
Normal file
120
lib/omv/types/integer.dart
Normal file
|
@ -0,0 +1,120 @@
|
|||
class Integer {
|
||||
static const int _minValue = -2147483648;
|
||||
static const int _maxValue = 2147483647;
|
||||
int _value = 0;
|
||||
|
||||
static final Integer FALSE = Integer(0);
|
||||
static final Integer TRUE = Integer(1);
|
||||
|
||||
int get value => _value;
|
||||
set value(int v) => _value = _clampTo32Bit(v);
|
||||
|
||||
// Constructor
|
||||
Integer(int value) : _value = _clampTo32Bit(value);
|
||||
|
||||
Integer.fromString(String s) {
|
||||
final f = double.parse(s);
|
||||
_value = _clampTo32Bit(f.toInt());
|
||||
}
|
||||
|
||||
// Implicit conversions (in Dart these are done with factory constructors)
|
||||
factory Integer.fromBool(bool x) => x ? TRUE : FALSE;
|
||||
factory Integer.fromInt(int x) => Integer(x);
|
||||
factory Integer.fromDouble(double x) => Integer(_clampTo32Bit(x.toInt()));
|
||||
factory Integer.fromStringExplicit(String x) => Integer.fromString(x);
|
||||
|
||||
// Explicit conversion to bool
|
||||
bool toBool() => value != 0;
|
||||
|
||||
// Logical negation (NOT) operator
|
||||
/// Functions the same as Second Life, where if positive, returns false, if 0/false, then return true.
|
||||
Integer not() {
|
||||
if (value > 0)
|
||||
return FALSE;
|
||||
else
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// Bitwise AND operator
|
||||
Integer operator &(Integer y) => Integer(value & y.value);
|
||||
|
||||
// Bitwise OR operator
|
||||
Integer operator |(Integer y) => Integer(value | y.value);
|
||||
|
||||
// Bitwise NOT operator
|
||||
Integer operator ~() => Integer(~value);
|
||||
|
||||
// Bitwise XOR operator
|
||||
Integer operator ^(Integer b) => Integer(value ^ b.value);
|
||||
|
||||
// Bitwise Shift right
|
||||
Integer operator >>(int b) => Integer(value >> b);
|
||||
|
||||
// Bitwise Shift left
|
||||
Integer operator <<(int b) => Integer(_clampTo32Bit(value << b));
|
||||
|
||||
// Modulus operator
|
||||
Integer operator %(Integer b) => Integer(value % b.value);
|
||||
|
||||
// Definitely true operator
|
||||
bool get isTrue => value != 0;
|
||||
|
||||
// Definitely false operator
|
||||
bool get isFalse => value == 0;
|
||||
|
||||
// Explicit conversion to String
|
||||
String toStringExplicit() => value.toString();
|
||||
|
||||
// Implicit conversion to int
|
||||
int toInt() => value;
|
||||
|
||||
// Addition operator
|
||||
Integer operator +(Integer b) => Integer(value + b.value);
|
||||
|
||||
// Subtraction operator
|
||||
Integer operator -(Integer b) => Integer(value - b.value);
|
||||
|
||||
/*
|
||||
Integer operator -(bool b) => this - Integer.fromBool(b);
|
||||
Integer operator +(bool b) => this + Integer.fromBool(b);
|
||||
*/
|
||||
|
||||
// Multiplication operator
|
||||
Integer operator *(Integer b) => Integer(value * b.value);
|
||||
|
||||
Integer multiplyByDouble(double b) {
|
||||
final result = value * b;
|
||||
return Integer(result.toInt());
|
||||
}
|
||||
|
||||
// Equality operators
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is Integer &&
|
||||
runtimeType == other.runtimeType &&
|
||||
value == other.value;
|
||||
|
||||
@override
|
||||
int get hashCode => value.hashCode;
|
||||
|
||||
//bool operator !=(Object other) => !this.==(other);
|
||||
|
||||
// Comparison method
|
||||
static int compare(Integer a, Integer b) {
|
||||
if (a.value < b.value) return -1;
|
||||
if (a.value > b.value) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Override toString
|
||||
@override
|
||||
String toString() => value.toString();
|
||||
|
||||
// Helper method to clamp the value to a 32-bit signed integer range
|
||||
static int _clampTo32Bit(int v) {
|
||||
if (v < _minValue) return _minValue;
|
||||
if (v > _maxValue) return _maxValue;
|
||||
return v;
|
||||
}
|
||||
}
|
77
lib/omv/types/rotation.dart
Normal file
77
lib/omv/types/rotation.dart
Normal file
|
@ -0,0 +1,77 @@
|
|||
class Rotation {
|
||||
double x = 0;
|
||||
double y = 0;
|
||||
double z = 0;
|
||||
double s = 0;
|
||||
|
||||
static final Rotation zeroRotation = Rotation(0, 0, 0, 1);
|
||||
|
||||
Rotation(this.x, this.y, this.z, this.s);
|
||||
|
||||
Rotation.fromString(String a) {
|
||||
final regex = RegExp(r'<(?<x>[^,]*),(?<y>[^,]*),(?<z>[^,]*),(?<s>[^,]*)>');
|
||||
final match = regex.firstMatch(a);
|
||||
|
||||
if (match != null) {
|
||||
x = double.parse(match.namedGroup('x') ?? '0');
|
||||
y = double.parse(match.namedGroup('y') ?? '0');
|
||||
z = double.parse(match.namedGroup('z') ?? '0');
|
||||
s = double.parse(match.namedGroup('s') ?? '0');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
String toString() {
|
||||
return "<${x.toStringAsFixed(5)}, ${y.toStringAsFixed(5)}, ${z.toStringAsFixed(5)}, ${s.toStringAsFixed(5)}>";
|
||||
}
|
||||
|
||||
Rotation operator -() {
|
||||
return Rotation(-x, -y, -z, -s);
|
||||
}
|
||||
|
||||
Rotation operator +(Rotation other) {
|
||||
return Rotation(x + other.x, y + other.y, z + other.z, s + other.s);
|
||||
}
|
||||
|
||||
Rotation operator *(Rotation other) {
|
||||
return Rotation(
|
||||
other.s * x - other.z * y + other.y * z + other.x * s,
|
||||
other.s * y + other.z * x + other.y * s - other.x * z,
|
||||
other.s * z + other.z * s - other.y * x + other.x * y,
|
||||
other.s * s - other.z * z - other.y * y - other.x * x,
|
||||
);
|
||||
}
|
||||
|
||||
Rotation operator /(Rotation other) {
|
||||
return Rotation(
|
||||
other.s * x + other.z * y - other.y * z - other.x * s,
|
||||
other.s * y - other.z * x - other.y * s + other.x * z,
|
||||
other.s * z - other.z * s + other.y * x - other.x * y,
|
||||
other.s * s + other.z * z + other.y * y + other.x * x,
|
||||
);
|
||||
}
|
||||
|
||||
/// Epsilon
|
||||
static const equalityTolerance = 1e-14;
|
||||
|
||||
bool operator ==(Object other) {
|
||||
if (other is Rotation) {
|
||||
return (x - other.x).abs() <= equalityTolerance &&
|
||||
(y - other.y).abs() <= equalityTolerance &&
|
||||
(z - other.z).abs() <= equalityTolerance &&
|
||||
(s - other.s).abs() <= equalityTolerance;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => x.hashCode ^ y.hashCode ^ z.hashCode ^ s.hashCode;
|
||||
|
||||
bool isTrue() {
|
||||
return !(x == 0 && y == 0 && z == 0 && s == 1);
|
||||
}
|
||||
|
||||
bool isFalse() {
|
||||
return x == 0 && y == 0 && z == 0 && s == 1;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
name: libac_dart
|
||||
description: "Aria's Creations code library"
|
||||
version: 1.2.082224+1226
|
||||
version: 1.2.082224+1252
|
||||
homepage: "https://zontreck.com"
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue