Merge pull request 'dev_yi' (#2) from dev_yi into main

Reviewed-on: #2
This commit is contained in:
张壹 2025-01-09 12:06:11 +08:00
commit aa362cb81b
6 changed files with 429 additions and 130 deletions

View File

@ -26,7 +26,7 @@ add_example(kde_ex OFF)
add_example(meshio_ex OFF)
add_example(autodiff_ex OFF)
add_example(multinary_ex OFF)
add_example(text_io_ex ON)
add_example(text_io_ex OFF)
add_example(getoption_ex OFF)
add_example(process_ex OFF)
add_example(array_ex OFF)

View File

@ -34,11 +34,15 @@ int main(int argc, char const *argv[]) try
{
// create a new array and give initial values
array<double> A(10, 0.0, 1.0);
A.memory_usage();
A.log2linear(2);
A.show();
A.linear2log(2);
A.for_each([](double &a, size_t i){ a += 1;});
A.show();
A.parallel_for_each([](double &a, size_t i){a += 1;});
A.show();
A.sequence(1.0, 0.5, 3, 4, 1);

View File

@ -71,7 +71,7 @@ void gctl::fir_filter::init(filting_type_e fi_type, window_type_e w_type, int ta
else if (fi_type == BandStop) band_stop_coefficient(f1/fs, f2/fs);
else throw std::runtime_error("[GCTL] Unkown filter type for gctl::fir_filter");
if (w_type == None) w_.assign_all(1.0);
if (w_type == None) w_.assign(1.0);
else if (w_type == Hamming) window_hamming(taps_, w_);
else if (w_type == Hanning) window_hanning(taps_, w_);
else if (w_type == Triangle) window_triangle(taps_, w_);

View File

@ -245,6 +245,103 @@ namespace gctl
*/
array<ArrValType>& operator/= (const array<ArrValType> &b);
/**
* @brief Overloaded summation operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType> operator+ (const ArrValType &b);
/**
* @brief Overloaded minus operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType> operator- (const ArrValType &b);
/**
* @brief Overloaded multiple operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType> operator* (const ArrValType &b);
/**
* @brief Overloaded division operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType> operator/ (const ArrValType &b);
/**
* @brief Overloaded summation operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*
*/
array<ArrValType>& operator+= (const ArrValType &b);
/**
* @brief Overloaded summation operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType>& operator-= (const ArrValType &b);
/**
* @brief Overloaded multiple operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType>& operator*= (const ArrValType &b);
/**
* @brief Overloaded division operator for the array template.
*
* @param a Input array
* @param b Scale value
* @return Target array
*/
array<ArrValType>& operator/= (const ArrValType &b);
/**
* @brief Compare two arraies.
*
* @return True if the two arraies are equal, false otherwise.
*/
bool operator== (const array<ArrValType> &b) const;
/**
* @brief Compare two arraies.
*
* @return True if the two arraies are not equal, false otherwise.
*/
bool operator!= (const array<ArrValType> &b) const;
/**
* @brief Compare two arraies.
*
* @param b Input array
* @param eps Tolerance for flaoting point number
*
* @return True if the two arraies are not equal, false otherwise.
*/
bool equals(const array<ArrValType> &b, ArrValType eps = ArrValType{}) const;
/**
* @brief Destructor
*/
@ -318,7 +415,7 @@ namespace gctl
*
* @param[in] in_val Input value.
*/
void assign_all(ArrValType in_val);
void assign(ArrValType in_val);
/**
* @brief Set value to segments of an array.
@ -334,7 +431,7 @@ namespace gctl
*
* @param b Input array
*/
void append_array(const array<ArrValType> &b);
void concat(const array<ArrValType> &b);
/**
* @brief Extract an array
@ -342,7 +439,7 @@ namespace gctl
* @param b Output array. Must be initialized before use.
* @param st Start index
*/
void extract_array(array<ArrValType> &b, size_t st = 0) const;
void slice(array<ArrValType> &b, size_t st = 0) const;
/**
* @brief Extract an array
@ -350,7 +447,7 @@ namespace gctl
* @param b Output array
* @param ids Index of the extracting elements. Must be initialized before use.
*/
void extract_array(array<ArrValType> &b, const array<size_t> &ids) const;
void slice(array<ArrValType> &b, const array<size_t> &ids) const;
/**
* @brief Insert data from an input array
@ -358,12 +455,12 @@ namespace gctl
* @param b The input array
* @param st The inserting point. The default is zero
*/
void insert_array(array<ArrValType> &b, size_t st = 0);
void insert(array<ArrValType> &b, size_t st = 0);
/**
* @brief Get element value at the given index.
*
* @note This function could be called by a pointer.
* @note This function will check if the index was out of range.
*
* @param[in] index index value.
*
@ -374,7 +471,7 @@ namespace gctl
/**
* @brief Get element value at the given index (Constant).
*
* @note This function could be called by a pointer.
* @note This function will check if the index was out of range.
*
* @param[in] index index value.
*
@ -391,7 +488,7 @@ namespace gctl
*
* @return element's value.
*/
ArrValType &operator[](size_t index);
ArrValType &operator[](size_t index) noexcept;
/**
* @brief Get element value at the given index .
@ -402,7 +499,7 @@ namespace gctl
*
* @return element's value.
*/
ArrValType &operator[](size_t index) const;
ArrValType &operator[](size_t index) const noexcept;
/**
* @brief Get the first element
@ -435,6 +532,8 @@ namespace gctl
/**
* @brief Get the pointer to a element at the given index.
*
* @note This function will check if the index was out of range.
*
* @param[in] index index value.
*
* @return Pointer to the element
@ -446,28 +545,28 @@ namespace gctl
*
* @return True for empty. False other wise.
*/
bool empty() const;
bool empty() const noexcept;
/**
* @brief Return the length of the class member array.
*
* @return Length.
*/
size_t size() const;
size_t size() const noexcept;
/**
* @brief Copy the array to a vector.
*
* @param b Target vector.
*/
void export_vector(std::vector<ArrValType> &b) const;
void output(std::vector<ArrValType> &b) const;
/**
* @brief Copy the array from a vector.
*
* @param b Target vector.
*/
void import_vector(const std::vector<ArrValType> &b);
void input(const std::vector<ArrValType> &b);
#ifdef GCTL_EIGEN
/**
@ -475,14 +574,14 @@ namespace gctl
*
* @param b Target vector.
*/
void export_eigen_vector(Eigen::VectorXd &b) const;
void output(Eigen::VectorXd &b) const;
/**
* @brief Copy the array from a Eigen3 vector.
*
* @param b Target vector.
*/
void import_eigen_vector(const Eigen::VectorXd &b);
void input(const Eigen::VectorXd &b);
#endif // GCTL_EIGEN
/**
@ -492,11 +591,23 @@ namespace gctl
typedef void (*foreach_a_ptr)(ArrValType &ele_ptr, size_t id);
/**
* @brief Operate on each and every element
* @brief
*
* @param func operation function
*
* [](ArrValType &a, size_t i){do something here...}
*
* @tparam BiaryOp
* @param op
*/
void for_each(foreach_a_ptr func);
template<typename BiaryOp>
void parallel_for_each(BiaryOp op)
{
#pragma omp parallel for
for (size_t i = 0; i < length_; i++)
{
op(val_[i], i);
}
}
/**
* @brief Display the elements.
@ -582,6 +693,13 @@ namespace gctl
*/
ArrValType dot(const array<ArrValType> &a);
/**
* @brief Return the sum of all elements.
*
* @return sum value.
*/
ArrValType sum() const;
/**
* @brief Return the mean value.
*
@ -680,6 +798,37 @@ namespace gctl
*
*/
void set2range(ArrValType min, ArrValType max, range_type_e rt = HardScale);
/**
* @brief
*
* @param value
* @return truefalse
*/
bool any_of(const ArrValType& value) const;
/**
* @brief
*
* @param value
* @return truefalse
*/
bool all_of(const ArrValType& value) const;
/**
* @brief
*
* @param value
* @return
*/
size_t count(const ArrValType& value) const;
/**
* @brief 使
*
* @return MB
*/
void memory_usage(std::ostream &os = std::cout) const noexcept;
};
template <typename ArrValType>
@ -761,12 +910,10 @@ namespace gctl
template <typename ArrValType>
array<ArrValType> array<ArrValType>::operator+ (const array<ArrValType> &b)
{
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("[gctl::array<T>::operator+] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
array<ArrValType> out(length_);
for (size_t i = 0; i < length_; i++)
@ -779,12 +926,10 @@ namespace gctl
template <typename ArrValType>
array<ArrValType> array<ArrValType>::operator- (const array<ArrValType> &b)
{
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("[gctl::array<T>::operator-] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
array<ArrValType> out(length_);
for (size_t i = 0; i < length_; i++)
@ -797,12 +942,10 @@ namespace gctl
template <typename ArrValType>
array<ArrValType> array<ArrValType>::operator* (const array<ArrValType> &b)
{
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("[gctl::array<T>::operator*] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
array<ArrValType> out(length_);
for (size_t i = 0; i < length_; i++)
@ -815,12 +958,10 @@ namespace gctl
template <typename ArrValType>
array<ArrValType> array<ArrValType>::operator/ (const array<ArrValType> &b)
{
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("[gctl::array<T>::operator/] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
array<ArrValType> out(length_);
for (size_t i = 0; i < length_; i++)
@ -833,12 +974,10 @@ namespace gctl
template <typename ArrValType>
array<ArrValType>& array<ArrValType>::operator+= (const array<ArrValType> &b)
{
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("[gctl::array<T>::operator+=] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
for (size_t i = 0; i < length_; i++)
{
@ -850,12 +989,10 @@ namespace gctl
template <typename ArrValType>
array<ArrValType>& array<ArrValType>::operator-= (const array<ArrValType> &b)
{
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("[gctl::array<T>::operator-=] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
for (size_t i = 0; i < length_; i++)
{
@ -867,12 +1004,10 @@ namespace gctl
template <typename ArrValType>
array<ArrValType>& array<ArrValType>::operator*= (const array<ArrValType> &b)
{
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("[gctl::array<T>::operator*=] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
for (size_t i = 0; i < length_; i++)
{
@ -884,12 +1019,10 @@ namespace gctl
template <typename ArrValType>
array<ArrValType>& array<ArrValType>::operator/= (const array<ArrValType> &b)
{
#ifdef GCTL_CHECK_SIZE
if (b.size() != length_)
{
throw std::runtime_error("[gctl::array<T>::operator/=] Incompatible array sizes.");
}
#endif // GCTL_CHECK_SIZE
for (size_t i = 0; i < length_; i++)
{
@ -898,6 +1031,130 @@ namespace gctl
return *this;
}
template <typename ArrValType>
array<ArrValType> array<ArrValType>::operator+ (const ArrValType &b)
{
array<ArrValType> out(length_);
for (size_t i = 0; i < length_; i++)
{
out[i] = val_[i] + b;
}
return out;
}
template <typename ArrValType>
array<ArrValType> array<ArrValType>::operator- (const ArrValType &b)
{
array<ArrValType> out(length_);
for (size_t i = 0; i < length_; i++)
{
out[i] = b - val_[i];
}
return out;
}
template <typename ArrValType>
array<ArrValType> array<ArrValType>::operator* (const ArrValType &b)
{
array<ArrValType> out(length_);
for (size_t i = 0; i < length_; i++)
{
out[i] = b*val_[i];
}
return out;
}
template <typename ArrValType>
array<ArrValType> array<ArrValType>::operator/ (const ArrValType &b)
{
array<ArrValType> out(length_);
for (size_t i = 0; i < length_; i++)
{
out[i] = b/val_[i];
}
return out;
}
template <typename ArrValType>
array<ArrValType>& array<ArrValType>::operator+= (const ArrValType &b)
{
for (size_t i = 0; i < length_; i++)
{
val_[i] += b;
}
return *this;
}
template <typename ArrValType>
array<ArrValType>& array<ArrValType>::operator-= (const ArrValType &b)
{
for (size_t i = 0; i < length_; i++)
{
val_[i] -= b;
}
return *this;
}
template <typename ArrValType>
array<ArrValType>& array<ArrValType>::operator*= (const ArrValType &b)
{
for (size_t i = 0; i < length_; i++)
{
val_[i] *= b;
}
return *this;
}
template <typename ArrValType>
array<ArrValType>& array<ArrValType>::operator/= (const ArrValType &b)
{
for (size_t i = 0; i < length_; i++)
{
val_[i] /= b;
}
return *this;
}
template <typename ArrValType>
bool array<ArrValType>::operator== (const array<ArrValType> &b) const
{
if (length_ != b.size()) return false;
for (size_t i = 0; i < length_; i++)
{
if (val_[i] != b[i]) return false;
}
return true;
}
template <typename ArrValType>
bool array<ArrValType>::operator!= (const array<ArrValType> &b) const
{
return !(*this == b);
}
template <typename ArrValType>
bool array<ArrValType>::equals(const array<ArrValType> &b, ArrValType eps) const
{
if (length_ != b.size()) return false;
if (eps == ArrValType{})
{
for (size_t i = 0; i < length_; i++)
{
if (val_[i] != b[i]) return false;
}
}
else
{
for (size_t i = 0; i < length_; i++)
{
if (std::abs(val_[i] - b[i]) > eps) return false;
}
}
return true;
}
template <typename ArrValType>
array<ArrValType>::~array()
{
@ -999,7 +1256,7 @@ namespace gctl
}
template <typename ArrValType>
void array<ArrValType>::assign_all(ArrValType in_val)
void array<ArrValType>::assign(ArrValType in_val)
{
for (size_t i = 0; i < length_; i++)
{
@ -1025,7 +1282,7 @@ namespace gctl
}
template <typename ArrValType>
void array<ArrValType>::append_array(const array<ArrValType> &b)
void array<ArrValType>::concat(const array<ArrValType> &b)
{
ArrValType *t = new ArrValType [length_ + b.size()];
@ -1047,7 +1304,7 @@ namespace gctl
}
template <typename ArrValType>
void array<ArrValType>::extract_array(array<ArrValType> &b, size_t st) const
void array<ArrValType>::slice(array<ArrValType> &b, size_t st) const
{
if (b.size() + st <= length_)
{
@ -1069,7 +1326,7 @@ namespace gctl
}
template <typename ArrValType>
void array<ArrValType>::extract_array(array<ArrValType> &b, const array<size_t> &ids) const
void array<ArrValType>::slice(array<ArrValType> &b, const array<size_t> &ids) const
{
b.resize(ids.size());
for (size_t i = 0; i < ids.size(); i++)
@ -1080,7 +1337,7 @@ namespace gctl
}
template <typename ArrValType>
void array<ArrValType>::insert_array(array<ArrValType> &b, size_t st)
void array<ArrValType>::insert(array<ArrValType> &b, size_t st)
{
if (b.size() + st <= length_)
{
@ -1104,10 +1361,8 @@ namespace gctl
template <typename ArrValType>
ArrValType *array<ArrValType>::get(size_t index) const
{
#ifdef GCTL_CHECK_BOUNDER
if (index >= length_)
throw std::out_of_range("[gctl::array<T>::at] Invalid index.");
#endif // GCTL_CHECK_BOUNDER
return &val_[index];
}
@ -1115,10 +1370,8 @@ namespace gctl
template <typename ArrValType>
ArrValType &array<ArrValType>::at(size_t index)
{
#ifdef GCTL_CHECK_BOUNDER
if (index >= length_)
throw std::out_of_range("[gctl::array<T>::at] Invalid index.");
#endif // GCTL_CHECK_BOUNDER
return val_[index];
}
@ -1126,22 +1379,20 @@ namespace gctl
template <typename ArrValType>
ArrValType &array<ArrValType>::at(size_t index) const
{
#ifdef GCTL_CHECK_BOUNDER
if (index >= length_)
throw std::out_of_range("[gctl::array<T>::at] Invalid index.");
#endif // GCTL_CHECK_BOUNDER
return val_[index];
}
template <typename ArrValType>
ArrValType &array<ArrValType>::operator[](size_t index)
ArrValType &array<ArrValType>::operator[](size_t index) noexcept
{
return val_[index];
}
template <typename ArrValType>
ArrValType &array<ArrValType>::operator[](size_t index) const
ArrValType &array<ArrValType>::operator[](size_t index) const noexcept
{
return val_[index];
}
@ -1171,20 +1422,20 @@ namespace gctl
}
template <typename ArrValType>
bool array<ArrValType>::empty() const
bool array<ArrValType>::empty() const noexcept
{
if (length_ == 0) return true;
else return false;
}
template <typename ArrValType>
size_t array<ArrValType>::size() const
size_t array<ArrValType>::size() const noexcept
{
return length_;
}
template <typename ArrValType>
void array<ArrValType>::export_vector(std::vector<ArrValType> &b) const
void array<ArrValType>::output(std::vector<ArrValType> &b) const
{
if (!b.empty())
{
@ -1200,7 +1451,7 @@ namespace gctl
}
template <typename ArrValType>
void array<ArrValType>::import_vector(const std::vector<ArrValType> &b)
void array<ArrValType>::input(const std::vector<ArrValType> &b)
{
resize(b.size());
for (size_t i = 0; i < length_; i++)
@ -1212,7 +1463,7 @@ namespace gctl
#ifdef GCTL_EIGEN
template <typename ArrValType>
void array<ArrValType>::export_eigen_vector(Eigen::VectorXd &b) const
void array<ArrValType>::output(Eigen::VectorXd &b) const
{
b.resize(length_);
for (size_t i = 0; i < length_; i++)
@ -1224,7 +1475,7 @@ namespace gctl
template <typename ArrValType>
void array<ArrValType>::import_eigen_vector(const Eigen::VectorXd &b)
void array<ArrValType>::input(const Eigen::VectorXd &b)
{
resize(b.size());
for (size_t i = 0; i < length_; i++)
@ -1235,6 +1486,7 @@ namespace gctl
}
#endif // GCTL_EIGEN
/*
template <typename ArrValType>
void array<ArrValType>::for_each(foreach_a_ptr func)
{
@ -1244,6 +1496,7 @@ namespace gctl
}
return;
}
*/
template <typename ArrValType>
void array<ArrValType>::show(std::ostream &os, char sep)
@ -1433,12 +1686,29 @@ namespace gctl
throw std::runtime_error("Incompatible array sizes. gctl::array<T>::dot(...)");
}
ArrValType sum = 0;
ArrValType s = 0;
for (size_t i = 0; i < length_; i++)
{
sum += val_[i]*a[i];
s += val_[i]*a[i];
}
return sum;
return s;
}
template <typename ArrValType>
ArrValType array<ArrValType>::sum() const
{
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::sum(...) could only be used with an arithmetic type.");
if (length_ == 0) return ArrValType{};
ArrValType s = val_[0];
#pragma omp parallel for reduction(+:s)
for (size_t i = 1; i < length_; i++)
{
s += val_[i];
}
return s;
}
template <typename ArrValType>
@ -1447,14 +1717,8 @@ namespace gctl
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::mean(...) could only be used with an arithmetic type.");
if (length_ == 0) return 0;
ArrValType m = 0;
for (size_t i = 0; i < length_; i++)
{
m = m + val_[i];
}
return m/length_;
if (length_ == 0) return ArrValType{};
return sum()/static_cast<ArrValType>(length_);
}
template <typename ArrValType>
@ -1463,14 +1727,14 @@ namespace gctl
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::std(...) could only be used with an arithmetic type.");
if (length_ == 0 || length_ == 1) return 0;
if (length_ == 0 || length_ == 1) return ArrValType{};
ArrValType m = mean(), s = 0;
for (size_t i = 0; i < length_; i++)
{
s += (val_[i] - m)*(val_[i] - m);
}
return sqrt(s/length_);
return sqrt(s/static_cast<ArrValType>(length_));
}
template <typename ArrValType>
@ -1479,14 +1743,14 @@ namespace gctl
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::rms(...) could only be used with an arithmetic type.");
if (length_ == 0) return 0;
if (length_ == 0) return ArrValType{};
ArrValType m = 0;
for (size_t i = 0; i < length_; i++)
{
m += val_[i]*val_[i];
}
return sqrt(m/length_);
return sqrt(m/static_cast<ArrValType>(length_));
}
template <typename ArrValType>
@ -1495,7 +1759,7 @@ namespace gctl
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::max(...) could only be used with an arithmetic type.");
if (length_ == 0) return 0;
if (length_ == 0) return ArrValType{};
ArrValType m = val_[0];
for (size_t i = 1; i < length_; i++)
@ -1511,7 +1775,7 @@ namespace gctl
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::min(...) could only be used with an arithmetic type.");
if (length_ == 0) return 0;
if (length_ == 0) return ArrValType{};
ArrValType m = val_[0];
for (size_t i = 1; i < length_; i++)
@ -1639,7 +1903,7 @@ namespace gctl
static_assert(std::is_arithmetic<ArrValType>::value,
"gctl::array<T>::variance(...) could only be used with an arithmetic type.");
if (length_ == 0) return 0;
if (length_ == 0) return ArrValType{};
ArrValType mn = mean();
ArrValType d = 0;
@ -1647,7 +1911,7 @@ namespace gctl
{
d = d + (val_[i] - mn)*(val_[i] - mn);
}
return d/length_;
return d/static_cast<ArrValType>(length_);
}
template <typename ArrValType>
@ -1710,6 +1974,53 @@ namespace gctl
}
return;
}
template <typename ArrValType>
bool array<ArrValType>::any_of(const ArrValType& value) const
{
bool result = false;
#pragma omp parallel for reduction(|:result)
for (size_t i = 0; i < length_; i++)
{
result = result || (val_[i] == value);
}
return result;
}
template <typename ArrValType>
bool array<ArrValType>::all_of(const ArrValType& value) const
{
bool result = true;
#pragma omp parallel for reduction(&:result)
for (size_t i = 0; i < length_; i++)
{
result = result && (val_[i] == value);
}
return result;
}
template <typename ArrValType>
size_t array<ArrValType>::count(const ArrValType& value) const
{
size_t result = 0;
#pragma omp parallel for reduction(+:result)
for (size_t i = 0; i < length_; i++)
{
if (val_[i] == value) ++result;
}
return result;
}
template <typename ArrValType>
void array<ArrValType>::memory_usage(std::ostream &os) const noexcept
{
size_t byte = sizeof(*this) + length_ * sizeof(ArrValType);
if (byte/1073741824 > 0) os << byte/1073741824 << " GB\n";
else if (byte/1048576 > 0) os << byte/1048576 << " MB\n";
else if (byte/1024 > 0) os << byte/1024 << " KB\n";
else os << byte << " B\n";
return;
}
}
#endif // _GCTL_ARRAY_H

View File

@ -471,25 +471,25 @@ namespace gctl
* @param[in] dlon lon间隔
* @param[in] dlat lat间隔
* @param[in] rad
* @param[in] cor
*/
template <typename T>
void get_grid_point3s(array<point3s<T>> &out_ps, T lonmin, T lonmax, T latmin,
T latmax, T dlon, T dlat, T rad)
void grid_points_2d(array<point3s<T>> &out_ps, T lonmin, T lonmax, T latmin,
T latmax, T dlon, T dlat, T rad, matrix_corner_e cor = BtmLeft)
{
if (lonmin >= lonmax || latmin >= latmax || lonmin+dlon>lonmax || latmin+dlat>latmax)
if (lonmin >= lonmax || latmin >= latmax ||
lonmin + dlon > lonmax || latmin + dlat > latmax ||
dlon <= 0 || dlat <= 0)
{
throw invalid_argument("Invalid range parameters. From get_grid_point3s(...)");
throw invalid_argument("[gctl::grid_points_2d] Invalid parameters.");
}
if (dlon <= 0 || dlat <= 0)
{
throw invalid_argument("Invalid interval parameters. From get_grid_point3s(...)");
}
int lonnum = floor((lonmax-lonmin)/dlon) + 1;
int latnum = floor((latmax-latmin)/dlat) + 1;
int lonnum = round((lonmax-lonmin)/dlon) + 1;
int latnum = round((latmax-latmin)/dlat) + 1;
out_ps.resize(lonnum*latnum);
if (cor == BtmLeft)
{
for (int j = 0; j < latnum; j++)
{
for (int i = 0; i < lonnum; i++)
@ -499,6 +499,19 @@ namespace gctl
out_ps.at(j*lonnum+i).rad = rad;
}
}
}
else // cor == TopLeft
{
for (int j = 0; j < latnum; j++)
{
for (int i = 0; i < lonnum; i++)
{
out_ps.at(j*lonnum+i).lon = lonmin + dlon*i;
out_ps.at(j*lonnum+i).lat = latmax - dlat*j;
out_ps.at(j*lonnum+i).rad = rad;
}
}
}
return;
}
}

View File

@ -951,15 +951,8 @@ void gctl::geodsv_io::fill_column_point2dc(const array<point2dc> &data, int xid,
std::string s;
for (size_t i = 1; i <= std::min(row_num_, (int) data.size()); i++)
{
ss.clear();
ss << data[i - 1].x;
ss >> s;
table_[i][xid].str_ = s;
ss.clear();
ss << data[i - 1].y;
ss >> s;
table_[i][yid].str_ = s;
table_[i][xid].value(data[i - 1].x, p);
table_[i][yid].value(data[i - 1].y, p);
}
return;
}
@ -980,22 +973,11 @@ void gctl::geodsv_io::fill_column_point3dc(const array<point3dc> &data, int xid,
std::stringstream ss;
std::string s;
for (size_t i = 1; i < std::min(row_num_, (int) data.size()); i++)
for (size_t i = 1; i <= std::min(row_num_, (int) data.size()); i++)
{
ss.clear();
ss << data[i - 1].x;
ss >> s;
table_[i][xid].str_ = s;
ss.clear();
ss << data[i - 1].y;
ss >> s;
table_[i][yid].str_ = s;
ss.clear();
ss << data[i - 1].z;
ss >> s;
table_[i][zid].str_ = s;
table_[i][xid].value(data[i - 1].x, p);
table_[i][yid].value(data[i - 1].y, p);
table_[i][zid].value(data[i - 1].z, p);
}
return;
}
@ -1016,22 +998,11 @@ void gctl::geodsv_io::fill_column_point3ds(const array<point3ds> &data, int rid,
std::stringstream ss;
std::string s;
for (size_t i = 1; i < std::min(row_num_, (int) data.size()); i++)
for (size_t i = 1; i <= std::min(row_num_, (int) data.size()); i++)
{
ss.clear();
ss << data[i - 1].rad;
ss >> s;
table_[i][rid].str_ = s;
ss.clear();
ss << data[i - 1].lon;
ss >> s;
table_[i][pid].str_ = s;
ss.clear();
ss << data[i - 1].lat;
ss >> s;
table_[i][tid].str_ = s;
table_[i][rid].value(data[i - 1].rad, p);
table_[i][pid].value(data[i - 1].lon, p);
table_[i][tid].value(data[i - 1].lat, p);
}
return;
}