-
Notifications
You must be signed in to change notification settings - Fork 4
/
pattern.py
83 lines (73 loc) · 3.04 KB
/
pattern.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
from __future__ import division
import numpy as np
from printer import PDFPrinter, DXFPrinter
import random
import sys
import os.path
import csv
import matplotlib.pyplot as plt
__author__ = "Robin Deits <[email protected]>"
RESOLUTION = 60
class PatternMaker:
def __init__(self, filename, printers,
image_width_in = 4,
viewing_height_in = 24):
self.setup_common(filename, printers, image_width_in, viewing_height_in)
def setup_common(self, filename, printers, image_width_in, viewing_height_in):
self.filename = filename
self.reader = csv.reader(open(filename, 'rb'))
self.printers = printers
self.data = np.array([[float(i) for i in row] for row in self.reader])
# print self.data
self.rescale(image_width_in)
self.viewing_height_in = viewing_height_in
def rescale(self, image_width_in):
z_max = np.max(np.abs(self.data[:,2]))
x_range = (np.max(self.data[:,0])
- np.min(self.data[:,0])
+ 2*z_max)
self.data[:,:3] *= image_width_in / x_range
def print_pattern(self):
num_points = len(self.data[:,0])
for i in range(num_points):
self.plot_point(self.data[i,:])
for printer in self.printers:
printer.save(os.path.splitext(self.filename)[0])
def plot_point(self, point):
x = point[0]
y = point[1]
z = point[2]
angles = -np.array([point[3], point[4]]) + np.pi / 2
for printer in self.printers:
printer.draw_arc([x, y + z], -z,
# printer.draw_arc([x, y], -z,
angles = angles)
def draw_view(self, angle):
view_pos = np.array([self.viewing_height_in * np.tan(angle),
0,
self.viewing_height_in])
num_points = len(self.data[:,0])
view_printer = PDFPrinter()
for i in range(num_points):
x = self.data[i,0]
y = self.data[i,1]
z = self.data[i,2]
point_angle = np.arctan((view_pos[0] - x)
/ (view_pos[2] - z))
if self.data[i, 3] < point_angle < self.data[i,4]:
draw_angle = -point_angle + np.pi/2
view_printer.draw_point([x - z * np.cos(draw_angle),
y + z - z * np.sin(draw_angle)], marker = '*',
# y - z * np.sin(draw_angle)], marker = '*',
color = 'k')
view_printer.save(os.path.splitext(self.filename)[0]
+ "_view_" + ("%+3d" %(angle * 180/np.pi)).strip())
def draw_views(self, angle):
for printer in self.printers:
if isinstance(printer, DXFPrinter):
print "DXFPrinter can't draw perspective views, aborting"
continue
self.draw_view(angle)
self.draw_view(-angle)
def distance(p0, p1):
return np.sqrt(np.sum(np.power(np.array(p1) - np.array(p0), 2)))