BRL-CAD
bn_plane_isect.c
Go to the documentation of this file.
1 /* B N _ P L A N E _ I S E C 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_isect_line_lseg(int argc, char **argv)
36 {
37  int expected_return = 0;
38  int actual_return = 0;
39  fastf_t expected_t;
40  fastf_t actual_t;
41  point_t p = VINIT_ZERO;
42  vect_t d = VINIT_ZERO;
43  point_t a = VINIT_ZERO;
44  point_t b = VINIT_ZERO;
45  struct bn_tol tol = TOL_INIT;
46 
47  if (argc != 8) {
48  bu_exit(1, "ERROR: input format is Px,Py,Pz Dx,Dy,Dz Ax,Ay,Az Bx,By,Bz expected_return expected_t [%s]\n", argv[0]);
49  }
50 
51  sscanf(argv[2], "%lf,%lf,%lf", &p[X], &p[Y], &p[Z]);
52  sscanf(argv[3], "%lf,%lf,%lf", &d[X], &d[Y], &d[Z]);
53  sscanf(argv[4], "%lf,%lf,%lf", &a[X], &a[Y], &a[Z]);
54  sscanf(argv[5], "%lf,%lf,%lf", &b[X], &b[Y], &b[Z]);
55  sscanf(argv[6], "%d", &expected_return);
56  sscanf(argv[7], "%lf", &expected_t);
57 
58  actual_return = bn_isect_line_lseg(&actual_t, p, d, a, b, &tol);
59 
60  bu_log("return: %d\n", actual_return);
61  bu_log("t: %f\n", actual_t);
62 
63  return !(expected_return == actual_return
64  && NEAR_EQUAL(expected_t, actual_t, BN_TOL_DIST));
65 }
66 
67 
68 static int
69 test_bn_isect_lseg3_lseg3(int argc, char **argv)
70 {
71  int expected_return = 0;
72  int actual_return = 0;
73  fastf_t expected_dist[2] = {0.0, 0.0};
74  fastf_t actual_dist[2] = {0.0, 0.0};
75  point_t p = VINIT_ZERO;
76  vect_t pdir = VINIT_ZERO;
77  point_t q = VINIT_ZERO;
78  vect_t qdir = VINIT_ZERO;
79  struct bn_tol tol = TOL_INIT;
80 
81  if (argc != 9) {
82  bu_exit(1, "ERROR: input format is Px,Py,Pz PDIRx,PDIRy,PDIRz Qx,Qy,Qz QDIRx,QDIRy,QDIRz expected_return expected_dist_0 expected_dist_1 [%s]\n", argv[0]);
83  }
84 
85  sscanf(argv[2], "%lf,%lf,%lf", &p[X], &p[Y], &p[Z]);
86  sscanf(argv[3], "%lf,%lf,%lf", &pdir[X], &pdir[Y], &pdir[Z]);
87  sscanf(argv[4], "%lf,%lf,%lf", &q[X], &q[Y], &q[Z]);
88  sscanf(argv[5], "%lf,%lf,%lf", &qdir[X], &qdir[Y], &qdir[Z]);
89  sscanf(argv[6], "%d", &expected_return);
90  sscanf(argv[7], "%lf", &expected_dist[0]);
91  sscanf(argv[8], "%lf", &expected_dist[1]);
92 
93  actual_return = bn_isect_lseg3_lseg3(actual_dist, p, pdir, q, qdir, &tol);
94 
95  bu_log("return: %d\n", actual_return);
96  bu_log("dist[0]: %f\n", actual_dist[0]);
97  bu_log("dist[1]: %f\n", actual_dist[1]);
98 
99  return !(expected_return == actual_return
100  && NEAR_EQUAL(expected_dist[0], actual_dist[0], BN_TOL_DIST)
101  && NEAR_EQUAL(expected_dist[1], actual_dist[1], BN_TOL_DIST));
102 }
103 
104 
105 int
106 main(int argc, char *argv[])
107 {
108  int function_num = 0;
109 
110  if (argc < 3) {
111  bu_exit(1, "ERROR: input format is function_num function_test_args [%s]\n", argv[0]);
112  }
113 
114  sscanf(argv[1], "%d", &function_num);
115  if (function_num < 1 || function_num > 5)
116  function_num = 0;
117 
118  switch (function_num) {
119  case 1:
120  return test_bn_isect_line_lseg(argc, argv);
121  case 2:
122  return test_bn_isect_lseg3_lseg3(argc, argv);
123  }
124  return 1;
125 }
126 
127 
128 /** @} */
129 /*
130  * Local Variables:
131  * mode: C
132  * tab-width: 8
133  * indent-tabs-mode: t
134  * c-file-style: "stroustrup"
135  * End:
136  * ex: shiftwidth=4 tabstop=8
137  */
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
int bn_isect_line_lseg(fastf_t *t, const point_t p, const vect_t d, const point_t a, const point_t b, const struct bn_tol *tol)
Intersect a line in parametric form:
int main(int argc, char *argv[])
Header file for the BRL-CAD common definitions.
#define TOL_INIT
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
Support for uniform tolerances.
Definition: tol.h:71
int bn_isect_lseg3_lseg3(fastf_t *dist, const point_t p, const vect_t pdir, const point_t q, const vect_t qdir, const struct bn_tol *tol)
Intersect two 3D line segments, defined by two points and two vectors. The vectors are unlikely to be...
Definition: color.c:51
double fastf_t
Definition: defines.h:300
Definition: color.c:50