Skip to content

Commit

Permalink
Fix particle count sampling rate not set resulting in division by zer…
Browse files Browse the repository at this point in the history
…o exception
  • Loading branch information
nyue committed Dec 1, 2016
1 parent 6b7735a commit 0d067cd
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 31 deletions.
24 changes: 21 additions & 3 deletions lib/XMLReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <errno.h>

#include <boost/format.hpp>
// #include <boost/filesystem.hpp>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
Expand All @@ -37,8 +37,17 @@ XMLReader::~XMLReader()
void XMLReader::read(const std::string& i_ncache_xml_filename)
{
try {
std::cout << boost::format("i_ncache_xml_filename '%1%'") % i_ncache_xml_filename << std::endl;
boost::property_tree::ptree pt;

// This is Autodesk convention
boost::filesystem::path p(i_ncache_xml_filename);
boost::filesystem::directory_entry d(p);

_base_cache_name = p.stem().c_str();
_cache_directory = p.parent_path().c_str();
std::cout << boost::format("_base_cache_name '%1%', _cache_directory '%2%'") % _base_cache_name % _cache_directory << std::endl;

// Load XML file and put its contents in property tree.
// No namespace qualification is needed, because of Koenig
// lookup on the second argument. If reading fails, exception
Expand All @@ -56,6 +65,12 @@ void XMLReader::read(const std::string& i_ncache_xml_filename)
_cache_type = pt.get<std::string>("Autodesk_Cache_File.cacheType.<xmlattr>.Type");
_cache_format = pt.get<std::string>("Autodesk_Cache_File.cacheType.<xmlattr>.Format");
}
else if (v.first.compare("time")==0)
{
std::string time_range = pt.get<std::string>("Autodesk_Cache_File.time.<xmlattr>.Range");
std::cout << boost::format("time_range = '%1%'") % time_range << std::endl;
sscanf(time_range.c_str(),"%d-%d",&_time_range_start,&_time_range_end);
}
else if (v.first.compare("cacheTimePerFrame")==0)
{
_cacheTimePerFrame_TimePerFrame = atoi(pt.get<std::string>("Autodesk_Cache_File.cacheTimePerFrame.<xmlattr>.TimePerFrame").c_str());
Expand All @@ -70,10 +85,9 @@ void XMLReader::read(const std::string& i_ncache_xml_filename)
// Now, focus on just the channel information
BOOST_FOREACH(boost::property_tree::ptree::value_type &c, pt.get_child("Autodesk_Cache_File.Channels"))
{
std::cout << boost::format("Channels %1% : %2%") % c.first % c.second.data() << std::endl;

ChannelInfo channel_info;
std::string channel_name = pt.get<std::string>((boost::format("Autodesk_Cache_File.Channels.%1%.<xmlattr>.ChannelName")%c.first).str());
std::cout << boost::format("Channels %1% : %2%") % c.first % channel_name << std::endl;
channel_info._channel_type = pt.get<std::string>((boost::format("Autodesk_Cache_File.Channels.%1%.<xmlattr>.ChannelType")%c.first).str());
channel_info._channel_interpretation = pt.get<std::string>((boost::format("Autodesk_Cache_File.Channels.%1%.<xmlattr>.ChannelInterpretation")%c.first).str());
std::string sampling_type_string = pt.get<std::string>((boost::format("Autodesk_Cache_File.Channels.%1%.<xmlattr>.SamplingType")%c.first).str());
Expand All @@ -86,6 +100,10 @@ void XMLReader::read(const std::string& i_ncache_xml_filename)
channel_info._start_time = atoi(pt.get<std::string>((boost::format("Autodesk_Cache_File.Channels.%1%.<xmlattr>.StartTime")%c.first).str()).c_str());
channel_info._end_time = atoi(pt.get<std::string>((boost::format("Autodesk_Cache_File.Channels.%1%.<xmlattr>.EndTime")%c.first).str()).c_str());

if (channel_info._channel_interpretation.compare("count")==0)
{
_particle_count_sampling_rate = channel_info._sampling_rate;
}
_channels.insert(ChannelInfoContainer::value_type(channel_name,channel_info));
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/XMLReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ namespace nCache
XMLReader();
~XMLReader();
void read(const std::string& i_ncache_xml_filename);
std::string getCacheType() const { return _cache_type; }
std::string getCacheFormat() const { return _cache_format; }
const ChannelInfoContainer& getChannels() const {return _channels; }
std::string get_cache_name() const { return _base_cache_name;};
std::string get_cache_type() const { return _cache_type; }
std::string get_cache_format() const { return _cache_format; }
const ChannelInfoContainer& get_channels() const {return _channels; }
std::string get_base_cache_name() const { return _base_cache_name;};
std::string get_cache_directory() const { return _cache_directory;};
// size_t get_num_frames() const { return _num_frames;};

Expand Down
131 changes: 123 additions & 8 deletions lib/nCacheAlembic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bool nCacheAlembic::process(const std::string& i_alembic_filename,
Alembic::Abc::OArchive archive(Alembic::Abc::CreateArchiveWithInfo(Alembic::AbcCoreHDF5::WriteArchive(),
std::string(i_alembic_filename.c_str()),
std::string("Procedural Insight Pty. Ltd."),
std::string("Multiple Samples Random points"),
std::string("nCache2Alembic points"),
Alembic::Abc::ErrorHandler::kThrowPolicy));

Alembic::AbcGeom::OObject iParent( archive, Alembic::AbcGeom::kTop );
Expand All @@ -44,7 +44,7 @@ bool nCacheAlembic::process(const std::string& i_alembic_filename,
std::cout << boost::format("FPS (calculated) = %1%") % fps << std::endl;
std::cout << boost::format("ncache_sampling_rate = %1%") % ncache_sampling_rate << std::endl;

Alembic::Util::uint32_t num_time_samples = (i_ncache_loader.get_xml_reader().get_time_range_end() - i_ncache_loader.get_xml_reader().get_time_range_start())/i_ncache_loader.get_xml_reader().get_particle_count_sampling_rate() + 1;
Alembic::Util::uint32_t num_time_samples = ((ncache_end - ncache_start)/ncache_sampling_rate) + 1;
std::cout << boost::format("num_time_samples = %1%") % num_time_samples << std::endl;
// Create the time sampling type.
Alembic::AbcCoreAbstract::TimeSampling ts(ncache_sampling_rate/6000.0, ncache_start/(ncache_ticks_per_frame*fps));
Expand Down Expand Up @@ -83,7 +83,7 @@ bool nCacheAlembic::process(const std::string& i_alembic_filename,


// Now process the individual *.mc/*.mcx files
bool is_mcx = xml_reader.getCacheFormat().compare("mcx") == 0;
bool is_mcx = xml_reader.get_cache_format().compare("mcx") == 0;
std::string cache_extension("mc");
if (is_mcx)
cache_extension = "mcx";
Expand All @@ -92,7 +92,7 @@ bool nCacheAlembic::process(const std::string& i_alembic_filename,
CacheReaderSharedPointerType cache_reader_ptr;
for (size_t frame_index = start_frame; frame_index <= end_frame; frame_index++)
{
std::string per_initial_frame_full_path = (boost::format("%1%/%2%Frame%3%.%4%") % xml_reader.get_cache_directory() % xml_reader.get_cache_name() % frame_index % cache_extension).str();
std::string per_initial_frame_full_path = (boost::format("%1%/%2%Frame%3%.%4%") % xml_reader.get_cache_directory() % xml_reader.get_base_cache_name() % frame_index % cache_extension).str();
// std::cout << boost::format("per_initial_frame_full_path = '%1%'") % per_initial_frame_full_path << std::endl;

if (is_mcx)
Expand All @@ -113,7 +113,121 @@ bool nCacheAlembic::process(const std::string& i_alembic_filename,
size_t accumulated_ticks = ncache_sampling_rate;
while ((accumulated_ticks < ncache_ticks_per_frame) && ((ncache_ticks_per_frame-accumulated_ticks)>=ncache_sampling_rate) )
{
std::string per_sub_frame_full_path = (boost::format("%1%/%2%Frame%3%Tick%4%.%5%") % xml_reader.get_cache_directory() % xml_reader.get_cache_name() % frame_index % accumulated_ticks % cache_extension).str();
std::string per_sub_frame_full_path = (boost::format("%1%/%2%Frame%3%Tick%4%.%5%") % xml_reader.get_cache_directory() % xml_reader.get_base_cache_name() % frame_index % accumulated_ticks % cache_extension).str();
// std::cout << boost::format("\t""per_sub_frame_full_path '%1%'") % per_sub_frame_full_path << std::endl;

if (is_mcx)
cache_reader_ptr.reset(new MCXMemoryReader(per_sub_frame_full_path));
else
cache_reader_ptr.reset(new MCMemoryReader(per_sub_frame_full_path));

if (cache_reader_ptr)
{
process_single_sample(cache_reader_ptr,
position_channel_name,
id_channel_name,
pSchema);
}
accumulated_ticks += ncache_sampling_rate;
}
}
}
return true;
}

bool nCacheAlembic::process_per_frame(const std::string& i_alembic_format_filename,
const nCacheLoader& i_ncache_loader)
{
const nCache::XMLReader& xml_reader = i_ncache_loader.get_xml_reader();
size_t ncache_start = xml_reader.get_time_range_start();
size_t ncache_end = xml_reader.get_time_range_end();
size_t ncache_ticks_per_frame = xml_reader.get_cacheTimePerFrame_TimePerFrame();

// Now process the individual *.mc/*.mcx files
bool is_mcx = xml_reader.get_cache_format().compare("mcx") == 0;
std::string cache_extension("mc");
if (is_mcx)
cache_extension = "mcx";
size_t start_frame = ncache_start / ncache_ticks_per_frame;
size_t end_frame = ncache_end / ncache_ticks_per_frame;
CacheReaderSharedPointerType cache_reader_ptr;
for (size_t frame_index = start_frame; frame_index <= end_frame; frame_index++)
{
std::string alembic_filename = (boost::format(i_alembic_format_filename) % frame_index).str();

// Alembic file setup - START
Alembic::Abc::OArchive archive(Alembic::Abc::CreateArchiveWithInfo(Alembic::AbcCoreHDF5::WriteArchive(),
std::string(alembic_filename.c_str()),
std::string("Procedural Insight Pty. Ltd."),
std::string("nCache2Alembic points"),
Alembic::Abc::ErrorHandler::kThrowPolicy));

Alembic::AbcGeom::OObject iParent( archive, Alembic::AbcGeom::kTop );
Alembic::AbcGeom::OXform xform = addXform(iParent,"Xform");

size_t ncache_sampling_rate = xml_reader.get_particle_count_sampling_rate();
double fps = 6000.0/ncache_ticks_per_frame;

std::cout << boost::format("ncache_start = %1%") % ncache_start << std::endl;
std::cout << boost::format("ncache_end = %1%") % ncache_end << std::endl;
std::cout << boost::format("ncache_ticks_per_frame = %1%") % ncache_ticks_per_frame << std::endl;
std::cout << boost::format("FPS (calculated) = %1%") % fps << std::endl;
std::cout << boost::format("ncache_sampling_rate = %1%") % ncache_sampling_rate << std::endl;

Alembic::Util::uint32_t num_time_samples = (i_ncache_loader.get_xml_reader().get_time_range_end() - i_ncache_loader.get_xml_reader().get_time_range_start())/i_ncache_loader.get_xml_reader().get_particle_count_sampling_rate() + 1;
std::cout << boost::format("num_time_samples = %1%") % num_time_samples << std::endl;
// Create the time sampling type.
Alembic::AbcCoreAbstract::TimeSampling ts(ncache_sampling_rate/6000.0, ncache_start/(ncache_ticks_per_frame*fps));
std::cout << boost::format("getNumStoredTimes = %1%") % ts.getNumStoredTimes() << std::endl;
Alembic::Util::uint32_t tsidx = iParent.getArchive().addTimeSampling(ts);

// Find the required channels
std::string position_channel_name;
std::string id_channel_name;
if (!xml_reader.find_channel_ends_with("_position",position_channel_name))
{
DLOG(INFO) << "BAD" << std::endl;
return false;
}
if (!xml_reader.find_channel_ends_with("_id",id_channel_name))
{
DLOG(INFO) << "BAD" << std::endl;
return false;
}
std::cout << boost::format("position_channel_name = '%1%'") % position_channel_name << std::endl;

// Create the Alembic OPoints schema
// Create our object.
Alembic::AbcGeom::OPoints partsOut( xform, "nCacheParticles", tsidx );

// Add attributes
Alembic::AbcGeom::OPointsSchema &pSchema = partsOut.getSchema();

// Alembic file setup - END


std::string per_initial_frame_full_path = (boost::format("%1%/%2%Frame%3%.%4%") % xml_reader.get_cache_directory() % xml_reader.get_base_cache_name() % frame_index % cache_extension).str();
// std::cout << boost::format("per_initial_frame_full_path = '%1%'") % per_initial_frame_full_path << std::endl;

if (is_mcx)
cache_reader_ptr.reset(new MCXMemoryReader(per_initial_frame_full_path));
else
cache_reader_ptr.reset(new MCMemoryReader(per_initial_frame_full_path));

if (cache_reader_ptr)
{
process_single_sample(cache_reader_ptr,
position_channel_name,
id_channel_name,
pSchema);
}
// sub frame data
if ((ncache_sampling_rate < ncache_ticks_per_frame) && (frame_index!=end_frame))
{
size_t accumulated_ticks = ncache_sampling_rate;
while ((accumulated_ticks < ncache_ticks_per_frame) && ((ncache_ticks_per_frame-accumulated_ticks)>=ncache_sampling_rate) )
{
std::string per_sub_frame_full_path = (boost::format("%1%/%2%Frame%3%Tick%4%.%5%") % xml_reader.get_cache_directory() % xml_reader.get_base_cache_name() % frame_index % accumulated_ticks % cache_extension).str();
// std::cout << boost::format("\t""per_sub_frame_full_path '%1%'") % per_sub_frame_full_path << std::endl;

if (is_mcx)
Expand All @@ -136,9 +250,9 @@ bool nCacheAlembic::process(const std::string& i_alembic_filename,
}

void nCacheAlembic::process_single_sample(const CacheReaderSharedPointerType& i_cache_reader_ptr,
const std::string& i_position_channel_name,
const std::string& i_id_channel_name,
Alembic::AbcGeom::OPointsSchema &o_pSchema)
const std::string& i_position_channel_name,
const std::string& i_id_channel_name,
Alembic::AbcGeom::OPointsSchema& o_pSchema)
{
const nCache::ChannelDataContainer& cdc = i_cache_reader_ptr->get_channels_data();

Expand Down Expand Up @@ -182,3 +296,4 @@ void nCacheAlembic::process_single_sample(const CacheReaderSharedPointerType& i_
std::cout << boost::format("position_data.size() = %1%, id_data.size() = %2%") % position_data.size() % id_data.size() << std::endl;

}

18 changes: 13 additions & 5 deletions lib/nCacheAlembic.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ namespace nCache
public:
nCacheAlembic();
virtual ~nCacheAlembic();
bool process(const std::string& i_alembic_filename, const nCacheLoader& i_ncache_loader);
/*!
* \brief Write a single Alembic file
*/
bool process(const std::string& i_alembic_filename,
const nCacheLoader& i_ncache_loader);
/*!
* \brief Write one Alembic file for each frame
*/
bool process_per_frame(const std::string& i_alembic_format_filename,
const nCacheLoader& i_ncache_loader);
protected:
void process_single_sample(const CacheReaderSharedPointerType& i_cache_reader_ptr,
const std::string& i_position_channel_name,
const std::string& i_id_channel_name,
Alembic::AbcGeom::OPointsSchema &o_pSchema
);
const std::string& i_position_channel_name,
const std::string& i_id_channel_name,
Alembic::AbcGeom::OPointsSchema& o_pSchema);
};

} // namespace nCache
Expand Down
24 changes: 16 additions & 8 deletions lib/nCacheLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,30 @@ void nCacheLoader::process(const std::string& i_ncache_xml_filename)
_ncache_xml_filename = i_ncache_xml_filename;
_xml_reader.read(i_ncache_xml_filename);

size_t num_channels = _xml_reader.getChannels().size();
size_t num_channels = _xml_reader.get_channels().size();
// size_t num_frames = _xml_reader.get_num_frames();
size_t cache_start_time_tick = _xml_reader.get_time_range_start();
size_t cache_end_time_tick = _xml_reader.get_time_range_end();
size_t cache_per_frame_tick = _xml_reader.get_cacheTimePerFrame_TimePerFrame();
size_t start_frame = cache_start_time_tick / cache_per_frame_tick;
size_t end_frame = cache_end_time_tick / cache_per_frame_tick;
size_t sampling_rate = _xml_reader.get_particle_count_sampling_rate();
// std::cout << boost::format("cache_per_frame_tick = %1%") % cache_per_frame_tick << std::endl;
// std::cout << boost::format("sampling_rate = %1%") % sampling_rate << std::endl;
std::cout << boost::format("cache type = '%1%'") % _xml_reader.getCacheType() << std::endl;
std::cout << boost::format("cache format = '%1%'") % _xml_reader.getCacheFormat() << std::endl;
bool is_mcx = _xml_reader.getCacheFormat().compare("mcx") == 0;
std::cout << boost::format("cache_per_frame_tick = %1%") % cache_per_frame_tick << std::endl;
std::cout << boost::format("sampling_rate = %1%") % sampling_rate << std::endl;
std::cout << boost::format("start_frame = %1%") % start_frame << std::endl;
std::cout << boost::format("end_frame = %1%") % end_frame << std::endl;
std::cout << boost::format("cache type = '%1%'") % _xml_reader.get_cache_type() << std::endl;
std::cout << boost::format("cache format = '%1%'") % _xml_reader.get_cache_format() << std::endl;
std::cout << boost::format("cache directory = '%1%'") % _xml_reader.get_cache_directory() << std::endl;
std::cout << boost::format("cache name = '%1%'") % _xml_reader.get_base_cache_name() << std::endl;

bool is_mcx = _xml_reader.get_cache_format().compare("mcx") == 0;
std::string cache_extension("mc");
if (is_mcx)
cache_extension = "mcx";
for (size_t frame_index = start_frame; frame_index <= end_frame; frame_index++)
{
std::string per_initial_frame_mcx_full_path = (boost::format("%1%/%2%Frame%3%.%4%") % _xml_reader.get_cache_directory() % _xml_reader.get_cache_name() % frame_index % cache_extension).str();
std::string per_initial_frame_mcx_full_path = (boost::format("%1%/%2%Frame%3%.%4%") % _xml_reader.get_cache_directory() % _xml_reader.get_base_cache_name() % frame_index % cache_extension).str();
std::cout << boost::format("START processing %1%") % per_initial_frame_mcx_full_path << std::endl;

#ifdef FILE_BASED
Expand Down Expand Up @@ -82,16 +87,19 @@ void nCacheLoader::process(const std::string& i_ncache_xml_filename)
// % frame_index
// % end_frame
// << std::endl;
#ifdef SUBSAMPLING_NOT_WORKING
if ((sampling_rate < cache_per_frame_tick) && (frame_index!=end_frame))
{
size_t accumulated_ticks = sampling_rate;
while ((accumulated_ticks < cache_per_frame_tick) && ((cache_per_frame_tick-accumulated_ticks)>=sampling_rate) )
{
std::string per_sub_frame_mcx_full_path = (boost::format("%1%/%2%Frame%3%Tick%4%.%5%") % _xml_reader.get_cache_directory() % _xml_reader.get_cache_name() % frame_index % accumulated_ticks % cache_extension).str();
std::string per_sub_frame_mcx_full_path = (boost::format("%1%/%2%Frame%3%Tick%4%.%5%") % _xml_reader.get_cache_directory() % _xml_reader.get_base_cache_name() % frame_index % accumulated_ticks % cache_extension).str();
std::cout << boost::format("\t""SUBFRAME PROCESSING %1%") % per_sub_frame_mcx_full_path << std::endl;
std::cout << boost::format("accumulated_ticks = %1%, cache_per_frame_tick = %2%, sampling_rate = %3%") % accumulated_ticks % cache_per_frame_tick % sampling_rate << std::endl;
accumulated_ticks += sampling_rate;
}
}
#endif // SUBSAMPLING_NOT_WORKING
}
// _xml_reader.debugDump();

Expand Down
Loading

0 comments on commit 0d067cd

Please sign in to comment.