X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fextension%2Finternal%2Fpov-out.cpp;h=6b8a5ce070744cb441d3da50119fc146cd632710;hb=025db6f90298127a666cd24bcfc2e22fd35ca84d;hp=4a09512247815a881e1876cf9960742aa5fcb775;hpb=a9e6d5a48e571acc0dbffe7b4149852b638dc6ca;p=inkscape.git diff --git a/src/extension/internal/pov-out.cpp b/src/extension/internal/pov-out.cpp index 4a0951224..6b8a5ce07 100644 --- a/src/extension/internal/pov-out.cpp +++ b/src/extension/internal/pov-out.cpp @@ -26,13 +26,13 @@ #include #include #include -#include #include #include <2geom/pathvector.h> #include <2geom/rect.h> #include <2geom/bezier-curve.h> #include <2geom/hvlinesegment.h> #include "helper/geom.h" +#include "helper/geom-curves.h" #include #include @@ -48,33 +48,26 @@ namespace Internal { - - //######################################################################## -//# U T I L I T Y +//# M E S S A G E S //######################################################################## - - -/** - * This function searches the Repr tree recursively from the given node, - * and adds refs to all nodes with the given name, to the result vector - */ -static void -findElementsByTagName(std::vector &results, - Inkscape::XML::Node *node, - char const *name) +static void err(const char *fmt, ...) { - if ( !name || strcmp(node->name(), name) == 0 ) - results.push_back(node); + va_list args; + g_log(NULL, G_LOG_LEVEL_WARNING, "Pov-out err: "); + va_start(args, fmt); + g_logv(NULL, G_LOG_LEVEL_WARNING, fmt, args); + va_end(args); + g_log(NULL, G_LOG_LEVEL_WARNING, "\n"); +} - for (Inkscape::XML::Node *child = node->firstChild() ; child ; - child = child->next()) - findElementsByTagName( results, child, name ); -} +//######################################################################## +//# U T I L I T Y +//######################################################################## @@ -112,7 +105,7 @@ static PovOutput::String dstr(double d) return s; } - +#define DSTR(d) (dstr(d).c_str()) /** @@ -137,7 +130,7 @@ void PovOutput::out(const char *fmt, ...) */ void PovOutput::vec2(double a, double b) { - out("<%s, %s>", dstr(a).c_str(), dstr(b).c_str()); + out("<%s, %s>", DSTR(a), DSTR(b)); } @@ -147,7 +140,7 @@ void PovOutput::vec2(double a, double b) */ void PovOutput::vec3(double a, double b, double c) { - out("<%s, %s, %s>", dstr(a).c_str(), dstr(b).c_str(), dstr(c).c_str()); + out("<%s, %s, %s>", DSTR(a), DSTR(b), DSTR(c)); } @@ -157,8 +150,7 @@ void PovOutput::vec3(double a, double b, double c) */ void PovOutput::vec4(double a, double b, double c, double d) { - out("<%s, %s, %s, %s>", dstr(a).c_str(), dstr(b).c_str(), - dstr(c).c_str(), dstr(d).c_str()); + out("<%s, %s, %s, %s>", DSTR(a), DSTR(b), DSTR(c), DSTR(d)); } @@ -208,7 +200,7 @@ bool PovOutput::doHeader() out("/*###################################################################\n"); out("### This PovRay document was generated by Inkscape\n"); out("### http://www.inkscape.org\n"); - out("### Created: %s", ctime(&tim)); + out("### Created: %s", ctime(&tim)); out("### Version: %s\n", INKSCAPE_VERSION); out("#####################################################################\n"); out("### NOTES:\n"); @@ -261,63 +253,33 @@ bool PovOutput::doTail() /** * Output the curve data to buffer */ -bool PovOutput::doCurves(SPDocument *doc) +bool PovOutput::doCurve(SPItem *item, const String &id) { using Geom::X; using Geom::Y; - std::vectorresults; - //findElementsByTagName(results, SP_ACTIVE_DOCUMENT->rroot, "path"); - findElementsByTagName(results, SP_ACTIVE_DOCUMENT->rroot, NULL); - if (results.size() == 0) + //### Get the Shape + if (!SP_IS_SHAPE(item))//Bulia's suggestion. Allow all shapes return true; - double bignum = 1000000.0; - double minx = bignum; - double maxx = -bignum; - double miny = bignum; - double maxy = -bignum; + SPShape *shape = SP_SHAPE(item); + SPCurve *curve = shape->curve; + if (curve->is_empty()) + return true; - for (unsigned int indx = 0; indx < results.size() ; indx++) - { - //### Fetch the object from the repr info - Inkscape::XML::Node *rpath = results[indx]; - char *str = (char *) rpath->attribute("id"); - if (!str) - continue; - - String id = str; - SPObject *reprobj = SP_ACTIVE_DOCUMENT->getObjectByRepr(rpath); - if (!reprobj) - continue; - - //### Get the transform of the item - if (!SP_IS_ITEM(reprobj)) - continue; - - SPItem *item = SP_ITEM(reprobj); - Geom::Matrix tf = sp_item_i2d_affine(item); - - //### Get the Shape - if (!SP_IS_SHAPE(reprobj))//Bulia's suggestion. Allow all shapes - continue; - - SPShape *shape = SP_SHAPE(reprobj); - SPCurve *curve = shape->curve; - if (curve->is_empty()) - continue; - - nrShapes++; - - PovShapeInfo shapeInfo; - shapeInfo.id = id; - shapeInfo.color = ""; - - //Try to get the fill color of the shape - SPStyle *style = SP_OBJECT_STYLE(shape); - /* fixme: Handle other fill types, even if this means translating gradients to a single + nrShapes++; + + PovShapeInfo shapeInfo; + shapeInfo.id = id; + shapeInfo.color = ""; + + //Try to get the fill color of the shape + SPStyle *style = SP_OBJECT_STYLE(shape); + /* fixme: Handle other fill types, even if this means translating gradients to a single flat colour. */ - if (style && (style->fill.isColor())) + if (style) + { + if (style->fill.isColor()) { // see color.h for how to parse SPColor float rgb[3]; @@ -333,117 +295,175 @@ bool PovOutput::doCurves(SPDocument *doc) rgbf.append(dstr(1.0 - dopacity)); rgbf.append(">"); shapeInfo.color += rgbf; } + } - povShapes.push_back(shapeInfo); //passed all tests. save the info + povShapes.push_back(shapeInfo); //passed all tests. save the info - // convert the path to only lineto's and cubic curveto's: - Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curve->get_pathvector() * tf ); + // convert the path to only lineto's and cubic curveto's: + Geom::Matrix tf = sp_item_i2d_affine(item); + Geom::PathVector pathv = pathv_to_linear_and_cubic_beziers( curve->get_pathvector() * tf ); - //Count the NR_CURVETOs/LINETOs (including closing line segment) - guint segmentCount = 0; - for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) { - segmentCount += (*it).size(); - if (it->closed()) - segmentCount += 1; + //Count the NR_CURVETOs/LINETOs (including closing line segment) + int segmentCount = 0; + for(Geom::PathVector::const_iterator it = pathv.begin(); it != pathv.end(); ++it) + { + segmentCount += (*it).size(); + if (it->closed()) + segmentCount += 1; } - out("/*###################################################\n"); - out("### PRISM: %s\n", id.c_str()); - out("###################################################*/\n"); - out("#declare %s = prism {\n", id.c_str()); - out(" linear_sweep\n"); - out(" bezier_spline\n"); - out(" 1.0, //top\n"); - out(" 0.0, //bottom\n"); - out(" %d //nr points\n", segmentCount * 4); - int segmentNr = 0; - - nrSegments += segmentCount; - - Geom::Rect cminmax( pathv.front().initialPoint(), pathv.front().initialPoint() ); // at moment of writing, 2geom lacks proper initialization of empty intervals in rect... + out("/*###################################################\n"); + out("### PRISM: %s\n", id.c_str()); + out("###################################################*/\n"); + out("#declare %s = prism {\n", id.c_str()); + out(" linear_sweep\n"); + out(" bezier_spline\n"); + out(" 1.0, //top\n"); + out(" 0.0, //bottom\n"); + out(" %d //nr points\n", segmentCount * 4); + int segmentNr = 0; + + nrSegments += segmentCount; + + /** + * at moment of writing, 2geom lacks proper initialization of empty intervals in rect... + */ + Geom::Rect cminmax( pathv.front().initialPoint(), pathv.front().initialPoint() ); - for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit) - { + /** + * For all Subpaths in the + */ + for (Geom::PathVector::const_iterator pit = pathv.begin(); pit != pathv.end(); ++pit) + { - cminmax.expandTo(pit->initialPoint()); + cminmax.expandTo(pit->initialPoint()); - for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_closed(); ++cit) - { + /** + * For all segments in the subpath + */ + for (Geom::Path::const_iterator cit = pit->begin(); cit != pit->end_closed(); ++cit) + { - if( dynamic_cast (&*cit) || - dynamic_cast(&*cit) || - dynamic_cast(&*cit) ) - { - Geom::Point p0 = cit->initialPoint() * tf; - Geom::Point p1 = cit->finalPoint() * tf; - segment(segmentNr++, - p0[X], p0[Y], p0[X], p0[Y], p1[X], p1[Y], p1[X], p1[Y] ); - nrNodes += 8; - } - else if(Geom::CubicBezier const *cubic = dynamic_cast(&*cit)) - { - std::vector points = cubic->points(); - Geom::Point p0 = points[0] * tf; - Geom::Point p1 = points[1] * tf; - Geom::Point p2 = points[2] * tf; - Geom::Point p3 = points[3] * tf; - segment(segmentNr++, + if( is_straight_curve(*cit) ) + { + Geom::Point p0 = cit->initialPoint(); + Geom::Point p1 = cit->finalPoint(); + segment(segmentNr++, + p0[X], p0[Y], p0[X], p0[Y], p1[X], p1[Y], p1[X], p1[Y] ); + nrNodes += 8; + } + else if(Geom::CubicBezier const *cubic = dynamic_cast(&*cit)) + { + std::vector points = cubic->points(); + Geom::Point p0 = points[0]; + Geom::Point p1 = points[1]; + Geom::Point p2 = points[2]; + Geom::Point p3 = points[3]; + segment(segmentNr++, p0[X],p0[Y], p1[X],p1[Y], p2[X],p2[Y], p3[X],p3[Y]); - nrNodes += 8; - } - else - { - g_warning("logical error, because pathv_to_linear_and_cubic_beziers was used"); - return false; - } + nrNodes += 8; + } + else + { + err("logical error, because pathv_to_linear_and_cubic_beziers was used"); + return false; + } - if (segmentNr <= static_cast(segmentCount)) - out(",\n"); - else - out("\n"); + if (segmentNr < segmentCount) + out(",\n"); + else + out("\n"); - cminmax.expandTo(cit->finalPoint()); + cminmax.expandTo(cit->finalPoint()); } } - out("}\n"); + out("}\n"); + + double cminx = cminmax.min()[X]; + double cmaxx = cminmax.max()[X]; + double cminy = cminmax.min()[Y]; + double cmaxy = cminmax.max()[Y]; + + out("#declare %s_MIN_X = %s;\n", id.c_str(), DSTR(cminx)); + out("#declare %s_CENTER_X = %s;\n", id.c_str(), DSTR((cmaxx+cminx)/2.0)); + out("#declare %s_MAX_X = %s;\n", id.c_str(), DSTR(cmaxx)); + out("#declare %s_WIDTH = %s;\n", id.c_str(), DSTR(cmaxx-cminx)); + out("#declare %s_MIN_Y = %s;\n", id.c_str(), DSTR(cminy)); + out("#declare %s_CENTER_Y = %s;\n", id.c_str(), DSTR((cmaxy+cminy)/2.0)); + out("#declare %s_MAX_Y = %s;\n", id.c_str(), DSTR(cmaxy)); + out("#declare %s_HEIGHT = %s;\n", id.c_str(), DSTR(cmaxy-cminy)); + if (shapeInfo.color.length()>0) + out("#declare %s_COLOR = %s;\n", + id.c_str(), shapeInfo.color.c_str()); + out("/*###################################################\n"); + out("### end %s\n", id.c_str()); + out("###################################################*/\n\n\n\n"); + + if (cminx < minx) + minx = cminx; + if (cmaxx > maxx) + maxx = cmaxx; + if (cminy < miny) + miny = cminy; + if (cmaxy > maxy) + maxy = cmaxy; - double cminx = cminmax.min()[X]; - double cmaxx = cminmax.max()[X]; - double cminy = cminmax.min()[Y]; - double cmaxy = cminmax.max()[Y]; + return true; +} - //# prefix for following declarations - char *pfx = (char *)id.c_str(); +/** + * Descend the svg tree recursively, translating data + */ +bool PovOutput::doTreeRecursive(SPDocument *doc, SPObject *obj) +{ - out("#declare %s_MIN_X = %s;\n", pfx, dstr(cminx).c_str()); - out("#declare %s_CENTER_X = %s;\n", pfx, dstr((cmaxx+cminx)/2.0).c_str()); - out("#declare %s_MAX_X = %s;\n", pfx, dstr(cmaxx).c_str()); - out("#declare %s_WIDTH = %s;\n", pfx, dstr(cmaxx-cminx).c_str()); - out("#declare %s_MIN_Y = %s;\n", pfx, dstr(cminy).c_str()); - out("#declare %s_CENTER_Y = %s;\n", pfx, dstr((cmaxy+cminy)/2.0).c_str()); - out("#declare %s_MAX_Y = %s;\n", pfx, dstr(cmaxy).c_str()); - out("#declare %s_HEIGHT = %s;\n", pfx, dstr(cmaxy-cminy).c_str()); - if (shapeInfo.color.length()>0) - out("#declare %s_COLOR = %s;\n", - pfx, shapeInfo.color.c_str()); - out("/*###################################################\n"); - out("### end %s\n", id.c_str()); - out("###################################################*/\n\n\n\n"); - if (cminx < minx) - minx = cminx; - if (cmaxx > maxx) - maxx = cmaxx; - if (cminy < miny) - miny = cminy; - if (cmaxy > maxy) - maxy = cmaxy; + String id; + if (!obj->id) + { + char buf[16]; + sprintf(buf, "id%d", idIndex++); + id = buf; + } + else + { + id = obj->id; + } - }//for + if (SP_IS_ITEM(obj)) + { + SPItem *item = SP_ITEM(obj); + if (!doCurve(item, id)) + return false; + } + /** + * Descend into children + */ + for (SPObject *child = obj->firstChild() ; child ; child = child->next) + { + if (!doTreeRecursive(doc, child)) + return false; + } + return true; +} + +/** + * Output the curve data to buffer + */ +bool PovOutput::doTree(SPDocument *doc) +{ + double bignum = 1000000.0; + minx = bignum; + maxx = -bignum; + miny = bignum; + maxy = -bignum; + + if (!doTreeRecursive(doc, doc->root)) + return false; //## Let's make a union of all of the Shapes if (povShapes.size()>0) @@ -489,7 +509,7 @@ bool PovOutput::doCurves(SPDocument *doc) out(" * Allow the user to redefine the Z-Increment\n"); out(" */\n"); out("#ifndef (AllShapes_Z_Increment)\n"); - out("#declare AllShapes_Z_Increment = %s;\n", dstr(zinc).c_str()); + out("#declare AllShapes_Z_Increment = %s;\n", DSTR(zinc)); out("#end\n"); out("\n"); out("#declare AllShapes_Z_Scale = 1.0;\n"); @@ -514,14 +534,14 @@ bool PovOutput::doCurves(SPDocument *doc) out("}\n"); - out("#declare %s_MIN_X = %s;\n", pfx, dstr(minx).c_str()); - out("#declare %s_CENTER_X = %s;\n", pfx, dstr((maxx+minx)/2.0).c_str()); - out("#declare %s_MAX_X = %s;\n", pfx, dstr(maxx).c_str()); - out("#declare %s_WIDTH = %s;\n", pfx, dstr(maxx-minx).c_str()); - out("#declare %s_MIN_Y = %s;\n", pfx, dstr(miny).c_str()); - out("#declare %s_CENTER_Y = %s;\n", pfx, dstr((maxy+miny)/2.0).c_str()); - out("#declare %s_MAX_Y = %s;\n", pfx, dstr(maxy).c_str()); - out("#declare %s_HEIGHT = %s;\n", pfx, dstr(maxy-miny).c_str()); + out("#declare %s_MIN_X = %s;\n", pfx, DSTR(minx)); + out("#declare %s_CENTER_X = %s;\n", pfx, DSTR((maxx+minx)/2.0)); + out("#declare %s_MAX_X = %s;\n", pfx, DSTR(maxx)); + out("#declare %s_WIDTH = %s;\n", pfx, DSTR(maxx-minx)); + out("#declare %s_MIN_Y = %s;\n", pfx, DSTR(miny)); + out("#declare %s_CENTER_Y = %s;\n", pfx, DSTR((maxy+miny)/2.0)); + out("#declare %s_MAX_Y = %s;\n", pfx, DSTR(maxy)); + out("#declare %s_HEIGHT = %s;\n", pfx, DSTR(maxy-miny)); out("/*##############################################\n"); out("### end %s\n", id.c_str()); out("##############################################*/\n"); @@ -532,8 +552,6 @@ bool PovOutput::doCurves(SPDocument *doc) } - - //######################################################################## //# M A I N O U T P U T //######################################################################## @@ -548,6 +566,7 @@ void PovOutput::reset() nrNodes = 0; nrSegments = 0; nrShapes = 0; + idIndex = 0; outbuf.clear(); povShapes.clear(); } @@ -563,9 +582,9 @@ void PovOutput::saveDocument(SPDocument *doc, gchar const *uri) //###### SAVE IN POV FORMAT TO BUFFER //# Lets do the curves first, to get the stats - if (!doCurves(doc)) + if (!doTree(doc)) { - g_warning("Could not output curves for %s\n", uri); + err("Could not output curves for %s", uri); return; } @@ -574,7 +593,7 @@ void PovOutput::saveDocument(SPDocument *doc, gchar const *uri) if (!doHeader()) { - g_warning("Could not write header for %s\n", uri); + err("Could not write header for %s", uri); return; } @@ -582,7 +601,7 @@ void PovOutput::saveDocument(SPDocument *doc, gchar const *uri) if (!doTail()) { - g_warning("Could not write footer for %s\n", uri); + err("Could not write footer for %s", uri); return; } @@ -621,7 +640,7 @@ void PovOutput::saveDocument(SPDocument *doc, gchar const *uri) * API call to save document */ void -PovOutput::save(Inkscape::Extension::Output *mod, +PovOutput::save(Inkscape::Extension::Output */*mod*/, SPDocument *doc, gchar const *uri) { saveDocument(doc, uri); @@ -632,7 +651,7 @@ PovOutput::save(Inkscape::Extension::Output *mod, /** * Make sure that we are in the database */ -bool PovOutput::check (Inkscape::Extension::Extension *module) +bool PovOutput::check (Inkscape::Extension::Extension */*module*/) { /* We don't need a Key if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_OUTPUT_POV))