Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boilerplate from ts version #1

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift click to select a range
65ff0db
get bolilerplate for loading caterfoil app
tiye Dec 22, 2024
0964147
first working demo with axis
tiye Dec 22, 2024
5c069ba
fix usage of group
tiye Dec 22, 2024
92fe8e1
random fixes from coderabbit
tiye Dec 22, 2024
96df7c3
split out lib part
tiye Dec 22, 2024
f5b1f19
include quaternion code
tiye Dec 22, 2024
be97bcd
render quaternion fractals
tiye Dec 23, 2024
7623ddd
split quaternion to library
tiye Dec 24, 2024
16dfaf9
trying with fractal tree in quaternion
tiye Dec 25, 2024
430d927
an experiment on curve
tiye Dec 26, 2024
c88d425
move vertexes to caterfoil lib
tiye Dec 28, 2024
a40d749
use Quaternion instead of Point4D
tiye Dec 28, 2024
134638c
cleanup cakes
tiye Dec 28, 2024
9bb5b91
use shader from ts lib; restructure several ffi
tiye Dec 28, 2024
f83032a
use alias for some types
tiye Dec 28, 2024
124ca43
encode some default colors
tiye Dec 28, 2024
bb0e9f5
expose type on vertex data; export alpha on color
tiye Dec 28, 2024
2d434a8
reduce noise with utilities
tiye Dec 28, 2024
2727799
add quaternion multiply example
tiye Dec 28, 2024
cf017dc
attempt work on Retained Atom
tiye Dec 29, 2024
b2a9053
enable states retaining logic
tiye Dec 29, 2024
84fbf6c
get hyper cube demo
tiye Dec 29, 2024
192dfc9
simplify definition of object
tiye Dec 29, 2024
b8d9d97
options to connect storage
tiye Dec 29, 2024
4c5ad68
refine demos in quat product
tiye Dec 29, 2024
fb26028
refine params of demos
tiye Dec 29, 2024
c71123a
refine demos; fix js in vite build
tiye Dec 29, 2024
75c823b
render some cubes
tiye Jan 1, 2025
d380c78
get custom shader for cubes
tiye Jan 1, 2025
2633fa4
control rendering with tab param
tiye Jan 2, 2025
8649007
a demo of prime walk
tiye Jan 2, 2025
76ca870
upgrade deps to fix line width issue
tiye Jan 3, 2025
10faf51
list options on README
tiye Jan 3, 2025
f052aec
a demo of tesselation on sphere
tiye Jan 4, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use Quaternion instead of Point4D
  • Loading branch information
tiye committed Dec 28, 2024
commit a40d74971f0283c1ce7c324ed4d8b82571ab15a0
39 changes: 38 additions & 1 deletion .mooncakes/tiye/quaternion/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 6,41 @@ MoonBit does not have `num_traits::Float` so this library is only using `Float`(
moon add tiye/quaternion
```

Find docs on Mooncaches.
Find docs on [Mooncakes](https://mooncakes.io/docs/#/tiye/quaternion/lib/members).

```moonbit
let a = Quaternion::{ w: 1.0, x: 2.0, y: 3.0, z: 4.0 };
a.w;

// quick creates
let b1 = q(1.0, 2.0, 3.0, 4.0);
//
// quick creates with integers
let b2 = qi(1, 2, 3, 4);

b1 b2;
b1 - b2;
b1 * b2;
b1 / b2;
b1.conjugate();
b1.scale(1.5);
b1.square_length();
b1.length();
b1.inverse();
```

There are also mutable APIs:

```moonbit
let mut c = Quaternion::id();
let b = qi(1, 2, 3, 4);

c = b;
c -= b;
c *= b;
// no division

c.inverse_mut();
c.conjugate_mut();
c.scale_mut(1.5);
```
2 changes: 1 addition & 1 deletion .mooncakes/tiye/quaternion/moon.mod.json
Original file line number Diff line number Diff line change
@@ -1,6 1,6 @@
{
"name": "tiye/quaternion",
"version": "0.0.1",
"version": "0.0.3",
"readme": "README.md",
"repository": "https://github.com/WebGPU-Art/quaternion",
"license": "Apache-2.0",
Expand Down
70 changes: 29 additions & 41 deletions .mooncakes/tiye/quaternion/src/lib/quaternion.mbt
Original file line number Diff line number Diff line change
@@ -1,44 1,3 @@
/// Using Float in this module, MoonBit does not support generics in Trait yet.
///
//! a simpler quaternion math library with traits.
//!
//! ```moonbit
//! let a = Quaternion::{ w: 1.0, x: 2.0, y: 3.0, z: 4.0 };
//! a.w;
//!
//! // quick creates
//! let b1 = q(1.0, 2.0, 3.0, 4.0);
//!
//! // quick creates with integers
//! let b2 = qi(1, 2, 3, 4);
//!
//! b1 b2;
//! b1 - b2;
//! b1 * b2;
//! b1 / b2;
//! b1.conjugate();
//! b1.scale(1.5);
//! b1.square_length();
//! b1.length();
//! b1.inverse();
//!```
//!
//! There are also mutable APIs:
//!
//! ```moonbit
//! let mut c = Quaternion::id();
//! let b = qi(1, 2, 3, 4);
//!
//! c = b;
//! c -= b;
//! c *= b;
//! // no division
//! c.inverse_mut();
//! c.conjugate_mut();
//! c.scale_mut(1.5);
//! ```
//!

///| Quaternion {w, x, y, z}
pub(all) struct Quaternion {
mut w : Float
Expand Down Expand Up @@ -235,6 194,35 @@ pub fn Quaternion::op_neg(self : Quaternion) -> Quaternion {
Quaternion::{ w: -self.w, x: -self.x, y: -self.y, z: -self.z }
}

///|
pub fn Quaternion::to_array(self : Quaternion) -> Array[Float] {
[self.w, self.x, self.y, self.z]
}

///| convert to xyzw, useful for Graphics
pub fn Quaternion::to_xyzw(self : Quaternion) -> Array[Float] {
[self.x, self.y, self.z, self.w]
}

///|
pub(all) type! QuatError String

///|
pub fn Quaternion::from_array(arr : Array[Float]) -> Quaternion!QuatError {
if arr.length() != 4 {
raise QuatError("Array must have 4 elements")
}
Quaternion::{ w: arr[0], x: arr[1], y: arr[2], z: arr[3] }
}

///| from_xyzw_array
pub fn Quaternion::from_xyzw_array(arr : Array[Float]) -> Quaternion!QuatError {
if arr.length() != 4 {
raise QuatError("Array must have 4 elements")
}
Quaternion::{ w: arr[3], x: arr[0], y: arr[1], z: arr[2] }
}

///| Construct a quaternion representing the given euler angle rotations (in radians)
pub fn Quaternion::from_euler_angles(
x : Float,
Expand Down
12 changes: 12 additions & 0 deletions .mooncakes/tiye/quaternion/src/lib/quaternion_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 234,15 @@ test "Quaternion::op_div/basic" {
)
inspect!(result * b, content="Quaternion { w: 1, x: 2, y: 3, z: 4 }")
}

test "Quaternion::to_array/basic" {
let q = @lib.q(1.0, 2.0, 3.0, 4.0)
let result = q.to_array()
inspect!(result, content="[1, 2, 3, 4]")
let result2 = q.to_xyzw()
inspect!(result2, content="[2, 3, 4, 1]")
let q2 = @lib.from_xyzw_array?([2.0, 3.0, 4.0, 1.0]).unwrap()
inspect!(q2, content="Quaternion { w: 1, x: 2, y: 3, z: 4 }")
let q3 = @lib.from_array?([1.0, 2.0, 3.0, 4.0]).unwrap()
inspect!(q3, content="Quaternion { w: 1, x: 2, y: 3, z: 4 }")
}
4 changes: 2 additions & 2 deletions moon.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 2,12 @@
"name": "tiye/caterfoil",
"version": "0.1.0",
"deps": {
"tiye/quaternion": "0.0.1"
"tiye/quaternion": "0.0.3"
},
"readme": "README.md",
"repository": "",
"license": "Apache-2.0",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick (assertive)

Add repository URL

The repository field is empty. Adding the repository URL would help users find the source code and contribute.

"keywords": [],
"description": "",
"source": "src"
}
}
43 changes: 5 additions & 38 deletions src/lib/caterfoil.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -116,39 116,6 @@ pub fn group(children : Array[CaterfoilRenderObject]) -> CaterfoilRenderObject {
caterfoil_group(arr)
}

///|
pub(all) struct Point4D {
x : Float
y : Float
z : Float
w : Float
}

///|
pub fn Point4D::new(x? : Float, y? : Float, z? : Float, w? : Float) -> Point4D {
Point4D::{
x: x.or_default(),
y: y.or_default(),
z: z.or_default(),
w: w.or_default(),
}
}

///|
pub fn Point4D::scale(self : Point4D, t : Float) -> Point4D {
Point4D::{ x: self.x * t, y: self.y * t, z: self.z * t, w: self.w * t }
}

///|
pub fn Point4D::to_array(self : Point4D) -> Array[Float] {
[self.x, self.y, self.z, self.w]
}

///|
pub fn Point4D::from_quaternion(q : @quaternion.Quaternion) -> Point4D {
Point4D::new(x=q.x, y=q.y, z=q.z, w=q.w)
}

///|
pub(all) struct Color {
r : Float
Expand All @@ -169,7 136,7 @@ pub fn Color::new(r? : Float, g? : Float, b? : Float, a? : Float) -> Color {

///|
pub(all) struct Vertex {
position : Point4D
position : @quaternion.Quaternion
color : Color
}

Expand Down Expand Up @@ -198,9 165,9 @@ pub fn Vertex::to_value(

///|
pub(all) struct PolylineVertex {
position : Point4D
position : @quaternion.Quaternion
color : Color
direction : Point4D
direction : @quaternion.Quaternion
side : Int
}

Expand All @@ -209,9 176,9 @@ pub fn PolylineVertex::to_value(
self : PolylineVertex
) -> @moonbitlang/core/hashmap.T[String, Array[Float]] {
let dict : @moonbitlang/core/hashmap.T[String, Array[Float]] = @moonbitlang/core/hashmap.new()
dict["position"] = self.position.to_array()
dict["position"] = self.position.to_xyzw()
dict["color"] = [self.color.r, self.color.g, self.color.b, self.color.a]
dict["direction"] = self.direction.to_array()
dict["direction"] = self.direction.to_xyzw()
dict["side"] = [self.side.to_float()] // need int but JavaScript auto converts
dict
}
16 changes: 8 additions & 8 deletions src/main/axis.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 3,35 @@ fn comp_axis() -> @caterfoil.CaterfoilRenderObject {
let data = []
let vertex_list : Array[@caterfoil.Vertex] = [
{
position: @caterfoil.Point4D::new(x=-100.0),
position: @quaternion.Quaternion::new(x=-100.0),
color: @caterfoil.Color::new(r=0.5, g=0.5, b=0.5),
},
{
position: @caterfoil.Point4D::new(x=100.0),
position: @quaternion.Quaternion::new(x=100.0),
color: @caterfoil.Color::new(r=0.5, g=0.5, b=0.5),
},
{
position: @caterfoil.Point4D::new(y=-100.0),
position: @quaternion.Quaternion::new(y=-100.0),
color: @caterfoil.Color::new(r=0.5, g=0.5, b=0.5),
},
{
position: @caterfoil.Point4D::new(y=100.0),
position: @quaternion.Quaternion::new(y=100.0),
color: @caterfoil.Color::new(g=1.0),
},
{
position: @caterfoil.Point4D::new(z=-100.0),
position: @quaternion.Quaternion::new(z=-100.0),
color: @caterfoil.Color::new(g=1.0),
},
{
position: @caterfoil.Point4D::new(z=100.0),
position: @quaternion.Quaternion::new(z=100.0),
color: @caterfoil.Color::new(r=1.0),
},
{
position: @caterfoil.Point4D::new(w=-100.0),
position: @quaternion.Quaternion::new(w=-100.0),
color: @caterfoil.Color::new(r=1.0),
},
{
position: @caterfoil.Point4D::new(w=100.0),
position: @quaternion.Quaternion::new(w=100.0),
color: @caterfoil.Color::new(r=1.0),
},
]
Expand Down
4 changes: 2 additions & 2 deletions src/main/lamp-tree.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 44,7 @@ fn comp_lamp_tree() -> @caterfoil.CaterfoilRenderObject {
let data = []
for point in lamp_tree {
let v = @caterfoil.Vertex::{
position: @caterfoil.Point4D::new(
position: @quaternion.Quaternion::new(
x=point.x,
y=point.y,
z=point.z,
Expand Down Expand Up @@ -89,7 89,7 @@ fn comp_fly_city() -> @caterfoil.CaterfoilRenderObject {
let data = []
for point in fly_city {
let v = @caterfoil.Vertex::{
position: @caterfoil.Point4D::new(
position: @quaternion.Quaternion::new(
x=point.x,
y=point.y,
z=point.z,
Expand Down
6 changes: 3 additions & 3 deletions src/main/quat-curve.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 18,9 @@ fn comp_quat_curve() -> @caterfoil.CaterfoilRenderObject {
let length = (p1 - p0).length()
let width = 2.0.to_float()
let direction = (p1 - p0).scale(1.0.to_float() / length)
let direction = @caterfoil.Point4D::from_quaternion(direction.scale(width))
let pos = @caterfoil.Point4D::from_quaternion(p0)
let pos_next = @caterfoil.Point4D::from_quaternion(p1)
let direction = direction.scale(width)
let pos = p0
let pos_next = p1
let color = @caterfoil.Color::new(r=1.0)
data.push(
@caterfoil.PolylineVertex::{ position: pos, color, direction, side: 0 }.to_value(),
Expand Down
4 changes: 2 additions & 2 deletions src/main/quat-tree.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 54,7 @@ fn comp_quat_tree() -> @caterfoil.CaterfoilRenderObject {
let data = []
for branch in lines {
let v = @caterfoil.Vertex::{
position: @caterfoil.Point4D::new(
position: @quaternion.Quaternion::new(
x=branch.from.x,
y=branch.from.y,
z=branch.from.z,
Expand All @@ -64,7 64,7 @@ fn comp_quat_tree() -> @caterfoil.CaterfoilRenderObject {
}
data.push(v.to_value())
let v = @caterfoil.Vertex::{
position: @caterfoil.Point4D::new(
position: @quaternion.Quaternion::new(
x=branch.to.x,
y=branch.to.y,
z=branch.to.z,
Expand Down