Revision: 76193
          http://sourceforge.net/p/brlcad/code/76193
Author:   starseeker
Date:     2020-06-24 15:15:17 +0000 (Wed, 24 Jun 2020)
Log Message:
-----------
add lseg-pt distance to libbg

Modified Paths:
--------------
    brlcad/trunk/doc/legal/embedded/CMakeLists.txt
    brlcad/trunk/include/bg/lseg.h
    brlcad/trunk/src/libbg/CMakeLists.txt
    brlcad/trunk/src/libbg/tests/CMakeLists.txt

Added Paths:
-----------
    brlcad/trunk/doc/legal/embedded/lseg_pt.txt
    brlcad/trunk/src/libbg/lseg_pt.c
    brlcad/trunk/src/libbg/tests/lseg_pt.c

Modified: brlcad/trunk/doc/legal/embedded/CMakeLists.txt
===================================================================
--- brlcad/trunk/doc/legal/embedded/CMakeLists.txt      2020-06-24 13:48:49 UTC 
(rev 76192)
+++ brlcad/trunk/doc/legal/embedded/CMakeLists.txt      2020-06-24 15:15:17 UTC 
(rev 76193)
@@ -26,6 +26,7 @@
   hv3_snit.txt
   libtermlib.txt
   lseg_lseg.txt
+  lseg_pt.txt
   lz4.txt
   marching_cubes.txt
   msinttypes.txt

Added: brlcad/trunk/doc/legal/embedded/lseg_pt.txt
===================================================================
--- brlcad/trunk/doc/legal/embedded/lseg_pt.txt                         (rev 0)
+++ brlcad/trunk/doc/legal/embedded/lseg_pt.txt 2020-06-24 15:15:17 UTC (rev 
76193)
@@ -0,0 +1,31 @@
+https://www.geometrictools.com/GTE/Mathematics/DistPointSegment.h
+
+David Eberly, Geometric Tools, Redmond WA 98052
+Copyright (c) 1998-2020
+
+Boost Software License - Version 1.0 - August 17th, 2003
+
+Permission is hereby granted, free of charge, to any person or organization
+obtaining a copy of the software and accompanying documentation covered by
+this license (the "Software") to use, reproduce, display, distribute,
+execute, and transmit the Software, and to prepare derivative works of the
+Software, and to permit third-parties to whom the Software is furnished to
+do so, all subject to the following:
+
+The copyright notices in the Software and this entire statement, including
+the above license grant, this restriction and the following disclaimer,
+must be included in all copies of the Software, in whole or in part, and
+all derivative works of the Software, unless such copies or derivative
+works are solely in the form of machine-executable object code generated by
+a source language processor.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
+SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
+FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
+
+file:/src/libbg/lseg_pt.c
+


Property changes on: brlcad/trunk/doc/legal/embedded/lseg_pt.txt
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Modified: brlcad/trunk/include/bg/lseg.h
===================================================================
--- brlcad/trunk/include/bg/lseg.h      2020-06-24 13:48:49 UTC (rev 76192)
+++ brlcad/trunk/include/bg/lseg.h      2020-06-24 15:15:17 UTC (rev 76193)
@@ -36,14 +36,6 @@
 
 __BEGIN_DECLS
 
-#if 0
-/* Compute the closest 2D point on the 2D line segment P0->P1 to point Q.
- * Returns the distance squared from Q to the closest point and the closest
- * point in question if c is non-NULL.
- */
-BG_EXPORT double
-bg_lseg2_pt2_dist_sq(point2d_t *c, const point2d_t P0, const point2d_t P1, 
const point2d_t Q);
-
 /* Compute the closest point on the line segment P0->P1 to point Q.  Returns
  * the distance squared from Q to the closest point and the closest point in
  * question if c is non-NULL.
@@ -50,7 +42,6 @@
  */
 BG_EXPORT double
 bg_lseg_pt_dist_sq(point_t *c, const point_t P0, const point_t P1, const 
point_t Q);
-#endif
 
 /* Compute the closest points on the line segments P0->P1 and Q0->Q1.  Returns
  * the distance squared between the closest points and (optionally) the closest

Modified: brlcad/trunk/src/libbg/CMakeLists.txt
===================================================================
--- brlcad/trunk/src/libbg/CMakeLists.txt       2020-06-24 13:48:49 UTC (rev 
76192)
+++ brlcad/trunk/src/libbg/CMakeLists.txt       2020-06-24 15:15:17 UTC (rev 
76193)
@@ -16,6 +16,7 @@
   chull3d.cpp
   clipper.cpp
   lseg_lseg.c
+  lseg_pt.c
   obr.c
   polygon.c
   polygon_triangulate.cpp

Added: brlcad/trunk/src/libbg/lseg_pt.c
===================================================================
--- brlcad/trunk/src/libbg/lseg_pt.c                            (rev 0)
+++ brlcad/trunk/src/libbg/lseg_pt.c    2020-06-24 15:15:17 UTC (rev 76193)
@@ -0,0 +1,68 @@
+// David Eberly, Geometric Tools, Redmond WA 98052
+// Copyright (c) 1998-2020
+// Distributed under the Boost Software License, Version 1.0.
+// http://www.boost.org/LICENSE_1_0.txt
+// http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
+// File Version: 4.0.2019.08.13
+
+// This file is basically
+// https://www.geometrictools.com/GTE/Mathematics/DistPointSegment.h
+// except we are using vmath.h types and the C programming language
+
+#include "common.h"
+#include "vmath.h"
+#include "bg/lseg.h"
+
+double
+bg_lseg_pt_dist_sq(point_t *c, const point_t P0, const point_t P1, const 
point_t Q)
+{
+    double ldist_sq, parameter;
+    vect_t closest, dir, diff;
+
+    // Note:  the dir vector is not unit length.  The normalization is
+    // deferred until it is needed.
+    VSUB2(dir, P1, P0);
+    VSUB2(diff, Q, P1);
+    double t = VDOT(dir, diff);
+    if (t > 0.0 || NEAR_EQUAL(t, 0.0, SMALL_FASTF)) {
+       parameter = 1.0;
+       VMOVE(closest, P1);
+    } else {
+       VSUB2(diff, Q, P0);
+       t = VDOT(dir, diff);
+       if (t < 0.0 || NEAR_EQUAL(t, 0.0, SMALL_FASTF)) {
+           parameter = 0.0;
+           VMOVE(closest, P0);
+       } else {
+           double sqrLength = VDOT(dir, dir);
+           if (sqrLength > 0.0) {
+               point_t ra, rb;
+               t /= sqrLength;
+               parameter = t;
+               VSCALE(ra, P0, (1.0 - parameter));
+               VSCALE(rb, P1, parameter);
+               VADD2(closest, ra, rb);
+           } else {
+               parameter = 0.0;
+               VMOVE(closest, P0);
+           }
+       }
+    }
+
+    VSUB2(diff, Q, closest);
+    ldist_sq = VDOT(diff, diff);
+    if (c) {
+       VMOVE(*c, closest);
+    }
+    return ldist_sq ;
+}
+
+/*
+ * Local Variables:
+ * tab-width: 8
+ * mode: C
+ * indent-tabs-mode: t
+ * c-file-style: "stroustrup"
+ * End:
+ * ex: shiftwidth=4 tabstop=8
+ */


Property changes on: brlcad/trunk/src/libbg/lseg_pt.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Modified: brlcad/trunk/src/libbg/tests/CMakeLists.txt
===================================================================
--- brlcad/trunk/src/libbg/tests/CMakeLists.txt 2020-06-24 13:48:49 UTC (rev 
76192)
+++ brlcad/trunk/src/libbg/tests/CMakeLists.txt 2020-06-24 15:15:17 UTC (rev 
76193)
@@ -63,6 +63,16 @@
 
 add_test(NAME bg_lseg_lseg_1   COMMAND bg_lseg_lseg_dist 1)
 
+#  ************ lseg_lseg.c tests ***********
+
+BRLCAD_ADDEXEC(bg_lseg_pt_dist lseg_pt.c "libbg;libbn;libbu" TEST)
+add_test(NAME bg_lseg_pt_dist_case0  COMMAND bg_lseg_pt_dist  -5,-5,-5 5,5,5 
0,0,0 0,0,0 0)
+add_test(NAME bg_lseg_pt_dist_case1  COMMAND bg_lseg_pt_dist  -5,-5,-5 5,5,5 
-5,-5,-5 -5,-5,-5, 0)
+add_test(NAME bg_lseg_pt_dist_case2  COMMAND bg_lseg_pt_dist  -5,-5,-5 5,5,5 
5,5,5 5,5,5, 0)
+add_test(NAME bg_lseg_pt_dist_case3  COMMAND bg_lseg_pt_dist  -5,-5,-5 5,5,5 
-10,-5,-10 -5,-5,-5 7.07107 )
+add_test(NAME bg_lseg_pt_dist_case4  COMMAND bg_lseg_pt_dist  -5,-5,-5 5,5,5 
5,10,10 5,5,5 7.07107)
+add_test(NAME bg_lseg_pt_dist_case5  COMMAND bg_lseg_pt_dist  -5,-5,-5 5,5,5 
0,-5,-5 -3.33333,-3.33333,-3.33333 4.08248)
+
 #  ************ tri_ray.c tests ***********
 
 BRLCAD_ADDEXEC(bg_tri_ray_isect tri_ray_isect.cpp "libbg;libbn;libbu" TEST)

Added: brlcad/trunk/src/libbg/tests/lseg_pt.c
===================================================================
--- brlcad/trunk/src/libbg/tests/lseg_pt.c                              (rev 0)
+++ brlcad/trunk/src/libbg/tests/lseg_pt.c      2020-06-24 15:15:17 UTC (rev 
76193)
@@ -0,0 +1,78 @@
+/*                      L S E G _ P T . C
+ * BRL-CAD
+ *
+ * Copyright (c) 2011-2020 United States Government as represented by
+ * the U.S. Army Research Laboratory.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * version 2.1 as published by the Free Software Foundation.
+ *
+ * This library is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this file; see the file named COPYING for more
+ * information.
+ */
+
+#include "common.h"
+
+#include <stdio.h>
+
+#include "bu.h"
+#include "bg.h"
+
+// Unless we want to require more precise input for expected values,
+// be fairly loose with the validation tolerance
+#define DCHECK_TOL 1e-5
+
+int
+main(int argc, char **argv)
+{
+    point_t P0, P1, Q, E, C;
+    double edist, cdist;
+
+    bu_setprogname(argv[0]);
+
+    if (argc != 6)
+       bu_exit(1, "ERROR: [%s] input format is: P0x,P0y,P0z P1x,P1y,P1z 
Qx,Qy,Qz Ex,Ey,Ez expected_dist \n", argv[0]);
+
+    sscanf(argv[1], "%lf,%lf,%lf", &P0[X], &P0[Y], &P0[Z]);
+    sscanf(argv[2], "%lf,%lf,%lf", &P1[X], &P1[Y], &P1[Z]);
+    sscanf(argv[3], "%lf,%lf,%lf", &Q[X], &Q[Y], &Q[Z]);
+    sscanf(argv[4], "%lf,%lf,%lf", &E[X], &E[Y], &E[Z]);
+    sscanf(argv[5], "%lf", &edist);
+
+    cdist = sqrt(bg_lseg_pt_dist_sq(&C, P0, P1, Q));
+
+    if (!NEAR_EQUAL(edist, cdist, DCHECK_TOL)) {
+       bu_log("expected %g, got %g\n", edist, cdist);
+       bu_exit(-1, "Fatal error - expected distance equality test failed\n");
+    }
+
+    if (DIST_PNT_PNT(C, E) > DCHECK_TOL) {
+       bu_log("Distance: %f\n", DIST_PNT_PNT(C, E));
+       bu_log("E       : %f %f %f\n", V3ARGS(E));
+       bu_log("C       : %f %f %f\n", V3ARGS(C));
+       bu_exit(-1, "Fatal error - expected and calculated points differ\n");
+    }
+
+    bu_log("%g: %g,%g,%g\n", cdist, V3ARGS(C));
+
+    return 0;
+}
+
+
+/** @} */
+/*
+ * Local Variables:
+ * mode: C
+ * tab-width: 8
+ * indent-tabs-mode: t
+ * c-file-style: "stroustrup"
+ * End:
+ * ex: shiftwidth=4 tabstop=8
+ */


Property changes on: brlcad/trunk/src/libbg/tests/lseg_pt.c
___________________________________________________________________
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
BRL-CAD Source Commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/brlcad-commits

Reply via email to