Skip to content

Commit

Permalink
Single Thread version tests successfully
Browse files Browse the repository at this point in the history
  • Loading branch information
legendPerceptor committed Mar 19, 2021
1 parent 1a0b49e commit 15361e3
Show file tree
Hide file tree
Showing 19 changed files with 566 additions and 58 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea/
.DS_Store
data/.DS_Store
data/.ipynb_checkpoints/
Binary file added data/test-cube.npy
Binary file not shown.
2 changes: 1 addition & 1 deletion data/test.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
go -fr "6 12 5" -at "0 0 0" -up "0 0 1" -nc -2.3 -fc 2.3 -fov 20 -sz "320 280" -id cube.npy -im cube-meta.npy -fov 14 -us 0.03 -s 0.01 -k ctmr -p rgbalit -b over -lit "1 1 1 -1 1 2 1" -dcn "1.1 1.1 1.1" -dcf "0.4 0.4 0.4" -nt 4 -lutd cube-luta.npy -od cube-lut.npy -om cube-lut-meta.npy
go -fr "6 12 5" -at "0 0 0" -up "0 0 1" -nc -2.3 -fc 2.3 -fov 20 -sz "320 280" -id cube.npy -im cube-meta.npy -fov 14 -us 0.03 -s 0.01 -k ctmr -p rgbalit -b over -lit "1 1 1 -1 1 2 1" -dcn "1.1 1.1 1.1" -dcf "0.4 0.4 0.4" -lutd cube-luta.npy -od cube-lut.png
67 changes: 55 additions & 12 deletions data/volume.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "french-nerve",
"execution_count": 2,
"id": "individual-chosen",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -12,8 +12,8 @@
},
{
"cell_type": "code",
"execution_count": 2,
"id": "changed-collapse",
"execution_count": 1,
"id": "lovely-realtor",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -23,7 +23,7 @@
{
"cell_type": "code",
"execution_count": 3,
"id": "treated-praise",
"id": "signal-owner",
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -61,7 +61,7 @@
{
"cell_type": "code",
"execution_count": 4,
"id": "complex-court",
"id": "systematic-facial",
"metadata": {},
"outputs": [
{
Expand All @@ -82,7 +82,7 @@
{
"cell_type": "code",
"execution_count": 5,
"id": "cubic-holmes",
"id": "motivated-structure",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -93,7 +93,7 @@
{
"cell_type": "code",
"execution_count": 13,
"id": "negative-adams",
"id": "accomplished-audience",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -103,7 +103,7 @@
{
"cell_type": "code",
"execution_count": 14,
"id": "satisfied-kidney",
"id": "exact-boutique",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -113,7 +113,7 @@
{
"cell_type": "code",
"execution_count": 7,
"id": "metric-nomination",
"id": "negative-spoke",
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -124,7 +124,7 @@
{
"cell_type": "code",
"execution_count": 9,
"id": "literary-crazy",
"id": "satisfactory-dating",
"metadata": {},
"outputs": [
{
Expand All @@ -145,12 +145,55 @@
{
"cell_type": "code",
"execution_count": 8,
"id": "undefined-values",
"id": "forced-edition",
"metadata": {},
"outputs": [],
"source": [
"np.save('cube-luta.npy', data)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "entire-conservation",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 0., 1., 2.],\n",
" [ 3., 4., 5.],\n",
" [ 6., 7., 8.]],\n",
"\n",
" [[ 9., 10., 11.],\n",
" [12., 13., 14.],\n",
" [15., 16., 17.]],\n",
"\n",
" [[18., 19., 20.],\n",
" [21., 22., 23.],\n",
" [24., 25., 26.]]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t = np.arange(27)\n",
"q = t.reshape(3,3,3).astype('float64')\n",
"q"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "continental-oakland",
"metadata": {},
"outputs": [],
"source": [
"np.save('test-cube.npy', q)"
]
}
],
"metadata": {
Expand Down
10 changes: 3 additions & 7 deletions learnGo/gonum/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ func tryme(_inv []float64) {
func main() {
//u := mat.NewVecDense(3, []float64{1, 2, 3})
//v := mat.NewVecDense(3, []float64{4, 5, 6})
//a := [3]float64{4,2,1}
o:=make([]float64, 3)
o[0] = 3
o[1] = 1
o[2] = 2
tryme(o)
fmt.Println(o)
a := [3]float64{4,2,1}
tryme(a[:])
fmt.Println(a)
}
Binary file added public/images/cube-lut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/cube-output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 10 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,16 @@ We chose Golang to implement the render mainly because of two things:
3. Handle the kernel evaluation, just `ctmr` for now.
4. Implement the rendr algorithms

Our version of rendering
## Results

Our version of rendering: we modified the parameters a bit for simplicity. First of all, use Python's pynrrd package to parse the `NRRD` files and save the data into `npy` format. We found a package `npyio` that is written in Go. Moreover, we simplified the lights to be only one light, and only support the `rgbalit` and `rgba` mode. For now the we only wrote `ctmr` kernel, but adding more kernels is just a matter of time. The testing command is shown below.
```bash
./rendr go -fr 6 12 5 -at 0 0 0 -up 0 0 1\
-nc -2.3 -fc 2.3 -fov 20\
-sz 320 280 -id cube.npy -im cube-meta.npy $PARMS -nt 4 \
-lutd cube-luta.npy -od cube-lut.npy -om cube-lut-meta.npy
```
-sz 320 280 -id cube.npy -im cube-meta.npy \
-fov 14 -us 0.03 -s 0.01 -k ctmr -p rgbalit -b over \
-lit "1 1 1 -1 1 2 1" -dcn 1.1 1.1 1.1 -dcf 0.4 0.4 0.4\
-nt 4 -lutd cube-luta.npy -od cube-lut.png
```
We render the image directly in Go using the `image` library instead of bothering the `NRRD` format. We didn't set the background to black but instead, we left it transparent. Following the tradition of representing transparency in Photoshop, the output image is shown here:
![output](public/images/cube-output.png)
1 change: 1 addition & 0 deletions rendr/camera.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func (cam *rndCamera) rndCameraUpdate() {
cam.Ncv = cam.Nc + cam.D
cam.Fcv = cam.Fc + cam.D
cam.Hght = 2* cam.D * math.Tan(cam.FOV/2 * math.Pi/180)
cam.Wdth = cam.Hght * float64(cam.Size[0]) / float64(cam.Size[1])
}

func (cam *rndCamera) rndCameraSet(fr,at,up [3]float64, nc,fc,FOV float64, size0, size1 uint, ortho int){
Expand Down
105 changes: 105 additions & 0 deletions rendr/convo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package rendr

import (
"gonum.org/v1/gonum/mat"
"math"
)

func rndConvoNew(ctx *rndCtx) *rndConvo{
support := ctx.Kern.Support
cnv := rndConvo{
Ipos: [3]float64{},
Value: 0,
Gradient: [3]float64{},
Inside: 0,
Kern_values: make([]float64, 3*support),
Deriv_values: make([]float64, 3*support),
}
return &cnv
}

func rndConvoEval(xw, yw, zw float64, cnv *rndConvo, ctx *rndCtx) {
input:= [4]float64{xw, yw, zw, 1}
var output [4]float64
inVec := mat.NewVecDense(4, input[:])
outVec:= mat.NewVecDense(4, output[:])
wtoi := mat.NewDense(4,4,ctx.WtoI[:])
outVec.MulVec(wtoi, inVec)
cnv.Ipos = [3]float64{output[0], output[1], output[2]}
var n1,n2,n3,start,end int
var alpha1, alpha2, alpha3 float64
if ctx.Kern.Support %2==0 {
n1 = int(math.Floor(output[0]))
n2 = int(math.Floor(output[1]))
n3 = int(math.Floor(output[2]))
start = 1-ctx.Kern.Support/2
end = ctx.Kern.Support/2
} else {
n1 = int(math.Floor(output[0]+0.5))
n2 = int(math.Floor(output[1]+0.5))
n3 = int(math.Floor(output[2]+0.5))
start = (1-ctx.Kern.Support)/2
end = (ctx.Kern.Support-1)/2
}
alpha1 = output[0]-float64(n1)
alpha2 = output[1]-float64(n2)
alpha3 = output[2]-float64(n3)
var i,j,k int // loop variables
outside:=0
if n1+start <0 || uint(n1+end) >=ctx.Vol.Size[0] {
outside = outside + 1
} else if n2+start<0 || uint(n2+end) >=ctx.Vol.Size[1] {
outside = outside + 1
} else if n3+start<0 || uint(n3+end) >= ctx.Vol.Size[2] {
outside = outside + 1
}
if outside>0 {
cnv.Value = ctx.OutsideValue
cnv.Gradient = [3]float64{ctx.OutsideValue, ctx.OutsideValue, ctx.OutsideValue}
cnv.Inside = 0
return
}
cnv.Inside = 1
result:=float64(0)
support:= ctx.Kern.Support
k1_val := cnv.Kern_values[:support]
k2_val := cnv.Kern_values[support:2*support]
k3_val := cnv.Kern_values[2*support:]

ctx.Kern.Apply(k1_val, alpha1)
ctx.Kern.Apply(k2_val, alpha2)
ctx.Kern.Apply(k3_val, alpha3)
volSize := ctx.Vol.Size
for i=start; i<=end; i++ {
for j=start; j<=end; j++ {
for k=start; k<=end; k++ {
dTmp:=ctx.Vol.Data[uint(n3+i)*volSize[1]*volSize[0]+uint(n2+j)*volSize[0]+uint(n1+k)]
result += dTmp*k1_val[k-start] * k2_val[j-start] * k3_val[i-start]
}
}
}
cnv.Value = result
//Deriv
k1_deriv:= cnv.Deriv_values[:support]
k2_deriv:= cnv.Deriv_values[support:2*support]
k3_deriv := cnv.Deriv_values[2*support:]
ctx.Kern.Deriv.Apply(k1_deriv, alpha1)
ctx.Kern.Deriv.Apply(k2_deriv, alpha2)
ctx.Kern.Deriv.Apply(k3_deriv, alpha3)
var tmp_gradient [3]float64
for i=start; i<=end; i++ {
for j = start; j <= end; j++ {
for k = start; k <= end; k++ {
dTmp:=ctx.Vol.Data[uint(n3+i)*volSize[1]*volSize[0]+uint(n2+j)*volSize[0]+uint(n1+k)]
tmp_gradient[0] += dTmp*k1_deriv[k-start]*k2_val[j-start]*k3_val[i-start]
tmp_gradient[1] += dTmp*k1_val[k-start]*k2_deriv[j-start]*k3_val[i-start]
tmp_gradient[2] += dTmp*k1_val[k-start]*k2_val[j-start]*k3_deriv[i-start]
}
}
}
// Conver from index space to world space
tgradVec := mat.NewVecDense(3, tmp_gradient[:])
gradVec := mat.NewVecDense(3, cnv.Gradient[:])
mt := mat.NewDense(3, 3, ctx.MT[:])
gradVec.MulVec(mt, tgradVec)
}
15 changes: 14 additions & 1 deletion rendr/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func rndCtxNew(vol *rndVolume, kern *rndKernel,
rgb: [3]float64{lit[0], lit[1], lit[2]},
dir: [3]float64{lit[3], lit[4], lit[5]},
vsp: int(lit[6]),
xyz: nil,
xyz: [3]float64{},
},
Lparam: _lparam{
ka: 0.2,
Expand All @@ -66,3 +66,16 @@ func rndCtxNew(vol *rndVolume, kern *rndKernel,
ctx.WtoI, ctx.MT = rnd_m4_affine_inv(ctx.Vol.ItoW)
return &ctx
}

func rndProbeLen(probe rndProbe) uint {
l:=uint(0)
switch probe {
case rndProbeRgba:
l=4
case rndProbeRgbaLit:
l=4
default:
l=1
}
return l
}
25 changes: 25 additions & 0 deletions rendr/go.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package rendr

import "fmt"

func rndRender(img *rndImage, ctx *rndCtx, _dhi, _dvi int) {
plen:= rndProbeLen(ctx.Probe)
//timing := uint(ctx.Timing)
img.rndImageAlloc(plen, ctx.Cam.Size[0], ctx.Cam.Size[1], rndTypefloat64)
cnv := rndConvoNew(ctx)
if ctx.ThreadNum==0 {
ray := rndNewRay()
index:= uint(0)
odata:=img.Data
//nipx := ctx.Cam.Size[0] * ctx.Cam.Size[1]
//fmt.Fprintf(os.Stderr, "Rendering")
for jj:= uint(0); jj<ctx.Cam.Size[1]; jj++ {
for ii:=uint(0); ii<ctx.Cam.Size[0]; ii++ {
rndRayGo(odata[index:index+plen], ii, jj, ray, cnv, ctx)
index = index+plen
}
}
} else {
fmt.Println("Multithreading version of Gorendr")
}
}
Loading

0 comments on commit 15361e3

Please sign in to comment.