TexGen
PatternDraft.cpp
Go to the documentation of this file.
1/*=============================================================================
2TexGen: Geometric textile modeller.
3Copyright (C) 2014 Louise Brown
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 "PatternDraft.h"
22
23using namespace TexGen;
24
25CPatternDraft::CPatternDraft()
26: m_NumHeddles(0)
27{
28}
29
31{
32}
33
34/*void CPatternDraft::AddRow( vector<int>& Row )
35{
36 m_WeavePattern.push_back( Row );
37}*/
38
40{
41 m_WeavePattern.clear();
42}
43
44void CPatternDraft::AddRow( string Row )
45{
46 // Remove end of line characters
47 Row.erase(remove(Row.begin(), Row.end(), '\n'), Row.end() );
48 Row.erase(remove(Row.begin(), Row.end(), '\r'), Row.end() );
49 m_WeavePattern.push_back( Row );
50}
51
53{
54 if ( !m_WeavePattern.empty() )
55 return (int)m_WeavePattern[0].size();
56 return 0;
57}
58
60{
61 if ( !m_WeavePattern.empty() )
62 return (int)m_WeavePattern.size();
63 return 0;
64}
65
67{
68 if ( m_WeavePattern.empty() )
69 {
70 TGERROR("Can't create pattern draft: no weave pattern specified");
71 return false;
72 }
74 if ( !CreateHeddleDraft() )
75 {
76 TGERROR("Failed to create heddle draft");
77 return false;
78 }
79 if ( !CreateChainDraft() )
80 {
81 TGERROR("Failed to create chain draft: no heddle draft specified");
82 return false;
83 }
84 return true;
85}
86
88{
89 // Create a set of unique columns in the weave pattern
90 vector<string>::iterator itWeavePattern;
91 int Warps = (int)m_WeavePattern[0].size();
92 m_Columns.clear();
93 m_Columns.resize( Warps );
94
95 // Create string for each column pattern
96 for ( itWeavePattern = m_WeavePattern.begin(); itWeavePattern != m_WeavePattern.end(); ++itWeavePattern )
97 {
98 for ( int i = 0; i < Warps; ++i )
99 {
100 if ( (*itWeavePattern)[i] == '2' ) // For purposes of this exercise no yarn equates to warp down
101 m_Columns[i].push_back( '0' );
102 else
103 m_Columns[i].push_back( (*itWeavePattern)[i] ); // if 0 or 1 leave as it is
104 }
105 }
106
107 // Only save one copy of each column pattern in UniqueColumns vector
108 vector<string>::iterator itColumns;
109 m_UniqueColumns.clear();
110 for ( itColumns = m_Columns.begin(); itColumns != m_Columns.end(); ++itColumns )
111 {
112 vector<string>::iterator itFind;
113 itFind = find( m_UniqueColumns.begin(),m_UniqueColumns.end(), *itColumns );
114 if ( itFind == m_UniqueColumns.end() )
115 m_UniqueColumns.push_back( *itColumns );
116 }
117
118 m_NumHeddles = (int)m_UniqueColumns.size();
119}
120
122{
123 if ( m_Columns.empty() || m_UniqueColumns.empty() )
124 return false;
125
126 int NumWarps = (int)m_Columns.size();
127 vector<string>::iterator itColumns;
128
129 m_HeddleDraft.clear();
130
132 for ( itColumns = m_Columns.begin(); itColumns != m_Columns.end(); ++itColumns )
133 {
134 vector<string>::iterator itFind;
135 itFind = find(m_UniqueColumns.begin(), m_UniqueColumns.end(), *itColumns );
136 m_HeddleDraft.push_back( m_NumHeddles - 1 - (int)distance( m_UniqueColumns.begin(), itFind ) ); //Heddle index is max at top
137 }
138 return true;
139}
140
142{
143 if ( m_HeddleDraft.empty() )
144 return false;
145 vector<string>::iterator itWeavePattern;
146 int NumWarps = (int)m_Columns.size();
147
149 m_ChainDraft.assign( m_ChainDraft.size(), 0 );
150
151 int j;
152
153 for ( itWeavePattern = m_WeavePattern.begin(), j=0; itWeavePattern != m_WeavePattern.end(); ++itWeavePattern, ++j )
154 {
155 for ( int i = 0; i < NumWarps; ++i )
156 {
157 if ( (*itWeavePattern)[i] == '1' ) // Weft
158 m_ChainDraft[ m_NumHeddles*j + m_HeddleDraft[i] ] = 1; // Set the chain draft entry on the current row, corresponding to the heddle draft entry
159 }
160 }
161 return true;
162}
163
164void CPatternDraft::Output( string Filename )
165{
166 AddExtensionIfMissing(Filename, ".txt");
167 ofstream Output(Filename.c_str());
168
169 vector<string> HeddleDraft;
170 HeddleDraft.clear();
171 HeddleDraft.resize( m_NumHeddles );
172 // Create output strings for heddle draft. Each string represents one row of draft
173 for ( int i = 0; i < m_HeddleDraft.size(); ++i )
174 {
175 for ( int j = 0; j < m_NumHeddles; ++j )
176 {
177 if ( m_HeddleDraft[i] == j )
178 HeddleDraft[j].push_back('1');
179 else
180 HeddleDraft[j].push_back('0');
181 }
182 }
183
184 // Create tie-up - assumed to be straight tie-up
185 // Sits next to heddle draft in output so added to heddle draft strings
186 for ( int i = 0; i < m_NumHeddles; ++i )
187 {
188 HeddleDraft[i].push_back('\t');
189 for ( int j = 0; j < m_NumHeddles; ++j )
190 {
191 if ( i == j )
192 HeddleDraft[i].push_back('1');
193 else
194 HeddleDraft[i].push_back('0');
195 }
196 }
197
198 vector<string>::reverse_iterator itHeddleDraft;
199 for ( itHeddleDraft = HeddleDraft.rbegin(); itHeddleDraft != HeddleDraft.rend(); ++itHeddleDraft )
200 {
201 Output << itHeddleDraft->c_str() << "\n";
202 }
203 Output << "\n\n";
204
205 // Output for weave pattern and chain draft stored in ChainDraft vector
206 vector< string >::iterator itWeavePattern;
207 vector<string> ChainDraft;
208 // First duplicate weave pattern..
209 for ( itWeavePattern = m_WeavePattern.begin(); itWeavePattern != m_WeavePattern.end(); ++itWeavePattern )
210 {
211 ChainDraft.push_back(*itWeavePattern);
212 }
213
214 // ..then add chain draft to its right
215 vector<bool>::iterator itChainDraft;
216 int j;
217 for ( itChainDraft = m_ChainDraft.begin(), j = 0; itChainDraft != m_ChainDraft.end(); ++j )
218 {
219 string ChainRow;
220 for ( int i = 0; i < m_NumHeddles; ++i )
221 {
222 ChainRow.push_back( *itChainDraft ? '1':'0' );
223 itChainDraft++;
224 }
225
226 ChainDraft[j].push_back('\t');
227 ChainDraft[j].append(ChainRow);
228 Output << ChainDraft[j].c_str() << "\n";
229 }
230}
#define TGERROR(MESSAGE)
Macros used to report the file name and line number to the TexGenError and TexGenLog functions.
Definition: Logger.h:29
vector< string > m_Columns
Definition: PatternDraft.h:60
void AddRow(string Row)
Add a row representing one weft insertion '1' indicates warp up, '0' warp down and '2' no yarn.
void Output(string Filename)
Output the complete pattern draft in a text file.
vector< bool > m_ChainDraft
Definition: PatternDraft.h:63
bool CreatePatternDraft()
Calculates the pattern draft based on the weave pattern.
virtual ~CPatternDraft(void)
vector< string > m_UniqueColumns
Definition: PatternDraft.h:61
vector< int > m_HeddleDraft
Definition: PatternDraft.h:62
vector< string > m_WeavePattern
Definition: PatternDraft.h:59
void CreateColumnsVector()
Creates vector of strings where each string represents one unique column of the weave pattern.
bool CreateHeddleDraft()
Create heddle draft. Stored as vector of int.
Namespace containing a series of customised math operations not found in the standard c++ library.
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