5 float plane_dist(const struct plane *p, const cgm_vec3 *pt)
7 return fabs(plane_sdist(p, pt));
10 float plane_sdist(const struct plane *p, const cgm_vec3 *pt)
14 return cgm_vdot(&v, &p->norm);
17 void midplane(struct plane *p, const cgm_vec3 *a, const cgm_vec3 *b)
20 cgm_vsub(&p->norm, a);
21 cgm_vnormalize(&p->norm);
22 p->pt.x = a->x + p->norm.x * 0.5f;
23 p->pt.y = a->y + p->norm.y * 0.5f;
24 p->pt.z = a->z + p->norm.z * 0.5f;
27 void poly_normal(const struct poly *poly, cgm_vec3 *n)
31 va = poly->verts[1].pos;
32 cgm_vsub(&va, &poly->verts[0].pos);
33 vb = poly->verts[2].pos;
34 cgm_vsub(&vb, &poly->verts[2].pos);
36 cgm_vcross(n, &va, &vb);
40 void poly_plane(const struct poly *poly, struct plane *plane)
42 plane->pt = poly->verts[0].pos;
43 poly_normal(poly, &plane->norm);
46 int plane_poly(const struct plane *plane, struct poly *poly, float size)
48 static const float corn[][2] = {{1,1}, {-1,1}, {-1,-1}, {1, -1}};
54 if(!(poly->verts = dynarr_alloc(4, sizeof *poly->verts))) {
55 fprintf(stderr, "plane_poly: failed to allocate polygon vertices\n");
59 cgm_vcons(&up, 0, 1, 0);
60 if(1.0f - fabs(cgm_vdot(&up, &plane->norm)) < 1e-2) {
61 cgm_vcons(&up, 0, 0, -1);
63 cgm_vcross(&right, &up, &plane->norm);
64 cgm_vnormalize(&right);
65 cgm_vcross(&up, &plane->norm, &right);
70 vptr->pos.x = plane->pt.x + (right.x * corn[i][0] + up.x * corn[i][1]) * size;
71 vptr->pos.y = plane->pt.y + (right.y * corn[i][0] + up.y * corn[i][1]) * size;
72 vptr->pos.z = plane->pt.z + (right.z * corn[i][0] + up.z * corn[i][1]) * size;
74 vptr->norm = plane->norm;
75 vptr->uv.x = vptr->uv.y = 0;
81 float ray_plane(const cgm_ray *ray, const struct plane *plane)
86 ndotdir = cgm_vdot(&plane->norm, &ray->dir);
87 if(fabs(ndotdir) < 1e-4) {
92 cgm_vsub(&pptdir, &ray->origin);
93 t = cgm_vdot(&plane->norm, &pptdir) / ndotdir;
100 float ray_poly(const cgm_ray *ray, const struct poly *poly)
104 cgm_vec3 hitp, vdir, edir, indir, vcross, incross;
107 poly_plane(poly, &plane);
108 if((t = ray_plane(ray, &plane)) < 0.0f) {
111 cgm_raypos(&hitp, ray, t);
113 /* see if the point is inside or outside the polygon */
114 nverts = dynarr_size(poly->verts);
115 for(i=0; i<nverts; i++) {
117 cgm_vsub(&hitp, &poly->verts[i].pos);
118 edir = poly->verts[(i + 1) & nverts].pos;
119 cgm_vsub(&edir, &poly->verts[i].pos);
120 indir = poly->verts[(i + 2) & nverts].pos;
121 cgm_vsub(&indir, &poly->verts[i].pos);
123 cgm_vcross(&vcross, &vdir, &edir);
124 cgm_vcross(&incross, &indir, &edir);
126 if(cgm_vdot(&vcross, &incross) < 0.0f) {
136 * 0 -> straddling and clipped
139 static int clip_edge(struct poly *pout, const struct vertex *v0, const struct vertex *v1,
140 const struct plane *plane)
146 d0 = plane_sdist(plane, &v0->pos);
147 d1 = plane_sdist(plane, &v1->pos);
149 ray.origin = v0->pos;
151 cgm_vsub(&ray.dir, &v0->pos);
157 DYNARR_PUSH(pout->verts, v1); /* append v1 */
161 t = ray_plane(&ray, plane);
162 cgm_raypos(&vnew.pos, &ray, t);
164 cgm_vlerp(&vnew.norm, &v0->norm, &v1->norm, t);
165 vnew.uv.x = cgm_lerp(v0->uv.x, v1->uv.x, t);
166 vnew.uv.y = cgm_lerp(v0->uv.y, v1->uv.y, t);
167 /* append new vertex on the intersection point */
168 DYNARR_PUSH(pout->verts, &vnew);
174 t = ray_plane(&ray, plane);
175 cgm_raypos(&vnew.pos, &ray, t);
177 cgm_vlerp(&vnew.norm, &v0->norm, &v1->norm, t);
178 vnew.uv.x = cgm_lerp(v0->uv.x, v1->uv.x, t);
179 vnew.uv.y = cgm_lerp(v0->uv.y, v1->uv.y, t);
180 /* append new vertex on the intersection point */
181 DYNARR_PUSH(pout->verts, &vnew);
182 /* then append v1 ... */
183 DYNARR_PUSH(pout->verts, v1);
194 int clip_poly(struct poly *pout, const struct poly *pin, const struct plane *plane)
196 int i, nextidx, res = 0, vnum;
197 int edges_clipped = 0;
199 dynarr_clear(pout->verts);
201 vnum = dynarr_size(pin->verts);
202 for(i=0; i<vnum; i++) {
204 if(nextidx >= vnum) nextidx = 0;
205 res = clip_edge(pout, pin->verts + i, pin->verts + nextidx, plane);
211 return edges_clipped > 0 ? 0 : 1;