Compare commits
202 Commits
version/20
...
version/20
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
679d4e1dcd | ||
|
|
aaa6231f89 | ||
|
|
446c8cd70c | ||
|
|
11ff1d256c | ||
|
|
4acd047982 | ||
|
|
ee4b6621e7 | ||
|
|
77517b15fd | ||
|
|
19a86a6f0d | ||
|
|
a7dee1164d | ||
|
|
7512bc0fb2 | ||
|
|
d6b57d0937 | ||
|
|
1e99c4b2ec | ||
|
|
04ca3ad8f4 | ||
|
|
2b8915e35f | ||
|
|
2a0801da6b | ||
|
|
2935d78490 | ||
|
|
2404924bcb | ||
|
|
e4c4db5cf9 | ||
|
|
e1c655c570 | ||
|
|
2788da9c51 | ||
|
|
4860a70443 | ||
|
|
22d1433ab0 | ||
|
|
ef9eedf35a | ||
|
|
a962c90b30 | ||
|
|
36275f5cce | ||
|
|
60d1c87cef | ||
|
|
9223f30f08 | ||
|
|
fe87e7f60d | ||
|
|
f3e83cf020 | ||
|
|
9eee41d74a | ||
|
|
991d76b69e | ||
|
|
6f0c7da6ad | ||
|
|
c170f576b6 | ||
|
|
1446f559cc | ||
|
|
4467e68db1 | ||
|
|
cc1118b330 | ||
|
|
a1126bd42c | ||
|
|
370523d5bf | ||
|
|
96b4d2c03d | ||
|
|
c4898502bf | ||
|
|
f7a511d1b3 | ||
|
|
b66c51a6f8 | ||
|
|
a4cf38925b | ||
|
|
41059a24a7 | ||
|
|
ee4f5a5190 | ||
|
|
141e98564c | ||
|
|
72341a6de4 | ||
|
|
55ee59ac99 | ||
|
|
61525c555e | ||
|
|
dad30b0cc2 | ||
|
|
9c9e4e86e7 | ||
|
|
b88aa46e1c | ||
|
|
10fa8a471a | ||
|
|
4e875be0dd | ||
|
|
23cc940743 | ||
|
|
4b010cc416 | ||
|
|
46f39d5fbd | ||
|
|
03515151f0 | ||
|
|
8cd723d91b | ||
|
|
6f2943ed9a | ||
|
|
e509fc3f5d | ||
|
|
707d9e12cf | ||
|
|
9840302931 | ||
|
|
983047982f | ||
|
|
f977be5fe4 | ||
|
|
e21ad4b5c1 | ||
|
|
619055f544 | ||
|
|
c9611fc45b | ||
|
|
79f869a7f3 | ||
|
|
1b8dfb2bef | ||
|
|
143a47482b | ||
|
|
a28cf0f860 | ||
|
|
d9f4d7373f | ||
|
|
c3f48c7261 | ||
|
|
48b7b70e23 | ||
|
|
b93b362e2f | ||
|
|
2082b18e2e | ||
|
|
7f65e7f905 | ||
|
|
3417ca7e49 | ||
|
|
6334c30eb6 | ||
|
|
c87dff7e8f | ||
|
|
332f76f34d | ||
|
|
3387f3d084 | ||
|
|
bd421c381c | ||
|
|
0cce949837 | ||
|
|
ed3ba67925 | ||
|
|
866f85064a | ||
|
|
9215c530b3 | ||
|
|
a6437f4e96 | ||
|
|
5bd7be6ed1 | ||
|
|
d9cc3738b9 | ||
|
|
edec5bbc01 | ||
|
|
6a2d86c526 | ||
|
|
6c64e9b36c | ||
|
|
bcecee0f76 | ||
|
|
c5cdfa1a1d | ||
|
|
f9f2b4cbdb | ||
|
|
33feb9a416 | ||
|
|
9e1aaa8b56 | ||
|
|
ef2eb635af | ||
|
|
2db412a923 | ||
|
|
061fea48c8 | ||
|
|
c4ea62a899 | ||
|
|
789c09a402 | ||
|
|
de9b329115 | ||
|
|
1b793a127c | ||
|
|
3cb3084725 | ||
|
|
637f67888a | ||
|
|
fbc0986fd8 | ||
|
|
14ebe0b618 | ||
|
|
fd34cc30b8 | ||
|
|
7b0faed03a | ||
|
|
8d1dc30b07 | ||
|
|
03cff6abca | ||
|
|
d7821324b8 | ||
|
|
f3e066cce0 | ||
|
|
45ac758cc9 | ||
|
|
f0e6402fff | ||
|
|
41bf142e31 | ||
|
|
2e9efa98d7 | ||
|
|
2c2a57f368 | ||
|
|
4ea1326126 | ||
|
|
09b44ac68a | ||
|
|
a48ab434ab | ||
|
|
d39a56d4fb | ||
|
|
892579456d | ||
|
|
4dde1d365c | ||
|
|
2f21b582cd | ||
|
|
b29536f8b7 | ||
|
|
039f9920db | ||
|
|
7c254e9c04 | ||
|
|
35a115bfd4 | ||
|
|
70dd9d35b1 | ||
|
|
31e3cf06fb | ||
|
|
175eddd1fa | ||
|
|
fe73247b82 | ||
|
|
203db3d095 | ||
|
|
8c4695b991 | ||
|
|
a2b111bb09 | ||
|
|
2a1542d544 | ||
|
|
e5b51677c5 | ||
|
|
f9450d136d | ||
|
|
0586cb62c3 | ||
|
|
8fee04b32b | ||
|
|
04e16c95e2 | ||
|
|
e257dbe6ed | ||
|
|
abf78f8e31 | ||
|
|
da13bd9f04 | ||
|
|
86fb1ed00f | ||
|
|
56fb81dc03 | ||
|
|
343ce57468 | ||
|
|
3e52e37181 | ||
|
|
e768553a4a | ||
|
|
b74d1a8351 | ||
|
|
801d8c4af5 | ||
|
|
3a4693803b | ||
|
|
79f0d3356e | ||
|
|
6a1bf02ddb | ||
|
|
6662800deb | ||
|
|
c9bb6102c0 | ||
|
|
90479419cc | ||
|
|
b4178ae888 | ||
|
|
ab4814c916 | ||
|
|
6b16f96c8a | ||
|
|
e655d41817 | ||
|
|
b5c1902a2d | ||
|
|
d088259739 | ||
|
|
d8acf44a3a | ||
|
|
4664af12fa | ||
|
|
7a909d0c0b | ||
|
|
919c25769c | ||
|
|
5a0908d5bb | ||
|
|
1c39daec07 | ||
|
|
835ae941ce | ||
|
|
63edff078f | ||
|
|
0ea9786601 | ||
|
|
9e0bb33d58 | ||
|
|
64531c85e3 | ||
|
|
14cdae5102 | ||
|
|
2aaad212e8 | ||
|
|
bd88bf1126 | ||
|
|
ea9da65b7c | ||
|
|
e7f80cf5f3 | ||
|
|
16d62f93c8 | ||
|
|
e96834fcc6 | ||
|
|
22a74c63b4 | ||
|
|
5681fcbdc5 | ||
|
|
7755f8e094 | ||
|
|
e266e44f63 | ||
|
|
4dc66a385e | ||
|
|
aec29a3a37 | ||
|
|
0d213a1990 | ||
|
|
8162a49f6c | ||
|
|
d2e2603400 | ||
|
|
43a8277bdb | ||
|
|
61a8bd5cd3 | ||
|
|
ad6f3d2db2 | ||
|
|
9088f41352 | ||
|
|
73f57bbbd8 | ||
|
|
a8673356a2 | ||
|
|
5ecc1ab6f2 | ||
|
|
0702f85540 |
@@ -9,6 +9,17 @@ if(COMMAND cmake_policy)
|
||||
endif()
|
||||
endif()
|
||||
|
||||
|
||||
message(STATUS "CMAKE Build type: ${CMAKE_BUILD_TYPE}")
|
||||
# Set a default build type if none was specified
|
||||
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
|
||||
message(STATUS "Setting build type to 'Debug' as none was specified.")
|
||||
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Choose the type of build." FORCE)
|
||||
# Set the possible values of build type for cmake-gui
|
||||
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
|
||||
"MinSizeRel" "RelWithDebInfo")
|
||||
endif()
|
||||
|
||||
include (CheckFunctionExists)
|
||||
include (CheckIncludeFile)
|
||||
include (CheckLibraryExists)
|
||||
@@ -109,11 +120,16 @@ endif()
|
||||
|
||||
option(SIMGEAR_HEADLESS "Set to ON to build SimGear without GUI/graphics support" OFF)
|
||||
option(ENABLE_RTI "Set to ON to build SimGear with RTI support" OFF)
|
||||
option(ENABLE_GDAL "Set to ON to build SimGear with GDAL support" OFF)
|
||||
option(ENABLE_TESTS "Set to OFF to disable building SimGear's test applications" ON)
|
||||
option(ENABLE_SOUND "Set to OFF to disable building SimGear's sound support" ON)
|
||||
option(USE_AEONWAVE "Set to ON to use AeonWave instead of OpenAL" OFF)
|
||||
option(ENABLE_PKGUTIL "Set to ON to build the sg_pkgutil application (default)" ON)
|
||||
option(ENABLE_DNS "Set to ON to use udns library and DNS service resolver" ON)
|
||||
option(ENABLE_SIMD "Enable SSE/SSE2 support for x86 compilers" ON)
|
||||
option(ENABLE_OPENMP "Enable OpenMP compiler support" OFF)
|
||||
|
||||
include (DetectArch)
|
||||
|
||||
# until the fstream fix is applied and generally available in OSG,
|
||||
# keep the compatability link option as the default
|
||||
@@ -239,7 +255,6 @@ endif(SYSTEM_EXPAT)
|
||||
|
||||
check_include_file(inttypes.h HAVE_INTTYPES_H)
|
||||
check_include_file(sys/time.h HAVE_SYS_TIME_H)
|
||||
check_include_file(sys/timeb.h HAVE_SYS_TIMEB_H)
|
||||
check_include_file(unistd.h HAVE_UNISTD_H)
|
||||
check_include_file(windows.h HAVE_WINDOWS_H)
|
||||
|
||||
@@ -256,14 +271,26 @@ else()
|
||||
message(STATUS "RTI: DISABLED")
|
||||
endif(ENABLE_RTI)
|
||||
|
||||
if(ENABLE_GDAL)
|
||||
find_package(GDAL 2.0.0 REQUIRED)
|
||||
if (GDAL_FOUND)
|
||||
include_directories(${GDAL_INCLUDE_DIR})
|
||||
endif(GDAL_FOUND)
|
||||
endif(ENABLE_GDAL)
|
||||
|
||||
check_function_exists(gettimeofday HAVE_GETTIMEOFDAY)
|
||||
check_function_exists(ftime HAVE_FTIME)
|
||||
check_function_exists(timegm HAVE_TIMEGM)
|
||||
check_function_exists(rint HAVE_RINT)
|
||||
check_function_exists(mkdtemp HAVE_MKDTEMP)
|
||||
check_function_exists(bcopy HAVE_BCOPY)
|
||||
check_function_exists(mmap HAVE_MMAP)
|
||||
|
||||
if (NOT MSVC)
|
||||
check_function_exists(timegm HAVE_TIMEGM)
|
||||
if (NOT HAVE_TIMEGM)
|
||||
message(FATAL_ERROR "Non-Windows platforms must support timegm()")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if(HAVE_UNISTD_H)
|
||||
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_INCLUDE_PATH})
|
||||
check_cxx_source_compiles(
|
||||
@@ -317,8 +344,8 @@ if (NOT ${HAVE_STD_ISNAN})
|
||||
endif()
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX)
|
||||
set(WARNING_FLAGS_CXX "-Wall")
|
||||
set(WARNING_FLAGS_C "-Wall")
|
||||
set(WARNING_FLAGS_CXX "-Wall -fPIC")
|
||||
set(WARNING_FLAGS_C "-Wall -fPIC")
|
||||
|
||||
if (CMAKE_VERSION VERSION_LESS 3.1)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
@@ -328,6 +355,13 @@ if(CMAKE_COMPILER_IS_GNUCXX)
|
||||
message(WARNING "GCC 4.4 will be required soon, please upgrade")
|
||||
endif()
|
||||
|
||||
if(ENABLE_SIMD)
|
||||
if (X86 OR X86_64)
|
||||
set(CMAKE_C_FLAGS_RELEASE "-O3 -msse2 -mfpmath=sse")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -msse2 -mfpmath=sse")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# certain GCC versions don't provide the atomic builds, and hence
|
||||
# require is to provide them in SGAtomic.cxx
|
||||
set(CMAKE_REQUIRED_INCLUDES ${CMAKE_INCLUDE_PATH})
|
||||
@@ -336,10 +370,10 @@ if(CMAKE_COMPILER_IS_GNUCXX)
|
||||
GCC_ATOMIC_BUILTINS_FOUND)
|
||||
endif(CMAKE_COMPILER_IS_GNUCXX)
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang")
|
||||
if (CLANG)
|
||||
# Boost redeclares class members
|
||||
set(WARNING_FLAGS_CXX "-Wall -Wno-overloaded-virtual -Wno-redeclared-class-member")
|
||||
set(WARNING_FLAGS_C "-Wall")
|
||||
set(WARNING_FLAGS_CXX "-Wall -fPIC -Wno-overloaded-virtual -Wno-redeclared-class-member")
|
||||
set(WARNING_FLAGS_C "-Wall -fPIC")
|
||||
set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
|
||||
# fix Boost compilation :(
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
|
||||
@@ -347,6 +381,26 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Ap
|
||||
if (CMAKE_VERSION VERSION_LESS 3.1)
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
|
||||
endif()
|
||||
|
||||
if(ENABLE_SIMD)
|
||||
if (X86 OR X86_64)
|
||||
set(CMAKE_C_FLAGS_RELEASE "-O3 -msse2 -mfpmath=sse")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -msse2 -mfpmath=sse")
|
||||
endif()
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if (ENABLE_OPENMP)
|
||||
find_package(OpenMP)
|
||||
if(OPENMP_FOUND)
|
||||
message(STATUS "OpenMP: ENABLED")
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
|
||||
else()
|
||||
message(STATUS "OpenMP: NOT FOUND")
|
||||
endif()
|
||||
else()
|
||||
message(STATUS "OpenMP: DISABLED")
|
||||
endif()
|
||||
|
||||
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
|
||||
@@ -365,6 +419,15 @@ if(WIN32)
|
||||
|
||||
if(MSVC)
|
||||
set(MSVC_FLAGS "-DWIN32 -DNOMINMAX -D_USE_MATH_DEFINES -D_CRT_SECURE_NO_WARNINGS -D__CRT_NONSTDC_NO_WARNINGS /MP")
|
||||
if(ENABLE_SIMD)
|
||||
if (X86)
|
||||
SET(CMAKE_C_FLAGS_RELEASE "/O2 /arch:SSE /arch:SSE2")
|
||||
SET(CMAKE_CXX_FLAGS_RELEASE "/O2 /arch:SSE /arch:SSE2")
|
||||
else()
|
||||
SET(CMAKE_C_FLAGS_RELEASE "/O2")
|
||||
SET(CMAKE_CXX_FLAGS_RELEASE "/O2")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
if (NOT OSG_FSTREAM_EXPORT_FIXED)
|
||||
message(STATUS "For better linking performance, use OSG with patched fstream header")
|
||||
@@ -423,7 +486,8 @@ set(TEST_LIBS_INTERNAL_CORE
|
||||
${RT_LIBRARY}
|
||||
${DL_LIBRARY}
|
||||
${COCOA_LIBRARY}
|
||||
${CURL_LIBRARIES})
|
||||
${CURL_LIBRARIES}
|
||||
${GDAL_LIBRARY})
|
||||
set(TEST_LIBS SimGearCore ${TEST_LIBS_INTERNAL_CORE})
|
||||
|
||||
if(NOT SIMGEAR_HEADLESS)
|
||||
|
||||
@@ -157,7 +157,7 @@ function(add_boost_test _name)
|
||||
endforeach()
|
||||
|
||||
if(NOT _boostTestTargetsNagged${_name} STREQUAL "${includeType}")
|
||||
if("includeType" STREQUAL "CONFIGURED")
|
||||
if("${includeType}" STREQUAL "CONFIGURED")
|
||||
message(STATUS
|
||||
"Test '${_name}' uses the CMake-configurable form of the boost test framework - congrats! (Including File: ${includeFileLoc})")
|
||||
elseif("${includeType}" STREQUAL "INCLUDED")
|
||||
|
||||
37
CMakeModules/DetectArch.cmake
Normal file
37
CMakeModules/DetectArch.cmake
Normal file
@@ -0,0 +1,37 @@
|
||||
IF(CMAKE_SYSTEM_PROCESSOR MATCHES amd64.*|x86_64.* OR CMAKE_GENERATOR MATCHES "Visual Studio.*Win64")
|
||||
IF(CMAKE_C_FLAGS MATCHES -m32 OR CMAKE_CXX_FLAGS MATCHES -m32)
|
||||
SET(X86 1)
|
||||
ELSE(CMAKE_C_FLAGS MATCHES -m32 OR CMAKE_CXX_FLAGS MATCHES -m32)
|
||||
SET(X86_64 1)
|
||||
ENDIF(CMAKE_C_FLAGS MATCHES -m32 OR CMAKE_CXX_FLAGS MATCHES -m32)
|
||||
ELSEIF(CMAKE_SYSTEM_PROCESSOR MATCHES i686.*|i386.*|x86.* OR WIN32)
|
||||
IF(CMAKE_C_FLAGS MATCHES -m64 OR CMAKE_CXX_FLAGS MATCHES -m64)
|
||||
SET(X86_64 1)
|
||||
ELSE(CMAKE_C_FLAGS MATCHES -m64 OR CMAKE_CXX_FLAGS MATCHES -m64)
|
||||
SET(X86 1)
|
||||
ENDIF(CMAKE_C_FLAGS MATCHES -m64 OR CMAKE_CXX_FLAGS MATCHES -m64)
|
||||
ELSEIF(CMAKE_SYSTEM_PROCESSOR MATCHES arm.* AND CMAKE_SYSTEM_NAME STREQUAL "Linux")
|
||||
SET(ARM 1)
|
||||
ELSEIF(CMAKE_SYSTEM_PROCESSOR MATCHES mips)
|
||||
SET(MIPS 1)
|
||||
ENDIF()
|
||||
|
||||
IF ("${CMAKE_C_COMPILER_ID}" STREQUAL "Clang" OR "${CMAKE_C_COMPILER_ID}" STREQUAL "AppleClang")
|
||||
# using Clang
|
||||
SET(CLANG 1)
|
||||
ELSEIF ("${CMAKE_C_COMPILER_ID}" STREQUAL "TinyCC")
|
||||
# using TinyCC
|
||||
SET(TINYCC 1)
|
||||
ELSEIF ("${CMAKE_C_COMPILER_ID}" STREQUAL "GNU")
|
||||
# using GCC
|
||||
SET(GCC 1)
|
||||
ELSEIF ("${CMAKE_C_COMPILER_ID}" STREQUAL "Intel")
|
||||
# using Intel C++
|
||||
SET(INTELCC 1)
|
||||
ELSEIF ("${CMAKE_C_COMPILER_ID}" STREQUAL "MSVC")
|
||||
# using Visual Studio C++
|
||||
SET(MSVC 1)
|
||||
ELSEIF ("${CMAKE_C_COMPILER_ID}" STREQUAL "MIPSpro")
|
||||
# using SGI MIPSpro
|
||||
SET(MIPSPRO 1)
|
||||
ENDIF()
|
||||
@@ -13,4 +13,13 @@ set(SIMGEAR_SOUND @ENABLE_SOUND@)
|
||||
# find_dependency(OpenAL)
|
||||
#endif()
|
||||
|
||||
# SSE/SSE2 support
|
||||
|
||||
set(ENABLE_SIMD @ENABLE_SIMD@)
|
||||
|
||||
# Alternative terrain engine based on pagedLOD
|
||||
|
||||
set(ENABLE_GDAL @ENABLE_GDAL@)
|
||||
set(ENABLE_OPENMP @ENABLE_OPENMP@)
|
||||
|
||||
include("${CMAKE_CURRENT_LIST_DIR}/SimGearTargets.cmake")
|
||||
|
||||
@@ -35,123 +35,124 @@ using std::endl;
|
||||
|
||||
void testBucketSpans()
|
||||
{
|
||||
COMPARE(sg_bucket_span(0.0), 0.125);
|
||||
COMPARE(sg_bucket_span(-20), 0.125);
|
||||
COMPARE(sg_bucket_span(-40), 0.25);
|
||||
COMPARE(sg_bucket_span(89.9), 12.0);
|
||||
COMPARE(sg_bucket_span(88.1), 4.0);
|
||||
COMPARE(sg_bucket_span(-89.9), 12.0);
|
||||
SG_CHECK_EQUAL(sg_bucket_span(0.0), 0.125);
|
||||
SG_CHECK_EQUAL(sg_bucket_span(-20), 0.125);
|
||||
SG_CHECK_EQUAL(sg_bucket_span(-40), 0.25);
|
||||
SG_CHECK_EQUAL(sg_bucket_span(89.9), 12.0);
|
||||
SG_CHECK_EQUAL(sg_bucket_span(88.1), 4.0);
|
||||
SG_CHECK_EQUAL(sg_bucket_span(-89.9), 12.0);
|
||||
}
|
||||
|
||||
void testBasic()
|
||||
{
|
||||
SGBucket b1(5.1, 55.05);
|
||||
COMPARE(b1.get_chunk_lon(), 5);
|
||||
COMPARE(b1.get_chunk_lat(), 55);
|
||||
COMPARE(b1.get_x(), 0);
|
||||
COMPARE(b1.get_y(), 0);
|
||||
COMPARE(b1.gen_index(), 3040320);
|
||||
COMPARE(b1.gen_base_path(), "e000n50/e005n55");
|
||||
VERIFY(b1.isValid());
|
||||
SG_CHECK_EQUAL(b1.get_chunk_lon(), 5);
|
||||
SG_CHECK_EQUAL(b1.get_chunk_lat(), 55);
|
||||
SG_CHECK_EQUAL(b1.get_x(), 0);
|
||||
SG_CHECK_EQUAL(b1.get_y(), 0);
|
||||
SG_CHECK_EQUAL(b1.gen_index(), 3040320);
|
||||
SG_CHECK_EQUAL(b1.gen_base_path(), "e000n50/e005n55");
|
||||
SG_VERIFY(b1.isValid());
|
||||
|
||||
SGBucket b2(-10.1, -43.8);
|
||||
COMPARE(b2.get_chunk_lon(), -11);
|
||||
COMPARE(b2.get_chunk_lat(), -44);
|
||||
COMPARE(b2.get_x(), 3);
|
||||
COMPARE(b2.get_y(), 1); // latitude chunks numbered bottom to top, it seems
|
||||
COMPARE(b2.gen_base_path(), "w020s50/w011s44");
|
||||
VERIFY(b2.isValid());
|
||||
SG_CHECK_EQUAL(b2.get_chunk_lon(), -11);
|
||||
SG_CHECK_EQUAL(b2.get_chunk_lat(), -44);
|
||||
SG_CHECK_EQUAL(b2.get_x(), 3);
|
||||
// Latitude chunks numbered bottom to top, it seems
|
||||
SG_CHECK_EQUAL(b2.get_y(), 1);
|
||||
SG_CHECK_EQUAL(b2.gen_base_path(), "w020s50/w011s44");
|
||||
SG_VERIFY(b2.isValid());
|
||||
|
||||
SGBucket b3(123.48, 9.01);
|
||||
COMPARE(b3.get_chunk_lon(), 123);
|
||||
COMPARE(b3.get_chunk_lat(), 9);
|
||||
COMPARE(b3.get_x(), 3);
|
||||
COMPARE(b3.get_y(), 0);
|
||||
COMPARE(b3.gen_base_path(), "e120n00/e123n09");
|
||||
VERIFY(b3.isValid());
|
||||
SG_CHECK_EQUAL(b3.get_chunk_lon(), 123);
|
||||
SG_CHECK_EQUAL(b3.get_chunk_lat(), 9);
|
||||
SG_CHECK_EQUAL(b3.get_x(), 3);
|
||||
SG_CHECK_EQUAL(b3.get_y(), 0);
|
||||
SG_CHECK_EQUAL(b3.gen_base_path(), "e120n00/e123n09");
|
||||
SG_VERIFY(b3.isValid());
|
||||
|
||||
SGBucket defBuck;
|
||||
VERIFY(!defBuck.isValid());
|
||||
SG_VERIFY(!defBuck.isValid());
|
||||
|
||||
b3.make_bad();
|
||||
VERIFY(!b3.isValid());
|
||||
SG_VERIFY(!b3.isValid());
|
||||
|
||||
SGBucket atAntiMeridian(180.0, 12.3);
|
||||
VERIFY(atAntiMeridian.isValid());
|
||||
COMPARE(atAntiMeridian.get_chunk_lon(), -180);
|
||||
COMPARE(atAntiMeridian.get_x(), 0);
|
||||
|
||||
SG_VERIFY(atAntiMeridian.isValid());
|
||||
SG_CHECK_EQUAL(atAntiMeridian.get_chunk_lon(), -180);
|
||||
SG_CHECK_EQUAL(atAntiMeridian.get_x(), 0);
|
||||
|
||||
SGBucket atAntiMeridian2(-180.0, -78.1);
|
||||
VERIFY(atAntiMeridian2.isValid());
|
||||
COMPARE(atAntiMeridian2.get_chunk_lon(), -180);
|
||||
COMPARE(atAntiMeridian2.get_x(), 0);
|
||||
|
||||
SG_VERIFY(atAntiMeridian2.isValid());
|
||||
SG_CHECK_EQUAL(atAntiMeridian2.get_chunk_lon(), -180);
|
||||
SG_CHECK_EQUAL(atAntiMeridian2.get_x(), 0);
|
||||
|
||||
// check comparisom operator overload
|
||||
SGBucket b4(5.11, 55.1);
|
||||
VERIFY(b1 == b4); // should be equal
|
||||
VERIFY(b1 == b1);
|
||||
VERIFY(b1 != defBuck);
|
||||
VERIFY(b1 != b2);
|
||||
SG_VERIFY(b1 == b4); // should be equal
|
||||
SG_VERIFY(b1 == b1);
|
||||
SG_VERIFY(b1 != defBuck);
|
||||
SG_VERIFY(b1 != b2);
|
||||
|
||||
// check wrapping/clipping of inputs
|
||||
SGBucket wrapMeridian(-200.0, 45.0);
|
||||
COMPARE(wrapMeridian.get_chunk_lon(), 160);
|
||||
|
||||
SG_CHECK_EQUAL(wrapMeridian.get_chunk_lon(), 160);
|
||||
|
||||
SGBucket clipPole(48.9, 91);
|
||||
COMPARE(clipPole.get_chunk_lat(), 89);
|
||||
SG_CHECK_EQUAL(clipPole.get_chunk_lat(), 89);
|
||||
}
|
||||
|
||||
void testPolar()
|
||||
{
|
||||
SGBucket b1(0.0, 89.92);
|
||||
SGBucket b2(10.0, 89.96);
|
||||
COMPARE(b1.get_chunk_lat(), 89);
|
||||
COMPARE(b1.get_chunk_lon(), 0);
|
||||
COMPARE(b1.get_x(), 0);
|
||||
COMPARE(b1.get_y(), 7);
|
||||
SG_CHECK_EQUAL(b1.get_chunk_lat(), 89);
|
||||
SG_CHECK_EQUAL(b1.get_chunk_lon(), 0);
|
||||
SG_CHECK_EQUAL(b1.get_x(), 0);
|
||||
SG_CHECK_EQUAL(b1.get_y(), 7);
|
||||
|
||||
COMPARE_EP(b1.get_highest_lat(), 90.0);
|
||||
COMPARE_EP(b1.get_width_m(), 10.0);
|
||||
SG_CHECK_EQUAL_EP(b1.get_highest_lat(), 90.0);
|
||||
SG_CHECK_EQUAL_EP(b1.get_width_m(), 10.0);
|
||||
|
||||
COMPARE(b2.get_chunk_lat(), 89);
|
||||
COMPARE(b2.get_chunk_lon(), 0);
|
||||
COMPARE(b2.get_x(), 0);
|
||||
COMPARE(b2.get_y(), 7);
|
||||
SG_CHECK_EQUAL(b2.get_chunk_lat(), 89);
|
||||
SG_CHECK_EQUAL(b2.get_chunk_lon(), 0);
|
||||
SG_CHECK_EQUAL(b2.get_x(), 0);
|
||||
SG_CHECK_EQUAL(b2.get_y(), 7);
|
||||
|
||||
COMPARE(b1.gen_index(), b2.gen_index());
|
||||
SG_CHECK_EQUAL(b1.gen_index(), b2.gen_index());
|
||||
|
||||
SGGeod actualNorthPole1 = b1.get_corner(2);
|
||||
SGGeod actualNorthPole2 = b1.get_corner(3);
|
||||
COMPARE_EP(actualNorthPole1.getLatitudeDeg(), 90.0);
|
||||
COMPARE_EP(actualNorthPole1.getLongitudeDeg(), 12.0);
|
||||
COMPARE_EP(actualNorthPole2.getLatitudeDeg(), 90.0);
|
||||
COMPARE_EP(actualNorthPole2.getLongitudeDeg(), 0.0);
|
||||
SG_CHECK_EQUAL_EP(actualNorthPole1.getLatitudeDeg(), 90.0);
|
||||
SG_CHECK_EQUAL_EP(actualNorthPole1.getLongitudeDeg(), 12.0);
|
||||
SG_CHECK_EQUAL_EP(actualNorthPole2.getLatitudeDeg(), 90.0);
|
||||
SG_CHECK_EQUAL_EP(actualNorthPole2.getLongitudeDeg(), 0.0);
|
||||
|
||||
SGBucket b3(-2, 89.88);
|
||||
SGBucket b4(-7, 89.88);
|
||||
COMPARE(b3.gen_index(), b4.gen_index());
|
||||
SG_CHECK_EQUAL(b3.gen_index(), b4.gen_index());
|
||||
|
||||
// south pole
|
||||
SGBucket b5(-170, -89.88);
|
||||
SGBucket b6(-179, -89.88);
|
||||
|
||||
COMPARE(b5.get_chunk_lat(), -90);
|
||||
COMPARE(b5.get_chunk_lon(), -180);
|
||||
COMPARE(b5.get_x(), 0);
|
||||
COMPARE(b5.get_y(), 0);
|
||||
COMPARE(b5.gen_index(), b6.gen_index());
|
||||
COMPARE_EP(b5.get_highest_lat(), -90.0);
|
||||
COMPARE_EP(b5.get_width_m(), 10.0);
|
||||
SG_CHECK_EQUAL(b5.get_chunk_lat(), -90);
|
||||
SG_CHECK_EQUAL(b5.get_chunk_lon(), -180);
|
||||
SG_CHECK_EQUAL(b5.get_x(), 0);
|
||||
SG_CHECK_EQUAL(b5.get_y(), 0);
|
||||
SG_CHECK_EQUAL(b5.gen_index(), b6.gen_index());
|
||||
SG_CHECK_EQUAL_EP(b5.get_highest_lat(), -90.0);
|
||||
SG_CHECK_EQUAL_EP(b5.get_width_m(), 10.0);
|
||||
|
||||
SGGeod actualSouthPole1 = b5.get_corner(0);
|
||||
SGGeod actualSouthPole2 = b5.get_corner(1);
|
||||
COMPARE_EP(actualSouthPole1.getLatitudeDeg(), -90.0);
|
||||
COMPARE_EP(actualSouthPole1.getLongitudeDeg(), -180);
|
||||
COMPARE_EP(actualSouthPole2.getLatitudeDeg(), -90.0);
|
||||
COMPARE_EP(actualSouthPole2.getLongitudeDeg(), -168);
|
||||
SG_CHECK_EQUAL_EP(actualSouthPole1.getLatitudeDeg(), -90.0);
|
||||
SG_CHECK_EQUAL_EP(actualSouthPole1.getLongitudeDeg(), -180);
|
||||
SG_CHECK_EQUAL_EP(actualSouthPole2.getLatitudeDeg(), -90.0);
|
||||
SG_CHECK_EQUAL_EP(actualSouthPole2.getLongitudeDeg(), -168);
|
||||
|
||||
SGBucket b7(200, 89.88);
|
||||
COMPARE(b7.get_chunk_lon(), -168);
|
||||
SG_CHECK_EQUAL(b7.get_chunk_lon(), -168);
|
||||
|
||||
}
|
||||
|
||||
@@ -160,15 +161,15 @@ void testNearPolar()
|
||||
{
|
||||
SGBucket b1(1, 88.5);
|
||||
SGBucket b2(-1, 88.8);
|
||||
COMPARE(b1.get_chunk_lon(), 0);
|
||||
COMPARE(b1.get_chunk_lat(), 88);
|
||||
VERIFY(b1.gen_index() != b2.gen_index());
|
||||
SG_CHECK_EQUAL(b1.get_chunk_lon(), 0);
|
||||
SG_CHECK_EQUAL(b1.get_chunk_lat(), 88);
|
||||
SG_VERIFY(b1.gen_index() != b2.gen_index());
|
||||
|
||||
SGBucket b3(176.1, 88.5);
|
||||
COMPARE(b3.get_chunk_lon(), 176);
|
||||
SG_CHECK_EQUAL(b3.get_chunk_lon(), 176);
|
||||
|
||||
SGBucket b4(-178, 88.5);
|
||||
COMPARE(b4.get_chunk_lon(), -180);
|
||||
SG_CHECK_EQUAL(b4.get_chunk_lon(), -180);
|
||||
}
|
||||
|
||||
void testOffset()
|
||||
@@ -176,74 +177,74 @@ void testOffset()
|
||||
// bucket just below the 22 degree cutoff, so the next tile north
|
||||
// is twice the width
|
||||
SGBucket b1(-59.8, 21.9);
|
||||
COMPARE(b1.get_chunk_lat(), 21);
|
||||
COMPARE(b1.get_chunk_lon(), -60);
|
||||
COMPARE(b1.get_x(), 1);
|
||||
COMPARE(b1.get_y(), 7);
|
||||
SG_CHECK_EQUAL(b1.get_chunk_lat(), 21);
|
||||
SG_CHECK_EQUAL(b1.get_chunk_lon(), -60);
|
||||
SG_CHECK_EQUAL(b1.get_x(), 1);
|
||||
SG_CHECK_EQUAL(b1.get_y(), 7);
|
||||
|
||||
// offset vertically
|
||||
SGBucket b2(b1.sibling(0, 1));
|
||||
COMPARE(b2.get_chunk_lat(), 22);
|
||||
COMPARE(b2.get_chunk_lon(), -60);
|
||||
COMPARE(b2.get_x(), 0);
|
||||
COMPARE(b2.get_y(), 0);
|
||||
SG_CHECK_EQUAL(b2.get_chunk_lat(), 22);
|
||||
SG_CHECK_EQUAL(b2.get_chunk_lon(), -60);
|
||||
SG_CHECK_EQUAL(b2.get_x(), 0);
|
||||
SG_CHECK_EQUAL(b2.get_y(), 0);
|
||||
|
||||
COMPARE(b2.gen_index(), sgBucketOffset(-59.8, 21.9, 0, 1));
|
||||
SG_CHECK_EQUAL(b2.gen_index(), sgBucketOffset(-59.8, 21.9, 0, 1));
|
||||
|
||||
// offset vertically and horizontally. We compute horizontal (x)
|
||||
// movement at the target latitude, so this should move 0.25 * -3 degrees,
|
||||
// NOT 0.125 * -3 degrees.
|
||||
SGBucket b3(b1.sibling(-3, 1));
|
||||
COMPARE(b3.get_chunk_lat(), 22);
|
||||
COMPARE(b3.get_chunk_lon(), -61);
|
||||
COMPARE(b3.get_x(), 1);
|
||||
COMPARE(b3.get_y(), 0);
|
||||
SG_CHECK_EQUAL(b3.get_chunk_lat(), 22);
|
||||
SG_CHECK_EQUAL(b3.get_chunk_lon(), -61);
|
||||
SG_CHECK_EQUAL(b3.get_x(), 1);
|
||||
SG_CHECK_EQUAL(b3.get_y(), 0);
|
||||
|
||||
COMPARE(b3.gen_index(), sgBucketOffset(-59.8, 21.9, -3, 1));
|
||||
SG_CHECK_EQUAL(b3.gen_index(), sgBucketOffset(-59.8, 21.9, -3, 1));
|
||||
}
|
||||
|
||||
void testPolarOffset()
|
||||
{
|
||||
SGBucket b1(-11.7, -89.6);
|
||||
COMPARE(b1.get_chunk_lat(), -90);
|
||||
COMPARE(b1.get_chunk_lon(), -12);
|
||||
COMPARE(b1.get_x(), 0);
|
||||
COMPARE(b1.get_y(), 3);
|
||||
SG_CHECK_EQUAL(b1.get_chunk_lat(), -90);
|
||||
SG_CHECK_EQUAL(b1.get_chunk_lon(), -12);
|
||||
SG_CHECK_EQUAL(b1.get_x(), 0);
|
||||
SG_CHECK_EQUAL(b1.get_y(), 3);
|
||||
|
||||
// offset horizontally
|
||||
SGBucket b2(b1.sibling(-2, 0));
|
||||
COMPARE(b2.get_chunk_lat(), -90);
|
||||
COMPARE(b2.get_chunk_lon(), -36);
|
||||
COMPARE(b2.get_x(), 0);
|
||||
COMPARE(b2.get_y(), 3);
|
||||
SG_CHECK_EQUAL(b2.get_chunk_lat(), -90);
|
||||
SG_CHECK_EQUAL(b2.get_chunk_lon(), -36);
|
||||
SG_CHECK_EQUAL(b2.get_x(), 0);
|
||||
SG_CHECK_EQUAL(b2.get_y(), 3);
|
||||
|
||||
COMPARE(b2.gen_index(), sgBucketOffset(-11.7, -89.6, -2, 0));
|
||||
SG_CHECK_EQUAL(b2.gen_index(), sgBucketOffset(-11.7, -89.6, -2, 0));
|
||||
|
||||
// offset and wrap
|
||||
SGBucket b3(-170, 89.1);
|
||||
SGBucket b4(b3.sibling(-1, 0));
|
||||
COMPARE(b4.get_chunk_lat(), 89);
|
||||
COMPARE(b4.get_chunk_lon(), 168);
|
||||
COMPARE(b4.get_x(), 0);
|
||||
COMPARE(b4.get_y(), 0);
|
||||
SG_CHECK_EQUAL(b4.get_chunk_lat(), 89);
|
||||
SG_CHECK_EQUAL(b4.get_chunk_lon(), 168);
|
||||
SG_CHECK_EQUAL(b4.get_x(), 0);
|
||||
SG_CHECK_EQUAL(b4.get_y(), 0);
|
||||
|
||||
COMPARE(b4.gen_index(), sgBucketOffset(-170, 89.1, -1, 0));
|
||||
SG_CHECK_EQUAL(b4.gen_index(), sgBucketOffset(-170, 89.1, -1, 0));
|
||||
|
||||
|
||||
SGBucket b5(177, 87.3);
|
||||
SGBucket b6(b5.sibling(1, 1));
|
||||
COMPARE(b6.get_chunk_lat(), 87);
|
||||
COMPARE(b6.get_chunk_lon(), -180);
|
||||
COMPARE(b6.get_x(), 0);
|
||||
COMPARE(b6.get_y(), 3);
|
||||
SG_CHECK_EQUAL(b6.get_chunk_lat(), 87);
|
||||
SG_CHECK_EQUAL(b6.get_chunk_lon(), -180);
|
||||
SG_CHECK_EQUAL(b6.get_x(), 0);
|
||||
SG_CHECK_EQUAL(b6.get_y(), 3);
|
||||
|
||||
COMPARE(b6.gen_index(), sgBucketOffset(177, 87.3, 1, 1));
|
||||
SG_CHECK_EQUAL(b6.gen_index(), sgBucketOffset(177, 87.3, 1, 1));
|
||||
|
||||
// offset vertically towards the pole
|
||||
SGBucket b7(b1.sibling(0, -5));
|
||||
VERIFY(!b7.isValid());
|
||||
SG_VERIFY(!b7.isValid());
|
||||
|
||||
VERIFY(!SGBucket(0, 90).sibling(0, 1).isValid());
|
||||
SG_VERIFY(!SGBucket(0, 90).sibling(0, 1).isValid());
|
||||
}
|
||||
|
||||
// test behaviour of bucket-offset near the anti-meridian (180-meridian)
|
||||
@@ -251,17 +252,17 @@ void testOffsetWrap()
|
||||
{
|
||||
// near the equator
|
||||
SGBucket b1(-179.8, 16.8);
|
||||
COMPARE(b1.get_chunk_lat(), 16);
|
||||
COMPARE(b1.get_chunk_lon(), -180);
|
||||
COMPARE(b1.get_x(), 1);
|
||||
COMPARE(b1.get_y(), 6);
|
||||
SG_CHECK_EQUAL(b1.get_chunk_lat(), 16);
|
||||
SG_CHECK_EQUAL(b1.get_chunk_lon(), -180);
|
||||
SG_CHECK_EQUAL(b1.get_x(), 1);
|
||||
SG_CHECK_EQUAL(b1.get_y(), 6);
|
||||
|
||||
SGBucket b2(b1.sibling(-2, 0));
|
||||
COMPARE(b2.get_chunk_lat(), 16);
|
||||
COMPARE(b2.get_chunk_lon(), 179);
|
||||
COMPARE(b2.get_x(), 7);
|
||||
COMPARE(b2.get_y(), 6);
|
||||
COMPARE(b2.gen_index(), sgBucketOffset(-179.8, 16.8, -2, 0));
|
||||
SG_CHECK_EQUAL(b2.get_chunk_lat(), 16);
|
||||
SG_CHECK_EQUAL(b2.get_chunk_lon(), 179);
|
||||
SG_CHECK_EQUAL(b2.get_x(), 7);
|
||||
SG_CHECK_EQUAL(b2.get_y(), 6);
|
||||
SG_CHECK_EQUAL(b2.gen_index(), sgBucketOffset(-179.8, 16.8, -2, 0));
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
//
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "BVHMaterial.hxx"
|
||||
|
||||
namespace simgear {
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
//
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "BVHPageNode.hxx"
|
||||
|
||||
#include "BVHPager.hxx"
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
//
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "BVHPageRequest.hxx"
|
||||
|
||||
namespace simgear {
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
//
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "BVHPager.hxx"
|
||||
|
||||
#include <list>
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
//
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "BVHStaticNode.hxx"
|
||||
|
||||
namespace simgear {
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
//
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include <iostream>
|
||||
#include <simgear/structure/SGSharedPtr.hxx>
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "Canvas.hxx"
|
||||
#include "CanvasEventManager.hxx"
|
||||
#include "CanvasEventVisitor.hxx"
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "CanvasEvent.hxx"
|
||||
|
||||
namespace simgear
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "CanvasEventManager.hxx"
|
||||
#include <simgear/canvas/events/MouseEvent.hxx>
|
||||
#include <simgear/canvas/elements/CanvasElement.hxx>
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "CanvasEvent.hxx"
|
||||
#include "CanvasEventVisitor.hxx"
|
||||
#include <simgear/canvas/elements/CanvasElement.hxx>
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "CanvasMgr.hxx"
|
||||
#include "Canvas.hxx"
|
||||
#include "CanvasEventManager.hxx"
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "Canvas.hxx"
|
||||
#include "CanvasObjectPlacement.hxx"
|
||||
#include <simgear/canvas/events/MouseEvent.hxx>
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "CanvasPlacement.hxx"
|
||||
#include <simgear/props/props.hxx>
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "CanvasMgr.hxx"
|
||||
#include "CanvasSystemAdapter.hxx"
|
||||
#include "CanvasWindow.hxx"
|
||||
|
||||
@@ -44,6 +44,11 @@
|
||||
#if defined(_MSC_VER)
|
||||
# pragma warning(disable:4311)
|
||||
# pragma warning(disable:4312)
|
||||
# define ALIGN16 __declspec(align(16))
|
||||
# define ALIGN16C
|
||||
#elif defined(__GNUC__)
|
||||
# define ALIGN16
|
||||
# define ALIGN16C __attribute__((aligned(16)))
|
||||
#endif
|
||||
|
||||
/* Type definitions */
|
||||
|
||||
@@ -35,21 +35,33 @@ void SHVector2_dtor(SHVector2 *v) {
|
||||
}
|
||||
|
||||
void SHVector3_ctor(SHVector3 *v) {
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
v->vec = _mm_setzero_ps();
|
||||
#else
|
||||
v->x=0.0f; v->y=0.0f; v->z=0.0f;
|
||||
#endif
|
||||
}
|
||||
|
||||
void SHVector3_dtor(SHVector3 *v) {
|
||||
}
|
||||
|
||||
void SHVector4_ctor(SHVector4 *v) {
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
v->vec = _mm_setzero_ps();
|
||||
#else
|
||||
v->x=0.0f; v->y=0.0f; v->z=0.0f; v->w=0.0f;
|
||||
#endif
|
||||
}
|
||||
|
||||
void SHVector4_dtor(SHVector4 *v) {
|
||||
}
|
||||
|
||||
void SHRectangle_ctor(SHRectangle *r) {
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
r->vec = _mm_setzero_ps();
|
||||
#else
|
||||
r->x=0.0f; r->y=0.0f; r->w=0.0f; r->h=0.0f;
|
||||
#endif
|
||||
}
|
||||
|
||||
void SHRectangle_dtor(SHRectangle *r) {
|
||||
@@ -135,3 +147,24 @@ int shLineLineXsection(SHVector2 *o1, SHVector2 *v1,
|
||||
xsection->y = o1->y + t1*v1->y;
|
||||
return 1;
|
||||
}
|
||||
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# ifdef __SSE3__
|
||||
# include <pmmintrin.h>
|
||||
inline float hsum_ps_sse(__m128 v) {
|
||||
__m128 shuf = _mm_movehdup_ps(v);
|
||||
__m128 sums = _mm_add_ps(v, shuf);
|
||||
shuf = _mm_movehl_ps(shuf, sums);
|
||||
sums = _mm_add_ss(sums, shuf);
|
||||
return _mm_cvtss_f32(sums);
|
||||
}
|
||||
# else
|
||||
inline float hsum_ps_sse(__m128 v) {
|
||||
__m128 shuf = _mm_shuffle_ps(v, v, _MM_SHUFFLE(2, 3, 0, 1));
|
||||
__m128 sums = _mm_add_ps(v, shuf);
|
||||
shuf = _mm_movehl_ps(shuf, sums);
|
||||
sums = _mm_add_ss(sums, shuf);
|
||||
return _mm_cvtss_f32(sums);
|
||||
}
|
||||
# endif
|
||||
#endif
|
||||
|
||||
@@ -21,6 +21,21 @@
|
||||
#ifndef __SHVECTORS_H
|
||||
#define __SHVECTORS_H
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <simgear/simgear_config.h>
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_SIMD
|
||||
# ifdef __SSE__
|
||||
// # define SHIVA_USE_SIMD
|
||||
# endif
|
||||
# endif
|
||||
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# include <xmmintrin.h>
|
||||
float hsum_ps_sse(__m128 v);
|
||||
#endif
|
||||
|
||||
#include "shDefs.h"
|
||||
|
||||
/* Vector structures
|
||||
@@ -33,9 +48,17 @@ typedef struct
|
||||
void SHVector2_ctor(SHVector2 *v);
|
||||
void SHVector2_dtor(SHVector2 *v);
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
union ALIGN16 {
|
||||
__m128 vec;
|
||||
struct { SHfloat x,y,z,w; };
|
||||
} ALIGN16C;
|
||||
#else
|
||||
SHfloat x,y,z;
|
||||
#endif
|
||||
} SHVector3;
|
||||
|
||||
void SHVector3_ctor(SHVector3 *v);
|
||||
@@ -43,7 +66,14 @@ void SHVector3_dtor(SHVector3 *v);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
union ALIGN16 {
|
||||
__m128 vec;
|
||||
struct { SHfloat x,y,z,w; };
|
||||
} ALIGN16C;
|
||||
#else
|
||||
SHfloat x,y,z,w;
|
||||
#endif
|
||||
} SHVector4;
|
||||
|
||||
void SHVector4_ctor(SHVector4 *v);
|
||||
@@ -51,7 +81,14 @@ void SHVector4_dtor(SHVector4 *v);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
union ALIGN16 {
|
||||
__m128 vec;
|
||||
struct { SHfloat x,y,w,h; };
|
||||
} ALIGN16C;
|
||||
#else
|
||||
SHfloat x,y,w,h;
|
||||
#endif
|
||||
} SHRectangle;
|
||||
|
||||
void SHRectangle_ctor(SHRectangle *r);
|
||||
@@ -61,7 +98,14 @@ void shRectangleSet(SHRectangle *r, SHfloat x,
|
||||
|
||||
typedef struct
|
||||
{
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
union ALIGN16 {
|
||||
__m128 mtx[4];
|
||||
SHfloat m[4][4];
|
||||
} ALIGN16C;
|
||||
#else
|
||||
SHfloat m[3][3];
|
||||
#endif
|
||||
} SHMatrix3x3;
|
||||
|
||||
void SHMatrix3x3_ctor(SHMatrix3x3 *m);
|
||||
@@ -83,12 +127,22 @@ void SHMatrix3x3_dtor(SHMatrix3x3 *m);
|
||||
*--------------------------------------------------------- */
|
||||
|
||||
#define SET2(v,xs,ys) { v.x=xs; v.y=ys; }
|
||||
#define SET3(v,xs,ys,zs) { v.x=xs; v.y=ys; v.z=zs; }
|
||||
#define SET4(v,xs,ys,zs,ws) { v.x=xs; v.y=ys; v.z=zs; v.w=ws; }
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define SET3(v,xs,ys,zs,ws) { v.vec=_mm_set_ps(0,zs,ys,xs); }
|
||||
# define SET4(v,xs,ys,zs,ws) { v.vec=_mm_set_ps(ws,zs,ys,xs); }
|
||||
#else
|
||||
# define SET3(v,xs,ys,zs) { v.x=xs; v.y=ys; v.z=zs; }
|
||||
# define SET4(v,xs,ys,zs,ws) { v.x=xs; v.y=ys; v.z=zs; v.w=ws; }
|
||||
#endif
|
||||
|
||||
#define SET2V(v1,v2) { v1.x=v2.x; v1.y=v2.y; }
|
||||
#define SET3V(v1,v2) { v1.x=v2.x; v1.y=v2.y; v1.z=v2.z; }
|
||||
#define SET4V(v1,v2) { v1.x=v2.x; v1.y=v2.y; v1.z=v2.z; v1.w=v2.w; }
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define SET3V(v1,v2) { v1.vec=v2.vec; }
|
||||
# define SET4V(v1,v2) { v1.vec=v2.vec; }
|
||||
#else
|
||||
# define SET3V(v1,v2) { v1.x=v2.x; v1.y=v2.y; v1.z=v2.z; }
|
||||
# define SET4V(v1,v2) { v1.x=v2.x; v1.y=v2.y; v1.z=v2.z; v1.w=v2.w; }
|
||||
#endif
|
||||
|
||||
#define EQ2(v,xx,yy) ( v.x==xx && v.y==yy )
|
||||
#define EQ3(v,xx,yy,zz) ( v.x==xx && v.y==yy && v.z==zz )
|
||||
@@ -103,48 +157,89 @@ void SHMatrix3x3_dtor(SHMatrix3x3 *m);
|
||||
#define EQ4V(v1,v2) ( v1.x==v2.x && v1.y==v2.y && v1.z==v2.z && v1.w==v2.w )
|
||||
|
||||
#define ADD2(v,xx,yy) { v.x+=xx; v.y+=yy; }
|
||||
#define ADD3(v,xx,yy,zz) { v.x+=xx; v.y+=yy; v.z+=zz; }
|
||||
#define ADD4(v,xx,yy,zz,ww) { v.x+=xx; v.y+=yy; v.z+=zz; v.w+=ww; }
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define ADD3(v,xx,yy,zz,ww) { v.vec=_mm_add_ps(v.vec,_mm_set_ps(0,zz,yy,xx)); }
|
||||
# define ADD4(v,xx,yy,zz,ww) { v.vec=_mm_add_ps(v.vec,_mm_set_ps(ww,zz,yy,xx)); }
|
||||
#else
|
||||
# define ADD3(v,xx,yy,zz) { v.x+=xx; v.y+=yy; v.z+=zz; }
|
||||
# define ADD4(v,xx,yy,zz,ww) { v.x+=xx; v.y+=yy; v.z+=zz; v.w+=ww; }
|
||||
#endif
|
||||
|
||||
#define ADD2V(v1,v2) { v1.x+=v2.x; v1.y+=v2.y; }
|
||||
#define ADD3V(v1,v2) { v1.x+=v2.x; v1.y+=v2.y; v1.z+=v2.z; }
|
||||
#define ADD4V(v1,v2) { v1.x+=v2.x; v1.y+=v2.y; v1.z+=v2.z; v1.w+=v2.w; }
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define ADD3V(v1,v2) { v1.vec=_mm_add_ps(v1.vec,v2.vec); }
|
||||
# define ADD4V(v1,v2) { v1.vec=_mm_add_ps(v1.vec,v2.vec); }
|
||||
#else
|
||||
# define ADD3V(v1,v2) { v1.x+=v2.x; v1.y+=v2.y; v1.z+=v2.z; }
|
||||
# define ADD4V(v1,v2) { v1.x+=v2.x; v1.y+=v2.y; v1.z+=v2.z; v1.w+=v2.w; }
|
||||
#endif
|
||||
|
||||
#define SUB2(v,xx,yy) { v.x-=xx; v.y-=yy; }
|
||||
#define SUB3(v,xx,yy,zz) { v.x-=xx; v.y-=yy; v.z-=zz; }
|
||||
#define SUB4(v,xx,yy,zz,ww) { v.x-=xx; v.y-=yy; v.z-=zz; v.w-=v2.w; }
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define SUB3(v,xx,yy,zz,ww) { v.vec=_mm_sub_ps(v.vec,_mm_set_ps(0,zz,yy,xx)); }
|
||||
# define SUB4(v,xx,yy,zz,ww) { v.vec=_mm_sub_ps(v.vec,_mm_set_ps(ww,zz,yy,xx)); }
|
||||
#else
|
||||
# define SUB3(v,xx,yy,zz) { v.x-=xx; v.y-=yy; v.z-=zz; }
|
||||
# define SUB4(v,xx,yy,zz,ww) { v.x-=xx; v.y-=yy; v.z-=zz; v.w-=v2.w; }
|
||||
#endif
|
||||
|
||||
#define SUB2V(v1,v2) { v1.x-=v2.x; v1.y-=v2.y; }
|
||||
#define SUB3V(v1,v2) { v1.x-=v2.x; v1.y-=v2.y; v1.z-=v2.z; }
|
||||
#define SUB4V(v1,v2) { v1.x-=v2.x; v1.y-=v2.y; v1.z-=v2.z; v1.w-=v2.w; }
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define SUB3V(v1,v2) { v1.vec=_mm_sub_ps(v1.vec,v2.vec); }
|
||||
# define SUB4V(v1,v2) { v1.vec=_mm_sub_ps(v1.vec,v2.vec); }
|
||||
#else
|
||||
# define SUB3V(v1,v2) { v1.x-=v2.x; v1.y-=v2.y; v1.z-=v2.z; }
|
||||
# define SUB4V(v1,v2) { v1.x-=v2.x; v1.y-=v2.y; v1.z-=v2.z; v1.w-=v2.w; }
|
||||
#endif
|
||||
|
||||
#define MUL2(v,f) { v.x*=f; v.y*=f; }
|
||||
#define MUL3(v,f) { v.x*=f; v.y*=f; v.z*=z; }
|
||||
#define MUL4(v,f) { v.x*=f; v.y*=f; v.z*=z; v.w*=w; }
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define MUL3(v,f) { v.vec=_mm_mul_ps(v.vec,_mm_set1_ps(f)); }
|
||||
# define MUL4(v,f) { v.vec=_mm_mul_ps(v.vec,_mm_set1_ps(f)); }
|
||||
#else
|
||||
# define MUL3(v,f) { v.x*=f; v.y*=f; v.z*=z; }
|
||||
# define MUL4(v,f) { v.x*=f; v.y*=f; v.z*=z; v.w*=w; }
|
||||
#endif
|
||||
|
||||
#define DIV2(v,f) { v.x/=f; v.y/=f; }
|
||||
#define DIV3(v,f) { v.x/=f; v.y/=f; v.z/=z; }
|
||||
#define DIV4(v,f) { v.x/=f; v.y/=f; v.z/=z; v.w/=w; }
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define DIV3(v,f) { v.vec=_mm_div_ps(v.vec,_mm_set1_ps(f)); }
|
||||
# define DIV4(v,f) { v.vec=_mm_div_ps(v.vec,_mm_set1_ps(f)); }
|
||||
#else
|
||||
# define DIV3(v,f) { v.x/=f; v.y/=f; v.z/=z; }
|
||||
# define DIV4(v,f) { v.x/=f; v.y/=f; v.z/=z; v.w/=w; }
|
||||
#endif
|
||||
|
||||
#define ABS2(v) { v.x=SH_ABS(v.x); v.y=SH_ABS(v.y); }
|
||||
#define ABS3(v) { v.x=SH_ABS(v.x); v.y=SH_ABS(v.y); v.z=SH_ABS(v.z); }
|
||||
#define ABS4(v) { v.x=SH_ABS(v.x); v.y=SH_ABS(v.y); v.z=SH_ABS(v.z); v.w=SH_ABS(v.w); }
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define ABS_MASK _mm_set1_ps(-0.f)
|
||||
# define ABS3(v) { v.vec=_mm_andnot_ps(ABS_MASK, v.vec); }
|
||||
# define ABS4(v) { v.vec=_mm_andnot_ps(ABS_MASK, v.vec); }
|
||||
#else
|
||||
# define ABS3(v) { v.x=SH_ABS(v.x); v.y=SH_ABS(v.y); v.z=SH_ABS(v.z); }
|
||||
# define ABS4(v) { v.x=SH_ABS(v.x); v.y=SH_ABS(v.y); v.z=SH_ABS(v.z); v.w=SH_ABS(v.w); }
|
||||
#endif
|
||||
|
||||
#define NORMSQ2(v) (v.x*v.x + v.y*v.y)
|
||||
#define NORMSQ3(v) (v.x*v.x + v.y*v.y + v.z*v.z)
|
||||
#define NORMSQ4(v) (v.x*v.x + v.y*v.y + v.z*v.z + v.w*v.w)
|
||||
#define NORMSQ2(v) DOT2(v,v)
|
||||
#define NORMSQ3(v) DOT3(v,v)
|
||||
#define NORMSQ4(v) DOT4(v,v)
|
||||
|
||||
#define NORM2(v) SH_SQRT(NORMSQ2(v))
|
||||
#define NORM3(v) SH_SQRT(NORMSQ3(v))
|
||||
#define NORM4(v) SH_SQRT(NORMSQ4(v))
|
||||
|
||||
#define NORMALIZE2(v) { SHfloat n=NORM2(v); v.x/=n; v.y/=n; }
|
||||
#define NORMALIZE3(v) { SHfloat n=NORM3(v); v.x/=n; v.y/=n; v.z/=n; }
|
||||
#define NORMALIZE4(v) { SHfloat n=NORM4(v); v.x/=n; v.y/=n; v.z/=n; v.w/=w; }
|
||||
#define NORMALIZE2(v) { SHfloat n=NORM2(v); DIV2(v,n); }
|
||||
#define NORMALIZE3(v) { SHfloat n=NORM3(v); DIV3(v,n); }
|
||||
#define NORMALIZE4(v) { SHfloat n=NORM4(v); DIV4(v,n); }
|
||||
|
||||
#define DOT2(v1,v2) (v1.x*v2.x + v1.y*v2.y)
|
||||
#define DOT3(v1,v2) (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z)
|
||||
#define DOT4(v1,v2) (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w)
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define DOT4(v1,v2) hsum_ps_sse(_mm_mul_ps(v1.vec,v2.vec))
|
||||
# define DOT4(v1,v2) hsum_ps_sse(_mm_mul_ps(v1.vec,v2.vec))
|
||||
#else
|
||||
# define DOT3(v1,v2) (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z)
|
||||
# define DOT4(v1,v2) (v1.x*v2.x + v1.y*v2.y + v1.z*v2.z + v1.w*v2.w)
|
||||
#endif
|
||||
|
||||
#define CROSS2(v1,v2) (v1.x*v2.y - v2.x*v1.y)
|
||||
|
||||
@@ -152,37 +247,84 @@ void SHMatrix3x3_dtor(SHMatrix3x3 *m);
|
||||
#define ANGLE2N(v1,v2) (SH_ACOS( DOT2(v1,v2) ))
|
||||
|
||||
#define OFFSET2V(v, o, s) { v.x += o.x*s; v.y += o.y*s; }
|
||||
#define OFFSET3V(v, o, s) { v.x += o.x*s; v.y += o.y*s; v.z += o.z*s; }
|
||||
#define OFFSET4V(v, o, s) { v.x += o.x*s; v.y += o.y*s; v.z += o.z*s; v.w += o.w*s; }
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define OFFSET4V(v, o, s) { v.vec=_mm_add_ps(v.vec,_mm_mul_ps(o.vec,_mm_set1_ps(s))); }
|
||||
# define OFFSET4V(v, o, s) { v.vec=_mm_add_ps(v.vec,_mm_mul_ps(o.vec,_mm_set1_ps(s))); }
|
||||
#else
|
||||
# define OFFSET3V(v, o, s) { v.x += o.x*s; v.y += o.y*s; v.z += o.z*s; }
|
||||
# define OFFSET4V(v, o, s) { v.x += o.x*s; v.y += o.y*s; v.z += o.z*s; v.w += o.w*s; }
|
||||
#endif
|
||||
|
||||
/*-----------------------------------------------------
|
||||
* Macros for matrix operations
|
||||
*-----------------------------------------------------*/
|
||||
|
||||
#define SETMAT(mat, m00, m01, m02, m10, m11, m12, m20, m21, m22) { \
|
||||
mat.m[0][0] = m00; mat.m[0][1] = m01; mat.m[0][2] = m02; \
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define SETMAT(mat, m00, m01, m02, m10, m11, m12, m20, m21, m22) { \
|
||||
mat.mtx[0] = _mm_set_ps(0,m02,m01,m00); \
|
||||
mat.mtx[1] = _mm_set_ps(0,m12,m11,m10); \
|
||||
mat.mtx[2] = _mm_set_ps(0,m22,m21,m20); \
|
||||
mat.mtx[3] = _mm_setzero_ps(); }
|
||||
#else
|
||||
# define SETMAT(mat, m00, m01, m02, m10, m11, m12, m20, m21, m22) { \
|
||||
mat.m[0][0] = m00; mat.m[0][1] = m01; mat.m[0][2] = m02; \
|
||||
mat.m[1][0] = m10; mat.m[1][1] = m11; mat.m[1][2] = m12; \
|
||||
mat.m[2][0] = m20; mat.m[2][1] = m21; mat.m[2][2] = m22; }
|
||||
#endif
|
||||
|
||||
#define SETMATMAT(m1, m2) { \
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define SETMATMAT(m1, m2) { \
|
||||
m1.mtx[0] = m2.mtx[0]; \
|
||||
m1.mtx[1] = m2.mtx[1]; \
|
||||
m1.mtx[2] = m2.mtx[2]; }
|
||||
#else
|
||||
# define SETMATMAT(m1, m2) { \
|
||||
int i,j; \
|
||||
for(i=0;i<3;i++) \
|
||||
for(j=0;j<3;j++) \
|
||||
m1.m[i][j] = m2.m[i][j]; }
|
||||
#endif
|
||||
|
||||
#define MULMATS(mat, s) { \
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define MULMATS(mat, s) { \
|
||||
mat.mtx[0] = _mm_mul_ps(mat.mtx[0],_mm_set1_ps(s)); \
|
||||
mat.mtx[1] = _mm_mul_ps(mat.mtx[1],_mm_set1_ps(s)); \
|
||||
mat.mtx[2] = _mm_mul_ps(mat.mtx[2],_mm_set1_ps(s)); }
|
||||
#else
|
||||
# define MULMATS(mat, s) { \
|
||||
int i,j; \
|
||||
for(i=0;i<3;i++) \
|
||||
for(j=0;j<3;j++) \
|
||||
mat.m[i][j] *= s; }
|
||||
#endif
|
||||
|
||||
#define DIVMATS(mat, s) { \
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define DIVMATS(mat, s) { \
|
||||
mat.mtx[0] = _mm_mul_ps(mat.mtx[0],_mm_set1_ps(1/s)); \
|
||||
mat.mtx[1] = _mm_mul_ps(mat.mtx[1],_mm_set1_ps(1/s)); \
|
||||
mat.mtx[2] = _mm_mul_ps(mat.mtx[2],_mm_set1_ps(1/s)); }
|
||||
#else
|
||||
# define DIVMATS(mat, s) { \
|
||||
int i,j; \
|
||||
for(i=0;i<3;i++) \
|
||||
for(j=0;j<3;j++) \
|
||||
mat.m[i][j] /= s; }
|
||||
#endif
|
||||
|
||||
#define MULMATMAT(m1, m2, mout) { \
|
||||
#ifdef SHIVA_USE_SIMD
|
||||
# define MULMATMAT(m2, m1, mout) { \
|
||||
int i,j; \
|
||||
for (i=0;i<4;i++) { \
|
||||
__m128 a = m1.mtx[0]; \
|
||||
__m128 b = _mm_set1_ps(m2.m[i][0]); \
|
||||
mout.mtx[i] = a*b; \
|
||||
for (j=1;j<4;j++) { \
|
||||
a = m1.mtx[j]; \
|
||||
b = _mm_set1_ps(m2.m[i][j]); \
|
||||
mout.mtx[i] += a*b; } } }
|
||||
|
||||
#else
|
||||
# define MULMATMAT(m1, m2, mout) { \
|
||||
int i,j; \
|
||||
for(i=0;i<3;i++) \
|
||||
for(j=0;j<3;j++) \
|
||||
@@ -190,6 +332,7 @@ int i,j; \
|
||||
m1.m[i][0] * m2.m[0][j] + \
|
||||
m1.m[i][1] * m2.m[1][j] + \
|
||||
m1.m[i][2] * m2.m[2][j]; }
|
||||
#endif
|
||||
|
||||
#define IDMAT(mat) SETMAT(mat, 1,0,0, 0,1,0, 0,0,1)
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "CanvasElement.hxx"
|
||||
#include <simgear/canvas/Canvas.hxx>
|
||||
#include <simgear/canvas/CanvasEventVisitor.hxx>
|
||||
@@ -640,6 +642,27 @@ namespace canvas
|
||||
_scissor->_coord_reference = rf;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void Element::setRotation(unsigned int index, double r)
|
||||
{
|
||||
_node->getChild(NAME_TRANSFORM, index, true)->setDoubleValue("rot", r);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void Element::setTranslation(unsigned int index, double x, double y)
|
||||
{
|
||||
SGPropertyNode* tf = _node->getChild(NAME_TRANSFORM, index, true);
|
||||
tf->getChild("t", 0, true)->setDoubleValue(x);
|
||||
tf->getChild("t", 1, true)->setDoubleValue(y);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void Element::setTransformEnabled(unsigned int index, bool enabled)
|
||||
{
|
||||
SGPropertyNode* tf = _node->getChild(NAME_TRANSFORM, index, true);
|
||||
tf->setBoolValue("enabled", enabled);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
osg::BoundingBox Element::getBoundingBox() const
|
||||
{
|
||||
@@ -701,6 +724,9 @@ namespace canvas
|
||||
continue;
|
||||
|
||||
SGPropertyNode* tf_node = _node->getChild("tf", i, true);
|
||||
if (!tf_node->getBoolValue("enabled", true)) {
|
||||
continue; // skip disabled transforms
|
||||
}
|
||||
|
||||
// Build up the matrix representation of the current transform node
|
||||
osg::Matrix tf;
|
||||
|
||||
@@ -172,6 +172,22 @@ namespace canvas
|
||||
*/
|
||||
void setClipFrame(ReferenceFrame rf);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
void setRotation(unsigned int index, double r);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
void setTranslation(unsigned int index, double x, double y);
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
*/
|
||||
void setTransformEnabled(unsigned int index, bool enabled);
|
||||
|
||||
/**
|
||||
* Get bounding box (may not be as tight as bounding box returned by
|
||||
* #getTightBoundingBox)
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "CanvasGroup.hxx"
|
||||
#include "CanvasImage.hxx"
|
||||
#include "CanvasMap.hxx"
|
||||
@@ -373,7 +375,7 @@ namespace canvas
|
||||
SG_LOG
|
||||
(
|
||||
SG_GENERAL,
|
||||
SG_INFO,
|
||||
SG_DEBUG,
|
||||
"canvas::Group: Moved element " << index << " to position " << index_new
|
||||
);
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "CanvasImage.hxx"
|
||||
|
||||
#include <simgear/canvas/Canvas.hxx>
|
||||
@@ -487,11 +489,17 @@ namespace canvas
|
||||
if( !fill.empty() // If no color is given default to white
|
||||
&& !parseColor(fill, color) )
|
||||
return;
|
||||
|
||||
_colors->front() = color;
|
||||
_colors->dirty();
|
||||
setFill(color);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void Image::setFill(const osg::Vec4& color)
|
||||
{
|
||||
_colors->front() = color;
|
||||
_colors->dirty();
|
||||
}
|
||||
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void Image::setOutset(const std::string& outset)
|
||||
{
|
||||
@@ -506,6 +514,13 @@ namespace canvas
|
||||
_attributes_dirty |= SRC_RECT;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void Image::setSourceRect(const SGRect<float>& sourceRect)
|
||||
{
|
||||
_attributes_dirty |= SRC_RECT;
|
||||
_src_rect = sourceRect;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void Image::setSlice(const std::string& slice)
|
||||
{
|
||||
|
||||
@@ -61,6 +61,7 @@ namespace canvas
|
||||
|
||||
void setImage(osg::ref_ptr<osg::Image> img);
|
||||
void setFill(const std::string& fill);
|
||||
void setFill(const osg::Vec4& color);
|
||||
|
||||
/**
|
||||
* @see http://www.w3.org/TR/css3-background/#border-image-outset
|
||||
@@ -95,6 +96,10 @@ namespace canvas
|
||||
|
||||
bool handleEvent(const EventPtr& event);
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
void setSourceRect(const SGRect<float>& sourceRect);
|
||||
protected:
|
||||
|
||||
enum ImageAttributes
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "CanvasMap.hxx"
|
||||
#include "map/geo_node_pair.hxx"
|
||||
#include "map/projection.hxx"
|
||||
|
||||
@@ -16,14 +16,18 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "CanvasPath.hxx"
|
||||
#include <simgear/scene/util/parse_color.hxx>
|
||||
#include <simgear/misc/strutils.hxx>
|
||||
|
||||
#include <osg/Drawable>
|
||||
#include <osg/Version>
|
||||
|
||||
#include <vg/openvg.h>
|
||||
#include <cassert>
|
||||
#include <cctype>
|
||||
|
||||
namespace simgear
|
||||
{
|
||||
@@ -57,6 +61,153 @@ namespace canvas
|
||||
*/
|
||||
std::vector<float> splitAndConvert(const char del[], const std::string& str);
|
||||
|
||||
static float parseCSSNumber(const std::string& s)
|
||||
{
|
||||
if (strutils::ends_with(s, "px")) {
|
||||
return std::stof(s.substr(0, s.length() - 2));
|
||||
} else if (s.back() == '%') {
|
||||
float f = std::stof(s.substr(0, s.length() - 1));
|
||||
return f / 100.0f;
|
||||
}
|
||||
return std::stof(s);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
static bool parseSVGPathToVGPath(const std::string& svgPath, CmdList& commands, CoordList& coords)
|
||||
{
|
||||
const string_list& tokens = simgear::strutils::split_on_any_of(svgPath, "\t \n\r,");
|
||||
char activeSVGCommand = 0;
|
||||
bool isRelative = false;
|
||||
int tokensNeeded = 0;
|
||||
|
||||
for (auto it = tokens.begin(); it != tokens.end(); ) {
|
||||
// set up the new command data
|
||||
if ((it->size() == 1) && std::isalpha(it->at(0))) {
|
||||
const char svgCommand = std::toupper(it->at(0));
|
||||
isRelative = std::islower(it->at(0));
|
||||
switch (svgCommand) {
|
||||
case 'Z':
|
||||
tokensNeeded = 0;
|
||||
break;
|
||||
case 'M':
|
||||
case 'L':
|
||||
case 'T':
|
||||
tokensNeeded = 2;
|
||||
break;
|
||||
case 'H':
|
||||
case 'V':
|
||||
tokensNeeded = 1;
|
||||
break;
|
||||
case 'C':
|
||||
tokensNeeded = 6;
|
||||
break;
|
||||
case 'S':
|
||||
case 'Q':
|
||||
tokensNeeded = 4;
|
||||
break;
|
||||
case 'A':
|
||||
tokensNeeded = 7;
|
||||
break;
|
||||
default:
|
||||
SG_LOG(SG_GENERAL, SG_WARN, "unrecognized SVG path command: "
|
||||
<< *it << " at token " << std::distance(tokens.begin(), it));
|
||||
return false;
|
||||
}
|
||||
|
||||
activeSVGCommand = svgCommand;
|
||||
++it; // advance to first coordinate token
|
||||
}
|
||||
|
||||
const int numTokensRemaining = std::distance(it, tokens.end());
|
||||
if (numTokensRemaining < tokensNeeded) {
|
||||
SG_LOG(SG_GENERAL, SG_WARN, "insufficent SVG path tokens");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool pushTokensDirectly = true;
|
||||
if (activeSVGCommand == 'Z') {
|
||||
commands.push_back(VG_CLOSE_PATH);
|
||||
activeSVGCommand = 0;
|
||||
} else if (activeSVGCommand == 'M') {
|
||||
commands.push_back(VG_MOVE_TO | isRelative);
|
||||
activeSVGCommand = 'L';
|
||||
} else if (activeSVGCommand == 'L') {
|
||||
commands.push_back(VG_LINE_TO | isRelative);
|
||||
} else if (activeSVGCommand == 'H') {
|
||||
commands.push_back(VG_HLINE_TO | isRelative);
|
||||
} else if (activeSVGCommand == 'V') {
|
||||
commands.push_back(VG_HLINE_TO | isRelative);
|
||||
} else if (activeSVGCommand == 'C') {
|
||||
commands.push_back(VG_CUBIC_TO | isRelative);
|
||||
} else if (activeSVGCommand == 'S') {
|
||||
commands.push_back(VG_SCUBIC_TO | isRelative);
|
||||
} else if (activeSVGCommand == 'Q') {
|
||||
commands.push_back(VG_SCUBIC_TO | isRelative);
|
||||
} else if (activeSVGCommand == 'T') {
|
||||
commands.push_back(VG_SCUBIC_TO | isRelative);
|
||||
} else if (activeSVGCommand == 'A') {
|
||||
pushTokensDirectly = false; // deal with tokens manually
|
||||
coords.push_back(parseCSSNumber(*it++)); // rx
|
||||
coords.push_back(parseCSSNumber(*it++)); // ry
|
||||
coords.push_back(parseCSSNumber(*it++)); // x-axis rotation
|
||||
|
||||
const bool isLargeArc = std::stoi(*it++); // large-angle
|
||||
const bool isCCW = std::stoi(*it++); // sweep-flag
|
||||
|
||||
int vgCmd = isLargeArc ? (isCCW ? VG_LCCWARC_TO : VG_LCWARC_TO) :
|
||||
(isCCW ? VG_SCCWARC_TO : VG_SCWARC_TO);
|
||||
|
||||
coords.push_back(parseCSSNumber(*it++));
|
||||
coords.push_back(parseCSSNumber(*it++));
|
||||
commands.push_back(vgCmd | isRelative);
|
||||
} else {
|
||||
SG_LOG(SG_GENERAL, SG_WARN, "malformed SVG path string: expected a command at token:"
|
||||
<< std::distance(tokens.begin(), it) << " :" << *it);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (pushTokensDirectly) {
|
||||
for (int i=0; i<tokensNeeded;++i) {
|
||||
coords.push_back(parseCSSNumber(*it++));
|
||||
}
|
||||
}
|
||||
} // of tokens iteration
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
static SGVec2f parseRectCornerRadius(SGPropertyNode* node, const std::string& xDir, const std::string& yDir, bool& haveCorner)
|
||||
{
|
||||
haveCorner = false;
|
||||
std::string propName = "border-" + yDir + "-" + xDir + "-radius";
|
||||
if (!node->hasChild(propName)) {
|
||||
propName = "border-" + yDir + "-radius";
|
||||
if (!node->hasChild(propName)) {
|
||||
propName = "border-radius";
|
||||
}
|
||||
}
|
||||
|
||||
PropertyList props = node->getChildren(propName);
|
||||
if (props.size() == 1) {
|
||||
double r = props.at(0)->getDoubleValue(propName);
|
||||
haveCorner = true;
|
||||
return SGVec2f(r, r);
|
||||
}
|
||||
|
||||
if (props.size() >= 2 ) {
|
||||
haveCorner = true;
|
||||
return SGVec2f(props.at(0)->getDoubleValue(),
|
||||
props.at(1)->getDoubleValue());
|
||||
}
|
||||
|
||||
return SGVec2f(-1.0f, -1.0f);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
class Path::PathDrawable:
|
||||
public osg::Drawable
|
||||
{
|
||||
@@ -533,7 +684,9 @@ namespace canvas
|
||||
const Style& parent_style,
|
||||
ElementWeakPtr parent ):
|
||||
Element(canvas, node, parent_style, parent),
|
||||
_path( new PathDrawable(this) )
|
||||
_path( new PathDrawable(this) ),
|
||||
_hasSVG(false),
|
||||
_hasRect(false)
|
||||
{
|
||||
staticInit();
|
||||
|
||||
@@ -561,6 +714,23 @@ namespace canvas
|
||||
_attributes_dirty &= ~(CMDS | COORDS);
|
||||
}
|
||||
|
||||
// SVG path overrides manual cmd/coord specification
|
||||
if ( _hasSVG && (_attributes_dirty & SVG))
|
||||
{
|
||||
CmdList cmds;
|
||||
CoordList coords;
|
||||
parseSVGPathToVGPath(_node->getStringValue("svg"), cmds, coords);
|
||||
_path->setSegments(cmds, coords);
|
||||
_attributes_dirty &= ~SVG;
|
||||
}
|
||||
|
||||
if ( _hasRect &&(_attributes_dirty & RECT))
|
||||
{
|
||||
parseRectToVGPath();
|
||||
_attributes_dirty &= ~RECT;
|
||||
|
||||
}
|
||||
|
||||
Element::update(dt);
|
||||
}
|
||||
|
||||
@@ -630,16 +800,75 @@ namespace canvas
|
||||
childChanged(child);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void Path::setSVGPath(const std::string& svgPath)
|
||||
{
|
||||
_node->setStringValue("svg", svgPath);
|
||||
_hasSVG = true;
|
||||
_attributes_dirty |= SVG;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void Path::setRect(const SGRect<float> &r)
|
||||
{
|
||||
_rect = r;
|
||||
_hasRect = true;
|
||||
_attributes_dirty |= RECT;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void Path::setRoundRect(const SGRect<float> &r, float radiusX, float radiusY)
|
||||
{
|
||||
if (radiusY < 0.0) {
|
||||
radiusY = radiusX;
|
||||
}
|
||||
|
||||
setRect(r);
|
||||
_node->getChild("border-radius", 0, true)->setDoubleValue(radiusX);
|
||||
_node->getChild("border-radius", 1, true)->setDoubleValue(radiusY);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void Path::childChanged(SGPropertyNode* child)
|
||||
{
|
||||
const std::string& name = child->getNameString();
|
||||
const std::string &prName = child->getParent()->getNameString();
|
||||
|
||||
if (simgear::strutils::starts_with(name, "border-"))
|
||||
{
|
||||
_attributes_dirty |= RECT;
|
||||
return;
|
||||
}
|
||||
|
||||
if (prName == "rect") {
|
||||
_hasRect = true;
|
||||
if (name == "left") {
|
||||
_rect.setLeft(child->getDoubleValue());
|
||||
} else if (name == "top") {
|
||||
_rect.setTop(child->getDoubleValue());
|
||||
} else if (name == "right") {
|
||||
_rect.setRight(child->getDoubleValue());
|
||||
} else if (name == "bottom") {
|
||||
_rect.setBottom(child->getDoubleValue());
|
||||
} else if (name == "width") {
|
||||
_rect.setWidth(child->getDoubleValue());
|
||||
} else if (name == "height") {
|
||||
_rect.setHeight(child->getDoubleValue());
|
||||
}
|
||||
_attributes_dirty |= RECT;
|
||||
return;
|
||||
}
|
||||
|
||||
if( child->getParent() != _node )
|
||||
return;
|
||||
|
||||
if( child->getNameString() == "cmd" )
|
||||
if( name == "cmd" )
|
||||
_attributes_dirty |= CMDS;
|
||||
else if( child->getNameString() == "coord" )
|
||||
else if( name == "coord" )
|
||||
_attributes_dirty |= COORDS;
|
||||
else if ( name == "svg")
|
||||
_hasSVG = true;
|
||||
_attributes_dirty |= SVG;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
@@ -664,5 +893,67 @@ namespace canvas
|
||||
return values;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
|
||||
void operator+=(CoordList& base, const std::initializer_list<VGfloat>& other)
|
||||
{
|
||||
base.insert(base.end(), other.begin(), other.end());
|
||||
}
|
||||
|
||||
void Path::parseRectToVGPath()
|
||||
{
|
||||
CmdList commands;
|
||||
CoordList coords;
|
||||
commands.reserve(4);
|
||||
coords.reserve(8);
|
||||
|
||||
bool haveCorner = false;
|
||||
SGVec2f topLeft = parseRectCornerRadius(_node, "left", "top", haveCorner);
|
||||
if (haveCorner) {
|
||||
commands.push_back(VG_MOVE_TO_ABS);
|
||||
coords += {_rect.l(), _rect.t() + topLeft.y()};
|
||||
commands.push_back(VG_SCCWARC_TO_REL);
|
||||
coords += {topLeft.x(), topLeft.y(), 0.0, topLeft.x(), -topLeft.y()};
|
||||
} else {
|
||||
commands.push_back(VG_MOVE_TO_ABS);
|
||||
coords += {_rect.l(), _rect.t()};
|
||||
}
|
||||
|
||||
SGVec2f topRight = parseRectCornerRadius(_node, "right", "top", haveCorner);
|
||||
if (haveCorner) {
|
||||
commands.push_back(VG_HLINE_TO_ABS);
|
||||
coords += {_rect.r() - topRight.x()};
|
||||
commands.push_back(VG_SCCWARC_TO_REL);
|
||||
coords += {topRight.x(), topRight.y(), 0.0, topRight.x(), topRight.y()};
|
||||
} else {
|
||||
commands.push_back(VG_HLINE_TO_ABS);
|
||||
coords += {_rect.r()};
|
||||
}
|
||||
|
||||
SGVec2f bottomRight = parseRectCornerRadius(_node, "right", "bottom", haveCorner);
|
||||
if (haveCorner) {
|
||||
commands.push_back(VG_VLINE_TO_ABS);
|
||||
coords += {_rect.b() - bottomRight.y()};
|
||||
commands.push_back(VG_SCCWARC_TO_REL);
|
||||
coords += {bottomRight.x(), bottomRight.y(), 0.0, -bottomRight.x(), bottomRight.y()};
|
||||
} else {
|
||||
commands.push_back(VG_VLINE_TO_ABS);
|
||||
coords += {_rect.b()};
|
||||
}
|
||||
|
||||
SGVec2f bottomLeft = parseRectCornerRadius(_node, "left", "bottom", haveCorner);
|
||||
if (haveCorner) {
|
||||
commands.push_back(VG_HLINE_TO_ABS);
|
||||
coords += {_rect.l() + bottomLeft.x()};
|
||||
commands.push_back(VG_SCCWARC_TO_REL);
|
||||
coords += {bottomLeft.x(), bottomLeft.y(), 0.0, -bottomLeft.x(), -bottomLeft.y()};
|
||||
} else {
|
||||
commands.push_back(VG_HLINE_TO_ABS);
|
||||
coords += {_rect.l()};
|
||||
}
|
||||
|
||||
commands.push_back(VG_CLOSE_PATH);
|
||||
_path->setSegments(commands, coords);
|
||||
}
|
||||
} // namespace canvas
|
||||
} // namespace simgear
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "CanvasElement.hxx"
|
||||
#include <boost/preprocessor/iteration/iterate.hpp>
|
||||
#include <simgear/math/SGRect.hxx>
|
||||
|
||||
namespace simgear
|
||||
{
|
||||
@@ -67,18 +68,30 @@ namespace canvas
|
||||
/** Close the path (implicit lineTo to first point of path) */
|
||||
Path& close();
|
||||
|
||||
void setSVGPath(const std::string& svgPath);
|
||||
|
||||
void setRect(const SGRect<float>& r);
|
||||
void setRoundRect(const SGRect<float>& r, float radiusX, float radiusY = -1.0);
|
||||
protected:
|
||||
|
||||
enum PathAttributes
|
||||
{
|
||||
CMDS = LAST_ATTRIBUTE << 1,
|
||||
COORDS = CMDS << 1
|
||||
COORDS = CMDS << 1,
|
||||
SVG = COORDS << 1,
|
||||
RECT = SVG << 1
|
||||
};
|
||||
|
||||
class PathDrawable;
|
||||
typedef osg::ref_ptr<PathDrawable> PathDrawableRef;
|
||||
PathDrawableRef _path;
|
||||
|
||||
bool _hasSVG : 1;
|
||||
bool _hasRect : 1;
|
||||
SGRect<float> _rect;
|
||||
|
||||
void parseRectToVGPath();
|
||||
|
||||
virtual void childRemoved(SGPropertyNode * child);
|
||||
virtual void childChanged(SGPropertyNode * child);
|
||||
};
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "CanvasText.hxx"
|
||||
#include <simgear/canvas/Canvas.hxx>
|
||||
#include <simgear/canvas/CanvasSystemAdapter.hxx>
|
||||
@@ -496,18 +497,31 @@ namespace canvas
|
||||
{
|
||||
case LEFT_TO_RIGHT:
|
||||
{
|
||||
osg::Vec2 delta( activefont->getKerning( previous_charcode,
|
||||
charcode,
|
||||
_kerningType ) );
|
||||
#if OSG_VERSION_LESS_THAN(3,5,2)
|
||||
osg::Vec2 delta(activefont->getKerning(previous_charcode,
|
||||
charcode,
|
||||
_kerningType));
|
||||
#else
|
||||
osg::Vec2 delta(activefont->getKerning(_fontSize,
|
||||
previous_charcode,
|
||||
charcode,
|
||||
_kerningType));
|
||||
#endif
|
||||
cursor.x() += delta.x() * wr;
|
||||
cursor.y() += delta.y() * hr;
|
||||
break;
|
||||
}
|
||||
case RIGHT_TO_LEFT:
|
||||
{
|
||||
osg::Vec2 delta( activefont->getKerning( charcode,
|
||||
previous_charcode,
|
||||
_kerningType ) );
|
||||
#if OSG_VERSION_LESS_THAN(3,5,2)
|
||||
osg::Vec2 delta(activefont->getKerning(charcode,
|
||||
previous_charcode,
|
||||
_kerningType));
|
||||
#else
|
||||
osg::Vec2 delta(activefont->getKerning(_fontSize, charcode,
|
||||
previous_charcode,
|
||||
_kerningType));
|
||||
#endif
|
||||
cursor.x() -= delta.x() * wr;
|
||||
cursor.y() -= delta.y() * hr;
|
||||
break;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "CustomEvent.hxx"
|
||||
|
||||
namespace simgear
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "DeviceEvent.hxx"
|
||||
#include <osgGA/GUIEventAdapter>
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "KeyboardEvent.hxx"
|
||||
#include "utf8.h"
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "MouseEvent.hxx"
|
||||
#include <osgGA/GUIEventAdapter>
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "KeyboardEvent.hxx"
|
||||
|
||||
#include <osgViewer/Viewer>
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "BoxLayout.hxx"
|
||||
#include "SpacerItem.hxx"
|
||||
#include <simgear/canvas/Canvas.hxx>
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "Layout.hxx"
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "LayoutItem.hxx"
|
||||
#include <simgear/canvas/Canvas.hxx>
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "NasalWidget.hxx"
|
||||
|
||||
#include <simgear/canvas/Canvas.hxx>
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "SpacerItem.hxx"
|
||||
|
||||
namespace simgear
|
||||
|
||||
@@ -109,10 +109,13 @@
|
||||
# define SG_UNIX
|
||||
#endif
|
||||
|
||||
#if defined( __GNUC__ )
|
||||
# define DEPRECATED __attribute__ ((deprecated))
|
||||
#ifdef __GNUC__
|
||||
#define SG_DEPRECATED(func) func __attribute__ ((deprecated))
|
||||
#elif defined(_MSC_VER)
|
||||
#define SG_DEPRECATED(func) __declspec(deprecated) func
|
||||
#else
|
||||
# define DEPRECATED
|
||||
#pragma message("WARNING: You need to implement SG_DEPRECATED for this compiler")
|
||||
#define SG_DEPRECATED(func) func
|
||||
#endif
|
||||
|
||||
#if defined(__clang__)
|
||||
|
||||
@@ -18,7 +18,8 @@
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
//
|
||||
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include <simgear/debug/BufferedLogCallback.hxx>
|
||||
|
||||
#include <boost/foreach.hpp>
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#define SG_DEBUG_BUFFEREDLOGCALLBACK_HXX
|
||||
|
||||
#include <vector>
|
||||
#include <memory> // for std::auto_ptr
|
||||
#include <memory> // for std::unique_ptr
|
||||
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
|
||||
@@ -70,10 +70,10 @@ public:
|
||||
unsigned int threadsafeCopy(vector_cstring& aOutput);
|
||||
private:
|
||||
class BufferedLogCallbackPrivate;
|
||||
std::auto_ptr<BufferedLogCallbackPrivate> d;
|
||||
std::unique_ptr<BufferedLogCallbackPrivate> d;
|
||||
};
|
||||
|
||||
|
||||
} // of namespace simgear
|
||||
|
||||
#endif // of SG_DEBUG_BUFFEREDLOGCALLBACK_HXX
|
||||
#endif // of SG_DEBUG_BUFFEREDLOGCALLBACK_HXX
|
||||
|
||||
@@ -42,6 +42,10 @@ typedef enum {
|
||||
|
||||
/**
|
||||
* Define the possible logging priorities (and their order).
|
||||
*
|
||||
* Caution - unfortunately, this enum is exposed to Nasal via the logprint()
|
||||
* function as an integer parameter. Therefore, new values should only be
|
||||
* appended, or the priority Nasal reports to compiled code will change.
|
||||
*/
|
||||
typedef enum {
|
||||
SG_BULK = 1, // For frequent messages
|
||||
@@ -49,8 +53,11 @@ typedef enum {
|
||||
SG_INFO, // Informatory messages
|
||||
SG_WARN, // Possible impending problem
|
||||
SG_ALERT, // Very possible impending problem
|
||||
SG_POPUP // Severe enough to alert using a pop-up window
|
||||
SG_POPUP, // Severe enough to alert using a pop-up window
|
||||
// SG_EXIT, // Problem (no core)
|
||||
// SG_ABORT // Abandon ship (core)
|
||||
|
||||
SG_DEV_WARN, // Warning for developers, translated to other priority
|
||||
SG_DEV_ALERT // Alert for developers, translated
|
||||
} sgDebugPriority;
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@
|
||||
#include <simgear/threads/SGQueue.hxx>
|
||||
#include <simgear/threads/SGGuard.hxx>
|
||||
|
||||
#include <simgear/misc/sgstream.hxx>
|
||||
#include <simgear/io/iostreams/sgstream.hxx>
|
||||
#include <simgear/misc/sg_path.hxx>
|
||||
|
||||
#if defined (SG_WINDOWS)
|
||||
@@ -46,39 +46,7 @@
|
||||
#include <io.h>
|
||||
#endif
|
||||
|
||||
const char* debugClassToString(sgDebugClass c)
|
||||
{
|
||||
switch (c) {
|
||||
case SG_NONE: return "none";
|
||||
case SG_TERRAIN: return "terrain";
|
||||
case SG_ASTRO: return "astro";
|
||||
case SG_FLIGHT: return "flight";
|
||||
case SG_INPUT: return "input";
|
||||
case SG_GL: return "opengl";
|
||||
case SG_VIEW: return "view";
|
||||
case SG_COCKPIT: return "cockpit";
|
||||
case SG_GENERAL: return "general";
|
||||
case SG_MATH: return "math";
|
||||
case SG_EVENT: return "event";
|
||||
case SG_AIRCRAFT: return "aircraft";
|
||||
case SG_AUTOPILOT: return "autopilot";
|
||||
case SG_IO: return "io";
|
||||
case SG_CLIPPER: return "clipper";
|
||||
case SG_NETWORK: return "network";
|
||||
case SG_ATC: return "atc";
|
||||
case SG_NASAL: return "nasal";
|
||||
case SG_INSTR: return "instruments";
|
||||
case SG_SYSTEMS: return "systems";
|
||||
case SG_AI: return "ai";
|
||||
case SG_ENVIRONMENT:return "environment";
|
||||
case SG_SOUND: return "sound";
|
||||
case SG_NAVAID: return "navaid";
|
||||
case SG_GUI: return "gui";
|
||||
case SG_TERRASYNC: return "terrasync";
|
||||
case SG_PARTICLES: return "particles";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
@@ -102,6 +70,40 @@ void LogCallback::setLogLevels( sgDebugClass c, sgDebugPriority p )
|
||||
m_class = c;
|
||||
}
|
||||
|
||||
const char* LogCallback::debugClassToString(sgDebugClass c)
|
||||
{
|
||||
switch (c) {
|
||||
case SG_NONE: return "none";
|
||||
case SG_TERRAIN: return "terrain";
|
||||
case SG_ASTRO: return "astro";
|
||||
case SG_FLIGHT: return "flight";
|
||||
case SG_INPUT: return "input";
|
||||
case SG_GL: return "opengl";
|
||||
case SG_VIEW: return "view";
|
||||
case SG_COCKPIT: return "cockpit";
|
||||
case SG_GENERAL: return "general";
|
||||
case SG_MATH: return "math";
|
||||
case SG_EVENT: return "event";
|
||||
case SG_AIRCRAFT: return "aircraft";
|
||||
case SG_AUTOPILOT: return "autopilot";
|
||||
case SG_IO: return "io";
|
||||
case SG_CLIPPER: return "clipper";
|
||||
case SG_NETWORK: return "network";
|
||||
case SG_ATC: return "atc";
|
||||
case SG_NASAL: return "nasal";
|
||||
case SG_INSTR: return "instruments";
|
||||
case SG_SYSTEMS: return "systems";
|
||||
case SG_AI: return "ai";
|
||||
case SG_ENVIRONMENT:return "environment";
|
||||
case SG_SOUND: return "sound";
|
||||
case SG_NAVAID: return "navaid";
|
||||
case SG_GUI: return "gui";
|
||||
case SG_TERRASYNC: return "terrasync";
|
||||
case SG_PARTICLES: return "particles";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
} // of namespace simgear
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
@@ -177,13 +179,13 @@ public:
|
||||
|
||||
#endif
|
||||
|
||||
class LogStreamPrivate : public SGThread
|
||||
class logstream::LogStreamPrivate : public SGThread
|
||||
{
|
||||
private:
|
||||
/**
|
||||
* storage of a single log entry. Note this is not used for a persistent
|
||||
* store, but rather for short term buffering between the submitting
|
||||
* and output threads.
|
||||
* storage of a single log entry. This is used to pass log entries from
|
||||
* the various threads to the logging thread, and also to store the startup
|
||||
* entries
|
||||
*/
|
||||
class LogEntry
|
||||
{
|
||||
@@ -195,19 +197,24 @@ private:
|
||||
{
|
||||
}
|
||||
|
||||
sgDebugClass debugClass;
|
||||
sgDebugPriority debugPriority;
|
||||
const sgDebugClass debugClass;
|
||||
const sgDebugPriority debugPriority;
|
||||
const char* file;
|
||||
int line;
|
||||
std::string message;
|
||||
const int line;
|
||||
const std::string message;
|
||||
};
|
||||
|
||||
/**
|
||||
* RAII object to pause the logging thread if it's running, and restart it.
|
||||
* used to safely make configuration changes.
|
||||
*/
|
||||
class PauseThread
|
||||
{
|
||||
public:
|
||||
PauseThread(LogStreamPrivate* parent) : m_parent(parent)
|
||||
PauseThread(LogStreamPrivate* parent)
|
||||
: m_parent(parent)
|
||||
, m_wasRunning(m_parent->stop())
|
||||
{
|
||||
m_wasRunning = m_parent->stop();
|
||||
}
|
||||
|
||||
~PauseThread()
|
||||
@@ -218,17 +225,13 @@ private:
|
||||
}
|
||||
private:
|
||||
LogStreamPrivate* m_parent;
|
||||
bool m_wasRunning;
|
||||
const bool m_wasRunning;
|
||||
};
|
||||
|
||||
public:
|
||||
LogStreamPrivate() :
|
||||
m_logClass(SG_ALL),
|
||||
m_logPriority(SG_ALERT),
|
||||
#if defined (SG_WINDOWS)
|
||||
m_stdout_isRedirectedAlready(false),
|
||||
m_stderr_isRedirectedAlready(false),
|
||||
#endif
|
||||
m_isRunning(false)
|
||||
m_logPriority(SG_ALERT)
|
||||
{
|
||||
#if defined (SG_WINDOWS)
|
||||
/*
|
||||
@@ -332,7 +335,7 @@ public:
|
||||
|
||||
~LogStreamPrivate()
|
||||
{
|
||||
BOOST_FOREACH(simgear::LogCallback* cb, m_callbacks) {
|
||||
for (simgear::LogCallback* cb : m_callbacks) {
|
||||
delete cb;
|
||||
}
|
||||
}
|
||||
@@ -340,6 +343,10 @@ public:
|
||||
SGMutex m_lock;
|
||||
SGBlockingQueue<LogEntry> m_entries;
|
||||
|
||||
// log entries posted during startup
|
||||
std::vector<LogEntry> m_startupEntries;
|
||||
bool m_startupLogging = false;
|
||||
|
||||
typedef std::vector<simgear::LogCallback*> CallbackVec;
|
||||
CallbackVec m_callbacks;
|
||||
/// subset of callbacks which correspond to stdout / console,
|
||||
@@ -348,12 +355,13 @@ public:
|
||||
|
||||
sgDebugClass m_logClass;
|
||||
sgDebugPriority m_logPriority;
|
||||
bool m_isRunning;
|
||||
bool m_isRunning = false;
|
||||
#if defined (SG_WINDOWS)
|
||||
// track whether the console was redirected on launch (in the constructor, which is called early on)
|
||||
bool m_stderr_isRedirectedAlready;
|
||||
bool m_stdout_isRedirectedAlready;
|
||||
bool m_stderr_isRedirectedAlready = false;
|
||||
bool m_stdout_isRedirectedAlready = false;
|
||||
#endif
|
||||
bool m_developerMode = false;
|
||||
|
||||
void startLog()
|
||||
{
|
||||
@@ -363,6 +371,16 @@ public:
|
||||
start();
|
||||
}
|
||||
|
||||
void setStartupLoggingEnabled(bool on)
|
||||
{
|
||||
if (m_startupLogging == on) {
|
||||
return;
|
||||
}
|
||||
|
||||
m_startupLogging = on;
|
||||
m_startupEntries.clear();
|
||||
}
|
||||
|
||||
virtual void run()
|
||||
{
|
||||
while (1) {
|
||||
@@ -373,8 +391,14 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
if (m_startupLogging) {
|
||||
// save to the startup list for not-yet-added callbacks to
|
||||
// pull down on startup
|
||||
m_startupEntries.push_back(entry);
|
||||
}
|
||||
|
||||
// submit to each installed callback in turn
|
||||
BOOST_FOREACH(simgear::LogCallback* cb, m_callbacks) {
|
||||
for (simgear::LogCallback* cb : m_callbacks) {
|
||||
(*cb)(entry.debugClass, entry.debugPriority,
|
||||
entry.file, entry.line, entry.message);
|
||||
}
|
||||
@@ -401,6 +425,13 @@ public:
|
||||
{
|
||||
PauseThread pause(this);
|
||||
m_callbacks.push_back(cb);
|
||||
|
||||
// we clear startup entries not using this, so always safe to run
|
||||
// this code, container will simply be empty
|
||||
for (auto entry : m_startupEntries) {
|
||||
(*cb)(entry.debugClass, entry.debugPriority,
|
||||
entry.file, entry.line, entry.message);
|
||||
}
|
||||
}
|
||||
|
||||
void removeCallback(simgear::LogCallback* cb)
|
||||
@@ -424,6 +455,7 @@ public:
|
||||
|
||||
bool would_log( sgDebugClass c, sgDebugPriority p ) const
|
||||
{
|
||||
p = translatePriority(p);
|
||||
if (p >= SG_INFO) return true;
|
||||
return ((c & m_logClass) != 0 && p >= m_logPriority);
|
||||
}
|
||||
@@ -431,53 +463,126 @@ public:
|
||||
void log( sgDebugClass c, sgDebugPriority p,
|
||||
const char* fileName, int line, const std::string& msg)
|
||||
{
|
||||
p = translatePriority(p);
|
||||
LogEntry entry(c, p, fileName, line, msg);
|
||||
m_entries.push(entry);
|
||||
}
|
||||
|
||||
sgDebugPriority translatePriority(sgDebugPriority in) const
|
||||
{
|
||||
if (in == SG_DEV_WARN) {
|
||||
return m_developerMode ? SG_WARN : SG_DEBUG;
|
||||
}
|
||||
|
||||
if (in == SG_DEV_ALERT) {
|
||||
return m_developerMode ? SG_POPUP : SG_WARN;
|
||||
}
|
||||
|
||||
return in;
|
||||
}
|
||||
};
|
||||
|
||||
/////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static logstream* global_logstream = NULL;
|
||||
static LogStreamPrivate* global_privateLogstream = NULL;
|
||||
static std::unique_ptr<logstream> global_logstream;
|
||||
static SGMutex global_logStreamLock;
|
||||
|
||||
logstream::logstream()
|
||||
{
|
||||
global_privateLogstream = new LogStreamPrivate;
|
||||
global_privateLogstream->startLog();
|
||||
d.reset(new LogStreamPrivate);
|
||||
d->startLog();
|
||||
}
|
||||
|
||||
logstream::~logstream()
|
||||
{
|
||||
popup_msgs.clear();
|
||||
global_privateLogstream->stop();
|
||||
delete global_privateLogstream;
|
||||
d->stop();
|
||||
}
|
||||
|
||||
void
|
||||
logstream::setLogLevels( sgDebugClass c, sgDebugPriority p )
|
||||
{
|
||||
global_privateLogstream->setLogLevels(c, p);
|
||||
d->setLogLevels(c, p);
|
||||
}
|
||||
|
||||
void logstream::setDeveloperMode(bool devMode)
|
||||
{
|
||||
d->m_developerMode = devMode;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
logstream::addCallback(simgear::LogCallback* cb)
|
||||
{
|
||||
global_privateLogstream->addCallback(cb);
|
||||
d->addCallback(cb);
|
||||
}
|
||||
|
||||
void
|
||||
logstream::removeCallback(simgear::LogCallback* cb)
|
||||
{
|
||||
global_privateLogstream->removeCallback(cb);
|
||||
d->removeCallback(cb);
|
||||
}
|
||||
|
||||
void
|
||||
logstream::log( sgDebugClass c, sgDebugPriority p,
|
||||
const char* fileName, int line, const std::string& msg)
|
||||
{
|
||||
global_privateLogstream->log(c, p, fileName, line, msg);
|
||||
d->log(c, p, fileName, line, msg);
|
||||
}
|
||||
|
||||
|
||||
void logstream::hexdump(sgDebugClass c, sgDebugPriority p, const char* fileName, int line, const void *mem, unsigned int len, int columns)
|
||||
{
|
||||
unsigned int i, j;
|
||||
char temp[3000], temp1[3000];
|
||||
*temp = 0;
|
||||
|
||||
for (i = 0; i < len + ((len % columns) ? (columns - len % columns) : 0); i++)
|
||||
{
|
||||
if (strlen(temp) > 500) return;
|
||||
/* print offset */
|
||||
if (i % columns == 0)
|
||||
{
|
||||
sprintf(temp1, "0x%06x: ", i);
|
||||
strcat(temp, temp1);
|
||||
}
|
||||
|
||||
/* print hex data */
|
||||
if (i < len)
|
||||
{
|
||||
sprintf(temp1, "%02x ", 0xFF & ((char*)mem)[i]);
|
||||
strcat(temp, temp1);
|
||||
}
|
||||
else /* end of block, just aligning for ASCII dump */
|
||||
{
|
||||
strcat(temp, " ");
|
||||
}
|
||||
|
||||
/* print ASCII dump */
|
||||
if (i % columns == (columns - 1))
|
||||
{
|
||||
for (j = i - (columns - 1); j <= i; j++)
|
||||
{
|
||||
if (j >= len) /* end of block, not really printing */
|
||||
{
|
||||
strcat(temp, " ");
|
||||
}
|
||||
else if (((((char*)mem)[j]) & (char)0x7f) > 32) /* printable char */
|
||||
{
|
||||
char t2[2];
|
||||
t2[0] = 0xFF & ((char*)mem)[j];
|
||||
t2[1] = 0;
|
||||
strcat(temp, t2);
|
||||
}
|
||||
else /* other char */
|
||||
{
|
||||
strcat(temp, ".");
|
||||
}
|
||||
}
|
||||
log(c, p, fileName, line, temp );
|
||||
*temp = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
@@ -507,31 +612,31 @@ logstream::has_popup()
|
||||
bool
|
||||
logstream::would_log( sgDebugClass c, sgDebugPriority p ) const
|
||||
{
|
||||
return global_privateLogstream->would_log(c,p);
|
||||
return d->would_log(c,p);
|
||||
}
|
||||
|
||||
sgDebugClass
|
||||
logstream::get_log_classes() const
|
||||
{
|
||||
return global_privateLogstream->m_logClass;
|
||||
return d->m_logClass;
|
||||
}
|
||||
|
||||
sgDebugPriority
|
||||
logstream::get_log_priority() const
|
||||
{
|
||||
return global_privateLogstream->m_logPriority;
|
||||
return d->m_logPriority;
|
||||
}
|
||||
|
||||
void
|
||||
logstream::set_log_priority( sgDebugPriority p)
|
||||
{
|
||||
global_privateLogstream->setLogLevels(global_privateLogstream->m_logClass, p);
|
||||
d->setLogLevels(d->m_logClass, p);
|
||||
}
|
||||
|
||||
void
|
||||
logstream::set_log_classes( sgDebugClass c)
|
||||
{
|
||||
global_privateLogstream->setLogLevels(c, global_privateLogstream->m_logPriority);
|
||||
d->setLogLevels(c, d->m_logPriority);
|
||||
}
|
||||
|
||||
|
||||
@@ -547,49 +652,54 @@ sglog()
|
||||
SGGuard<SGMutex> g(global_logStreamLock);
|
||||
|
||||
if( !global_logstream )
|
||||
global_logstream = new logstream();
|
||||
return *global_logstream;
|
||||
global_logstream.reset(new logstream);
|
||||
return *(global_logstream.get());
|
||||
}
|
||||
|
||||
void
|
||||
logstream::logToFile( const SGPath& aPath, sgDebugClass c, sgDebugPriority p )
|
||||
{
|
||||
global_privateLogstream->addCallback(new FileLogCallback(aPath, c, p));
|
||||
d->addCallback(new FileLogCallback(aPath, c, p));
|
||||
}
|
||||
|
||||
namespace simgear
|
||||
void logstream::setStartupLoggingEnabled(bool enabled)
|
||||
{
|
||||
d->setStartupLoggingEnabled(enabled);
|
||||
}
|
||||
|
||||
void requestConsole()
|
||||
{
|
||||
void logstream::requestConsole()
|
||||
{
|
||||
#if defined (SG_WINDOWS)
|
||||
/*
|
||||
* 2016-09-20(RJH) - Reworked console handling
|
||||
* This is part of the reworked console handling for Win32. This is for building as a Win32 GUI Subsystem where no
|
||||
* console is allocated on launch. If building as a console app then the startup will ensure that a console is created - but
|
||||
* we don't need to handle that.
|
||||
* The new handling is quite simple:
|
||||
* 1. The constructor will ensure that these streams exists. It will attach to the
|
||||
* parent command prompt if started from the command prompt, otherwise the
|
||||
* stdout/stderr will be bound to the NUL device.
|
||||
* 2. with --console a window will always appear regardless of where the process was
|
||||
* started from. Any non redirected streams will be redirected
|
||||
* 3. You cannot use --console and either redirected stream.
|
||||
*
|
||||
* This is called after the Private Log Stream constructor so we need to undo any console that it has attached to.
|
||||
*/
|
||||
const bool stderrAlreadyRedirected = d->m_stderr_isRedirectedAlready;
|
||||
const bool stdoutAlreadyRedirected = d->m_stdout_isRedirectedAlready;
|
||||
|
||||
if (!global_privateLogstream->m_stderr_isRedirectedAlready && !global_privateLogstream->m_stdout_isRedirectedAlready) {
|
||||
/*
|
||||
* 2016-09-20(RJH) - Reworked console handling
|
||||
* This is part of the reworked console handling for Win32. This is for building as a Win32 GUI Subsystem where no
|
||||
* console is allocated on launch. If building as a console app then the startup will ensure that a console is created - but
|
||||
* we don't need to handle that.
|
||||
* The new handling is quite simple:
|
||||
* 1. The constructor will ensure that these streams exists. It will attach to the
|
||||
* parent command prompt if started from the command prompt, otherwise the
|
||||
* stdout/stderr will be bound to the NUL device.
|
||||
* 2. with --console a window will always appear regardless of where the process was
|
||||
* started from. Any non redirected streams will be redirected
|
||||
* 3. You cannot use --console and either redirected stream.
|
||||
*
|
||||
* This is called after the Private Log Stream constructor so we need to undo any console that it has attached to.
|
||||
*/
|
||||
|
||||
if (!stderrAlreadyRedirected && !stdoutAlreadyRedirected) {
|
||||
FreeConsole();
|
||||
if (AllocConsole()) {
|
||||
if (!global_privateLogstream->m_stdout_isRedirectedAlready)
|
||||
if (!stdoutAlreadyRedirected)
|
||||
freopen("conout$", "w", stdout);
|
||||
|
||||
if (!global_privateLogstream->m_stderr_isRedirectedAlready)
|
||||
if (!stderrAlreadyRedirected)
|
||||
freopen("conout$", "w", stderr);
|
||||
|
||||
//http://stackoverflow.com/a/25927081
|
||||
//Clear the error state for each of the C++ standard stream objects.
|
||||
//Clear the error state for each of the C++ standard stream objects.
|
||||
std::wcout.clear();
|
||||
std::cout.clear();
|
||||
std::wcerr.clear();
|
||||
@@ -602,11 +712,19 @@ void requestConsole()
|
||||
}
|
||||
|
||||
|
||||
namespace simgear
|
||||
{
|
||||
|
||||
void requestConsole()
|
||||
{
|
||||
sglog().requestConsole();
|
||||
}
|
||||
|
||||
|
||||
void shutdownLogging()
|
||||
{
|
||||
SGGuard<SGMutex> g(global_logStreamLock);
|
||||
delete global_logstream;
|
||||
global_logstream = 0;
|
||||
global_logstream.reset();
|
||||
}
|
||||
|
||||
} // of namespace simgear
|
||||
|
||||
@@ -30,7 +30,8 @@
|
||||
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
|
||||
#include <memory>
|
||||
|
||||
// forward decls
|
||||
class SGPath;
|
||||
|
||||
@@ -49,6 +50,8 @@ protected:
|
||||
LogCallback(sgDebugClass c, sgDebugPriority p);
|
||||
|
||||
bool shouldLog(sgDebugClass c, sgDebugPriority p) const;
|
||||
|
||||
static const char* debugClassToString(sgDebugClass c);
|
||||
private:
|
||||
sgDebugClass m_class;
|
||||
sgDebugPriority m_priority;
|
||||
@@ -74,6 +77,14 @@ public:
|
||||
~logstream();
|
||||
|
||||
static void initGlobalLogstream();
|
||||
|
||||
/**
|
||||
* Helper force a console on platforms where it might optional, when
|
||||
* we need to show a console. This basically means Windows at the
|
||||
* moment - on other plaforms it's a no-op
|
||||
*/
|
||||
void requestConsole();
|
||||
|
||||
/**
|
||||
* Set the global log class and priority level.
|
||||
* @param c debug class
|
||||
@@ -93,12 +104,25 @@ public:
|
||||
|
||||
sgDebugPriority get_log_priority() const;
|
||||
|
||||
/**
|
||||
* set developer mode on/off. In developer mode, SG_DEV_WARN messags
|
||||
* are treated as warnings. In normal (non-developer) mode they are
|
||||
* treated as SG_DEBUG.
|
||||
*/
|
||||
void setDeveloperMode(bool devMode);
|
||||
|
||||
/**
|
||||
* the core logging method
|
||||
*/
|
||||
void log( sgDebugClass c, sgDebugPriority p,
|
||||
const char* fileName, int line, const std::string& msg);
|
||||
|
||||
/**
|
||||
* output formatted hex dump of memory block
|
||||
*/
|
||||
void hexdump(sgDebugClass c, sgDebugPriority p, const char* fileName, int line, const void *mem, unsigned int len, int columns = 16);
|
||||
|
||||
|
||||
/**
|
||||
* support for the SG_POPUP logging class
|
||||
* set the content of the popup message
|
||||
@@ -134,11 +158,21 @@ public:
|
||||
|
||||
void removeCallback(simgear::LogCallback* cb);
|
||||
|
||||
/**
|
||||
* optionally record all entries and submit them to new log callbacks that
|
||||
* are added. This allows simplified logging configuration, but still including
|
||||
* early startup information in all logs.
|
||||
*/
|
||||
void setStartupLoggingEnabled(bool enabled);
|
||||
private:
|
||||
// constructor
|
||||
logstream();
|
||||
|
||||
std::vector<std::string> popup_msgs;
|
||||
|
||||
class LogStreamPrivate;
|
||||
|
||||
std::unique_ptr<LogStreamPrivate> d;
|
||||
};
|
||||
|
||||
logstream& sglog();
|
||||
@@ -155,12 +189,14 @@ logstream& sglog();
|
||||
do { if(sglog().would_log(C,P)) { \
|
||||
std::ostringstream os; os << M; \
|
||||
sglog().log(C, P, __FILE__, __LINE__, os.str()); \
|
||||
if (P == SG_POPUP) sglog().popup(os.str()); \
|
||||
if ((P) == SG_POPUP) sglog().popup(os.str()); \
|
||||
} } while(0)
|
||||
#ifdef FG_NDEBUG
|
||||
# define SG_LOG(C,P,M) do { if(P == SG_POPUP) SG_LOGX(C,P,M) } while(0)
|
||||
# define SG_LOG(C,P,M) do { if((P) == SG_POPUP) SG_LOGX(C,P,M) } while(0)
|
||||
# define SG_HEXDUMP(C,P,MEM,LEN)
|
||||
#else
|
||||
# define SG_LOG(C,P,M) SG_LOGX(C,P,M)
|
||||
# define SG_LOG_HEXDUMP(C,P,MEM,LEN) if(sglog().would_log(C,P)) sglog().hexdump(C, P, __FILE__, __LINE__, MEM, LEN)
|
||||
#endif
|
||||
|
||||
#define SG_ORIGIN __FILE__ ":" SG_STRINGIZE(__LINE__)
|
||||
|
||||
@@ -10,13 +10,14 @@ if(ENABLE_TESTS)
|
||||
add_executable(test_metar test_metar.cxx)
|
||||
|
||||
if (SIMGEAR_SHARED)
|
||||
target_link_libraries(test_metar SimGearScene)
|
||||
target_link_libraries(test_metar SimGearScene ${GDAL_LIBRARY})
|
||||
else()
|
||||
target_link_libraries(test_metar
|
||||
SimGearScene SimGearCore
|
||||
${CMAKE_THREAD_LIBS_INIT}
|
||||
${ZLIB_LIBRARY}
|
||||
${RT_LIBRARY})
|
||||
${RT_LIBRARY}
|
||||
${GDAL_LIBRARY})
|
||||
endif()
|
||||
|
||||
add_test(metar ${EXECUTABLE_OUTPUT_PATH}/test_metar)
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#endif
|
||||
|
||||
#include <simgear/compiler.h>
|
||||
#include <simgear/misc/test_macros.hxx>
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdlib>
|
||||
@@ -23,75 +24,56 @@ using std::cerr;
|
||||
using std::endl;
|
||||
using std::string;
|
||||
|
||||
#define COMPARE(a, b) \
|
||||
if ((a) != (b)) { \
|
||||
cerr << "failed:" << #a << " != " << #b << endl; \
|
||||
cerr << "\tgot:" << a << endl; \
|
||||
exit(1); \
|
||||
}
|
||||
|
||||
#define FUZZY_COMPARE(a, b, epsilon) \
|
||||
if (fabs(a - b) > epsilon) { \
|
||||
cerr << "failed:" << #a << " != " << #b << endl; \
|
||||
cerr << "\tgot:" << a << endl; \
|
||||
cerr << "\tepsilon:" << epsilon << endl; \
|
||||
}
|
||||
|
||||
#define VERIFY(a) \
|
||||
if (!(a)) { \
|
||||
cerr << "failed:" << #a << endl; \
|
||||
exit(1); \
|
||||
}
|
||||
|
||||
const double TEST_EPSILON = 1e-9;
|
||||
|
||||
void test_basic()
|
||||
{
|
||||
SGMetar m1("2011/10/20 11:25 EHAM 201125Z 27012KT 240V300 9999 VCSH FEW025CB SCT048 10/05 Q1025 TEMPO VRB03KT");
|
||||
COMPARE(m1.getYear(), 2011);
|
||||
COMPARE(m1.getMonth(), 10);
|
||||
COMPARE(m1.getDay(), 20);
|
||||
COMPARE(m1.getHour(), 11);
|
||||
COMPARE(m1.getMinute(), 25);
|
||||
COMPARE(m1.getReportType(), -1); // should default to NIL?
|
||||
|
||||
COMPARE(m1.getWindDir(), 270);
|
||||
FUZZY_COMPARE(m1.getWindSpeed_kt(), 12, TEST_EPSILON);
|
||||
|
||||
COMPARE(m1.getWeather().size(), 1);
|
||||
COMPARE(m1.getClouds().size(), 2);
|
||||
SG_CHECK_EQUAL(m1.getYear(), 2011);
|
||||
SG_CHECK_EQUAL(m1.getMonth(), 10);
|
||||
SG_CHECK_EQUAL(m1.getDay(), 20);
|
||||
SG_CHECK_EQUAL(m1.getHour(), 11);
|
||||
SG_CHECK_EQUAL(m1.getMinute(), 25);
|
||||
SG_CHECK_EQUAL(m1.getReportType(), -1); // should default to NIL?
|
||||
|
||||
FUZZY_COMPARE(m1.getTemperature_C(), 10, TEST_EPSILON);
|
||||
FUZZY_COMPARE(m1.getDewpoint_C(), 5, TEST_EPSILON);
|
||||
FUZZY_COMPARE(m1.getPressure_hPa(), 1025, TEST_EPSILON);
|
||||
SG_CHECK_EQUAL(m1.getWindDir(), 270);
|
||||
SG_CHECK_EQUAL_EP2(m1.getWindSpeed_kt(), 12, TEST_EPSILON);
|
||||
|
||||
SG_CHECK_EQUAL(m1.getWeather().size(), 1);
|
||||
SG_CHECK_EQUAL(m1.getClouds().size(), 2);
|
||||
|
||||
SG_CHECK_EQUAL_EP2(m1.getTemperature_C(), 10, TEST_EPSILON);
|
||||
SG_CHECK_EQUAL_EP2(m1.getDewpoint_C(), 5, TEST_EPSILON);
|
||||
SG_CHECK_EQUAL_EP2(m1.getPressure_hPa(), 1025, TEST_EPSILON);
|
||||
}
|
||||
|
||||
void test_sensor_failure_weather()
|
||||
{
|
||||
SGMetar m1("2011/10/20 11:25 EHAM 201125Z 27012KT 240V300 9999 // FEW025CB SCT048 10/05 Q1025");
|
||||
COMPARE(m1.getWindDir(), 270);
|
||||
FUZZY_COMPARE(m1.getWindSpeed_kt(), 12, TEST_EPSILON);
|
||||
SG_CHECK_EQUAL(m1.getWindDir(), 270);
|
||||
SG_CHECK_EQUAL_EP2(m1.getWindSpeed_kt(), 12, TEST_EPSILON);
|
||||
|
||||
COMPARE(m1.getWeather().size(), 0);
|
||||
COMPARE(m1.getClouds().size(), 2);
|
||||
SG_CHECK_EQUAL(m1.getWeather().size(), 0);
|
||||
SG_CHECK_EQUAL(m1.getClouds().size(), 2);
|
||||
|
||||
FUZZY_COMPARE(m1.getTemperature_C(), 10, TEST_EPSILON);
|
||||
FUZZY_COMPARE(m1.getDewpoint_C(), 5, TEST_EPSILON);
|
||||
FUZZY_COMPARE(m1.getPressure_hPa(), 1025, TEST_EPSILON);
|
||||
SG_CHECK_EQUAL_EP2(m1.getTemperature_C(), 10, TEST_EPSILON);
|
||||
SG_CHECK_EQUAL_EP2(m1.getDewpoint_C(), 5, TEST_EPSILON);
|
||||
SG_CHECK_EQUAL_EP2(m1.getPressure_hPa(), 1025, TEST_EPSILON);
|
||||
}
|
||||
|
||||
void test_sensor_failure_cloud()
|
||||
{
|
||||
SGMetar m1("2011/10/20 11:25 EHAM 201125Z 27012KT 240V300 9999 FEW025CB/// SCT048/// 10/05 Q1025");
|
||||
COMPARE(m1.getWindDir(), 270);
|
||||
FUZZY_COMPARE(m1.getWindSpeed_kt(), 12, TEST_EPSILON);
|
||||
SG_CHECK_EQUAL(m1.getWindDir(), 270);
|
||||
SG_CHECK_EQUAL_EP2(m1.getWindSpeed_kt(), 12, TEST_EPSILON);
|
||||
|
||||
COMPARE(m1.getWeather().size(), 0);
|
||||
COMPARE(m1.getClouds().size(), 2);
|
||||
SG_CHECK_EQUAL(m1.getWeather().size(), 0);
|
||||
SG_CHECK_EQUAL(m1.getClouds().size(), 2);
|
||||
|
||||
FUZZY_COMPARE(m1.getTemperature_C(), 10, TEST_EPSILON);
|
||||
FUZZY_COMPARE(m1.getDewpoint_C(), 5, TEST_EPSILON);
|
||||
FUZZY_COMPARE(m1.getPressure_hPa(), 1025, TEST_EPSILON);
|
||||
SG_CHECK_EQUAL_EP2(m1.getTemperature_C(), 10, TEST_EPSILON);
|
||||
SG_CHECK_EQUAL_EP2(m1.getDewpoint_C(), 5, TEST_EPSILON);
|
||||
SG_CHECK_EQUAL_EP2(m1.getPressure_hPa(), 1025, TEST_EPSILON);
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
* $Id$
|
||||
**************************************************************************/
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
|
||||
#include <math.h>
|
||||
|
||||
@@ -22,7 +22,7 @@
|
||||
* $Id$
|
||||
**************************************************************************/
|
||||
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
* $Id$
|
||||
**************************************************************************/
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include <cmath>
|
||||
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
|
||||
@@ -26,7 +26,7 @@
|
||||
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
#include <simgear/misc/sg_path.hxx>
|
||||
#include <simgear/misc/sgstream.hxx>
|
||||
#include <simgear/io/iostreams/sgstream.hxx>
|
||||
|
||||
#include "stardata.hxx"
|
||||
|
||||
|
||||
@@ -117,7 +117,7 @@ public:
|
||||
// bool unconditionalAttributeOwnershipDivestiture(const RTIHandle& objectHandle, const RTIHandleSet& attributeHandles)
|
||||
// {
|
||||
// try {
|
||||
// std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// std::unique_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// for (RTIHandleSet::const_iterator i = attributeHandles.begin(); i != attributeHandles.end(); ++i)
|
||||
// attributeHandleSet->add(*i);
|
||||
// _rtiAmbassador.unconditionalAttributeOwnershipDivestiture(objectHandle, *attributeHandleSet);
|
||||
@@ -136,7 +136,7 @@ public:
|
||||
// bool negotiatedAttributeOwnershipDivestiture(const RTIHandle& objectHandle, const RTIHandleSet& attributeHandles, const RTIData& tag)
|
||||
// {
|
||||
// try {
|
||||
// std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// std::unique_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// for (RTIHandleSet::const_iterator i = attributeHandles.begin(); i != attributeHandles.end(); ++i)
|
||||
// attributeHandleSet->add(*i);
|
||||
// _rtiAmbassador.negotiatedAttributeOwnershipDivestiture(objectHandle, *attributeHandleSet, tag.data());
|
||||
@@ -156,7 +156,7 @@ public:
|
||||
// bool attributeOwnershipAcquisition(const RTIHandle& objectHandle, const RTIHandleSet& attributeHandles, const RTIData& tag)
|
||||
// {
|
||||
// try {
|
||||
// std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// std::unique_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// for (RTIHandleSet::const_iterator i = attributeHandles.begin(); i != attributeHandles.end(); ++i)
|
||||
// attributeHandleSet->add(*i);
|
||||
// _rtiAmbassador.attributeOwnershipAcquisition(objectHandle, *attributeHandleSet, tag.data());
|
||||
@@ -177,7 +177,7 @@ public:
|
||||
// bool attributeOwnershipAcquisitionIfAvailable(const RTIHandle& objectHandle, const RTIHandleSet& attributeHandles)
|
||||
// {
|
||||
// try {
|
||||
// std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// std::unique_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// for (RTIHandleSet::const_iterator i = attributeHandles.begin(); i != attributeHandles.end(); ++i)
|
||||
// attributeHandleSet->add(*i);
|
||||
// _rtiAmbassador.attributeOwnershipAcquisitionIfAvailable(objectHandle, *attributeHandleSet);
|
||||
@@ -199,7 +199,7 @@ public:
|
||||
// RTIHandleSet attributeOwnershipReleaseResponse(const RTIHandle& objectHandle, const RTIHandleSet& attributeHandles)
|
||||
// {
|
||||
// try {
|
||||
// std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// std::unique_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// for (RTIHandleSet::const_iterator i = attributeHandles.begin(); i != attributeHandles.end(); ++i)
|
||||
// attributeHandleSet->add(*i);
|
||||
// attributeHandleSet.reset(_rtiAmbassador.attributeOwnershipReleaseResponse(objectHandle, *attributeHandleSet));
|
||||
@@ -223,7 +223,7 @@ public:
|
||||
// bool cancelNegotiatedAttributeOwnershipDivestiture(const RTIHandle& objectHandle, const RTIHandleSet& attributeHandles)
|
||||
// {
|
||||
// try {
|
||||
// std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// std::unique_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// for (RTIHandleSet::const_iterator i = attributeHandles.begin(); i != attributeHandles.end(); ++i)
|
||||
// attributeHandleSet->add(*i);
|
||||
// _rtiAmbassador.cancelNegotiatedAttributeOwnershipDivestiture(objectHandle, *attributeHandleSet);
|
||||
@@ -243,7 +243,7 @@ public:
|
||||
// bool cancelAttributeOwnershipAcquisition(const RTIHandle& objectHandle, const RTIHandleSet& attributeHandles)
|
||||
// {
|
||||
// try {
|
||||
// std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// std::unique_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(attributeHandles.size()));
|
||||
// for (RTIHandleSet::const_iterator i = attributeHandles.begin(); i != attributeHandles.end(); ++i)
|
||||
// attributeHandleSet->add(*i);
|
||||
// _rtiAmbassador.cancelAttributeOwnershipAcquisition(objectHandle, *attributeHandleSet);
|
||||
|
||||
@@ -101,7 +101,7 @@ RTI13ObjectClass::publish(const HLAIndexList& indexList)
|
||||
|
||||
try {
|
||||
unsigned numAttributes = getNumAttributes();
|
||||
std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(numAttributes));
|
||||
std::unique_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(numAttributes));
|
||||
for (HLAIndexList::const_iterator i = indexList.begin(); i != indexList.end(); ++i) {
|
||||
if (_attributeHandleVector.size() <= *i) {
|
||||
SG_LOG(SG_NETWORK, SG_WARN, "RTI13ObjectClass::publish(): Invalid attribute index!");
|
||||
@@ -195,7 +195,7 @@ RTI13ObjectClass::subscribe(const HLAIndexList& indexList, bool active)
|
||||
|
||||
try {
|
||||
unsigned numAttributes = getNumAttributes();
|
||||
std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(numAttributes));
|
||||
std::unique_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(numAttributes));
|
||||
for (HLAIndexList::const_iterator i = indexList.begin(); i != indexList.end(); ++i) {
|
||||
if (_attributeHandleVector.size() <= *i) {
|
||||
SG_LOG(SG_NETWORK, SG_WARN, "RTI13ObjectClass::subscribe(): Invalid attribute index!");
|
||||
|
||||
@@ -243,7 +243,7 @@ RTI13ObjectInstance::requestObjectAttributeValueUpdate(const HLAIndexList& index
|
||||
|
||||
try {
|
||||
unsigned numAttributes = getNumAttributes();
|
||||
std::auto_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(numAttributes));
|
||||
std::unique_ptr<RTI::AttributeHandleSet> attributeHandleSet(RTI::AttributeHandleSetFactory::create(numAttributes));
|
||||
for (HLAIndexList::const_iterator i = indexList.begin(); i != indexList.end(); ++i) {
|
||||
if (getAttributeOwned(*i)) {
|
||||
SG_LOG(SG_NETWORK, SG_WARN, "RTI13ObjectInstance::requestObjectAttributeValueUpdate(): "
|
||||
|
||||
@@ -101,7 +101,7 @@ private:
|
||||
SGSharedPtr<RTI13Ambassador> _ambassador;
|
||||
|
||||
// cached storage for updates
|
||||
std::auto_ptr<RTI::AttributeHandleValuePairSet> _attributeValuePairSet;
|
||||
std::unique_ptr<RTI::AttributeHandleValuePairSet> _attributeValuePairSet;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
|
||||
add_subdirectory(iostreams)
|
||||
|
||||
include (SimGearComponent)
|
||||
|
||||
|
||||
@@ -21,8 +21,9 @@
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
//
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "DNSClient.hxx"
|
||||
#include "udns.h"
|
||||
#include <udns.h>
|
||||
#include <time.h>
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
|
||||
@@ -33,18 +34,33 @@ namespace DNS {
|
||||
class Client::ClientPrivate {
|
||||
public:
|
||||
ClientPrivate() {
|
||||
if (dns_init(NULL, 0) < 0)
|
||||
SG_LOG(SG_IO, SG_ALERT, "Can't init udns library" );
|
||||
|
||||
if( dns_open(NULL) < 0 )
|
||||
if( instanceCounter++ == 0 )
|
||||
if (dns_init(NULL, 0) < 0)
|
||||
SG_LOG(SG_IO, SG_ALERT, "Can't init udns library" );
|
||||
|
||||
ctx = dns_new(NULL);
|
||||
|
||||
if (dns_init(ctx, 0) < 0)
|
||||
SG_LOG(SG_IO, SG_ALERT, "Can't create udns context" );
|
||||
|
||||
if( dns_open(ctx) < 0 )
|
||||
SG_LOG(SG_IO, SG_ALERT, "Can't open udns context" );
|
||||
}
|
||||
|
||||
~ClientPrivate() {
|
||||
dns_close(NULL);
|
||||
dns_close(ctx);
|
||||
dns_free(ctx);
|
||||
if( --instanceCounter == 0 )
|
||||
dns_close(NULL);
|
||||
}
|
||||
|
||||
struct dns_ctx * ctx;
|
||||
static size_t instanceCounter;
|
||||
};
|
||||
|
||||
size_t Client::ClientPrivate::instanceCounter = 0;
|
||||
|
||||
Request::Request( const std::string & dn ) :
|
||||
_dn(dn),
|
||||
_type(DNS_T_ANY),
|
||||
@@ -111,10 +127,10 @@ static void dnscbSRV(struct dns_ctx *ctx, struct dns_rr_srv *result, void *data)
|
||||
r->setComplete();
|
||||
}
|
||||
|
||||
void SRVRequest::submit()
|
||||
void SRVRequest::submit( Client * client )
|
||||
{
|
||||
// if service is defined, pass service and protocol
|
||||
if (!dns_submit_srv(NULL, getDn().c_str(), _service.empty() ? NULL : _service.c_str(), _service.empty() ? NULL : _protocol.c_str(), 0, dnscbSRV, this )) {
|
||||
if (!dns_submit_srv(client->d->ctx, getDn().c_str(), _service.empty() ? NULL : _service.c_str(), _service.empty() ? NULL : _protocol.c_str(), 0, dnscbSRV, this )) {
|
||||
SG_LOG(SG_IO, SG_ALERT, "Can't submit dns request for " << getDn());
|
||||
return;
|
||||
}
|
||||
@@ -148,10 +164,10 @@ static void dnscbTXT(struct dns_ctx *ctx, struct dns_rr_txt *result, void *data)
|
||||
r->setComplete();
|
||||
}
|
||||
|
||||
void TXTRequest::submit()
|
||||
void TXTRequest::submit( Client * client )
|
||||
{
|
||||
// protocol and service an already encoded in DN so pass in NULL for both
|
||||
if (!dns_submit_txt(NULL, getDn().c_str(), DNS_C_IN, 0, dnscbTXT, this )) {
|
||||
if (!dns_submit_txt(client->d->ctx, getDn().c_str(), DNS_C_IN, 0, dnscbTXT, this )) {
|
||||
SG_LOG(SG_IO, SG_ALERT, "Can't submit dns request for " << getDn());
|
||||
return;
|
||||
}
|
||||
@@ -175,11 +191,11 @@ static void dnscbNAPTR(struct dns_ctx *ctx, struct dns_rr_naptr *result, void *d
|
||||
r->ttl = result->dnsnaptr_ttl;
|
||||
for (int i = 0; i < result->dnsnaptr_nrr; i++) {
|
||||
if( !r->qservice.empty() && r->qservice != result->dnsnaptr_naptr[i].service )
|
||||
return;
|
||||
continue;
|
||||
|
||||
//TODO: case ignore and result flags may have more than one flag
|
||||
if( !r->qflags.empty() && r->qflags != result->dnsnaptr_naptr[i].flags )
|
||||
return;
|
||||
continue;
|
||||
|
||||
NAPTRRequest::NAPTR_ptr naptr(new NAPTRRequest::NAPTR);
|
||||
r->entries.push_back(naptr);
|
||||
@@ -196,9 +212,9 @@ static void dnscbNAPTR(struct dns_ctx *ctx, struct dns_rr_naptr *result, void *d
|
||||
r->setComplete();
|
||||
}
|
||||
|
||||
void NAPTRRequest::submit()
|
||||
void NAPTRRequest::submit( Client * client )
|
||||
{
|
||||
if (!dns_submit_naptr(NULL, getDn().c_str(), 0, dnscbNAPTR, this )) {
|
||||
if (!dns_submit_naptr(client->d->ctx, getDn().c_str(), 0, dnscbNAPTR, this )) {
|
||||
SG_LOG(SG_IO, SG_ALERT, "Can't submit dns request for " << getDn());
|
||||
return;
|
||||
}
|
||||
@@ -217,15 +233,16 @@ Client::Client() :
|
||||
|
||||
void Client::makeRequest(const Request_ptr& r)
|
||||
{
|
||||
r->submit();
|
||||
r->submit(this);
|
||||
}
|
||||
|
||||
void Client::update(int waitTimeout)
|
||||
{
|
||||
time_t now = time(NULL);
|
||||
if( dns_timeouts( NULL, waitTimeout, now ) < 0 )
|
||||
if( dns_timeouts( d->ctx, -1, now ) < 0 )
|
||||
return;
|
||||
dns_ioevent(NULL, now);
|
||||
|
||||
dns_ioevent(d->ctx, now);
|
||||
}
|
||||
|
||||
} // of namespace DNS
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#ifndef SG_DNS_CLIENT_HXX
|
||||
#define SG_DNS_CLIENT_HXX
|
||||
|
||||
#include <memory> // for std::auto_ptr
|
||||
#include <memory> // for std::unique_ptr
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <ctime> // for time_t
|
||||
@@ -39,6 +39,7 @@ namespace simgear
|
||||
namespace DNS
|
||||
{
|
||||
|
||||
class Client;
|
||||
class Request : public SGReferenced
|
||||
{
|
||||
public:
|
||||
@@ -50,7 +51,7 @@ public:
|
||||
bool isTimeout() const;
|
||||
void setComplete( bool b = true ) { _complete = b; }
|
||||
|
||||
virtual void submit() = 0;
|
||||
virtual void submit( Client * client) = 0;
|
||||
|
||||
std::string cname;
|
||||
std::string qname;
|
||||
@@ -68,7 +69,7 @@ class NAPTRRequest : public Request
|
||||
{
|
||||
public:
|
||||
NAPTRRequest( const std::string & dn );
|
||||
virtual void submit();
|
||||
virtual void submit( Client * client );
|
||||
|
||||
struct NAPTR : SGReferenced {
|
||||
int order;
|
||||
@@ -91,7 +92,7 @@ class SRVRequest : public Request
|
||||
public:
|
||||
SRVRequest( const std::string & dn );
|
||||
SRVRequest( const std::string & dn, const string & service, const string & protocol );
|
||||
virtual void submit();
|
||||
virtual void submit( Client * client );
|
||||
|
||||
struct SRV : SGReferenced {
|
||||
int priority;
|
||||
@@ -111,7 +112,7 @@ class TXTRequest : public Request
|
||||
{
|
||||
public:
|
||||
TXTRequest( const std::string & dn );
|
||||
virtual void submit();
|
||||
virtual void submit( Client * client );
|
||||
|
||||
typedef std::vector<string> TXT_list;
|
||||
typedef std::map<std::string,std::string> TXT_Attribute_map;
|
||||
@@ -131,9 +132,8 @@ public:
|
||||
|
||||
// void cancelRequest(const Request_ptr& r, std::string reason = std::string());
|
||||
|
||||
private:
|
||||
class ClientPrivate;
|
||||
std::auto_ptr<ClientPrivate> d;
|
||||
std::unique_ptr<ClientPrivate> d;
|
||||
};
|
||||
|
||||
} // of namespace DNS
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
//
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "HTTPClient.hxx"
|
||||
#include "HTTPFileRequest.hxx"
|
||||
|
||||
@@ -24,7 +24,7 @@
|
||||
#ifndef SG_HTTP_CLIENT_HXX
|
||||
#define SG_HTTP_CLIENT_HXX
|
||||
|
||||
#include <memory> // for std::auto_ptr
|
||||
#include <memory> // for std::unique_ptr
|
||||
#include <stdint.h> // for uint_64t
|
||||
|
||||
#include <simgear/io/HTTPFileRequest.hxx>
|
||||
@@ -125,7 +125,7 @@ private:
|
||||
friend class Request;
|
||||
|
||||
class ClientPrivate;
|
||||
std::auto_ptr<ClientPrivate> d;
|
||||
std::unique_ptr<ClientPrivate> d;
|
||||
};
|
||||
|
||||
} // of namespace HTTP
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "HTTPFileRequest.hxx"
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
#include <simgear/misc/sg_path.hxx>
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#include "HTTPRequest.hxx"
|
||||
|
||||
#include <simgear/misc/sgstream.hxx>
|
||||
#include <simgear/io/iostreams/sgstream.hxx>
|
||||
|
||||
namespace simgear
|
||||
{
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "HTTPMemoryRequest.hxx"
|
||||
|
||||
namespace simgear
|
||||
|
||||
@@ -16,10 +16,10 @@
|
||||
// along with this program; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
|
||||
#include "HTTPRepository.hxx"
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "HTTPRepository.hxx"
|
||||
|
||||
#include <iostream>
|
||||
#include <cassert>
|
||||
#include <algorithm>
|
||||
@@ -37,7 +37,7 @@
|
||||
#include <simgear/misc/sg_dir.hxx>
|
||||
#include <simgear/io/HTTPClient.hxx>
|
||||
#include <simgear/io/sg_file.hxx>
|
||||
#include <simgear/misc/sgstream.hxx>
|
||||
#include <simgear/io/iostreams/sgstream.hxx>
|
||||
#include <simgear/structure/exception.hxx>
|
||||
#include <simgear/timing/timestamp.hxx>
|
||||
|
||||
@@ -57,6 +57,8 @@ namespace simgear
|
||||
{
|
||||
}
|
||||
|
||||
virtual void cancel();
|
||||
|
||||
size_t contentSize() const
|
||||
{
|
||||
return _contentSize;
|
||||
@@ -102,7 +104,6 @@ public:
|
||||
hashCacheDirty(false),
|
||||
p(parent),
|
||||
isUpdating(false),
|
||||
updateEverything(false),
|
||||
status(HTTPRepository::REPO_NO_ERROR),
|
||||
totalDownloaded(0)
|
||||
{ ; }
|
||||
@@ -114,14 +115,10 @@ public:
|
||||
std::string baseUrl;
|
||||
SGPath basePath;
|
||||
bool isUpdating;
|
||||
bool updateEverything;
|
||||
string_list updatePaths;
|
||||
HTTPRepository::ResultCode status;
|
||||
HTTPDirectory* rootDir;
|
||||
size_t totalDownloaded;
|
||||
|
||||
void updateWaiting();
|
||||
|
||||
HTTP::Request_ptr updateFile(HTTPDirectory* dir, const std::string& name,
|
||||
size_t sz);
|
||||
HTTP::Request_ptr updateDir(HTTPDirectory* dir, const std::string& hash,
|
||||
@@ -196,12 +193,10 @@ class HTTPDirectory
|
||||
typedef std::vector<ChildInfo> ChildInfoList;
|
||||
ChildInfoList children;
|
||||
|
||||
|
||||
public:
|
||||
HTTPDirectory(HTTPRepoPrivate* repo, const std::string& path) :
|
||||
_repository(repo),
|
||||
_relativePath(path),
|
||||
_state(DoNotUpdate)
|
||||
_relativePath(path)
|
||||
{
|
||||
assert(repo);
|
||||
|
||||
@@ -238,8 +233,6 @@ public:
|
||||
fpath.append(".dirindex");
|
||||
_repository->updatedFileContents(fpath, hash);
|
||||
|
||||
_state = Updated;
|
||||
|
||||
children.clear();
|
||||
parseDirIndex(children);
|
||||
std::sort(children.begin(), children.end());
|
||||
@@ -247,7 +240,6 @@ public:
|
||||
|
||||
void failedToUpdate(HTTPRepository::ResultCode status)
|
||||
{
|
||||
_state = UpdateFailed;
|
||||
if (_relativePath.empty()) {
|
||||
// root dir failed
|
||||
_repository->failedToGetRootIndex(status);
|
||||
@@ -261,7 +253,7 @@ public:
|
||||
if (_repository->installedCopyPath.isNull()) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
string_list indexNames = indexChildren();
|
||||
const_string_list_iterator nameIt = indexNames.begin();
|
||||
for (; nameIt != indexNames.end(); ++nameIt) {
|
||||
@@ -284,7 +276,7 @@ public:
|
||||
}
|
||||
|
||||
SG_LOG(SG_TERRASYNC, SG_BULK, "new child, copying existing file" << cp << p);
|
||||
|
||||
|
||||
SGBinaryFile src(cp);
|
||||
SGBinaryFile dst(p);
|
||||
src.open(SG_IO_IN);
|
||||
@@ -306,11 +298,7 @@ public:
|
||||
|
||||
void updateChildrenBasedOnHash()
|
||||
{
|
||||
// if we got here for a dir which is still updating or excluded
|
||||
// from updates, just bail out right now.
|
||||
if (_state != Updated) {
|
||||
return;
|
||||
}
|
||||
//SG_LOG(SG_TERRASYNC, SG_DEBUG, "updated children for:" << relativePath());
|
||||
|
||||
copyInstalledChildren();
|
||||
|
||||
@@ -345,9 +333,6 @@ public:
|
||||
SG_LOG(SG_TERRASYNC, SG_DEBUG, "file exists hash is good:" << it->file() );
|
||||
if (c->type == ChildInfo::DirectoryType) {
|
||||
HTTPDirectory* childDir = childDirectory(it->file());
|
||||
if (childDir->_state == NotUpdated) {
|
||||
childDir->_state = Updated;
|
||||
}
|
||||
childDir->updateChildrenBasedOnHash();
|
||||
}
|
||||
}
|
||||
@@ -365,95 +350,6 @@ public:
|
||||
scheduleUpdates(toBeUpdated);
|
||||
}
|
||||
|
||||
void markAsUpToDate()
|
||||
{
|
||||
_state = Updated;
|
||||
}
|
||||
|
||||
void markAsUpdating()
|
||||
{
|
||||
assert(_state == NotUpdated);
|
||||
_state = HTTPDirectory::UpdateInProgress;
|
||||
}
|
||||
|
||||
void markAsEnabled()
|
||||
{
|
||||
// assert because this should only get invoked on newly created
|
||||
// directory objects which are inside the sub-tree(s) to be updated
|
||||
assert(_state == DoNotUpdate);
|
||||
_state = NotUpdated;
|
||||
}
|
||||
|
||||
void markSubtreeAsNeedingUpdate()
|
||||
{
|
||||
if (_state == Updated) {
|
||||
_state = NotUpdated; // reset back to not-updated
|
||||
}
|
||||
|
||||
ChildInfoList::iterator cit;
|
||||
for (cit = children.begin(); cit != children.end(); ++cit) {
|
||||
if (cit->type == ChildInfo::DirectoryType) {
|
||||
HTTPDirectory* childDir = childDirectory(cit->name);
|
||||
childDir->markSubtreeAsNeedingUpdate();
|
||||
}
|
||||
} // of child iteration
|
||||
}
|
||||
|
||||
void markSubtreeAsEnabled()
|
||||
{
|
||||
if (_state == DoNotUpdate) {
|
||||
markAsEnabled();
|
||||
}
|
||||
|
||||
ChildInfoList::iterator cit;
|
||||
for (cit = children.begin(); cit != children.end(); ++cit) {
|
||||
if (cit->type == ChildInfo::DirectoryType) {
|
||||
HTTPDirectory* childDir = childDirectory(cit->name);
|
||||
childDir->markSubtreeAsEnabled();
|
||||
}
|
||||
} // of child iteration
|
||||
}
|
||||
|
||||
|
||||
void markAncestorChainAsEnabled()
|
||||
{
|
||||
if (_state == DoNotUpdate) {
|
||||
markAsEnabled();
|
||||
}
|
||||
|
||||
if (_relativePath.empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string prPath = SGPath(_relativePath).dir();
|
||||
if (prPath.empty()) {
|
||||
_repository->rootDir->markAncestorChainAsEnabled();
|
||||
} else {
|
||||
HTTPDirectory* prDir = _repository->getOrCreateDirectory(prPath);
|
||||
prDir->markAncestorChainAsEnabled();
|
||||
}
|
||||
}
|
||||
|
||||
void updateIfWaiting(const std::string& hash, size_t sz)
|
||||
{
|
||||
if (_state == NotUpdated) {
|
||||
_repository->updateDir(this, hash, sz);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((_state == DoNotUpdate) || (_state == UpdateInProgress)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ChildInfoList::iterator cit;
|
||||
for (cit = children.begin(); cit != children.end(); ++cit) {
|
||||
if (cit->type == ChildInfo::DirectoryType) {
|
||||
HTTPDirectory* childDir = childDirectory(cit->name);
|
||||
childDir->updateIfWaiting(cit->hash, cit->sizeInBytes);
|
||||
}
|
||||
} // of child iteration
|
||||
}
|
||||
|
||||
HTTPDirectory* childDirectory(const std::string& name)
|
||||
{
|
||||
std::string childPath = relativePath().empty() ? name : relativePath() + "/" + name;
|
||||
@@ -494,11 +390,6 @@ public:
|
||||
_repository->updateFile(this, *it, cit->sizeInBytes);
|
||||
} else {
|
||||
HTTPDirectory* childDir = childDirectory(*it);
|
||||
if (childDir->_state == DoNotUpdate) {
|
||||
SG_LOG(SG_TERRASYNC, SG_WARN, "scheduleUpdate, child:" << *it << " is marked do not update so skipping");
|
||||
continue;
|
||||
}
|
||||
|
||||
_repository->updateDir(childDir, cit->hash, cit->sizeInBytes);
|
||||
}
|
||||
}
|
||||
@@ -603,6 +494,11 @@ private:
|
||||
continue; // ignore path, next line
|
||||
}
|
||||
|
||||
if( typeData == "time" && tokens.size() > 1 ) {
|
||||
SG_LOG(SG_TERRASYNC, SG_INFO, ".dirindex at '" << p.str() << "' timestamp: " << tokens[1] );
|
||||
continue;
|
||||
}
|
||||
|
||||
if( tokens.size() < 3 ) {
|
||||
SG_LOG(SG_TERRASYNC, SG_WARN, "malformed .dirindex file: not enough tokens in line '" << line << "' (ignoring line)" );
|
||||
continue;
|
||||
@@ -612,6 +508,14 @@ private:
|
||||
SG_LOG(SG_TERRASYNC, SG_WARN, "malformed .dirindex file: invalid type in line '" << line << "', expected 'd' or 'f', (ignoring line)" );
|
||||
continue;
|
||||
}
|
||||
|
||||
// security: prevent writing outside the repository via ../../.. filenames
|
||||
// (valid filenames never contain / - subdirectories have their own .dirindex)
|
||||
if ((tokens[1] == "..") || (tokens[1].find_first_of("/\\") != std::string::npos)) {
|
||||
SG_LOG(SG_TERRASYNC, SG_WARN, "malformed .dirindex file: invalid filename in line '" << line << "', (ignoring line)" );
|
||||
continue;
|
||||
}
|
||||
|
||||
children.push_back(ChildInfo(typeData == "f" ? ChildInfo::FileType : ChildInfo::DirectoryType, tokens[1], tokens[2]));
|
||||
|
||||
if (tokens.size() > 3) {
|
||||
@@ -656,16 +560,7 @@ private:
|
||||
HTTPRepoPrivate* _repository;
|
||||
std::string _relativePath; // in URL and file-system space
|
||||
|
||||
typedef enum
|
||||
{
|
||||
NotUpdated,
|
||||
UpdateInProgress,
|
||||
Updated,
|
||||
UpdateFailed,
|
||||
DoNotUpdate
|
||||
} State;
|
||||
|
||||
State _state;
|
||||
};
|
||||
|
||||
HTTPRepository::HTTPRepository(const SGPath& base, HTTP::Client *cl) :
|
||||
@@ -703,38 +598,14 @@ SGPath HTTPRepository::fsBase() const
|
||||
|
||||
void HTTPRepository::update()
|
||||
{
|
||||
_d->rootDir->markSubtreeAsNeedingUpdate();
|
||||
_d->updateWaiting();
|
||||
}
|
||||
|
||||
void HTTPRepository::setEntireRepositoryMode()
|
||||
{
|
||||
if (!_d->updateEverything) {
|
||||
// this is a one-way decision
|
||||
_d->updateEverything = true;
|
||||
}
|
||||
|
||||
// probably overkill but not expensive so let's check everything
|
||||
// we have in case someone did something funky and switched from partial
|
||||
// to 'whole repo' updating.
|
||||
_d->rootDir->markSubtreeAsEnabled();
|
||||
}
|
||||
|
||||
|
||||
void HTTPRepository::addSubpath(const std::string& relPath)
|
||||
{
|
||||
if (_d->updateEverything) {
|
||||
SG_LOG(SG_TERRASYNC, SG_WARN, "called HTTPRepository::addSubpath but updating everything");
|
||||
if (_d->isUpdating) {
|
||||
return;
|
||||
}
|
||||
|
||||
_d->updatePaths.push_back(relPath);
|
||||
|
||||
HTTPDirectory* dir = _d->getOrCreateDirectory(relPath);
|
||||
dir->markSubtreeAsEnabled();
|
||||
dir->markAncestorChainAsEnabled();
|
||||
|
||||
_d->updateWaiting();
|
||||
_d->status = REPO_NO_ERROR;
|
||||
_d->isUpdating = true;
|
||||
_d->failures.clear();
|
||||
_d->updateDir(_d->rootDir, std::string(), 0);
|
||||
}
|
||||
|
||||
bool HTTPRepository::isDoingSync() const
|
||||
@@ -794,6 +665,12 @@ HTTPRepository::failure() const
|
||||
return _d->status;
|
||||
}
|
||||
|
||||
void HTTPRepoGetRequest::cancel()
|
||||
{
|
||||
_directory->repository()->http->cancelRequest(this, "Reposiotry cancelled");
|
||||
_directory = 0;
|
||||
}
|
||||
|
||||
class FileGetRequest : public HTTPRepoGetRequest
|
||||
{
|
||||
public:
|
||||
@@ -825,7 +702,6 @@ HTTPRepository::failure() const
|
||||
virtual void onDone()
|
||||
{
|
||||
file->close();
|
||||
|
||||
if (responseCode() == 200) {
|
||||
std::string hash = strutils::encodeHex(sha1_result(&hashContext), HASH_LENGTH);
|
||||
_directory->didUpdateFile(fileName, hash, contentSize());
|
||||
@@ -862,7 +738,7 @@ HTTPRepository::failure() const
|
||||
std::string fileName; // if empty, we're getting the directory itself
|
||||
SGPath pathInRepo;
|
||||
simgear::sha1nfo hashContext;
|
||||
std::auto_ptr<SGBinaryFile> file;
|
||||
std::unique_ptr<SGBinaryFile> file;
|
||||
};
|
||||
|
||||
class DirGetRequest : public HTTPRepoGetRequest
|
||||
@@ -922,8 +798,8 @@ HTTPRepository::failure() const
|
||||
of.write(body.data(), body.size());
|
||||
of.close();
|
||||
_directory->dirIndexUpdated(hash);
|
||||
} else {
|
||||
_directory->markAsUpToDate();
|
||||
|
||||
//SG_LOG(SG_TERRASYNC, SG_INFO, "updated dir index " << _directory->absolutePath());
|
||||
}
|
||||
|
||||
_directory->repository()->totalDownloaded += contentSize();
|
||||
@@ -934,7 +810,7 @@ HTTPRepository::failure() const
|
||||
SGTimeStamp st;
|
||||
st.stamp();
|
||||
_directory->updateChildrenBasedOnHash();
|
||||
SG_LOG(SG_TERRASYNC, SG_INFO, "after update of:" << _directory->absolutePath() << " child update took:" << st.elapsedMSec());
|
||||
SG_LOG(SG_TERRASYNC, SG_DEBUG, "after update of:" << _directory->absolutePath() << " child update took:" << st.elapsedMSec());
|
||||
} catch (sg_exception& ) {
|
||||
_directory->failedToUpdate(HTTPRepository::REPO_ERROR_IO);
|
||||
}
|
||||
@@ -999,7 +875,6 @@ HTTPRepository::failure() const
|
||||
|
||||
HTTP::Request_ptr HTTPRepoPrivate::updateDir(HTTPDirectory* dir, const std::string& hash, size_t sz)
|
||||
{
|
||||
dir->markAsUpdating();
|
||||
RepoRequestPtr r(new DirGetRequest(dir, hash));
|
||||
r->setContentSize(sz);
|
||||
makeRequest(r);
|
||||
@@ -1166,25 +1041,6 @@ HTTPRepository::failure() const
|
||||
|
||||
HTTPDirectory* d = new HTTPDirectory(this, path);
|
||||
directories.push_back(d);
|
||||
if (updateEverything) {
|
||||
d->markAsEnabled();
|
||||
} else {
|
||||
string_list::const_iterator s;
|
||||
bool shouldUpdate = false;
|
||||
|
||||
for (s = updatePaths.begin(); s != updatePaths.end(); ++s) {
|
||||
size_t minLen = std::min(path.size(), s->size());
|
||||
if (s->compare(0, minLen, path, 0, minLen) == 0) {
|
||||
shouldUpdate = true;
|
||||
break;
|
||||
}
|
||||
} // of paths iteration
|
||||
|
||||
if (shouldUpdate) {
|
||||
d->markAsEnabled();
|
||||
}
|
||||
}
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
@@ -1280,21 +1136,4 @@ HTTPRepository::failure() const
|
||||
SG_LOG(SG_TERRASYNC, SG_WARN, "failed to update entry:" << relativePath << " code:" << fileStatus);
|
||||
}
|
||||
|
||||
void HTTPRepoPrivate::updateWaiting()
|
||||
{
|
||||
if (!isUpdating) {
|
||||
status = HTTPRepository::REPO_NO_ERROR;
|
||||
isUpdating = true;
|
||||
failures.clear();
|
||||
}
|
||||
|
||||
// find to-be-updated sub-trees and kick them off
|
||||
rootDir->updateIfWaiting(std::string(), 0);
|
||||
|
||||
// maybe there was nothing to do
|
||||
if (activeRequests.empty()) {
|
||||
isUpdating = false;
|
||||
}
|
||||
}
|
||||
|
||||
} // of namespace simgear
|
||||
|
||||
@@ -57,13 +57,6 @@ public:
|
||||
|
||||
virtual void update();
|
||||
|
||||
/**
|
||||
* set if we should sync the entire repository
|
||||
*/
|
||||
void setEntireRepositoryMode();
|
||||
|
||||
void addSubpath(const std::string& relPath);
|
||||
|
||||
virtual bool isDoingSync() const;
|
||||
|
||||
virtual ResultCode failure() const;
|
||||
@@ -80,7 +73,7 @@ public:
|
||||
private:
|
||||
bool isBare() const;
|
||||
|
||||
std::auto_ptr<HTTPRepoPrivate> _d;
|
||||
std::unique_ptr<HTTPRepoPrivate> _d;
|
||||
};
|
||||
|
||||
} // of namespace simgear
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "HTTPRequest.hxx"
|
||||
|
||||
#include <simgear/compiler.h>
|
||||
@@ -132,15 +133,15 @@ Request::HTTPVersion decodeHTTPVersion(const std::string& v)
|
||||
//------------------------------------------------------------------------------
|
||||
void Request::responseStart(const std::string& r)
|
||||
{
|
||||
const int maxSplit = 2; // HTTP/1.1 nnn reason-string
|
||||
const int maxSplit = 2; // HTTP/1.1 nnn reason-string?
|
||||
string_list parts = strutils::split(r, NULL, maxSplit);
|
||||
if (parts.size() != 3) {
|
||||
if (parts.size() < 2) {
|
||||
throw sg_io_exception("bad HTTP response:" + r);
|
||||
}
|
||||
|
||||
_responseVersion = decodeHTTPVersion(parts[0]);
|
||||
_responseStatus = strutils::to_int(parts[1]);
|
||||
_responseReason = parts[2];
|
||||
_responseReason = parts.size() > 2 ? parts[2] : "";
|
||||
|
||||
setReadyState(STATUS_RECEIVED);
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
|
||||
29
simgear/io/iostreams/CMakeLists.txt
Normal file
29
simgear/io/iostreams/CMakeLists.txt
Normal file
@@ -0,0 +1,29 @@
|
||||
include (SimGearComponent)
|
||||
|
||||
set(HEADERS
|
||||
sgstream.hxx
|
||||
gzfstream.hxx
|
||||
gzcontainerfile.hxx
|
||||
zlibstream.hxx
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
sgstream.cxx
|
||||
gzfstream.cxx
|
||||
gzcontainerfile.cxx
|
||||
zlibstream.cxx
|
||||
)
|
||||
|
||||
simgear_component(IOStreams io/iostreams "${SOURCES}" "${HEADERS}")
|
||||
|
||||
if(ENABLE_TESTS)
|
||||
|
||||
add_executable(test_streams sgstream_test.cxx )
|
||||
target_link_libraries(test_streams ${TEST_LIBS})
|
||||
add_test(streams ${EXECUTABLE_OUTPUT_PATH}/test_streams)
|
||||
|
||||
add_executable(test_zlibstream zlibstream_test.cxx)
|
||||
target_link_libraries(test_zlibstream ${TEST_LIBS})
|
||||
add_test(zlibstream ${EXECUTABLE_OUTPUT_PATH}/test_zlibstream)
|
||||
|
||||
endif(ENABLE_TESTS)
|
||||
@@ -22,7 +22,7 @@
|
||||
#define GZ_CONTAINER_FILE_HXX
|
||||
|
||||
#include <string>
|
||||
#include <simgear/misc/sgstream.hxx>
|
||||
#include <simgear/io/iostreams/sgstream.hxx>
|
||||
|
||||
class SGPropertyNode;
|
||||
|
||||
@@ -34,7 +34,7 @@
|
||||
#include <simgear/structure/exception.hxx>
|
||||
|
||||
#include <zlib.h>
|
||||
#include "zfstream.hxx"
|
||||
#include "gzfstream.hxx"
|
||||
|
||||
//
|
||||
// Construct a gzfilebuf object.
|
||||
@@ -1,5 +1,5 @@
|
||||
/**
|
||||
* \file zfstream.hxx
|
||||
* \file gzfstream.hxx
|
||||
* A C++ I/O streams interface to the zlib gz* functions.
|
||||
*/
|
||||
|
||||
@@ -24,8 +24,8 @@
|
||||
//
|
||||
// $Id$
|
||||
|
||||
#ifndef _zfstream_hxx
|
||||
#define _zfstream_hxx
|
||||
#ifndef _gzfstream_hxx
|
||||
#define _gzfstream_hxx
|
||||
|
||||
#include <simgear/compiler.h>
|
||||
|
||||
@@ -164,4 +164,4 @@ struct gzofstream_base
|
||||
gzfilebuf gzbuf;
|
||||
};
|
||||
|
||||
#endif // _zfstream_hxx
|
||||
#endif // _gzfstream_hxx
|
||||
@@ -20,6 +20,7 @@
|
||||
//
|
||||
// $Id$
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include <simgear/compiler.h>
|
||||
#include <string>
|
||||
#include <ctype.h> // isspace()
|
||||
@@ -40,7 +40,7 @@
|
||||
#include <string>
|
||||
|
||||
#include <zlib.h>
|
||||
#include <simgear/misc/zfstream.hxx>
|
||||
#include <simgear/io/iostreams/gzfstream.hxx>
|
||||
|
||||
class SGPath;
|
||||
|
||||
@@ -1,52 +1,55 @@
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <cstdlib> // for EXIT_SUCCESS
|
||||
|
||||
#include <cstdlib> // for EXIT_FAILURE
|
||||
|
||||
using std::ofstream;
|
||||
using std::string;
|
||||
using std::cout;
|
||||
using std::endl;
|
||||
|
||||
#include <simgear/misc/sgstream.hxx>
|
||||
#include <simgear/misc/test_macros.hxx>
|
||||
#include <simgear/io/iostreams/sgstream.hxx>
|
||||
#include <simgear/misc/sg_path.hxx>
|
||||
#include <simgear/misc/sg_dir.hxx>
|
||||
|
||||
int main()
|
||||
{
|
||||
const char* fileName = "testfile";
|
||||
const string fileName = "testfile";
|
||||
simgear::Dir tmpDir = simgear::Dir::tempDir("FlightGear");
|
||||
tmpDir.setRemoveOnDestroy();
|
||||
SGPath p(tmpDir.path() / fileName);
|
||||
|
||||
{
|
||||
ofstream f;
|
||||
f.open(fileName, std::ios::binary | std::ios::trunc | std::ios::out);
|
||||
sg_ofstream f(p, std::ios::binary | std::ios::trunc | std::ios::out);
|
||||
f.write("first line ends with line-feed\n"
|
||||
"second line ends with just a cr\r"
|
||||
"third line ends with both\r\n"
|
||||
"fourth line as well\r\n"
|
||||
"fifth line is another CR/LF line\r\n"
|
||||
"end of test\r\n", 158);
|
||||
f.close();
|
||||
}
|
||||
}
|
||||
|
||||
SGPath p(fileName);
|
||||
sg_gzifstream sg(p);
|
||||
std::string stuff;
|
||||
sg >> skipeol;
|
||||
sg >> stuff;
|
||||
if (stuff != "second") return EXIT_FAILURE;
|
||||
SG_CHECK_EQUAL(stuff, "second");
|
||||
cout << "Detection of LF works." << endl;
|
||||
|
||||
sg >> skipeol;
|
||||
sg >> stuff;
|
||||
if (stuff != "third") return EXIT_FAILURE;
|
||||
SG_CHECK_EQUAL(stuff, "third");
|
||||
cout << "Detection of CR works." << endl;
|
||||
|
||||
sg >> skipeol;
|
||||
sg >> stuff;
|
||||
if (stuff != "fourth") return EXIT_FAILURE;
|
||||
SG_CHECK_EQUAL(stuff, "fourth");
|
||||
cout << "Detection of CR/LF works." << endl;
|
||||
|
||||
sg >> skipeol;
|
||||
sg >> skipeol;
|
||||
sg >> stuff;
|
||||
if (stuff != "end") return EXIT_FAILURE;
|
||||
SG_CHECK_EQUAL(stuff, "end");
|
||||
cout << "Detection of 2 following CR/LF lines works." << endl;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
845
simgear/io/iostreams/zlibstream.cxx
Normal file
845
simgear/io/iostreams/zlibstream.cxx
Normal file
@@ -0,0 +1,845 @@
|
||||
// -*- coding: utf-8 -*-
|
||||
//
|
||||
// zlibstream.cxx --- IOStreams classes for working with RFC 1950 and RFC 1952
|
||||
// compression formats (respectively known as the zlib and
|
||||
// gzip formats)
|
||||
//
|
||||
// Copyright (C) 2017 Florent Rougon
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Library General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Library General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Library General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
// MA 02110-1301 USA.
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include <string>
|
||||
#include <ios> // std::streamsize
|
||||
#include <istream>
|
||||
#include <memory> // std::unique_ptr
|
||||
#include <utility> // std::move()
|
||||
#include <algorithm>
|
||||
#include <stdexcept>
|
||||
#include <unordered_map>
|
||||
#include <limits> // std::numeric_limits
|
||||
#include <type_traits> // std::make_unsigned(), std::underlying_type
|
||||
#include <cstddef> // std::size_t, std::ptrdiff_t
|
||||
#include <cassert>
|
||||
|
||||
#include <zlib.h>
|
||||
|
||||
#include <simgear/io/iostreams/zlibstream.hxx>
|
||||
#include <simgear/misc/strutils.hxx>
|
||||
#include <simgear/misc/sg_path.hxx>
|
||||
#include <simgear/structure/exception.hxx>
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
|
||||
using std::string;
|
||||
using traits = std::char_traits<char>;
|
||||
|
||||
// Cast an enum value to its underlying type
|
||||
template <typename T>
|
||||
static constexpr typename std::underlying_type<T>::type enumValue(T e) {
|
||||
return static_cast<typename std::underlying_type<T>::type>(e);
|
||||
}
|
||||
|
||||
// Private utility function
|
||||
static string zlibErrorMessage(const z_stream& zstream, int errorCode)
|
||||
{
|
||||
string res;
|
||||
std::unordered_map<int, string> errorCodeToMessageMap = {
|
||||
{Z_OK, "zlib: no error (code Z_OK)"},
|
||||
{Z_STREAM_END, "zlib stream end"},
|
||||
{Z_NEED_DICT, "zlib: Z_NEED_DICT"},
|
||||
{Z_STREAM_ERROR, "zlib stream error"},
|
||||
{Z_DATA_ERROR, "zlib data error"},
|
||||
{Z_MEM_ERROR, "zlib memory error"},
|
||||
{Z_BUF_ERROR, "zlib buffer error"},
|
||||
{Z_VERSION_ERROR, "zlib version error"}
|
||||
};
|
||||
|
||||
if (errorCode == Z_ERRNO) {
|
||||
res = simgear::strutils::error_string(errno);
|
||||
} else if (zstream.msg != nullptr) {
|
||||
// Caution: this only works if the zstream structure hasn't been
|
||||
// deallocated!
|
||||
res = "zlib: " + string(zstream.msg);
|
||||
} else {
|
||||
try {
|
||||
res = errorCodeToMessageMap.at(errorCode);
|
||||
} catch (const std::out_of_range&) {
|
||||
res = string("unknown zlib error (code " + std::to_string(errorCode) +
|
||||
")");
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// Requirement: 'val' must be non-negative.
|
||||
//
|
||||
// Return:
|
||||
// - 'val' cast as BoundType if it's lower than the largest value BoundType
|
||||
// can represent;
|
||||
// - this largest value otherwise.
|
||||
template<class BoundType, class T>
|
||||
static BoundType clipCast(T val)
|
||||
{
|
||||
typedef typename std::make_unsigned<T>::type uT;
|
||||
typedef typename std::make_unsigned<BoundType>::type uBoundType;
|
||||
assert(val >= 0); // otherwise, the comparison and cast to uT would be unsafe
|
||||
|
||||
// Casts to avoid the signed-compare warning; they don't affect the values,
|
||||
// since both are non-negative.
|
||||
if (static_cast<uT>(val) <
|
||||
static_cast<uBoundType>( std::numeric_limits<BoundType>::max() )) {
|
||||
return static_cast<BoundType>(val);
|
||||
} else {
|
||||
return std::numeric_limits<BoundType>::max();
|
||||
}
|
||||
}
|
||||
|
||||
// Requirement: 'size' must be non-negative.
|
||||
//
|
||||
// Return:
|
||||
// - 'size' if it is lower than or equal to std::numeric_limits<uInt>::max();
|
||||
// - std::numeric_limits<uInt>::max() cast as a T otherwise (this is always
|
||||
// possible in a lossless way, since in this case, one has
|
||||
// 0 <= std::numeric_limits<uInt>::max() < size, and 'size' is of type T).
|
||||
//
|
||||
// Note: uInt is the type of z_stream.avail_in and z_stream.avail_out, hence
|
||||
// the function name.
|
||||
template<class T>
|
||||
static T zlibChunk(T size)
|
||||
{
|
||||
typedef typename std::make_unsigned<T>::type uT;
|
||||
assert(size >= 0); // otherwise, the comparison and cast to uT would be unsafe
|
||||
|
||||
if (static_cast<uT>(size) <= std::numeric_limits<uInt>::max()) {
|
||||
return size;
|
||||
} else {
|
||||
// In this case, we are sure that T can represent
|
||||
// std::numeric_limits<uInt>::max(), thus the cast is safe.
|
||||
return static_cast<T>(std::numeric_limits<uInt>::max());
|
||||
}
|
||||
}
|
||||
|
||||
namespace simgear
|
||||
{
|
||||
|
||||
// ***************************************************************************
|
||||
// * ZlibAbstractIStreambuf class *
|
||||
// ***************************************************************************
|
||||
|
||||
// Common initialization. Subclasses must complete the z_stream struct
|
||||
// initialization with a call to deflateInit2() or inflateInit2(), typically.
|
||||
ZlibAbstractIStreambuf::ZlibAbstractIStreambuf(std::istream& iStream,
|
||||
const SGPath& path,
|
||||
char* inBuf,
|
||||
std::size_t inBufSize,
|
||||
char *outBuf,
|
||||
std::size_t outBufSize,
|
||||
std::size_t putbackSize)
|
||||
: _iStream(iStream),
|
||||
_iStream_p(nullptr),
|
||||
_path(path),
|
||||
_inBuf(inBuf),
|
||||
_inBufSize(inBufSize),
|
||||
_outBuf(outBuf),
|
||||
_outBufSize(outBufSize),
|
||||
_putbackSize(putbackSize)
|
||||
{
|
||||
assert(_inBufSize > 0);
|
||||
assert(_putbackSize >= 0); // guaranteed unless the type is changed...
|
||||
assert(_putbackSize < _outBufSize);
|
||||
|
||||
if (_inBuf == nullptr) {
|
||||
_inBuf = new char[_inBufSize];
|
||||
_inBufMustBeFreed = true;
|
||||
}
|
||||
|
||||
if (_outBuf == nullptr) {
|
||||
_outBuf = new char[_outBufSize];
|
||||
_outBufMustBeFreed = true;
|
||||
}
|
||||
|
||||
_inBufEndPtr = _inBuf;
|
||||
// The input buffer is empty.
|
||||
_zstream.next_in = reinterpret_cast<unsigned char *>(_inBuf);
|
||||
_zstream.avail_in = 0;
|
||||
// ZLib's documentation says its init functions such as inflateInit2() might
|
||||
// consume stream input, therefore let's fill the input buffer now. This
|
||||
// way, constructors of derived classes just have to call the appropriate
|
||||
// ZLib init function: the data will already be in place.
|
||||
getInputData();
|
||||
|
||||
// Force underflow() of the stream buffer on the first read. We could use
|
||||
// some other value, but I avoid nullptr in order to be sure we can always
|
||||
// reliably compare the three pointers with < and >, as well as compute the
|
||||
// difference between any two of them.
|
||||
setg(_outBuf, _outBuf, _outBuf);
|
||||
}
|
||||
|
||||
ZlibAbstractIStreambuf::ZlibAbstractIStreambuf(
|
||||
std::unique_ptr<std::istream> iStream_p,
|
||||
const SGPath& path,
|
||||
char* inBuf,
|
||||
std::size_t inBufSize,
|
||||
char *outBuf,
|
||||
std::size_t outBufSize,
|
||||
std::size_t putbackSize)
|
||||
: ZlibAbstractIStreambuf(*iStream_p, path, inBuf, inBufSize,
|
||||
outBuf, outBufSize, putbackSize)
|
||||
{
|
||||
// Take ownership of the object. This is a way to ensure that the _iStream
|
||||
// reference stays valid as long as our instance is alive, and that the
|
||||
// corresponding std::istream object is automatically destroyed as soon as
|
||||
// our instance is itself destroyed.
|
||||
_iStream_p = std::move(iStream_p);
|
||||
}
|
||||
|
||||
ZlibAbstractIStreambuf::~ZlibAbstractIStreambuf()
|
||||
{
|
||||
if (_inBufMustBeFreed) {
|
||||
delete[] _inBuf;
|
||||
}
|
||||
|
||||
if (_outBufMustBeFreed) {
|
||||
delete[] _outBuf;
|
||||
}
|
||||
}
|
||||
|
||||
// Fill or refill the output buffer, and update the three pointers
|
||||
// corresponding to eback(), gptr() and egptr().
|
||||
int ZlibAbstractIStreambuf::underflow()
|
||||
{
|
||||
if (_allFinished) {
|
||||
return traits::eof();
|
||||
}
|
||||
|
||||
// According to the C++11 standard: “The public members of basic_streambuf
|
||||
// call this virtual function only if gptr() is null or gptr() >= egptr()”.
|
||||
// Still, it seems some people do the following or similar (maybe in case
|
||||
// underflow() is called “incorrectly”?). See for instance N. Josuttis, The
|
||||
// C++ Standard Library (1st edition), p. 584. One sure thing is that it
|
||||
// can't hurt (except performance, marginally), so let's do it.
|
||||
if (gptr() < egptr()) {
|
||||
return traits::to_int_type(*gptr());
|
||||
}
|
||||
|
||||
assert(gptr() == egptr());
|
||||
assert(egptr() - eback() >= 0);
|
||||
std::size_t nbPutbackChars = std::min(
|
||||
static_cast<std::size_t>(egptr() - eback()), // OK because egptr() >= eback()
|
||||
_putbackSize);
|
||||
std::copy(egptr() - nbPutbackChars, egptr(),
|
||||
_outBuf + _putbackSize - nbPutbackChars);
|
||||
|
||||
setg(_outBuf + _putbackSize - nbPutbackChars, // start of putback area
|
||||
_outBuf + _putbackSize, // start of obtained data
|
||||
fillOutputBuffer()); // one-past-end of obtained data
|
||||
|
||||
return (gptr() == egptr()) ? traits::eof() : traits::to_int_type(*gptr());
|
||||
}
|
||||
|
||||
// Simple utility method for fillOutputBuffer(), used to improve readability.
|
||||
// Return the remaining space available in the output buffer, where zlib can
|
||||
// write.
|
||||
std::size_t
|
||||
ZlibAbstractIStreambuf::fOB_remainingSpace(unsigned char* nextOutPtr) const
|
||||
{
|
||||
std::ptrdiff_t remainingSpaceInOutBuf = (
|
||||
_outBuf + _outBufSize - reinterpret_cast<char*>(nextOutPtr));
|
||||
assert(remainingSpaceInOutBuf >= 0);
|
||||
|
||||
return static_cast<std::size_t>(remainingSpaceInOutBuf);
|
||||
}
|
||||
|
||||
// Simple method for dealing with the Z_BUF_ERROR code that may be returned
|
||||
// by zlib's deflate() and inflate() methods.
|
||||
[[ noreturn ]] void ZlibAbstractIStreambuf::handleZ_BUF_ERROR() const
|
||||
{
|
||||
switch (operationType()) {
|
||||
case OperationType::DECOMPRESSION:
|
||||
{
|
||||
string message = (_path.isNull()) ?
|
||||
"Got Z_BUF_ERROR from zlib while decompressing a stream. The stream "
|
||||
"was probably incomplete."
|
||||
:
|
||||
"Got Z_BUF_ERROR from zlib during decompression. The compressed stream "
|
||||
"was probably incomplete.";
|
||||
// When _path.isNull(), sg_location(_path) is equivalent to sg_location()
|
||||
throw sg_io_exception(message, sg_location(_path));
|
||||
}
|
||||
case OperationType::COMPRESSION:
|
||||
throw std::logic_error(
|
||||
"Called ZlibAbstractIStreambuf::handleZ_BUF_ERROR() with "
|
||||
"operationType() == ZlibAbstractIStreambuf::OperationType::COMPRESSION");
|
||||
default:
|
||||
throw std::logic_error(
|
||||
"Unexpected operationType() in "
|
||||
"ZlibAbstractIStreambuf::handleZ_BUF_ERROR(): " +
|
||||
std::to_string(enumValue(operationType())));
|
||||
}
|
||||
}
|
||||
|
||||
// Fill or refill the output buffer. Return a pointer to the first unused char
|
||||
// in _outBuf (i.e., right after the data written by this method, if any;
|
||||
// otherwise: _outBuf + _putbackSize).
|
||||
char* ZlibAbstractIStreambuf::fillOutputBuffer()
|
||||
{
|
||||
bool allInputRead = false;
|
||||
std::size_t remainingSpaceInOutBuf;
|
||||
int retCode;
|
||||
|
||||
// We have to do these unpleasant casts, because zlib uses pointers to
|
||||
// unsigned char for its input and output buffers, while the IOStreams
|
||||
// library in the C++ standard uses plain char pointers...
|
||||
_zstream.next_out = reinterpret_cast<unsigned char*>(_outBuf + _putbackSize);
|
||||
remainingSpaceInOutBuf = fOB_remainingSpace(_zstream.next_out);
|
||||
|
||||
while (remainingSpaceInOutBuf > 0) {
|
||||
_zstream.avail_out = clipCast<uInt>(remainingSpaceInOutBuf);
|
||||
|
||||
if (_zstream.avail_in == 0 && !allInputRead) {
|
||||
// Get data from _iStream, store it in _inBuf
|
||||
allInputRead = getInputData();
|
||||
}
|
||||
|
||||
// Make zlib process some data (compress or decompress it). This updates
|
||||
// _zstream.{avail,next}_{in,out} (4 fields of the z_stream struct).
|
||||
retCode = zlibProcessData();
|
||||
|
||||
if (retCode == Z_BUF_ERROR) {
|
||||
handleZ_BUF_ERROR(); // doesn't return
|
||||
} else if (retCode == Z_STREAM_END) {
|
||||
assert(_zstream.avail_in == 0); // all of _inBuf must have been used
|
||||
_allFinished = true;
|
||||
break;
|
||||
} else if (retCode < 0) { // negative codes are errors
|
||||
throw sg_io_exception(::zlibErrorMessage(_zstream, retCode),
|
||||
sg_location(_path));
|
||||
}
|
||||
|
||||
remainingSpaceInOutBuf = fOB_remainingSpace(_zstream.next_out);
|
||||
}
|
||||
|
||||
return reinterpret_cast<char*>(_zstream.next_out);
|
||||
}
|
||||
|
||||
// This method provides input data to zlib:
|
||||
// - if data is already available in _inBuf, it updates _zstream.avail_in
|
||||
// accordingly (using the largest possible amount given _zstream.avail_in's
|
||||
// type, i.e., uInt);
|
||||
// - otherwise, it reads() as much data as possible into _inBuf from
|
||||
// _iStream and updates _zstream.avail_in to tell zlib about this new
|
||||
// available data (again: largest possible amount given the uInt type).
|
||||
//
|
||||
// This method must be called only when _zstream.avail_in == 0, which means
|
||||
// zlib has read everything we fed it (and does *not* mean, by the way, that
|
||||
// the input buffer starting at _inBuf is empty; this is because the buffer
|
||||
// size might exceed what can be represented by an uInt).
|
||||
//
|
||||
// On return:
|
||||
// - either EOF has been reached for _iStream, and the return value is true;
|
||||
// - or _zstream.avail_in > 0, and the return value is false.
|
||||
//
|
||||
// Note: don't read more in the previous paragraph concerning the state on
|
||||
// return. In the first case, there is *no guarantee* about
|
||||
// _zstream.avail_in's value; in the second case, there is *no guarantee*
|
||||
// about whether EOF has been reached for _iStream.
|
||||
bool ZlibAbstractIStreambuf::getInputData()
|
||||
{
|
||||
bool allInputRead = false;
|
||||
|
||||
assert(_zstream.avail_in == 0);
|
||||
std::ptrdiff_t alreadyAvailable =
|
||||
_inBufEndPtr - reinterpret_cast<char*>(_zstream.next_in);
|
||||
assert(alreadyAvailable >= 0);
|
||||
|
||||
// Data already available?
|
||||
if (alreadyAvailable > 0) {
|
||||
_zstream.avail_in = clipCast<uInt>(alreadyAvailable);
|
||||
return allInputRead;
|
||||
}
|
||||
|
||||
if (_inBufEndPtr == _inBuf + _inBufSize) { // buffer full, rewind
|
||||
_inBufEndPtr = _inBuf;
|
||||
_zstream.next_in = reinterpret_cast<unsigned char*>(_inBuf);
|
||||
}
|
||||
|
||||
// Fill the input buffer (as much as possible)
|
||||
while (_inBufEndPtr < _inBuf + _inBufSize && !_iStream.eof()) {
|
||||
std::streamsize nbCharsToRead = clipCast<std::streamsize>(
|
||||
_inBuf + _inBufSize - _inBufEndPtr);
|
||||
_iStream.read(_inBufEndPtr, nbCharsToRead);
|
||||
|
||||
if (_iStream.bad()) {
|
||||
string errMsg = simgear::strutils::error_string(errno);
|
||||
string msgStart = (_path.isNull()) ?
|
||||
"Error while reading from a stream" : "Read error";
|
||||
throw sg_io_exception(msgStart + ": " + errMsg, sg_location(_path));
|
||||
}
|
||||
|
||||
// Could be zero if at EOF
|
||||
std::streamsize nbCharsRead = _iStream.gcount();
|
||||
// std::streamsize is a signed integral type!
|
||||
assert(0 <= nbCharsRead);
|
||||
_inBufEndPtr += nbCharsRead;
|
||||
}
|
||||
|
||||
std::ptrdiff_t availableChars =
|
||||
_inBufEndPtr - reinterpret_cast<char*>(_zstream.next_in);
|
||||
// assert(availableChars >= 0); <-- already done in clipCast<uInt>()
|
||||
_zstream.avail_in = clipCast<uInt>(availableChars);
|
||||
|
||||
if (_iStream.eof()) {
|
||||
allInputRead = true;
|
||||
} else {
|
||||
// Trying to rewind a fully read std::istringstream with seekg() can lead
|
||||
// to a weird state, where the stream doesn't return any character but
|
||||
// doesn't report EOF either. Make sure we are not in this situation.
|
||||
assert(_zstream.avail_in > 0);
|
||||
}
|
||||
|
||||
return allInputRead;
|
||||
}
|
||||
|
||||
// Implementing this method is optional, but should provide better
|
||||
// performance. It makes zlib write the data directly in the buffer starting
|
||||
// at 'dest'. Without it, the data would go through an intermediate buffer
|
||||
// (_outBuf) before being copied to its (hopefully) final destination.
|
||||
std::streamsize ZlibAbstractIStreambuf::xsgetn(char* dest, std::streamsize n)
|
||||
{
|
||||
// Despite the somewhat misleading footnote 296 of §27.5.3 of the C++11
|
||||
// standard, one can't assume std::size_t to be at least as large as
|
||||
// std::streamsize (64 bits std::streamsize in a 32 bits Windows program).
|
||||
std::streamsize remaining = n;
|
||||
char* origGptr = gptr();
|
||||
char* writePtr = dest; // we'll need dest later -> work with a copy
|
||||
|
||||
// First, let's take data present in our internal buffer (_outBuf)
|
||||
while (remaining > 0) {
|
||||
// Number of available chars in _outBuf
|
||||
std::ptrdiff_t avail = egptr() - gptr();
|
||||
if (avail == 0) { // our internal buffer is empty
|
||||
break;
|
||||
}
|
||||
|
||||
// We need an int for gbump(), at least in C++11.
|
||||
int chunkSize_i = clipCast<int>(avail);
|
||||
if (chunkSize_i > remaining) {
|
||||
chunkSize_i = static_cast<int>(remaining);
|
||||
}
|
||||
assert(chunkSize_i >= 0);
|
||||
|
||||
std::copy(gptr(), gptr() + chunkSize_i, writePtr);
|
||||
gbump(chunkSize_i);
|
||||
writePtr += chunkSize_i;
|
||||
// This cast is okay because 0 <= chunkSize_i <= remaining, which is an
|
||||
// std::streamsize
|
||||
remaining -= static_cast<std::streamsize>(chunkSize_i);
|
||||
}
|
||||
|
||||
if (remaining == 0) {
|
||||
// Everything we needed was already in _outBuf. The putback area is set up
|
||||
// as it should between eback() and the current gptr(), so we are fine to
|
||||
// return.
|
||||
return n;
|
||||
}
|
||||
|
||||
// Now, let's make it so that the remaining data we need is directly written
|
||||
// to the destination area, without going through _outBuf.
|
||||
_zstream.next_out = reinterpret_cast<unsigned char*>(writePtr);
|
||||
bool allInputRead = false;
|
||||
int retCode;
|
||||
|
||||
while (remaining > 0) {
|
||||
std::streamsize chunkSize_s = zlibChunk(remaining);
|
||||
// chunkSize_s > 0 and does fit in a zlib uInt: that's the whole point of
|
||||
// zlibChunk.
|
||||
_zstream.avail_out = static_cast<uInt>(chunkSize_s);
|
||||
|
||||
if (_zstream.avail_in == 0 && !allInputRead) {
|
||||
allInputRead = getInputData();
|
||||
}
|
||||
|
||||
// Make zlib process some data (compress or decompress). This updates
|
||||
// _zstream.{avail,next}_{in,out} (4 fields of the z_stream struct).
|
||||
retCode = zlibProcessData();
|
||||
// chunkSize_s - _zstream.avail_out is the number of chars written by zlib.
|
||||
// 0 <= _zstream.avail_out <= chunkSize_s, which is an std::streamsize.
|
||||
remaining -= chunkSize_s - static_cast<std::streamsize>(_zstream.avail_out);
|
||||
|
||||
if (retCode == Z_BUF_ERROR) {
|
||||
handleZ_BUF_ERROR(); // doesn't return
|
||||
} else if (retCode == Z_STREAM_END) {
|
||||
assert(_zstream.avail_in == 0); // all of _inBuf must have been used
|
||||
_allFinished = true;
|
||||
break;
|
||||
} else if (retCode < 0) { // negative codes are errors
|
||||
throw sg_io_exception(::zlibErrorMessage(_zstream, retCode),
|
||||
sg_location(_path));
|
||||
}
|
||||
}
|
||||
|
||||
// Finally, copy chars to the putback area.
|
||||
std::size_t nbPutbackChars = xsgetn_preparePutbackArea(
|
||||
origGptr, dest, reinterpret_cast<char*>(_zstream.next_out));
|
||||
setg(_outBuf + _putbackSize - nbPutbackChars, // start of putback area
|
||||
_outBuf + _putbackSize, // the buffer for pending,
|
||||
_outBuf + _putbackSize); // available data is empty
|
||||
|
||||
assert(remaining >= 0);
|
||||
assert(n - remaining >= 0);
|
||||
// Total number of chars copied.
|
||||
return n - remaining;
|
||||
}
|
||||
|
||||
// Utility method for xsgetn(): copy some chars to the putback area
|
||||
std::size_t ZlibAbstractIStreambuf::xsgetn_preparePutbackArea(
|
||||
char* origGptr, char* dest, char* writePtr)
|
||||
{
|
||||
// There are two buffers containing characters we potentially have to copy
|
||||
// to the putback area: the one starting at _outBuf and the one starting at
|
||||
// dest. In the following diagram, ***** represents those from _outBuf,
|
||||
// which are right-aligned at origGptr[1], and the series of # represents
|
||||
// those in the [dest, writePtr) address range:
|
||||
//
|
||||
// |_outBuf |eback() *****|origGptr |_outBuf + _outBufSize
|
||||
//
|
||||
// |dest #################|writePtr
|
||||
//
|
||||
// Together, these two memory blocks logically form a contiguous stream of
|
||||
// chars we gave to the “client”: *****#################. All we have to do
|
||||
// now is copy the appropriate amount of chars from this logical stream to
|
||||
// the putback area, according to _putbackSize. These chars are the last
|
||||
// ones in said stream (i.e., right portion of *****#################), but
|
||||
// we have to copy them in order of increasing address to avoid possible
|
||||
// overlapping problems in _outBuf. This is because some of the chars to
|
||||
// copy may be located before _outBuf + _putbackSize (i.e., already be in
|
||||
// the putback area).
|
||||
//
|
||||
// [1] This means that the last char represented by a star is at address
|
||||
// origGptr-1.
|
||||
|
||||
// It seems std::ptrdiff_t is the signed counterpart of std::size_t,
|
||||
// therefore this should always hold (even with equality).
|
||||
static_assert(sizeof(std::size_t) >= sizeof(std::ptrdiff_t),
|
||||
"Unexpected: sizeof(std::size_t) < sizeof(std::ptrdiff_t)");
|
||||
assert(writePtr - dest >= 0);
|
||||
std::size_t inDestBuffer = static_cast<std::size_t>(writePtr - dest);
|
||||
|
||||
assert(origGptr - eback() >= 0);
|
||||
std::size_t nbPutbackChars = std::min(
|
||||
static_cast<std::size_t>(origGptr - eback()) + inDestBuffer,
|
||||
_putbackSize);
|
||||
std::size_t nbPutbackCharsToGo = nbPutbackChars;
|
||||
|
||||
// Are there chars in _outBuf that need to be copied to the putback area?
|
||||
if (nbPutbackChars > inDestBuffer) {
|
||||
std::size_t chunkSize = nbPutbackChars - inDestBuffer; // yes, this number
|
||||
std::copy(origGptr - chunkSize, origGptr,
|
||||
_outBuf + _putbackSize - nbPutbackChars);
|
||||
nbPutbackCharsToGo -= chunkSize;
|
||||
}
|
||||
|
||||
// Finally, copy those that are not in _outBuf
|
||||
std::copy(writePtr - nbPutbackCharsToGo, writePtr,
|
||||
_outBuf + _putbackSize - nbPutbackCharsToGo);
|
||||
|
||||
return nbPutbackChars;
|
||||
}
|
||||
|
||||
// ***************************************************************************
|
||||
// * ZlibCompressorIStreambuf class *
|
||||
// ***************************************************************************
|
||||
|
||||
ZlibCompressorIStreambuf::ZlibCompressorIStreambuf(
|
||||
std::istream& iStream,
|
||||
const SGPath& path,
|
||||
int compressionLevel,
|
||||
ZLibCompressionFormat format,
|
||||
ZLibMemoryStrategy memStrategy,
|
||||
char* inBuf,
|
||||
std::size_t inBufSize,
|
||||
char *outBuf,
|
||||
std::size_t outBufSize,
|
||||
std::size_t putbackSize)
|
||||
: ZlibAbstractIStreambuf(iStream, path, inBuf, inBufSize, outBuf, outBufSize,
|
||||
putbackSize)
|
||||
{
|
||||
zStreamInit(compressionLevel, format, memStrategy);
|
||||
}
|
||||
|
||||
ZlibCompressorIStreambuf::ZlibCompressorIStreambuf(
|
||||
std::unique_ptr<std::istream> iStream_p,
|
||||
const SGPath& path,
|
||||
int compressionLevel,
|
||||
ZLibCompressionFormat format,
|
||||
ZLibMemoryStrategy memStrategy,
|
||||
char* inBuf,
|
||||
std::size_t inBufSize,
|
||||
char *outBuf,
|
||||
std::size_t outBufSize,
|
||||
std::size_t putbackSize)
|
||||
: ZlibCompressorIStreambuf(*iStream_p, path, compressionLevel, format,
|
||||
memStrategy, inBuf, inBufSize, outBuf, outBufSize,
|
||||
putbackSize)
|
||||
{
|
||||
_iStream_p = std::move(iStream_p); // take ownership of the object
|
||||
}
|
||||
|
||||
ZlibCompressorIStreambuf::~ZlibCompressorIStreambuf()
|
||||
{
|
||||
int retCode = deflateEnd(&_zstream); // deallocate the z_stream struct
|
||||
if (retCode != Z_OK) {
|
||||
// In C++11, we can't throw exceptions from a destructor.
|
||||
SG_LOG(SG_IO, SG_ALERT, "ZlibCompressorIStreambuf: " <<
|
||||
::zlibErrorMessage(_zstream, retCode));
|
||||
}
|
||||
}
|
||||
|
||||
ZlibAbstractIStreambuf::OperationType
|
||||
ZlibCompressorIStreambuf::operationType() const
|
||||
{
|
||||
return OperationType::COMPRESSION;
|
||||
}
|
||||
|
||||
void ZlibCompressorIStreambuf::zStreamInit(int compressionLevel,
|
||||
ZLibCompressionFormat format,
|
||||
ZLibMemoryStrategy memStrategy)
|
||||
{
|
||||
int windowBits, memLevel;
|
||||
|
||||
// Intentionally not listing ZLibCompressionFormat::AUTODETECT here (it is
|
||||
// only for decompression!)
|
||||
switch (format) {
|
||||
case ZLibCompressionFormat::ZLIB:
|
||||
windowBits = 15;
|
||||
break;
|
||||
case ZLibCompressionFormat::GZIP:
|
||||
windowBits = 31;
|
||||
break;
|
||||
default:
|
||||
throw std::logic_error("Unexpected compression format: " +
|
||||
std::to_string(enumValue(format)));
|
||||
}
|
||||
|
||||
switch (memStrategy) {
|
||||
case ZLibMemoryStrategy::FAVOR_MEMORY_OVER_SPEED:
|
||||
memLevel = 8;
|
||||
break;
|
||||
case ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY:
|
||||
memLevel = 9;
|
||||
break;
|
||||
default:
|
||||
throw std::logic_error("Unexpected memory strategy: " +
|
||||
std::to_string(enumValue(memStrategy)));
|
||||
}
|
||||
|
||||
_zstream.zalloc = Z_NULL; // No custom memory allocation routines
|
||||
_zstream.zfree = Z_NULL; // Ditto. Therefore, the 'opaque' field won't
|
||||
_zstream.opaque = Z_NULL; // be used, actually.
|
||||
|
||||
int retCode = deflateInit2(&_zstream, compressionLevel, Z_DEFLATED,
|
||||
windowBits, memLevel, Z_DEFAULT_STRATEGY);
|
||||
if (retCode != Z_OK) {
|
||||
throw sg_io_exception(::zlibErrorMessage(_zstream, retCode),
|
||||
sg_location(_path));
|
||||
}
|
||||
}
|
||||
|
||||
int ZlibCompressorIStreambuf::zlibProcessData()
|
||||
{
|
||||
// Compress as much data as possible given _zstream.avail_in and
|
||||
// _zstream.avail_out. The input data starts at _zstream.next_in, the output
|
||||
// at _zstream.next_out, and these four fields are all updated by this call
|
||||
// to reflect exactly the amount of data zlib has consumed and produced.
|
||||
return deflate(&_zstream, _zstream.avail_in ? Z_NO_FLUSH : Z_FINISH);
|
||||
}
|
||||
|
||||
// ***************************************************************************
|
||||
// * ZlibDecompressorIStreambuf class *
|
||||
// ***************************************************************************
|
||||
|
||||
ZlibDecompressorIStreambuf::ZlibDecompressorIStreambuf(
|
||||
std::istream& iStream,
|
||||
const SGPath& path,
|
||||
ZLibCompressionFormat format,
|
||||
char* inBuf,
|
||||
std::size_t inBufSize,
|
||||
char *outBuf,
|
||||
std::size_t outBufSize,
|
||||
std::size_t putbackSize)
|
||||
: ZlibAbstractIStreambuf(iStream, path, inBuf, inBufSize, outBuf, outBufSize,
|
||||
putbackSize)
|
||||
{
|
||||
zStreamInit(format);
|
||||
}
|
||||
|
||||
ZlibDecompressorIStreambuf::ZlibDecompressorIStreambuf(
|
||||
std::unique_ptr<std::istream> iStream_p,
|
||||
const SGPath& path,
|
||||
ZLibCompressionFormat format,
|
||||
char* inBuf,
|
||||
std::size_t inBufSize,
|
||||
char *outBuf,
|
||||
std::size_t outBufSize,
|
||||
std::size_t putbackSize)
|
||||
: ZlibDecompressorIStreambuf(*iStream_p, path, format, inBuf, inBufSize,
|
||||
outBuf, outBufSize, putbackSize)
|
||||
{
|
||||
_iStream_p = std::move(iStream_p); // take ownership of the object
|
||||
}
|
||||
|
||||
ZlibDecompressorIStreambuf::~ZlibDecompressorIStreambuf()
|
||||
{
|
||||
int retCode = inflateEnd(&_zstream); // deallocate the z_stream struct
|
||||
if (retCode != Z_OK) {
|
||||
// In C++11, we can't throw exceptions from a destructor.
|
||||
SG_LOG(SG_IO, SG_ALERT, "ZlibDecompressorIStreambuf: " <<
|
||||
::zlibErrorMessage(_zstream, retCode));
|
||||
}
|
||||
}
|
||||
|
||||
ZlibAbstractIStreambuf::OperationType
|
||||
ZlibDecompressorIStreambuf::operationType() const
|
||||
{
|
||||
return OperationType::DECOMPRESSION;
|
||||
}
|
||||
|
||||
void ZlibDecompressorIStreambuf::zStreamInit(ZLibCompressionFormat format)
|
||||
{
|
||||
int windowBits;
|
||||
|
||||
switch (format) {
|
||||
case ZLibCompressionFormat::ZLIB:
|
||||
windowBits = 15;
|
||||
break;
|
||||
case ZLibCompressionFormat::GZIP:
|
||||
windowBits = 31;
|
||||
break;
|
||||
case ZLibCompressionFormat::AUTODETECT:
|
||||
windowBits = 47; // 47 = 32 + 15
|
||||
break;
|
||||
default:
|
||||
throw std::logic_error("Unexpected compression format: " +
|
||||
std::to_string(enumValue(format)));
|
||||
}
|
||||
|
||||
_zstream.zalloc = Z_NULL; // No custom memory allocation routines
|
||||
_zstream.zfree = Z_NULL; // Ditto. Therefore, the 'opaque' field won't
|
||||
_zstream.opaque = Z_NULL; // be used, actually.
|
||||
|
||||
int retCode = inflateInit2(&_zstream, windowBits);
|
||||
if (retCode != Z_OK) {
|
||||
throw sg_io_exception(::zlibErrorMessage(_zstream, retCode),
|
||||
sg_location(_path));
|
||||
}
|
||||
}
|
||||
|
||||
int ZlibDecompressorIStreambuf::zlibProcessData()
|
||||
{
|
||||
// Decompress as much data as possible given _zstream.avail_in and
|
||||
// _zstream.avail_out. The input data starts at _zstream.next_in, the output
|
||||
// at _zstream.next_out, and these four fields are all updated by this call
|
||||
// to reflect exactly the amount of data zlib has consumed and produced.
|
||||
return inflate(&_zstream, _zstream.avail_in ? Z_NO_FLUSH : Z_FINISH);
|
||||
}
|
||||
|
||||
// ***************************************************************************
|
||||
// * ZlibCompressorIStream class *
|
||||
// ***************************************************************************
|
||||
|
||||
ZlibCompressorIStream::ZlibCompressorIStream(std::istream& iStream,
|
||||
const SGPath& path,
|
||||
int compressionLevel,
|
||||
ZLibCompressionFormat format,
|
||||
ZLibMemoryStrategy memStrategy,
|
||||
char* inBuf,
|
||||
std::size_t inBufSize,
|
||||
char *outBuf,
|
||||
std::size_t outBufSize,
|
||||
std::size_t putbackSize)
|
||||
: std::istream(nullptr),
|
||||
_streamBuf(iStream, path, compressionLevel, format, memStrategy, inBuf,
|
||||
inBufSize, outBuf, outBufSize, putbackSize)
|
||||
{
|
||||
// Associate _streamBuf to 'this' and clear the error state flags
|
||||
rdbuf(&_streamBuf);
|
||||
}
|
||||
|
||||
ZlibCompressorIStream::ZlibCompressorIStream(
|
||||
std::unique_ptr<std::istream> iStream_p,
|
||||
const SGPath& path,
|
||||
int compressionLevel,
|
||||
ZLibCompressionFormat format,
|
||||
ZLibMemoryStrategy memStrategy,
|
||||
char* inBuf,
|
||||
std::size_t inBufSize,
|
||||
char *outBuf,
|
||||
std::size_t outBufSize,
|
||||
std::size_t putbackSize)
|
||||
: std::istream(nullptr),
|
||||
_streamBuf(std::move(iStream_p), path, compressionLevel, format,
|
||||
memStrategy, inBuf, inBufSize, outBuf, outBufSize, putbackSize)
|
||||
{
|
||||
// Associate _streamBuf to 'this' and clear the error state flags
|
||||
rdbuf(&_streamBuf);
|
||||
}
|
||||
|
||||
ZlibCompressorIStream::~ZlibCompressorIStream()
|
||||
{ }
|
||||
|
||||
// ***************************************************************************
|
||||
// * ZlibDecompressorIStream class *
|
||||
// ***************************************************************************
|
||||
|
||||
ZlibDecompressorIStream::ZlibDecompressorIStream(std::istream& iStream,
|
||||
const SGPath& path,
|
||||
ZLibCompressionFormat format,
|
||||
char* inBuf,
|
||||
std::size_t inBufSize,
|
||||
char *outBuf,
|
||||
std::size_t outBufSize,
|
||||
std::size_t putbackSize)
|
||||
: std::istream(nullptr),
|
||||
_streamBuf(iStream, path, format, inBuf, inBufSize, outBuf, outBufSize,
|
||||
putbackSize)
|
||||
{
|
||||
// Associate _streamBuf to 'this' and clear the error state flags
|
||||
rdbuf(&_streamBuf);
|
||||
}
|
||||
|
||||
ZlibDecompressorIStream::ZlibDecompressorIStream(
|
||||
std::unique_ptr<std::istream> iStream_p,
|
||||
const SGPath& path,
|
||||
ZLibCompressionFormat format,
|
||||
char* inBuf,
|
||||
std::size_t inBufSize,
|
||||
char *outBuf,
|
||||
std::size_t outBufSize,
|
||||
std::size_t putbackSize)
|
||||
: std::istream(nullptr),
|
||||
_streamBuf(std::move(iStream_p), path, format, inBuf, inBufSize,
|
||||
outBuf, outBufSize, putbackSize)
|
||||
{
|
||||
// Associate _streamBuf to 'this' and clear the error state flags
|
||||
rdbuf(&_streamBuf);
|
||||
}
|
||||
|
||||
ZlibDecompressorIStream::~ZlibDecompressorIStream()
|
||||
{ }
|
||||
|
||||
} // of namespace simgear
|
||||
477
simgear/io/iostreams/zlibstream.hxx
Normal file
477
simgear/io/iostreams/zlibstream.hxx
Normal file
@@ -0,0 +1,477 @@
|
||||
// -*- coding: utf-8 -*-
|
||||
//
|
||||
// zlibstream.hxx --- IOStreams classes for working with RFC 1950 and RFC 1952
|
||||
// compression formats (respectively known as the zlib and
|
||||
// gzip formats)
|
||||
//
|
||||
// Copyright (C) 2017 Florent Rougon
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Library General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Library General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Library General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
// MA 02110-1301 USA.
|
||||
|
||||
#ifndef SG_ZLIBSTREAM_HXX
|
||||
#define SG_ZLIBSTREAM_HXX
|
||||
|
||||
#include <ios> // std::streamsize
|
||||
#include <istream>
|
||||
#include <streambuf>
|
||||
#include <memory> // std::unique_ptr
|
||||
#include <zlib.h> // struct z_stream
|
||||
|
||||
#include <simgear/misc/sg_path.hxx>
|
||||
|
||||
// This file contains:
|
||||
//
|
||||
// - two stream buffer classes (ZlibCompressorIStreambuf and
|
||||
// ZlibDecompressorIStreambuf), both based on the same abstract class:
|
||||
// ZlibAbstractIStreambuf;
|
||||
//
|
||||
// - two std::istream subclasses (ZlibCompressorIStream and
|
||||
// ZlibDecompressorIStream), each creating and using the corresponding
|
||||
// stream buffer class from the previous item.
|
||||
//
|
||||
// All these allow one to work with RFC 1950 and RFC 1952 compression
|
||||
// formats, respectively known as the zlib and gzip formats.
|
||||
//
|
||||
// These classes are *input* streaming classes, which means they can
|
||||
// efficiently handle arbitrary amounts of data without using any disk
|
||||
// space nor increasing amounts of memory, and allow “client code” to pull
|
||||
// exactly as much data as it wants at any given time, resuming later
|
||||
// when it is ready to handle the next chunk.
|
||||
//
|
||||
// So, for example, assuming you've created an instance of
|
||||
// ZlibCompressorIStream (bound to some input stream of your choice, let's
|
||||
// call it iStream), you could read 512 bytes of data from it, and you
|
||||
// would get the first 512 bytes of *compressed* data corresponding to what
|
||||
// iStream provided. Then you could resume at any time and ask for the next
|
||||
// 512 bytes of compressed data (or any other amount), etc.
|
||||
//
|
||||
// Therefore, these classes are well suited, among others, to compress or
|
||||
// decompress data streams while at the same time packing the result into
|
||||
// discrete chunks or packets with size constraints (you can think of the
|
||||
// process as making sausages :).
|
||||
//
|
||||
// The input being in each case an std::istream (for compressing as well as
|
||||
// for decompressing), it can be tied to an arbitrary source: a file with
|
||||
// sg_ifstream or std::ifstream, a memory buffer with std::istringstream or
|
||||
// std::stringstream, a TCP socket with a custom std::streambuf subclass[1]
|
||||
// to interface with the sockets API, etc.
|
||||
//
|
||||
// [1] Possibly wrapped in an std::istream.
|
||||
//
|
||||
// The stream buffer classes upon which ZlibCompressorIStream and
|
||||
// ZlibDecompressorIStream are built have an xsgetn() implementation that
|
||||
// avoids useless copies of data by asking zlib to write directly to the
|
||||
// destination buffer. This xsgetn() method is used when calling read() on
|
||||
// the std::istream subclasses, or sgetn() if you are using the stream
|
||||
// buffer classes directly (i.e., ZlibCompressorIStreambuf and
|
||||
// ZlibDecompressorIStreambuf). Other std::istream methods may instead rely
|
||||
// only on the internal buffer and the underflow() method, and therefore be
|
||||
// less efficient for large amounts of data. You may want to take a look at
|
||||
// zlibstream_test.cxx to see various ways of using these classes.
|
||||
//
|
||||
// In case you use std::istream& operator>>(std::istream&, std::string&) or
|
||||
// its overload friends, beware that it splits fields at spaces, and by
|
||||
// default ignores spaces at the beginning of a field (cf. std::skipws,
|
||||
// std::noskipws and friends). As far as I understand it, most of these
|
||||
// operators are mainly intended in the IOStreams library to be used to
|
||||
// read an int here, a double there, a space-delimited string afterwards,
|
||||
// etc. (the exception could be the overload writing to a stream buffer,
|
||||
// however it doesn't seem to be very efficient on my system with GNU
|
||||
// libstdc++ [it is not using xsgetn()], so beware also of this one if you
|
||||
// are handling large amounts of data). For moderately complex or large
|
||||
// input handling, I'd suggest to use std::istream methods such as read(),
|
||||
// gcount() and getline() (std::getline() can be useful too). Or directly
|
||||
// use the stream buffer classes, in particular with sgetn().
|
||||
|
||||
|
||||
namespace simgear
|
||||
{
|
||||
|
||||
enum class ZLibCompressionFormat {
|
||||
ZLIB = 0,
|
||||
GZIP,
|
||||
AUTODETECT
|
||||
};
|
||||
|
||||
enum class ZLibMemoryStrategy {
|
||||
FAVOR_MEMORY_OVER_SPEED = 0,
|
||||
FAVOR_SPEED_OVER_MEMORY
|
||||
};
|
||||
|
||||
// Abstract base class for both the compressor and decompressor stream buffers.
|
||||
class ZlibAbstractIStreambuf: public std::streambuf
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for ZlibAbstractIStreambuf.
|
||||
* @param iStream Input stream to read from.
|
||||
* @param path Optional path to the file corresponding to iStream,
|
||||
* if any. Only used for error messages.
|
||||
* @param inBuf Pointer to the input buffer (data read from iStream is
|
||||
* written there before being compressed or decompressed).
|
||||
* If nullptr, the buffer is allocated on the heap in the
|
||||
* constructor and deallocated in the destructor.
|
||||
* @param inBufSize Size of the input buffer, in chars.
|
||||
* @param outBuf Pointer to the output buffer. Data is read by zlib
|
||||
* from the input buffer, compressed or decompressed, and
|
||||
* the result is directly written to the output buffer.
|
||||
* If nullptr, the buffer is allocated on the heap in the
|
||||
* constructor and deallocated in the destructor.
|
||||
* @param outBufSize Size of the output buffer, in chars.
|
||||
* @param putbackSize Size of the putback area inside the output buffer, in
|
||||
* chars.
|
||||
*
|
||||
* It is required that putbackSize < outBufSize. It is guaranteed that,
|
||||
* if at least putbackSize chars have been read without any putback (or
|
||||
* unget) operation intermixed, then at least putbackSize chars can be
|
||||
* put back in sequence. If you don't need this feature, use zero for the
|
||||
* putbackSize value (the default) for best performance.
|
||||
*/
|
||||
explicit ZlibAbstractIStreambuf(std::istream& iStream,
|
||||
const SGPath& path = SGPath(),
|
||||
char* inBuf = nullptr,
|
||||
std::size_t inBufSize = 262144,
|
||||
char* outBuf = nullptr,
|
||||
std::size_t outBufSize = 262144,
|
||||
std::size_t putbackSize = 0);
|
||||
|
||||
// Alternate constructor with sink semantics for the “source” std::istream.
|
||||
// When used, the class takes ownership of the std::istream instance pointed
|
||||
// to by the first constructor argument, and keeps it alive as long as the
|
||||
// object this constructor is for is itself alive.
|
||||
explicit ZlibAbstractIStreambuf(std::unique_ptr<std::istream> iStream_p,
|
||||
const SGPath& path = SGPath(),
|
||||
char* inBuf = nullptr,
|
||||
std::size_t inBufSize = 262144,
|
||||
char* outBuf = nullptr,
|
||||
std::size_t outBufSize = 262144,
|
||||
std::size_t putbackSize = 0);
|
||||
|
||||
ZlibAbstractIStreambuf(const ZlibAbstractIStreambuf&) = delete;
|
||||
ZlibAbstractIStreambuf& operator=(const ZlibAbstractIStreambuf&) = delete;
|
||||
virtual ~ZlibAbstractIStreambuf();
|
||||
|
||||
protected:
|
||||
enum class OperationType {
|
||||
COMPRESSION = 0,
|
||||
DECOMPRESSION
|
||||
};
|
||||
|
||||
virtual OperationType operationType() const = 0;
|
||||
|
||||
// Either compress or decompress a chunk of data (depending on the
|
||||
// particular subclass implementation). The semantics are the same as for
|
||||
// zlib's inflate() and deflate() functions applied to member _zstream,
|
||||
// concerning 1) the return value and 2) where, and how much to read and
|
||||
// write (which thus depends on _zstream.avail_in, _zstream.next_in,
|
||||
// _zstream.avail_out and _zstream.next_out).
|
||||
virtual int zlibProcessData() = 0;
|
||||
|
||||
// The input stream, from which data is read before being processed by zlib
|
||||
std::istream& _iStream;
|
||||
// Pointer to the same, used when calling the constructor that takes an
|
||||
// std::unique_ptr<std::istream> as its first argument; empty
|
||||
// std::unique_ptr object otherwise.
|
||||
std::unique_ptr<std::istream> _iStream_p;
|
||||
|
||||
// Corresponding path, if any (default-constructed SGPath instance otherwise)
|
||||
const SGPath _path;
|
||||
// Structure used to communicate with zlib
|
||||
z_stream _zstream;
|
||||
|
||||
private:
|
||||
// Callback whose role is to refill the output buffer when it's empty and
|
||||
// the “client” tries to read more.
|
||||
virtual int underflow() override;
|
||||
// Optional override when subclassing std::streambuf. This is the most
|
||||
// efficient way of reading several characters (as soon as we've emptied the
|
||||
// output buffer, data is written by zlib directly to the destination
|
||||
// buffer).
|
||||
virtual std::streamsize xsgetn(char* dest, std::streamsize n) override;
|
||||
// Utility method for xsgetn()
|
||||
std::size_t xsgetn_preparePutbackArea(char* origGptr, char* dest,
|
||||
char* writePtr);
|
||||
// Make sure there is data to read in the input buffer, or signal EOF.
|
||||
bool getInputData();
|
||||
// Utility method for fillOutputBuffer()
|
||||
std::size_t fOB_remainingSpace(unsigned char* nextOutPtr) const;
|
||||
// Fill the output buffer (using zlib functions) as much as possible.
|
||||
char* fillOutputBuffer();
|
||||
// Utility method
|
||||
[[ noreturn ]] void handleZ_BUF_ERROR() const;
|
||||
|
||||
bool _allFinished = false;
|
||||
|
||||
// The buffers
|
||||
// ~~~~~~~~~~~
|
||||
//
|
||||
// The input buffer receives data obtained from _iStream, before it is
|
||||
// processed by zlib. In underflow(), zlib reads from this buffer it and
|
||||
// writes the resulting data(*) to the output buffer. Then we point the
|
||||
// standard std::streambuf pointers (gptr() and friends) directly towards
|
||||
// the data inside that output buffer. xsgetn() is even more optimized: it
|
||||
// first empties the output buffer, then makes zlib write the remaining data
|
||||
// directly to the destination area.
|
||||
//
|
||||
// (*) Compressed or decompressed, depending on the particular
|
||||
// implementation of zlibProcessData() in each subclass.
|
||||
char* _inBuf;
|
||||
const std::size_t _inBufSize;
|
||||
// _inBufEndPtr points right after the last data retrieved from _iStream and
|
||||
// stored into _inBuf. When zlib has read all such data, _zstream.next_in is
|
||||
// equal to _inBufEndPtr (after proper casting). Except in this particular
|
||||
// situation, only _zstream.next_in <= _inBufEndPtr is guaranteed.
|
||||
char* _inBufEndPtr;
|
||||
// Layout of the _outBuf buffer:
|
||||
//
|
||||
// |_outBuf <putback area> |_outBuf + _putbackSize |_outBuf + _outBufSize
|
||||
//
|
||||
// The first _putbackSize chars in _outBuf are reserved for the putback area
|
||||
// (right-aligned at _outBuf + _putbackSize). The actual output buffer thus
|
||||
// starts at _outBuf + _putbackSize. At any given time for callers of this
|
||||
// class, the number of characters that can be put back is gptr() - eback().
|
||||
// It may be lower than _putbackSize if we haven't read that many characters
|
||||
// yet. It may also be larger if gptr() > _outBuf + _putbackSize, i.e.,
|
||||
// when the buffer for pending data is non-empty.
|
||||
//
|
||||
// At any given time, callers should see:
|
||||
//
|
||||
// _outBuf <= eback() <= _outBuf + _putbackSize <= gptr() <= egptr()
|
||||
// <= _outBuf + _outBufSize
|
||||
//
|
||||
// (hoping this won't get out of sync with the code!)
|
||||
char *_outBuf;
|
||||
const std::size_t _outBufSize;
|
||||
// Space reserved for characters to be put back into the stream. Must be
|
||||
// strictly smaller than _outBufSize (this is checked in the constructor).
|
||||
// It is guaranteed that this number of chars can be put back, except of
|
||||
// course if we haven't read that many characters from the input stream yet.
|
||||
// If characters are buffered in _outBuf[2], then it may be that more
|
||||
// characters than _putbackSize can be put back (it is essentially a matter
|
||||
// for std::streambuf of decreasing the “next pointer for the input
|
||||
// sequence”, i.e., the one returned by gptr()).
|
||||
//
|
||||
// [2] In the [_outBuf + _putbackSize, _outBuf + _outBufSize) area.
|
||||
const std::size_t _putbackSize;
|
||||
|
||||
// Since the constructor optionally allocates memory for the input and
|
||||
// output buffers, these members allow the destructor to know which buffers
|
||||
// have to be deallocated, if any.
|
||||
bool _inBufMustBeFreed = false;
|
||||
bool _outBufMustBeFreed = false;
|
||||
};
|
||||
|
||||
|
||||
// Stream buffer class for compressing data. Input data is obtained from an
|
||||
// std::istream instance; the corresponding compressed data can be read using
|
||||
// the standard std::streambuf read interface (mainly: sbumpc(), sgetc(),
|
||||
// snextc(), sgetn(), sputbackc(), sungetc()). Input, uncompressed data is
|
||||
// “pulled” as needed for the amount of compressed data requested by the
|
||||
// “client” using the methods I just listed.
|
||||
class ZlibCompressorIStreambuf: public ZlibAbstractIStreambuf
|
||||
{
|
||||
public:
|
||||
// Same parameters as for ZlibAbstractIStreambuf, except:
|
||||
//
|
||||
// compressionLevel: in the [0,9] range. 0 means no compression at all.
|
||||
// Levels 1 to 9 yield compressed data, with 1 giving
|
||||
// the highest compression speed but worst compression
|
||||
// ratio, and 9 the highest compression ratio but lowest
|
||||
// compression speed.
|
||||
// format either ZLibCompressionFormat::ZLIB or
|
||||
// ZLibCompressionFormat::GZIP
|
||||
// memStrategy either ZLibMemoryStrategy::FAVOR_MEMORY_OVER_SPEED or
|
||||
// ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY
|
||||
explicit ZlibCompressorIStreambuf(
|
||||
std::istream& iStream,
|
||||
const SGPath& path = SGPath(),
|
||||
int compressionLevel = Z_DEFAULT_COMPRESSION,
|
||||
ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB,
|
||||
ZLibMemoryStrategy memStrategy = ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY,
|
||||
char* inBuf = nullptr,
|
||||
std::size_t inBufSize = 262144,
|
||||
char* outBuf = nullptr,
|
||||
std::size_t outBufSize = 262144,
|
||||
std::size_t putbackSize = 0);
|
||||
|
||||
// Alternate constructor with sink semantics for the “source” std::istream.
|
||||
explicit ZlibCompressorIStreambuf(
|
||||
std::unique_ptr<std::istream> _iStream_p,
|
||||
const SGPath& path = SGPath(),
|
||||
int compressionLevel = Z_DEFAULT_COMPRESSION,
|
||||
ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB,
|
||||
ZLibMemoryStrategy memStrategy = ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY,
|
||||
char* inBuf = nullptr,
|
||||
std::size_t inBufSize = 262144,
|
||||
char* outBuf = nullptr,
|
||||
std::size_t outBufSize = 262144,
|
||||
std::size_t putbackSize = 0);
|
||||
|
||||
ZlibCompressorIStreambuf(const ZlibCompressorIStreambuf&) = delete;
|
||||
ZlibCompressorIStreambuf& operator=(const ZlibCompressorIStreambuf&) = delete;
|
||||
virtual ~ZlibCompressorIStreambuf();
|
||||
|
||||
protected:
|
||||
virtual OperationType operationType() const override;
|
||||
// Initialize the z_stream struct used by zlib
|
||||
void zStreamInit(int compressionLevel, ZLibCompressionFormat format,
|
||||
ZLibMemoryStrategy memStrategy);
|
||||
// Call zlib's deflate() function to compress data.
|
||||
virtual int zlibProcessData() override;
|
||||
};
|
||||
|
||||
|
||||
// Stream buffer class for decompressing data. Input data is obtained from an
|
||||
// std::istream instance; the corresponding decompressed data can be read
|
||||
// using the standard std::streambuf read interface (mainly: sbumpc(),
|
||||
// sgetc(), snextc(), sgetn(), sputbackc(), sungetc()). Input, compressed data
|
||||
// is “pulled” as needed for the amount of uncompressed data requested by the
|
||||
// “client” using the methods I just listed.
|
||||
class ZlibDecompressorIStreambuf: public ZlibAbstractIStreambuf
|
||||
{
|
||||
public:
|
||||
// Same parameters as for ZlibAbstractIStreambuf, except:
|
||||
//
|
||||
// format ZLibCompressionFormat::ZLIB,
|
||||
// ZLibCompressionFormat::GZIP or
|
||||
// ZLibCompressionFormat::AUTODETECT
|
||||
explicit ZlibDecompressorIStreambuf(
|
||||
std::istream& iStream,
|
||||
const SGPath& path = SGPath(),
|
||||
ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB,
|
||||
char* inBuf = nullptr,
|
||||
std::size_t inBufSize = 262144,
|
||||
char* outBuf = nullptr,
|
||||
std::size_t outBufSize = 262144,
|
||||
std::size_t putbackSize = 0); // default optimized for speed
|
||||
|
||||
// Alternate constructor with sink semantics for the “source” std::istream.
|
||||
explicit ZlibDecompressorIStreambuf(
|
||||
std::unique_ptr<std::istream> _iStream_p,
|
||||
const SGPath& path = SGPath(),
|
||||
ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB,
|
||||
char* inBuf = nullptr,
|
||||
std::size_t inBufSize = 262144,
|
||||
char* outBuf = nullptr,
|
||||
std::size_t outBufSize = 262144,
|
||||
std::size_t putbackSize = 0); // default optimized for speed
|
||||
|
||||
ZlibDecompressorIStreambuf(const ZlibDecompressorIStreambuf&) = delete;
|
||||
ZlibDecompressorIStreambuf& operator=(const ZlibDecompressorIStreambuf&)
|
||||
= delete;
|
||||
virtual ~ZlibDecompressorIStreambuf();
|
||||
|
||||
protected:
|
||||
virtual OperationType operationType() const override;
|
||||
void zStreamInit(ZLibCompressionFormat format);
|
||||
virtual int zlibProcessData() override;
|
||||
};
|
||||
|
||||
// std::istream subclass for compressing data. Input data is obtained from an
|
||||
// std::istream instance; the corresponding compressed data can be read using
|
||||
// the standard std::istream interface (read(), readsome(), gcount(), get(),
|
||||
// getline(), operator>>(), peek(), putback(), ignore(), unget()... plus
|
||||
// operator overloads such as istream& operator>>(istream&, string&) as
|
||||
// defined in <string>, and std::getline()). Input, uncompressed data is
|
||||
// “pulled” as needed for the amount of compressed data requested by the
|
||||
// “client”.
|
||||
//
|
||||
// To get data efficiently from an instance of this class, use its read()
|
||||
// method (typically in conjunction with gcount(), inside a loop).
|
||||
class ZlibCompressorIStream: public std::istream
|
||||
{
|
||||
public:
|
||||
// Same parameters as for ZlibCompressorIStreambuf
|
||||
explicit ZlibCompressorIStream(
|
||||
std::istream& iStream,
|
||||
const SGPath& path = SGPath(),
|
||||
int compressionLevel = Z_DEFAULT_COMPRESSION,
|
||||
ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB,
|
||||
ZLibMemoryStrategy memStrategy = ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY,
|
||||
char* inBuf = nullptr,
|
||||
std::size_t inBufSize = 262144,
|
||||
char* outBuf = nullptr,
|
||||
std::size_t outBufSize = 262144,
|
||||
std::size_t putbackSize = 0); // default optimized for speed
|
||||
|
||||
// Alternate constructor with sink semantics for the “source” std::istream.
|
||||
explicit ZlibCompressorIStream(
|
||||
std::unique_ptr<std::istream> _iStream_p,
|
||||
const SGPath& path = SGPath(),
|
||||
int compressionLevel = Z_DEFAULT_COMPRESSION,
|
||||
ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB,
|
||||
ZLibMemoryStrategy memStrategy = ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY,
|
||||
char* inBuf = nullptr,
|
||||
std::size_t inBufSize = 262144,
|
||||
char* outBuf = nullptr,
|
||||
std::size_t outBufSize = 262144,
|
||||
std::size_t putbackSize = 0); // default optimized for speed
|
||||
|
||||
ZlibCompressorIStream(const ZlibCompressorIStream&) = delete;
|
||||
ZlibCompressorIStream& operator=(const ZlibCompressorIStream&) = delete;
|
||||
virtual ~ZlibCompressorIStream();
|
||||
|
||||
private:
|
||||
ZlibCompressorIStreambuf _streamBuf;
|
||||
};
|
||||
|
||||
// std::istream subclass for decompressing data. Input data is obtained from
|
||||
// an std::istream instance; the corresponding decompressed data can be read
|
||||
// using the standard std::istream interface (read(), readsome(), gcount(),
|
||||
// get(), getline(), operator>>(), peek(), putback(), ignore(), unget()...
|
||||
// plus operator overloads such as istream& operator>>(istream&, string&) as
|
||||
// defined in <string>, and std::getline()). Input, compressed data is
|
||||
// “pulled” as needed for the amount of uncompressed data requested by the
|
||||
// “client”.
|
||||
//
|
||||
// To get data efficiently from an instance of this class, use its read()
|
||||
// method (typically in conjunction with gcount(), inside a loop).
|
||||
class ZlibDecompressorIStream: public std::istream
|
||||
{
|
||||
public:
|
||||
// Same parameters as for ZlibDecompressorIStreambuf
|
||||
explicit ZlibDecompressorIStream(
|
||||
std::istream& iStream,
|
||||
const SGPath& path = SGPath(),
|
||||
ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB,
|
||||
char* inBuf = nullptr,
|
||||
std::size_t inBufSize = 262144,
|
||||
char* outBuf = nullptr,
|
||||
std::size_t outBufSize = 262144,
|
||||
std::size_t putbackSize = 0); // default optimized for speed
|
||||
|
||||
// Alternate constructor with sink semantics for the “source” std::istream.
|
||||
explicit ZlibDecompressorIStream(
|
||||
std::unique_ptr<std::istream> _iStream_p,
|
||||
const SGPath& path = SGPath(),
|
||||
ZLibCompressionFormat format = ZLibCompressionFormat::ZLIB,
|
||||
char* inBuf = nullptr,
|
||||
std::size_t inBufSize = 262144,
|
||||
char* outBuf = nullptr,
|
||||
std::size_t outBufSize = 262144,
|
||||
std::size_t putbackSize = 0); // default optimized for speed
|
||||
|
||||
ZlibDecompressorIStream(const ZlibDecompressorIStream&) = delete;
|
||||
ZlibDecompressorIStream& operator=(const ZlibDecompressorIStream&) = delete;
|
||||
virtual ~ZlibDecompressorIStream();
|
||||
|
||||
private:
|
||||
ZlibDecompressorIStreambuf _streamBuf;
|
||||
};
|
||||
|
||||
} // of namespace simgear
|
||||
|
||||
#endif // of SG_ZLIBSTREAM_HXX
|
||||
732
simgear/io/iostreams/zlibstream_test.cxx
Normal file
732
simgear/io/iostreams/zlibstream_test.cxx
Normal file
@@ -0,0 +1,732 @@
|
||||
// -*- coding: utf-8 -*-
|
||||
//
|
||||
// zlibstream_test.cxx --- Automated tests for zlibstream.cxx / zlibstream.hxx
|
||||
//
|
||||
// Copyright (C) 2017 Florent Rougon
|
||||
//
|
||||
// This library is free software; you can redistribute it and/or
|
||||
// modify it under the terms of the GNU Library General Public
|
||||
// License as published by the Free Software Foundation; either
|
||||
// version 2 of the License, or (at your option) any later version.
|
||||
//
|
||||
// This library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
// Library General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Library General Public
|
||||
// License along with this library; if not, write to the Free Software
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
// MA 02110-1301 USA.
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include <ios> // std::basic_ios, std::streamsize...
|
||||
#include <iostream> // std::ios_base, std::cerr, etc.
|
||||
#include <sstream>
|
||||
#include <array>
|
||||
#include <random>
|
||||
#include <memory> // std::unique_ptr
|
||||
#include <utility> // std::move()
|
||||
#include <limits> // std::numeric_limits
|
||||
#include <type_traits> // std::make_unsigned()
|
||||
#include <functional> // std::bind()
|
||||
#include <cassert>
|
||||
#include <cstdlib> // EXIT_SUCCESS
|
||||
#include <cstddef> // std::size_t
|
||||
#include <cstring> // strcmp()
|
||||
|
||||
#include <zlib.h> // Z_BEST_COMPRESSION
|
||||
|
||||
#include <simgear/misc/test_macros.hxx>
|
||||
#include <simgear/io/iostreams/sgstream.hxx>
|
||||
#include <simgear/io/iostreams/zlibstream.hxx>
|
||||
#include <simgear/misc/sg_path.hxx>
|
||||
#include <simgear/misc/sg_dir.hxx>
|
||||
|
||||
using std::string;
|
||||
using std::cout;
|
||||
using std::cerr;
|
||||
using traits = std::char_traits<char>;
|
||||
|
||||
typedef typename std::make_unsigned<std::streamsize>::type uStreamSize;
|
||||
|
||||
// Safely convert a non-negative std::streamsize into an std::size_t. If
|
||||
// impossible, bail out.
|
||||
static std::size_t streamsizeToSize_t(std::streamsize n)
|
||||
{
|
||||
SG_CHECK_GE(n, 0);
|
||||
SG_CHECK_LE(static_cast<uStreamSize>(n),
|
||||
std::numeric_limits<std::size_t>::max());
|
||||
|
||||
return static_cast<std::size_t>(n);
|
||||
}
|
||||
|
||||
// In many tests below, I use very small buffer sizes. Of course, this is bad
|
||||
// for performance. The reason I do it this way is simply because it better
|
||||
// exercises the code we want to *test* here (we are more likely to find bugs
|
||||
// if the buffer(s) has/have to be refilled one or more times). Similarly, if
|
||||
// you don't need the putback feature in non-test code, best performance is
|
||||
// achieved with putback size = 0.
|
||||
//
|
||||
// I suggest reading test_IStreamConstructorWithSinkSemantics() below to see
|
||||
// how to use the classes efficiently.
|
||||
|
||||
static std::default_random_engine randomNumbersGenerator;
|
||||
|
||||
// Sample string for tests
|
||||
static const string lipsum = "\
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque congue ornare\n\
|
||||
congue. Mauris mollis est et porttitor condimentum. Vivamus laoreet blandit\n\
|
||||
odio eget consectetur. Etiam quis magna eu enim luctus pretium. In et\n\
|
||||
tristique nunc, non efficitur metus. Nullam efficitur tristique velit.\n\
|
||||
Praesent et luctus nunc. Mauris eros eros, rutrum at molestie quis, egestas et\n\
|
||||
lorem. Ut nulla turpis, eleifend sed mauris ac, faucibus molestie nulla.\n\
|
||||
Quisque viverra vel turpis nec efficitur. Proin non rutrum velit. Nam sodales\n\
|
||||
metus felis, eu pharetra velit posuere ut.\n\
|
||||
\n\
|
||||
Suspendisse pellentesque tincidunt ligula et pretium. Etiam id justo mauris.\n\
|
||||
Aenean porta, sapien in suscipit tristique, metus diam malesuada dui, in porta\n\
|
||||
felis mi non felis. Etiam vel aliquam leo, non vehicula magna. Proin justo\n\
|
||||
massa, ultrices at porta eu, tempor in ligula. Duis enim ipsum, dictum quis\n\
|
||||
ultricies et, tempus eu libero. Morbi vulputate libero ut dolor rutrum, a\n\
|
||||
imperdiet nibh egestas. Phasellus finibus massa vel tempus hendrerit. Nulla\n\
|
||||
lobortis est non ligula viverra, quis egestas ante hendrerit. Pellentesque\n\
|
||||
finibus mollis blandit. In ac sollicitudin mauris, eget dignissim mauris.";
|
||||
|
||||
// Utility function: generate a random string whose length is in the
|
||||
// [minLen, maxLen] range.
|
||||
string randomString(string::size_type minLen, string::size_type maxLen)
|
||||
{
|
||||
std::uniform_int_distribution<string::size_type> sLenDist(minLen, maxLen);
|
||||
std::uniform_int_distribution<int> byteDist(0, 255);
|
||||
auto randomByte = std::bind(byteDist, randomNumbersGenerator);
|
||||
|
||||
string::size_type len = sLenDist(randomNumbersGenerator);
|
||||
string str;
|
||||
|
||||
while (str.size() < len) {
|
||||
str += traits::to_char_type(randomByte());
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
// Utility function: perform a compression-decompression cycle on the input
|
||||
// string using the stream buffer classes:
|
||||
//
|
||||
// 1) Compress the input string, write the result to a temporary file using
|
||||
// std::ostream& operator<<(streambuf* sb), where 'sb' points to a
|
||||
// ZlibCompressorIStreambuf instance.
|
||||
//
|
||||
// 2) Close the temporary file to make sure all the data is flushed.
|
||||
//
|
||||
// 3) Create a ZlibDecompressorIStreambuf instance reading from the
|
||||
// temporary file, then decompress to an std::ostringstream using
|
||||
// std::ostream& operator<<(streambuf* sb), where 'sb' points to our
|
||||
// ZlibDecompressorIStreambuf instance.
|
||||
//
|
||||
// 4) Compare the result with the input string.
|
||||
//
|
||||
// Of course, it is possible to do this without any temporary file, even
|
||||
// without any std::stringstream or such to hold the intermediate, compressed
|
||||
// data. See the other tests for actual compressor + decompressor pipelines.
|
||||
void pipeCompOrDecompIStreambufIntoOStream(const string& input)
|
||||
{
|
||||
// Test “std::ostream << &ZlibCompressorIStreambuf”
|
||||
std::istringstream input_ss(input);
|
||||
simgear::ZlibCompressorIStreambuf compSBuf(input_ss, SGPath(), 9);
|
||||
simgear::Dir tmpDir = simgear::Dir::tempDir("FlightGear");
|
||||
tmpDir.setRemoveOnDestroy();
|
||||
SGPath path = tmpDir.path() / "testFile.dat";
|
||||
sg_ofstream oFile(path,
|
||||
std::ios::binary | std::ios::trunc | std::ios::out);
|
||||
|
||||
oFile << &compSBuf;
|
||||
// Make sure the compressed stream is flushed, otherwise when we read the
|
||||
// file back, decompression is likely to fail with Z_BUF_ERROR.
|
||||
oFile.close();
|
||||
SG_CHECK_EQUAL(compSBuf.sgetc(), EOF);
|
||||
|
||||
// Read back and decompress the data we've written to 'path', testing
|
||||
// “std::ostream << &ZlibDecompressorIStreambuf”
|
||||
sg_ifstream iFile(path, std::ios::binary | std::ios::in);
|
||||
simgear::ZlibDecompressorIStreambuf decompSBuf(iFile, path);
|
||||
std::ostringstream roundTripResult_ss;
|
||||
// This is also possible, though maybe not as good for error detection:
|
||||
//
|
||||
// decompSBuf >> roundTripResult_ss.rdbuf();
|
||||
roundTripResult_ss << &decompSBuf;
|
||||
SG_CHECK_EQUAL(decompSBuf.sgetc(), EOF);
|
||||
SG_CHECK_EQUAL(roundTripResult_ss.str(), input);
|
||||
}
|
||||
|
||||
// Perform a compression-decompression cycle on bunch of strings, using the
|
||||
// stream buffer classes directly (not the std::istream subclasses).
|
||||
void test_pipeCompOrDecompIStreambufIntoOStream()
|
||||
{
|
||||
cerr << "Compression-decompression cycles using the stream buffer classes "
|
||||
"directly\n";
|
||||
|
||||
pipeCompOrDecompIStreambufIntoOStream(""); // empty string
|
||||
pipeCompOrDecompIStreambufIntoOStream("a");
|
||||
pipeCompOrDecompIStreambufIntoOStream("lorem ipsum");
|
||||
|
||||
for (int i=0; i < 10; i++) {
|
||||
pipeCompOrDecompIStreambufIntoOStream(randomString(0, 20));
|
||||
}
|
||||
|
||||
for (int i=0; i < 10; i++) {
|
||||
pipeCompOrDecompIStreambufIntoOStream(randomString(21, 1000));
|
||||
}
|
||||
|
||||
assert(std::numeric_limits<string::size_type>::max() >= 65535);
|
||||
for (int i=0; i < 10; i++) {
|
||||
pipeCompOrDecompIStreambufIntoOStream(randomString(1000, 65535));
|
||||
}
|
||||
}
|
||||
|
||||
void test_StreambufBasicOperations()
|
||||
{
|
||||
cerr << "Testing basic operations on ZlibDecompressorIStreambuf\n";
|
||||
|
||||
const string text = "0123456789abcdefghijklmnopqrstuvwxyz\nABCDEF\nGHIJK "
|
||||
"LMNOPQ";
|
||||
std::istringstream text_ss(text);
|
||||
|
||||
static constexpr std::size_t compInBufSize = 6;
|
||||
static constexpr std::size_t compOutBufSize = 4;
|
||||
static constexpr std::size_t compPutbackSize = 0;
|
||||
simgear::ZlibCompressorIStreambuf compSBuf(
|
||||
text_ss, SGPath(), 8, simgear::ZLibCompressionFormat::ZLIB,
|
||||
simgear::ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY,
|
||||
nullptr, compInBufSize, nullptr, compOutBufSize, compPutbackSize);
|
||||
std::stringstream compressedOutput_ss;
|
||||
compressedOutput_ss << &compSBuf;
|
||||
|
||||
static constexpr std::size_t decompInBufSize = 5;
|
||||
static constexpr std::size_t decompOutBufSize = 4;
|
||||
static constexpr std::size_t decompPutbackSize = 2;
|
||||
simgear::ZlibDecompressorIStreambuf decompSBuf(
|
||||
compressedOutput_ss, SGPath(), simgear::ZLibCompressionFormat::ZLIB,
|
||||
nullptr, decompInBufSize, nullptr, decompOutBufSize, decompPutbackSize);
|
||||
|
||||
int ch = decompSBuf.sgetc();
|
||||
SG_VERIFY(ch != EOF && traits::to_char_type(ch) == '0');
|
||||
ch = decompSBuf.sbumpc();
|
||||
SG_VERIFY(ch != EOF && traits::to_char_type(ch) == '0');
|
||||
ch = decompSBuf.sbumpc();
|
||||
SG_VERIFY(ch != EOF && traits::to_char_type(ch) == '1');
|
||||
ch = decompSBuf.snextc();
|
||||
SG_VERIFY(ch != EOF && traits::to_char_type(ch) == '3');
|
||||
ch = decompSBuf.sbumpc();
|
||||
SG_VERIFY(ch != EOF && traits::to_char_type(ch) == '3');
|
||||
ch = decompSBuf.sbumpc();
|
||||
SG_VERIFY(ch != EOF && traits::to_char_type(ch) == '4');
|
||||
ch = decompSBuf.sputbackc('4');
|
||||
SG_VERIFY(ch != EOF && traits::to_char_type(ch) == '4');
|
||||
ch = decompSBuf.sputbackc('u'); // doesn't match what we read from the stream
|
||||
SG_VERIFY(ch == EOF);
|
||||
ch = decompSBuf.sputbackc('3'); // this one does
|
||||
SG_VERIFY(ch != EOF && traits::to_char_type(ch) == '3');
|
||||
|
||||
static constexpr std::streamsize bufSize = 10;
|
||||
char buf[bufSize];
|
||||
// Most efficient way (with the underlying xsgetn()) to read several chars
|
||||
// at once.
|
||||
std::streamsize n = decompSBuf.sgetn(buf, bufSize);
|
||||
SG_CHECK_EQUAL(n, bufSize);
|
||||
SG_CHECK_EQUAL(string(buf, static_cast<std::size_t>(bufSize)),
|
||||
"3456789abc");
|
||||
|
||||
ch = decompSBuf.sungetc(); // same as sputbackc(), except no value to check
|
||||
SG_VERIFY(ch != EOF && traits::to_char_type(ch) == 'c');
|
||||
ch = decompSBuf.sungetc();
|
||||
SG_VERIFY(ch != EOF && traits::to_char_type(ch) == 'b');
|
||||
ch = decompSBuf.sbumpc();
|
||||
SG_VERIFY(ch != EOF && traits::to_char_type(ch) == 'b');
|
||||
ch = decompSBuf.sputbackc('b'); // this one does
|
||||
SG_VERIFY(ch != EOF && traits::to_char_type(ch) == 'b');
|
||||
|
||||
n = decompSBuf.sgetn(buf, bufSize);
|
||||
SG_CHECK_EQUAL(n, bufSize);
|
||||
SG_CHECK_EQUAL(string(buf, static_cast<std::size_t>(bufSize)),
|
||||
"bcdefghijk");
|
||||
|
||||
ch = decompSBuf.sungetc();
|
||||
SG_VERIFY(ch != EOF && traits::to_char_type(ch) == 'k');
|
||||
|
||||
static char buf2[64];
|
||||
n = decompSBuf.sgetn(buf2, sizeof(buf2));
|
||||
SG_CHECK_EQUAL(n, 36);
|
||||
SG_CHECK_EQUAL(string(buf2, 36), "klmnopqrstuvwxyz\nABCDEF\nGHIJK LMNOPQ");
|
||||
|
||||
ch = decompSBuf.sbumpc();
|
||||
SG_CHECK_EQUAL(ch, EOF);
|
||||
}
|
||||
|
||||
// Utility function: take a string as input to compress and return the
|
||||
// compressed output as a string.
|
||||
string compress(const string& dataToCompress,
|
||||
simgear::ZLibCompressionFormat compressionFormat,
|
||||
int compressionLevel,
|
||||
simgear::ZLibMemoryStrategy memStrategy,
|
||||
std::size_t putbackSize,
|
||||
SGPath path = SGPath())
|
||||
{
|
||||
// Static storage is only okay for very small sizes like these, and because
|
||||
// we won't call the function from several threads at the same time. Plain
|
||||
// char arrays would be fine here, but I'll use std::array to show it can be
|
||||
// used here too.
|
||||
static std::array<char, 7> inBuf;
|
||||
static std::array<char, 7> outBuf;
|
||||
|
||||
std::istringstream iss(dataToCompress);
|
||||
simgear::ZlibCompressorIStream compressor(
|
||||
iss, path, compressionLevel, compressionFormat, memStrategy,
|
||||
&inBuf.front(), inBuf.size(), &outBuf.front(), outBuf.size(),
|
||||
putbackSize);
|
||||
compressor.exceptions(std::ios_base::badbit); // throw if badbit is set
|
||||
std::ostringstream compressedData_ss;
|
||||
compressor >> compressedData_ss.rdbuf();
|
||||
|
||||
return compressedData_ss.str();
|
||||
}
|
||||
|
||||
// Test simgear::ZlibDecompressorIStreambuf::[x]sgetn(), asking the largest
|
||||
// possible amount of chars every time it is called (i.e., the largest value
|
||||
// that can be represented by std::streamsize).
|
||||
void test_ZlibDecompressorIStreambuf_readLargestPossibleAmount()
|
||||
{
|
||||
// Nothing special with these values
|
||||
constexpr std::size_t maxDataSize = 8192;
|
||||
std::istringstream input_ss(randomString(4096, maxDataSize));
|
||||
|
||||
simgear::ZlibCompressorIStream compIStream(
|
||||
input_ss, // input stream
|
||||
SGPath(), // this stream is not associated to a file
|
||||
9, // compression level
|
||||
simgear::ZLibCompressionFormat::ZLIB,
|
||||
simgear::ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY,
|
||||
nullptr, // dynamically allocate the input buffer
|
||||
230, // input buffer size
|
||||
nullptr, // dynamically allocate the output buffer
|
||||
120, // output buffer size
|
||||
1 // putback size
|
||||
);
|
||||
|
||||
// Decompressor stream buffer (std::streambuf subclass) that gets input data
|
||||
// from our compressor 'compIStream' (std::istream subclass)
|
||||
simgear::ZlibDecompressorIStreambuf decompSBuf(
|
||||
compIStream, SGPath(), simgear::ZLibCompressionFormat::ZLIB,
|
||||
nullptr, 150, nullptr, 175, 2);
|
||||
|
||||
std::unique_ptr<char[]> buf(new char[maxDataSize]);
|
||||
std::ostringstream roundTripResult_ss;
|
||||
std::streamsize totalCharsToRead = input_ss.str().size();
|
||||
|
||||
while (totalCharsToRead > 0) {
|
||||
// Ask sgetn() the largest possible amount of chars. Of course, we know we
|
||||
// can't get more than maxDataSize, but this does exercise the code in
|
||||
// interesting ways due to the various types involved (zlib's uInt,
|
||||
// std::size_t and std::streamsize, which have various sizes depending on
|
||||
// the platform).
|
||||
std::streamsize nbCharsRead = decompSBuf.sgetn(
|
||||
&buf[0], std::numeric_limits<std::streamsize>::max());
|
||||
if (nbCharsRead == 0) {
|
||||
break; // no more data
|
||||
}
|
||||
|
||||
// The conversion to std::size_t is safe because decompSBuf.sgetn()
|
||||
// returned a non-negative value which, in this case, can't exceed
|
||||
// maxDataSize.
|
||||
roundTripResult_ss << string(&buf[0], streamsizeToSize_t((nbCharsRead)));
|
||||
}
|
||||
|
||||
SG_CHECK_EQUAL(decompSBuf.sgetc(), EOF);
|
||||
SG_CHECK_EQUAL(roundTripResult_ss.str(), input_ss.str());
|
||||
}
|
||||
|
||||
void test_formattedInputFromDecompressor()
|
||||
{
|
||||
cerr << "Testing ZlibDecompressorIStream >> std::string\n";
|
||||
|
||||
static char inBuf[6];
|
||||
static char outBuf[15];
|
||||
string compressed = compress(
|
||||
lipsum, simgear::ZLibCompressionFormat::ZLIB, Z_BEST_COMPRESSION,
|
||||
simgear::ZLibMemoryStrategy::FAVOR_MEMORY_OVER_SPEED, /* putback size */ 0);
|
||||
std::istringstream compressed_ss(compressed);
|
||||
|
||||
simgear::ZlibDecompressorIStream decompressor(
|
||||
compressed_ss, SGPath(), simgear::ZLibCompressionFormat::ZLIB,
|
||||
inBuf, sizeof(inBuf), outBuf, sizeof(outBuf), /* putback size */ 1);
|
||||
decompressor.exceptions(std::ios_base::badbit); // throw if badbit is set
|
||||
|
||||
int count = 0;
|
||||
string word;
|
||||
|
||||
while (decompressor >> word) {
|
||||
count++;
|
||||
}
|
||||
|
||||
SG_CHECK_EQUAL(count, 175); // Number of words in 'lipsum'
|
||||
// If set, badbit would have caused an exception to be raised
|
||||
SG_VERIFY(!decompressor.bad());
|
||||
|
||||
if (!decompressor.eof()) {
|
||||
assert(decompressor.fail());
|
||||
cerr << "Decompressor: stream extraction (operator>>) failed before "
|
||||
"reaching EOF.\n";
|
||||
SG_TEST_FAIL("Did not expect operator>> to fail with an std::string "
|
||||
"argument.");
|
||||
}
|
||||
}
|
||||
|
||||
void test_ZlibDecompressorIStream_readPutbackEtc()
|
||||
{
|
||||
cerr << "Testing many operations on ZlibDecompressorIStream (read(), "
|
||||
"putback(), etc.\n";
|
||||
|
||||
static char compInBuf[4];
|
||||
static char compOutBuf[6];
|
||||
static char decompInBuf[8];
|
||||
static char decompOutBuf[5];
|
||||
const string text = "0123456789abcdefghijklmnopqrstuvwxyz\nABCDEF\nGHIJK "
|
||||
"LMNOPQ";
|
||||
std::istringstream text_ss(text);
|
||||
|
||||
simgear::ZlibCompressorIStream compressor(
|
||||
text_ss, SGPath(), Z_BEST_COMPRESSION,
|
||||
simgear::ZLibCompressionFormat::ZLIB,
|
||||
simgear::ZLibMemoryStrategy::FAVOR_MEMORY_OVER_SPEED,
|
||||
compInBuf, sizeof(compInBuf), compOutBuf, sizeof(compOutBuf),
|
||||
/* putback size */ 0);
|
||||
compressor.exceptions(std::ios_base::badbit); // throw if badbit is set
|
||||
|
||||
// Use the compressor (subclass of std::istream) as input to the decompressor
|
||||
simgear::ZlibDecompressorIStream decompressor(
|
||||
compressor, SGPath(), simgear::ZLibCompressionFormat::ZLIB,
|
||||
decompInBuf, sizeof(decompInBuf), decompOutBuf, sizeof(decompOutBuf),
|
||||
/* putback size */ 3);
|
||||
decompressor.exceptions(std::ios_base::badbit);
|
||||
|
||||
{
|
||||
static std::array<char, 17> buf;
|
||||
decompressor.read(&buf.front(), 10);
|
||||
SG_VERIFY(decompressor.good() && decompressor.gcount() == 10);
|
||||
SG_CHECK_EQUAL(string(&buf.front(), 10), "0123456789");
|
||||
|
||||
SG_VERIFY(decompressor.putback('9'));
|
||||
SG_VERIFY(decompressor.putback('8'));
|
||||
SG_VERIFY(decompressor.putback('7'));
|
||||
|
||||
SG_VERIFY(decompressor.get(buf[10]));
|
||||
SG_VERIFY(decompressor.read(&buf[11], 6));
|
||||
SG_CHECK_EQUAL(string(&buf.front(), 17), "0123456789789abcd");
|
||||
}
|
||||
|
||||
{
|
||||
bool gotException = false;
|
||||
try {
|
||||
// 'Z' is not the last character read from the stream
|
||||
decompressor.putback('Z');
|
||||
} catch (std::ios_base::failure) {
|
||||
gotException = true;
|
||||
} catch (const std::exception& e) {
|
||||
// gcc fails to catch std::ios_base::failure due to an inconsistent C++11
|
||||
// ABI between headers and libraries. See bug#66145 for more details.
|
||||
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66145
|
||||
if (!strcmp(e.what(), "basic_ios::clear"))
|
||||
gotException = true;
|
||||
else
|
||||
throw e;
|
||||
}
|
||||
SG_VERIFY(gotException && decompressor.bad());
|
||||
}
|
||||
|
||||
{
|
||||
int c;
|
||||
|
||||
decompressor.clear();
|
||||
// Check we can resume normally now that we've cleared the error state flags
|
||||
c = decompressor.get();
|
||||
SG_VERIFY(c != traits::eof() && traits::to_char_type(c) == 'e');
|
||||
|
||||
// unget() and get() in sequence
|
||||
SG_VERIFY(decompressor.unget());
|
||||
c = decompressor.get();
|
||||
SG_VERIFY(c != traits::eof() && traits::to_char_type(c)== 'e');
|
||||
|
||||
// peek() looks at, but doesn't consume
|
||||
c = decompressor.peek();
|
||||
SG_VERIFY(c != traits::eof() && traits::to_char_type(c) == 'f');
|
||||
c = decompressor.get();
|
||||
SG_VERIFY(c != traits::eof() && traits::to_char_type(c)== 'f');
|
||||
}
|
||||
|
||||
{
|
||||
static std::array<char, 21> buf; // 20 chars + terminating NUL char
|
||||
|
||||
// std::istream::getline() will be stopped by \n after reading 20 chars
|
||||
SG_VERIFY(decompressor.getline(&buf.front(), 25));
|
||||
SG_VERIFY(decompressor.gcount() == 21 &&
|
||||
!strcmp(&buf.front(), "ghijklmnopqrstuvwxyz"));
|
||||
|
||||
string str;
|
||||
// std::getline() will be stopped by \n after 7 chars have been extracted,
|
||||
// namely 6 chars plus the \n which is extracted and discarded.
|
||||
SG_VERIFY(std::getline(decompressor, str));
|
||||
SG_CHECK_EQUAL(str, "ABCDEF");
|
||||
|
||||
SG_VERIFY(decompressor.ignore(2));
|
||||
SG_VERIFY(decompressor >> str);
|
||||
SG_CHECK_EQUAL(str, "IJK");
|
||||
|
||||
static char buf2[5];
|
||||
// Read up to sizeof(buf) chars, without waiting if not that many are
|
||||
// immediately avaiable.
|
||||
int nbCharsRead = decompressor.readsome(buf2, sizeof(buf2));
|
||||
|
||||
string rest(buf2, nbCharsRead);
|
||||
do {
|
||||
decompressor.read(buf2, sizeof(buf2));
|
||||
// The conversion to std::size_t is safe because decompressor.read()
|
||||
// returns a non-negative value which, in this case, can't exceed
|
||||
// sizeof(buf2).
|
||||
rest += string(buf2, streamsizeToSize_t(decompressor.gcount()));
|
||||
} while (decompressor);
|
||||
|
||||
SG_CHECK_EQUAL(rest, " LMNOPQ");
|
||||
}
|
||||
|
||||
// If set, badbit would have caused an exception to be raised, anyway
|
||||
SG_VERIFY(decompressor.eof() && !decompressor.bad());
|
||||
}
|
||||
|
||||
// Utility function: parametrized round-trip test with a compressor +
|
||||
// decompressor pipeline.
|
||||
//
|
||||
// Note: this is nice conceptually, allows to keep memory use constant even in
|
||||
// case an arbitrary amount of data is passed through, and exercises the
|
||||
// stream buffer classes well, however this technique is more than twice
|
||||
// as slow on my computer than using an intermediate buffer like this:
|
||||
//
|
||||
// std::stringstream compressedData_ss;
|
||||
// compressor >> compressedData_ss.rdbuf();
|
||||
//
|
||||
// simgear::ZlibDecompressorIStream decompressor(compressedData_ss,
|
||||
// SGPath(), ...
|
||||
void roundTripWithIStreams(
|
||||
simgear::ZLibCompressionFormat compressionFormat,
|
||||
int compressionLevel,
|
||||
simgear::ZLibMemoryStrategy memStrategy,
|
||||
std::size_t compInBufSize,
|
||||
std::size_t compOutBufSize,
|
||||
std::size_t decompInBufSize,
|
||||
std::size_t decompOutBufSize,
|
||||
std::size_t compPutbackSize,
|
||||
std::size_t decompPutbackSize,
|
||||
bool useAutoFormatForDecompression = false)
|
||||
{
|
||||
const simgear::ZLibCompressionFormat decompFormat =
|
||||
(useAutoFormatForDecompression) ?
|
||||
simgear::ZLibCompressionFormat::AUTODETECT : compressionFormat;
|
||||
|
||||
std::istringstream lipsum_ss(lipsum);
|
||||
// This tests the optional dynamic buffer allocation in ZlibAbstractIStreambuf
|
||||
simgear::ZlibCompressorIStream compressor(
|
||||
lipsum_ss, SGPath(), compressionLevel, compressionFormat, memStrategy,
|
||||
nullptr, compInBufSize, nullptr, compOutBufSize, compPutbackSize);
|
||||
compressor.exceptions(std::ios_base::badbit); // throw if badbit is set
|
||||
|
||||
// Use the compressor as input to the decompressor (pipeline). The
|
||||
// decompressor uses read() with chunks that are as large as possible given
|
||||
// the available space in its input buffer. These read() calls are served by
|
||||
// ZlibCompressorIStreambuf::xsgetn(), which is efficient.
|
||||
simgear::ZlibDecompressorIStream decompressor(
|
||||
compressor, SGPath(), decompFormat,
|
||||
nullptr, decompInBufSize, nullptr, decompOutBufSize, decompPutbackSize);
|
||||
decompressor.exceptions(std::ios_base::badbit);
|
||||
std::ostringstream roundTripResult;
|
||||
// This, on the other hand, appears not to use xsgetn() (tested with the GNU
|
||||
// libstdc++ from g++ 6.3.0, 20170124). This causes useless copying of the
|
||||
// data to the decompressor output buffer, instead of writing it directly to
|
||||
// roundTripResult's internal buffer. This is simple and nice in appearance,
|
||||
// but if you are after performance, better use decompressor.read()
|
||||
// (typically in conjunction with gcount(), inside a loop).
|
||||
decompressor >> roundTripResult.rdbuf();
|
||||
SG_CHECK_EQUAL(roundTripResult.str(), lipsum);
|
||||
}
|
||||
|
||||
// Round-trip conversion with simgear::ZlibCompressorIStream and
|
||||
// simgear::ZlibDecompressorIStream, using various parameter combinations.
|
||||
void test_RoundTripMultiWithIStreams()
|
||||
{
|
||||
cerr <<
|
||||
"Compression-decompression cycles using the std::istream subclasses (many\n"
|
||||
"combinations of buffer sizes and compression parameters tested)\n";
|
||||
|
||||
{ // More variations on these later
|
||||
const std::size_t compPutbackSize = 1;
|
||||
const std::size_t decompPutbackSize = 1;
|
||||
|
||||
for (auto format: {simgear::ZLibCompressionFormat::ZLIB,
|
||||
simgear::ZLibCompressionFormat::GZIP}) {
|
||||
for (int compressionLevel: {1, 4, 7, 9}) {
|
||||
for (auto memStrategy: {
|
||||
simgear::ZLibMemoryStrategy::FAVOR_MEMORY_OVER_SPEED,
|
||||
simgear::ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY}) {
|
||||
for (std::size_t compInBufSize: {3, 4}) {
|
||||
for (std::size_t compOutBufSize: {3, 5}) {
|
||||
for (std::size_t decompInBufSize: {3, 4}) {
|
||||
for (std::size_t decompOutBufSize: {3, 4,}) {
|
||||
roundTripWithIStreams(
|
||||
format, compressionLevel, memStrategy, compInBufSize,
|
||||
compOutBufSize, decompInBufSize, decompOutBufSize,
|
||||
compPutbackSize, decompPutbackSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const auto format = simgear::ZLibCompressionFormat::ZLIB;
|
||||
const int compressionLevel = Z_DEFAULT_COMPRESSION;
|
||||
const auto memStrategy =
|
||||
simgear::ZLibMemoryStrategy::FAVOR_SPEED_OVER_MEMORY;
|
||||
|
||||
for (std::size_t compInBufSize: {3, 4, 31, 256, 19475}) {
|
||||
for (std::size_t compOutBufSize: {3, 5, 9, 74, 4568}) {
|
||||
for (std::size_t decompInBufSize: {3, 4, 256, 24568}) {
|
||||
for (std::size_t decompOutBufSize: {3, 5, 42, 4568}) {
|
||||
for (std::size_t compPutbackSize: {0, 1, 2}) {
|
||||
for (std::size_t decompPutbackSize: {0, 1, 2}) {
|
||||
roundTripWithIStreams(
|
||||
format, compressionLevel, memStrategy, compInBufSize,
|
||||
compOutBufSize, decompInBufSize, decompOutBufSize,
|
||||
compPutbackSize, decompPutbackSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const std::size_t compInBufSize = 5;
|
||||
const std::size_t compOutBufSize = 107;
|
||||
const std::size_t decompInBufSize = 65536;
|
||||
const std::size_t decompOutBufSize = 84;
|
||||
int i = 0;
|
||||
|
||||
for (std::size_t compPutbackSize: {25, 40, 105}) {
|
||||
for (std::size_t decompPutbackSize: {30, 60, 81}) {
|
||||
const simgear::ZLibCompressionFormat compFormat = (i++ % 2) ?
|
||||
simgear::ZLibCompressionFormat::ZLIB :
|
||||
simgear::ZLibCompressionFormat::GZIP;
|
||||
|
||||
roundTripWithIStreams(
|
||||
compFormat, Z_BEST_COMPRESSION,
|
||||
simgear::ZLibMemoryStrategy::FAVOR_MEMORY_OVER_SPEED,
|
||||
compInBufSize, compOutBufSize, decompInBufSize, decompOutBufSize,
|
||||
compPutbackSize, decompPutbackSize,
|
||||
/* automatic format detection for decompression */ true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Utility function showing how to return a (unique_ptr to a)
|
||||
// ZlibCompressorIStream instance, that keeps a reference to its data source
|
||||
// as long as the ZlibCompressorIStream instance is alive. Thus, calling code
|
||||
// doesn't have to worry about the lifetime of said data source (here, an
|
||||
// std::istringstream instance).
|
||||
std::unique_ptr<simgear::ZlibCompressorIStream>
|
||||
IStreamConstructorWithSinkSemantics_compressorFactory(const string& str)
|
||||
{
|
||||
std::unique_ptr<std::istringstream> iss(new std::istringstream(str));
|
||||
|
||||
// The returned compressor object retains a “reference” (of unique_ptr type)
|
||||
// to the std::istringstream object pointed to by 'iss' as long as it is
|
||||
// alive. When the returned compressor object (wrapped in a unique_ptr) is
|
||||
// destroyed, this std::istringstream object will be automatically
|
||||
// destroyed too.
|
||||
//
|
||||
// Note: it's an implementation detail, but this test also indirectly
|
||||
// exercises the ZlibCompressorIStreambuf constructor taking an
|
||||
// argument of type std::unique_ptr<std::istream>.
|
||||
return std::unique_ptr<simgear::ZlibCompressorIStream>(
|
||||
new simgear::ZlibCompressorIStream(std::move(iss)));
|
||||
}
|
||||
|
||||
void test_IStreamConstructorWithSinkSemantics()
|
||||
{
|
||||
cerr << "Testing the unique_ptr-based ZlibCompressorIStream constructor\n";
|
||||
|
||||
string someString = randomString(4096, 8192); // arbitrary values
|
||||
// This shows how to get a new compressor or decompressor object from a
|
||||
// factory function. Of course, we could create the object directly on the
|
||||
// stack without using a separate function!
|
||||
std::unique_ptr<simgear::ZlibCompressorIStream> compressor =
|
||||
IStreamConstructorWithSinkSemantics_compressorFactory(someString);
|
||||
compressor->exceptions(std::ios_base::badbit); // throw if badbit is set
|
||||
|
||||
// Use the compressor as input to the decompressor (pipeline). The
|
||||
// decompressor uses read() with chunks that are as large as possible given
|
||||
// the available space in its input buffer. These read() calls are served by
|
||||
// ZlibCompressorIStreambuf::xsgetn(), which is efficient. We won't need the
|
||||
// compressor afterwards, so let's just std::move() its unique_ptr.
|
||||
simgear::ZlibDecompressorIStream decompressor(std::move(compressor));
|
||||
decompressor.exceptions(std::ios_base::badbit);
|
||||
|
||||
std::ostringstream roundTripResult;
|
||||
// Of course, you may want to adjust bufSize depending on the application.
|
||||
static constexpr std::size_t bufSize = 1024;
|
||||
std::unique_ptr<char[]> buf(new char[bufSize]);
|
||||
|
||||
// Relatively efficient way of reading from the decompressor (modulo
|
||||
// possible adjustments to 'bufSize', of course). The decompressed data is
|
||||
// first written to 'buf', then copied to 'roundTripResult'. There is no
|
||||
// other useless copy via, for instance, an intermediate std::string object,
|
||||
// as would be the case if we used std::string(buf.get(), bufSize).
|
||||
//
|
||||
// Of course, ideally 'roundTripResult' would directly pull from
|
||||
// 'decompressor' without going through 'buf', but I don't think this is
|
||||
// possible with std::stringstream and friends. Such an optimized data flow
|
||||
// is however straightforward to implement if you replace 'roundTripResult'
|
||||
// with a custom data sink that calls decompressor.read().
|
||||
do {
|
||||
decompressor.read(buf.get(), bufSize);
|
||||
if (decompressor.gcount() > 0) { // at least one char could be read
|
||||
roundTripResult.write(buf.get(), decompressor.gcount());
|
||||
}
|
||||
} while (decompressor && roundTripResult);
|
||||
|
||||
// 1) If set, badbit would have caused an exception to be raised (see above).
|
||||
// 2) failbit doesn't necessarily indicate an error here: it is set as soon
|
||||
// as the read() call can't provide the requested number of characters.
|
||||
SG_VERIFY(decompressor.eof() && !decompressor.bad());
|
||||
// Because of std::ostringstream::write(), 'roundTripResult' might have its
|
||||
// failbit or badbit set, either of which would indicate a real problem.
|
||||
SG_VERIFY(roundTripResult);
|
||||
SG_CHECK_EQUAL(roundTripResult.str(), someString);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
test_pipeCompOrDecompIStreambufIntoOStream();
|
||||
test_StreambufBasicOperations();
|
||||
test_ZlibDecompressorIStreambuf_readLargestPossibleAmount();
|
||||
test_RoundTripMultiWithIStreams();
|
||||
test_formattedInputFromDecompressor();
|
||||
test_ZlibDecompressorIStream_readPutbackEtc();
|
||||
test_IStreamConstructorWithSinkSemantics();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
@@ -94,6 +94,17 @@ enum sgVertexAttributeTypes {
|
||||
SG_VA_FLOAT_3 = 0x00000800,
|
||||
};
|
||||
|
||||
static gzFile gzFileFromSGPath(const SGPath& path, const char* mode)
|
||||
{
|
||||
#if defined(SG_WINDOWS)
|
||||
std::wstring ws = path.wstr();
|
||||
return gzopen_w(ws.c_str(), mode);
|
||||
#else
|
||||
std::string ps = path.utf8Str();
|
||||
return gzopen(ps.c_str(), mode);
|
||||
#endif
|
||||
}
|
||||
|
||||
enum sgPropertyTypes {
|
||||
SG_MATERIAL = 0,
|
||||
SG_INDEX_TYPES = 1,
|
||||
@@ -534,15 +545,16 @@ bool SGBinObject::read_bin( const SGPath& file ) {
|
||||
fans_vas.clear();
|
||||
fan_materials.clear();
|
||||
|
||||
gzFile fp;
|
||||
string f = file.local8BitStr();
|
||||
if ( (fp = gzopen( f.c_str(), "rb" )) == NULL ) {
|
||||
string filegz = f + ".gz";
|
||||
if ( (fp = gzopen( filegz.c_str(), "rb" )) == NULL ) {
|
||||
gzFile fp = gzFileFromSGPath(file, "rb");
|
||||
if ( fp == NULL ) {
|
||||
SGPath withGZ = file;
|
||||
withGZ.concat(".gz");
|
||||
fp = gzFileFromSGPath(withGZ, "rb");
|
||||
if (fp == nullptr) {
|
||||
SG_LOG( SG_EVENT, SG_ALERT,
|
||||
"ERROR: opening " << file << " or " << filegz << " for reading!");
|
||||
"ERROR: opening " << file << " or " << withGZ << " for reading!");
|
||||
|
||||
throw sg_io_exception("Error opening for reading (and .gz)", sg_location(f));
|
||||
throw sg_io_exception("Error opening for reading (and .gz)", sg_location(file));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -957,9 +969,8 @@ bool SGBinObject::write_bin_file(const SGPath& file)
|
||||
SGPath file2(file);
|
||||
file2.create_dir( 0755 );
|
||||
|
||||
gzFile fp;
|
||||
std::string localPath = file.local8BitStr();
|
||||
if ( (fp = gzopen( localPath.c_str(), "wb9" )) == NULL ) {
|
||||
gzFile fp = gzFileFromSGPath(file, "wb9");
|
||||
if ( fp == nullptr ) {
|
||||
cout << "ERROR: opening " << file << " for writing!" << endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
//
|
||||
// $Id$
|
||||
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include <simgear/compiler.h>
|
||||
|
||||
#include <string>
|
||||
@@ -69,16 +69,27 @@ SGFile::~SGFile() {
|
||||
bool SGFile::open( const SGProtocolDir d ) {
|
||||
set_dir( d );
|
||||
|
||||
std::string n = file_name.local8BitStr();
|
||||
#if defined(SG_WINDOWS)
|
||||
std::wstring n = file_name.wstr();
|
||||
#else
|
||||
std::string n = file_name.utf8Str();
|
||||
#endif
|
||||
|
||||
|
||||
if ( get_dir() == SG_IO_OUT ) {
|
||||
#ifdef _WIN32
|
||||
#if defined(SG_WINDOWS)
|
||||
int mode = _S_IREAD | _S_IWRITE;
|
||||
fp = ::_wopen(n.c_str(), O_WRONLY | O_CREAT | O_TRUNC | extraoflags, mode);
|
||||
#else
|
||||
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
|
||||
#endif
|
||||
fp = ::open( n.c_str(), O_WRONLY | O_CREAT | O_TRUNC | extraoflags, mode );
|
||||
#endif
|
||||
} else if ( get_dir() == SG_IO_IN ) {
|
||||
#if defined(SG_WINDOWS)
|
||||
fp = ::_wopen( n.c_str(), O_RDONLY | extraoflags );
|
||||
#else
|
||||
fp = ::open( n.c_str(), O_RDONLY | extraoflags );
|
||||
#endif
|
||||
} else {
|
||||
SG_LOG( SG_IO, SG_ALERT,
|
||||
"Error: bidirection mode not available for files." );
|
||||
@@ -145,7 +156,7 @@ int SGFile::readline( char *buf, int length ) {
|
||||
result = i;
|
||||
}
|
||||
lseek( fp, pos + result, SEEK_SET );
|
||||
|
||||
|
||||
// just in case ...
|
||||
buf[ result ] = '\0';
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
$Id: netBuffer.cxx 1568 2002-09-02 06:05:49Z sjbaker $
|
||||
*/
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "sg_netBuffer.hxx"
|
||||
|
||||
#include <cassert>
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
// to write or something...]
|
||||
// Maybe assert valid handle, too?
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include "sg_netChannel.hxx"
|
||||
|
||||
#include <memory>
|
||||
|
||||
@@ -20,6 +20,7 @@
|
||||
//
|
||||
// $Id$
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <errno.h>
|
||||
#include <thread>
|
||||
#include <atomic>
|
||||
|
||||
#include <boost/algorithm/string/case_conv.hpp>
|
||||
|
||||
@@ -16,6 +18,7 @@
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
#include <simgear/misc/strutils.hxx>
|
||||
#include <simgear/timing/timestamp.hxx>
|
||||
#include <simgear/misc/test_macros.hxx>
|
||||
|
||||
using std::cout;
|
||||
using std::cerr;
|
||||
@@ -23,23 +26,82 @@ using std::endl;
|
||||
|
||||
using namespace simgear;
|
||||
|
||||
#define COMPARE(a, b) \
|
||||
if ((a) != (b)) { \
|
||||
cerr << "failed:" << #a << " != " << #b << endl; \
|
||||
cerr << "\tgot:'" << a << "'" << endl; \
|
||||
exit(1); \
|
||||
|
||||
class Watchdog
|
||||
{
|
||||
public:
|
||||
Watchdog() : _interval(0), _timer(0), _running(false) {}
|
||||
~Watchdog() {
|
||||
stop();
|
||||
}
|
||||
|
||||
void start(unsigned int milliseconds)
|
||||
{
|
||||
_interval = milliseconds;
|
||||
_timer = 0;
|
||||
_running = true;
|
||||
_thread = std::thread(&Watchdog::loop, this);
|
||||
}
|
||||
|
||||
void stop()
|
||||
{
|
||||
_running = false;
|
||||
_thread.join();
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned int _interval;
|
||||
std::atomic<unsigned int> _timer;
|
||||
std::atomic<bool> _running;
|
||||
std::thread _thread;
|
||||
|
||||
void loop()
|
||||
{
|
||||
while (_running)
|
||||
{
|
||||
_timer++;
|
||||
|
||||
if (_timer >= _interval)
|
||||
{
|
||||
_running = false;
|
||||
std::cerr << "Failure: timeout." << endl;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
sglog().setLogLevels( SG_ALL, SG_DEBUG );
|
||||
|
||||
DNS::Client cl;
|
||||
#define EXISTING_RECORD "terrasync.flightgear.org"
|
||||
const char * EXISTING_RECORD = argc > 1 ? argv[1] : "terrasync.flightgear.org";
|
||||
const char * QSERVICE = argc > 2 ? argv[2] : "https+ws20";
|
||||
|
||||
// test existing NAPTR
|
||||
// fgtest.t3r.de. 600 IN NAPTR 999 99 "U" "test" "!^.*$!http://dnstest.flightgear.org/!" .
|
||||
Watchdog watchdog;
|
||||
watchdog.start(100);
|
||||
|
||||
simgear::Socket::initSockets();
|
||||
|
||||
|
||||
DNS::Client cl;
|
||||
|
||||
cout << "test update without prior pending request" << endl;
|
||||
{
|
||||
cout << "polling.";
|
||||
for( int i = 0; i < 20; i++ ) {
|
||||
SGTimeStamp::sleepForMSec(200);
|
||||
cl.update(0);
|
||||
cout << ".";
|
||||
cout.flush();
|
||||
}
|
||||
cout << "done" << endl;
|
||||
}
|
||||
|
||||
cout << "test existing NAPTR: " << EXISTING_RECORD << endl;
|
||||
{
|
||||
DNS::NAPTRRequest * naptrRequest = new DNS::NAPTRRequest(EXISTING_RECORD);
|
||||
DNS::Request_ptr r(naptrRequest);
|
||||
@@ -50,23 +112,24 @@ int main(int argc, char* argv[])
|
||||
}
|
||||
|
||||
if( r->isTimeout() ) {
|
||||
cerr << "timeout testing existing record " EXISTING_RECORD << endl;
|
||||
cerr << "timeout testing existing record " << EXISTING_RECORD << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if(naptrRequest->entries.empty()) {
|
||||
cerr << "no results for " EXISTING_RECORD << endl;
|
||||
cerr << "no results for " << EXISTING_RECORD << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
// test for ascending preference/order
|
||||
cout << "test for ascending preference/order" << endl;
|
||||
int order = -1, preference = -1;
|
||||
for( DNS::NAPTRRequest::NAPTR_list::const_iterator it = naptrRequest->entries.begin(); it != naptrRequest->entries.end(); ++it ) {
|
||||
cout << "NAPTR " << (*it)->order << " " << (*it)->preference << " '" << (*it)->service << "' '" << (*it)->regexp << "' '" << (*it)->replacement << "'" << endl;
|
||||
// currently only support "U" which implies empty replacement
|
||||
COMPARE((*it)->flags, "U" );
|
||||
COMPARE(naptrRequest->entries[0]->replacement, "" );
|
||||
SG_CHECK_EQUAL((*it)->flags, "U" );
|
||||
SG_CHECK_EQUAL(naptrRequest->entries[0]->replacement, "" );
|
||||
|
||||
// currently only support ws20
|
||||
COMPARE((*it)->service, "ws20" );
|
||||
// currently only support ws20, disable temporarily
|
||||
//SG_CHECK_EQUAL((*it)->service, "ws20" );
|
||||
|
||||
if( (*it)->order < order ) {
|
||||
cerr << "NAPTR entries not ascending for field 'order'" << endl;
|
||||
@@ -94,8 +157,31 @@ int main(int argc, char* argv[])
|
||||
|
||||
}
|
||||
}
|
||||
cout << "test existing NAPTR with explicit qservice: " << QSERVICE << endl;
|
||||
{
|
||||
DNS::NAPTRRequest * naptrRequest = new DNS::NAPTRRequest(EXISTING_RECORD);
|
||||
naptrRequest->qservice = QSERVICE;
|
||||
DNS::Request_ptr r(naptrRequest);
|
||||
cl.makeRequest(r);
|
||||
while( !r->isComplete() && !r->isTimeout()) {
|
||||
SGTimeStamp::sleepForMSec(200);
|
||||
cl.update(0);
|
||||
}
|
||||
|
||||
// test non-existing NAPTR
|
||||
if( r->isTimeout() ) {
|
||||
cerr << "timeout testing existing record " << EXISTING_RECORD << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
if(naptrRequest->entries.empty()) {
|
||||
cerr << "no results for " << EXISTING_RECORD << endl;
|
||||
//return EXIT_FAILURE; // not yet a failure - probably add this for 2017.4 and create DNS entries
|
||||
}
|
||||
for( DNS::NAPTRRequest::NAPTR_list::const_iterator it = naptrRequest->entries.begin(); it != naptrRequest->entries.end(); ++it ) {
|
||||
cout << "NAPTR " << (*it)->order << " " << (*it)->preference << " '" << (*it)->service << "' '" << (*it)->regexp << "' '" << (*it)->replacement << "'" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "test non-existing NAPTR" << endl;
|
||||
{
|
||||
DNS::NAPTRRequest * naptrRequest = new DNS::NAPTRRequest("jurkxkqdiufqzpfvzqok.prozhqrlcaavbxifkkhf");
|
||||
DNS::Request_ptr r(naptrRequest);
|
||||
@@ -109,7 +195,7 @@ int main(int argc, char* argv[])
|
||||
cerr << "timeout testing non-existing record." << endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
COMPARE(naptrRequest->entries.size(), 0 );
|
||||
SG_CHECK_EQUAL(naptrRequest->entries.size(), 0 );
|
||||
}
|
||||
|
||||
cout << "all tests passed ok" << endl;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
#include <iostream>
|
||||
#include <map>
|
||||
#include <sstream>
|
||||
#include <errno.h>
|
||||
#include <cerrno>
|
||||
|
||||
#include <boost/algorithm/string/case_conv.hpp>
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <simgear/misc/strutils.hxx>
|
||||
#include <simgear/timing/timestamp.hxx>
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
#include <simgear/misc/test_macros.hxx>
|
||||
|
||||
#include <curl/multi.h>
|
||||
|
||||
@@ -41,18 +42,6 @@ const char* BODY3 = "Cras ut neque nulla. Duis ut velit neque, sit amet "
|
||||
const unsigned int body2Size = 8 * 1024;
|
||||
char body2[body2Size];
|
||||
|
||||
#define COMPARE(a, b) \
|
||||
if ((a) != (b)) { \
|
||||
cerr << "failed:" << #a << " != " << #b << endl; \
|
||||
cerr << "\tgot:'" << a << "'" << endl; \
|
||||
exit(1); \
|
||||
}
|
||||
|
||||
#define VERIFY(a) \
|
||||
if (!(a)) { \
|
||||
cerr << "failed:" << #a << endl; \
|
||||
exit(1); \
|
||||
}
|
||||
|
||||
class TestRequest : public HTTP::Request
|
||||
{
|
||||
@@ -126,8 +115,9 @@ public:
|
||||
d << contentStr;
|
||||
push(d.str().c_str());
|
||||
} else if (path == "/test_headers") {
|
||||
COMPARE(requestHeaders["X-Foo"], string("Bar"));
|
||||
COMPARE(requestHeaders["X-AnotherHeader"], string("A longer value"));
|
||||
SG_CHECK_EQUAL(requestHeaders["X-Foo"], string("Bar"));
|
||||
SG_CHECK_EQUAL(requestHeaders["X-AnotherHeader"],
|
||||
string("A longer value"));
|
||||
|
||||
string contentStr(BODY1);
|
||||
stringstream d;
|
||||
@@ -304,7 +294,7 @@ public:
|
||||
} else if (path == "/test_put") {
|
||||
std::cerr << "sending PUT response" << std::endl;
|
||||
|
||||
COMPARE(buffer, BODY3);
|
||||
SG_CHECK_EQUAL(buffer, BODY3);
|
||||
stringstream d;
|
||||
d << "HTTP/1.1 " << 204 << " " << reasonForCode(204) << "\r\n";
|
||||
d << "\r\n"; // final CRLF to terminate the headers
|
||||
@@ -314,7 +304,7 @@ public:
|
||||
|
||||
std::string entityStr = "http://localhost:2000/something.txt";
|
||||
|
||||
COMPARE(buffer, BODY3);
|
||||
SG_CHECK_EQUAL(buffer, BODY3);
|
||||
stringstream d;
|
||||
d << "HTTP/1.1 " << 201 << " " << reasonForCode(201) << "\r\n";
|
||||
d << "Location:" << entityStr << "\r\n";
|
||||
@@ -385,18 +375,18 @@ int main(int argc, char* argv[])
|
||||
|
||||
// test URL parsing
|
||||
TestRequest* tr1 = new TestRequest("http://localhost.woo.zar:2000/test1?foo=bar");
|
||||
COMPARE(tr1->scheme(), "http");
|
||||
COMPARE(tr1->hostAndPort(), "localhost.woo.zar:2000");
|
||||
COMPARE(tr1->host(), "localhost.woo.zar");
|
||||
COMPARE(tr1->port(), 2000);
|
||||
COMPARE(tr1->path(), "/test1");
|
||||
SG_CHECK_EQUAL(tr1->scheme(), "http");
|
||||
SG_CHECK_EQUAL(tr1->hostAndPort(), "localhost.woo.zar:2000");
|
||||
SG_CHECK_EQUAL(tr1->host(), "localhost.woo.zar");
|
||||
SG_CHECK_EQUAL(tr1->port(), 2000);
|
||||
SG_CHECK_EQUAL(tr1->path(), "/test1");
|
||||
|
||||
TestRequest* tr2 = new TestRequest("http://192.168.1.1/test1/dir/thing/file.png");
|
||||
COMPARE(tr2->scheme(), "http");
|
||||
COMPARE(tr2->hostAndPort(), "192.168.1.1");
|
||||
COMPARE(tr2->host(), "192.168.1.1");
|
||||
COMPARE(tr2->port(), 80);
|
||||
COMPARE(tr2->path(), "/test1/dir/thing/file.png");
|
||||
SG_CHECK_EQUAL(tr2->scheme(), "http");
|
||||
SG_CHECK_EQUAL(tr2->hostAndPort(), "192.168.1.1");
|
||||
SG_CHECK_EQUAL(tr2->host(), "192.168.1.1");
|
||||
SG_CHECK_EQUAL(tr2->port(), 80);
|
||||
SG_CHECK_EQUAL(tr2->path(), "/test1/dir/thing/file.png");
|
||||
|
||||
// basic get request
|
||||
{
|
||||
@@ -405,11 +395,11 @@ int main(int argc, char* argv[])
|
||||
cl.makeRequest(tr);
|
||||
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseReason(), string("OK"));
|
||||
COMPARE(tr->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr->responseBytesReceived(), strlen(BODY1));
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseReason(), string("OK"));
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
}
|
||||
|
||||
{
|
||||
@@ -418,11 +408,11 @@ int main(int argc, char* argv[])
|
||||
cl.makeRequest(tr);
|
||||
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseReason(), string("OK"));
|
||||
COMPARE(tr->responseLength(), strlen(BODY3));
|
||||
COMPARE(tr->responseBytesReceived(), strlen(BODY3));
|
||||
COMPARE(tr->bodyData, string(BODY3));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseReason(), string("OK"));
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY3));
|
||||
}
|
||||
|
||||
{
|
||||
@@ -430,7 +420,7 @@ int main(int argc, char* argv[])
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -441,11 +431,11 @@ int main(int argc, char* argv[])
|
||||
cl.makeRequest(tr);
|
||||
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseReason(), string("OK"));
|
||||
COMPARE(tr->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr->responseBytesReceived(), strlen(BODY1));
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseReason(), string("OK"));
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
}
|
||||
|
||||
// larger get request
|
||||
@@ -458,9 +448,9 @@ int main(int argc, char* argv[])
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseBytesReceived(), body2Size);
|
||||
COMPARE(tr->bodyData, string(body2, body2Size));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), body2Size);
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(body2, body2Size));
|
||||
}
|
||||
|
||||
cerr << "testing chunked transfer encoding" << endl;
|
||||
@@ -470,12 +460,12 @@ int main(int argc, char* argv[])
|
||||
cl.makeRequest(tr);
|
||||
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseReason(), string("OK"));
|
||||
COMPARE(tr->responseBytesReceived(), 30);
|
||||
COMPARE(tr->bodyData, "ABCDEFGHABCDEFABCDSTUVABCDSTUV");
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseReason(), string("OK"));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), 30);
|
||||
SG_CHECK_EQUAL(tr->bodyData, "ABCDEFGHABCDEFABCDSTUVABCDSTUV");
|
||||
// check trailers made it too
|
||||
COMPARE(tr->headers["x-foobar"], string("wibble"));
|
||||
SG_CHECK_EQUAL(tr->headers["x-foobar"], string("wibble"));
|
||||
}
|
||||
|
||||
// test 404
|
||||
@@ -484,9 +474,9 @@ int main(int argc, char* argv[])
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 404);
|
||||
COMPARE(tr->responseReason(), string("not found"));
|
||||
COMPARE(tr->responseLength(), 0);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 404);
|
||||
SG_CHECK_EQUAL(tr->responseReason(), string("not found"));
|
||||
SG_CHECK_EQUAL(tr->responseLength(), 0);
|
||||
}
|
||||
|
||||
cout << "done 404 test" << endl;
|
||||
@@ -496,7 +486,7 @@ int main(int argc, char* argv[])
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
}
|
||||
|
||||
cout << "done1" << endl;
|
||||
@@ -506,9 +496,9 @@ int main(int argc, char* argv[])
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
}
|
||||
|
||||
cout << "done2" << endl;
|
||||
@@ -518,9 +508,9 @@ int main(int argc, char* argv[])
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
}
|
||||
cout << "done3" << endl;
|
||||
// test connectToHost failure
|
||||
@@ -534,7 +524,7 @@ int main(int argc, char* argv[])
|
||||
|
||||
|
||||
const int HOST_NOT_FOUND_CODE = CURLE_COULDNT_RESOLVE_HOST;
|
||||
COMPARE(tr->responseCode(), HOST_NOT_FOUND_CODE);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), HOST_NOT_FOUND_CODE);
|
||||
}
|
||||
|
||||
cout << "testing abrupt close" << endl;
|
||||
@@ -546,7 +536,7 @@ int main(int argc, char* argv[])
|
||||
waitForFailed(&cl, tr);
|
||||
|
||||
const int SERVER_NO_DATA_CODE = CURLE_GOT_NOTHING;
|
||||
COMPARE(tr->responseCode(), SERVER_NO_DATA_CODE);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), SERVER_NO_DATA_CODE);
|
||||
}
|
||||
|
||||
cout << "testing proxy close" << endl;
|
||||
@@ -557,9 +547,9 @@ cout << "testing proxy close" << endl;
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseLength(), body2Size);
|
||||
COMPARE(tr->bodyData, string(body2, body2Size));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseLength(), body2Size);
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(body2, body2Size));
|
||||
}
|
||||
|
||||
{
|
||||
@@ -568,9 +558,9 @@ cout << "testing proxy close" << endl;
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseBytesReceived(), body2Size);
|
||||
COMPARE(tr->bodyData, string(body2, body2Size));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), body2Size);
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(body2, body2Size));
|
||||
}
|
||||
|
||||
// pipelining
|
||||
@@ -595,17 +585,17 @@ cout << "testing proxy close" << endl;
|
||||
cl.makeRequest(tr3);
|
||||
|
||||
waitForComplete(&cl, tr3);
|
||||
VERIFY(tr->complete);
|
||||
VERIFY(tr2->complete);
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_VERIFY(tr->complete);
|
||||
SG_VERIFY(tr2->complete);
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
|
||||
COMPARE(tr2->responseLength(), strlen(BODY3));
|
||||
COMPARE(tr2->responseBytesReceived(), strlen(BODY3));
|
||||
COMPARE(tr2->bodyData, string(BODY3));
|
||||
SG_CHECK_EQUAL(tr2->responseLength(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr2->responseBytesReceived(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr2->bodyData, string(BODY3));
|
||||
|
||||
COMPARE(tr3->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->bodyData, string(BODY1));
|
||||
|
||||
COMPARE(testServer.connectCount(), 1);
|
||||
SG_CHECK_EQUAL(testServer.connectCount(), 1);
|
||||
}
|
||||
|
||||
// multiple requests with an HTTP 1.0 server
|
||||
@@ -626,17 +616,17 @@ cout << "testing proxy close" << endl;
|
||||
cl.makeRequest(tr3);
|
||||
|
||||
waitForComplete(&cl, tr3);
|
||||
VERIFY(tr->complete);
|
||||
VERIFY(tr2->complete);
|
||||
SG_VERIFY(tr->complete);
|
||||
SG_VERIFY(tr2->complete);
|
||||
|
||||
COMPARE(tr->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr->responseBytesReceived(), strlen(BODY1));
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
|
||||
COMPARE(tr2->responseLength(), strlen(BODY3));
|
||||
COMPARE(tr2->responseBytesReceived(), strlen(BODY3));
|
||||
COMPARE(tr2->bodyData, string(BODY3));
|
||||
COMPARE(tr3->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr2->responseLength(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr2->responseBytesReceived(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr2->bodyData, string(BODY3));
|
||||
SG_CHECK_EQUAL(tr3->bodyData, string(BODY1));
|
||||
}
|
||||
|
||||
// POST
|
||||
@@ -648,7 +638,7 @@ cout << "testing proxy close" << endl;
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 204);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 204);
|
||||
}
|
||||
|
||||
// PUT
|
||||
@@ -660,7 +650,7 @@ cout << "testing proxy close" << endl;
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 204);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 204);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -671,7 +661,7 @@ cout << "testing proxy close" << endl;
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 201);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 201);
|
||||
}
|
||||
|
||||
// test_zero_length_content
|
||||
@@ -681,9 +671,9 @@ cout << "testing proxy close" << endl;
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->bodyData, string());
|
||||
COMPARE(tr->responseBytesReceived(), 0);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->bodyData, string());
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), 0);
|
||||
}
|
||||
|
||||
// test cancel
|
||||
@@ -711,12 +701,12 @@ cout << "testing proxy close" << endl;
|
||||
|
||||
waitForComplete(&cl, tr3);
|
||||
|
||||
COMPARE(tr->responseCode(), -1);
|
||||
COMPARE(tr2->responseReason(), "my reason 2");
|
||||
SG_CHECK_EQUAL(tr->responseCode(), -1);
|
||||
SG_CHECK_EQUAL(tr2->responseReason(), "my reason 2");
|
||||
|
||||
COMPARE(tr3->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr3->responseBytesReceived(), strlen(BODY1));
|
||||
COMPARE(tr3->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->bodyData, string(BODY1));
|
||||
}
|
||||
|
||||
// test cancel
|
||||
@@ -742,16 +732,16 @@ cout << "testing proxy close" << endl;
|
||||
|
||||
waitForComplete(&cl, tr3);
|
||||
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr->responseBytesReceived(), strlen(BODY1));
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
|
||||
COMPARE(tr2->responseCode(), -1);
|
||||
SG_CHECK_EQUAL(tr2->responseCode(), -1);
|
||||
|
||||
COMPARE(tr3->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr3->responseBytesReceived(), strlen(BODY1));
|
||||
COMPARE(tr3->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->bodyData, string(BODY1));
|
||||
}
|
||||
|
||||
{
|
||||
@@ -776,12 +766,12 @@ cout << "testing proxy close" << endl;
|
||||
cl.makeRequest(tr2);
|
||||
|
||||
waitForComplete(&cl, tr2);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->bodyData, string(BODY3));
|
||||
COMPARE(tr->responseBytesReceived(), strlen(BODY3));
|
||||
COMPARE(tr2->responseCode(), 200);
|
||||
COMPARE(tr2->bodyData, string(BODY1));
|
||||
COMPARE(tr2->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY3));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr2->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr2->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr2->responseBytesReceived(), strlen(BODY1));
|
||||
}
|
||||
|
||||
cout << "all tests passed ok" << endl;
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#endif
|
||||
|
||||
#include <simgear/misc/sg_dir.hxx>
|
||||
#include <simgear/misc/test_macros.hxx>
|
||||
|
||||
#include "sg_binobj.hxx"
|
||||
|
||||
@@ -22,19 +23,7 @@ using std::cerr;
|
||||
using std::endl;
|
||||
using std::string;
|
||||
|
||||
#define COMPARE(a, b) \
|
||||
if ((a) != (b)) { \
|
||||
cerr << "failed:" << #a << " != " << #b << endl; \
|
||||
cerr << "\tgot:" << a << endl; \
|
||||
exit(1); \
|
||||
}
|
||||
|
||||
#define VERIFY(a) \
|
||||
if (!(a)) { \
|
||||
cerr << "failed:" << #a << endl; \
|
||||
exit(1); \
|
||||
}
|
||||
|
||||
void generate_points(int count, std::vector<SGVec3d>& vec)
|
||||
{
|
||||
for (int i=0; i<count; ++i) {
|
||||
@@ -61,15 +50,15 @@ void test_empty()
|
||||
SGBinObject empty;
|
||||
SGPath path(simgear::Dir::current().file("empty.btg.gz"));
|
||||
bool ok = empty.write_bin_file(path);
|
||||
VERIFY( ok );
|
||||
SG_VERIFY(ok);
|
||||
SGBinObject rd;
|
||||
ok = rd.read_bin(path) ;
|
||||
VERIFY( ok);
|
||||
ok = rd.read_bin(path) ;
|
||||
SG_VERIFY(ok);
|
||||
|
||||
SG_CHECK_EQUAL(rd.get_wgs84_nodes().size(), 0);
|
||||
SG_VERIFY(rd.get_pt_materials().empty());
|
||||
}
|
||||
|
||||
COMPARE(rd.get_wgs84_nodes().size(), 0);
|
||||
VERIFY(rd.get_pt_materials().empty());
|
||||
}
|
||||
|
||||
void comparePoints(const SGBinObject& rd, const std::vector<SGVec3d>& b)
|
||||
{
|
||||
for (unsigned int i=1; i<b.size(); i += 10) {
|
||||
@@ -82,7 +71,7 @@ void comparePoints(const SGBinObject& rd, const std::vector<SGVec3d>& b)
|
||||
cout << pos << endl;
|
||||
}
|
||||
|
||||
VERIFY(equivalent(pos, b[i], 0.1));
|
||||
SG_VERIFY(equivalent(pos, b[i], 0.1));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,7 +79,7 @@ void compareTexCoords(const SGBinObject& rd, const std::vector<SGVec2f>& b)
|
||||
{
|
||||
for (unsigned int i=1; i<b.size(); i += 10) {
|
||||
SGVec2f pos = rd.get_texcoords()[i];
|
||||
VERIFY(equivalent(pos, b[i], 0.001f));
|
||||
SG_VERIFY(equivalent(pos, b[i], 0.001f));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,13 +126,13 @@ void compareTris(const SGBinObject& a, const SGBinObject& b)
|
||||
for (unsigned int i=0; i<count; i += 39) {
|
||||
const int_list& vA(a.get_tris_v()[i]);
|
||||
const int_list& vB(b.get_tris_v()[i]);
|
||||
VERIFY(vA == vB);
|
||||
SG_VERIFY(vA == vB);
|
||||
|
||||
SG_CHECK_EQUAL(a.get_tri_materials()[i], b.get_tri_materials()[i]);
|
||||
|
||||
COMPARE(a.get_tri_materials()[i], b.get_tri_materials()[i]);
|
||||
|
||||
const int_list& tA(a.get_tris_tcs()[i][0]);
|
||||
const int_list& tB(b.get_tris_tcs()[i][0]);
|
||||
VERIFY(tA == tB);
|
||||
SG_VERIFY(tA == tB);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,16 +178,17 @@ void test_basic()
|
||||
basic.set_texcoords(texCoords);
|
||||
|
||||
bool ok = basic.write_bin_file(path);
|
||||
VERIFY( ok );
|
||||
SG_VERIFY( ok );
|
||||
|
||||
SGBinObject rd;
|
||||
ok = rd.read_bin(path) ;
|
||||
VERIFY( ok);
|
||||
COMPARE(rd.get_version(), 7); // should be version 7 since indices are < 2^16
|
||||
COMPARE(rd.get_gbs_center(), center);
|
||||
COMPARE(rd.get_gbs_radius(), 12345);
|
||||
COMPARE(rd.get_wgs84_nodes().size(), points.size());
|
||||
|
||||
SG_VERIFY( ok);
|
||||
// Should be version 7 since indices are < 2^16
|
||||
SG_CHECK_EQUAL(rd.get_version(), 7);
|
||||
SG_CHECK_EQUAL(rd.get_gbs_center(), center);
|
||||
SG_CHECK_EQUAL(rd.get_gbs_radius(), 12345);
|
||||
SG_CHECK_EQUAL(rd.get_wgs84_nodes().size(), points.size());
|
||||
|
||||
comparePoints(rd, points);
|
||||
compareTexCoords(rd, texCoords);
|
||||
}
|
||||
@@ -226,15 +216,16 @@ void test_many_tcs()
|
||||
generate_tris(basic, 20000);
|
||||
|
||||
bool ok = basic.write_bin_file(path);
|
||||
VERIFY( ok );
|
||||
SG_VERIFY( ok );
|
||||
|
||||
SGBinObject rd;
|
||||
ok = rd.read_bin(path) ;
|
||||
VERIFY( ok);
|
||||
COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
|
||||
COMPARE(rd.get_wgs84_nodes().size(), points.size());
|
||||
COMPARE(rd.get_texcoords().size(), texCoords.size());
|
||||
|
||||
SG_VERIFY( ok);
|
||||
// Should be version 10 since indices are > 2^16
|
||||
SG_CHECK_EQUAL(rd.get_version(), 10);
|
||||
SG_CHECK_EQUAL(rd.get_wgs84_nodes().size(), points.size());
|
||||
SG_CHECK_EQUAL(rd.get_texcoords().size(), texCoords.size());
|
||||
|
||||
comparePoints(rd, points);
|
||||
compareTexCoords(rd, texCoords);
|
||||
compareTris(basic, rd);
|
||||
@@ -263,15 +254,16 @@ void test_big()
|
||||
generate_tris(basic, 200000);
|
||||
|
||||
bool ok = basic.write_bin_file(path);
|
||||
VERIFY( ok );
|
||||
SG_VERIFY( ok );
|
||||
|
||||
SGBinObject rd;
|
||||
ok = rd.read_bin(path) ;
|
||||
VERIFY( ok);
|
||||
COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
|
||||
COMPARE(rd.get_wgs84_nodes().size(), points.size());
|
||||
COMPARE(rd.get_texcoords().size(), texCoords.size());
|
||||
|
||||
SG_VERIFY( ok);
|
||||
// Should be version 10 since indices are > 2^16
|
||||
SG_CHECK_EQUAL(rd.get_version(), 10);
|
||||
SG_CHECK_EQUAL(rd.get_wgs84_nodes().size(), points.size());
|
||||
SG_CHECK_EQUAL(rd.get_texcoords().size(), texCoords.size());
|
||||
|
||||
comparePoints(rd, points);
|
||||
compareTexCoords(rd, texCoords);
|
||||
compareTris(basic, rd);
|
||||
@@ -300,15 +292,15 @@ void test_some_objects()
|
||||
generate_tris(basic, 30000); // a number smaller than 2^15!
|
||||
|
||||
bool ok = basic.write_bin_file(path);
|
||||
VERIFY( ok );
|
||||
SG_VERIFY( ok );
|
||||
|
||||
SGBinObject rd;
|
||||
ok = rd.read_bin(path) ;
|
||||
VERIFY( ok);
|
||||
COMPARE(rd.get_version(), 7); // since we have less than 2^15 tris
|
||||
COMPARE(rd.get_wgs84_nodes().size(), points.size());
|
||||
COMPARE(rd.get_texcoords().size(), texCoords.size());
|
||||
|
||||
SG_VERIFY( ok);
|
||||
SG_CHECK_EQUAL(rd.get_version(), 7); // since we have less than 2^15 tris
|
||||
SG_CHECK_EQUAL(rd.get_wgs84_nodes().size(), points.size());
|
||||
SG_CHECK_EQUAL(rd.get_texcoords().size(), texCoords.size());
|
||||
|
||||
comparePoints(rd, points);
|
||||
compareTexCoords(rd, texCoords);
|
||||
compareTris(basic, rd);
|
||||
@@ -337,15 +329,16 @@ void test_many_objects()
|
||||
generate_tris(basic, 200000);
|
||||
|
||||
bool ok = basic.write_bin_file(path);
|
||||
VERIFY( ok );
|
||||
SG_VERIFY( ok );
|
||||
|
||||
SGBinObject rd;
|
||||
ok = rd.read_bin(path) ;
|
||||
VERIFY( ok);
|
||||
COMPARE(rd.get_version(), 10); // should be version 10 since indices are > 2^16
|
||||
COMPARE(rd.get_wgs84_nodes().size(), points.size());
|
||||
COMPARE(rd.get_texcoords().size(), texCoords.size());
|
||||
|
||||
SG_VERIFY( ok);
|
||||
// Should be version 10 since indices are > 2^16
|
||||
SG_CHECK_EQUAL(rd.get_version(), 10);
|
||||
SG_CHECK_EQUAL(rd.get_wgs84_nodes().size(), points.size());
|
||||
SG_CHECK_EQUAL(rd.get_texcoords().size(), texCoords.size());
|
||||
|
||||
comparePoints(rd, points);
|
||||
compareTexCoords(rd, texCoords);
|
||||
compareTris(basic, rd);
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
#include <simgear/timing/timestamp.hxx>
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
#include <simgear/misc/sg_dir.hxx>
|
||||
#include <simgear/misc/sgstream.hxx>
|
||||
#include <simgear/io/iostreams/sgstream.hxx>
|
||||
#include <simgear/structure/exception.hxx>
|
||||
#include <simgear/structure/callback.hxx>
|
||||
|
||||
@@ -71,7 +71,7 @@ public:
|
||||
int requestCount;
|
||||
bool getWillFail;
|
||||
bool returnCorruptData;
|
||||
std::auto_ptr<SGCallback> accessCallback;
|
||||
std::unique_ptr<SGCallback> accessCallback;
|
||||
|
||||
void clearRequestCounts();
|
||||
|
||||
@@ -400,7 +400,7 @@ void waitForUpdateComplete(HTTP::Client* cl, HTTPRepository* repo)
|
||||
|
||||
void testBasicClone(HTTP::Client* cl)
|
||||
{
|
||||
std::auto_ptr<HTTPRepository> repo;
|
||||
std::unique_ptr<HTTPRepository> repo;
|
||||
SGPath p(simgear::Dir::current().path());
|
||||
p.append("http_repo_basic");
|
||||
simgear::Dir pd(p);
|
||||
@@ -408,7 +408,6 @@ void testBasicClone(HTTP::Client* cl)
|
||||
|
||||
repo.reset(new HTTPRepository(p, cl));
|
||||
repo->setBaseUrl("http://localhost:2000/repo");
|
||||
repo->setEntireRepositoryMode();
|
||||
repo->update();
|
||||
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
@@ -436,7 +435,7 @@ void testBasicClone(HTTP::Client* cl)
|
||||
|
||||
void testModifyLocalFiles(HTTP::Client* cl)
|
||||
{
|
||||
std::auto_ptr<HTTPRepository> repo;
|
||||
std::unique_ptr<HTTPRepository> repo;
|
||||
SGPath p(simgear::Dir::current().path());
|
||||
p.append("http_repo_modify_local_2");
|
||||
simgear::Dir pd(p);
|
||||
@@ -446,7 +445,6 @@ void testModifyLocalFiles(HTTP::Client* cl)
|
||||
|
||||
repo.reset(new HTTPRepository(p, cl));
|
||||
repo->setBaseUrl("http://localhost:2000/repo");
|
||||
repo->setEntireRepositoryMode();
|
||||
repo->update();
|
||||
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
@@ -478,7 +476,7 @@ void testNoChangesUpdate()
|
||||
|
||||
void testMergeExistingFileWithoutDownload(HTTP::Client* cl)
|
||||
{
|
||||
std::auto_ptr<HTTPRepository> repo;
|
||||
std::unique_ptr<HTTPRepository> repo;
|
||||
SGPath p(simgear::Dir::current().path());
|
||||
p.append("http_repo_merge_existing");
|
||||
simgear::Dir pd(p);
|
||||
@@ -488,7 +486,6 @@ void testMergeExistingFileWithoutDownload(HTTP::Client* cl)
|
||||
|
||||
repo.reset(new HTTPRepository(p, cl));
|
||||
repo->setBaseUrl("http://localhost:2000/repo");
|
||||
repo->setEntireRepositoryMode();
|
||||
|
||||
createFile(p, "dirC/fileCB", 4); // should match
|
||||
createFile(p, "dirC/fileCC", 3); // mismatch
|
||||
@@ -521,7 +518,7 @@ void testMergeExistingFileWithoutDownload(HTTP::Client* cl)
|
||||
|
||||
void testLossOfLocalFiles(HTTP::Client* cl)
|
||||
{
|
||||
std::auto_ptr<HTTPRepository> repo;
|
||||
std::unique_ptr<HTTPRepository> repo;
|
||||
SGPath p(simgear::Dir::current().path());
|
||||
p.append("http_repo_lose_local");
|
||||
simgear::Dir pd(p);
|
||||
@@ -531,7 +528,6 @@ void testLossOfLocalFiles(HTTP::Client* cl)
|
||||
|
||||
repo.reset(new HTTPRepository(p, cl));
|
||||
repo->setBaseUrl("http://localhost:2000/repo");
|
||||
repo->setEntireRepositoryMode();
|
||||
repo->update();
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
verifyFileState(p, "dirB/subdirA/fileBAA");
|
||||
@@ -556,7 +552,7 @@ void testLossOfLocalFiles(HTTP::Client* cl)
|
||||
|
||||
void testAbandonMissingFiles(HTTP::Client* cl)
|
||||
{
|
||||
std::auto_ptr<HTTPRepository> repo;
|
||||
std::unique_ptr<HTTPRepository> repo;
|
||||
SGPath p(simgear::Dir::current().path());
|
||||
p.append("http_repo_missing_files");
|
||||
simgear::Dir pd(p);
|
||||
@@ -569,7 +565,6 @@ void testAbandonMissingFiles(HTTP::Client* cl)
|
||||
|
||||
repo.reset(new HTTPRepository(p, cl));
|
||||
repo->setBaseUrl("http://localhost:2000/repo");
|
||||
repo->setEntireRepositoryMode();
|
||||
repo->update();
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
if (repo->failure() != HTTPRepository::REPO_PARTIAL_UPDATE) {
|
||||
@@ -581,7 +576,7 @@ void testAbandonMissingFiles(HTTP::Client* cl)
|
||||
|
||||
void testAbandonCorruptFiles(HTTP::Client* cl)
|
||||
{
|
||||
std::auto_ptr<HTTPRepository> repo;
|
||||
std::unique_ptr<HTTPRepository> repo;
|
||||
SGPath p(simgear::Dir::current().path());
|
||||
p.append("http_repo_corrupt_files");
|
||||
simgear::Dir pd(p);
|
||||
@@ -594,11 +589,10 @@ void testAbandonCorruptFiles(HTTP::Client* cl)
|
||||
|
||||
repo.reset(new HTTPRepository(p, cl));
|
||||
repo->setBaseUrl("http://localhost:2000/repo");
|
||||
repo->setEntireRepositoryMode();
|
||||
|
||||
repo->update();
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
if (repo->failure() != HTTPRepository::REPO_ERROR_CHECKSUM) {
|
||||
std::cerr << "Got failure state:" << repo->failure() << std::endl;
|
||||
throw sg_exception("Bad result from corrupt files test");
|
||||
}
|
||||
|
||||
@@ -611,149 +605,6 @@ void testAbandonCorruptFiles(HTTP::Client* cl)
|
||||
std::cout << "Passed test: detect corrupted download" << std::endl;
|
||||
}
|
||||
|
||||
void testPartialUpdateBasic(HTTP::Client* cl)
|
||||
{
|
||||
std::auto_ptr<HTTPRepository> repo;
|
||||
SGPath p(simgear::Dir::current().path());
|
||||
p.append("http_repo_partial_update");
|
||||
simgear::Dir pd(p);
|
||||
if (pd.exists()) {
|
||||
pd.removeChildren();
|
||||
}
|
||||
|
||||
global_repo->clearRequestCounts();
|
||||
global_repo->clearFailFlags();
|
||||
global_repo->defineFile("dirA/subdirF/fileAFA");
|
||||
global_repo->defineFile("dirA/subdirF/fileAFB");
|
||||
global_repo->defineFile("dirA/subdirH/fileAHA");
|
||||
global_repo->defineFile("dirA/subdirH/fileAHB");
|
||||
|
||||
global_repo->defineFile("dirG/subdirA/subsubA/fileGAAB");
|
||||
|
||||
// request subdir of A
|
||||
repo.reset(new HTTPRepository(p, cl));
|
||||
repo->setBaseUrl("http://localhost:2000/repo");
|
||||
repo->addSubpath("dirA/subdirF");
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
|
||||
verifyFileState(p, "dirA/subdirF/fileAFA");
|
||||
verifyFileState(p, "dirA/subdirF/fileAFB");
|
||||
|
||||
verifyFileState(p, "fileA"); // files are always synced
|
||||
verifyFileState(p, "dirA/fileAB");
|
||||
verifyFileNotPresent(p, "dirB/subdirB/fileBBB");
|
||||
verifyFileNotPresent(p, "dirD");
|
||||
verifyFileNotPresent(p, "dirA/subdirH/fileAHB");
|
||||
|
||||
verifyRequestCount("dirA", 1);
|
||||
verifyRequestCount("dirA/fileAA", 1);
|
||||
verifyRequestCount("dirA/subdirF", 1);
|
||||
verifyRequestCount("dirA/subdirF/fileAFA", 1);
|
||||
verifyRequestCount("dirA/subdirF/fileAFB", 1);
|
||||
verifyRequestCount("dirB", 0);
|
||||
verifyRequestCount("dirG", 0);
|
||||
|
||||
// now request dir B
|
||||
repo->addSubpath("dirB");
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
|
||||
verifyFileState(p, "dirA/subdirF/fileAFB");
|
||||
verifyFileState(p, "dirB/subdirB/fileBBA");
|
||||
verifyFileState(p, "dirB/subdirB/fileBBB");
|
||||
|
||||
verifyRequestCount("dirB", 1);
|
||||
verifyRequestCount("dirB/subdirA/fileBAC", 1);
|
||||
verifyRequestCount("dirA", 1);
|
||||
verifyRequestCount("dirA/fileAA", 1);
|
||||
verifyRequestCount("dirG", 0);
|
||||
|
||||
// widen subdir to parent
|
||||
repo->addSubpath("dirA");
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
|
||||
verifyFileState(p, "dirA/subdirH/fileAHA");
|
||||
verifyFileState(p, "dirA/subdirH/fileAHB");
|
||||
|
||||
verifyRequestCount("dirA", 1);
|
||||
verifyRequestCount("dirB/subdirA/fileBAC", 1);
|
||||
verifyRequestCount("dirA/subdirF/fileAFA", 1);
|
||||
|
||||
// request an already fetched subdir - should be a no-op
|
||||
repo->addSubpath("dirB/subdirB");
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
|
||||
verifyRequestCount("dirB", 1);
|
||||
verifyRequestCount("dirB/subdirB/fileBBB", 1);
|
||||
|
||||
// add new / modify files inside
|
||||
global_repo->defineFile("dirA/subdirF/fileAFC");
|
||||
global_repo->defineFile("dirA/subdirF/fileAFD");
|
||||
repo->update();
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
|
||||
if (global_repo->requestCount != 2) {
|
||||
throw sg_exception("Bad root request count");
|
||||
}
|
||||
|
||||
verifyFileState(p, "dirA/subdirF/fileAFC");
|
||||
verifyFileState(p, "dirA/subdirF/fileAFD");
|
||||
|
||||
std::cout << "Passed test: basic partial clone and update" << std::endl;
|
||||
}
|
||||
|
||||
void testPartialUpdateExisting(HTTP::Client* cl)
|
||||
{
|
||||
std::auto_ptr<HTTPRepository> repo;
|
||||
SGPath p(simgear::Dir::current().path());
|
||||
p.append("http_repo_partial_update_existing");
|
||||
simgear::Dir pd(p);
|
||||
if (pd.exists()) {
|
||||
pd.removeChildren();
|
||||
}
|
||||
|
||||
global_repo->clearRequestCounts();
|
||||
global_repo->clearFailFlags();
|
||||
|
||||
// full update to sync everything
|
||||
repo.reset(new HTTPRepository(p, cl));
|
||||
repo->setBaseUrl("http://localhost:2000/repo");
|
||||
repo->setEntireRepositoryMode();
|
||||
repo->update();
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
|
||||
// new repo for partial
|
||||
global_repo->clearRequestCounts();
|
||||
repo.reset(new HTTPRepository(p, cl));
|
||||
repo->setBaseUrl("http://localhost:2000/repo");
|
||||
repo->addSubpath("dirA/subdirF");
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
|
||||
if (global_repo->requestCount != 1) {
|
||||
throw sg_exception("Bad root request count");
|
||||
}
|
||||
|
||||
verifyRequestCount("dirA", 0);
|
||||
verifyRequestCount("dirA/fileAA", 0);
|
||||
verifyRequestCount("dirA/subdirF", 0);
|
||||
verifyRequestCount("dirA/subdirF/fileAFA", 0);
|
||||
verifyRequestCount("dirA/subdirF/fileAFB", 0);
|
||||
|
||||
// and request more dirs
|
||||
// this is a good simulation of terrasync requesting more subdirs of
|
||||
// an already created and in sync tree. should not generate any more
|
||||
// network trip
|
||||
repo->addSubpath("dirC");
|
||||
|
||||
verifyFileState(p, "dirC/subdirA/subsubA/fileCAAA");
|
||||
verifyRequestCount("dirC/subdirA/subsubA/fileCAAA", 0);
|
||||
|
||||
if (global_repo->requestCount != 1) {
|
||||
throw sg_exception("Bad root request count");
|
||||
}
|
||||
|
||||
std::cout << "Passed test: partial update of existing" << std::endl;
|
||||
}
|
||||
|
||||
void modifyBTree()
|
||||
{
|
||||
std::cout << "Modifying sub-tree" << std::endl;
|
||||
@@ -763,53 +614,9 @@ void modifyBTree()
|
||||
global_repo->findEntry("dirB/subdirB/fileBBB")->revision++;
|
||||
}
|
||||
|
||||
void testPartialUpdateWidenWhileInProgress(HTTP::Client* cl)
|
||||
{
|
||||
std::auto_ptr<HTTPRepository> repo;
|
||||
SGPath p(simgear::Dir::current().path());
|
||||
p.append("http_repo_partial_update_widen");
|
||||
simgear::Dir pd(p);
|
||||
if (pd.exists()) {
|
||||
pd.removeChildren();
|
||||
}
|
||||
|
||||
global_repo->clearRequestCounts();
|
||||
global_repo->clearFailFlags();
|
||||
|
||||
// full update to sync everything
|
||||
repo.reset(new HTTPRepository(p, cl));
|
||||
repo->setBaseUrl("http://localhost:2000/repo");
|
||||
|
||||
repo->addSubpath("dirA/subdirF");
|
||||
repo->addSubpath("dirB/subdirB");
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
|
||||
verifyRequestCount("dirA/subdirF", 1);
|
||||
if (global_repo->requestCount != 1) {
|
||||
throw sg_exception("Bad root request count");
|
||||
}
|
||||
|
||||
repo->addSubpath("dirA");
|
||||
repo->addSubpath("dirB");
|
||||
repo->addSubpath("dirC");
|
||||
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
|
||||
// should not request the root again
|
||||
verifyRequestCount("dirA/subdirF", 1);
|
||||
if (global_repo->requestCount != 1) {
|
||||
throw sg_exception("Bad root request count");
|
||||
}
|
||||
|
||||
verifyFileState(p, "dirA/subdirF/fileAFA");
|
||||
verifyFileState(p, "dirC/subdirA/subsubA/fileCAAA");
|
||||
|
||||
std::cout << "Passed test: partial update with widen" << std::endl;
|
||||
}
|
||||
|
||||
void testServerModifyDuringSync(HTTP::Client* cl)
|
||||
{
|
||||
std::auto_ptr<HTTPRepository> repo;
|
||||
std::unique_ptr<HTTPRepository> repo;
|
||||
SGPath p(simgear::Dir::current().path());
|
||||
p.append("http_repo_server_modify_during_sync");
|
||||
simgear::Dir pd(p);
|
||||
@@ -822,7 +629,6 @@ void testServerModifyDuringSync(HTTP::Client* cl)
|
||||
|
||||
repo.reset(new HTTPRepository(p, cl));
|
||||
repo->setBaseUrl("http://localhost:2000/repo");
|
||||
repo->setEntireRepositoryMode();
|
||||
|
||||
global_repo->findEntry("dirA/fileAA")->accessCallback.reset(make_callback(&modifyBTree));
|
||||
|
||||
@@ -832,7 +638,7 @@ void testServerModifyDuringSync(HTTP::Client* cl)
|
||||
global_repo->findEntry("dirA/fileAA")->accessCallback.reset();
|
||||
|
||||
if (repo->failure() != HTTPRepository::REPO_ERROR_CHECKSUM) {
|
||||
throw sg_exception("Bad result from corrupt files test");
|
||||
throw sg_exception("Bad result from modify during sync test");
|
||||
}
|
||||
|
||||
std::cout << "Passed test modify server during sync" << std::endl;
|
||||
@@ -841,7 +647,7 @@ void testServerModifyDuringSync(HTTP::Client* cl)
|
||||
|
||||
void testDestroyDuringSync(HTTP::Client* cl)
|
||||
{
|
||||
std::auto_ptr<HTTPRepository> repo;
|
||||
std::unique_ptr<HTTPRepository> repo;
|
||||
SGPath p(simgear::Dir::current().path());
|
||||
p.append("http_repo_destory_during_sync");
|
||||
simgear::Dir pd(p);
|
||||
@@ -854,7 +660,6 @@ void testDestroyDuringSync(HTTP::Client* cl)
|
||||
|
||||
repo.reset(new HTTPRepository(p, cl));
|
||||
repo->setBaseUrl("http://localhost:2000/repo");
|
||||
repo->setEntireRepositoryMode();
|
||||
|
||||
repo->update();
|
||||
|
||||
@@ -869,10 +674,63 @@ void testDestroyDuringSync(HTTP::Client* cl)
|
||||
std::cout << "Passed test destory during sync" << std::endl;
|
||||
}
|
||||
|
||||
void testCopyInstalledChildren(HTTP::Client* cl)
|
||||
{
|
||||
std::unique_ptr<HTTPRepository> repo;
|
||||
SGPath p(simgear::Dir::current().path());
|
||||
p.append("http_repo_copy_installed_children");
|
||||
simgear::Dir pd(p);
|
||||
if (pd.exists()) {
|
||||
pd.removeChildren();
|
||||
}
|
||||
|
||||
// setup installation data
|
||||
SGPath p2(simgear::Dir::current().path());
|
||||
p2.append("http_repo_copy_installed_children_install");
|
||||
simgear::Dir pd2(p2);
|
||||
if (pd2.exists()) {
|
||||
pd2.removeChildren();
|
||||
} else {
|
||||
pd2.create(0700);
|
||||
}
|
||||
|
||||
// fill in 'install' tree data
|
||||
createFile(p, "dirJ/fileJA", 2);
|
||||
createFile(p, "dirJ/fileJB", 3);
|
||||
createFile(p, "dirJ/fileJC", 1);
|
||||
|
||||
global_repo->defineFile("dirJ/fileJA", 2);
|
||||
global_repo->defineFile("dirJ/fileJB", 3);
|
||||
global_repo->defineFile("dirJ/fileJC", 3); // newer
|
||||
global_repo->defineFile("dirJ/fileJD", 3); // not present in install
|
||||
|
||||
global_repo->clearRequestCounts();
|
||||
global_repo->clearFailFlags();
|
||||
|
||||
repo.reset(new HTTPRepository(p, cl));
|
||||
repo->setBaseUrl("http://localhost:2000/repo");
|
||||
repo->setInstalledCopyPath(p2);
|
||||
repo->update();
|
||||
|
||||
// verify correct files were downloaded, only dirs
|
||||
|
||||
waitForUpdateComplete(cl, repo.get());
|
||||
verifyFileState(p, "dirJ/fileJA");
|
||||
verifyFileState(p, "dirJ/fileJB");
|
||||
verifyFileState(p, "dirJ/fileJC");
|
||||
verifyFileState(p, "dirJ/fileJD");
|
||||
|
||||
verifyRequestCount("dirJ/fileJA", 0);
|
||||
verifyRequestCount("dirJ/fileJB", 0);
|
||||
verifyRequestCount("dirJ/fileJC", 1);
|
||||
verifyRequestCount("dirJ/fileJD", 1);
|
||||
|
||||
std::cout << "Copy installed children" << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
sglog().setLogLevels( SG_ALL, SG_DEBUG );
|
||||
sglog().setLogLevels( SG_ALL, SG_INFO );
|
||||
|
||||
HTTP::Client cl;
|
||||
cl.setMaxConnections(1);
|
||||
@@ -907,13 +765,14 @@ int main(int argc, char* argv[])
|
||||
testServer.disconnectAll();
|
||||
cl.clearAllConnections();
|
||||
|
||||
testPartialUpdateBasic(&cl);
|
||||
testPartialUpdateExisting(&cl);
|
||||
testPartialUpdateWidenWhileInProgress(&cl);
|
||||
|
||||
testServerModifyDuringSync(&cl);
|
||||
|
||||
testDestroyDuringSync(&cl);
|
||||
|
||||
testServer.disconnectAll();
|
||||
cl.clearAllConnections();
|
||||
|
||||
testCopyInstalledChildren(&cl);
|
||||
|
||||
std::cout << "all tests passed ok" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// Test harness.
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include <simgear_config.h>
|
||||
#include <simgear/compiler.h>
|
||||
|
||||
#include <iostream>
|
||||
@@ -30,7 +31,7 @@ void testTarGz()
|
||||
uint8_t* buf = (uint8_t*) alloca(8192);
|
||||
size_t bufSize = f.read((char*) buf, 8192);
|
||||
|
||||
VERIFY(TarExtractor::isTarData(buf, bufSize));
|
||||
SG_VERIFY(TarExtractor::isTarData(buf, bufSize));
|
||||
|
||||
}
|
||||
|
||||
@@ -45,7 +46,7 @@ void testPlainTar()
|
||||
uint8_t* buf = (uint8_t*) alloca(8192);
|
||||
size_t bufSize = f.read((char*) buf, 8192);
|
||||
|
||||
VERIFY(TarExtractor::isTarData(buf, bufSize));
|
||||
SG_VERIFY(TarExtractor::isTarData(buf, bufSize));
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
#include <simgear/misc/strutils.hxx>
|
||||
#include <simgear/timing/timestamp.hxx>
|
||||
#include <simgear/debug/logstream.hxx>
|
||||
#include <simgear/misc/test_macros.hxx>
|
||||
|
||||
#include <curl/multi.h>
|
||||
|
||||
@@ -41,18 +42,6 @@ const char* BODY3 = "Cras ut neque nulla. Duis ut velit neque, sit amet "
|
||||
const unsigned int body2Size = 8 * 1024;
|
||||
char body2[body2Size];
|
||||
|
||||
#define COMPARE(a, b) \
|
||||
if ((a) != (b)) { \
|
||||
cerr << "failed:" << #a << " != " << #b << endl; \
|
||||
cerr << "\tgot:'" << a << "'" << endl; \
|
||||
exit(1); \
|
||||
}
|
||||
|
||||
#define VERIFY(a) \
|
||||
if (!(a)) { \
|
||||
cerr << "failed:" << #a << endl; \
|
||||
exit(1); \
|
||||
}
|
||||
|
||||
class TestRequest : public HTTP::Request
|
||||
{
|
||||
@@ -126,8 +115,9 @@ public:
|
||||
d << contentStr;
|
||||
push(d.str().c_str());
|
||||
} else if (path == "/test_headers") {
|
||||
COMPARE(requestHeaders["X-Foo"], string("Bar"));
|
||||
COMPARE(requestHeaders["X-AnotherHeader"], string("A longer value"));
|
||||
SG_CHECK_EQUAL(requestHeaders["X-Foo"], string("Bar"));
|
||||
SG_CHECK_EQUAL(requestHeaders["X-AnotherHeader"],
|
||||
string("A longer value"));
|
||||
|
||||
string contentStr(BODY1);
|
||||
stringstream d;
|
||||
@@ -304,7 +294,7 @@ public:
|
||||
} else if (path == "/test_put") {
|
||||
std::cerr << "sending PUT response" << std::endl;
|
||||
|
||||
COMPARE(buffer, BODY3);
|
||||
SG_CHECK_EQUAL(buffer, BODY3);
|
||||
stringstream d;
|
||||
d << "HTTP/1.1 " << 204 << " " << reasonForCode(204) << "\r\n";
|
||||
d << "\r\n"; // final CRLF to terminate the headers
|
||||
@@ -314,7 +304,7 @@ public:
|
||||
|
||||
std::string entityStr = "http://localhost:2000/something.txt";
|
||||
|
||||
COMPARE(buffer, BODY3);
|
||||
SG_CHECK_EQUAL(buffer, BODY3);
|
||||
stringstream d;
|
||||
d << "HTTP/1.1 " << 201 << " " << reasonForCode(201) << "\r\n";
|
||||
d << "Location:" << entityStr << "\r\n";
|
||||
@@ -385,18 +375,18 @@ int main(int argc, char* argv[])
|
||||
|
||||
// test URL parsing
|
||||
TestRequest* tr1 = new TestRequest("http://localhost.woo.zar:2000/test1?foo=bar");
|
||||
COMPARE(tr1->scheme(), "http");
|
||||
COMPARE(tr1->hostAndPort(), "localhost.woo.zar:2000");
|
||||
COMPARE(tr1->host(), "localhost.woo.zar");
|
||||
COMPARE(tr1->port(), 2000);
|
||||
COMPARE(tr1->path(), "/test1");
|
||||
SG_CHECK_EQUAL(tr1->scheme(), "http");
|
||||
SG_CHECK_EQUAL(tr1->hostAndPort(), "localhost.woo.zar:2000");
|
||||
SG_CHECK_EQUAL(tr1->host(), "localhost.woo.zar");
|
||||
SG_CHECK_EQUAL(tr1->port(), 2000);
|
||||
SG_CHECK_EQUAL(tr1->path(), "/test1");
|
||||
|
||||
TestRequest* tr2 = new TestRequest("http://192.168.1.1/test1/dir/thing/file.png");
|
||||
COMPARE(tr2->scheme(), "http");
|
||||
COMPARE(tr2->hostAndPort(), "192.168.1.1");
|
||||
COMPARE(tr2->host(), "192.168.1.1");
|
||||
COMPARE(tr2->port(), 80);
|
||||
COMPARE(tr2->path(), "/test1/dir/thing/file.png");
|
||||
SG_CHECK_EQUAL(tr2->scheme(), "http");
|
||||
SG_CHECK_EQUAL(tr2->hostAndPort(), "192.168.1.1");
|
||||
SG_CHECK_EQUAL(tr2->host(), "192.168.1.1");
|
||||
SG_CHECK_EQUAL(tr2->port(), 80);
|
||||
SG_CHECK_EQUAL(tr2->path(), "/test1/dir/thing/file.png");
|
||||
|
||||
// basic get request
|
||||
{
|
||||
@@ -405,11 +395,11 @@ int main(int argc, char* argv[])
|
||||
cl.makeRequest(tr);
|
||||
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseReason(), string("OK"));
|
||||
COMPARE(tr->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr->responseBytesReceived(), strlen(BODY1));
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseReason(), string("OK"));
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
}
|
||||
|
||||
{
|
||||
@@ -418,11 +408,11 @@ int main(int argc, char* argv[])
|
||||
cl.makeRequest(tr);
|
||||
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseReason(), string("OK"));
|
||||
COMPARE(tr->responseLength(), strlen(BODY3));
|
||||
COMPARE(tr->responseBytesReceived(), strlen(BODY3));
|
||||
COMPARE(tr->bodyData, string(BODY3));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseReason(), string("OK"));
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY3));
|
||||
}
|
||||
|
||||
{
|
||||
@@ -430,7 +420,7 @@ int main(int argc, char* argv[])
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -441,11 +431,11 @@ int main(int argc, char* argv[])
|
||||
cl.makeRequest(tr);
|
||||
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseReason(), string("OK"));
|
||||
COMPARE(tr->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr->responseBytesReceived(), strlen(BODY1));
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseReason(), string("OK"));
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
}
|
||||
|
||||
// larger get request
|
||||
@@ -458,9 +448,9 @@ int main(int argc, char* argv[])
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseBytesReceived(), body2Size);
|
||||
COMPARE(tr->bodyData, string(body2, body2Size));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), body2Size);
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(body2, body2Size));
|
||||
}
|
||||
|
||||
cerr << "testing chunked transfer encoding" << endl;
|
||||
@@ -470,12 +460,12 @@ int main(int argc, char* argv[])
|
||||
cl.makeRequest(tr);
|
||||
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseReason(), string("OK"));
|
||||
COMPARE(tr->responseBytesReceived(), 30);
|
||||
COMPARE(tr->bodyData, "ABCDEFGHABCDEFABCDSTUVABCDSTUV");
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseReason(), string("OK"));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), 30);
|
||||
SG_CHECK_EQUAL(tr->bodyData, "ABCDEFGHABCDEFABCDSTUVABCDSTUV");
|
||||
// check trailers made it too
|
||||
COMPARE(tr->headers["x-foobar"], string("wibble"));
|
||||
SG_CHECK_EQUAL(tr->headers["x-foobar"], string("wibble"));
|
||||
}
|
||||
|
||||
// test 404
|
||||
@@ -484,9 +474,9 @@ int main(int argc, char* argv[])
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 404);
|
||||
COMPARE(tr->responseReason(), string("not found"));
|
||||
COMPARE(tr->responseLength(), 0);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 404);
|
||||
SG_CHECK_EQUAL(tr->responseReason(), string("not found"));
|
||||
SG_CHECK_EQUAL(tr->responseLength(), 0);
|
||||
}
|
||||
|
||||
cout << "done 404 test" << endl;
|
||||
@@ -496,7 +486,7 @@ int main(int argc, char* argv[])
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
}
|
||||
|
||||
cout << "done1" << endl;
|
||||
@@ -506,9 +496,9 @@ int main(int argc, char* argv[])
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
}
|
||||
|
||||
cout << "done2" << endl;
|
||||
@@ -518,9 +508,9 @@ int main(int argc, char* argv[])
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
}
|
||||
cout << "done3" << endl;
|
||||
// test connectToHost failure
|
||||
@@ -532,7 +522,7 @@ int main(int argc, char* argv[])
|
||||
waitForFailed(&cl, tr);
|
||||
|
||||
const int HOST_NOT_FOUND_CODE = CURLE_COULDNT_RESOLVE_HOST;
|
||||
COMPARE(tr->responseCode(), HOST_NOT_FOUND_CODE);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), HOST_NOT_FOUND_CODE);
|
||||
}
|
||||
|
||||
cout << "testing abrupt close" << endl;
|
||||
@@ -544,7 +534,7 @@ int main(int argc, char* argv[])
|
||||
waitForFailed(&cl, tr);
|
||||
|
||||
const int SERVER_NO_DATA_CODE = CURLE_GOT_NOTHING;
|
||||
COMPARE(tr->responseCode(), SERVER_NO_DATA_CODE);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), SERVER_NO_DATA_CODE);
|
||||
}
|
||||
|
||||
cout << "testing proxy close" << endl;
|
||||
@@ -555,9 +545,9 @@ cout << "testing proxy close" << endl;
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseLength(), body2Size);
|
||||
COMPARE(tr->bodyData, string(body2, body2Size));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseLength(), body2Size);
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(body2, body2Size));
|
||||
}
|
||||
|
||||
{
|
||||
@@ -566,9 +556,9 @@ cout << "testing proxy close" << endl;
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseBytesReceived(), body2Size);
|
||||
COMPARE(tr->bodyData, string(body2, body2Size));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), body2Size);
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(body2, body2Size));
|
||||
}
|
||||
|
||||
// pipelining
|
||||
@@ -593,17 +583,17 @@ cout << "testing proxy close" << endl;
|
||||
cl.makeRequest(tr3);
|
||||
|
||||
waitForComplete(&cl, tr3);
|
||||
VERIFY(tr->complete);
|
||||
VERIFY(tr2->complete);
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_VERIFY(tr->complete);
|
||||
SG_VERIFY(tr2->complete);
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
|
||||
COMPARE(tr2->responseLength(), strlen(BODY3));
|
||||
COMPARE(tr2->responseBytesReceived(), strlen(BODY3));
|
||||
COMPARE(tr2->bodyData, string(BODY3));
|
||||
SG_CHECK_EQUAL(tr2->responseLength(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr2->responseBytesReceived(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr2->bodyData, string(BODY3));
|
||||
|
||||
COMPARE(tr3->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->bodyData, string(BODY1));
|
||||
|
||||
COMPARE(testServer.connectCount(), 1);
|
||||
SG_CHECK_EQUAL(testServer.connectCount(), 1);
|
||||
}
|
||||
|
||||
// multiple requests with an HTTP 1.0 server
|
||||
@@ -624,17 +614,17 @@ cout << "testing proxy close" << endl;
|
||||
cl.makeRequest(tr3);
|
||||
|
||||
waitForComplete(&cl, tr3);
|
||||
VERIFY(tr->complete);
|
||||
VERIFY(tr2->complete);
|
||||
SG_VERIFY(tr->complete);
|
||||
SG_VERIFY(tr2->complete);
|
||||
|
||||
COMPARE(tr->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr->responseBytesReceived(), strlen(BODY1));
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
|
||||
COMPARE(tr2->responseLength(), strlen(BODY3));
|
||||
COMPARE(tr2->responseBytesReceived(), strlen(BODY3));
|
||||
COMPARE(tr2->bodyData, string(BODY3));
|
||||
COMPARE(tr3->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr2->responseLength(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr2->responseBytesReceived(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr2->bodyData, string(BODY3));
|
||||
SG_CHECK_EQUAL(tr3->bodyData, string(BODY1));
|
||||
}
|
||||
|
||||
// POST
|
||||
@@ -646,7 +636,7 @@ cout << "testing proxy close" << endl;
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 204);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 204);
|
||||
}
|
||||
|
||||
// PUT
|
||||
@@ -658,7 +648,7 @@ cout << "testing proxy close" << endl;
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 204);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 204);
|
||||
}
|
||||
|
||||
{
|
||||
@@ -669,7 +659,7 @@ cout << "testing proxy close" << endl;
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 201);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 201);
|
||||
}
|
||||
|
||||
// test_zero_length_content
|
||||
@@ -679,9 +669,9 @@ cout << "testing proxy close" << endl;
|
||||
HTTP::Request_ptr own(tr);
|
||||
cl.makeRequest(tr);
|
||||
waitForComplete(&cl, tr);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->bodyData, string());
|
||||
COMPARE(tr->responseBytesReceived(), 0);
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->bodyData, string());
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), 0);
|
||||
}
|
||||
|
||||
// test cancel
|
||||
@@ -709,12 +699,12 @@ cout << "testing proxy close" << endl;
|
||||
|
||||
waitForComplete(&cl, tr3);
|
||||
|
||||
COMPARE(tr->responseCode(), -1);
|
||||
COMPARE(tr2->responseReason(), "my reason 2");
|
||||
SG_CHECK_EQUAL(tr->responseCode(), -1);
|
||||
SG_CHECK_EQUAL(tr2->responseReason(), "my reason 2");
|
||||
|
||||
COMPARE(tr3->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr3->responseBytesReceived(), strlen(BODY1));
|
||||
COMPARE(tr3->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->bodyData, string(BODY1));
|
||||
}
|
||||
|
||||
// test cancel
|
||||
@@ -740,16 +730,16 @@ cout << "testing proxy close" << endl;
|
||||
|
||||
waitForComplete(&cl, tr3);
|
||||
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr->responseBytesReceived(), strlen(BODY1));
|
||||
COMPARE(tr->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY1));
|
||||
|
||||
COMPARE(tr2->responseCode(), -1);
|
||||
SG_CHECK_EQUAL(tr2->responseCode(), -1);
|
||||
|
||||
COMPARE(tr3->responseLength(), strlen(BODY1));
|
||||
COMPARE(tr3->responseBytesReceived(), strlen(BODY1));
|
||||
COMPARE(tr3->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->responseLength(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr3->bodyData, string(BODY1));
|
||||
}
|
||||
|
||||
{
|
||||
@@ -774,12 +764,12 @@ cout << "testing proxy close" << endl;
|
||||
cl.makeRequest(tr2);
|
||||
|
||||
waitForComplete(&cl, tr2);
|
||||
COMPARE(tr->responseCode(), 200);
|
||||
COMPARE(tr->bodyData, string(BODY3));
|
||||
COMPARE(tr->responseBytesReceived(), strlen(BODY3));
|
||||
COMPARE(tr2->responseCode(), 200);
|
||||
COMPARE(tr2->bodyData, string(BODY1));
|
||||
COMPARE(tr2->responseBytesReceived(), strlen(BODY1));
|
||||
SG_CHECK_EQUAL(tr->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr->bodyData, string(BODY3));
|
||||
SG_CHECK_EQUAL(tr->responseBytesReceived(), strlen(BODY3));
|
||||
SG_CHECK_EQUAL(tr2->responseCode(), 200);
|
||||
SG_CHECK_EQUAL(tr2->bodyData, string(BODY1));
|
||||
SG_CHECK_EQUAL(tr2->responseBytesReceived(), strlen(BODY1));
|
||||
}
|
||||
|
||||
cout << "all tests passed ok" << endl;
|
||||
|
||||
@@ -15,6 +15,8 @@
|
||||
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
//
|
||||
|
||||
#include <simgear_config.h>
|
||||
|
||||
#include "untar.hxx"
|
||||
|
||||
#include <cstdlib>
|
||||
@@ -101,7 +103,7 @@ public:
|
||||
};
|
||||
|
||||
size_t bytesRemaining;
|
||||
std::auto_ptr<SGFile> currentFile;
|
||||
std::unique_ptr<SGFile> currentFile;
|
||||
size_t currentFileSize;
|
||||
z_stream zlibStream;
|
||||
uint8_t* zlibOutput;
|
||||
|
||||
@@ -44,7 +44,7 @@ public:
|
||||
bool hasError() const;
|
||||
|
||||
private:
|
||||
std::auto_ptr<TarExtractorPrivate> d;
|
||||
std::unique_ptr<TarExtractorPrivate> d;
|
||||
};
|
||||
|
||||
} // of namespace simgear
|
||||
|
||||
@@ -33,6 +33,7 @@ set(HEADERS
|
||||
sg_types.hxx
|
||||
sg_random.h
|
||||
simd.hxx
|
||||
simd4x4.hxx
|
||||
)
|
||||
|
||||
set(SOURCES
|
||||
@@ -46,6 +47,10 @@ simgear_component(math math "${SOURCES}" "${HEADERS}")
|
||||
|
||||
if(ENABLE_TESTS)
|
||||
|
||||
add_executable(sgvec4_test test_sgvec4.cxx)
|
||||
target_link_libraries(sgvec4_test ${TEST_LIBS})
|
||||
add_test(sgvec4 ${EXECUTABLE_OUTPUT_PATH}/sgvec4_test)
|
||||
|
||||
add_executable(math_test SGMathTest.cxx)
|
||||
target_link_libraries(math_test ${TEST_LIBS})
|
||||
add_test(math ${EXECUTABLE_OUTPUT_PATH}/math_test)
|
||||
|
||||
@@ -28,6 +28,9 @@
|
||||
#include "SGRect.hxx"
|
||||
#include "sg_random.h"
|
||||
|
||||
int lineno = 0;
|
||||
|
||||
|
||||
template<typename T>
|
||||
bool
|
||||
Vec3Test(void)
|
||||
@@ -38,45 +41,45 @@ Vec3Test(void)
|
||||
v1 = SGVec3<T>(1, 2, 3);
|
||||
v2 = SGVec3<T>(3, 2, 1);
|
||||
if (equivalent(v1, v2))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check the unary minus operator
|
||||
v3 = SGVec3<T>(-1, -2, -3);
|
||||
if (!equivalent(-v1, v3))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check the unary plus operator
|
||||
v3 = SGVec3<T>(1, 2, 3);
|
||||
if (!equivalent(+v1, v3))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check the addition operator
|
||||
v3 = SGVec3<T>(4, 4, 4);
|
||||
if (!equivalent(v1 + v2, v3))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check the subtraction operator
|
||||
v3 = SGVec3<T>(-2, 0, 2);
|
||||
if (!equivalent(v1 - v2, v3))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check the scaler multiplication operator
|
||||
v3 = SGVec3<T>(2, 4, 6);
|
||||
if (!equivalent(2*v1, v3))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check the dot product
|
||||
if (fabs(dot(v1, v2) - 10) > 10*SGLimits<T>::epsilon())
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check the cross product
|
||||
v3 = SGVec3<T>(-4, 8, -4);
|
||||
if (!equivalent(cross(v1, v2), v3))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check the euclidean length
|
||||
if (fabs(14 - length(v1)*length(v1)) > 14*SGLimits<T>::epsilon())
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -89,11 +92,11 @@ isSameRotation(const SGQuat<T>& q1, const SGQuat<T>& q2)
|
||||
const SGVec3<T> e2(0, 1, 0);
|
||||
const SGVec3<T> e3(0, 0, 1);
|
||||
if (!equivalent(q1.transform(e1), q2.transform(e1)))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
if (!equivalent(q1.transform(e2), q2.transform(e2)))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
if (!equivalent(q1.transform(e3), q2.transform(e3)))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -111,37 +114,37 @@ QuatTest(void)
|
||||
v1 = SGVec3<T>(1, 2, 3);
|
||||
v2 = SGVec3<T>(1, -2, -3);
|
||||
if (!equivalent(q1.transform(v1), v2))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check a rotation around the x axis
|
||||
q1 = SGQuat<T>::fromAngleAxis(0.5*SGMisc<T>::pi(), e1);
|
||||
v2 = SGVec3<T>(1, 3, -2);
|
||||
if (!equivalent(q1.transform(v1), v2))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check a rotation around the y axis
|
||||
q1 = SGQuat<T>::fromAngleAxis(SGMisc<T>::pi(), e2);
|
||||
v2 = SGVec3<T>(-1, 2, -3);
|
||||
if (!equivalent(q1.transform(v1), v2))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check a rotation around the y axis
|
||||
q1 = SGQuat<T>::fromAngleAxis(0.5*SGMisc<T>::pi(), e2);
|
||||
v2 = SGVec3<T>(-3, 2, 1);
|
||||
if (!equivalent(q1.transform(v1), v2))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check a rotation around the z axis
|
||||
q1 = SGQuat<T>::fromAngleAxis(SGMisc<T>::pi(), e3);
|
||||
v2 = SGVec3<T>(-1, -2, 3);
|
||||
if (!equivalent(q1.transform(v1), v2))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check a rotation around the z axis
|
||||
q1 = SGQuat<T>::fromAngleAxis(0.5*SGMisc<T>::pi(), e3);
|
||||
v2 = SGVec3<T>(2, -1, 3);
|
||||
if (!equivalent(q1.transform(v1), v2))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Now check some successive transforms
|
||||
// We can reuse the prevously tested stuff
|
||||
@@ -150,7 +153,7 @@ QuatTest(void)
|
||||
q3 = q1*q2;
|
||||
v2 = q2.transform(q1.transform(v1));
|
||||
if (!equivalent(q3.transform(v1), v2))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
/// Test from Euler angles
|
||||
float x = 0.2*SGMisc<T>::pi();
|
||||
@@ -162,7 +165,7 @@ QuatTest(void)
|
||||
v2 = q3.transform(q2.transform(q1.transform(v1)));
|
||||
q4 = SGQuat<T>::fromEulerRad(z, y, x);
|
||||
if (!equivalent(q4.transform(v1), v2))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
/// Test angle axis forward and back transform
|
||||
q1 = SGQuat<T>::fromAngleAxis(0.2*SGMisc<T>::pi(), e1);
|
||||
@@ -172,15 +175,15 @@ QuatTest(void)
|
||||
q1.getAngleAxis(angleAxis);
|
||||
q4 = SGQuat<T>::fromAngleAxis(angleAxis);
|
||||
if (!isSameRotation(q1, q4))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
q2.getAngleAxis(angleAxis);
|
||||
q4 = SGQuat<T>::fromAngleAxis(angleAxis);
|
||||
if (!isSameRotation(q2, q4))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
q3.getAngleAxis(angleAxis);
|
||||
q4 = SGQuat<T>::fromAngleAxis(angleAxis);
|
||||
if (!isSameRotation(q3, q4))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
/// Test angle axis forward and back transform
|
||||
q1 = SGQuat<T>::fromAngleAxis(0.2*SGMisc<T>::pi(), e1);
|
||||
@@ -189,15 +192,15 @@ QuatTest(void)
|
||||
SGVec3<T> positiveAngleAxis = q1.getPositiveRealImag();
|
||||
q4 = SGQuat<T>::fromPositiveRealImag(positiveAngleAxis);
|
||||
if (!isSameRotation(q1, q4))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
positiveAngleAxis = q2.getPositiveRealImag();
|
||||
q4 = SGQuat<T>::fromPositiveRealImag(positiveAngleAxis);
|
||||
if (!isSameRotation(q2, q4))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
positiveAngleAxis = q3.getPositiveRealImag();
|
||||
q4 = SGQuat<T>::fromPositiveRealImag(positiveAngleAxis);
|
||||
if (!isSameRotation(q3, q4))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -219,13 +222,13 @@ QuatDerivativeTest(void)
|
||||
// Check if we can restore the angular velocity
|
||||
SGVec3<T> av2 = SGQuat<T>::forwardDifferenceVelocity(o0, o1, dt);
|
||||
if (!equivalent(av, av2))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Test with the equivalent orientation
|
||||
o1 = -o1;
|
||||
av2 = SGQuat<T>::forwardDifferenceVelocity(o0, o1, dt);
|
||||
if (!equivalent(av, av2))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@@ -250,23 +253,23 @@ MatrixTest(void)
|
||||
invert(m2, m0);
|
||||
m3 = transNeg(m0);
|
||||
if (!equivalent(m1, m2))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
if (!equivalent(m2, m3))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Check matrix multiplication and inversion
|
||||
if (!equivalent(m0*m1, SGMatrix<T>::unit()))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
if (!equivalent(m1*m0, SGMatrix<T>::unit()))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
if (!equivalent(m0*m2, SGMatrix<T>::unit()))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
if (!equivalent(m2*m0, SGMatrix<T>::unit()))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
if (!equivalent(m0*m3, SGMatrix<T>::unit()))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
if (!equivalent(m3*m0, SGMatrix<T>::unit()))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -276,26 +279,26 @@ void doRectTest()
|
||||
{
|
||||
SGRect<T> rect(10, 15, 20, 25);
|
||||
|
||||
COMPARE(rect.x(), 10)
|
||||
COMPARE(rect.y(), 15)
|
||||
COMPARE(rect.width(), 20)
|
||||
COMPARE(rect.height(), 25)
|
||||
SG_CHECK_EQUAL(rect.x(), 10)
|
||||
SG_CHECK_EQUAL(rect.y(), 15)
|
||||
SG_CHECK_EQUAL(rect.width(), 20)
|
||||
SG_CHECK_EQUAL(rect.height(), 25)
|
||||
|
||||
COMPARE(rect.pos(), SGVec2<T>(10, 15))
|
||||
COMPARE(rect.size(), SGVec2<T>(20, 25))
|
||||
SG_CHECK_EQUAL(rect.pos(), SGVec2<T>(10, 15))
|
||||
SG_CHECK_EQUAL(rect.size(), SGVec2<T>(20, 25))
|
||||
|
||||
COMPARE(rect.l(), 10)
|
||||
COMPARE(rect.t(), 15)
|
||||
COMPARE(rect.r(), 30)
|
||||
COMPARE(rect.b(), 40)
|
||||
SG_CHECK_EQUAL(rect.l(), 10)
|
||||
SG_CHECK_EQUAL(rect.t(), 15)
|
||||
SG_CHECK_EQUAL(rect.r(), 30)
|
||||
SG_CHECK_EQUAL(rect.b(), 40)
|
||||
|
||||
VERIFY(rect == rect)
|
||||
VERIFY(rect == SGRect<T>(10, 15, 20, 25))
|
||||
VERIFY(rect != SGRect<T>(11, 15, 20, 25))
|
||||
SG_VERIFY(rect == rect)
|
||||
SG_VERIFY(rect == SGRect<T>(10, 15, 20, 25))
|
||||
SG_VERIFY(rect != SGRect<T>(11, 15, 20, 25))
|
||||
|
||||
VERIFY(rect.contains(10, 15))
|
||||
VERIFY(!rect.contains(9, 15))
|
||||
VERIFY(rect.contains(9, 15, 1))
|
||||
SG_VERIFY(rect.contains(10, 15))
|
||||
SG_VERIFY(!rect.contains(9, 15))
|
||||
SG_VERIFY(rect.contains(9, 15, 1))
|
||||
}
|
||||
|
||||
bool
|
||||
@@ -320,13 +323,13 @@ GeodesyTest(void)
|
||||
if (epsDeg < fabs(geod0.getLongitudeDeg() - geod1.getLongitudeDeg()) ||
|
||||
epsDeg < fabs(geod0.getLatitudeDeg() - geod1.getLatitudeDeg()) ||
|
||||
epsM < fabs(geod0.getElevationM() - geod1.getElevationM()))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// Test the conversion routines to radial coordinates
|
||||
geoc0 = SGGeoc::fromCart(cart0);
|
||||
cart1 = SGVec3<double>::fromGeoc(geoc0);
|
||||
if (!equivalent(cart0, cart1))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
// test course / advance routines
|
||||
// uses examples from Williams aviation formulary
|
||||
@@ -336,12 +339,12 @@ GeodesyTest(void)
|
||||
double distNm = SGGeodesy::distanceRad(lax, jfk) * SG_RAD_TO_NM;
|
||||
std::cout << "distance is " << distNm << std::endl;
|
||||
if (0.5 < fabs(distNm - 2144)) // 2144 nm
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
double crsDeg = SGGeodesy::courseRad(lax, jfk) * SG_RADIANS_TO_DEGREES;
|
||||
std::cout << "course is " << crsDeg << std::endl;
|
||||
if (0.5 < fabs(crsDeg - 66)) // 66 degrees
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
SGGeoc adv;
|
||||
SGGeodesy::advanceRadM(lax, crsDeg * SG_DEGREES_TO_RADIANS, 100 * SG_NM_TO_METER, adv);
|
||||
@@ -349,7 +352,7 @@ GeodesyTest(void)
|
||||
|
||||
if (0.01 < fabs(adv.getLongitudeRad() - (-2.034206)) ||
|
||||
0.01 < fabs(adv.getLatitudeRad() - 0.604180))
|
||||
return false;
|
||||
{ lineno = __LINE__; return false; }
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -361,25 +364,25 @@ main(void)
|
||||
|
||||
// Do vector tests
|
||||
if (!Vec3Test<float>())
|
||||
return EXIT_FAILURE;
|
||||
{ fprintf(stderr, "Error at line: %i called from line: %i\n", lineno, __LINE__); return EXIT_FAILURE; }
|
||||
if (!Vec3Test<double>())
|
||||
return EXIT_FAILURE;
|
||||
{ fprintf(stderr, "Error at line: %i called from line: %i\n", lineno, __LINE__); return EXIT_FAILURE; }
|
||||
|
||||
// Do quaternion tests
|
||||
if (!QuatTest<float>())
|
||||
return EXIT_FAILURE;
|
||||
{ fprintf(stderr, "Error at line: %i called from line: %i\n", lineno, __LINE__); return EXIT_FAILURE; }
|
||||
if (!QuatTest<double>())
|
||||
return EXIT_FAILURE;
|
||||
{ fprintf(stderr, "Error at line: %i called from line: %i\n", lineno, __LINE__); return EXIT_FAILURE; }
|
||||
if (!QuatDerivativeTest<float>())
|
||||
return EXIT_FAILURE;
|
||||
{ fprintf(stderr, "Error at line: %i called from line: %i\n", lineno, __LINE__); return EXIT_FAILURE; }
|
||||
if (!QuatDerivativeTest<double>())
|
||||
return EXIT_FAILURE;
|
||||
{ fprintf(stderr, "Error at line: %i called from line: %i\n", lineno, __LINE__); return EXIT_FAILURE; }
|
||||
|
||||
// Do matrix tests
|
||||
if (!MatrixTest<float>())
|
||||
return EXIT_FAILURE;
|
||||
{ fprintf(stderr, "Error at line: %i called from line: %i\n", lineno, __LINE__); return EXIT_FAILURE; }
|
||||
if (!MatrixTest<double>())
|
||||
return EXIT_FAILURE;
|
||||
{ fprintf(stderr, "Error at line: %i called from line: %i\n", lineno, __LINE__); return EXIT_FAILURE; }
|
||||
|
||||
// Do rect tests
|
||||
doRectTest<int>();
|
||||
@@ -387,7 +390,7 @@ main(void)
|
||||
|
||||
// Check geodetic/geocentric/cartesian conversions
|
||||
if (!GeodesyTest())
|
||||
return EXIT_FAILURE;
|
||||
{ fprintf(stderr, "Error at line: %i called from line: %i\n", lineno, __LINE__); return EXIT_FAILURE; }
|
||||
|
||||
std::cout << "Successfully passed all tests!" << std::endl;
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
@@ -18,6 +18,8 @@
|
||||
#ifndef SGMatrix_H
|
||||
#define SGMatrix_H
|
||||
|
||||
#include <simgear/math/simd4x4.hxx>
|
||||
|
||||
/// Expression templates for poor programmers ... :)
|
||||
template<typename T>
|
||||
struct TransNegRef;
|
||||
@@ -38,13 +40,13 @@ public:
|
||||
/// uninitialized values in the debug build very fast ...
|
||||
#ifndef NDEBUG
|
||||
for (unsigned i = 0; i < nEnts; ++i)
|
||||
_data.flat[i] = SGLimits<T>::quiet_NaN();
|
||||
_data[i] = SGLimits<T>::quiet_NaN();
|
||||
#endif
|
||||
}
|
||||
/// Constructor. Initialize by the content of a plain array,
|
||||
/// make sure it has at least 16 elements
|
||||
explicit SGMatrix(const T* data)
|
||||
{ for (unsigned i = 0; i < nEnts; ++i) _data.flat[i] = data[i]; }
|
||||
{ _data = simd4x4_t<T,4>(data); }
|
||||
|
||||
/// Constructor, build up a SGMatrix from given elements
|
||||
SGMatrix(T m00, T m01, T m02, T m03,
|
||||
@@ -52,14 +54,8 @@ public:
|
||||
T m20, T m21, T m22, T m23,
|
||||
T m30, T m31, T m32, T m33)
|
||||
{
|
||||
_data.flat[0] = m00; _data.flat[1] = m10;
|
||||
_data.flat[2] = m20; _data.flat[3] = m30;
|
||||
_data.flat[4] = m01; _data.flat[5] = m11;
|
||||
_data.flat[6] = m21; _data.flat[7] = m31;
|
||||
_data.flat[8] = m02; _data.flat[9] = m12;
|
||||
_data.flat[10] = m22; _data.flat[11] = m32;
|
||||
_data.flat[12] = m03; _data.flat[13] = m13;
|
||||
_data.flat[14] = m23; _data.flat[15] = m33;
|
||||
_data = simd4x4_t<T,4>(m00,m01,m02,m03,m10,m11,m12,m13,
|
||||
m20,m21,m22,m23,m30,m31,m32,m33);
|
||||
}
|
||||
|
||||
/// Constructor, build up a SGMatrix from a translation
|
||||
@@ -80,14 +76,8 @@ public:
|
||||
template<typename S>
|
||||
void set(const SGVec3<S>& trans)
|
||||
{
|
||||
_data.flat[0] = 1; _data.flat[4] = 0;
|
||||
_data.flat[8] = 0; _data.flat[12] = T(trans(0));
|
||||
_data.flat[1] = 0; _data.flat[5] = 1;
|
||||
_data.flat[9] = 0; _data.flat[13] = T(trans(1));
|
||||
_data.flat[2] = 0; _data.flat[6] = 0;
|
||||
_data.flat[10] = 1; _data.flat[14] = T(trans(2));
|
||||
_data.flat[3] = 0; _data.flat[7] = 0;
|
||||
_data.flat[11] = 0; _data.flat[15] = 1;
|
||||
simd4x4::unit(_data);
|
||||
simd4x4::translate(_data, trans.simd3());
|
||||
}
|
||||
|
||||
/// Set from a scale/rotation and tranlation
|
||||
@@ -98,82 +88,77 @@ public:
|
||||
T xx = x*x; T yy = y*y; T zz = z*z;
|
||||
T wx = w*x; T wy = w*y; T wz = w*z;
|
||||
T xy = x*y; T xz = x*z; T yz = y*z;
|
||||
_data.flat[0] = 1-2*(yy+zz); _data.flat[1] = 2*(xy-wz);
|
||||
_data.flat[2] = 2*(xz+wy); _data.flat[3] = 0;
|
||||
_data.flat[4] = 2*(xy+wz); _data.flat[5] = 1-2*(xx+zz);
|
||||
_data.flat[6] = 2*(yz-wx); _data.flat[7] = 0;
|
||||
_data.flat[8] = 2*(xz-wy); _data.flat[9] = 2*(yz+wx);
|
||||
_data.flat[10] = 1-2*(xx+yy); _data.flat[11] = 0;
|
||||
_data.flat[12] = 0; _data.flat[13] = 0;
|
||||
_data.flat[14] = 0; _data.flat[15] = 1;
|
||||
_data[0] = 1-2*(yy+zz); _data[1] = 2*(xy-wz);
|
||||
_data[2] = 2*(xz+wy); _data[3] = 0;
|
||||
_data[4] = 2*(xy+wz); _data[5] = 1-2*(xx+zz);
|
||||
_data[6] = 2*(yz-wx); _data[7] = 0;
|
||||
_data[8] = 2*(xz-wy); _data[9] = 2*(yz+wx);
|
||||
_data[10] = 1-2*(xx+yy); _data[11] = 0;
|
||||
_data[12] = 0; _data[13] = 0;
|
||||
_data[14] = 0; _data[15] = 1;
|
||||
}
|
||||
|
||||
/// set from a transposed negated matrix
|
||||
void set(const TransNegRef<T>& tm)
|
||||
{
|
||||
const SGMatrix& m = tm.m;
|
||||
_data.flat[0] = m(0,0);
|
||||
_data.flat[1] = m(0,1);
|
||||
_data.flat[2] = m(0,2);
|
||||
_data.flat[3] = m(3,0);
|
||||
|
||||
_data.flat[4] = m(1,0);
|
||||
_data.flat[5] = m(1,1);
|
||||
_data.flat[6] = m(1,2);
|
||||
_data.flat[7] = m(3,1);
|
||||
|
||||
_data.flat[8] = m(2,0);
|
||||
_data.flat[9] = m(2,1);
|
||||
_data.flat[10] = m(2,2);
|
||||
_data.flat[11] = m(3,2);
|
||||
_data = simd4x4::transpose(m.simd4x4());
|
||||
_data[3] = m(3,0);
|
||||
_data[7] = m(3,1);
|
||||
_data[11] = m(3,2);
|
||||
|
||||
// Well, this one is ugly here, as that xform method on the current
|
||||
// object needs the above data to be already set ...
|
||||
SGVec3<T> t = xformVec(SGVec3<T>(m(0,3), m(1,3), m(2,3)));
|
||||
_data.flat[12] = -t(0);
|
||||
_data.flat[13] = -t(1);
|
||||
_data.flat[14] = -t(2);
|
||||
_data.flat[15] = m(3,3);
|
||||
_data.set(3, -t.simd3());
|
||||
_data[15] = m(3,3);
|
||||
}
|
||||
|
||||
/// Access by index, the index is unchecked
|
||||
const T& operator()(unsigned i, unsigned j) const
|
||||
{ return _data.flat[i + 4*j]; }
|
||||
{ return _data[i + 4*j]; }
|
||||
/// Access by index, the index is unchecked
|
||||
T& operator()(unsigned i, unsigned j)
|
||||
{ return _data.flat[i + 4*j]; }
|
||||
{ return _data[i + 4*j]; }
|
||||
|
||||
/// Access raw data by index, the index is unchecked
|
||||
const T& operator[](unsigned i) const
|
||||
{ return _data.flat[i]; }
|
||||
{ return _data[i]; }
|
||||
/// Access by index, the index is unchecked
|
||||
T& operator[](unsigned i)
|
||||
{ return _data.flat[i]; }
|
||||
{ return _data[i]; }
|
||||
|
||||
/// Get the data pointer
|
||||
const T* data(void) const
|
||||
{ return _data.flat; }
|
||||
{ return _data; }
|
||||
/// Get the data pointer
|
||||
T* data(void)
|
||||
{ return _data.flat; }
|
||||
{ return _data; }
|
||||
|
||||
/// Readonly interface function to ssg's sgMat4/sgdMat4
|
||||
const T (&sg(void) const)[4][4]
|
||||
{ return _data.carray; }
|
||||
{ return _data.ptr(); }
|
||||
/// Interface function to ssg's sgMat4/sgdMat4
|
||||
T (&sg(void))[4][4]
|
||||
{ return _data.carray; }
|
||||
{ return _data.ptr(); }
|
||||
/// Readonly raw storage interface
|
||||
const simd4x4_t<T,4> (&simd4x4(void) const)
|
||||
{ return _data; }
|
||||
/// Readonly raw storage interface
|
||||
simd4x4_t<T,4> (&simd4x4(void))
|
||||
{ return _data; }
|
||||
|
||||
|
||||
/// Inplace addition
|
||||
SGMatrix& operator+=(const SGMatrix& m)
|
||||
{ for (unsigned i = 0; i < nEnts; ++i) _data.flat[i] += m._data.flat[i]; return *this; }
|
||||
{ _data += m.simd4x4(); return *this; }
|
||||
/// Inplace subtraction
|
||||
SGMatrix& operator-=(const SGMatrix& m)
|
||||
{ for (unsigned i = 0; i < nEnts; ++i) _data.flat[i] -= m._data.flat[i]; return *this; }
|
||||
{ _data -= m.simd4x4(); return *this; }
|
||||
/// Inplace scalar multiplication
|
||||
template<typename S>
|
||||
SGMatrix& operator*=(S s)
|
||||
{ for (unsigned i = 0; i < nEnts; ++i) _data.flat[i] *= s; return *this; }
|
||||
{ _data *= s; return *this; }
|
||||
/// Inplace scalar multiplication by 1/s
|
||||
template<typename S>
|
||||
SGMatrix& operator/=(S s)
|
||||
@@ -184,27 +169,13 @@ public:
|
||||
template<typename S>
|
||||
SGMatrix& preMultTranslate(const SGVec3<S>& t)
|
||||
{
|
||||
for (unsigned i = 0; i < 3; ++i) {
|
||||
T tmp = T(t(i));
|
||||
if (tmp == 0)
|
||||
continue;
|
||||
(*this)(i,0) += tmp*(*this)(3,0);
|
||||
(*this)(i,1) += tmp*(*this)(3,1);
|
||||
(*this)(i,2) += tmp*(*this)(3,2);
|
||||
(*this)(i,3) += tmp*(*this)(3,3);
|
||||
}
|
||||
simd4x4::pre_translate(_data,t.simd3());
|
||||
return *this;
|
||||
}
|
||||
template<typename S>
|
||||
SGMatrix& postMultTranslate(const SGVec3<S>& t)
|
||||
{
|
||||
SGVec4<T> col3((*this)(0,3), (*this)(1,3), (*this)(2,3), (*this)(3,3));
|
||||
for (unsigned i = 0; i < SGMatrix<T>::nCols-1; ++i) {
|
||||
SGVec4<T> tmp((*this)(0,i), (*this)(1,i), (*this)(2,i), (*this)(3,i));
|
||||
col3 += T(t(i))*tmp;
|
||||
}
|
||||
(*this)(0,3) = col3(0); (*this)(1,3) = col3(1);
|
||||
(*this)(2,3) = col3(2); (*this)(3,3) = col3(3);
|
||||
simd4x4::post_translate(_data,t.simd3());
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -230,51 +201,27 @@ public:
|
||||
SGVec3<T> xformPt(const SGVec3<T>& pt) const
|
||||
{
|
||||
SGVec3<T> tpt;
|
||||
tpt(0) = (*this)(0,3);
|
||||
tpt(1) = (*this)(1,3);
|
||||
tpt(2) = (*this)(2,3);
|
||||
for (unsigned i = 0; i < SGMatrix<T>::nCols-1; ++i) {
|
||||
T tmp = pt(i);
|
||||
tpt(0) += tmp*(*this)(0,i);
|
||||
tpt(1) += tmp*(*this)(1,i);
|
||||
tpt(2) += tmp*(*this)(2,i);
|
||||
}
|
||||
tpt.simd3() = simd4x4::transform(_data,pt.simd3());
|
||||
return tpt;
|
||||
}
|
||||
SGVec3<T> xformVec(const SGVec3<T>& v) const
|
||||
{
|
||||
SGVec3<T> tv;
|
||||
T tmp = v(0);
|
||||
tv(0) = tmp*(*this)(0,0);
|
||||
tv(1) = tmp*(*this)(1,0);
|
||||
tv(2) = tmp*(*this)(2,0);
|
||||
for (unsigned i = 1; i < SGMatrix<T>::nCols-1; ++i) {
|
||||
T tmp = v(i);
|
||||
tv(0) += tmp*(*this)(0,i);
|
||||
tv(1) += tmp*(*this)(1,i);
|
||||
tv(2) += tmp*(*this)(2,i);
|
||||
}
|
||||
tv.simd3() = _data * v.simd3();
|
||||
return tv;
|
||||
}
|
||||
|
||||
/// Return an all zero matrix
|
||||
static SGMatrix zeros(void)
|
||||
{ return SGMatrix(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); }
|
||||
{ SGMatrix r; simd4x4::zeros(r.simd4x4()); return r; }
|
||||
|
||||
/// Return a unit matrix
|
||||
static SGMatrix unit(void)
|
||||
{ return SGMatrix(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); }
|
||||
{ SGMatrix r; simd4x4::unit(r.simd4x4()); return r; }
|
||||
|
||||
private:
|
||||
/// Required to make that alias safe.
|
||||
union Data {
|
||||
T flat[16];
|
||||
T carray[4][4];
|
||||
};
|
||||
|
||||
/// The actual data, the matrix is stored in column major order,
|
||||
/// that matches the storage format of OpenGL
|
||||
Data _data;
|
||||
simd4x4_t<T,4> _data;
|
||||
};
|
||||
|
||||
/// Class to distinguish between a matrix and the matrix with a transposed
|
||||
@@ -296,61 +243,45 @@ operator+(const SGMatrix<T>& m)
|
||||
template<typename T>
|
||||
inline
|
||||
SGMatrix<T>
|
||||
operator-(const SGMatrix<T>& m)
|
||||
operator-(SGMatrix<T> m)
|
||||
{
|
||||
SGMatrix<T> ret;
|
||||
for (unsigned i = 0; i < SGMatrix<T>::nEnts; ++i)
|
||||
ret[i] = -m[i];
|
||||
return ret;
|
||||
m.simd4x4() = -m.simd4x4();
|
||||
return m;
|
||||
}
|
||||
|
||||
/// Binary +
|
||||
template<typename T>
|
||||
inline
|
||||
SGMatrix<T>
|
||||
operator+(const SGMatrix<T>& m1, const SGMatrix<T>& m2)
|
||||
operator+(SGMatrix<T> m1, const SGMatrix<T>& m2)
|
||||
{
|
||||
SGMatrix<T> ret;
|
||||
for (unsigned i = 0; i < SGMatrix<T>::nEnts; ++i)
|
||||
ret[i] = m1[i] + m2[i];
|
||||
return ret;
|
||||
m1.simd4x4() += m2.simd4x4();
|
||||
return m1;
|
||||
}
|
||||
|
||||
/// Binary -
|
||||
template<typename T>
|
||||
inline
|
||||
SGMatrix<T>
|
||||
operator-(const SGMatrix<T>& m1, const SGMatrix<T>& m2)
|
||||
operator-(SGMatrix<T> m1, const SGMatrix<T>& m2)
|
||||
{
|
||||
SGMatrix<T> ret;
|
||||
for (unsigned i = 0; i < SGMatrix<T>::nEnts; ++i)
|
||||
ret[i] = m1[i] - m2[i];
|
||||
return ret;
|
||||
m1.simd4x4() -= m2.simd4x4();
|
||||
return m1;
|
||||
}
|
||||
|
||||
/// Scalar multiplication
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGMatrix<T>
|
||||
operator*(S s, const SGMatrix<T>& m)
|
||||
{
|
||||
SGMatrix<T> ret;
|
||||
for (unsigned i = 0; i < SGMatrix<T>::nEnts; ++i)
|
||||
ret[i] = s*m[i];
|
||||
return ret;
|
||||
}
|
||||
operator*(S s, SGMatrix<T> m)
|
||||
{ m.simd4x4() *= s; return m; }
|
||||
|
||||
/// Scalar multiplication
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGMatrix<T>
|
||||
operator*(const SGMatrix<T>& m, S s)
|
||||
{
|
||||
SGMatrix<T> ret;
|
||||
for (unsigned i = 0; i < SGMatrix<T>::nEnts; ++i)
|
||||
ret[i] = s*m[i];
|
||||
return ret;
|
||||
}
|
||||
operator*(SGMatrix<T> m, S s)
|
||||
{ m.simd4x4() *= s; return m; }
|
||||
|
||||
/// Vector multiplication
|
||||
template<typename T>
|
||||
@@ -359,18 +290,7 @@ SGVec4<T>
|
||||
operator*(const SGMatrix<T>& m, const SGVec4<T>& v)
|
||||
{
|
||||
SGVec4<T> mv;
|
||||
T tmp = v(0);
|
||||
mv(0) = tmp*m(0,0);
|
||||
mv(1) = tmp*m(1,0);
|
||||
mv(2) = tmp*m(2,0);
|
||||
mv(3) = tmp*m(3,0);
|
||||
for (unsigned i = 1; i < SGMatrix<T>::nCols; ++i) {
|
||||
T tmp = v(i);
|
||||
mv(0) += tmp*m(0,i);
|
||||
mv(1) += tmp*m(1,i);
|
||||
mv(2) += tmp*m(2,i);
|
||||
mv(3) += tmp*m(3,i);
|
||||
}
|
||||
mv.simd4() = m.simd4x4() * v.simd4();
|
||||
return mv;
|
||||
}
|
||||
|
||||
@@ -405,20 +325,7 @@ SGMatrix<T>
|
||||
operator*(const SGMatrix<T>& m1, const SGMatrix<T>& m2)
|
||||
{
|
||||
SGMatrix<T> m;
|
||||
for (unsigned j = 0; j < SGMatrix<T>::nCols; ++j) {
|
||||
T tmp = m2(0,j);
|
||||
m(0,j) = tmp*m1(0,0);
|
||||
m(1,j) = tmp*m1(1,0);
|
||||
m(2,j) = tmp*m1(2,0);
|
||||
m(3,j) = tmp*m1(3,0);
|
||||
for (unsigned i = 1; i < SGMatrix<T>::nCols; ++i) {
|
||||
T tmp = m2(i,j);
|
||||
m(0,j) += tmp*m1(0,i);
|
||||
m(1,j) += tmp*m1(1,i);
|
||||
m(2,j) += tmp*m1(2,i);
|
||||
m(3,j) += tmp*m1(3,i);
|
||||
}
|
||||
}
|
||||
m.simd4x4() = m1.simd4x4() * m2.simd4x4();
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
#include <simgear/math/SGLimits.hxx>
|
||||
#include <simgear/math/SGMisc.hxx>
|
||||
#include <simgear/math/SGMathFwd.hxx>
|
||||
#include <simgear/math/simd.hxx>
|
||||
|
||||
/// 2D Vector Class
|
||||
template<typename T>
|
||||
@@ -44,11 +45,11 @@ public:
|
||||
}
|
||||
/// Constructor. Initialize by the given values
|
||||
SGVec2(T x, T y)
|
||||
{ data()[0] = x; data()[1] = y; }
|
||||
{ _data = simd4_t<T,2>(x, y); }
|
||||
/// Constructor. Initialize by the content of a plain array,
|
||||
/// make sure it has at least 2 elements
|
||||
explicit SGVec2(const T* d)
|
||||
{ data()[0] = d[0]; data()[1] = d[1]; }
|
||||
{ _data = d ? simd4_t<T,2>(d) : simd4_t<T,2>(T(0)); }
|
||||
template<typename S>
|
||||
explicit SGVec2(const SGVec2<S>& d)
|
||||
{ data()[0] = d[0]; data()[1] = d[1]; }
|
||||
@@ -82,25 +83,30 @@ public:
|
||||
|
||||
/// Access raw data
|
||||
const T (&data(void) const)[2]
|
||||
{ return _data; }
|
||||
{ return _data.ptr(); }
|
||||
/// Access raw data
|
||||
T (&data(void))[2]
|
||||
{ return _data.ptr(); }
|
||||
const simd4_t<T,2> (&simd2(void) const)
|
||||
{ return _data; }
|
||||
/// Readonly raw storage interface
|
||||
simd4_t<T,2> (&simd2(void))
|
||||
{ return _data; }
|
||||
|
||||
/// Inplace addition
|
||||
SGVec2& operator+=(const SGVec2& v)
|
||||
{ data()[0] += v(0); data()[1] += v(1); return *this; }
|
||||
{ _data += v.simd2(); return *this; }
|
||||
/// Inplace subtraction
|
||||
SGVec2& operator-=(const SGVec2& v)
|
||||
{ data()[0] -= v(0); data()[1] -= v(1); return *this; }
|
||||
{ _data -= v.simd2(); return *this; }
|
||||
/// Inplace scalar multiplication
|
||||
template<typename S>
|
||||
SGVec2& operator*=(S s)
|
||||
{ data()[0] *= s; data()[1] *= s; return *this; }
|
||||
{ _data *= s; return *this; }
|
||||
/// Inplace scalar multiplication by 1/s
|
||||
template<typename S>
|
||||
SGVec2& operator/=(S s)
|
||||
{ return operator*=(1/T(s)); }
|
||||
{ _data*=(1/T(s)); return *this; }
|
||||
|
||||
/// Return an all zero vector
|
||||
static SGVec2 zeros(void)
|
||||
@@ -112,7 +118,7 @@ public:
|
||||
{ return SGVec2(0, 1); }
|
||||
|
||||
private:
|
||||
T _data[2];
|
||||
simd4_t<T,2> _data;
|
||||
};
|
||||
|
||||
/// Unary +, do nothing ...
|
||||
@@ -126,36 +132,36 @@ operator+(const SGVec2<T>& v)
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec2<T>
|
||||
operator-(const SGVec2<T>& v)
|
||||
{ return SGVec2<T>(-v(0), -v(1)); }
|
||||
operator-(SGVec2<T> v)
|
||||
{ v *= -1; return v; }
|
||||
|
||||
/// Binary +
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec2<T>
|
||||
operator+(const SGVec2<T>& v1, const SGVec2<T>& v2)
|
||||
{ return SGVec2<T>(v1(0)+v2(0), v1(1)+v2(1)); }
|
||||
operator+(SGVec2<T> v1, const SGVec2<T>& v2)
|
||||
{ v1.simd2() += v2.simd2(); return v1; }
|
||||
|
||||
/// Binary -
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec2<T>
|
||||
operator-(const SGVec2<T>& v1, const SGVec2<T>& v2)
|
||||
{ return SGVec2<T>(v1(0)-v2(0), v1(1)-v2(1)); }
|
||||
operator-(SGVec2<T> v1, const SGVec2<T>& v2)
|
||||
{ v1.simd2() -= v2.simd2(); return v1; }
|
||||
|
||||
/// Scalar multiplication
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec2<T>
|
||||
operator*(S s, const SGVec2<T>& v)
|
||||
{ return SGVec2<T>(s*v(0), s*v(1)); }
|
||||
operator*(S s, SGVec2<T> v)
|
||||
{ v.simd2() *= s; return v; }
|
||||
|
||||
/// Scalar multiplication
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec2<T>
|
||||
operator*(const SGVec2<T>& v, S s)
|
||||
{ return SGVec2<T>(s*v(0), s*v(1)); }
|
||||
operator*(SGVec2<T> v, S s)
|
||||
{ v.simd2() *= s; return v; }
|
||||
|
||||
/// multiplication as a multiplicator, that is assume that the first vector
|
||||
/// represents a 2x2 diagonal matrix with the diagonal elements in the vector.
|
||||
@@ -163,42 +169,42 @@ operator*(const SGVec2<T>& v, S s)
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec2<T>
|
||||
mult(const SGVec2<T>& v1, const SGVec2<T>& v2)
|
||||
{ return SGVec2<T>(v1(0)*v2(0), v1(1)*v2(1)); }
|
||||
mult(SGVec2<T> v1, const SGVec2<T>& v2)
|
||||
{ v1.simd2() *= v2.simd2(); return v1; }
|
||||
|
||||
/// component wise min
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec2<T>
|
||||
min(const SGVec2<T>& v1, const SGVec2<T>& v2)
|
||||
{return SGVec2<T>(SGMisc<T>::min(v1(0), v2(0)), SGMisc<T>::min(v1(1), v2(1)));}
|
||||
min(SGVec2<T> v1, const SGVec2<T>& v2)
|
||||
{ v1.simd2() = simd4::min(v1.simd2(), v2.simd2()); return v1; }
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec2<T>
|
||||
min(const SGVec2<T>& v, S s)
|
||||
{ return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
|
||||
min(SGVec2<T> v, S s)
|
||||
{ v.simd2() = simd4::min(v.simd2(), simd4_t<T,2>(s)); return v; }
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec2<T>
|
||||
min(S s, const SGVec2<T>& v)
|
||||
{ return SGVec2<T>(SGMisc<T>::min(s, v(0)), SGMisc<T>::min(s, v(1))); }
|
||||
min(S s, SGVec2<T> v)
|
||||
{ v.sim2() = simd4::min(v.simd2(), simd4_t<T,2>(s)); return v; }
|
||||
|
||||
/// component wise max
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec2<T>
|
||||
max(const SGVec2<T>& v1, const SGVec2<T>& v2)
|
||||
{return SGVec2<T>(SGMisc<T>::max(v1(0), v2(0)), SGMisc<T>::max(v1(1), v2(1)));}
|
||||
{ v1 = simd4::max(v1.simd2(), v2.simd2()); return v1; }
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec2<T>
|
||||
max(const SGVec2<T>& v, S s)
|
||||
{ return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
|
||||
{ v = simd4::max(v.simd2(), simd4_t<T,2>(s)); return v; }
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec2<T>
|
||||
max(S s, const SGVec2<T>& v)
|
||||
{ return SGVec2<T>(SGMisc<T>::max(s, v(0)), SGMisc<T>::max(s, v(1))); }
|
||||
{ v = simd4::max(v.simd2(), simd4_t<T,2>(s)); return v; }
|
||||
|
||||
/// Add two vectors taking care of (integer) overflows. The values are limited
|
||||
/// to the respective minimum and maximum values.
|
||||
@@ -216,36 +222,39 @@ template<typename T>
|
||||
inline
|
||||
T
|
||||
dot(const SGVec2<T>& v1, const SGVec2<T>& v2)
|
||||
{ return v1(0)*v2(0) + v1(1)*v2(1); }
|
||||
{ return simd4::dot(v1.simd2(), v2.simd2()); }
|
||||
|
||||
/// The euclidean norm of the vector, that is what most people call length
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
norm(const SGVec2<T>& v)
|
||||
{ return sqrt(dot(v, v)); }
|
||||
{ return simd4::magnitude(v.simd2()); }
|
||||
|
||||
/// The euclidean norm of the vector, that is what most people call length
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
length(const SGVec2<T>& v)
|
||||
{ return sqrt(dot(v, v)); }
|
||||
{ return simd4::magnitude(v.simd2()); }
|
||||
|
||||
/// The 1-norm of the vector, this one is the fastest length function we
|
||||
/// can implement on modern cpu's
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
norm1(const SGVec2<T>& v)
|
||||
{ return fabs(v(0)) + fabs(v(1)); }
|
||||
norm1(SGVec2<T> v)
|
||||
{ v.simd2() = simd4::abs(v.simd2()); return (v(0)+v(1)); }
|
||||
|
||||
/// The inf-norm of the vector
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
normI(const SGVec2<T>& v)
|
||||
{ return SGMisc<T>::max(fabs(v(0)), fabs(v(1))); }
|
||||
normI(SGVec2<T> v)
|
||||
{
|
||||
v.simd2() = simd4::abs(v.simd2());
|
||||
return SGMisc<T>::max(v(0), v(1));
|
||||
}
|
||||
|
||||
/// The euclidean norm of the vector, that is what most people call length
|
||||
template<typename T>
|
||||
@@ -335,14 +344,14 @@ template<typename T>
|
||||
inline
|
||||
T
|
||||
dist(const SGVec2<T>& v1, const SGVec2<T>& v2)
|
||||
{ return norm(v1 - v2); }
|
||||
{ return simd4::magnitude(v1.simd2() - v2.simd2()); }
|
||||
|
||||
/// The squared euclidean distance of the two vectors
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
distSqr(const SGVec2<T>& v1, const SGVec2<T>& v2)
|
||||
{ SGVec2<T> tmp = v1 - v2; return dot(tmp, tmp); }
|
||||
distSqr(SGVec2<T> v1, const SGVec2<T>& v2)
|
||||
{ return simd4::magnitude2(v1.simd2() - v2.simd2()); }
|
||||
|
||||
// calculate the projection of u along the direction of d.
|
||||
template<typename T>
|
||||
@@ -350,12 +359,22 @@ inline
|
||||
SGVec2<T>
|
||||
projection(const SGVec2<T>& u, const SGVec2<T>& d)
|
||||
{
|
||||
T denom = dot(d, d);
|
||||
T denom = simd4::magnitude2(d.simd2());
|
||||
T ud = dot(u, d);
|
||||
if (SGLimits<T>::min() < denom) return u;
|
||||
else return d * (dot(u, d) / denom);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec2<T>
|
||||
interpolate(T tau, const SGVec2<T>& v1, const SGVec2<T>& v2)
|
||||
{
|
||||
SGVec2<T> r;
|
||||
r.simd2() = simd4::interpolate(tau, v1.simd2(), v2.simd2());
|
||||
return r;
|
||||
}
|
||||
|
||||
#ifndef NDEBUG
|
||||
template<typename T>
|
||||
inline
|
||||
@@ -376,11 +395,11 @@ operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec2<T>& v)
|
||||
inline
|
||||
SGVec2f
|
||||
toVec2f(const SGVec2d& v)
|
||||
{ return SGVec2f((float)v(0), (float)v(1)); }
|
||||
{ SGVec2f f(v); return f; }
|
||||
|
||||
inline
|
||||
SGVec2d
|
||||
toVec2d(const SGVec2f& v)
|
||||
{ return SGVec2d(v(0), v(1)); }
|
||||
{ SGVec2d d(v); return d; }
|
||||
|
||||
#endif
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
#include <simgear/math/SGVec2.hxx>
|
||||
#include <simgear/math/SGGeodesy.hxx>
|
||||
#include <simgear/math/simd.hxx>
|
||||
|
||||
/// 3D Vector Class
|
||||
template<typename T>
|
||||
@@ -54,16 +55,16 @@ public:
|
||||
|
||||
/// Constructor. Initialize by the given values
|
||||
SGVec3(T x, T y, T z)
|
||||
{ data()[0] = x; data()[1] = y; data()[2] = z; }
|
||||
{ _data = simd4_t<T,3>(x, y, z); }
|
||||
/// Constructor. Initialize by the content of a plain array,
|
||||
/// make sure it has at least 3 elements
|
||||
explicit SGVec3(const T* d)
|
||||
{ data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
|
||||
{ _data = d ? simd4_t<T,3>(d) : simd4_t<T,3>(T(0)); }
|
||||
template<typename S>
|
||||
explicit SGVec3(const SGVec3<S>& d)
|
||||
{ data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; }
|
||||
explicit SGVec3(const SGVec2<T>& v2, const T& v3 = 0)
|
||||
{ data()[0] = v2[0]; data()[1] = v2[1]; data()[2] = v3; }
|
||||
{ _data = v2.simd2(); data()[2] = v3; }
|
||||
|
||||
/// Access by index, the index is unchecked
|
||||
const T& operator()(unsigned i) const
|
||||
@@ -100,25 +101,31 @@ public:
|
||||
|
||||
/// Readonly raw storage interface
|
||||
const T (&data(void) const)[3]
|
||||
{ return _data; }
|
||||
{ return _data.ptr(); }
|
||||
/// Readonly raw storage interface
|
||||
T (&data(void))[3]
|
||||
{ return _data.ptr(); }
|
||||
/// Readonly raw storage interface
|
||||
const simd4_t<T,3> (&simd3(void) const)
|
||||
{ return _data; }
|
||||
/// Readonly raw storage interface
|
||||
simd4_t<T,3> (&simd3(void))
|
||||
{ return _data; }
|
||||
|
||||
/// Inplace addition
|
||||
SGVec3& operator+=(const SGVec3& v)
|
||||
{ data()[0] += v(0); data()[1] += v(1); data()[2] += v(2); return *this; }
|
||||
{ _data += v.simd3(); return *this; }
|
||||
/// Inplace subtraction
|
||||
SGVec3& operator-=(const SGVec3& v)
|
||||
{ data()[0] -= v(0); data()[1] -= v(1); data()[2] -= v(2); return *this; }
|
||||
{ _data -= v.simd3(); return *this; }
|
||||
/// Inplace scalar multiplication
|
||||
template<typename S>
|
||||
SGVec3& operator*=(S s)
|
||||
{ data()[0] *= s; data()[1] *= s; data()[2] *= s; return *this; }
|
||||
{ _data *= s; return *this; }
|
||||
/// Inplace scalar multiplication by 1/s
|
||||
template<typename S>
|
||||
SGVec3& operator/=(S s)
|
||||
{ return operator*=(1/T(s)); }
|
||||
{ _data*=(1/T(s)); return *this; }
|
||||
|
||||
/// Return an all zero vector
|
||||
static SGVec3 zeros(void)
|
||||
@@ -139,7 +146,8 @@ public:
|
||||
static SGVec3 fromGeoc(const SGGeoc& geoc);
|
||||
|
||||
private:
|
||||
T _data[3];
|
||||
simd4_t<T,3> _data;
|
||||
|
||||
};
|
||||
|
||||
template<>
|
||||
@@ -193,36 +201,36 @@ operator+(const SGVec3<T>& v)
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
operator-(const SGVec3<T>& v)
|
||||
{ return SGVec3<T>(-v(0), -v(1), -v(2)); }
|
||||
operator-(SGVec3<T> v)
|
||||
{ v *= -1; return v; }
|
||||
|
||||
/// Binary +
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
operator+(const SGVec3<T>& v1, const SGVec3<T>& v2)
|
||||
{ return SGVec3<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2)); }
|
||||
operator+(SGVec3<T> v1, const SGVec3<T>& v2)
|
||||
{ v1.simd3() += v2.simd3(); return v1; }
|
||||
|
||||
/// Binary -
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
operator-(const SGVec3<T>& v1, const SGVec3<T>& v2)
|
||||
{ return SGVec3<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2)); }
|
||||
operator-(SGVec3<T> v1, const SGVec3<T>& v2)
|
||||
{ v1.simd3() -= v2.simd3(); return v1; }
|
||||
|
||||
/// Scalar multiplication
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
operator*(S s, const SGVec3<T>& v)
|
||||
{ return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
|
||||
operator*(S s, SGVec3<T> v)
|
||||
{ v.simd3() *= s; return v; }
|
||||
|
||||
/// Scalar multiplication
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
operator*(const SGVec3<T>& v, S s)
|
||||
{ return SGVec3<T>(s*v(0), s*v(1), s*v(2)); }
|
||||
operator*(SGVec3<T> v, S s)
|
||||
{ v.simd3() *= s; return v; }
|
||||
|
||||
/// multiplication as a multiplicator, that is assume that the first vector
|
||||
/// represents a 3x3 diagonal matrix with the diagonal elements in the vector.
|
||||
@@ -230,66 +238,42 @@ operator*(const SGVec3<T>& v, S s)
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
mult(const SGVec3<T>& v1, const SGVec3<T>& v2)
|
||||
{ return SGVec3<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2)); }
|
||||
mult(SGVec3<T> v1, const SGVec3<T>& v2)
|
||||
{ v1.simd3() *= v2.simd3(); return v1; }
|
||||
|
||||
/// component wise min
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
min(const SGVec3<T>& v1, const SGVec3<T>& v2)
|
||||
{
|
||||
return SGVec3<T>(SGMisc<T>::min(v1(0), v2(0)),
|
||||
SGMisc<T>::min(v1(1), v2(1)),
|
||||
SGMisc<T>::min(v1(2), v2(2)));
|
||||
}
|
||||
min(SGVec3<T> v1, const SGVec3<T>& v2)
|
||||
{ v1.simd3() = simd4::min(v1.simd3(), v2.simd3()); return v1; }
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
min(const SGVec3<T>& v, S s)
|
||||
{
|
||||
return SGVec3<T>(SGMisc<T>::min(s, v(0)),
|
||||
SGMisc<T>::min(s, v(1)),
|
||||
SGMisc<T>::min(s, v(2)));
|
||||
}
|
||||
min(SGVec3<T> v, S s)
|
||||
{ v.simd3() = simd4::min(v.simd3(), simd4_t<T,3>(s)); return v; }
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
min(S s, const SGVec3<T>& v)
|
||||
{
|
||||
return SGVec3<T>(SGMisc<T>::min(s, v(0)),
|
||||
SGMisc<T>::min(s, v(1)),
|
||||
SGMisc<T>::min(s, v(2)));
|
||||
}
|
||||
min(S s, SGVec3<T> v)
|
||||
{ v.simd3() = simd4::min(v.simd3(), simd4_t<T,3>(s)); return v; }
|
||||
|
||||
/// component wise max
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
max(const SGVec3<T>& v1, const SGVec3<T>& v2)
|
||||
{
|
||||
return SGVec3<T>(SGMisc<T>::max(v1(0), v2(0)),
|
||||
SGMisc<T>::max(v1(1), v2(1)),
|
||||
SGMisc<T>::max(v1(2), v2(2)));
|
||||
}
|
||||
max(SGVec3<T> v1, const SGVec3<T>& v2)
|
||||
{ v1.simd3() = simd4::max(v1.simd3(), v2.simd3()); return v1; }
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
max(const SGVec3<T>& v, S s)
|
||||
{
|
||||
return SGVec3<T>(SGMisc<T>::max(s, v(0)),
|
||||
SGMisc<T>::max(s, v(1)),
|
||||
SGMisc<T>::max(s, v(2)));
|
||||
}
|
||||
max(SGVec3<T> v, S s)
|
||||
{ v.simd3() = simd4::max(v.simd3(), simd4_t<T,3>(s)); return v; }
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
max(S s, const SGVec3<T>& v)
|
||||
{
|
||||
return SGVec3<T>(SGMisc<T>::max(s, v(0)),
|
||||
SGMisc<T>::max(s, v(1)),
|
||||
SGMisc<T>::max(s, v(2)));
|
||||
}
|
||||
max(S s, SGVec3<T> v)
|
||||
{ v.simd3() = simd4::max(v.simd3(), simd4_t<T,3>(s)); return v; }
|
||||
|
||||
/// Add two vectors taking care of (integer) overflows. The values are limited
|
||||
/// to the respective minimum and maximum values.
|
||||
@@ -308,47 +292,46 @@ template<typename T>
|
||||
inline
|
||||
T
|
||||
dot(const SGVec3<T>& v1, const SGVec3<T>& v2)
|
||||
{ return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2); }
|
||||
{ return simd4::dot(v1.simd3(), v2.simd3()); }
|
||||
|
||||
/// The euclidean norm of the vector, that is what most people call length
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
norm(const SGVec3<T>& v)
|
||||
{ return sqrt(dot(v, v)); }
|
||||
{ return simd4::magnitude(v.simd3()); }
|
||||
|
||||
/// The euclidean norm of the vector, that is what most people call length
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
length(const SGVec3<T>& v)
|
||||
{ return sqrt(dot(v, v)); }
|
||||
{ return simd4::magnitude(v.simd3()); }
|
||||
|
||||
/// The 1-norm of the vector, this one is the fastest length function we
|
||||
/// can implement on modern cpu's
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
norm1(const SGVec3<T>& v)
|
||||
{ return fabs(v(0)) + fabs(v(1)) + fabs(v(2)); }
|
||||
norm1(SGVec3<T> v)
|
||||
{ v.simd3() = simd4::abs(v.simd3()); return (v(0)+v(1)+v(2)); }
|
||||
|
||||
/// The inf-norm of the vector
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
normI(const SGVec3<T>& v)
|
||||
{ return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2))); }
|
||||
normI(SGVec3<T> v)
|
||||
{
|
||||
v.simd3() = simd4::abs(v.simd3());
|
||||
return SGMisc<T>::max(v(0), v(1), v(2));
|
||||
}
|
||||
|
||||
/// Vector cross product
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
cross(const SGVec3<T>& v1, const SGVec3<T>& v2)
|
||||
{
|
||||
return SGVec3<T>(v1(1)*v2(2) - v1(2)*v2(1),
|
||||
v1(2)*v2(0) - v1(0)*v2(2),
|
||||
v1(0)*v2(1) - v1(1)*v2(0));
|
||||
}
|
||||
cross(SGVec3<T> v1, const SGVec3<T>& v2)
|
||||
{ v1.simd3() = simd4::cross(v1.simd3(), v2.simd3()); return v1; }
|
||||
|
||||
/// return any normalized vector perpendicular to v
|
||||
template<typename T>
|
||||
@@ -468,14 +451,14 @@ template<typename T>
|
||||
inline
|
||||
T
|
||||
dist(const SGVec3<T>& v1, const SGVec3<T>& v2)
|
||||
{ return norm(v1 - v2); }
|
||||
{ return simd4::magnitude(v1.simd3() - v2.simd3()); }
|
||||
|
||||
/// The squared euclidean distance of the two vectors
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
distSqr(const SGVec3<T>& v1, const SGVec3<T>& v2)
|
||||
{ SGVec3<T> tmp = v1 - v2; return dot(tmp, tmp); }
|
||||
distSqr(SGVec3<T> v1, const SGVec3<T>& v2)
|
||||
{ return simd4::magnitude2(v1.simd3() - v2.simd3()); }
|
||||
|
||||
// calculate the projection of u along the direction of d.
|
||||
template<typename T>
|
||||
@@ -483,12 +466,22 @@ inline
|
||||
SGVec3<T>
|
||||
projection(const SGVec3<T>& u, const SGVec3<T>& d)
|
||||
{
|
||||
T denom = dot(d, d);
|
||||
T denom = simd4::magnitude2(d.simd3());
|
||||
T ud = dot(u, d);
|
||||
if (SGLimits<T>::min() < denom) return u;
|
||||
else return d * (dot(u, d) / denom);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec3<T>
|
||||
interpolate(T tau, const SGVec3<T>& v1, const SGVec3<T>& v2)
|
||||
{
|
||||
SGVec3<T> r;
|
||||
r.simd3() = simd4::interpolate(tau, v1.simd3(), v2.simd3());
|
||||
return r;
|
||||
}
|
||||
|
||||
#ifndef NDEBUG
|
||||
template<typename T>
|
||||
inline
|
||||
@@ -510,11 +503,11 @@ operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec3<T>& v)
|
||||
inline
|
||||
SGVec3f
|
||||
toVec3f(const SGVec3d& v)
|
||||
{ return SGVec3f((float)v(0), (float)v(1), (float)v(2)); }
|
||||
{ SGVec3f f(v); return f; }
|
||||
|
||||
inline
|
||||
SGVec3d
|
||||
toVec3d(const SGVec3f& v)
|
||||
{ return SGVec3d(v(0), v(1), v(2)); }
|
||||
{ SGVec3d d(v); return d; }
|
||||
|
||||
#endif
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
|
||||
#include <iosfwd>
|
||||
|
||||
#include <simgear/math/simd.hxx>
|
||||
|
||||
/// 4D Vector Class
|
||||
template<typename T>
|
||||
class SGVec4 {
|
||||
@@ -40,16 +42,16 @@ public:
|
||||
}
|
||||
/// Constructor. Initialize by the given values
|
||||
SGVec4(T x, T y, T z, T w)
|
||||
{ data()[0] = x; data()[1] = y; data()[2] = z; data()[3] = w; }
|
||||
{ _data = simd4_t<T,4>(x, y, z, w); }
|
||||
/// Constructor. Initialize by the content of a plain array,
|
||||
/// make sure it has at least 3 elements
|
||||
explicit SGVec4(const T* d)
|
||||
{ data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
|
||||
{ _data = d ? simd4_t<T,4>(d) : simd4_t<T,4>(T(0)); }
|
||||
template<typename S>
|
||||
explicit SGVec4(const SGVec4<S>& d)
|
||||
{ data()[0] = d[0]; data()[1] = d[1]; data()[2] = d[2]; data()[3] = d[3]; }
|
||||
explicit SGVec4(const SGVec3<T>& v3, const T& v4 = 0)
|
||||
{ data()[0] = v3[0]; data()[1] = v3[1]; data()[2] = v3[2]; data()[3] = v4; }
|
||||
{ _data = v3.simd3(); data()[3] = v4; }
|
||||
|
||||
/// Access by index, the index is unchecked
|
||||
const T& operator()(unsigned i) const
|
||||
@@ -92,25 +94,31 @@ public:
|
||||
|
||||
/// Readonly raw storage interface
|
||||
const T (&data(void) const)[4]
|
||||
{ return _data; }
|
||||
{ return _data.ptr(); }
|
||||
/// Readonly raw storage interface
|
||||
T (&data(void))[4]
|
||||
{ return _data.ptr(); }
|
||||
/// Readonly raw storage interface
|
||||
const simd4_t<T,4> (&simd4(void) const)
|
||||
{ return _data; }
|
||||
/// Readonly raw storage interface
|
||||
simd4_t<T,4> (&simd4(void))
|
||||
{ return _data; }
|
||||
|
||||
/// Inplace addition
|
||||
SGVec4& operator+=(const SGVec4& v)
|
||||
{ data()[0]+=v(0);data()[1]+=v(1);data()[2]+=v(2);data()[3]+=v(3);return *this; }
|
||||
{ _data += v.simd4(); return *this; }
|
||||
/// Inplace subtraction
|
||||
SGVec4& operator-=(const SGVec4& v)
|
||||
{ data()[0]-=v(0);data()[1]-=v(1);data()[2]-=v(2);data()[3]-=v(3);return *this; }
|
||||
{ _data -= v.simd4(); return *this; }
|
||||
/// Inplace scalar multiplication
|
||||
template<typename S>
|
||||
SGVec4& operator*=(S s)
|
||||
{ data()[0] *= s; data()[1] *= s; data()[2] *= s; data()[3] *= s; return *this; }
|
||||
{ _data *= s; return *this; }
|
||||
/// Inplace scalar multiplication by 1/s
|
||||
template<typename S>
|
||||
SGVec4& operator/=(S s)
|
||||
{ return operator*=(1/T(s)); }
|
||||
{ _data*=(1/T(s)); return *this; }
|
||||
|
||||
/// Return an all zero vector
|
||||
static SGVec4 zeros(void)
|
||||
@@ -126,7 +134,7 @@ public:
|
||||
{ return SGVec4(0, 0, 0, 1); }
|
||||
|
||||
private:
|
||||
T _data[4];
|
||||
simd4_t<T,4> _data;
|
||||
};
|
||||
|
||||
/// Unary +, do nothing ...
|
||||
@@ -140,36 +148,36 @@ operator+(const SGVec4<T>& v)
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec4<T>
|
||||
operator-(const SGVec4<T>& v)
|
||||
{ return SGVec4<T>(-v(0), -v(1), -v(2), -v(3)); }
|
||||
operator-(SGVec4<T> v)
|
||||
{ v *= -1; return v; }
|
||||
|
||||
/// Binary +
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec4<T>
|
||||
operator+(const SGVec4<T>& v1, const SGVec4<T>& v2)
|
||||
{ return SGVec4<T>(v1(0)+v2(0), v1(1)+v2(1), v1(2)+v2(2), v1(3)+v2(3)); }
|
||||
operator+(SGVec4<T> v1, const SGVec4<T>& v2)
|
||||
{ v1.simd4() += v2.simd4(); return v1; }
|
||||
|
||||
/// Binary -
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec4<T>
|
||||
operator-(const SGVec4<T>& v1, const SGVec4<T>& v2)
|
||||
{ return SGVec4<T>(v1(0)-v2(0), v1(1)-v2(1), v1(2)-v2(2), v1(3)-v2(3)); }
|
||||
operator-(SGVec4<T> v1, const SGVec4<T>& v2)
|
||||
{ v1.simd4() -= v2.simd4(); return v1; }
|
||||
|
||||
/// Scalar multiplication
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec4<T>
|
||||
operator*(S s, const SGVec4<T>& v)
|
||||
{ return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
|
||||
operator*(S s, SGVec4<T> v)
|
||||
{ v.simd4() *= s; return v; }
|
||||
|
||||
/// Scalar multiplication
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec4<T>
|
||||
operator*(const SGVec4<T>& v, S s)
|
||||
{ return SGVec4<T>(s*v(0), s*v(1), s*v(2), s*v(3)); }
|
||||
operator*(SGVec4<T> v, S s)
|
||||
{ v.simd4() *= s; return v; }
|
||||
|
||||
/// multiplication as a multiplicator, that is assume that the first vector
|
||||
/// represents a 4x4 diagonal matrix with the diagonal elements in the vector.
|
||||
@@ -177,72 +185,42 @@ operator*(const SGVec4<T>& v, S s)
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec4<T>
|
||||
mult(const SGVec4<T>& v1, const SGVec4<T>& v2)
|
||||
{ return SGVec4<T>(v1(0)*v2(0), v1(1)*v2(1), v1(2)*v2(2), v1(3)*v2(3)); }
|
||||
mult(SGVec4<T> v1, const SGVec4<T>& v2)
|
||||
{ v1.simd4() *= v2.simd4(); return v1; }
|
||||
|
||||
/// component wise min
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec4<T>
|
||||
min(const SGVec4<T>& v1, const SGVec4<T>& v2)
|
||||
{
|
||||
return SGVec4<T>(SGMisc<T>::min(v1(0), v2(0)),
|
||||
SGMisc<T>::min(v1(1), v2(1)),
|
||||
SGMisc<T>::min(v1(2), v2(2)),
|
||||
SGMisc<T>::min(v1(3), v2(3)));
|
||||
}
|
||||
min(SGVec4<T> v1, const SGVec4<T>& v2)
|
||||
{ v1.simd4() = simd4::min(v1.simd4(), v2.simd4()); return v1; }
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec4<T>
|
||||
min(const SGVec4<T>& v, S s)
|
||||
{
|
||||
return SGVec4<T>(SGMisc<T>::min(s, v(0)),
|
||||
SGMisc<T>::min(s, v(1)),
|
||||
SGMisc<T>::min(s, v(2)),
|
||||
SGMisc<T>::min(s, v(3)));
|
||||
}
|
||||
min(SGVec4<T> v, S s)
|
||||
{ v.simd4() = simd4::min(v.simd4(), simd4_t<T,4>(s)); return v; }
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec4<T>
|
||||
min(S s, const SGVec4<T>& v)
|
||||
{
|
||||
return SGVec4<T>(SGMisc<T>::min(s, v(0)),
|
||||
SGMisc<T>::min(s, v(1)),
|
||||
SGMisc<T>::min(s, v(2)),
|
||||
SGMisc<T>::min(s, v(3)));
|
||||
}
|
||||
min(S s, SGVec4<T> v)
|
||||
{ v.simd4() = simd4::min(v.simd4(), simd4_t<T,4>(s)); return v; }
|
||||
|
||||
/// component wise max
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec4<T>
|
||||
max(const SGVec4<T>& v1, const SGVec4<T>& v2)
|
||||
{
|
||||
return SGVec4<T>(SGMisc<T>::max(v1(0), v2(0)),
|
||||
SGMisc<T>::max(v1(1), v2(1)),
|
||||
SGMisc<T>::max(v1(2), v2(2)),
|
||||
SGMisc<T>::max(v1(3), v2(3)));
|
||||
}
|
||||
max(SGVec4<T> v1, const SGVec4<T>& v2)
|
||||
{ v1.simd4() = simd4::max(v1.simd4(), v2.simd4()); return v1; }
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec4<T>
|
||||
max(const SGVec4<T>& v, S s)
|
||||
{
|
||||
return SGVec4<T>(SGMisc<T>::max(s, v(0)),
|
||||
SGMisc<T>::max(s, v(1)),
|
||||
SGMisc<T>::max(s, v(2)),
|
||||
SGMisc<T>::max(s, v(3)));
|
||||
}
|
||||
max(SGVec4<T> v, S s)
|
||||
{ v.simd4() = simd4::max(v.simd4(), simd4_t<T,4>(s)); return v; }
|
||||
template<typename S, typename T>
|
||||
inline
|
||||
SGVec4<T>
|
||||
max(S s, const SGVec4<T>& v)
|
||||
{
|
||||
return SGVec4<T>(SGMisc<T>::max(s, v(0)),
|
||||
SGMisc<T>::max(s, v(1)),
|
||||
SGMisc<T>::max(s, v(2)),
|
||||
SGMisc<T>::max(s, v(3)));
|
||||
}
|
||||
max(S s, SGVec4<T> v)
|
||||
{ v.simd4() = simd4::max(v.simd4(), simd4_t<T,4>(s)); return v; }
|
||||
|
||||
/// Add two vectors taking care of (integer) overflows. The values are limited
|
||||
/// to the respective minimum and maximum values.
|
||||
@@ -262,36 +240,39 @@ template<typename T>
|
||||
inline
|
||||
T
|
||||
dot(const SGVec4<T>& v1, const SGVec4<T>& v2)
|
||||
{ return v1(0)*v2(0) + v1(1)*v2(1) + v1(2)*v2(2) + v1(3)*v2(3); }
|
||||
{ return simd4::dot(v1.simd4(), v2.simd4()); }
|
||||
|
||||
/// The euclidean norm of the vector, that is what most people call length
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
norm(const SGVec4<T>& v)
|
||||
{ return sqrt(dot(v, v)); }
|
||||
{ return simd4::magnitude(v.simd4()); }
|
||||
|
||||
/// The euclidean norm of the vector, that is what most people call length
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
length(const SGVec4<T>& v)
|
||||
{ return sqrt(dot(v, v)); }
|
||||
{ return simd4::magnitude(v.simd4()); }
|
||||
|
||||
/// The 1-norm of the vector, this one is the fastest length function we
|
||||
/// can implement on modern cpu's
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
norm1(const SGVec4<T>& v)
|
||||
{ return fabs(v(0)) + fabs(v(1)) + fabs(v(2)) + fabs(v(3)); }
|
||||
norm1(SGVec4<T> v)
|
||||
{ v.simd4() = simd4::abs(v.simd4()); return (v(0)+v(1)+v(2)+v(3)); }
|
||||
|
||||
/// The inf-norm of the vector
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
normI(const SGVec4<T>& v)
|
||||
{ return SGMisc<T>::max(fabs(v(0)), fabs(v(1)), fabs(v(2)), fabs(v(2))); }
|
||||
normI(SGVec4<T> v)
|
||||
{
|
||||
v.simd4() = simd4::abs(v.simd4());
|
||||
return SGMisc<T>::max(v(0), v(1), v(2), v(3));
|
||||
}
|
||||
|
||||
/// The euclidean norm of the vector, that is what most people call length
|
||||
template<typename T>
|
||||
@@ -389,14 +370,14 @@ template<typename T>
|
||||
inline
|
||||
T
|
||||
dist(const SGVec4<T>& v1, const SGVec4<T>& v2)
|
||||
{ return norm(v1 - v2); }
|
||||
{ return simd4::magnitude(v1.simd4() - v2.simd4()); }
|
||||
|
||||
/// The squared euclidean distance of the two vectors
|
||||
template<typename T>
|
||||
inline
|
||||
T
|
||||
distSqr(const SGVec4<T>& v1, const SGVec4<T>& v2)
|
||||
{ SGVec4<T> tmp = v1 - v2; return dot(tmp, tmp); }
|
||||
distSqr(SGVec4<T> v1, const SGVec4<T>& v2)
|
||||
{ return simd4::magnitude2(v1.simd4() - v2.simd4()); }
|
||||
|
||||
// calculate the projection of u along the direction of d.
|
||||
template<typename T>
|
||||
@@ -404,12 +385,22 @@ inline
|
||||
SGVec4<T>
|
||||
projection(const SGVec4<T>& u, const SGVec4<T>& d)
|
||||
{
|
||||
T denom = dot(d, d);
|
||||
T denom = simd4::magnitude2(d.simd4());
|
||||
T ud = dot(u, d);
|
||||
if (SGLimits<T>::min() < denom) return u;
|
||||
else return d * (dot(u, d) / denom);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline
|
||||
SGVec4<T>
|
||||
interpolate(T tau, const SGVec4<T>& v1, const SGVec4<T>& v2)
|
||||
{
|
||||
SGVec4<T> r;
|
||||
r.simd4() = simd4::interpolate(tau, v1.simd4(), v2.simd4());
|
||||
return r;
|
||||
}
|
||||
|
||||
#ifndef NDEBUG
|
||||
template<typename T>
|
||||
inline
|
||||
@@ -431,11 +422,11 @@ operator<<(std::basic_ostream<char_type, traits_type>& s, const SGVec4<T>& v)
|
||||
inline
|
||||
SGVec4f
|
||||
toVec4f(const SGVec4d& v)
|
||||
{ return SGVec4f((float)v(0), (float)v(1), (float)v(2), (float)v(3)); }
|
||||
{ SGVec4f f(v); return f; }
|
||||
|
||||
inline
|
||||
SGVec4d
|
||||
toVec4d(const SGVec4f& v)
|
||||
{ return SGVec4d(v(0), v(1), v(2), v(3)); }
|
||||
{ SGVec4d d(v); return d; }
|
||||
|
||||
#endif
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user