This repository has been archived on 2024-12-30. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
2024CG-project-render/include/read_json.h
github-classroom[bot] 5d6a4935ec
Initial commit
2024-09-26 09:17:55 +00:00

137 lines
3.9 KiB
C++

#ifndef READ_JSON_H
#define READ_JSON_H
#include <vector>
#include <string>
#include <memory>
// Forward declaration
class Object;
// Read a scene description from a .json file
//
// Input:
// filename path to .json file
// Output:
// camera camera looking at the scene
// objects list of shared pointers to objects
inline bool read_json(
const std::string & filename,
Camera & camera,
std::vector<std::shared_ptr<Object> > & objects);
// Implementation
#include <json.hpp>
#include "readSTL.h"
#include "dirname.h"
#include "Object.h"
#include "Sphere.h"
#include "Plane.h"
#include "Triangle.h"
#include "TriangleSoup.h"
#include <Eigen/Geometry>
#include <fstream>
#include <iostream>
#include <cassert>
inline bool read_json(
const std::string & filename,
Camera & camera,
std::vector<std::shared_ptr<Object> > & objects)
{
// Heavily borrowing from
// https://github.com/yig/graphics101-raycasting/blob/master/parser.cpp
using json = nlohmann::json;
std::ifstream infile( filename );
if( !infile ) return false;
json j;
infile >> j;
// parse a vector
auto parse_Vector3d = [](const json & j) -> Eigen::Vector3d
{
return Eigen::Vector3d(j[0],j[1],j[2]);
};
// parse camera
auto parse_camera =
[&parse_Vector3d](const json & j, Camera & camera)
{
assert(j["type"] == "perspective" && "Only handling perspective cameras");
camera.d = j["focal_length"].get<double>();
camera.e = parse_Vector3d(j["eye"]);
camera.v = parse_Vector3d(j["up"]).normalized();
camera.w = -parse_Vector3d(j["look"]).normalized();
camera.u = camera.v.cross(camera.w);
camera.height = j["height"].get<double>();
camera.width = j["width"].get<double>();
};
parse_camera(j["camera"],camera);
auto parse_objects = [&parse_Vector3d,&filename](
const json & j,
std::vector<std::shared_ptr<Object> > & objects)
{
objects.clear();
for(const json & jobj : j)
{
if(jobj["type"] == "sphere")
{
std::shared_ptr<Sphere> sphere(new Sphere());
sphere->center = parse_Vector3d(jobj["center"]);
sphere->radius = jobj["radius"].get<double>();
objects.push_back(sphere);
}else if(jobj["type"] == "plane")
{
std::shared_ptr<Plane> plane(new Plane());
plane->point = parse_Vector3d(jobj["point"]);
plane->normal = parse_Vector3d(jobj["normal"]).normalized();
objects.push_back(plane);
}else if(jobj["type"] == "triangle")
{
std::shared_ptr<Triangle> tri(new Triangle());
tri->corners = std::make_tuple(
parse_Vector3d(jobj["corners"][0]),
parse_Vector3d(jobj["corners"][1]),
parse_Vector3d(jobj["corners"][2]));
objects.push_back(tri);
}else if(jobj["type"] == "soup")
{
std::vector<std::vector<double> > V;
std::vector<std::vector<double> > F;
std::vector<std::vector<int> > N;
{
#if defined(WIN32) || defined(_WIN32)
#define PATH_SEPARATOR std::string("\\")
#else
#define PATH_SEPARATOR std::string("/")
#endif
const std::string stl_path = jobj["stl"];
igl::readSTL(
igl::dirname(filename)+
PATH_SEPARATOR +
stl_path,
V,F,N);
}
std::shared_ptr<TriangleSoup> soup(new TriangleSoup());
for(int f = 0;f<F.size();f++)
{
std::shared_ptr<Triangle> tri(new Triangle());
tri->corners = std::make_tuple(
Eigen::Vector3d( V[(long long)(F[f][0])][0], V[(long long)(F[f][0])][1], V[(long long)(F[f][0])][2]),
Eigen::Vector3d( V[(long long)(F[f][1])][0], V[(long long)(F[f][1])][1], V[(long long)(F[f][1])][2]),
Eigen::Vector3d( V[(long long)(F[f][2])][0], V[(long long)(F[f][2])][1], V[(long long)(F[f][2])][2])
);
soup->triangles.push_back(tri);
}
objects.push_back(soup);
}
}
};
parse_objects(j["objects"],objects);
return true;
}
#endif