initial import from /gvsu/cs658 repository
git-svn-id: svn://anubis/fart/trunk@2 7f9b0f55-74a9-4bce-be96-3c2cd072584d
This commit is contained in:
parent
a00c525abe
commit
206ede3304
16
Makefile
Normal file
16
Makefile
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
ifdef WIN32
|
||||||
|
export CPPFLAGS += -I"$(shell cd)"
|
||||||
|
else
|
||||||
|
export CPPFLAGS += -I"$(shell pwd)"
|
||||||
|
endif
|
||||||
|
|
||||||
|
all:
|
||||||
|
make -C util
|
||||||
|
make -C shapes
|
||||||
|
make -C test
|
||||||
|
|
||||||
|
clean:
|
||||||
|
make -C test clean
|
||||||
|
make -C shapes clean
|
||||||
|
make -C util clean
|
20
shapes/Makefile
Normal file
20
shapes/Makefile
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
OBJS := $(patsubst %.cc,%.o,$(wildcard *.cc))
|
||||||
|
|
||||||
|
all: $(OBJS)
|
||||||
|
|
||||||
|
%.o: %.cc
|
||||||
|
$(CXX) -c -o $@ $(CPPFLAGS) $(CXXFLAGS) $<
|
||||||
|
|
||||||
|
# Make dependency files
|
||||||
|
%.dep: %.cc
|
||||||
|
@set -e; rm -f $@; \
|
||||||
|
$(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \
|
||||||
|
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
|
||||||
|
rm -f $@.$$$$
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-$(RM) -f *.o *.dep $(ARCHIVE)
|
||||||
|
|
||||||
|
# Include dependency files
|
||||||
|
include $(OBJS:.o=.dep)
|
15
shapes/Shape.h
Normal file
15
shapes/Shape.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
|
||||||
|
#ifndef SHAPE_H
|
||||||
|
#define SHAPE_H SHAPE_H
|
||||||
|
|
||||||
|
#include "util/Solver.h"
|
||||||
|
#include "util/Ray.h"
|
||||||
|
|
||||||
|
class Shape
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
virtual Solver::Result intersect(const Ray & ray) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
22
shapes/Sphere.cc
Normal file
22
shapes/Sphere.cc
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
#include "Sphere.h"
|
||||||
|
#include "util/Solver.h"
|
||||||
|
|
||||||
|
Sphere::Sphere(double radius)
|
||||||
|
{
|
||||||
|
m_radius = radius;
|
||||||
|
}
|
||||||
|
|
||||||
|
Solver::Result Sphere::intersect(const Ray & ray)
|
||||||
|
{
|
||||||
|
Solver::Result res;
|
||||||
|
QuadraticSolver solver(1.0,
|
||||||
|
2 * ( ray.getOrigin()[0] * ray.getDirection()[0]
|
||||||
|
+ ray.getOrigin()[1] * ray.getDirection()[1]
|
||||||
|
+ ray.getOrigin()[2] * ray.getDirection()[2] ),
|
||||||
|
ray.getOrigin()[0] * ray.getOrigin()[0]
|
||||||
|
+ ray.getOrigin()[1] * ray.getOrigin()[1]
|
||||||
|
+ ray.getOrigin()[2] * ray.getOrigin()[2] );
|
||||||
|
Solver::Result quadSolutions = solver.solve();
|
||||||
|
return res;
|
||||||
|
}
|
18
shapes/Sphere.h
Normal file
18
shapes/Sphere.h
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
|
||||||
|
#ifndef SPHERE_H
|
||||||
|
#define SPHERE_H SPHERE_H
|
||||||
|
|
||||||
|
#include "Shape.h"
|
||||||
|
|
||||||
|
class Sphere : public Shape
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Sphere(double radius);
|
||||||
|
Solver::Result intersect(const Ray & ray);
|
||||||
|
|
||||||
|
private:
|
||||||
|
double m_radius;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
6
test/Makefile
Normal file
6
test/Makefile
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
|
||||||
|
all:
|
||||||
|
@echo Nothing yet.
|
||||||
|
|
||||||
|
clean:
|
||||||
|
@echo Nothing yet.
|
20
util/Makefile
Normal file
20
util/Makefile
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
|
||||||
|
OBJS := $(patsubst %.cc,%.o,$(wildcard *.cc))
|
||||||
|
|
||||||
|
all: $(OBJS)
|
||||||
|
|
||||||
|
%.o: %.cc
|
||||||
|
$(CXX) -c -o $@ $(CPPFLAGS) $(CXXFLAGS) $<
|
||||||
|
|
||||||
|
# Make dependency files
|
||||||
|
%.dep: %.cc
|
||||||
|
@set -e; rm -f $@; \
|
||||||
|
$(CXX) -MM $(CPPFLAGS) $< > $@.$$$$; \
|
||||||
|
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
|
||||||
|
rm -f $@.$$$$
|
||||||
|
|
||||||
|
clean:
|
||||||
|
-$(RM) -f *.o *.dep $(ARCHIVE)
|
||||||
|
|
||||||
|
# Include dependency files
|
||||||
|
include $(OBJS:.o=.dep)
|
26
util/Ray.cc
Normal file
26
util/Ray.cc
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
#include "Ray.h"
|
||||||
|
|
||||||
|
Ray::Ray()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Ray::Ray(const Vector & origin, const Vector & direction)
|
||||||
|
{
|
||||||
|
m_origin = origin;
|
||||||
|
m_direction = direction;
|
||||||
|
m_direction.normalize();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* return a vector for the point at distance dist
|
||||||
|
* from the ray's origin point, along its direction.
|
||||||
|
*/
|
||||||
|
Vector Ray::getPositionAt(double dist) const
|
||||||
|
{
|
||||||
|
Vector v;
|
||||||
|
v[0] = m_origin[0] + dist * m_direction[0];
|
||||||
|
v[1] = m_origin[1] + dist * m_direction[1];
|
||||||
|
v[2] = m_origin[2] + dist * m_direction[2];
|
||||||
|
return v;
|
||||||
|
}
|
22
util/Ray.h
Normal file
22
util/Ray.h
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
#ifndef RAY_H
|
||||||
|
#define RAY_H RAY_H
|
||||||
|
|
||||||
|
#include "Vector.h"
|
||||||
|
|
||||||
|
class Ray
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Ray();
|
||||||
|
Ray(const Vector & origin, const Vector & direction);
|
||||||
|
const Vector & getOrigin() const { return m_origin; }
|
||||||
|
const Vector & getDirection() const { return m_direction; }
|
||||||
|
Vector getPositionAt(double dist) const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
Vector m_origin;
|
||||||
|
Vector m_direction;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
77
util/Solver.cc
Normal file
77
util/Solver.cc
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
|
||||||
|
#include "Solver.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
/* Generic Solver constructor */
|
||||||
|
Solver::Solver(double a, double b, double c, double d, double e)
|
||||||
|
{
|
||||||
|
this->a = a;
|
||||||
|
this->b = b;
|
||||||
|
this->c = c;
|
||||||
|
this->d = d;
|
||||||
|
this->e = e;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**************************************************************************
|
||||||
|
* QuadraticSolver methods *
|
||||||
|
*************************************************************************/
|
||||||
|
QuadraticSolver::QuadraticSolver(double a, double b, double c)
|
||||||
|
: Solver(a, b, c)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/* solve a quadratic equation */
|
||||||
|
Solver::Result QuadraticSolver::solve()
|
||||||
|
{
|
||||||
|
Result res;
|
||||||
|
double discriminant = b * b - 4 * a * c;
|
||||||
|
if (discriminant < 0.0)
|
||||||
|
{
|
||||||
|
res.numResults = 0;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
double sqrt_discriminant = sqrt(discriminant);
|
||||||
|
double two_a = 2.0 * a;
|
||||||
|
if (sqrt_discriminant = 0.0)
|
||||||
|
{
|
||||||
|
res.numResults = 1;
|
||||||
|
res.results[0] = (-b) / two_a;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
res.numResults = 2;
|
||||||
|
res.results[0] = (-b - sqrt_discriminant) / two_a;
|
||||||
|
res.results[1] = (-b + sqrt_discriminant) / two_a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**************************************************************************
|
||||||
|
* CubicSolver methods *
|
||||||
|
*************************************************************************/
|
||||||
|
CubicSolver::CubicSolver(double a, double b, double c, double d)
|
||||||
|
: Solver(a, b, c, d)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Solver::Result CubicSolver::solve()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**************************************************************************
|
||||||
|
* QuarticSolver methods *
|
||||||
|
*************************************************************************/
|
||||||
|
QuarticSolver::QuarticSolver(double a, double b, double c, double d, double e)
|
||||||
|
: Solver(a, b, c, d, e)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
Solver::Result QuarticSolver::solve()
|
||||||
|
{
|
||||||
|
}
|
47
util/Solver.h
Normal file
47
util/Solver.h
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
|
||||||
|
#ifndef SOLVER_H
|
||||||
|
#define SOLVER_H SOLVER_H
|
||||||
|
|
||||||
|
class Solver
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
int numResults;
|
||||||
|
double results[4];
|
||||||
|
} Result;
|
||||||
|
|
||||||
|
Solver(double a = 0.0,
|
||||||
|
double b = 0.0,
|
||||||
|
double c = 0.0,
|
||||||
|
double d = 0.0,
|
||||||
|
double e = 0.0);
|
||||||
|
virtual Result solve() = 0;
|
||||||
|
|
||||||
|
protected:
|
||||||
|
double a, b, c, d, e;
|
||||||
|
};
|
||||||
|
|
||||||
|
class QuadraticSolver : public Solver
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
QuadraticSolver(double a, double b, double c);
|
||||||
|
Result solve();
|
||||||
|
};
|
||||||
|
|
||||||
|
class CubicSolver : public Solver
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
CubicSolver(double a, double b, double c, double d);
|
||||||
|
Result solve();
|
||||||
|
};
|
||||||
|
|
||||||
|
class QuarticSolver : public Solver
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
QuarticSolver(double a, double b, double c, double d, double e);
|
||||||
|
Result solve();
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
43
util/Vector.cc
Normal file
43
util/Vector.cc
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
|
||||||
|
#include "Vector.h"
|
||||||
|
#include <iostream>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
Vector::Vector()
|
||||||
|
{
|
||||||
|
m_array[0] = 0.0;
|
||||||
|
m_array[1] = 0.0;
|
||||||
|
m_array[2] = 0.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Vector::normalize()
|
||||||
|
{
|
||||||
|
double length = sqrt(m_array[0] * m_array[0]
|
||||||
|
+ m_array[1] * m_array[1]
|
||||||
|
+ m_array[2] * m_array[2]);
|
||||||
|
m_array[0] /= length;
|
||||||
|
m_array[1] /= length;
|
||||||
|
m_array[2] /= length;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::ostream & operator<<(std::ostream & out, const Vector & v)
|
||||||
|
{
|
||||||
|
out << "[" << v[0] << ", " << v[1] << ", " << v[2] << "]";
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Compute the dot-product of two vectors */
|
||||||
|
double operator%(const Vector & v1, const Vector & v2)
|
||||||
|
{
|
||||||
|
return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Compute the cross-product of two vectors */
|
||||||
|
Vector operator*(const Vector & v1, const Vector & v2)
|
||||||
|
{
|
||||||
|
Vector result;
|
||||||
|
result[0] = v1[1] * v2[2] - v1[2] * v2[1];
|
||||||
|
result[1] = v1[2] * v2[0] - v1[0] * v2[2];
|
||||||
|
result[2] = v1[0] * v2[1] - v1[1] * v2[0];
|
||||||
|
return result;
|
||||||
|
}
|
24
util/Vector.h
Normal file
24
util/Vector.h
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
#ifndef VECTOR_H
|
||||||
|
#define VECTOR_H VECTOR_H
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
class Vector
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Vector();
|
||||||
|
~Vector();
|
||||||
|
double & operator[](int idx) { return m_array[idx]; }
|
||||||
|
double operator[](int idx) const { return m_array[idx]; }
|
||||||
|
void normalize();
|
||||||
|
|
||||||
|
private:
|
||||||
|
double m_array[3];
|
||||||
|
};
|
||||||
|
|
||||||
|
std::ostream & operator<<(std::ostream & out, const Vector & v);
|
||||||
|
double operator%(const Vector & v1, const Vector & v2);
|
||||||
|
Vector operator*(const Vector & v1, const Vector & v2);
|
||||||
|
|
||||||
|
#endif
|
Loading…
x
Reference in New Issue
Block a user