Skip to content

Commit

Permalink
chore: remove temporary cg namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
pvaiko committed Jun 29, 2024
1 parent d194f13 commit 752203f
Show file tree
Hide file tree
Showing 45 changed files with 589 additions and 592 deletions.
4 changes: 2 additions & 2 deletions scripts/Logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ end
--- Debug print, will either just call print when running standalone
-- or use the CP debug channel when running in the game.
function Logger:log(...)
if cg.isRunningInGame() then
if CourseGenerator.isRunningInGame() then
CpUtil.debugVehicle(CpDebug.DBG_COURSES, g_currentMission.controlledVehicle, ...)
else
local message = self:_getCurrentTimeStr() .. ' ' .. string.format(...)
Expand All @@ -80,7 +80,7 @@ function Logger:log(...)
end

function Logger:_getCurrentTimeStr()
if cg.isRunningInGame() then
if CourseGenerator.isRunningInGame() then
-- the game logs hour minute, just add the seconds
return getDate(':%S')
else
Expand Down
4 changes: 2 additions & 2 deletions scripts/courseGenerator/AnalyticHelper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ function AnalyticHelper.getDubinsSolutionAsVertices(from, to, r, enabledPathType
return path, solution:getLength(r), type
end

---@class cg.AnalyticHelper
cg.AnalyticHelper = AnalyticHelper
---@class CourseGenerator.AnalyticHelper
CourseGenerator.AnalyticHelper = AnalyticHelper
22 changes: 11 additions & 11 deletions scripts/courseGenerator/Block.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

local Block = CpObject()

---@param rowPattern cg.RowPattern pattern to use for the up/down rows
---@param rowPattern CourseGenerator.RowPattern pattern to use for the up/down rows
function Block:init(rowPattern, id)
self.id = id or 0
self.logger = Logger('Block ' .. self.id)
Expand All @@ -28,7 +28,7 @@ function Block:init(rowPattern, id)
-- rows in the order they will be worked on, every second row in this sequence is reversed
-- so we remain on the same side of the block when switching to the next row
self.rowsInWorkSequence = {}
self.rowPattern = rowPattern or cg.RowPatternAlternating()
self.rowPattern = rowPattern or CourseGenerator.RowPatternAlternating()
end

function Block:getId()
Expand Down Expand Up @@ -58,7 +58,7 @@ function Block:getNumberOfRows()
end

--- Does this row overlap with this block, that is, with the last row of this block.
---@param row cg.Row
---@param row CourseGenerator.Row
---@return boolean true if row overlaps this block or if the block has no rows
function Block:overlaps(row)
return #self.rows == 0 or self.rows[#self.rows]:overlaps(row)
Expand All @@ -74,23 +74,23 @@ function Block:getPolygon()
return self.polygon
end

---@return cg.Row[] rows in the order they should be worked on. Every other row is reversed, so it starts at the
---@return CourseGenerator.Row[] rows in the order they should be worked on. Every other row is reversed, so it starts at the
--- end where the previous one ends.
function Block:getRows()
return self.rowsInWorkSequence
end

---@return cg.Row first row of the block in the work sequence
---@return CourseGenerator.Row first row of the block in the work sequence
function Block:getFirstRow()
return self.rowsInWorkSequence[1]
end

---@return cg.Row last row of the block in the work sequence
---@return CourseGenerator.Row last row of the block in the work sequence
function Block:getLastRow()
return self.rowsInWorkSequence[#self.rowsInWorkSequence]
end

---@return cg.RowPattern.Entry[]
---@return CourseGenerator.RowPattern.Entry[]
function Block:getPossibleEntries()
if not self.possibleEntries then
-- cache this as the genetic algorithm needs it frequently, also, this way
Expand All @@ -102,7 +102,7 @@ end

--- Finalize this block, set the entry we will be using, rearrange rows accordingly, set all row attributes and create
--- a sequence in which the rows must be worked on
---@param entry cg.RowPattern.Entry the entry to be used for this block
---@param entry CourseGenerator.RowPattern.Entry the entry to be used for this block
---@return Vertex the last vertex of the last row, the exit point from this block (to be used to find the entry
--- to the next one.
function Block:finalize(entry)
Expand All @@ -122,7 +122,7 @@ function Block:finalize(entry)
self.logger:debug('row %d is now at position %d, left/right worked %s/%s, headland %s/%s',
row:getOriginalSequenceNumber(), i, rowOnLeftWorked, rowOnRightWorked, leftSideBlockBoundary, rightSideBlockBoundary)
-- need vertices close enough so the smoothing in goAround() only starts close to the island
row:splitEdges(cg.cRowWaypointDistance)
row:splitEdges(CourseGenerator.cRowWaypointDistance)
row:adjustLength()
row:setRowNumber(i)
row:setAllAttributes()
Expand Down Expand Up @@ -211,5 +211,5 @@ function Block:_getAdjacentRowInfo(originalIx, reversed, rows, previousRowsInWor
return rowOnLeftWorked, rowOnRightWorked, rowLeftIx == 0 or rowLeftIx > #rows, rowRightIx == 0 or rowRightIx > #rows
end

---@class cg.Block
cg.Block = Block
---@class CourseGenerator.Block
CourseGenerator.Block = Block
4 changes: 2 additions & 2 deletions scripts/courseGenerator/CacheMap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,5 @@ function CacheMap:_getMap(...)
return map
end

---@class cg.CacheMap
cg.CacheMap = CacheMap
---@class CourseGenerator.CacheMap
CourseGenerator.CacheMap = CacheMap
54 changes: 27 additions & 27 deletions scripts/courseGenerator/Center.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,24 @@
---@class Center
local Center = CpObject()

---@param context cg.FieldworkContext
---@param context CourseGenerator.FieldworkContext
---@param boundary Polygon the field boundary
---@param headland cg.Headland|nil the innermost headland if exists
---@param headland CourseGenerator.Headland|nil the innermost headland if exists
---@param startLocation Vector location of the vehicle before it starts working on the center.
---@param bigIslands cg.Island[] islands too big to circle
---@param bigIslands CourseGenerator.Island[] islands too big to circle
function Center:init(context, boundary, headland, startLocation, bigIslands)
self.logger = Logger('Center', Logger.level.debug)
self.context = context
if headland == nil then
-- if there are no headlands, we generate a virtual one, from the field boundary
-- so using this later is equivalent of having an actual headland
local virtualHeadland = cg.FieldworkCourseHelper.createVirtualHeadland(boundary, self.context.headlandClockwise,
local virtualHeadland = CourseGenerator.FieldworkCourseHelper.createVirtualHeadland(boundary, self.context.headlandClockwise,
self.context.workingWidth)
if self.context.sharpenCorners then
virtualHeadland:sharpenCorners(self.context.turningRadius)
end
self.headlandPolygon = virtualHeadland:getPolygon()
cg.addDebugPolyline(self.headlandPolygon, { 1, 1, 0, 0.5 })
CourseGenerator.addDebugPolyline(self.headlandPolygon, { 1, 1, 0, 0.5 })
self.headland = virtualHeadland
self.mayOverlapHeadland = false
else
Expand Down Expand Up @@ -57,7 +57,7 @@ function Center:getPath()
return self.path
end

---@return cg.Block[]
---@return CourseGenerator.Block[]
function Center:getBlocks()
return self.blocks
end
Expand All @@ -74,7 +74,7 @@ function Center:getConnectingPaths()
end

--- Return the set of rows covering the entire field center, uncut. For debug purposes only.
---@return cg.Row[]
---@return CourseGenerator.Row[]
function Center:getDebugRows()
return self.rows
end
Expand All @@ -84,7 +84,7 @@ function Center:generate()
-- first, we split the field into blocks. Simple convex fields have just one block only,
-- but odd shaped, concave fields or fields with island may have more blocks
if self.useBaselineEdge then
self.rows = cg.CurvedPathHelper.generateCurvedUpDownRows(self.headlandPolygon, self.context.baselineEdge,
self.rows = CourseGenerator.CurvedPathHelper.generateCurvedUpDownRows(self.headlandPolygon, self.context.baselineEdge,
self.context.workingWidth, self.context.turningRadius, nil)
else
local angle = self.context.autoRowAngle and self:_findBestRowAngle() or self.context.rowAngle
Expand All @@ -103,7 +103,7 @@ function Center:generate()
-- headland from one block to the other

-- clear cache for the block sequencer
self.closestVertexCache, self.pathCache = cg.CacheMap(2), cg.CacheMap(3)
self.closestVertexCache, self.pathCache = CourseGenerator.CacheMap(2), CourseGenerator.CacheMap(3)

-- first run of the genetic search will restrict connecting path between blocks to the same
-- headland, that is, the entry to the next block must be adjacent to the headland where the
Expand All @@ -114,8 +114,8 @@ function Center:generate()
-- or may not work, but most likely won't be a pretty one.
local strict = true

---@param sequencedBlocks cg.Block[] an array of blocks in the sequence they should be worked on
---@param entries cg.RowPattern.Entry[] entries for each block are in the entries table, indexed by
---@param sequencedBlocks CourseGenerator.Block[] an array of blocks in the sequence they should be worked on
---@param entries CourseGenerator.RowPattern.Entry[] entries for each block are in the entries table, indexed by
--- the block itself
---@return number, Polyline[] total distance to travel on the headland (from the start location to the
--- entry of the first block, then from the exit of the first block to the entry of the second block, etc.)
Expand Down Expand Up @@ -166,11 +166,11 @@ function Center:generate()
chromosome:setFitness(10000 / distance)
end

local blocksInSequence, entries, _ = cg.BlockSequencer(blocks):findBlockSequence(calculateFitness)
local blocksInSequence, entries, _ = CourseGenerator.BlockSequencer(blocks):findBlockSequence(calculateFitness)
if blocksInSequence == nil then
self.logger:warning('Could not find a valid path on headland between blocks, retry with allowing connections between different headland')
strict = false
blocksInSequence, entries, _ = cg.BlockSequencer(blocks):findBlockSequence(calculateFitness)
blocksInSequence, entries, _ = CourseGenerator.BlockSequencer(blocks):findBlockSequence(calculateFitness)
end
_, self.connectingPaths = calculateDistanceAndConnectingPaths(blocksInSequence, entries)
self.blocks = blocksInSequence
Expand Down Expand Up @@ -198,7 +198,7 @@ function Center:bypassSmallIsland(islandHeadlandPolygon, circle)
-- and then we have those connecting paths between the blocks
for _, connectingPath in ipairs(self.connectingPaths) do
if #connectingPath > 1 then
thisIslandCircled = cg.FieldworkCourseHelper.bypassSmallIsland(connectingPath, self.context.workingWidth,
thisIslandCircled = CourseGenerator.FieldworkCourseHelper.bypassSmallIsland(connectingPath, self.context.workingWidth,
islandHeadlandPolygon, 1, not thisIslandCircled) or thisIslandCircled
end
end
Expand All @@ -208,7 +208,7 @@ end
function Center:bypassBigIsland(islandHeadlandPolygon)
for _, connectingPath in ipairs(self.connectingPaths) do
if #connectingPath > 1 then
cg.FieldworkCourseHelper.bypassSmallIsland(connectingPath, self.context.workingWidth,
CourseGenerator.FieldworkCourseHelper.bypassSmallIsland(connectingPath, self.context.workingWidth,
islandHeadlandPolygon, 1, false)
end
end
Expand Down Expand Up @@ -257,8 +257,8 @@ function Center:_calculateSmallBlockPenalty(blocks, nTotalRows)
end
for _, b in ipairs(blocks) do
local percentageOfRowsInBlock = 100 * (b:getNumberOfRows() / nTotalRows)
if percentageOfRowsInBlock < cg.cSmallBlockRowPercentageLimit then
nResult = nResult + cg.cSmallBlockRowPercentageLimit - percentageOfRowsInBlock
if percentageOfRowsInBlock < CourseGenerator.cSmallBlockRowPercentageLimit then
nResult = nResult + CourseGenerator.cSmallBlockRowPercentageLimit - percentageOfRowsInBlock
end
end
return nResult
Expand Down Expand Up @@ -287,7 +287,7 @@ function Center:_findBestRowAngle()
return bestAngle
end

---@return cg.Row
---@return CourseGenerator.Row
function Center:_createStraightBaseline(rowAngle)
-- Set up a baseline. This goes through the lower left or right corner of the bounding box, at the requested
-- angle, and long enough that when shifted (offset copies are created), will cover the field at any angle.
Expand All @@ -307,7 +307,7 @@ function Center:_createStraightBaseline(rowAngle)
baselineStart = lowerRight - Vector(w * math.cos(rowAngle), 0):setHeading(-rowAngle)
baselineEnd = lowerRight + Vector(h * math.sin(rowAngle), 0):setHeading(-rowAngle)
end
return cg.Row(self.context.workingWidth, { baselineStart, baselineEnd })
return CourseGenerator.Row(self.context.workingWidth, { baselineStart, baselineEnd })
end

--- Calculate how many rows we need with a given work width to fully cover a field and how far apart those
Expand Down Expand Up @@ -378,8 +378,8 @@ function Center:_calculateRowDistribution(workingWidth, fieldWidth, sameWidth, o
end
end

---@param rows cg.Row[]
---@param headland cg.Headland
---@param rows CourseGenerator.Row[]
---@param headland CourseGenerator.Headland
function Center:_splitIntoBlocks(rows, headland)
local blocks
local openBlocks = {}
Expand Down Expand Up @@ -431,7 +431,7 @@ function Center:_splitIntoBlocks(rows, headland)
self.logger:trace(' %.1f m, %d vertices, overlaps with %d block(s)',
section:getLength(), #section, #overlappedBlocks)
if #overlappedBlocks == 0 or #overlappedBlocks > 1 then
local newBlock = cg.Block(self.context.rowPattern, blockId)
local newBlock = CourseGenerator.Block(self.context.rowPattern, blockId)
blockId = blockId + 1
newBlock:addRow(section)
-- remember that we added a section for row #i
Expand All @@ -458,7 +458,7 @@ end
--- generations are calculated. As the population consists of the same blocks and same entry/exit
--- points (and thus, same paths), just in different order, we cache the entries/exits/paths for
--- a better performance.
---@param headland cg.Headland
---@param headland CourseGenerator.Headland
---@param v1 Vector
---@param v2 Vector
---@return Polyline always has at least one vertex
Expand Down Expand Up @@ -488,11 +488,11 @@ function Center:_wrapUpConnectingPaths()
end
else
for _, c in ipairs(self.connectingPaths) do
c:setAttribute(nil, cg.WaypointAttributes.setOnConnectingPath)
c:setAttribute(nil, cg.WaypointAttributes.setHeadlandTurn, nil)
c:setAttribute(nil, CourseGenerator.WaypointAttributes.setOnConnectingPath)
c:setAttribute(nil, CourseGenerator.WaypointAttributes.setHeadlandTurn, nil)
end
end
end

---@class cg.Center
cg.Center = Center
---@class CourseGenerator.Center
CourseGenerator.Center = Center
20 changes: 10 additions & 10 deletions scripts/courseGenerator/CenterTwoSided.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
--- With that pattern, we always use baseline edges, that is, the rows following the
--- field edge (instead of always being straight) and do not allow multiple blocks.

---@class CenterTwoSided : cg.Center
local CenterTwoSided = CpObject(cg.Center)
---@class CenterTwoSided : CourseGenerator.Center
local CenterTwoSided = CpObject(CourseGenerator.Center)

---@param context cg.FieldworkContext
---@param context CourseGenerator.FieldworkContext
---@param boundary Polygon the field boundary
---@param headland cg.Headland|nil the innermost headland if exists
---@param headland CourseGenerator.Headland|nil the innermost headland if exists
---@param startLocation Vector location of the vehicle before it starts working on the center.
---@param bigIslands cg.Island[] islands too big to circle
---@param lastRow cg.Row the last row of the center (before cut), this will be added to the ones generated
---@param bigIslands CourseGenerator.Island[] islands too big to circle
---@param lastRow CourseGenerator.Row the last row of the center (before cut), this will be added to the ones generated
function CenterTwoSided:init(context, boundary, headland, startLocation, bigIslands, lastRow)
cg.Center.init(self, context, boundary, headland, startLocation, bigIslands, lastRow)
CourseGenerator.Center.init(self, context, boundary, headland, startLocation, bigIslands, lastRow)
-- force using the baseline edge, no matter what the context is
self.useBaselineEdge = true
end

function CenterTwoSided:_splitIntoBlocks(rows)
local block = cg.Block(self.context.rowPattern)
local block = CourseGenerator.Block(self.context.rowPattern)
for _, row in ipairs(rows) do
local sections = row:split(self.headland, {}, true)
if #sections == 1 then
Expand All @@ -37,5 +37,5 @@ function CenterTwoSided:_wrapUpConnectingPaths()
self.blocks[1]:getEntryVertex():getAttributes():setUsePathfinderToThisWaypoint()
end

---@class cg.CenterTwoSided : cg.Center
cg.CenterTwoSided = CenterTwoSided
---@class CourseGenerator.CenterTwoSided : CourseGenerator.Center
CourseGenerator.CenterTwoSided = CenterTwoSided
Loading

0 comments on commit 752203f

Please sign in to comment.