File: polygon.C

package info (click to toggle)
battleball 2.0-13
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,016 kB
  • ctags: 3,097
  • sloc: cpp: 15,310; makefile: 48; csh: 34
file content (206 lines) | stat: -rw-r--r-- 4,717 bytes parent folder | download | duplicates (6)
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
/*
 *	file:		polygon.c
 *	description:	polygon module
 *	author:		A. T. Campbell, III
 *	date:		February 9, 1995
 */

#include <math.h>
#include <stdlib.h>
#include "polygon.h"
#include "brep.h"
#include "bspvector.h"

/* copy a polygon */
void
PolygonCopy3f(Polygon3f *src, Polygon3f *dest)
{
	int i;
	dest->numverts = src->numverts;
	for (i = 0; i < src->numverts; i++)
		Vcopy3f(src->verts[i], dest->verts[i]);
	Vcopy3f(src->plane_normal, dest->plane_normal);
	dest->material_index = src->material_index;
	dest->facesForward= src->facesForward;
}

/* allocate memory */
Polygon3f *
PolygonCreate3f(void)
{
	Polygon3f *p;
	p = (Polygon3f *)malloc(sizeof(Polygon3f));
	if (p != NULL) {
		p->numverts = 0;
		Vset3f(0., 0., 0., p->plane_normal);
		p->material_index = 0;
	}
	return p;
}

/* split a convex polygon by a plane */
void
PolygonCutConvex3f(Polygon3f *p, Vec3f pntOnPlane, Vec3f planeNormal,
		   int maxPieces,
		   int *numpos, Polygon3f *posarr[],
		   int *numneg, Polygon3f *negarr[])
{
	E_side side;
	Vec4f coeffs;

	side = PolygonSidePlane3f(p, pntOnPlane, planeNormal);
	if (side == sidePositive) {
		*numpos = 1;
		*numneg = 0;
		posarr[0] = PolygonCreate3f();
		PolygonCopy3f(p, posarr[0]);
	}
	else if (side == sideNegative) {
		*numpos = 0;
		*numneg = 1;
		negarr[0] = PolygonCreate3f();
		PolygonCopy3f(p, negarr[0]);
	}
	else if (side == sideBoth) {
		/* Dave Carver gets to write this one */
		*numpos = *numneg = 0;
		posarr[0] = PolygonCreate3f();
		negarr[0] = PolygonCreate3f();
		Vset4f (planeNormal[0], planeNormal[1], planeNormal[2],
			-Vdot3f(pntOnPlane,planeNormal),coeffs);
		brep_split(planeNormal, p->numverts, p->verts, UNIVEPS,
			&(posarr[0]->numverts), posarr[0]->verts, 
			&(negarr[0]->numverts), negarr[0]->verts);
		if (posarr[0]->numverts == 0)
			PolygonDestroy3f(posarr[0]);
		else {
			*numpos = 1;
			Vcopy3f(p->plane_normal, posarr[0]->plane_normal);
			posarr[0]->material_index = p->material_index;
		}
		if (negarr[0]->numverts == 0)
			PolygonDestroy3f(negarr[0]);
		else {
			*numneg = 1;
			Vcopy3f(p->plane_normal, negarr[0]->plane_normal);
			negarr[0]->material_index = p->material_index;
		}
	}
	else { /* side = sideNeither */
		*numpos = *numneg = 0;
	}
		
}

/* free memory */
void
PolygonDestroy3f(Polygon3f *p)
{
	if (p != NULL)
		free((char *)p);
}

/* compute normal to a polygon */
/* use Newell's formulation (Foley, Van Dam, Feiner, & Hughes, pp. 477) */
void
PolygonNormal3f(Polygon3f *p, Vec3f normv)
{
	Vec3f *v;
	int i, ip1, n;
	if (p == NULL) return;
	n = p->numverts;
	v = p->verts;
	Vset3f(0., 0., 0., normv);
	i = n - 1;
	for (ip1 = 0; ip1 < n; ip1++) {
		normv[0] += (v[i][1] - v[ip1][1]) *
			(v[i][2] + v[ip1][2]);
		normv[1] += (v[i][2] - v[ip1][2]) *
			(v[i][0] + v[ip1][0]);
		normv[2] += (v[i][0] - v[ip1][0]) *
                                   (v[i][1] + v[ip1][1]);
		i = ip1;
	}
	Vnormalize3f(normv);
	
}

/* print information about a polygon */
void
PolygonPrint3f(FILE *fp, Polygon3f *p)
{
	int i;
	fprintf(fp, "numverts=%d material_index=%d\n", p->numverts, p->material_index);
	for (i = 0; i < p->numverts; i++)
		fprintf(fp, "%f %f %f\n",
			p->verts[i][0], p->verts[i][1], p->verts[i][2]);
}

/* set the vertices of a polygon */
void
PolygonSet3f(Polygon3f *p, int n, Vec3f v[])
{
	int i;
	p->numverts = n;
	for (i = 0; i < n; i++)
		Vcopy3f(v[i], p->verts[i]);
}

/* determine what side of a plane a polygon is on */
E_side
PolygonSidePlane3f(Polygon3f *p, Vec3f pntOnPlane, Vec3f planeNormal)
{
	int positive, negative;
	Vec4f planeCoeffs;
	float testVal;
	int i;

	/* compute planar equation */
	Vset4f(planeNormal[0], planeNormal[1], planeNormal[2],
	       -Vdot3f(pntOnPlane, planeNormal), planeCoeffs);

	/* test all vertices */
	positive = negative = 0;
	for (i = 0; i < p->numverts; i++) {
		testVal = planeCoeffs[0]*p->verts[i][0] +
			planeCoeffs[1]*p->verts[i][1] +
			planeCoeffs[2]*p->verts[i][2] +
			planeCoeffs[3];
		if (testVal < 0)
			negative = 1;
		if (testVal > 0)
			positive = 1;
	}

	/* determine return value */
	if (positive && !negative)
		return sidePositive;
	else if (negative && !positive)
		return sideNegative;
	else if (negative && positive)
		return sideBoth;
	else
		return sideNeither;
}

/* translate the vertices of a polygon */
void
PolygonTranslate3f(Polygon3f *p, Vec3f delta)
{
	int i;
	for (i = 0; i < p->numverts; i++)
		Vadd3f(delta, p->verts[i], p->verts[i]);
}

/* get the bounding box of a polygon */
void PolygonBBox3f(Polygon3f *p, Vec3f low, Vec3f high) {
  int i;
  if (p->numverts <1) return;
  
  Vcopy3f(p->verts[0],low);
  Vcopy3f(p->verts[0],high);
  for (i=1; i <p->numverts; i++) {
    Vmin3f(low,p->verts[i],low);
    Vmax3f(high,p->verts[i],high);
  }
}