<p>I have implemented a ray tracer using reinforcement learning. But I see some problems.
I tried to divide the planes into rectangular grids. I searched the moire effect and realized that I can solve my problem by adding noise ( for example <span class="math-container">$\epsilon = 1e-5$</span>) to the hit points on the diffuse surfaces, but even after doing this, I haven't seen any change. I created grid using ceiling function.</p>

<p>It seems that I shouldn't use the ceiling function (to understand which cell current hit point is in) and use another algorithm to find in which rectangular cells, the hit points are. Could someone tell me which algorithm to use?</p>

<p><a href="https://i.sstatic.net/1xPBD.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/1xPBD.png" alt="enter image description here"></a></p>

<p>EDIT 2: I sample primary rays randomly in each pixel without jittering. Is it the cause of moire effect? Do I have to use stratified sampling?</p>

<p>EDIT 3: I have to show some parts of code:
This function generates a ray inside patches on sphere:</p>

<pre><code>function Q_dir(ith::Int64)
    i = ceil(Int64, ith/H)
    j = ith - H*(i-1)
    u = (i-rand())/V
    ϕ = 2π*(j-rand())/H
    return Vec3(cos(ϕ)*sqrt(1.0 - u*u), sin(ϕ)*sqrt(1.0 - u*u), u)
end
</code></pre>

<p>This function returns the index of a rectangular cell on each plane which contains the hit point:</p>

<pre><code>function find_state(a::Float64, a1::Float64, a2::Float64, b::Float64, b1::Float64, b2::Float64)
    return [ceil(Int64, M*(abs(a)/(a2-a1))),   ceil(Int64, N*(abs(b)/(b2-b1)))]
end
</code></pre>

<p>Ant this intersection function, returns the hit point and the cell</p>

<pre><code>function intersect!(plane::xzPlane, ray::Ray)
    x1 = plane.a1
    x2 = plane.a2
    z1 = plane.b1
    z2 = plane.b2
    t = (plane.y - ray.org.y)/ray.dir.y
    point = ray.org + t*ray.dir
    if x1 &lt; point.x &lt; x2 &amp;&amp; z1 &lt; point.z &lt; z2 &amp;&amp; 0 &lt; t
        hsect, wsect = find_state(point.x, x1, x2, point.z, z1, z2)
        normal = Vec3(0,1,0)
        return HitPoint(t, point , normal, hsect, wsect)
    else
        return HitPoint()
    end
end
</code></pre>

<p>The problem is surely related to this part, because I can reproduce the results of the paper. My only problem is this artifact.</p>

<p>EDIT 4: When I generate a ray uniformly through each uniformly selected stratum, everything works fine. But when I generate a ray uniformly through importance sampled stratum (each stratum has a different value), I encounter this problem. </p>

<p>EDIT 5: Sampling discretized hemisphere is surely part of the problem. I guess the Q_dir function is causing some problems. Here is another generated photo:
<a href="https://i.sstatic.net/vmYeF.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/vmYeF.png" alt="enter image description here"></a></p>

<p>H in Q-dir function is equal to 6. The number of half-lines generated on the floor equals 6. So this sampling method is incorrect. </p>

CC BY-SA 4.0
2019-11-10T12:54:42.557Z
Can you provide images? With highlights for the relevant parts?
lightxbulb
CC BY-SA 4.0
2019-11-12T14:42:05.510Z
I divided the planes using regular grids and I added noise to the points, but the problem hasn't been solved.
bitWise
CC BY-SA 4.0
2019-11-12T16:11:42.743Z
You simply have a bug in your sampling somewhere. These are not moire patterns.
lightxbulb
CC BY-SA 4.0
2019-11-12T16:39:41.743Z
No, it is moire effect. Just scale down the picture you even see some concentric semicircles.  I have generated some photos with different parameters and the semicircles are obvious.
bitWise
CC BY-SA 4.0
2019-11-12T16:55:09.813Z
I do not think it is. There's also nothing in your scene that should cause it, so I assume it's just broken sampling.
lightxbulb
CC BY-SA 4.0
2019-11-12T18:05:11.283Z
Sorry, I was wrong. I used stratified sampling, but nothing changed. So it's not moire effect. It is a bug. I should rewrite the code.
bitWise
<raytracing>
Strange behaviour when tracing rays through hemisphere patches
bitWise
# Strange behaviour when tracing rays through hemisphere patches

(Asked by: username_0 on 2019-11-10)

I have implemented a ray tracer using reinforcement learning. But I see some problems.
I tried to divide the planes into rectangular grids. I searched the moire effect and realized that I can solve my problem by adding noise ( for example $\epsilon = 1e-5$) to the hit points on the diffuse surfaces, but even after doing this, I haven't seen any change. I created grid using ceiling function.

It seems that I shouldn't use the ceiling function (to understand which cell current hit point is in) and use another algorithm to find in which rectangular cells, the hit points are. Could someone tell me which algorithm to use?

[![enter image description here](https://i.sstatic.net/1xPBD.png)](https://i.sstatic.net/1xPBD.png)

EDIT 2: I sample primary rays randomly in each pixel without jittering. Is it the cause of moire effect? Do I have to use stratified sampling?

EDIT 3: I have to show some parts of code:
This function generates a ray inside patches on sphere:

```
function Q_dir(ith::Int64)
    i = ceil(Int64, ith/H)
    j = ith - H*(i-1)
    u = (i-rand())/V
    ϕ = 2π*(j-rand())/H
    return Vec3(cos(ϕ)*sqrt(1.0 - u*u), sin(ϕ)*sqrt(1.0 - u*u), u)
end

```

This function returns the index of a rectangular cell on each plane which contains the hit point:

```
function find_state(a::Float64, a1::Float64, a2::Float64, b::Float64, b1::Float64, b2::Float64)
    return [ceil(Int64, M*(abs(a)/(a2-a1))),   ceil(Int64, N*(abs(b)/(b2-b1)))]
end

```

Ant this intersection function, returns the hit point and the cell

```
function intersect!(plane::xzPlane, ray::Ray)
    x1 = plane.a1
    x2 = plane.a2
    z1 = plane.b1
    z2 = plane.b2
    t = (plane.y - ray.org.y)/ray.dir.y
    point = ray.org + t*ray.dir
    if x1 < point.x < x2 && z1 < point.z < z2 && 0 < t
        hsect, wsect = find_state(point.x, x1, x2, point.z, z1, z2)
        normal = Vec3(0,1,0)
        return HitPoint(t, point , normal, hsect, wsect)
    else
        return HitPoint()
    end
end

```

The problem is surely related to this part, because I can reproduce the results of the paper. My only problem is this artifact.

EDIT 4: When I generate a ray uniformly through each uniformly selected stratum, everything works fine. But when I generate a ray uniformly through importance sampled stratum (each stratum has a different value), I encounter this problem.

EDIT 5: Sampling discretized hemisphere is surely part of the problem. I guess the Q\_dir function is causing some problems. Here is another generated photo:
[![enter image description here](https://i.sstatic.net/vmYeF.png)](https://i.sstatic.net/vmYeF.png)

H in Q-dir function is equal to 6. The number of half-lines generated on the floor equals 6. So this sampling method is incorrect.

**Tags:** raytracing

> **Comments:**
> **username_1**: Can you provide images? With highlights for the relevant parts?  
> **username_0**: I divided the planes using regular grids and I added noise to the points, but the problem hasn't been solved.  
> **username_1**: You simply have a bug in your sampling somewhere. These are not moire patterns.  
> **username_0**: No, it is moire effect. Just scale down the picture you even see some concentric semicircles.  I have generated some photos with different parameters and the semicircles are obvious.  
> **username_1**: I do not think it is. There's also nothing in your scene that should cause it, so I assume it's just broken sampling.  
> **username_0**: Sorry, I was wrong. I used stratified sampling, but nothing changed. So it's not moire effect. It is a bug. I should rewrite the code.  

---

## 0 answers

