Skip to content

Commit

Permalink
Merge pull request #19 from tatsy/develop
Browse files Browse the repository at this point in the history
Improve code coverage
  • Loading branch information
tatsy committed Aug 27, 2015
2 parents 83c584e + 2ee6be4 commit cbe3a20
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 59 deletions.
46 changes: 0 additions & 46 deletions src/random/random.cc
Original file line number Diff line number Diff line change
Expand Up @@ -108,34 +108,6 @@ namespace spica {
}
}

/* initialize by an array with array-length */
/* init_key is the array for initializing keys */
/* key_length is its length */
/* slight change for C++, 2004/2/26 */
void Random::init_by_array(unsigned int init_key[], int key_length) {
int i, j, k;
init_genrand(19650218U);
i = 1; j = 0;
k = (N > key_length ? N : key_length);
for (; k; k--) {
mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1664525UL))
+ init_key[j] + j; /* non linear */
mt[i] &= 0xffffffffULL; /* for WORDSIZE > 32 machines */
i++; j++;
if (i >= N) { mt[0] = mt[N - 1]; i = 1; }
if (j >= key_length) j = 0;
}
for (k = N - 1; k; k--) {
mt[i] = (mt[i] ^ ((mt[i - 1] ^ (mt[i - 1] >> 30)) * 1566083941UL))
- i; /* non linear */
mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
i++;
if (i >= N) { mt[0] = mt[N - 1]; i = 1; }
}

mt[0] = 0x80000000ULL; /* MSB is 1; assuring non-zero initial array */
}

/* generates a random number on [0,0xffffffff]-interval */
unsigned int Random::genrand_int32(void) {
unsigned int y;
Expand Down Expand Up @@ -184,28 +156,10 @@ namespace spica {
return static_cast<int>(genrand_int32() >> 1);
}

/* generates a random number on [0,1]-real-interval */
double Random::genrand_real1(void) {
return genrand_int32()*(1.0 / 4294967295.0);
/* divided by 2^32-1 */
}

/* generates a random number on [0,1)-real-interval */
double Random::genrand_real2(void) {
return genrand_int32()*(1.0 / 4294967296.0);
/* divided by 2^32 */
}

/* generates a random number on (0,1)-real-interval */
double Random::genrand_real3(void) {
return ((static_cast<double>(genrand_int32())) + 0.5)*(1.0 / 4294967296.0);
/* divided by 2^32 */
}

/* generates a random number on [0,1) with 53-bit resolution*/
double Random::genrand_res53(void) {
unsigned int a = genrand_int32() >> 5, b = genrand_int32() >> 6;
return(a*67108864.0 + b)*(1.0 / 9007199254740992.0);
}

} // namespace spica
8 changes: 4 additions & 4 deletions src/random/random.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#ifdef _MSC_VER
#pragma once
#endif

#ifndef _SPICA_RANDOM_H_
#define _SPICA_RANDOM_H_

Expand Down Expand Up @@ -50,13 +54,9 @@ namespace spica {

private:
void init_genrand(unsigned int s);
void init_by_array(unsigned int init_key[], int key_length);
unsigned int genrand_int32(void);
int genrand_int31(void);
double genrand_real1(void);
double genrand_real2(void);
double genrand_real3(void);
double genrand_res53(void);
};

}
Expand Down
6 changes: 6 additions & 0 deletions src/utils/sampler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ namespace spica {
(*normal) = tri.normal();
}

void onTriangle(const Triangle& tri, Vector3D* position, Vector3D* normal) {
double r1 = rng.nextReal();
double r2 = rng.nextReal();
onTriangle(tri, position, normal, r1, r2);
}

void onQuad(const Quad& quad, Vector3D* position, Vector3D* normal, double r1, double r2) {
// TODO: this sampler can properly work only for rectangles and squares
Vector3D e1 = quad[1] - quad[0];
Expand Down
1 change: 1 addition & 0 deletions src/utils/vector3d.cc
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ namespace spica {
}

Vector3D Vector3D::sqrt(const Vector3D& v) {
Assertion(v.x() >= 0.0 && v.y() >= 0.0 && v.z() >= 0.0, "Specified vector has negative entries !!");
return Vector3D(::sqrt(v.x()), ::sqrt(v.y()), ::sqrt(v.z()));
}

Expand Down
65 changes: 56 additions & 9 deletions test/test_sampler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@ using namespace spica;

const int nTrial = 100;

TEST(SamplerTest, HemisphereSampleTest) {
Vector3D n(0.0, 1.0, 0.0);

for (int i = 0; i < nTrial; i++) {
Vector3D d;
sampler::onHemisphere(n, &d);

EXPECT_LE(0.0, n.dot(d));
EXPECT_LE(n.dot(d), 1.0);
}
}

TEST(SamplerTest, DiskSampleTest) {
Vector3D C(0.0, 0.0, 0.0);
Vector3D N(0.0, 0.0, 1.0);
Expand All @@ -27,20 +39,55 @@ TEST(SamplerTest, DiskSampleTest) {
}
}

TEST(SamplerTest, SphereSampleTest) {
Vector3D c(0.0, 1.0, 2.0);
double r = 5.0;
Sphere sph(r, c);

for (int i = 0; i < nTrial; i++) {
Vector3D v, n;
sampler::onSphere(sph, &v, &n);
EXPECT_FLOAT_EQ(r, (v - c).norm());
EXPECT_EQ_VEC(n, (v - c).normalized());
}
}

TEST(SamplerTest, TriangleSampleTest) {
Vector3D a(0.0, 1.0, 0.0);
Vector3D b(0.0, 1.0, 1.0);
Vector3D c(0.0, 0.0, 1.0);
Triangle tri(a, b, c);

for (int i = 0; i < nTrial; i++) {
Vector3D v, n;
sampler::onTriangle(tri, &v, &n);
EXPECT_EQ_VEC(tri.normal(), n);
EXPECT_FLOAT_EQ(0.0, (v - tri[0]).dot(n));

Vector3D e1 = tri[1] - tri[0];
Vector3D e2 = tri[2] - tri[0];
Vector3D e = v - tri[0];
EXPECT_LE(0.0, e1.dot(e) / e1.squaredNorm());
EXPECT_GE(1.0, e1.dot(e) / e1.squaredNorm());
EXPECT_LE(0.0, e2.dot(e) / e2.squaredNorm());
EXPECT_GE(1.0, e2.dot(e) / e2.squaredNorm());
}
}

TEST(SamplerTest, PoissonDiskTest) {
/*
const double mindist = 0.1;
const double maxdist = 0.5;
Trimesh trimesh(kDataDirectory + "bunny.ply");
Trimesh mesh(kDataDirectory + "bunny.ply");
std::vector<Triangle> tris = mesh.triangulate();
double mindist = 0.1;

std::vector<Vector3D> points;
std::vector<Vector3D> normals;
sampler::poissonDisk(trimesh, mindist, maxdist, &points, &normals);
sampler::poissonDisk(tris, mindist, &points, &normals);

EXPECT_NE(0, points.size());
EXPECT_EQ(points.size(), normals.size());
for (int i = 0; i < points.size(); i++) {
for (int j = i + 1; j < points.size(); j++) {
EXPECT_LE(mindist, (points[i] - points[j]).norm());
for (int j = i + 1; j < points.size(); j++) {
EXPECT_LE((points[i] - points[j]).norm(), 2.0 * mindist);
}
}
*/
}
}
37 changes: 37 additions & 0 deletions test/test_vector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,45 @@ TEST(Vector3DTest, AlgebraTest) {
EXPECT_EQ(u.x() / nrm, w.x());
EXPECT_EQ(u.y() / nrm, w.y());
EXPECT_EQ(u.z() / nrm, w.z());
EXPECT_FLOAT_EQ(w.norm(), 1.0);
EXPECT_NE(u.norm(), 1.0);

Vector3D z = Vector3D::normalize(u);
EXPECT_EQ(u.x() / nrm, z.x());
EXPECT_EQ(u.y() / nrm, z.y());
EXPECT_EQ(u.z() / nrm, z.z());
EXPECT_FLOAT_EQ(1.0, z.norm());
EXPECT_NE(u.norm(), 1.0);
}

TEST(Vector3DTest, NegateTest) {
Vector3D u(1.0, 2.0, 3.0);
Vector3D v = -u;
EXPECT_EQ(-u.x(), v.x());
EXPECT_EQ(-u.y(), v.y());
EXPECT_EQ(-u.z(), v.z());
}

TEST(Vector3DTest, SqrtTest) {
Vector3D u(1.0, 2.0, 3.0);
Vector3D v = Vector3D::sqrt(u);
EXPECT_EQ(sqrt(u.x()), v.x());
EXPECT_EQ(sqrt(u.y()), v.y());
EXPECT_EQ(sqrt(u.z()), v.z());

u = Vector3D(-1.0, 2.0, 3.0);
ASSERT_DEATH(Vector3D::sqrt(u), "");
}

TEST(Vector3DTest, ExpTest) {
Vector3D u(1.0, 2.0, 3.0);
Vector3D v = Vector3D::exp(u);
EXPECT_EQ(exp(u.x()), v.x());
EXPECT_EQ(exp(u.y()), v.y());
EXPECT_EQ(exp(u.z()), v.z());
}


TEST(Vector3DTest, DotCrossTest) {
Vector3D u(1.0, 2.0, 3.0);
Vector3D v(4.0, 5.0, 6.0);
Expand Down

0 comments on commit cbe3a20

Please sign in to comment.