Advertisement
Guest User

Raf's advice

a guest
May 5th, 2019
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Julia 5.90 KB | None | 0 0
  1. ##rewrite of Tree of Objects approach
  2. using Images, ImageView, Gtk.ShortNames, Colors, FixedPointNumbers, StaticArrays, LinearAlgebra, ColorVectorSpace, BenchmarkTools, Profile, StatProfilerHTML
  3.  
  4. N = 0
  5. Nm = Float64
  6. Nic = Normed{UInt8,8}
  7. Ct = RGB{Nic}
  8.  
  9. abstract type Shader end
  10. abstract type Sdf end
  11.  
  12.  
  13. #=
  14. struct RayInfo ## making RayInfo parametric ends up with worse performance
  15.    pos::SVector{3,Nm}
  16.    dir::SVector{3,Nm}
  17.    t_Sdf::Sdf
  18.    step::Nm
  19.    initial::SVector{3,Nm}
  20.    length::Nm
  21.    render_lim::Nm
  22. end
  23. =#
  24.  
  25. struct RayInfo{G} ## making RayInfo parametric ends up with worse performance
  26.    pos::SVector{3,Nm}
  27.    dir::SVector{3,Nm}
  28.    t_Sdf::G
  29.    step::Nm
  30.    initial::SVector{3,Nm}
  31.    length::Nm
  32.    render_lim::Nm
  33. end
  34.  
  35. function gradient(Sdf::Sdf, Ri::RayInfo)
  36.   local Step = Ri.step
  37.   normalize([(dist(Sdf,Ri.pos+SVector{3,Nm}(Step,0.0,0.0)) - dist(Sdf,Ri.pos))/Step,
  38.              (dist(Sdf,Ri.pos+SVector{3,Nm}(0.0,Step,0.0)) - dist(Sdf,Ri.pos))/Step,
  39.              (dist(Sdf,Ri.pos+SVector{3,Nm}(0.0,0.0,Step)) - dist(Sdf,Ri.pos))/Step])
  40. end
  41.  
  42. struct ConstantShader <: Shader
  43.   color::Ct
  44. end
  45.  
  46. function shade(Shadr::ConstantShader,Ri::RayInfo)
  47.   Shadr.color
  48. end
  49.  
  50. struct Checkbox{S1,S2} <: Shader
  51.   box_size::Nm
  52.   shd1::S1
  53.   shd2::S2
  54. end
  55.  
  56. function shade(Shadr::Checkbox,Ri::RayInfo)
  57.   x,y,z = Ri.pos
  58.   Bs = Shadr.box_size
  59.   if xor(mod(x,2*Bs)>Bs,mod(y,2*Bs)>Bs,mod(z,2*Bs)>Bs)
  60.     shade(Shadr.shd1,Ri)
  61.   else
  62.     shade(Shadr.shd2,Ri)
  63.   end
  64. end
  65.  
  66. struct Spot{S1} <: Shader
  67.   light_pos::SVector{3,Nm}
  68.   brightness::Nm
  69.   shd1::S1
  70. end
  71.  
  72. function shade(Shadr::Spot,Ri::RayInfo)
  73.   shade(Shadr.shd1,Ri)*Nic(min(Nm(1.0),max(Nm(0.1),Shadr.brightness*dot(gradient(Ri.t_Sdf,Ri),normalize(Shadr.light_pos-Ri.pos)))))
  74. end
  75.  
  76. struct Conjun <: Sdf
  77.   coll::Tuple
  78. end
  79.  
  80. function dist(Sdf::Conjun, Position::SVector{3,Nm})
  81.   min(map(x->dist(x,Position),Sdf.coll)...)
  82. end
  83.  
  84. function color(Sdf::Conjun, Ri::RayInfo)
  85.   color(Sdf.coll[argmin(map(x->dist(x,Ri.pos),Sdf.coll))],Ri)
  86. end
  87.  
  88.  
  89.  
  90. struct Transl{S1} <: Sdf
  91.   off::SVector{3,Nm}
  92.   vict::S1
  93. end
  94.  
  95. function dist(Sdf::Transl, Position::SVector{3,Nm})
  96.   dist(Sdf.vict,Position-Sdf.off)
  97. end
  98.  
  99. function color(Sdf::Transl, Ri::RayInfo)
  100.   color(Sdf.vict,RayInfo(Ri.pos-Sdf.off,Ri.dir,Ri.t_Sdf,Ri.step,Ri.initial-Sdf.off,Ri.length,Ri.render_lim))
  101. end
  102.  
  103. abstract type SSdf <: Sdf end
  104.  
  105. struct Sphere{S1} <: SSdf
  106.    Radius::Nm
  107.    shadr::S1
  108. end
  109.  
  110. function dist(Sph::Sphere, Position::SVector{3,Nm})
  111.    norm(Position)-Sph.Radius
  112. end
  113.  
  114. function color(Sph::Sphere, Ri::RayInfo)
  115.   shade(Sph.shadr,Ri)
  116. end
  117.  
  118. struct Plane{S1} <: SSdf
  119.    normal::SVector{3,Nm} ##Check for normality
  120.    shadr::S1
  121. end
  122.  
  123. function dist(Pln::Plane, Position::SVector{3,Nm})
  124.    norm(dot(Position,Pln.normal))
  125. end
  126.  
  127. function color(Pln::Plane, Ri::RayInfo)
  128.   shade(Pln.shadr,Ri)
  129. end
  130.  
  131. const Red   = ConstantShader(Ct(1,0,0))
  132. const Green = ConstantShader(Ct(0,1,0))
  133. const Blue  = ConstantShader(Ct(0,0,1))
  134. const Gelb  = ConstantShader(Ct(1,1,0))
  135. const Black = ConstantShader(Ct(0,0,0))
  136. const White = ConstantShader(Ct(1,1,1))
  137. const Grey = ConstantShader(Ct(0.5,0.5,0.5))
  138.  
  139. const scene = Conjun((
  140.    Transl(SVector{3,Nm}(0,20,0),Sphere(Nm(3.0),Spot(SVector{3,Nm}(0,0,0),Nm(1.5),Red))),
  141.    Transl(SVector{3,Nm}(12,9,-4),Sphere(Nm(12.0),Spot(SVector{3,Nm}(0,0,0),Nm(1.8),Blue))),
  142.    Transl(SVector{3,Nm}(3,-13,0),Sphere(Nm(9.0),Spot(SVector{3,Nm}(0,0,0),Nm(1.0),Green))),
  143.    Sphere(Nm(1.0),White),
  144.    Transl(SVector{3,Nm}(-20,0,0),Sphere(Nm(0.5),White)),
  145.    Transl(SVector{3,Nm}(-18,0,0),Sphere(Nm(1.0),Red)),
  146.    Plane(SVector{3,Nm}(0.0,0.0,1.0),Checkbox(Nm(10),Grey,Black))
  147. ))
  148.  
  149. function raymarch(Pos::SVector{3,Nm},Normal::SVector{3,Nm},Gf::Sdf,Fb::Shader,renderlim::Nm)
  150. stepsize=Nm(1.0);
  151. Start=Pos;
  152. Strecke=Nm(0.0);
  153.  while (stepsize > Nm(0.001))
  154.    global N += 1
  155.    stepsize=dist(Gf,Pos);
  156.    Pos+=Normal*stepsize;
  157.    Strecke+=stepsize
  158.    if Strecke > renderlim
  159.      return shade(Fb,RayInfo(Pos,Normal,Gf,stepsize,Start,Strecke,renderlim-Strecke))
  160.    end
  161.  end
  162.    color(Gf,RayInfo(Pos,Normal,Gf,stepsize,Start,Strecke,renderlim-Strecke))
  163. end
  164.  
  165. function ray_source(x::Real,y::Real,Pos::SVector{3,Nm},Direction::SVector{3,Nm},
  166.         maxx::Int64,maxy::Int64,Sensorxv::SVector{3,Nm},Sensoryv::SVector{3,Nm})
  167.   (SVector{3,Nm}(Pos+(maxx/2-x)/maxx*Sensorxv+(maxy/2-y)/maxy*Sensoryv),SVector{3,Nm}(normalize(Direction)))
  168. end
  169.  
  170. const x = 800
  171. const y = 800
  172.  
  173. Pic  = Array{Ct}(undef,x,y)
  174.  
  175. const CamPosition = SVector{3,Nm}(-10.0,10.0,20.0)
  176. const Normal = SVector{3,Nm}(10.0,-10.0,-20.0)
  177. const SclX = Nm(60.0)
  178. const SclY = Nm(60.0)
  179.  
  180. const Ortho1 = SVector{3,Nm}(  0.0, -0.8944271909999159, 0.4472135954999579)
  181. const Ortho2 = SVector{3,Nm}( 0.9128709291752769, 0.18257418583505536, 0.3651483716701107 )
  182.  
  183. #=
  184. @profile for i=1:x
  185.   for j=1:y
  186.      lpos, lnormal = ray_source(Nm(i),Nm(j),CamPosition,Normal,x,y,SclX * Ortho1,SclY * Ortho2)
  187.      Pic[i,j]=raymarch(lpos,lnormal,scene,Black,Nm(500.0))
  188.   end
  189. end
  190. statprofilehtml()
  191. =#
  192.  
  193.  
  194. #=
  195. @btime for i=1:x
  196.   for j=1:y
  197.      lpos, lnormal = ray_source(Nm(i),Nm(j),CamPosition,Normal,x,y,SclX * Ortho1,SclY * Ortho2)
  198.      Pic[i,j]=raymarch(lpos,lnormal,scene,Black,Nm(500.0))
  199.   end
  200. end
  201. =#
  202.  
  203.  
  204. ##     12.352 s (173535725 allocations: 4.96 GiB) with RayInfo not parametric
  205. ##     14.537 s (213811941 allocations: 7.19 GiB) with Sdf parametric in RayInfo
  206.  
  207. for i=1:x
  208.   for j=1:y
  209.      lpos, lnormal = ray_source(Nm(i),Nm(j),CamPosition,Normal,x,y,SclX * Ortho1,SclY * Ortho2)
  210.      Pic[i,j]=raymarch(lpos,lnormal,scene,Black,Nm(500.0))
  211.   end
  212. end
  213. println(N)
  214. #=
  215. guidict = imshow(Pic);
  216.  
  217. if (!isinteractive())
  218.  
  219.     # Create a condition object
  220.     c = Condition()
  221.  
  222.     # Get the window
  223.     win = guidict["gui"]["window"]
  224.  
  225.     # Notify the condition object when the window closes
  226.     signal_connect(win, :destroy) do widget
  227.         notify(c)
  228.     end
  229.  
  230.     # Wait for the notification before proceeding ...
  231.     wait(c)
  232. end
  233. =#
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement