forked from markjessell/functionNoddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3dBitmap.c
327 lines (298 loc) · 7.58 KB
/
3dBitmap.c
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
/*****************************************************************************
* File: 3dBitmaps.c
*
* Purpose: Conversion of 2d bit mapped images to 3d faceted solids.
*
* © 1989 Mark M. Owen. All rights reserved.
*****************************************************************************
*/
#include "3dHier.h"
#include "3dColor.h"
#include "3dBitmap.h"
#include "3dFMath.h"
#define ENDSCANMARK 0x80000000
#if XVT_CC_PROTO
static int IsolateAdjacent (Fixed,Fixed,Fixed,int,Point3d*,Fixed**,Fixed**);
#else
static int IsolateAdjacent ();
#endif
/*****************************************************************************
*
* Function: FacetBlock(É)
*
* Purpose: Given the end points of two scan lines segments emits an array
* of 6 facets (a rectangular solid) and 8 associated vertices,
* and updates the vertex and facet counts passed as parameters.
* The depth of the block generated is equal to twice the depth
* requested by the parameter (d).
*
* Returns: nada, except via parameters
*
*****************************************************************************
*/
void FacetBlock(p0,p1,p2,p3,d,nV,pV,nF,pF)
Point3d *p0,*p1,*p2,*p3; /* scan line segments */
Fixed d; /* extrusion depth */
int *nV; /* number of vertices */
Vrtx _huge *pV; /* array of vertices */
int *nF; /* number of facets */
Facet _huge *pF; /* array of facets */
{
int i;
/*
8 Points:
TLn TRn P0 P3 P4 P7
BLn BRn n=0..1 P1 P2 P5 P6
6 Facets:
P0 P3 P3 P7 P7 P4 P4 P0 P0 P4 P1 P2
P1 P2 P2 P6 P6 P5 P5 P1 P3 P7 P5 P6
*/
i=*nV;
pV[0+i].p.x = p0->x;
pV[0+i].p.y = p0->y;
pV[0+i].p.z = -d;
pV[1+i].p.x = p1->x;
pV[1+i].p.y = p1->y;
pV[1+i].p.z = -d;
pV[2+i].p.x = p2->x;
pV[2+i].p.y = p2->y;
pV[2+i].p.z = -d;
pV[3+i].p.x = p3->x;
pV[3+i].p.y = p3->y;
pV[3+i].p.z = -d;
pV[4+i].p.x = p0->x;
pV[4+i].p.y = p0->y;
pV[4+i].p.z = d;
pV[5+i].p.x = p1->x;
pV[5+i].p.y = p1->y;
pV[5+i].p.z = d;
pV[6+i].p.x = p2->x;
pV[6+i].p.y = p2->y;
pV[6+i].p.z = d;
pV[7+i].p.x = p3->x;
pV[7+i].p.y = p3->y;
pV[7+i].p.z = d;
pV[0+i].impure = FALSE;
pV[1+i].impure = FALSE;
pV[2+i].impure = FALSE;
pV[3+i].impure = FALSE;
pV[4+i].impure = FALSE;
pV[5+i].impure = FALSE;
pV[6+i].impure = FALSE;
pV[7+i].impure = FALSE;
MakeRectFacet(pF,(*nF)++,0 + i, 1 + i, 2 + i, 3 + i,pV);
MakeRectFacet(pF,(*nF)++,3 + i, 2 + i, 6 + i, 7 + i,pV);
MakeRectFacet(pF,(*nF)++,7 + i, 6 + i, 5 + i, 4 + i,pV);
MakeRectFacet(pF,(*nF)++,4 + i, 5 + i, 1 + i, 0 + i,pV);
MakeRectFacet(pF,(*nF)++,0 + i, 3 + i, 7 + i, 4 + i,pV);
MakeRectFacet(pF,(*nF)++,1 + i, 5 + i, 6 + i, 2 + i,pV);
*nV+=8;
}
/*****************************************************************************
*
* Function: IsolateAdjacent(É)
*
* Purpose: Determines the extents of any adjacent elements on the next
* scan line to be processed.
*
* Warnings: Private Function
*
* Returns: the number of adjacent elements, and via parameters the left
* and right limits of any adjacent elements.
*
*****************************************************************************
*/
static int IsolateAdjacent(L,R,Y,iP,pP,pL,pR)
Fixed L,R,Y;
int iP;
Point3d *pP;
Fixed **pL,**pR;
{
int nA=0; /* number of adjacent elements */
int i,j;
if( (pP[iP].x==ENDSCANMARK)&&(pP[iP].y==ENDSCANMARK)&&(pP[iP].z==ENDSCANMARK) )
return 0;
for(i=iP;i>0;i-=2)
if (pP[i].y>Y+1.0) /* not on the next Y scan line */
break;
else
if ( (pP[i].y==Y)
&& (pP[i].x>=L)
&& (pP[i-1].x<=R)
)
nA++;
if(!nA)
return 0;
*pL=(Fixed*)xvt_mem_alloc(sizeof(Fixed)*nA);
if(!pL)
return 0;
*pR=(Fixed*)xvt_mem_alloc(sizeof(Fixed)*nA);
if(!pR)
return 0;
for(i=iP,j=nA-1;i>0;i-=2)
if (pP[i].y>Y+1.0) /* not on the next Y scan line */
break;
else
if ( (pP[i].y==Y)
&& (pP[i].x>=L)
&& (pP[i-1].x<=R)
)
{
(*pL)[j]=pP[i-1].x;
(*pR)[j]=pP[i].x;
j--;
}
return nA;
}
/*****************************************************************************
*
* Function: Generate3dSegs(É)
*
* Purpose: Given an array of line segments (represented as vertex pairs)
* generates and attaches a surface patch to a specified group.
* The patch generated will consist of a number of rectangular
* blocks, one for each line segment, whose depth (in terms of z
* axis) is twice the depth parameter supplied.
*
* Returns: MEMORY_ALLOC_ERROR if memory allocation fails
* TRUE if successfull
*
*****************************************************************************
*/
int Generate3dSegs(pG,ixP,pPt,nP,depth,raOptions,xf) /* retained mode */
pGroup pG;
int ixP; /* output patch index */
Point3d *pPt;
int nP; /* number of input points */
int depth; /* depth to extrude line segments to (z axis) */
RendAttr raOptions;
Matrix3D *xf;
{
Vrtx _huge *pV;
Facet _huge *pF;
int nV, nF;
Point3d p0,p1,p2,p3;
Fixed *pPaL;
Fixed *pPaR;
Fixed L,R;
int nA;
pV = (Vrtx _huge *) xvt_mem_halloc(4*nP, sizeof(Vrtx));
if( !pV )
return MEMORY_ALLOC_ERROR;
pF = (Facet _huge *) xvt_mem_halloc(3*nP, sizeof(Facet));
if( !pF )
{
xvt_mem_hfree((char _huge *) pV);
return MEMORY_ALLOC_ERROR;
}
if (!depth)
depth=2;
nV=0;
nF=0;
while(nP-4)
{
p0 = pPt[nP-2];
p1 = pPt[nP-2];
p2 = pPt[nP-1];
p3 = pPt[nP-1];
p0.y+=1.0;
p3.y+=1.0;
L=p1.x;
R=p2.x;
nA = IsolateAdjacent(L,R,p0.y,nP-3,pPt,&pPaL,&pPaR);
if(nA)
{ int i=0,n=nA;
while(nA--)
{
if(i==0)
if(pPaR[nA]>L)
p3.x=pPaR[nA];
if(pPaL[nA]<R)
p0.x=pPaL[nA];
i++;
}
xvt_mem_free( (char *)pPaL);
xvt_mem_free( (char *)pPaR);
}
FacetBlock(&p0,&p1,&p2,&p3,Int2Fix(depth),&nV,pV,&nF,pF);
nP-=2;
}
if(NewPatch(pG,ixP,nV,pV,nF,pF,raOptions,xf) != TRUE)
{
xvt_mem_hfree((char _huge *) pF);
xvt_mem_hfree((char _huge *) pV);
return MEMORY_ALLOC_ERROR;
}
xvt_mem_hfree((char _huge *) pF);
xvt_mem_hfree((char _huge *) pV);
return TRUE;
}
/*****************************************************************************
*
* Function: BitMapTo3d(É)
*
* Purpose: Given an input bitmap, converts the black portions of each
* horizontal scan line to a pair of vertices representing a 3d
* line segment. If successfull, returns the number of points
* generated and an array of points via parameters.
*
* Returns: MEMORY_ALLOC_ERROR if memory allocation fails
* TRUE if successfull
*
*****************************************************************************
*/
int BitMapTo3d(image, ppPt, pInt)
XVT_IMAGE image;
Point3d **ppPt;
int *pInt;
{
Point3d _huge *pPt;
PNT mmax;
int i,x,y,tx,ty;
xvt_image_get_dimensions (image, &(mmax.h), &(mmax.v));
tx = mmax.h/2;
ty = mmax.v/2;
pPt = (Point3d _huge *) xvt_mem_alloc(sizeof(Point3d)*mmax.h*mmax.v+4);
if( !pPt )
return MEMORY_ALLOC_ERROR;
pPt[0].x = ENDSCANMARK;
pPt[0].y = ENDSCANMARK;
pPt[0].z = ENDSCANMARK;
pPt[1] = pPt[0];
pPt[2] = pPt[1];
pPt[3] = pPt[2];
for( y=0,i=4; y<mmax.v; y++ )
{
x=0;
while( x < mmax.h )
{
if (xvt_image_get_pixel (image, (short) x, (short) y) != COLOR_BLACK)
{
pPt[i].x = Int2Fix(x-tx);
pPt[i].y = -Int2Fix(y-ty);
pPt[i].z = 0.0;
pPt[i+1] = pPt[i];
i++;
while( x++ < mmax.h )
if (xvt_image_get_pixel (image, (short) x, (short) y) != COLOR_BLACK)
pPt[i].x = Int2Fix(x-tx);
else
break;
if(pPt[i].x==pPt[i-1].x)
pPt[i].x+=1.0;
i++;
}
x++;
}
}
*ppPt = (Point3d _huge *) xvt_mem_halloc (i, sizeof(Point3d));
if( !*ppPt )
{
xvt_mem_hfree((char _huge *) pPt);
return MEMORY_ALLOC_ERROR;
}
*pInt = i;
hmemcpy((char *) *ppPt, (char *) pPt, sizeof(Point3d)*i);
xvt_mem_hfree((char _huge *)pPt);
return TRUE;
}