Skip to content

Commit

Permalink
Minor update.
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsy committed Nov 15, 2017
1 parent 9baf8c6 commit 6661246
Show file tree
Hide file tree
Showing 14 changed files with 259 additions and 150 deletions.
34 changes: 16 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"io/ioutil"
"log"
"os"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"strings"

. "github.com/tatsy/gopt/src/accelerator"
Expand Down Expand Up @@ -41,22 +39,22 @@ type JsonParam struct {
}

func main() {
cpuprofile := "profile.prof"
f, err := os.Create(cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
for sig := range c {
log.Printf("captured %v, stopping profiler and exiting...", sig)
pprof.StopCPUProfile()
os.Exit(1)
}
}()
// cpuprofile := "profile.prof"
// f, err := os.Create(cpuprofile)
// if err != nil {
// log.Fatal(err)
// }
// pprof.StartCPUProfile(f)
// defer pprof.StopCPUProfile()
// c := make(chan os.Signal, 1)
// signal.Notify(c, os.Interrupt)
// go func() {
// for sig := range c {
// log.Printf("captured %v, stopping profiler and exiting...", sig)
// pprof.StopCPUProfile()
// os.Exit(1)
// }
// }()

// Parse command line args
var jsonFile string
Expand Down
5 changes: 5 additions & 0 deletions src/bsdf/bxdf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bsdf

const (
deltaEps = 1.0e-12
)
5 changes: 4 additions & 1 deletion src/bsdf/lambert.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ func (f *LambertReflection) Eval(wi, wo *Vector3d) *Color {
}

func (f *LambertReflection) Pdf(wi, wo *Vector3d) Float {
return 1.0
if SameHemisphere(wi, wo) {
return AbsCosTheta(wi) / math.Pi
}
return 0.0
}

func (f *LambertReflection) Sample(wo *Vector3d, u *Vector2d) (*Color, *Vector3d, Float) {
Expand Down
14 changes: 7 additions & 7 deletions src/bsdf/specular.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ func NewSpecularReflection(re *Color) *SpecularReflection {
}

func (f *SpecularReflection) Eval(wi, wo *Vector3d) *Color {
if NewVector3d(-wi.X, -wi.Y, wi.Z).Dot(wo) >= 1.0-Eps {
return f.re
if NewVector3d(-wi.X, -wi.Y, wi.Z).Dot(wo) >= 1.0-deltaEps {
return f.re.Scale(1.0 / AbsCosTheta(wi))
}
return NewColor(0.0, 0.0, 0.0)
}

func (f *SpecularReflection) Pdf(wi, wo *Vector3d) Float {
if NewVector3d(-wi.X, -wi.Y, wi.Z).Dot(wo) >= 1.0-Eps {
if NewVector3d(-wi.X, -wi.Y, wi.Z).Dot(wo) >= 1.0-deltaEps {
return 1.0
}
return 0.0
Expand Down Expand Up @@ -58,7 +58,7 @@ func (f *SpecularFresnel) Eval(wi, wo *Vector3d) *Color {
cosThetaI := CosTheta(wo)
F := FresnelDielectric(cosThetaI, f.etaA, f.etaB)
if SameHemisphere(wi, wo) {
if NewVector3d(-wi.X, -wi.Y, wi.Z).Dot(wo) >= 1.0-Eps {
if NewVector3d(-wi.X, -wi.Y, wi.Z).Dot(wo) >= 1.0-deltaEps {
return f.re.Scale(F / AbsCosTheta(wi))
}
} else {
Expand All @@ -74,7 +74,7 @@ func (f *SpecularFresnel) Eval(wi, wo *Vector3d) *Color {
}

wt, isRefr := Refract(wo, faceForwardNormal, etaI/etaT)
if isRefr && wt.Dot(wi) > 1.0-Eps {
if isRefr && wt.Dot(wi) > 1.0-deltaEps {
return f.tr.Scale((1.0 - F) / AbsCosTheta(wi))
}
}
Expand All @@ -85,7 +85,7 @@ func (f *SpecularFresnel) Pdf(wi, wo *Vector3d) Float {
cosThetaI := CosTheta(wo)
F := FresnelDielectric(cosThetaI, f.etaA, f.etaB)
if SameHemisphere(wi, wo) {
if NewVector3d(-wi.X, -wi.Y, wi.Z).Dot(wo) >= 1.0-Eps {
if NewVector3d(-wi.X, -wi.Y, wi.Z).Dot(wo) >= 1.0-deltaEps {
return F
}
} else {
Expand All @@ -101,7 +101,7 @@ func (f *SpecularFresnel) Pdf(wi, wo *Vector3d) Float {
}

wt, isRefract := Refract(wo, faceForwardNormal, etaI/etaT)
if isRefract && wt.Dot(wi) > 1.0-Eps {
if isRefract && wt.Dot(wi) > 1.0-deltaEps {
return 1.0 - F
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/core/bsdf.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,19 @@ func (bsdf *Bsdf) Eval(wi, wo *Vector3d) *Color {
zLocal = bsdf.ns.Dot(wo)
woLocal := NewVector3d(xLocal, yLocal, zLocal)
return bsdf.bxdf.Eval(wiLocal, woLocal)
}

func (bsdf *Bsdf) Pdf(wi, wo *Vector3d) Float {
var xLocal, yLocal, zLocal Float
xLocal = bsdf.ts.Dot(wi)
yLocal = bsdf.bs.Dot(wi)
zLocal = bsdf.ns.Dot(wi)
wiLocal := NewVector3d(xLocal, yLocal, zLocal)
xLocal = bsdf.ts.Dot(wo)
yLocal = bsdf.bs.Dot(wo)
zLocal = bsdf.ns.Dot(wo)
woLocal := NewVector3d(xLocal, yLocal, zLocal)
return bsdf.bxdf.Pdf(wiLocal, woLocal)
}

func (bsdf *Bsdf) SampleWi(wo *Vector3d, u *Vector2d) (*Color, *Vector3d, Float, int) {
Expand Down
74 changes: 43 additions & 31 deletions src/core/color.go
Original file line number Diff line number Diff line change
@@ -1,59 +1,71 @@
package core

import (
"fmt"
"fmt"
"math"
)

type Color struct {
R, G, B Float
R, G, B Float
}

func NewColor(r, g, b Float) *Color {
c := &Color{}
c.R = r
c.G = g
c.B = b
return c
c := &Color{}
c.R = r
c.G = g
c.B = b
return c
}

func NewColorWithString(s string) *Color {
var r, g, b Float
n, _ := fmt.Sscanf(s, "(%f, %f, %f)", &r, &g, &b)
if n != 3 {
panic(fmt.Sprintf("Failed to parse Vector3d: %s", s))
}
return &Color{r, g, b}
var r, g, b Float
n, _ := fmt.Sscanf(s, "(%f, %f, %f)", &r, &g, &b)
if n != 3 {
panic(fmt.Sprintf("Failed to parse Vector3d: %s", s))
}
return &Color{r, g, b}
}


func (c *Color) Y() Float {
return 0.299 * c.R + 0.587 * c.G + 0.114 * c.B
return 0.299*c.R + 0.587*c.G + 0.114*c.B
}

func (c *Color) IsBlack() bool {
return c.R == 0.0 && c.G == 0.0 && c.B == 0.0
return c.R == 0.0 && c.G == 0.0 && c.B == 0.0
}

func (c *Color) IsNaN() bool {
return math.IsNaN(c.R) || math.IsNaN(c.G) || math.IsNaN(c.B)
}

func (c *Color) IsInf() bool {
return math.IsInf(c.R, 0) || math.IsInf(c.G, 0) || math.IsInf(c.B, 0)
}

func (c *Color) IsValid() bool {
return !c.IsInf() && !c.IsNaN()
}

func (c1 *Color) Add(c2 *Color) *Color {
ret := &Color{}
ret.R = c1.R + c2.R
ret.G = c1.G + c2.G
ret.B = c1.B + c2.B
return ret
ret := &Color{}
ret.R = c1.R + c2.R
ret.G = c1.G + c2.G
ret.B = c1.B + c2.B
return ret
}

func (c *Color) Scale(x Float) *Color {
ret := &Color{}
ret.R = c.R * x
ret.G = c.G * x
ret.B = c.B * x
return ret
ret := &Color{}
ret.R = c.R * x
ret.G = c.G * x
ret.B = c.B * x
return ret
}

func (c1 *Color) Multiply(c2 *Color) *Color {
ret := &Color{}
ret.R = c1.R * c2.R
ret.G = c1.G * c2.G
ret.B = c1.B * c2.B
return ret
ret := &Color{}
ret.R = c1.R * c2.R
ret.G = c1.G * c2.G
ret.B = c1.B * c2.B
return ret
}
4 changes: 3 additions & 1 deletion src/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ type Accelerator interface {

type Shape interface {
Intersect(ray *Ray, tHit *Float, isect *Intersection) bool
SampleP(u *Vector2d) (*Vector3d, *Vector3d, Float)
SamplePoint(u *Vector2d) (*Vector3d, *Vector3d)
Pdf(isect *Intersection, wi *Vector3d) Float
Bounds() *Bounds3d
}

Expand All @@ -32,4 +33,5 @@ type Light interface {
Le() *Color
LeWithRay(ray *Ray) *Color
SampleLi(isect *Intersection, u *Vector2d) (*Color, *Vector3d, Float, *VisibilityTester)
PdfLi(isect *Intersection, wi *Vector3d) Float
}
32 changes: 17 additions & 15 deletions src/core/intersection.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
package core

import "math"

type Intersection struct {
Pos, Normal, Wo *Vector3d
primitive *Primitive
Pos, Normal, Wo *Vector3d
primitive *Primitive
}

func NewIntersection(pos, normal, wo *Vector3d) *Intersection {
isect := &Intersection{}
isect.Pos = pos
isect.Normal = normal
isect.Wo = wo
isect.primitive = nil
return isect
isect := &Intersection{}
isect.Pos = pos
isect.Normal = normal
isect.Wo = wo
isect.primitive = nil
return isect
}

func (isect *Intersection) SpawnRay(wi *Vector3d) *Ray {
return NewRay(isect.Pos, wi)
return NewRay(isect.Pos, wi)
}

func (isect *Intersection) Bsdf() *Bsdf {
return NewBsdf(isect, isect.primitive.Bxdf())
return NewBsdf(isect, isect.primitive.Bxdf())
}

func (isect *Intersection) Le(wi *Vector3d) *Color {
if isect.primitive.Light() == nil {
return NewColor(0.0, 0.0, 0.0)
}
dot := wi.Dot(isect.Normal)
return isect.primitive.Light().Le().Scale(dot)
if isect.primitive.Light() == nil {
return NewColor(0.0, 0.0, 0.0)
}
dot := math.Max(0.0, wi.Dot(isect.Normal))
return isect.primitive.Light().Le().Scale(dot)
}

0 comments on commit 6661246

Please sign in to comment.