TexGen
Misc.cpp
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#include "PrecompiledHeaders.h"
21#include "Misc.h"
22#include "../units/units.h"
23
24namespace TexGen
25{
26 using namespace std;
27
29 {
30 static double ColorArray[][3] =
31 {
32 { 1, 0, 0 },
33 { 0, 1, 0 },
34 { 0, 0, 1 },
35 { 1, 1, 0 },
36 { 0, 1, 1 },
37 { 1, 0, 1 },
38 { 1, 0, 0.5 },
39 { 0, 0.5, 1 },
40 { 1, 0.5, 0 },
41 { 0.5, 1, 0 },
42 };
43 double* Color = ColorArray[iIndex%(sizeof(ColorArray)/(3*sizeof(double)))];
44 return COLOR(Color[0], Color[1], Color[2]);
45 }
46
47 void CopyToRange(vector<XYZ> &Offsets, XYZ Vector, int iLowerLimit, int iUpperLimit)
48 {
49 vector<XYZ> OriginalOffsets = Offsets;
50 vector<XYZ>::iterator itOffset;
51 Offsets.clear();
52 int i;
53 for (i=iLowerLimit; i<=iUpperLimit; ++i)
54 {
55 for (itOffset=OriginalOffsets.begin(); itOffset != OriginalOffsets.end(); ++itOffset)
56 {
57 Offsets.push_back(*itOffset+i*Vector);
58 }
59 }
60 }
61
62 // Warning possible memory leaks contained within the units library!!!
63 double ConvertUnits(double dValue, std::string SourceUnits, std::string TargetUnits)
64 {
65 units_clear_exception();
66 double dResult = dValue*units_convert(const_cast<char*>(SourceUnits.c_str()), const_cast<char*>(TargetUnits.c_str()));
67 char* szError = units_check_exception();
68 if (szError)
69 TGERROR("Error converting units: " << szError);
70 return dResult;
71 }
72
73 bool CompatibleUnits(std::string SourceUnits, std::string TargetUnits, std::string *pErrorMessage)
74 {
75 units_clear_exception();
76 units_convert(const_cast<char*>(SourceUnits.c_str()), const_cast<char*>(TargetUnits.c_str()));
77 char* szError = units_check_exception();
78 if (szError)
79 {
80 if (pErrorMessage)
81 *pErrorMessage = szError;
82 return false;
83 }
84 else
85 return true;
86 }
87
88 std::string ReduceUnits(std::string Units)
89 {
90 units_clear_exception();
91 std::string Result = units_reduce(const_cast<char*>(Units.c_str()));
92 char* szError = units_check_exception();
93 if (szError)
94 TGERROR("Error reducing units: " << szError);
95 return Result;
96 }
97
98 void AddNewUnits(std::string NewUnit, std::string BaseUnits)
99 {
100 units_clear_exception();
101 units_new(const_cast<char*>(NewUnit.c_str()), const_cast<char*>(BaseUnits.c_str()));
102 char* szError = units_check_exception();
103 if (szError)
104 TGERROR("Error adding new units: " << szError);
105 }
106
107 std::string StripPath(std::string Filename)
108 {
109 int iPos = Filename.find_last_of("\\/");
110 if (iPos == std::string::npos)
111 return Filename;
112 else
113 return Filename.substr(iPos+1);
114 }
115
116 void AddExtensionIfMissing(std::string &Filename, std::string Extension)
117 {
118 if (Filename.size()<Extension.size() || Filename.rfind(Extension) != Filename.size()-Extension.size())
119 Filename += Extension;
120 }
121
122 std::string RemoveExtension( std::string &Filename, std::string Extension)
123 {
124 int iPos = Filename.rfind(Extension);
125 if (iPos == std::string::npos)
126 return Filename;
127 return Filename.substr(0, iPos);
128 }
129
130 std::string ReplaceFilenameSpaces(std::string Filename)
131 {
132 int iPos = Filename.find_last_of("\\/");
133 int iEnd = Filename.size();
134 std::string str;
135 if (iPos == std::string::npos)
136 str = Filename;
137 else
138 str = Filename.substr(iPos+1);
139
140 size_t found;
141
142 found=str.find_first_of(" ");
143 while (found!=string::npos)
144 {
145 str[found]='_';
146 found=str.find_first_of(" ",found+1);
147 }
148
149 if (iPos == std::string::npos)
150 return str;
151 else
152 {
153 return Filename.replace( iPos+1, iEnd, str );
154 }
155 }
156
157 void WriteOrientationsHeader( std::ostream &Output )
158 {
159 Output << "********************" << std::endl;
160 Output << "*** ORIENTATIONS ***" << std::endl;
161 Output << "********************" << std::endl;
162 Output << "** Orientation vectors" << std::endl;
163 Output << "** 1st vector represents the fibre direction" << std::endl;
164 Output << "** 2nd vector is an arbitrary vector perpendicular to the first" << std::endl;
165 }
166
167 void WriteElementsHeader( std::ostream &Output )
168 {
169 Output << "********************" << std::endl;
170 Output << "*** ELEMENT DATA ***" << std::endl;
171 Output << "********************" << std::endl;
172 Output << "** Element data stored as a depvars " << std::endl;
173 Output << "** 1 - Element index" << std::endl;
174 Output << "** 2 - Yarn Index (-1 for matrix, first yarn starting at 0) " << std::endl;
175 Output << "** 3/4 - Location (x and y cross-section coordinates of element relative to yarn centerline) " << std::endl;
176 Output << "** 5 - Volume fraction " << std::endl;
177 Output << "** 6 - Distance of element from the surface of the yarn (for yarn elements only, distance is negative) " << std::endl;
178 }
179}
180
181
182
183
184
185
186
187
188
#define TGERROR(MESSAGE)
Macros used to report the file name and line number to the TexGenError and TexGenLog functions.
Definition: Logger.h:29
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
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
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
bool CompatibleUnits(std::string SourceUnits, std::string TargetUnits, std::string *pErrorMessage)
Definition: Misc.cpp:73
std::string ReduceUnits(std::string Units)
Definition: Misc.cpp:88
void AddNewUnits(std::string NewUnit, std::string BaseUnits)
Definition: Misc.cpp:98
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
double ConvertUnits(double dValue, std::string SourceUnits, std::string TargetUnits)
Definition: Misc.cpp:63
Struct for representing points in 3D space.
Definition: mymath.h:56