/usr/include/BALL/MATHS/FFT3D.h is in libball1.4-dev 1.4.1+20111206-3.
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 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 | // -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
#ifndef BALL_MATHS_TFFT3D_H
#define BALL_MATHS_TFFT3D_H
#ifndef BALL_COMMON_EXCEPTION_H
# include <BALL/COMMON/exception.h>
#endif
#ifndef BALL_DATATYPE_REGULARDATA3D_H
# include <BALL/DATATYPE/regularData3D.h>
#endif
//#ifndef BALL_MATHS_VECTOR2_H
//# include <BALL/MATHS/vector3.h>
//#endif
#include <BALL/MATHS/fftwCommon.h>
#include <math.h>
#include <complex>
#include <fftw3.h>
namespace BALL
{
/** A class to perform Fast Fourier Transforms and inverse Fast Fourier Transforms
on regularly spaced three dimensional data. \par
This class makes use of the freely available library <b>FFTW</b>, which can be
found at www.fftw.org
coordinate system can be handled automatically. The normalization is chosen
symmetrically.
\par
S/TFFT3D.h
\ingroup FFT
*/
template <typename ComplexTraits>
class TFFT3D
: public TRegularData3D<std::complex<typename ComplexTraits::ComplexPrecision> >
{
public:
typedef std::complex<typename ComplexTraits::ComplexPrecision> Complex;
typedef TRegularData3D<std::complex<typename ComplexTraits::ComplexPrecision> > ComplexVector;
BALL_CREATE(TFFT3D)
/** @name Constructors and Destructors
*/
//@{
/// Default constructor
TFFT3D();
/// Copy constructor
TFFT3D(const TFFT3D &data);
/** Detailed constructor. \par
@param ldnX The binary logarithm of the number of grid points in X direction (we use the logarithm to
ensure that the number of points is a power of two, which is important for
the FFT)
@param ldnY The binary logarithm of the number of grid points in Y direction
@param ldnZ The binary logarithm of the number of grid points in Z direction
@param stepPhysX The step width in X direction in physical space
@param stepPhysY The step width in Y direction in physical space
@param stepPhysZ The step width in Z direction in physical space
@param origin The origin of the coordinate system
@param inFourierSpace Flag to decide whether the data is assumed to be in physical or Fourier
space
*/
// AR: ldn is not any longer the binary logarithm but the absolute number of grid points
TFFT3D(Size ldnX, Size ldnY, Size ldnZ, double stepPhysX=1., double stepPhysY=1., double stepPhysZ=1., Vector3 origin=Vector3(0.,0.,0), bool inFourierSpace=false);
/// Destructor
virtual ~TFFT3D();
//@}
/** @name Assignment
*/
//@{
/// Assignment operator
const TFFT3D& operator = (const TFFT3D& fft_3d);
/** Clear the contents.
*/
virtual void clear();
/** Clear the contents and reset all attributes.
*/
virtual void destroy();
//@}
/** @name Predicates
*/
//@{
/** Equality operator.
*/
bool operator == (const TFFT3D& fft3d) const;
//@}
// @name Accessors
//@{
/** Perform a single fast Fourier transform on the data.
*/
void doFFT();
/** Perform a single inverse Fourier transform on the data.
*/
void doiFFT();
/** Translate the origin in physical space about {\em trans_origin},
i.e. the new origin will be located at the former position {\em trans_origin}.
If the result is out of bounds, the function does nothing and
returns <b> false </b>.
*/
bool translate(const Vector3& trans_origin);
/** Set the step width in physical space to {\em new_width_x, new_width_y, new_width_z}.
The step width in Fourier space is automatically adjusted
accordingly. {\em new_width_x, new_width_y and new_width_z} must be positive, otherwise
the function does nothing and retuns <b> false </b>.
*/
bool setPhysStepWidth(double new_width_x, double new_width_y, double new_width_z);
/** Returns the step width in physical space in X direction.
*/
double getPhysStepWidthX() const;
/** Returns the step width in physical space in Y direction.
*/
double getPhysStepWidthY() const;
/** Returns the step width in physical space in Z direction.
*/
double getPhysStepWidthZ() const;
/** Returns the step width in Fourier space in X direction.
*/
double getFourierStepWidthX() const;
/** Returns the step width in Fourier space in Y direction.
*/
double getFourierStepWidthY() const;
/** Returns the step width in Fourier space in Z direction.
*/
double getFourierStepWidthZ() const;
/** Returns the minimal position of the grid in physical space in X direction.
*/
double getPhysSpaceMinX() const;
/** Returns the minimal position of the grid in physical space in Y direction.
*/
double getPhysSpaceMinY() const
;
/** Returns the minimal position of the grid in physical space in Z direction.
*/
double getPhysSpaceMinZ() const;
/** Returns the maximal position of the grid in physical space in X direction.
*/
double getPhysSpaceMaxX() const;
/** Returns the maximal position of the grid in physical space in Y direction.
*/
double getPhysSpaceMaxY() const;
/** Returns the maximal position of the grid in physical space in Z direction.
*/
double getPhysSpaceMaxZ() const;
/** Returns the minimal position of the grid in Fourier space in X direction.
*/
double getFourierSpaceMinX() const;
/** Returns the minimal position of the grid in Fourier space in Y direction.
*/
double getFourierSpaceMinY() const;
/** Returns the minimal position of the grid in Fourier space in Z direction.
*/
double getFourierSpaceMinZ() const;
/** Returns the maximal position of the grid in Fourier space in X direction.
*/
double getFourierSpaceMaxX() const;
/** Returns the maximal position of the grid in Fourier space in Y direction.
*/
double getFourierSpaceMaxY() const;
/** Returns the maximal position of the grid in Fourier space in Z direction.
*/
double getFourierSpaceMaxZ() const;
/** Return the largest grid position for the x direction.
This method returns the maximum position allowed in the grid. As the point
in the origin has the indices (0, 0, 0), this method returns the number of
points in X direction minus one.
*/
Size getMaxXIndex() const;
/** Return the largest grid position for the y direction.
This method returns the maximum position allowed in the grid. As the point
in the origin has the indices (0, 0, 0), this method returns the number of
points in Y direction minus one.
*/
Size getMaxYIndex() const;
/** Return the largest grid position for the z direction.
This method returns the maximum position allowed in the grid. As the point
in the origin has the indices (0, 0, 0), this method returns the number of
points in Z direction minus one.
*/
Size getMaxZIndex() const;
/** Return the number of inverse transforms that have been carried out using this class.
This is an important factor for the normalization of the data.
*/
Size getNumberOfInverseTransforms() const;
/** Returns the grid coordinate corresponding to the position.
*/
Vector3 getGridCoordinates(Position position) const;
/** Returns the data at the grid position closest to <b> pos </b>,
* and automatically includes
* the correct phase factor and (symmetric) normalization.
*
* @throw Exception::OutOfGrid if pos is outside the grid boundaries
*/
Complex getData(const Vector3& pos) const;
/** Returns the data at point <b>pos</b>. If <b>pos</b> is not a
* point on the grid, the data is linearly interpolated.
* This method automatically includes the correct phase factor
* and (symmetric) normalization.
*
* @throw Exception::OutOfGrid if pos is outside the grid boundaries
*/
Complex getInterpolatedValue(const Vector3& pos) const;
/** Sets the data point at the grid position closest to <b> pos </b>
* to the value <b> val </b>, and -- if called in Fourier space --
* automatically includes the correct phase factor and
* (symmetric) normalization.
*
* @throw Exception::OutOfGrid if pos is outside the grid boundaries
*/
void setData(const Vector3& pos, Complex val);
/** Access the data at the grid position closest to <b> pos </b>.
* This function returns the "raw" data at that position.
*
* @throw Exception::OutOfGrid if pos is outside the grid boundaries
*/
Complex& operator[](const Vector3& pos);
/** Access the data at the grid position closest to <b> pos </b>.
* This function returns the "raw" data at that position.
*
* @throw Exception::OutOfGrid if pos is outside the grid boundaries
*/
const Complex& operator[](const Vector3& pos) const;
/** Access the (raw) data at Position pos.
*
* @throw Exception::OutOfGrid if pos is outside the grid boundaries
*/
Complex& operator[](const Position& pos)
{
return TRegularData3D<Complex>::operator [] (pos);
}
/** Access the (raw) data at Position pos. Const method.
*
* @throw Exception::OutOfGrid if pos is outside the grid boundaries
*/
const Complex& operator[](const Position& pos) const
{
return TRegularData3D<Complex>::operator [] (pos);
}
// AR:
void setNumberOfFFTTransforms(Size num)
{
numPhysToFourier_ = num;
}
// AR:
void setNumberOfiFFTTransforms(Size num)
{
numFourierToPhys_ = num;
}
/** This computes the phase factor in Fourier space that results
if the origin of the coordinate system in physical space
is not in the "lower left corner".
*/
Complex phase(const Vector3& pos) const;
//@}
/** @name Predicates
*/
//@{
/** Returns <b>true</b> if the data is considered to be in Fourier space,
<b>false</b> otherwise.
*/
bool isInFourierSpace() const;
//@}
protected:
Size lengthX_, lengthY_, lengthZ_;
bool inFourierSpace_;
Size numPhysToFourier_;
Size numFourierToPhys_;
Vector3 origin_;
double stepPhysX_, stepPhysY_, stepPhysZ_;
double stepFourierX_, stepFourierY_, stepFourierZ_;
Vector3 minPhys_, maxPhys_;
Vector3 minFourier_, maxFourier_;
// AR: new version for FFTW3
typename ComplexTraits::FftwPlan planForward_;
typename ComplexTraits::FftwPlan planBackward_;
// AR: to control plan calculation with new fftw3
Size dataLength_;
Complex *dataAdress_;
bool planCalculated_;
};
/** Default type
*/
typedef TFFT3D<BALL_FFTW_DEFAULT_TRAITS> FFT3D;
/** Global assignment operator from TFFT3D to TRegularData3D<Complex>
*/
template <typename ComplexTraits>
const TRegularData3D<typename TFFT3D<ComplexTraits>::Complex>& operator <<
(TRegularData3D<typename TFFT3D<ComplexTraits>::Complex>& to, const TFFT3D<ComplexTraits>& from);
/** Global assignment operator from TFFT3D to TRegularData3D<float>.
This operator assigns the <b>real</b> part of the complex TFFT3D-data to the
TRegularData3D<float> to.
*/
template <typename ComplexTraits>
const RegularData3D& operator << (RegularData3D& to, const TFFT3D<ComplexTraits>& from);
template <typename ComplexTraits>
TFFT3D<ComplexTraits>::TFFT3D()
: TRegularData3D<Complex>(),
dataLength_(0),
dataAdress_(0),
planCalculated_(false)
{
}
template <typename ComplexTraits>
bool TFFT3D<ComplexTraits>::operator == (const TFFT3D& fft3D) const
{
// AR: test whether data_.size() == fft3D.data_.size()
// instead of testing 3 lengths. Better for vector handling.
if (lengthX_ == fft3D.lengthX_ &&
lengthY_ == fft3D.lengthY_ &&
lengthZ_ == fft3D.lengthZ_ &&
origin_ == fft3D.origin_ &&
stepPhysX_ == fft3D.stepPhysX_ &&
stepPhysY_ == fft3D.stepPhysY_ &&
stepPhysZ_ == fft3D.stepPhysZ_ &&
stepFourierX_ == fft3D.stepFourierX_ &&
stepFourierY_ == fft3D.stepFourierY_ &&
stepFourierZ_ == fft3D.stepFourierZ_ &&
minPhys_ == fft3D.minPhys_ &&
maxPhys_ == fft3D.maxPhys_ &&
minFourier_ == fft3D.minFourier_ &&
maxFourier_ == fft3D.maxFourier_ &&
numPhysToFourier_ == fft3D.numPhysToFourier_ &&
numFourierToPhys_ == fft3D.numFourierToPhys_ &&
planCalculated_ == fft3D.planCalculated_)
{
Vector3 min = inFourierSpace_ ? minFourier_ : minPhys_;
Vector3 max = inFourierSpace_ ? maxFourier_ : maxPhys_;
double stepX = inFourierSpace_ ? stepFourierX_ : stepPhysX_;
double stepY = inFourierSpace_ ? stepFourierY_ : stepPhysY_;
double stepZ = inFourierSpace_ ? stepFourierZ_ : stepPhysZ_;
for (double posX=min.x; posX<=max.x; posX+=stepX)
{
for (double posY=min.y; posY<=max.y; posY+=stepY)
{
for (double posZ=min.z; posZ<=max.z; posZ+=stepZ)
{
if (getData(Vector3(posX,posY,posZ)) != fft3D.getData(Vector3(posX,posY,posZ)))
{
return false;
}
}
}
}
return true;
}
return false;
}
template <typename ComplexTraits>
bool TFFT3D<ComplexTraits>::translate(const Vector3& trans_origin)
{
Position internalOriginX = (Position) Maths::rint(trans_origin.x*stepPhysX_);
Position internalOriginY = (Position) Maths::rint(trans_origin.y*stepPhysY_);
Position internalOriginZ = (Position) Maths::rint(trans_origin.z*stepPhysZ_);
if ((internalOriginX <= lengthX_) && (internalOriginY <= lengthY_) && (internalOriginZ <= lengthZ_))
{
origin_.x = trans_origin.x;
origin_.y = trans_origin.y;
origin_.z = trans_origin.z;
minPhys_ = Vector3(-origin_.x,-origin_.y,-origin_.z);
maxPhys_ = Vector3(((lengthX_-1)*stepPhysX_)-origin_.x,((lengthY_-1)*stepPhysY_)-origin_.y,((lengthZ_-1)*stepPhysZ_)-origin_.z);
minFourier_ = Vector3(-(lengthX_/2.-1)*stepFourierX_,-(lengthY_/2.-1)*stepFourierY_,-(lengthZ_/2.-1)*stepFourierZ_);
maxFourier_ = Vector3((lengthX_/2.)*stepFourierX_,(lengthY_/2.)*stepFourierY_,(lengthZ_/2.)*stepFourierZ_);
return true;
}
else
{
return false;
}
}
template <typename ComplexTraits>
bool TFFT3D<ComplexTraits>::setPhysStepWidth(double new_width_x, double new_width_y, double new_width_z)
{
if ((new_width_x <= 0) || (new_width_y <= 0) || (new_width_z <= 0))
{
return false;
}
else
{
stepPhysX_ = new_width_x;
stepPhysY_ = new_width_y;
stepPhysZ_ = new_width_z;
stepFourierX_ = 2.*M_PI/(stepPhysX_*lengthX_);
stepFourierY_ = 2.*M_PI/(stepPhysY_*lengthY_);
stepFourierZ_ = 2.*M_PI/(stepPhysZ_*lengthZ_);
minPhys_ = Vector3(-origin_.x,-origin_.y,-origin_.z);
maxPhys_ = Vector3(((lengthX_-1)*stepPhysX_)-origin_.x,((lengthY_-1)*stepPhysY_)-origin_.y,((lengthZ_-1)*stepPhysZ_)-origin_.z);
minFourier_ = Vector3(-(lengthX_/2.-1)*stepFourierX_,-(lengthY_/2.-1)*stepFourierY_,-(lengthZ_/2.-1)*stepFourierZ_);
maxFourier_ = Vector3((lengthX_/2.)*stepFourierX_,(lengthY_/2.)*stepFourierY_,(lengthZ_/2.)*stepFourierZ_);
return true;
}
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getPhysStepWidthX() const
{
return stepPhysX_;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getPhysStepWidthY() const
{
return stepPhysY_;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getPhysStepWidthZ() const
{
return stepPhysZ_;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getFourierStepWidthX() const
{
return stepFourierX_;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getFourierStepWidthY() const
{
return stepFourierY_;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getFourierStepWidthZ() const
{
return stepFourierZ_;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getPhysSpaceMinX() const
{
return minPhys_.x;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getPhysSpaceMinY() const
{
return minPhys_.y;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getPhysSpaceMinZ() const
{
return minPhys_.z;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getPhysSpaceMaxX() const
{
return maxPhys_.x;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getPhysSpaceMaxY() const
{
return maxPhys_.y;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getPhysSpaceMaxZ() const
{
return maxPhys_.z;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getFourierSpaceMinX() const
{
return minFourier_.x;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getFourierSpaceMinY() const
{
return minFourier_.y;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getFourierSpaceMinZ() const
{
return minFourier_.z;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getFourierSpaceMaxX() const
{
return maxFourier_.x;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getFourierSpaceMaxY() const
{
return maxFourier_.y;
}
template <typename ComplexTraits>
double TFFT3D<ComplexTraits>::getFourierSpaceMaxZ() const
{
return maxFourier_.z;
}
template <typename ComplexTraits>
Size TFFT3D<ComplexTraits>::getMaxXIndex() const
{
return (lengthX_ - 1);
}
template <typename ComplexTraits>
Size TFFT3D<ComplexTraits>::getMaxYIndex() const
{
return (lengthY_ - 1);
}
template <typename ComplexTraits>
Size TFFT3D<ComplexTraits>::getMaxZIndex() const
{
return (lengthZ_ - 1);
}
template <typename ComplexTraits>
Size TFFT3D<ComplexTraits>::getNumberOfInverseTransforms() const
{
return numFourierToPhys_;
}
template <typename ComplexTraits>
Vector3 TFFT3D<ComplexTraits>::getGridCoordinates(Position position) const
{
if (!inFourierSpace_)
{
if (position >= ComplexVector::size())
{
throw Exception::OutOfGrid(__FILE__, __LINE__);
}
Vector3 r;
Position x, y, z;
z = position % lengthZ_;
y = (position % (lengthY_ * lengthZ_)) / lengthZ_;
x = position / (lengthY_ * lengthZ_);
r.set(-origin_.x + (float)x * stepPhysX_,
-origin_.y + (float)y * stepPhysY_,
-origin_.z + (float)z * stepPhysZ_);
return r;
}
else
{
if (position >= ComplexVector::size())
{
throw Exception::OutOfGrid(__FILE__, __LINE__);
}
Vector3 r;
Index x, y, z;
z = position % lengthZ_;
y = (position % (lengthY_ * lengthZ_)) / lengthZ_;
x = position / (lengthY_ * lengthZ_);
if (x>=lengthX_/2.)
{
x-=lengthX_;
}
if (y>=lengthY_/2.)
{
y-=lengthY_;
}
if (z>=lengthZ_/2.)
{
z-=lengthZ_;
}
r.set((float)x * stepFourierX_,
(float)y * stepFourierY_,
(float)z * stepFourierZ_);
return r;
}
}
template <typename ComplexTraits>
typename TFFT3D<ComplexTraits>::Complex TFFT3D<ComplexTraits>::getData(const Vector3& pos) const
{
Complex result;
double normalization=1.;
if (!inFourierSpace_)
{
result = (*this)[pos];
normalization=1./((float)pow((float)(lengthX_*lengthY_*lengthZ_),(int)numFourierToPhys_));
}
else
{
// AR:
//old: result = (*this)[pos];
result = (*this)[pos]*phase(pos);
//normalization=1./pow(sqrt(2.*M_PI),3)*(stepPhysX_*stepPhysY_*stepPhysZ_)/((float)pow((float)(lengthX_*lengthY_*lengthZ_),(int)numFourierToPhys_));
normalization=1./pow(sqrt(2.*M_PI),3)/((float)pow((float)(lengthX_*lengthY_*lengthZ_),(int)numFourierToPhys_));
//normalization=1./(sqrt(2.*M_PI))*(stepPhysX_*stepPhysY_*stepPhysZ_)/((float)pow((float)(lengthX_*lengthY_*lengthZ_),(int)numFourierToPhys_));
}
result *= normalization;
return result;
}
template <typename ComplexTraits>
typename TFFT3D<ComplexTraits>::Complex TFFT3D<ComplexTraits>::getInterpolatedValue(const Vector3& pos) const
{
Complex result;
Vector3 min = inFourierSpace_ ? minFourier_ : minPhys_;
Vector3 max = inFourierSpace_ ? maxFourier_ : maxPhys_;
double stepX = inFourierSpace_ ? stepFourierX_ : stepPhysX_;
double stepY = inFourierSpace_ ? stepFourierY_ : stepPhysY_;
double stepZ = inFourierSpace_ ? stepFourierZ_ : stepPhysZ_;
if ( (pos.x < min.x) || (pos.y < min.y) || (pos.z < min.z)
|| (pos.x > max.x) || (pos.y > max.y) || (pos.z > max.z) )
{
throw Exception::OutOfGrid(__FILE__, __LINE__);
}
Vector3 h(pos.x - min.x, pos.y - min.y, pos.z - min.z);
double modX = fmod((double)h.x,stepX);
double modY = fmod((double)h.y,stepY);
double modZ = fmod((double)h.z,stepZ);
if (modX==0 && modY==0 && modZ==0) // we are on the grid
{
return getData(pos);
}
double beforeX = floor(h.x/stepX)*stepX+min.x;
double beforeY = floor(h.y/stepY)*stepY+min.y;
double beforeZ = floor(h.z/stepZ)*stepZ+min.z;
double afterX = ceil(h.x/stepX)*stepX+min.x;
double afterY = ceil(h.y/stepY)*stepY+min.y;
double afterZ = ceil(h.z/stepZ)*stepZ+min.z;
double tx = (pos.x - beforeX)/stepX;
double ty = (pos.y - beforeY)/stepY;
double tz = (pos.z - beforeZ)/stepZ;
result = getData(Vector3(beforeX,beforeY,beforeZ))*(typename ComplexTraits::ComplexPrecision)((1.-tx)*(1.-ty)*(1.-tz));
result += getData(Vector3(afterX, beforeY,beforeZ))*(typename ComplexTraits::ComplexPrecision)( tx *(1.-ty)*(1.-tz));
result += getData(Vector3(beforeX,afterY, beforeZ))*(typename ComplexTraits::ComplexPrecision)((1.-tx)* ty *(1.-tz));
result += getData(Vector3(beforeX,beforeY,afterZ ))*(typename ComplexTraits::ComplexPrecision)((1.-tx)*(1.-ty)* tz );
result += getData(Vector3(afterX, afterY, beforeZ))*(typename ComplexTraits::ComplexPrecision)( tx * ty *(1.-tz));
result += getData(Vector3(afterX, beforeY,afterZ ))*(typename ComplexTraits::ComplexPrecision)( tx *(1.-ty)* tz );
result += getData(Vector3(beforeX,afterY, afterZ ))*(typename ComplexTraits::ComplexPrecision)((1.-tx)* ty * tz );
result += getData(Vector3(afterX, afterY, afterZ ))*(typename ComplexTraits::ComplexPrecision)( tx * ty * tz );
return result;
}
template <typename ComplexTraits>
void TFFT3D<ComplexTraits>::setData(const Vector3& pos, Complex val)
{
Complex dummy;
if (!inFourierSpace_)
{
dummy = Complex(val.real()*((typename ComplexTraits::ComplexPrecision)pow((typename ComplexTraits::ComplexPrecision)(lengthX_*lengthY_*lengthZ_),(int)numFourierToPhys_)),
val.imag()*((typename ComplexTraits::ComplexPrecision)pow((typename ComplexTraits::ComplexPrecision)(lengthX_*lengthY_*lengthZ_),(int)numFourierToPhys_)));
(*this)[pos]=dummy;
}
else
{
val*=phase(pos)*(typename ComplexTraits::ComplexPrecision)((pow(sqrt(2*M_PI),3)/(stepPhysX_*stepPhysY_*stepPhysZ_)))
*((typename ComplexTraits::ComplexPrecision)pow((typename ComplexTraits::ComplexPrecision)(lengthX_*lengthY_*lengthZ_),(int)numFourierToPhys_));
/*val*=phase(pos)*(typename ComplexTraits::ComplexPrecision)((sqrt(2*M_PI)/(stepPhysX_*stepPhysY_*stepPhysZ_)))
*((typename ComplexTraits::ComplexPrecision)pow((typename ComplexTraits::ComplexPrecision)(lengthX_*lengthY_*lengthZ_),(int)numFourierToPhys_));*/
dummy = val;
(*this)[pos]=dummy;
}
}
template <typename ComplexTraits>
typename TFFT3D<ComplexTraits>::Complex& TFFT3D<ComplexTraits>::operator[](const Vector3& pos)
{
Index internalPos;
if (!inFourierSpace_)
{
Index i, j, k;
i = (Index) Maths::rint((pos.x+origin_.x)/stepPhysX_);
j = (Index) Maths::rint((pos.y+origin_.y)/stepPhysY_);
k = (Index) Maths::rint((pos.z+origin_.z)/stepPhysZ_);
internalPos = (k + (j + i*lengthY_)*lengthZ_);
/*(Index) rint( (pos.z+origin_.z)/stepPhysZ_
+ ( (pos.y+origin_.y)/stepPhysY_
+ (pos.x+origin_.x)/stepPhysX_*lengthY_
) * lengthZ_
); */
}
else
{
Index i, j, k;
i = (Index) Maths::rint(pos.x/stepFourierX_);
j = (Index) Maths::rint(pos.y/stepFourierY_);
k = (Index) Maths::rint(pos.z/stepFourierZ_);
if (i<0)
{
i+=lengthX_;
}
if (j<0)
{
j+=lengthY_;
}
if (k<0)
{
k+=lengthZ_;
}
internalPos = (k + (j + i*lengthY_)*lengthZ_);
}
if ((internalPos < 0) || (internalPos>=(Index) (lengthX_*lengthY_*lengthZ_)))
{
throw Exception::OutOfGrid(__FILE__, __LINE__);
}
return TRegularData3D<Complex>::operator[]((Position)internalPos);
}
template <typename ComplexTraits>
const typename TFFT3D<ComplexTraits>::Complex& TFFT3D<ComplexTraits>::operator[](const Vector3& pos) const
{
Index internalPos;
if (!inFourierSpace_)
{
Index i, j, k;
i = (Index) Maths::rint((pos.x+origin_.x)/stepPhysX_);
j = (Index) Maths::rint((pos.y+origin_.y)/stepPhysY_);
k = (Index) Maths::rint((pos.z+origin_.z)/stepPhysZ_);
internalPos = (k + (j + i*lengthY_)*lengthZ_);
/*(Index) rint( (pos.z+origin_.z)/stepPhysZ_
+ ( (pos.y+origin_.y)/stepPhysY_
+ (pos.x+origin_.x)/stepPhysX_*lengthY_
) * lengthZ_
); */
}
else
{
Index i, j, k;
i = (Index) Maths::rint(pos.x/stepFourierX_);
j = (Index) Maths::rint(pos.y/stepFourierY_);
k = (Index) Maths::rint(pos.z/stepFourierZ_);
if (i<0)
{
i+=lengthX_;
}
if (j<0)
{
j+=lengthY_;
}
if (k<0)
{
k+=lengthZ_;
}
internalPos = (k + (j + i*lengthY_)*lengthZ_);
}
if ((internalPos < 0) || (internalPos>=(Index) (lengthX_*lengthY_*lengthZ_)))
{
throw Exception::OutOfGrid(__FILE__, __LINE__);
}
return TRegularData3D<Complex>::operator[]((Position)internalPos);
}
/*Complex& TFFT3D<ComplexTraits>::operator[](const Position& pos)
{
return operator [] (pos);
}
const Complex& TFFT3D<ComplexTraits>::operator[](const Position& pos) const
{
return operator [] (pos);
}
*/
template <typename ComplexTraits>
typename TFFT3D<ComplexTraits>::Complex TFFT3D<ComplexTraits>::phase(const Vector3& pos) const
{
// AR: old version: -2.*M_PI...
double phase = 2.*M_PI*( (Maths::rint(pos.x/stepFourierX_))*(Maths::rint(origin_.x/stepPhysX_))
/lengthX_
+ (Maths::rint(pos.y/stepFourierY_))*(Maths::rint(origin_.y/stepPhysY_))
/lengthY_
+ (Maths::rint(pos.z/stepFourierZ_))*(Maths::rint(origin_.z/stepPhysZ_))
/lengthZ_ );
Complex result = Complex(cos(phase), sin(phase));
return result;
/*double phase = -2.*M_PI*( (rint(pos.x/stepFourierX_))*(rint(origin_.x/stepPhysX_))
/lengthX_
+ (Maths::rint(pos.y/stepFourierY_))*(Maths::rint(origin_.y/stepPhysY_))
/lengthY_
+ (Maths::rint(pos.z/stepFourierZ_))*(Maths::rint(origin_.z/stepPhysZ_))
/lengthZ_ );
Complex result = Complex(cos(phase), sin(phase));
return result;*/
}
template <typename ComplexTraits>
bool TFFT3D<ComplexTraits>::isInFourierSpace() const
{
return inFourierSpace_;
}
template <typename ComplexTraits>
const TRegularData3D<typename TFFT3D<ComplexTraits>::Complex>& operator<<
(TRegularData3D<typename TFFT3D<ComplexTraits>::Complex>& to, const TFFT3D<ComplexTraits>& from)
{
// first decide if the TFFT3D data is in Fourier space.
if (!from.isInFourierSpace())
{
// create a new grid
Size lengthX = from.getMaxXIndex()+1;
Size lengthY = from.getMaxYIndex()+1;
Size lengthZ = from.getMaxZIndex()+1;
TRegularData3D<typename TFFT3D<ComplexTraits>::Complex> newGrid(TRegularData3D<typename TFFT3D<ComplexTraits>::Complex>::IndexType(lengthX, lengthY, lengthZ),
Vector3(from.getPhysSpaceMinX(), from.getPhysSpaceMinY(), from.getPhysSpaceMinZ()),
Vector3(from.getPhysSpaceMaxX(), from.getPhysSpaceMaxY(), from.getPhysSpaceMaxZ()));
// and fill it
double normalization=1./(pow((float)(lengthX*lengthY*lengthZ),(int)from.getNumberOfInverseTransforms()));
typename TFFT3D<ComplexTraits>::Complex dataIn;
typename TFFT3D<ComplexTraits>::Complex dataOut;
for (Position i = 0; i < from.size(); i++)
{
Position x, y, z;
z = i % lengthZ;
y = (i % (lengthY * lengthZ)) / lengthZ;
x = i / (lengthY * lengthZ);
dataIn = from[i];
dataOut = dataIn;
newGrid[x + (y + z*lengthY)*lengthZ] = dataOut*(typename ComplexTraits::ComplexPrecision)normalization;
}
to = newGrid;
return to;
}
else
{
// we are in Fourier space
// create a new grid
Size lengthX = from.getMaxXIndex()+1;
Size lengthY = from.getMaxYIndex()+1;
Size lengthZ = from.getMaxZIndex()+1;
//float stepPhysX = from.getPhysStepWidthX();
//float stepPhysY = from.getPhysStepWidthY();
//float stepPhysZ = from.getPhysStepWidthZ();
float stepFourierX = from.getFourierStepWidthX();
float stepFourierY = from.getFourierStepWidthY();
float stepFourierZ = from.getFourierStepWidthZ();
TRegularData3D<typename TFFT3D<ComplexTraits>::Complex> newGrid(TRegularData3D<typename TFFT3D<ComplexTraits>::Complex>::IndexType(lengthX, lengthY, lengthZ),
Vector3(from.getFourierSpaceMinX(),
from.getFourierSpaceMinY(),
from.getFourierSpaceMinZ()),
Vector3(from.getFourierSpaceMaxX(),
from.getFourierSpaceMaxY(),
from.getFourierSpaceMaxZ()));
// and fill it
// AR: old double normalization=1./(sqrt(2.*M_PI))*(stepPhysX*stepPhysY*stepPhysZ)/(pow((float)(lengthX*lengthY*lengthZ),from.getNumberOfInverseTransforms()));
double normalization=1./pow(sqrt(2.*M_PI),3)/(pow((float)(lengthX*lengthY*lengthZ),(int)from.getNumberOfInverseTransforms()));
Index x, y, z;
Vector3 r;
typename TFFT3D<ComplexTraits>::Complex dataIn;
typename TFFT3D<ComplexTraits>::Complex dataOut;
for (Position i = 0; i < from.size(); i++)
{
z = i % lengthZ;
y = (i % (lengthY * lengthZ)) / lengthZ;
x = i / (lengthY * lengthZ);
if (x>lengthX/2.)
{
x-=lengthX;
}
if (y>lengthY/2.)
{
y-=lengthY;
}
if (z>lengthZ/2.)
{
z-=lengthZ;
}
r.set((float)x * stepFourierX,
(float)y * stepFourierY,
(float)z * stepFourierZ);
dataIn = from[i];
dataOut = dataIn;
newGrid[x + (y + z*lengthY)*lengthZ] = dataOut*(typename ComplexTraits::ComplexPrecision)normalization*from.phase(r);
}
to = newGrid;
return to;
}
}
template <typename ComplexTraits>
const RegularData3D& operator << (RegularData3D& to, const TFFT3D<ComplexTraits>& from)
{
// first decide if the TFFT3D data is in Fourier space.
if (!from.isInFourierSpace())
{
// create a new grid
Size lengthX = from.getMaxXIndex()+1;
Size lengthY = from.getMaxYIndex()+1;
Size lengthZ = from.getMaxZIndex()+1;
RegularData3D newGrid(RegularData3D::IndexType(lengthX, lengthY, lengthZ), Vector3(from.getPhysSpaceMinX(), from.getPhysSpaceMinY(), from.getPhysSpaceMinZ()),
Vector3(from.getPhysSpaceMaxX(), from.getPhysSpaceMaxY(), from.getPhysSpaceMaxZ()));
// and fill it
double normalization = 1./(pow((float)(lengthX*lengthY*lengthZ),(int)from.getNumberOfInverseTransforms()));
typename TFFT3D<ComplexTraits>::Complex dataIn;
typename TFFT3D<ComplexTraits>::Complex dataOut;
for (Position i = 0; i < from.size(); i++)
{
Position x, y, z;
z = i % lengthZ;
y = (i % (lengthY * lengthZ)) / lengthZ;
x = i / (lengthY * lengthZ);
dataIn = from[i];
dataOut = dataIn;
newGrid[x + (y + z*lengthY)*lengthZ] = dataOut.real()*normalization;
}
to = newGrid;
return to;
}
else
{
// we are in Fourier space
// create a new grid
Size lengthX = from.getMaxXIndex()+1;
Size lengthY = from.getMaxYIndex()+1;
Size lengthZ = from.getMaxZIndex()+1;
//float stepPhysX = from.getPhysStepWidthX();
//float stepPhysY = from.getPhysStepWidthY();
//float stepPhysZ = from.getPhysStepWidthZ();
float stepFourierX = from.getFourierStepWidthX();
float stepFourierY = from.getFourierStepWidthY();
float stepFourierZ = from.getFourierStepWidthZ();
RegularData3D newGrid(RegularData3D::IndexType(lengthX, lengthY, lengthZ), Vector3(from.getFourierSpaceMinX(), from.getFourierSpaceMinY(), from.getFourierSpaceMinZ()), Vector3(from.getFourierSpaceMaxX(), from.getFourierSpaceMaxY(), from.getFourierSpaceMaxZ()));
// and fill it
// AR: old version double normalization=1./(sqrt(2.*M_PI))*(stepPhysX*stepPhysY*stepPhysZ)/(pow((float)(lengthX*lengthY*lengthZ),from.getNumberOfInverseTransforms()));
double normalization=1./pow(sqrt(2.*M_PI),3)/(pow((float)(lengthX*lengthY*lengthZ),(int)from.getNumberOfInverseTransforms()));
Index x, y, z;
signed int xp, yp, zp;
Vector3 r;
typename TFFT3D<ComplexTraits>::Complex dataIn;
typename TFFT3D<ComplexTraits>::Complex dataOut;
for (Position i = 0; i < from.size(); i++)
{
z = i % lengthZ;
y = (i % (lengthY * lengthZ)) / lengthZ;
x = i / (lengthY * lengthZ);
xp = x;
yp = y;
zp = z;
if (xp>=lengthX/2.)
{
xp-=(int)lengthX;
}
if (yp>=lengthY/2.)
{
yp-=(int)lengthY;
}
if (zp>=lengthZ/2.)
{
zp-=(int)lengthZ;
}
if (x>=lengthX/2.)
{
x-=(int)(lengthX/2.);
}
else
{
x+=(int)(lengthX/2.);
}
if (y>=lengthY/2.)
{
y-=(int)(lengthY/2.);
}
else
{
y+=(int)(lengthY/2.);
}
if (z>=lengthZ/2.)
{
z-=(int)(lengthZ/2.);
}
else
{
z+=(int)(lengthZ/2.);
}
r.set((float)xp * stepFourierX,
(float)yp * stepFourierY,
(float)zp * stepFourierZ);
dataIn = from[i];
dataOut = dataIn;
newGrid[x + (y + z*lengthY)*lengthZ] = (dataOut*(typename ComplexTraits::ComplexPrecision)normalization*from.phase(r)).real();
}
to = newGrid;
return to;
}
}
}
#endif // BALL_MATHS_TFFT3D_H
|