File: tiledCanvas.cpp

package info (click to toggle)
contextfree 2.2%2Bdfsg1-2.1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 4,336 kB
  • sloc: cpp: 20,388; yacc: 507; objc: 494; ansic: 270; makefile: 113; lex: 92; xml: 24
file content (289 lines) | stat: -rw-r--r-- 9,280 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// tiledCanvas.cpp
// this file is part of Context Free
// ---------------------
// Copyright (C) 2006 John Horigan - john@glyphic.com
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
// 
// John Horigan can be contacted at john@glyphic.com or at
// John Horigan, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
//

#include "tiledCanvas.h"
#include <math.h>
#include "primShape.h"
#include "bounds.h"
#include <cstdlib>
#include <stdlib.h>

void tiledCanvas::start(bool clear, const agg::rgba& bk, int , int )
{
	mTile->start(clear, bk, mTile->mWidth, mTile->mHeight);
}

void tiledCanvas::end() 
{
	mTile->end();
}

void tiledCanvas::circle(RGBA8 c, agg::trans_affine tr)
{
    switch (tileTransform(tr, primShape::shapeMap[primShape::circleType])) {
        case inside:
            mTile->circle(c, tr);
            return;
        case simple:
        case complex:
            for (unsigned int i = 0; i < mTileList.size(); ++i) {
                agg::trans_affine t(tr);
                t.tx += mTileList[i].x;
                t.ty += mTileList[i].y;
                mTile->circle(c, t);
            }
            return;
    }
    return;
}

void tiledCanvas::square(RGBA8 c, agg::trans_affine tr)
{
    switch (tileTransform(tr, primShape::shapeMap[primShape::squareType])) {
        case inside:
            mTile->square(c, tr);
            return;
        case simple:
        case complex:
            for (unsigned int i = 0; i < mTileList.size(); ++i) {
                agg::trans_affine t(tr);
                t.tx += mTileList[i].x;
                t.ty += mTileList[i].y;
                mTile->square(c, t);
            }
            return;
    }
    return;
}

void tiledCanvas::triangle(RGBA8 c, agg::trans_affine tr)
{
    switch (tileTransform(tr, primShape::shapeMap[primShape::triangleType])) {
        case inside:
            mTile->triangle(c, tr);
            return;
        case simple:
        case complex:
            for (unsigned int i = 0; i < mTileList.size(); ++i) {
                agg::trans_affine t(tr);
                t.tx += mTileList[i].x;
                t.ty += mTileList[i].y;
                mTile->triangle(c, t);
            }
            return;
    }
    return;
}

void tiledCanvas::path(RGBA8 c, agg::trans_affine tr, agg::path_storage* path, 
                       pathAttr* attr)
{
    switch (tileTransform(tr, path, attr)) {
        case inside:
            mTile->path(c, tr, path, attr);
            return;
        case simple:
        case complex:
            for (unsigned int i = 0; i < mTileList.size(); ++i) {
                agg::trans_affine t(tr);
                t.tx += mTileList[i].x;
                t.ty += mTileList[i].y;
                mTile->path(c, t, path, attr);
            }
            return;
    }
    return;
}

static const double tileBuffer = 1.05;

tiledCanvas::tileType tiledCanvas::tileTransform(agg::trans_affine& tr, 
                                                 agg::path_storage* path,
                                                 pathAttr* attr)
// Adjust the translation part of the transform so that it falls within the 
// tile parallelogram at the origin. 
//
// Returns whether the shape is close to the edge of the canvas 
// (true=not close, false=close/overlapping).
{
	double dummy;
    mInvert.transform(&(tr.tx), &(tr.ty));  // transform to unit square tesselation
	tr.tx = modf(tr.tx, &dummy);            // translate to unit square at the origin
	tr.ty = modf(tr.ty, &dummy);
    if (tr.tx < 0.0) tr.tx += 1.0;
    if (tr.ty < 0.0) tr.ty += 1.0;
    mOffset.transform(&(tr.tx), &(tr.ty));  // transform back to specified tesselation
	
    Bounds b(tr, path, attr, tileBuffer);
    if (b.mMin_X > 0 && b.mMax_X < mWidth && b.mMin_Y > 0 && b.mMax_Y < mHeight)
        return inside;
    //if ((b.mMax_X - b.mMin_X) < mWidth && (b.mMax_Y - b.mMin_Y) < mHeight)
    //    return simple;
    
    getTesselation(b);
    return complex;
}

tiledCanvas::tiledCanvas(Canvas* tile, const agg::trans_affine& tr) 
:   Canvas(tile->mWidth, tile->mHeight), 
    mTile(tile), 
    mOffset(tr)
{
}

void tiledCanvas::scale(double scaleFactor)
{
    agg::trans_affine_scaling scale(scaleFactor);
    
    // Generate the tiling transform in pixel units
    mOffset *= scale;
    
    // The mInvert transform can transform coordinates from the pixel unit tiling
    // to the unit square tiling.
    mInvert = mOffset;
    mInvert.invert();
}

tileList tiledCanvas::getTesselation(int w, int h, int x, int y, bool flipY)
{
    // Produce an integer version of mOffset that is centered in the w x h screen
    agg::trans_affine tess(mWidth, floor(mOffset.shy + 0.5), floor(mOffset.shx + 0.5),
        flipY ? -mHeight : mHeight, x, y);
    agg::rect_i screen(0, 0, w - 1, h - 1);
    
    tileList tessPoints;
    tessPoints.push_back(agg::point_i(x, y));   // always include the center tile
    
    // examine rings of tile units around the center unit until you encounter a
    // ring that doesn't have any tile units that intersect the screen. Then stop.
    for (int ring = 1; ; ring++) {
        bool hit = false;
        for (int y = -ring; y <= ring; y++) {
            for (int x = -ring; x <= ring; x++) {
                // These loops enumerate all tile units on and within the ring.
                // Skip tile units that are within (not on) the ring.
                if (abs(x) < ring && abs(y) < ring) continue;
                
                // Find where this tile is on the screen
                double dx = x;
                double dy = y;
                tess.transform(&dx, &dy);
                int px = (int)floor(dx + 0.5);
                int py = (int)floor(dy + 0.5);
                
                // If the tile is visible then record it
                agg::rect_i tile(px, py, px + mWidth - 1, py + mHeight - 1);
                if (tile.overlaps(screen)) {
                    hit = true;
                    tessPoints.push_back(agg::point_i(px, py));
                }
            }
        }
        
        if (!hit) break;
    }
    
    return tessPoints;
}

void tiledCanvas::getTesselation(Bounds b) 
// use the same algorithm as getTesselation(int ...) , but purely in the 
// floating point domain, to see what tesselation points the large shape
// needs to be drawn at such that all parts of it that overlap the canvas are 
// drawn. 
{
    mTileList.clear();
    mTileList.push_back(agg::point_d(0, 0));
    agg::rect_d canvas(0, 0, (double)(mWidth - 1), (double)(mHeight - 1));
    
    for (int ring = 1; ; ring++) {
        bool hit = false;
        for (int y = -ring; y <= ring; y++) {
            for (int x = -ring; x <= ring; x++) {
                // These loops enumerate all tile units on and within the ring.
                // Skip tile units that are within (not on) the ring.
                if (abs(x) < ring && abs(y) < ring) continue;
                
                // Find where this tile is on the canvas
                double dx = x;
                double dy = y;
                mOffset.transform(&dx, &dy);
                
                // If the tile might touch the canvas then record it
                agg::rect_d shape(b.mMin_X + dx, b.mMin_Y + dy, b.mMax_X + dx, b.mMax_Y + dy);
                if (shape.overlaps(canvas)) {
                    hit = true;
                    mTileList.push_back(agg::point_d(dx, dy));
                }
            }
        }
        
        if (!hit) break;
    }
}

bool tiledCanvas::isRectangular(int* x_factor, int* y_factor)
{
    int shx = (int)floor(mOffset.shx + 0.5);
    int shy = (int)floor(mOffset.shy + 0.5);
    
    if (shx == 0 && shy == 0) {
        if (x_factor && y_factor) *x_factor = *y_factor = 1;
        return true;
    }
    
    if (shx && mWidth % abs(shx) == 0) {
        if (x_factor && y_factor) {
            *x_factor = 1;
            *y_factor = mWidth / abs(shx);
        }
        return true;
    }
    
    if (shx && mWidth % (mWidth - abs(shx)) == 0) {
        if (x_factor && y_factor) {
            *x_factor = 1;
            *y_factor = mWidth / (mWidth - abs(shx));
        }
        return true;
    }
    
    if (shy && mHeight % shy == 0) {
        if (x_factor && y_factor) {
            *x_factor = mHeight / abs(shy);
            *y_factor = 1;
        }
        return true;
    }
    
    if (shy && mHeight % (mHeight - abs(shy)) == 0) {
        if (x_factor && y_factor) {
            *x_factor = mHeight / (mHeight - abs(shy));
            *y_factor = 1;
        }
        return true;
    }
    
    return false;
}