Skip to content

Commit

Permalink
CATTY-686 Fix computer vision sensors in landscape mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoeg authored and wallisch committed May 12, 2023
1 parent b9fc152 commit 7b02e33
Show file tree
Hide file tree
Showing 117 changed files with 609 additions and 750 deletions.
6 changes: 5 additions & 1 deletion src/Catty/Functions/FunctionManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ import BluetoothHelper

public static var defaultValueForUndefinedFunction: Double = 0
private static var functionMap = [String: Function]() // TODO: make instance let
private let landscapeMode: Bool

init(functions: [Function]) {
init(functions: [Function], landscapeMode: Bool = false) {
self.landscapeMode = landscapeMode
super.init()
registerFunctions(functionList: functions)
}
Expand Down Expand Up @@ -58,6 +60,8 @@ import BluetoothHelper
value = function.value() as AnyObject
} else if let function = function as? SingleParameterDoubleFunction {
value = function.value(parameter: firstParameter) as AnyObject
} else if let function = function as? SingleParameterDoubleLandscapeFunction {
value = function.value(parameter: firstParameter, landscapeMode: landscapeMode) as AnyObject
} else if let function = function as? DoubleParameterDoubleFunction {
value = function.value(firstParameter: firstParameter, secondParameter: secondParameter) as AnyObject
} else if let function = function as? ZeroParameterStringFunction {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

class HeightOfObjectWithIDFunction: SingleParameterDoubleFunction {
class HeightOfObjectWithIDFunction: SingleParameterDoubleLandscapeFunction {

static let tag = "HEIGHT_OF_OBJECT_WITH_ID"
static let name = kUIFEFunctionHeightOfObjectWithID
Expand All @@ -30,10 +30,12 @@ class HeightOfObjectWithIDFunction: SingleParameterDoubleFunction {
static let requiredResource = ResourceType.objectRecognition

let getVisualDetectionManager: () -> VisualDetectionManagerProtocol?
let stageHeight: Double?
let stageSize: CGSize
var stageHeight: Double

init(stageSize: CGSize, visualDetectionManagerGetter: @escaping () -> VisualDetectionManagerProtocol?) {
self.getVisualDetectionManager = visualDetectionManagerGetter
self.stageSize = stageSize
self.stageHeight = Double(stageSize.height)
}

Expand All @@ -45,17 +47,17 @@ class HeightOfObjectWithIDFunction: SingleParameterDoubleFunction {
.number(defaultValue: 1)
}

func value(parameter: AnyObject?) -> Double {
func value(parameter: AnyObject?, landscapeMode: Bool) -> Double {
guard let idAsDouble = parameter as? Double,
let visualDetectionManager = self.getVisualDetectionManager(),
let stageHeight = self.stageHeight
let visualDetectionManager = self.getVisualDetectionManager()
else { return type(of: self).defaultValue }

let id = Int(idAsDouble)
guard id >= 0 && id < visualDetectionManager.objectRecognitions.count else {
return type(of: self).defaultValue
}

stageHeight = Double(landscapeMode ? stageSize.width : stageSize.height)
let boundingBoxHeight = visualDetectionManager.objectRecognitions[id].boundingBox.height
return stageHeight * boundingBoxHeight
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

class WidthOfObjectWithIDFunction: SingleParameterDoubleFunction {
class WidthOfObjectWithIDFunction: SingleParameterDoubleLandscapeFunction {

static let tag = "WIDTH_OF_OBJECT_WITH_ID"
static let name = kUIFEFunctionWidthOfObjectWithID
Expand All @@ -30,10 +30,12 @@ class WidthOfObjectWithIDFunction: SingleParameterDoubleFunction {
static let requiredResource = ResourceType.objectRecognition

let getVisualDetectionManager: () -> VisualDetectionManagerProtocol?
let stageWidth: Double?
let stageSize: CGSize
var stageWidth: Double

init(stageSize: CGSize, visualDetectionManagerGetter: @escaping () -> VisualDetectionManagerProtocol?) {
self.getVisualDetectionManager = visualDetectionManagerGetter
self.stageSize = stageSize
self.stageWidth = Double(stageSize.width)
}

Expand All @@ -45,10 +47,9 @@ class WidthOfObjectWithIDFunction: SingleParameterDoubleFunction {
.number(defaultValue: 1)
}

func value(parameter: AnyObject?) -> Double {
func value(parameter: AnyObject?, landscapeMode: Bool) -> Double {
guard let idAsDouble = parameter as? Double,
let visualDetectionManager = self.getVisualDetectionManager(),
let stageWidth = self.stageWidth
let visualDetectionManager = self.getVisualDetectionManager()
else { return type(of: self).defaultValue }

let id = Int(idAsDouble)
Expand All @@ -57,6 +58,8 @@ class WidthOfObjectWithIDFunction: SingleParameterDoubleFunction {
}

let boundingBoxWidth = visualDetectionManager.objectRecognitions[id].boundingBox.width

stageWidth = Double(landscapeMode ? stageSize.height : stageSize.width)
return stageWidth * boundingBoxWidth
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

class XOfObjectWithIDFunction: SingleParameterDoubleFunction {
class XOfObjectWithIDFunction: SingleParameterDoubleLandscapeFunction {

static let tag = "X_OF_OBJECT_WITH_ID"
static let name = kUIFEFunctionXOfObjectWithID
Expand All @@ -30,10 +30,12 @@ class XOfObjectWithIDFunction: SingleParameterDoubleFunction {
static let requiredResource = ResourceType.objectRecognition

let getVisualDetectionManager: () -> VisualDetectionManagerProtocol?
let stageWidth: Double?
let stageSize: CGSize
var stageWidth: Double

init(stageSize: CGSize, visualDetectionManagerGetter: @escaping () -> VisualDetectionManagerProtocol?) {
self.getVisualDetectionManager = visualDetectionManagerGetter
self.stageSize = stageSize
self.stageWidth = Double(stageSize.width)
}

Expand All @@ -45,10 +47,9 @@ class XOfObjectWithIDFunction: SingleParameterDoubleFunction {
.number(defaultValue: 1)
}

func value(parameter: AnyObject?) -> Double {
func value(parameter: AnyObject?, landscapeMode: Bool) -> Double {
guard let idAsDouble = parameter as? Double,
let visualDetectionManager = self.getVisualDetectionManager(),
let stageWidth = self.stageWidth
let visualDetectionManager = self.getVisualDetectionManager()
else { return type(of: self).defaultValue }

let id = Int(idAsDouble)
Expand All @@ -59,6 +60,7 @@ class XOfObjectWithIDFunction: SingleParameterDoubleFunction {
let boundingBox = visualDetectionManager.objectRecognitions[id].boundingBox
let objectPositionX = boundingBox.origin.x + boundingBox.width / 2.0

stageWidth = Double(landscapeMode ? stageSize.height : stageSize.width)
return stageWidth * objectPositionX - stageWidth / 2.0
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

class YOfObjectWithIDFunction: SingleParameterDoubleFunction {
class YOfObjectWithIDFunction: SingleParameterDoubleLandscapeFunction {

static let tag = "Y_OF_OBJECT_WITH_ID"
static let name = kUIFEFunctionYOfObjectWithID
Expand All @@ -30,10 +30,12 @@ class YOfObjectWithIDFunction: SingleParameterDoubleFunction {
static let requiredResource = ResourceType.objectRecognition

let getVisualDetectionManager: () -> VisualDetectionManagerProtocol?
let stageHeight: Double?
let stageSize: CGSize
var stageHeight: Double

init(stageSize: CGSize, visualDetectionManagerGetter: @escaping () -> VisualDetectionManagerProtocol?) {
self.getVisualDetectionManager = visualDetectionManagerGetter
self.stageSize = stageSize
self.stageHeight = Double(stageSize.height)
}

Expand All @@ -45,10 +47,9 @@ class YOfObjectWithIDFunction: SingleParameterDoubleFunction {
.number(defaultValue: 1)
}

func value(parameter: AnyObject?) -> Double {
func value(parameter: AnyObject?, landscapeMode: Bool) -> Double {
guard let idAsDouble = parameter as? Double,
let visualDetectionManager = self.getVisualDetectionManager(),
let stageHeight = self.stageHeight
let visualDetectionManager = self.getVisualDetectionManager()
else { return type(of: self).defaultValue }

let id = Int(idAsDouble)
Expand All @@ -59,6 +60,7 @@ class YOfObjectWithIDFunction: SingleParameterDoubleFunction {
let boundingBox = visualDetectionManager.objectRecognitions[id].boundingBox
let objectPositionY = boundingBox.origin.y + boundingBox.height / 2.0

stageHeight = Double(landscapeMode ? stageSize.width : stageSize.height)
return stageHeight * objectPositionY - stageHeight / 2.0
}

Expand Down
4 changes: 4 additions & 0 deletions src/Catty/Functions/Protocols/Function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ protocol SingleParameterDoubleFunction: DoubleFunction, SingleParameterFunctionP
func value(parameter: AnyObject?) -> Double
}

protocol SingleParameterDoubleLandscapeFunction: DoubleFunction, SingleParameterFunctionProtocol {
func value(parameter: AnyObject?, landscapeMode: Bool) -> Double
}

protocol DoubleParameterDoubleFunction: DoubleFunction, DoubleParameterFunctionProtocol {
func value(firstParameter: AnyObject?, secondParameter: AnyObject?) -> Double
}
Expand Down
12 changes: 7 additions & 5 deletions src/Catty/Functions/TextRecognition/TextBlockXFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

class TextBlockXFunction: SingleParameterDoubleFunction {
class TextBlockXFunction: SingleParameterDoubleLandscapeFunction {

static let tag = "TEXT_BLOCK_X"
static let name = kUIFEFunctionTextBlockX
Expand All @@ -30,10 +30,12 @@ class TextBlockXFunction: SingleParameterDoubleFunction {
static let requiredResource = ResourceType.textRecognition

let getVisualDetectionManager: () -> VisualDetectionManagerProtocol?
let stageWidth: Double?
let stageSize: CGSize
var stageWidth: Double

init(stageSize: CGSize, visualDetectionManagerGetter: @escaping () -> VisualDetectionManagerProtocol?) {
self.getVisualDetectionManager = visualDetectionManagerGetter
self.stageSize = stageSize
self.stageWidth = Double(stageSize.width)
}

Expand All @@ -45,10 +47,9 @@ class TextBlockXFunction: SingleParameterDoubleFunction {
.number(defaultValue: 1)
}

func value(parameter: AnyObject?) -> Double {
func value(parameter: AnyObject?, landscapeMode: Bool) -> Double {
guard let textBlockNumberAsDouble = parameter as? Double,
let visualDetectionManager = self.getVisualDetectionManager(),
let stageWidth = self.stageWidth
let visualDetectionManager = self.getVisualDetectionManager()
else { return type(of: self).defaultValue }

let textBlockNumber = Int(textBlockNumberAsDouble)
Expand All @@ -58,6 +59,7 @@ class TextBlockXFunction: SingleParameterDoubleFunction {

let textBlockPositionX = visualDetectionManager.textBlockPosition[textBlockNumber - 1].x

stageWidth = Double(landscapeMode ? stageSize.height : stageSize.width)
return stageWidth * textBlockPositionX - stageWidth / 2.0
}

Expand Down
13 changes: 8 additions & 5 deletions src/Catty/Functions/TextRecognition/TextBlockYFunction.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* along with this program. If not, see http://www.gnu.org/licenses/.
*/

class TextBlockYFunction: SingleParameterDoubleFunction {
class TextBlockYFunction: SingleParameterDoubleLandscapeFunction {

static let tag = "TEXT_BLOCK_Y"
static let name = kUIFEFunctionTextBlockY
Expand All @@ -30,10 +30,12 @@ class TextBlockYFunction: SingleParameterDoubleFunction {
static let requiredResource = ResourceType.textRecognition

let getVisualDetectionManager: () -> VisualDetectionManagerProtocol?
let stageHeight: Double?
let stageSize: CGSize
var stageHeight: Double

init(stageSize: CGSize, visualDetectionManagerGetter: @escaping () -> VisualDetectionManagerProtocol?) {
self.getVisualDetectionManager = visualDetectionManagerGetter
self.stageSize = stageSize
self.stageHeight = Double(stageSize.height)
}

Expand All @@ -45,10 +47,9 @@ class TextBlockYFunction: SingleParameterDoubleFunction {
.number(defaultValue: 1)
}

func value(parameter: AnyObject?) -> Double {
func value(parameter: AnyObject?, landscapeMode: Bool) -> Double {
guard let textBlockNumberAsDouble = parameter as? Double,
let visualDetectionManager = self.getVisualDetectionManager(),
let stageHeight = self.stageHeight
let visualDetectionManager = self.getVisualDetectionManager()
else { return type(of: self).defaultValue }

let textBlockNumber = Int(textBlockNumberAsDouble)
Expand All @@ -57,6 +58,8 @@ class TextBlockYFunction: SingleParameterDoubleFunction {
}

let textBlockPositionY = visualDetectionManager.textBlockPosition[textBlockNumber - 1].y

stageHeight = Double(landscapeMode ? stageSize.width : stageSize.height)
return stageHeight * textBlockPositionY - stageHeight / 2.0
}

Expand Down
16 changes: 10 additions & 6 deletions src/Catty/PlayerEngine/Formula/FormulaManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ import CoreMotion
FormulaManager.buildFunctionManager(stageSize: stageSize,
touchManager: touchManager,
visualDetectionManager: visualDetectionManager,
bluetoothService: bluetoothService)
bluetoothService: bluetoothService,
landscapeMode: landscapeMode)

let operatorManager = FormulaManager.buildOperatorManager()

Expand Down Expand Up @@ -157,12 +158,15 @@ import CoreMotion
private static func buildFunctionManager(stageSize: CGSize,
touchManager: TouchManagerProtocol,
visualDetectionManager: VisualDetectionManager,
bluetoothService: BluetoothService) -> FunctionManager {
bluetoothService: BluetoothService,
landscapeMode: Bool) -> FunctionManager {

FunctionManager(functions: CatrobatSetup.registeredFunctions(stageSize: stageSize,
touchManager: touchManager,
visualDetectionManager: visualDetectionManager,
bluetoothService: bluetoothService))
let functions = CatrobatSetup.registeredFunctions(stageSize: stageSize,
touchManager: touchManager,
visualDetectionManager: visualDetectionManager,
bluetoothService: bluetoothService)

return FunctionManager(functions: functions, landscapeMode: landscapeMode)
}

private static func buildOperatorManager() -> OperatorManagerProtocol {
Expand Down
12 changes: 5 additions & 7 deletions src/Catty/PlayerEngine/Sensors/BodyPose/LeftAnkleXSensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ class LeftAnkleXSensor: DeviceDoubleSensor {
static let requiredResource = ResourceType.bodyPoseDetection

let getVisualDetectionManager: () -> VisualDetectionManagerProtocol?
let stageWidth: Double?
let stageSize: CGSize
var stageWidth: Double

init(stageSize: CGSize, visualDetectionManagerGetter: @escaping () -> VisualDetectionManagerProtocol?) {
self.getVisualDetectionManager = visualDetectionManagerGetter
self.stageSize = stageSize
self.stageWidth = Double(stageSize.width)
}

Expand All @@ -41,17 +43,13 @@ class LeftAnkleXSensor: DeviceDoubleSensor {
}

func rawValue(landscapeMode: Bool) -> Double {
stageWidth = Double(landscapeMode ? stageSize.height : stageSize.width)
guard let positionX = self.getVisualDetectionManager()?.bodyPosePositionRatioDictionary[self.tag()] else { return type(of: self).defaultRawValue }
return positionX
}

func convertToStandardized(rawValue: Double) -> Double {
if rawValue == type(of: self).defaultRawValue {
return rawValue
}
guard let stageWidth = self.stageWidth else {
return type(of: self).defaultRawValue }
return stageWidth * rawValue - stageWidth / 2.0
stageWidth * rawValue - stageWidth / 2.0
}

func formulaEditorSections(for spriteObject: SpriteObject) -> [FormulaEditorSection] {
Expand Down
11 changes: 5 additions & 6 deletions src/Catty/PlayerEngine/Sensors/BodyPose/LeftAnkleYSensor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ class LeftAnkleYSensor: DeviceDoubleSensor {
static let requiredResource = ResourceType.bodyPoseDetection

let getVisualDetectionManager: () -> VisualDetectionManagerProtocol?
let stageHeight: Double?
let stageSize: CGSize
var stageHeight: Double

init(stageSize: CGSize, visualDetectionManagerGetter: @escaping () -> VisualDetectionManagerProtocol?) {
self.getVisualDetectionManager = visualDetectionManagerGetter
self.stageSize = stageSize
self.stageHeight = Double(stageSize.height)
}

Expand All @@ -41,16 +43,13 @@ class LeftAnkleYSensor: DeviceDoubleSensor {
}

func rawValue(landscapeMode: Bool) -> Double {
stageHeight = Double(landscapeMode ? stageSize.width : stageSize.height)
guard let positionY = self.getVisualDetectionManager()?.bodyPosePositionRatioDictionary[self.tag()] else { return type(of: self).defaultRawValue }
return positionY
}

func convertToStandardized(rawValue: Double) -> Double {
if rawValue == type(of: self).defaultRawValue {
return rawValue
}
guard let stageHeight = self.stageHeight else { return type(of: self).defaultRawValue }
return stageHeight * rawValue - stageHeight / 2.0
stageHeight * rawValue - stageHeight / 2.0
}

func formulaEditorSections(for spriteObject: SpriteObject) -> [FormulaEditorSection] {
Expand Down
Loading

0 comments on commit 7b02e33

Please sign in to comment.