Points&Forces (core)
Software tools facilitating the task of surveying architecture
a_unit_system.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2019 Pierre SMARS (smars@yuntech.edu.tw)
3  This program is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 2 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 #include "a_unit_system.h"
17 #include <sstream>
18 #include <iomanip>
19 #include <math.h>
20 #include <fstream>
21 
22 //---------------------------------------------------------------------------
23 const std::string a_unit_system::help()
24 {
25  std::ostringstream o;
26  o << "***************" << std::endl;
27  o << "a_unit_system:" << std::endl;
28  o << "***************" << std::endl;
29  o << "This is a 'a_unit_system' class" << std::endl;
30  o << std::endl;
31  o << "Create a unit system:" << std::endl;
32  o << "a_unit_system s" << std::endl;
33  o << std::endl;
34  o << "Commands:" << std::endl;
35  o << "name _name: set name of system of units" << std::endl;
36  o << "name: get name of system of units" << std::endl;
37  o << "decimals1 _val: set number of decimals in 1D" << std::endl;
38  o << "decimals1: get number of decimals in 1D" << std::endl;
39  o << "decimals2 _val: set number of decimals in 2D" << std::endl;
40  o << "decimals2: get number of decimals in 2D" << std::endl;
41  o << "addlevel1 _unit: add a unit to 1D unit set" << std::endl;
42  o << "addlevel2 _unit: add a unit to 2D unit set" << std::endl;
43  o << "level1: get pointer to a unit of the 1D unit set" << std::endl;
44  o << "level2: get pointer to a unit of the 2D unit set" << std::endl;
45  o << "dim1 _val: format a 1D measure" << std::endl;
46  o << "dim1_det _val: format a 1D measure (+ system name)" << std::endl;
47  o << "dim2 _val: format a 2D measure" << std::endl;
48  o << "dim2_det _val: format a 2D measure (+ system name)" << std::endl;
49  o << "dim1 _point: format a point" << std::endl;
50  o << "print: set parameters" << std::endl;
51  o << "read _file: read parameters from file" << std::endl;
52  o << "write _file: write parameters to file" << std::endl;
53  o << std::endl;
54  o << a_unit_base::help();
55  return o.str();
56 }
57 //***************************************************************************
58 //---------------------------------------------------------------------------
59 void a_unit_system::read(std::istream &in)
60 {
61  std::getline(in,name_);
62  int n;
63  in >> n;
64  for (int i=0; i<n; i++)
65  {
66  auto u = new a_unit;
67  in >> u;
68  this->addlevel1(*u);
69  }
70  in >> decimals1_;
71  in >> n;
72  for (int i=0; i<n; i++)
73  {
74  auto u = new a_unit;
75  in >> u;
76  this->addlevel2(*u);
77  }
78  in >> decimals2_;
79 }
80 //---------------------------------------------------------------------------
81 void a_unit_system::write(std::ostream &o) const
82 {
83  o << this->name() << std::endl;
84  int n1=this->levels1();
85  o << n1 << std::endl;
86  for (int i=0; i<n1; i++)
87  o << level1(i) << std::endl;
88  o << decimals1_ << std::endl;
89  int n2=this->levels2();
90  o << n2 << std::endl;
91  for (int i=0; i<n2; i++)
92  o << level2(i) << std::endl;
93  o << decimals2_ << std::endl;
94 }
95 //---------------------------------------------------------------------------
97 {
98  for (std::deque<a_unit *>::iterator u = levels1_.begin(); u < levels1_.end(); u++)
99  delete *u;
100  levels1_.clear();
101  for (std::deque<a_unit *>::iterator u = levels2_.begin(); u < levels2_.end(); u++)
102  delete *u;
103  levels2_.clear();
104 }
105 //***************************************************************************
106 //---------------------------------------------------------------------------
108 {
109  return levels1_[n];
110 }
111 //---------------------------------------------------------------------------
113 {
114  return levels2_[n];
115 }
116 //---------------------------------------------------------------------------
118 {
119  name_ = u.name();
120  decimals1_ = u.decimals1();
121  for (int tel = 0; tel < u.levels1(); tel++)
122  {
123  auto temp = new a_unit(*u.level1(tel));
124  levels1_.push_back(temp);
125  }
126  decimals2_ = u.decimals2();
127  for (int tel = 0; tel < u.levels2(); tel++)
128  {
129  auto temp = new a_unit(*u.level2(tel));
130  levels2_.push_back(temp);
131  }
132 }
133 //---------------------------------------------------------------------------
135 {
136  this->reset();
137 }
138 //---------------------------------------------------------------------------
140 {
141  double r = u.ratio();
142  for (std::deque<a_unit *>::iterator it=levels1_.begin(); it<levels1_.end(); it++)
143  {
144  if ((*it)->ratio()>r)
145  {
146  levels1_.insert(it,&u);
147  return;
148  }
149  }
150  levels1_.push_back(&u);
151 }
152 //---------------------------------------------------------------------------
154 {
155  double r = u.ratio();
156  for (std::deque<a_unit *>::iterator it=levels2_.begin(); it<levels2_.end(); it++)
157  {
158  if ((*it)->ratio()>r)
159  {
160  levels2_.insert(it,&u);
161  return;
162  }
163  }
164  levels2_.push_back(&u);
165 }
166 //---------------------------------------------------------------------------
167 std::string a_unit_system::dim1(const double v)
168 {
169  double rem = fabs(v);
170  std::ostringstream s;
171  if (v<0) {s << "-";}
172  int num = levels1_.size();
173  for (int tel = 0; tel < num; tel++)
174  {
175  int v0;
176  double v1;
177  a_unit * unit = levels1_[tel];
178  v1 = rem*unit->ratio();
179  v0 = int(v1);
180  rem -= v0/unit->ratio();
181  if ((v0 != 0)||(tel == num-1))
182  {
183  if ((tel == num-1)&&(decimals1_ != 0))
184  {
185  s << std::fixed << std::setprecision(decimals1_);
186  s << v0+rem*unit->ratio();
187  }
188  else
189  s << v0;
190  s << unit->short_name() << " ";
191  }
192  }
193  return s.str();
194 }
195 //---------------------------------------------------------------------------
196 std::string a_unit_system::dim1(const a_point& p)
197 {
198  std::ostringstream s;
199  s << this->dim1(p.x()) << " " << this->dim1(p.y()) << " " << this->dim1(p.z());
200  return s.str();
201 }
202 //---------------------------------------------------------------------------
203 std::string a_unit_system::dim1_det(const double v)
204 {
205  std::string s0 = dim1(v);
206  std::ostringstream s;
207  s << "[" << this->name() << "]";
208  return (s0+s.str());
209 }
210 //---------------------------------------------------------------------------
211 std::string a_unit_system::dim2(const double v)
212 {
213  double rem = fabs(v);
214  std::ostringstream s;
215  if (v<0) {s << "-";}
216  int num = levels2_.size();
217  for (int tel = 0; tel < num; tel++)
218  {
219  int v0;
220  double v1;
221  a_unit * unit = levels2_[tel];
222  v1 = rem*unit->ratio();
223  v0 = int(v1);
224  rem -= v0/unit->ratio();
225  if ((v0 != 0)||(tel == num-1))
226  {
227  if ((tel == num-1)&&(decimals2_ != 0))
228  {
229  s << std::fixed << std::setprecision(decimals2_);
230  s << v0+rem*unit->ratio();
231  }
232  else
233  s << v0;
234  s << unit->short_name() << " ";
235  }
236  }
237  return s.str();
238 }
239 //---------------------------------------------------------------------------
240 std::string a_unit_system::dim2_det(const double v)
241 {
242  std::string s0 = dim2(v);
243  std::ostringstream s;
244  s << "[" << this->name() << "]";
245  return (s0+s.str());
246 }
double z() const
Definition: a_point.h:56
double y() const
Definition: a_point.h:55
double x() const
Definition: a_point.h:54
static const std::string help()
Definition: a_unit_base.cxx:21
unit system
Definition: a_unit_system.h:34
virtual void write(std::ostream &o) const
std::string name_
Definition: a_unit_system.h:68
std::string dim2_det(const double v)
std::string name() const
Definition: a_unit_system.h:43
void addlevel2(a_unit &u)
void decimals1(const int val)
Definition: a_unit_system.h:44
int levels2() const
Definition: a_unit_system.h:49
a_unit * level1(int n) const
std::deque< a_unit * > levels1_
Definition: a_unit_system.h:69
static const std::string help()
std::string dim1_det(const double v)
int levels1() const
Definition: a_unit_system.h:48
std::string dim1(const double v)
void addlevel1(a_unit &u)
std::deque< a_unit * > levels2_
Definition: a_unit_system.h:70
void name(const std::string name)
Definition: a_unit_system.h:42
virtual void read(std::istream &i)
void decimals2(const int val)
Definition: a_unit_system.h:46
std::string dim2(const double v)
a_unit * level2(int n) const
unit
Definition: a_unit.h:29
std::string short_name() const
Definition: a_unit.h:38
double ratio() const
Definition: a_unit.h:40
std::cin getline(buf, 256)