This repository has been archived by the owner on Dec 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
sort.py
executable file
·153 lines (126 loc) · 4.7 KB
/
sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import numpy
import pyrr
def sort_plane_front_to_back(
render_position,
render_direction,
objects,
object_positions
):
"""Sorts objects from front to back along a flat plane.
Sorts objects based upon their dot product value along
the camera direction.
:param numpy.array render_position: The position of the camera the scene
is being rendered from.
:param numpy.array render_direction: The direction the camera is facing.
:param list objects: List of objects to be sorted.
:param list object_positions: List of object positions. These values
map directly to the objects list.
:rtype: numpy.array
:return: A sorted list containing the objects.
"""
if len( object_positions ) <= 0:
return numpy.empty( 0 )
# make positions relative to render position
relative_positions = object_positions - render_position
# do a dot product against the render direction
dot_values = pyrr.vector.dot(
relative_positions,
render_direction
)
# sort the object array by the
# dot product of the object positions
# sort in ascending order
# this first call sorts and returns the indices
inds = numpy.argsort( dot_values, axis = -1 )
# we then rebuilt the object array by the indices
sorted_objects = numpy.take( objects, inds )
return sorted_objects
def sort_plane_back_to_front(
render_position,
render_direction,
objects,
object_positions
):
"""Sorts objects from back to front along a flat plane.
Sorts objects based upon their dot product value along
the camera direction.
:param numpy.array render_position: The position of the camera the scene
is being rendered from.
:param numpy.array render_direction: The direction the camera is facing.
:param list objects: List of objects to be sorted.
:param list object_positions: List of object positions. These values
map directly to the objects list.
:rtype: numpy.array
:return: A sorted list containing the objects.
"""
# sort front to back
sorted_objects = sort_plane_front_to_back(
render_position,
render_direction,
objects,
object_positions
)
# reverse the array
return sorted_objects[::-1]
def sort_radius_front_to_back(
render_position,
render_direction,
objects,
object_positions
):
"""Sorts objects from front to back based on their distance
from the camera.
:param numpy.array render_position: The position of the camera the scene
is being rendered from.
:param numpy.array render_direction: The direction the camera is facing.
:param list objects: List of objects to be sorted.
:param list object_positions: List of object positions. These values
map directly to the objects list.
:rtype: numpy.array
:return: A sorted list containing the objects.
If no objects are passed, an empty numpy array is returned.
"""
if len( object_positions ) <= 0:
return numpy.zeros( 0 )
# make positions relative to render position
relative_positions = object_positions - render_position
# do a dot product against the render direction
lengths = pyrr.vector.squared_length(
relative_positions
)
# sort the object array by the
# dot product of the object positions
# sort in ascending order
# this first call sorts and returns the indices
inds = numpy.argsort( lengths, axis = -1 )
# we then rebuilt the object array by the indices
sorted_objects = numpy.take( objects, inds )
# return inverted
return sorted_objects
def sort_radius_back_to_front(
render_position,
render_direction,
objects,
object_positions
):
"""Sorts objects from back to front based on their distance
from the camera.
:param numpy.array render_position: The position of the camera the scene
is being rendered from.
:param numpy.array render_direction: The direction the camera is facing.
:param list objects: List of objects to be sorted.
:param list object_positions: List of object positions. These values
map directly to the objects list.
:rtype: numpy.array
:return: A sorted list containing the objects.
If no objects are passed, an empty numpy array is returned.
"""
# sort front to back
sorted_objects = sort_radius_front_to_back(
render_position,
render_direction,
objects,
object_positions
)
# reverse the array
return sorted_objects[::-1]