nbox.h

00001 /*<std-header orig-src='shore' incl-file-exclusion='NBOX_H'>
00002 
00003  $Id: nbox.h,v 1.19 2010/05/26 01:20:21 nhall Exp $
00004 
00005 SHORE -- Scalable Heterogeneous Object REpository
00006 
00007 Copyright (c) 1994-99 Computer Sciences Department, University of
00008                       Wisconsin -- Madison
00009 All Rights Reserved.
00010 
00011 Permission to use, copy, modify and distribute this software and its
00012 documentation is hereby granted, provided that both the copyright
00013 notice and this permission notice appear in all copies of the
00014 software, derivative works or modified versions, and any portions
00015 thereof, and that both notices appear in supporting documentation.
00016 
00017 THE AUTHORS AND THE COMPUTER SCIENCES DEPARTMENT OF THE UNIVERSITY
00018 OF WISCONSIN - MADISON ALLOW FREE USE OF THIS SOFTWARE IN ITS
00019 "AS IS" CONDITION, AND THEY DISCLAIM ANY LIABILITY OF ANY KIND
00020 FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
00021 
00022 This software was developed with support by the Advanced Research
00023 Project Agency, ARPA order number 018 (formerly 8230), monitored by
00024 the U.S. Army Research Laboratory under contract DAAB07-91-C-Q518.
00025 Further funding for this work was provided by DARPA through
00026 Rome Research Laboratory Contract No. F30602-97-2-0247.
00027 
00028 */
00029 
00030 #ifndef NBOX_H
00031 #define NBOX_H
00032 
00033 #include "w_defines.h"
00034 
00035 /*  -- do not edit anything above this line --   </std-header>*/
00036 
00037 #include <w_base.h>
00038 #include <iosfwd>
00039 
00040 #ifdef __GNUG__
00041 #pragma interface
00042 #endif
00043 
00044 /**\brief Spatial object class: n-dimensional box 
00045  * \details
00046 * An n-dimensional box is represented as: 
00047 * -xlow, ylow,  (lower left point)
00048 * -xhigh, yhigh. (higher right point).
00049 * Only integer coordinates are supported for the points.
00050 * This is used in the API for R*-trees.
00051 */ 
00052 class nbox_t {
00053     friend ostream& operator<<(ostream& os, const nbox_t& box);
00054 
00055 public:
00056     typedef w_base_t::int4_t int4_t;
00057 
00058     enum { max_dimension = 4 }; 
00059     static const int4_t    max_int4;
00060     static const int4_t    min_int4;
00061 
00062     /**\brief Special marker for queries.
00063     * This is not a box with area 0, but rather, a
00064     * non-box i.e., not a legit value
00065     */
00066     static nbox_t&     Null;
00067 
00068 public:
00069     /**\brief Enum to describe comparisons. */ 
00070     enum sob_cmp_t { t_exact = 1, t_overlap, t_cover, t_inside, t_bad };
00071 
00072     bool is_Null() const { return (dim == 0)?true:false; }
00073 
00074 protected:
00075     int4_t    array[2*max_dimension];    // boundary points
00076     int       dim;              // dimension
00077 private:
00078     fill4    filler;         // 8 byte alignment
00079     int4_t* box() { return array; }    // reveal internal storage
00080 public:
00081     /** Create a box.
00082      * @param[in] dimension  Number of dimensions for the "box".
00083      */
00084     nbox_t(int dimension = max_dimension);
00085     /** Create a box.
00086      * @param[in] dimension  Number of dimensions for the "box".
00087      * @param[in] box   Contains the dimensions.
00088      */
00089     nbox_t(int dimension, int box[]);
00090     /** Create a box.
00091      * @param[in] nbox  Source for copy constructor.
00092      */
00093     nbox_t(const nbox_t& nbox);
00094     /** Create a box.
00095      * @param[in] s     String: used only for Tcl-based tester.
00096      * @param[in] len   Length of s; used for Tcl-based tester.
00097      */
00098     nbox_t(const char* s, int len);    // for conversion from tuple key 
00099 
00100     virtual ~nbox_t() {}
00101 
00102     /// return given dimension
00103     int dimension() const     { return dim; }
00104     /// return highest value for \a nth dimension
00105     int bound(int n) const     { return array[n]; }
00106     /// return length of \a nth side
00107     int side(int n) const      { return array[n+dim]-array[n]; }
00108     /// return middle of \a nth side
00109     int center(int n) const { return (array[n+dim]-array[n])/2+array[n]; }
00110 
00111     bool    empty() const;    // test if box is empty
00112     void    squared();    // make the box squared
00113     void    nullify();    // make the box empty (poor name)
00114 
00115     /**\brief Make canonical.
00116      * \details
00117      * First point low in all dimensions, 2nd high in all dimensions
00118      */
00119     void    canonize(); 
00120 
00121     /**\brief Return Hilbert value
00122      */
00123     int hvalue(const nbox_t& universe, int level=0) const; // hilbert value
00124     /**\brief Comparison function for Hilbert values.
00125      * \details
00126      * For use with bulk load.
00127      */
00128     int hcmp(const nbox_t& other, const nbox_t& universe, 
00129             int level=0) const; // hilbert value comparison
00130 
00131     void print(ostream &, int level) const;
00132     void draw(int level, ostream &DrawFile, const nbox_t& CoverAll) const;
00133 
00134     /**\brief Area of a box
00135      * Return value:
00136      *    >0 : valid box
00137      *    =0 : a point
00138      *    <0 : empty box 
00139      */
00140     double area() const;
00141 
00142     /**\brief Margin of a Rectangle
00143     */
00144     int margin();
00145 
00146     /*!
00147     * Some binary operations:
00148     *    ^: intersection  ->  box
00149     *    +: bounding box  ->  box (result of addition)
00150     *    +=: enlarge by adding the new box  
00151     *    ==: exact match  ->  boolean
00152     *    /: containment   ->  boolean
00153     *    ||: overlap     ->  boolean
00154     *    >: bigger (comapre low values) -> boolean
00155     *    <: smaller (comapre low values) -> boolean
00156     *    *: square of distance between centers of two boxes 
00157     */
00158     nbox_t operator^(const nbox_t& other) const;
00159     nbox_t operator+(const nbox_t& other) const;
00160 
00161     nbox_t& operator+=(const nbox_t& other);
00162     nbox_t& operator=(const nbox_t& other);
00163     bool operator==(const nbox_t& other) const;
00164     bool operator/(const nbox_t& other) const;
00165     bool contains(const nbox_t& other) const { return *this / other; }
00166     bool operator||(const nbox_t& other) const;
00167     bool operator>(const nbox_t& other) const;
00168     bool operator<(const nbox_t& other) const;
00169     double operator*(const nbox_t& other) const;
00170 
00171     /**\brief For Tcl/smsh use only */
00172     nbox_t(const char* s);        // for conversion from ascii for tcl
00173     /**\brief For Tcl/smsh use only */
00174     operator char*();    // conversion to ascii for tcl
00175     /**\brief For Tcl/smsh use only */
00176     void put(const char*);  // conversion from ascii for tcl
00177 
00178     /**\brief conversion from key to box */
00179     void  bytes2box(const char* key, int klen);
00180     /**\brief conversion from box to key */
00181     const void* kval() const { return (void *) array; }
00182     /**\brief conversion from box to key */
00183     int   klen() const { return 2*sizeof(int)*dim; }
00184 
00185 };
00186 
00187 inline nbox_t& nbox_t::operator=(const nbox_t &r)
00188 {
00189     if (this != &r) {
00190         int i;
00191         dim = r.dim;
00192         for (i = 0; i < dim*2; i++)
00193             array[i] = r.array[i];
00194     }
00195     return *this;
00196 }
00197 
00198 /*<std-footer incl-file-exclusion='NBOX_H'>  -- do not edit anything below this line -- */
00199 
00200 #endif          /*</std-footer>*/

Generated on Wed Jul 7 17:22:32 2010 for Shore Storage Manager by  doxygen 1.4.7