Skip to content

Commit

Permalink
Switch to cleaner enum implementation for lines
Browse files Browse the repository at this point in the history
  • Loading branch information
datskos committed Jun 10, 2016
1 parent c43aa78 commit 6c968c1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ extension ViewController: CameraDelegate {
if let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
let attachments = CMCopyDictionaryOfAttachments(kCFAllocatorDefault, sampleBuffer, CMAttachmentMode(kCMAttachmentMode_ShouldPropagate))!
let img = CIImage(CVPixelBuffer: pixelBuffer, options: attachments as? [String: AnyObject])
var lines = [LineProtocol]()
var lines = [Line]()
for feature in faceDetector.featuresInImage(img, options: [CIDetectorImageOrientation: 6]) {
if feature is CIFaceFeature {
lines = lines + faceLines(feature.bounds)
Expand All @@ -73,7 +73,7 @@ extension ViewController: CameraDelegate {
}
}

func faceLines(bounds: CGRect) -> [LineProtocol] {
func faceLines(bounds: CGRect) -> [Line] {
// convert from CoreImage to GL coords
let flip = CGAffineTransformMakeScale(1, -1)
let rotate = CGAffineTransformRotate(flip, CGFloat(-M_PI_2))
Expand All @@ -91,9 +91,9 @@ extension ViewController: CameraDelegate {
let bl = CGPoint(x: x, y: y + height)
let br = CGPoint(x: x + width, y: y + height)

return [LineSegment(p1: tl, p2: tr), // top
LineSegment(p1: tr, p2: br), // right
LineSegment(p1: br, p2: bl), // bottom
LineSegment(p1: bl, p2: tl)] // left
return [.Segment(p1: tl, p2: tr), // top
.Segment(p1: tr, p2: br), // right
.Segment(p1: br, p2: bl), // bottom
.Segment(p1: bl, p2: tl)] // left
}
}
47 changes: 14 additions & 33 deletions framework/Source/Operations/LineGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,43 +12,24 @@
#endif
#endif

public protocol LineProtocol {
func toGLEndpoints() -> [GLfloat]
}
public enum Line {
case Infinite(slope: Float, intercept: Float)
case Segment(p1: CGPoint, p2: CGPoint)

public struct Line: LineProtocol {
public let slope:Float
public let intercept:Float

public init (slope:Float, intercept:Float) {
self.slope = slope
self.intercept = intercept
}

public func toGLEndpoints() -> [GLfloat] {
if (slope > 9000.0) {// Vertical line
return [intercept, -1.0, intercept, 1.0]
} else {
return [-1.0, GLfloat(slope * -1.0 + intercept), 1.0, GLfloat(slope * 1.0 + intercept)]
func toGLEndpoints() -> [GLfloat] {
switch self {
case .Infinite(let slope, let intercept):
if (slope > 9000.0) {// Vertical line
return [intercept, -1.0, intercept, 1.0]
} else {
return [-1.0, GLfloat(slope * -1.0 + intercept), 1.0, GLfloat(slope * 1.0 + intercept)]
}
case .Segment(let p1, let p2):
return [p1.x, p1.y, p2.x, p2.y].map {GLfloat($0)}
}
}
}

public struct LineSegment: LineProtocol {
public let p1: CGPoint
public let p2: CGPoint

public init(p1:CGPoint, p2:CGPoint) {
self.p1 = p1
self.p2 = p2
}

public func toGLEndpoints() -> [GLfloat] {
return [p1.x, p1.y, p2.x, p2.y].map {GLfloat($0)}
}
}


public class LineGenerator: ImageGenerator {
public var lineColor:Color = Color.Green { didSet { uniformSettings["lineColor"] = lineColor } }
public var lineWidth:Float = 1.0 {
Expand All @@ -69,7 +50,7 @@ public class LineGenerator: ImageGenerator {
({lineColor = Color.Red})()
}

public func renderLines(lines:[LineProtocol]) {
public func renderLines(lines:[Line]) {
imageFramebuffer.activateFramebufferForRendering()

lineShader.use()
Expand Down

0 comments on commit 6c968c1

Please sign in to comment.