# Untitled

a guest
Aug 24th, 2014
302
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #import "GameScene.h"
2.
3. #include <math.h>
4. #include <stdlib.h>
5. #include <stdio.h>
6.
7. // smallpt, a Path Tracer by Kevin Beason, 2008
8. //edited by Blagovest Taskov
9. namespace /*small pt*/ {
10.     struct Color {
11.         float x, y, z, a;
12.         Color(float x_=0, float y_=0, float z_=0){ x=x_; y=y_; z=z_; a=1.f ;}
13.         Color operator*(float b) const { return Color(x*b,y*b,z*b); }
14.         Color operator+(const Color &b) const { return Color(x+b.x,y+b.y,z+b.z); }
15.     };
16.
17.     struct Vec {        // Usage: time ./smallpt 5000 && xv image.ppm
18.         double x, y, z;                  // position, also color (r,g,b)
19.         Vec(double x_=0, double y_=0, double z_=0){ x=x_; y=y_; z=z_; }
20.         Vec operator+(const Vec &b) const { return Vec(x+b.x,y+b.y,z+b.z); }
21.         Vec operator-(const Vec &b) const { return Vec(x-b.x,y-b.y,z-b.z); }
22.         Vec operator*(double b) const { return Vec(x*b,y*b,z*b); }
23.         Vec mult(const Vec &b) const { return Vec(x*b.x,y*b.y,z*b.z); }
24.         Vec& norm(){ return *this = *this * (1/sqrt(x*x+y*y+z*z)); }
25.         double dot(const Vec &b) const { return x*b.x+y*b.y+z*b.z; } // cross:
26.         Vec operator%(Vec&b){return Vec(y*b.z-z*b.y,z*b.x-x*b.z,x*b.y-y*b.x);}
27.     };
28.     struct Ray { Vec o, d; Ray(Vec o_, Vec d_) : o(o_), d(d_) {} };
29.     enum Refl_t { DIFF, SPEC, REFR };  // material types, used in radiance()
30.     struct Sphere {
32.         Vec p, e, c;      // position, emission, color
33.         Refl_t refl;      // reflection type (DIFFuse, SPECular, REFRactive)
34.         Sphere(double rad_, Vec p_, Vec e_, Vec c_, Refl_t refl_):
35.         rad(rad_), p(p_), e(e_), c(c_), refl(refl_) {}
36.         double intersect(const Ray &r) const { // returns distance, 0 if nohit
37.             Vec op = p-r.o; // Solve t^2*d.d + 2*t*(o-p).d + (o-p).(o-p)-R^2 = 0
39.             if (det<0) return 0; else det=sqrt(det);
40.             return (t=b-det)>eps ? t : ((t=b+det)>eps ? t : 0);
41.         }
42.     };
43.     Sphere spheres[] = {//Scene: radius, position, emission, color, material
44.         Sphere(1e5, Vec( 1e5+1,40.8,81.6), Vec(),Vec(.75,.25,.25),DIFF),//Left
45.         Sphere(1e5, Vec(-1e5+99,40.8,81.6),Vec(),Vec(.25,.25,.75),DIFF),//Rght
46.         Sphere(1e5, Vec(50,40.8, 1e5),     Vec(),Vec(.75,.75,.75),DIFF),//Back
47.         Sphere(1e5, Vec(50,40.8,-1e5+170), Vec(),Vec(),           DIFF),//Frnt
48.         Sphere(1e5, Vec(50, 1e5, 81.6),    Vec(),Vec(.75,.75,.75),DIFF),//Botm
49.         Sphere(1e5, Vec(50,-1e5+81.6,81.6),Vec(),Vec(.75,.75,.75),DIFF),//Top
50.         Sphere(16.5,Vec(27,16.5,47),       Vec(),Vec(1,1,1)*.999, SPEC),//Mirr
51.         Sphere(16.5,Vec(73,16.5,78),       Vec(),Vec(1,1,1)*.999, REFR),//Glas
52.         Sphere(600, Vec(50,681.6-.27,81.6),Vec(12,12,12),  Vec(), DIFF) //Lite
53.     };
54.     inline double clamp(double x){ return x<0 ? 0 : x>1 ? 1 : x; }
55.     inline int toInt(double x){ return int(pow(clamp(x),1/2.2)*255+.5); }
56.     inline bool intersect(const Ray &r, double &t, int &id){
57.         double n=sizeof(spheres)/sizeof(Sphere), d, inf=t=1e20;
58.         for(int i=int(n);i--;) if((d=spheres[i].intersect(r))&&d<t){t=d;id=i;}
59.         return t<inf;
60.     }
61.     Vec radiance(const Ray &r, int depth, unsigned short *Xi){
62.         double t;                               // distance to intersection
63.         int id=0;                               // id of intersected object
64.         if (!intersect(r, t, id)) return Vec(); // if miss, return black
65.         const Sphere &obj = spheres[id];        // the hit object
66.         Vec x=r.o+r.d*t, n=(x-obj.p).norm(), nl=n.dot(r.d)<0?n:n*-1, f=obj.c;
67.         double p = f.x>f.y && f.x>f.z ? f.x : f.y>f.z ? f.y : f.z; // max refl
68.         if (++depth>5) if (erand48(Xi)<p) f=f*(1/p); else return obj.e; //R.R.
69.         if (obj.refl == DIFF){                  // Ideal DIFFUSE reflection
70.             double r1=2*M_PI*erand48(Xi), r2=erand48(Xi), r2s=sqrt(r2);
71.             Vec w=nl, u=((fabs(w.x)>.1?Vec(0,1):Vec(1))%w).norm(), v=w%u;
72.             Vec d = (u*cos(r1)*r2s + v*sin(r1)*r2s + w*sqrt(1-r2)).norm();
73.             return obj.e + f.mult(radiance(Ray(x,d),depth,Xi));
74.         } else if (obj.refl == SPEC)            // Ideal SPECULAR reflection
75.             return obj.e + f.mult(radiance(Ray(x,r.d-n*2*n.dot(r.d)),depth,Xi));
76.         Ray reflRay(x, r.d-n*2*n.dot(r.d));     // Ideal dielectric REFRACTION
77.         bool into = n.dot(nl)>0;                // Ray from outside going in?
78.         double nc=1, nt=1.5, nnt=into?nc/nt:nt/nc, ddn=r.d.dot(nl), cos2t;
79.         if ((cos2t=1-nnt*nnt*(1-ddn*ddn))<0)    // Total internal reflection
80.             return obj.e + f.mult(radiance(reflRay,depth,Xi));
81.         Vec tdir = (r.d*nnt - n*((into?1:-1)*(ddn*nnt+sqrt(cos2t)))).norm();
82.         double a=nt-nc, b=nt+nc, R0=a*a/(b*b), c = 1-(into?-ddn:tdir.dot(n));
83.         double Re=R0+(1-R0)*c*c*c*c*c,Tr=1-Re,P=.25+.5*Re,RP=Re/P,TP=Tr/(1-P);
84.         return obj.e + f.mult(depth>2 ? (erand48(Xi)<P ?   // Russian roulette
87.     }
88. }
89.
90. //returns used time in seconds
91. double raytrace() {
92.     int w=768, h=1024, samps = 1; // # samples
93.     Ray cam(Vec(50,52,295.6), Vec(0,-0.042612,-1).norm()); // cam pos, dir
94.     Vec cx=Vec(w*.5135/h), cy=(cx%cam.d).norm()*.5135, r;
95.     Color *c=new Color[w*h];
96.
97.
98.     NSTimeInterval date = [NSDate date].timeIntervalSince1970;
99.
100.     for (int y=0; y<h; y++){                       // Loop over image rows
101.         for (unsigned short x=0, Xi[3]={0,0,static_cast<unsigned short>(y*y*y)}; x<w; x++)   // Loop cols
102.             for (int sy=0, i=(h-y-1)*w+x; sy<2; sy++)     // 2x2 subpixel rows
103.                 for (int sx=0; sx<2; sx++, r=Vec()){        // 2x2 subpixel cols
104.                     for (int s=0; s<samps; s++){
105.                         double r1=2*erand48(Xi), dx=r1<1 ? sqrt(r1)-1: 1-sqrt(2-r1);
106.                         double r2=2*erand48(Xi), dy=r2<1 ? sqrt(r2)-1: 1-sqrt(2-r2);
107.                         Vec d = cx*( ( (sx+.5 + dx)/2 + x)/w - .5) +
108.                         cy*( ( (sy+.5 + dy)/2 + y)/h - .5) + cam.d;
109.                         r = r + radiance(Ray(cam.o+d*140,d.norm()),0,Xi)*(1./samps);
110.                     } // Camera rays are pushed ^^^^^ forward to start in interior
111.                     c[i] = c[i] + Color(clamp(r.x),clamp(r.y),clamp(r.z))*.25;
112.                 }
113.     }
114.
115.     double result = [NSDate date].timeIntervalSince1970 - date;
116.
117.
118.     delete[] c;
119.     return result;
120. }
121.
122.
123. namespace /*naive quick sort*/ {
124.     // The partition function
125.     int partition(int* input, int p, int r)
126.     {
127.         int pivot = input[r];
128.
129.         while ( p < r )
130.         {
131.             while ( input[p] < pivot )
132.                 p++;
133.
134.             while ( input[r] > pivot )
135.                 r--;
136.
137.             if ( input[p] == input[r] )
138.                 p++;
139.             else if ( p < r )
140.             {
141.                 int tmp = input[p];
142.                 input[p] = input[r];
143.                 input[r] = tmp;
144.             }
145.         }
146.
147.         return r;
148.     }
149.
150.     // The quicksort recursive function
151.     void quicksort(int* input, int p, int r)
152.     {
153.         if ( p < r )
154.         {
155.             int j = partition(input, p, r);
156.             quicksort(input, p, j-1);
157.             quicksort(input, j+1, r);
158.         }
159.     }
160.
161. }
162.
163. //returns used time in seconds
164. double sort () {
165.     const int n = 42000000;
166.
167.     int* arr = new int[n];
168.
169.     for (int i =0; i < n; ++i) {
170.         arr[i] = rand() % (1 << 31);
171.     }
172.
173.     NSTimeInterval seconds = [NSDate date].timeIntervalSince1970;
174.
175.     quicksort(arr, 0, n-1);
176.
177.     double result = [NSDate date].timeIntervalSince1970 - seconds;
178.
179.     delete[] arr;
180.     return result;
181. }
182.
183. //returns used time in seconds
184. double mem() {
185.     const int n = 96000000;
186.     int* arr = new int[n];
187.     NSTimeInterval seconds = [NSDate date].timeIntervalSince1970;
188.
189.     for (int i = 0; i < n; ++i) {
190.         const int i0 = rand() % n;
191.         const int i1 = rand() % n;
192.         const int t = arr[i0];
193.         arr[i0] = arr[i1];
194.         arr[i1] = t;
195.     }
196.     double result = [NSDate date].timeIntervalSince1970 - seconds;
197.
198.     delete[] arr;
199.     return result;
200. }
201.
202. //returns used time in seconds
203. double sfu() {
204.     int di = rand() % 2;
205.     if (di > 0)
206.         di = 0;
207.     NSTimeInterval seconds = [NSDate date].timeIntervalSince1970;
208.
209.     float t = 0.0;
210.     for (int i = 0; i < (12000000 + di); ++i) {
211.         t += sinf(i * 2.718f);
212.         t -= cosf(cosf(i / 3.1415926f));
213.         const float acosValue = MAX(-0.5f, MIN(0.5f, t*cosf(i) + 0.995f));
214.         t = t * 0.196f + acos(acosValue);
215.         if (fabs(t) > 1e-5f) {
216.             t /=t;
217.         }
218.     }
219.     if (t == 196.0f) {
220.         return 0.f;
221.     }
222.
223.     double result = [NSDate date].timeIntervalSince1970 - seconds;
224.     return result;
225. }
226.
227. @implementation GameScene
228.
229. -(void)didMoveToView:(SKView *)view {
230.
231.     double test0 = raytrace();
232.     double test1 = sort();
233.     double test2 = mem();
234.     double test3 = sfu();
235.
236. #ifdef TARGET_OS_IPHONE
237.     [[[UIAlertView alloc] initWithTitle:@"Time" message:[NSString stringWithFormat:@"Raytace : %fs\nSort: %fs\nRand mem access: %fs\nFPU %fs", test0, test1,test2,test3] delegate:self cancelButtonTitle:@"Okay" otherButtonTitles:nil] show];
238. #else
239.     NSString* string =[NSString stringWithFormat:@"Raytace : %fs\nSort: %fs\nRand mem access: %fs\nFPU %fs", test0, test1,test2,test3];