Skip to content

Commit

Permalink
Merge pull request #1762 from mstoeg/CATTY-686
Browse files Browse the repository at this point in the history
CATTY-686 Fix computer vision sensors in landscape mode
  • Loading branch information
wallisch authored May 12, 2023
2 parents d46dbca + 1ed2114 commit 9724bae
Show file tree
Hide file tree
Showing 121 changed files with 623 additions and 756 deletions.
20 changes: 14 additions & 6 deletions src/Catty.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -4886,6 +4886,14 @@
path = CattyTests/Resources/EmbroideryReference;
sourceTree = SOURCE_ROOT;
};
17399B8F2A0E336800BCECC1 /* DetectionManager */ = {
isa = PBXGroup;
children = (
4CD27AA521B55DBB00DDADB5 /* VisualDetectionManagerTest.swift */,
);
path = DetectionManager;
sourceTree = "<group>";
};
180D12E424D979A800341518 /* IO */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -7155,13 +7163,13 @@
4C822615213FA7A300F3D750 /* Sensors */ = {
isa = PBXGroup;
children = (
17399B8F2A0E336800BCECC1 /* DetectionManager */,
4974B2BC281A686A00EEF0D9 /* TextRecognition */,
4C822651213FA7A300F3D750 /* SensorManagerTests.swift */,
4C822640213FA7A300F3D750 /* Audio */,
49402B9E28117888009FCBF8 /* HandPose */,
49949B4A2806FCEC0073BF65 /* BodyPose */,
49E494E927F5993E0035578D /* FacePose */,
4C822626213FA7A300F3D750 /* Camera */,
4C822626213FA7A300F3D750 /* Face */,
4C822640213FA7A300F3D750 /* Audio */,
4C822632213FA7A300F3D750 /* Date */,
4C822630213FA7A300F3D750 /* Heading */,
4C82262B213FA7A300F3D750 /* Location */,
Expand All @@ -7170,6 +7178,7 @@
4C822642213FA7A300F3D750 /* Object */,
4C82264F213FA7A300F3D750 /* Phiro */,
4C822621213FA7A300F3D750 /* Touch */,
4C822651213FA7A300F3D750 /* SensorManagerTests.swift */,
);
name = Sensors;
path = PlayerEngine/Sensors;
Expand Down Expand Up @@ -7203,15 +7212,14 @@
path = Touch;
sourceTree = "<group>";
};
4C822626213FA7A300F3D750 /* Camera */ = {
4C822626213FA7A300F3D750 /* Face */ = {
isa = PBXGroup;
children = (
4CD27AA521B55DBB00DDADB5 /* VisualDetectionManagerTest.swift */,
4C822627213FA7A300F3D750 /* FaceDetectionSensorTest.swift */,
4C822628213FA7A300F3D750 /* FaceSizeSensorTest.swift */,
4C822629213FA7A300F3D750 /* FacePositionSensorTest.swift */,
);
path = Camera;
path = Face;
sourceTree = "<group>";
};
4C82262B213FA7A300F3D750 /* Location */ = {
Expand Down
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
Loading

0 comments on commit 9724bae

Please sign in to comment.