msbGrid  1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
common.hh
Go to the documentation of this file.
1 /*****************************************************************************
2 * This program is part of the msbGrid software *
3 * *
4 * msbGrid stands for multi-structured block Grid generator *
5 * *
6 * msbGrid is a free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * msbGrid is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * See the file COPYING for full copying permissions. *
17 *****************************************************************************/
23 #ifndef _COMMON_HH_
24 #define _COMMON_HH_
25 
26 #include <iostream>
27 #include <cmath>
28 #include <limits>
29 #include <cstdlib>
30 #include <vector>
31 
32 namespace msbGrid
33 {
34 typedef double Scalar;
35 enum { dim = 2 };
36 
37 namespace Constants
38 {
39 const Scalar PI = 4.0 * std :: atan(1.0); /* in radiants */
40 const Scalar _PI = 180.0; /* in degrees */
41 const Scalar EPSILON = std :: numeric_limits< Scalar > :: epsilon();
42 const Scalar TOLERANCE = 1e2 * EPSILON;
43 } /* namespace Constants */
44 
45 enum Color
47 
48 std :: string colorName(const Color &color)
49 {
50  std :: string res;
51  switch ( color ) {
52  case Black: res = "Black";
53  break;
54  case Blue: res = "Blue";
55  break;
56  case Green: res = "Green";
57  break;
58  case Red: res = "Red";
59  break;
60  case Cyan: res = "Cyan";
61  break;
62  case Magenta: res = "Magenta";
63  break;
64  case Yellow: res = "Yellow";
65  break;
66  case White: res = "White";
67  break;
68  default:
69  std :: cerr << "error: colorName(.), not defined color = " << color << std :: endl;
70  std :: exit(1);
71  }
72 
73  return res;
74 }
75 
76 std :: string colorSymbol(const Color &color)
77 {
78  std :: string res;
79  switch ( color ) {
80  case Black: res = "'k'";
81  break;
82  case Blue: res = "'b'";
83  break;
84  case Green: res = "'g'";
85  break;
86  case Red: res = "'r'";
87  break;
88  case Cyan: res = "'c'";
89  break;
90  case Magenta: res = "'m'";
91  break;
92  case Yellow: res = "'y'";
93  break;
94  case White: res = "'w'";
95  break;
96  default:
97  std :: cerr << "error: colorSymbol(), not defined color!!, " << color << std :: endl;
98  std :: exit(1);
99  }
100 
101  return res;
102 }
103 
105 {
106  return Color( ( std :: rand() % 8 ) );
107 
108  /*
109  * TODO :
110  * implement more sophisticated color capabilities
111  */
112 }
113 
114 struct Marker
115 {
116  std :: string symbol;
117  int size;
120 };
121 
122 /*
123  * random angle between 0 and 360 [degrees]
124  */
126 {
127  return ( std :: rand() % 360 );
128 }
129 
130 const Scalar deg2Rad(const Scalar &_angle) // [deg]
131 {
132  return ( ( _angle / msbGrid :: Constants :: _PI ) * msbGrid :: Constants :: PI ); // [rad]
133 }
134 
135 void periodicDistribution(std :: vector< std :: pair< Scalar, Scalar > > *blockCenter
136  , const Scalar &xMin, const Scalar &xMax, const Scalar &yMin, const Scalar &yMax
137  , const Scalar &dx, const Scalar &dy, const bool &verbose = true)
138 {
139  const Scalar xBegin = xMin + dx / Scalar(2.0), yBegin = yMin + dy / Scalar(2.0);
140  for ( Scalar x = xBegin; x <= xMax; x += dx ) {
141  for ( Scalar y = yBegin; y <= yMax; y += dy ) {
142  const std :: pair< Scalar, Scalar > p0(x, y);
143  blockCenter->push_back(p0);
144  if ( verbose ) {
145  std :: cout << "x = " << x << " , y = " << y << "\n";
146  }
147  }
148  }
149 }
150 } /* namespace msbGrid */
151 
152 #endif /* #ifndef _COMMON_HH_ */