This commit is contained in:
Shuo Feng 2024-01-28 20:14:37 -05:00
commit de866d23bf
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
28 changed files with 46878 additions and 0 deletions

22100
external/json.hpp vendored Normal file

File diff suppressed because it is too large Load diff

25
external/simpleppm.cpp vendored Normal file
View file

@ -0,0 +1,25 @@
#include "simpleppm.h"
/*
This code was adapted from here:
https://rosettacode.org/wiki/Bitmap/Write_a_PPM_file#C.2B.2B
*/
using namespace std;
int save_ppm(std::string file_name, const std::vector<double>& buffer, int dimx, int dimy) {
ofstream ofs(file_name, ios_base::out | ios_base::binary);
ofs << "P6" << endl << dimx << ' ' << dimy << endl << "255" << endl;
for (unsigned int j = 0; j < dimy; ++j)
for (unsigned int i = 0; i < dimx; ++i)
ofs << (char) (255.0 * buffer[3*j*dimx+3*i+0]) << (char) (255.0 * buffer[3*j*dimx+3*i+1]) << (char) (255.0 * buffer[3*j*dimx+3*i+2]);
ofs.close();
return 0;
}

9
external/simpleppm.h vendored Normal file
View file

@ -0,0 +1,9 @@
#include <fstream>
#include <cstdio>
#include <vector>
#include <string>
int save_ppm(std::string file_name, const std::vector<double>& buffer, int dimx, int dimy);
int test_save_ppm();

42
external/test_eigen.cpp vendored Normal file
View file

@ -0,0 +1,42 @@
#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <sstream>
using namespace std;
int print_eigen(Eigen::Matrix4d m)
{
// Eigen Matrices do have rule to print them with std::cout
std::cout << m << std::endl;
return 0;
}
int test_eigen()
{
Eigen::Matrix4d test; //4 by 4 double precision matrix initialization
// Let's make it a symmetric matrix
for(int i=0; i<4; i++)
{
for(int j=0; j<4; j++)
test(i,j) = (i+1)*(1+j);
}
// Print
print_eigen(test);
return 0;
}

169
external/test_json.cpp vendored Normal file
View file

@ -0,0 +1,169 @@
#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>
#include "json.hpp"
#include "simpleppm.h"
#include <sstream>
using namespace std;
using namespace nlohmann;
bool test_parse_geometry(json& j){
cout<<"Geometry: "<<endl;
int gc = 0;
// use iterators to read-in array types
for (auto itr = j["geometry"].begin(); itr!= j["geometry"].end(); itr++){
std::string type;
if(itr->contains("type")){
// type = static_cast<std::string>((*itr)["type"]);
type = (*itr)["type"].get<std::string>();
} else {
cout<<"Fatal error: geometry shoudl always contain a type!!!"<<endl;
return false;
}
if(type=="sphere"){
cout<<"Sphere: "<<endl;
Eigen::Vector3f centre(0,0,0);
int i = 0;
for (auto itr2 =(*itr)["centre"].begin(); itr2!= (*itr)["centre"].end(); itr2++){
if(i<3){
centre[i++] = (*itr2).get<float>();
} else {
cout<<"Warning: Too many entries in centre"<<endl;
}
}
cout<<"Centre: "<<centre<<endl;
}
++gc;
}
cout<<"We have: "<<gc<<" objects!"<<endl;
return true;
}
bool test_parse_lights(json& j){
cout<<"Light: "<<endl;
int lc = 0;
// use iterators to read-in array types
for (auto itr = j["light"].begin(); itr!= j["light"].end(); itr++){
std::string type;
if(itr->contains("type")){
// type = static_cast<std::string>((*itr)["type"]);
type = (*itr)["type"].get<std::string>();
} else {
cout<<"Fatal error: light shoudl always contain a type!!!"<<endl;
return false;
}
if(type=="point"){
cout<<"Point based light: "<<endl;
Eigen::Vector3f centre(0,0,0);
int i = 0;
for (auto itr2 =(*itr)["centre"].begin(); itr2!= (*itr)["centre"].end(); itr2++){
if(i<3){
centre[i++] = (*itr2).get<float>();
} else {
cout<<"Warning: Too many entries in centre"<<endl;
}
}
cout<<"Centre: "<<centre<<endl;
}
++lc;
}
cout<<"We have: "<<lc<<" objects!"<<endl;
return true;
}
bool test_parse_output(json& j){
cout<<"Outputs: "<<endl;
int lc = 0;
// use iterators to read-in array types
for (auto itr = j["output"].begin(); itr!= j["output"].end(); itr++){
std::string filename;
if(itr->contains("filename")){
// filename = static_cast<std::string>((*itr)["filename"]);
filename = (*itr)["filename"].get<std::string>();
} else {
cout<<"Fatal error: output shoudl always contain a filename!!!"<<endl;
return false;
}
int size[2];
int i = 0;
for (auto itr2 =(*itr)["size"].begin(); itr2!= (*itr)["size"].end(); itr2++){
if(i<2){
size[i++] = (*itr2).get<float>();
} else {
cout<<"Warning: Too many entries in size"<<endl;
}
}
Eigen::Vector3f lookat(0,0,0), up(0,0,0), centre(0,0,0);
i = 0;
for (auto itr2 =(*itr)["centre"].begin(); itr2!= (*itr)["centre"].end(); itr2++){
if(i<3){
centre[i++] = (*itr2).get<float>();
} else {
cout<<"Warning: Too many entries in centre"<<endl;
}
}
// Similarly to the centre array you need to read the lookat and up
//Maybe create a separate functiomn to read arrays - ythey are pretty common
// I am retrieving the field of view
// this is mandatory field here, but if I dont check if it exists,
// the code will throw an exception which if not caught will end the execution of yoru program
cout<<"A"<<endl;
float fov = (*itr)["fov"].get<float>();
cout<<"B"<<endl;
cout<<"Filename: "<<filename<<endl;
cout<<"Camera centre: "<<centre<<endl;
cout<<"FOV: "<<fov<<endl;
++lc;
}
cout<<"We have: "<<lc<<" objects!"<<endl;
return true;
}
int test_json(json& j){
// 1 - parse geometry
test_parse_geometry(j);
// 2 - parse lights
test_parse_lights(j);
// 3 - parse lights
test_parse_output(j);
return 0;
}

43
external/test_ppm.cpp vendored Normal file
View file

@ -0,0 +1,43 @@
#include <iostream>
#include <Eigen/Core>
#include <Eigen/Dense>
#include "json.hpp"
#include "simpleppm.h"
#include <sstream>
using namespace std;
using namespace nlohmann;
int test_save_ppm(){
int dimx = 800;
int dimy = 600;
int w = 100;
std::vector<double> buffer(3*dimx*dimy);
for(int j=0;j<dimy;++j){
for(int i=0;i<dimx;++i){
if(((i+j)/w)%2==0){
buffer[3*j*dimx+3*i+0]=1;
buffer[3*j*dimx+3*i+1]=1;
buffer[3*j*dimx+3*i+2]=0;
} else {
buffer[3*j*dimx+3*i+0]=0;
buffer[3*j*dimx+3*i+1]=1;
buffer[3*j*dimx+3*i+2]=1;
}
}
}
save_ppm("test.ppm", buffer, dimx, dimy);
return 0;
}