NEMoSys  0.63.0
A modular, extensible resource with robust automated mesh generation, mesh quality analysis, adaptive mesh refinement, and data transfer between arbitrary meshes.
ep16Post.C
Go to the documentation of this file.
1 /*******************************************************************************
2 * Promesh *
3 * Copyright (C) 2022, IllinoisRocstar LLC. All rights reserved. *
4 * *
5 * Promesh is the property of IllinoisRocstar LLC. *
6 * *
7 * IllinoisRocstar LLC *
8 * Champaign, IL *
9 * www.illinoisrocstar.com *
10 * promesh@illinoisrocstar.com *
11 *******************************************************************************/
12 /*******************************************************************************
13 * This file is part of Promesh *
14 * *
15 * This version of Promesh is free software: you can redistribute it and/or *
16 * modify it under the terms of the GNU Lesser General Public License as *
17 * published by the Free Software Foundation, either version 3 of the License, *
18 * or (at your option) any later version. *
19 * *
20 * Promesh is distributed in the hope that it will be useful, but WITHOUT ANY *
21 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more *
23 * details. *
24 * *
25 * You should have received a copy of the GNU Lesser General Public License *
26 * along with this program. If not, see <https://www.gnu.org/licenses/>. *
27 * *
28 *******************************************************************************/
29 #ifdef HAVE_EPIC
30 
31 #include "AuxiliaryFunctions.H"
34 
35 #include <iostream>
36 #include <fstream>
37 #include <memory>
38 
39 #include <point.h>
40 #include <kmeans.h>
41 #include <csv.h>
42 
43 namespace NEM {
44 
45 namespace EPC {
46 
47 
48 ep16Post *ep16Post::readJSON(const std::string &ifname)
49 {
50  std::cout << "Reading JSON file" << std::endl;
51  std::ifstream inputStream(ifname);
52  if (!inputStream.good() || nemAux::find_ext(ifname) != ".json")
53  {
54  std::cerr << "Error opening file " << ifname << std::endl;
55  exit(1);
56  }
57  if (nemAux::find_ext(ifname) != ".json")
58  {
59  std::cerr << "Input File must be in .json format" << std::endl;
60  exit(1);
61  }
62 
63  jsoncons::json inputjson;
64  inputStream >> inputjson;
65 
66  // checking if array
67  if (inputjson.is_array())
68  {
69  std::cerr
70  << "Warning: Input is an array. Only first element will be processed"
71  << std::endl;
72  inputjson = inputjson[0];
73  }
74 
75  ep16Post *ep = new ep16Post(inputjson);
76  ep->readJSON();
77  return ep;
78 }
79 
80 
81 ep16Post *ep16Post::readJSON(const jsoncons::json &inputjson, int& ret)
82 {
83  ep16Post *ep = new ep16Post(inputjson);
84  ret = ep->readJSON();
85  return ep;
86 }
87 
88 int ep16Post::readJSON()
89 {
90  std::cout << "Post-porcessing EP16-csc output files." << std::endl;
91 
92  // reading mandatory fields
93  // int nTask;
94  if (!_jstrm.contains("Number of Task"))
95  {
96  std::cerr << "Number of Task field is not defined.\n";
97  return(-1);
98  }
99  // nTask = _jstrm["Number of Task"].as<int>();
100  if (!_jstrm.contains("Task"))
101  {
102  std::cerr << "Task field is not defined.\n";
103  return(-1);
104  }
105 
106  // processing tasks
107  if (_jstrm["Task"].is_array())
108  {
109  int ret = 0;
110  std::string task;
111  for (const auto &jtask : _jstrm["Task"].array_range())
112  {
113  task = jtask["Type"].as<std::string>();
114  if (task != "erode triangulation" && task != "erode" && task != "triangulation")
115  ret = procErode(jtask);
116  else
117  {
118  std::cerr << "Unknown post-process task " << task << std::endl;
119  return(-1);
120  }
121  }
122  if (ret != 0)
123  {
124  std::cerr << "Problem occured during Task " << task << std::endl;
125  return(-1);
126  }
127  }
128  return(0);
129 }
130 
131 
132 int ep16Post::procErode(const jsoncons::json &jtsk)
133 {
134  // reading other necessary fields
135  int nClst = jtsk["Number of Cluster"].as<int>();
136  std::string ofClStem = jtsk["Output Stem"].as<std::string>();
137 
138  // read csv input file
139  std::string fname;
140  if (!jtsk.contains("Input File"))
141  {
142  std::cerr << "An input file should be provided.\n";
143  return(-1);
144  }
145  fname = jtsk["Input File"].as<std::string>();
146  io::CSVReader<5,
147  io::trim_chars<' ','\t'>,
148  io::no_quote_escape<','>,
149  io::throw_on_overflow,
150  io::single_line_comment<'#'> > csv(fname);
151 
152  csv.read_header(io::ignore_extra_column, "ELM#", "MAT", "X", "Y", "Z");
153  size_t elm,mat;
154  double x,y,z;
155  std::vector<kmeans::Point> pnts;
156  std::vector<size_t> elmIds, matIds;
157 
158  while(csv.read_row(elm, mat, x, y, z))
159  {
160  //std::cout << elm << " " << mat << " "
161  // << x << " " << y << " " << z << std::endl;
162  kmeans::Point p(x,y,z);
163  pnts.push_back(p);
164  elmIds.push_back(elm);
165  matIds.push_back(mat);
166  }
167 
168  // kmeans
169  auto kmeans = new kmeans::KMeans(nClst, _kmeans_max_itr);
170  //kmeans->verbose();
171  kmeans->init(pnts);
172  bool ret = kmeans->run();
173  if (!ret)
174  {
175  std::cout << "Kmeans analysis failed.\n";
176  return(-1);
177  }
178  kmeans->printMeans();
179  pnts = kmeans->getPoints();
180  delete kmeans;
181 
182  // creating convex containers and output
183  for (int iClst=0; iClst<nClst; iClst++)
184  {
185  std::vector<quickhull::Vector3<double> > clPntCrd;
186  for (auto pit=pnts.begin(); pit!=pnts.end(); pit++)
187  {
188  if (pit->cluster_ == iClst)
189  {
190  clPntCrd.push_back(
191  quickhull::Vector3<double>(pit->data_[0], pit->data_[1], pit->data_[2]) );
192  }
193  }
194  auto cont = new NEM::GEO::convexContainer(clPntCrd);
195  cont->computeConvexHull();
196  std::stringstream ss;
197  ss << iClst;
198  std::string clFName = ofClStem + ss.str();
199  cont->toSTL(clFName);
200  delete cont;
201  }
202 
203  return(0);
204 }
205 
206 } // namespace EPC
207 
208 } // namespace NEM
209 #endif
std::string find_ext(const std::string &fname)
An implementation of convex container object for point The class implements methods to generate a con...