def __iter__(self): figure_margin = 2 paper_size = Vector2D(self._paper.width, self._paper.height) / 10 # mm paper_margin = (self._paper.margin + figure_margin) / 10 area_vector = paper_size - Vector2D(paper_margin, paper_margin) * 2 number_of_columns = ceil_int(self._bounding_box.x.length / area_vector.x) number_of_rows = ceil_int(self._bounding_box.y.length / area_vector.y) self._logger.info('Bounding Box {}'.format(self._bounding_box)) self._logger.info('Area {}'.format(area_vector)) self._logger.info('Grid {}x{}'.format(number_of_rows, number_of_columns)) min_point = Vector2D( (self._bounding_box.x.inf, self._bounding_box.y.inf)) for r in range(number_of_rows): for c in range(number_of_columns): local_min_point = min_point + area_vector * Vector2D(r, c) local_max_point = local_min_point + area_vector interval = Interval2D((local_min_point.x, local_max_point.x), (local_min_point.y, local_max_point.y)) yield interval
def test(self): p0 = Vector2D(0, 0) p1 = Vector2D(3, 5) p2 = Vector2D(6, 5) p3 = Vector2D(10, 0) curve = CubicBezier2D(p0, p1, p2, p3) self.assertAlmostEqual(curve.q_length(), curve.interpolated_length(), 0) self.assertAlmostEqual(curve.length, curve.interpolated_length(), 3) split = curve.split_at_t(.5) self.assertAlmostEqual(sum([curve.length for curve in split]), curve.length, 4) # Self intersecting curve curve = CubicBezier2D(p0, p2, p1, p3) self.assertAlmostEqual(curve.q_length(), curve.interpolated_length(), 0) self.assertAlmostEqual(curve.length, curve.interpolated_length(), 3) split = curve.split_at_t(.5) self.assertAlmostEqual(sum([curve.length for curve in split]), curve.length, 4)
def test_simplification(self): # Points are clockwise oriented points = Vector2D.from_coordinates( ( 0.1, 10.1), # 0 ( -0.1, 10.9), # 1 ( 0.1, 20.1), # 2 ( 0 , 30 ), # 3 * ( 15.1, 30.1), # 4 ( 30 , 30 ), # 5 * ( 29.9, 10.0), # 6 ( 30 , 0 ), # 7 * ( 15.1, 0.1), # 8 ( 0 , 0 ), # 9 * ) polygon = Polygon2D(*points) simplified_polygon = polygon.simplify(threshold=1.) simplified_polygon_truth = Polygon2D(*[points[i] for i in (3, 5, 7, 9)]) self.assertEqual(simplified_polygon, simplified_polygon_truth) points = Vector2D.from_coordinates( ( 0 , 0 ), ( 0.1, 10.1), ( 0.2, 20.1), ) polygon = Polygon2D(*points) simplified_polygon = polygon.simplify(threshold=1.) self.assertIsNone(simplified_polygon)
def eval_internal(self): control_point1_offset = Vector2D.from_angle( self._angle1.value) * self._length1.value control_point2_offset = Vector2D.from_angle( self._angle2.value) * self._length2.value self._control_point1 = self.first_point.vector + control_point1_offset self._control_point2 = self.second_point.vector + control_point2_offset
def _make_quadratic(self, start_point): path = Path2D(start_point) path.quadratic_to( Vector2D(0, 10), Vector2D(10, 10), ) return path
def _make_cubic(self, start_point): path = Path2D(start_point) path.cubic_to( Vector2D(5, 10), Vector2D(10, 10), Vector2D(15, 0), ) return path
def _make_absolute_quadratic(self, start_point): path = Path2D(start_point) path.quadratic_to( path.p0 + Vector2D(0, 10), path.p0 + Vector2D(10, 10), absolute=True, ) return path
def _make_absolute_cubic(self, start_point): path = Path2D(start_point) path.cubic_to( path.p0 + Vector2D(5, 10), path.p0 + Vector2D(10, 10), path.p0 + Vector2D(15, 0), absolute=True, ) return path
def test(self): p0 = Vector2D(0, 0) p1 = Vector2D(5, 5) p2 = Vector2D(10, 0) curve = QuadraticBezier2D(p0, p1, p2) self.assertAlmostEqual(curve.length, curve.interpolated_length(), 4) split = curve.split_at_t(.5) self.assertAlmostEqual(sum([curve.length for curve in split]), curve.length, 4)
def _make_figure1(self): items = [] for angle in range(0, 360, 30): point1 = Vector2D(0, 0) point2 = Vector2D.from_polar(50, angle) segment = Segment2D(point1, point2) items.append(segment) head = TriangularHead(point2, segment.direction, length=5, width=5) # head = head.to_path() items.append(head) return items
def _paint_axis_grid(self, xinf, xsup, yinf, ysup, is_x, step): for i in range(int(xinf // step), int(xsup // step) + 1): x = i * step if xinf <= x <= xsup: if is_x: p0 = Vector2D(x, yinf) p1 = Vector2D(x, ysup) else: p0 = Vector2D(yinf, x) p1 = Vector2D(ysup, x) p0 = self.cast_position(p0) p1 = self.cast_position(p1) self._painter.drawLine(p0, p1)
def _on_polyline(self, item): polyline = Polyline(item.closed) for x, y, s, e, b in item.get_points(): polyline.add(Vector2D(x, y), b) geometry = polyline.geometry() self._add(geometry)
def __init__(self, path, scene, paper): super().__init__(scene) self._path = path self._paper = paper self._coordinates = {} bounding_box = scene.bounding_box self._transformation = AffineTransformation2D.Scale(10, -10) self._transformation *= AffineTransformation2D.Translation( -Vector2D(bounding_box.x.inf, bounding_box.y.sup)) self._tree = [] self._append( SvgFormat.Style(text=''' .normal { font: 12px sans-serif; } ''')) self.paint() self._svg_file = SvgFile() self._svg_file.write(paper, self._tree, transformation=None, path=path)
def as_vector(cls, args): number_of_args = len(args) number_of_vectors = number_of_args // 2 if number_of_args != number_of_vectors * 2: raise ValueError( 'len(args) is not // 2: {}'.format(number_of_args)) return [Vector2D(args[i:i + 2]) for i in range(0, number_of_args, 2)]
def test_basic(self): x, y = 10, 20 # points = Vector2D.from_coordinates() p0 = Vector2D( x, y) p1 = Vector2D( x, -y) p2 = Vector2D(-x, -y) p3 = Vector2D(-x, y) points = [p0, p1, p2, p3] # Ctor polygon = Polygon2D(*points) self.assertEqual(polygon.number_of_points, len(points)) self.assertListEqual(list(polygon.points), points) # Clone clone = polygon.clone() self.assertEqual(polygon, clone) for i in range(polygon.number_of_points): self.assertEqual(polygon[i], clone[i]) # same value self.assertIsNot(polygon[i], clone[i]) # but cloned ! self.assertTrue(polygon.is_closed) self.assertTrue(polygon.is_simple) self.assertTrue(polygon.is_convex) # Edges self.assertEqual(polygon.number_of_edges, len(points)) for i in range(i): self.assertEqual(polygon.edges[i], Segment2D(points[i], points[(i+1)%len(points)])) self.assertEqual(polygon.edges[1], Segment2D(p1, p2)) self.assertEqual(polygon.edges[2], Segment2D(p2, p3)) self.assertEqual(polygon.edges[3], Segment2D(p3, p0)) # Perimeter Area self.assertEqual(polygon.perimeter, 4*(x+y)) self.assertEqual(polygon.area, 4*x*y) # Barycenter origin = Vector2D(0, 0) self.assertEqual(polygon.point_barycenter, origin) self.assertEqual(polygon.barycenter, origin) # Simplify self.assertIs(polygon.simplify(threshold=1.), polygon)
def test_convex_hull(self): # 6 * # 5 * # 4 + # 3 + + + * # 2 * + * # 1 * * # 0 * # - 0 1 2 3 4 5 6 7 # Points are clockwise oriented points = Vector2D.from_coordinates( (0, 0), # 0 p0 (-1, 2), # 1 * (0, 3), # 2 (2, 4), # 3 (1, 5), # 4 * (4, 6), # 5 * (3, 3), # 6 (5, 3), # 7 (7, 3), # 8 * (7, 2), # 9 * (6, 1), # 10 * (3, 2), # 11 (2, 1), # 12 ) convex_hull_points = [points[i] for i in (0, 10, 9, 8, 5, 4, 1)] # ccw polygon = Polygon2D(*points) convex_hull_truth = Polygon2D(*convex_hull_points) self.assertTrue(polygon.is_simple) self.assertFalse(polygon.is_convex) self.assertTrue(polygon.is_concave) self.assertTrue(polygon.is_clockwise) self.assertFalse(polygon.is_counterclockwise) convex_hull = polygon.convex_hull() self.assertListEqual(list(convex_hull.points), convex_hull_points) self.assertEqual(convex_hull, convex_hull_truth) self.assertFalse(convex_hull.is_clockwise) self.assertTrue(convex_hull.is_counterclockwise) reversed_polygon = Polygon2D(*reversed(points)) self.assertFalse(reversed_polygon.is_clockwise) self.assertTrue(reversed_polygon.is_counterclockwise) convex_hull2 = reversed_polygon.convex_hull() self.assertEqual(convex_hull, convex_hull2) self.assertFalse(convex_hull2.is_clockwise) self.assertTrue(convex_hull2.is_counterclockwise) # Simplify simplified_polygon = convex_hull.simplify(threshold=.1) self.assertEqual(simplified_polygon, convex_hull_truth)
def test(self): x, y = 10, 20 p0 = Vector2D(x, y) p1 = Vector2D(x, -y) p2 = Vector2D(-x, -y) p3 = Vector2D(-x, y) points = [p0, p1, p2, p3] polygon = Polygon2D(*points) self.assertEqual(polygon.number_of_points, len(points)) self.assertListEqual(list(polygon.points), points) self.assertEqual(polygon.perimeter, 4 * (x + y)) self.assertEqual(polygon.area, 4 * x * y) origin = Vector2D(0, 0) self.assertEqual(polygon.point_barycenter, origin) self.assertEqual(polygon.barycenter, origin)
def to_python(cls, transforms, concat=True): def complete(values, size): return values + [0] * (size - len(values)) global_transform = AffineTransformation2D.Identity() py_transforms = [] for name, values in transforms: transform = None if name == 'matrix': array = [values[i] for i in (0, 2, 4, 1, 3, 5)] + [0, 0, 1] transform = AffineTransformation2D(array) elif name == 'translate': vector = Vector2D(complete(values, 2)) transform = AffineTransformation2D.Translation(vector) elif name == 'scale': transform = AffineTransformation2D.Scale(*values) elif name == 'rotate': angle, *vector = complete(values, 3) vector = Vector2D(vector) transform = AffineTransformation2D.RotationAt(vector, angle) elif name == 'skewX': angle = values[0] raise NotImplementedError elif name == 'skewY': angle = values[0] raise NotImplementedError else: raise NotImplementedError if concat: global_transform = transform * global_transform else: py_transforms.append(transform) if concat: return global_transform else: return py_transforms
def to_geometry(cls, commands): # cls._logger.info('Path:\n' + str(commands).replace('), ', '),\n ')) path = None for command, args in commands: command_lower = command.lower() absolute = command_lower != command # Upper case means absolute # if is_lower: # cls._logger.warning('incremental command') # raise NotImplementedError if path is None: if command_lower != 'm': raise NameError('Path must start with m') path = Path2D(args) # Vector2D() else: if command_lower == 'l': path.line_to(args, absolute=absolute) elif command == 'h': path.horizontal_to(*args, absolute=False) elif command == 'H': path.absolute_horizontal_to(*args) elif command_lower == 'v': path.vertical_to(*args, absolute=absolute) elif command == 'V': path.absolute_vertical_to(*args) elif command_lower == 'c': path.cubic_to(*cls.as_vector(args), absolute=absolute) elif command_lower == 's': path.stringed_quadratic_to(*cls.as_vector(args), absolute=absolute) elif command_lower == 'q': path.quadratic_to(*cls.as_vector(args), absolute=absolute) elif command_lower == 't': path.stringed_cubic_to(*cls.as_vector(args), absolute=absolute) elif command_lower == 'a': radius_x, radius_y, angle, large_arc, sweep, x, y = args point = Vector2D(x, y) path.arc_to(point, radius_x, radius_y, angle, bool(large_arc), bool(sweep), absolute=absolute) elif command_lower == 'z': path.close() return path
def _make_absolute_ccw_path(self, start_point, radius): path = Path2D(start_point) for i, vector in enumerate(( (10, 0), (15, 10), (5, 15), (-5, 10), )): path.line_to(path.p0 + Vector2D(vector), absolute=True, radius=(radius if i else None)) path.close(radius=radius, close_radius=radius) return path
def _make_closed_path(self, start_point, radius): path = Path2D(start_point) path.line_to(Vector2D(10, 0)) path.line_to(Vector2D(0, 10), radius=radius) path.line_to(Vector2D(10, 0), radius=radius) path.line_to(Vector2D(0, 20), radius=radius) path.line_to(Vector2D(-10, 0), radius=radius) path.line_to(Vector2D(0, -10), radius=radius) path.close(radius=radius, close_radius=radius) return path
def item_at(self, position, radius_px=10): self._scene.update_rtree() self._scene.unselect_items() scene_position = Vector2D( self._viewport_area.viewport_to_scene(position)) radius = self.length_viewport_to_scene(radius_px) self._logger.info('Item selection at {} with radius {:1f} mm'.format( scene_position, radius)) items = self._scene.item_at(scene_position, radius) if items: distance, nearest_item = items[0] # print('nearest item at {} #{:6.2f} {} {}'.format(scene_position, len(items), distance, nearest_item.user_data)) nearest_item.selected = True # Fixme: z_value ??? for pair in items[1:]: distance, item = pair # print(' {:6.2f} {}'.format(distance, item.user_data)) self.update()
def _make_polygon(self): points = Vector2D.from_coordinates( (0, 0), (-1, 2), (0, 3), (2, 4), (1, 5), (4, 6), (3, 3), (5, 3), (7, 3), (7, 2), (6, 1), (3, 2), (2, 1), ) polygon = Polygon2D(*points) return polygon
def geometry(self): # Fixme: width is str width = float(self.width) height = float(self.height) # Fixme: which one ??? radius_x = self.rx radius_y = self.ry if radius_y == 0: radius = None else: radius = radius_y point = Vector2D(self.x, self.y) path = Path2D(point) path.horizontal_to(width) path.vertical_to(height, radius=radius) path.horizontal_to(-width, radius=radius) path.close(radius=radius, close_radius=radius) return path
#################################################################################################### # Read measurements file vit_file = VitFile(Path('patterns', 'measurements.vit')) measurements = vit_file.measurements #################################################################################################### pattern = Pattern(measurements, 'cm') #################################################################################################### pattern.SinglePoint(name='A0', x=0.79375, y=1.05833, label_offset=Vector2D(0.132292, 0.264583)) pattern.EndLinePoint(name='A1', base_point='A0', angle=0, length='waist_circ/2+10', label_offset=Vector2D(0.132292, 0.264583), line_style='dashDotLine', line_color='black') pattern.NormalPoint( name='A2', first_point='A0', second_point='A1', angle=180, length='leg_waist_side_to_floor+@longeur_ourlet_bas_pantalon', label_offset=Vector2D(0.132292, 0.264583), line_style='dashDotLine',
# order = degree +1 # for number_of_points in range(order, order + 5): # print(BSpline2D.uniform_knots(degree, number_of_points)) # points = ( # Vector2D(1, 1), # Vector2D(2, 2), # Vector2D(3, 2), # Vector2D(4, 1), # ) # degree = 3 # spline = BSpline2D(points, degree) # plot_spline(spline) points = ( Vector2D(0, 0), Vector2D(5, 5), Vector2D(10, -5), Vector2D(15, 5), Vector2D(20, -5), Vector2D(25, 5), ) degree = 3 spline = BSpline2D(points, degree) # spline2 = spline # spline2 = spline2.insert_knot(1.5) # spline2 = spline2.insert_knot(2.5) spline2 = spline.to_bezier_form() bezier_curves = spline.to_bezier()
def eval_internal(self): self._vector = Vector2D(self._x.value, self._y.value) self._post_eval_internal()
def to_operation(self, sketch): kwargs = self.to_dict(exclude=('mx', 'my')) # id' kwargs['label_offset'] = Vector2D(self.mx, self.my) return self.call_operation_function(sketch, kwargs)
def eval_internal(self): self._vector = self._base_point._vector + Vector2D.from_angle( self._angle.value) * self._length.value self._post_eval_internal()
def eval_internal(self): self._vector = Vector2D(self._first_point.vector.x, self._second_point.vector.y) self._post_eval_internal()