Skip to content

Commit 3c9cb95

Browse files
authored
Merge pull request #28 from Azure-Samples/eli-dev/swiftupdates
review updates and bug fixes
2 parents 35a223c + 7a9f83b commit 3c9cb95

File tree

15 files changed

+230
-227
lines changed

15 files changed

+230
-227
lines changed

swift/InkRecognition/Recognizer/Recognizer.xcodeproj/project.pbxproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
CF7B3D9C2250112500CBE1FC /* InkParagraph.swift in Sources */ = {isa = PBXBuildFile; fileRef = CF7B3D8E2250112400CBE1FC /* InkParagraph.swift */; };
2525
CF7B3D9D2250112500CBE1FC /* InkListItem.swift in Sources */ = {isa = PBXBuildFile; fileRef = CF7B3D8F2250112400CBE1FC /* InkListItem.swift */; };
2626
CF7B3D9E2250112500CBE1FC /* InkRecognitionUnit.swift in Sources */ = {isa = PBXBuildFile; fileRef = CF7B3D902250112400CBE1FC /* InkRecognitionUnit.swift */; };
27-
CF7B3DA12250116E00CBE1FC /* InkRenderer.swift in Sources */ = {isa = PBXBuildFile; fileRef = CF7B3DA02250116E00CBE1FC /* InkRenderer.swift */; };
27+
CF7B3DA12250116E00CBE1FC /* InkRendererView.swift in Sources */ = {isa = PBXBuildFile; fileRef = CF7B3DA02250116E00CBE1FC /* InkRendererView.swift */; };
2828
CF7B3DA3225011F100CBE1FC /* Line.swift in Sources */ = {isa = PBXBuildFile; fileRef = CF7B3DA2225011F100CBE1FC /* Line.swift */; };
2929
/* End PBXBuildFile section */
3030

@@ -48,7 +48,7 @@
4848
CF7B3D8E2250112400CBE1FC /* InkParagraph.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InkParagraph.swift; sourceTree = "<group>"; };
4949
CF7B3D8F2250112400CBE1FC /* InkListItem.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InkListItem.swift; sourceTree = "<group>"; };
5050
CF7B3D902250112400CBE1FC /* InkRecognitionUnit.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InkRecognitionUnit.swift; sourceTree = "<group>"; };
51-
CF7B3DA02250116E00CBE1FC /* InkRenderer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InkRenderer.swift; sourceTree = "<group>"; };
51+
CF7B3DA02250116E00CBE1FC /* InkRendererView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InkRendererView.swift; sourceTree = "<group>"; };
5252
CF7B3DA2225011F100CBE1FC /* Line.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Line.swift; sourceTree = "<group>"; };
5353
/* End PBXFileReference section */
5454

@@ -83,7 +83,7 @@
8383
isa = PBXGroup;
8484
children = (
8585
CF7B3DA2225011F100CBE1FC /* Line.swift */,
86-
CF7B3DA02250116E00CBE1FC /* InkRenderer.swift */,
86+
CF7B3DA02250116E00CBE1FC /* InkRendererView.swift */,
8787
CF7B3D852250112400CBE1FC /* InkRecognizer.swift */,
8888
CF7B3D842250112400CBE1FC /* InkBullet.swift */,
8989
CF7B3D8C2250112400CBE1FC /* InkDrawing.swift */,
@@ -186,7 +186,7 @@
186186
CF7B3D9B2250112500CBE1FC /* InkPoint.swift in Sources */,
187187
CF7B3D9A2250112500CBE1FC /* InkDrawing.swift in Sources */,
188188
CF7B3D952250112500CBE1FC /* InkRoot.swift in Sources */,
189-
CF7B3DA12250116E00CBE1FC /* InkRenderer.swift in Sources */,
189+
CF7B3DA12250116E00CBE1FC /* InkRendererView.swift in Sources */,
190190
CF7B3D9E2250112500CBE1FC /* InkRecognitionUnit.swift in Sources */,
191191
CF7B3D982250112500CBE1FC /* InkWritingRegion.swift in Sources */,
192192
CF7B3D962250112500CBE1FC /* InkLine.swift in Sources */,

swift/InkRecognition/Recognizer/Recognizer/Base.lproj/Main.storyboard

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
1919
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2020
<subviews>
21-
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="9kK-y6-3QI" customClass="InkRenderer" customModule="Recognizer" customModuleProvider="target">
21+
<view contentMode="scaleToFill" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="9kK-y6-3QI" customClass="InkRendererView" customModule="Recognizer" customModuleProvider="target">
2222
<rect key="frame" x="0.0" y="48" width="375" height="350"/>
2323
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2424
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>

swift/InkRecognition/Recognizer/Recognizer/InkDrawing.swift

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,85 +4,85 @@ import Foundation
44

55
@objc
66
public enum Shape : Int {
7-
case DRAWING,
8-
SQUARE,
9-
RECTANGLE,
10-
CIRCLE,
11-
ELLIPSE,
12-
TRIANGLE,
13-
ISOSCELESTRIANGLE,
14-
EQUILATERALTRIANGLE,
15-
RIGHTTRIANGLE,
16-
QUADRILATERAL,
17-
DIAMOND,
18-
TRAPEZOID,
19-
PARALLELOGRAM,
20-
PENTAGON,
21-
HEXAGON,
22-
BLOCKARROW,
23-
HEART,
24-
STARSIMPLE,
25-
STARCROSSED,
26-
CLOUD,
27-
LINE,
28-
CURVE,
29-
POLYLINE
7+
case drawing,
8+
square,
9+
rectangle,
10+
circle,
11+
ellipse,
12+
triangle,
13+
isoscelesTriangle,
14+
equilateralTriangle,
15+
rightTriangle,
16+
quadrilateral,
17+
diamond,
18+
trapezoid,
19+
parallelogram,
20+
pentagon,
21+
hexagon,
22+
blockArrow,
23+
heart,
24+
starSimple,
25+
starcrossed,
26+
cloud,
27+
line,
28+
curve,
29+
polyline
3030
}
3131

3232
@objc
3333
class InkDrawing : InkRecognitionUnit {
3434

3535
private var center: InkPoint!
36-
private var confidence: Double!
36+
private var confidence: Float!
3737
var shape:Shape!
3838
var shapeName : String!
39-
var rotationAngle: Double!
39+
var rotationAngle: Float!
4040
var alternates = [Shape]()
4141
var points = [InkPoint]()
42-
let supportedShapes = ["drawing": Shape.DRAWING,
43-
"circle": Shape.CIRCLE,
44-
"square": Shape.SQUARE,
45-
"rectangle": Shape.RECTANGLE,
46-
"triangle": Shape.TRIANGLE,
47-
"ellipse": Shape.ELLIPSE,
48-
"isoscelesTriangle": Shape.ISOSCELESTRIANGLE,
49-
"equilateralTriangle": Shape.EQUILATERALTRIANGLE,
50-
"rightTriangle": Shape.RIGHTTRIANGLE,
51-
"quadrilateral": Shape.QUADRILATERAL,
52-
"diamond": Shape.DIAMOND,
53-
"trapezoid": Shape.TRAPEZOID,
54-
"parallelogram": Shape.PARALLELOGRAM,
55-
"pentagon": Shape.PENTAGON,
56-
"hexagon": Shape.HEXAGON,
57-
"blockArrow": Shape.BLOCKARROW,
58-
"heart": Shape.HEART,
59-
"starSimple": Shape.STARSIMPLE,
60-
"starCrossed": Shape.STARCROSSED,
61-
"cloud": Shape.CLOUD,
62-
"line": Shape.LINE,
63-
"curve": Shape.CURVE,
64-
"polyline": Shape.POLYLINE]
42+
let supportedShapes = ["drawing": Shape.drawing,
43+
"circle": Shape.circle,
44+
"square": Shape.square,
45+
"rectangle": Shape.rectangle,
46+
"triangle": Shape.triangle,
47+
"ellipse": Shape.ellipse,
48+
"isoscelesTriangle": Shape.isoscelesTriangle,
49+
"equilateralTriangle": Shape.equilateralTriangle,
50+
"rightTriangle": Shape.rightTriangle,
51+
"quadrilateral": Shape.quadrilateral,
52+
"diamond": Shape.diamond,
53+
"trapezoid": Shape.trapezoid,
54+
"parallelogram": Shape.parallelogram,
55+
"pentagon": Shape.pentagon,
56+
"hexagon": Shape.hexagon,
57+
"blockArrow": Shape.blockArrow,
58+
"heart": Shape.heart,
59+
"starSimple": Shape.starSimple,
60+
"starCrossed": Shape.starcrossed,
61+
"cloud": Shape.cloud,
62+
"line": Shape.line,
63+
"curve": Shape.curve,
64+
"polyline": Shape.polyline]
6565

6666
@objc
6767
override init(json : [String: Any]) {
6868
super.init(json: json)
6969

70-
let jsonCenter = json["center"] as? [String: Any]
71-
let xValue = jsonCenter!["x"] as! Double
72-
let yValue = jsonCenter!["y"] as! Double
70+
let jsonCenter = json["center"] as! [String: Any]
71+
let xValue = jsonCenter["x"] as! Float
72+
let yValue = jsonCenter["y"] as! Float
7373
self.center = InkPoint(x: xValue, y: yValue)
7474
//extract the "beautified" points. These can be used to draw a more strucutured version of the shape.
7575
if let shapePoints = json["points"] as? [[String:Any]] {
7676
for shapePoint in shapePoints {
77-
let x = shapePoint["x"] as! Double
78-
let y = shapePoint["y"] as! Double
77+
let x = shapePoint["x"] as! Float
78+
let y = shapePoint["y"] as! Float
7979
self.points.append(InkPoint(x: x, y: y));
8080
}
8181
}
82-
self.rotationAngle = json["rotationAngle"] as? Double ?? 0.0
83-
self.shapeName = (json["recognizedObject"] as! String)
82+
self.rotationAngle = json["rotationAngle"] as? Float ?? 0.0
83+
self.shapeName = (json["recognizedObject"] as! String)
8484
self.shape = stringToShape(shape: shapeName)
85-
self.confidence = json["confidence"] as? Double
85+
self.confidence = json["confidence"] as? Float
8686
if let alternates = json["alternates"] as? [[String: Any]] {
8787
for alternate in alternates {
8888
let alternateName = alternate["recognizedObject"] as! String
@@ -95,6 +95,6 @@ class InkDrawing : InkRecognitionUnit {
9595
if let concreteShape = supportedShapes[shape] {
9696
return concreteShape
9797
}
98-
return Shape.DRAWING
98+
return Shape.drawing
9999
}
100100
}

swift/InkRecognition/Recognizer/Recognizer/InkLine.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ class InkLine : InkRecognitionUnit {
2828
return wordAlternates
2929
}
3030
}
31+

swift/InkRecognition/Recognizer/Recognizer/InkListItem.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
import Foundation
33

44
@objc
5-
class InkListItem : InkRecognitionUnit {
5+
class InkListItem : InkRecognitionUnit {
66

7-
@objc
8-
override init(json : [String: Any]) {
9-
super.init(json:json)
10-
}
117
}

swift/InkRecognition/Recognizer/Recognizer/InkParagraph.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
import Foundation
33

44
@objc
5-
class InkParagraph : InkRecognitionUnit {
5+
class InkParagraph : InkRecognitionUnit {
66

7-
@objc
8-
override init(json : [String: Any]) {
9-
super.init(json:json)
10-
}
117
}

swift/InkRecognition/Recognizer/Recognizer/InkPoint.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,27 @@ import UIKit
55

66
@objc
77
class InkPoint : NSObject, Decodable {
8-
var x : Double = 0;
9-
var y : Double = 0;
8+
var x : Float = 0;
9+
var y : Float = 0;
1010

11+
//Values from the iphone XR. Change to match your target devices with a table of values for each device.
12+
static let ppi : Float = 326.0
13+
static let mmPerInch : Float = 25.4
1114
@objc
12-
init(x: Double, y: Double) {
15+
init(x: Float, y: Float) {
1316
self.x = x
1417
self.y = y
1518
}
1619

20+
@objc
21+
static func millimeterToCGFloat(mmPosition: Float) -> CGFloat {
22+
return CGFloat(mmPosition/InkPoint.mmPerInch*InkPoint.ppi)
23+
}
24+
1725
@objc
1826
init(point: CGPoint) {
19-
self.x = Double(point.x)
20-
self.y = Double(point.y)
27+
self.x = Float(point.x)/InkPoint.ppi*InkPoint.mmPerInch
28+
self.y = Float(point.y)/InkPoint.ppi*InkPoint.mmPerInch
2129
}
2230
}
31+
Lines changed: 39 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
11

2-
32
import Foundation
4-
3+
import UIKit
54
@objc
65
public enum InkRecognitionUnitCategory: Int {
7-
case UNKNOWN,
8-
INKWORD,
9-
INKDRAWING,
10-
INKBULLET,
11-
LISTITEM,
12-
PARAGRAPH,
13-
LINE,
14-
WRITINGREGION
6+
case unknown,
7+
inkWord,
8+
inkDrawing,
9+
inkBullet,
10+
listItem,
11+
paragraph,
12+
line,
13+
writingRegion
1514
}
1615

1716
@objc
1817
class InkBoundingRectangle : NSObject, Decodable {
19-
var topX : Double = 0
20-
var topY : Double = 0
21-
var width : Double = 0
22-
var height : Double = 0
18+
var topX : CGFloat = 0
19+
var topY : CGFloat = 0
20+
var width : CGFloat = 0
21+
var height : CGFloat = 0
2322

24-
init(x: Double, y: Double, width: Double, height: Double) {
25-
self.topX = x
26-
self.topY = y
27-
self.width = width
28-
self.height = height
23+
init(x: Float, y: Float, width: Float, height: Float) {
24+
self.topX = InkPoint.millimeterToCGFloat(mmPosition: x)
25+
self.topY = InkPoint.millimeterToCGFloat(mmPosition: y)
26+
self.width = InkPoint.millimeterToCGFloat(mmPosition: width)
27+
self.height = InkPoint.millimeterToCGFloat(mmPosition: height)
2928
}
3029
}
3130

3231
@objc
3332
class InkRecognitionUnit : NSObject {
3433
private var categoryString : String
3534
private var boundingRectangle : InkBoundingRectangle!
36-
private var rotatedBoundingRectangle = [InkPoint]()
35+
private var rotatedBoundingRectangle = [CGPoint]()
3736
private var childIds = [Int]();
3837
private var parentId = -1
3938
private var strokeIds : [Int]
@@ -43,27 +42,29 @@ class InkRecognitionUnit : NSObject {
4342

4443
@objc
4544
public var category : InkRecognitionUnitCategory {
46-
get{
45+
get {
4746
var recognitionCategory: InkRecognitionUnitCategory
48-
switch(self.categoryString) {
47+
switch (self.categoryString) {
4948
case "inkWord":
50-
recognitionCategory = InkRecognitionUnitCategory.INKWORD
49+
recognitionCategory = InkRecognitionUnitCategory.inkWord
5150
case "inkDrawing":
52-
recognitionCategory = InkRecognitionUnitCategory.INKDRAWING
51+
recognitionCategory = InkRecognitionUnitCategory.inkDrawing
5352
case "inkBullet":
54-
recognitionCategory = InkRecognitionUnitCategory.INKBULLET
53+
recognitionCategory = InkRecognitionUnitCategory.inkBullet
5554
case "line":
56-
recognitionCategory = InkRecognitionUnitCategory.LINE
55+
recognitionCategory = InkRecognitionUnitCategory.line
56+
case "listItem":
57+
recognitionCategory = InkRecognitionUnitCategory.listItem
5758
case "paragraph":
58-
recognitionCategory = InkRecognitionUnitCategory.PARAGRAPH
59+
recognitionCategory = InkRecognitionUnitCategory.paragraph
5960
case "writingRegion":
60-
recognitionCategory = InkRecognitionUnitCategory.WRITINGREGION
61+
recognitionCategory = InkRecognitionUnitCategory.writingRegion
6162
default:
62-
recognitionCategory = InkRecognitionUnitCategory.UNKNOWN
63+
recognitionCategory = InkRecognitionUnitCategory.unknown
6364
}
6465
return recognitionCategory;
6566
}
66-
}
67+
}
6768

6869
@objc
6970
public var children : [InkRecognitionUnit] {
@@ -81,8 +82,8 @@ class InkRecognitionUnit : NSObject {
8182
}
8283

8384
@objc
84-
public var rotatedBoundingBox :[InkPoint] {
85-
return self.rotatedBoundingRectangle
85+
public var rotatedBoundingBox: [CGPoint] {
86+
return self.rotatedBoundingRectangle
8687
}
8788

8889
@objc
@@ -97,14 +98,16 @@ class InkRecognitionUnit : NSObject {
9798
self.strokeIds = json["strokeIds"] as! [Int]
9899

99100
let jsonBoundingRect = json["boundingRectangle"] as! [String: Any]
100-
self.boundingRectangle = InkBoundingRectangle(x: jsonBoundingRect["topX"] as! Double, y: jsonBoundingRect["topY"] as!Double,width: jsonBoundingRect["width"] as! Double,height: jsonBoundingRect["height"] as! Double)
101+
self.boundingRectangle = InkBoundingRectangle(x: jsonBoundingRect["topX"] as! Float, y: jsonBoundingRect["topY"] as!Float,width: jsonBoundingRect["width"] as! Float,height: jsonBoundingRect["height"] as! Float)
101102

102103
let jsonRotatedRectPoints = json["rotatedBoundingRectangle"] as! [[String: Any]]
103104

104105
for point in jsonRotatedRectPoints {
105-
let xValue = point["x"] as! Double
106-
let yValue = point["y"] as! Double
107-
self.rotatedBoundingRectangle.append( InkPoint(x: xValue, y: yValue))
106+
let xValue = point["x"] as! Float
107+
let yValue = point["y"] as! Float
108+
let pointX = InkPoint.millimeterToCGFloat(mmPosition: xValue)
109+
let pointY = InkPoint.millimeterToCGFloat(mmPosition: yValue)
110+
self.rotatedBoundingRectangle.append(CGPoint(x: pointX, y: pointY))
108111
}
109112
}
110113
}

0 commit comments

Comments
 (0)