Skip to content

Instantly share code, notes, and snippets.

@takeru
Last active April 23, 2025 05:52
Show Gist options
  • Save takeru/fbf167324f3b6327bf87a6de70f625ca to your computer and use it in GitHub Desktop.
Save takeru/fbf167324f3b6327bf87a6de70f625ca to your computer and use it in GitHub Desktop.
debug 3dgs babylon.js ply spz...
import numpy as np
class Splat:
def __init__(self, position, rotation, scale, opacity, color):
self.position = position
self.rotation = rotation
self.scale = scale
self.opacity = opacity
self.color = color
@staticmethod
def create(position, rotation=[0, 0, 0, 1], scale=[1.0, 1.0, 1.0], opacity=1.0, color="black"):
return Splat(position, rotation, scale, opacity, color)
def color_data(self):
if isinstance(self.color, str):
if self.color == "black":
return [0.0, 0.0, 0.0]
elif self.color == "white":
return [1.0, 1.0, 1.0]
elif self.color == "red":
return [1.0, 0.0, 0.0]
elif self.color == "green":
return [0.0, 1.0, 0.0]
elif self.color == "blue":
return [0.0, 0.0, 1.0]
else:
raise ValueError(f"Invalid color: {self.color}")
return self.color
def to_data(self):
color = self.color_data()
return [self.position[0], self.position[1], self.position[2], # position
self.rotation[0], self.rotation[1], self.rotation[2], self.rotation[3], # rotation
self.scale[0], self.scale[1], self.scale[2], # scale
self.opacity, # opacity
color[0], color[1], color[2]] # color
splats = []
max = 100
step = 10
for i in range(max):
splats.append(
Splat.create(position=[i*step, 0, 0], scale=[0.1, 0.1, 0.1], color="red")
)
splats.append(
Splat.create(position=[0, i*step, 0], scale=[0.1, 0.1, 0.1], color="green")
)
splats.append(
Splat.create(position=[0, 0, i*step], scale=[0.1, 0.1, 0.1], color="blue")
)
splats.append(
Splat.create(position=[i*step, max*step, max*step], scale=[0.1, 0.1, 0.1], color=[0, 1, 1])
)
splats.append(
Splat.create(position=[max*step, i*step, max*step], scale=[0.1, 0.1, 0.1], color=[1, 0, 1])
)
splats.append(
Splat.create(position=[max*step, max*step, i*step], scale=[0.1, 0.1, 0.1], color=[1, 1, 0])
)
splats.append(
Splat.create(position=[0.0, 0.0, 0.0], scale=[3, 3, 0.0001], color="white")
)
def quat_rot_y_angle(angle):
theta = np.radians(angle)
w = np.cos(theta/2)
y = np.sin(theta/2)
return [0, y, 0, w]
rot_y = quat_rot_y_angle(45)
for i in [1,2,3]:
splats.append(
Splat.create(position=[100.0*i, 0.0, 0.0], rotation=rot_y, scale=[3, 4, 0.0001], color="red")
)
for i in [1,2]:
splats.append(
Splat.create(position=[0.0, 100.0*i, 0.0], rotation=rot_y, scale=[3, 3, 0.0001], color="green")
)
for i in [1]:
splats.append(
Splat.create(position=[0.0, 0.0, 100.0*i], rotation=rot_y, scale=[3, 4, 0.0001], color="blue")
)
data = [splat.to_data() for splat in splats]
header = f"""ply
format binary_little_endian 1.0
element vertex {len(data)}
property float x
property float y
property float z
property float rot_1
property float rot_2
property float rot_3
property float rot_0
property float scale_0
property float scale_1
property float scale_2
property float opacity
property float f_dc_0
property float f_dc_1
property float f_dc_2
end_header
"""
data = np.array(data, dtype=np.float32)
with open('gaussian_test_xxx.ply', 'wb') as f:
f.write(header.encode('ascii'))
data.tofile(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment