Skip to content

Commit 55aee25

Browse files
add simple modern-bgeo reader
1 parent 48b39d5 commit 55aee25

File tree

9 files changed

+274
-35
lines changed

9 files changed

+274
-35
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ else()
129129
set(ZLIB_LIBRARY "")
130130
endif()
131131

132+
find_package(bgeo)
133+
if (bgeo_FOUND)
134+
message("Building with modern Bgeo support")
135+
else()
136+
message("Building with NO modern Bgeo support")
137+
endif()
138+
132139
# Make modules able to see partio library
133140
set(PARTIO_LIBRARIES partio ${ZLIB_LIBRARY})
134141

src/lib/CMakeLists.txt

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,34 @@
3131
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3232
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
3333

34-
file(GLOB io_cpp "io/*.cpp")
35-
file(GLOB core_cpp "core/*.cpp")
34+
set(core_cpp
35+
core/Particle.cpp
36+
core/ParticleCaching.cpp
37+
core/ParticleHeaders.cpp
38+
core/ParticleSimple.cpp
39+
core/ParticleSimpleInterleave.cpp)
40+
41+
set(io_cpp
42+
io/BGEO.cpp
43+
io/BHCLASSIC.cpp
44+
io/BIN.cpp
45+
io/GEO.cpp
46+
io/HCLASSIC.cpp
47+
io/MC.cpp
48+
io/ParticleIO.cpp
49+
io/PDA.cpp
50+
io/PDB.cpp
51+
io/PDC.cpp
52+
io/PRT.cpp
53+
io/PTC.cpp
54+
io/PTS.cpp
55+
io/RIB.cpp
56+
io/ZIP.cpp
57+
)
58+
59+
if (bgeo_FOUND)
60+
list(APPEND io_cpp io/BJSON.cpp )
61+
endif()
3662

3763
if(PARTIO_BUILD_SHARED_LIBS)
3864
set(PARTIO_LIBRARY_TYPE SHARED)
@@ -55,6 +81,11 @@ if (ZLIB_FOUND)
5581
target_link_libraries(partio PUBLIC ZLIB::ZLIB)
5682
endif()
5783

84+
if (bgeo_FOUND)
85+
target_link_libraries(partio PUBLIC bgeo::bgeo)
86+
target_compile_definitions(partio PRIVATE MODERN_BGEO)
87+
endif()
88+
5889
install(TARGETS partio DESTINATION ${CMAKE_INSTALL_LIBDIR})
5990

6091
file(GLOB public_includes "*.h")

src/lib/core/KdTree.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,16 @@ void KdTree<k>::sortSubtree(int n, int size, int j)
325325

326326
// partition range [n, n+size) along axis j into two subranges:
327327
// [n, n+leftSize+1) and [n+leftSize+1, n+size)
328+
#ifdef NDEBUG
328329
std::nth_element(&_ids[n], &_ids[n+left], &_ids[n+size],
329330
ComparePointsById(&_points[0].p[j]));
331+
#else
332+
// In Debug mode element compiler asserting on _ids[n+size] with
333+
// Expression: vector subscript out of range
334+
// whereas it's not accessed by algorithm
335+
std::nth_element(&_ids[n], &_ids[n+left], &_ids[std::min(n+size, this->size()-1)],
336+
ComparePointsById(&_points[0].p[j]));
337+
#endif
330338
// move median value (nth element) to front as root node of subtree
331339
std::swap(_ids[n], _ids[n+left]);
332340

src/lib/io/BGEO.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#include "../core/ParticleHeaders.h"
3+
#include "readers.h"
4+
5+
namespace Partio
6+
{
7+
ParticlesDataMutable* readBGEO(const char* filename, const bool headersOnly, std::ostream* errorStream)
8+
{
9+
10+
#ifdef MODERN_BGEO
11+
ParticlesDataMutable *result = readBJSON(filename, headersOnly, errorStream);
12+
if (result != nullptr)
13+
return result;
14+
#endif
15+
16+
// Fallback to bhclassic
17+
return readBHCLASSIC(filename, headersOnly, errorStream);
18+
}
19+
20+
bool writeBGEO(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream)
21+
{
22+
return writeBHCLASSIC(filename, p, compressed, errorStream);
23+
}
24+
}

src/lib/io/BJSON.cpp

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
// Utilize this library
2+
// https://github.com/LaikaStudios/bgeo_reader
3+
4+
5+
#include "../core/ParticleHeaders.h"
6+
7+
#ifdef MODERN_BGEO
8+
#include <bgeo/Bgeo.h>
9+
#include <bgeo/BgeoHeader.h>
10+
#include <bgeo/parser/ReadError.h>
11+
#endif
12+
13+
#include <unordered_map>
14+
15+
using namespace ika::bgeo;
16+
17+
namespace Partio
18+
{
19+
std::string getMapping(const char* name)
20+
{
21+
const std::unordered_map<std::string, std::string> mapping =
22+
{
23+
{"P", "position"},
24+
};
25+
auto it = mapping.find(name);
26+
if (it != mapping.end())
27+
return it->second;
28+
else
29+
return name;
30+
}
31+
32+
ParticlesDataMutable* readBJSON(const char* filename, const bool headersOnly, std::ostream* errorStream)
33+
{
34+
try
35+
{
36+
BgeoHeader bgeoHeader(filename);
37+
}
38+
catch(const ika::bgeo::parser::ReadError& e)
39+
{
40+
return nullptr;
41+
}
42+
43+
Bgeo bgeo(filename, false);
44+
45+
// Allocate a simple particle with the appropriate number of points
46+
ParticlesDataMutable* particles = headersOnly ? new ParticleHeaders : create();
47+
particles->addParticles(bgeo.getPointCount());
48+
49+
int attribCount = bgeo.getPointAttributeCount();
50+
for (int k = 0; k < attribCount; k++)
51+
{
52+
Bgeo::AttributePtr currentAttrib = bgeo.getPointAttribute(k);
53+
54+
if (strcmp(currentAttrib->getType(), "string") == 0)
55+
{
56+
std::vector<int> values;
57+
std::vector<std::string> strings;
58+
currentAttrib->getData(values);
59+
currentAttrib->getStrings(strings);
60+
61+
ParticleAttribute attrib = particles->addAttribute( getMapping(currentAttrib->getName()).c_str(), INDEXEDSTR, 1);
62+
for(size_t i = 0; i < strings.size(); i++)
63+
{
64+
particles->registerIndexedStr(attrib, strings[i].c_str());
65+
}
66+
67+
for (size_t i = 0; i < bgeo.getPointCount(); i++)
68+
{
69+
int* data = particles->dataWrite<int>(attrib, i);
70+
*data = values[i];
71+
}
72+
continue;
73+
}
74+
75+
int tupleSize = currentAttrib->getTupleSize();
76+
parser::storage::Storage fundamentalType = currentAttrib->getFundamentalType();// 1: Int32, 2: Fpreal32
77+
ParticleAttributeType myType = NONE;
78+
79+
if (fundamentalType == parser::storage::Int32)
80+
myType = INT;
81+
else if(fundamentalType == parser::storage::Fpreal32)
82+
{
83+
myType = FLOAT;
84+
const char *subType = currentAttrib->getSubType();
85+
86+
if ((tupleSize == 3) &&
87+
((strcmp(subType, "point") == 0) ||
88+
(strcmp(subType, "vector") == 0) ||
89+
(strcmp(subType, "normal") == 0)))
90+
myType = VECTOR;
91+
}
92+
else
93+
continue;
94+
95+
ParticleAttribute attrib = particles->addAttribute( getMapping(currentAttrib->getName()).c_str(), myType, tupleSize);
96+
97+
if (fundamentalType == parser::storage::Int32)
98+
{
99+
std::vector<int> values;
100+
currentAttrib->getData(values);
101+
size_t idx = 0;
102+
103+
for (size_t i = 0; i < bgeo.getPointCount(); i++)
104+
{
105+
int* data = particles->dataWrite<int>(attrib, i);
106+
107+
for (int c = 0; c < tupleSize; c++)
108+
{
109+
data[c] = values[idx+c];
110+
}
111+
idx += tupleSize;
112+
}
113+
}
114+
else if(fundamentalType == parser::storage::Fpreal32)
115+
{
116+
std::vector<float> values;
117+
currentAttrib->getData(values);
118+
size_t idx = 0;
119+
120+
for (size_t i = 0; i < bgeo.getPointCount(); i++)
121+
{
122+
float* data = particles->dataWrite<float>(attrib, i);
123+
for (int c = 0; c < tupleSize; c++)
124+
{
125+
data[c] = values[idx+c];
126+
}
127+
idx += tupleSize;
128+
}
129+
}
130+
else
131+
continue;
132+
}
133+
134+
return particles;
135+
}
136+
}

src/lib/io/GEO.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
#include "../core/ParticleHeaders.h"
3+
#include "readers.h"
4+
5+
namespace Partio
6+
{
7+
ParticlesDataMutable* readGEO(const char* filename, const bool headersOnly, std::ostream* errorStream)
8+
{
9+
10+
#ifdef MODERN_BGEO
11+
ParticlesDataMutable *result = readBJSON(filename, headersOnly, errorStream);
12+
if (result != nullptr)
13+
return result;
14+
#endif
15+
16+
// Fallback to hclassic
17+
return readHCLASSIC(filename, headersOnly, errorStream);
18+
}
19+
20+
bool writeGEO(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream)
21+
{
22+
return writeHCLASSIC(filename, p, compressed, errorStream);
23+
}
24+
}

src/lib/io/ParticleIO.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ readers()
5555
if(!initialized){
5656
initializationMutex.lock();
5757
data["bgeo"]=readBGEO;
58-
data["bhclassic"]=readBGEO;
58+
data["bhclassic"]=readBHCLASSIC;
5959
data["geo"]=readGEO;
60-
data["hclassic"]=readGEO;
60+
data["hclassic"]=readHCLASSIC;
6161
data["pdb"]=readPDB;
6262
data["pdb32"]=readPDB32;
6363
data["pdb64"]=readPDB64;
@@ -69,8 +69,8 @@ readers()
6969
data["bin"]=readBIN;
7070
data["pts"]=readPTS;
7171
data["ptf"]=readPTC;
72-
data["itbl"]=readBGEO;
73-
data["atbl"]=readBGEO;
72+
data["itbl"]=readBHCLASSIC;
73+
data["atbl"]=readBHCLASSIC;
7474
initialized=true;
7575
initializationMutex.unlock();
7676
}
@@ -85,9 +85,9 @@ writers()
8585
if(!initialized){
8686
initializationMutex.lock();
8787
data["bgeo"]=writeBGEO;
88-
data["bhclassic"]=writeBGEO;
88+
data["bhclassic"]=writeBHCLASSIC;
8989
data["geo"]=writeGEO;
90-
data["hclassic"]=writeGEO;
90+
data["hclassic"]=writeHCLASSIC;
9191
data["pdb"]=writePDB;
9292
data["pdb32"]=writePDB32;
9393
data["pdb64"]=writePDB64;
@@ -98,8 +98,8 @@ writers()
9898
data["prt"]=writePRT;
9999
data["bin"]=writeBIN;
100100
data["ptf"]=writePTC;
101-
data["itbl"]=writeBGEO;
102-
data["atbl"]=writeBGEO;
101+
data["itbl"]=writeBHCLASSIC;
102+
data["atbl"]=writeBHCLASSIC;
103103
initialized=true;
104104
initializationMutex.unlock();
105105
}
@@ -117,7 +117,7 @@ bool extensionIgnoringGz(const string& filename,string& ret,bool &endsWithGz,std
117117
return false;
118118
}
119119
string extension=filename.substr(period+1);
120-
if(extension=="gz"){
120+
if(extension=="gz" || extension=="sc"){
121121
endsWithGz=true;
122122
size_t period2=filename.rfind('.',period-1);
123123
if(period2==string::npos){

src/lib/io/readers.h

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,39 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
3636
#define _READERS_h_
3737

3838
namespace Partio{
39-
ParticlesDataMutable* readBGEO( const char* filename,const bool headersOnly,std::ostream* errorStream);
40-
ParticlesDataMutable* readGEO( const char* filename,const bool headersOnly,std::ostream* errorStream);
41-
ParticlesDataMutable* readPDB( const char* filename,const bool headersOnly,std::ostream* errorStream);
42-
ParticlesDataMutable* readPDB32(const char* filename,const bool headersOnly,std::ostream* errorStream);
43-
ParticlesDataMutable* readPDB64(const char* filename,const bool headersOnly,std::ostream* errorStream);
44-
ParticlesDataMutable* readPDA( const char* filename,const bool headersOnly,std::ostream* errorStream);
45-
ParticlesDataMutable* readMC( const char* filename,const bool headersOnly,std::ostream* errorStream);
46-
ParticlesDataMutable* readPTC( const char* filename,const bool headersOnly,std::ostream* errorStream);
47-
ParticlesDataMutable* readPDC( const char* filename,const bool headersOnly,std::ostream* errorStream);
48-
ParticlesDataMutable* readPRT( const char* filename,const bool headersOnly,std::ostream* errorStream);
49-
ParticlesDataMutable* readBIN( const char* filename,const bool headersOnly,std::ostream* errorStream);
50-
ParticlesDataMutable* readPTS( const char* filename,const bool headersOnly,std::ostream* errorStream);
39+
ParticlesDataMutable* readBGEO( const char* filename,const bool headersOnly,std::ostream* errorStream);
40+
ParticlesDataMutable* readGEO( const char* filename,const bool headersOnly,std::ostream* errorStream);
41+
ParticlesDataMutable* readBHCLASSIC(const char* filename,const bool headersOnly,std::ostream* errorStream);
42+
ParticlesDataMutable* readHCLASSIC( const char* filename,const bool headersOnly,std::ostream* errorStream);
43+
ParticlesDataMutable* readPDB( const char* filename,const bool headersOnly,std::ostream* errorStream);
44+
ParticlesDataMutable* readPDB32( const char* filename,const bool headersOnly,std::ostream* errorStream);
45+
ParticlesDataMutable* readPDB64( const char* filename,const bool headersOnly,std::ostream* errorStream);
46+
ParticlesDataMutable* readPDA( const char* filename,const bool headersOnly,std::ostream* errorStream);
47+
ParticlesDataMutable* readMC( const char* filename,const bool headersOnly,std::ostream* errorStream);
48+
ParticlesDataMutable* readPTC( const char* filename,const bool headersOnly,std::ostream* errorStream);
49+
ParticlesDataMutable* readPDC( const char* filename,const bool headersOnly,std::ostream* errorStream);
50+
ParticlesDataMutable* readPRT( const char* filename,const bool headersOnly,std::ostream* errorStream);
51+
ParticlesDataMutable* readBIN( const char* filename,const bool headersOnly,std::ostream* errorStream);
52+
ParticlesDataMutable* readPTS( const char* filename,const bool headersOnly,std::ostream* errorStream);
5153

52-
bool writeBGEO(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
53-
bool writeGEO(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
54-
bool writePDB(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
55-
bool writePDB32(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
56-
bool writePDB64(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
57-
bool writePDA(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
58-
bool writePTC(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
59-
bool writeRIB(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
60-
bool writePDC(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
61-
bool writePRT(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
62-
bool writeBIN(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
54+
#ifdef MODERN_BGEO
55+
ParticlesDataMutable* readBJSON( const char* filename,const bool headersOnly,std::ostream* errorStream);
56+
ParticlesDataMutable* readJSON( const char* filename,const bool headersOnly,std::ostream* errorStream);
57+
#endif
58+
59+
bool writeBGEO( const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
60+
bool writeGEO( const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
61+
bool writeBHCLASSIC(const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
62+
bool writeHCLASSIC( const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
63+
bool writePDB( const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
64+
bool writePDB32( const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
65+
bool writePDB64( const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
66+
bool writePDA( const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
67+
bool writePTC( const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
68+
bool writeRIB( const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
69+
bool writePDC( const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
70+
bool writePRT( const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
71+
bool writeBIN( const char* filename,const ParticlesData& p,const bool compressed,std::ostream* errorStream);
6372
}
6473

6574
#endif

src/tests/testcluster.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,6 @@ int main(int argc,char *argv[])
4545
{
4646
Partio::ParticlesDataMutable* p=Partio::read(PARTIO_DATA_DIR "/scatter.bgeo");
4747
Partio::ParticlesDataMutable* c=Partio::computeClustering(p,5,1.5,100,2,5);
48-
Partio::write("/tmp/partio-cluster.bgeo",*c);
48+
Partio::write("partio-cluster.bgeo",*c);
4949
return 0;
5050
}

0 commit comments

Comments
 (0)