-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.go
135 lines (108 loc) · 3.34 KB
/
line.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (C) 2021 David Vogel
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"encoding/json"
"math"
"time"
"github.com/vugu/vgrouter"
)
type Line struct {
vgrouter.NavigatorRef `json:"-"`
site *Site
key string
CreatedAt time.Time
P1, P2 string
// Fit line along a given direction vector.
DirectionEnabled bool
DirectionVector Coordinate
DirectionAccuracy Angle
}
func (s *Site) NewLine() *Line {
l := new(Line)
l.initData()
l.initReferences(s, s.shortIDGen.MustGenerate())
return l
}
// initData initializes the object with default values and other stuff.
func (l *Line) initData() {
l.CreatedAt = time.Now()
l.DirectionVector = Coordinate{0, 0, 1}
l.DirectionAccuracy = Angle(1 * math.Pi / 180)
}
// initReferences updates references from and to this object and its key.
// This is only used internally to update references for copies or marshalled objects.
// This can't be used on its own to transfer an object from one parent to another.
func (l *Line) initReferences(newParent *Site, newKey string) {
l.site, l.key = newParent, newKey
l.site.Lines[l.Key()] = l
}
func (l *Line) Key() string {
return l.key
}
func (l *Line) Delete() {
delete(l.site.Lines, l.Key())
}
// Copy returns a copy of the given object.
// Expensive data like images will not be copied, but referenced.
func (l *Line) Copy(newParent *Site, newKey string) *Line {
copy := new(Line)
copy.initData()
copy.initReferences(newParent, newKey)
copy.CreatedAt = l.CreatedAt
copy.P1 = l.P1
copy.P2 = l.P2
copy.DirectionEnabled = l.DirectionEnabled
copy.DirectionVector = l.DirectionVector
copy.DirectionAccuracy = l.DirectionAccuracy
return copy
}
func (l *Line) UnmarshalJSON(data []byte) error {
l.initData()
// Unmarshal structure normally. Cast it into a different type to prevent recursion with json.Unmarshal.
type tempType *Line
if err := json.Unmarshal(data, tempType(l)); err != nil {
return err
}
// Update parent references and keys.
return nil
}
// GetTweakablesAndResiduals returns a list of tweakable variables and residuals.
func (l *Line) GetTweakablesAndResiduals() ([]Tweakable, []Residualer) {
return nil, []Residualer{l}
}
// ResidualSqr returns the sum of squared residuals. (Each residual is divided by the accuracy of the measurement device).
func (l *Line) ResidualSqr() float64 {
site := l.site
p1, ok := site.Points[l.P1]
if !ok {
return 0
}
p2, ok := site.Points[l.P2]
if !ok {
return 0
}
ssr := 0.0
if l.DirectionEnabled {
v1, v2 := l.DirectionVector.Vec3(), p2.Position.Vec3().Sub(p1.Position.Vec3())
r := math.Acos(v1.Dot(v2)/v1.Len()/v2.Len()) / float64(l.DirectionAccuracy)
sr := r * r
if math.IsNaN(sr) {
sr = 1000000
}
ssr += math.Min(sr, 1000000)
}
return ssr
}