comparison src/Modules/Output/Graphics/Devices/GraphicsOutputDevicePlotutils.cc @ 117:c5ac2f0c7fc5

- All \t to two spaces (style guide compliance)
author tomwalters
date Fri, 15 Oct 2010 05:46:53 +0000
parents 47b009f2c936
children 73c6d61440ad
comparison
equal deleted inserted replaced
116:47b009f2c936 117:c5ac2f0c7fc5
34 #include "Support/util.h" 34 #include "Support/util.h"
35 #include "Modules/Output/Graphics/Devices/GraphicsOutputDevicePlotutils.h" 35 #include "Modules/Output/Graphics/Devices/GraphicsOutputDevicePlotutils.h"
36 36
37 GraphicsOutputDevicePlotutils::GraphicsOutputDevicePlotutils(Parameters *pParam) 37 GraphicsOutputDevicePlotutils::GraphicsOutputDevicePlotutils(Parameters *pParam)
38 : GraphicsOutputDevice(pParam) { 38 : GraphicsOutputDevice(pParam) {
39 m_iPlotHandle = 0; 39 m_iPlotHandle = 0;
40 m_pOutputFile = NULL; 40 m_pOutputFile = NULL;
41 m_iFileNumber = 0; 41 m_iFileNumber = 0;
42 m_iVertexType = VertexTypeNone; 42 m_iVertexType = VertexTypeNone;
43 m_bUseMemoryBuffer = false; 43 m_bUseMemoryBuffer = false;
44 m_pMemoryBuffer = NULL; 44 m_pMemoryBuffer = NULL;
45 m_uWidth=0; 45 m_uWidth=0;
46 m_uHeight=0; 46 m_uHeight=0;
47 } 47 }
48 48
49 bool GraphicsOutputDevicePlotutils::Initialize(const char *sDir) { 49 bool GraphicsOutputDevicePlotutils::Initialize(const char *sDir) {
50 Init(); 50 Init();
51 51
52 //! \todo Output to file if sDir is a file, to directory with multiple 52 //! \todo Output to file if sDir is a file, to directory with multiple
53 //! images if it's a directory. 53 //! images if it's a directory.
54 strncpy(m_sDir, sDir, sizeof(m_sDir)/sizeof(m_sDir[0])); 54 strncpy(m_sDir, sDir, sizeof(m_sDir)/sizeof(m_sDir[0]));
55 55
56 /* Try to open an image to see if everything is allright. We want to avoid 56 /* Try to open an image to see if everything is allright. We want to avoid
57 * errors in the main Process()ing loop. */ 57 * errors in the main Process()ing loop. */
58 if (!OpenFile(0)) { 58 if (!OpenFile(0)) {
59 //! \todo Better error message that is more specific about the cause. 59 //! \todo Better error message that is more specific about the cause.
60 AIM_ERROR(_T("Could not open output directory '%s' using graphics format '%s'."), 60 AIM_ERROR(_T("Could not open output directory '%s' using graphics format '%s'."),
61 m_sDir, m_pParam->GetString("output.img.format") ); 61 m_sDir, m_pParam->GetString("output.img.format") );
62 return false; 62 return false;
63 } 63 }
64 CloseFile(); 64 CloseFile();
65 return true; 65 return true;
66 } 66 }
67 67
68 void GraphicsOutputDevicePlotutils::Init() { 68 void GraphicsOutputDevicePlotutils::Init() {
69 AIM_ASSERT(m_pParam); 69 AIM_ASSERT(m_pParam);
70 /* 70 /*
71 * Set parameters 71 * Set parameters
72 */ 72 */
73 pl_parampl("BG_COLOR", (void*)m_pParam->GetString("output.img.color.background")); 73 pl_parampl("BG_COLOR", (void*)m_pParam->GetString("output.img.color.background"));
74 // Handle GIFs as other output formats, don't merge frames into single GIF. 74 // Handle GIFs as other output formats, don't merge frames into single GIF.
75 pl_parampl("GIF_ANIMATION", (void*)"no"); 75 pl_parampl("GIF_ANIMATION", (void*)"no");
76 pl_parampl("INTERLACE", (void*)"no"); 76 pl_parampl("INTERLACE", (void*)"no");
77 77
78 m_bInvertColors = m_pParam->GetBool("output.img.color.invert"); 78 m_bInvertColors = m_pParam->GetBool("output.img.color.invert");
79 79
80 // Output size! 80 // Output size!
81 m_uWidth = m_pParam->GetUInt("output.img.width"); 81 m_uWidth = m_pParam->GetUInt("output.img.width");
82 m_uHeight = m_pParam->GetUInt("output.img.height"); 82 m_uHeight = m_pParam->GetUInt("output.img.height");
83 char strSize[100]; 83 char strSize[100];
84 snprintf(strSize, sizeof(strSize)/sizeof(strSize[0]), "%ux%u", m_uWidth, m_uHeight); 84 snprintf(strSize, sizeof(strSize)/sizeof(strSize[0]), "%ux%u", m_uWidth, m_uHeight);
85 pl_parampl("BITMAPSIZE", (void*)strSize); 85 pl_parampl("BITMAPSIZE", (void*)strSize);
86 } 86 }
87 87
88 bool GraphicsOutputDevicePlotutils::Initialize() { 88 bool GraphicsOutputDevicePlotutils::Initialize() {
89 Init(); 89 Init();
90 m_bUseMemoryBuffer = true; 90 m_bUseMemoryBuffer = true;
134 #endif 134 #endif
135 return false; 135 return false;
136 strPlottype="pnm"; // Force pnm format or this doesn't work 136 strPlottype="pnm"; // Force pnm format or this doesn't work
137 } 137 }
138 138
139 /* 139 /*
140 * Create a plotter 140 * Create a plotter
141 * 141 *
142 * Plotutils knows the following types for file output: 142 * Plotutils knows the following types for file output:
143 * pnm gif ai ps gif pcl hpgl tek meta 143 * pnm gif ai ps gif pcl hpgl tek meta
144 */ 144 */
145 if ((m_iPlotHandle = pl_newpl(strPlottype, 145 if ((m_iPlotHandle = pl_newpl(strPlottype,
146 NULL, 146 NULL,
147 m_pOutputFile, 147 m_pOutputFile,
148 stderr)) < 0 ) { 148 stderr)) < 0 ) {
149 return false; 149 return false;
150 } 150 }
151 pl_selectpl(m_iPlotHandle); 151 pl_selectpl(m_iPlotHandle);
152 152
153 if ( pl_openpl() < 0) { 153 if ( pl_openpl() < 0) {
154 return false; 154 return false;
155 } 155 }
156 156
157 // Now setup things for this plotter 157 // Now setup things for this plotter
158 pl_fontname(m_pParam->GetString("output.img.fontname")); 158 pl_fontname(m_pParam->GetString("output.img.fontname"));
159 //! \todo Make fontsize work in Plotutils, currently disabled 159 //! \todo Make fontsize work in Plotutils, currently disabled
160 //pl_ffontsize(m_pParam->GetUInt("output.fontsize")); 160 //pl_ffontsize(m_pParam->GetUInt("output.fontsize"));
161 161
162 return true; 162 return true;
163 } 163 }
164 164
165 char* GraphicsOutputDevicePlotutils::GetBuffer() { 165 char* GraphicsOutputDevicePlotutils::GetBuffer() {
166 if(m_bUseMemoryBuffer && (m_pMemoryBuffer!=NULL)) 166 if(m_bUseMemoryBuffer && (m_pMemoryBuffer!=NULL))
167 return (&m_pMemoryBuffer[m_sMemoryBufferSize-(m_uWidth*m_uHeight*3)]); 167 return (&m_pMemoryBuffer[m_sMemoryBufferSize-(m_uWidth*m_uHeight*3)]);
168 else 168 else
169 return NULL; 169 return NULL;
170 } 170 }
171 171
172 void GraphicsOutputDevicePlotutils::CloseFile() { 172 void GraphicsOutputDevicePlotutils::CloseFile() {
173 // Plotting library 173 // Plotting library
174 if (m_iPlotHandle>0) { 174 if (m_iPlotHandle>0) {
175 pl_closepl(); 175 pl_closepl();
176 176
177 pl_selectpl(0); 177 pl_selectpl(0);
178 pl_deletepl(m_iPlotHandle); 178 pl_deletepl(m_iPlotHandle);
179 m_iPlotHandle = 0; 179 m_iPlotHandle = 0;
180 } 180 }
181 181
182 // And the output file 182 // And the output file
183 if (m_pOutputFile) { 183 if (m_pOutputFile) {
184 fclose(m_pOutputFile); 184 fclose(m_pOutputFile);
185 m_pOutputFile = NULL; 185 m_pOutputFile = NULL;
186 } 186 }
187 } 187 }
188 188
189 GraphicsOutputDevicePlotutils::~GraphicsOutputDevicePlotutils() { 189 GraphicsOutputDevicePlotutils::~GraphicsOutputDevicePlotutils() {
190 // Output file should be closed by gRelease() 190 // Output file should be closed by gRelease()
191 AIM_ASSERT(!m_pOutputFile); 191 AIM_ASSERT(!m_pOutputFile);
192 AIM_ASSERT(!m_iPlotHandle); 192 AIM_ASSERT(!m_iPlotHandle);
193 CloseFile(); 193 CloseFile();
194 } 194 }
195 195
196 PixelFormat GraphicsOutputDevicePlotutils::GetPixelFormat() { 196 PixelFormat GraphicsOutputDevicePlotutils::GetPixelFormat() {
197 return AIM_PIX_FMT_RGB24_24; 197 return AIM_PIX_FMT_RGB24_24;
198 } 198 }
199 199
200 void GraphicsOutputDevicePlotutils::gGrab() { 200 void GraphicsOutputDevicePlotutils::gGrab() {
201 // Open file 201 // Open file
202 if (!OpenFile(m_iFileNumber)) { 202 if (!OpenFile(m_iFileNumber)) {
203 return; 203 return;
204 } 204 }
205 // Setup plotting area 205 // Setup plotting area
206 pl_fspace(0.0, 0.0, 1.0, 1.0); 206 pl_fspace(0.0, 0.0, 1.0, 1.0);
207 pl_flinewidth(0.0001); 207 pl_flinewidth(0.0001);
208 pl_pencolorname("darkblue"); 208 pl_pencolorname("darkblue");
209 pl_erase(); 209 pl_erase();
210 } 210 }
211 211
212 void GraphicsOutputDevicePlotutils::gBeginLineStrip() { 212 void GraphicsOutputDevicePlotutils::gBeginLineStrip() {
213 m_bIsFirstVertex = true; 213 m_bIsFirstVertex = true;
214 m_iVertexType = VertexTypeLine; 214 m_iVertexType = VertexTypeLine;
215 pl_filltype(0); 215 pl_filltype(0);
216 } 216 }
217 217
218 void GraphicsOutputDevicePlotutils::gBeginQuadStrip() { 218 void GraphicsOutputDevicePlotutils::gBeginQuadStrip() {
219 m_bIsFirstVertex = true; 219 m_bIsFirstVertex = true;
220 m_iVertexType = VertexTypeQuad; 220 m_iVertexType = VertexTypeQuad;
221 m_iPrevVertexCount = 0; 221 m_iPrevVertexCount = 0;
222 pl_filltype(1); 222 pl_filltype(1);
223 } 223 }
224 224
225 void GraphicsOutputDevicePlotutils::gColor3f(float r, float g, float b) { 225 void GraphicsOutputDevicePlotutils::gColor3f(float r, float g, float b) {
226 if (m_bInvertColors) { 226 if (m_bInvertColors) {
227 r = 1-r; 227 r = 1-r;
228 g = 1-g; 228 g = 1-g;
229 b = 1-b; 229 b = 1-b;
230 } 230 }
231 int ir = (int)(r*0xffff); 231 int ir = (int)(r*0xffff);
232 int ig = (int)(g*0xffff); 232 int ig = (int)(g*0xffff);
233 int ib = (int)(b*0xffff); 233 int ib = (int)(b*0xffff);
234 ir = MIN(0xffff, MAX(0,ir)); 234 ir = MIN(0xffff, MAX(0,ir));
235 ig = MIN(0xffff, MAX(0,ig)); 235 ig = MIN(0xffff, MAX(0,ig));
236 ib = MIN(0xffff, MAX(0,ib)); 236 ib = MIN(0xffff, MAX(0,ib));
237 pl_color(ir, ig, ib); 237 pl_color(ir, ig, ib);
238 } 238 }
239 239
240 void GraphicsOutputDevicePlotutils::gVertex3f(float x, float y, float z) { 240 void GraphicsOutputDevicePlotutils::gVertex3f(float x, float y, float z) {
241 switch(m_iVertexType) { 241 switch(m_iVertexType) {
242 case VertexTypeLine: 242 case VertexTypeLine:
243 if (m_bIsFirstVertex) { 243 if (m_bIsFirstVertex) {
244 m_bIsFirstVertex = false; 244 m_bIsFirstVertex = false;
245 pl_fmove(x, y); 245 pl_fmove(x, y);
246 } else { 246 } else {
247 pl_fcont(x, y); 247 pl_fcont(x, y);
248 } 248 }
249 break; 249 break;
250 case VertexTypeQuad: 250 case VertexTypeQuad:
251 /* Store vertices until we got four in a row. 251 /* Store vertices until we got four in a row.
252 * The order of vertices when processing quads is: 252 * The order of vertices when processing quads is:
253 * 1-----3-----5 253 * 1-----3-----5
254 * | | | 254 * | | |
255 * 0-----2-----4 255 * 0-----2-----4
256 */ 256 */
257 if (m_iPrevVertexCount>=3) { 257 if (m_iPrevVertexCount>=3) {
258 // Plot this quad 258 // Plot this quad
259 pl_fmove(m_aPrevX[0],m_aPrevY[0]); 259 pl_fmove(m_aPrevX[0],m_aPrevY[0]);
260 pl_fcont(m_aPrevX[1],m_aPrevY[1]); 260 pl_fcont(m_aPrevX[1],m_aPrevY[1]);
261 pl_fcont(x,y); 261 pl_fcont(x,y);
262 pl_fcont(m_aPrevX[2],m_aPrevY[2]); 262 pl_fcont(m_aPrevX[2],m_aPrevY[2]);
263 pl_endpath(); 263 pl_endpath();
264 // Last vertices of this quad are the first of the next 264 // Last vertices of this quad are the first of the next
265 m_aPrevX[0] = m_aPrevX[2]; 265 m_aPrevX[0] = m_aPrevX[2];
266 m_aPrevY[0] = m_aPrevY[2]; 266 m_aPrevY[0] = m_aPrevY[2];
267 m_aPrevX[1] = x; 267 m_aPrevX[1] = x;
268 m_aPrevY[1] = y; 268 m_aPrevY[1] = y;
269 m_iPrevVertexCount = 2; 269 m_iPrevVertexCount = 2;
270 } else { 270 } else {
271 // Not at the fourth, keep storing 271 // Not at the fourth, keep storing
272 m_aPrevX[m_iPrevVertexCount] = x; 272 m_aPrevX[m_iPrevVertexCount] = x;
273 m_aPrevY[m_iPrevVertexCount] = y; 273 m_aPrevY[m_iPrevVertexCount] = y;
274 m_iPrevVertexCount++; 274 m_iPrevVertexCount++;
275 } 275 }
276 break; 276 break;
277 default: 277 default:
278 // Should not happen 278 // Should not happen
279 AIM_ASSERT(0); 279 AIM_ASSERT(0);
280 } 280 }
281 } 281 }
282 282
283 void GraphicsOutputDevicePlotutils::gEnd() { 283 void GraphicsOutputDevicePlotutils::gEnd() {
284 pl_endpath(); 284 pl_endpath();
285 m_iVertexType = VertexTypeNone; 285 m_iVertexType = VertexTypeNone;
286 } 286 }
287 287
288 void GraphicsOutputDevicePlotutils::gText3f(float x, 288 void GraphicsOutputDevicePlotutils::gText3f(float x,
289 float y, 289 float y,
290 float z, 290 float z,
291 const char *sStr, 291 const char *sStr,
292 bool bRotated) { 292 bool bRotated) {
293 if (bRotated) { 293 if (bRotated) {
294 pl_textangle(90); 294 pl_textangle(90);
295 pl_fmove(x, y); 295 pl_fmove(x, y);
296 pl_alabel('l', 't', sStr); 296 pl_alabel('l', 't', sStr);
297 } else { 297 } else {
298 pl_textangle(0); 298 pl_textangle(0);
299 pl_fmove(x, y); 299 pl_fmove(x, y);
300 pl_alabel('l', 'b', sStr); 300 pl_alabel('l', 'b', sStr);
301 } 301 }
302 } 302 }
303 303
304 void GraphicsOutputDevicePlotutils::gRelease() { 304 void GraphicsOutputDevicePlotutils::gRelease() {
305 AIM_ASSERT(m_pOutputFile); 305 AIM_ASSERT(m_pOutputFile);
306 AIM_ASSERT(m_iPlotHandle>0); 306 AIM_ASSERT(m_iPlotHandle>0);
307 CloseFile(); 307 CloseFile();
308 m_iFileNumber++; 308 m_iFileNumber++;
309 } 309 }