JunkBox_Lib 1.10.1
Loading...
Searching...
No Matches
shape_file.c
Go to the documentation of this file.
1
7#include "shape_file.h"
8#include "jbxl_state.h"
9
10
12{
13 int magic, type, rdint;
14 size_t rs;
15 UNUSED(rs);
16
17 // Magic Number
18 fseek(fp, 0L, SEEK_SET);
19 rs = fread(&rdint, 4, 1, fp);
20 magic = int_from_big_endian((void*)&rdint);
21 if (magic!=9994) {
22 DEBUG_MODE PRINT_MESG("READ_SHAPE_INDEX_FILE: But Mafic Number = %d\n", magic);
23 return NULL;
24 }
25
26 // Shape Type
27 fseek(fp, 32L, SEEK_SET);
28 rs = fread(&rdint, 4, 1, fp);
29 type = int_from_little_endian((void*)&rdint);
30
31 //
32 int i, count = 0;
33
34 fseek(fp, 100L, SEEK_SET);
35 rs = fread(&rdint, 4, 1, fp);
36 rs = fread(&rdint, 4, 1, fp);
37 while(!feof(fp)) {
38 count++;
39 rs = fread(&rdint, 4, 1, fp); // next record
40 rs = fread(&rdint, 4, 1, fp); // next record
41 }
42
43 ShapeIndex* shpidx = (ShapeIndex*)malloc(count*sizeof(ShapeIndex));
44 if (shpidx==NULL) return NULL;
45 memset(shpidx, 0, count*sizeof(ShapeIndex));
46
47 fseek(fp, 100L, SEEK_SET);
48 for (i=0; i<count; i++) {
49 shpidx[i].datanum = i;
50 shpidx[i].type = type;
51 shpidx[i].maxnum = count;
52 rs = fread(&rdint, 4, 1, fp);
53 shpidx[i].offset = int_from_big_endian((void*)&rdint);
54 rs = fread(&rdint, 4, 1, fp);
55 shpidx[i].size = int_from_big_endian((void*)&rdint);
56 }
57
58 return shpidx;
59}
60
61
63{
64 int magic, type, rdint;
65 unsigned char rdbyte[8];
66 double xmin, ymin, zmin, xmax, ymax, zmax;
67 size_t rs;
68 UNUSED(rs);
69
70 // Magic Number
71 fseek(fp, 0L, SEEK_SET);
72 rs = fread(&rdint, 4, 1, fp);
73 magic = int_from_big_endian((void*)&rdint);
74 if (magic!=9994) {
75 DEBUG_MODE PRINT_MESG("READ_SHAPE_MAIN_FILE: But Mafic Number = %d\n", magic);
76 return NULL;
77 }
78
79 // Shape Type
80 fseek(fp, 32L, SEEK_SET);
81 rs = fread(&rdint, 4, 1, fp);
82 type = int_from_little_endian((void*)&rdint);
83
84 if (idx->type!=type) {
85 DEBUG_MODE PRINT_MESG("READ_SHAPE_MAIN_FILE: Type mismatch with index file %d != %d\n", type, idx->type);
86 return NULL;
87 }
88
89 if (idx->type!=SHAPE_TYPE_POLYGON) {
90 DEBUG_MODE PRINT_MESG("READ_SHAPE_MAIN_FILE: Unsupported Shape Type = %d\n", idx->type);
91 return NULL;
92 }
93
94 // Bounding Box
95 fseek(fp, 36L, SEEK_SET);
96 rs = fread(rdbyte, 8, 1, fp);
97 xmin = double_from_little_endian((void*)rdbyte);
98 rs = fread(rdbyte, 8, 1, fp);
99 ymin = double_from_little_endian((void*)rdbyte);
100 rs = fread(rdbyte, 8, 1, fp);
101 xmax = double_from_little_endian((void*)rdbyte);
102 rs = fread(rdbyte, 8, 1, fp);
103 ymax = double_from_little_endian((void*)rdbyte);
104 rs = fread(rdbyte, 8, 1, fp);
105 zmin = double_from_little_endian((void*)rdbyte);
106 rs = fread(rdbyte, 8, 1, fp);
107 zmax = double_from_little_endian((void*)rdbyte);
108
109 //
110 if (mp==NULL) {
111 mp = (BSGraph*)malloc(sizeof(BSGraph));
112 memset(mp, 0, sizeof(BSGraph));
113 }
114 if (mp->gp==NULL) {
115 if (dd.x<=0.0) dd.x = 1.0;
116 if (dd.y<=0.0) dd.y = 1.0;
117 if (dd.z<=0.0) dd.z = 1.0;
118 int xsize = (int)((xmax - xmin)/dd.x) + 1;
119 int ysize = (int)((ymax - ymin)/dd.y) + 1;
120 int zsize = (int)((zmax - zmin)/dd.z) + 1;
121 *mp = make_BSGraph(xsize, ysize, zsize);
122 og = set_vector(xmin, ymax, zmin);
123 }
124
125 //
126 switch(idx->type) {
127
128 case SHAPE_TYPE_POLYGON :
129 draw_shape_polygon(idx, mp, og, dd, 255, fp);
130 break;
131 }
132
133 mp->state = JBXL_NORMAL;
134 return mp;
135}
136
137
138void draw_shape_polygon(ShapeIndex* idx, BSGraph* mp, vector og, vector dd, int cc, FILE* fp)
139{
140 size_t rs;
141 UNUSED(rs);
142
143 int i, j, k, rdint;
144 int kk = (int)(og.z + 0.5); // color
145 if (kk<0 || kk>mp->zs-1) kk = 0;
146
147 ShapeIndex* sx = idx;
148
149 for (i=0; i<idx->maxnum; i++) {
150 fseek(fp, sx->offset*2+8, SEEK_SET);
151 rs = fread(&rdint, 4, 1, fp);
152 int type = int_from_little_endian((void*)&rdint);
153 if (type!=SHAPE_TYPE_POLYGON) return;
154
155 fseek(fp, 32L, SEEK_CUR);
156 rs = fread(&rdint, 4, 1, fp);
157 int parts = int_from_little_endian((void*)&rdint);
158 rs = fread(&rdint, 4, 1, fp);
159 int points = int_from_little_endian((void*)&rdint);
160
161 for (j=0; j<parts; j++) {
162 fseek(fp, sx->offset*2+52+j*4, SEEK_SET); // offset*2 + 8 + 44 + j*4
163 rs = fread(&rdint, 4, 1, fp);
164 int sttidx = int_from_little_endian((void*)&rdint);
165 int endidx = points - 1;
166
167 if (j<parts-1) {
168 rs = fread(&rdint, 4, 1, fp);
169 endidx = int_from_little_endian((void*)&rdint) - 1;
170 }
171
172 double xx, yy, ox, oy;
173 ox = oy = 0.0;
174 fseek(fp, sx->offset*2+52+parts*4+sttidx*16, SEEK_SET); // offset*2 + 8 + 44 + parts*4 + sttidx*16
175
176 for (k=sttidx; k<=endidx; k++) {
177 rs = fread(&xx, 8, 1, fp);
178 rs = fread(&yy, 8, 1, fp);
179 xx = double_from_little_endian((void*)&xx);
180 yy = double_from_little_endian((void*)&yy);
181
182 if (k!=sttidx) {
183 int oi = (int)((ox-og.x)/dd.x + 0.5);
184 int oj = (int)((og.y-oy)/dd.y + 0.5);
185 int ii = (int)((xx-og.x)/dd.x + 0.5);
186 int jj = (int)((og.y-yy)/dd.y + 0.5);
187 bline3d(*mp, oi, oj, kk, ii, jj, kk, cc);
188 }
189 ox = xx;
190 oy = yy;
191 }
192 }
193 sx++;
194 }
195}
196
#define UNUSED(x)
Definition common.h:264
BSGraph make_BSGraph(int xs, int ys, int zs)
Definition gdata.c:66
void bline3d(BSGraph gd, int x1, int y1, int z1, int x2, int y2, int z2, int cc)
Definition graph.c:613
JunkBox_Lib 状態ヘッダ
#define JBXL_NORMAL
正常
Definition jbxl_state.h:32
vector set_vector(double x, double y, double z)
Definition matrix.c:82
BSGraph * read_shape_main_file(ShapeIndex *idx, BSGraph *mp, vector og, vector dd, FILE *fp)
Definition shape_file.c:62
ShapeIndex * read_shape_index_file(FILE *fp)
Definition shape_file.c:11
void draw_shape_polygon(ShapeIndex *idx, BSGraph *mp, vector og, vector dd, int cc, FILE *fp)
Definition shape_file.c:138
SHAPE FILE TOOL Header.
#define SHAPE_TYPE_POLYGON
Definition shape_file.h:18
int zs
zサイズ. 4Byte. 2Dの場合は 1.
Definition gdata.h:30
int state
状態
Definition gdata.h:31
uByte * gp
グラフィックデータへのポインタ. xs*ys*zs*1Byte.
Definition gdata.h:32
double z
z方向成分
Definition matrix.h:32
double y
y方向成分
Definition matrix.h:31
double x
x方向成分
Definition matrix.h:30
int int_from_little_endian(void *ptr)
Definition tools.c:192
int int_from_big_endian(void *ptr)
Definition tools.c:300
double double_from_little_endian(void *ptr)
エンディアンによるデータ変換
Definition tools.c:156
#define PRINT_MESG
環境依存用の出力関数.print_message()
Definition tools.h:475
#define DEBUG_MODE
Definition tools.h:502