DGtal  1.1.0
io/viewDualSurface.cpp

Example of viewing dual surfaces in Viewer3D.

#include <iostream>
#include <algorithm>
#include "DGtal/base/Common.h"
#include "DGtal/helpers/StdDefs.h"
#include "DGtal/topology/helpers/Surfaces.h"
#include "ConfigExamples.h"
#include "DGtal/io/viewers/Viewer3D.h"
using namespace std;
using namespace DGtal;
using namespace Z3i;
template <typename Vector>
Vector wedge( const Vector & v1, const Vector & v2 )
{
return Vector( v1[ 1 ] * v2[ 2 ] - v1[ 2 ] * v2[ 1 ],
v1[ 2 ] * v2[ 0 ] - v1[ 0 ] * v2[ 2 ],
v1[ 0 ] * v2[ 1 ] - v1[ 1 ] * v2[ 0 ] );
}
template <typename Vector>
struct LessThanOnFace
{
Vector N; // expected normal
Vector P; // origin or first point
const std::vector< Vector > & pts;
inline LessThanOnFace( const Vector & aN, const Vector & aP,
const std::vector< Vector > & aPts )
: N( aN ), P( aP ), pts( aPts )
{}
inline bool operator()( unsigned int i1, unsigned int i2 ) const
{
return N.dot( wedge( pts[ i1 ] - P, pts[ i2 ] - P ) ) > 0;
}
};
// Very naive convex hull algorithm. O(n^4) complexity ! But very
// short. Takes care also of polygonal faces.
template <typename Vector>
( std::vector< std::vector< unsigned int > > & indices,
const std::vector<Vector> & points, bool left_handed )
{
typedef typename Vector::Component Scalar;
// Checks all triplets of points.
std::vector< unsigned int > aFace;
for ( unsigned int i1 = 0; i1 < points.size(); ++i1 )
for ( unsigned int i2 = 0; i2 < points.size(); ++i2 )
if ( i1 != i2 )
for ( unsigned int i3 = i1 > i2 ? i1+1 : i2+1; i3 < points.size(); ++i3 )
{
Vector P12 = points[ i2 ] - points[ i1 ];
Vector P13 = points[ i3 ] - points[ i1 ];
Vector N = wedge( P12, P13 );
if ( N == Vector::zero ) continue;
for ( unsigned int i4 = 0; i4 < points.size(); ++i4 )
{
Vector P14 = points[ i4 ] - points[ i1 ];
Scalar c = N.dot( P14 );
if ( c == 0 ) aFace.push_back( i4 );
else if ( ( left_handed && ( c > 0 ) )
|| ( ! left_handed && ( c < 0 ) ) )
}
if ( nbBadPos == 0 )
{
LessThanOnFace<Vector> LTOF( N, points[ aFace[ 0 ] ], points );
std::sort( ++aFace.begin(), aFace.end(), LTOF );
indices.push_back( aFace );
}
aFace.clear();
}
// purge faces.
for ( unsigned int i = 0; i < indices.size(); ++i )
{
auto s = indices[ i ].size();
for ( unsigned int j = i+1; j < indices.size(); )
{
if ( indices[ j ].size() == s )
{
bool equal = true;
for ( unsigned int k = 0; equal && ( k < s ); ++k )
if ( indices[ i ][ k ] != indices[ j ][ k ] )
equal = false;
if ( equal )
{
std::swap( indices[ j ], indices.back() );
indices.pop_back();
}
else
++j;
}
else ++j;
}
}
}
double rescale( double x )
{
return ( x - 1.0 ) * 0.5 + 0.5;
}
template <typename Viewer,
typename Vector>
( Viewer & viewer,
const DGtal::Color & color,
const std::vector< std::vector< unsigned int > > & indices,
const std::vector<Vector> & points )
{
typedef typename Viewer::RealPoint RealPoint;
std::vector<RealPoint> pts3d;
DGtal::Color fillColorSave = viewer.getFillColor();
for ( unsigned int f = 0; f < indices.size(); ++f )
{
pts3d.clear();
for ( unsigned int v = 0; v < indices[ f ].size(); ++v )
{
unsigned int i = indices[ f ][ v ];
P[0] = rescale( points[ i ][ 0 ] );
P[1] = rescale( points[ i ][ 1 ] );
P[2] = rescale( points[ i ][ 2 ] );
pts3d.push_back( P );
}
viewer.setFillColor(color);
}
}
template <typename Vector>
unsigned int dim( const Vector & z )
{
unsigned int d = 0;
for ( unsigned int i = 0; i < Vector::dimension; ++i )
if ( ( z[ i ] % 2 ) == 1 ) ++d;
return d;
}
template <typename Vector>
unsigned int openDim( const Vector & z )
{
for ( unsigned int i = 0; i < Vector::dimension; ++i )
if ( ( z[ i ] % 2 ) == 1 ) return i;
return Vector::dimension;
}
template <typename Vector>
Vector lower( const Vector & z, unsigned int k )
{
Vector z2( z );
--z2[ k ];
return z2;
}
template <typename Vector>
Vector upper( const Vector & z, unsigned int k )
{
Vector z2( z );
++z2[ k ];
return z2;
}
template <typename Vector>
unsigned int nbLighted( std::map< Vector, bool > & f,
const Vector & z )
{ // z of dim >=2
unsigned int d = dim( z );
if ( d == 0 ) return f[ z ] ? 1 : 0;
unsigned int i = openDim( z );
return nbLighted( f, lower( z, i ) )
+ nbLighted( f, upper( z, i ) );
}
// Most similar to convex hull... but not exactly, e.g. cfg 31.
template <typename Vector>
bool lightBetween( std::map< Vector, bool > & f,
const Vector & z )
{
unsigned int d = dim( z );
if ( d == 0 ) return f[ z ];
else if ( d == 1 )
{
unsigned int i = openDim( z );
return f[ lower( z, i ) ] || f[ upper( z, i ) ];
}
else
{
Vector v1, v2;
for ( unsigned int i = 0; i < Vector::dimension; ++i )
{
v1[ i ] = ( ( z[ i ] % 2 ) == 1 ) ? z[ i ] - 1 : z[ i ];
v2[ i ] = ( ( z[ i ] % 2 ) == 1 ) ? z[ i ] + 1 : z[ i ];
}
Domain domain( v1, v2 );
for ( Domain::ConstIterator it = domain.begin(), itE = domain.end();
it != itE; ++it )
{
if ( *it == z ) break;
Point zp = z*2 - *it;
// std::cerr << *it << " <--> " << zp << std::endl;
if ( lightBetween( f, *it ) && lightBetween( f, zp ) )
return true;
}
return false;
}
}
template <typename Vector>
bool lightMax( std::map< Vector, bool > & f,
const Vector & z )
{
unsigned int d = dim( z );
if ( d == 0 ) return f[ z ];
else if ( d == 1 )
{
unsigned int i = openDim( z );
return f[ lower( z, i ) ] || f[ upper( z, i ) ];
}
else // if ( d > 1 )
{
unsigned int n = nbLighted( f, z );
return n >= 2;
}
}
template <typename Vector>
bool lightMinMax( std::map< Vector, bool > & f,
const Vector & z )
{
unsigned int d = dim( z );
if ( d == 0 ) return f[ z ];
else
{
Vector tmp( z );
bool val = true;
for ( unsigned i = 0; i < d; ++i )
{
unsigned int k = openDim( tmp );
tmp = lower( tmp, k );
val = val && ( lightMinMax( f, lower( z, k ) ) || lightMinMax( f, upper( z, k ) ) );
}
return val;
}
}
template <typename Vector>
bool lightMaxMin( std::map< Vector, bool > & f,
const Vector & z )
{
unsigned int d = dim( z );
if ( d == 0 ) return f[ z ];
else
{
Vector tmp( z );
bool val = false;
for ( unsigned i = 0; i < d; ++i )
{
unsigned int k = openDim( tmp );
tmp = lower( tmp, k );
val = val || ( lightMaxMin( f, lower( z, k ) ) && lightMaxMin( f, upper( z, k ) ) );
}
return val;
}
}
template <typename Vector>
bool lightEpsilon( std::map< Vector, bool > & f,
const Vector & z,
unsigned int epsilon )
{
unsigned int d = dim( z );
if ( d == 0 ) return f[ z ];
else
{
Vector tmp( z );
bool eps_d = ( ( epsilon >> (d-1)) & 1 ) != 0;
bool val = eps_d ? true : false;
for ( unsigned i = 0; i < d; ++i )
{
unsigned int k = openDim( tmp );
tmp = lower( tmp, k );
if ( eps_d )
val = val && ( lightEpsilon( f, lower( z, k ), epsilon )
|| lightEpsilon( f, upper( z, k ), epsilon ) );
else
val = val || ( lightEpsilon( f, lower( z, k ), epsilon )
&& lightEpsilon( f, upper( z, k ), epsilon ) );
}
return val;
}
}
template <typename Vector>
void fillCfg( std::map< Vector, bool > & f,
const Vector & z,
unsigned int cfg )
{
unsigned int d = dim( z );
if ( d == 0 )
{
f[ z ] = (cfg == 1);
//std::cerr << "f[" << z << "] = " << f[ z ] << std::endl;
}
else
{
unsigned n = 1 << ( d - 1 );
unsigned int cfgLow = 0;
unsigned int cfgUp = 0;
for ( unsigned int j = 0; j < n; ++j )
{
cfgLow += ( cfg & 1 ) << j;
cfg >>= 1;
cfgUp += ( cfg & 1 ) << j;
cfg >>= 1;
}
unsigned int i = openDim( z );
fillCfg( f, lower( z, i ), cfgLow );
fillCfg( f, upper( z, i ), cfgUp );
}
}
template <typename Vector>
void localDualVolume( std::vector<Vector> & points,
std::map< Vector, bool > & f,
const Vector & z )
{
points.clear();
Z3i::Domain domain( z, z + Vector::diagonal(1) );
it != itE; ++it )
{
if ( f[ *it ] ) points.push_back( *it );
}
}
template <typename Vector>
struct ConfigPointPredicate
{
std::map< Vector, bool > & fct;
Vector base;
ConfigPointPredicate( std::map< Vector, bool > & aFct, Vector aBase )
: fct( aFct ), base( aBase ) {}
bool operator()( const Vector & p ) const
{
return fct[ p * 2 + base];
}
};
int main( int argc, char** argv )
{
typedef KSpace::CellSet CellSet;
QApplication application(argc,argv);
KSpace KS;
viewer.show();
DGtal::Color fillColor( 200, 200, 220, 255 );
DGtal::Color surfelColor( 255, 0, 0, 150 );
DGtal::Color voxelColor( 150, 150, 0, 150 );
std::vector<Vector> pts;
unsigned int cfg = argc > 1 ? atoi( argv[1] ) : 0;
unsigned int cfg2 = argc > 2 ? atoi( argv[2] ) : 255;
std::map< Vector, bool > f;
for ( unsigned int y = 0; (y < 16) && (cfg <= cfg2); ++y )
for ( unsigned int x = 0; (x < 16) && (cfg <= cfg2); ++x, ++cfg )
{
Vector offset( x*6, y*6, 0 );
fillCfg( f, offset + Vector( 1, 1, 1 ), cfg );
Domain domain( offset + Vector( 0, 0, 0), offset + Vector( 2, 2, 2 ) );
K.init( Vector( 0, 0, 0), Vector( 2, 2, 2 ), true );
ConfigPointPredicate<Vector> cpp( f, offset );
CellSet aBoundary;
Surfaces<KSpace>::uMakeBoundary( aBoundary, K, cpp, Vector( 0, 0, 0), Vector( 1, 1, 1 ) );
for ( CellSet::const_iterator it = aBoundary.begin(), itE = aBoundary.end();
it != itE; ++it )
{
viewer << CustomColors3D( surfelColor, surfelColor );
viewer << KS.uTranslation( *it, offset/2 );
}
for ( Domain::ConstIterator it = domain.begin(), itE = domain.end();
it != itE; ++it )
{
// lightEpsilon( f, *it, 5 ); // {1,-1,1}=5 // interesting
f[ *it ] = lightBetween( f, *it );
}
viewer << CustomColors3D( DGtal::Color( 255, 0, 0, 255 ), fillColor );
std::vector< std::vector< unsigned int > > indices;
Domain domain2( offset + Vector( 0, 0, 0), offset + Vector( 1, 1, 1 ) );
for ( Domain::ConstIterator it = domain.begin(), itE = domain.end();
it != itE; ++it )
{
localDualVolume( pts, f, *it );
indices.clear();
naiveConvexHull( indices, pts, false ); // right_handed
viewPolygons( viewer, fillColor, indices, pts );
}
}
viewer << Viewer3D<>::updateDisplay;
return application.exec();
}
lightMaxMin
bool lightMaxMin(std::map< Vector, bool > &f, const Vector &z)
Definition: viewDualSurface.cpp:289
DGtal::Surfaces
Aim: A utility class for constructing surfaces (i.e. set of (n-1)-cells).
Definition: Surfaces.h:79
nbLighted
unsigned int nbLighted(std::map< Vector, bool > &f, const Vector &z)
Definition: viewDualSurface.cpp:205
DGtal::HyperRectDomain< Space >
lower
Vector lower(const Vector &z, unsigned int k)
Definition: viewDualSurface.cpp:190
DGtal::Color
Structure representing an RGB triple with alpha component.
Definition: Color.h:67
DGtal::KhalimskySpaceND::init
bool init(const Point &lower, const Point &upper, bool isClosed)
Specifies the upper and lower bounds for the maximal cells in this space.
K
KSpace K
Definition: testCubicalComplex.cpp:62
DGtal::Display3D::getFillColor
virtual DGtal::Color getFillColor()
dim
unsigned int dim(const Vector &z)
Definition: viewDualSurface.cpp:174
DGtal::KhalimskySpaceND::CellSet
std::set< Cell > CellSet
Preferred type for defining a set of Cell(s).
Definition: KhalimskySpaceND.h:444
lightMinMax
bool lightMinMax(std::map< Vector, bool > &f, const Vector &z)
Definition: viewDualSurface.cpp:270
DGtal::Viewer3D
Definition: Viewer3D.h:133
localDualVolume
void localDualVolume(std::vector< Vector > &points, std::map< Vector, bool > &f, const Vector &z)
Definition: viewDualSurface.cpp:366
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition: ClosedIntegerHalfPlane.h:49
openDim
unsigned int openDim(const Vector &z)
Definition: viewDualSurface.cpp:183
DGtal::HyperRectDomain::end
const ConstIterator & end() const
Definition: HyperRectDomain.h:201
Vector
FreemanChain< int >::Vector Vector
Definition: testCombinDSS.cpp:60
main
int main(int argc, char **argv)
Definition: testArithmeticDSS-benchmark.cpp:147
void addPolygon(const std::vector< RealPoint > &vertices)
DGtal::Display3D::setFillColor
virtual void setFillColor(DGtal::Color aColor)
DGtal::HyperRectDomain_Iterator
Iterator for HyperRectDomain.
Definition: HyperRectDomain_Iterator.h:154
upper
Vector upper(const Vector &z, unsigned int k)
Definition: viewDualSurface.cpp:197
domain
Domain domain
Definition: testProjection.cpp:88
DGtal::PointVector
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
fillCfg
void fillCfg(std::map< Vector, bool > &f, const Vector &z, unsigned int cfg)
Definition: viewDualSurface.cpp:337
DGtal::Viewer3D::show
virtual void show()
Overload QWidget method in order to add a call to updateList() method (to ensure that the lists are w...
viewPolygons
void viewPolygons(Viewer &viewer, const DGtal::Color &color, const std::vector< std::vector< unsigned int > > &indices, const std::vector< Vector > &points)
Definition: viewDualSurface.cpp:148
lightBetween
bool lightBetween(std::map< Vector, bool > &f, const Vector &z)
Definition: viewDualSurface.cpp:218
naiveConvexHull
void naiveConvexHull(std::vector< std::vector< unsigned int > > &indices, const std::vector< Vector > &points, bool left_handed)
Definition: viewDualSurface.cpp:81
DGtal::HyperRectDomain::begin
const ConstIterator & begin() const
Definition: HyperRectDomain.h:176
Point
MyPointD Point
Definition: testClone2.cpp:383
RealPoint
Z2i::RealPoint RealPoint
Definition: testAstroid2D.cpp:46
rescale
double rescale(double x)
Definition: viewDualSurface.cpp:140
DGtal::KhalimskySpaceND::uTranslation
Cell uTranslation(const Cell &p, const Vector &vec) const
Add the vector [vec] to [p].
lightMax
bool lightMax(std::map< Vector, bool > &f, const Vector &z)
Definition: viewDualSurface.cpp:253
lightEpsilon
bool lightEpsilon(std::map< Vector, bool > &f, const Vector &z, unsigned int epsilon)
Definition: viewDualSurface.cpp:309
DGtal::KhalimskySpaceND
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
Definition: KhalimskySpaceND.h:394
wedge
Vector wedge(const Vector &v1, const Vector &v2)
Definition: viewDualSurface.cpp:54