Make bezier control point coordinates relative
authorSam Moore <[email protected]>
Thu, 14 Aug 2014 16:48:03 +0000 (00:48 +0800)
committerSam Moore <[email protected]>
Thu, 14 Aug 2014 16:48:03 +0000 (00:48 +0800)
So the quad tree should just have to care about bounding rectangles.
Also so the GPU renderer now doesn't need transforming but CPU renderer does now.

AddBezier assumes absolute coordinates and transforms to relative coordinates
AddBezierData is assuming relative coordinates.

Totally clear.

PS: Basically whatever is convenient/efficient for the GPU is inconvenient/inefficient for the CPU
and vice versa. Fun!

src/bezier.h
src/document.cpp
src/main.cpp
src/objectrenderer.cpp

index 449d7bd..722ed57 100644 (file)
@@ -64,11 +64,16 @@ namespace IPDF
 
                Rect SolveBounds() const;
                
+               Bezier ToAbsolute(const Rect & bounds) const
+               {
+                       return Bezier(*this, bounds);
+               }
+               
                /** Convert absolute control points to control points relative to bounds
                 * (This basically does the opposite of the Copy constructor)
                 * ie: If this is absolute, the returned Bezier will be relative to the bounds rectangle
                 */
-               Bezier CopyInverse(const Rect & bounds) const
+               Bezier ToRelative(const Rect & bounds) const
                {
                        // x' <- (x - x0)/w etc
                        // special cases when w or h = 0
index 6a86291..4cf6c40 100644 (file)
@@ -297,10 +297,15 @@ unsigned Document::AddGroup(unsigned start_index, unsigned end_index)
        return result;
 }
 
+/**
+ * Add a Bezier using Absolute coords
+ */
 unsigned Document::AddBezier(const Bezier & bezier)
 {
-       unsigned index = AddBezierData(bezier);
-       return Add(BEZIER, bezier.SolveBounds(), index);
+       Rect bounds = bezier.SolveBounds();
+       Bezier data = bezier.ToRelative(bounds); // Relative
+       unsigned index = AddBezierData(data);
+       return Add(BEZIER, bounds, index);
 }
 
 unsigned Document::Add(ObjectType type, const Rect & bounds, unsigned data_index)
@@ -826,7 +831,7 @@ void Document::AddFontGlyphAtPoint(stbtt_fontinfo *font, int character, Real sca
                Real old_x(current_x), old_y(current_y);
                current_x = inst_x;
                current_y = inst_y;
-               unsigned bezier_index;
+               //unsigned bezier_index;
                switch(instructions[i].type)
                {
                // Move To
@@ -834,8 +839,8 @@ void Document::AddFontGlyphAtPoint(stbtt_fontinfo *font, int character, Real sca
                        break;
                // Line To
                case STBTT_vline:
-                       bezier_index = AddBezierData(Bezier(old_x + x, old_y + y, old_x + x, old_y + y, current_x + x, current_y + y, current_x + x, current_y + y));
-                       Add(BEZIER,Rect(0,0,1,1),bezier_index);
+                       AddBezier(Bezier(old_x + x, old_y + y, old_x + x, old_y + y, current_x + x, current_y + y, current_x + x, current_y + y));
+                       //Add(BEZIER,Rect(0,0,1,1),bezier_index);
                        break;
                // Quadratic Bezier To:
                case STBTT_vcurve:
@@ -843,7 +848,7 @@ void Document::AddFontGlyphAtPoint(stbtt_fontinfo *font, int character, Real sca
                        // - Endpoints are the same.
                        // - cubic1 = quad0+(2/3)*(quad1-quad0)
                        // - cubic2 = quad2+(2/3)*(quad1-quad2)
-                       bezier_index = AddBezier(Bezier(old_x + x, old_y + y, old_x + Real(2)*(inst_cx-old_x)/Real(3) + x, old_y + Real(2)*(inst_cy-old_y)/Real(3) + y,
+                       AddBezier(Bezier(old_x + x, old_y + y, old_x + Real(2)*(inst_cx-old_x)/Real(3) + x, old_y + Real(2)*(inst_cy-old_y)/Real(3) + y,
                                                current_x + Real(2)*(inst_cx-current_x)/Real(3) + x, current_y + Real(2)*(inst_cy-current_y)/Real(3) + y, current_x + x, current_y + y));
                        break;
                }
index 718361f..46946f9 100644 (file)
@@ -83,7 +83,8 @@ int main(int argc, char ** argv)
        else 
        {
                //doc.AddBezier(Bezier(0,0, 1,0.5, 0.5,1, 1,1));
-               doc.AddText("c",1,0,0);
+               doc.AddText("The quick brown\nfox jumps over\nthe lazy dog",0.1,0,0.5);
+               //doc.AddBezier(Bezier(0,0,0,0.1,0,0.1,0,0.1));
        }
        Debug("Start!");
        Rect bounds(b[0],b[1],b[2],b[3]);
index 23d09c3..7a0f8cd 100644 (file)
@@ -221,10 +221,10 @@ void BezierRenderer::RenderUsingCPU(const Objects & objects, const View & view,
        {
                if (m_indexes[i] < first_obj_id) continue;
                if (m_indexes[i] >= last_obj_id) continue;
-               Rect bounds(CPURenderBounds(objects.bounds[m_indexes[i]], view, target));
-               PixelBounds pix_bounds(bounds);
+               const Rect & bounds = objects.bounds[m_indexes[i]];
+               PixelBounds pix_bounds(CPURenderBounds(bounds,view,target));
 
-               Bezier control(objects.beziers[objects.data_indices[m_indexes[i]]],CPURenderBounds(Rect(0,0,1,1), view, target));
+               Bezier control(objects.beziers[objects.data_indices[m_indexes[i]]].ToAbsolute(bounds),CPURenderBounds(Rect(0,0,1,1), view, target));
                //Debug("%s -> %s via %s", objects.beziers[objects.data_indices[m_indexes[i]]].Str().c_str(), control.Str().c_str(), bounds.Str().c_str());
                // Draw a rectangle around the bezier for debugging the bounds rectangle calculations
                ObjectRenderer::RenderLineOnCPU(pix_bounds.x, pix_bounds.y, pix_bounds.x+pix_bounds.w, pix_bounds.y, target, Colour(1,0,0,1));
@@ -283,7 +283,7 @@ void BezierRenderer::PrepareBezierGPUBuffer(const Objects& objects)
        for (unsigned i = 0; i < objects.types.size(); ++i)
        {
                if (objects.types[i] != BEZIER) continue;
-               Bezier bez = objects.beziers[objects.data_indices[i]].CopyInverse(objects.bounds[i]);
+               const Bezier & bez = objects.beziers[objects.data_indices[i]];//objects.beziers[objects.data_indices[i]].CopyInverse(objects.bounds[i]);
                
                GPUBezierCoeffs coeffs = {
                        Float(bez.x0), Float(bez.y0),

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