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
|
// tiledCanvas.cpp
// this file is part of Context Free
// ---------------------
// Copyright (C) 2006-2016 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 <cmath>
#include "primShape.h"
#include "bounds.h"
#include <cstdlib>
void tiledCanvas::start(bool clear, const agg::rgba& bk, int w, int h)
{
mWidth = w;
mHeight = h;
mTile->start(clear, bk, w, h);
}
void tiledCanvas::end()
{
mTile->end();
}
void tiledCanvas::primitive(int shape, RGBA8 c, agg::trans_affine tr)
{
if (shape == primShape::fillType) {
mTile->primitive(shape, c, tr);
return;
}
for (auto&& tile: mTileList) {
agg::trans_affine t(tr);
t.tx += tile.x;
t.ty += tile.y;
mTile->primitive(shape, c, t);
}
}
void tiledCanvas::path(RGBA8 c, agg::trans_affine tr, const AST::CommandInfo& attr)
{
for (auto&& tile: mTileList) {
agg::trans_affine t(tr);
t.tx += tile.x;
t.ty += tile.y;
mTile->path(c, t, attr);
}
}
inline bool
tiledCanvas::checkTile(const Bounds& b, const agg::rect_d& canvas, double dx, double dy)
{
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);
bool hit = shape.overlaps(canvas);
if (hit)
mTileList.emplace_back(dx, dy);
return hit;
}
void
tiledCanvas::tileTransform(const Bounds& b)
// Compute a list of tiling offsets for all tiled copies of the shape that overlap
// the canvas. Used for subsequent drawing.
{
double centx = (b.mMin_X + b.mMax_X) * 0.5;
double centy = (b.mMin_Y + b.mMax_Y) * 0.5;
mInvert.transform(¢x, ¢y); // transform to unit square tessellation
centx = std::floor(centx + 0.5); // round to nearest integer
centy = std::floor(centy + 0.5); // round to nearest integer
mTileList.clear();
double dx = -centx, dy = -centy;
mOffset.transform(&dx, &dy);
mTileList.emplace_back(dx, dy);
agg::rect_d canvas(-5, -5, static_cast<double>(mWidth + 9), static_cast<double>(mHeight + 9));
if (mFrieze)
centx = centy = centx + centy; // one will be zero, set them both to the other one
for (int ring = 1; ; ring++) {
bool hit = false;
if (mFrieze) {
// Works for x frieze and y frieze, the other dimension gets zeroed
hit = checkTile(b, canvas, ring - centx, ring - centy);
hit = checkTile(b, canvas, -ring - centx, -ring - centy) || hit;
} else {
for (int pos = -ring; pos < ring; pos++) {
hit = checkTile(b, canvas, pos - centx, -ring - centy) || hit;
hit = checkTile(b, canvas, ring - centx, pos - centy) || hit;
hit = checkTile(b, canvas, -pos - centx, ring - centy) || hit;
hit = checkTile(b, canvas, -ring - centx, -pos - centy) || hit;
}
}
if (!hit) return;
}
}
tiledCanvas::tiledCanvas(Canvas* tile, const agg::trans_affine& tr, CFDG::frieze_t f)
: Canvas(tile->mWidth, tile->mHeight),
mTile(tile),
mTileTransform(tr),
mFrieze(f)
{
}
void tiledCanvas::scale(double scaleFactor)
{
agg::trans_affine_scaling scale(scaleFactor);
// Generate the tiling transform in pixel units
mOffset = mTileTransform * scale;
// The mInvert transform can transform coordinates from the pixel unit tiling
// to the unit square tiling.
if (mFrieze) {
mInvert.reset();
mInvert.sx = mOffset.sx == 0.0 ? 0.0 : 1/mOffset.sx;
mInvert.sy = mOffset.sy == 0.0 ? 0.0 : 1/mOffset.sy;
} else {
mInvert = ~mOffset;
}
}
inline bool
tiledCanvas::checkTileInt(const agg::rect_i& screen,
const agg::trans_affine& screenTessellation,
int x, int y, tileList& points)
{
double dx = x;
double dy = y;
screenTessellation.transform(&dx, &dy);
int px = static_cast<int>(std::floor(dx + 0.5));
int py = static_cast<int>(std::floor(dy + 0.5));
// If the tile is visible then record it
agg::rect_i tile(px, py, px + mWidth - 1, py + mHeight - 1);
bool hit = tile.overlaps(screen);
if (hit)
points.emplace_back(px, py);
return hit;
}
tileList tiledCanvas::getTessellation(int w, int h, int x1, int y1, bool flipY)
{
tileList tessPoints;
// Produce an integer version of mOffset that is centered in the w x h screen
agg::trans_affine tess(mWidth, std::floor(mOffset.shy + 0.5), std::floor(mOffset.shx + 0.5),
flipY ? -mHeight : mHeight, x1, y1);
agg::rect_i screen(0, 0, w - 1, h - 1);
if (mFrieze == CFDG::frieze_x)
tess.sy = 0.0;
if (mFrieze == CFDG::frieze_y)
tess.sx = 0.0;
tessPoints.push_back(agg::point_i(x1, y1)); // 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;
if (mFrieze) {
hit = checkTileInt(screen, tess, ring, ring, tessPoints);
hit = checkTileInt(screen, tess, -ring, -ring, tessPoints) || hit;
} else {
for (int pos = -ring; pos < ring; pos++) {
hit = checkTileInt(screen, tess, pos, -ring, tessPoints) || hit;
hit = checkTileInt(screen, tess, ring, pos, tessPoints) || hit;
hit = checkTileInt(screen, tess, -pos, ring, tessPoints) || hit;
hit = checkTileInt(screen, tess, -ring, -pos, tessPoints) || hit;
}
}
if (!hit) break;
}
return tessPoints;
}
|