User Tools

Site Tools


moudot:solution_homework2
Critères & points attribués (/10)
Rappel du contexte 6
Résultats 7
Qualité du code (commentaires, structures utilisées, …) 8
Animation du dessin 4
Explication en dehors du code 0
Total (/50) 25

B.P. : Les machines du CREMI vous permettent de travailler dans de bonnes conditions. Votre code fonctionne bien sur un arbre complet. Il y a encore quelques problèmes sur un arbre standard. Vous n'avez mis aucun commentaire en dehors du code (sauf le rappel du sujet).

Onion tree

Once again, due to the lack of power of my netbook, I wasn't able to test in good conditions the animation (i.e., it is not working); you can find a try at the bottom of the script.

homework_2_onion_tree_by_Maxime_Oudot.py
# Powered by Python 2.7
 
# To cancel the modifications performed by the script
# on the current graph, click on the undo button.
 
# Some useful keyboards shortcuts : 
#   * Ctrl + D : comment selected lines.
#   * Ctrl + Shift + D  : uncomment selected lines.
#   * Ctrl + I : indent selected lines.
#   * Ctrl + Shift + I  : unindent selected lines.
#   * Ctrl + Return  : run script.
#   * Ctrl + F  : find selected text.
#   * Ctrl + R  : replace selected text.
#   * Ctrl + Space  : show auto-completion dialog.
 
from tulip import *
from threading import *
from random import randint
from math import *
 
# the updateVisualization(centerViews = True) function can be called
# during script execution to update the opened views
 
# the pauseScript() function can be called to pause the script execution.
# To resume the script execution, you will have to click on the "Run script " button.
 
# the runGraphScript(scriptFile, graph) function can be called to launch another edited script on a tlp.Graph object.
# The scriptFile parameter defines the script name to call (in the form [a-zA-Z0-9_]+.py)
 
# the main(graph) function must be defined 
# to run the script on the current graph
 
def main(graph): 
	Random_layout__viewLayout =  graph.getLayoutProperty("Random layout - (viewLayout)")
	viewBorderColor =  graph.getColorProperty("viewBorderColor")
	viewBorderWidth =  graph.getDoubleProperty("viewBorderWidth")
	viewColor =  graph.getColorProperty("viewColor")
	viewFont =  graph.getStringProperty("viewFont")
	viewFontSize =  graph.getIntegerProperty("viewFontSize")
	viewLabel =  graph.getStringProperty("viewLabel")
	viewLabelColor =  graph.getColorProperty("viewLabelColor")
	viewLabelPosition =  graph.getIntegerProperty("viewLabelPosition")
	viewLayout =  graph.getLayoutProperty("viewLayout")
	viewMetaGraph =  graph.getGraphProperty("viewMetaGraph")
	viewMetric =  graph.getDoubleProperty("viewMetric")
	viewRotation =  graph.getDoubleProperty("viewRotation")
	viewSelection =  graph.getBooleanProperty("viewSelection")
	viewShape =  graph.getIntegerProperty("viewShape")
	viewSize =  graph.getSizeProperty("viewSize")
	viewSrcAnchorShape =  graph.getIntegerProperty("viewSrcAnchorShape")
	viewSrcAnchorSize =  graph.getSizeProperty("viewSrcAnchorSize")
	viewTexture =  graph.getStringProperty("viewTexture")
	viewTgtAnchorShape =  graph.getIntegerProperty("viewTgtAnchorShape")
	viewTgtAnchorSize =  graph.getSizeProperty("viewTgtAnchorSize")
 
	# Builds the onion tree.
	def onion_tree(node, vertical = False, width = 1000, height = 600, x = 0, y = 0, gap = 10, color = tlp.Color(100, 100, 100), number = 0):
		# Call to the not working animation.
		#onion_tree_move_nodes_over_time([node], [[x, y]])
		onion_tree_move_node(node, x, y)
		onion_tree_show_node_label(node, "node " + str(number))
		onion_tree_change_node_shape(node, tlp.Size(width, height, 0), color, shape = "4")
 
		childCount = graph.outdeg(node)
		if childCount > 0:
			if not vertical:
				x -= width / 2
			if vertical:
				y += height / 2
			newWidth = width - gap
			newHeight = height - gap
 
			# So that we know which dimension should be reduced to make every nodes fit in the parent node.
			if vertical:
				newHeight = (newHeight - gap * (childCount + 1 / 2)) / childCount
				newWidth -= gap
				y -= (newHeight / 2 + gap)
			else:
				newWidth = (newWidth  - gap * (childCount + 1 / 2)) / childCount
				newHeight -= gap
				x += (newWidth / 2 + gap)
 
			color = onion_tree_get_random_color()
			for n in graph.getOutNodes(node):
				number += 1
				onion_tree(n, not vertical, newWidth, newHeight, x, y, gap, color, number)
				# So that we know in which direction to proceed.
				if vertical:
					y -= (newHeight + gap)
				else:
					x += (newWidth + gap)
 
	# Returns a random color.
	def onion_tree_get_random_color(alpha = 255):
		return tlp.Color(randint(0, 256), randint(0, 256), randint(0, 256), alpha)		
 
	# Change the given node's shape.
	def onion_tree_change_node_shape(node, size = tlp.Size(10, 10, 10), color = tlp.Color(255, 255, 255), shape = "4"):
		viewSize.setNodeValue(node, size)
		viewColor[node] = color
		viewShape.setAllNodeStringValue(shape)		
 
	# Shows a label near the given node, with the given text and given color.
	def onion_tree_show_node_label(node, label = "node",  color = tlp.Color(50, 50, 50)):
		viewLabel.setNodeValue(node, label)
		viewLabelColor.setNodeValue(node, color)	
 
	# Moves the node at given (x, y) position.
	def onion_tree_move_node(node, x, y):
		nodeValue = viewLayout.getNodeValue(node)	
		nodeValue.setX(x)
		nodeValue.setY(y)
		viewLayout.setNodeValue(node, nodeValue)
 
 
	# Execution of the onion tree building script.
	onion_tree(tlp.Graph.getSource(graph), color = onion_tree_get_random_color())
 
# This is the not working animation.
#
#	def onion_tree_move_node_by_delta(node, x, y):
#		nodeValue = viewLayout.getNodeValue(node)	
#		nodeValue.setX(nodeValue.getX() + x)
#		nodeValue.setY(nodeValue.getY() + y)
#		viewLayout.setNodeValue(node, nodeValue)		
#		
#	def onion_tree_compute_vector_with_velocity(origin, dest, velocity):
#		vector = (origin[0] - dest[0], origin[1] - dest[1])
#		norm = sqrt(vector[0] ** 2 + vector[1] ** 2)
#		vector = (vector[0] * velocity / norm, vector[1] * velocity / norm)
#		return vector
#		
#	def onion_tree_position_equal(point1, point2):
#		return (point1[0] == point2[0] and point1[1] == point2[1])		
#		
#	def onion_tree_distance(point1, point2):
#		return sqrt((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2)
#		
#	def onion_tree_move_nodes_over_time(nodes, points, velocity = 1, wait = 2):
#		while len(nodes) > 0:
#			for i in range(len(nodes)):
#				if i < len(nodes):
#					n = nodes[i]
#					p = points[i]
#					vector = onion_tree_compute_vector_with_velocity((viewLayout.getNodeValue(n).getX(), viewLayout.getNodeValue(n).getY()), p, velocity)
#					onion_tree_move_node_by_delta(n, vector[0], vector[1])
#					value = viewLayout.getNodeValue(n)
#					newPos = [value.getX(), value.getY()]
#					if onion_tree_distance(newPos, p):
#						onion_tree_move_node(n, p[0], p[1])
#						newPos = p
#					if onion_tree_position_equal(newPos, p):
#						nodes.remove(n)
#			t = Timer(wait, onion_tree_move_nodes_over_time)
#			t.start()

/net/html/perso/melancon/Visual_Analytics_Course/data/pages/moudot/solution_homework2.txt · Last modified: 2012/10/23 12:13 by bpinaud