Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into mesh
Browse files Browse the repository at this point in the history
  • Loading branch information
iaomw committed Sep 13, 2024
2 parents 2341d65 + 9de610f commit 2265d0a
Show file tree
Hide file tree
Showing 6 changed files with 718 additions and 19 deletions.
39 changes: 37 additions & 2 deletions projects/CalcGeometryUV/PrimitivePlyIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

static void readply(
std::vector<zeno::vec3f> &verts,
std::vector<zeno::vec3f> &color,
std::vector<zeno::vec3i> &zfaces,
const std::string & filepath
) {
Expand All @@ -36,11 +37,20 @@ static void readply(
tinyply::PlyFile file;
file.parse_header(*file_stream);

std::shared_ptr<tinyply::PlyData> vertices, faces;
std::shared_ptr<tinyply::PlyData> vertices, r,g,b, faces;

try { vertices = file.request_properties_from_element("vertex", { "x", "y", "z" }); }
catch (const std::exception & e) { std::cerr << "tinyply exception: " << e.what() << std::endl; }

try { r = file.request_properties_from_element("vertex", {"red"}); }
catch (const std::exception & e) { std::cerr << "tinyply exception: " << e.what() << std::endl; }

try { g = file.request_properties_from_element("vertex", {"green"}); }
catch (const std::exception & e) { std::cerr << "tinyply exception: " << e.what() << std::endl; }

try { b = file.request_properties_from_element("vertex", {"blue"}); }
catch (const std::exception & e) { std::cerr << "tinyply exception: " << e.what() << std::endl; }

try { faces = file.request_properties_from_element("face", { "vertex_indices" }, 3); }
catch (const std::exception & e) { std::cerr << "tinyply exception: " << e.what() << std::endl; }

Expand All @@ -59,6 +69,30 @@ static void readply(
verts.emplace_back(v[0], v[1], v[2]);
}
}

if(r->count>0)
{
color.resize(verts.size());
if(r->t == tinyply::Type::UINT8 || r->t == tinyply::Type::INT8)
{
std::vector<uint8_t> rr;
std::vector<uint8_t> gg;
std::vector<uint8_t> bb;
rr.resize(r->count);
gg.resize(g->count);
bb.resize(b->count);
std::memcpy(rr.data(), r->buffer.get(), r->count );
std::memcpy(gg.data(), g->buffer.get(), g->count );
std::memcpy(bb.data(), b->buffer.get(), b->count );

for(size_t i=0;i<rr.size();i++)
{
color[i] = zeno::vec3f(rr[i],gg[i],bb[i])/255.0f;
}
}
}


const size_t numFacesBytes = faces->buffer.size_bytes();
if (faces->t == tinyply::Type::INT32 || faces->t == tinyply::Type::UINT32) {
zfaces.resize(faces->count);
Expand Down Expand Up @@ -89,8 +123,9 @@ struct ReadPlyPrimitive : zeno::INode {
auto path = get_input<zeno::StringObject>("path")->get();
auto prim = std::make_shared<zeno::PrimitiveObject>();
auto &pos = prim->verts;
auto &clr = prim->add_attr<zeno::vec3f>("clr");
auto &tris = prim->tris;
readply(pos, tris, path);
readply(pos, clr, tris, path);
prim->resize(pos.size());
set_output("prim", std::move(prim));
}
Expand Down
34 changes: 21 additions & 13 deletions projects/CuLagrange/geometry/MeshIO.cu
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <zeno/types/NumericObject.h>
#include <zeno/types/PrimitiveObject.h>
#include <zeno/types/StringObject.h>

#include <iostream>


namespace zeno {
Expand All @@ -21,24 +21,31 @@ struct ReadVTKMesh : INode {
void apply() override {
auto view_interior = get_param<int>("view_interior");
auto path = get_input<StringObject>("path")->get();
auto type = get_input<StringObject>("method")->get();
auto prim = std::make_shared<PrimitiveObject>();
bool ret = load_vtk_data(path,prim,0);
bool ret;

if(type == "mesh")
ret = load_vtk_data(path,prim,0);
else
ret = read_unstructured_grid_to_points(path, prim);

if(view_interior && prim->quads.size() > 0){
if(type == "mesh") {
if (view_interior && prim->quads.size() > 0) {
prim->tris.resize(prim->quads.size() * 4);

constexpr auto space = zs::execspace_e::openmp;
auto ompExec = zs::omp_exec();

ompExec(zs::range(prim->quads.size()),
[prim] (int ei) mutable {
const auto& tet = prim->quads[ei];
// for(int i = 0;i < 4;++i)
prim->tris[ei * 4 + 0] = zeno::vec3i{tet[0],tet[1],tet[2]};
prim->tris[ei * 4 + 1] = zeno::vec3i{tet[1],tet[3],tet[2]};
prim->tris[ei * 4 + 2] = zeno::vec3i{tet[0],tet[2],tet[3]};
prim->tris[ei * 4 + 3] = zeno::vec3i{tet[0],tet[3],tet[1]};
});
ompExec(zs::range(prim->quads.size()), [prim](int ei) mutable {
const auto &tet = prim->quads[ei];
// for(int i = 0;i < 4;++i)
prim->tris[ei * 4 + 0] = zeno::vec3i{tet[0], tet[1], tet[2]};
prim->tris[ei * 4 + 1] = zeno::vec3i{tet[1], tet[3], tet[2]};
prim->tris[ei * 4 + 2] = zeno::vec3i{tet[0], tet[2], tet[3]};
prim->tris[ei * 4 + 3] = zeno::vec3i{tet[0], tet[3], tet[1]};
});
}
}

set_output("prim",std::move(prim));
Expand All @@ -47,6 +54,7 @@ struct ReadVTKMesh : INode {

ZENDEFNODE(ReadVTKMesh, {/* inputs: */ {
{"readpath", "path"},
{"enum verts mesh", "method", "verts"},
},
/* outputs: */
{
Expand Down
195 changes: 195 additions & 0 deletions projects/CuLagrange/geometry/file_parser/read_vtk_mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <cassert>
#include <array>
#include <cstdio>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
Expand Down Expand Up @@ -543,7 +544,200 @@ namespace zeno {
printf("finish reading polydata\n");
return true;
}
bool stringReplace(std::string &in, std::string del, std::string add)
{
size_t start_pos = in.find(del);
if(start_pos == std::string::npos)
return false;
in.replace(start_pos, del.length(), add);
return true;
}
bool read_unstructured_grid_to_points(std::string file_name, std::shared_ptr<zeno::PrimitiveObject>& prim)
{
std::ifstream file;
file.open(file_name.c_str());
int num_points;
int num_field_data;
std::string readtype;
while(!file.eof())
{
std::string line_str;
std::getline(file,line_str);
if(line_str == "BINARY")
{
readtype = "BINARY";
}
if(line_str == "ASCII")
{
readtype = "ASCII";
}
if(line_str.find("POINTS ")!=std::string::npos)
{
std::string a;
std::string type;
std::stringstream ss;
ss<<line_str;
ss>>a>>num_points>>type;
prim->verts.resize(num_points);
if(readtype == "ASCII") {
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < 3; j++)
file >> prim->verts[i][j];
}
}else if(readtype == "BINARY")
{
//read chunck of buffer
}
}
else if(line_str.find("Velocity ")!=std::string::npos)
{
prim->verts.add_attr<zeno::vec3f>("Velocity");
if(readtype == "ASCII") {
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < 3; j++)
file >> prim->verts.attr<zeno::vec3f>("Velocity")[i][j];
}
}else if(readtype == "BINARY"){

}
}
else if(line_str.find("FieldData "))
{
std::string a;
std::string b;
std::stringstream ss;
ss<<line_str;
ss>>a>>b>>num_field_data;
}
if(num_field_data>0) {
std::vector<std::string> strs;
std::string a;
std::stringstream ss;
ss<<line_str;
while(ss>>a)
{
strs.push_back(a);
}
std::string varname;
if(strs.size()==4 && std::stoi(strs[2]) == num_points)
{
varname = strs[0];
while(stringReplace(varname, ".", "_"));
while(stringReplace(varname, "-", "_"));
if(strs[1] == "1") {
prim->verts.add_attr<float>(varname);
if(readtype == "ASCII") {
for (int i = 0; i < num_points; i++) {
file >> prim->verts.attr<float>(varname)[i];
}
}else if(readtype == "BINARY")
{

}
}else if(strs[1] == "3")
{
prim->verts.add_attr<zeno::vec3f>(varname);
if(readtype == "ASCII") {
for (int i = 0; i < num_points; i++) {
for (int j = 0; j < 3; j++)
file >> prim->verts.attr<zeno::vec3f>(varname)[i][j];
}
}else if(readtype == "BINARY")
{

}
}
}


// if (line_str.find("SpeciesDensity ") != std::string::npos) {
// prim->verts.add_attr<float>("SpeciesDensity");
// for (int i = 0; i < num_points; i++) {
// file >> prim->verts.attr<float>("SpeciesDensity")[i];
// }
// }
// else if (line_str.find("ViscosityEddy ") != std::string::npos) {
// prim->verts.add_attr<float>("ViscosityEddy");
// for (int i = 0; i < num_points; i++) {
// file >> prim->verts.attr<float>("ViscosityEddy")[i];
// }
// }
// else if (line_str.find("Mach ") != std::string::npos) {
// prim->verts.add_attr<float>("Mach");
// for (int i = 0; i < num_points; i++) {
// file >> prim->verts.attr<float>("Mach")[i];
// }
// }
// else if (line_str.find("MachNumberinStnFrame ") !=
// std::string::npos) {
// prim->verts.add_attr<float>("MachNumberinStnFrame");
// for (int i = 0; i < num_points; i++) {
// file >> prim->verts.attr<float>("MachNumberinStnFrame")[i];
// }
// }
// else if (line_str.find("Pressure ") != std::string::npos) {
// prim->verts.add_attr<float>("Pressure");
// for (int i = 0; i < num_points; i++) {
// file >> prim->verts.attr<float>("Pressure")[i];
// }
// }
// else if (line_str.find("Temperature ") != std::string::npos) {
// prim->verts.add_attr<float>("Temperature");
// for (int i = 0; i < num_points; i++) {
// file >> prim->verts.attr<float>("Temperature")[i];
// }
// }
// else if (line_str.find("PressureStagnation ") != std::string::npos) {
// prim->verts.add_attr<float>("PressureStagnation");
// for (int i = 0; i < num_points; i++) {
// file >> prim->verts.attr<float>("PressureStagnation")[i];
// }
// }
// else if (line_str.find("TotalPressureinStnFrame ") !=
// std::string::npos) {
// prim->verts.add_attr<float>("TotalPressureinStnFrame");
// for (int i = 0; i < num_points; i++) {
// file >> prim->verts.attr<float>("TotalPressureinStnFrame")[i];
// }
// }
// else if (line_str.find("TemperatureStagnation ") !=
// std::string::npos) {
// prim->verts.add_attr<float>("TemperatureStagnation");
// for (int i = 0; i < num_points; i++) {
// file >> prim->verts.attr<float>("TemperatureStagnation")[i];
// }
// }
// else if (line_str.find("TotalTemperatureinStnFrame ") !=
// std::string::npos) {
// prim->verts.add_attr<float>("TotalTemperatureinStnFrame");
// for (int i = 0; i < num_points; i++) {
// file >> prim->verts.attr<float>("TotalTemperatureinStnFrame")[i];
// }
// }
// else if (line_str.find("TurbulenceEddyFrequency ") !=
// std::string::npos) {
// prim->verts.add_attr<float>("TurbulenceEddyFrequency");
// for (int i = 0; i < num_points; i++) {
// file >> prim->verts.attr<float>("TurbulenceEddyFrequency")[i];
// }
// }
// else if (line_str.find("TurbulentEnergyKinetic ") !=
// std::string::npos) {
// prim->verts.add_attr<float>("TurbulentEnergyKinetic");
// for (int i = 0; i < num_points; i++) {
// file >> prim->verts.attr<float>("TurbulentEnergyKinetic")[i];
// }
// }
// else if (line_str.find("VelocityinStnFrame ") != std::string::npos) {
// prim->verts.add_attr<float>("VelocityinStnFrame");
// for (int i = 0; i < num_points; i++) {
// file >> prim->verts.attr<float>("VelocityinStnFrame")[i];
// }
// }
}
}
file.close();
}
// DATASET UNSTRUCTURED_GRID
// POINTS n dataType
// p0x p0y p0z
Expand All @@ -564,6 +758,7 @@ namespace zeno {
// type2
// ...
// typen-1

constexpr char unstructured_grid[] = "UNSTRUCTURED_GRID";
bool read_unstructured_grid(FILE *fp,std::shared_ptr<zeno::PrimitiveObject>& prim,int& line_count) {
char *bufferp;
Expand Down
Loading

0 comments on commit 2265d0a

Please sign in to comment.