BRL-CAD
bn_plane_dist.c
Go to the documentation of this file.
1 /* B N _ P L A N E _ D I S T . C
2  * BRL-CAD
3  *
4  * Copyright (c) 2013-2014 United States Government as represented by
5  * the U.S. Army Research Laboratory.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * version 2.1 as published by the Free Software Foundation.
10  *
11  * This library is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; see the file named COPYING for more
18  * information.
19  */
20 
21 #include "common.h"
22 
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "bu.h"
28 #include "bn.h"
29 
30 
31 #define TOL_INIT { BN_TOL_MAGIC, BN_TOL_DIST, BN_TOL_DIST * BN_TOL_DIST, 1e-6, 1.0 - 1e-6 }
32 
33 
34 static int
35 test_bn_dist_pt3_line3(int argc, char **argv)
36 {
37  int expected_return = 0;
38  int actual_return = 0;
39  fastf_t expected_dist;
40  fastf_t actual_dist;
41  point_t expected_pca = VINIT_ZERO;
42  point_t actual_pca = VINIT_ZERO;
43  point_t a = VINIT_ZERO;
44  vect_t dir = VINIT_ZERO;
45  point_t p = VINIT_ZERO;
46  struct bn_tol tol = TOL_INIT;
47 
48  if (argc != 8) {
49  bu_exit(1, "ERROR: input format is Ax,Ay,Az DIRx,DIRy,DIRz Px,Py,Pz expected_return expected_dist PCAx,PCAy,PCAz [%s]\n", argv[0]);
50  }
51 
52  sscanf(argv[2], "%lf,%lf,%lf", &a[X], &a[Y], &a[Z]);
53  sscanf(argv[3], "%lf,%lf,%lf", &dir[X], &dir[Y], &dir[Z]);
54  sscanf(argv[4], "%lf,%lf,%lf", &p[X], &p[Y], &p[Z]);
55  sscanf(argv[5], "%d", &expected_return);
56  sscanf(argv[6], "%lf", &expected_dist);
57  sscanf(argv[7], "%lf,%lf,%lf", &expected_pca[X], &expected_pca[Y], &expected_pca[Z]);
58 
59  actual_return = bn_dist_pt3_line3(&actual_dist, actual_pca, a, p, dir, &tol);
60 
61  bu_log("return: %d\n", actual_return);
62  bu_log("dist: %lf\n", actual_dist);
63  bu_log("pca: %lf,%lf,%lf\n", actual_pca[X], actual_pca[Y], actual_pca[Z]);
64 
65  return (expected_return != actual_return
66  && !NEAR_EQUAL(expected_dist,actual_dist, BN_TOL_DIST)
67  && !NEAR_EQUAL(expected_pca[X], actual_pca[X], BN_TOL_DIST)
68  && !NEAR_EQUAL(expected_pca[Y], actual_pca[Y], BN_TOL_DIST)
69  && !NEAR_EQUAL(expected_pca[Z], actual_pca[Z], BN_TOL_DIST));
70 }
71 
72 
73 static int
74 test_bn_dist_pt3_lseg3(int argc, char **argv)
75 {
76  int expected_return = 0;
77  int actual_return = 0;
78  fastf_t expected_dist;
79  fastf_t actual_dist;
80  point_t expected_pca = VINIT_ZERO;
81  point_t actual_pca = VINIT_ZERO;
82  point_t a = VINIT_ZERO;
83  point_t b = VINIT_ZERO;
84  point_t p = VINIT_ZERO;
85  struct bn_tol tol = TOL_INIT;
86 
87  if (argc != 8) {
88  bu_exit(1, "ERROR: input format is Ax,Ay,Az Bx,By,Bz Px,Py,Pz expected_return expected_dist PCAx,PCAy,PCAz [%s]\n", argv[0]);
89  }
90 
91  sscanf(argv[2], "%lf,%lf,%lf", &a[X], &a[Y], &a[Z]);
92  sscanf(argv[3], "%lf,%lf,%lf", &b[X], &b[Y], &b[Z]);
93  sscanf(argv[4], "%lf,%lf,%lf", &p[X], &p[Y], &p[Z]);
94  sscanf(argv[5], "%d", &expected_return);
95  sscanf(argv[6], "%lf", &expected_dist);
96  sscanf(argv[7], "%lf,%lf,%lf", &expected_pca[X], &expected_pca[Y], &expected_pca[Z]);
97 
98  actual_return = bn_dist_pt3_lseg3(&actual_dist, actual_pca, a, b, p, &tol);
99 
100  bu_log("return: %d\n", actual_return);
101  bu_log("dist: %lf\n", actual_dist);
102  bu_log("pca: %lf,%lf,%lf\n", actual_pca[X], actual_pca[Y], actual_pca[Z]);
103 
104  return (expected_return != actual_return
105  && !NEAR_EQUAL(expected_dist,actual_dist, BN_TOL_DIST)
106  && !NEAR_EQUAL(expected_pca[X], actual_pca[X], BN_TOL_DIST)
107  && !NEAR_EQUAL(expected_pca[Y], actual_pca[Y], BN_TOL_DIST)
108  && !NEAR_EQUAL(expected_pca[Z], actual_pca[Z], BN_TOL_DIST));
109 }
110 
111 
112 static int
113 test_bn_dist_pt3_pt3(int argc, char **argv)
114 {
115  double expected_return = 0;
116  double actual_return = 0;
117  point_t a = VINIT_ZERO;
118  point_t b = VINIT_ZERO;
119 
120  if (argc != 5) {
121  bu_exit(1, "ERROR: input format is Ax,Ay,Az Bx,By,Bz expected_return [%s]\n", argv[0]);
122  }
123 
124  sscanf(argv[2], "%lf,%lf,%lf", &a[X], &a[Y], &a[Z]);
125  sscanf(argv[3], "%lf,%lf,%lf", &b[X], &b[Y], &b[Z]);
126  sscanf(argv[4], "%lf", &expected_return);
127 
128  actual_return = bn_dist_pt3_pt3(a, b);
129 
130  bu_log("return: %.30lf\n", actual_return);
131  bu_log("er: %.30lf\n", expected_return);
132  bu_log("retval: %d\n", NEAR_EQUAL(expected_return, actual_return, BN_TOL_DIST));
133  bu_log("error: %f\n", fabs(expected_return-actual_return)/expected_return);
134 
135  return !NEAR_EQUAL(expected_return, actual_return, BN_TOL_DIST);
136 }
137 
138 
139 int
140 main(int argc, char *argv[])
141 {
142  int function_num = 0;
143 
144  if (argc < 3) {
145  bu_exit(1, "ERROR: input format is function_num function_test_args [%s]\n", argv[0]);
146  }
147 
148  sscanf(argv[1], "%d", &function_num);
149  if (function_num < 1 || function_num > 5)
150  function_num = 0;
151 
152  switch (function_num) {
153  case 1:
154  return test_bn_dist_pt3_line3(argc, argv);
155  case 2:
156  return test_bn_dist_pt3_lseg3(argc, argv);
157  case 3:
158  return test_bn_dist_pt3_pt3(argc, argv);
159  }
160  return 1;
161 }
162 
163 
164 /** @} */
165 /*
166  * Local Variables:
167  * mode: C
168  * tab-width: 8
169  * indent-tabs-mode: t
170  * c-file-style: "stroustrup"
171  * End:
172  * ex: shiftwidth=4 tabstop=8
173  */
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
Header file for the BRL-CAD common definitions.
Definition: color.c:49
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
#define BN_TOL_DIST
Definition: tol.h:109
#define TOL_INIT
Definition: bn_plane_dist.c:31
Support for uniform tolerances.
Definition: tol.h:71
int bn_dist_pt3_line3(fastf_t *dist, point_t pca, const point_t a, const point_t p, const vect_t dir, const struct bn_tol *tol)
double bn_dist_pt3_pt3(const point_t a, const point_t b)
Returns distance between two points.
int bn_dist_pt3_lseg3(fastf_t *dist, point_t pca, const point_t a, const point_t b, const point_t p, const struct bn_tol *tol)
Find the distance from a point P to a line segment described by the two endpoints A and B...
Definition: color.c:51
double fastf_t
Definition: defines.h:300
int main(int argc, char *argv[])
Definition: color.c:50