BRL-CAD
test_curve_intersect.cpp
Go to the documentation of this file.
1 /* T E S T _ C U R V E _ 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_curve_intersect.cpp
21  *
22  * Do some test on CCI and CSI.
23  *
24  */
25 
26 #include "common.h"
27 
28 #include "vmath.h"
29 #include "bu/log.h"
30 
31 #include "brep.h"
32 
33 
34 static void
35 test_cci(ON_Curve *c1, ON_Curve *c2)
36 {
37  ON_wString wstr;
38  ON_TextLog textlog(wstr);
39  ON_SimpleArray<ON_X_EVENT> x;
40 
41  // Use default tolerance
42  ON_Intersect(c1, c2, x);
43 
44  if (x.Count() == 0) {
45  bu_log("No intersection.\n");
46  } else {
47  for (int i = 0; i < x.Count(); i++)
48  x[i].Dump(textlog);
49  ON_String str(wstr);
50  bu_log(str.Array());
51  }
52  bu_log("\n\n");
53 }
54 
55 
56 static void
57 test_csi(ON_Curve *c1, ON_Surface *s2)
58 {
59  ON_wString wstr;
60  ON_TextLog textlog(wstr);
61  ON_SimpleArray<ON_X_EVENT> x;
62 
63  // Use default tolerance
64  ON_Intersect(c1, s2, x);
65 
66  if (x.Count() == 0) {
67  bu_log("No intersection.\n");
68  } else {
69  for (int i = 0; i < x.Count(); i++)
70  x[i].Dump(textlog);
71  ON_String str(wstr);
72  bu_log(str.Array());
73  }
74  bu_log("\n\n");
75 }
76 
77 
78 int
79 main(int, char**)
80 {
81  ON_3dPoint origin(0.0, 0.0, 0.0);
82  ON_3dVector xdir(1.0, 0.0, 0.0);
83  ON_3dVector ydir(0.0, 1.0, 1.0);
84  ydir.Unitize();
85  ON_Plane plane(origin, xdir, ydir);
86  double radius = 10.0;
87 
88  // curve-curve intersection
89  bu_log("*** Curve-curve intersection ***\n");
90 
91  bu_log("Test 1:\n");
92  // circle A is a circle fixed at the origin
93  ON_Circle circleA = ON_Circle(plane, origin, radius);
94  ON_NurbsCurve *curveA = ON_NurbsCurve::New();
95  circleA.GetNurbForm(*curveA);
96 
97  // We start circle B from somewhere that it doesn't intersect with
98  // circleA, and move it closer and closer (along the x-axis). Then
99  // it should first circumscribe with circleA (one intersection point),
100  // then intersect (two points), coincident (overlap), intersect (two
101  // points), circumscribe again, and finally depart from circleA.
102 
103  ON_3dPoint start(-2.5*radius, 0.0, 0.0); // the starting center of circleB
104  ON_3dPoint end(2.5*radius, 0.0, 0.0); // the end center of circleB
105  ON_3dVector move_dir = end - start;
106  for (int i = 0; i <= 50.0; i++) {
107  ON_3dPoint centerB = start + move_dir*((double)i/50.0);
108  ON_Circle circleB(plane, centerB, radius);
109  ON_NurbsCurve *curveB = ON_NurbsCurve::New();
110  circleB.GetNurbForm(*curveB);
111  bu_log("Center of circleB: (%f,%f,%f):\n", centerB.x, centerB.y, centerB.z);
112  test_cci(curveA, curveB);
113  delete curveB;
114  }
115  delete curveA;
116 
117  bu_log("Test 2:\n");
118  // Test the merge correctness of overlap events.
119  ON_3dPointArray ptarrayA, ptarrayB;
120  ptarrayA.Append(ON_3dPoint(0.0, -1.0, 0.0));
121  ptarrayA.Append(ON_3dPoint(0.0, 1.0, 0.0));
122  ptarrayA.Append(ON_3dPoint(-1.0, 0.0, 0.0));
123  ptarrayA.Append(ON_3dPoint(1.0, 0.0, 0.0));
124  ptarrayB.Append(ON_3dPoint(0.0, -2.0, 0.0));
125  ptarrayB.Append(ON_3dPoint(0.0, 0.0, 0.0));
126  ptarrayB.Append(ON_3dPoint(2.0, 0.0, 0.0));
127  ON_PolylineCurve polyA(ptarrayA), polyB(ptarrayB);
128  ON_NurbsCurve* polycurveA = ON_NurbsCurve::New();
129  ON_NurbsCurve* polycurveB = ON_NurbsCurve::New();
130  polyA.GetNurbForm(*polycurveA);
131  polyB.GetNurbForm(*polycurveB);
132  test_cci(polycurveA, polycurveB);
133 
134  delete polycurveA;
135  delete polycurveB;
136 
137  // curve-surface intersection
138  bu_log("*** Curve-surface intersection ***\n");
139 
140  bu_log("Test 1:\n");
141  // sphere A is a circle fixed at the origin
142  ON_Sphere sphereA = ON_Sphere(origin, radius);
143  ON_NurbsSurface* surfA = ON_NurbsSurface::New();
144  sphereA.GetNurbForm(*surfA);
145 
146  // We start circle B from somewhere that it doesn't intersect with
147  // surfA, and move it closer and closer (along the x-axis). Then
148  // it should first circumscribe with circleA (one intersection point),
149  // then intersect (two points), overlap, intersect (two points),
150  // circumscribe again, and finally depart from circleA.
151 
152  for (int i = 0; i <= 50.0; i++) {
153  ON_3dPoint centerB = start + move_dir*((double)i/50.0);
154  ON_Circle circleB(plane, centerB, radius);
155  ON_NurbsCurve *curveB = ON_NurbsCurve::New();
156  circleB.GetNurbForm(*curveB);
157  bu_log("Center of circleB: (%f,%f,%f):\n", centerB.x, centerB.y, centerB.z);
158  test_csi(curveB, surfA);
159  delete curveB;
160  }
161  delete surfA;
162 
163  bu_log("Test 2:\n");
164  // Test the optimization for linear curve and planar surface intersections
165  ON_PlaneSurface planesurf(plane);
166  planesurf.SetDomain(0, -1.0, 1.0);
167  planesurf.SetDomain(1, -1.0, 1.0);
168  planesurf.SetExtents(0, planesurf.Domain(0));
169  planesurf.SetExtents(1, planesurf.Domain(1));
170 
171  // csx_point
172  ON_LineCurve line1(ON_3dPoint(0.0, 0.0, -1.0), ON_3dPoint(0.0, 0.0, 1.0));
173  test_csi(&line1, &planesurf);
174 
175  // csx_overlap
176  ON_LineCurve line2(ON_3dPoint(-2.0, 0.0, 0.0), ON_3dPoint(2.0, 0.0, 0.0));
177  test_csi(&line2, &planesurf);
178 
179  // csx_overlap
180  ON_3dPointArray ptarrayC;
181  ptarrayC.Append(ON_3dPoint(0.0, 0.0, 1.0));
182  ptarrayC.Append(ON_3dPoint(0.0, 0.0, 0.0));
183  ptarrayC.Append(ON_3dPoint(1.0, 0.0, 0.0));
184  ptarrayC.Append(ON_3dPoint(1.0, 0.0, 1.0));
185  ON_PolylineCurve polyC(ptarrayC);
186  test_csi(&polyC, &planesurf);
187 
188  bu_log("Test 3:\n");
189  // A line and a torus
190  ON_Torus torus(circleA, radius*0.5);
191  ON_LineCurve ray(ON_3dPoint(-20.0, -2.0, -1.0), ON_3dPoint(20.0, 2.0, 1.0));
192  ON_NurbsSurface* torus_surface = ON_NurbsSurface::New();
193  torus.GetNurbForm(*torus_surface);
194  test_csi(&ray, torus_surface);
195  delete torus_surface;
196 
197  bu_log("All finished.\n");
198  return 0;
199 }
200 
201 // Local Variables:
202 // tab-width: 8
203 // mode: C++
204 // c-basic-offset: 4
205 // indent-tabs-mode: t
206 // c-file-style: "stroustrup"
207 // End:
208 // 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
Header file for the BRL-CAD common definitions.
off_t end
Definition: ptbl.h:64
int main(int, char **)