Skip to content

Commit

Permalink
Add xyz file support for example apps (doyubkim#306)
Browse files Browse the repository at this point in the history
  • Loading branch information
doyubkim authored Aug 22, 2020
1 parent 45a1882 commit af7df69
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 107 deletions.
41 changes: 41 additions & 0 deletions src/examples/example_utils/io_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright (c) 2020 Doyub Kim
//
// I am making my contributions/submissions to this project solely in my
// personal capacity and am not conveying any rights to any intellectual
// property of any third parties.

#include <jet/jet.h>
#include <pystring/pystring.h>

inline bool readPositions(const std::string& filename,
jet::Array1<jet::Vector3D>& positions) {
if (pystring::endswith(filename, ".xyz")) {
std::ifstream xyzFile(filename.c_str());
while (xyzFile) {
jet::Vector3D xyz;

xyzFile >> xyz.x >> xyz.y >> xyz.z;

if (xyzFile.fail() || xyzFile.eof()) {
break;
}

positions.append(xyz);
}

xyzFile.close();
return true;
} else {
std::ifstream posFile(filename.c_str(), std::ifstream::binary);
if (posFile) {
std::vector<uint8_t> buffer(
(std::istreambuf_iterator<char>(posFile)),
(std::istreambuf_iterator<char>()));
deserialize(buffer, &positions);
posFile.close();
return true;
} else {
return false;
}
}
}
18 changes: 5 additions & 13 deletions src/examples/hybrid_liquid_sim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,8 @@ target_compile_definitions(${target}
)

# Link libraries
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
else()
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
endif()
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
18 changes: 5 additions & 13 deletions src/examples/level_set_liquid_sim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,8 @@ target_compile_definitions(${target}
)

# Link libraries
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
else()
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
endif()
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
15 changes: 4 additions & 11 deletions src/examples/obj2sdf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ target_compile_options(${target}
)

# Link libraries
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet)
else()
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet)
endif()
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet)
18 changes: 5 additions & 13 deletions src/examples/particles2obj/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,8 @@ target_compile_options(${target}
)

# Link libraries
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
else()
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
endif()
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
14 changes: 4 additions & 10 deletions src/examples/particles2obj/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <pystring/pystring.h>

#include <example_utils/clara_utils.h>
#include <example_utils/io_utils.h>
#include <clara.hpp>

#include <algorithm>
Expand Down Expand Up @@ -107,8 +108,8 @@ int main(int argc, char* argv[]) {
// Parsing
auto parser =
clara::Help(showHelp) |
clara::Opt(inputFilename,
"inputFilename")["-i"]["--input"]("input obj file name") |
clara::Opt(inputFilename, "inputFilename")["-i"]["--input"](
"input pos/xyz file name") |
clara::Opt(outputFilename,
"outputFilename")["-o"]["--output"]("output obj file name") |
clara::Opt(strResolution, "resolution")["-r"]["--resolution"](
Expand Down Expand Up @@ -221,14 +222,7 @@ int main(int argc, char* argv[]) {

// Read particle positions
Array1<Vector3D> positions;
std::ifstream positionFile(inputFilename.c_str(), std::ifstream::binary);
if (positionFile) {
std::vector<uint8_t> buffer(
(std::istreambuf_iterator<char>(positionFile)),
(std::istreambuf_iterator<char>()));
deserialize(buffer, &positions);
positionFile.close();
} else {
if (!readPositions(inputFilename, positions)) {
printf("Cannot read file %s.\n", inputFilename.c_str());
exit(EXIT_FAILURE);
}
Expand Down
16 changes: 5 additions & 11 deletions src/examples/particles2xml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,8 @@ target_compile_options(${target}
)

# Link libraries
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet)
else()
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet)
endif()
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
12 changes: 3 additions & 9 deletions src/examples/particles2xml/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <pystring/pystring.h>

#include <example_utils/clara_utils.h>
#include <example_utils/io_utils.h>
#include <clara.hpp>

#include <algorithm>
Expand Down Expand Up @@ -65,7 +66,7 @@ int main(int argc, char* argv[]) {
auto parser =
clara::Help(showHelp) |
clara::Opt(inputFilename, "inputFilename")["-i"]["--input"](
"input particle position file name") |
"input particle pos/xyz file name") |
clara::Opt(outputFilename,
"outputFilename")["-o"]["--output"]("output xml file name");

Expand All @@ -87,14 +88,7 @@ int main(int argc, char* argv[]) {

// Read particle positions
Array1<Vector3D> positions;
std::ifstream positionFile(inputFilename.c_str(), std::ifstream::binary);
if (positionFile) {
std::vector<uint8_t> buffer(
(std::istreambuf_iterator<char>(positionFile)),
(std::istreambuf_iterator<char>()));
deserialize(buffer, &positions);
positionFile.close();
} else {
if (!readPositions(inputFilename, positions)) {
printf("Cannot read file %s.\n", inputFilename.c_str());
exit(EXIT_FAILURE);
}
Expand Down
19 changes: 5 additions & 14 deletions src/examples/smoke_sim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,8 @@ target_compile_definitions(${target}
)

# Link libraries
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
else()
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
endif()

target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
18 changes: 5 additions & 13 deletions src/examples/sph_sim/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,8 @@ target_compile_definitions(${target}
)

# Link libraries
if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
else()
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)
endif()
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
jet
pystring)

0 comments on commit af7df69

Please sign in to comment.