TexGen
Misc.h
Go to the documentation of this file.
1/*=============================================================================
2TexGen: Geometric textile modeller.
3Copyright (C) 2006 Martin Sherburn
4
5This program is free software; you can redistribute it and/or
6modify it under the terms of the GNU General Public License
7as published by the Free Software Foundation; either version 2
8of the License, or (at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18=============================================================================*/
19
20#pragma once
21namespace TexGen
22{
26 #ifndef CLASS_DECLSPEC
27 #ifdef WIN32
28 #pragma warning(disable:4251)
29 #ifdef EXPORT
30 #define CLASS_DECLSPEC __declspec(dllexport)
31 #else
32 #define CLASS_DECLSPEC __declspec(dllimport)
33 #endif
34 #else
35 #define CLASS_DECLSPEC
36 #endif
37 #endif
38
40
45 #define FOR_EACH_TIXMLELEMENT(CHILDELEMENT, PARENTELEMENT, ELEMENTNAME) \
46 for (TiXmlElement* CHILDELEMENT = (PARENTELEMENT).FirstChildElement(ELEMENTNAME); CHILDELEMENT; CHILDELEMENT = CHILDELEMENT->NextSiblingElement(ELEMENTNAME))
47
49 template<typename T>
50 inline std::string stringify(const T& x, int iPrecision = 12, bool bScientific = true)
51 {
52 std::ostringstream o;
53 if (bScientific)
54 o << std::scientific;
55 o << std::setprecision(iPrecision) << x;
56 return o.str();
57 }
58
60 template<typename T>
61 inline T valueify(const std::string& x)
62 {
63 T value;
64 memset((void*)&value, 0, sizeof(T));
65 std::stringstream i(x);
66 i >> value;
67 return value;
68 }
69
71 template<typename T>
72 inline T valueify(const char* x)
73 {
74 T value;
75 memset((void*)&value, 0, sizeof(T));
76 if (x)
77 {
78 std::stringstream i(x);
79 i >> value;
80 }
81 return value;
82 }
83
85 struct LessPairDoubleInt : public std::binary_function<std::pair<double, int>, std::pair<double, int>, bool>
86 {
87 bool operator()(std::pair<double, int> x, std::pair<double, int> y) { return x.first < y.first; }
88 };
89
91 struct LessPairDoubleXYZ : public std::binary_function<std::pair<double, XYZ>, std::pair<double, XYZ>, bool>
92 {
93 bool operator()(std::pair<double, XYZ> x, std::pair<double, XYZ> y) { return x.first < y.first; }
94 };
95
96 struct COLOR
97 {
98 double r, g, b;
99 COLOR(double r, double g, double b):r(r), g(g), b(b) {}
100 COLOR():r(1), g(1), b(1) {}
101 inline double* Array() { return (double*)this; }
102 };
103
105 {
109 };
110
112 {
116 };
117
119 {
124 };
125
126 CLASS_DECLSPEC COLOR GetIndexedColor(int iIndex);
127 CLASS_DECLSPEC void CopyToRange(std::vector<XYZ> &Offsets, XYZ Vector, int iLowerLimit, int iUpperLimit);
128
129 // Wrapper functions around the units library
130 CLASS_DECLSPEC double ConvertUnits(double dValue, std::string SourceUnits, std::string TargetUnits);
131 CLASS_DECLSPEC bool CompatibleUnits(std::string SourceUnits, std::string TargetUnits, std::string *pErrorMessage = NULL);
132 CLASS_DECLSPEC std::string ReduceUnits(std::string Units);
133 CLASS_DECLSPEC void AddNewUnits(std::string NewUnit, std::string BaseUnits);
134
135 // Misc functions
137 CLASS_DECLSPEC std::string StripPath(std::string Filename);
139 CLASS_DECLSPEC void AddExtensionIfMissing(std::string &Filename, std::string Extension);
141 CLASS_DECLSPEC std::string ReplaceFilenameSpaces(std::string Filename);
143 CLASS_DECLSPEC std::string RemoveExtension( std::string &Filename, std::string Extension );
145 CLASS_DECLSPEC void WriteOrientationsHeader( std::ostream &Output );
147 CLASS_DECLSPEC void WriteElementsHeader( std::ostream &Output );
148
150 template<typename T>
151 T GetInterpedValue(const std::vector<std::pair<double, T> > &InterpValues, double fraction)
152 {
153 if (InterpValues.empty())
154 return T();
155 if (fraction <= InterpValues.front().first)
156 return InterpValues.front().second;
157 if (fraction >= InterpValues.back().first)
158 return InterpValues.back().second;
159 typename std::vector<std::pair<double, T> >::const_iterator itCurrentValue;
160 typename std::vector<std::pair<double, T> >::const_iterator itPrevValue = InterpValues.end();
161 for (itCurrentValue = InterpValues.begin(); itCurrentValue != InterpValues.end(); ++itCurrentValue)
162 {
163 if (itPrevValue != InterpValues.end())
164 {
165 if (fraction <= itCurrentValue->first)
166 {
167 double u = (fraction-itPrevValue->first)/(itCurrentValue->first-itPrevValue->first);
168 T value = itPrevValue->second + u*(itCurrentValue->second-itPrevValue->second);
169 return value;
170 }
171 }
172 itPrevValue = itCurrentValue;
173 }
174 return T(); // Should never get here
175 }
176
178 template<typename T>
179 std::pair<double, std::pair<T, T> > GetClosestValues(const std::vector<std::pair<double, T> > &InterpValues, double fraction)
180 {
181 std::pair<double, std::pair<T, T> > ClosestVals;
182 if (InterpValues.empty())
183 return ClosestVals;
184 if (fraction <= InterpValues.front().first)
185 {
186 ClosestVals.first = 0.0;
187 ClosestVals.second = std::make_pair(InterpValues.front().second, InterpValues.front().second);
188 return ClosestVals;
189 }
190 if (fraction >= InterpValues.back().first)
191 {
192 ClosestVals.first = 1.0;
193 ClosestVals.second = std::make_pair(InterpValues.back().second, InterpValues.back().second);
194 return ClosestVals;
195 }
196 typename std::vector<std::pair<double, T> >::const_iterator itCurrentValue;
197 typename std::vector<std::pair<double, T> >::const_iterator itPrevValue = InterpValues.end();
198 for (itCurrentValue = InterpValues.begin(); itCurrentValue != InterpValues.end(); ++itCurrentValue)
199 {
200 if (itPrevValue != InterpValues.end())
201 {
202 if (fraction <= itCurrentValue->first)
203 {
204 ClosestVals.first = (fraction-itPrevValue->first)/(itCurrentValue->first-itPrevValue->first);
205 ClosestVals.second.first = itPrevValue->second;
206 ClosestVals.second.second = itCurrentValue->second;
207 return ClosestVals;
208 }
209 }
210 itPrevValue = itCurrentValue;
211 }
212 return ClosestVals; // Should never get here
213 }
214
217 {
223
230 };
231
233 {
236/* int iYarnIndex;
237 XYZ YarnTangent;
238 XY Location;
239 double dVolumeFraction;
240 double dSurfaceDistance;*/
241 MESHER_ELEMENT_DATA():iLayer(-1),iRegion(-1)/*,iYarnIndex(-1),dVolumeFraction(0),dSurfaceDistance(0)*/{}
242 };
243
244 inline void GetMinMaxXY(const std::vector<XY> &Points, XY &Min, XY &Max )
245 {
246 Min = Points[0];
247 Max = Points[0];
248 for ( int i = 1; i < (int)Points.size(); ++i )
249 {
250 if ( Points[i].x < Min.x )
251 Min.x = Points[i].x;
252 else if ( Points[i].x > Max.x )
253 Max.x = Points[i].x;
254
255 if ( Points[i].y < Min.y )
256 Min.y = Points[i].y;
257 else if ( Points[i].y > Max.y )
258 Max.y = Points[i].y;
259 }
260 }
261
263 {
271 };
272
273 template <typename T>
274 void WriteValues(std::ostream &Output, T &Values, int iMaxPerLine)
275 {
276 int iLinePos = 0;
277 typename T::const_iterator itValue;
278 for (itValue = Values.begin(); itValue != Values.end(); ++itValue)
279 {
280 if (iLinePos == 0)
281 {
282 // Do nothing...
283 }
284 else if (iLinePos < iMaxPerLine)
285 {
286 Output << ", ";
287 }
288 else
289 {
290 Output << "\n";
291 iLinePos = 0;
292 }
293 Output << *itValue;
294 ++iLinePos;
295 }
296 Output << "\n";
297 }
298
299}; // namespace TexGen
300
#define CLASS_DECLSPEC
Definition: Misc.h:35
#define NULL
Definition: ShinyConfig.h:50
Namespace containing a series of customised math operations not found in the standard c++ library.
std::string RemoveExtension(std::string &Filename, std::string Extension)
Strip the extension from the filename.
Definition: Misc.cpp:122
std::string StripPath(std::string Filename)
Strip the path from the filename (e.g. "c:\folder\file.ext -> file.ext")
Definition: Misc.cpp:107
void GetMinMaxXY(const std::vector< XY > &Points, XY &Min, XY &Max)
Definition: Misc.h:244
std::pair< double, std::pair< T, T > > GetClosestValues(const std::vector< std::pair< double, T > > &InterpValues, double fraction)
Get the two closest values to fraction in the vector InterpValues.
Definition: Misc.h:179
std::string ReplaceFilenameSpaces(std::string Filename)
Replaces spaces in filename with underscore.
Definition: Misc.cpp:130
void CopyToRange(vector< XYZ > &Offsets, XYZ Vector, int iLowerLimit, int iUpperLimit)
Definition: Misc.cpp:47
T valueify(const std::string &x)
Function to convert a string to a value (e.g. int, double, etc...)
Definition: Misc.h:61
void WriteElementsHeader(std::ostream &Output)
Write elements header for ABAQUS .eld files.
Definition: Misc.cpp:167
void WriteOrientationsHeader(std::ostream &Output)
Write orientations header for ABAQUS .ori files.
Definition: Misc.cpp:157
COLOR GetIndexedColor(int iIndex)
Definition: Misc.cpp:28
OUTPUT_TYPE
Definition: Misc.h:105
@ OUTPUT_MINIMAL
Definition: Misc.h:106
@ OUTPUT_STANDARD
Definition: Misc.h:107
@ OUTPUT_FULL
Definition: Misc.h:108
bool CompatibleUnits(std::string SourceUnits, std::string TargetUnits, std::string *pErrorMessage)
Definition: Misc.cpp:73
DOMAIN_TYPE
Definition: Misc.h:119
@ SHEARED_DOMAIN
Definition: Misc.h:121
@ ROTATED_DOMAIN
Definition: Misc.h:122
@ PRISM_DOMAIN
Definition: Misc.h:123
@ BOX_DOMAIN
Definition: Misc.h:120
void WriteValues(std::ostream &Output, T &Values, int iMaxPerLine)
Definition: Misc.h:274
std::string ReduceUnits(std::string Units)
Definition: Misc.cpp:88
T GetInterpedValue(const std::vector< std::pair< double, T > > &InterpValues, double fraction)
Get an interpolated value.
Definition: Misc.h:151
EXPORT_TYPE
Definition: Misc.h:112
@ SCIRUN_EXPORT
Definition: Misc.h:115
@ INP_EXPORT
Definition: Misc.h:113
@ VTU_EXPORT
Definition: Misc.h:114
double Max(XYZ &Vector)
Get maximum element of vector and return it.
Definition: mymath.h:642
void AddNewUnits(std::string NewUnit, std::string BaseUnits)
Definition: Misc.cpp:98
std::string stringify(const T &x, int iPrecision=12, bool bScientific=true)
Function to convert a value (e.g. int, double, etc...) to a string.
Definition: Misc.h:50
XYZ Min(const XYZ &P1, const XYZ &P2)
Given two points, return a new point who's coordinates are the smaller of the two.
Definition: mymath.h:1142
void AddExtensionIfMissing(std::string &Filename, std::string Extension)
Adds an extension to the filename if it is missing otherwise do nothing (e.g. picture -> picture....
Definition: Misc.cpp:116
PERIODIC_BOUNDARY_CONDITIONS
Definition: Misc.h:263
@ SINGLE_LAYER_RVE
Definition: Misc.h:265
@ BENDING_BC
Definition: Misc.h:269
@ MATERIAL_CONTINUUM
Definition: Misc.h:264
@ SHEARED_BC
Definition: Misc.h:267
@ STAGGERED_BC
Definition: Misc.h:266
@ ROTATED_BC
Definition: Misc.h:268
@ NO_BOUNDARY_CONDITIONS
Definition: Misc.h:270
double ConvertUnits(double dValue, std::string SourceUnits, std::string TargetUnits)
Definition: Misc.cpp:63
double b
Definition: Misc.h:98
double r
Definition: Misc.h:98
double * Array()
Definition: Misc.h:101
double g
Definition: Misc.h:98
COLOR(double r, double g, double b)
Definition: Misc.h:99
Used to sort double-int pairs.
Definition: Misc.h:86
bool operator()(std::pair< double, int > x, std::pair< double, int > y)
Definition: Misc.h:87
Used to sort double-XYZ pairs.
Definition: Misc.h:92
bool operator()(std::pair< double, XYZ > x, std::pair< double, XYZ > y)
Definition: Misc.h:93
Structure used to retreive information about a particular point in space.
Definition: Misc.h:217
double dSurfaceDistance
Returns the closest distance from the point to the surface of the yarn.
Definition: Misc.h:226
int iYarnIndex
Index of the yarn, -1 when the point is not inside a yarn.
Definition: Misc.h:218
XYZ Orientation
Local fibre orientation.
Definition: Misc.h:227
XY Location
Location of the point relative to the yarn centerline.
Definition: Misc.h:220
XYZ YarnTangent
Tangent of the yarn centreline.
Definition: Misc.h:219
XYZ Up
Local Up vector.
Definition: Misc.h:228
double dVolumeFraction
Definition: Misc.h:221
Struct for representing points in 2D space.
Definition: mymath.h:103
Struct for representing points in 3D space.
Definition: mymath.h:56
double x
Definition: mymath.h:57
double y
Definition: mymath.h:57