------------------------------------------------------------------------------ Given structured coordinates (i,j,k) for a point in a structured point dataset, compute the gradient vector from the scalar data at that point. The scalars s are the scalars from which the gradient is to be computed. This method will treat structured point datasets of any dimension.
| 568 | // The scalars s are the scalars from which the gradient is to be computed. |
| 569 | // This method will treat structured point datasets of any dimension. |
| 570 | void vtkImageData::GetPointGradient(int i, int j, int k, vtkDataArray* s, double g[3]) |
| 571 | { |
| 572 | const double* ar = this->Spacing; |
| 573 | double sp, sm; |
| 574 | const int* extent = this->GetExtent(); |
| 575 | |
| 576 | vtkIdType dims[3]; |
| 577 | this->GetDimensions(dims); |
| 578 | vtkIdType ijsize = dims[0] * dims[1]; |
| 579 | |
| 580 | // Adjust i,j,k to the start of the extent |
| 581 | i -= extent[0]; |
| 582 | j -= extent[2]; |
| 583 | k -= extent[4]; |
| 584 | |
| 585 | // Check for out-of-bounds |
| 586 | if (i < 0 || i >= dims[0] || j < 0 || j >= dims[1] || k < 0 || k >= dims[2]) |
| 587 | { |
| 588 | g[0] = g[1] = g[2] = 0.0; |
| 589 | return; |
| 590 | } |
| 591 | |
| 592 | // i-axis |
| 593 | if (dims[0] == 1) |
| 594 | { |
| 595 | g[0] = 0.0; |
| 596 | } |
| 597 | else if (i == 0) |
| 598 | { |
| 599 | sp = s->GetComponent(i + 1 + j * dims[0] + k * ijsize, 0); |
| 600 | sm = s->GetComponent(i + j * dims[0] + k * ijsize, 0); |
| 601 | g[0] = (sm - sp) / ar[0]; |
| 602 | } |
| 603 | else if (i == (dims[0] - 1)) |
| 604 | { |
| 605 | sp = s->GetComponent(i + j * dims[0] + k * ijsize, 0); |
| 606 | sm = s->GetComponent(i - 1 + j * dims[0] + k * ijsize, 0); |
| 607 | g[0] = (sm - sp) / ar[0]; |
| 608 | } |
| 609 | else |
| 610 | { |
| 611 | sp = s->GetComponent(i + 1 + j * dims[0] + k * ijsize, 0); |
| 612 | sm = s->GetComponent(i - 1 + j * dims[0] + k * ijsize, 0); |
| 613 | g[0] = 0.5 * (sm - sp) / ar[0]; |
| 614 | } |
| 615 | |
| 616 | // j-axis |
| 617 | if (dims[1] == 1) |
| 618 | { |
| 619 | g[1] = 0.0; |
| 620 | } |
| 621 | else if (j == 0) |
| 622 | { |
| 623 | sp = s->GetComponent(i + (j + 1) * dims[0] + k * ijsize, 0); |
| 624 | sm = s->GetComponent(i + j * dims[0] + k * ijsize, 0); |
| 625 | g[1] = (sm - sp) / ar[1]; |
| 626 | } |
| 627 | else if (j == (dims[1] - 1)) |
no test coverage detected