DGtal 2.1.0
Loading...
Searching...
No Matches
exampleAffineGeometry.cpp
Go to the documentation of this file.
1
58#include <iostream>
59#include <vector>
60#include <random>
61#include "DGtal/base/Common.h"
63#include "DGtal/geometry/tools/AffineGeometry.h"
65#include "ConfigExamples.h"
66
67using namespace DGtal;
68
69std::random_device rd;
70std::mt19937 g(rd());
71
72template < typename Point >
73std::vector< Point >
74makeRandomLatticePointsFromDirVectors( int nb, const std::vector< Point>& V )
75{
76 std::uniform_int_distribution<int> U(-10, 10);
77 std::vector< Point > P;
78 int n = V[0].size();
79 int m = V.size();
80 Point A;
81 for ( auto i = 0; i < n; i++ )
82 A[ i ] = U( g );
83 P.push_back( A );
84 for ( auto k = 0; k < nb; k++ )
85 {
86 Point B = A;
87 for ( auto i = 0; i < m; i++ )
88 {
89 int l = U( g );
90 B += l * V[ i ];
91 }
92 P.push_back( B );
93 }
94 std::shuffle( P.begin(), P.end(), g );
95 return P;
96}
97
98template <typename T>
99std::ostream&
100operator<<( std::ostream& out, const std::vector< T >& object )
101{
102 out << "[";
103 for ( auto t : object ) out << " " << t;
104 out << " ]";
105 return out;
106}
107
108template < typename Point >
109bool checkAffineGeometry( int nb, const std::vector< Point>& V )
110{
112 std::cout << "Dimension is " << Point::dimension << "\n";
113 std::cout << "X = " << X << "\n";
114 std::cout << "Expected dimension of affine set of points X is "
115 << (Point::dimension-1) << "\n";
116 std::cout << "AffineDim(X) = " << functions::computeAffineDimension( X ) << "\n";
117 auto I = functions::computeAffineSubset( X );
118 std::cout << "AffineSubset(X) = " << I << "\n";
119 Point o;
120 std::vector<Point> B;
121 functions::getAffineBasis( o, B, X, I );
122 std::cout << "AffineBasis(X) =: p+B = " << o << " + " << B << "\n";
124 std::cout << "Independent(X) =: e = " << e << "\n";
126 std::cout << "Orthogonal(X) =: n = " << n << "\n";
128 std::cout << "Orthogonal(X)/gcd =: ns = " << ns << "\n";
129 for ( auto i = 0; i < B.size(); i++ )
130 std::cout << "B[" << i << "] . ns = " << B[i] << " . " << ns
131 << " == " << B[i].dot(ns) << " (should be 0)\n";
132 return true;
133}
134
135
136int main( int argc, char* argv[] )
137{
138 int dim = argc > 1 ? atoi( argv[ 1 ] ) : 3; // dimension of the space
139 if ( dim > 5 ) dim = 5;
140 if ( dim < 2 ) dim = 2;
141 int nb = argc > 2 ? atoi( argv[ 2 ] ) : 10; // number of points
142 if ( dim == 2 )
143 {
145 std::vector< Point > X = { Point{3,1} };
146 return checkAffineGeometry( nb, X );
147 }
148 if ( dim == 3 )
149 {
151 std::vector< Point > X = { Point{3,1,-1}, Point{-1,4,2} };
152 return checkAffineGeometry( nb, X );
153 }
154 if ( dim == 4 )
155 {
157 std::vector< Point > X = { Point{3,1,-1,2}, Point{-1,4,2,0}, Point{0,5,3,1} };
158 return checkAffineGeometry( nb, X );
159 }
160 if ( dim == 5 )
161 {
163 std::vector< Point > X = { Point{3,1,-1,2,3}, Point{-1,4,2,0,-2}, Point{0,5,3,1,-1}, Point{-4,-3,2,1,0} };
164 return checkAffineGeometry( nb, X );
165 }
166}
Aim: Implements basic operations that will be used in Point and Vector classes.
std::mt19937 g(rd())
std::random_device rd
std::vector< Point > makeRandomLatticePointsFromDirVectors(int nb, const std::vector< Point > &V)
bool checkAffineGeometry(int nb, const std::vector< Point > &V)
DGtal::int64_t computeAffineDimension(const std::vector< TPoint > &X, const double tolerance=1e-12)
void getAffineBasis(TInputPoint &o, std::vector< TPoint > &basis, const std::vector< TInputPoint > &X, const double tolerance=1e-12)
TPoint computeIndependentVector(const std::vector< TPoint > &basis, const double tolerance=1e-12)
static TPoint computeOrthogonalVectorToBasis(const std::vector< TPoint > &basis)
std::vector< std::size_t > computeAffineSubset(const std::vector< TPoint > &X, const double tolerance=1e-12)
TPoint computeSimplifiedVector(const TPoint &v)
DGtal is the top-level namespace which contains all DGtal functions and types.
std::ostream & operator<<(std::ostream &out, const ClosedIntegerHalfPlane< TSpace > &object)
int main()
Definition testBits.cpp:56
MyPointD Point