BRL-CAD
test_point_intersect.cpp
Go to the documentation of this file.
1 /* T E S T _ P O I N T _ I N T E R S E C T . C P P
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 /** @file test_point_intersect.cpp
21  *
22  * Do some test on PPI, PCI and PSI.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include "vmath.h"
29 #include "bu/log.h"
30 
31 #include "brep.h"
32 
33 static void
34 test_ppi(ON_3dPoint &p1, ON_3dPoint &p2)
35 {
36  ON_wString wstr;
37  ON_TextLog textlog(wstr);
38  ON_ClassArray<ON_PX_EVENT> x;
39 
40  // Use default tolerance
41  ON_Intersect(p1, p2, x);
42 
43  bu_log("(%f,%f,%f) and (%f,%f,%f):\n", p1[0], p1[1], p1[2], p2[0], p2[1], p2[2]);
44 
45  if (x.Count() == 0) {
46  bu_log("No intersection.\n");
47  } else {
48  for (int i = 0; i < x.Count(); i++)
49  x[i].Dump(textlog);
50  ON_String str(wstr);
51  bu_log(str.Array());
52  }
53  bu_log("\n\n");
54 }
55 
56 
57 static void
58 test_pci(ON_3dPoint &p, ON_Curve &c)
59 {
60  ON_wString wstr;
61  ON_TextLog textlog(wstr);
62  ON_ClassArray<ON_PX_EVENT> x;
63 
64  // Use default tolerance
65  ON_Intersect(p, c, x);
66 
67  ON_3dPoint start = c.PointAtStart();
68  ON_3dPoint end = c.PointAtEnd();
69  bu_log("(%f,%f,%f) and [(%f,%f,%f) to (%f,%f,%f)]:\n",
70  p[0], p[1], p[2], start[0], start[1], start[2], end[0], end[1], end[2]);
71  if (x.Count() == 0) {
72  bu_log("No intersection.\n");
73  } else {
74  for (int i = 0; i < x.Count(); i++)
75  x[i].Dump(textlog);
76  ON_String str(wstr);
77  bu_log(str.Array());
78  }
79  bu_log("\n\n");
80 }
81 
82 
83 static void
84 test_psi(ON_3dPoint &p, ON_Surface &s)
85 {
86  ON_wString wstr;
87  ON_TextLog textlog(wstr);
88  ON_ClassArray<ON_PX_EVENT> x;
89 
90  // Use default tolerance
91  ON_Intersect(p, s, x);
92 
93  // XXX: How to simply show a surface?
94  bu_log("(%f,%f,%f) and a surface:\n", p[0], p[1], p[2]);
95  if (x.Count() == 0) {
96  bu_log("No intersection.\n");
97  } else {
98  for (int i = 0; i < x.Count(); i++)
99  x[i].Dump(textlog);
100  ON_String str(wstr);
101  bu_log(str.Array());
102  }
103  bu_log("\n\n");
104 }
105 
106 
107 static double
108 rand_f(double min, double max)
109 {
110  double f = (double)rand() / RAND_MAX;
111  return min + f * (max - min);
112 }
113 
114 
115 int
116 main(int, char**)
117 {
118  srand(time(0));
119 
120  ON_3dPoint center(0.0, 0.0, 0.0);
121  double radius = 10.0;
122  ON_Sphere sphere(center, radius);
123  ON_Brep *brep = ON_BrepSphere(sphere);
124 
125  ON_3dPoint p1(0.0, 0.0, 0.0);
126  ON_3dPoint p2(0.0, 0.0, radius);
127 
128  // Point-point intersection
129  bu_log("*** Point-point intersection ***\n");
130  test_ppi(p1, p1);
131  test_ppi(p1, p2);
132 
133  // Point-curve intersection
134  bu_log("*** Point-curve intersection ***\n");
135  // brep->m_C3[0] is an arc curve that starts from (0, 0, -R)
136  // to (0, 0, R) through (R, 0, 0) which forms a semicircle.
137  ON_Curve *curve = brep->m_C3[0];
138 
139  ON_3dPoint mid = curve->PointAt(curve->Domain().Mid());
140  bu_log("debug: %f %f %f\n", mid[0], mid[1], mid[2]);
141 
142  bu_log("** Part 1 **\n");
143  test_pci(p1, *curve);
144  test_pci(p2, *curve);
145 
146  // Now we use some randomized points (should intersect)
147  bu_log("** Part 2 **\n");
148  for (int i = 0; i < 10; i++) {
149  double x = rand_f(0.0, radius);
150  double y = 0.0;
151  double z = sqrt(radius*radius-x*x);
152  if (rand() % 2) z = -z; // sometimes we have it negative
153  ON_3dPoint test_pt(x, y, z);
154  test_pci(test_pt, *curve);
155  }
156 
157  // More randomize points (maybe no intersection)
158  bu_log("** Part 3 **\n");
159  for (int i = 0; i < 10; i++) {
160  // We use test points randomly distributed inside a cube
161  // from (-R, -R, -R) to (R, R, R)
162  double x = rand_f(-radius, radius);
163  double y = rand_f(-radius, radius);
164  double z = rand_f(-radius, radius);
165  ON_3dPoint test_pt(x, y, z);
166  test_pci(test_pt, *curve);
167  }
168 
169  // Point-surface intersection
170  bu_log("*** Point-surface intersection ***\n");
171  bu_log("** Part 1 **\n");
172  ON_Surface *surf = brep->m_S[0];
173  test_psi(p1, *surf);
174  test_psi(p2, *surf);
175 
176  // Now we use some randomized points (should intersect)
177  bu_log("** Part 2 **\n");
178  for (int i = 0; i < 10; i++) {
179  double x = rand_f(-radius, radius);
180  double y_range = sqrt(radius*radius-x*x);
181  double y = rand_f(-y_range, y_range);
182  double z = sqrt(y_range*y_range-y*y);
183  if (rand() % 2) z = -z; // sometimes we have it negative
184  ON_3dPoint test_pt(x, y, z);
185  test_psi(test_pt, *surf);
186  }
187 
188  // More randomize points (maybe no intersection)
189  bu_log("** Part 3 **\n");
190  for (int i = 0; i < 10; i++) {
191  // We use test points randomly distributed inside a cube
192  // from (-R, -R, -R) to (R, R, R)
193  double x = rand_f(-radius, radius);
194  double y = rand_f(-radius, radius);
195  double z = rand_f(-radius, radius);
196  ON_3dPoint test_pt(x, y, z);
197  test_psi(test_pt, *surf);
198  }
199 
200  delete brep;
201  bu_log("All finished.\n");
202  return 0;
203 }
204 
205 // Local Variables:
206 // tab-width: 8
207 // mode: C++
208 // c-basic-offset: 4
209 // indent-tabs-mode: t
210 // c-file-style: "stroustrup"
211 // End:
212 // ex: shiftwidth=4 tabstop=8
bool ON_Intersect(const ON_3dPoint &pointA, const ON_3dPoint &pointB, ON_ClassArray< ON_PX_EVENT > &x, double tol)
Definition: intersect.cpp:647
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
if lu s
Definition: nmg_mod.c:3860
Header file for the BRL-CAD common definitions.
long time(time_t *)
int main(int, char **)
off_t end
Definition: ptbl.h:64