Skip to content

Commit 7b4f444

Browse files
committed
Make various corrections to polygon and projection logic
Hello rabbit hole. When trying to construct ARBN primitives from ARB8 primitives, found a problem with the closest point calculation that was being masked in other use cases. Also saw an issue where passing point_t rather than a pointer to the point_t was causing a problem. Correct the libbg plane/point unit tests, updated a few of the ell drawing images which were showing pixel differences after fixing the projection issue, and verified the qged polygon drawing works.
1 parent 3bb2f40 commit 7b4f444

21 files changed

+116
-111
lines changed

include/bg/plane.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1156,13 +1156,13 @@ BG_EXPORT extern int bg_fit_plane(point_t *c, vect_t *n, size_t npnts, point_t *
11561156
* @brief
11571157
* Find the closest U,V point on the plane p to 3d point pt.
11581158
*/
1159-
BG_EXPORT extern int bg_plane_closest_pt(fastf_t *u, fastf_t *v, plane_t p, point_t pt);
1159+
BG_EXPORT extern int bg_plane_closest_pt(fastf_t *u, fastf_t *v, plane_t *p, point_t *pt);
11601160

11611161
/**
11621162
* @brief
11631163
* Return the 3D point on the plane at parametric coordinates u, v.
11641164
*/
1165-
BG_EXPORT extern int bg_plane_pt_at(point_t *pt, plane_t p, fastf_t u, fastf_t v);
1165+
BG_EXPORT extern int bg_plane_pt_at(point_t *pt, plane_t *p, fastf_t u, fastf_t v);
11661166

11671167

11681168

include/bv/polygon.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ struct bv_polygon {
7676
BV_EXPORT extern struct bv_scene_obj *bv_create_polygon_obj(struct bview *v, int flags, struct bv_polygon *p);
7777

7878
// Creates a scene object with a default polygon
79-
BV_EXPORT extern struct bv_scene_obj *bv_create_polygon(struct bview *v, int flags, int type, point_t fp);
79+
BV_EXPORT extern struct bv_scene_obj *bv_create_polygon(struct bview *v, int flags, int type, point_t *fp);
8080

8181
// Various update modes have similar logic - we pass in the flags to the update
8282
// routine to enable/disable specific portions of the overall flow.
@@ -92,9 +92,9 @@ BV_EXPORT extern int bv_update_polygon(struct bv_scene_obj *s, struct bview *v,
9292
BV_EXPORT extern void bv_polygon_vlist(struct bv_scene_obj *s);
9393

9494
// Find the closest polygon obj to a point
95-
BV_EXPORT extern struct bv_scene_obj *bv_select_polygon(struct bu_ptbl *objs, point_t cp);
95+
BV_EXPORT extern struct bv_scene_obj *bv_select_polygon(struct bu_ptbl *objs, point_t *cp);
9696

97-
BV_EXPORT extern int bv_move_polygon(struct bv_scene_obj *s, point_t cp, point_t pp);
97+
BV_EXPORT extern int bv_move_polygon(struct bv_scene_obj *s, point_t *cp, point_t *pp);
9898
BV_EXPORT extern struct bv_scene_obj *bv_dup_view_polygon(const char *nname, struct bv_scene_obj *s);
9999

100100
// Copy a bv polygon. Note that this also performs a

src/libbg/plane.cpp

+14-14
Original file line numberDiff line numberDiff line change
@@ -2608,18 +2608,18 @@ bg_fit_plane(point_t *c, vect_t *n, size_t npnts, point_t *pnts)
26082608

26092609
/* Translate the OpenNURBS algorithm to VMATH types */
26102610
extern "C" int
2611-
bg_plane_closest_pt(fastf_t *u, fastf_t *v, plane_t p, point_t pt)
2611+
bg_plane_closest_pt(fastf_t *u, fastf_t *v, plane_t *p, point_t *pt)
26122612
{
2613-
if (!u || !v)
2613+
if (!u || !v || !p || !pt)
26142614
return -1;
26152615

26162616
point_t origin;
26172617
vect_t xaxis, yaxis, zaxis;
2618-
VSET(zaxis, p[0], p[1], p[2]);
2618+
VSET(zaxis, (*p)[0], (*p)[1], (*p)[2]);
26192619
fastf_t x, y, z, d;
2620-
x = fabs(p[0]);
2621-
y = fabs(p[1]);
2622-
z = fabs(p[2]);
2620+
x = fabs((*p)[0]);
2621+
y = fabs((*p)[1]);
2622+
z = fabs((*p)[2]);
26232623
d = 0.0;
26242624
if ( y >= x && y >= z ) {
26252625
d = x;
@@ -2642,14 +2642,14 @@ bg_plane_closest_pt(fastf_t *u, fastf_t *v, plane_t p, point_t pt)
26422642
return -1;
26432643

26442644
VMOVE(origin, zaxis);
2645-
VSCALE(origin, origin, -1/d*p[3]);
2645+
VSCALE(origin, origin, 1/d*(*p)[3]);
26462646
VUNITIZE(zaxis);
26472647
bn_vec_perp(xaxis, zaxis);
26482648
VUNITIZE(xaxis);
26492649
VCROSS(yaxis, zaxis, xaxis);
26502650
VUNITIZE(yaxis);
26512651
vect_t vc;
2652-
VSUB2(vc, pt, origin);
2652+
VSUB2(vc, *pt, origin);
26532653

26542654
*u = VDOT(vc, xaxis);
26552655
*v = VDOT(vc, yaxis);
@@ -2659,18 +2659,18 @@ bg_plane_closest_pt(fastf_t *u, fastf_t *v, plane_t p, point_t pt)
26592659

26602660
/* Translate the OpenNURBS algorithm to VMATH types */
26612661
extern "C" int
2662-
bg_plane_pt_at(point_t *pt, plane_t p, fastf_t u, fastf_t v)
2662+
bg_plane_pt_at(point_t *pt, plane_t *p, fastf_t u, fastf_t v)
26632663
{
26642664
if (!pt)
26652665
return -1;
26662666

26672667
point_t origin;
26682668
vect_t xaxis, yaxis, zaxis;
2669-
VSET(zaxis, p[0], p[1], p[2]);
2669+
VSET(zaxis, (*p)[0], (*p)[1], (*p)[2]);
26702670
fastf_t x, y, z, d;
2671-
x = fabs(p[0]);
2672-
y = fabs(p[1]);
2673-
z = fabs(p[2]);
2671+
x = fabs((*p)[0]);
2672+
y = fabs((*p)[1]);
2673+
z = fabs((*p)[2]);
26742674
d = 0.0;
26752675
if ( y >= x && y >= z ) {
26762676
d = x;
@@ -2693,7 +2693,7 @@ bg_plane_pt_at(point_t *pt, plane_t p, fastf_t u, fastf_t v)
26932693
return -1;
26942694

26952695
VMOVE(origin, zaxis);
2696-
VSCALE(origin, origin, -1/d*p[3]);
2696+
VSCALE(origin, origin, 1/d*(*p)[3]);
26972697
VUNITIZE(zaxis);
26982698
bn_vec_perp(xaxis, zaxis);
26992699
VUNITIZE(xaxis);

src/libbg/polygon_op.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bg_find_polygon_area(struct bg_polygon *gpoly, fastf_t sf, plane_t *vp, fastf_t
5656
poly.resize(n);
5757
for (k = 0; k < n; k++) {
5858
fastf_t fx, fy;
59-
bg_plane_closest_pt(&fx, &fy, *vp, gpoly->contour[j].point[k]);
59+
bg_plane_closest_pt(&fx, &fy, vp, &gpoly->contour[j].point[k]);
6060

6161
poly[k].X = (ClipperLib::long64)(fx * sf);
6262
poly[k].Y = (ClipperLib::long64)(fy * sf);
@@ -110,7 +110,7 @@ bg_polygons_overlap(struct bg_polygon *polyA, struct bg_polygon *polyB, plane_t
110110
for (size_t j = 0; j < polyA->contour[i].num_points; ++j) {
111111
point_t vpoint;
112112
fastf_t fx, fy;
113-
bg_plane_closest_pt(&fx, &fy, *vp, polyA->contour[i].point[j]);
113+
bg_plane_closest_pt(&fx, &fy, vp, &polyA->contour[i].point[j]);
114114
VSET(vpoint, fx * scale, fy * scale, 0);
115115
V2MOVE(polyA_2d.p_contour[i].pc_point[j], vpoint);
116116
}
@@ -128,7 +128,7 @@ bg_polygons_overlap(struct bg_polygon *polyA, struct bg_polygon *polyB, plane_t
128128
for (size_t j = 0; j < polyB->contour[i].num_points; ++j) {
129129
point_t vpoint;
130130
fastf_t fx, fy;
131-
bg_plane_closest_pt(&fx, &fy, *vp, polyB->contour[i].point[j]);
131+
bg_plane_closest_pt(&fx, &fy, vp, &polyB->contour[i].point[j]);
132132
VSET(vpoint, fx * scale, fy * scale, 0);
133133
V2MOVE(polyB_2d.p_contour[i].pc_point[j], vpoint);
134134
}
@@ -323,7 +323,7 @@ load_polygon(ClipperLib::Clipper &clipper, ClipperLib::PolyType ptype, struct bg
323323
fastf_t fx = gpoly->contour[j].point[k][0];
324324
fastf_t fy = gpoly->contour[j].point[k][1];
325325
if (vp)
326-
bg_plane_closest_pt(&fx, &fy, *vp, gpoly->contour[j].point[k]);
326+
bg_plane_closest_pt(&fx, &fy, vp, &gpoly->contour[j].point[k]);
327327

328328
curr_poly[k].X = (ClipperLib::long64)(fx * sf);
329329
curr_poly[k].Y = (ClipperLib::long64)(fy * sf);
@@ -382,7 +382,7 @@ extract(ClipperLib::PolyTree &clipper_polytree, fastf_t sf, plane_t *vp)
382382

383383
for (j = 0; j < outp->contour[n].num_points; ++j) {
384384
if (vp) {
385-
bg_plane_pt_at(&outp->contour[n].point[j], *vp, (fastf_t)(path[j].X) * sf, (fastf_t)(path[j].Y) * sf);
385+
bg_plane_pt_at(&outp->contour[n].point[j], vp, (fastf_t)(path[j].X) * sf, (fastf_t)(path[j].Y) * sf);
386386
} else {
387387
VSET(outp->contour[n].point[j], (fastf_t)(path[j].X) * sf, (fastf_t)(path[j].Y) * sf, 0);
388388
}

src/libbg/polygon_triangulate.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ bg_polygon_triangulate(int **faces, int *num_faces, point_t **out_pts, int *num_
848848
}
849849
for (size_t j = 0; j < p->contour[i].num_points; ++j) {
850850
vect2d_t p2d;
851-
bg_plane_closest_pt(&p2d[0], &p2d[1], pl, p->contour[i].point[j]);
851+
bg_plane_closest_pt(&p2d[0], &p2d[1], &pl, &p->contour[i].point[j]);
852852
V2MOVE(pnts_2d[curr_pnt], p2d);
853853
cpnts[j] = curr_pnt;
854854
curr_pnt++;
@@ -859,13 +859,13 @@ bg_polygon_triangulate(int **faces, int *num_faces, point_t **out_pts, int *num_
859859
int tri_num_faces = 0;
860860
point2d_t *tri_out_pts = NULL;
861861
int tri_num_outpts = 0;
862-
int ret = bg_nested_poly_triangulate(&tri_faces, &tri_num_faces, &tri_out_pts, &tri_num_outpts, ocontour, ocontour_cnt, (const int **)holes_array, (const size_t *)holes_npts, p->num_contours - 1, NULL, 0, pnts_2d, pnt_cnt, type);
862+
int ret = bg_nested_poly_triangulate(&tri_faces, &tri_num_faces, &tri_out_pts, &tri_num_outpts, ocontour, ocontour_cnt, (const int **)holes_array, (const size_t *)holes_npts, p->num_contours - 1, NULL, 0, pnts_2d, pnt_cnt, type);
863863

864864

865865
// Translate 2D plane points into 3D points
866866
point_t *pnts_3d = (point_t *)bu_calloc(pnt_cnt, sizeof(point_t), "3D points");
867867
for (int i = 0; i < pnt_cnt; i++) {
868-
bg_plane_pt_at(&pnts_3d[i], pl, pnts_2d[i][0], pnts_2d[i][1]);
868+
bg_plane_pt_at(&pnts_3d[i], &pl, pnts_2d[i][0], pnts_2d[i][1]);
869869
}
870870

871871
// Assign outputs

src/libbg/tests/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,8 @@ BRLCAD_ADD_TEST(NAME bg_plane_mk_plane_3pts_succeed COMMAND bg_test pla
130130
# A,B,C,D Px,Py,Pz
131131
#
132132
# A-D are plane equation parameters, and P is the test point.
133-
BRLCAD_ADD_TEST(NAME bg_plane_pt_project COMMAND bg_test plane_pt 6 1,2,3,4 5.345220032350307,-3.124899935299395,-4.687349902949093)
133+
BRLCAD_ADD_TEST(NAME bg_plane_pt_project_1 COMMAND bg_test plane_pt 6 0,0,1,1 2,2,1)
134+
BRLCAD_ADD_TEST(NAME bg_plane_pt_project_2 COMMAND bg_test plane_pt 6 0.737397,0.461098,0.493593,236.3 77.526016919831704,185.674010580054585,189.465011325663767)
134135

135136
# For function #7 (bg_coplanar_pts), the <args> format is as follows:
136137
#

src/libbg/tests/plane_pt.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,11 +188,11 @@ test_bg_plane_closest_pt(int argc, char **argv)
188188

189189
fastf_t u, v;
190190
point_t npt;
191-
if (bg_plane_closest_pt(&u, &v, plane, p)) {
191+
if (bg_plane_closest_pt(&u, &v, &plane, &p)) {
192192
bu_log("closest pt calculation failed\n");
193193
return -1;
194194
}
195-
if (bg_plane_pt_at(&npt, plane, u, v)) {
195+
if (bg_plane_pt_at(&npt, &plane, u, v)) {
196196
bu_log("pt at u,v calculation failed\n");
197197
return -1;
198198
}

src/libbv/lod.cpp

+8-6
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,10 @@ view_obb(struct bview *v,
216216
plane_t p;
217217
bg_plane_pt_nrml(&p, sbbc, dir);
218218
fastf_t pu, pv;
219-
bg_plane_closest_pt(&pu, &pv, p, ec);
220-
bg_plane_pt_at(&v->obb_center, p, pu, pv);
219+
point_t lec;
220+
VMOVE(lec, ec);
221+
bg_plane_closest_pt(&pu, &pv, &p, &lec);
222+
bg_plane_pt_at(&v->obb_center, &p, pu, pv);
221223

222224
// The first extent is just the scene radius in the lookat direction
223225
VSCALE(dir, dir, radius);
@@ -405,8 +407,8 @@ bv_view_objs_select(struct bu_ptbl *sset, struct bview *v, int x, int y)
405407
plane_t p;
406408
bg_plane_pt_nrml(&p, sbbc, dir);
407409
fastf_t pu, pv;
408-
bg_plane_closest_pt(&pu, &pv, p, ec);
409-
bg_plane_pt_at(&obb_c, p, pu, pv);
410+
bg_plane_closest_pt(&pu, &pv, &p, &ec);
411+
bg_plane_pt_at(&obb_c, &p, pu, pv);
410412

411413

412414
// The first extent is just the scene radius in the lookat direction
@@ -499,8 +501,8 @@ bv_view_objs_rect_select(struct bu_ptbl *sset, struct bview *v, int x1, int y1,
499501
plane_t p;
500502
bg_plane_pt_nrml(&p, sbbc, dir);
501503
fastf_t pu, pv;
502-
bg_plane_closest_pt(&pu, &pv, p, ec);
503-
bg_plane_pt_at(&obb_c, p, pu, pv);
504+
bg_plane_closest_pt(&pu, &pv, &p, &ec);
505+
bg_plane_pt_at(&obb_c, &p, pu, pv);
504506

505507

506508
// The first extent is just the scene radius in the lookat direction

0 commit comments

Comments
 (0)