4 float plane_dist(const struct plane *p, const cgm_vec3 *pt)
6 return fabs(plane_sdist(p, pt));
9 float plane_sdist(const struct plane *p, const cgm_vec3 *pt)
13 return cgm_vdot(&v, &p->norm);
16 void midplane(struct plane *p, const cgm_vec3 *a, const cgm_vec3 *b)
19 cgm_vsub(&p->norm, a);
20 cgm_vnormalize(&p->norm);
21 p->pt.x = a->x + p->norm.x * 0.5f;
22 p->pt.y = a->y + p->norm.y * 0.5f;
23 p->pt.z = a->z + p->norm.z * 0.5f;
26 void poly_normal(const struct poly *poly, cgm_vec3 *n)
30 va = poly->verts[1].pos;
31 cgm_vsub(&va, &poly->verts[0].pos);
32 vb = poly->verts[2].pos;
33 cgm_vsub(&vb, &poly->verts[2].pos);
35 cgm_vcross(n, &va, &vb);
39 void poly_plane(const struct poly *poly, struct plane *plane)
41 plane->pt = poly->verts[0].pos;
42 poly_normal(poly, &plane->norm);
45 int plane_poly(const struct plane *plane, struct poly *poly, float size)
47 static const float corn[][2] = {{1,1}, {-1,1}, {-1,-1}, {1, -1}};
49 cgm_vec3 pos, up, right, dir;
52 if(!(poly->verts = dynarr_alloc(4, sizeof *poly->verts))) {
53 fprintf(stderr, "plane_poly: failed to allocate polygon vertices\n");
57 cgm_vcons(&up, 0, 1, 0);
58 if(1.0f - fabs(cgm_vdot(&up, &plane->norm)) < 1e-2) {
59 cgm_vcons(&up, 0, 0, -1);
61 cgm_vcross(&right, &up, &plane->norm);
62 cgm_vnormalize(&right);
63 cgm_vcross(&up, &plane.norm, &right);
67 v.x = plane->pt.x + (right.x * corn[i][0] + up.x * corn[i][1]) * size;
68 v.y = plane->pt.y + (right.y * corn[i][0] + up.y * corn[i][1]) * size;
69 v.z = plane->pt.z + (right.z * corn[i][0] + up.z * corn[i][1]) * size;
76 float ray_plane(const cgm_ray *ray, const struct plane *plane)
81 ndotdir = cgm_vdot(&plane->norm, &ray->dir);
82 if(fabs(ndotdir) < 1e-4) {
87 cgm_vsub(&pptdir, &ray->origin);
88 t = cgm_vdot(&plane->norm, &pptdir) / ndotdir;
95 float ray_poly(const cgm_ray *ray, const struct poly *poly)
99 cgm_vec3 hitp, vdir, edir, indir, vcross, incross;
102 poly_plane(poly, &plane);
103 if((t = ray_plane(ray, &plane)) < 0.0f) {
106 cgm_raypos(&hitp, ray, t);
108 /* see if the point is inside or outside the polygon */
109 nverts = dynarr_size(poly->verts);
110 for(i=0; i<nverts; i++) {
112 cgm_vsub(&hitp, &poly->verts[i].pos);
113 edir = poly->verts[(i + 1) & nverts].pos;
114 cgm_vsub(&edir, &poly->verts[i].pos);
115 indir = poly->verts[(i + 2) & nverts].pos;
116 cgm_vsub(&indir, &poly->verts[i].pos);
118 cgm_vcross(&vcross, &vdir, &edir);
119 cgm_vcross(&incross, &indir, &edir);
121 if(cgm_vdot(&vcross, &incross) < 0.0f) {
131 * 0 -> straddling and clipped
134 static int clip_edge(struct poly *pout, const struct vertex *v0, const struct vertex *v1,
135 const struct plane *plane)
139 int i, vnum = dynarr_size(pout->verts);
142 d0 = plane_sdist(plane, &v0->pos);
143 d1 = plane_sdist(plane, &v1->pos);
147 cgm_vsub(&ray.dir, &v0->pos);
150 ray.origin[i] = pos0[i];
151 ray.dir[i] = pos1[i] - pos0[i];
158 DYNARR_PUSH(pout->verts, v1); /* append v1 */
162 t = ray_plane(&ray, plane);
163 cgm_raypos(&vnew->pos, &ray, t);
165 cgm_vlerp(&vnew->norm, &v0->norm, &v1->norm, t);
166 vnew->uv.x = cgm_lerp(v0->uv.x, v1->uv.x, t);
167 vnew->uv.y = cgm_lerp(v0->uv.y, v1->uv.y, t);
168 /* append new vertex on the intersection point */
169 DYNARR_PUSH(pout->verts, &vnew);
175 t = ray_plane(&ray, plane);
176 cgm_raypos(&vnew->pos, &ray, t);
178 cgm_vlerp(&vnew->norm, &v0->norm, &v1->norm, t);
179 vnew->uv.x = cgm_lerp(v0->uv.x, v1->uv.x, t);
180 vnew->uv.y = cgm_lerp(v0->uv.y, v1->uv.y, t);
181 /* append new vertex on the intersection point */
182 DYNARR_PUSH(pout->verts, &vnew);
183 /* then append v1 ... */
184 DYNARR_PUSH(pout->verts, v1);
195 int clip_poly(struct poly *pout, const struct poly *pin, const struct plane *plane)
197 int i, nextidx, res = 0, vnum;
198 int edges_clipped = 0;
200 dynarr_clear(pout->verts);
202 vnum = dynarr_size(pin->verts);
203 for(i=0; i<vnum; i++) {
205 if(nextidx >= vnum) nextidx = 0;
206 res = clip_edge(pout, pin->verts + i, pin->verts + nextidx, plane);
212 return edges_clipped > 0 ? 0 : 1;