Initial commit. I can successfully create random tensor.

This commit is contained in:
Jordan
2024-12-13 05:59:57 -08:00
commit 1162ca9cf9
146 changed files with 5117 additions and 0 deletions

40
lib/src/splat/camera.dart Normal file
View File

@ -0,0 +1,40 @@
import 'dart:typed_data';
import 'package:archimedes_test/src/splat/tensor.dart';
import 'package:camera/camera.dart';
class Camera {
int id = -1;
int width = 0;
int height = 0;
double fx = 0;
double fy = 0;
double cx = 0;
double cy = 0;
double k1 = 0;
double k2 = 0;
double k3 = 0;
double p1 = 0;
double p2 = 0;
ByteBuffer? image;
Tensor? camToWorld;
List<List<double>> getIntrinsicsMatrix() {
return [
[fx, 0.0, cx],
[0.0, fy, cy],
[0.0, 0.0, 1.0]
];
}
Future<CameraDescription?> getFrontCameraDescription() async {
List<CameraDescription> cameras = await availableCameras();
for (CameraDescription cd in cameras) {
if (cd.lensDirection == CameraLensDirection.front) {
return cd;
}
}
return null;
}
}

View File

@ -0,0 +1,5 @@
import 'package:archimedes_test/src/splat/camera.dart';
class InputData {
List<Camera>? cameras;
}

View File

@ -0,0 +1 @@

48
lib/src/splat/model.dart Normal file
View File

@ -0,0 +1,48 @@
import 'dart:math';
import 'package:archimedes_test/src/splat/tensor.dart';
double minus(double i) {
return 1 - i;
}
Tensor randomQuantTensor(int n) {
Tensor u = Tensor.random([n]);
Tensor v = Tensor.random([n]);
Tensor w = Tensor.random([n]);
Tensor a1 = u.each((f, _, __) {
return sin(2 * pi * f);
});
Tensor a2 = u.each((f, _, __) {
return sqrt(1 - f);
});
Tensor a = a1 * a2;
Tensor b1 = v.each((f, _, __) {
return cos(2 * pi * f);
});
Tensor b2 = v.each((f, _, __) {
return sqrt(1 - f);
});
Tensor b = b1 * b2;
Tensor c1 = u.each((f, _, __) {
return sqrt(f);
});
Tensor c2 = w.each((f, _, __) {
return sin(2 * pi * f);
});
Tensor c = c1 * c2;
Tensor d1 = u.each((f, _, __) {
return sqrt(f);
});
Tensor d2 = w.each((f, _, __) {
return cos(2 * pi * f);
});
Tensor d = d1 * d2;
return Tensor.stack(Tensor([a[0], b[0], c[0], d[0]]));
}

View File

@ -0,0 +1,8 @@
import 'package:archimedes_test/src/splat/tensor.dart';
class Points {
Tensor xyz;
Tensor rgb;
Points(this.xyz, this.rgb);
}

85
lib/src/splat/tensor.dart Normal file
View File

@ -0,0 +1,85 @@
import 'dart:math';
import 'package:collection/collection.dart';
class Tensor extends DelegatingList<DelegatingList<double>> {
Tensor(super.base);
Tensor each(Function(double, int, int) f) {
Tensor other = Tensor([]);
for (int j = 0; j < length; ++j) {
other[j] = const DelegatingList([]);
for (int k = 0; k < this[j].length; ++k) {
other[j][k] = f(this[j][k], j, k);
}
}
return other;
}
/// Generate a random tensor of shape `shape`
static Tensor random(List<int> shape) {
Random r = Random();
int d1 = 0, d2 = 0;
if (shape.length == 1) {
d1 = shape[0];
} else if (shape.length == 2) {
d1 = shape[0];
d2 = shape[1];
} else if (shape.length == 3) {
// d3 = shapes[2];
}
Tensor ret = Tensor(List.filled(d1, DelegatingList(List.filled(d2, 0.0))));
for (int i = 0; i < d1; ++i) {
for (int j = 0; j < d2; ++j) {
ret[i][j] = r.nextDouble();
}
}
return ret;
}
static Tensor stack(Tensor tensors, {int axis = 0}) {
if (axis < -1 || axis > tensors.length - 1) {
throw ArgumentError('Invalid axis value');
}
int newAxisSize = tensors.length;
for (var tensor in tensors) {
newAxisSize *= tensor.length;
}
Tensor result = Tensor([]);
for (int i = 0; i < newAxisSize; i++) {
int index = i;
int currentAxisIndex = axis;
List<int> currentAxisIndexes = List.filled(tensors.length, -1);
int currentTensorIndex = 0;
while (currentAxisIndexes[currentTensorIndex] < tensors.length) {
if (currentAxisIndexes[currentTensorIndex] == currentAxisIndex) {
index = currentAxisIndexes[currentTensorIndex] +
(index ~/ tensors.length);
currentAxisIndexes[currentTensorIndex]++;
}
currentAxisIndex += (axis > 0 ? 1 : -1);
currentTensorIndex =
(currentAxisIndex < 0 || currentTensorIndex >= tensors.length)
? 0
: currentTensorIndex + 1;
}
result.add(tensors[
currentTensorIndex]); // Access tensors[currentTensorIndex] as a List<double> rather than using the index operator [] with it
}
return Tensor(result);
}
operator *(Tensor other) {
return each((d, i, j) {
return [i][j] * other[i][j];
});
}
}