mirror of
https://github.com/PrimitiveAnything/PrimitiveAnything.git
synced 2025-12-27 18:40:34 +08:00
init
This commit is contained in:
1
primitive_anything/michelangelo/graphics/__init__.py
Executable file
1
primitive_anything/michelangelo/graphics/__init__.py
Executable file
@@ -0,0 +1 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
9
primitive_anything/michelangelo/graphics/primitives/__init__.py
Executable file
9
primitive_anything/michelangelo/graphics/primitives/__init__.py
Executable file
@@ -0,0 +1,9 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from .volume import generate_dense_grid_points
|
||||
|
||||
from .mesh import (
|
||||
MeshOutput,
|
||||
save_obj,
|
||||
savemeshtes2
|
||||
)
|
||||
114
primitive_anything/michelangelo/graphics/primitives/mesh.py
Executable file
114
primitive_anything/michelangelo/graphics/primitives/mesh.py
Executable file
@@ -0,0 +1,114 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import os
|
||||
import cv2
|
||||
import numpy as np
|
||||
import PIL.Image
|
||||
from typing import Optional
|
||||
|
||||
import trimesh
|
||||
|
||||
|
||||
def save_obj(pointnp_px3, facenp_fx3, fname):
|
||||
fid = open(fname, "w")
|
||||
write_str = ""
|
||||
for pidx, p in enumerate(pointnp_px3):
|
||||
pp = p
|
||||
write_str += "v %f %f %f\n" % (pp[0], pp[1], pp[2])
|
||||
|
||||
for i, f in enumerate(facenp_fx3):
|
||||
f1 = f + 1
|
||||
write_str += "f %d %d %d\n" % (f1[0], f1[1], f1[2])
|
||||
fid.write(write_str)
|
||||
fid.close()
|
||||
return
|
||||
|
||||
|
||||
def savemeshtes2(pointnp_px3, tcoords_px2, facenp_fx3, facetex_fx3, tex_map, fname):
|
||||
fol, na = os.path.split(fname)
|
||||
na, _ = os.path.splitext(na)
|
||||
|
||||
matname = "%s/%s.mtl" % (fol, na)
|
||||
fid = open(matname, "w")
|
||||
fid.write("newmtl material_0\n")
|
||||
fid.write("Kd 1 1 1\n")
|
||||
fid.write("Ka 0 0 0\n")
|
||||
fid.write("Ks 0.4 0.4 0.4\n")
|
||||
fid.write("Ns 10\n")
|
||||
fid.write("illum 2\n")
|
||||
fid.write("map_Kd %s.png\n" % na)
|
||||
fid.close()
|
||||
####
|
||||
|
||||
fid = open(fname, "w")
|
||||
fid.write("mtllib %s.mtl\n" % na)
|
||||
|
||||
for pidx, p in enumerate(pointnp_px3):
|
||||
pp = p
|
||||
fid.write("v %f %f %f\n" % (pp[0], pp[1], pp[2]))
|
||||
|
||||
for pidx, p in enumerate(tcoords_px2):
|
||||
pp = p
|
||||
fid.write("vt %f %f\n" % (pp[0], pp[1]))
|
||||
|
||||
fid.write("usemtl material_0\n")
|
||||
for i, f in enumerate(facenp_fx3):
|
||||
f1 = f + 1
|
||||
f2 = facetex_fx3[i] + 1
|
||||
fid.write("f %d/%d %d/%d %d/%d\n" % (f1[0], f2[0], f1[1], f2[1], f1[2], f2[2]))
|
||||
fid.close()
|
||||
|
||||
PIL.Image.fromarray(np.ascontiguousarray(tex_map), "RGB").save(
|
||||
os.path.join(fol, "%s.png" % na))
|
||||
|
||||
return
|
||||
|
||||
|
||||
class MeshOutput(object):
|
||||
|
||||
def __init__(self,
|
||||
mesh_v: np.ndarray,
|
||||
mesh_f: np.ndarray,
|
||||
vertex_colors: Optional[np.ndarray] = None,
|
||||
uvs: Optional[np.ndarray] = None,
|
||||
mesh_tex_idx: Optional[np.ndarray] = None,
|
||||
tex_map: Optional[np.ndarray] = None):
|
||||
|
||||
self.mesh_v = mesh_v
|
||||
self.mesh_f = mesh_f
|
||||
self.vertex_colors = vertex_colors
|
||||
self.uvs = uvs
|
||||
self.mesh_tex_idx = mesh_tex_idx
|
||||
self.tex_map = tex_map
|
||||
|
||||
def contain_uv_texture(self):
|
||||
return (self.uvs is not None) and (self.mesh_tex_idx is not None) and (self.tex_map is not None)
|
||||
|
||||
def contain_vertex_colors(self):
|
||||
return self.vertex_colors is not None
|
||||
|
||||
def export(self, fname):
|
||||
|
||||
if self.contain_uv_texture():
|
||||
savemeshtes2(
|
||||
self.mesh_v,
|
||||
self.uvs,
|
||||
self.mesh_f,
|
||||
self.mesh_tex_idx,
|
||||
self.tex_map,
|
||||
fname
|
||||
)
|
||||
|
||||
elif self.contain_vertex_colors():
|
||||
mesh_obj = trimesh.Trimesh(vertices=self.mesh_v, faces=self.mesh_f, vertex_colors=self.vertex_colors)
|
||||
mesh_obj.export(fname)
|
||||
|
||||
else:
|
||||
save_obj(
|
||||
self.mesh_v,
|
||||
self.mesh_f,
|
||||
fname
|
||||
)
|
||||
|
||||
|
||||
|
||||
21
primitive_anything/michelangelo/graphics/primitives/volume.py
Executable file
21
primitive_anything/michelangelo/graphics/primitives/volume.py
Executable file
@@ -0,0 +1,21 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import numpy as np
|
||||
|
||||
|
||||
def generate_dense_grid_points(bbox_min: np.ndarray,
|
||||
bbox_max: np.ndarray,
|
||||
octree_depth: int,
|
||||
indexing: str = "ij"):
|
||||
length = bbox_max - bbox_min
|
||||
num_cells = np.exp2(octree_depth)
|
||||
x = np.linspace(bbox_min[0], bbox_max[0], int(num_cells) + 1, dtype=np.float32)
|
||||
y = np.linspace(bbox_min[1], bbox_max[1], int(num_cells) + 1, dtype=np.float32)
|
||||
z = np.linspace(bbox_min[2], bbox_max[2], int(num_cells) + 1, dtype=np.float32)
|
||||
[xs, ys, zs] = np.meshgrid(x, y, z, indexing=indexing)
|
||||
xyz = np.stack((xs, ys, zs), axis=-1)
|
||||
xyz = xyz.reshape(-1, 3)
|
||||
grid_size = [int(num_cells) + 1, int(num_cells) + 1, int(num_cells) + 1]
|
||||
|
||||
return xyz, grid_size, length
|
||||
|
||||
Reference in New Issue
Block a user