New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes GrayScottStruct to be a struct #3

Merged
merged 5 commits into from Aug 19, 2014
Jump to file or symbol
Failed to load files and symbols.
+108 −183
Diff settings

Always

Just for now

View
@@ -0,0 +1,9 @@
*.xccheckout
*.xcuserstate
*.xcbkptlist
ThreadsExperiment.xcodeproj/xcuserdata/*
@@ -214,10 +214,11 @@
TargetAttributes = {
BE0E5C61198CA26700395741 = {
CreatedOnToolsVersion = 6.0;
DevelopmentTeam = ZBFYF9JG5V;
DevelopmentTeam = PPUD3Y4398;
};
BE0E5C76198CA26700395741 = {
CreatedOnToolsVersion = 6.0;
DevelopmentTeam = PPUD3Y4398;
TestTargetID = BE0E5C61198CA26700395741;
};
};
@@ -392,10 +393,13 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
INFOPLIST_FILE = ThreadsExperiment/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
};
name = Debug;
};
@@ -404,10 +408,13 @@
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
CODE_SIGN_IDENTITY = "iPhone Developer";
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
INFOPLIST_FILE = ThreadsExperiment/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 8.0;
IPHONEOS_DEPLOYMENT_TARGET = 7.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
};
name = Release;
};
@@ -9,52 +9,31 @@
import Foundation
import UIKit
public class GrayScottRenderer : NSOperation
{
private var grayScottData = NSMutableArray(capacity: Constants.LENGTH_SQUARED);
private var grayScottImage : UIImage?;
func renderGrayScott(grayScottData:[GrayScottStruct])->UIImage
{
let startTime : CFAbsoluteTime = CFAbsoluteTimeGetCurrent();
init(grayScottData : NSMutableArray)
{
super.init();
self.setGrayScott(grayScottData);
}
UIGraphicsBeginImageContextWithOptions(CGSize(width: Constants.LENGTH, height: Constants.LENGTH), true, 1);
let context = UIGraphicsGetCurrentContext();
override public func main() -> ()
for i in 0 ..< Constants.LENGTH
{
let startTime : CFAbsoluteTime = CFAbsoluteTimeGetCurrent();
UIGraphicsBeginImageContextWithOptions(CGSize(width: Constants.LENGTH, height: Constants.LENGTH), true, 1);
let context = UIGraphicsGetCurrentContext();
for i in 0 ..< Constants.LENGTH
for j in 0 ..< Constants.LENGTH
{
for j in 0 ..< Constants.LENGTH
{
let grayScottCell : GrayScottStruct = grayScottData[i * Constants.LENGTH + j] as GrayScottStruct;
CGContextSetRGBFillColor (context, CGFloat(grayScottCell.u), CGFloat(grayScottCell.u), CGFloat(grayScottCell.v), 1);
CGContextFillRect (context, CGRectMake (CGFloat(i), CGFloat(j), 1, 1));
}
let grayScottCell : GrayScottStruct = grayScottData[i * Constants.LENGTH + j] as GrayScottStruct;
CGContextSetRGBFillColor (context, CGFloat(grayScottCell.u), CGFloat(grayScottCell.u), CGFloat(grayScottCell.v), 1);
CGContextFillRect (context, CGRectMake (CGFloat(i), CGFloat(j), 1, 1));
}
grayScottImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
println(" R RENDER:" + NSString(format: "%.4f", CFAbsoluteTimeGetCurrent() - startTime));
}
private func setGrayScott(value : NSMutableArray)
{
grayScottData = value;
}
let outputImage = UIGraphicsGetImageFromCurrentImageContext();
public func getGrayScottImage() -> UIImage?
{
return grayScottImage;
}
}
UIGraphicsEndImageContext();
println(" R RENDER:" + NSString(format: "%.4f", CFAbsoluteTimeGetCurrent() - startTime));
return outputImage
}
@@ -13,80 +13,47 @@
import Foundation
import UIKit
import CoreImage
public class GrayScottSolver : NSOperation
{
private var grayScottData = NSMutableArray(capacity: Constants.LENGTH_SQUARED);
private var f : Double?;
private var k : Double?;
private var dU : Double?;
private var dV : Double?;
init(grayScottData : NSMutableArray)
{
super.init();
self.setGrayScott(grayScottData);
}
struct GrayScottParmeters {
var f : Double
var k : Double
var dU : Double
var dV : Double
}
func grayScottSolver(grayScottConstData: [GrayScottStruct], parameters:GrayScottParmeters)->[GrayScottStruct] {
let startTime : CFAbsoluteTime = CFAbsoluteTimeGetCurrent();
override public func main() -> ()
var index : Int = 0;
var outputArray = grayScottConstData // Copy to get array big enough
for i in 0 ..< Constants.LENGTH
{
let startTime : CFAbsoluteTime = CFAbsoluteTimeGetCurrent();
let grayScottConstData = grayScottData;
var outputArray = NSMutableArray(capacity: Constants.LENGTH_SQUARED);
var index : Int = 0;
for i in 0 ..< Constants.LENGTH
for j in 0 ..< Constants.LENGTH
{
for j in 0 ..< Constants.LENGTH
{
let thisPixel : GrayScottStruct! = grayScottConstData[i * Constants.LENGTH + j] as GrayScottStruct;
let northPixel : GrayScottStruct! = grayScottConstData[i * Constants.LENGTH + (j + 1).wrap(Constants.LENGTH_MINUS_ONE)] as GrayScottStruct;
let southPixel : GrayScottStruct! = grayScottConstData[i * Constants.LENGTH + (j - 1).wrap(Constants.LENGTH_MINUS_ONE)] as GrayScottStruct;
let eastPixel : GrayScottStruct! = grayScottConstData[(i - 1).wrap(Constants.LENGTH_MINUS_ONE) * Constants.LENGTH + j] as GrayScottStruct;
let westPixel : GrayScottStruct! = grayScottConstData[(i + 1).wrap(Constants.LENGTH_MINUS_ONE) * Constants.LENGTH + j] as GrayScottStruct;
let laplacianU = northPixel.u + southPixel.u + westPixel.u + eastPixel.u - (4.0 * thisPixel.u);
let laplacianV = northPixel.v + southPixel.v + westPixel.v + eastPixel.v - (4.0 * thisPixel.v);
let reactionRate = thisPixel.u * thisPixel.v * thisPixel.v;
let deltaU : Double = dU! * laplacianU - reactionRate + f! * (1.0 - thisPixel.u);
let deltaV : Double = dV! * laplacianV + reactionRate - k! * thisPixel.v;
let outputPixel = GrayScottStruct(u: (thisPixel.u + deltaU).clip(), v: (thisPixel.v + deltaV).clip());
// setting values by subscripting is about 15% faster than append()!
//outputArray.append(outputPixel);
outputArray[index++] = outputPixel;
}
let thisPixel = grayScottConstData[i * Constants.LENGTH + j]
let northPixel = grayScottConstData[i * Constants.LENGTH + (j + 1).wrap(Constants.LENGTH_MINUS_ONE)]
let southPixel = grayScottConstData[i * Constants.LENGTH + (j - 1).wrap(Constants.LENGTH_MINUS_ONE)]
let eastPixel = grayScottConstData[(i - 1).wrap(Constants.LENGTH_MINUS_ONE) * Constants.LENGTH + j]
let westPixel = grayScottConstData[(i + 1).wrap(Constants.LENGTH_MINUS_ONE) * Constants.LENGTH + j]
let laplacianU = northPixel.u + southPixel.u + westPixel.u + eastPixel.u - (4.0 * thisPixel.u);
let laplacianV = northPixel.v + southPixel.v + westPixel.v + eastPixel.v - (4.0 * thisPixel.v);
let reactionRate = thisPixel.u * thisPixel.v * thisPixel.v;
let deltaU : Double = parameters.dU * laplacianU - reactionRate + parameters.f * (1.0 - thisPixel.u);
let deltaV : Double = parameters.dV * laplacianV + reactionRate - parameters.k * thisPixel.v;
let outputPixel = GrayScottStruct(u: (thisPixel.u + deltaU).clip(), v: (thisPixel.v + deltaV).clip())
//outputArray.append(outputPixel)
outputArray[index++] = outputPixel;
}
grayScottData = outputArray;
println("S SOLVER:" + NSString(format: "%.4f", CFAbsoluteTimeGetCurrent() - startTime));
}
// Double is faster that Double...
public func setParameterValues(#f: Double, k : Double, dU : Double, dV : Double)
{
self.f = f;
self.k = k;
self.dU = dU;
self.dV = dV;
}
println("S SOLVER:" + NSString(format: "%.4f", CFAbsoluteTimeGetCurrent() - startTime));
private func setGrayScott(value : NSMutableArray)
{
grayScottData = value;
}
public func getGrayScott() -> NSMutableArray
{
return grayScottData;
}
}
return outputArray
}
@@ -8,8 +8,7 @@
import Foundation
class GrayScottStruct : GrayScottProtocol
{
struct GrayScottStruct {
var u : Double = 0.0;
var v : Double = 0.0;
@@ -7,7 +7,6 @@
//
import UIKit
import SpriteKit
class ViewController: UIViewController
{
@@ -17,77 +16,45 @@ class ViewController: UIViewController
@IBOutlet var parameterButtonBar: UISegmentedControl!
@IBOutlet var parameterValueLabel: UILabel!
let queue = NSOperationQueue();
var solver : GrayScottSolver?;
var renderer : GrayScottRenderer?;
var f : Double = 0.023;
var k : Double = 0.0795;
var dU : Double = 0.16;
var dV : Double = 0.08;
var grayScottData : NSMutableArray = NSMutableArray(capacity: Constants.LENGTH_SQUARED);
override func viewDidLoad()
{
for i in 0..<Constants.LENGTH_SQUARED
{
grayScottData[i] = GrayScottStruct(u:1.0, v:0.0);
}
for i in 25 ..< 45
{
for j in 25 ..< 45
var grayScottData:[GrayScottStruct] = {
var data = [GrayScottStruct]()
for i in 0..<Constants.LENGTH_SQUARED
{
if arc4random() % 100 > 5
data.append(GrayScottStruct(u:1.0, v:0.0))
}
for i in 25 ..< 45
{
for j in 25 ..< 45
{
grayScottData[i * Constants.LENGTH + j] = GrayScottStruct(u: 0.5, v: 0.25);
if arc4random() % 100 > 5
{
data[i * Constants.LENGTH + j] = GrayScottStruct(u: 0.5, v: 0.25);
}
}
}
}
return data
}()
override func viewDidLoad()
{
let timer = NSTimer.scheduledTimerWithTimeInterval(0.025, target: self, selector: Selector("timerHandler"), userInfo: nil, repeats: true);
updateLabel();
dispatchSolverOperation();
dispatchSolverOperation()
}
func timerHandler()
{
if solver!.finished
{
grayScottData = solver!.getGrayScott();
if let tmp = renderer
{
if !tmp.executing
{
dispatchRenderOperation();
}
}
else
{
dispatchRenderOperation();
}
dispatchSolverOperation();
}
if let tmp = renderer
{
if(tmp.finished)
{
renderGrayScott();
}
}
}
func dispatchRenderOperation()
{
renderer = GrayScottRenderer(grayScottData: grayScottData);
renderer!.threadPriority = 0;
queue.addOperation(renderer);
//self.dispatchSolverOperation()
}
@IBAction func sliderValueChangeHandler(sender: AnyObject)
@@ -136,23 +103,20 @@ class ViewController: UIViewController
}
}
private func dispatchSolverOperation()
{
solver = GrayScottSolver(grayScottData: grayScottData);
solver!.setParameterValues(f: f, k: k, dU: dU, dV: dV)
solver!.threadPriority = 0;
// this doesn't work because the completion block isn't executed in the main thread
//solver.completionBlock = {self.didSolve(self.solver.getGrayScott())};
queue.addOperation(solver);
}
private func renderGrayScott()
private func dispatchSolverOperation()
{
imageView.image = renderer!.getGrayScottImage();
let dataCopy = grayScottData
let params = GrayScottParmeters(f: f, k: k, dU: dU, dV: dV)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)) {
let newGSData = grayScottSolver(dataCopy, params)
let newImage = renderGrayScott(newGSData)
dispatch_async(dispatch_get_main_queue()) {
self.grayScottData = newGSData
self.imageView.image = newImage
self.dispatchSolverOperation()
}
}
}
}
ProTip! Use n and p to navigate between commits in a pull request.