1 /*
2 Copyright (C) 2003 Cedric Cellier, Dominique Lavault
3 
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17 */
18 #ifndef CSG_H
19 #define CSG_H
20 
21 #include "data.h"
22 
23 typedef enum { CSG_STRING, CSG_PRIM, CSG_OP } csg_node_type;
24 typedef enum { CSG_NOOP, CSG_MINUS, CSG_UNION, CSG_AND } csg_op;
25 
26 typedef struct {
27 	char vide;
28 	GLfloat p_min[3];
29 	GLfloat p_max[3];
30 } b_box;
31 
32 typedef struct {
33 	char vide;
34 	GLint xmin, ymin;
35 	GLint xmax, ymax;
36 } b_box_2d;
37 
38 /* we use primitives rather than mesh for the enigms, because we
39  * have a special positionning system, and we don't care about
40  * rendering informations of a mesh */
41 struct csg_node_s {
42 	csg_node_type type;
43 	union {
44 		struct {
45 			char *string;
46 			unsigned len;
47 		} s;
48 		struct {
49 			mesh *m;
50 			b_box box3d;
51 			b_box_2d box2d;
52 		} prim;
53 		struct {
54 			csg_op op;
55 			struct csg_node_s *left, *right;
56 		} op;
57 	} u;
58 };
59 typedef struct csg_node_s csg_node;
60 
61 #define NB_MAX_PRIMS_IN_TREE 16
62 
63 typedef struct {
64 	unsigned nb_unions;
65 	struct {
66 		unsigned nb_products;
67 		struct {
68 			csg_node *node;
69 			csg_op op_to_next;
70 		} products[NB_MAX_PRIMS_IN_TREE];
71 	} unions[NB_MAX_PRIMS_IN_TREE];
72 } csg_union_of_products;
73 
74 /* A partial product is an array of primitive, the first
75  * beeing the target and the other the trimmers (primitive against which the
76  * target is trimmed). Beeing frontal, for a trimmer, means that target
77  * intersects the negation of this primitive ; and for the target, beeing frontal
78  * means targeting the front facing faces, back facing otherwise.
79  * The union_of_product must be converted to a union of partial products before
80  * rendering.
81  */
82 
83 typedef struct {
84 	unsigned nb_products;
85 	unsigned group_id;
86 	struct {
87 		csg_node *node;
88 		char frontal;
89 	} products[NB_MAX_PRIMS_IN_TREE];
90 } csg_trimmed_target;	/* the first product is the target */
91 
92 typedef struct {
93 	unsigned nb_unions;
94 	csg_trimmed_target unions[NB_MAX_PRIMS_IN_TREE*NB_MAX_PRIMS_IN_TREE];
95 	b_box_2d max2d_group[NB_MAX_PRIMS_IN_TREE];	/* max 2d region of a group */
96 } csg_union_of_partial_products;
97 
98 /* ascii representation of operators */
99 extern const char operator[];	/* c++ compilos wont like this one :) */
100 
101 /* build a csg tree from a string, with the primitives given, or NULL if error */
102 extern csg_node *csg_build_tree(char *, unsigned, mesh **);
103 /* deletes (recursively) a node - then, a tree - */
104 extern void csg_node_del(csg_node *);
105 /* for debug purpose, print the csg tree on stdout */
106 extern void csg_print_tree(csg_node *);
107 /* computes the flat version of a csg tree : a union of products */
108 extern csg_union_of_products *csg_union_of_products_new(csg_node *);
109 /* rebuilds the uop */
110 extern int csg_union_of_products_reset(csg_union_of_products *, csg_node *);
111 /* deletes it */
112 extern void csg_union_of_products_del(csg_union_of_products *);
113 /* for debug purpose, prints it */
114 extern void csg_union_of_products_print(csg_union_of_products *);
115 /* get bounding box of a csg */
116 void csg_get_b_box(csg_node *, b_box *);
117 /* computes the union of partial products */
118 extern csg_union_of_partial_products *csg_union_of_partial_products_new(csg_union_of_products *);
119 /* rebuilds the uopp */
120 extern int csg_union_of_partial_products_reset(csg_union_of_partial_products *, csg_union_of_products *);
121 /* deletes it */
122 extern void csg_union_of_partial_products_del(csg_union_of_partial_products *);
123 /* for debug purpose, prints it */
124 extern void csg_union_of_partial_products_print(csg_union_of_partial_products *);
125 /* compute the bounding boxs (3d and 2d) for the csg once the camera and projection have been set up */
126 extern void csg_union_of_partial_products_resize(csg_union_of_partial_products *);
127 /* tells wether 2 csg are the same (without considering positions) */
128 extern int csg_is_equivalent(csg_union_of_partial_products *, csg_union_of_partial_products *);
129 
130 #endif
131