add geometry parser

This commit is contained in:
Shuo Feng 2024-02-15 18:02:19 -05:00
parent a42eae91de
commit 9f63434b12
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
2 changed files with 56 additions and 2 deletions

View file

@ -1,4 +1,5 @@
#include "Parser.h"
#include "Geometry.h"
#include <Eigen/Core>
#include <string>
@ -43,3 +44,52 @@ Scene *Parser::getScene(const nlohmann::json &j) {
return sc;
}
// TODO: Get Matrix4f transform for geometries and lights
const Matrix4f getTransform(const nlohmann::json &j) { return Matrix4f(); }
Geometry *Parser::getGeometry(const nlohmann::json &j) {
float ka = j["ka"].get<float>();
float kd = j["kd"].get<float>();
float ks = j["ks"].get<float>();
Vector3f ca = getVector3f(j["ac"]);
Vector3f cd = getVector3f(j["dc"]);
Vector3f cs = getVector3f(j["sc"]);
float phong = j["pc"].get<float>();
Geometry *g;
if (j["type"].get<string>().compare("rectangle"))
g = getSphere(j, ka, kd, ks, ca, cd, cs, phong);
else
g = getRectangle(j, ka, kd, ks, ca, cd, cs, phong);
if (j.contains("transform")) {
g->setTransform(getTransform(j["transform"]));
}
return g;
}
// TODO: return four corners for rectangles and area lights
const Matrix<float, 3, 4> getCorners(const nlohmann::json &j) {
return Matrix<float, 3, 4>();
}
Sphere *Parser::getSphere(const nlohmann::json &j, float ka, float kd, float ks,
const Vector3f &ca, const Vector3f &cd,
const Vector3f &cs, float pc) {
float radius = j["radius"].get<float>();
Vector3f center = getVector3f(j["centre"]);
return new Sphere(ka, kd, ks, ca, cd, cs, pc, radius, center);
}
Rectangle *Parser::getRectangle(const nlohmann::json &j, float ka, float kd,
float ks, const Vector3f &ca,
const Vector3f &cd, const Vector3f &cs,
float pc) {
Matrix<float, 3, 4> corners = getCorners(j);
return new Rectangle(ka, kd, ks, ca, cd, cs, pc, corners);
}

View file

@ -16,8 +16,12 @@ public:
private:
static PointLight *getPointLight(const nlohmann::json &);
static AreaLight *getAreaLight(const nlohmann::json &);
static Rectangle *getRectangle(const nlohmann::json &);
static Sphere *getSphere(const nlohmann::json &);
static Rectangle *getRectangle(const nlohmann::json &, float, float, float,
const Vector3f &, const Vector3f &,
const Vector3f &, float);
static Sphere *getSphere(const nlohmann::json &, float, float, float,
const Vector3f &, const Vector3f &, const Vector3f &,
float);
};
#endif // !PARSER_H_