58 #define CUR_TIME std::chrono::system_clock::now() 59 #define ELAPSED_TIME(x,y) ((std::chrono::duration<double>)(y - x)).count() 60 #define CUR_TIME_DURATION CUR_TIME.time_since_epoch() 61 #define CUR_TIME_DURATION_MILLI_SEC std::chrono::duration_cast<std::chrono::milliseconds>(CUR_TIME_DURATION).count() 63 template<
typename type,
typename T>
65 return type(p._Val[
_RE]);
68 template<
typename type,
typename T>
75 for (
auto item : arr) {
if (item < min) min = item; }
81 for (
int i = 0; i < size; i++) {
82 if (*(src + i) < min) min = *(src + i);
89 for (
auto item : arr) {
if (item > max) max = item; }
95 for (
int i = 0; i < size; i++) {
96 if (*(src + i) > max) max = *(src + i);
104 for (
int i = 0; i < size; i++) {
113 inline void abs(
const oph::Complex<T>& src, oph::Complex<T>& dst) {
118 inline void absArr(
const std::vector<Complex<T>>& src, std::vector<oph::Complex<T>>& dst) {
120 dst.reserve(src.size());
121 for (
auto item : src) dst.push_back(
oph::abs(item));
125 inline void absMat(
const oph::matrix<oph::Complex<T>>& src, oph::matrix<oph::Complex<T>>& dst) {
126 if (src.getSize() != dst.getSize())
return;
128 for (
int x = 0; x < matSize[
_X]; x++) {
129 for (
int y = 0; y < matSize[
_Y]; y++)
135 inline void absCplx(
const oph::Complex<T>& src, T& dst) {
136 dst = sqrt(src._Val[
_RE]*src._Val[
_RE] + src._Val[
_IM]*src._Val[
_IM]);
140 inline void absCplxArr(
const oph::Complex<T>* src, T* dst,
const int& size) {
141 for (
int i = 0; i < size; i++) {
142 absCplx<T>(*(src + i), *(dst + i));
148 return src->_Val[
_RE];
152 inline void realPart(
const oph::Complex<T>* src, T* dst,
const int& size) {
153 for (
int i = 0; i < size; i++) {
154 *(dst + i) = (src + i)->_Val[
_RE];
159 inline void angle(
const std::vector<Complex<T>>& src, std::vector<T>& dst) {
161 dst.reserve(src.size());
162 for (
auto item : src) { dst.push_back(src.angle()); }
166 inline T
angle(
const oph::Complex<T>& src) {
168 dst = atan2(src.imag(), src.real());
176 inline void normalize(
const Complex<T>* src, Complex<T>* dst,
const int& size) {
178 oph::absCplxArr<Real>(src,
abs, size);
182 for (
int i = 0; i < size; i++) {
183 *(dst + i) = *(src + i) / max;
188 inline void normalize(
const T* src, T* dst,
const int& size) {
193 for (
int i = 0; i < size; i++) {
194 *(dst + i) = (*(src + i) - min) / (max - min);
204 for (
oph::uint ydx = 0; ydx < ny; ydx++){
205 for (
oph::uint xdx = 0; xdx < nx; xdx++){
206 T *temp_pos = src + xdx + ydx * nx;
207 if ((xdx == 0) && (ydx == 0)) { minVal = *(temp_pos); maxVal = *(temp_pos); }
209 if ((*temp_pos) < minVal) minVal = (*temp_pos);
210 if ((*temp_pos) > maxVal) maxVal = (*temp_pos);
215 for (
oph::uint ydx = 0; ydx < ny; ydx++) {
216 for (
oph::uint xdx = 0; xdx < nx; xdx++) {
217 T *src_pos = src + xdx + ydx * nx;
220 *(res_pos) = oph::force_cast<oph::uchar>(((*(src_pos)-minVal) / (maxVal - minVal)) * 255 + 0.5);
229 inline void normalize(
const std::vector<T>* src, std::vector<oph::uchar>* dst) {
231 if (src->size() != dst->size())
234 dst->reserve(src->size());
237 auto iter = src->begin();
238 for (iter; iter != src->end(); iter++) {
239 if (iter == src->begin()) {
243 if (*iter < minVal) minVal = *iter;
244 if (*iter > maxVal) maxVal = *iter;
249 for (iter; iter != src->end(); iter++)
250 dst->push_back(oph::force_cast<oph::uchar>((((*iter) - minVal) / (maxVal - minVal)) * 255 + 0.5));
258 auto iter = pArr->begin() + (beginIndex);
259 auto it_end = pArr->begin() + (endIndex);
260 for (; iter != it_end; iter++) { (*iter) = _Value; }
265 for (
uint i = beginIndex; i <= endIndex; i++) {
266 *(pArr + i) = _Value;
274 inline void circShift(
const T* src, T* dst,
int shift_x,
int shift_y,
int xdim,
int ydim) {
275 for (
int i = 0; i < xdim; i++) {
276 int ti = (i + shift_x) % xdim;
277 if (ti < 0) ti = xdim + ti;
278 for (
int j = 0; j < ydim; j++) {
279 int tj = (j + shift_y) % ydim;
280 if (tj < 0) tj = ydim + tj;
281 dst[ti * ydim + tj] = src[i * ydim + j];
296 std::mt19937_64 rand_dev;
298 else rand_dev = std::mt19937_64(_SEED_VALUE);
300 std::uniform_real_distribution<Real> dist(min, max);
302 return dist(rand_dev);
315 std::mt19937_64 rand_dev;
317 else rand_dev = std::mt19937_64(_SEED_VALUE);
319 std::uniform_int_distribution<int> dist(min, max);
321 return dist(rand_dev);
324 inline void getPhase(oph::Complex<Real>* src,
Real* dst,
const int& size)
326 for (
int i = 0; i < size; i++) {
327 *(dst + i) = angle<Real>(*(src + i)) +
M_PI;
332 absCplxArr<Real>(src, dst, size);
337 ivec2 bufferSize = src.getSize();
339 *dst =
new oph::Complex<Real>[bufferSize[
_X] * bufferSize[
_Y]];
342 for (
int x = 0; x < bufferSize[
_X]; x++) {
343 for (
int y = 0; y < bufferSize[
_Y]; y++) {
344 (*dst)[idx] = src[x][y];
351 inline void Buffer2Field(
const T* src, matrix<T>& dst,
const ivec2 buffer_size) {
352 ivec2 matSize = buffer_size;
355 for (
int x = 0; x < matSize[
_X]; x++) {
356 for (
int y = 0; y < matSize[
_Y]; y++) {
357 dst[x][y] = src[idx];
373 #endif // !__function_h void abs(const oph::Complex< T > &src, oph::Complex< T > &dst)
void absMat(const oph::matrix< oph::Complex< T >> &src, oph::matrix< oph::Complex< T >> &dst)
void circShift(const T *src, T *dst, int shift_x, int shift_y, int xdim, int ydim)
Shifts the elements by shift_x, shift_y.
Real average(const Real *src, const int &size)
void absCplxArr(const oph::Complex< T > *src, T *dst, const int &size)
void getPhase(oph::Complex< Real > *src, Real *dst, const int &size)
void normalize(const Complex< T > *src, Complex< T > *dst, const int &size)
Normalize all elements of Complex<T>* src from 0 to 1.
void realPart(const oph::Complex< T > *src, T *dst, const int &size)
void absArr(const std::vector< Complex< T >> &src, std::vector< oph::Complex< T >> &dst)
void memsetArr(const std::vector< T > *pArr, T _Value, oph::uint beginIndex, oph::uint endIndex)
Set elements to specific values from begin index to end index.
void Field2Buffer(matrix< T > &src, T **dst)
type force_cast(const Complex< T > &p)
#define CUR_TIME_DURATION_MILLI_SEC
T getReal(const oph::Complex< T > src)
Real rand(const Real min, const Real max, oph::ulong _SEED_VALUE=0)
Get random Real value from min to max.
void getAmplitude(oph::Complex< Real > *src, Real *dst, const int &size)
void angle(const std::vector< Complex< T >> &src, std::vector< T > &dst)
Real minOfArr(const std::vector< Real > &arr)
Real maxOfArr(const std::vector< Real > &arr)
void Buffer2Field(const T *src, matrix< T > &dst, const ivec2 buffer_size)
void absCplx(const oph::Complex< T > &src, T &dst)