1 /*****
2  * bbox3.h
3  * Andy Hammerlindl 2002/06/06
4  *
5  * Stores a rectangle that encloses a drawing object.
6  *****/
7 
8 #ifndef BBOX3_H
9 #define BBOX3_H
10 
11 #include "triple.h"
12 
13 // For CYGWIN
14 #undef near
15 #undef far
16 
17 namespace camp {
18 
19 // The box that encloses a path
20 struct bbox3 {
21   bool empty;
22   double left;
23   double bottom;
24   double near;
25   double right;
26   double top;
27   double far;
28 
29   // Start bbox3 about the origin
bbox3bbox330   bbox3()
31     : empty(true), left(0.0), bottom(0.0), near(0.0),
32       right(0.0), top(0.0), far(0.0)
33   {
34   }
35 
bbox3bbox336   bbox3(double left, double bottom, double near,
37         double right, double top, double far)
38     : empty(false), left(left), bottom(bottom), near(near),
39       right(right), top(top), far(far)
40   {
41   }
42 
43   // Start a bbox3 with a point
bbox3bbox344   bbox3(double x, double y, double z)
45     : empty(false), left(x), bottom(y), near(z), right(x), top(y), far(z)
46   {
47   }
48 
49   // Start a bbox3 with a point
bbox3bbox350   bbox3(const triple& v)
51     : empty(false), left(v.getx()), bottom(v.gety()), near(v.getz()),
52       right(v.getx()), top(v.gety()), far(v.getz())
53   {
54   }
55 
56   // Start a bbox3 with 2 points
bbox3bbox357   bbox3(const triple& m, const triple& M)
58     : empty(false),
59       left(m.getx()), bottom(m.gety()), near(m.getz()),
60       right(M.getx()),    top(M.gety()), far(M.getz())
61   {
62   }
63 
64   // Add a point to a bbox3
addbbox365   void add(const triple& v)
66   {
67     const double x = v.getx(), y = v.gety(), z = v.getz();
68     add(x,y,z);
69   }
addbbox370   void add(double x, double y, double z)
71   {
72     if (empty) {
73       left = right = x;
74       top = bottom = y;
75       near = far = z;
76       empty = false;
77     }
78     else {
79       if(x < left)
80         left = x;
81       else if(x > right)
82         right = x;
83       if(y < bottom)
84         bottom = y;
85       else if(y > top)
86         top = y;
87       if(z < near)
88         near = z;
89       else if(z > far)
90         far = z;
91     }
92   }
93 
94   // Add a point to a nonempty bbox3
addnonemptybbox395   void addnonempty(double x, double y, double z)
96   {
97     if(x < left)
98       left = x;
99     else if(x > right)
100       right = x;
101     if(y < bottom)
102       bottom = y;
103     else if(y > top)
104       top = y;
105     if(z < near)
106       near = z;
107     else if(z > far)
108       far = z;
109   }
110 
111   // Add (x,y) pair to a nonempty bbox3
addnonemptybbox3112   void addnonempty(pair v)
113   {
114     double x=v.getx();
115     if(x < left)
116       left = x;
117     else if(x > right)
118       right = x;
119     double y=v.gety();
120     if(y < bottom)
121       bottom = y;
122     else if(y > top)
123       top = y;
124   }
125 
126   // Add a point to a nonempty bbox3
addnonemptybbox3127   void addnonempty(const triple& v)
128   {
129     addnonempty(v.getx(),v.gety(),v.getz());
130   }
131 
132   // Add a point to a nonempty bbox, updating bounding times
addnonemptybbox3133   void addnonempty(const triple& v, bbox3& times, double t)
134   {
135     double x = v.getx(), y = v.gety(), z = v.getz();
136 
137     if(x < left) {
138       left = x;
139       times.left = t;
140     }
141     else if(x > right) {
142       right = x;
143       times.right = t;
144     }
145     if(y < bottom) {
146       bottom = y;
147       times.bottom = t;
148     }
149     else if(y > top) {
150       top = y;
151       times.top = t;
152     }
153     if(z < near) {
154       near = z;
155       times.near=t;
156     }
157     else if(z > far) {
158       far = z;
159       times.far=t;
160     }
161   }
162 
163   bbox3 operator+= (const triple& v)
164   {
165     add(v);
166     return *this;
167   }
168 
Minbbox3169   triple Min() const {
170     return triple(left,bottom,near);
171   }
172 
Maxbbox3173   triple Max() const {
174     return triple(right,top,far);
175   }
176 
Min2bbox3177   pair Min2() const {
178     return pair(left,bottom);
179   }
180 
Max2bbox3181   pair Max2() const {
182     return pair(right,top);
183   }
184 
185   friend ostream& operator << (ostream& out, const bbox3& b)
186   {
187     out << "Min " << b.Min() << " Max " << b.Max();
188     return out;
189   }
190 
191 };
192 
193 } // namespace camp
194 
195 GC_DECLARE_PTRFREE(camp::bbox3);
196 
197 #endif
198