22 vector<double*> cameras;
23 vector<double*> lights;
24 map<string, int> colors;
25 vector<string> shapesJSON;
35 string getColorString(
double r,
double g,
double b) {
37 sstr << r/255 <<
"," << g/255 <<
"," << b/255;
38 string hash = sstr.str();
39 if (colors.find(hash) == colors.end()) {
40 colors[hash] = colors.size();
43 ret <<
"color" << colors[hash];
52 string getMaterialsJSON() {
54 json <<
"\"materials\":{\n";
56 for (
const pair<string, int>& c : colors) {
57 json <<
"\t\"color" << c.second <<
"\": {";
58 json <<
"\"kd\":[" << c.first <<
"]}";
59 if (i+1 < colors.size()) {
74 string getLightsJSON() {
76 json <<
"\"lights\":[\n";
77 for (
size_t i = 0; i < lights.size(); i++) {
78 double* L = lights.at(i);
80 json <<
"\t\"pos\":[" << L[0] <<
", " << L[1] <<
", " << L[2] <<
"],\n";
81 json <<
"\t\"color\":[" << L[3] <<
", " << L[4] <<
", " << L[5] <<
"]\n";
83 if (i < lights.size() - 1) {
97 string getCamerasJSON() {
99 json <<
"\"cameras\":[\n";
100 for (
size_t i = 0; i < cameras.size(); i++) {
101 double* c = cameras.at(i);
103 json <<
"\t\"pos\":[" << c[0] <<
", " << c[1] <<
", " << c[2] <<
"],\n";
105 double sh = sin(c[3]*PI/360);
106 double ch = cos(c[3]*PI/360);
107 json <<
"\t\"rot\": [0, " << sh <<
", 0, " << ch <<
"]\n";
109 if (i+1 < cameras.size()) {
123 string getShapesJSON() {
125 json <<
"\"children\":[\n";
126 for (
size_t i = 0; i < shapesJSON.size(); i++) {
127 json << shapesJSON.at(i);
128 if (i+1 < shapesJSON.size()) {
142 for (
size_t i = 0; i < cameras.size(); i++) {
143 delete[] cameras.at(i);
145 for (
size_t i = 0; i < lights.size(); i++) {
146 delete[] lights.at(i);
162 double rx,
double ry,
double rz,
163 double sx,
double sy,
double sz,
string shape) {
167 json <<
"\"transform\":[1, 0, 0, " << tx <<
", 0, 1, 0, " << ty <<
", 0, 0, 1, " << tz <<
", 0, 0, 0, 1],\n";
168 json <<
"\"children\":[{";
170 double c = cos(rz*PI/180);
171 double s = sin(rz*PI/180);
172 json <<
"\"transform\":[" << c <<
", " << -s <<
", 0, 0, " << s <<
", " << c <<
", 0, 0, 0, 0, 1, 0, 0, 0, 0, 1],\n";
173 json <<
"\"children\":[{\n";
177 json <<
"\"transform\":[" << c <<
", 0, " << s <<
", 0, 0, 1, 0, 0, " << -s <<
", 0, " << c <<
", 0, 0, 0, 0, 1],\n";
178 json <<
"\"children\":[{\n";
182 json <<
"\"transform\":[1, 0, 0, 0, 0, " << c <<
", " << -s <<
", 0, 0, " << s <<
", " << c <<
", 0, 0, 0, 0, 1],\n";
183 json <<
"\"children\":[{\n";
184 json <<
"\"transform\":[" << sx <<
", 0, 0, 0, 0, " << sy <<
", 0, 0, 0, 0, " << sz <<
", 0, 0, 0, 0, 1],\n";
185 json <<
"\"shapes\":[\n";
211 void addBox(
double cx,
double cy,
double cz,
double xlen,
212 double ylen,
double zlen,
213 double r,
double g,
double b,
214 double rx,
double ry,
double rz) {
215 stringstream boxJSON;
216 boxJSON <<
"{\"type\":\"box\",\n";
217 boxJSON <<
"\"material\":\"" << getColorString(r, g, b) <<
"\"}";
218 shapesJSON.push_back(getTransformationHierarchy(cx, cy, cz, rx, ry, rz, xlen, ylen, zlen, boxJSON.str()));
233 void addBox(
double cx,
double cy,
double cz,
234 double xlen,
double ylen,
double zlen,
235 double r,
double g,
double b) {
236 addBox(cx, cy, cz, xlen, ylen, zlen, r, g, b, 0, 0, 0);
257 double height,
double r,
double g,
double b,
258 double rx,
double ry,
double rz,
259 double sx,
double sy,
double sz) {
260 stringstream cylinderJSON;
261 cylinderJSON <<
"{\"type\":\"cylinder\",\n";
262 cylinderJSON <<
"\"radius\":" << radius <<
",\n";
263 cylinderJSON <<
"\"height\":" << height <<
",\n";
264 cylinderJSON <<
"\"material\":\"" << getColorString(r, g, b) <<
"\"}";
265 shapesJSON.push_back(getTransformationHierarchy(cx, cy, cz, rx, ry, rz, sx, sy, sz, cylinderJSON.str()));
280 double height,
double r,
double g,
double b) {
281 addCylinder(cx, cy, cz, radius, height, r, g, b, 0, 0, 0, 1, 1, 1);
301 void addCone(
double cx,
double cy,
double cz,
double radius,
302 double height,
double r,
double g,
double b,
303 double rx,
double ry,
double rz,
304 double sx,
double sy,
double sz) {
305 stringstream coneJSON;
306 coneJSON <<
"{\"type\":\"cone\",\n";
307 coneJSON <<
"\"radius\":" << radius <<
",\n";
308 coneJSON <<
"\"height\":" << height <<
",\n";
309 coneJSON <<
"\"material\":\"" << getColorString(r, g, b) <<
"\"}";
310 shapesJSON.push_back(getTransformationHierarchy(cx, cy, cz, rx, ry, rz, sx, sy, sz, coneJSON.str()));
324 void addCone(
double cx,
double cy,
double cz,
double radius,
325 double height,
double r,
double g,
double b) {
326 addCone(cx, cy, cz, radius, height, r, g, b, 0, 0, 0, 1, 1, 1);
345 double radx,
double rady,
double radz,
346 double r,
double g,
double b,
347 double rx,
double ry,
double rz) {
348 stringstream ellipsoidJSON;
349 ellipsoidJSON <<
"{\"type\":\"sphere\",\n";
350 ellipsoidJSON <<
"\"material\":\"" << getColorString(r, g, b) <<
"\"}";
351 shapesJSON.push_back(getTransformationHierarchy(cx, cy, cz, rx, ry, rz, radx, rady, radz, ellipsoidJSON.str()));
367 double radx,
double rady,
double radz,
368 double r,
double g,
double b) {
369 addEllipsoid(cx, cy, cz, radx, rady, radz, r, g, b, 0, 0, 0);
382 void addSphere(
double cx,
double cy,
double cz,
double radius,
383 double r,
double g,
double b) {
384 addEllipsoid(cx, cy, cz, radius, radius, radius, r, g, b);
405 double cx,
double cy,
double cz,
406 double rx,
double ry,
double rz,
407 double sx,
double sy,
double sz,
408 double r,
double g,
double b) {
409 stringstream meshJSON;
410 meshJSON <<
"{\"type\":\"mesh\",\n";
411 meshJSON <<
"\"material\":\"" << getColorString(r, g, b) <<
"\",\n";
412 meshJSON <<
"\"filename\":\"../meshes/" << meshName <<
".off\"}";
413 shapesJSON.push_back(getTransformationHierarchy(cx, cy, cz, rx, ry, rz, sx, sy, sz, meshJSON.str()));
424 void addCamera(
double x,
double y,
double z,
double rot) {
425 double* xyzr =
new double[4];
430 cameras.push_back(xyzr);
443 void addLight(
double x,
double y,
double z,
double r,
double g,
double b) {
444 double* light =
new double[6];
451 lights.push_back(light);
462 json <<
"{\n\"name\":\"" << sceneName <<
"\",\n";
463 json << getMaterialsJSON() <<
",\n";
464 json << getLightsJSON() <<
",\n";
465 json << getCamerasJSON() <<
",\n";
466 json << getShapesJSON() <<
"\n}";
467 std::ofstream out(filename.c_str());