From ae58c6f74af442acfbe1aca0afe7140678e63c53 Mon Sep 17 00:00:00 2001 From: ishmal Date: Fri, 9 Mar 2007 12:07:12 +0000 Subject: [PATCH] Add some code cleanup --- src/extension/internal/pov-out.cpp | 364 ++++++++++++++++++----------- 1 file changed, 230 insertions(+), 134 deletions(-) diff --git a/src/extension/internal/pov-out.cpp b/src/extension/internal/pov-out.cpp index c26af6f36..8e224b4dc 100644 --- a/src/extension/internal/pov-out.cpp +++ b/src/extension/internal/pov-out.cpp @@ -28,9 +28,11 @@ #include "libnr/n-art-bpath.h" #include "extension/system.h" +#include "io/sys.h" +#include +#include -#include "io/sys.h" namespace Inkscape { @@ -39,31 +41,7 @@ namespace Extension namespace Internal { - - -static const char * -dstr(gchar *sbuffer, double d) -{ - return (const char *)g_ascii_formatd(sbuffer, - G_ASCII_DTOSTR_BUF_SIZE, "%.8g", (gdouble)d); - -} - - -/** - * Make sure that we are in the database - */ -bool PovOutput::check (Inkscape::Extension::Extension *module) -{ - /* We don't need a Key - if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_OUTPUT_POV)) - return FALSE; - */ - - return true; -} - - +typedef std::string String; /** @@ -99,8 +77,8 @@ public: { assign(other); return *this; } virtual ~PovShapeInfo() {} - std::string id; - std::string color; + String id; + String color; private: void assign(const PovShapeInfo &other) @@ -126,6 +104,110 @@ effective_opacity(SPItem const *item) } +//######################################################################## +//# OUTPUT FORMATTING +//######################################################################## + +static const char *formatDouble(gchar *sbuffer, double d) +{ + return (const char *)g_ascii_formatd(sbuffer, + G_ASCII_DTOSTR_BUF_SIZE, "%.8g", (gdouble)d); + +} + + +/** + * Not-threadsafe version + */ +static char _dstr_buf[G_ASCII_DTOSTR_BUF_SIZE+1]; + +static const char *dstr(double d) +{ + return formatDouble(_dstr_buf, d); +} + + + + +static String vec2Str(double a, double b) +{ + String str; + str.append("<"); + str.append(dstr(a)); + str.append(", "); + str.append(dstr(b)); + str.append(">"); + return str; +} + +/* +static String vec3Str(double a, double b, double c) +{ + String str; + str.append("<"); + str.append(dstr(a)); + str.append(", "); + str.append(dstr(b)); + str.append(", "); + str.append(dstr(c)); + str.append(">"); + return str; +} +*/ + +static String vec4Str(double a, double b, double c, double d) +{ + String str; + str.append("<"); + str.append(dstr(a)); + str.append(", "); + str.append(dstr(b)); + str.append(", "); + str.append(dstr(c)); + str.append(", "); + str.append(dstr(d)); + str.append(">"); + return str; +} + + +static String formatRgbf(double r, double g, double b, double f) +{ + //"rgbf < %1.3f, %1.3f, %1.3f %1.3f>" + String str; + str.append("rgbf "); + str.append(vec4Str(r, g, b, f)); + return str; +} + +static String formatSeg(int segNr, double a0, double a1, + double b0, double b1, + double c0, double c1, + double d0, double d1) +{ + //" /*%4d*/ <%f, %f>, <%f, %f>, <%f,%f>, <%f,%f>" + String str; + char buf[32]; + snprintf(buf, 31, " /*%4d*/ ", segNr); + str.append(buf); + str.append(vec2Str(a0, a1)); + str.append(", "); + str.append(vec2Str(b0, b1)); + str.append(", "); + str.append(vec2Str(c0, c1)); + str.append(", "); + str.append(vec2Str(d0, d1)); + return str; +} + + + + + + +//######################################################################## +//# M A I N O U T P U T +//######################################################################## /** @@ -146,24 +228,28 @@ PovOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const * return; time_t tim = time(NULL); - fprintf(f, "/*#################################################\n"); + fprintf(f, "/*###################################################################\n"); fprintf(f, "### This PovRay document was generated by Inkscape\n"); fprintf(f, "### http://www.inkscape.org\n"); fprintf(f, "### Created: %s", ctime(&tim)); fprintf(f, "### Version: %s\n", VERSION); - fprintf(f, "##################################################*/\n\n\n"); - - std::vectorpovShapes; //A list for saving information about the shapes - - //we only need 8 for printf() calls that call dstr() 8 times - gchar s1[G_ASCII_DTOSTR_BUF_SIZE + 1]; - gchar s2[G_ASCII_DTOSTR_BUF_SIZE + 1]; - gchar s3[G_ASCII_DTOSTR_BUF_SIZE + 1]; - gchar s4[G_ASCII_DTOSTR_BUF_SIZE + 1]; - gchar s5[G_ASCII_DTOSTR_BUF_SIZE + 1]; - gchar s6[G_ASCII_DTOSTR_BUF_SIZE + 1]; - gchar s7[G_ASCII_DTOSTR_BUF_SIZE + 1]; - gchar s8[G_ASCII_DTOSTR_BUF_SIZE + 1]; + fprintf(f, "#####################################################################\n"); + fprintf(f, "### NOTES:\n"); + fprintf(f, "### ============\n"); + fprintf(f, "### POVRay information can be found at\n"); + fprintf(f, "### http://www.povray.org\n"); + fprintf(f, "###\n"); + fprintf(f, "### The 'AllShapes' objects at the bottom are provided as a\n"); + fprintf(f, "### preview of how the output would look in a trace. However,\n"); + fprintf(f, "### the main intent of this file is to provide the individual\n"); + fprintf(f, "### shapes for inclusion in a POV project.\n"); + fprintf(f, "###\n"); + fprintf(f, "### For an example of how to use this file, look at\n"); + fprintf(f, "### share/examples/istest.pov\n"); + fprintf(f, "####################################################################*/\n\n\n"); + + //A list for saving information about the shapes + std::vectorpovShapes; double bignum = 1000000.0; double minx = bignum; @@ -171,14 +257,15 @@ PovOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const * double miny = bignum; double maxy = -bignum; - - - unsigned indx; - for (indx = 0; indx < results.size() ; indx++) + for (unsigned int indx = 0; indx < results.size() ; indx++) { //### Fetch the object from the repr info Inkscape::XML::Node *rpath = results[indx]; - gchar *id = (gchar *)rpath->attribute("id"); + char *str = (char *) rpath->attribute("id"); + if (!str) + continue; + + String id = str; SPObject *reprobj = SP_ACTIVE_DOCUMENT->getObjectByRepr(rpath); if (!reprobj) continue; @@ -216,22 +303,17 @@ PovOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const * * effective_opacity(shape) ); //gchar *str = g_strdup_printf("rgbf < %1.3f, %1.3f, %1.3f %1.3f>", // rgb[0], rgb[1], rgb[2], 1.0 - dopacity); - gchar *str = g_strdup_printf("rgbf < %s, %s, %s %s>", - dstr(s1, rgb[0]), dstr(s2, rgb[1]), dstr(s3, rgb[2]), dstr(s4, 1.0 - dopacity)); - - shapeInfo.color += str; - g_free(str); + shapeInfo.color += formatRgbf(rgb[0], rgb[1], rgb[2], 1.0 - dopacity); } povShapes.push_back(shapeInfo); //passed all tests. save the info - int curveNr; int curveLength = SP_CURVE_LENGTH(curve); //Count the NR_CURVETOs/LINETOs int segmentCount=0; NArtBpath *bp = SP_CURVE_BPATH(curve); - for (curveNr=0 ; curveNrcode == NR_CURVETO || bp->code == NR_LINETO) segmentCount++; @@ -242,18 +324,18 @@ PovOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const * double lastx = 0.0; double lasty = 0.0; - fprintf(f, "/*##############################################\n"); - fprintf(f, "### PRISM: %s\n", id); - fprintf(f, "##############################################*/\n"); - fprintf(f, "#declare %s = prism {\n", id); + fprintf(f, "/*###################################################\n"); + fprintf(f, "### PRISM: %s\n", id.c_str()); + fprintf(f, "###################################################*/\n"); + fprintf(f, "#declare %s = prism {\n", id.c_str()); fprintf(f, " linear_sweep\n"); fprintf(f, " bezier_spline\n"); fprintf(f, " 1.0, //top\n"); fprintf(f, " 0.0, //bottom\n"); fprintf(f, " %d, //nr points\n", segmentCount * 4); int segmentNr = 0; - curveNr = 0; - for (bp = SP_CURVE_BPATH(curve) ; curveNr < curveLength ; curveNr++, bp++) + bp = SP_CURVE_BPATH(curve); + for (int curveNr=0 ; curveNr < curveLength ; curveNr++) { using NR::X; using NR::Y; @@ -268,18 +350,17 @@ PovOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const * { case NR_MOVETO: case NR_MOVETO_OPEN: + { //fprintf(f, "moveto: %f %f\n", bp->x3, bp->y3); break; + } case NR_CURVETO: - + { //fprintf(f, " /*%4d*/ <%f, %f>, <%f, %f>, <%f,%f>, <%f,%f>", // segmentNr++, lastx, lasty, x1, y1, x2, y2, x3, y3); - fprintf(f, " /*%4d*/ <%s, %s>, <%s, %s>, <%s,%s>, <%s,%s>", - segmentNr++, - dstr(s1, lastx), dstr(s2, lasty), - dstr(s3, x1), dstr(s4, y1), - dstr(s5, x2), dstr(s6, y2), - dstr(s7, x3), dstr(s8, y3)); + String seg = formatSeg(segmentNr++, + lastx, lasty, x1, y1, x2, y2, x3, y3); + fprintf(f, "%s", seg.c_str()); if (segmentNr < segmentCount) fprintf(f, ",\n"); @@ -295,17 +376,14 @@ PovOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const * if (lasty > cmaxy) cmaxy = lasty; break; - + } case NR_LINETO: - + { //fprintf(f, " /*%4d*/ <%f, %f>, <%f, %f>, <%f,%f>, <%f,%f>", // segmentNr++, lastx, lasty, lastx, lasty, x3, y3, x3, y3); - fprintf(f, " /*%4d*/ <%s, %s>, <%s, %s>, <%s,%s>, <%s,%s>", - segmentNr++, - dstr(s1, lastx), dstr(s2, lasty), - dstr(s3, lastx), dstr(s4, lasty), - dstr(s5, x3), dstr(s6, y3), - dstr(s7, x3), dstr(s8, y3)); + String seg = formatSeg(segmentNr++, + lastx, lasty, lastx, lasty, x3, y3, x3, y3); + fprintf(f, "%s", seg.c_str()); if (segmentNr < segmentCount) fprintf(f, ",\n"); @@ -322,40 +400,36 @@ PovOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const * if (lasty > cmaxy) cmaxy = lasty; break; + } case NR_END: + { //fprintf(f, "end\n"); break; + } } lastx = x3; lasty = y3; + bp++; } fprintf(f, "}\n"); - /* - fprintf(f, "#declare %s_MIN_X = %4.3f;\n", id, cminx); - fprintf(f, "#declare %s_CENTER_X = %4.3f;\n", id, (cmaxx+cminx)/2.0); - fprintf(f, "#declare %s_MAX_X = %4.3f;\n", id, cmaxx); - fprintf(f, "#declare %s_WIDTH = %4.3f;\n", id, cmaxx-cminx); - fprintf(f, "#declare %s_MIN_Y = %4.3f;\n", id, cminy); - fprintf(f, "#declare %s_CENTER_Y = %4.3f;\n", id, (cmaxy+cminy)/2.0); - fprintf(f, "#declare %s_MAX_Y = %4.3f;\n", id, cmaxy); - fprintf(f, "#declare %s_HEIGHT = %4.3f;\n", id, cmaxy-cminy); - */ - fprintf(f, "#declare %s_MIN_X = %s;\n", id, dstr(s1, cminx)); - fprintf(f, "#declare %s_CENTER_X = %s;\n", id, dstr(s1, (cmaxx+cminx)/2.0)); - fprintf(f, "#declare %s_MAX_X = %s;\n", id, dstr(s1, cmaxx)); - fprintf(f, "#declare %s_WIDTH = %s;\n", id, dstr(s1, cmaxx-cminx)); - fprintf(f, "#declare %s_MIN_Y = %s;\n", id, dstr(s1, cminy)); - fprintf(f, "#declare %s_CENTER_Y = %s;\n", id, dstr(s1, (cmaxy+cminy)/2.0)); - fprintf(f, "#declare %s_MAX_Y = %s;\n", id, dstr(s1, cmaxy)); - fprintf(f, "#declare %s_HEIGHT = %s;\n", id, dstr(s1, cmaxy-cminy)); + char *pfx = (char *)id.c_str(); + + fprintf(f, "#declare %s_MIN_X = %s;\n", pfx, dstr(cminx)); + fprintf(f, "#declare %s_CENTER_X = %s;\n", pfx, dstr((cmaxx+cminx)/2.0)); + fprintf(f, "#declare %s_MAX_X = %s;\n", pfx, dstr(cmaxx)); + fprintf(f, "#declare %s_WIDTH = %s;\n", pfx, dstr(cmaxx-cminx)); + fprintf(f, "#declare %s_MIN_Y = %s;\n", pfx, dstr(cminy)); + fprintf(f, "#declare %s_CENTER_Y = %s;\n", pfx, dstr((cmaxy+cminy)/2.0)); + fprintf(f, "#declare %s_MAX_Y = %s;\n", pfx, dstr(cmaxy)); + fprintf(f, "#declare %s_HEIGHT = %s;\n", pfx, dstr(cmaxy-cminy)); if (shapeInfo.color.length()>0) fprintf(f, "#declare %s_COLOR = %s;\n", - id, shapeInfo.color.c_str()); - fprintf(f, "/*##############################################\n"); - fprintf(f, "### end %s\n", id); - fprintf(f, "##############################################*/\n\n\n\n"); + pfx, shapeInfo.color.c_str()); + fprintf(f, "/*###################################################\n"); + fprintf(f, "### end %s\n", id.c_str()); + fprintf(f, "###################################################*/\n\n\n\n"); if (cminx < minx) minx = cminx; if (cmaxx > maxx) @@ -370,36 +444,39 @@ PovOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const * //## Let's make a union of all of the Shapes - if (povShapes.size()>0) { - char const *id = "AllShapes"; - fprintf(f, "/*##############################################\n"); + if (povShapes.size()>0) + { + String id = "AllShapes"; + char *pfx = (char *)id.c_str(); + fprintf(f, "/*###################################################\n"); fprintf(f, "### UNION OF ALL SHAPES IN DOCUMENT\n"); - fprintf(f, "##############################################*/\n"); + fprintf(f, "###################################################*/\n"); fprintf(f, "\n\n"); fprintf(f, "/**\n"); fprintf(f, " * Allow the user to redefine the finish{}\n"); fprintf(f, " * by declaring it before #including this file\n"); fprintf(f, " */\n"); - fprintf(f, "#ifndef (%s_Finish)\n", id); - fprintf(f, "#declare %s_Finish = finish {\n", id); + fprintf(f, "#ifndef (%s_Finish)\n", pfx); + fprintf(f, "#declare %s_Finish = finish {\n", pfx); fprintf(f, " phong 0.5\n"); fprintf(f, " reflection 0.3\n"); fprintf(f, " specular 0.5\n"); fprintf(f, "}\n"); fprintf(f, "#end\n"); fprintf(f, "\n\n"); - fprintf(f, "#declare %s = union {\n", id); - for (unsigned i = 0 ; i < povShapes.size() ; i++) { + fprintf(f, "#declare %s = union {\n", id.c_str()); + for (unsigned i = 0 ; i < povShapes.size() ; i++) + { fprintf(f, " object { %s\n", povShapes[i].id.c_str()); fprintf(f, " texture { \n"); if (povShapes[i].color.length()>0) fprintf(f, " pigment { %s }\n", povShapes[i].color.c_str()); else fprintf(f, " pigment { rgb <0,0,0> }\n"); - fprintf(f, " finish { %s_Finish }\n", id); + fprintf(f, " finish { %s_Finish }\n", pfx); fprintf(f, " } \n"); fprintf(f, " } \n"); - } + } fprintf(f, "}\n\n\n\n"); @@ -410,57 +487,76 @@ PovOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const * fprintf(f, " * Allow the user to redefine the Z-Increment\n"); fprintf(f, " */\n"); fprintf(f, "#ifndef (AllShapes_Z_Increment)\n"); - fprintf(f, "#declare AllShapes_Z_Increment = %s;\n", dstr(s1, zinc)); + fprintf(f, "#declare AllShapes_Z_Increment = %s;\n", dstr(zinc)); fprintf(f, "#end\n"); fprintf(f, "\n"); fprintf(f, "#declare AllShapes_Z_Scale = 1.0;\n"); fprintf(f, "\n\n"); - fprintf(f, "#declare %s_Z = union {\n", id); - for (unsigned i = 0 ; i < povShapes.size() ; i++) { + fprintf(f, "#declare %s_Z = union {\n", pfx); + + for (unsigned i = 0 ; i < povShapes.size() ; i++) + { fprintf(f, " object { %s\n", povShapes[i].id.c_str()); fprintf(f, " texture { \n"); if (povShapes[i].color.length()>0) fprintf(f, " pigment { %s }\n", povShapes[i].color.c_str()); else fprintf(f, " pigment { rgb <0,0,0> }\n"); - fprintf(f, " finish { %s_Finish }\n", id); + fprintf(f, " finish { %s_Finish }\n", pfx); fprintf(f, " } \n"); - fprintf(f, " scale <1, %s_Z_Scale, 1>\n", id); + fprintf(f, " scale <1, %s_Z_Scale, 1>\n", pfx); fprintf(f, " } \n"); fprintf(f, "#declare %s_Z_Scale = %s_Z_Scale + %s_Z_Increment;\n\n", - id, id, id); - } + pfx, pfx, pfx); + } fprintf(f, "}\n"); - /* - fprintf(f, "#declare %s_MIN_X = %4.3f;\n", id, minx); - fprintf(f, "#declare %s_CENTER_X = %4.3f;\n", id, (maxx+minx)/2.0); - fprintf(f, "#declare %s_MAX_X = %4.3f;\n", id, maxx); - fprintf(f, "#declare %s_WIDTH = %4.3f;\n", id, maxx-minx); - fprintf(f, "#declare %s_MIN_Y = %4.3f;\n", id, miny); - fprintf(f, "#declare %s_CENTER_Y = %4.3f;\n", id, (maxy+miny)/2.0); - fprintf(f, "#declare %s_MAX_Y = %4.3f;\n", id, maxy); - fprintf(f, "#declare %s_HEIGHT = %4.3f;\n", id, maxy-miny); - */ - fprintf(f, "#declare %s_MIN_X = %s;\n", id, dstr(s1, minx)); - fprintf(f, "#declare %s_CENTER_X = %s;\n", id, dstr(s1, (maxx+minx)/2.0)); - fprintf(f, "#declare %s_MAX_X = %s;\n", id, dstr(s1, maxx)); - fprintf(f, "#declare %s_WIDTH = %s;\n", id, dstr(s1, maxx-minx)); - fprintf(f, "#declare %s_MIN_Y = %s;\n", id, dstr(s1, miny)); - fprintf(f, "#declare %s_CENTER_Y = %s;\n", id, dstr(s1, (maxy+miny)/2.0)); - fprintf(f, "#declare %s_MAX_Y = %s;\n", id, dstr(s1, maxy)); - fprintf(f, "#declare %s_HEIGHT = %s;\n", id, dstr(s1, maxy-miny)); + + fprintf(f, "#declare %s_MIN_X = %s;\n", pfx, dstr(minx)); + fprintf(f, "#declare %s_CENTER_X = %s;\n", pfx, dstr((maxx+minx)/2.0)); + fprintf(f, "#declare %s_MAX_X = %s;\n", pfx, dstr(maxx)); + fprintf(f, "#declare %s_WIDTH = %s;\n", pfx, dstr(maxx-minx)); + fprintf(f, "#declare %s_MIN_Y = %s;\n", pfx, dstr(miny)); + fprintf(f, "#declare %s_CENTER_Y = %s;\n", pfx, dstr((maxy+miny)/2.0)); + fprintf(f, "#declare %s_MAX_Y = %s;\n", pfx, dstr(maxy)); + fprintf(f, "#declare %s_HEIGHT = %s;\n", pfx, dstr(maxy-miny)); fprintf(f, "/*##############################################\n"); - fprintf(f, "### end %s\n", id); + fprintf(f, "### end %s\n", id.c_str()); fprintf(f, "##############################################*/\n\n\n\n"); - } + } //All done fclose(f); } + + + +//######################################################################## +//# EXTENSION API +//######################################################################## + + + #include "clear-n_.h" + + +/** + * Make sure that we are in the database + */ +bool PovOutput::check (Inkscape::Extension::Extension *module) +{ + /* We don't need a Key + if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_OUTPUT_POV)) + return FALSE; + */ + + return true; +} + + + /** * This is the definition of PovRay output. This function just * calls the extension system with the memory allocated XML that -- 2.30.2