| 1 |
// Copyright ©2011-2012 The bíogo Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package filter |
| 6 |
|
| 7 |
// Type to store a successfully filtered w × e Parallelogram |
| 8 |
type Trapezoid struct { |
| 9 |
Top, Bottom int // B (query) coords of top and bottom of trapzoidal zone |
| 10 |
Left, Right int // Left and right diagonals of trapzoidal zone |
| 11 |
} |
| 12 |
|
| 13 |
type trapezoid struct { |
| 14 |
next *trapezoid // Organized in a list linked on this field |
| 15 |
Trapezoid |
| 16 |
} |
| 17 |
|
| 18 |
// prependFrontTo prepends a copy of front of list onto the front of the list |
| 19 |
// taking the allocation from the receiver which acts as a pool. It returns the |
| 20 |
// next element of the pool. |
| 21 |
func (tr *trapezoid) prependFrontTo(list *trapezoid) *trapezoid { |
| 22 |
next := tr.next |
| 23 |
*tr = *list |
| 24 |
list.join(tr) |
| 25 |
return next |
| 26 |
} |
| 27 |
|
| 28 |
// join joins list to the receiver, returning the receiver. |
| 29 |
func (tr *trapezoid) join(list *trapezoid) *trapezoid { |
| 30 |
tr.next = list |
| 31 |
return tr |
| 32 |
} |
| 33 |
|
| 34 |
// Return the receiver and the subsequent trapezoid in the list. |
| 35 |
func (tr *trapezoid) decapitate() (*trapezoid, *trapezoid) { |
| 36 |
return tr, tr.next |
| 37 |
} |
| 38 |
|
| 39 |
// clip is the trapezoid trimming method used during merge. |
| 40 |
func (tr *trapezoid) clip(lagPosition, lagClip int) { |
| 41 |
if bottom := lagClip + tr.Left; tr.Bottom < bottom { |
| 42 |
tr.Bottom = bottom |
| 43 |
} |
| 44 |
if top := lagPosition + tr.Right; tr.Top > top { |
| 45 |
tr.Top = top |
| 46 |
} |
| 47 |
midPosition := (tr.Bottom + tr.Top) / 2 |
| 48 |
if left := midPosition - lagPosition; tr.Left < left { |
| 49 |
tr.Left = left |
| 50 |
} |
| 51 |
if right := midPosition - lagClip; tr.Right > right { |
| 52 |
tr.Right = right |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
// Trapezoids implements the sort.Sort interface. |
| 57 |
type Trapezoids []Trapezoid |
| 58 |
|
| 59 |
// Return the sum of all Trapezoids in the slice. |
| 60 |
func (tr Trapezoids) Sum() (a, b int) { |
| 61 |
for _, t := range tr { |
| 62 |
la, lb := t.Top-t.Bottom, t.Right-t.Left |
| 63 |
a, b = a+la, b+lb |
| 64 |
} |
| 65 |
return |
| 66 |
} |
| 67 |
|
| 68 |
// Required for sort.Interface |
| 69 |
func (tr Trapezoids) Len() int { |
| 70 |
return len(tr) |
| 71 |
} |
| 72 |
|
| 73 |
// Required for sort.Interface |
| 74 |
func (tr Trapezoids) Less(i, j int) bool { |
| 75 |
return tr[i].Bottom < tr[j].Bottom |
| 76 |
} |
| 77 |
|
| 78 |
// Required for sort.Interface |
| 79 |
func (tr Trapezoids) Swap(i, j int) { |
| 80 |
tr[i], tr[j] = tr[j], tr[i] |
| 81 |
} |