Skip to content

I634 progress bar in export#300

Merged
ABSitf merged 17 commits intomasterfrom
i634_progress_bar_in_export
Jul 27, 2022
Merged

I634 progress bar in export#300
ABSitf merged 17 commits intomasterfrom
i634_progress_bar_in_export

Conversation

@ABSitf
Copy link
Contributor

@ABSitf ABSitf commented Jul 22, 2022

No description provided.

Comment on lines +16 to +25
extern "C" {

MRVIEWER_API EMSCRIPTEN_KEEPALIVE int load_files( int count, const char** filenames );

MRVIEWER_API EMSCRIPTEN_KEEPALIVE int save_file( const char* filename );

MRVIEWER_API EMSCRIPTEN_KEEPALIVE int save_scene( const char* filename );

}
#endif
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is not needed in header

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reworked

Comment on lines +12 to +15
const ProgressCallback emptyProgressCallback = [] ( float )
{
return true;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed

Comment on lines +20 to +21
MRMESH_API tl::expected<void, std::string> saveRAW( const std::filesystem::path& path, const ObjectVoxels& voxelsObject,
ProgressCallback callback = emptyProgressCallback );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

callback = {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines +76 to +77
if ( !callback( 0.03f ) )
return tl::make_unexpected( std::string( "Saving canceled" ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add callbacks also for actual saving

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reworked

Comment on lines +28 to +29
if ( !callback( 0.f ) )
return "Saving canceled";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ( callback && !callback( 0.f ) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines +18 to +19
if ( callback && !callback( float( blockIndex * blockSize ) / dataSize ) )
return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need to check callback existance, we checked it in the beggining of this function

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines +8 to +9
#include <fstream>
#include "MRMesh/MRProgressReadWrite.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better add new includes before std ones

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines +150 to +168
saveData.callbackFn = [callback, &saveData] ( float progress )
{
progress = ( saveData.sum + progress * saveData.blockSize ) / saveData.maxSize;
float newProgress = 0.f;
for ( int i = 0; i < 5; ++i )
{
if ( progress < 0.2f )
{
newProgress += progress / 0.2f * 0.7f * ( 1 - newProgress );
break;
}
else
{
progress = ( progress - 0.2f ) / 0.8f;
newProgress += ( 1 - newProgress ) * 0.7f;
}
}
return callback( newProgress );
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add comments about this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added comments

Comment on lines +373 to +399
struct SaveData
{
std::function<bool( float )> callbackFn{};
std::ostream* stream;
size_t sum{ 0 };
size_t blockSize{ 0 };
size_t maxSize{ 0 };
} saveData;
saveData.callbackFn = [callback, &saveData] ( float progress )
{
progress = ( saveData.sum + progress * saveData.blockSize ) / saveData.maxSize;
float newProgress = 0.f;
for ( int i = 0; i < 5; ++i )
{
if ( progress < 0.2f )
{
newProgress += progress / 0.2f * 0.7f * ( 1 - newProgress );
break;
}
else
{
progress = ( progress - 0.2f ) / 0.8f;
newProgress += ( 1 - newProgress ) * 0.7f;
}
}
return callback( newProgress );
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add comments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added comments

* \brief save visual object (mesh, lines, points or voxels) to file
* \param callback - callback function to set progress (for progress bar)
*/
MRVIEWER_API std::string saveObjectToFile( const std::shared_ptr<VisualObject>& obj, const std::filesystem::path& filename,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is returned std::string?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added comment

* \brief save visual object (mesh, lines, points or voxels) to file
* \param callback - callback function to set progress (for progress bar)
*/
MRVIEWER_API std::string saveObjectToFile( const std::shared_ptr<VisualObject>& obj, const std::filesystem::path& filename,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change the first parameter to const VisualObject & obj or even to const Object & obj?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


/**
* \brief write dataSize bytes from data to out stream by blocks blockSize bytes
* \details if progress callback not setted write all data by one block
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setted -> set

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

/**
* \brief write dataSize bytes from data to out stream by blocks blockSize bytes
* \details if progress callback not setted write all data by one block
* \return false if process was canceled (callback setted and return false )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and what is returned if write fails?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this function does not handle errors

for ( auto v : points.validPoints )
{
out << points.points[v] << "\n";
if ( callback && !callback( float( pointIndex ) / pointsNum ) )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is too expensive to run callback for every point! It shall be done on every 1000 point, for example. Here and in other similar places.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines +157 to +165
if ( progress < 0.2f )
{
newProgress += progress / 0.2f * 0.7f * ( 1 - newProgress );
break;
}
else
{
progress = ( progress - 0.2f ) / 0.8f;
newProgress += ( 1 - newProgress ) * 0.7f;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please introduce named constants (fewer - better) instead of 0.2, 0.7, 0.8

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed magic numbers

// calculate full progress in partical-linear scale (we don't know compressed size and it less than real size)
progress = ( saveData.sum + progress * saveData.blockSize ) / saveData.maxSize;
float newProgress = 0.f;
for ( int i = 0; i < 5; ++i )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why 5?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

cVert.r = c.r; cVert.g = c.g; cVert.b = c.b;
out.write( (const char*) &cVert, 15 );
out.write( ( const char* )&cVert, 15 );
if ( callback && !callback( float( v ) / numVertices ) )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

again too expensive to invoke callback on every triangle

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


const bool cancel = !MR::writeByBlocks( out, ( const char* )mesh.points.data(), mesh.points.size() * sizeof( Vector3f ), callback );
if ( cancel )
return tl::make_unexpected( std::string( "Saving canceled" ) );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

canceled or write error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

canceled. writeByBlocks return true only if process was canceled

* \param callback - callback function to set progress (for progress bar)
* \return empty string if no error or error text
*/
MRVIEWER_API std::string saveObjectToFile( const Object& obj, const std::filesystem::path& filename,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let us better return tl::expected<void, std::string> as in MRMeshSave

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

{
out << points.points[v] << "\n";
++pointIndex;
if ( !( pointIndex % 1000 ) && callback && !callback( float( pointIndex ) / pointsNum ) )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better

if ( callback && !( pointIndex % 1000 ) && !callback( float( pointIndex ) / pointsNum ) )

to avoid division when no callback, and in other similar places

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@ABSitf ABSitf merged commit f75d637 into master Jul 27, 2022
@ABSitf ABSitf deleted the i634_progress_bar_in_export branch July 27, 2022 19:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants