Merge pull request #5 from vonhyou/geometry

Add geometry related classes
This commit is contained in:
Shuo Feng 2024-02-13 19:32:28 -05:00 committed by GitHub
commit 9fcdfd2773
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 71 additions and 3 deletions

9
src/Geometry.cc Normal file
View file

@ -0,0 +1,9 @@
#include "Geometry.h"
void Geometry::setTransform(const Matrix4f &transform) {
this->transform = transform;
}
bool Sphere::intersect() const { return false; }
bool Rectangle::intersect() const { return false; }

60
src/Geometry.h Normal file
View file

@ -0,0 +1,60 @@
#ifndef GEOMETRY_H_
#define GEOMETRY_H_
#include <Eigen/Core>
using Eigen::Matrix;
using Eigen::Matrix4f;
using Eigen::Vector3f;
// Abstract class for Geometries
class Geometry {
public:
enum class Type { SPHERE, RECTANGLE };
virtual ~Geometry() = default;
virtual bool intersect() const = 0;
protected:
Geometry(Type type, float ka, float kd, float ks, const Vector3f &ca,
const Vector3f &cd, const Vector3f &cs, float pc)
: type(type), ka(ka), kd(kd), ks(ks), ca(ca), cd(cd), cs(cs), phong(pc) {}
Type type;
float ka, kd, ks; // coefficients for ambient, diffuse and specular
Vector3f ca, cd, cs; // ambient, diffuse and specular reflection color
float phong; // phone coefficient, for `pc`
Matrix4f transform = Matrix4f::Identity();
public:
void setTransform(const Matrix4f &);
};
class Sphere : public Geometry {
public:
Sphere(float ka, float kd, float ks, const Vector3f &ca, const Vector3f &cd,
const Vector3f &cs, float pc, float radius, const Vector3f &center)
: Geometry(Type::SPHERE, ka, kd, ks, ca, cd, cs, pc), radius(radius),
center(center) {}
bool intersect() const override;
private:
float radius;
Vector3f center;
};
class Rectangle : public Geometry {
public:
Rectangle(float ka, float kd, float ks, const Vector3f &ca, const Vector3f cd,
const Vector3f &cs, float pc, const Matrix<float, 3, 4> &corners)
: Geometry(Type::RECTANGLE, ka, kd, ks, ca, cd, cs, pc),
corners(corners) {}
bool intersect() const override;
private:
Matrix<float, 3, 4> corners;
};
#endif // !GEOMETRY_H_

View file

@ -13,7 +13,6 @@ public:
enum class Type { Point, Area };
virtual ~Light() = default;
// a pure virtual function for derived class implementation
virtual void illumination() const = 0;
protected:
@ -21,8 +20,8 @@ protected:
: type(type), diffuse(id), specular(is) {}
Type type;
Vector3f diffuse; // mandatory member `id`
Vector3f specular; // mandatory member `is`
Vector3f diffuse;
Vector3f specular;
Matrix4f transform = Matrix4f::Identity(); // optional member `transform`
unsigned int gridSize = 0; // optional member `n`
bool useCenter = false; // optional member `usecenter`