/usr/include/synfig-1.0/synfig/valueoperations.h is in libsynfig-dev 1.0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 | /* === S Y N F I G ========================================================= */
/*! \file valueoperations.h
** \brief Common operations with ValueBase
**
** $Id$
**
** \legal
** ......... ... 2013 Ivan Mahonin
**
** This package 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 package 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.
** \endlegal
*/
/* ========================================================================= */
/* === S T A R T =========================================================== */
#ifndef __SYNFIG_VALUEOPERATIONS_H
#define __SYNFIG_VALUEOPERATIONS_H
/* === H E A D E R S ======================================================= */
#include "value.h"
#include "transformation.h"
#include <vector>
/* === M A C R O S ========================================================= */
/* === T Y P E D E F S ===================================================== */
/* === C L A S S E S & S T R U C T S ======================================= */
namespace synfig {
namespace types_namespace { class TypeWeightedValueBase; }
/*! \class ValueVector
** \todo writeme
*/
class ValueVector
{
private:
//! it's static class
ValueVector() { }
public:
static bool check_type(Type &type) {
return type == type_bline_point
|| type == type_matrix
|| type == type_transformation
|| type == type_vector;
}
static bool check_type(const ValueBase &value)
{ return check_type(value.get_type()); }
static Vector get_vector(const ValueBase &value) {
Type &type(value.get_type());
if (type == type_bline_point)
return value.get(BLinePoint()).get_vertex();
else
if (type == type_matrix)
return value.get(Matrix()).get_transformed(Vector(0, 0));
else
if (type == type_transformation)
return value.get(Transformation()).transform(Vector(0,0));
else
if (type == type_vector)
return value.get(Vector());
return Vector(0, 0);
}
};
/*! \class ValueTransformation
** \todo writeme
*/
class ValueTransformation
{
private:
//! it's static class
ValueTransformation() { }
public:
static bool check_type(Type &type) {
return type == type_angle
|| type == type_bline_point
|| type == type_matrix
|| type == type_segment
|| type == type_transformation
|| type == type_vector
|| type == type_width_point;
}
static bool check_type(const ValueBase &value)
{ return check_type(value.get_type()); }
static ValueBase transform(const Transformation &transformation, const ValueBase &value) {
Type &type(value.get_type());
if (type == type_angle)
return value.get(Angle()) + transformation.angle;
else
if (type == type_bline_point)
{
BLinePoint bp(value.get(BLinePoint()));
bp.set_vertex( transformation.transform(bp.get_vertex()) );
bp.set_tangent1( transformation.transform(bp.get_tangent1(), false) );
bp.set_tangent2( transformation.transform(bp.get_tangent2(), false) );
return bp;
}
else
if (type == type_matrix)
return transformation.transform(value.get(Matrix()));
else
if (type == type_segment)
{
Segment s(value.get(Segment()));
s.p1 = transformation.transform(s.p1);
s.t1 = transformation.transform(s.t1, false);
s.p2 = transformation.transform(s.p2);
s.t2 = transformation.transform(s.t2, false);
return s;
}
else
if (type == type_transformation)
return transformation.transform(value.get(Transformation()));
else
if (type == type_vector)
return transformation.transform(value.get(Vector()));
else
if (type == type_width_point)
{
WidthPoint wp(value.get(WidthPoint()));
wp.set_width( wp.get_width()*transformation.scale[1] );
return wp;
}
return value;
}
static ValueBase back_transform(const Transformation &transformation, const ValueBase &value)
{ return transform(transformation.get_back_transformation(), value); }
};
/*! \class ValueAverage
** \todo writeme
*/
class ValueAverage
{
private:
//! it's static class
ValueAverage() { }
static types_namespace::TypeWeightedValueBase *allowed_types[];
public:
static types_namespace::TypeWeightedValueBase* get_weighted_type_for(Type &type);
static Type& convert_to_weighted_type(Type &type);
static Type& get_type_from_weighted(Type& type);
static bool check_weighted_type(Type& type);
static bool check_type(Type& type)
{ return get_weighted_type_for(type) != NULL; }
static bool check_type(const ValueBase &value)
{ return check_type(value.get_type()); }
static ValueBase add(const ValueBase &value_a, const ValueBase &value_b, const ValueBase &default_value)
{
if (value_a.get_type() != value_b.get_type()) return default_value;
Type &type(value_a.get_type());
if (type == type_real)
return value_a.get(Real()) + value_b.get(Real());
else
if (type == type_bline_point)
{
BLinePoint res(value_a.get(BLinePoint()));
const BLinePoint &b = value_b.get(BLinePoint());
res.set_vertex( res.get_vertex() + b.get_vertex() );
res.set_tangent1( res.get_tangent1() + b.get_tangent1() );
res.set_tangent2( res.get_tangent2() + b.get_tangent2() );
return res;
}
else
if (type == type_matrix)
return value_a.get(Matrix()) + value_b.get(Matrix());
else
if (type == type_segment)
{
Segment res(value_a.get(Segment()));
const Segment &b = value_b.get(Segment());
res.p1 += b.p1;
res.t1 += b.t1;
res.p2 += b.p2;
res.t2 += b.t2;
return res;
}
else
if (type == type_transformation)
return Transformation(
value_a.get(Transformation()).get_matrix()
+ value_b.get(Transformation()).get_matrix() );
else
if (type == type_vector)
return value_a.get(Vector()) + value_b.get(Vector());
else
if (type == type_width_point)
{
WidthPoint res(value_a.get(WidthPoint()));
const WidthPoint &b = value_b.get(WidthPoint());
res.set_width( res.get_width() + b.get_width() );
return res;
}
return default_value;
}
static ValueBase add(const ValueBase &value_a, const ValueBase &value_b)
{ return add(value_a, value_b, value_a); }
static ValueBase multiply(const ValueBase &value, Real amplifier)
{
Type &type(value.get_type());
if (type == type_real)
return value.get(Real()) * amplifier;
else
if (type == type_bline_point)
{
BLinePoint res(value.get(BLinePoint()));
res.set_vertex( res.get_vertex() * amplifier );
res.set_tangent1( res.get_tangent1() * amplifier );
res.set_tangent2( res.get_tangent2() * amplifier );
return res;
}
else
if (type == type_matrix)
return value.get(Matrix()) * amplifier;
else
if (type == type_segment)
{
Segment res(value.get(Segment()));
res.p1 *= amplifier;
res.t1 *= amplifier;
res.p2 *= amplifier;
res.t2 *= amplifier;
return res;
}
else
if (type == type_transformation)
return Transformation( value.get(Transformation()).get_matrix() * amplifier );
else
if (type == type_vector)
return value.get(Vector()) * amplifier;
else
if (type == type_width_point)
{
WidthPoint res(value.get(WidthPoint()));
res.set_width( res.get_width() * amplifier );
return res;
}
return value;
}
// iterators should provide following operations:
// comparison i == j, increment ++i, indirection *i, copy constructor i(j)
template<typename ConstIterator, typename ConstWeightIterator>
static ValueBase average_generic(
ConstIterator begin,
ConstIterator end,
ConstWeightIterator weight_begin,
ConstWeightIterator weight_end,
const ValueBase &default_value = ValueBase() )
{
if (begin == end) return ValueBase();
// check values
int count = 0;
Type &type = (*begin).get_type();
if (!check_type(type)) return ValueBase();
for(ConstIterator i(begin); !(i == end); ++i, ++count)
if ((*i).get_type() != type) return ValueBase();
// check weights
bool weights = !(weight_begin == weight_end);
Real summary_weight = 0.0;
int weights_count = 0;
if (weights)
for(ConstWeightIterator i(weight_begin); !(i == weight_end) && weights_count < count; ++i, ++weights_count)
summary_weight += *i;
if (weights_count < count || summary_weight == 0.0) weights = false;
if (!weights) summary_weight = (Real)count;
Real amplifier = 1.0/summary_weight;
// process
ValueBase summary;
if (weights)
{
// weighted
ConstWeightIterator j(weight_begin);
ConstIterator i(begin);
summary = multiply(*i, (*j) * amplifier);
for(++i, ++j; !(i == end); ++i, ++j)
summary = add(summary, multiply(*i, (*j) * amplifier), ValueBase());
}
else
{
// simple
ConstIterator i(begin);
summary = multiply(*i, amplifier);
for(++i; !(i == end); ++i)
summary = add(summary, multiply(*i, amplifier), ValueBase());
}
return summary.get_type() == type_nil ? default_value : summary;
}
template<typename ConstIterator>
static ValueBase average_generic(ConstIterator begin, ConstIterator end, const ValueBase &default_value = ValueBase())
{ return average_generic(begin, end, (Real*)NULL, (Real*)NULL, default_value); }
static ValueBase average(const ValueBase &list, const ValueBase &weights, const ValueBase &default_value)
{
if (list.get_type() != type_list) return default_value;
const std::vector<ValueBase> &list_vector = list.get_list();
if (weights.get_type() == type_list)
{
std::vector<Real> weights_vector_real;
weights_vector_real.reserve(weights.get_list().size());
const std::vector<ValueBase> &weights_vector = weights.get_list();
for(std::vector<ValueBase>::const_iterator i = weights_vector.begin(); i != weights_vector.end(); ++i)
if (i->get_type() == type_real)
weights_vector_real.push_back(i->get(Real())); else break;
if (weights_vector.size() >= list_vector.size())
return average_generic(
list_vector.begin(), list_vector.end(),
weights_vector_real.begin(), weights_vector_real.end(),
default_value );
}
return average_generic(list_vector.begin(), list_vector.end(), default_value);
}
static ValueBase average(const ValueBase &list, const ValueBase &weights)
{ return average(list, weights, ValueBase()); }
static ValueBase average(const ValueBase &list)
{ return average(list, ValueBase()); }
static ValueBase average_weighted(const ValueBase &weighted_list, const ValueBase &default_value);
static ValueBase average_weighted(const ValueBase &weighted_list)
{ return average_weighted(weighted_list, ValueBase()); }
// iterators should provide following operations:
// comparison i == j, increment ++i, indirection *i, copy constructor i(j)
template<typename Iterator, typename ConstWeightIterator>
static void set_average_value_generic(
Iterator begin,
Iterator end,
ConstWeightIterator weight_begin,
ConstWeightIterator weight_end,
const ValueBase &value )
{
if (begin == end) return;
// check values
int count = 0;
Type &type = (*begin).get_type();
if (!check_type(type)) return;
for(Iterator i(begin); !(i == end); ++i, ++count)
if ((*i).get_type() != type) return;
// find difference
ValueBase previous_value = average_generic(begin, end, weight_begin, weight_end);
ValueBase difference = add(value, multiply(previous_value, -1.0));
// simple
for(Iterator i(begin); !(i == end); ++i)
*i = add(*i, difference);
}
template<typename Iterator>
static void set_average_value_generic(Iterator begin, Iterator end, const ValueBase &value)
{ set_average_value_generic(begin, end, (Real*)NULL, (Real*)NULL, value); }
static void set_average_value(ValueBase &list, const ValueBase &weights, const ValueBase &value)
{
if (list.get_type() != type_list) return;
std::vector<ValueBase> list_vector = list.get_list();
if (weights.get_type() == type_list)
{
std::vector<Real> weights_vector_real;
weights_vector_real.reserve(weights.get_list().size());
const std::vector<ValueBase> &weights_vector = weights.get_list();
for(std::vector<ValueBase>::const_iterator i = weights_vector.begin(); i != weights_vector.end(); ++i)
if (i->get_type() == type_real)
weights_vector_real.push_back(i->get(Real())); else break;
if (weights_vector.size() >= list_vector.size())
{
set_average_value_generic(
list_vector.begin(), list_vector.end(),
weights_vector_real.begin(), weights_vector_real.end(),
value );
return;
}
}
set_average_value_generic(list_vector.begin(), list_vector.end(), value);
list = list_vector;
}
static void set_average_value(ValueBase &list, const ValueBase &value)
{ return set_average_value(list, ValueBase(), value); }
static void set_average_value_weighted(ValueBase &weighted_list, const ValueBase &value);
};
}; // END of namespace synfig
/* === E N D =============================================================== */
#endif
|