-
Notifications
You must be signed in to change notification settings - Fork 2
/
Geo-Shader-Sphere-100-repulsion.py
193 lines (160 loc) · 5.56 KB
/
Geo-Shader-Sphere-100-repulsion.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 3 17:40:55 2018
@author: Ashu
"""
import vtk
import numpy as np
#from random import randint
import random
colors = vtk.vtkNamedColors()
# Create the geometry of a point (the coordinate)
points = vtk.vtkPoints()
# Create the topology of the point (a vertex)
vertices = vtk.vtkCellArray()
# We need an an array of point id's for InsertNextCell.
pid = [0, 1000, 0, 0]
number_of_Spheres = 30000
pid = [0]*number_of_Spheres
for i in range(number_of_Spheres):
pid[i] = points.InsertNextPoint([random.uniform(0, 500), random.uniform(0, 500), random.uniform(0, 500)])
vertices.InsertNextCell(number_of_Spheres, pid)
# Create a polydata object
point = vtk.vtkPolyData()
# Set the points and vertices we created as the geometry and
# topology of the polydata
point.SetPoints(points)
point.SetVerts(vertices)
# Create array of vertex colors
colorArray = vtk.vtkUnsignedCharArray()
colorArray.SetNumberOfTuples(number_of_Spheres)
colorArray.SetNumberOfComponents(3)
for h in range(number_of_Spheres):
colorArray.InsertTuple(h, (random.uniform(0, 255), random.uniform(0, 255), random.uniform(0, 255)))
point.GetPointData().SetScalars(colorArray)
# Visualize
mapper = vtk.vtkOpenGLPolyDataMapper()
mapper.SetInputData(point)
mapper2 = vtk.vtkOpenGLPolyDataMapper()
mapper2.SetInputData(point)
#LOAD THE DIPY 100 Repulsion FILE
# sphere = np.load('C:/Users/Ashu/Downloads_New/dipy-master/dipy/data/files/repulsion100.npz')
# faces = sphere['faces'].astype('i8')
# vertices = sphere['vertices']
faces = np.load('faces.npy')
faces = faces.astype('i8')
vertices = np.load('vertices.npy')
# sphere = dipy.data.get_sphere("repulsion100")
# faces = sphere.faces
# vertices = sphere.vertices
##################FINDING LIST OF VERTICES####################
first=0
second=1
def find_instance(faces, first, second):
found_flag = 0
for i in range(196):
if faces[i, 0] == first and faces[i, 1] == second and found_flag == 0:
found_flag = 1
temp = faces[i, :]
x1 = temp[1]
x2 = temp[2]
final_vertices_list.append(x2)
faces[i, :] = np.array([-1,-1,-1])
if found_flag == 1:
return find_instance(faces, x1, x2)
else:
return 0
final_vertices_list = []
for j in range(196):
x = faces[j, :] == np.array([-1, -1, -1])
if x.sum() == 0:
first = faces[j, 1]
second = faces[j, 2]
final_vertices_list.append(faces[j, 0])
final_vertices_list.append(faces[j, 1])
final_vertices_list.append(faces[j, 2])
find_instance(faces, first, second)
############################################################
geometry_shader_code = """
//VTK::System::Dec
//VTK::PositionVC::Dec
uniform mat4 MCDCMatrix;
uniform vec3 vertices[100];
uniform vec3 order[330];
uniform vec3 red[3];
uniform int delay=165;
//VTK::PrimID::Dec
// declarations below aren't necessary because
// they are already injected by PrimID template
//in vec4 vertexColorVSOutput[];
//out vec4 vertexColorGSOutput;
//VTK::Color::Dec
//VTK::Normal::Dec
//VTK::Light::Dec
//VTK::TCoord::Dec
//VTK::Picking::Dec
//VTK::DepthPeeling::Dec
//VTK::Clip::Dec
//VTK::Output::Dec
layout(points) in;
layout(triangle_strip, max_vertices = 256) out;
in VS_OUT {
vec3 color;
} gs_in[];
out vec3 fColor;
void build_house(vec4 position)
{
//fColor = gs_in[1].color;
for(int g=0;g<165;g++){
gl_Position = position + (MCDCMatrix * vec4(vertices[int(order[g+delay].x)].x, vertices[int(order[g+delay].x)].y, vertices[int(order[g+delay].x)].z, 0.0));
EmitVertex();
}
EndPrimitive();
}
void main() {
vertexColorGSOutput = vertexColorVSOutput[0];
build_house(gl_in[0].gl_Position);
}
"""
mapper.SetGeometryShaderCode(geometry_shader_code)
mapper2.SetGeometryShaderCode(geometry_shader_code)
vertices = vertices * 5 #To increase radius of sphere
@vtk.calldata_type(vtk.VTK_OBJECT)
def vtkShaderCallback(caller, event, calldata=None):
program = calldata
if program is not None:
for i in range(100):
program.SetUniform3f("vertices[%d]"%(i), vertices[i].tolist())
for j in range(330):
program.SetUniform3f("order[%d]"%(j), [final_vertices_list[j], 0, 0])
program.SetUniformi("delay", 165)
mapper.AddObserver(vtk.vtkCommand.UpdateShaderEvent,vtkShaderCallback)
@vtk.calldata_type(vtk.VTK_OBJECT)
def vtkShaderCallback2(caller, event, calldata=None):
program = calldata
if program is not None:
for i in range(100):
program.SetUniform3f("vertices[%d]"%(i), vertices[i].tolist())
for j in range(330):
program.SetUniform3f("order[%d]"%(j), [final_vertices_list[j], 0, 0])
program.SetUniformi("delay", 0)
mapper2.AddObserver(vtk.vtkCommand.UpdateShaderEvent,vtkShaderCallback2)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
# actor.GetProperty().SetColor(colors.GetColor3d("Tomato"))
actor.GetProperty().SetPointSize(2)
actor2 = vtk.vtkActor()
actor2.SetMapper(mapper2)
actor2.GetProperty().SetPointSize(2)
renderer = vtk.vtkRenderer()
renderWindow = vtk.vtkRenderWindow()
renderWindow.SetWindowName("Point")
renderWindow.SetSize(500, 500)
renderWindow.AddRenderer(renderer)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
renderer.AddActor(actor)
renderer.AddActor(actor2)
renderer.SetBackground(colors.GetColor3d("Black"))
renderWindow.Render()
renderWindowInteractor.Start()