demos/plyreader.py
author Radek Brich <radek.brich@devl.cz>
Tue, 26 Jul 2016 17:41:36 +0200
branchpyrit
changeset 103 3b3257a410fe
parent 97 2a853d284a6a
permissions -rw-r--r--
Updated to compile: - KdTree+Octree: max_depth changed to static const (this should be configured at compile time) - wget tool replaced by curl, which is now more widespread - added CMakeLists (to eventually replace SCons) - various fixes
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
90
f6a72eb99631 rename Python module from 'raytracer' to 'pyrit'
Radek Brich <radek.brich@devl.cz>
parents: 75
diff changeset
     3
from pyrit import Triangle, NormalVertex
29
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
97
2a853d284a6a added bench.py demo which prints tree build and render times
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    36
	num = face_num
2a853d284a6a added bench.py demo which prints tree build and render times
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    37
	while (num):
29
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    38
		tokens = fp.readline().split()
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    39
		if (tokens[0] != "3"):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    40
			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
    41
		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
    42
		face = Triangle(v[0], v[2], v[1], mat)
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    43
		n = face.getNormal()
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    44
		for x in tokens[1:4]:
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    45
			for i in range(3):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    46
				normals[int(x)][i] += n[i]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    47
			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
    48
		rt.addShape(face)
97
2a853d284a6a added bench.py demo which prints tree build and render times
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    49
		num -= 1
29
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    50
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    51
	# interpolate normals at vertices
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    52
	num = 0
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    53
	while (num < vertex_num):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    54
		if (vertex_face_num[num] > 0):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    55
			for i in range(3):
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    56
				normals[num][i] /= vertex_face_num[num]
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    57
		vertices[num].setNormal(tuple(normals[num]))
574c34441a1c new C++ demo: realtime_bunny
Radek Brich <radek.brich@devl.cz>
parents:
diff changeset
    58
		num += 1
97
2a853d284a6a added bench.py demo which prints tree build and render times
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    59
2a853d284a6a added bench.py demo which prints tree build and render times
Radek Brich <radek.brich@devl.cz>
parents: 90
diff changeset
    60
	return face_num