forked from VanHulleOne/SciSlice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwireframe.py
95 lines (76 loc) · 3 KB
/
wireframe.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
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 22 13:56:44 2016
@author: adiebold
"""
import math
class Node:
def __init__(self, coordinates):
self.x = coordinates[0]
self.y = coordinates[1]
self.z = coordinates[2]
class Edge:
def __init__(self, start, stop):
self.start = start
self.stop = stop
class Wireframe:
def __init__(self):
self.nodes = []
self.edges = []
def addNodes(self, nodeList):
for node in nodeList:
self.nodes.append(Node(node))
def addEdges(self, edgeList):
for (start, stop) in edgeList:
self.edges.append(Edge(self.nodes[start], self.nodes[stop]))
def outputNodes(self):
print("\n --- Nodes --- ")
for i, node in enumerate(self.nodes):
print(" %d: (%.2f, %.2f, %.2f)" % (i, node.x, node.y, node.z))
def outputEdges(self):
print("\n --- Edges --- ")
for i, edge in enumerate(self.edges):
print(" %d: (%.2f, %.2f, %.2f)" % (i, edge.start.x, edge.start.y, edge.start.z),)
print("to (%.2f, %.2f, %.2f)" % (edge.stop.x, edge.stop.y, edge.stop.z))
def translate(self, axis, d):
""" Add constant 'd' to the coordinate 'axis' of each node of a wireframe """
if axis in ['x', 'y', 'z']:
for node in self.nodes:
setattr(node, axis, getattr(node, axis) + d)
def scale(self, center_x, center_y, scale):
""" Scale the wireframe from the center of the screen """
for node in self.nodes:
node.x = center_x + scale * (node.x - center_x)
node.y = center_y + scale * (node.y - center_y)
node.z *= scale
def findcenter(self):
""" Find the center of the wireframe. """
num_nodes = len(self.nodes)
meanX = sum([node.x for node in self.nodes]) / num_nodes
meanY = sum([node.y for node in self.nodes]) / num_nodes
meanZ = sum([node.z for node in self.nodes]) / num_nodes
return (meanX, meanY, meanZ)
def rotateX(self, cx,cy,cz, radians):
for node in self.nodes:
y = node.y - cy
z = node.z - cz
d = math.hypot(y, z)
theta = math.atan2(y, z) + -radians
node.z = cz + d * math.cos(theta)
node.y = cy + d * math.sin(theta)
def rotateY(self, cx,cy,cz, radians):
for node in self.nodes:
x = node.x - cx
z = node.z - cz
d = math.hypot(x, z)
theta = math.atan2(x, z) + radians
node.z = cz + d * math.cos(theta)
node.x = cx + d * math.sin(theta)
def rotateZ(self, cx,cy,cz, radians):
for node in self.nodes:
x = node.x - cx
y = node.y - cy
d = math.hypot(y, x)
theta = math.atan2(y, x) + -radians
node.x = cx + d * math.cos(theta)
node.y = cy + d * math.sin(theta)