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
|
// cfdgimpl.cpp
// this file is part of Context Free
// ---------------------
// Copyright (C) 2006-2008 Mark Lentczner - markl@glyphic.com
// Copyright (C) 2006-2008 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
//
// Mark Lentczner can be contacted at markl@glyphic.com or at
// Mark Lentczner, 1209 Villa St., Mountain View, CA 94041-1123, USA
//
//
#include "cfdgimpl.h"
#include "renderimpl.h"
#include "primShape.h"
#include <assert.h>
#include <algorithm>
#ifdef _WIN32
#pragma warning( disable : 4800 4189 )
#endif
using namespace std;
CFDGImpl::CFDGImpl(AbstractSystem* m)
: mBackgroundSet(false), m_system(m), m_Parameters(0), mTiled(false),
mSized(false), mTileOffset(0, 0)
{
// These have to be encoded first so that their type number will fit
// within an unsigned char
string circle_name = "CIRCLE";
string square_name = "SQUARE";
string triangle_name = "TRIANGLE";
string loopstart_name = "~~LOOPSTART~~";
string loopend_name = "~~LOOPEND~~";
// ensure these four get the first four shape numbers
int circle_num = encodeShapeName(circle_name);
int square_num = encodeShapeName(square_name);
int triangle_num = encodeShapeName(triangle_name);
int loopstart_num = encodeShapeName(loopstart_name);
int loopend_num = encodeShapeName(loopend_name);
// Make sure that the shape numbers correspond to the order of primShape::shapeMap[]
assert(circle_num == primShape::circleType);
assert(square_num == primShape::squareType);
assert(triangle_num == primShape::triangleType);
assert(loopstart_num == primShape::loopStartType);
assert(loopend_num == primShape::loopEndType);
}
CFDGImpl::~CFDGImpl() { }
void
CFDGImpl::setInitialShape(const Shape& s)
{
// set the startshape if it hasn't already been set
if (m_initialShape.mShapeType == -1)
m_initialShape = s;
}
const Shape&
CFDGImpl::getInitialShape()
{
m_initialShape.mWorldState.m_transform.tx = mTileOffset.x;
m_initialShape.mWorldState.m_transform.ty = mTileOffset.y;
return m_initialShape;
}
namespace {
// We need a light-weight version of the Rule class to give to the lower_bound
// template function. Using a Rule object incurs the cost of allocating a
// Replacements vector that is never used.
struct RuleSpecifier
{
int mInitialShapeType;
double mWeight;
RuleSpecifier(int type, double weight) : mInitialShapeType(type), mWeight(weight) {};
};
inline bool operator<(const Rule& l, const RuleSpecifier& r) {
return (l.mInitialShapeType == r.mInitialShapeType) ?
(l.mWeight < r.mWeight) :
(l.mInitialShapeType < r.mInitialShapeType);
}
// This is a pointless comparator that is needed to be specified for the debug
// version of STL under VC2005.
inline bool operator<(const RuleSpecifier& l, const Rule& r) {
return (l.mInitialShapeType == r.mInitialShapeType) ?
(l.mWeight < r.mWeight) :
(l.mInitialShapeType < r.mInitialShapeType);
}
}
Rule *
CFDGImpl::findRule(int shapetype, double r)
{
RuleSpecifier temp(shapetype, r);
// Technically it is illegal for temp to not be a Rule, but we seem to be able
// to get away with it.
vector<Rule>::iterator first = lower_bound(mRules.begin(), mRules.end(), temp);
return &*first;
}
void
CFDGImpl::addRule(const Rule& r, bool isShape)
{
mRules.push_back(r);
if (r.mInitialShapeType < int(m_shapeTypes.size()))
m_shapeTypes[r.mInitialShapeType].hasRules = true;
m_shapeTypes[r.mInitialShapeType].shapeType = isShape ? pathType : ruleType;
}
void
CFDGImpl::addParameter(Parameter p)
{
m_Parameters |= p;
usesColor = m_Parameters & Color;
usesAlpha = m_Parameters & Alpha;
}
void
CFDGImpl::setBackgroundColor(const agg::rgba& bk)
{
if (!mBackgroundSet) {
backgroundColor = bk;
mBackgroundSet = true;
}
}
bool
CFDGImpl::isTiled(agg::trans_affine* tr, double* x, double* y) const
{
if (!mTiled) return false;
if (tr) *tr = mTileTransform;
if (x) *x = mTileX;
if (y) *y = mTileY;
return true;
}
bool
CFDGImpl::isSized(double* x, double* y) const
{
if (!mSized) return false;
if (x) *x = mTileX;
if (y) *y = mTileY;
return true;
}
void
CFDGImpl::setTiled(const agg::trans_affine& tr, const double& x, const double& y)
{
if (mTiled) return; // only accept the first tile directive
mTileTransform = tr;
mTileOffset.x = tr.tx; mTileTransform.tx = 0;
mTileOffset.y = tr.ty; mTileTransform.ty = 0;
mTileX = x;
mTileY = y;
mTiled = true;
}
void
CFDGImpl::setSized(const agg::trans_affine& tr)
{
mTileTransform = tr;
mTileOffset.x = tr.tx; mTileTransform.tx = 0;
mTileOffset.y = tr.ty; mTileTransform.ty = 0;
mTileX = tr.sx;
mTileY = tr.sy;
mSized = true;
}
void
CFDGImpl::rulesLoaded()
{
// thanks to by Brent Yorgey
vector<double> weightsums( m_shapeTypes.size(), 0.0 );
vector<double> unitweightsums( m_shapeTypes.size(), 0.0 );
vector<int> rulecounts( m_shapeTypes.size(), 0 );
unsigned int i;
// first pass: sum all the weights for each shape type
for ( i = 0; i < mRules.size(); i++ ) {
Rule& r = mRules[i];
weightsums[ r.mInitialShapeType ] += r.mWeight;
rulecounts[ r.mInitialShapeType ]++;
}
// second pass: normalize each weight by dividing by the
// total weight for that shape type
for ( i = 0; i < mRules.size(); i++ ) {
Rule& r = mRules[i];
double weight = r.mWeight / weightsums[ r.mInitialShapeType ];
unitweightsums[ r.mInitialShapeType ] += weight;
if (--rulecounts[ r.mInitialShapeType ]) {
r.mWeight = unitweightsums[ r.mInitialShapeType ];
} else {
// make sure that last rule of a type has a weightsum > 1.0
r.mWeight = 1.1;
}
}
// third pass: sort the rules by shape type, preserving the rule order
// with respect to rules of the same shape type
sort(mRules.begin(), mRules.end());
}
int
CFDGImpl::numRules()
{
return mRules.size();
}
string
CFDGImpl::decodeShapeName(int shapetype)
{
if (shapetype < int(m_shapeTypes.size()))
return m_shapeTypes[shapetype].name;
else
return ("**unnamed shape**");
}
int
CFDGImpl::encodeShapeName(const string& s)
{
for (unsigned int i = 0; i < m_shapeTypes.size(); i++) {
if (s == m_shapeTypes[i].name) {
return i;
}
}
m_shapeTypes.push_back(ShapeType(s));
return m_shapeTypes.size() - 1;
}
int
CFDGImpl::getShapeType(const string& s)
{
for (unsigned int i = 0; i < m_shapeTypes.size(); i++) {
if (s == m_shapeTypes[i].name) {
return m_shapeTypes[i].shapeType;
}
}
return newShape;
}
int
CFDGImpl::getShapeType(int shapetype)
{
return m_shapeTypes[shapetype].shapeType;
}
bool
CFDGImpl::shapeHasRules(int shapetype)
{
if (shapetype < int(m_shapeTypes.size()))
return m_shapeTypes[shapetype].hasRules;
else
return false;
}
Renderer*
CFDGImpl::renderer(int width, int height, float minSize,
int variation, double border)
{
return new RendererImpl(this, width, height, minSize, variation, border);
}
|