-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
mariabrbz
committed
Jun 15, 2020
1 parent
ca7e354
commit 618a01d
Showing
3 changed files
with
88 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,76 +1,78 @@ | ||
#include <vector> | ||
#include "../Raytracer/vector.cpp" | ||
#include <cstdio> | ||
#include <string> | ||
using namespace std; | ||
|
||
// source: https://pastebin.com/bEYVtqYy | ||
// if the Polygon class name conflicts with a class in wingdi.h on Windows, use a namespace or change the name | ||
class Polygon { | ||
public: | ||
std::vector<Vector> vertices; | ||
vector< vector<Vector> > edges; | ||
vector<Vector> vertices; | ||
}; | ||
|
||
// saves a static svg file. The polygon vertices are supposed to be in the range [0..1], and a canvas of size 1000x1000 is created | ||
void save_svg(const std::vector<Polygon> &polygons, std::string filename, std::string fillcol = "none") { | ||
FILE* f = fopen(filename.c_str(), "w+"); | ||
fprintf(f, "<svg xmlns = \"http://www.w3.org/2000/svg\" width = \"1000\" height = \"1000\">\n"); | ||
for (int i=0; i<polygons.size(); i++) { | ||
fprintf(f, "<g>\n"); | ||
fprintf(f, "<polygon points = \""); | ||
for (int j = 0; j < polygons[i].vertices.size(); j++) { | ||
fprintf(f, "%3.3f, %3.3f ", (polygons[i].vertices[j][0] * 1000), (1000 - polygons[i].vertices[j][1] * 1000)); | ||
} | ||
fprintf(f, "\"\nfill = \"%s\" stroke = \"black\"/>\n", fillcol.c_str()); | ||
fprintf(f, "</g>\n"); | ||
void save_svg(const vector<Polygon> &polygons, string filename, string fillcol = "none") { | ||
FILE* f = fopen(filename.c_str(), "w+"); | ||
fprintf(f, "<svg xmlns = \"http://www.w3.org/2000/svg\" width = \"1000\" height = \"1000\">\n"); | ||
for (int i=0; i<polygons.size(); i++) { | ||
fprintf(f, "<g>\n"); | ||
fprintf(f, "<polygon points = \""); | ||
for (int j = 0; j < polygons[i].vertices.size(); j++) { | ||
fprintf(f, "%3.3f, %3.3f ", (polygons[i].vertices[j][0] * 1000), (1000 - polygons[i].vertices[j][1] * 1000)); | ||
} | ||
fprintf(f, "</svg>\n"); | ||
fclose(f); | ||
fprintf(f, "\"\nfill = \"%s\" stroke = \"black\"/>\n", fillcol.c_str()); | ||
fprintf(f, "</g>\n"); | ||
} | ||
|
||
fprintf(f, "</svg>\n"); | ||
fclose(f); | ||
} | ||
|
||
|
||
// Adds one frame of an animated svg file. frameid is the frame number (between 0 and nbframes-1). | ||
// polygons is a list of polygons, describing the current frame. | ||
// The polygon vertices are supposed to be in the range [0..1], and a canvas of size 1000x1000 is created | ||
void save_svg_animated(const std::vector<Polygon> &polygons, std::string filename, int frameid, int nbframes) { | ||
FILE* f; | ||
if (frameid == 0) { | ||
f = fopen(filename.c_str(), "w+"); | ||
fprintf(f, "<svg xmlns = \"http://www.w3.org/2000/svg\" width = \"1000\" height = \"1000\">\n"); | ||
fprintf(f, "<g>\n"); | ||
} else { | ||
f = fopen(filename.c_str(), "a+"); | ||
} | ||
void save_svg_animated(const std::vector<Polygon> &polygons, std::string filename, int frameid, int nbframes) { | ||
FILE* f; | ||
if (frameid == 0) { | ||
f = fopen(filename.c_str(), "w+"); | ||
fprintf(f, "<svg xmlns = \"http://www.w3.org/2000/svg\" width = \"1000\" height = \"1000\">\n"); | ||
fprintf(f, "<g>\n"); | ||
for (int i = 0; i < polygons.size(); i++) { | ||
fprintf(f, "<polygon points = \""); | ||
for (int j = 0; j < polygons[i].vertices.size(); j++) { | ||
fprintf(f, "%3.3f, %3.3f ", (polygons[i].vertices[j][0] * 1000), (1000-polygons[i].vertices[j][1] * 1000)); | ||
} | ||
fprintf(f, "\"\nfill = \"none\" stroke = \"black\"/>\n"); | ||
} | ||
fprintf(f, "<animate\n"); | ||
fprintf(f, " id = \"frame%u\"\n", frameid); | ||
fprintf(f, " attributeName = \"display\"\n"); | ||
fprintf(f, " values = \""); | ||
for (int j = 0; j < nbframes; j++) { | ||
if (frameid == j) { | ||
fprintf(f, "inline"); | ||
} else { | ||
fprintf(f, "none"); | ||
} | ||
fprintf(f, ";"); | ||
} else { | ||
f = fopen(filename.c_str(), "a+"); | ||
} | ||
fprintf(f, "<g>\n"); | ||
for (int i = 0; i < polygons.size(); i++) { | ||
fprintf(f, "<polygon points = \""); | ||
for (int j = 0; j < polygons[i].vertices.size(); j++) { | ||
fprintf(f, "%3.3f, %3.3f ", (polygons[i].vertices[j][0] * 1000), (1000-polygons[i].vertices[j][1] * 1000)); | ||
} | ||
fprintf(f, "none\"\n keyTimes = \""); | ||
for (int j = 0; j < nbframes; j++) { | ||
fprintf(f, "%2.3f", j / (double)(nbframes)); | ||
fprintf(f, ";"); | ||
fprintf(f, "\"\nfill = \"none\" stroke = \"black\"/>\n"); | ||
} | ||
fprintf(f, "<animate\n"); | ||
fprintf(f, " id = \"frame%u\"\n", frameid); | ||
fprintf(f, " attributeName = \"display\"\n"); | ||
fprintf(f, " values = \""); | ||
for (int j = 0; j < nbframes; j++) { | ||
if (frameid == j) { | ||
fprintf(f, "inline"); | ||
} else { | ||
fprintf(f, "none"); | ||
} | ||
fprintf(f, "1\"\n dur = \"5s\"\n"); | ||
fprintf(f, " begin = \"0s\"\n"); | ||
fprintf(f, " repeatCount = \"indefinite\"/>\n"); | ||
fprintf(f, ";"); | ||
} | ||
fprintf(f, "none\"\n keyTimes = \""); | ||
for (int j = 0; j < nbframes; j++) { | ||
fprintf(f, "%2.3f", j / (double)(nbframes)); | ||
fprintf(f, ";"); | ||
} | ||
fprintf(f, "1\"\n dur = \"5s\"\n"); | ||
fprintf(f, " begin = \"0s\"\n"); | ||
fprintf(f, " repeatCount = \"indefinite\"/>\n"); | ||
fprintf(f, "</g>\n"); | ||
if (frameid == nbframes - 1) { | ||
fprintf(f, "</g>\n"); | ||
if (frameid == nbframes - 1) { | ||
fprintf(f, "</g>\n"); | ||
fprintf(f, "</svg>\n"); | ||
} | ||
fclose(f); | ||
} | ||
fprintf(f, "</svg>\n"); | ||
} | ||
fclose(f); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#include <cmath> | ||
|
||
class Vector { | ||
private: | ||
double coords[3]; | ||
|