Skip to content

Commit

Permalink
Fix issues with the expanding box search
Browse files Browse the repository at this point in the history
Primarily fixes the legs length calculation for expanding box searches.
Also, allow controlling iterations of sector searches.
  • Loading branch information
sparlane committed Aug 19, 2023
1 parent 7bb824e commit 334fcaf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 21 deletions.
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as ReactDOM from 'react-dom/client'
import { SearchDisplay } from './react'
import { SectorSearch, ExpandingBoxSearch, CreepingLineAheadSearch } from './sar-search-patterns'

const search = new SectorSearch(200, 2, 45)
const search = new SectorSearch(200, 2, 2, 45)
const ebsearch = new ExpandingBoxSearch(200, 2, 45)
const clsearch = new CreepingLineAheadSearch(200, 1000, 5, 45)

Expand Down
6 changes: 4 additions & 2 deletions react.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ class SearchDisplay extends React.Component {
constructor (props) {
super(props)
this.state = {
search: this.props.search
search: this.props.search,
searchLeg: this.props.leg !== undefined ? this.props.leg : this.props.search.currentLeg
}
this.canvasRef = React.createRef()
this.minX = 0
Expand Down Expand Up @@ -87,7 +88,8 @@ class SearchDisplay extends React.Component {
}
}
SearchDisplay.propTypes = {
search: PropTypes.object.isRequired
search: PropTypes.object.isRequired,
leg: PropTypes.number
}

export { SearchDisplay }
49 changes: 31 additions & 18 deletions sar-search-patterns.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
class SearchLeg {
constructor (from, to, bearing) {
constructor (from, to, distance, bearing) {
this.from = from
this.to = to
this.bearing = bearing
this.distance = distance
}
}

Expand Down Expand Up @@ -39,7 +40,7 @@ function move (from, direction, distance) {
}

class SectorSearch extends SearchPattern {
constructor (sweepWidth, multiplier, startingDirection) {
constructor (sweepWidth, multiplier, iterations, startingDirection) {
super(sweepWidth)
this.startingDirection = startingDirection
if (this.startingDirection === undefined) {
Expand All @@ -52,23 +53,28 @@ class SectorSearch extends SearchPattern {
if (this.multiplier > 3) {
this.multiplier = 3
}
this.iterations = iterations
if (!(this.iterations === 1 || this.iterations === 2 || this.iterations === 3)) {
this.iterations = 1
}
this.generateSearchLegs()
}

generateSearchLegs () {
this.searchLegs = []
let currentBearing = this.startingDirection
let lastPoint = { x: 0, y: 0 }
for (let i = 1; i < (9 * 1) + 1; i++) {
const legLength = this.sweepWidth * this.multiplier
for (let i = 1; i < (9 * this.iterations) + 1; i++) {
const from = lastPoint
let to = { x: 0, y: 0 }
if ((i % 3) !== 0) {
to = move(from, currentBearing, this.sweepWidth * this.multiplier)
to = move(from, currentBearing, legLength)
}
this.searchLegs.push(new SearchLeg(from, to, currentBearing))
this.searchLegs.push(new SearchLeg(from, to, legLength, currentBearing))
if ((i % 3) === 0) {
if ((i % 9) === 0) {
currentBearing = (currentBearing + 60) % 360
currentBearing = (currentBearing + 30) % 360
}
} else {
currentBearing = (currentBearing + 120) % 360
Expand All @@ -91,9 +97,10 @@ class ExpandingBoxSearch extends SearchPattern {
let direction = this.startingDirection
let from = { x: 0, y: 0 }
for (let i = 0; i < (this.iterations * 4); i++) {
const to = move(from, direction, this.sweepWidth * (1 + (i / 2)))
this.searchLegs.push(new SearchLeg(from, to, direction))
direction = (direction + 90 % 360)
const legLength = this.sweepWidth * (1 + Math.round((i - 1) / 2))
const to = move(from, direction, legLength)
this.searchLegs.push(new SearchLeg(from, to, this.sweepWidth * (1 + Math.round((i - 1) / 2)), direction))
direction = (direction + 90) % 360
from = to
}
}
Expand All @@ -117,22 +124,28 @@ class CreepingLineAheadSearch extends SearchPattern {
let direction = this.progressDirection
let from
let to
let distance
const leg = Math.round(i / 2)
if ((i % 4) === 0) {
from = move(baseNear, this.progressDirection, this.sweepWidth * Math.round(i / 2))
to = move(baseNear, this.progressDirection, this.sweepWidth * (Math.round(i / 2) + 1))
from = move(baseNear, this.progressDirection, this.sweepWidth * leg)
to = move(baseNear, this.progressDirection, this.sweepWidth * (leg + 1))
distance = this.sweepWidth
} else if ((i % 4) === 1) {
from = move(baseNear, this.progressDirection, this.sweepWidth * Math.round(i / 2))
to = move(baseFar, this.progressDirection, this.sweepWidth * Math.round(i / 2))
from = move(baseNear, this.progressDirection, this.sweepWidth * leg)
to = move(baseFar, this.progressDirection, this.sweepWidth * leg)
direction = (direction + 90) % 360
distance = this.legLength
} else if ((i % 4) === 2) {
from = move(baseFar, this.progressDirection, this.sweepWidth * Math.round(i / 2))
to = move(baseFar, this.progressDirection, this.sweepWidth * (Math.round(i / 2) + 1))
from = move(baseFar, this.progressDirection, this.sweepWidth * leg)
to = move(baseFar, this.progressDirection, this.sweepWidth * (leg + 1))
distance = this.sweepWidth
} else {
from = move(baseFar, this.progressDirection, this.sweepWidth * Math.round(i / 2))
to = move(baseNear, this.progressDirection, this.sweepWidth * (Math.round(i / 2)))
from = move(baseFar, this.progressDirection, this.sweepWidth * leg)
to = move(baseNear, this.progressDirection, this.sweepWidth * leg)
direction = (direction - 90) % 360
distance = this.legLength
}
this.searchLegs.push(new SearchLeg(from, to, direction))
this.searchLegs.push(new SearchLeg(from, to, distance, direction))
}
}
}
Expand Down

0 comments on commit 334fcaf

Please sign in to comment.