BRL-CAD
nurbs_tests.cpp
Go to the documentation of this file.
1 #include "common.h"
2 
3 #include <cstdlib>
4 #include <cstdio>
5 #include <cstring>
6 #include <limits>
7 #include <iomanip>
8 
9 #include "brep.h"
10 #include "raytrace.h"
11 
12 
13 int
14 get_surface(const char *name, struct db_i *dbip, struct rt_db_internal *intern, struct rt_brep_internal **brep_ip) {
15 
16  struct directory *dp;
17  (*brep_ip) = NULL;
18 
19 
20  dp = db_lookup(dbip, name, LOOKUP_QUIET);
21  if (dp == RT_DIR_NULL) {
22  bu_log("ERROR: Unable to look up object %s\n", name);
23  return -1;
24  }
25 
26  if (rt_db_get_internal(intern, dp, dbip, NULL, &rt_uniresource) < 0) {
27  bu_log("ERROR: Unable to get internal representation of %s\n", name);
28  return -1;
29  }
30 
31  if (intern->idb_minor_type != DB5_MINORTYPE_BRLCAD_BREP) {
32  bu_log("ERROR: object %s does not appear to be of type BRep\n", name);
33  return -1;
34  } else {
35  (*brep_ip) = (struct rt_brep_internal *)(intern->idb_ptr);
36  }
37 
38  RT_BREP_CK_MAGIC(*brep_ip);
39  return 0;
40 }
41 
42 int
43 nurbs_test(long int test_number, struct db_i *dbip)
44 {
45  /* Have various specific surface structures - there are potentially
46  * multiple surfaces in the .g, but multiple tests may re-use a single
47  * surface.*/
48 
49  /* openNURBS pointers */
50  ON_Brep *brep = NULL;
51 
52  switch (test_number)
53  {
54  case 1: /* 3d -> 2d pull-back function get_closest_point */
55  {
56  struct rt_db_internal case_1_intern;
57  struct rt_brep_internal *case_1_brep_ip = NULL;
58  RT_DB_INTERNAL_INIT(&case_1_intern);
59  if (get_surface("case_1_surface.s", dbip, &case_1_intern, &case_1_brep_ip)) return -1;
60  brep = case_1_brep_ip->brep;
61  ON_BrepFace& c1_face = brep->m_F[0];
62 
63  ON_2dPoint pt_2d_1, pt_2d_2;
64  ON_3dPoint pt_3d_1(11204.05366897583,-16726.489562988281, 3358.7263298034668);
65  ON_3dPoint pt_3d_2(11204.007682800293, -16726.479568481445, 3358.8327312469482);
66  ON_3dPoint p3d_pullback_1 = ON_3dPoint::UnsetPoint;
67  ON_3dPoint p3d_pullback_2 = ON_3dPoint::UnsetPoint;
68  double distance1 = DBL_MAX;
69  double distance2 = DBL_MAX;
70  bool p1_result = surface_GetClosestPoint3dFirstOrder(c1_face.SurfaceOf(),pt_3d_1,pt_2d_1,p3d_pullback_1,distance1,0,BREP_SAME_POINT_TOLERANCE,BREP_EDGE_MISS_TOLERANCE);
71  bool p2_result = surface_GetClosestPoint3dFirstOrder(c1_face.SurfaceOf(),pt_3d_2,pt_2d_2,p3d_pullback_2,distance2,0,BREP_SAME_POINT_TOLERANCE,BREP_EDGE_MISS_TOLERANCE);
72  rt_db_free_internal(&case_1_intern);
73  if (pt_2d_1 == pt_2d_2) {
74  std::cout << "NURBS test case 1 failure (surface_GetClosestPoint3dFirstOrder): Unexpectedly identical 2D pullbacks from different 3D points\n";
75  std::cout << std::setprecision(std::numeric_limits<double>::digits10) << "Inputs: 3d Point 1: " << pt_3d_1.x << "," << pt_3d_1.y << "," << pt_3d_1.z << "\n";
76  std::cout << std::setprecision(std::numeric_limits<double>::digits10) << " 3d Point 2: " << pt_3d_2.x << "," << pt_3d_2.y << "," << pt_3d_2.z << "\n";
77  std::cout << std::setprecision(std::numeric_limits<double>::digits10) << "Output: 2d Point 1: " << pt_2d_1.x << "," << pt_2d_1.y << "\n";
78  std::cout << std::setprecision(std::numeric_limits<double>::digits10) << " 2d Point 2: " << pt_2d_2.x << "," << pt_2d_2.y << "\n";
79  return -1;
80  }
81  if (p1_result == false) std::cout << "Warning - p1 pullback failed\n";
82  if (p2_result == false) std::cout << "Warning - p2 pullback failed\n";
83  return 0;
84  }
85  default:
86  return -1;
87  }
88 
89 }
90 
91 
92 int
93 main(int argc, char **argv)
94 {
95  long int test_number = 0;
96  int retval = 0;
97  long int all_tests = 1;
98  struct db_i *dbip;
99  char *endptr;
100 
101  /* Set up the .g file */
102  if (argc > 3 || argc < 2) {
103  bu_log("Usage: %s file.g [test_number]", argv[0]);
104  return -1;
105  }
106 
107  dbip = db_open(argv[1], DB_OPEN_READWRITE);
108  if (dbip == DBI_NULL) {
109  bu_log("ERROR: Unable to read from geometry database file %s\n", argv[1]);
110  return -1;
111  }
112 
113  if (db_dirbuild(dbip) < 0) {
114  bu_log("ERROR: Unable to read from %s\n", argv[1]);
115  return -1;
116  }
117 
118  if (argc == 3) {
119  test_number = strtol(argv[2], &endptr, 10);
120  if (*endptr) return -1;
121  }
122 
123  if (!test_number) {
124  int current_test = 1;
125  while (current_test <= all_tests && retval != -1) {
126  retval = nurbs_test(current_test, dbip);
127  current_test++;
128  }
129  } else {
130  retval = nurbs_test(test_number, dbip);
131  }
132  return retval;
133 }
134 
135 /*
136  * Local Variables:
137  * mode: C
138  * tab-width: 8
139  * indent-tabs-mode: t
140  * c-file-style: "stroustrup"
141  * End:
142  * ex: shiftwidth=4 tabstop=8
143  */
Definition: raytrace.h:800
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
int rt_db_get_internal(struct rt_db_internal *ip, const struct directory *dp, const struct db_i *dbip, const mat_t mat, struct resource *resp)
Definition: dir.c:76
int main(int argc, char **argv)
Definition: nurbs_tests.cpp:93
Definition: clone.c:90
struct directory * db_lookup(const struct db_i *, const char *name, int noisy)
Definition: db_lookup.c:153
Header file for the BRL-CAD common definitions.
#define DB_OPEN_READWRITE
Definition: raytrace.h:3555
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
#define RT_DB_INTERNAL_INIT(_p)
Definition: raytrace.h:199
#define LOOKUP_QUIET
Definition: raytrace.h:893
bool surface_GetClosestPoint3dFirstOrder(const ON_Surface *surf, const ON_3dPoint &p, ON_2dPoint &p2d, ON_3dPoint &p3d, double &current_distance, int quadrant, double same_point_tol, double within_distance_tol)
void * idb_ptr
Definition: raytrace.h:195
#define DBI_NULL
Definition: raytrace.h:827
#define RT_DIR_NULL
Definition: raytrace.h:875
struct db_i * db_open(const char *name, const char *mode)
Definition: db_open.c:59
int idb_minor_type
ID_xxx.
Definition: raytrace.h:193
int db_dirbuild(struct db_i *dbip)
Definition: db5_scan.c:301
int nurbs_test(long int test_number, struct db_i *dbip)
Definition: nurbs_tests.cpp:43
void rt_db_free_internal(struct rt_db_internal *ip)
Definition: dir.c:216
int get_surface(const char *name, struct db_i *dbip, struct rt_db_internal *intern, struct rt_brep_internal **brep_ip)
Definition: nurbs_tests.cpp:14