forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvtkCGMWriter.cxx
4502 lines (4061 loc) · 113 KB
/
vtkCGMWriter.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: vtkCGMWriter.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkCGMWriter.h"
#include "vtkMath.h"
#include "vtkUnsignedCharArray.h"
#include "vtkViewport.h"
#include "vtkIdList.h"
#include "vtkPolyData.h"
#include "vtkCellArray.h"
#include "vtkGenericCell.h"
#include "vtkCellData.h"
#include "vtkObjectFactory.h"
vtkStandardNewMacro(vtkCGMWriter);
vtkCxxSetObjectMacro(vtkCGMWriter, Viewport, vtkViewport);
vtkCGMWriter::vtkCGMWriter()
{
this->Viewport = NULL;
this->ColorMode = VTK_COLOR_MODE_DEFAULT;
this->SpecifiedColor[0] = 1.0;
this->SpecifiedColor[1] = 1.0;
this->SpecifiedColor[2] = 1.0;
this->Resolution = 10000;
this->Sort = 0;
}
vtkCGMWriter::~vtkCGMWriter()
{
if ( this->Viewport != NULL )
{
this->Viewport->Delete();
this->Viewport = NULL;
}
}
//--------------------------#defines and method descriptions for CGM output
//---defines.h
#define b0 01
#define b1 02
#define b2 04
#define b3 010
#define b4 020
#define b5 040
#define b6 0100
#define b7 0200
#define b8 0400
#define b9 01000
#define b10 02000
#define b11 04000
#define b12 010000
#define b13 020000
#define b14 040000
#define b15 0100000
// Defines the default values for different attributes. In general,
// these track the CGM specificaition, so changing them is not a good idea.
// however, it is generally ok to set them to -1 (undefined) if you want.
//
#define CGMLTYPE 1
#define CGMLWIDTH 0
#define CGMLCOLOR 1
#define CGMSHAPESTYLE 0
#define CGMSHAPECOLOR 1
#define CGMSHAPEHATCH 1
#define CGMEDGETYPE 1
#define CGMEDGECOLOR 1
#define CGMEDGEWIDTH 1
#define CGMEDGEVIS 0
#define CGMTEXTFONT 1
#define CGMTEXTCOLOR 1
#define CGMTEXTHEIGHT -1
#define CGMTEXTPATH 0
#define CGMMTYPE 1
#define CGMMSIZE 0
#define CGMMCOLOR 1
#define CGMLINESPEC 1
#define CGMEDGESPEC 1
#define CGMMARKERSPEC 1
//--the include file CGM.h
// This can not be changed to a value larger than 256, though smaller
// values can be used.
//
#define cgmMaxColors 256
// If you know you will be working with large pictures, increase the values
// of the next two constants.
//
// The initial size of the element list. When it fills up, we will just
// make it bigger. Starting with a larger number reduces the frequency of
// the list growing, but increases the memory needed for small pictures
//
#define CGMSTARTLISTSIZE 4096
// How much the element list grows by. When the list fills up, we allocate
// a new larger list. This number is how much larger. using a larger number
// decreases the frequency of the list growing, but if only a small amount
// more is needed, it could waste memory
//
#define CGMGROWLISTSIZE 2048
// Image type. See functions below; you will not need to change
// the elements directly. Use the provided macros to
// access sx, sy, the color table, and colorsTotal for
// read-only purposes.
typedef struct cgmImageStruct {
// Don't mess with these
unsigned char * elemlist;
short int state;
int red[cgmMaxColors];
int green[cgmMaxColors];
int blue[cgmMaxColors];
int open[cgmMaxColors];
int colorsTotal;
// You can have multiple pictures in the file, this keeps track
// of which one you are on
int picnum;
// these take effect only when the first picture is created.
// subsequent changes have no effect
unsigned char *desc;
unsigned char *fontlist;
short int numfonts;
FILE *outfile;
// these take effect when a new picture is opened. Subsequent
// changes are for the next picture
int linespec;
int edgespec;
int markerspec;
int sx;
int sy;
// these take effect immediately
// Linetype, line width, line color have a broader scope in CGM
int ltype;
int lwidth;
int lcolor;
// interior style [of filled objects] (for me) can be empty, hollow,
// solid, hatch [don't do pattern, geometric pattern, interpolated
int shapestyle;
// fill color, color used on inside of closed objects, significant
// if interior style is hollow, solid, hatch, or geometric pattern
int shapecolor;
// hatch index, which hatch style to use, 1=horizontal, 2=vertical,
// 3=pos.slope, 4=neg.slope, 5=hor/vert.crosshatch,
// 6=pos/neg.crosshatch
int shapehatch;
// The edges of filled shapes can have line styles too. They
// correspond to the ones for lines. These next few set them.
int edgetype;
int edgewidth;
int edgecolor;
int edgevis; // is the edge visible or invisible
// now for the TEXT related attributes, Text Color, Text Height,
// and Text font index
int textfont;
int textcolor;
int textheight;
int textpath;
// Marker type, Marker size, marker color
int mtype;
int msize;
int mcolor;
// the next three are used for maintaining the element list
long int bytestoend; // number of bytes to end of the element list
long int listlen; // the total length of the element list
unsigned char * curelemlist; // where we curently are in the list
} cgmImage;
typedef cgmImage* cgmImagePtr;
// Point type for use in polygon drawing.
typedef struct cgmPointStruct{
int x, y, e;
} cgmPoint, *cgmPointPtr;
// Functions to manipulate images.
static cgmImagePtr cgmImageCreate(int sx, int sy);
#ifdef VTK_NOT_DEFINED
static int cgmCgmNewPic(cgmImagePtr im, int sticky);
#endif
static int cgmImageCgm(cgmImagePtr im, FILE *);
static int cgmImageDestroy(cgmImagePtr im);
#ifdef VTK_NOT_DEFINED
// Use cgmLine, not cgmImageLine
static int cgmLine(cgmImagePtr im, int x1, int y1, int x2, int y2);
// Specify corners (not width and height). Upper left first, lower right second.
static int cgmRectangle(cgmImagePtr im, int x1, int y1, int x2, int y2);
// center x, then center y, then radius of circle
static int cgmCircle(cgmImagePtr im, int cx, int cy, int r);
// start, middle and end of arc
static int cgmArc3Pt(cgmImagePtr im, int sx,int sy, int ix,int iy, int ex,int ey);
// cl is 0 for pie closure, 1 for cord closure
static int cgmArc3PtClose(cgmImagePtr im, int sx,int sy, int ix,int iy, int ex,int ey, int cl);
static int cgmEllipse(cgmImagePtr im, int cx,int cy, int d1x,int d1y, int d2x,int d2y );
static int cgmMarker(cgmImagePtr im, int x, int y);
#endif
// polyshapes
static int cgmPolygon(cgmImagePtr im, cgmPointPtr p, int n);
#ifdef VTK_NOT_DEFINED
static int cgmPolygonSet(cgmImagePtr im, cgmPointPtr p, int n);
#endif
static int cgmPolyLine(cgmImagePtr im, cgmPointPtr p, int n);
static int cgmPolyMarker(cgmImagePtr im, cgmPointPtr p, int n);
// Functions for Compatibility with gd
#ifdef VTK_NOT_DEFINED
static int cgmImageLine(cgmImagePtr im, int x1, int y1, int x2, int y2, int color);
static int cgmImageRectangle(cgmImagePtr im, int x1, int y1, int x2, int y2, int color);
#endif
#ifdef VTK_NOT_DEFINED
static int cgmImageBoundsSafe(cgmImagePtr im, int x, int y);
// These put characters in the picture. CGM can handle fonts
// (x,y) is the lower left corner of where the text goes
static int cgmText(cgmImagePtr im, int x, int y, const char *);
#endif
// Functions for allocating colors
static int cgmImageColorAllocate(cgmImagePtr im, int r, int g, int b);
#ifdef VTK_NOT_DEFINED
static int cgmImageColorClosest(cgmImagePtr im, int r, int g, int b);
static int cgmImageColorExact(cgmImagePtr im, int r, int g, int b);
static int cgmImageColorDeallocate(cgmImagePtr im, int color);
#endif
static int cgmImageColorGet(cgmImagePtr im, int cgmIndex,
int& r, int& g, int& b);
#ifdef VTK_NOT_DEFINED
// wogl: the parameter names are commented to avoid compiler warnings
static int cgmImageColor16(cgmImagePtr im);
#endif
// gej: functions that set style attributes
static int cgmSetLineAttrib(cgmImagePtr im, int lntype, int lnwidth, int lncolor);
static int cgmSetShapeFillAttrib(cgmImagePtr im, int instyle, int incolor, int inhatch);
static int cgmSetShapeEdgeAttrib(cgmImagePtr im, int edtype, int edwidth, int edcolor, int edvis);
static int cgmSetTextAttrib(cgmImagePtr im, int font, int color, int height);
static int cgmSetMarkerAttrib(cgmImagePtr im, int mtype, int msize, int mcolor);
// gej: or if you prefer, set the attributes individually
static int cgmSetLineType(cgmImagePtr im, int lntype);
static int cgmSetLineWidth(cgmImagePtr im, int lnwidth);
static int cgmSetLineColor(cgmImagePtr im, int lncolor);
static int cgmSetFillStyle(cgmImagePtr im, int instyle);
static int cgmSetFillColor(cgmImagePtr im, int incolor);
static int cgmSetFillHatch(cgmImagePtr im, int inhatch);
static int cgmSetEdgeType(cgmImagePtr im, int edtype);
static int cgmSetEdgeWidth(cgmImagePtr im, int edwidth);
static int cgmSetEdgeColor(cgmImagePtr im, int edcolor);
static int cgmSetEdgeVis(cgmImagePtr im, int edvis);
static int cgmSetTextFont(cgmImagePtr im, int font);
static int cgmSetTextColor(cgmImagePtr im, int color);
static int cgmSetTextHeight(cgmImagePtr im, int height);
// geJ: these individual attributes can't be set with a group function
static int cgmSetTextPath(cgmImagePtr im, int tpath);
#ifdef VTK_NOT_DEFINED
static int cgmSetTextOrient(cgmImagePtr im, int xup, int yup, int xbase, int ybase);
#endif
static int cgmSetMarkerType(cgmImagePtr im, int mtype);
static int cgmSetMarkerSize(cgmImagePtr im, int msize);
static int cgmSetMarkerColor(cgmImagePtr im, int mcolor);
// EJ: Expert Functions, If you just need more control
static int cgmImageSetSize(cgmImagePtr im, int x, int y);
#ifdef VTK_NOT_DEFINED
static int cgmImageSetLineSpec(cgmImagePtr im, int specmode);
static int cgmImageSetMarkerSpec(cgmImagePtr im, int specmode);
static int cgmImageSetEdgeSpec(cgmImagePtr im, int specmode);
#endif
static int cgmImageSetOutput(cgmImagePtr im, FILE *output);
#ifdef VTK_NOT_DEFINED
static int cgmImageAddFont(cgmImagePtr im, const char *fontname);
static int cgmImageClearFonts(cgmImagePtr im);
#endif
static cgmImagePtr cgmImageStartCgm();
static int cgmCgmHeader(cgmImagePtr);
static int cgmCgmPic(cgmImagePtr, int);
static int cgmImageSetDefaults(cgmImagePtr im);
static int cgmImageEndPic(cgmImagePtr im);
static int cgmImageEndCgm (cgmImagePtr im);
// Macros to access information about images. READ ONLY. Changing
// these values will NOT have the desired result.
#define cgmImageSX(im) ((im)->sx)
#define cgmImageSY(im) ((im)->sy)
#define cgmImageColorsTotal(im) ((im)->colorsTotal)
#define cgmImageRed(im, c) ((im)->red[(c)])
#define cgmImageGreen(im, c) ((im)->green[(c)])
#define cgmImageBlue(im, c) ((im)->blue[(c)])
// Source: Independent JPEG Group
// In ANSI C, and indeed any rational implementation, size_t is also the
// type returned by sizeof(). However, it seems there are some irrational
// implementations out there, in which sizeof() returns an int even though
// size_t is defined as long or unsigned long. To ensure consistent results
// we always use this SIZEOF() macro in place of using sizeof() directly.
//
#define SIZEOF(object) (static_cast<size_t>(sizeof(object)))
// GeJ: these are helper functions I use in cgm. That means DON'T call
// them from your program. Yes, that means you.
static int cgmImageColorClear(cgmImagePtr im);
//-------------------methods vtk uses to write data---------------------------
//
// Define class for looking up colors
class vtkColorHash {
public:
vtkColorHash();
~vtkColorHash();
int InsertUniqueColor(cgmImagePtr im, int r, int g, int b);
int GetColorIndex(cgmImagePtr im, int r, int g, int b);
protected:
vtkIdList **Table;
};
#define VTK_HASH_INDEX 737
vtkColorHash::vtkColorHash()
{
int i;
this->Table = new vtkIdList * [VTK_HASH_INDEX];
for (i=0; i<VTK_HASH_INDEX; i++)
{
this->Table[i] = NULL;
}
}
vtkColorHash::~vtkColorHash()
{
int i;
for (i=0; i<VTK_HASH_INDEX; i++)
{
if ( this->Table[i] != NULL )
{
this->Table[i]->Delete();
}
}
delete [] this->Table;
}
int vtkColorHash::InsertUniqueColor(cgmImagePtr im, int r, int g, int b)
{
int index = (65536*r + 256*g * b) % VTK_HASH_INDEX;
int cgmIndex=0; //remove warning
// If no list, just insert the color
if ( this->Table[index] == NULL )
{
this->Table[index] = vtkIdList::New();
this->Table[index]->Allocate(3,3);
cgmIndex = cgmImageColorAllocate(im, r, g, b);
this->Table[index]->InsertNextId(cgmIndex);
}
// otherwise, check to see if color exists
else
{
vtkIdType numIds=this->Table[index]->GetNumberOfIds();
int red, green, blue;
vtkIdType i;
for (i=0; i<numIds; i++)
{
cgmIndex = this->Table[index]->GetId(i);
cgmImageColorGet(im, cgmIndex, red, green, blue);
if ( r == red && g == green && b == blue )
{
break;
}
}
if ( i >= numIds ) //means didn't find one
{
cgmIndex = cgmImageColorAllocate(im, r, g, b);
this->Table[index]->InsertNextId(cgmIndex);
}
}
return cgmIndex;
}
int vtkColorHash::GetColorIndex(cgmImagePtr im, int r, int g, int b)
{
int index = (65536*r + 256*g * b) % VTK_HASH_INDEX;
vtkIdType cgmIndex;
vtkIdType numIds=this->Table[index]->GetNumberOfIds();
int red, green, blue;
int i;
for (i=0; i<numIds; i++)
{
cgmIndex = this->Table[index]->GetId(i);
cgmImageColorGet(im, cgmIndex, red, green, blue);
if ( r == red && g == green && b == blue )
{
return cgmIndex;
}
}
return 0;
}
#undef VTK_HASH_INDEX
// ------------------------------end vtkColorHash stuff---------------
// Build colors consisting of 3 bits red, 3 bits green, 2 bits blue
// (total of 256 colors)
//
static void DefineColors(cgmImagePtr im, int CGMcolors[256])
{
int red, green, blue, idx=0;
// use 3-3-2 bits for rgb
for (blue=0; blue<256; blue+=64)
{
for (green=0; green<256; green+=32)
{
for (red=0; red<256; red+=32)
{
CGMcolors[idx++] = cgmImageColorAllocate(im, red, green, blue);
}
}
}
}
// Define CGM colors from the lookup table provided
//
static vtkColorHash *DefineLUTColors(cgmImagePtr im, unsigned char *colors,
int numColors, int bpp)
{
vtkColorHash *colorHash = new vtkColorHash;
unsigned char *ptr;
int r=0, g=0, b=0; //warnings
int id;
for (id=0; id < numColors; id++)
{
ptr = colors + bpp*id;
switch (bpp)
{
case 1: case 2:
r = g = b = *ptr;
break;
case 3: case 4:
r = ptr[0];
g = ptr[1];
b = ptr[2];
break;
}
colorHash->InsertUniqueColor(im, r, g, b);
}
return colorHash;
}
// Get a CGM color from the RGB value specified.
//
static int GetColor(int red, int green, int blue, int CGMColors[256])
{
// round to nearest value
red = (red + 16) / 32;
red = (red > 7 ? 7 : red);
green =(green + 16) / 32;
green = (green > 7 ? 7 : green);
blue = (blue + 32) / 64;
blue = (blue > 3 ? 3 : blue);
return CGMColors[red + green*8 + blue*64];
}
#ifdef VTK_NOT_DEFINED
static int GetLUTColor(int vtkNotUsed(red), int vtkNotUsed(green), int vtkNotUsed(blue))
{
return 0;
}
#endif
typedef struct _vtkSortValues {
float z;
int cellId;
} vtkSortValues;
extern "C"
{
int vtkCGMqsortCompare(const void *val1, const void *val2)
{
if (((vtkSortValues *)val1)->z > ((vtkSortValues *)val2)->z)
{
return (-1);
}
else if (((vtkSortValues *)val1)->z < ((vtkSortValues *)val2)->z)
{
return (1);
}
else
{
return (0);
}
}
}
void vtkCGMWriter::WriteData()
{
FILE *outf;
vtkPolyData *input=this->GetInput();
vtkIdType numCells=input->GetNumberOfCells(), cellId;
vtkIdType numPts=input->GetNumberOfPoints();
// Check that there is something to write
if ( numPts < 1 || numCells < 1 )
{
vtkErrorMacro(<<"No data to write");
return;
}
// Try opening the file
if ( (outf = fopen(this->FileName, "wb")) == NULL )
{
vtkErrorMacro(<<"Cannot open CGM file");
return;
}
cgmImagePtr im;
vtkPoints *inPts=input->GetPoints(), *pts;
vtkGenericCell *cell=vtkGenericCell::New();
vtkDataArray *inScalars=input->GetCellData()->GetScalars();
int i, id, type, npts, size[2];
vtkIdType *p;
double bounds[6], xRange, yRange, x[3], factor[2];
int color, bpp=1, colorMode;
unsigned char *ptr, *colors=NULL;
int rgbColor[3], maxCellSize;
cgmPoint *points;
vtkSortValues *depth=NULL; //warnings
// Figure out the coordinate range of the data.
// Generate the points that will be used for output.
//
if ( this->Viewport == NULL ) //zero-out z values
{
input->GetBounds(bounds);
pts = inPts;
}
else //transform into view coordinates
{
vtkPoints *displayPts = vtkPoints::New();
displayPts->SetNumberOfPoints(numPts);
for ( i=0; i < numPts; i++ )
{
inPts->GetPoint(i, x);
this->Viewport->SetWorldPoint(x[0], x[1], x[2], 1.0);
this->Viewport->WorldToDisplay();
this->Viewport->GetDisplayPoint(x);
displayPts->SetPoint(i, x);
}
displayPts->GetBounds(bounds);
pts = displayPts;
}
// Get the bounding box of the points
//
xRange = bounds[1] - bounds[0];
yRange = bounds[3] - bounds[2];
if ( xRange > yRange )
{
factor[0] = 1.0;
factor[1] = yRange/xRange;
size[0] = this->Resolution;
size[1] = static_cast<int>(factor[1] * this->Resolution);
}
else
{
factor[0] = yRange/xRange;
factor[1] = 1.0;
size[0] = static_cast<int>(factor[0] * this->Resolution);
size[1] = this->Resolution;
}
// Loop over the points again, transforming them into resolution specified
//
vtkPoints *scaledPts = vtkPoints::New();
scaledPts->SetDataTypeToInt();
scaledPts->SetNumberOfPoints(numPts);
x[2] = 0.0;
for (i=0; i<numPts; i++)
{
pts->GetPoint(i,x);
x[0] = (x[0] - bounds[0]) / xRange * this->Resolution * factor[0];
x[1] = (x[1] - bounds[2]) / yRange * this->Resolution * factor[1];
scaledPts->SetPoint(i,x);
}
// Generate the colors according to specified method
//
int CGMColors[256];
im = cgmImageCreate(size[0], size[1]);
vtkColorHash *colorHash=NULL;
if ( this->ColorMode == VTK_COLOR_MODE_DEFAULT )
{
if ( inScalars && inScalars->GetDataType() == VTK_UNSIGNED_CHAR )
{
colorMode = VTK_COLOR_MODE_DEFAULT;
bpp = inScalars->GetNumberOfComponents();
colors = static_cast<vtkUnsignedCharArray *>(inScalars)->GetPointer(0);
}
else
{
colorMode = VTK_COLOR_MODE_SPECIFIED_COLOR;
}
}
else
{
colorMode = this->ColorMode;
}
if ( colorMode == VTK_COLOR_MODE_DEFAULT )
{
colorHash = DefineLUTColors(im, colors, numCells, bpp);
}
else //random or specified color
{
DefineColors(im, CGMColors);
}
// Setup creation of the CGM file
//
maxCellSize = input->GetVerts()->GetMaxCellSize();
maxCellSize = (input->GetLines()->GetMaxCellSize() > maxCellSize ?
input->GetLines()->GetMaxCellSize() : maxCellSize );
maxCellSize = (input->GetPolys()->GetMaxCellSize() > maxCellSize ?
input->GetPolys()->GetMaxCellSize() : maxCellSize );
maxCellSize = (input->GetStrips()->GetMaxCellSize() > maxCellSize ?
input->GetStrips()->GetMaxCellSize() : maxCellSize );
points = new cgmPoint [maxCellSize];
// If sorting is turned on, then traverse the cells, generating a depth
// value which is used for sorting.
//
if ( this->Sort )
{
depth = new vtkSortValues [numCells];
for ( cellId=0; cellId < numCells; cellId++ )
{
input->GetCell(cellId, cell);
id = cell->PointIds->GetId(0);
pts->GetPoint(id,x);
depth[cellId].z = x[2];
depth[cellId].cellId = cellId;
}
qsort(depth, numCells, sizeof(vtkSortValues), vtkCGMqsortCompare);
}
// Traverse the cells and spit out the appropriate primitives.
cgmSetShapeEdgeAttrib(im, 1, 0, 0, 0);
for ( cellId=0; cellId < numCells; cellId++ )
{
if ( this->Sort )
{
id = depth[cellId].cellId;
}
else
{
id = cellId;
}
input->GetCell(id, cell);
type = cell->GetCellType();
npts = cell->GetNumberOfPoints();
p = cell->GetPointIds()->GetPointer(0);
if ( colorMode == VTK_COLOR_MODE_DEFAULT )
{
ptr = colors + bpp*id;
switch (bpp)
{
case 1: case 2:
rgbColor[0] = *ptr;
rgbColor[1] = *ptr;
rgbColor[2] = *ptr;
break;
case 3: case 4:
rgbColor[0] = ptr[0];
rgbColor[1] = ptr[1];
rgbColor[2] = ptr[2];
break;
default:
vtkErrorMacro( << "Unsupported bpp in vtkCGMWriter::WriteData" );
rgbColor[0] = 0;
rgbColor[1] = 0;
rgbColor[2] = 0;
break;
}
color = colorHash->GetColorIndex(im, rgbColor[0], rgbColor[1], rgbColor[2]);
}
else if ( colorMode == VTK_COLOR_MODE_SPECIFIED_COLOR )
{
color = GetColor(static_cast<int>(this->SpecifiedColor[0] * 255.0),
static_cast<int>(this->SpecifiedColor[1] * 255.0),
static_cast<int>(this->SpecifiedColor[2] * 255.0),
CGMColors);
}
else //if ( colorMode == VTK_COLOR_MODE_RANDOM_COLORS )
{
color = GetColor(static_cast<int>(vtkMath::Random(0,255)),
static_cast<int>(vtkMath::Random(0,255)),
static_cast<int>(vtkMath::Random(0,255)), CGMColors);
}
switch (type)
{
case VTK_VERTEX: case VTK_POLY_VERTEX:
for (i=0; i<npts; i++)
{
scaledPts->GetPoint(p[i], x);
points[0].x = static_cast<int>(x[0]);
points[0].y = static_cast<int>(x[1]);
}
cgmPolyMarker(im, points, 1);
break;
case VTK_LINE: case VTK_POLY_LINE:
for (i=0; i<npts; i++)
{
scaledPts->GetPoint(p[i], x);
points[i].x = static_cast<int>(x[0]);
points[i].y = static_cast<int>(x[1]);
}
cgmSetLineColor(im, color);
cgmPolyLine(im, points, npts);
break;
case VTK_TRIANGLE: case VTK_QUAD: case VTK_POLYGON:
for (i=0; i<npts; i++)
{
scaledPts->GetPoint(p[i], x);
points[i].x = static_cast<int>(x[0]);
points[i].y = static_cast<int>(x[1]);
}
cgmSetShapeFillAttrib(im, 1, color, -1);
cgmPolygon(im, points, npts);
break;
case VTK_TRIANGLE_STRIP:
for (i=0; i<(npts-2); i++)
{
scaledPts->GetPoint(p[i], x);
points[0].x = static_cast<int>(x[0]);
points[0].y = static_cast<int>(x[1]);
scaledPts->GetPoint(p[i+1], x);
points[1].x = static_cast<int>(x[0]);
points[1].y = static_cast<int>(x[1]);
scaledPts->GetPoint(p[i+2], x);
points[2].x = static_cast<int>(x[0]);
points[2].y = static_cast<int>(x[1]);
}
cgmSetShapeFillAttrib(im, 1, color, -1);
cgmPolygon(im, points, 3);
break;
default:
vtkErrorMacro(<<"Unsupported CGM type");
}
}
if ( colorMode == VTK_COLOR_MODE_DEFAULT )
{
delete colorHash;
}
cell->Delete();
scaledPts->Delete();
delete [] points;
if ( this->Sort )
{
delete [] depth;
}
// Write out the CGM file
cgmImageCgm(im, outf);
// Clean up and get out
fclose(outf);
cgmImageDestroy(im); //destroys image
}
void vtkCGMWriter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
if ( this->Viewport )
{
os << indent << "Viewport: "
<< this->Viewport << "\n";
this->Viewport->PrintSelf(os, indent.GetNextIndent());
}
else
{
os << indent << "No Viewport defined\n";
}
os << indent << "Sort: " << (this->Sort ? "On\n" : "Off\n");
os << indent << "Color Mode: ";
if ( this->ColorMode == VTK_COLOR_MODE_DEFAULT )
{
os << "Default" << endl;
}
else if ( this->ColorMode == VTK_COLOR_MODE_SPECIFIED_COLOR )
{
os << "Specified Color: (" << this->SpecifiedColor[0] << ", "
<< this->SpecifiedColor[1] << ", " << this->SpecifiedColor[2] << ")\n";
}
else
{
os << "Random Colors";
}
os << indent << "Resolution: " << this->Resolution << endl;
}
//------------------private helper functions---------------------
//---the CGM functions
static int cgmImageAddColor(cgmImagePtr im, int si, int ei);
/* Creates a new image of size (sx,sy). Most people should always
* start by calling this function */
static cgmImagePtr cgmImageCreate(int sx, int sy)
{
cgmImagePtr im;
im = cgmImageStartCgm();
if (!im)
{
return 0; /* memory allocation failed */
}
if (!cgmImageSetSize(im, sx,sy))
{
free (im);
return 0;
}
if (!cgmCgmHeader(im))
{
free (im);
return 0;
}
if (cgmCgmPic(im, 0))
{
return im;
}
else
{
free(im);
return 0;
}
}
static int cgmAppNull(unsigned char *es, int x)
{
/* put x nulls in the string.
* return value is number of octets added (1) */
int y;
for(y=0; y<x; y++)
{
*es = '\0';
es++;
}
return x;
}
static int cgmAppByte(unsigned char *es, short int addme)
{
/* Append an octet to the end of es
* Return value is number of octets added
* for internal cgm functions only, do not call
*/
*es = static_cast<unsigned char>(addme) & 0377;
return 1;
}
static int cgmAppShort(unsigned char *es, short int addme)
{
/* Append a short to the end of es
* return value is number of octets added
* For internal cgm functions only, do not call!
*/
short int temp;
temp = addme >> 8;
*es = static_cast<unsigned char>(temp) & 0377;
es++;
*es = static_cast<unsigned char>(addme) & 0377;
return 2;
}
static int cgmcomhead(unsigned char *es, int elemclass, int id, int len)
{
/* sets the command header in the first two bytes of string es
* element class is in bits 15-12
* element id is in bits 11-5
* parameter list length is in bits 4-0
*/
int temp;
if (!es)
{
return 0; /* the string must be allocated first */
}
/* set the element class */
*es = static_cast<unsigned char>(elemclass) << 4;
/* set the element id */
temp = 0177 & id ;
temp = temp >> 3;
*es = *es | temp;
es++;
id = id << 5;
*es = static_cast<unsigned char>(id);
*es = *es | static_cast<unsigned char>( 037 & len );
return 1;
}
static int cgmcomheadlong(unsigned char *es, int elemclass, int id, int len)
{
/* sets the command header for the long form.
* first 16 bits:
* element class is in bits 15-12
* element id is in bits 11-5
* parameter list length is in bits 4-0 = 31
* second 16 bits:
* bit 15 = 0 (for last partition)
* bit 14-0 param list len
*/
/* I'm lazy, call cgmcomhead to set the first two bytes */
if (!cgmcomhead(es, elemclass, id, 31))
{
return 0;
}
es += 2;
/* now set the second two bytes */
cgmAppShort(es, static_cast<short int>(len));
*es = *es & 0177; /* make bit 15 = 0 */
es += 2;
return 1;
}
static int cgmAddElem(cgmImagePtr im, unsigned char *es, int octet_count)
/* adds a string, which is a CGM element to the elemlist.
* This function is called by other functions in this library and
* should NOT be called by users of the library
* For internal cgm functions only, do not call!
*/
{
unsigned char *newlist; /* in case memory allocation fails */
int x; /* counter */
while ((octet_count + 1) >= im->bytestoend)
{
/* not enough space, must grow elemlist */
im->listlen = im->listlen + CGMGROWLISTSIZE;
newlist = static_cast<unsigned char *>(
realloc(im->elemlist,SIZEOF(unsigned char ) * im->listlen));
if (newlist)
{
/* successfully allocated memory */
im->elemlist = newlist;