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);
130 char vishost[] =
"localhost";
132 sol_sock.
open(vishost, visport);
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 j = 0; j <= sd; j++)
184 for (
int i = 0; i < sd; i++)
186 dx += abs(mat[j*s + i+1] - mat[j*s + i]);
187 dy += abs(mat[(i+1)*s + j] - mat[i*s + j]);
192 for (
int k = 0; k <= sd; k++)
193 for (
int j = 0; j <= sd; j++)
194 for (
int i = 0; i < sd; i++)
196 dx += abs(mat[(k*s + j)*s + i+1] - mat[(k*s + j)*s + i]);
197 dy += abs(mat[(k*s + i+1)*s + j] - mat[(k*s + i)*s + j]);
198 dz += abs(mat[((i+1)*s + j)*s + k] - mat[(i*s + j)*s + k]);
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 = x(1)*N, j = 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.
virtual void Print(std::ostream &out=mfem::out) const
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)
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.
int GetNE() const
Returns number of elements.
int main(int argc, char *argv[])
Geometry::Type GetElementBaseGeometry(int i) const
IntegrationPoint & IntPoint(int i)
Returns a reference to the i-th integration point.
int material(Vector &x, Vector &xmin, Vector &xmax)
virtual void SetAttributes()
int Append(const T &el)
Append element to array, resize if necessary.
virtual void SetCurvature(int order, bool discont=false, int space_dim=-1, int ordering=1)
int MeshGenerator()
Get the mesh generator/type.
GeometryRefiner GlobGeometryRefiner
RefinedGeometry * Refine(Geometry::Type Geom, int Times, int ETimes=1)
void PrintUsage(std::ostream &out) const
int SpaceDimension() const
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)
virtual void Update()
Transform by the Space UpdateMatrix (e.g., on Mesh change).
NURBSExtension * NURBSext
Optional NURBS mesh extension.
void GetElementTransformation(int i, IsoparametricTransformation *ElTr)
void PrintOptions(std::ostream &out) const
int open(const char hostname[], int port)
void SetAttribute(int i, int attr)
Set the attribute of element i.
void GeneralRefinement(const Array< Refinement > &refinements, int nonconforming=-1, int nc_limit=0)
Arbitrary order "L2-conforming" discontinuous finite elements.