46 ParsePGM(
const char *filename);
49 int Height()
const {
return N; }
50 int Width()
const {
return M; }
52 int operator()(
int i,
int j)
const 53 {
return int((pgm8) ? pgm8[M*i+j] : pgm16[M*i+j]); }
60 unsigned short int *pgm16;
62 void ReadMagicNumber(istream &in);
63 void ReadComments(istream &in);
64 void ReadDimensions(istream &in);
65 void ReadDepth(istream &in);
66 void ReadPGM(istream &in);
71 int material(
const ParsePGM &pgm,
int NC,
74 int main(
int argc,
char *argv[])
76 const char *mesh_file =
"../../data/inline-quad.mesh";
77 const char *img_file =
"australia.pgm";
82 bool visualization = 1;
86 args.
AddOption(&mesh_file,
"-m",
"--mesh",
87 "Input mesh file to shape materials in.");
90 args.
AddOption(&sd,
"-sd",
"--sub-divisions",
91 "Number of element subdivisions for interface detection.");
92 args.
AddOption(&nclimit,
"-ncl",
"--nc-limit",
93 "Level of hanging nodes allowed (-1 = unlimited).");
94 args.
AddOption(&ncolors,
"-nc",
"--num-colors",
95 "Number of colors considered (1-256, based on binning).");
96 args.
AddOption(&aniso,
"-a",
"--aniso",
"-i",
"--iso",
97 "Enable anisotropic refinement of quads and hexes.");
98 args.
AddOption(&visualization,
"-vis",
"--visualization",
"-no-vis",
100 "Enable or disable GLVis visualization.");
106 ParsePGM pgm(img_file);
109 Mesh mesh(mesh_file, 1, 1);
133 sol_sock.precision(8);
137 for (
int iter = 0; 1; iter++)
140 for (
int i = 0; i < mesh.
GetNE(); i++)
159 int m =
material(pgm, 256/ncolors, pt, xmin, xmax);
162 if ((
int)matsum != m*(j+1))
179 int dx = 0, dy = 0, dz = 0;
183 for (
int jj = 0; jj <= sd; jj++)
184 for (
int ii = 0; ii < sd; ii++)
186 dx += abs(mat[jj*
s + ii+1] - mat[jj*
s + ii]);
187 dy += abs(mat[(ii+1)*
s + jj] - mat[ii*
s + jj]);
192 for (
int kk = 0; kk <= sd; kk++)
193 for (
int jj = 0; jj <= sd; jj++)
194 for (
int ii = 0; ii < sd; ii++)
196 dx += abs(mat[(kk*
s + jj)*
s + ii+1] - mat[(kk*
s + jj)*
s + ii]);
197 dy += abs(mat[(kk*
s + ii+1)*
s + jj] - mat[(kk*
s + ii)*
s + jj]);
198 dz += abs(mat[((ii+1)*
s + jj)*
s + kk] - mat[(ii*
s + jj)*
s + kk]);
202 const int tol = mat.Size() / 10;
203 if (dx > tol) { type |= 1; }
204 if (dy > tol) { type |= 2; }
205 if (dz > tol) { type |= 4; }
206 if (!type) { type = 7; }
216 sol_sock <<
"solution\n" << mesh << attr;
217 if (iter == 0 && sdim == 2)
219 sol_sock <<
"keys 'RjlmpppppppppppppA*************'\n";
221 if (iter == 0 && sdim == 3)
223 sol_sock <<
"keys 'YYYYYYYYYXXXXXXXmA********8888888pppttt";
224 if (
dim == 3) { sol_sock <<
"iiM"; }
231 cout <<
"Iteration " << iter+1 <<
": mesh has " << mesh.
GetNE() <<
233 if ((iter+1) % 3 == 0)
235 if (!visualization) {
break; }
237 cout <<
"Continue shaping? --> ";
239 if (yn ==
'n' || yn ==
'q') {
break; }
249 for (
int i = 0; i < mesh.
GetNE(); i++)
256 ofstream mesh_ofs(
"mondrian.mesh");
257 mesh_ofs.precision(8);
258 mesh.
Print(mesh_ofs);
261 ParsePGM::ParsePGM(
const char *filename)
262 : M(-1), N(-1), depth(-1), pgm8(NULL), pgm16(NULL)
264 ifstream in(filename);
268 MFEM_ABORT(
"Image file not found: " << filename <<
'\n');
279 ParsePGM::~ParsePGM()
281 if (pgm8 != NULL) {
delete [] pgm8; }
282 if (pgm16 != NULL) {
delete [] pgm16; }
285 void ParsePGM::ReadMagicNumber(istream &in)
290 MFEM_VERIFY(c ==
'P' && (
p == 2 ||
p == 5),
291 "Invalid PGM file! Unrecognized magic number\"" 296 void ParsePGM::ReadComments(istream &in)
300 while (in.peek() ==
'#')
302 std::getline(in,buf);
307 void ParsePGM::ReadDimensions(istream &in)
315 void ParsePGM::ReadDepth(istream &in)
321 void ParsePGM::ReadPGM(istream &in)
325 pgm8 =
new char[M*N];
329 pgm16 =
new unsigned short int[M*N];
334 for (
int i=0; i<M*N; i++)
341 for (
int i=0; i<M*N; i++)
351 for (
int i = 0; i < x.
Size(); i++)
353 x(i) = (x(i)-xmin(i))/(xmax(i)-xmin(i));
357 int N = pgm.Height();
359 int i = (int)(x(1)*N), j = (
int)(x(0)*M);
360 if (i == N) { i = N-1; }
361 if (j == M) { j = M-1; }
364 return pgm(i,j)/NC+1;
int GetNPoints() const
Returns the number of the points in the integration rule.
Class for an integration rule - an Array of IntegrationPoint.
Class for grid function - Vector with associated FE space.
virtual void Update(bool want_transform=true)
Reflect changes in the mesh: update number of DOFs, etc. Also, calculate GridFunction transformation ...
int material(const ParsePGM &pgm, int NC, Vector &x, Vector &xmin, Vector &xmax)
void PrintOptions(std::ostream &out) const
Print the options.
Geometry::Type GetElementBaseGeometry(int i) const
void PrintUsage(std::ostream &out) const
Print the usage message.
void GetBoundingBox(Vector &min, Vector &max, int ref=2)
Returns the minimum and maximum corners of the mesh bounding box.
int Size() const
Returns the size of the vector.
bool Good() const
Return true if the command line options were parsed successfully.
IntegrationPoint & IntPoint(int i)
Returns a reference to the i-th integration point.
GeometryRefiner GlobGeometryRefiner
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
virtual void SetAttributes()
int Append(const T &el)
Append element 'el' to array, resize if necessary.
virtual void SetCurvature(int order, bool discont=false, int space_dim=-1, int ordering=1)
Set the curvature of the mesh nodes using the given polynomial degree.
int MeshGenerator()
Get the mesh generator/type.
RefinedGeometry * Refine(Geometry::Type Geom, int Times, int ETimes=1)
double p(const Vector &x, double t)
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
void AddOption(bool *var, const char *enable_short_name, const char *enable_long_name, const char *disable_short_name, const char *disable_long_name, const char *description, bool required=false)
Add a boolean option and set 'var' to receive the value. Enable/disable tags are used to set the bool...
virtual void Update()
Transform by the Space UpdateMatrix (e.g., on Mesh change).
int SpaceDimension() const
int GetNE() const
Returns number of elements.
NURBSExtension * NURBSext
Optional NURBS mesh extension.
int main(int argc, char *argv[])
void GetElementTransformation(int i, IsoparametricTransformation *ElTr)
int open(const char hostname[], int port)
Open the socket stream on 'port' at 'hostname'.
void SetAttribute(int i, int attr)
Set the attribute of element i.
virtual void Print(std::ostream &os=mfem::out) const
void GeneralRefinement(const Array< Refinement > &refinements, int nonconforming=-1, int nc_limit=0)
Arbitrary order "L2-conforming" discontinuous finite elements.