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);
71int material(
const ParsePGM &pgm,
int NC,
74int 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;
87 args.
AddOption(&mesh_file,
"-m",
"--mesh",
88 "Input mesh file to shape materials in.");
91 args.
AddOption(&sd,
"-sd",
"--sub-divisions",
92 "Number of element subdivisions for interface detection.");
93 args.
AddOption(&nclimit,
"-ncl",
"--nc-limit",
94 "Level of hanging nodes allowed (-1 = unlimited).");
95 args.
AddOption(&ncolors,
"-nc",
"--num-colors",
96 "Number of colors considered (1-256, based on binning).");
97 args.
AddOption(&aniso,
"-a",
"--aniso",
"-i",
"--iso",
98 "Enable anisotropic refinement of quads and hexes.");
99 args.
AddOption(&visualization,
"-vis",
"--visualization",
"-no-vis",
100 "--no-visualization",
101 "Enable or disable GLVis visualization.");
102 args.
AddOption(&visport,
"-p",
"--send-port",
"Socket for GLVis.");
108 ParsePGM pgm(img_file);
111 Mesh mesh(mesh_file, 1, 1);
134 sol_sock.precision(8);
138 for (
int iter = 0; 1; iter++)
141 for (
int i = 0; i < mesh.
GetNE(); i++)
160 int m =
material(pgm, 256/ncolors, pt, xmin, xmax);
163 if ((
int)matsum != m*(j+1))
180 int dx = 0, dy = 0, dz = 0;
184 for (
int jj = 0; jj <= sd; jj++)
185 for (
int ii = 0; ii < sd; ii++)
187 dx += abs(mat[jj*s + ii+1] - mat[jj*s + ii]);
188 dy += abs(mat[(ii+1)*s + jj] - mat[ii*s + jj]);
193 for (
int kk = 0; kk <= sd; kk++)
194 for (
int jj = 0; jj <= sd; jj++)
195 for (
int ii = 0; ii < sd; ii++)
197 dx += abs(mat[(kk*s + jj)*s + ii+1] - mat[(kk*s + jj)*s + ii]);
198 dy += abs(mat[(kk*s + ii+1)*s + jj] - mat[(kk*s + ii)*s + jj]);
199 dz += abs(mat[((ii+1)*s + jj)*s + kk] - mat[(ii*s + jj)*s + kk]);
203 const int tol = mat.
Size() / 10;
204 if (dx > tol) { type |= 1; }
205 if (dy > tol) { type |= 2; }
206 if (dz > tol) { type |= 4; }
207 if (!type) { type = 7; }
217 sol_sock <<
"solution\n" << mesh << attr;
218 if (iter == 0 && sdim == 2)
220 sol_sock <<
"keys 'RjlmpppppppppppppA*************'\n";
222 if (iter == 0 && sdim == 3)
224 sol_sock <<
"keys 'YYYYYYYYYXXXXXXXmA********8888888pppttt";
225 if (
dim == 3) { sol_sock <<
"iiM"; }
232 cout <<
"Iteration " << iter+1 <<
": mesh has " << mesh.
GetNE() <<
234 if ((iter+1) % 3 == 0)
236 if (!visualization) {
break; }
238 cout <<
"Continue shaping? --> ";
240 if (yn ==
'n' || yn ==
'q') {
break; }
250 for (
int i = 0; i < mesh.
GetNE(); i++)
257 ofstream mesh_ofs(
"mondrian.mesh");
258 mesh_ofs.precision(8);
259 mesh.
Print(mesh_ofs);
262ParsePGM::ParsePGM(
const char *filename)
263 : M(-1), N(-1), depth(-1), pgm8(NULL), pgm16(NULL)
265 ifstream in(filename);
269 MFEM_ABORT(
"Image file not found: " << filename <<
'\n');
282 if (pgm8 != NULL) {
delete [] pgm8; }
283 if (pgm16 != NULL) {
delete [] pgm16; }
286void ParsePGM::ReadMagicNumber(istream &in)
291 MFEM_VERIFY(c ==
'P' && (
p == 2 ||
p == 5),
292 "Invalid PGM file! Unrecognized magic number\""
297void ParsePGM::ReadComments(istream &in)
301 while (in.peek() ==
'#')
303 std::getline(in,buf);
308void ParsePGM::ReadDimensions(istream &in)
316void ParsePGM::ReadDepth(istream &in)
322void ParsePGM::ReadPGM(istream &in)
326 pgm8 =
new char[M*N];
330 pgm16 =
new unsigned short int[M*N];
335 for (
int i=0; i<M*N; i++)
342 for (
int i=0; i<M*N; i++)
352 for (
int i = 0; i < x.
Size(); i++)
354 x(i) = (x(i)-xmin(i))/(xmax(i)-xmin(i));
358 int N = pgm.Height();
360 int i = (int)(x(1)*N), j = (
int)(x(0)*M);
361 if (i == N) { i = N-1; }
362 if (j == M) { j = M-1; }
365 return pgm(i,j)/NC+1;
int Size() const
Return the logical size of the array.
int Append(const T &el)
Append element 'el' to array, resize if necessary.
Class FiniteElementSpace - responsible for providing FEM view of the mesh, mainly managing the set of...
virtual void Update(bool want_transform=true)
Reflect changes in the mesh: update number of DOFs, etc. Also, calculate GridFunction transformation ...
RefinedGeometry * Refine(Geometry::Type Geom, int Times, int ETimes=1)
Class for grid function - Vector with associated FE space.
virtual void Update()
Transform by the Space UpdateMatrix (e.g., on Mesh change).
Class for an integration rule - an Array of IntegrationPoint.
int GetNPoints() const
Returns the number of the points in the integration rule.
IntegrationPoint & IntPoint(int i)
Returns a reference to the i-th integration point.
Arbitrary order "L2-conforming" discontinuous finite elements.
NURBSExtension * NURBSext
Optional NURBS mesh extension.
void GeneralRefinement(const Array< Refinement > &refinements, int nonconforming=-1, int nc_limit=0)
void SetAttribute(int i, int attr)
Set the attribute of element i.
virtual void Print(std::ostream &os=mfem::out, const std::string &comments="") const
int GetNE() const
Returns number of elements.
void GetBoundingBox(Vector &min, Vector &max, int ref=2)
Returns the minimum and maximum corners of the mesh bounding box.
int Dimension() const
Dimension of the reference space used within the elements.
void GetElementTransformation(int i, IsoparametricTransformation *ElTr) const
Builds the transformation defining the i-th element in ElTr. ElTr must be allocated in advance and wi...
int SpaceDimension() const
Dimension of the physical space containing the mesh.
int MeshGenerator() const
Get the mesh generator/type.
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.
Geometry::Type GetElementBaseGeometry(int i) const
virtual void SetAttributes()
Determine the sets of unique attribute values in domain and boundary elements.
void Parse()
Parse the command-line options. Note that this function expects all the options provided through the ...
void PrintUsage(std::ostream &out) const
Print the usage message.
void PrintOptions(std::ostream &out) const
Print the options.
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...
bool Good() const
Return true if the command line options were parsed successfully.
int Size() const
Returns the size of the vector.
int open(const char hostname[], int port)
Open the socket stream on 'port' at 'hostname'.
int material(const ParsePGM &pgm, int NC, Vector &x, Vector &xmin, Vector &xmax)
GeometryRefiner GlobGeometryRefiner
real_t p(const Vector &x, real_t t)