DGtal  1.1.0
exampleSurfaceATNormals.cpp
Go to the documentation of this file.
1 
30 #include <iostream>
33 #include "ConfigExamples.h"
34 #include "DGtal/base/Common.h"
35 #include "DGtal/helpers/StdDefs.h"
36 #include "DGtal/helpers/Shortcuts.h"
37 #include "DGtal/helpers/ShortcutsGeometry.h"
38 #include "DGtal/dec/ATSolver2D.h"
39 #include "DGtal/dec/DiscreteExteriorCalculusFactory.h"
42 
43 using namespace std;
44 using namespace DGtal;
45 
47 
48 int main( int argc, char** argv )
49 {
50  typedef Z3i::KSpace KSpace;
51  typedef Shortcuts< KSpace > SH3;
53  typedef SH3::Surfel Surfel;
54  typedef SH3::Cell Cell;
56 
57  const double alpha_at = 0.1;
58  const double lambda_at = 0.01;
59  const double e1 = 2.0;
60  const double e2 = 0.25;
61  const double er = 2.0;
62 
63  const string volfile = argc > 1 ? argv[ 1 ] : examplesPath + "samples/Al.100.vol";
64  const double threshold = argc > 2 ? atof( argv[ 2 ] ) : 0.5;
65  trace.beginBlock ( "Load vol file -> build digital surface -> estimate II normals." );
68  auto bimage = SH3::makeBinaryImage( volfile, params );
69  auto K = SH3::getKSpace( bimage, params );
70  auto surface = SH3::makeDigitalSurface( bimage, K, params );
71  auto surfels = SH3::getSurfelRange( surface, params );
72  auto linels = SH3::getCellRange( surface, 1 );
73  auto ii_normals= SHG3::getIINormalVectors( bimage, surfels, params );
74  auto uembedder = SH3::getCellEmbedder( K );
75  auto embedder = SH3::getSCellEmbedder( K );
77  trace.endBlock();
78 
79  trace.beginBlock ( "Creating AT solver for digital surface" );
82  const auto calculus = CalculusFactory::createFromNSCells<2>( surfels.begin(), surfels.end() );
85  ATSolver2D< KSpace > at_solver(calculus, 1);
86  at_solver.initInputVectorFieldU2( ii_normals, surfels.cbegin(), surfels.cend() );
87  at_solver.setUp( alpha_at, lambda_at );
88  at_solver.solveGammaConvergence( e1, e2, er );
90  trace.endBlock();
91 
92  trace.beginBlock ( "Save AT normals as OBJ file" );
94  auto at_normals = ii_normals;
95  at_solver.getOutputVectorFieldU2( at_normals, surfels.cbegin(), surfels.cend() );
97  SH3::RealPoints positions( surfels.size() );
98  std::transform( surfels.cbegin(), surfels.cend(), positions.begin(),
99  [&] (const SH3::SCell& c) { return embedder( c ); } );
100  SH3::Colors colors( surfels.size() );
101  for ( size_t i = 0; i < surfels.size(); i++ )
102  colors[ i ] = SH3::Color( (unsigned char) 255.0*fabs( at_normals[ i ][ 0 ] ),
103  (unsigned char) 255.0*fabs( at_normals[ i ][ 1 ] ),
104  (unsigned char) 255.0*fabs( at_normals[ i ][ 2 ] ) );
105  std::transform( surfels.cbegin(), surfels.cend(), positions.begin(),
106  [&] (const SH3::SCell& c) { return embedder( c ); } );
107 
108  bool ok1 = SH3::saveOBJ( surface, at_normals, SH3::Colors(),
109  "output-surface.obj" );
110  bool ok2 = SH3::saveOBJ( surface, at_normals, colors,
111  "output-surface-at-normals.obj" );
112  bool ok3 = SH3::saveVectorFieldOBJ( positions, at_normals, 0.05, SH3::Colors(),
113  "output-vf-at-normals.obj",
114  SH3::Color( 0, 0, 0 ), SH3::Color::Red );
116  SH3::Scalars features( linels.size() );
117  at_solver.getOutputScalarFieldV0( features, linels.cbegin(), linels.cend(),
118  at_solver.Maximum );
120  SH3::RealPoints f0;
121  SH3::RealVectors f1;
122  for ( size_t i = 0; i < linels.size(); i++ )
123  {
124  if ( features[ i ] < threshold )
125  {
126  const Cell linel = linels[ i ];
127  const Dimension d = * K.uDirs( linel );
128  const Cell p0 = K.uIncident( linel, d, false );
129  const Cell p1 = K.uIncident( linel, d, true );
130  f0.push_back( uembedder( p0 ) );
131  f1.push_back( uembedder( p1 ) - uembedder( p0 ) );
132  }
133  }
134  bool ok4 = SH3::saveVectorFieldOBJ( f0, f1, 0.1, SH3::Colors(),
135  "output-features.obj",
136  SH3::Color( 0, 0, 0 ), SH3::Color::Red );
137 
138  trace.endBlock();
139  return 0;
140 }
141 // //
DGtal::ShortcutsGeometry::getIINormalVectors
static RealVectors getIINormalVectors(CountedPtr< BinaryImage > bimage, const SurfelRange &surfels, const Parameters &params=parametersGeometryEstimation()|parametersKSpace())
Definition: ShortcutsGeometry.h:848
KSpace
Z3i::KSpace KSpace
Definition: sphereCotangentLaplaceOperator.cpp:70
DGtal::ATSolver2D::getOutputVectorFieldU2
void getOutputVectorFieldU2(VectorFieldOutput &output, SurfelRangeConstIterator itB, SurfelRangeConstIterator itE)
Definition: ATSolver2D.h:771
DGtal::ATSolver2D::setUp
void setUp(double a, double l)
Definition: ATSolver2D.h:444
DGtal::Trace::endBlock
double endBlock()
DGtal::Shortcuts::getCellEmbedder
static CanonicCellEmbedder< KSpace > getCellEmbedder(const KSpace &K)
Definition: Shortcuts.h:437
DGtal::Shortcuts::Cell
LightDigitalSurface::Cell Cell
Definition: Shortcuts.h:162
DGtal::Shortcuts::getCellRange
static CellRange getCellRange(Cell2Index &c2i, CountedPtr< ::DGtal::DigitalSurface< TDigitalSurfaceContainer > > surface, const Dimension k)
Definition: Shortcuts.h:1398
DGtal::Color
Structure representing an RGB triple with alpha component.
Definition: Color.h:67
RealVector
Z3i::RealVector RealVector
Definition: sphereCotangentLaplaceOperator.cpp:71
DGtal::trace
Trace trace
Definition: Common.h:150
DGtal::Shortcuts::Colors
std::vector< Color > Colors
Definition: Shortcuts.h:191
K
KSpace K
Definition: testCubicalComplex.cpp:62
DGtal::Dimension
DGtal::uint32_t Dimension
Definition: Common.h:133
DGtal::Shortcuts::makeDigitalSurface
static CountedPtr< DigitalSurface > makeDigitalSurface(CountedPtr< TPointPredicate > bimage, const KSpace &K, const Parameters &params=parametersDigitalSurface())
Definition: Shortcuts.h:1208
DGtal::Trace::beginBlock
void beginBlock(const std::string &keyword="")
DGtal::Shortcuts
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
Definition: Shortcuts.h:105
DGtal::Shortcuts::saveVectorFieldOBJ
static bool saveVectorFieldOBJ(const RealPoints &positions, const RealVectors &vf, double thickness, const Colors &diffuse_colors, std::string objfile, const Color &ambient_color=Color(32, 32, 32), const Color &diffuse_color=Color(200, 200, 255), const Color &specular_color=Color::White)
Definition: Shortcuts.h:1938
DGtal::Shortcuts::RealVectors
std::vector< RealVector > RealVectors
Definition: Shortcuts.h:179
DGtal::ATSolver2D::solveGammaConvergence
bool solveGammaConvergence(double eps1=2.0, double eps2=0.25, double epsr=2.0, bool compute_smallest_epsilon_map=false, double n_oo_max=1e-4, unsigned int iter_max=10)
Definition: ATSolver2D.h:656
SHG3
ShortcutsGeometry< Z3i::KSpace > SHG3
Definition: testIntegralInvariantShortcuts.cpp:47
DGtal::Color::Red
static const Color Red
Definition: Color.h:400
SH3
Shortcuts< Z3i::KSpace > SH3
Definition: testIntegralInvariantShortcuts.cpp:46
DGtal::Shortcuts::defaultParameters
static Parameters defaultParameters()
Definition: Shortcuts.h:202
DGtal::ATSolver2D
Aim: This class solves Ambrosio-Tortorelli functional on a two-dimensional digital space (a 2D grid o...
Definition: ATSolver2D.h:91
DGtal
DGtal is the top-level namespace which contains all DGtal functions and types.
Definition: ClosedIntegerHalfPlane.h:49
DGtal::ATSolver2D::Maximum
@ Maximum
compute maximum value at cell vertices
Definition: ATSolver2D.h:105
DGtal::Shortcuts::Scalars
std::vector< Scalar > Scalars
Definition: Shortcuts.h:178
DGtal::DiscreteExteriorCalculusFactory
Aim: This class provides static members to create DEC structures from various other DGtal structures.
Definition: DiscreteExteriorCalculusFactory.h:62
DGtal::Shortcuts::getSCellEmbedder
static CanonicSCellEmbedder< KSpace > getSCellEmbedder(const KSpace &K)
Definition: Shortcuts.h:445
main
int main(int argc, char **argv)
Definition: exampleSurfaceATNormals.cpp:48
DGtal::ShortcutsGeometry
Aim: This class is used to simplify shape and surface creation. With it, you can create new shapes an...
Definition: ShortcutsGeometry.h:77
DGtal::Shortcuts::SCell
LightDigitalSurface::SCell SCell
Definition: Shortcuts.h:163
DGtal::KhalimskySpaceND::uDirs
DirIterator uDirs(const Cell &p) const
Given an unsigned cell [p], returns an iterator to iterate over each coordinate the cell spans.
DGtal::Shortcuts::RealPoints
std::vector< RealPoint > RealPoints
Definition: Shortcuts.h:180
DGtal::Shortcuts::getKSpace
static KSpace getKSpace(const Point &low, const Point &up, Parameters params=parametersKSpace())
Definition: Shortcuts.h:331
DGtal::Shortcuts::Surfel
LightDigitalSurface::Surfel Surfel
Definition: Shortcuts.h:161
DGtal::KhalimskySpaceND::uIncident
Cell uIncident(const Cell &c, Dimension k, bool up) const
Return the forward or backward unsigned cell incident to [c] along axis [k], depending on [up].
DGtal::PointVector
Aim: Implements basic operations that will be used in Point and Vector classes.
Definition: PointVector.h:593
DGtal::ShortcutsGeometry::defaultParameters
static Parameters defaultParameters()
Definition: ShortcutsGeometry.h:209
Cell
KSpace::Cell Cell
Definition: testCubicalComplex.cpp:56
DGtal::Shortcuts::saveOBJ
static bool saveOBJ(CountedPtr< ::DGtal::DigitalSurface< TDigitalSurfaceContainer > > digsurf, const TCellEmbedder &embedder, const RealVectors &normals, const Colors &diffuse_colors, std::string objfile, const Color &ambient_color=Color(32, 32, 32), const Color &diffuse_color=Color(200, 200, 255), const Color &specular_color=Color::White)
Definition: Shortcuts.h:1738
DGtal::Shortcuts::getSurfelRange
static SurfelRange getSurfelRange(CountedPtr< ::DGtal::DigitalSurface< TDigitalSurfaceContainer > > surface, const Parameters &params=parametersDigitalSurface())
Definition: Shortcuts.h:1546
DGtal::Shortcuts::makeBinaryImage
static CountedPtr< BinaryImage > makeBinaryImage(Domain shapeDomain)
Definition: Shortcuts.h:560
DGtal::ATSolver2D::initInputVectorFieldU2
void initInputVectorFieldU2(const VectorFieldInput &input, SurfelRangeConstIterator itB, SurfelRangeConstIterator itE, bool normalize=false)
Definition: ATSolver2D.h:288
DGtal::ATSolver2D::getOutputScalarFieldV0
void getOutputScalarFieldV0(ScalarFieldOutput &output, CellRangeConstIterator itB, CellRangeConstIterator itE, CellOutputPolicy policy=CellOutputPolicy::Average)
Definition: ATSolver2D.h:823
DGtal::KhalimskyCell< dim, Integer >
DGtal::KhalimskySpaceND
Aim: This class is a model of CCellularGridSpaceND. It represents the cubical grid as a cell complex,...
Definition: KhalimskySpaceND.h:394