Graphite  Version 3
An experimental 3D geometry processing program
geometry.h
Go to the documentation of this file.
1 /*
2  * OGF/Graphite: Geometry and Graphics Programming Library + Utilities
3  * Copyright (C) 2000 Bruno Levy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  *
19  * If you modify this software, you should include a notice giving the
20  * name of the person performing the modification, the date of modification,
21  * and the reason for such modification.
22  *
23  * Contact: Bruno Levy
24  *
25  * levy@loria.fr
26  *
27  * ISA Project
28  * LORIA, INRIA Lorraine,
29  * Campus Scientifique, BP 239
30  * 54506 VANDOEUVRE LES NANCY CEDEX
31  * FRANCE
32  *
33  * Note that the GNU General Public License does not permit incorporating
34  * the Software into proprietary programs.
35  */
36 
37 #ifndef H_OGF_BASIC_MATH_GEOMETRY_H
38 #define H_OGF_BASIC_MATH_GEOMETRY_H
39 
46 #include <geogram/basic/geometry.h>
47 
48 namespace OGF {
49 
50  namespace Geom {
51  using namespace ::GEO::Geom;
52  }
53 
54 //_________________________________________________________
55 
56 
71  // TODO: there seems to be something wrong here,
72  // depending on transform, can be a 3D point ??
73  // Check usage in tools.
74  template <class FT> vecng<2,FT> transform_point(
75  const vecng<2,FT>& v,
76  const Matrix<4,FT>& m
77  ) {
78  FT result[4] ;
79  FT w[4] ;
80  index_t i,j ;
81 
82  for(i=0; i<4; i++) {
83  result[i] = 0 ;
84  }
85  w[0] = v.x ;
86  w[1] = v.y ;
87  w[2] = FT(0) ;
88  w[3] = FT(1) ;
89 
90  for(i=0; i<4; i++) {
91  for(j=0; j<4; j++) {
92  result[i] += w[j] * m(j,i) ;
93  }
94  }
95 
96  return vecng<2,FT> (
97  result[0] / result[3],
98  result[1] / result[3]
99  ) ;
100  }
101 
102 /************************************************************/
103 
107  class Box2d {
108  public:
112  Box2d() : initialized_(false) {
113  }
114 
120  void add_point(const vec2& p) {
121  if(!initialized_) {
122  for(index_t c=0; c<2; ++c) {
123  xy_min_[c] = xy_max_[c] = p[c];
124  }
125  } else {
126  for(index_t c=0; c<2; ++c) {
127  xy_min_[c] = std::min(xy_min_[c],p[c]);
128  xy_max_[c] = std::max(xy_max_[c],p[c]);
129  }
130  }
131  }
132 
140  void add_box(const Box2d& B) {
141  if(B.initialized_) {
142  add_point(vec2(B.xy_min_));
143  add_point(vec2(B.xy_max_));
144  }
145  }
146 
151  double width() const {
152  return initialized_ ? (xy_max_[0] - xy_min_[0]) : 0.0;
153  }
154 
159  double height() const {
160  return initialized_ ? (xy_max_[1] - xy_min_[1]) : 0.0;
161  }
162 
170  double x_min() const {
171  return xy_min_[0];
172  }
173 
181  double x_max() const {
182  return xy_max_[0];
183  }
184 
192  double y_min() const {
193  return xy_min_[1];
194  }
195 
203  double y_max() const {
204  return xy_max_[1];
205  }
206 
213  bool initialized() const {
214  return initialized_;
215  }
216 
220  void clear() {
221  initialized_ = false;
222  }
223 
224  private:
225  double xy_min_[2];
226  double xy_max_[2];
227  bool initialized_;
228  };
229 
230 //_________________________________________________________
231 
235  class Box3d : public Box {
236  public:
240  Box3d() : initialized_(false) {
241  }
242 
243 
250  bool initialized() const {
251  return initialized_;
252  }
253 
259  void add_point(const vec3& p) {
260  if(initialized_) {
261  for(index_t c=0; c<3; ++c) {
262  Box::xyz_min[c] = std::min(Box::xyz_min[c],p[c]);
263  Box::xyz_max[c] = std::max(Box::xyz_max[c],p[c]);
264  }
265  } else {
266  for(index_t c=0; c<3; ++c) {
267  Box::xyz_min[c] = Box::xyz_max[c] = p[c];
268  }
269  initialized_ = true;
270  }
271  }
272 
280  void add_box(const Box3d& B) {
281  if(B.initialized()) {
282  add_point(vec3(B.xyz_min));
283  add_point(vec3(B.xyz_max));
284  }
285  }
286 
291  vec3 center() const {
292  vec3 result;
293  for(index_t c=0; c<3; ++c) {
294  result[c] = 0.5*(Box::xyz_min[c] + Box::xyz_max[c]);
295  }
296  return result;
297  }
298 
304  double radius() const {
305  double result = 0.0;
306  for(index_t c=0; c<3; ++c) {
307  result += ogf_sqr(Box::xyz_max[c] - Box::xyz_min[c]);
308  }
309  return 0.5*::sqrt(result);
310  }
311 
319  double x_min() const {
320  return xyz_min[0];
321  }
322 
330  double y_min() const {
331  return xyz_min[1];
332  }
333 
341  double z_min() const {
342  return xyz_min[2];
343  }
344 
352  double x_max() const {
353  return xyz_max[0];
354  }
355 
363  double y_max() const {
364  return xyz_max[1];
365  }
366 
374  double z_max() const {
375  return xyz_max[2];
376  }
377 
381  void clear() {
382  initialized_ = false;
383  }
384 
385  private:
386  bool initialized_;
387  };
388 }
389 
390 #endif
Axis-aligned bounding box.
Definition: geometry.h:669
A matrix type.
Definition: matrix.h:66
Generic maths vector.
Definition: vecg.h:70
A 2d axis aligned box.
Definition: geometry.h:107
void add_box(const Box2d &B)
Adds a box to this Box2d.
Definition: geometry.h:140
double x_min() const
Gets the minimum x coordinate in this box.
Definition: geometry.h:170
double y_max() const
Gets the maximum y coordinate in this box.
Definition: geometry.h:203
double y_min() const
Gets the minimum y coordinate in this box.
Definition: geometry.h:192
double width() const
Gets the width of this box.
Definition: geometry.h:151
bool initialized() const
Tests whether the box is initialized.
Definition: geometry.h:213
double height() const
Gets the height of this box.
Definition: geometry.h:159
double x_max() const
Gets the maximum x coordinate in this box.
Definition: geometry.h:181
void add_point(const vec2 &p)
Adds a point to this Box2d.
Definition: geometry.h:120
Box2d()
Constructs a new uninitialized Box2d.
Definition: geometry.h:112
void clear()
Clears the box.
Definition: geometry.h:220
A 3d axis aligned box.
Definition: geometry.h:235
void add_box(const Box3d &B)
Adds a box to this Box3d.
Definition: geometry.h:280
double radius() const
Gets the radius of this Box3d.
Definition: geometry.h:304
void add_point(const vec3 &p)
Adds a point to this Box3d.
Definition: geometry.h:259
bool initialized() const
Tests whether the box is initialized.
Definition: geometry.h:250
double x_min() const
Gets the minimum x coordinate in this box.
Definition: geometry.h:319
double z_min() const
Gets the minimum z coordinate in this box.
Definition: geometry.h:341
double y_min() const
Gets the minimum y coordinate in this box.
Definition: geometry.h:330
double y_max() const
Gets the maximum y coordinate in this box.
Definition: geometry.h:363
Box3d()
Constructs a new uninitialized Box3d.
Definition: geometry.h:240
void clear()
Clears the box.
Definition: geometry.h:381
double z_max() const
Gets the maximum z coordinate in this box.
Definition: geometry.h:374
double x_max() const
Gets the maximum x coordinate in this box.
Definition: geometry.h:352
vec3 center() const
Gets the center of this Box3d.
Definition: geometry.h:291
Geometric functions in 2d and 3d.
Geometric functions and utilities.
Definition: geometry.h:181
vecng< 3, Numeric::float64 > vec3
Represents points and vectors in 3d.
Definition: geometry.h:65
geo_index_t index_t
The type for storing and manipulating indices.
Definition: numeric.h:329
vecng< 2, Numeric::float64 > vec2
Represents points and vectors in 2d.
Definition: geometry.h:59
Global Graphite namespace.
Definition: common.h:76
vecng< 2, FT > transform_point(const vecng< 2, FT > &v, const Matrix< 4, FT > &m)
Applies a 3d transform to a 2d point.
Definition: geometry.h:74
Definitions common to all include files in the basic library.