BRL-CAD
test_diff.c
Go to the documentation of this file.
1 /* T E S T _ D I F F . C
2  * BRL-CAD
3  *
4  * Copyright (c) 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 <string.h>
24 
25 #include "tcl.h"
26 
27 #include "mater.h"
28 #include "raytrace.h"
29 #include "rt/db_diff.h"
30 
31 void
32 print_diff_summary(struct bu_ptbl *results)
33 {
34  int i = 0;
35  for (i = 0; i < (int)BU_PTBL_LEN(results); i++) {
36  struct diff_result *dr = (struct diff_result *)BU_PTBL_GET(results, i);
37  if (dr->param_state != DIFF_UNCHANGED) {
38  int j = 0;
39  bu_log("Object %s parameters have changed:\n", dr->obj_name);
40  for (j = 0; j < (int)BU_PTBL_LEN(dr->param_diffs); j++) {
41  struct diff_avp *avp = (struct diff_avp *)BU_PTBL_GET(dr->param_diffs, j);
42  if (avp->state == DIFF_ADDED)
43  bu_log("A %s: %s\n", avp->name, avp->right_value);
44  if (avp->state == DIFF_REMOVED)
45  bu_log("D %s: %s\n", avp->name, avp->left_value);
46  if (avp->state == DIFF_CHANGED)
47  bu_log("M %s: %s -> %s\n", avp->name, avp->left_value, avp->right_value);
48  }
49  bu_log("\n");
50  }
51  if (dr->attr_state != DIFF_UNCHANGED && dr->attr_state != DIFF_EMPTY) {
52  int j = 0;
53  bu_log("Object %s attributes have changed:\n", dr->obj_name);
54  for (j = 0; j < (int)BU_PTBL_LEN(dr->attr_diffs); j++) {
55  struct diff_avp *avp = (struct diff_avp *)BU_PTBL_GET(dr->attr_diffs, j);
56  if (avp->state == DIFF_ADDED)
57  bu_log("A %s: %s\n", avp->name, avp->right_value);
58  if (avp->state == DIFF_REMOVED)
59  bu_log("D %s: %s\n", avp->name, avp->left_value);
60  if (avp->state == DIFF_CHANGED)
61  bu_log("M %s: %s -> %s\n", avp->name, avp->left_value, avp->right_value);
62  }
63  bu_log("\n");
64  }
65  if ((dr->param_state != DIFF_UNCHANGED && dr->param_state != DIFF_EMPTY) ||
66  (dr->attr_state != DIFF_UNCHANGED && dr->attr_state != DIFF_EMPTY)) {
67  bu_log("\n");
68  }
69  }
70 }
71 
72 int
73 main(int argc, char **argv)
74 {
75  int i = 0;
76  int diff_state = 0;
77  struct bu_ptbl results;
78  struct db_i *dbip1 = DBI_NULL;
79  struct db_i *dbip2 = DBI_NULL;
80  /*struct bu_vls diff_log = BU_VLS_INIT_ZERO;*/
81  struct bn_tol *diff_tol;
82  BU_GET(diff_tol, struct bn_tol);
83  diff_tol->dist = BN_TOL_DIST;
84 
85  BU_PTBL_INIT(&results);
86 
87  if (argc != 3) {
88  bu_log("Error - please specify two .g files\n");
89  bu_exit(EXIT_FAILURE, NULL);
90  }
91 
92  if (!bu_file_exists(argv[1], NULL)) {
93  bu_exit(1, "Cannot stat file %s\n", argv[1]);
94  }
95 
96  if (!bu_file_exists(argv[2], NULL)) {
97  bu_exit(1, "Cannot stat file %s\n", argv[2]);
98  }
99 
100  if ((dbip1 = db_open(argv[1], DB_OPEN_READONLY)) == DBI_NULL) {
101  bu_exit(1, "Cannot open geometry database file %s\n", argv[1]);
102  }
103  RT_CK_DBI(dbip1);
104  if (db_dirbuild(dbip1) < 0) {
105  db_close(dbip1);
106  bu_exit(1, "db_dirbuild failed on geometry database file %s\n", argv[1]);
107  }
108 
109  /* Reset the material head so we don't get warnings when the global
110  * is overwritten. This will go away when material_head ceases to
111  * be a librt global.*/
112  rt_new_material_head(MATER_NULL);
113 
114  if ((dbip2 = db_open(argv[2], DB_OPEN_READONLY)) == DBI_NULL) {
115  bu_exit(1, "Cannot open geometry database file %s\n", argv[2]);
116  }
117  RT_CK_DBI(dbip2);
118  if (db_dirbuild(dbip2) < 0) {
119  db_close(dbip1);
120  db_close(dbip2);
121  bu_exit(1, "db_dirbuild failed on geometry database file %s\n", argv[1]);
122  }
123 
124 
125  diff_state = db_diff(dbip1, dbip2, diff_tol, DB_COMPARE_ALL, &results);
126  if (diff_state == DIFF_UNCHANGED) {
127  bu_log("No differences found.\n");
128  } else {
129  print_diff_summary(&results);
130  }
131 
132  for (i = 0; i < (int)BU_PTBL_LEN(&results); i++) {
133  struct diff_result *result = (struct diff_result *)BU_PTBL_GET(&results, i);
134  diff_free_result(result);
135  }
136 
137  bu_ptbl_free(&results);
138  BU_PUT(diff_tol, struct bn_tol);
139 
140  db_close(dbip1);
141  db_close(dbip2);
142  return 0;
143 }
144 
145 /*
146  * Local Variables:
147  * tab-width: 8
148  * mode: C
149  * indent-tabs-mode: t
150  * c-file-style: "stroustrup"
151  * End:
152  * ex: shiftwidth=4 tabstop=8
153  */
int attr_state
Definition: db_diff.h:59
Definition: raytrace.h:800
void bu_log(const char *,...) _BU_ATTR_PRINTF12
Definition: log.c:176
char * right_value
Definition: db_diff.h:48
#define DIFF_CHANGED
Definition: db_diff.h:36
double dist
>= 0
Definition: tol.h:73
char * left_value
Definition: db_diff.h:46
Header file for the BRL-CAD common definitions.
int param_state
Definition: db_diff.h:58
Definition: ptbl.h:62
#define DIFF_UNCHANGED
Definition: db_diff.h:33
void bu_exit(int status, const char *fmt,...) _BU_ATTR_NORETURN _BU_ATTR_PRINTF23
Definition: bomb.c:195
struct bu_ptbl * param_diffs
Definition: db_diff.h:60
#define BU_PTBL_GET(ptbl, i)
Definition: ptbl.h:108
#define DIFF_EMPTY
Definition: db_diff.h:32
#define BU_GET(_ptr, _type)
Definition: malloc.h:201
void diff_free_result(struct diff_result *result)
Definition: db_diff.c:299
void rt_new_material_head(struct mater *newmat)
Definition: mater.c:253
#define BN_TOL_DIST
Definition: tol.h:109
char * name
Definition: db_diff.h:44
void db_close(struct db_i *dbip)
#define BU_PTBL_INIT(_p)
Definition: ptbl.h:80
#define BU_PUT(_ptr, _type)
Definition: malloc.h:215
Support for uniform tolerances.
Definition: tol.h:71
#define BU_PTBL_LEN(ptbl)
Definition: ptbl.h:107
void bu_ptbl_free(struct bu_ptbl *b)
Definition: ptbl.c:226
int db_diff(const struct db_i *dbip_left, const struct db_i *dbip_right, const struct bn_tol *diff_tol, db_compare_criteria_t flags, struct bu_ptbl *diff_results)
Definition: db_diff.c:640
#define RT_CK_DBI(_p)
Definition: raytrace.h:829
char * obj_name
Definition: db_diff.h:53
#define DBI_NULL
Definition: raytrace.h:827
struct bu_ptbl * attr_diffs
Definition: db_diff.h:61
#define DB_OPEN_READONLY
Definition: raytrace.h:3550
int main(int argc, char **argv)
Definition: test_diff.c:73
struct db_i * db_open(const char *name, const char *mode)
Definition: db_open.c:59
void print_diff_summary(struct bu_ptbl *results)
Definition: test_diff.c:32
int db_dirbuild(struct db_i *dbip)
Definition: db5_scan.c:301
int bu_file_exists(const char *path, int *fd)
Definition: file.c:57
int state
Definition: db_diff.h:45