Skip to content
This repository has been archived by the owner on May 21, 2019. It is now read-only.

Commit

Permalink
looking for scaling bug. draw bbox.
Browse files Browse the repository at this point in the history
  • Loading branch information
inconvergent committed Apr 13, 2017
1 parent a3e1299 commit 0cd7e39
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 21 deletions.
25 changes: 25 additions & 0 deletions draw_bbox.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-


from xy.device import Device
from numpy import array


def main():

paths = [array([
[0, 0],
[1, 0],
[1, 1],
[0, 1],
[0, 0]], 'float')]

with Device(scale=0.99, penup=0.4) as device:
device.do_paths(paths)


if __name__ == '__main__':

main()

7 changes: 7 additions & 0 deletions draw_edges.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@

from xy.device import Device

# from numpy import row_stack
# from numpy import min, max


def main(args):
from modules.utils import get_edges_from_file as get

fn = args.fn
paths = get(fn)

# ss = row_stack(paths)
# print(min(ss[:,0], axis=0), max(ss[:,0], axis=0))
# print(min(ss[:,1], axis=0), max(ss[:,1], axis=0))

with Device(scale=0.99, penup=0.4) as device:
device.do_paths(paths)

Expand Down
30 changes: 26 additions & 4 deletions modules/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@ def do_scale(xy):

def fit(vertices):
from modules.ddd import get_mid_2d as get_mid
vertices -= get_mid(vertices)
mid = get_mid(vertices)
vertices -= mid
do_scale(vertices)
mid = get_mid(vertices)
vertices[:, :] += array([[0.5]*2])
mid = get_mid(vertices)

def get_paths_from_n_files(
pattern,
Expand Down Expand Up @@ -56,7 +59,11 @@ def get_paths_from_n_files(

vertices = row_stack(vertices)

print('orig size:')
print_values(*get_bounding_box(vertices))

fit(vertices)

print('scaled size:')
print_values(*get_bounding_box(vertices))

Expand All @@ -81,7 +88,11 @@ def get_paths_from_file(
vertices = data['vertices']
lines = data['lines']

print('orig size:')
print_values(*get_bounding_box(vertices))

fit(vertices)

print('scaled size:')
print_values(*get_bounding_box(vertices))

Expand All @@ -105,7 +116,11 @@ def get_tris_from_file(
data = load(fn)
vertices = data['vertices']

print('orig size:')
print_values(*get_bounding_box(vertices))

fit(vertices)

print('scaled size:')
print_values(*get_bounding_box(vertices))

Expand All @@ -126,13 +141,17 @@ def get_dots_from_file(
data = load(fn)
vertices = data['vertices']

print('orig size:')
print_values(*get_bounding_box(vertices))

fit(vertices)
dots = vertices

print('scaled size:')
print_values(*get_bounding_box(vertices))

dots = sort(dots) if spatial_sort else dots
return dots
vertices = sort(vertices) if spatial_sort else vertices
print('dots: ', len(vertices))
return vertices

def get_edges_from_file(
fn,
Expand All @@ -147,6 +166,9 @@ def get_edges_from_file(
data = load(fn)
vertices = data['vertices']

print('orig size:')
print_values(*get_bounding_box(vertices))

fit(vertices)
print('scaled size:')
print_values(*get_bounding_box(vertices))
Expand Down
43 changes: 26 additions & 17 deletions xy/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@

from numpy import array
from numpy import isclose
from numpy import row_stack
import urllib.request
import json

from modules.utils import print_values
from modules.utils import get_bounding_box

HEADERS = {
'Content-Type': 'application/json'
}
Expand All @@ -25,13 +29,13 @@ class Device(object):

def __init__(
self,
scale = 1.0,
penup = 0.5,
pendown = 1,
host = 'http://localhost:4242',
drawing_speed = 7,
moving_speed = 30,
verbose = False
scale=1.0,
penup=0.5,
pendown=1,
host='http://localhost:4242',
drawing_speed=7,
moving_speed=30,
verbose=False
):
self._scale = float(scale)
self._penup = float(penup)
Expand All @@ -51,14 +55,16 @@ def __init__(
self.penup()

self._moves = 0
self._history = []

def __enter__(self):
input('enter to start ...')
return self

def __exit__(self,*arg, **args):
def __exit__(self, *arg, **args):
self.penup()
self.move(array([0,0], 'float'))
self.move(array([0, 0], 'float'))
print_values(*get_bounding_box(row_stack(self._history)))

def _settings(self):
self._cmd(
Expand Down Expand Up @@ -113,30 +119,33 @@ def _xy_transform(self, xy):

# TODO: this is a bit overzealous. consider improving.

if any(txy>100.0):
print('WARN: value greater than 100. correcting')
if any(txy > 100.0):
print('WARNING: value greater than 100. correcting {:s}'.format(str(txy)))
mask = isclose(txy, 100.0)
txy[mask] = 100.0
if any(txy>100.0):
print('WARNING: corrected to {:s}'.format(str(txy)))
if any(txy > 100.0):
print('offending value:')
print(txy)
raise ValueError('unable to correct error. aborting.')

if any(txy<0.0):
print('WARN: value less than 0. correcting.')
if any(txy < 0.0):
print('WARNING: value less than 0. correcting: {:s}'.format(str(txy)))
mask = isclose(txy, 0.0)
txy[mask] = 0.0
if any(txy<0.0):
print('WARNING: corrected to {:s}'.format(str(txy)))
if any(txy < 0.0):
print('offending value:')
print(txy)
raise ValueError('unable to correct error. aborting.')

self._history.append(txy)
return txy

def reset(self):
self._cmd_del(self._buffer_url)
self.penup()
self.move(array([0,0], 'float'))
self.move(array([0, 0], 'float'))

def move(self, xy):
self._moves += 1
Expand All @@ -147,7 +156,7 @@ def move(self, xy):
)

def pen(self, position):
if position>1.0 or position<0.0:
if position > 1.0 or position < 0.0:
raise ValueError('bad pen elevation.')
self._cmd(
{'state': position},
Expand Down

0 comments on commit 0cd7e39

Please sign in to comment.