Code

Use subdirectories with icon sizes.
[inkscape.git] / src / spray-context.cpp
index b68fe0818cf00aef002b4d78dc32635e1a0b786a..0cf74f59b97d5e617f8a841dbab2333bc478d278 100644 (file)
@@ -1,5 +1,3 @@
-#define __SP_SPRAY_CONTEXT_C__
-
 /*
  * Spray Tool
  *
@@ -11,6 +9,9 @@
  *   Benoît LAVORATA
  *   Vincent MONTAGNE
  *   Pierre BARBRY-BLOT
+ *   Steren GIANNINI (steren.giannini@gmail.com)
+ *   Jon A. Cruz <jon@joncruz.org>
+ *   Abhishek Sharma
  *
  * Copyright (C) 2009 authors
  *
 #include "desktop-style.h"
 #include "message-context.h"
 #include "pixmaps/cursor-spray.xpm"
-//#include "pixmaps/cursor-spray-move.xpm"
-//#include "pixmaps/cursor-thin.xpm"
-//#include "pixmaps/cursor-thicken.xpm"
-//#include "pixmaps/cursor-attract.xpm"
-//#include "pixmaps/cursor-repel.xpm"
-//#include "pixmaps/cursor-push.xpm"
-//#include "pixmaps/cursor-roughen.xpm"
-//#include "pixmaps/cursor-color.xpm"
 #include <boost/optional.hpp>
 #include "libnr/nr-matrix-ops.h"
 #include "libnr/nr-scale-translate-ops.h"
@@ -63,7 +56,6 @@
 #include "path-chemistry.h"
 #include "sp-gradient.h"
 #include "sp-stop.h"
-#include "sp-stop-fns.h"
 #include "sp-gradient-reference.h"
 #include "sp-linear-gradient.h"
 #include "sp-radial-gradient.h"
@@ -89,6 +81,8 @@
 #include "helper/action.h"
 
 #include <iostream>
+
+using Inkscape::DocumentUndo;
 using namespace std;
 
 
@@ -107,34 +101,15 @@ static gint sp_spray_context_root_handler(SPEventContext *ec, GdkEvent *event);
 static SPEventContextClass *parent_class = 0;
 
 
-/*
-   RAND is a macro which returns a pseudo-random numbers from a uniform
-   distribution on the interval [0 1]
-*/
-#define RAND ((double) rand())/((double) RAND_MAX)
-
-/*
-    TWOPI = 2.0*pi
-*/
-#define TWOPI 2.0*3.141592653589793238462643383279502884197169399375
-
-/*
-   RANDN is a macro which returns a pseudo-random numbers from a normal
-   distribution with mean zero and standard deviation one. This macro uses Box
-   Muller's algorithm
-*/
-#define RANDN sqrt(-2.0*log(RAND))*cos(TWOPI*RAND)
-
-
-double NormalDistribution(double mu,double sigma)
+/**
+ * This function returns pseudo-random numbers from a normal distribution
+ * @param mu : mean
+ * @param sigma : standard deviation ( > 0 )
+ */
+inline double NormalDistribution(double mu,double sigma)
 {
-/*
-   This function returns a pseudo-random numbers from a normal distribution with
-   mean equal at mu and standard deviation equal at sigma > 0
-*/
-
-  return (mu+sigma*RANDN);
-
+  // use Box Muller's algorithm
+  return mu + sigma * sqrt( -2.0 * log(g_random_double_range(0, 1)) ) * cos( 2.0*M_PI*g_random_double_range(0, 1) );
 }
 
 
@@ -178,9 +153,9 @@ void sp_spray_rotate_rel(Geom::Point c,SPDesktop */*desktop*/,SPItem *item, Geom
     Geom::Translate const s(c);
     Geom::Matrix affine = Geom::Matrix(s).inverse() * Geom::Matrix(rotation) * Geom::Matrix(s);
     // Rotate item.
-    sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * (Geom::Matrix)affine);
+    item->set_i2d_affine(item->i2d_affine() * (Geom::Matrix)affine);
     // Use each item's own transform writer, consistent with sp_selection_apply_affine()
-    sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
+    item->doWriteTransform(SP_OBJECT_REPR(item), item->transform);
     // Restore the center position (it's changed because the bbox center changed)
     if (item->isCenterSet()) {
         item->setCenter(c);
@@ -192,8 +167,8 @@ void sp_spray_rotate_rel(Geom::Point c,SPDesktop */*desktop*/,SPItem *item, Geom
 void sp_spray_scale_rel(Geom::Point c, SPDesktop */*desktop*/, SPItem *item, Geom::Scale  const &scale)
 {
     Geom::Translate const s(c);
-    sp_item_set_i2d_affine(item, sp_item_i2d_affine(item) * s.inverse() * scale * s  );
-    sp_item_write_transform(item, SP_OBJECT_REPR(item), item->transform);
+    item->set_i2d_affine(item->i2d_affine() * s.inverse() * scale * s  );
+    item->doWriteTransform(SP_OBJECT_REPR(item), item->transform);
 }
 
 static void sp_spray_context_init(SPSprayContext *tc)
@@ -261,14 +236,14 @@ void sp_spray_update_cursor(SPSprayContext *tc, bool /*with_shift*/)
    SPEventContext *event_context = SP_EVENT_CONTEXT(tc);
    SPDesktop *desktop = event_context->desktop;
 
-                guint num = 0;
-                gchar *sel_message = NULL;
-                if (!desktop->selection->isEmpty()) {
-                    num = g_slist_length((GSList *) desktop->selection->itemList());
-                    sel_message = g_strdup_printf(ngettext("<b>%i</b> object selected","<b>%i</b> objects selected",num), num);
-                } else {
-                    sel_message = g_strdup_printf(_("<b>Nothing</b> selected"));
-                }
+    guint num = 0;
+    gchar *sel_message = NULL;
+    if (!desktop->selection->isEmpty()) {
+        num = g_slist_length((GSList *) desktop->selection->itemList());
+        sel_message = g_strdup_printf(ngettext("<b>%i</b> object selected","<b>%i</b> objects selected",num), num);
+    } else {
+        sel_message = g_strdup_printf(_("<b>Nothing</b> selected"));
+    }
 
 
    switch (tc->mode) {
@@ -439,25 +414,32 @@ double get_move_standard_deviation(SPSprayContext *tc)
     return tc->standard_deviation;
 }
 
-/** Method to handle the distribution of the items */
-void random_position( double &r, double &p, double &a, double &s, int choice)
+/**
+ * Method to handle the distribution of the items
+ * @param[out]  radius : radius of the position of the sprayed object
+ * @param[out]  angle : angle of the position of the sprayed object
+ * @param[in]   a : mean
+ * @param[in]   s : standard deviation
+ * @param[in]   choice : 
+
+ */
+void random_position( double &radius, double &angle, double &a, double &s, int /*choice*/)
 {
-    if (choice == 0) // 1 : uniform repartition
-    {
-        r = (1-pow(g_random_double_range(0, 1),2));
-        p = g_random_double_range(0, M_PI*2);
-    }
-    if (choice == 1) // 0 : gaussian repartition
+    // angle is taken from an uniform distribution
+    angle = g_random_double_range(0, M_PI*2.0);
+
+    // radius is taken from a Normal Distribution
+    double radius_temp =-1;
+    while(!((radius_temp>=0)&&(radius_temp<=1)))
     {
-        double r_temp =-1;
-        while(!((r_temp>=0)&&(r_temp<=1)))
-        {
-            r_temp = NormalDistribution(a,s/4);
-        }
-        // generates a number following a normal distribution
-        p = g_random_double_range(0, M_PI*2);
-        r=r_temp;
+        radius_temp = NormalDistribution( a, s );
     }
+    // Because we are in polar coordinates, a special treatment has to be done to the radius.
+    // Otherwise, positions taken from an uniform repartition on radius and angle will not seam to 
+    // be uniformily distributed on the disk (more at the center and less at the boundary).
+    // We counter this effect with a 0.5 exponent. This is empiric.
+    radius = pow( radius_temp, 0.5);
+
 }
 
 
@@ -495,18 +477,18 @@ bool sp_spray_recursive(SPDesktop *desktop,
     double angle = g_random_double_range( - rotation_variation / 100.0 * M_PI , rotation_variation / 100.0 * M_PI );
     double _scale = g_random_double_range( 1.0 - scale_variation / 100.0, 1.0 + scale_variation / 100.0 );
     double dr; double dp;
-    random_position(dr,dp,mean,standard_deviation,_distrib);
+    random_position( dr, dp, mean, standard_deviation, _distrib );
     dr=dr*radius;
 
     if (mode == SPRAY_MODE_COPY) {
-        Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
+        Geom::OptRect a = item->getBounds(item->i2doc_affine());
         if (a) {
             SPItem *item_copied;
             if(_fid<=population)
             {
                 // duplicate
                 SPDocument *doc = SP_OBJECT_DOCUMENT(item);
-                Inkscape::XML::Document* xml_doc = sp_document_repr_doc(doc);
+                Inkscape::XML::Document* xml_doc = doc->getReprDoc();
                 Inkscape::XML::Node *old_repr = SP_OBJECT_REPR(item);
                 Inkscape::XML::Node *parent = old_repr->parent();
                 Inkscape::XML::Node *copy = old_repr->duplicate(xml_doc);
@@ -547,11 +529,11 @@ bool sp_spray_recursive(SPDesktop *desktop,
             i++;
         }
         SPDocument *doc = SP_OBJECT_DOCUMENT(father);
-        Inkscape::XML::Document* xml_doc = sp_document_repr_doc(doc);
+        Inkscape::XML::Document* xml_doc = doc->getReprDoc();
         Inkscape::XML::Node *old_repr = SP_OBJECT_REPR(father);
         Inkscape::XML::Node *parent = old_repr->parent();
 
-        Geom::OptRect a = father->getBounds(sp_item_i2doc_affine(father));
+        Geom::OptRect a = father->getBounds(father->i2doc_affine());
         if (a) {
             if (i==2) {
                 Inkscape::XML::Node *copy1 = old_repr->duplicate(xml_doc);
@@ -582,19 +564,19 @@ bool sp_spray_recursive(SPDesktop *desktop,
                 selection->clear();
                 selection->add(item_copied);
                 selection->add(unionResult);
-                sp_selected_path_union(selection->desktop());
+                sp_selected_path_union_skip_undo(selection->desktop());
                 selection->add(father);
                 Inkscape::GC::release(copy2);
                 did = true;
             }
         }
     } else if (mode == SPRAY_MODE_CLONE) {
-        Geom::OptRect a = item->getBounds(sp_item_i2doc_affine(item));
+        Geom::OptRect a = item->getBounds(item->i2doc_affine());
         if (a) {
             if(_fid<=population) {
                 SPItem *item_copied;
                 SPDocument *doc = SP_OBJECT_DOCUMENT(item);
-                Inkscape::XML::Document* xml_doc = sp_document_repr_doc(doc);
+                Inkscape::XML::Document* xml_doc = doc->getReprDoc();
                 Inkscape::XML::Node *old_repr = SP_OBJECT_REPR(item);
                 Inkscape::XML::Node *parent = old_repr->parent();
 
@@ -625,32 +607,6 @@ bool sp_spray_recursive(SPDesktop *desktop,
     return did;
 }
 
-
-bool sp_spray_color_recursive(guint /*mode*/,
-                              SPItem */*item*/,
-                              SPItem */*item_at_point*/,
-                              guint32 /*fill_goal*/,
-                              bool /*do_fill*/,
-                              guint32 /*stroke_goal*/,
-                              bool /*do_stroke*/,
-                              float /*opacity_goal*/,
-                              bool /*do_opacity*/,
-                              bool /*do_blur*/,
-                              bool /*reverse*/,
-                              Geom::Point /*p*/,
-                              double /*radius*/,
-                              double /*force*/,
-                              bool /*do_h*/,
-                              bool /*do_s*/,
-                              bool /*do_l*/,
-                              bool /*do_o*/)
-{
-    bool did = false;
-
-    return did;
-}
-
-
 bool sp_spray_dilate(SPSprayContext *tc, Geom::Point /*event_p*/, Geom::Point p, Geom::Point vector, bool reverse)
 {
     Inkscape::Selection *selection = sp_desktop_selection(SP_EVENT_CONTEXT(tc)->desktop);
@@ -730,7 +686,8 @@ void sp_spray_update_area(SPSprayContext *tc)
 
 void sp_spray_switch_mode(SPSprayContext *tc, gint mode, bool with_shift)
 {
-    SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("spray_tool_mode", mode); //sélectionne le bouton numéro "mode"
+    // select the button mode
+    SP_EVENT_CONTEXT(tc)->desktop->setToolboxSelectOneValue ("spray_tool_mode", mode); 
     // need to set explicitly, because the prefs may not have changed by the previous
     tc->mode = mode;
     sp_spray_update_cursor (tc, with_shift);
@@ -922,16 +879,16 @@ case GDK_SCROLL_LEFT:
             tc->has_dilated = false;
             switch (tc->mode) {
                 case SPRAY_MODE_COPY:
-                    sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
-                                     SP_VERB_CONTEXT_SPRAY, _("Spray with copies"));
+                    DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
+                                       SP_VERB_CONTEXT_SPRAY, _("Spray with copies"));
                     break;
                 case SPRAY_MODE_CLONE:
-                    sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
-                                     SP_VERB_CONTEXT_SPRAY, _("Spray with clones"));
+                    DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
+                                       SP_VERB_CONTEXT_SPRAY, _("Spray with clones"));
                     break;
                 case SPRAY_MODE_SINGLE_PATH:
-                    sp_document_done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
-                                     SP_VERB_CONTEXT_SPRAY, _("Spray in single path"));
+                    DocumentUndo::done(sp_desktop_document(SP_EVENT_CONTEXT(tc)->desktop),
+                                       SP_VERB_CONTEXT_SPRAY, _("Spray in single path"));
                     break;
             }
         }
@@ -1096,5 +1053,5 @@ break;
   fill-column:99
   End:
 */
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :