Skip to content

Commit

Permalink
Add Disk and Quad geometries.
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsy committed May 5, 2015
1 parent 19a4789 commit 129eeee
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 24 deletions.
3 changes: 3 additions & 0 deletions src/geometry/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ set(SOURCES ${SOURCES}
${CMAKE_CURRENT_LIST_DIR}/plane.cc
${CMAKE_CURRENT_LIST_DIR}/primitive.cc
${CMAKE_CURRENT_LIST_DIR}/quad.cc
${CMAKE_CURRENT_LIST_DIR}/disk.cc
${CMAKE_CURRENT_LIST_DIR}/sphere.cc
${CMAKE_CURRENT_LIST_DIR}/triangle.cc
${CMAKE_CURRENT_LIST_DIR}/trimesh.cc
PARENT_SCOPE)

set(HEADERS ${HEADERS}
${CMAKE_CURRENT_LIST_DIR}/geometry.h
${CMAKE_CURRENT_LIST_DIR}/bbox.h
${CMAKE_CURRENT_LIST_DIR}/plane.h
${CMAKE_CURRENT_LIST_DIR}/primitive.h
${CMAKE_CURRENT_LIST_DIR}/quad.h
${CMAKE_CURRENT_LIST_DIR}/disk.h
${CMAKE_CURRENT_LIST_DIR}/sphere.h
${CMAKE_CURRENT_LIST_DIR}/triangle.h
${CMAKE_CURRENT_LIST_DIR}/trimesh.h
Expand Down
46 changes: 46 additions & 0 deletions src/geometry/disk.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#define SPICA_DISK_EXPORT
#include "disk.h"

namespace spica {

Disk::Disk()
: _center()
, _normal()
, _radius(0.0)
{
}

Disk::Disk(const Vector3& center, const Vector3& normal, double radius)
: _center(center)
, _normal(normal)
, _radius(radius)
{
}

Disk::Disk(const Disk& disk)
: _center()
, _normal()
, _radius()
{
operator=(disk);
}

Disk::~Disk()
{
}

Disk& Disk::operator=(const Disk& disk) {
this->_center = disk._center;
this->_normal = disk._normal;
this->_radius = disk._radius;
return *this;
}

bool Disk::intersect(const Ray& ray, double* tHit) const {
*tHit = (_center - ray.origin()).dot(ray.direction());
Vector3 posHit = ray.origin() + (*tHit) * ray.direction();
return (posHit - _center).norm() < _radius;
}

} // namepspace spica

43 changes: 43 additions & 0 deletions src/geometry/disk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef _SPICA_DISK_H_
#define _SPICA_DISK_H_

#if defined(_WIN32) || defined(__WIN32__)
#ifdef SPICA_DISK_EXPORT
#define SPICA_DISK_DLL __declspec(dllexport)
#else
#define SPICA_DISK_DLL __declspec(dllimport)
#endif
#else
#define SPICA_DISK_DLL
#endif

#include "../renderer/ray.h"
#include "../utils/vector3.h"

namespace spica {

class Disk {
private:
Vector3 _center;
Vector3 _normal;
double _radius;

public:
Disk();
Disk(const Vector3& center, const Vector3& normal, double radius);
Disk(const Disk& disk);
~Disk();

Disk& operator=(const Disk& disk);

bool intersect(const Ray& ray, double* tHit) const;

inline Vector3 center() const { return _center; }
inline Vector3 normal() const { return _normal; }
inline double radius() const { return _radius; }

};

} // namespace spica

#endif // _SPICA_DISK_H_
9 changes: 9 additions & 0 deletions src/geometry/geometry.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef _SPICA_GEOMETRY_H_
#define _SPICA_GEOMETRY_H_

#include "bbox.h"
#include "triangle.h"
#include "quad.h"
#include "disk.h"

#endif // _SPICA_GEOMETRY_H_
47 changes: 47 additions & 0 deletions src/geometry/quad.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#define SPICA_QUAD_EXPORT
#include "quad.h"

namespace spica {

Quad::Quad()
: _t0()
, _t1()
{
}

Quad::Quad(const Vector3& v0, const Vector3& v1, const Vector3& v2, const Vector3& v3)
: _t0(v0, v1, v2)
, _t1(v2, v3, v0)
{
}

Quad::Quad(const Quad& quad)
: _t0()
, _t1()
{
operator=(quad);
}

Quad::~Quad()
{
}

Quad& Quad::operator=(const Quad& quad) {
this->_t0 = quad._t0;
this->_t1 = quad._t1;
return *this;
}

bool Quad::intersect(const Ray& ray, double* tHit) const {
if (_t0.intersect(ray, tHit)) {
return true;
}

if (_t1.intersect(ray, tHit)) {
return true;
}

return false;
}

} // namespace spica
42 changes: 42 additions & 0 deletions src/geometry/quad.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#ifndef _SPICA_QUAD_H_
#define _SPICA_QUAD_H_

#if defined(_WIN32) || defined(__WIN32__)
#ifdef SPICA_QUAD_EXPORT
#define SPICA_QUAD_DLL __declspec(dllexport)
#else
#define SPICA_QUAD_DLL __declspec(dllimport)
#endif
#else
#define SPICA_QUAD_DLL
#endif

#include "../utils/vector3.h"
#include "triangle.h"

namespace spica {

class SPICA_QUAD_DLL Quad {
private:
Triangle _t0;
Triangle _t1;

public:
Quad();
Quad(const Vector3& v0, const Vector3& v1, const Vector3& v2, const Vector3& v3);
Quad(const Quad& quad);
~Quad();

Quad& operator=(const Quad& quad);

bool intersect(const Ray& ray, double* tHit) const;

inline Vector3 p0() const { return _t0.p0(); }
inline Vector3 p1() const { return _t0.p1(); }
inline Vector3 p2() const { return _t0.p2(); }
inline Vector3 p3() const { return _t1.p1(); }
};

} // namespace spica

#endif // _SPICA_QUAD_H_
51 changes: 34 additions & 17 deletions src/utils/sampler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,40 @@

namespace spica {

Random Sampler::rng = Random::getRNG();

Vector3 Sampler::onDisk(const Vector3& center, const Vector3& normal, double radius) {
double r0 = sqrt(rng.randReal());
double r1 = rng.randNorm() * (2.0 * PI);
double rx = radius * r0 * cos(r1);
double ry = radius * r0 * sin(r1);
Vector3 u, v, w;
w = normal;
if (abs(w.x()) > EPS) {
u = Vector3::cross(Vector3(0.0, 1.0, 0.0), w).normalized();
} else {
u = Vector3::cross(Vector3(1.0, 0.0, 0.0), w).normalized();
namespace {

Random rng = Random::getRNG();

} // anonymous namespace

namespace sampler {

Vector3 onDisk(const Disk& disk) {
double r0 = sqrt(rng.randReal());
double r1 = rng.randNorm() * (2.0 * PI);
double rx = disk.radius() * r0 * cos(r1);
double ry = disk.radius() * r0 * sin(r1);
Vector3 u, v, w;
w = disk.normal();
if (abs(w.x()) > EPS) {
u = Vector3::cross(Vector3(0.0, 1.0, 0.0), w).normalized();
} else {
u = Vector3::cross(Vector3(1.0, 0.0, 0.0), w).normalized();
}
v = Vector3::cross(u, w);

return disk.center() + u * rx + v * ry;
}

Vector3 onQuad(const Quad& quad) {
Vector3 e1 = quad.p1() - quad.p0();
Vector3 e2 = quad.p3() - quad.p0();
double r1 = rng.randReal();
double r2 = rng.randReal();

return r1 * e1 + r2 * e2;
}
v = Vector3::cross(u, w);

return center + u * rx + v * ry;
}
} // namespace sampler

}
} // namespace spica
14 changes: 7 additions & 7 deletions src/utils/sampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@

#include "random.h"
#include "vector3.h"
#include "../geometry/geometry.h"

namespace spica {

class Sampler {
private:
static Random rng;
namespace sampler {

public:
static Vector3 onDisk(const Vector3& center, const Vector3& normal, double radius);
Vector3 onDisk(const Disk& disk);

Vector3 onQuad(const Quad& quad);

};
} // namespace sampler

}
} // namespace spica

#endif // SPICA_SAMPLER_H_

0 comments on commit 129eeee

Please sign in to comment.