-
Notifications
You must be signed in to change notification settings - Fork 1
/
boundingbox.go
151 lines (131 loc) · 4.46 KB
/
boundingbox.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// liquidhandling/lhinterfaces.go: Part of the Antha language
// Copyright (C) 2014 the Antha authors. All rights reserved.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// For more information relating to the software or licensing issues please
// contact license@antha-lang.Org or write to the Antha team c/o
// Synthace Ltd. The London Bioscience Innovation Centre
// 2 Royal College St, London NW1 0NH UK
// defines types for dealing with liquid handling requests
package wtype
import (
"fmt"
"math"
)
//BBox is a simple LHObject representing a bounding box,
//useful for checking if there's stuff in the way
type BBox struct {
Position Coordinates3D
Size Coordinates3D
}
//Equals defines an equivalence relation over bounding boxes
//returns true iff position and size are both equal
func (bb BBox) Equals(bb2 BBox) bool {
return bb.Position.Equals(bb2.Position) && bb.Size.Equals(bb2.Size)
}
func NewBBox(pos, Size Coordinates3D) *BBox {
if Size.X < 0. {
pos.X = pos.X + Size.X
Size.X = -Size.X
}
if Size.Y < 0. {
pos.Y = pos.Y + Size.Y
Size.Y = -Size.Y
}
if Size.Z < 0. {
pos.Z = pos.Z + Size.Z
Size.Z = -Size.Z
}
r := BBox{pos, Size}
return &r
}
func NewBBox6f(pos_x, pos_y, pos_z, Size_x, Size_y, Size_z float64) *BBox {
return NewBBox(Coordinates3D{pos_x, pos_y, pos_z},
Coordinates3D{Size_x, Size_y, Size_z})
}
func NewXBox4f(pos_y, pos_z, Size_y, Size_z float64) *BBox {
return NewBBox(Coordinates3D{-math.MaxFloat64 / 2., pos_y, pos_z},
Coordinates3D{math.MaxFloat64, Size_y, Size_z})
}
func NewYBox4f(pos_x, pos_z, Size_x, Size_z float64) *BBox {
return NewBBox(Coordinates3D{pos_x, -math.MaxFloat64 / 2., pos_z},
Coordinates3D{Size_x, math.MaxFloat64, Size_z})
}
func NewZBox4f(pos_x, pos_y, Size_x, Size_y float64) *BBox {
return NewBBox(Coordinates3D{pos_x, pos_y, -math.MaxFloat64 / 2.},
Coordinates3D{Size_x, Size_y, math.MaxFloat64})
}
func (self BBox) GetPosition() Coordinates3D {
return self.Position
}
func (self BBox) ZMax() float64 {
return self.Position.Z + self.Size.Z
}
func (self BBox) GetSize() Coordinates3D {
return self.Size
}
//String gets a string representation of the box
func (self BBox) String() string {
return fmt.Sprintf("[%v+%v]", self.Position, self.Size)
}
//Dup duplicate the bounding box
func (self *BBox) Dup() *BBox {
if self == nil {
return nil
}
return &BBox{self.Position, self.Size}
}
func (self *BBox) SetPosition(c Coordinates3D) {
if self == nil {
return
}
self.Position = c
}
func (self *BBox) SetSize(c Coordinates3D) {
if self == nil {
return
}
self.Size = c
}
func (self BBox) Contains(rhs Coordinates3D) bool {
return (rhs.X >= self.Position.X && rhs.X < self.Position.X+self.Size.X &&
rhs.Y >= self.Position.Y && rhs.Y < self.Position.Y+self.Size.Y &&
rhs.Z >= self.Position.Z && rhs.Z < self.Position.Z+self.Size.Z)
}
func (self BBox) ContainsBox(rhs BBox) bool {
return self.Contains(rhs.GetPosition()) && self.Contains(rhs.GetPosition().Add(rhs.GetSize()))
}
//IntersectsBox checks for bounding box intersection
func (self BBox) IntersectsBox(rhs BBox) bool {
//test a single dimension.
//(a,b) are the start and end of the first Position
//(c,d) are the start and end of the second pos
// assert(a > b and d > c)
f := func(a, b, c, d float64) bool {
return !(c >= b || d <= a)
}
s := self.Position.Add(self.Size)
r := rhs.GetPosition().Add(rhs.GetSize())
return (f(self.Position.X, s.X, rhs.GetPosition().X, r.X) &&
f(self.Position.Y, s.Y, rhs.GetPosition().Y, r.Y) &&
f(self.Position.Z, s.Z, rhs.GetPosition().Z, r.Z))
}
//IntersectsPoint
func (self BBox) IntersectsPoint(rhs Coordinates3D) bool {
return (rhs.X >= self.Position.X && rhs.X < self.Position.X+self.Size.X &&
rhs.Y >= self.Position.Y && rhs.Y < self.Position.Y+self.Size.Y &&
rhs.Z >= self.Position.Z && rhs.Z < self.Position.Z+self.Size.Z)
}