OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * ***** BEGIN GPL LICENSE BLOCK ***** |
| 3 * |
| 4 * This program is free software; you can redistribute it and/or |
| 5 * modify it under the terms of the GNU General Public License |
| 6 * as published by the Free Software Foundation; either version 2 |
| 7 * of the License, or (at your option) any later version. |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software Foundation, |
| 16 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 * |
| 18 * The Original Code is Copyright (C) 2010 Blender Foundation. |
| 19 * All rights reserved. |
| 20 * |
| 21 * The Original Code is: all of this file. |
| 22 * |
| 23 * Contributor(s): none yet. |
| 24 * |
| 25 * ***** END GPL LICENSE BLOCK ***** |
| 26 */ |
| 27 |
| 28 /** \file blender/freestyle/intern/stroke/AdvancedStrokeShaders.cpp |
| 29 * \ingroup freestyle |
| 30 * \brief Fredo's stroke shaders |
| 31 * \author Fredo Durand |
| 32 * \date 17/12/2002 |
| 33 */ |
| 34 |
| 35 #include "AdvancedStrokeShaders.h" |
| 36 #include "StrokeIterators.h" |
| 37 |
| 38 #include "../system/PseudoNoise.h" |
| 39 #include "../system/RandGen.h" |
| 40 |
| 41 ///////////////////////////////////////// |
| 42 // |
| 43 // CALLIGRAPHICS SHADER |
| 44 // |
| 45 ///////////////////////////////////////// |
| 46 |
| 47 CalligraphicShader::CalligraphicShader(real iMinThickness, real iMaxThickness, c
onst Vec2f &iOrientation, bool clamp) |
| 48 : StrokeShader() |
| 49 { |
| 50 _minThickness = iMinThickness; |
| 51 _maxThickness = iMaxThickness; |
| 52 _orientation = iOrientation; |
| 53 _orientation.normalize(); |
| 54 _clamp = clamp; |
| 55 } |
| 56 |
| 57 float ksinToto = 0.0f; |
| 58 |
| 59 int CalligraphicShader::shade(Stroke &ioStroke) const |
| 60 { |
| 61 Interface0DIterator v; |
| 62 Functions0D::VertexOrientation2DF0D fun; |
| 63 StrokeVertex *sv; |
| 64 for (v = ioStroke.verticesBegin(); !v.isEnd(); ++v) { |
| 65 real thickness; |
| 66 if (fun(v) < 0) |
| 67 return -1; |
| 68 |
| 69 Vec2f vertexOri(fun.result); |
| 70 Vec2r ori2d(-vertexOri[1], vertexOri[0]); |
| 71 ori2d.normalizeSafe(); |
| 72 real scal = ori2d * _orientation; |
| 73 sv = dynamic_cast<StrokeVertex*>(&(*v)); |
| 74 if (_clamp && (scal<0)) { |
| 75 scal = 0.0; |
| 76 sv->attribute().setColor(1, 1, 1); |
| 77 } |
| 78 else { |
| 79 scal = fabs(scal); |
| 80 sv->attribute().setColor(0, 0, 0); |
| 81 } |
| 82 thickness = _minThickness + scal * (_maxThickness - _minThicknes
s); |
| 83 if (thickness < 0.0) |
| 84 thickness = 0.0; |
| 85 sv->attribute().setThickness(thickness / 2.0, thickness / 2.0); |
| 86 } |
| 87 |
| 88 return 0; |
| 89 } |
| 90 |
| 91 #if 0 |
| 92 void TipRemoverShader::shade(Stroke &ioStroke) const |
| 93 { |
| 94 StrokeInternal::StrokeVertexIterator v, vend; |
| 95 for (v = ioStroke.strokeVerticesBegin(), vend = ioStroke.strokeVerticesE
nd(); v != vend; ++v) { |
| 96 if (((*v)->curvilinearAbscissa() < _tipLength) || |
| 97 (((*v)->strokeLength() - (*v)->curvilinearAbscissa()) < _tip
Length)) { |
| 98 (*v)->attribute().setThickness(0.0, 0.0); |
| 99 (*v)->attribute().setColor(1, 1, 1); |
| 100 } |
| 101 } |
| 102 } |
| 103 #endif |
| 104 |
| 105 ///////////////////////////////////////// |
| 106 // |
| 107 // SPATIAL NOISE SHADER |
| 108 // |
| 109 ///////////////////////////////////////// |
| 110 |
| 111 static const unsigned NB_VALUE_NOISE = 512; |
| 112 |
| 113 SpatialNoiseShader::SpatialNoiseShader(float ioamount, float ixScale, int nbOcta
ve, bool smooth, bool pureRandom) |
| 114 : StrokeShader() |
| 115 { |
| 116 _amount = ioamount; |
| 117 if (ixScale == 0) |
| 118 _xScale = 0; |
| 119 else |
| 120 _xScale = 1.0 / ixScale / real(NB_VALUE_NOISE); |
| 121 _nbOctave = nbOctave; |
| 122 _smooth = smooth; |
| 123 _pureRandom = pureRandom; |
| 124 } |
| 125 |
| 126 int SpatialNoiseShader::shade(Stroke &ioStroke) const |
| 127 { |
| 128 Interface0DIterator v, v2; |
| 129 v = ioStroke.verticesBegin(); |
| 130 Vec2r p(v->getProjectedX(), v->getProjectedY()); |
| 131 v2 = v; |
| 132 ++v2; |
| 133 Vec2r p0(v2->getProjectedX(), v2->getProjectedY()); |
| 134 p0 = p + 2 * (p - p0); |
| 135 StrokeVertex *sv; |
| 136 sv = dynamic_cast<StrokeVertex*>(&(*v)); |
| 137 real initU = sv->strokeLength() * real(NB_VALUE_NOISE); |
| 138 if (_pureRandom) |
| 139 initU += RandGen::drand48() * real(NB_VALUE_NOISE); |
| 140 |
| 141 Functions0D::VertexOrientation2DF0D fun; |
| 142 while (!v.isEnd()) { |
| 143 sv = dynamic_cast<StrokeVertex*>(&(*v)); |
| 144 Vec2r p(sv->getPoint()); |
| 145 if (fun(v) < 0) |
| 146 return -1; |
| 147 Vec2r vertexOri(fun.result); |
| 148 Vec2r ori2d(vertexOri[0], vertexOri[1]); |
| 149 ori2d = Vec2r(p - p0); |
| 150 ori2d.normalizeSafe(); |
| 151 |
| 152 PseudoNoise mynoise; |
| 153 real bruit; |
| 154 |
| 155 if (_smooth) |
| 156 bruit = mynoise.turbulenceSmooth(_xScale * sv->curviline
arAbscissa() + initU, _nbOctave); |
| 157 else |
| 158 bruit = mynoise.turbulenceLinear(_xScale * sv->curviline
arAbscissa() + initU, _nbOctave); |
| 159 |
| 160 Vec2r noise(-ori2d[1] * _amount * bruit, ori2d[0] * _amount * br
uit); |
| 161 |
| 162 sv->setPoint(p[0] + noise[0], p[1] + noise[1]); |
| 163 p0 = p; |
| 164 |
| 165 ++v; |
| 166 } |
| 167 |
| 168 return 0; |
| 169 } |
| 170 |
| 171 ///////////////////////////////////////// |
| 172 // |
| 173 // SMOOTHING SHADER |
| 174 // |
| 175 ///////////////////////////////////////// |
| 176 |
| 177 SmoothingShader::SmoothingShader(int ionbIteration, real iFactorPoint, real ifac
torCurvature, |
| 178 real iFactorCurvatureDifference, real iAnisoPoi
nt, real iAnisoNormal, |
| 179 real iAnisoCurvature, real iCarricatureFactor) |
| 180 : StrokeShader() |
| 181 { |
| 182 _nbIterations = ionbIteration; |
| 183 _factorCurvature = ifactorCurvature; |
| 184 _factorCurvatureDifference = iFactorCurvatureDifference; |
| 185 _anisoNormal = iAnisoNormal; |
| 186 _anisoCurvature = iAnisoCurvature; |
| 187 _carricatureFactor = iCarricatureFactor; |
| 188 _factorPoint = iFactorPoint; |
| 189 _anisoPoint = iAnisoPoint; |
| 190 } |
| 191 |
| 192 int SmoothingShader::shade(Stroke &ioStroke) const |
| 193 { |
| 194 // cerr << " Smoothing a stroke " << endl; |
| 195 |
| 196 Smoother smoother(ioStroke); |
| 197 smoother.smooth(_nbIterations, _factorPoint, _factorCurvature, _factorCu
rvatureDifference, _anisoPoint, |
| 198 _anisoNormal, _anisoCurvature, _carricatureFactor); |
| 199 return 0; |
| 200 } |
| 201 |
| 202 // SMOOTHER |
| 203 //////////////////////////// |
| 204 |
| 205 Smoother::Smoother(Stroke &ioStroke) |
| 206 { |
| 207 _stroke = &ioStroke; |
| 208 |
| 209 _nbVertices = ioStroke.vertices_size(); |
| 210 _vertex = new Vec2r[_nbVertices]; |
| 211 _curvature = new real[_nbVertices]; |
| 212 _normal = new Vec2r[_nbVertices]; |
| 213 StrokeInternal::StrokeVertexIterator v, vend; |
| 214 int i = 0; |
| 215 for (v = ioStroke.strokeVerticesBegin(), vend = ioStroke.strokeVerticesE
nd(); v != vend; ++v, ++i) { |
| 216 _vertex[i] = (v)->getPoint(); |
| 217 } |
| 218 Vec2r vec_tmp(_vertex[0] - _vertex[_nbVertices - 1]); |
| 219 _isClosedCurve = (vec_tmp.norm() < M_EPSILON); |
| 220 |
| 221 _safeTest = (_nbVertices > 4); |
| 222 } |
| 223 |
| 224 void Smoother::smooth(int nbIteration, real iFactorPoint, real ifactorCurvature,
real iFactorCurvatureDifference, |
| 225 real iAnisoPoint, real iAnisoNormal, real iAnisoCurvature,
real iCarricatureFactor) |
| 226 { |
| 227 _factorCurvature = ifactorCurvature; |
| 228 _factorCurvatureDifference = iFactorCurvatureDifference; |
| 229 _anisoNormal = iAnisoNormal; |
| 230 _anisoCurvature = iAnisoCurvature; |
| 231 _carricatureFactor = iCarricatureFactor; |
| 232 _factorPoint = iFactorPoint; |
| 233 _anisoPoint = iAnisoPoint; |
| 234 |
| 235 for (int i = 0; i < nbIteration; ++i) |
| 236 iteration (); |
| 237 copyVertices(); |
| 238 } |
| 239 |
| 240 static real edgeStopping(real x, real sigma) |
| 241 { |
| 242 if (sigma == 0.0) |
| 243 return 1.0; |
| 244 return exp(-x * x / (sigma * sigma)); |
| 245 } |
| 246 |
| 247 void Smoother::iteration() |
| 248 { |
| 249 computeCurvature(); |
| 250 for (int i = 1; i < (_nbVertices - 1); ++i) { |
| 251 real motionNormal = _factorCurvature * _curvature[i] * edgeStopp
ing(_curvature[i], _anisoNormal); |
| 252 |
| 253 real diffC1 = _curvature[i] - _curvature[i - 1]; |
| 254 real diffC2 = _curvature[i] - _curvature[i + 1]; |
| 255 real motionCurvature = edgeStopping(diffC1, _anisoCurvature) * d
iffC1 + |
| 256 edgeStopping(diffC2, _anisoCurvature) * d
iffC2; //_factorCurvatureDifference; |
| 257 motionCurvature *= _factorCurvatureDifference; |
| 258 //motionCurvature = _factorCurvatureDifference * (diffC1 + diffC
2); |
| 259 if (_safeTest) |
| 260 _vertex[i] = Vec2r(_vertex[i] + (motionNormal + motionCu
rvature) * _normal[i]); |
| 261 Vec2r v1(_vertex[i - 1] - _vertex[i]); |
| 262 Vec2r v2(_vertex[i + 1] - _vertex[i]); |
| 263 real d1 = v1.norm(); |
| 264 real d2 = v2.norm(); |
| 265 _vertex[i] = Vec2r(_vertex[i] + |
| 266 _factorPoint * edgeStopping(d2, _anisoPoint)
* (_vertex[i - 1] - _vertex[i]) + |
| 267 _factorPoint * edgeStopping(d1, _anisoPoint)
* (_vertex[i + 1] - _vertex[i])); |
| 268 } |
| 269 |
| 270 if (_isClosedCurve) { |
| 271 real motionNormal = _factorCurvature * _curvature[0] * edgeStopp
ing(_curvature[0], _anisoNormal); |
| 272 |
| 273 real diffC1 = _curvature[0] - _curvature[_nbVertices - 2]; |
| 274 real diffC2 = _curvature[0] - _curvature[1]; |
| 275 real motionCurvature = edgeStopping(diffC1, _anisoCurvature) * d
iffC1 + |
| 276 edgeStopping(diffC2, _anisoCurvature) * d
iffC2; //_factorCurvatureDifference; |
| 277 motionCurvature *= _factorCurvatureDifference; |
| 278 //motionCurvature = _factorCurvatureDifference * (diffC1 + diffC
2); |
| 279 _vertex[0] = Vec2r(_vertex[0] + (motionNormal + motionCurvature)
* _normal[0]); |
| 280 _vertex[_nbVertices - 1] = _vertex[0]; |
| 281 } |
| 282 } |
| 283 |
| 284 |
| 285 void Smoother::computeCurvature() |
| 286 { |
| 287 int i; |
| 288 Vec2r BA, BC, normalCurvature; |
| 289 for (i = 1; i < (_nbVertices - 1); ++i) { |
| 290 BA = _vertex[i - 1] - _vertex[i]; |
| 291 BC = _vertex[i + 1] - _vertex[i]; |
| 292 real lba = BA.norm(), lbc = BC.norm(); |
| 293 BA.normalizeSafe(); |
| 294 BC.normalizeSafe(); |
| 295 normalCurvature = BA + BC; |
| 296 |
| 297 _normal[i] = Vec2r(-(BC - BA)[1], (BC - BA)[0]); |
| 298 _normal[i].normalizeSafe(); |
| 299 |
| 300 _curvature[i] = normalCurvature * _normal[i]; |
| 301 if (lba + lbc > M_EPSILON) |
| 302 _curvature[i] /= (0.5 * lba + lbc); |
| 303 } |
| 304 _curvature[0] = _curvature[1]; |
| 305 _curvature[_nbVertices - 1] = _curvature[_nbVertices - 2]; |
| 306 Vec2r di(_vertex[1] - _vertex[0]); |
| 307 _normal[0] = Vec2r(-di[1], di[0]); |
| 308 _normal[0].normalizeSafe(); |
| 309 di = _vertex[_nbVertices - 1] - _vertex[_nbVertices - 2]; |
| 310 _normal[_nbVertices - 1] = Vec2r(-di[1], di[0]); |
| 311 _normal[_nbVertices - 1].normalizeSafe(); |
| 312 |
| 313 if (_isClosedCurve) { |
| 314 BA = _vertex[_nbVertices - 2] - _vertex[0]; |
| 315 BC = _vertex[1] - _vertex[0]; |
| 316 real lba = BA.norm(), lbc = BC.norm(); |
| 317 BA.normalizeSafe(); |
| 318 BC.normalizeSafe(); |
| 319 normalCurvature = BA + BC; |
| 320 |
| 321 _normal[i] = Vec2r(-(BC - BA)[1], (BC - BA)[0]); |
| 322 _normal[i].normalizeSafe(); |
| 323 |
| 324 _curvature[i] = normalCurvature * _normal[i]; |
| 325 if (lba + lbc > M_EPSILON) |
| 326 _curvature[i] /= (0.5 * lba + lbc); |
| 327 |
| 328 _normal[_nbVertices - 1] = _normal[0]; |
| 329 _curvature[_nbVertices - 1] = _curvature[0]; |
| 330 } |
| 331 } |
| 332 |
| 333 void Smoother::copyVertices() |
| 334 { |
| 335 int i = 0; |
| 336 StrokeInternal::StrokeVertexIterator v, vend; |
| 337 for (v = _stroke->strokeVerticesBegin(), vend = _stroke->strokeVerticesE
nd(); v != vend; ++v) { |
| 338 const Vec2r p0((v)->getPoint()); |
| 339 const Vec2r p1(_vertex[i]); |
| 340 Vec2r p(p0 + _carricatureFactor * (p1 - p0)); |
| 341 |
| 342 (v)->setPoint(p[0], p[1]); |
| 343 ++i; |
| 344 } |
| 345 } |
| 346 |
| 347 #if 0 // FIXME |
| 348 |
| 349 ///////////////////////////////////////// |
| 350 // |
| 351 // OMISSION SHADER |
| 352 // |
| 353 ///////////////////////////////////////// |
| 354 |
| 355 OmissionShader::OmissionShader(real sizeWindow, real thrVari, real thrFlat, real
lFlat) |
| 356 { |
| 357 _sizeWindow = sizeWindow; |
| 358 _thresholdVariation = thrVari; |
| 359 _thresholdFlat = thrFlat; |
| 360 _lengthFlat = lFlat; |
| 361 } |
| 362 |
| 363 int OmissionShader::shade(Stroke &ioStroke) const |
| 364 { |
| 365 Omitter omi(ioStroke); |
| 366 omi.omit(_sizeWindow, _thresholdVariation, _thresholdFlat, _lengthFlat); |
| 367 |
| 368 return 0; |
| 369 } |
| 370 |
| 371 |
| 372 // OMITTER |
| 373 /////////////////////////// |
| 374 |
| 375 Omitter::Omitter(Stroke &ioStroke) : Smoother(ioStroke) |
| 376 { |
| 377 StrokeInternal::StrokeVertexIterator v, vend; |
| 378 int i = 0; |
| 379 for (v = ioStroke.strokeVerticesBegin(), vend = ioStroke.strokeVerticesE
nd(); v != vend; ++v, ++i) { |
| 380 _u[i] = (v)->curvilinearAbscissa(); |
| 381 } |
| 382 } |
| 383 |
| 384 void Omitter::omit(real sizeWindow, real thrVari, real thrFlat, real lFlat) |
| 385 { |
| 386 _sizeWindow=sizeWindow; |
| 387 _thresholdVariation=thrVari; |
| 388 _thresholdFlat=thrFlat; |
| 389 _lengthFlat=lFlat; |
| 390 |
| 391 for (int i = 1; i < _nbVertices-1; ++i) { |
| 392 if (_u[i] < _lengthFlat) |
| 393 continue; |
| 394 // is the previous segment flat? |
| 395 int j = i - 1; |
| 396 while ((j >= 0) && (_u[i] - _u[j] < _lengthFlat)) { |
| 397 if ((_normal[j] * _normal[i]) < _thresholdFlat) |
| 398 ; // FIXME |
| 399 --j; |
| 400 } |
| 401 } |
| 402 } |
| 403 |
| 404 #endif |
OLD | NEW |