-
Notifications
You must be signed in to change notification settings - Fork 2
/
tf.py
126 lines (118 loc) · 3.99 KB
/
tf.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
from math import *
from euclid import *
from omega import *
from cyclops import *
from omegaToolkit import *
class transferFunction(object):
def reset(self):
self.key = [0, 1]
self.array.update({0: [0, 0, 0, 0]})
self.array.update({1: [1, 1, 1, 1]})
self.volume.clearTransferFunction()
self.volume.addTransferPoint(0, 0, 0, 0, 0);
self.volume.addTransferPoint(1, 1, 1, 1, 1);
self.reDrawLine()
def update( self, intensity, r, g, b, a ):
if (not intensity in self.key) :
self.key.append(intensity)
self.key = sorted(self.key)
self.array.update({intensity: [r, g, b, a]})
else:
self.array.update({intensity: [r, g, b, a]})
self.volume.addTransferPoint(intensity, r, g, b, a)
if (self.sphereList.get(intensity)):
sph = self.sphereList[intensity]
sph.getMaterial().setColor(Color(r, g, b, a), Color(r, g, b, a))
sph.setPosition(Vector3(intensity*self.size[0], a*self.size[1], 0))
def getValue( self, intensity ):
(left, right) = self.getNeighbor( intensity )
ratioRight = (intensity - left)/(right - left)
ratioLeft = 1 - ratioRight
return [ self.array[left][0]*ratioLeft + self.array[right][0]*ratioRight ,self.array[left][1]*ratioLeft + self.array[right][1]*ratioRight, self.array[left][2]*ratioLeft + self.array[right][2]*ratioRight, self.array[left][3]*ratioLeft + self.array[right][3]*ratioRight]
def getNeighbor( self, intensity ):
left = 0
right = 1
for value in self.key:
if (value < intensity):
left = value
else:
right = value
break
return (left, right)
def getClosest( self, intensity ):
(left, right) = self.getNeighbor(intensity)
if (right-intensity) < (intensity-left):
return right
else:
return left
def delete(self, intensity):
if len(self.key) <=2:
return
else:
rkey = self.getClosest(intensity)
self.key.remove(rkey)
del self.array[rkey]
def show(self):
print self.array
def reDrawLine(self):
for line in self.lineList:
line.setThickness(0)
self.sphereNode.setChildrenVisible(False)
data = self.array
left = None
for i in range(0, len(self.key)):
x = self.key[i]
sph = self.sphereList.get(x)
if (sph):
sph.setVisible(True)
sph.getMaterial().setColor(Color(data[x][0], data[x][1], data[x][2], data[x][3]), Color(data[x][0], data[x][1], data[x][2], data[x][3]))
else:
sph = SphereShape.create(self.width*4, 1)
sph.getMaterial().setColor(Color(data[x][0], data[x][1], data[x][2], data[x][3]), Color(data[x][0], data[x][1], data[x][2], data[x][3]))
sph.setPosition(Vector3(x*self.size[0], data[x][3]*self.size[1], 0))
self.sphereNode.addChild(sph)
self.sphereList.update({x: sph})
if (left == None):
left = x
else:
right = x
line = self.line.addLine()
self.lineList.append(line)
line.setStart(Vector3(left*self.size[0], data[left][3]*self.size[1], 0))
line.setEnd(Vector3(right*self.size[0], data[right][3]*self.size[1], 0))
line.setThickness(self.width/2)
left = right
def setPosition(self, x, y, z):
self.root.setPosition(Vector3(x, y, z))
def __init__(self, volume, x, y, z, width, height, lw):
self.volume = volume
self.array = {}
self.key = []
self.root = SceneNode.create('transferFunc')
self.setPosition(x, y, z)
self.size = [width, height]
self.line = LineSet.create()
self.lineList = []
self.sphereList = {}
self.sphereNode = SceneNode.create('sphereRoot')
self.root.addChild(self.sphereNode)
self.root.addChild(self.line)
self.line.setEffect('colored -e white')
line = self.line.addLine()
line.setStart(Vector3(0, 0, 0))
line.setEnd(Vector3(width, 0, 0))
line.setThickness(lw)
line = self.line.addLine()
line.setStart(Vector3(width, 0, 0))
line.setEnd(Vector3(width, height, 0))
line.setThickness(lw)
line = self.line.addLine()
line.setStart(Vector3(0, height, 0))
line.setEnd(Vector3(width, height, 0))
line.setThickness(lw)
line = self.line.addLine()
line.setStart(Vector3(0, 0, 0))
line.setEnd(Vector3(0, height, 0))
line.setThickness(lw)
self.width = lw
self.reset()