Skip to content

Commit 3d111fe

Browse files
committed
fix: unit test
1 parent 367ae55 commit 3d111fe

File tree

4 files changed

+77
-29
lines changed

4 files changed

+77
-29
lines changed

Library/PAX_SAPIENTICA/GeographicInformation/ConvertToInt.hpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,16 @@
2222

2323
namespace paxs {
2424

25+
constexpr double u8_max_p_log2_slope_max = 38.4154715724661; // 250 / log2(91)
26+
27+
// 傾斜を uint8 (0-250) から double (度)へ変換
28+
double slopeDegLog2U8ToF64(const unsigned char char_value_) {
29+
return (char_value_ >= 251u) ? std::numeric_limits<double>::quiet_NaN() : // 251 以上は NaN
30+
std::pow(2, char_value_ / u8_max_p_log2_slope_max) - 1.0; // 250 以下は値を変換
31+
}
32+
2533
// 傾斜(度)を double から uint8 (0-250) へ変換
26-
unsigned char slopeF64ToLog2U8(const double float_value_) {
34+
unsigned char slopeDegF64ToLog2U8(const double float_value_) {
2735
// NaN は 251
2836
if (float_value_ == std::numeric_limits<double>::quiet_NaN()) {
2937
return 251u;
@@ -36,7 +44,16 @@ namespace paxs {
3644
else if (float_value_ <= 0.0) {
3745
return 0u;
3846
}
39-
return static_cast<unsigned char>(std::ceil(std::log2(float_value_ + 1.0) * 38.4154715724661/* 250 / log2(91) */));
47+
return static_cast<unsigned char>(std::ceil(std::log2(float_value_ + 1.0) * u8_max_p_log2_slope_max));
48+
}
49+
50+
constexpr double s16_max_p_log2_elevation_max = 2440.16038278159; // 32760 / log2(11000 + 1)
51+
52+
// 標高を int16 から double (m) へ変換
53+
double elevationLog2S16ToF64(const std::int_least16_t char_value_) {
54+
return (char_value_ >= 32761) ? std::numeric_limits<double>::quiet_NaN() : // 32761 以上は NaN
55+
((char_value_ < 0) ? -std::pow(2, -char_value_ / s16_max_p_log2_elevation_max) + 1.0 : // 32760 以下は値を変換
56+
std::pow(2, char_value_ / s16_max_p_log2_elevation_max) - 1.0); // 32760 以下は値を変換
4057
}
4158

4259
// 標高 (m) を double から int16 へ変換
@@ -54,8 +71,8 @@ namespace paxs {
5471
return -32760;
5572
}
5673
return (float_value_ < 0) ?
57-
static_cast<std::int_least16_t>(-std::ceil(std::log2(-float_value_ + 1.0) * 2440.16038278159/* 32760 / log2(11000 + 1) */)) :
58-
static_cast<std::int_least16_t>(std::ceil(std::log2(float_value_ + 1.0) * 2440.16038278159/* 32760 / log2(11000 + 1) */));
74+
static_cast<std::int_least16_t>(-std::ceil(std::log2(-float_value_ + 1.0) * s16_max_p_log2_elevation_max)) :
75+
static_cast<std::int_least16_t>(std::ceil(std::log2(float_value_ + 1.0) * s16_max_p_log2_elevation_max));
5976
}
6077

6178
}

Library/PAX_SAPIENTICA/StringExtensions.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ namespace paxs {
7979
while (std::getline(stream, field, delimiter) && counter < size) { // 1 行ごとに文字列を分割
8080
if (field.size() == 0) result[counter] = 251; // 文字列が空の時は NaN(251) を入れる
8181
else {
82-
result[counter] = paxs::slopeF64ToLog2U8(std::stod(field)); // 文字列を数値に変換する
82+
result[counter] = paxs::slopeDegF64ToLog2U8(std::stod(field)); // 文字列を数値に変換する
8383

8484
/* バイナリデータをより小さくするための実験 */
8585
//if (result[counter] > 181) result[counter] = 181;

Project/MicrosoftVisualStudio/XYZTilesTSVtoBinary/main.cpp

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414

1515
#include <iostream>
1616

17+
int main() {
18+
std::cout << paxs::elevationF64ToLog2S16(paxs::elevationLog2S16ToF64(-20000)) << '\n';
19+
//std::cout << (paxs::slopeDegLog2U8ToF64(7)) << '\n';
20+
//std::cout << (int)paxs::slopeDegF64ToLog2U8(55);
21+
//std::cout << paxs::elevationF64ToLog2S16(3.138);
22+
}
23+
1724
//// 標高版 : TSV から バイナリデータを出力
1825
//int main() {
1926
// //std::cout << paxs::elevationS16(-10996.0) << '\n';
@@ -53,26 +60,26 @@
5360
// return 0;
5461
//}
5562

56-
// 標高のバイナリデータをコンソール上で表示
57-
int main() {
58-
// 標高のバイナリデータを読み込む
59-
const std::string input_name = "test8/1/zxy_1_0_0.bin";
60-
paxs::Input16BitBinary i16bbe(input_name, "./");
61-
62-
std::int_least16_t xyz_tiles[256 * 256]{};
63-
64-
for (std::size_t i = 0; i < 256 * 256; ++i) {
65-
xyz_tiles[i] = 22; // エラー値を入れ、エラー値が出ないか確認する
66-
}
67-
68-
i16bbe.calc(xyz_tiles);
69-
70-
71-
for (std::size_t i = 0; i < 256 * 256; ++i) {
72-
std::cout << xyz_tiles[i] << ",";
73-
if ((i & 255) == 255) std::cout << "\n";
74-
}
75-
}
63+
//// 標高のバイナリデータをコンソール上で表示
64+
//int main() {
65+
// // 標高のバイナリデータを読み込む
66+
// const std::string input_name = "test8/1/zxy_1_0_0.bin";
67+
// paxs::Input16BitBinary i16bbe(input_name, "./");
68+
//
69+
// std::int_least16_t xyz_tiles[256 * 256]{};
70+
//
71+
// for (std::size_t i = 0; i < 256 * 256; ++i) {
72+
// xyz_tiles[i] = 22; // エラー値を入れ、エラー値が出ないか確認する
73+
// }
74+
//
75+
// i16bbe.calc(xyz_tiles);
76+
//
77+
//
78+
// for (std::size_t i = 0; i < 256 * 256; ++i) {
79+
// std::cout << xyz_tiles[i] << ",";
80+
// if ((i & 255) == 255) std::cout << "\n";
81+
// }
82+
//}
7683

7784
// 傾斜のバイナリデータをコンソール上で表示
7885
//int main() {

Project/UnitTest/source/ConvertToInt.cpp

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,38 @@
1313

1414
#include <PAX_SAPIENTICA/GeographicInformation/ConvertToInt.hpp>
1515

16-
TEST(ConvertToIntUnitTest, slope) {
16+
TEST(ConvertToIntUnitTest, slope1) {
17+
unsigned char expected = 90u;
18+
unsigned char actual = paxs::slopeDegF64ToLog2U8(4.0);
19+
ASSERT_EQ(expected, actual);
20+
}
21+
22+
TEST(ConvertToIntUnitTest, slope2) {
1723
unsigned char expected = 100u;
18-
unsigned char actual = paxs::slopeF64ToLog2U8(5.07598309582116);
24+
unsigned char actual = paxs::slopeDegF64ToLog2U8(5.0);
1925
ASSERT_EQ(expected, actual);
2026
}
2127

22-
TEST(ConvertToIntUnitTest, elevation) {
28+
TEST(ConvertToIntUnitTest, elevation1) {
2329
std::int_least16_t expected = -10000;
24-
std::int_least16_t actual = paxs::elevationF64ToLog2S16(-16.1257027576632);
30+
std::int_least16_t actual = paxs::elevationF64ToLog2S16(-16.125);
31+
ASSERT_EQ(expected, actual);
32+
}
33+
34+
TEST(ConvertToIntUnitTest, elevation2) {
35+
std::int_least16_t expected = 5000;
36+
std::int_least16_t actual = paxs::elevationF64ToLog2S16(3.138);
37+
ASSERT_EQ(expected, actual);
38+
}
39+
40+
TEST(ConvertToIntUnitTest, slope3) {
41+
unsigned char expected = 100u;
42+
unsigned char actual = paxs::slopeDegF64ToLog2U8(paxs::slopeDegLog2U8ToF64(100));
43+
ASSERT_EQ(expected, actual);
44+
}
45+
46+
TEST(ConvertToIntUnitTest, elevation3) {
47+
std::int_least16_t expected = -20000;
48+
std::int_least16_t actual = paxs::elevationF64ToLog2S16(paxs::elevationLog2S16ToF64(-20000));
2549
ASSERT_EQ(expected, actual);
2650
}

0 commit comments

Comments
 (0)