BRL-CAD
bn_plane_pt.c
Go to the documentation of this file.
1 /* B N _ P L A N E _ P 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_3pts_collinear(int argc, char **argv)
36 {
37  int expected_result = 0;
38  int actual_result = 0;
39  point_t p0 = VINIT_ZERO;
40  point_t p1 = VINIT_ZERO;
41  point_t p2 = VINIT_ZERO;
42  struct bn_tol tol = TOL_INIT;
43 
44  if (argc != 6) {
45  bu_exit(1, "ERROR: input format is P0x,P0y,P0z P1x,P1y,P1z P2x,P2y,P2z expected_result [%s]\n", argv[0]);
46  }
47 
48  sscanf(argv[2], "%lf,%lf,%lf", &p0[X], &p0[Y], &p0[Z]);
49  sscanf(argv[3], "%lf,%lf,%lf", &p1[X], &p1[Y], &p1[Z]);
50  sscanf(argv[4], "%lf,%lf,%lf", &p2[X], &p2[Y], &p2[Z]);
51  sscanf(argv[5], "%d", &expected_result);
52 
53  actual_result = bn_3pts_collinear(p0, p1, p2, &tol);
54 
55  bu_log("result: %d\n", actual_result);
56 
57  return (expected_result != actual_result);
58 }
59 
60 
61 static int
62 test_bn_3pts_distinct(int argc, char **argv)
63 {
64  int expected_result = 0;
65  int actual_result = 0;
66  point_t p0 = VINIT_ZERO;
67  point_t p1 = VINIT_ZERO;
68  point_t p2 = VINIT_ZERO;
69  struct bn_tol tol = TOL_INIT;
70 
71  if (argc != 6) {
72  bu_exit(1, "ERROR: input format is P0x,P0y,P0z P1x,P1y,P1z P2x,P2y,P2z expected_result [%s]\n", argv[0]);
73  }
74 
75  sscanf(argv[2], "%lf,%lf,%lf", &p0[X], &p0[Y], &p0[Z]);
76  sscanf(argv[3], "%lf,%lf,%lf", &p1[X], &p1[Y], &p1[Z]);
77  sscanf(argv[4], "%lf,%lf,%lf", &p2[X], &p2[Y], &p2[Z]);
78  sscanf(argv[5], "%d", &expected_result);
79 
80  actual_result = bn_3pts_distinct(p0, p1, p2, &tol);
81 
82  bu_log("result: %d\n", actual_result);
83 
84  return (expected_result != actual_result);
85 }
86 
87 
88 static int
89 test_bn_distsq_line3_pt3(int argc, char **argv)
90 {
91  float expected_result = 0;
92  float actual_result = 0;
93  point_t pt = VINIT_ZERO;
94  vect_t dir = VINIT_ZERO;
95  point_t a = VINIT_ZERO;
96 
97  if (argc != 6) {
98  bu_exit(1, "ERROR: input format is PTx,PTy,PTz DIRx,DIRy,DIRz Ax,Ay,Az expected_result [%s]\n", argv[0]);
99  }
100 
101  sscanf(argv[2], "%lf,%lf,%lf", &pt[X], &pt[Y], &pt[Z]);
102  sscanf(argv[3], "%lf,%lf,%lf", &dir[X], &dir[Y], &dir[Z]);
103  sscanf(argv[4], "%lf,%lf,%lf", &a[X], &a[Y], &a[Z]);
104  sscanf(argv[5], "%f", &expected_result);
105 
106  actual_result = bn_distsq_line3_pt3(pt, dir, a);
107 
108  bu_log("result: %f\n", actual_result);
109 
110  return !EQUAL(expected_result, actual_result);
111 }
112 
113 
114 static int
115 test_bn_distsq_pt3_lseg3_v2(int argc, char **argv)
116 {
117  int expected_return = 0;
118  fastf_t expected_dist = 0;
119  int actual_return = 0;
120  fastf_t actual_dist = 0;
121  point_t a = VINIT_ZERO;
122  point_t b = VINIT_ZERO;
123  point_t p = VINIT_ZERO;
124  struct bn_tol tol = TOL_INIT;
125 
126  if (argc != 7) {
127  bu_exit(1, "ERROR: input format is Ax,Ay,Az Bx,By,Bz Px,Py,Pz expected_return expected_dist [%s]\n", argv[0]);
128  }
129 
130  sscanf(argv[2], "%lf,%lf,%lf", &a[X], &a[Y], &a[Z]);
131  sscanf(argv[3], "%lf,%lf,%lf", &b[X], &b[Y], &b[Z]);
132  sscanf(argv[4], "%lf,%lf,%lf", &p[X], &p[Y], &p[Z]);
133  sscanf(argv[5], "%d", &expected_return);
134  sscanf(argv[6], "%lf", &expected_dist);
135 
136  actual_return = bn_distsq_pt3_lseg3_v2(&actual_dist, a, b, p, &tol);
137 
138  bu_log("return: %d\n", actual_return);
139  bu_log("dist: %f\n", actual_dist);
140 
141  if (expected_return == actual_return && NEAR_EQUAL(expected_dist, actual_dist, BN_TOL_DIST)) {
142  return 0;
143  } else {
144  return -1;
145  }
146 }
147 
148 
149 static int
150 test_bn_mk_plane_3pts(int argc, char **argv)
151 {
152  int expected_return = 0;
153  int actual_return = 0;
154  point_t a = VINIT_ZERO;
155  point_t b = VINIT_ZERO;
156  point_t c = VINIT_ZERO;
157  plane_t plane = HINIT_ZERO;
158  struct bn_tol tol = TOL_INIT;
159 
160  if (argc != 6) {
161  bu_exit(1, "ERROR: input format is Ax,Ay,Az Bx,By,Bz Cx,Cy,Cz expected_return [%s]\n", argv[0]);
162  }
163 
164  sscanf(argv[2], "%lf,%lf,%lf", &a[X], &a[Y], &a[Z]);
165  sscanf(argv[3], "%lf,%lf,%lf", &b[X], &b[Y], &b[Z]);
166  sscanf(argv[4], "%lf,%lf,%lf", &c[X], &c[Y], &c[Z]);
167  sscanf(argv[5], "%d", &expected_return);
168 
169  actual_return = bn_mk_plane_3pts(plane, a, b, c, &tol);
170 
171  bu_log("return: %f\n", actual_return);
172 
173  return (expected_return != actual_return);
174 }
175 
176 
177 int
178 main(int argc, char *argv[])
179 {
180  int function_num = 0;
181 
182  if (argc < 3) {
183  bu_exit(1, "ERROR: input format is function_num function_test_args [%s]\n", argv[0]);
184  }
185 
186  sscanf(argv[1], "%d", &function_num);
187  if (function_num < 1 || function_num > 5)
188  function_num = 0;
189 
190  switch (function_num) {
191  case 1:
192  return test_bn_3pts_collinear(argc, argv);
193  case 2:
194  return test_bn_3pts_distinct(argc, argv);
195  case 3:
196  return test_bn_distsq_line3_pt3(argc, argv);
197  case 4:
198  return test_bn_distsq_pt3_lseg3_v2(argc, argv);
199  case 5:
200  return test_bn_mk_plane_3pts(argc, argv);
201  }
202  return 1;
203 }
204 
205 
206 /** @} */
207 /*
208  * Local Variables:
209  * mode: C
210  * tab-width: 8
211  * indent-tabs-mode: t
212  * c-file-style: "stroustrup"
213  * End:
214  * ex: shiftwidth=4 tabstop=8
215  */
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
#define TOL_INIT
Definition: bn_plane_pt.c:31
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
int main(int argc, char *argv[])
Definition: bn_plane_pt.c:178
#define BN_TOL_DIST
Definition: tol.h:109
Support for uniform tolerances.
Definition: tol.h:71
int bn_distsq_pt3_lseg3_v2(fastf_t *distsq, const fastf_t *a, const fastf_t *b, const fastf_t *p, const struct bn_tol *tol)
Definition: plane.c:1793
int bn_3pts_collinear(point_t a, point_t b, point_t c, const struct bn_tol *tol)
Check to see if three points are collinear.
int bn_3pts_distinct(const point_t a, const point_t b, const point_t c, const struct bn_tol *tol)
Definition: color.c:51
double fastf_t
Definition: defines.h:300
int bn_mk_plane_3pts(plane_t plane, const point_t a, const point_t b, const point_t c, const struct bn_tol *tol)
double bn_distsq_line3_pt3(const point_t pt, const vect_t dir, const point_t a)
Definition: color.c:50