Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve_vtk #2015

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 43 additions & 125 deletions projects/CuLagrange/geometry/file_parser/read_vtk_mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <zeno/types/PrimitiveObject.h>
#include <zeno/utils/log.h>
#include <zeno/utils/bit_operations.h>
#include <zeno/zeno.h>


Expand Down Expand Up @@ -450,6 +451,42 @@ namespace zeno {
return false;
}

std::vector<zeno::vec3f> read_attr_vec3f(std::ifstream &file, int num_points, bool type_float) {
std::vector<zeno::vec3f> res(num_points);
if (type_float) {
float* data = new float[num_points * 3];
file.read((char*)data, sizeof (float) * num_points * 3);
for (auto i = 0; i < num_points * 3; i++) {
reinterpret_cast<float *>(res.data())[i] = byteswap(data[i]);
}
}
else {
double* data = new double[num_points * 3];
file.read((char*)data, sizeof (double) * num_points * 3);
for (auto i = 0; i < num_points * 3; i++) {
reinterpret_cast<float *>(res.data())[i] = byteswap(data[i]);
}
}
return res;
}
std::vector<float> read_attr_float(std::ifstream &file, int num_points, bool type_float) {
std::vector<float> res(num_points);
if (type_float) {
float* data = new float[num_points];
file.read((char*)data, sizeof (float) * num_points);
for (auto i = 0; i < num_points; i++) {
reinterpret_cast<float *>(res.data())[i] = byteswap(data[i]);
}
}
else {
double* data = new double[num_points];
file.read((char*)data, sizeof (double) * num_points);
for (auto i = 0; i < num_points; i++) {
reinterpret_cast<float *>(res.data())[i] = byteswap(data[i]);
}
}
return res;
}
// DATASET POLYDATA
// POINTS n dataType
// p0x p0y p0z
Expand Down Expand Up @@ -587,28 +624,7 @@ namespace zeno {
}
}else if(readtype == "BINARY")
{
if (type == "float") {
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < 3; j++) {
char buf[4];
for (auto k = 3; k >= 0; k--) {
buf[k] = file.get();
}
prim->verts[i][j] = *(float*)buf;
}
}
}
else if (type == "double") {
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < 3; j++) {
char buf[8];
for (auto k = 7; k >= 0; k--) {
buf[k] = file.get();
}
prim->verts[i][j] = *(double*)buf;
}
}
}
prim->verts.values = read_attr_vec3f(file, num_points, type == "float");
}
}
else if(line_str.find("Velocity ")!=std::string::npos)
Expand All @@ -620,28 +636,7 @@ namespace zeno {
file >> Velocity[i][j];
}
}else if(readtype == "BINARY"){
if (line_str.find(" float")!=std::string::npos) {
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < 3; j++) {
char buf[4];
for (auto k = 3; k >= 0; k--) {
buf[k] = file.get();
}
Velocity[i][j] = *(float*)buf;
}
}
}
else if (line_str.find(" double")!=std::string::npos){
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < 3; j++) {
char buf[8];
for (auto k = 7; k >= 0; k--) {
buf[k] = file.get();
}
Velocity[i][j] = *(double*)buf;
}
}
}
Velocity = read_attr_vec3f(file, num_points, line_str.find(" float")!=std::string::npos);
}
}
else if(line_str.find("POINT_DATA ")!=std::string::npos)
Expand All @@ -667,27 +662,7 @@ namespace zeno {
file >> Velocity[i][j];
}
} else if (readtype == "BINARY") {
if (c=="float") {
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < 3; j++) {
char buf[4];
for (auto k = 3; k >= 0; k--) {
buf[k] = file.get();
}
Velocity[i][j] = *(float *)buf;
}
}
} else if (c=="double") {
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < 3; j++) {
char buf[8];
for (auto k = 7; k >= 0; k--) {
buf[k] = file.get();
}
Velocity[i][j] = *(double *)buf;
}
}
}
Velocity = read_attr_vec3f(file, num_points, c=="float");
}
}else if(a=="SCALARS") {
auto &Velocity = prim->verts.add_attr<float>(name);
Expand All @@ -696,27 +671,7 @@ namespace zeno {
file >> Velocity[i];
}
} else if (readtype == "BINARY") {
if (c=="float") {
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < 1; j++) {
char buf[4];
for (auto k = 3; k >= 0; k--) {
buf[k] = file.get();
}
Velocity[i] = *(float *)buf;
}
}
} else if (c=="double") {
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < 1; j++) {
char buf[8];
for (auto k = 7; k >= 0; k--) {
buf[k] = file.get();
}
Velocity[i] = *(double *)buf;
}
}
}
Velocity = read_attr_float(file, num_points, c=="float");
}
}
}
Expand Down Expand Up @@ -751,24 +706,7 @@ namespace zeno {
}
}else if(readtype == "BINARY")
{
if (strs[3] == "float") {
for (int i = 0; i < num_points; i++) {
char buf[4];
for (auto k = 3; k >= 0; k--) {
buf[k] = file.get();
}
vars[i] = *(float*)buf;
}
}
else if (strs[3] == "double") {
for (int i = 0; i < num_points; i++) {
char buf[8];
for (auto k = 7; k >= 0; k--) {
buf[k] = file.get();
}
vars[i] = *(double*)buf;
}
}
vars = read_attr_float(file, num_points, strs[3]=="float");
}
}else if(strs[1] == "3")
{
Expand All @@ -781,27 +719,7 @@ namespace zeno {
file >> vars[i][j];
}
} else if (readtype == "BINARY") {
if (strs[3] == "float") {
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < 3; j++) {
char buf[4];
for (auto k = 3; k >= 0; k--) {
buf[k] = file.get();
}
vars[i][j] = *(float *)buf;
}
}
} else if (strs[3] == "double") {
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < 3; j++) {
char buf[8];
for (auto k = 7; k >= 0; k--) {
buf[k] = file.get();
}
vars[i][j] = *(double *)buf;
}
}
}
vars = read_attr_vec3f(file, num_points, strs[3]=="float");
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions zeno/include/zeno/utils/bit_operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ constexpr To &bit_cast(T &t) noexcept {
return *reinterpret_cast<To *>(std::addressof(t));
}


template<class T>
constexpr T byteswap(T value) noexcept
{
T out;
char * value_ptr = (char *)&value;
char * out_ptr = (char *)&out;
for (auto i = 0; i < sizeof(T); i++) {
out_ptr[i] = value_ptr[sizeof (T) - 1 - i];
}
return out;
}

static constexpr std::uint8_t ceil_log2(std::size_t x) {
const std::uint64_t t[6] = {
0xFFFFFFFF00000000ull,
Expand Down
Loading