Fixed packages
This commit is contained in:
0
d3/__init__.py
Normal file
0
d3/__init__.py
Normal file
0
d3/controls/__init__.py
Normal file
0
d3/controls/__init__.py
Normal file
60
d3/controls/controls.py
Normal file
60
d3/controls/controls.py
Normal file
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from ..conv.model import Vertex
|
||||
|
||||
import pygame
|
||||
|
||||
from OpenGL.GL import *
|
||||
from OpenGL.GLU import *
|
||||
from OpenGL.GLUT import *
|
||||
|
||||
import math
|
||||
|
||||
class Controls:
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def apply(self):
|
||||
pass
|
||||
|
||||
def update(self, time = 10):
|
||||
pass
|
||||
|
||||
class TrackBallControls(Controls):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.vertex = Vertex()
|
||||
self.theta = 0
|
||||
|
||||
def apply(self):
|
||||
glRotatef(self.theta * 180 / math.pi, self.vertex.x, self.vertex.y, self.vertex.z)
|
||||
|
||||
def update(self, time = 10):
|
||||
|
||||
if not pygame.mouse.get_pressed()[0]:
|
||||
return
|
||||
|
||||
coeff = 0.001
|
||||
move = pygame.mouse.get_rel()
|
||||
|
||||
dV = Vertex(move[1] * time * coeff, move[0] * time * coeff, 0)
|
||||
dTheta = dV.norm2()
|
||||
|
||||
if abs(dTheta) < 0.00001:
|
||||
return
|
||||
|
||||
dV.normalize()
|
||||
|
||||
cosT2 = math.cos(self.theta / 2)
|
||||
sinT2 = math.sin(self.theta / 2)
|
||||
cosDT2 = math.cos(dTheta / 2)
|
||||
sinDT2 = math.sin(dTheta / 2)
|
||||
|
||||
A = cosT2 * sinDT2 * dV + cosDT2 * sinT2 * self.vertex + sinDT2 * sinT2 * Vertex.cross_product(dV, self.vertex)
|
||||
|
||||
self.theta = 2 * math.acos(cosT2 * cosDT2 - sinT2 * sinDT2 * Vertex.dot(dV, self.vertex))
|
||||
|
||||
self.vertex = A
|
||||
self.vertex.normalize()
|
||||
|
||||
|
||||
0
d3/conv/__init__.py
Normal file
0
d3/conv/__init__.py
Normal file
36
d3/conv/loadmodel.py
Normal file
36
d3/conv/loadmodel.py
Normal file
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from .obj import is_obj, OBJParser, OBJExporter
|
||||
from .ply import is_ply, PLYParser, PLYExporter
|
||||
|
||||
def load_model(path):
|
||||
parser = None
|
||||
|
||||
if is_obj(path):
|
||||
parser = OBJParser()
|
||||
elif is_ply(path):
|
||||
parser = PLYParser()
|
||||
else:
|
||||
raise Exception("File format not supported")
|
||||
|
||||
parser.parse_file(path)
|
||||
|
||||
return parser
|
||||
|
||||
def export_model(model, path):
|
||||
exporter = None
|
||||
|
||||
if is_obj(path):
|
||||
exporter = OBJExporter(model)
|
||||
elif is_ply(path):
|
||||
exporter = PLYExporter(model)
|
||||
else:
|
||||
raise Exception("File format not supported")
|
||||
|
||||
return exporter
|
||||
|
||||
def convert(input, output):
|
||||
model = load_model(input)
|
||||
exporter = export_model(model, output)
|
||||
return str(exporter)
|
||||
|
||||
302
d3/conv/model.py
Normal file
302
d3/conv/model.py
Normal file
@@ -0,0 +1,302 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from math import sqrt
|
||||
|
||||
class Vertex:
|
||||
def __init__(self, x = 0.0, y = 0.0, z = 0.0):
|
||||
self.x = x
|
||||
self.y = y
|
||||
self.z = z
|
||||
|
||||
def from_array(self, arr):
|
||||
self.x = float(arr[0]) if len(arr) > 0 else None
|
||||
self.y = float(arr[1]) if len(arr) > 1 else None
|
||||
self.z = float(arr[2]) if len(arr) > 2 else None
|
||||
return self
|
||||
|
||||
def __add__(self, other):
|
||||
return Vertex(self.x + other.x, self.y + other.y, self.z + other.z)
|
||||
|
||||
def __mul__(self, other):
|
||||
return Vertex(self.x * other, self.y * other, self.z * other)
|
||||
|
||||
def __rmul__(self, other):
|
||||
return self.__mul__(other)
|
||||
|
||||
def norm2(self):
|
||||
return self.x * self.x + self.y * self.y + self.z * self.z
|
||||
|
||||
def norm(self):
|
||||
return sqrt(self.norm2())
|
||||
|
||||
def normalize(self):
|
||||
norm = self.norm()
|
||||
if abs(norm) > 0.0001:
|
||||
self.x /= norm
|
||||
self.y /= norm
|
||||
self.z /= norm
|
||||
|
||||
def cross_product(v1, v2):
|
||||
return Vertex(
|
||||
v1.y * v2.z - v1.z * v2.y,
|
||||
v1.z * v2.x - v1.x * v2.z,
|
||||
v1.x * v2.y - v1.y * v2.x)
|
||||
|
||||
def from_points(v1, v2):
|
||||
return Vertex(
|
||||
v2.x - v1.x,
|
||||
v2.y - v1.y,
|
||||
v2.z - v1.z)
|
||||
|
||||
def __str__(self):
|
||||
return '(' + ", ".join([str(self.x), str(self.y), str(self.z)]) + ")"
|
||||
|
||||
def dot(self, other):
|
||||
return self.x * other.x + self.y * other.y + self.z * other.z
|
||||
|
||||
Normal = Vertex
|
||||
TexCoord = Vertex
|
||||
|
||||
class FaceVertex:
|
||||
def __init__(self, vertex = None, texture = None, normal = None):
|
||||
self.vertex = vertex
|
||||
self.texture = texture
|
||||
self.normal = normal
|
||||
|
||||
def from_array(self, arr):
|
||||
self.vertex = int(arr[0]) if len(arr) > 0 else None
|
||||
|
||||
try:
|
||||
self.texture = int(arr[1]) if len(arr) > 1 else None
|
||||
except:
|
||||
self.texture = None
|
||||
|
||||
try:
|
||||
self.normal = int(arr[2]) if len(arr) > 2 else None
|
||||
except:
|
||||
self.normal = None
|
||||
|
||||
return self
|
||||
|
||||
class Face:
|
||||
def __init__(self, a = None, b = None, c = None, mtl = None):
|
||||
self.a = a
|
||||
self.b = b
|
||||
self.c = c
|
||||
self.mtl = mtl
|
||||
|
||||
# Expects array of array
|
||||
def from_array(self, arr):
|
||||
self.a = FaceVertex().from_array(arr[0])
|
||||
self.b = FaceVertex().from_array(arr[1])
|
||||
self.c = FaceVertex().from_array(arr[2])
|
||||
return self
|
||||
|
||||
class ModelParser:
|
||||
|
||||
def __init__(self):
|
||||
self.vertices = []
|
||||
self.normals = []
|
||||
self.tex_coords = []
|
||||
self.faces = []
|
||||
self.bounding_box = BoundingBox()
|
||||
self.center_and_scale = True
|
||||
self.vertex_vbo = None
|
||||
self.tex_coord_vbo = None
|
||||
self.normal_vbo = None
|
||||
|
||||
def add_vertex(self, vertex):
|
||||
self.vertices.append(vertex)
|
||||
self.bounding_box.add(vertex)
|
||||
|
||||
def add_tex_coord(self, tex_coord):
|
||||
self.tex_coords.append(tex_coord)
|
||||
|
||||
def add_normal(self, normal):
|
||||
self.normals.append(normal)
|
||||
|
||||
def add_face(self, face):
|
||||
self.faces.append(face)
|
||||
|
||||
def parse_line(self, string):
|
||||
pass
|
||||
|
||||
def parse_file(self, path):
|
||||
with open(path) as f:
|
||||
for line in f.readlines():
|
||||
line = line.rstrip()
|
||||
self.parse_line(line)
|
||||
|
||||
def gl_draw(self):
|
||||
|
||||
import OpenGL.GL as gl
|
||||
|
||||
gl.glColor3f(1.0,0.0,0.0)
|
||||
|
||||
if self.center_and_scale:
|
||||
center = self.bounding_box.get_center()
|
||||
scale = self.bounding_box.get_scale() / 2
|
||||
gl.glPushMatrix()
|
||||
gl.glScalef(1/scale, 1/scale, 1/scale)
|
||||
gl.glTranslatef(-center.x, -center.y, -center.z)
|
||||
|
||||
if self.vertex_vbo is not None:
|
||||
|
||||
self.vertex_vbo.bind()
|
||||
gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
|
||||
gl.glVertexPointerf(self.vertex_vbo)
|
||||
self.vertex_vbo.unbind()
|
||||
|
||||
self.normal_vbo.bind()
|
||||
gl.glEnableClientState(gl.GL_NORMAL_ARRAY);
|
||||
gl.glNormalPointerf(self.normal_vbo)
|
||||
self.normal_vbo.unbind()
|
||||
|
||||
gl.glDrawArrays(gl.GL_TRIANGLES, 0, len(self.vertex_vbo.data) * 9)
|
||||
|
||||
else:
|
||||
|
||||
gl.glBegin(gl.GL_TRIANGLES)
|
||||
for face in self.faces:
|
||||
v1 = self.vertices[face.a.vertex]
|
||||
v2 = self.vertices[face.b.vertex]
|
||||
v3 = self.vertices[face.c.vertex]
|
||||
|
||||
if face.a.normal is not None:
|
||||
n1 = self.normals[face.a.normal]
|
||||
n2 = self.normals[face.b.normal]
|
||||
n3 = self.normals[face.c.normal]
|
||||
|
||||
if face.a.normal is not None:
|
||||
gl.glNormal3f(n1.x, n1.y, n1.z)
|
||||
gl.glVertex3f(v1.x, v1.y, v1.z)
|
||||
|
||||
if face.b.normal is not None:
|
||||
gl.glNormal3f(n2.x, n2.y, n2.z)
|
||||
gl.glVertex3f(v2.x, v2.y, v2.z)
|
||||
|
||||
if face.c.normal is not None:
|
||||
gl.glNormal3f(n3.x, n3.y, n3.z)
|
||||
gl.glVertex3f(v3.x, v3.y, v3.z)
|
||||
|
||||
gl.glEnd()
|
||||
|
||||
# Draw the normals
|
||||
# gl.glDisable(gl.GL_LIGHTING)
|
||||
# gl.glColor3f(1,0,0)
|
||||
# gl.glBegin(gl.GL_LINES)
|
||||
# for index in range(len(self.vertices)):
|
||||
# origin = self.vertices[index]
|
||||
# target = self.vertices[index] + self.normals[index]
|
||||
# gl.glVertex3f(origin.x, origin.y, origin.z)
|
||||
# gl.glVertex3f(target.x, target.y, target.z)
|
||||
# gl.glEnd()
|
||||
# gl.glEnable(gl.GL_LIGHTING)
|
||||
|
||||
if self.center_and_scale:
|
||||
gl.glPopMatrix()
|
||||
|
||||
def generate_vbos(self):
|
||||
from OpenGL.arrays import vbo
|
||||
from numpy import array
|
||||
# Build VBO
|
||||
v = []
|
||||
n = []
|
||||
|
||||
for face in self.faces:
|
||||
v1 = self.vertices[face.a.vertex]
|
||||
v2 = self.vertices[face.b.vertex]
|
||||
v3 = self.vertices[face.c.vertex]
|
||||
v += [[v1.x, v1.y, v1.z], [v2.x, v2.y, v2.z], [v3.x, v3.y, v3.z]]
|
||||
|
||||
n1 = self.normals[face.a.normal]
|
||||
n2 = self.normals[face.b.normal]
|
||||
n3 = self.normals[face.c.normal]
|
||||
n += [[n1.x, n1.y, n1.z], [n2.x, n2.y, n2.z], [n3.x, n3.y, n3.z]]
|
||||
|
||||
|
||||
self.vertex_vbo = vbo.VBO(array(v, 'f'))
|
||||
self.normal_vbo = vbo.VBO(array(n, 'f'))
|
||||
|
||||
def generate_vertex_normals(self):
|
||||
self.normals = [Normal() for i in self.vertices]
|
||||
|
||||
for face in self.faces:
|
||||
v1 = Vertex.from_points(self.vertices[face.a.vertex], self.vertices[face.b.vertex])
|
||||
v2 = Vertex.from_points(self.vertices[face.a.vertex], self.vertices[face.c.vertex])
|
||||
cross = Vertex.cross_product(v1, v2)
|
||||
self.normals[face.a.vertex] += cross
|
||||
self.normals[face.b.vertex] += cross
|
||||
self.normals[face.c.vertex] += cross
|
||||
|
||||
for normal in self.normals:
|
||||
normal.normalize()
|
||||
|
||||
for face in self.faces:
|
||||
face.a.normal = face.a.vertex
|
||||
face.b.normal = face.b.vertex
|
||||
face.c.normal = face.c.vertex
|
||||
|
||||
def generate_face_normals(self):
|
||||
|
||||
self.normals = [Normal() for i in self.faces]
|
||||
|
||||
for (index, face) in enumerate(self.faces):
|
||||
|
||||
v1 = Vertex.from_points(self.vertices[face.a.vertex], self.vertices[face.b.vertex])
|
||||
v2 = Vertex.from_points(self.vertices[face.a.vertex], self.vertices[face.c.vertex])
|
||||
cross = Vertex.cross_product(v1, v2)
|
||||
cross.normalize()
|
||||
self.normals[index] = cross
|
||||
|
||||
face.a.normal = index
|
||||
face.b.normal = index
|
||||
face.c.normal = index
|
||||
|
||||
|
||||
class BoundingBox:
|
||||
def __init__(self):
|
||||
self.min_x = +float('inf')
|
||||
self.min_y = +float('inf')
|
||||
self.min_z = +float('inf')
|
||||
|
||||
self.max_x = -float('inf')
|
||||
self.max_y = -float('inf')
|
||||
self.max_z = -float('inf')
|
||||
|
||||
def add(self, vertex):
|
||||
self.min_x = min(self.min_x, vertex.x)
|
||||
self.min_y = min(self.min_y, vertex.y)
|
||||
self.min_z = min(self.min_z, vertex.z)
|
||||
|
||||
self.max_x = max(self.max_x, vertex.x)
|
||||
self.max_y = max(self.max_y, vertex.y)
|
||||
self.max_z = max(self.max_z, vertex.z)
|
||||
|
||||
def __str__(self):
|
||||
return "[{},{}],[{},{}],[{},{}]".format(
|
||||
self.min_x,
|
||||
self.min_y,
|
||||
self.min_z,
|
||||
self.max_x,
|
||||
self.max_y,
|
||||
self.max_z)
|
||||
|
||||
def get_center(self):
|
||||
return Vertex(
|
||||
(self.min_x + self.max_x) / 2,
|
||||
(self.min_y + self.max_y) / 2,
|
||||
(self.min_z + self.max_z) / 2)
|
||||
|
||||
def get_scale(self):
|
||||
return max(
|
||||
abs(self.max_x - self.min_x),
|
||||
abs(self.max_y - self.min_y),
|
||||
abs(self.max_z - self.min_z))
|
||||
|
||||
|
||||
class Exporter:
|
||||
def __init__(self, model):
|
||||
self.model = model
|
||||
|
||||
|
||||
83
d3/conv/obj.py
Normal file
83
d3/conv/obj.py
Normal file
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from .model import ModelParser, Exporter, Vertex, TexCoord, Normal, FaceVertex, Face
|
||||
from functools import reduce
|
||||
|
||||
def is_obj(filename):
|
||||
return filename[-4:] == '.obj'
|
||||
|
||||
class OBJParser(ModelParser):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.materials = []
|
||||
|
||||
def parse_line(self, string):
|
||||
if string == '':
|
||||
return
|
||||
|
||||
split = string.split()
|
||||
first = split[0]
|
||||
split = split[1:]
|
||||
|
||||
if first == 'usemtl':
|
||||
self.currentMaterial = split[0]
|
||||
elif first == 'v':
|
||||
self.add_vertex(Vertex().from_array(split))
|
||||
elif first == 'vn':
|
||||
self.add_normal(Normal().from_array(split))
|
||||
elif first == 'vt':
|
||||
self.add_tex_coord(TexCoord().from_array(split))
|
||||
elif first == 'f':
|
||||
splits = list(map(lambda x: x.split('/'), split))
|
||||
|
||||
for i in range(len(splits)):
|
||||
for j in range(len(splits[i])):
|
||||
if splits[i][j] is not '':
|
||||
splits[i][j] = int(splits[i][j]) - 1
|
||||
|
||||
self.add_face(Face().from_array(splits))
|
||||
|
||||
class OBJExporter(Exporter):
|
||||
def __init__(self, model):
|
||||
super().__init__(model)
|
||||
|
||||
def __str__(self):
|
||||
string = ""
|
||||
|
||||
for vertex in self.model.vertices:
|
||||
string += "v " + ' '.join([str(vertex.x), str(vertex.y), str(vertex.z)]) + "\n"
|
||||
|
||||
string += "\n"
|
||||
|
||||
if len(self.model.tex_coords) > 0:
|
||||
for tex_coord in self.model.tex_coords:
|
||||
string += "vt " + ' '.join([str(tex_coord.x), str(tex_coord.y)]) + "\n"
|
||||
|
||||
string += "\n"
|
||||
|
||||
if len(self.model.normals) > 0:
|
||||
for normal in self.model.normals:
|
||||
string += "vn " + ' '.join([str(normal.x), str(normal.y), str(normal.z)]) + "\n"
|
||||
|
||||
string += "\n"
|
||||
|
||||
for face in self.model.faces:
|
||||
string += "f "
|
||||
arr = []
|
||||
for v in [face.a, face.b, face.c]:
|
||||
sub_arr = []
|
||||
sub_arr.append(str(v.vertex + 1))
|
||||
if v.normal is None:
|
||||
if v.texture is not None:
|
||||
sub_arr.append('')
|
||||
sub_arr.append(str(v.texture + 1))
|
||||
elif v.texture is not None:
|
||||
sub_arr.append(str(v.texture + 1))
|
||||
if v.normal is not None:
|
||||
sub_arr.append(str(v.normal + 1))
|
||||
arr.append('/'.join(sub_arr))
|
||||
|
||||
string += ' '.join(arr) + '\n'
|
||||
return string
|
||||
|
||||
107
d3/conv/ply.py
Normal file
107
d3/conv/ply.py
Normal file
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
from .model import ModelParser, Exporter, Vertex, Face, FaceVertex
|
||||
|
||||
def is_ply(filename):
|
||||
return filename[-4:] == '.ply'
|
||||
|
||||
class PLYParser(ModelParser):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.counter = 0
|
||||
self.elements = []
|
||||
self.inner_parser = PLYHeaderParser(self)
|
||||
|
||||
def parse_line(self, string):
|
||||
self.inner_parser.parse_line(string)
|
||||
|
||||
class PLYHeaderParser:
|
||||
def __init__(self, parent):
|
||||
self.current_element = None
|
||||
self.parent = parent
|
||||
|
||||
def parse_line(self, string):
|
||||
split = string.split()
|
||||
if string == 'ply':
|
||||
return
|
||||
|
||||
elif split[0] == 'format':
|
||||
if split[1] != 'ascii' or split[2] != '1.0':
|
||||
print('Only ascii 1.0 format is supported', file=sys.stderr)
|
||||
sys.exit(-1)
|
||||
|
||||
elif split[0] == 'element':
|
||||
self.current_element = PLYElement(split[1], int(split[2]))
|
||||
self.parent.elements.append(self.current_element)
|
||||
|
||||
elif split[0] == 'property':
|
||||
self.current_element.add_property(split[2], split[1])
|
||||
|
||||
elif split[0] == 'end_header':
|
||||
self.parent.inner_parser = PLYContentParser(self.parent)
|
||||
|
||||
|
||||
class PLYElement:
|
||||
def __init__(self, name, number):
|
||||
self.name = name
|
||||
self.number = number
|
||||
self.properties = []
|
||||
|
||||
def add_property(self, name, type):
|
||||
self.properties.append((name, type))
|
||||
|
||||
class PLYContentParser:
|
||||
def __init__(self, parent):
|
||||
self.parent = parent
|
||||
self.element_index = 0
|
||||
self.counter = 0
|
||||
self.current_element = self.parent.elements[0]
|
||||
|
||||
def parse_line(self, string):
|
||||
|
||||
split = string.split()
|
||||
|
||||
if self.current_element.name == 'vertex':
|
||||
self.parent.add_vertex(Vertex().from_array(split))
|
||||
elif self.current_element.name == 'face':
|
||||
self.parent.add_face(Face(FaceVertex(int(split[1])), FaceVertex(int(split[2])), FaceVertex(int(split[3]))))
|
||||
|
||||
self.counter += 1
|
||||
|
||||
if self.counter == self.current_element.number:
|
||||
self.next_element()
|
||||
|
||||
def next_element(self):
|
||||
self.element_index += 1
|
||||
if self.element_index < len(self.parent.elements):
|
||||
self.current_element = self.parent.elements[self.element_index]
|
||||
|
||||
class PLYExporter(Exporter):
|
||||
def __init__(self, model):
|
||||
super().__init__(model)
|
||||
|
||||
def __str__(self):
|
||||
# Header
|
||||
string = "ply\nformat ascii 1.0\ncomment Automatically gnerated by model-converter\n"
|
||||
|
||||
# Types : vertices
|
||||
string += "element vertex " + str(len(self.model.vertices)) +"\n"
|
||||
string += "property float32 x\nproperty float32 y\nproperty float32 z\n"
|
||||
|
||||
# Types : faces
|
||||
string += "element face " + str(len(self.model.faces)) + "\n"
|
||||
string += "property list uint8 int32 vertex_indices\n"
|
||||
|
||||
# End header
|
||||
string += "end_header\n"
|
||||
|
||||
# Content of the model
|
||||
for vertex in self.model.vertices:
|
||||
string += str(vertex.x) + " " + str(vertex.y) + " " + str(vertex.z) + "\n"
|
||||
|
||||
for face in self.model.faces:
|
||||
string += "3 " + str(face.a.vertex) + " " + str(face.b.vertex) + " " + str(face.c.vertex) + "\n"
|
||||
|
||||
return string
|
||||
|
||||
Reference in New Issue
Block a user