29d6d48d5a02c8b91191d1e6ed1fc7b55aa029a6
1 /*
2 * A simple utility for exporting Inkscape svg Shapes as PovRay bezier
3 * prisms. Note that this is output-only, and would thus seem to be
4 * better placed as an 'export' rather than 'output'. However, Export
5 * handles all or partial documents, while this outputs ALL shapes in
6 * the current SVG document.
7 *
8 * For information on the PovRay file format, see:
9 * http://www.povray.org
10 *
11 * Authors:
12 * Bob Jamison <rjamison@titan.com>
13 *
14 * Copyright (C) 2004 Authors
15 *
16 * Released under GNU GPL, read the file 'COPYING' for more information
17 */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23 #include "pov-out.h"
24 #include "inkscape.h"
25 #include "sp-path.h"
26 #include <style.h>
27 #include "display/curve.h"
28 #include "libnr/n-art-bpath.h"
29 #include "extension/system.h"
33 #include "io/sys.h"
35 namespace Inkscape {
36 namespace Extension {
37 namespace Internal {
41 static const char *
42 dstr(gchar *sbuffer, double d)
43 {
44 return (const char *)g_ascii_formatd(sbuffer,
45 G_ASCII_DTOSTR_BUF_SIZE, "%.8g", (gdouble)d);
47 }
50 /**
51 * Make sure that we are in the database
52 */
53 bool
54 PovOutput::check (Inkscape::Extension::Extension *module)
55 {
56 /* We don't need a Key
57 if (NULL == Inkscape::Extension::db.get(SP_MODULE_KEY_OUTPUT_POV))
58 return FALSE;
59 */
61 return TRUE;
62 }
67 /**
68 * This function searches the Repr tree recursively from the given node,
69 * and adds refs to all nodes with the given name, to the result vector
70 */
71 static void
72 findElementsByTagName(std::vector<Inkscape::XML::Node *> &results,
73 Inkscape::XML::Node *node,
74 char const *name)
75 {
76 if ( !name
77 || strcmp(node->name(), name) == 0 ) {
78 results.push_back(node);
79 }
81 for (Inkscape::XML::Node *child = node->firstChild() ; child ; child = child->next())
82 findElementsByTagName( results, child, name );
84 }
87 /**
88 * used for saving information about shapes
89 */
90 class PovShapeInfo
91 {
92 public:
93 PovShapeInfo()
94 {}
95 virtual ~PovShapeInfo()
96 {}
97 std::string id;
98 std::string color;
99 };
103 static double
104 effective_opacity(SPItem const *item)
105 {
106 double ret = 1.0;
107 for (SPObject const *obj = item; obj; obj = obj->parent) {
108 SPStyle const *const style = SP_OBJECT_STYLE(obj);
109 g_return_val_if_fail(style, ret);
110 ret *= SP_SCALE24_TO_FLOAT(style->opacity.value);
111 }
112 return ret;
113 }
116 /**
117 * Saves the <paths> of an Inkscape SVG file as PovRay spline definitions
118 */
119 void
120 PovOutput::save(Inkscape::Extension::Output *mod, SPDocument *doc, gchar const *uri)
121 {
122 std::vector<Inkscape::XML::Node *>results;
123 //findElementsByTagName(results, SP_ACTIVE_DOCUMENT->rroot, "path");
124 findElementsByTagName(results, SP_ACTIVE_DOCUMENT->rroot, NULL);//Check all nodes
125 if (results.size() == 0)
126 return;
127 Inkscape::IO::dump_fopen_call(uri, "L");
128 FILE *f = Inkscape::IO::fopen_utf8name(uri, "w");
129 if (!f)
130 return;
132 time_t tim = time(NULL);
133 fprintf(f, "/*#################################################\n");
134 fprintf(f, "### This PovRay document was generated by Inkscape\n");
135 fprintf(f, "### http://www.inkscape.org\n");
136 fprintf(f, "### Created: %s", ctime(&tim));
137 fprintf(f, "##################################################*/\n\n\n");
139 std::vector<PovShapeInfo>povShapes; //A list for saving information about the shapes
141 //we only need 8 for printf() calls that call dstr() 8 times
142 gchar s1[G_ASCII_DTOSTR_BUF_SIZE + 1];
143 gchar s2[G_ASCII_DTOSTR_BUF_SIZE + 1];
144 gchar s3[G_ASCII_DTOSTR_BUF_SIZE + 1];
145 gchar s4[G_ASCII_DTOSTR_BUF_SIZE + 1];
146 gchar s5[G_ASCII_DTOSTR_BUF_SIZE + 1];
147 gchar s6[G_ASCII_DTOSTR_BUF_SIZE + 1];
148 gchar s7[G_ASCII_DTOSTR_BUF_SIZE + 1];
149 gchar s8[G_ASCII_DTOSTR_BUF_SIZE + 1];
151 double bignum = 1000000.0;
152 double minx = bignum;
153 double maxx = -bignum;
154 double miny = bignum;
155 double maxy = -bignum;
159 unsigned indx;
160 for (indx = 0; indx < results.size() ; indx++)
161 {
162 //### Fetch the object from the repr info
163 Inkscape::XML::Node *rpath = results[indx];
164 gchar *id = (gchar *)rpath->attribute("id");
165 SPObject *reprobj = SP_ACTIVE_DOCUMENT->getObjectByRepr(rpath);
166 if (!reprobj)
167 continue;
169 //### Get the transform of the item
170 if (!SP_IS_ITEM(reprobj))
171 {
172 continue;
173 }
174 SPItem *item = SP_ITEM(reprobj);
175 NR::Matrix tf = sp_item_i2d_affine(item);
177 //### Get the Shape
178 if (!SP_IS_SHAPE(reprobj))//Bulia's suggestion. Allow all shapes
179 {
180 continue;
181 }
182 SPShape *shape = SP_SHAPE(reprobj);
183 SPCurve *curve = shape->curve;
184 if (sp_curve_empty(curve))
185 continue;
187 PovShapeInfo shapeInfo;
189 shapeInfo.id = id;
190 shapeInfo.color = "";
192 //Try to get the fill color of the shape
193 SPStyle *style = SP_OBJECT_STYLE(shape);
194 /* fixme: Handle other fill types, even if this means translating gradients to a single
195 flat colour. */
196 if (style && (style->fill.type == SP_PAINT_TYPE_COLOR)) {
197 // see color.h for how to parse SPColor
198 float rgb[3];
199 sp_color_get_rgb_floatv(&style->fill.value.color, rgb);
200 double const dopacity = ( SP_SCALE24_TO_FLOAT(style->fill_opacity.value)
201 * effective_opacity(shape) );
202 //gchar *str = g_strdup_printf("rgbf < %1.3f, %1.3f, %1.3f %1.3f>",
203 // rgb[0], rgb[1], rgb[2], 1.0 - dopacity);
204 gchar *str = g_strdup_printf("rgbf < %s, %s, %s %s>",
205 dstr(s1, rgb[0]), dstr(s2, rgb[1]), dstr(s3, rgb[2]), dstr(s4, 1.0 - dopacity));
207 shapeInfo.color += str;
208 g_free(str);
209 }
211 povShapes.push_back(shapeInfo); //passed all tests. save the info
213 int curveNr;
215 //Count the NR_CURVETOs/LINETOs
216 int segmentCount=0;
217 NArtBpath *bp = curve->bpath;
218 for (curveNr=0 ; curveNr<curve->length ; curveNr++, bp++)
219 if (bp->code == NR_CURVETO || bp->code == NR_LINETO)
220 segmentCount++;
222 bp = curve->bpath;
223 double cminx = bignum;
224 double cmaxx = -bignum;
225 double cminy = bignum;
226 double cmaxy = -bignum;
227 double lastx = 0.0;
228 double lasty = 0.0;
230 fprintf(f, "/*##############################################\n");
231 fprintf(f, "### PRISM: %s\n", id);
232 fprintf(f, "##############################################*/\n");
233 fprintf(f, "#declare %s = prism {\n", id);
234 fprintf(f, " linear_sweep\n");
235 fprintf(f, " bezier_spline\n");
236 fprintf(f, " 1.0, //top\n");
237 fprintf(f, " 0.0, //bottom\n");
238 fprintf(f, " %d, //nr points\n", segmentCount * 4);
239 int segmentNr = 0;
240 for (bp = curve->bpath, curveNr=0 ; curveNr<curve->length ; curveNr++, bp++) {
241 using NR::X;
242 using NR::Y;
243 NR::Point const p1(bp->c(1) * tf);
244 NR::Point const p2(bp->c(2) * tf);
245 NR::Point const p3(bp->c(3) * tf);
246 double const x1 = p1[X], y1 = p1[Y];
247 double const x2 = p2[X], y2 = p2[Y];
248 double const x3 = p3[X], y3 = p3[Y];
250 switch (bp->code) {
251 case NR_MOVETO:
252 case NR_MOVETO_OPEN:
253 //fprintf(f, "moveto: %f %f\n", bp->x3, bp->y3);
254 break;
255 case NR_CURVETO:
257 //fprintf(f, " /*%4d*/ <%f, %f>, <%f, %f>, <%f,%f>, <%f,%f>",
258 // segmentNr++, lastx, lasty, x1, y1, x2, y2, x3, y3);
259 fprintf(f, " /*%4d*/ <%s, %s>, <%s, %s>, <%s,%s>, <%s,%s>",
260 segmentNr++,
261 dstr(s1, lastx), dstr(s2, lasty),
262 dstr(s3, x1), dstr(s4, y1),
263 dstr(s5, x2), dstr(s6, y2),
264 dstr(s7, x3), dstr(s8, y3));
266 if (segmentNr < segmentCount)
267 fprintf(f, ",\n");
268 else
269 fprintf(f, "\n");
271 if (lastx < cminx)
272 cminx = lastx;
273 if (lastx > cmaxx)
274 cmaxx = lastx;
275 if (lasty < cminy)
276 cminy = lasty;
277 if (lasty > cmaxy)
278 cmaxy = lasty;
279 break;
280 case NR_LINETO:
282 //fprintf(f, " /*%4d*/ <%f, %f>, <%f, %f>, <%f,%f>, <%f,%f>",
283 // segmentNr++, lastx, lasty, lastx, lasty, x3, y3, x3, y3);
284 fprintf(f, " /*%4d*/ <%s, %s>, <%s, %s>, <%s,%s>, <%s,%s>",
285 segmentNr++,
286 dstr(s1, lastx), dstr(s2, lasty),
287 dstr(s3, lastx), dstr(s4, lasty),
288 dstr(s5, x3), dstr(s6, y3),
289 dstr(s7, x3), dstr(s8, y3));
291 if (segmentNr < segmentCount)
292 fprintf(f, ",\n");
293 else
294 fprintf(f, "\n");
296 //fprintf(f, "lineto\n");
297 if (lastx < cminx)
298 cminx = lastx;
299 if (lastx > cmaxx)
300 cmaxx = lastx;
301 if (lasty < cminy)
302 cminy = lasty;
303 if (lasty > cmaxy)
304 cmaxy = lasty;
305 break;
306 case NR_END:
307 //fprintf(f, "end\n");
308 break;
309 }
310 lastx = x3;
311 lasty = y3;
312 }
313 fprintf(f, "}\n");
314 /*
315 fprintf(f, "#declare %s_MIN_X = %4.3f;\n", id, cminx);
316 fprintf(f, "#declare %s_CENTER_X = %4.3f;\n", id, (cmaxx+cminx)/2.0);
317 fprintf(f, "#declare %s_MAX_X = %4.3f;\n", id, cmaxx);
318 fprintf(f, "#declare %s_WIDTH = %4.3f;\n", id, cmaxx-cminx);
319 fprintf(f, "#declare %s_MIN_Y = %4.3f;\n", id, cminy);
320 fprintf(f, "#declare %s_CENTER_Y = %4.3f;\n", id, (cmaxy+cminy)/2.0);
321 fprintf(f, "#declare %s_MAX_Y = %4.3f;\n", id, cmaxy);
322 fprintf(f, "#declare %s_HEIGHT = %4.3f;\n", id, cmaxy-cminy);
323 */
324 fprintf(f, "#declare %s_MIN_X = %s;\n", id, dstr(s1, cminx));
325 fprintf(f, "#declare %s_CENTER_X = %s;\n", id, dstr(s1, (cmaxx+cminx)/2.0));
326 fprintf(f, "#declare %s_MAX_X = %s;\n", id, dstr(s1, cmaxx));
327 fprintf(f, "#declare %s_WIDTH = %s;\n", id, dstr(s1, cmaxx-cminx));
328 fprintf(f, "#declare %s_MIN_Y = %s;\n", id, dstr(s1, cminy));
329 fprintf(f, "#declare %s_CENTER_Y = %s;\n", id, dstr(s1, (cmaxy+cminy)/2.0));
330 fprintf(f, "#declare %s_MAX_Y = %s;\n", id, dstr(s1, cmaxy));
331 fprintf(f, "#declare %s_HEIGHT = %s;\n", id, dstr(s1, cmaxy-cminy));
332 if (shapeInfo.color.length()>0)
333 fprintf(f, "#declare %s_COLOR = %s;\n",
334 id, shapeInfo.color.c_str());
335 fprintf(f, "/*##############################################\n");
336 fprintf(f, "### end %s\n", id);
337 fprintf(f, "##############################################*/\n\n\n\n");
338 if (cminx < minx)
339 minx = cminx;
340 if (cmaxx > maxx)
341 maxx = cmaxx;
342 if (cminy < miny)
343 miny = cminy;
344 if (cmaxy > maxy)
345 maxy = cmaxy;
348 }//for
352 //## Let's make a union of all of the Shapes
353 if (!povShapes.empty()) {
354 char const *id = "AllShapes";
355 fprintf(f, "/*##############################################\n");
356 fprintf(f, "### UNION OF ALL SHAPES IN DOCUMENT\n");
357 fprintf(f, "##############################################*/\n");
358 fprintf(f, "\n\n");
359 fprintf(f, "/**\n");
360 fprintf(f, " * Allow the user to redefine the finish{}\n");
361 fprintf(f, " * by declaring it before #including this file\n");
362 fprintf(f, " */\n");
363 fprintf(f, "#ifndef (%s_Finish)\n", id);
364 fprintf(f, "#declare %s_Finish = finish {\n", id);
365 fprintf(f, " phong 0.5\n");
366 fprintf(f, " reflection 0.3\n");
367 fprintf(f, " specular 0.5\n");
368 fprintf(f, "}\n");
369 fprintf(f, "#end\n");
370 fprintf(f, "\n\n");
371 fprintf(f, "#declare %s = union {\n", id);
372 for (unsigned i = 0 ; i < povShapes.size() ; i++) {
373 fprintf(f, " object { %s\n", povShapes[i].id.c_str());
374 fprintf(f, " texture { \n");
375 if (povShapes[i].color.length()>0)
376 fprintf(f, " pigment { %s }\n", povShapes[i].color.c_str());
377 else
378 fprintf(f, " pigment { rgb <0,0,0> }\n");
379 fprintf(f, " finish { %s_Finish }\n", id);
380 fprintf(f, " } \n");
381 fprintf(f, " } \n");
382 }
383 fprintf(f, "}\n\n\n\n");
386 double zinc = 0.2 / (double)povShapes.size();
387 fprintf(f, "/*#### Same union, but with Z-diffs (actually Y in pov) ####*/\n");
388 fprintf(f, "\n\n");
389 fprintf(f, "/**\n");
390 fprintf(f, " * Allow the user to redefine the Z-Increment\n");
391 fprintf(f, " */\n");
392 fprintf(f, "#ifndef (AllShapes_Z_Increment)\n");
393 fprintf(f, "#declare AllShapes_Z_Increment = %s;\n", dstr(s1, zinc));
394 fprintf(f, "#end\n");
395 fprintf(f, "\n");
396 fprintf(f, "#declare AllShapes_Z_Scale = 1.0;\n");
397 fprintf(f, "\n\n");
398 fprintf(f, "#declare %s_Z = union {\n", id);
399 for (unsigned i = 0 ; i < povShapes.size() ; i++) {
400 fprintf(f, " object { %s\n", povShapes[i].id.c_str());
401 fprintf(f, " texture { \n");
402 if (povShapes[i].color.length()>0)
403 fprintf(f, " pigment { %s }\n", povShapes[i].color.c_str());
404 else
405 fprintf(f, " pigment { rgb <0,0,0> }\n");
406 fprintf(f, " finish { %s_Finish }\n", id);
407 fprintf(f, " } \n");
408 fprintf(f, " scale <1, %s_Z_Scale, 1>\n", id);
409 fprintf(f, " } \n");
410 fprintf(f, "#declare %s_Z_Scale = %s_Z_Scale + %s_Z_Increment;\n\n",
411 id, id, id);
412 }
414 fprintf(f, "}\n");
415 /*
416 fprintf(f, "#declare %s_MIN_X = %4.3f;\n", id, minx);
417 fprintf(f, "#declare %s_CENTER_X = %4.3f;\n", id, (maxx+minx)/2.0);
418 fprintf(f, "#declare %s_MAX_X = %4.3f;\n", id, maxx);
419 fprintf(f, "#declare %s_WIDTH = %4.3f;\n", id, maxx-minx);
420 fprintf(f, "#declare %s_MIN_Y = %4.3f;\n", id, miny);
421 fprintf(f, "#declare %s_CENTER_Y = %4.3f;\n", id, (maxy+miny)/2.0);
422 fprintf(f, "#declare %s_MAX_Y = %4.3f;\n", id, maxy);
423 fprintf(f, "#declare %s_HEIGHT = %4.3f;\n", id, maxy-miny);
424 */
425 fprintf(f, "#declare %s_MIN_X = %s;\n", id, dstr(s1, minx));
426 fprintf(f, "#declare %s_CENTER_X = %s;\n", id, dstr(s1, (maxx+minx)/2.0));
427 fprintf(f, "#declare %s_MAX_X = %s;\n", id, dstr(s1, maxx));
428 fprintf(f, "#declare %s_WIDTH = %s;\n", id, dstr(s1, maxx-minx));
429 fprintf(f, "#declare %s_MIN_Y = %s;\n", id, dstr(s1, miny));
430 fprintf(f, "#declare %s_CENTER_Y = %s;\n", id, dstr(s1, (maxy+miny)/2.0));
431 fprintf(f, "#declare %s_MAX_Y = %s;\n", id, dstr(s1, maxy));
432 fprintf(f, "#declare %s_HEIGHT = %s;\n", id, dstr(s1, maxy-miny));
433 fprintf(f, "/*##############################################\n");
434 fprintf(f, "### end %s\n", id);
435 fprintf(f, "##############################################*/\n\n\n\n");
436 }
438 //All done
439 fclose(f);
440 }
442 /**
443 * This is the definition of PovRay output. This function just
444 * calls the extension system with the memory allocated XML that
445 * describes the data.
446 */
447 void
448 PovOutput::init()
449 {
450 Inkscape::Extension::build_from_mem(
451 "<inkscape-extension>\n"
452 "<name>PovRay Output</name>\n"
453 "<id>org.inkscape.output.pov</id>\n"
454 "<output>\n"
455 "<extension>.pov</extension>\n"
456 "<mimetype>text/x-povray-script</mimetype>\n"
457 "<filetypename>PovRay (*.pov) (export splines)</filetypename>\n"
458 "<filetypetooltip>PovRay Raytracer File</filetypetooltip>\n"
459 "</output>\n"
460 "</inkscape-extension>",
461 new PovOutput());
462 }
468 } //namespace Internal
469 } //namespace Extension
470 } //namespace Inkscape
473 /*
474 Local Variables:
475 mode:c++
476 c-file-style:"stroustrup"
477 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
478 indent-tabs-mode:nil
479 fill-column:99
480 End:
481 */
482 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :