Browse Source

Add polar point

pull/1/head
Wouter Horlings 1 year ago
parent
commit
a75c6db7ee
3 changed files with 73 additions and 27 deletions
  1. +50
    -0
      pkg/pattern/template/point.go
  2. +20
    -21
      spec/pattern.yaml
  3. +3
    -6
      templates/classic_trouser_block.yaml

+ 50
- 0
pkg/pattern/template/point.go View File

@@ -32,6 +32,7 @@ type Point struct {
Between *BetweenPoint `yaml:"between"` Between *BetweenPoint `yaml:"between"`
Extend *ExtendPoint `yaml:"extend"` Extend *ExtendPoint `yaml:"extend"`
Hide bool `yaml:"hide"` Hide bool `yaml:"hide"`
Polar *PolarPoint `yaml:"polar"`
} }


var ErrInvalidPointID = errors.New("type cannot be converted to a PointID") var ErrInvalidPointID = errors.New("type cannot be converted to a PointID")
@@ -141,6 +142,11 @@ func (p Points) addSingleToPattern(id point.ID, pat *pattern.Pattern, depth int)
var newPoint point.Point var newPoint point.Point


switch { switch {
case templatePoint.RelativeTo != nil && templatePoint.Polar != nil:
newPoint, err = p.createPolar(id, pat, depth)
if err != nil {
return err
}
case templatePoint.RelativeTo != nil: case templatePoint.RelativeTo != nil:
newPoint, err = p.createRelative(id, pat, depth) newPoint, err = p.createRelative(id, pat, depth)
if err != nil { if err != nil {
@@ -282,8 +288,52 @@ func (p Points) createExtend(id point.ID, pat *pattern.Pattern, depth int) (poin
return point.NewExtendPoint(fromPoint, toPoint, offset, id), nil return point.NewExtendPoint(fromPoint, toPoint, offset, id), nil
} }


func (p Points) createPolar(id point.ID, pat *pattern.Pattern, depth int) (point.Point, error) {
templatePoint, ok := p[id]
if !ok {
return nil, ErrPointNotFound
}

relativePointID := *templatePoint.RelativeTo
if relativePointID == id || depth > maxRecursionDepth {
return nil, ErrRelativePointRecursion
}

relativePoint, err := p.getOrCreate(relativePointID, pat, depth)
if err != nil {
return nil, err
}

x, y, err := templatePoint.Polar.evaluate(pat.Parameters(), p.Functions(pat))
if err != nil {
return nil, err
}

return point.NewRelativePoint(relativePoint).
WithXOffset(x).WithYOffset(y).MarkWith(id), nil
}

type ExtendPoint struct { type ExtendPoint struct {
From point.ID `yaml:"from"` From point.ID `yaml:"from"`
To point.ID `yaml:"to"` To point.ID `yaml:"to"`
Offset *Value `yaml:"offset"` Offset *Value `yaml:"offset"`
} }

type PolarPoint struct {
Length *Value `yaml:"length"`
Rotation *Value `yaml:"rotation"`
}

func (p PolarPoint) evaluate(params govaluate.MapParameters, funcs map[string]govaluate.ExpressionFunction) (x, y float64, err error) {
rotation, err := p.Rotation.Evaluate(params, funcs)
if err != nil {
return 0, 0, err
}

length, err := p.Length.Evaluate(params, funcs)
if err != nil {
return 0, 0, err
}

return math.Sin(rotation) * -length, math.Cos(rotation) * -length, nil
}

+ 20
- 21
spec/pattern.yaml View File

@@ -44,34 +44,20 @@ components:
$ref: '#/components/schemas/position' $ref: '#/components/schemas/position'
relativeTo: relativeTo:
$ref: '#/components/schemas/pointID' $ref: '#/components/schemas/pointID'
polar:
$ref: '#/components/schemas/polar'
description: description:
type: string type: string
between: between:
type: object
properties:
from:
$ref: '#/components/schemas/pointID'
to:
$ref: '#/components/schemas/pointID'
offset:
$ref: '#/components/schemas/expression'
$ref: '#/components/schemas/between'
hide: hide:
type: bool type: bool
extend: extend:
type: object
properties:
from:
$ref: '#/components/schemas/pointID'
to:
$ref: '#/components/schemas/pointID'
offset:
$ref: '#/components/schemas/expression'

$ref: '#/components/schemas/between'
points: points:
type: object type: object
additionalProperties: additionalProperties:
$ref: '#/components/schemas/point' $ref: '#/components/schemas/point'

position: position:
type: object type: object
properties: properties:
@@ -81,7 +67,22 @@ components:
$ref: '#/components/schemas/expression' $ref: '#/components/schemas/expression'
rotation: rotation:
$ref: '#/components/schemas/expression' $ref: '#/components/schemas/expression'

polar:
type: object
properties:
length:
$ref: '#/components/schemas/expression'
rotation:
$ref: '#/components/schemas/expression'
between:
type: object
properties:
from:
$ref: '#/components/schemas/pointID'
to:
$ref: '#/components/schemas/pointID'
offset:
$ref: '#/components/schemas/expression'
line: line:
type: object type: object
properties: properties:
@@ -95,7 +96,6 @@ components:
start: start:
$ref: '#/components/schemas/pointID' $ref: '#/components/schemas/pointID'
end: end:
type:
$ref: '#/components/schemas/pointID' $ref: '#/components/schemas/pointID'
pointID: pointID:
oneOf: oneOf:
@@ -103,7 +103,6 @@ components:
- type: string - type: string
expression: expression:
oneOf: oneOf:
- type: integer
- type: number - type: number
- type: string - type: string




+ 3
- 6
templates/classic_trouser_block.yaml View File

@@ -164,14 +164,11 @@ panels:
from: 13 from: 13
to: 15 to: 15
offset: 1.5 offset: 1.5
r5:
position:
h5:
polar:
rotation: 3*pi/4 rotation: 3*pi/4
length: 30
relativeTo: 5 relativeTo: 5
h5:
position:
x: 30
relativeTo: r5
hide: true hide: true
1extend: 1extend:
position: position:


Loading…
Cancel
Save