BRL-CAD
test_nurbsfit.cpp
Go to the documentation of this file.
1 /* T E S T _ N U R B S F I 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_nurbsfit.cpp
21  *
22  * Test the fitting routines that map a NURBS surface to a BoT patch.
23  * *Very* simple - doesn't validate that the BoT projects to a plane
24  * without overlaps.
25  *
26  */
27 
28 #include "common.h"
29 #include "vmath.h"
30 #include "raytrace.h"
31 #include "wdb.h"
32 
33 #include "opennurbs_fit.h"
34 
35 void
36 BoT2Vector3d(struct rt_bot_internal *ip, on_fit::vector_vec3d &data)
37 {
38  int i = 0;
39  for (i = 0; i < ip->num_vertices; i++) {
40  //printf("v %f %f %f\n", V3ARGS(&ip->vertices[3*i]));
41  data.push_back (ON_3dPoint(V3ARGS(&ip->vertices[3*i])));
42  }
43 }
44 
45 
46 int
47 main (int argc, char *argv[])
48 {
49  struct db_i *dbip;
50  struct directory *dp;
51  struct rt_db_internal intern;
52  struct rt_bot_internal *bot_ip;
53  struct rt_wdb *wdbp;
54 
55  unsigned order (3);
56  unsigned refinement (5);
57  unsigned iterations (2);
59  ON_Brep* brep = ON_Brep::New();
60  char *bname;
61 
62 
63  if (argc != 3) {
64  bu_exit(1, "Usage: %s file.g object", argv[0]);
65  }
66 
67  //dbip = db_open(argv[1], DB_OPEN_READONLY);
68  dbip = db_open(argv[1], DB_OPEN_READWRITE);
69  if (dbip == DBI_NULL) {
70  bu_exit(1, "ERROR: Unable to read from %s\n", argv[1]);
71  }
72 
73  if (db_dirbuild(dbip) < 0)
74  bu_exit(1, "ERROR: Unable to read from %s\n", argv[1]);
75 
76  dp = db_lookup(dbip, argv[2], LOOKUP_QUIET);
77  if (dp == RT_DIR_NULL) {
78  bu_exit(1, "ERROR: Unable to look up object %s\n", argv[2]);
79  } else {
80  bu_log("Got %s\n", dp->d_namep);
81  }
82 
83  RT_DB_INTERNAL_INIT(&intern)
84  if (rt_db_get_internal(&intern, dp, dbip, NULL, &rt_uniresource) < 0) {
85  bu_exit(1, "ERROR: Unable to get internal representation of %s\n", argv[2]);
86  }
87 
88  if (intern.idb_minor_type != DB5_MINORTYPE_BRLCAD_BOT) {
89  bu_exit(1, "ERROR: object %s does not appear to be of type BoT\n", argv[2]);
90  } else {
91  bot_ip = (struct rt_bot_internal *)intern.idb_ptr;
92  }
93  RT_BOT_CK_MAGIC(bot_ip);
94 
95  BoT2Vector3d(bot_ip, data.interior);
96 
97  ON_NurbsSurface nurbs = on_fit::FittingSurface::initNurbsPCABoundingBox (order, &data);
98  on_fit::FittingSurface fit (&data, nurbs);
100  params.interior_smoothness = 0.15;
101  params.interior_weight = 1.0;
102  params.boundary_smoothness = 0.15;
103  params.boundary_weight = 0.0;
104 
105  // NURBS refinement
106  for (unsigned i = 0; i < refinement; i++)
107  {
108  fit.refine (0);
109  fit.refine (1);
110  }
111 
112  // fitting iterations
113  for (unsigned i = 0; i < iterations; i++)
114  {
115  bu_log("Assembling params - iteration %d\n", i);
116  fit.assemble (params);
117  bu_log("Solving - iteration %d\n", i);
118  fit.solve ();
119  }
120 
121  // print resulting surface
122  ON_wString wonstr;
123  ON_TextLog log(wonstr);
124  fit.m_nurbs.Dump(log);
125  ON_String onstr = ON_String(wonstr);
126  printf("NURBS surface:\n");
127  printf("%s\n", onstr.Array());
128 
129  brep->NewFace(fit.m_nurbs);
130  wdbp = wdb_dbopen(dbip, RT_WDB_TYPE_DB_DISK);
131  bname = (char*)bu_malloc(strlen(argv[2])+6, "char");
132  bu_strlcpy(bname, argv[2], strlen(argv[2])+1);
133  bu_strlcat(bname, "_brep", strlen(bname)+6);
134  if (mk_brep(wdbp, bname, brep) == 0) {
135  bu_log("Generated brep object %s\n", bname);
136  }
137  bu_free(bname, "char");
138 
139  return 0;
140 }
char * d_namep
pointer to name string
Definition: raytrace.h:859
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
static ON_NurbsSurface initNurbsPCABoundingBox(int order, NurbsDataSurface *data, ON_3dVector z=ON_3dVector(0.0, 0.0, 1.0))
Initializing a B-Spline surface using principal-component-analysis and bounding box of points...
Fitting a B-Spline surface to 3D point-clouds using point-distance-minimization Based on paper: TODO...
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.
void refine(int dim)
Refines surface by inserting a knot in the middle of each element.
virtual void assemble(Parameter param=Parameter())
Assemble the system of equations for fitting.
void * bu_malloc(size_t siz, const char *str)
Definition: malloc.c:314
#define DB_OPEN_READWRITE
Definition: raytrace.h:3555
ON_NurbsSurface m_nurbs
COMPLEX data[64]
Definition: fftest.c:34
vector_vec3d interior
< input
struct resource rt_uniresource
default. Defined in librt/globals.c
Definition: globals.c:41
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
#define RT_DB_INTERNAL_INIT(_p)
Definition: raytrace.h:199
int main(int argc, char *argv[])
#define LOOKUP_QUIET
Definition: raytrace.h:893
Parameters for fitting.
#define bu_strlcpy(dst, src, size)
Definition: str.h:60
#define V3ARGS(a)
Definition: color.c:56
#define RT_WDB_TYPE_DB_DISK
Definition: raytrace.h:1295
Data structure for NURBS surface fitting (FittingSurface, FittingSurfaceTDM, FittingCylinder, GlobalOptimization, GlobalOptimizationTDM)
Definition: opennurbs_fit.h:96
std::vector< ON_3dVector > vector_vec3d
Definition: opennurbs_fit.h:92
int mk_brep(struct rt_wdb *wdbp, const char *name, ON_Brep *brep)
Definition: brep.cpp:42
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
void bu_free(void *ptr, const char *str)
Definition: malloc.c:328
int db_dirbuild(struct db_i *dbip)
Definition: db5_scan.c:301
struct rt_wdb * wdb_dbopen(struct db_i *dbip, int mode)
Definition: wdb.c:64
virtual void solve(double damp=1.0)
Solve system of equations using Eigen or UmfPack (can be defined in on_nurbs.cmake), and updates B-Spline surface if a solution can be obtained.
void BoT2Vector3d(struct rt_bot_internal *ip, on_fit::vector_vec3d &data)
#define bu_strlcat(dst, src, size)
Definition: str.h:50