comparison pkg/octree/vertex.go @ 2466:a1e751c08c56 octree-diff

Calculate difference on single core.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 25 Feb 2019 18:21:33 +0100
parents f4dcbe8941a1
children 63475c8e710e
comparison
equal deleted inserted replaced
2465:86c7a023400e 2466:a1e751c08c56
58 Plane2D struct { 58 Plane2D struct {
59 A float64 59 A float64
60 B float64 60 B float64
61 C float64 61 C float64
62 } 62 }
63
64 Plane3D struct {
65 A float64
66 B float64
67 C float64
68 D float64
69 }
63 ) 70 )
71
72 func (t *Triangle) Plane3D() Plane3D {
73
74 v0 := t[1].Sub(t[0])
75 v1 := t[2].Sub(t[0])
76 n := v0.Cross(v1).Normalize()
77
78 // x*nx+ y*ny+ z*nz + d = 0
79 // d = - (x*nx+ y*ny + z*nz)
80 d := -t[0].Dot(n)
81 return Plane3D{
82 A: n.X,
83 B: n.Y,
84 C: n.Z,
85 D: d,
86 }
87 }
88
89 func (p Plane3D) Z(x, y float64) float64 {
90 // p.A*x + p.B*y + p.C*z + p.D = 0
91 return -(p.A*x + p.B*y + p.D) / p.C
92 }
93
94 func (v Vertex) Normalize() Vertex {
95 s := 1 / v.Length()
96 return Vertex{
97 X: s * v.X,
98 Y: s * v.Y,
99 Z: s * v.Z,
100 }
101 }
102
103 func (v Vertex) Dot(w Vertex) float64 {
104 return v.X*w.X + v.Y*w.Y + v.Z*w.Z
105 }
106
107 func (v Vertex) Length() float64 {
108 return math.Sqrt(v.Dot(v))
109 }
64 110
65 // Minimize adjust this vertex v to hold the minimum 111 // Minimize adjust this vertex v to hold the minimum
66 // values component-wise of v and w. 112 // values component-wise of v and w.
67 func (v *Vertex) Minimize(w Vertex) { 113 func (v *Vertex) Minimize(w Vertex) {
68 if w.X < v.X { 114 if w.X < v.X {
162 return -1 208 return -1
163 case z > h: 209 case z > h:
164 return +1 210 return +1
165 } 211 }
166 return 0 212 return 0
213 }
214
215 func (v Vertex) Dot2(w Vertex) float64 {
216 return v.X*w.X + v.Y*w.Y
217 }
218
219 func (t *Triangle) Contains(x, y float64) bool {
220 v0 := t[2].Sub(t[0])
221 v1 := t[1].Sub(t[0])
222 v2 := Vertex{X: x, Y: y}.Sub(t[0])
223
224 dot00 := v0.Dot2(v0)
225 dot01 := v0.Dot2(v1)
226 dot02 := v0.Dot2(v2)
227 dot11 := v1.Dot2(v1)
228 dot12 := v1.Dot2(v2)
229
230 // Compute barycentric coordinates
231 invDenom := 1 / (dot00*dot11 - dot01*dot01)
232 u := (dot11*dot02 - dot01*dot12) * invDenom
233 v := (dot00*dot12 - dot01*dot02) * invDenom
234
235 // Check if point is in triangle
236 return u >= 0 && v >= 0 && u+v < 1
167 } 237 }
168 238
169 // IntersectHorizontal calculates the line string that 239 // IntersectHorizontal calculates the line string that
170 // results when cutting a triangle a a certain height. 240 // results when cutting a triangle a a certain height.
171 // Can be empty (on intersection), 241 // Can be empty (on intersection),
514 584
515 // Intersects checks if two Box2Ds intersect. 585 // Intersects checks if two Box2Ds intersect.
516 func (a Box2D) Intersects(b Box2D) bool { 586 func (a Box2D) Intersects(b Box2D) bool {
517 return !(a.X2 < a.X1 || a.X2 < b.X1 || 587 return !(a.X2 < a.X1 || a.X2 < b.X1 ||
518 a.Y2 < a.Y1 || a.Y2 < b.Y1) 588 a.Y2 < a.Y1 || a.Y2 < b.Y1)
589 }
590
591 func (a Box2D) Contains(x, y float64) bool {
592 return a.X1 <= x && x <= a.X2 &&
593 a.Y1 <= y && y <= a.Y2
519 } 594 }
520 595
521 // Xi returns the i-th x component. 596 // Xi returns the i-th x component.
522 func (a Box2D) Xi(i int) float64 { 597 func (a Box2D) Xi(i int) float64 {
523 if i == 0 { 598 if i == 0 {