Merge branch 'master' of git.ucc.asn.au:ipdf/code
authorDavid Gow <[email protected]>
Mon, 29 Sep 2014 04:28:22 +0000 (12:28 +0800)
committerDavid Gow <[email protected]>
Mon, 29 Sep 2014 04:28:22 +0000 (12:28 +0800)
Conflicts:
src/bezier.cpp
src/view.h

1  2 
src/bezier.h
src/document.cpp
src/view.cpp

diff --combined src/bezier.h
@@@ -6,6 -6,9 +6,9 @@@
  
  #include "real.h"
  #include "rect.h"
  namespace IPDF
  {
        extern int Factorial(int n);
  
                Bezier ReParametrise(const Real& t0, const Real& t1)
                {
 -                      Debug("Reparametrise: %f -> %f",Double(t0),Double(t1));
 +                      //Debug("Reparametrise: %f -> %f",Double(t0),Double(t1));
                        Bezier new_bezier;
                        // Subdivide to get from [0,t1]
                        new_bezier = DeCasteljauSubdivideLeft(t1);
                        // Convert t0 from [0,1] range to [0, t1]
                        Real new_t0 = t0 / t1;
 -                      Debug("New t0 = %f", Double(new_t0));
 +                      //Debug("New t0 = %f", Double(new_t0));
                        new_bezier = new_bezier.DeCasteljauSubdivideRight(new_t0);
  
 -                      Debug("%s becomes %s", this->Str().c_str(), new_bezier.Str().c_str());
 +                      //Debug("%s becomes %s", this->Str().c_str(), new_bezier.Str().c_str());
                        return new_bezier;
                }
                
  
                        // Find its roots.
                        std::vector<Real> x_intersection = SolveXParam(r.x);
 -                      Debug("Found %d intersections on left edge", x_intersection.size());
 +                      //Debug("Found %d intersections on left edge", x_intersection.size());
  
                        // And for the other side.
  
                        std::vector<Real> x_intersection_pt2 = SolveXParam(r.x + r.w);
                        x_intersection.insert(x_intersection.end(), x_intersection_pt2.begin(), x_intersection_pt2.end());
 -                      Debug("Found %d intersections on right edge (total x: %d)", x_intersection_pt2.size(), x_intersection.size());
 +                      //Debug("Found %d intersections on right edge (total x: %d)", x_intersection_pt2.size(), x_intersection.size());
  
                        // Find its roots.
                        std::vector<Real> y_intersection = SolveYParam(r.y);
 -                      Debug("Found %d intersections on top edge", y_intersection.size());
 +                      //Debug("Found %d intersections on top edge", y_intersection.size());
  
                        std::vector<Real> y_intersection_pt2 = SolveYParam(r.y+r.h);
                        y_intersection.insert(y_intersection.end(), y_intersection_pt2.begin(), y_intersection_pt2.end());
 -                      Debug("Found %d intersections on bottom edge (total y: %d)", y_intersection_pt2.size(), y_intersection.size());
 +                      //Debug("Found %d intersections on bottom edge (total y: %d)", y_intersection_pt2.size(), y_intersection.size());
  
                        // Merge and sort.
                        x_intersection.insert(x_intersection.end(), y_intersection.begin(), y_intersection.end());
                        x_intersection.push_back(Real(1));
                        std::sort(x_intersection.begin(), x_intersection.end());
  
 -                      Debug("Found %d intersections.\n", x_intersection.size());
 -                      for(auto t : x_intersection)
 +                      //Debug("Found %d intersections.\n", x_intersection.size());
 +                      /*for(auto t : x_intersection)
                        {
                                Real ptx, pty;
                                Evaluate(ptx, pty, t);
                                Debug("Root: t = %f, (%f,%f)", Double(t), Double(ptx), Double(pty));
 -                      }
 +                      }*/
                        
                        std::vector<Bezier> all_beziers;
                        if (x_intersection.size() <= 2)
                        {
                                Real t1 = *it;
                                if (t1 == t0) continue;
 -                              Debug(" -- t0: %f to t1: %f: %f", Double(t0), Double(t1), Double((t1 + t0)/Real(2)));
 +                              //Debug(" -- t0: %f to t1: %f: %f", Double(t0), Double(t1), Double((t1 + t0)/Real(2)));
                                Real ptx, pty;
                                Evaluate(ptx, pty, ((t1 + t0) / Real(2)));
                                if (r.PointIn(ptx, pty))
                                {
 -                                      Debug("Adding segment: (point at %f, %f)", Double(ptx), Double(pty));
 +                                      //Debug("Adding segment: (point at %f, %f)", Double(ptx), Double(pty));
                                        all_beziers.push_back(this->ReParametrise(t0, t1));
                                }
                                else
                                {
 -                                      Debug("Segment removed (point at %f, %f)", Double(ptx), Double(pty));
 +                                      //Debug("Segment removed (point at %f, %f)", Double(ptx), Double(pty));
                                }
                                t0 = t1;
                        }
  
  }
  
+ #undef Real
  #endif //_BEZIER_H
diff --combined src/document.cpp
@@@ -168,7 -168,7 +168,7 @@@ int Document::ClipObjectToQuadChild(in
  QuadTreeIndex Document::GenQuadChild(QuadTreeIndex parent, QuadTreeNodeChildren type)
  {
        QuadTreeIndex new_index = m_quadtree.nodes.size();
 -      Debug("-------------- Generating Quadtree Node %d (parent %d) ----------------------", new_index, parent);
 +      Debug("-------------- Generating Quadtree Node %d (parent %d, type %d) ----------------------", new_index, parent, type);
        m_quadtree.nodes.push_back(QuadTreeNode{QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, QUADTREE_EMPTY, parent, type, 0, 0, -1});
  
        m_quadtree.nodes[new_index].object_begin = m_objects.bounds.size();
@@@ -303,6 -303,7 +303,7 @@@ unsigned Document::AddPath(unsigned sta
        unsigned data_index = AddPathData(path);
        Rect bounds = path.SolveBounds(m_objects);
        unsigned result = Add(PATH, bounds,data_index);
+       m_objects.paths[data_index].m_index = result;
        //Debug("Added path %u -> %u (%u objects) colour {%u,%u,%u,%u}, stroke {%u,%u,%u,%u}", start_index, end_index, (end_index - start_index), fill.r, fill.g, fill.b, fill.a, stroke.r, stroke.g, stroke.b, stroke.a);
        return result;
  }
@@@ -731,7 -732,7 +732,7 @@@ void Document::LoadSVG(const string & f
        if (!result)
                Error("Couldn't load \"%s\" - %s", filename.c_str(), result.description());
                
-       Debug("Loaded XML - %s", result.description());
+       Debug("Loaded XML from \"%s\" - %s", filename.c_str(), result.description());
        
        input.close();
                                                // a c e, b d f
@@@ -1052,19 -1053,51 +1053,51 @@@ void Document::TransformObjectBounds(co
        }
  }
  
- void Document::TranslateObjects(const Real & dx, const Real & dy)
+ void Document::TranslateObjects(const Real & dx, const Real & dy, ObjectType type)
  {
+       #ifdef TRANSFORM_BEZIERS_TO_PATH
+               for (unsigned i = 0; i < m_objects.paths.size(); ++i)
+               {
+                       Path & p = m_objects.paths[i];
+                       p.x += dx;
+                       p.y += dy;
+               }
+               return;
+       #endif  
+       
        for (unsigned i = 0; i < m_count; ++i)
        {
-               m_objects.bounds[i].x += dx;
-               m_objects.bounds[i].y += dy;
+               if (type == NUMBER_OF_OBJECT_TYPES || m_objects.types[i] == type)
+               {
+                       m_objects.bounds[i].x += dx;
+                       m_objects.bounds[i].y += dy;
+               }
        }
  }
  
- void Document::ScaleObjectsAboutPoint(const Real & x, const Real & y, const Real & scale_amount)
+ void Document::ScaleObjectsAboutPoint(const Real & x, const Real & y, const Real & scale_amount, ObjectType type)
  {
+       #ifdef TRANSFORM_BEZIERS_TO_PATH
+               for (unsigned i = 0; i < m_objects.paths.size(); ++i)
+               {
+                       Path & p = m_objects.paths[i];
+                       p.w /= scale_amount;
+                       p.h /= scale_amount;
+                       p.x -= x;
+                       p.x /= scale_amount;
+                       p.x += x;
+                       p.y -= y;
+                       p.y /= scale_amount;
+                       p.y += y;
+               }
+               return;
+       #endif
+       
        for (unsigned i = 0; i < m_count; ++i)
        {
+               if (type != NUMBER_OF_OBJECT_TYPES && m_objects.types[i] != type)
+                       continue;
+               
                m_objects.bounds[i].w /= scale_amount;
                m_objects.bounds[i].h /= scale_amount;
                //m_objects.bounds[i].x = x + (m_objects.bounds[i].x-x)/scale_amount;
                m_objects.bounds[i].y -= y;
                m_objects.bounds[i].y /= scale_amount;
                m_objects.bounds[i].y += y;
+       }
  
-       }       
-       
  }
diff --combined src/view.cpp
@@@ -7,6 -7,13 +7,13 @@@
        #include "controlpanel.h"
  #endif //CONTROLPANEL_DISABLED
  
+ #ifdef TRANSFORM_BEZIERS_TO_PATH 
+       #ifndef TRANSFORM_OBJECTS_NOT_VIEW
+       //#error Cannot TRANSFORM_BEZIERS_TO_PATH _without_ TRANSFORM_OBJECTS_NOT_VIEW
+       #endif
+ #endif
  using namespace IPDF;
  using namespace std;
  
@@@ -83,7 -90,11 +90,11 @@@ void View::Translate(Real x, Real y
                m_buffer_dirty = true;
        m_bounds_dirty = true;
        #ifdef TRANSFORM_OBJECTS_NOT_VIEW
-       m_document.TranslateObjects(-x, -y);
+       ObjectType type = NUMBER_OF_OBJECT_TYPES;
+               #ifdef TRANSFORM_BEZIERS_TO_PATH
+                       type = PATH;
+               #endif
+       m_document.TranslateObjects(-x, -y, type);
        #endif
        x *= m_bounds.w;
        y *= m_bounds.h;
@@@ -126,7 -137,11 +137,11 @@@ void View::ScaleAroundPoint(Real x, Rea
        
        
        #ifdef TRANSFORM_OBJECTS_NOT_VIEW
-       m_document.ScaleObjectsAboutPoint(x, y, scale_amount);
+       ObjectType type = NUMBER_OF_OBJECT_TYPES;
+       #ifdef TRANSFORM_BEZIERS_TO_PATH
+               type = PATH;
+       #endif
+       m_document.ScaleObjectsAboutPoint(x, y, scale_amount, type);
        #endif
        x *= m_bounds.w;
        y *= m_bounds.h;
@@@ -159,12 -174,7 +174,7 @@@ Rect View::TransformToViewCoords(const 
        #ifdef TRANSFORM_OBJECTS_NOT_VIEW
                return inp;
        #endif
-       Rect out;
-       out.x = (inp.x - m_bounds.x) / m_bounds.w;
-       out.y = (inp.y - m_bounds.y) / m_bounds.h;
-       out.w = inp.w / m_bounds.w;
-       out.h = inp.h / m_bounds.h;
-       return out;
+       return TransformRectCoordinates(m_bounds, inp);
  }
  
  /**
@@@ -201,7 -211,7 +211,7 @@@ void View::Render(int width, int height
  #ifndef QUADTREE_DISABLED
        if (m_bounds_dirty || !m_lazy_rendering)
        {
 -              if ( (m_bounds.x > 1.0 || m_bounds.x < 0.0 || m_bounds.y > 1.0 || m_bounds.y < 0.0 || m_bounds.w > 1.0 || m_bounds.h > 1.0))
 +              if ( m_bounds.w > 1.0 || m_bounds.h > 1.0)
                {
                        //TODO: Generate a new parent node.
                        if (m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
                                m_current_quadtree_node = m_document.GetQuadTree().nodes[m_current_quadtree_node].parent;
                        }
                }
 +
 +              // TODO: Support generating new parent nodes.
 +              if (false && m_document.GetQuadTree().nodes[m_current_quadtree_node].parent != QUADTREE_EMPTY)
 +              {
 +                      if (m_bounds.x < -0.5)
 +                      {
 +                              m_bounds = Rect(m_bounds.x + 1, m_bounds.y, m_bounds.w, m_bounds.h);
 +                              m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, -1, 0, &m_document);
 +                      }
 +                      if (m_bounds.y < -0.5)
 +                      {
 +                              m_bounds = Rect(m_bounds.x, m_bounds.y + 1, m_bounds.w, m_bounds.h);
 +                              m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, -1, &m_document);
 +                      }
 +                      if (m_bounds.w + m_bounds.x > 0.5)
 +                      {
 +                              m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
 +                              m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 1, 0, &m_document);
 +                      }
 +                      if (m_bounds.h + m_bounds.y > 0.5)
 +                      {
 +                              m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
 +                              m_current_quadtree_node = m_document.GetQuadTree().GetNeighbour(m_current_quadtree_node, 0, 1, &m_document);
 +                      }
 +              }
 +
                if (ContainedInQuadChild(m_bounds, QTC_TOP_LEFT))
                {
                        if (m_document.GetQuadTree().nodes[m_current_quadtree_node].top_left == QUADTREE_EMPTY)
@@@ -358,56 -342,49 +368,56 @@@ void View::RenderQuadtreeNode(int width
        {
                m_bounds = Rect(m_bounds.x - 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
                m_bounds_dirty = true;
 -              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, -1), remaining_depth - 1);
 +              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, -1, &m_document), remaining_depth - 1);
        }
 +      m_bounds = old_bounds;
        if (m_bounds.Intersects(Rect(-1,0,1,1)))
        {
                m_bounds = Rect(m_bounds.x - 1, m_bounds.y, m_bounds.w, m_bounds.h);
                m_bounds_dirty = true;
 -              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 0), remaining_depth - 1);
 +              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 0, &m_document), remaining_depth - 1);
        }
 +      m_bounds = old_bounds;
        if (m_bounds.Intersects(Rect(-1,1,1,1)))
        {
                m_bounds = Rect(m_bounds.x - 1, m_bounds.y + 1, m_bounds.w, m_bounds.h);
                m_bounds_dirty = true;
 -              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 1), remaining_depth - 1);
 +              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, -1, 1, &m_document), remaining_depth - 1);
        }
 +      m_bounds = old_bounds;
        if (m_bounds.Intersects(Rect(0,-1,1,1)))
        {
                m_bounds = Rect(m_bounds.x, m_bounds.y - 1, m_bounds.w, m_bounds.h);
                m_bounds_dirty = true;
 -              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, -1), remaining_depth - 1);
 +              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, -1, &m_document), remaining_depth - 1);
        }
 +      m_bounds = old_bounds;
        if (m_bounds.Intersects(Rect(0,1,1,1)))
        {
                m_bounds = Rect(m_bounds.x, m_bounds.y + 1, m_bounds.w, m_bounds.h);
                m_bounds_dirty = true;
 -              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, 1), remaining_depth - 1);
 +              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 0, 1, &m_document), remaining_depth - 1);
        }
 +      m_bounds = old_bounds;
        if (m_bounds.Intersects(Rect(1,-1,1,1)))
        {
                m_bounds = Rect(m_bounds.x + 1, m_bounds.y - 1, m_bounds.w, m_bounds.h);
                m_bounds_dirty = true;
 -              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, -1), remaining_depth - 1);
 +              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, -1, &m_document), remaining_depth - 1);
        }
 +      m_bounds = old_bounds;
        if (m_bounds.Intersects(Rect(1,0,1,1)))
        {
                m_bounds = Rect(m_bounds.x + 1, m_bounds.y, m_bounds.w, m_bounds.h);
                m_bounds_dirty = true;
 -              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 0), remaining_depth - 1);
 +              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 0, &m_document), remaining_depth - 1);
        }
 +      m_bounds = old_bounds;
        if (m_bounds.Intersects(Rect(1,1,1,1)))
        {
                m_bounds = Rect(m_bounds.x + 1, m_bounds.y + 1, m_bounds.w, m_bounds.h);
                m_bounds_dirty = true;
 -              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 1), remaining_depth - 1);
 +              RenderQuadtreeNode(width, height, m_document.GetQuadTree().GetNeighbour(node, 1, 1, &m_document), remaining_depth - 1);
        }
        m_bounds = old_bounds;
        m_bounds_dirty = true;
@@@ -446,6 -423,7 +456,7 @@@ void View::RenderRange(int width, int h
        if (m_use_gpu_transform)
        {
                #ifdef TRANSFORM_OBJECTS_NOT_VIEW
+                       //Debug("Transform objects, not view");
                                GLfloat glbounds[] = {0.0f, 0.0f, 1.0f, 1.0f,
                                        0.0f, 0.0f, float(width), float(height)};
                #else
@@@ -503,11 -481,14 +514,14 @@@ void View::UpdateObjBoundsVBO(unsigned 
        //m_objbounds_vbo.Invalidate();
        m_objbounds_vbo.SetType(GraphicsBuffer::BufferTypeVertex);
        m_objbounds_vbo.SetName("Object Bounds VBO");
+       
+       #ifndef TRANSFORM_OBJECTS_NOT_VIEW
        if (m_use_gpu_transform)
        {
                m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageStaticDraw);
        }
        else
+       #endif //TRANSFORM_OBJECTS_NOT_VIEW
        {
                m_objbounds_vbo.SetUsage(GraphicsBuffer::BufferUsageDynamicCopy);
        }
  
        BufferBuilder<GPUObjBounds> obj_bounds_builder(m_objbounds_vbo.MapRange(first_obj*sizeof(GPUObjBounds), (last_obj-first_obj)*sizeof(GPUObjBounds), false, true, true), m_objbounds_vbo.GetSize());
  
+       #ifndef TRANSFORM_BEZIERS_TO_PATH
        for (unsigned id = first_obj; id < last_obj; ++id)
        {
                Rect obj_bounds;
                        (float)Float(obj_bounds.x + obj_bounds.w),
                        (float)Float(obj_bounds.y + obj_bounds.h)
                };
-               obj_bounds_builder.Add(gpu_bounds);
  
+               obj_bounds_builder.Add(gpu_bounds);
+       }
+       #else
+       for (unsigned i = 0; i < m_document.m_objects.paths.size(); ++i)
+       {
+               Path & path = m_document.m_objects.paths[i];
+               Rect & pbounds = path.GetBounds(m_document.m_objects); // Not very efficient...
+               for (unsigned id = path.m_start; id <= path.m_end; ++id)
+               {
+                       if (id < first_obj || id >= last_obj)
+                               continue;
+                               
+                       Rect obj_bounds = m_document.m_objects.bounds[id];
+                       obj_bounds.x *= pbounds.w;
+                       obj_bounds.x += pbounds.x;
+                       obj_bounds.y *= pbounds.h;
+                       obj_bounds.y += pbounds.y;
+                       obj_bounds.w *= pbounds.w;
+                       obj_bounds.h *= pbounds.h;
+                       
+                       if (!m_use_gpu_transform)
+                               obj_bounds = TransformToViewCoords(obj_bounds);
+                       GPUObjBounds gpu_bounds = {
+                               Float(obj_bounds.x),
+                               Float(obj_bounds.y),
+                               Float(obj_bounds.x + obj_bounds.w),
+                               Float(obj_bounds.y + obj_bounds.h)
+                       };
+                       obj_bounds_builder.Add(gpu_bounds);
+                       //Debug("Path %d %s -> %s via %s", id, m_document.m_objects.bounds[id].Str().c_str(), obj_bounds.Str().c_str(), pbounds.Str().c_str()); 
+               }
+               GPUObjBounds p_gpu_bounds = {
+                               Float(pbounds.x),
+                               Float(pbounds.y),
+                               Float(pbounds.x + pbounds.w),
+                               Float(pbounds.y + pbounds.h)
+               };              
+               obj_bounds_builder.Add(p_gpu_bounds);
        }
+       #endif
        m_objbounds_vbo.UnMap();
  }
  /**
@@@ -582,7 -603,9 +636,9 @@@ void View::PrepareRender(
                m_object_renderers[i]->FinaliseBuffers();
        }
        if (UsingGPURendering())
+       {
                dynamic_cast<BezierRenderer*>(m_object_renderers[BEZIER])->PrepareBezierGPUBuffer(m_document.m_objects);
+       }
        m_render_dirty = false;
  }
  

UCC git Repository :: git.ucc.asn.au