demos/plyreader.py
author Radek Brich <radek.brich@devl.cz>
Wed, 23 Apr 2008 10:38:33 +0200
branchpyrit
changeset 78 9569e9f35374
parent 75 20dee9819b17
child 90 f6a72eb99631
permissions -rw-r--r--
move shapes to extra source file add serialize header and source file with common serialization functions dump/load feature for shapes and kd-tree fix few minor bugs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41
c1080cb5bd6d fix possible division by zero in ccdemos/common_ply.h
Radek Brich <radek.brich@devl.cz>
parents: 29
diff changeset
     1
# Stanford .ply file loader
c1080cb5bd6d fix possible division by zero in ccdemos/common_ply.h
Radek Brich <radek.brich@devl.cz>
parents: 29
diff changeset
     2
29
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     3
from raytracer import Triangle, NormalVertex
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     4
69
303583d2fb97 move "smooth" attribute from Triangle to Material
Radek Brich <radek.brich@devl.cz>
parents: 41
diff changeset
     5
def LoadStanfordPlyFile(rt, filename, mat, scale=(1,1,1), trans=(0,0,0)):
29
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     6
	if (type(scale) == float or type(scale) == int):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     7
		scale = (scale,)*3
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     8
	vertices = []
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
     9
	normals = []
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    10
	vertex_face_num = []
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    11
	fp = file(filename)
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    12
	# read header
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    13
	tokens = (0,)
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    14
	while (tokens[0] != "end_header"):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    15
		tokens = fp.readline().split()
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    16
		if (tokens[0] == "element"):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    17
			if (tokens[1] == "vertex"):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    18
				vertex_num = int(tokens[2])
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    19
			if (tokens[1] == "face"):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    20
				face_num = int(tokens[2])
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    21
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    22
	# read vertices
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    23
	num = vertex_num
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    24
	while (num):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    25
		tokens = fp.readline().split()
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    26
		v = [float(x) for x in tokens[0:3]]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    27
		v[0] = scale[0]*v[0] + trans[0]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    28
		v[1] = scale[1]*v[1] + trans[1]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    29
		v[2] = scale[2]*v[2] + trans[2]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    30
		vertices.append(NormalVertex(tuple(v)))
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    31
		normals.append([0.,0.,0.])
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    32
		vertex_face_num.append(0)
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    33
		num -= 1
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    34
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    35
	# read faces
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    36
	while (face_num):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    37
		tokens = fp.readline().split()
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
		if (tokens[0] != "3"):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
			print "ply warning: faces of %d vertices not supported" % tokens[0]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
		v = [vertices[int(x)] for x in tokens[1:4]]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    41
		face = Triangle(v[0], v[2], v[1], mat)
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    42
		n = face.getNormal()
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
		for x in tokens[1:4]:
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
			for i in range(3):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
				normals[int(x)][i] += n[i]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
			vertex_face_num[int(x)] += 1
75
20dee9819b17 unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents: 69
diff changeset
    47
		rt.addShape(face)
29
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    48
		face_num -= 1
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    49
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
	# interpolate normals at vertices
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
	num = 0
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
	while (num < vertex_num):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
		if (vertex_face_num[num] > 0):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
			for i in range(3):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
				normals[num][i] /= vertex_face_num[num]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
		vertices[num].setNormal(tuple(normals[num]))
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
		num += 1