| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | #![allow(non_snake_case)] |
| 5 | |
| 6 | use std::ops::{Sub, Mul, Add, AddAssign, SubAssign, MulAssign, Div}; |
| 7 | |
| 8 | macro_rules! IFC { |
| 9 | ($e: expr) => { |
| 10 | assert_eq!($e, S_OK); |
| 11 | } |
| 12 | } |
| 13 | |
| 14 | pub type HRESULT = i32; |
| 15 | |
| 16 | pub const S_OK: i32 = 0; |
| 17 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 18 | pub struct GpPointR { |
| 19 | pub x: f32, |
| 20 | pub y: f32 |
| 21 | } |
| 22 | |
| 23 | impl Sub for GpPointR { |
| 24 | type Output = Self; |
| 25 | |
| 26 | fn sub(self, rhs: Self) -> Self::Output { |
| 27 | GpPointR { x: self.x - rhs.x, y: self.y - rhs.y } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | impl Add for GpPointR { |
| 32 | type Output = Self; |
| 33 | |
| 34 | fn add(self, rhs: Self) -> Self::Output { |
| 35 | GpPointR { x: self.x + rhs.x, y: self.y + rhs.y } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl AddAssign for GpPointR { |
| 40 | fn add_assign(&mut self, rhs: Self) { |
| 41 | *self = *self + rhs; |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | impl SubAssign for GpPointR { |
| 46 | fn sub_assign(&mut self, rhs: Self) { |
| 47 | *self = *self - rhs; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | impl MulAssign<f32> for GpPointR { |
| 52 | fn mul_assign(&mut self, rhs: f32) { |
| 53 | *self = *self * rhs; |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | |
| 58 | impl Mul<f32> for GpPointR { |
| 59 | type Output = Self; |
| 60 | |
| 61 | fn mul(self, rhs: f32) -> Self::Output { |
| 62 | GpPointR { x: self.x * rhs, y: self.y * rhs } |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | impl Div<f32> for GpPointR { |
| 67 | type Output = Self; |
| 68 | |
| 69 | fn div(self, rhs: f32) -> Self::Output { |
| 70 | GpPointR { x: self.x / rhs, y: self.y / rhs } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | |
| 75 | impl Mul for GpPointR { |
| 76 | type Output = f32; |
| 77 | |
| 78 | fn mul(self, rhs: Self) -> Self::Output { |
| 79 | self.x * rhs.x + self.y * rhs.y |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl GpPointR { |
| 84 | pub fn ApproxNorm(&self) -> f32 { |
| 85 | self.x.abs().max(self.y.abs()) |
| 86 | } |
| 87 | pub fn Norm(&self) -> f32 { |
| 88 | self.x.hypot(self.y) |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Relative to this is relative to the tolerance squared. In other words, a vector |
| 93 | // whose length is less than .01*tolerance will be considered 0 |
| 94 | const SQ_LENGTH_FUZZ: f32 = 1.0e-4; |
| 95 | |
| 96 | // Some of these constants need further thinking |
| 97 | |
| 98 | //const FUZZ: f64 = 1.0e-6; // Relative 0 |
| 99 | // Minimum allowed tolerance - should probably be adjusted to the size of the |
| 100 | // geometry we are rendering, but for now --- |
| 101 | |
| 102 | /* |
| 103 | const FUZZ_DOUBLE: f64 = 1.0e-12; // Double-precision relative 0 |
| 104 | const MIN_TOLERANCE: f64 = 1.0e-6; |
| 105 | const DEFAULT_FLATTENING_TOLERANCE: f64 = 0.25;*/ |
| 106 | const TWICE_MIN_BEZIER_STEP_SIZE: f32 = 1.0e-3; // The step size in the Bezier flattener should |
| 107 | // never go below half this amount. |
| 108 | //+----------------------------------------------------------------------------- |
| 109 | // |
| 110 | |
| 111 | // |
| 112 | // $TAG ENGR |
| 113 | |
| 114 | // $Module: win_mil_graphics_geometry |
| 115 | // $Keywords: |
| 116 | // |
| 117 | // $Description: |
| 118 | // Definition of CBezierFlattener. |
| 119 | // |
| 120 | // $ENDTAG |
| 121 | // |
| 122 | //------------------------------------------------------------------------------ |
| 123 | |
| 124 | //+----------------------------------------------------------------------------- |
| 125 | // |
| 126 | // Class: |
| 127 | // CFlatteningSink |
| 128 | // |
| 129 | // Synopsis: |
| 130 | // Callback interface for the results of curve flattening |
| 131 | // |
| 132 | // Notes: |
| 133 | // Methods are implemented rather than pure, for callers who do not use all |
| 134 | // of them. |
| 135 | // |
| 136 | //------------------------------------------------------------------------------ |
| 137 | // |
| 138 | // Definition of CFlatteningSink |
| 139 | // |
| 140 | //------------------------------------------------------------------------------ |
| 141 | /* |
| 142 | struct CFlatteningSink |
| 143 | { |
| 144 | public: |
| 145 | CFlatteningSink() {} |
| 146 | |
| 147 | virtual ~CFlatteningSink() {} |
| 148 | |
| 149 | virtual HRESULT Begin( |
| 150 | __in_ecount(1) const GpPointR &) |
| 151 | // First point (transformed) |
| 152 | { |
| 153 | // Do nothing stub, should not be called |
| 154 | RIP("Base class Begin called"); |
| 155 | return E_NOTIMPL; |
| 156 | } |
| 157 | |
| 158 | virtual HRESULT AcceptPoint( |
| 159 | __in_ecount(1) const GpPointR &pt, |
| 160 | // The point |
| 161 | IN GpReal t, |
| 162 | // Parameter we're at |
| 163 | __out_ecount(1) bool &fAborted) |
| 164 | // Set to true to signal aborting |
| 165 | { |
| 166 | UNREFERENCED_PARAMETER(pt); |
| 167 | UNREFERENCED_PARAMETER(t); |
| 168 | UNREFERENCED_PARAMETER(fAborted); |
| 169 | |
| 170 | // Do nothing stub, should not be called |
| 171 | RIP("Base class AcceptPoint called"); |
| 172 | return E_NOTIMPL; |
| 173 | } |
| 174 | |
| 175 | virtual HRESULT AcceptPointAndTangent( |
| 176 | __in_ecount(1) const GpPointR &, |
| 177 | //The point |
| 178 | __in_ecount(1) const GpPointR &, |
| 179 | //The tangent there |
| 180 | IN bool fLast) // Is this the last point on the curve? |
| 181 | { |
| 182 | // Do nothing stub, should not be called |
| 183 | RIP("Base class AcceptPointAndTangent called"); |
| 184 | return E_NOTIMPL; |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | |
| 189 | |
| 190 | */ |
| 191 | #[derive(Clone, Debug)] |
| 192 | |
| 193 | pub struct CBezier |
| 194 | { |
| 195 | /* |
| 196 | public: |
| 197 | CBezier() |
| 198 | { |
| 199 | } |
| 200 | |
| 201 | CBezier( |
| 202 | __in_ecount(4) const GpPointR *pPt) |
| 203 | // The defining Bezier points |
| 204 | { |
| 205 | Assert(pPt); |
| 206 | memcpy(&m_ptB, pPt, 4 * sizeof(GpPointR)); |
| 207 | } |
| 208 | |
| 209 | CBezier( |
| 210 | __in_ecount(1) const CBezier &other) |
| 211 | // Another Bezier to copy |
| 212 | { |
| 213 | Copy(other); |
| 214 | } |
| 215 | |
| 216 | void Copy( |
| 217 | __in_ecount(1) const CBezier &other) |
| 218 | // Another Bezier to copy |
| 219 | { |
| 220 | memcpy(&m_ptB, other.m_ptB, 4 * sizeof(GpPointR)); |
| 221 | } |
| 222 | |
| 223 | void Initialize( |
| 224 | __in_ecount(1) const GpPointR &ptFirst, |
| 225 | // The first Bezier point |
| 226 | __in_ecount(3) const GpPointR *pPt) |
| 227 | // The remaining 3 Bezier points |
| 228 | { |
| 229 | m_ptB[0] = ptFirst; |
| 230 | memcpy(m_ptB + 1, pPt, 3 * sizeof(GpPointR)); |
| 231 | } |
| 232 | |
| 233 | __outro_ecount(1) const GpPointR &GetControlPoint(__range(0, 3) UINT i) const |
| 234 | { |
| 235 | Assert(i < 4); |
| 236 | return m_ptB[i]; |
| 237 | } |
| 238 | |
| 239 | __outro_ecount(1) const GpPointR &GetFirstPoint() const |
| 240 | { |
| 241 | return m_ptB[0]; |
| 242 | } |
| 243 | |
| 244 | __outro_ecount(1) const GpPointR &GetLastPoint() const |
| 245 | { |
| 246 | return m_ptB[3]; |
| 247 | } |
| 248 | |
| 249 | void GetPoint( |
| 250 | _In_ double t, |
| 251 | // Parameter value |
| 252 | __out_ecount(1) GpPointR &pt) const; |
| 253 | // Point there |
| 254 | |
| 255 | void GetPointAndDerivatives( |
| 256 | __in double t, |
| 257 | // Parameter value |
| 258 | __out_ecount(3) GpPointR *pValues) const; |
| 259 | // Point, first derivative and second derivative there |
| 260 | |
| 261 | void TrimToStartAt( |
| 262 | IN double t); // Parameter value |
| 263 | |
| 264 | void TrimToEndAt( |
| 265 | IN double t); // Parameter value |
| 266 | |
| 267 | bool TrimBetween( |
| 268 | __in double rStart, |
| 269 | // Parameter value for the new start, must be between 0 and 1 |
| 270 | __in double rEnd); |
| 271 | // Parameter value for the new end, must be between 0 and 1 |
| 272 | |
| 273 | bool operator ==(__in_ecount(1) const CBezier &other) const |
| 274 | { |
| 275 | return (m_ptB[0] == other.m_ptB[0]) && |
| 276 | (m_ptB[1] == other.m_ptB[1]) && |
| 277 | (m_ptB[2] == other.m_ptB[2]) && |
| 278 | (m_ptB[3] == other.m_ptB[3]); |
| 279 | } |
| 280 | |
| 281 | void AssertEqualOrNaN(__in_ecount(1) const CBezier &other) const |
| 282 | { |
| 283 | m_ptB[0].AssertEqualOrNaN(other.m_ptB[0]); |
| 284 | m_ptB[1].AssertEqualOrNaN(other.m_ptB[1]); |
| 285 | m_ptB[2].AssertEqualOrNaN(other.m_ptB[2]); |
| 286 | m_ptB[3].AssertEqualOrNaN(other.m_ptB[3]); |
| 287 | } |
| 288 | |
| 289 | protected: |
| 290 | */ |
| 291 | // Data |
| 292 | m_ptB: [GpPointR; 4], |
| 293 | // The defining Bezier points |
| 294 | } |
| 295 | |
| 296 | impl CBezier { |
| 297 | pub fn new(curve: [GpPointR; 4]) -> Self { |
| 298 | Self { m_ptB: curve } |
| 299 | } |
| 300 | |
| 301 | pub fn is_degenerate(&self) -> bool { |
| 302 | self.m_ptB[0] == self.m_ptB[1] && |
| 303 | self.m_ptB[0] == self.m_ptB[2] && |
| 304 | self.m_ptB[0] == self.m_ptB[3] |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | pub trait CFlatteningSink { |
| 309 | fn FirstTangent(&mut self, vecTangent: Option<GpPointR>); |
| 310 | fn AcceptPointAndTangent(&mut self, |
| 311 | pt: &GpPointR, |
| 312 | // The point |
| 313 | vec: &GpPointR, |
| 314 | // The tangent there |
| 315 | fLast: bool |
| 316 | // Is this the last point on the curve? |
| 317 | ) -> HRESULT; |
| 318 | |
| 319 | fn AcceptPoint(&mut self, |
| 320 | pt: &GpPointR, |
| 321 | // The point |
| 322 | t: f32, |
| 323 | // Parameter we're at |
| 324 | fAborted: &mut bool, |
| 325 | lastPoint: bool |
| 326 | ) -> HRESULT; |
| 327 | } |
| 328 | |
| 329 | //+----------------------------------------------------------------------------- |
| 330 | // |
| 331 | // Class: |
| 332 | // CBezierFlattener |
| 333 | // |
| 334 | // Synopsis: |
| 335 | // Generates a polygonal apprximation to a given Bezier curve |
| 336 | // |
| 337 | //------------------------------------------------------------------------------ |
| 338 | pub struct CBezierFlattener<'a> |
| 339 | { |
| 340 | bezier: CBezier, |
| 341 | // Flattening defining data |
| 342 | m_pSink: &'a mut dyn CFlatteningSink, // The recipient of the flattening data |
| 343 | m_rTolerance: f32, // Prescribed tolerance |
| 344 | m_fWithTangents: bool, // Generate tangent vectors if true |
| 345 | m_rQuarterTolerance: f32,// Prescribed tolerance/4 (for doubling the step) |
| 346 | m_rFuzz: f32, // Computational zero |
| 347 | |
| 348 | // Flattening working data |
| 349 | m_ptE: [GpPointR; 4], // The moving basis of the curve definition |
| 350 | m_cSteps: i32, // The number of steps left to the end of the curve |
| 351 | m_rParameter: f32, // Parameter value |
| 352 | m_rStepSize: f32, // Steps size in parameter domain |
| 353 | } |
| 354 | impl<'a> CBezierFlattener<'a> { |
| 355 | /*fn new( |
| 356 | __in_ecount_opt(1) CFlatteningSink *pSink, |
| 357 | // The reciptient of the flattened data |
| 358 | IN GpReal rTolerance) |
| 359 | // Flattening tolerance |
| 360 | { |
| 361 | Initialize(pSink, rTolerance); |
| 362 | }*/ |
| 363 | /* |
| 364 | void SetTarget(__in_ecount_opt(1) CFlatteningSink *pSink) |
| 365 | { |
| 366 | m_pSink = pSink; |
| 367 | } |
| 368 | |
| 369 | void Initialize( |
| 370 | __in_ecount_opt(1) CFlatteningSink *pSink, |
| 371 | // The reciptient of the flattened data |
| 372 | IN GpReal rTolerance); |
| 373 | // Flattening tolerance |
| 374 | |
| 375 | void SetPoint( |
| 376 | __in UINT i, |
| 377 | // index of the point (must be between 0 and 3) |
| 378 | __in_ecount(1) const GpPointR &pt) |
| 379 | // point value |
| 380 | { |
| 381 | Assert(i < 4); |
| 382 | m_ptB[i] = pt; |
| 383 | } |
| 384 | |
| 385 | HRESULT GetFirstTangent( |
| 386 | __out_ecount(1) GpPointR &vecTangent) const; |
| 387 | // Tangent vector there |
| 388 | |
| 389 | GpPointR GetLastTangent() const; |
| 390 | |
| 391 | HRESULT Flatten( |
| 392 | IN bool fWithTangents); // Return tangents with the points if true |
| 393 | |
| 394 | private: |
| 395 | // Disallow copy constructor |
| 396 | CBezierFlattener(__in_ecount(1) const CBezierFlattener &) |
| 397 | { |
| 398 | RIP("CBezierFlattener copy constructor reached."); |
| 399 | } |
| 400 | |
| 401 | protected: |
| 402 | */ |
| 403 | /* fn Step( |
| 404 | __out_ecount(1) bool &fAbort); // Set to true if flattening should be aborted |
| 405 | |
| 406 | fn HalveTheStep(); |
| 407 | |
| 408 | fn TryDoubleTheStep();*/ |
| 409 | |
| 410 | } |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | // Licensed to the .NET Foundation under one or more agreements. |
| 416 | // The .NET Foundation licenses this file to you under the MIT license. |
| 417 | // See the LICENSE file in the project root for more information. |
| 418 | |
| 419 | |
| 420 | //+----------------------------------------------------------------------------- |
| 421 | // |
| 422 | |
| 423 | // |
| 424 | // $TAG ENGR |
| 425 | |
| 426 | // $Module: win_mil_graphics_geometry |
| 427 | // $Keywords: |
| 428 | // |
| 429 | // $Description: |
| 430 | // Implementation of CBezierFlattener. |
| 431 | // |
| 432 | // $ENDTAG |
| 433 | // |
| 434 | //------------------------------------------------------------------------------ |
| 435 | |
| 436 | impl<'a> CBezierFlattener<'a> { |
| 437 | ///////////////////////////////////////////////////////////////////////////////// |
| 438 | // |
| 439 | // Implementation of CBezierFlattener |
| 440 | |
| 441 | //+----------------------------------------------------------------------------- |
| 442 | // |
| 443 | // Member: |
| 444 | // CBezierFlattener::Initialize |
| 445 | // |
| 446 | // Synopsis: |
| 447 | // Initialize the sink and tolerance |
| 448 | // |
| 449 | //------------------------------------------------------------------------------ |
| 450 | pub fn new(bezier: &CBezier, |
| 451 | pSink: &'a mut dyn CFlatteningSink, |
| 452 | // The reciptient of the flattened data |
| 453 | rTolerance: f32) // Flattening tolerance |
| 454 | -> Self |
| 455 | { |
| 456 | let mut result = CBezierFlattener { |
| 457 | bezier: bezier.clone(), |
| 458 | // Flattening defining data |
| 459 | m_pSink: pSink, // The recipient of the flattening data |
| 460 | m_rTolerance: 0., // Prescribed tolerance |
| 461 | m_fWithTangents: false, // Generate tangent vectors if true |
| 462 | m_rQuarterTolerance: 0.,// Prescribed tolerance/4 (for doubling the step) |
| 463 | m_rFuzz: 0., // Computational zero |
| 464 | |
| 465 | // Flattening working data |
| 466 | m_ptE: [GpPointR { x: 0., y: 0.}; 4], // The moving basis of the curve definition |
| 467 | m_cSteps: 0, // The number of steps left to the end of the curve |
| 468 | m_rParameter: 0., // Parameter value |
| 469 | m_rStepSize: 0., // Steps size in parameter domain |
| 470 | }; |
| 471 | |
| 472 | // If rTolerance == NaN or less than 0, we'll treat it as 0. |
| 473 | result.m_rTolerance = if rTolerance >= 0.0 { rTolerance } else { 0.0 }; |
| 474 | result.m_rFuzz = rTolerance * rTolerance * SQ_LENGTH_FUZZ; |
| 475 | |
| 476 | // The error is tested on max(|e2|, |e2|), which represent 6 times the actual error, so: |
| 477 | result.m_rTolerance *= 6.; |
| 478 | result.m_rQuarterTolerance = result.m_rTolerance * 0.25; |
| 479 | result |
| 480 | } |
| 481 | |
| 482 | //+----------------------------------------------------------------------------- |
| 483 | // |
| 484 | // Member: |
| 485 | // CBezierFlattener::Flatten |
| 486 | // |
| 487 | // Synopsis: |
| 488 | // Flatten this curve |
| 489 | // |
| 490 | // Notes: |
| 491 | |
| 492 | // The algorithm is described in detail in the 1995 patent # 5367617 "System and |
| 493 | // method of hybrid forward differencing to render Bezier splines" to be found |
| 494 | // on the Microsoft legal dept. web site (LCAWEB). Additional references are: |
| 495 | // Lien, Shantz and Vaughan Pratt, "Adaptive Forward Differencing for |
| 496 | // Rendering Curves and Surfaces", Computer Graphics, July 1987 |
| 497 | // Chang and Shantz, "Rendering Trimmed NURBS with Adaptive Forward |
| 498 | // Differencing", Computer Graphics, August 1988 |
| 499 | // Foley and Van Dam, "Fundamentals of Interactive Computer Graphics" |
| 500 | // |
| 501 | // The basic idea is to replace the Bernstein basis (underlying Bezier curves) |
| 502 | // with the Hybrid Forward Differencing (HFD) basis which is more efficient at |
| 503 | // for flattening. Each one of the 3 actions - Step, Halve and Double (step |
| 504 | // size) this basis affords very efficient formulas for computing coefficients |
| 505 | // for the new interval. |
| 506 | // |
| 507 | // The coefficients of the HFD basis are defined in terms of the Bezier |
| 508 | // coefficients as follows: |
| 509 | // |
| 510 | // e0 = p0, e1 = p3 - p0, e2 = 6(p1 - 2p2 + p3), e3 = 6(p0 - 2p1 + p2), |
| 511 | // |
| 512 | // but formulas may be easier to understand by going through the power basis |
| 513 | // representation: f(t) = a*t + b*t + c * t^2 + d * t^3. |
| 514 | // |
| 515 | // The conversion is then: |
| 516 | // e0 = a |
| 517 | // e1 = f(1) - f(0) = b + c + d |
| 518 | // e2 = f"(1) = 2c + 6d |
| 519 | // e3 = f"(0) = 2c |
| 520 | // |
| 521 | // This is inverted to: |
| 522 | // a = e0 |
| 523 | // c = e3 / 2 |
| 524 | // d = (e2 - 2c) / 6 = (e2 - e3) / 6 |
| 525 | // b = e1 - c - d = e1 - e2 / 6 - e3 / 3 |
| 526 | // |
| 527 | // a, b, c, d for the new (halved, doubled or forwarded) interval are derived |
| 528 | // and then converted to e0, e1, e2, e3 using these relationships. |
| 529 | // |
| 530 | // An exact integer version is implemented in Bezier.h and Bezier.cpp. |
| 531 | // |
| 532 | //------------------------------------------------------------------------------ |
| 533 | |
| 534 | |
| 535 | pub fn Flatten(&mut self, |
| 536 | fWithTangents: bool) // Return tangents with the points if true |
| 537 | -> HRESULT |
| 538 | { |
| 539 | |
| 540 | let hr = S_OK; |
| 541 | let mut fAbort = false; |
| 542 | |
| 543 | /*if (!self.m_pSink) |
| 544 | { |
| 545 | return E_UNEXPECTED; |
| 546 | }*/ |
| 547 | |
| 548 | self.m_fWithTangents = fWithTangents; |
| 549 | if self.m_fWithTangents { |
| 550 | self.m_pSink.FirstTangent(self.GetFirstTangent()) |
| 551 | } |
| 552 | |
| 553 | self.m_cSteps = 1; |
| 554 | |
| 555 | self.m_rParameter = 0.; |
| 556 | self.m_rStepSize = 1.; |
| 557 | |
| 558 | // Compute the HFD basis |
| 559 | self.m_ptE[0] = self.bezier.m_ptB[0]; |
| 560 | self.m_ptE[1] = self.bezier.m_ptB[3] - self.bezier.m_ptB[0]; |
| 561 | self.m_ptE[2] = (self.bezier.m_ptB[1] - self.bezier.m_ptB[2] * 2. + self.bezier.m_ptB[3]) * 6.; // The second derivative at curve end |
| 562 | self.m_ptE[3] = (self.bezier.m_ptB[0] - self.bezier.m_ptB[1] * 2. + self.bezier.m_ptB[2]) * 6.; // The second derivative at curve start |
| 563 | |
| 564 | // Determine the initial step size |
| 565 | self.m_cSteps = 1; |
| 566 | while ((self.m_ptE[2].ApproxNorm() > self.m_rTolerance) || (self.m_ptE[3].ApproxNorm() > self.m_rTolerance)) && |
| 567 | (self.m_rStepSize > TWICE_MIN_BEZIER_STEP_SIZE) |
| 568 | |
| 569 | { |
| 570 | self.HalveTheStep(); |
| 571 | } |
| 572 | |
| 573 | while self.m_cSteps > 1 |
| 574 | { |
| 575 | IFC!(self.Step(&mut fAbort)); |
| 576 | if fAbort { |
| 577 | return hr; |
| 578 | } |
| 579 | |
| 580 | // E[3] was already tested as E[2] in the previous step |
| 581 | if self.m_ptE[2].ApproxNorm() > self.m_rTolerance && |
| 582 | self.m_rStepSize > TWICE_MIN_BEZIER_STEP_SIZE |
| 583 | { |
| 584 | // Halving the step once is provably sufficient (see Notes above), so --- |
| 585 | self.HalveTheStep(); |
| 586 | } |
| 587 | else |
| 588 | { |
| 589 | // --- but the step can possibly be more than doubled, hence the while loop |
| 590 | while self.TryDoubleTheStep() { |
| 591 | continue; |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | // Last point |
| 597 | if self.m_fWithTangents |
| 598 | { |
| 599 | IFC!(self.m_pSink.AcceptPointAndTangent(&self.bezier.m_ptB[3], &self.GetLastTangent(), true /* last point */)); |
| 600 | } |
| 601 | else |
| 602 | { |
| 603 | IFC!(self.m_pSink.AcceptPoint(&self.bezier.m_ptB[3], 1., &mut fAbort, true)); |
| 604 | } |
| 605 | |
| 606 | return hr; |
| 607 | } |
| 608 | //+----------------------------------------------------------------------------- |
| 609 | // |
| 610 | // Member: |
| 611 | // CBezierFlattener::Step |
| 612 | // |
| 613 | // Synopsis: |
| 614 | // Step forward on the polygonal approximation of the curve |
| 615 | // |
| 616 | // Notes: |
| 617 | // Taking a step means replacing a,b,c,d by coefficients of g(t) = f(t+1). |
| 618 | // Express those in terms of a,b,c,d and convert to e0, e1, e2, e3 to get: |
| 619 | // |
| 620 | // New e0 = e0 + e1 |
| 621 | // New e1 = e1 + e2 |
| 622 | // New e2 = 2e2 - e3 |
| 623 | // New e3 = e2 |
| 624 | // |
| 625 | // The patent application (see above) explains why. |
| 626 | // |
| 627 | // Getting a tangent vector is a minor enhancement along the same lines: |
| 628 | // f'(0) = b = 6e1 - e2 - 2e3. |
| 629 | // |
| 630 | //------------------------------------------------------------------------------ |
| 631 | |
| 632 | fn Step(&mut self, |
| 633 | fAbort: &mut bool) -> HRESULT // Set to true if flattening should be aborted, untouched otherwise |
| 634 | { |
| 635 | let hr = S_OK; |
| 636 | |
| 637 | // Compute the basis for the same curve on the next interval |
| 638 | let mut pt; |
| 639 | |
| 640 | self.m_ptE[0] += self.m_ptE[1]; |
| 641 | pt = self.m_ptE[2]; |
| 642 | self.m_ptE[1] += pt; |
| 643 | self.m_ptE[2] += pt; self.m_ptE[2] -= self.m_ptE[3]; |
| 644 | self.m_ptE[3] = pt; |
| 645 | |
| 646 | // Increment the parameter |
| 647 | self.m_rParameter += self.m_rStepSize; |
| 648 | |
| 649 | // Generate the start point of the new interval |
| 650 | if self.m_fWithTangents |
| 651 | { |
| 652 | // Compute the tangent there |
| 653 | pt = self.m_ptE[1] * 6. - self.m_ptE[2] - self.m_ptE[3] * 2.; // = twice the derivative at E[0] |
| 654 | IFC!(self.m_pSink.AcceptPointAndTangent(&self.m_ptE[0], &pt, false /* not the last point */)); |
| 655 | } |
| 656 | else |
| 657 | { |
| 658 | IFC!(self.m_pSink.AcceptPoint(&self.m_ptE[0], self.m_rParameter, fAbort, false)); |
| 659 | } |
| 660 | |
| 661 | self.m_cSteps-=1; |
| 662 | return hr; |
| 663 | } |
| 664 | //+----------------------------------------------------------------------------- |
| 665 | // |
| 666 | // Member: |
| 667 | // CBezierFlattener::HalveTheStep |
| 668 | // |
| 669 | // Synopsis: |
| 670 | // Halve the size of the step |
| 671 | // |
| 672 | // Notes: |
| 673 | // Halving the step means replacing a,b,c,d by coefficients of g(t) = |
| 674 | // f(t/2). Experss those in terms of a,b,c,d and convert to e0, e1, e2, e3 |
| 675 | // to get: |
| 676 | // |
| 677 | // New e0 = e0 |
| 678 | // New e1 = (e1 - e2) / 2 |
| 679 | // New e2 = (e2 + e3) / 8 |
| 680 | // New e3 = e3 / 4 |
| 681 | // |
| 682 | // The patent application (see above) explains why. |
| 683 | // |
| 684 | //------------------------------------------------------------------------------ |
| 685 | fn HalveTheStep(&mut self) |
| 686 | { |
| 687 | self.m_ptE[2] += self.m_ptE[3]; self.m_ptE[2] *= 0.125; |
| 688 | self.m_ptE[1] -= self.m_ptE[2]; self.m_ptE[1] *= 0.5; |
| 689 | self.m_ptE[3] *= 0.25; |
| 690 | |
| 691 | self.m_cSteps *= 2; // Double the number of steps left |
| 692 | self.m_rStepSize *= 0.5; |
| 693 | } |
| 694 | //+----------------------------------------------------------------------------- |
| 695 | // |
| 696 | // Member: |
| 697 | // CBezierFlattener::TryDoubleTheStep |
| 698 | // |
| 699 | // Synopsis: |
| 700 | // Double the step size if possible within tolerance. |
| 701 | // |
| 702 | // Notes: |
| 703 | // Coubling the step means replacing a,b,c,d by coefficients of g(t) = |
| 704 | // f(2t). Experss those in terms of a,b,c,d and convert to e0, e1, e2, e3 |
| 705 | // to get: |
| 706 | // |
| 707 | // New e0 = e0 |
| 708 | // New e1 = 2e1 + e2 |
| 709 | // New e2 = 8e2 - 4e3 |
| 710 | // New e3 = 4e3 |
| 711 | // |
| 712 | // The patent application (see above) explains why. Note also that these |
| 713 | // formulas are the inverse of those for halving the step. |
| 714 | // |
| 715 | //------------------------------------------------------------------------------ |
| 716 | fn |
| 717 | TryDoubleTheStep(&mut self) -> bool |
| 718 | { |
| 719 | let mut fDoubled = 0 == (self.m_cSteps & 1); |
| 720 | if fDoubled |
| 721 | { |
| 722 | let ptTemp = self.m_ptE[2] * 2. - self.m_ptE[3]; |
| 723 | |
| 724 | fDoubled = (self.m_ptE[3].ApproxNorm() <= self.m_rQuarterTolerance) && |
| 725 | (ptTemp.ApproxNorm() <= self.m_rQuarterTolerance); |
| 726 | |
| 727 | if fDoubled |
| 728 | { |
| 729 | self.m_ptE[1] *= 2.; self.m_ptE[1] += self.m_ptE[2]; |
| 730 | self.m_ptE[3] *= 4.; |
| 731 | self.m_ptE[2] = ptTemp * 4.; |
| 732 | |
| 733 | self.m_cSteps /= 2; // Halve the number of steps left |
| 734 | self.m_rStepSize *= 2.; |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | return fDoubled; |
| 739 | } |
| 740 | //+----------------------------------------------------------------------------- |
| 741 | // |
| 742 | // Member: |
| 743 | // CBezierFlattener::GetFirstTangent |
| 744 | // |
| 745 | // Synopsis: |
| 746 | // Get the tangent at curve start |
| 747 | // |
| 748 | // Return: |
| 749 | // WGXERR_ZEROVECTOR if the tangent vector has practically 0 length |
| 750 | // |
| 751 | // Notes: |
| 752 | // This method can return an error if all the points are bunched together. |
| 753 | // The idea is that the caller will detect that, abandon this curve, and |
| 754 | // never call GetLasttangent, which can therefore be presumed to succeed. |
| 755 | // The failure here is benign. |
| 756 | // |
| 757 | //------------------------------------------------------------------------------ |
| 758 | pub fn GetFirstTangent(&self) -> Option<GpPointR> // Tangent vector there |
| 759 | |
| 760 | { |
| 761 | |
| 762 | let mut vecTangent = self.bezier.m_ptB[1] - self.bezier.m_ptB[0]; |
| 763 | if vecTangent * vecTangent > self.m_rFuzz |
| 764 | { |
| 765 | return Some(vecTangent); // - we're done |
| 766 | } |
| 767 | // Zero first derivative, go for the second |
| 768 | vecTangent = self.bezier.m_ptB[2] - self.bezier.m_ptB[0]; |
| 769 | if vecTangent * vecTangent > self.m_rFuzz |
| 770 | { |
| 771 | return Some(vecTangent); // - we're done |
| 772 | } |
| 773 | // Zero second derivative, go for the third |
| 774 | vecTangent = self.bezier.m_ptB[3] - self.bezier.m_ptB[0]; |
| 775 | |
| 776 | if vecTangent * vecTangent <= self.m_rFuzz |
| 777 | { |
| 778 | return None; |
| 779 | } |
| 780 | |
| 781 | return Some(vecTangent); // no RRETURN, error is expected |
| 782 | } |
| 783 | //+----------------------------------------------------------------------------- |
| 784 | // |
| 785 | // Member: |
| 786 | // CBezierFlattener::GetLastTangent |
| 787 | // |
| 788 | // Synopsis: |
| 789 | // Get the tangent at curve end |
| 790 | // |
| 791 | // Return: |
| 792 | // The tangent |
| 793 | // |
| 794 | // Notes: |
| 795 | // This method has no error return while GetFirstTangent returns |
| 796 | // WGXERR_ZEROVECTOR if the tangent is zero. The idea is that we should |
| 797 | // only fail if all the control points coincide, that should have been |
| 798 | // detected at GetFirstTangent, and then we should have not be called. |
| 799 | // |
| 800 | //------------------------------------------------------------------------------ |
| 801 | fn GetLastTangent(&self) -> GpPointR |
| 802 | { |
| 803 | let mut vecTangent = self.bezier.m_ptB[3] - self.bezier.m_ptB[2]; |
| 804 | |
| 805 | // If the curve is degenerate, we should have detected it at curve-start, skipped this curve |
| 806 | // altogether and not be here. But the test in GetFirstTangent is for the point-differences |
| 807 | // 1-0, 2-0 and 3-0, while here it is for points 3-2, 3-1 and 3-0, which is not quite the same. |
| 808 | // Still, In a disk of radius r no 2 points are more than 2r apart. The tests are done with |
| 809 | // squared distance, and m_rFuzz is the minimal accepted squared distance. GetFirstTangent() |
| 810 | // succeeded, so there is a pair of points whose squared distance is greater than m_rfuzz. |
| 811 | // So the squared radius of a disk about point 3 that contains the remaining points must be |
| 812 | // at least m_rFuzz/4. Allowing some margin for arithmetic error: |
| 813 | |
| 814 | let rLastTangentFuzz = self.m_rFuzz/8.; |
| 815 | |
| 816 | if vecTangent * vecTangent <= rLastTangentFuzz |
| 817 | { |
| 818 | // Zero first derivative, go for the second |
| 819 | vecTangent = self.bezier.m_ptB[3] - self.bezier.m_ptB[1]; |
| 820 | if vecTangent * vecTangent <= rLastTangentFuzz |
| 821 | { |
| 822 | // Zero second derivative, go for the third |
| 823 | vecTangent = self.bezier.m_ptB[3] - self.bezier.m_ptB[0]; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | debug_assert! (!(vecTangent * vecTangent < rLastTangentFuzz)); // Ignore NaNs |
| 828 | |
| 829 | return vecTangent; |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | |
| 834 | #[test] |
| 835 | #[cfg_attr(debug_assertions, should_panic)] |
| 836 | fn degenerate_tangent() { |
| 837 | struct Sink; |
| 838 | impl CFlatteningSink for Sink { |
| 839 | fn FirstTangent(&mut self, _: Option<GpPointR>) { } |
| 840 | fn AcceptPointAndTangent(&mut self, |
| 841 | _: &GpPointR, _: &GpPointR, _: bool |
| 842 | ) -> HRESULT { |
| 843 | return S_OK; |
| 844 | } |
| 845 | |
| 846 | fn AcceptPoint(&mut self, |
| 847 | _: &GpPointR, _: f32, |
| 848 | _: &mut bool, _: bool |
| 849 | ) -> HRESULT { |
| 850 | return S_OK; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | let bezier = CBezier {m_ptB: [ |
| 855 | GpPointR {x: 0., y: 0.0005}, |
| 856 | GpPointR {x: 0., y: 0.0005}, |
| 857 | GpPointR {x: 0., y: 0.}, |
| 858 | GpPointR {x: 0., y: 0.}]}; |
| 859 | |
| 860 | let mut t = Sink; |
| 861 | let mut f = CBezierFlattener::new(&bezier, &mut t, 0.25); |
| 862 | f.Flatten(true); |
| 863 | } |