SpikeStream Library  0.2
Box.cpp
Go to the documentation of this file.
00001 //SpikeStream includes
00002 #include "Box.h"
00003 #include "SpikeStreamException.h"
00004 using namespace spikestream;
00005 
00006 
00008 Box::Box(){
00009     x1 = 0;
00010     y1 = 0;
00011     z1 = 0;
00012     x2 = 0;
00013     y2 = 0;
00014     z2 = 0;
00015 }
00016 
00017 
00019 Box::Box(float x1, float y1, float z1, float x2, float y2, float z2){
00020     this->x1 = x1;
00021     this->y1 = y1;
00022     this->z1 = z1;
00023     this->x2 = x2;
00024     this->y2 = y2;
00025     this->z2 = z2;
00026 }
00027 
00028 
00030 Box::Box(const Box& box){
00031     this->x1 = box.x1;
00032     this->y1 = box.y1;
00033     this->z1 = box.z1;
00034     this->x2 = box.x2;
00035     this->y2 = box.y2;
00036     this->z2 = box.z2;
00037 }
00038 
00039 
00041 Box::~Box(){
00042 }
00043 
00044 
00045 /*----------------------------------------------------------*/
00046 /*-----                 PUBLIC METHODS                 -----*/
00047 /*----------------------------------------------------------*/
00048 
00050 Box& Box::operator=(const Box& rhs){
00051         if(this == &rhs)
00052                 return *this;
00053 
00054         this->x1 = rhs.x1;
00055         this->y1 = rhs.y1;
00056         this->z1 = rhs.z1;
00057         this->x2 = rhs.x2;
00058         this->y2 = rhs.y2;
00059         this->z2 = rhs.z2;
00060 
00061         return *this;
00062 }
00063 
00064 
00066 bool Box::operator==(const Box& rhs){
00067         if(this == &rhs)//Same object
00068                 return true;
00069 
00070         if(this->x1 == rhs.x1 && this->y1 == rhs.y1 && this->z1 == rhs.z1 &&
00071                 this->x2 == rhs.x2 && this->y2 == rhs.y2 && this->z2 == rhs.z2)
00072                 return true;
00073 
00074         return false;
00075 }
00076 
00077 
00079 Point3D Box::centre() const{
00080         Point3D point(
00081                         0.5*(x1 + x2),
00082                         0.5*(y1 + y2),
00083                         0.5*(z1 + z2)
00084         );
00085         return point;
00086 }
00087 
00088 
00091 bool Box::contains(const Point3D& point) const{
00092         if(point.getXPos() < x1 || point.getYPos() < y1 || point.getZPos() < z1
00093                                 || point.getXPos() > x2 || point.getYPos() > y2 || point.getZPos() > z2)
00094                 return false;
00095         return true;
00096 }
00097 
00098 
00100 void Box::expand_percent(float percent){
00101     //Check input
00102     if(percent < 0.0f)
00103         throw SpikeStreamException("Method does not support expansion by a negative number.");
00104 
00105     float xExpansion = (1.0f + percent / 100.0f) * (x2-x1) - (x2-x1);
00106     x1 -= xExpansion / 2.0f;
00107     x2 += xExpansion / 2.0f;
00108 
00109     float yExpansion = (1.0f + percent / 100.0f) * (y2-y1) - (y2-y1);
00110     y1 -= yExpansion / 2.0f;
00111     y2 += yExpansion / 2.0f;
00112 
00113     float zExpansion = (1.0f + percent / 100.0f) * (z2-z1) - (z2-z1);
00114     z1 -= zExpansion / 2.0f;
00115     z2 += zExpansion / 2.0f;
00116 }
00117 
00118 
00120 Box Box::getEnclosingBox(const QList<Box>& boxList){
00121         Box newBox;
00122         bool firstTime = true;
00123         for(int i=0; i<boxList.size(); ++i){
00124                 const Box& tmpBox = boxList.at(i);
00125                 if(firstTime){
00126                         newBox = tmpBox;
00127                         firstTime = false;
00128                 }
00129                 else{
00130                         if(tmpBox.x1 < newBox.x1)
00131                                 newBox.x1 = tmpBox.x1;
00132                         if(tmpBox.y1 < newBox.y1)
00133                                 newBox.y1 = tmpBox.y1;
00134                         if(tmpBox.z1 < newBox.z1)
00135                                 newBox.z1 = tmpBox.z1;
00136 
00137                         if(tmpBox.x2 > newBox.x2)
00138                                 newBox.x2 = tmpBox.x2;
00139                         if(tmpBox.y2 > newBox.y2)
00140                                 newBox.y2 = tmpBox.y2;
00141                         if(tmpBox.z2 > newBox.z2)
00142                                 newBox.z2 = tmpBox.z2;
00143                 }
00144         }
00145         return newBox;
00146 }
00147 
00148 
00150 Box Box::getEnclosingBox(const QList<Point3D>& pointList){
00151         Box newBox;
00152         bool firstTime = true;
00153         for(int i=0; i<pointList.size(); ++i){
00154                 const Point3D& tmpPnt = pointList.at(i);
00155                 if(firstTime){
00156                         newBox.setCoordinates(tmpPnt.getXPos(), tmpPnt.getYPos(), tmpPnt.getZPos(),
00157                                                                   tmpPnt.getXPos(), tmpPnt.getYPos(), tmpPnt.getZPos());
00158                         firstTime = false;
00159                 }
00160                 else{
00161                         if(tmpPnt.getXPos() < newBox.x1)
00162                                 newBox.x1 = tmpPnt.getXPos();
00163                         if(tmpPnt.getYPos() < newBox.y1)
00164                                 newBox.y1 = tmpPnt.getYPos();
00165                         if(tmpPnt.getZPos() < newBox.z1)
00166                                 newBox.z1 = tmpPnt.getZPos();
00167 
00168                         if(tmpPnt.getXPos() > newBox.x2)
00169                                 newBox.x2 = tmpPnt.getXPos();
00170                         if(tmpPnt.getYPos() > newBox.y2)
00171                                 newBox.y2 = tmpPnt.getYPos();
00172                         if(tmpPnt.getZPos() > newBox.z2)
00173                                 newBox.z2 = tmpPnt.getZPos();
00174                 }
00175         }
00176         return newBox;
00177 }
00178 
00179 
00181 Box Box::getEnclosingBox(const QList<Box> &boxList, const QList<Point3D> &pointList){
00182         Box newBox;
00183         QList<Box> tmpBoxList = boxList;//Overcome constant constraint
00184         if(!pointList.isEmpty()){
00185                 newBox = getEnclosingBox(pointList);
00186                 tmpBoxList.append(newBox);
00187         }
00188         if(!tmpBoxList.isEmpty()){
00189                 newBox = getEnclosingBox(tmpBoxList);
00190         }
00191         return newBox;
00192 }
00193 
00194 
00196 float Box::getWidth() const{
00197         if( (x2-x1) >= 0.0f )
00198                 return x2-x1;
00199         return x1-x2;
00200 }
00201 
00202 
00204 float Box::getLength() const{
00205         if( (y2-y1) >= 0.0f )
00206                 return y2-y1;
00207         return y1-y2;
00208 }
00209 
00210 
00212 float Box::getHeight() const{
00213         if( (z2-z1) >= 0.0f )
00214                 return z2-z1;
00215         return z1-z2;
00216 }
00217 
00218 
00220 bool Box::intersects(const Box &box) const{
00221         if((box.x1 >= x1 && box.x1 <= x2) || (box.x2 >= x1 && box.x2 <= x2)){//Overlap on X axis
00222                 if((box.y1 >= y1 && box.y1 <= y2) || (box.y2 >= y1 && box.y2 <= y2)){//Overlap on Y axis
00223                         if((box.z1 >= z1 && box.z1 <= z2) || (box.z2 >= z1 && box.z2 <= z2)){//Overlap on Z axis
00224                                 return true;
00225                         }
00226                 }
00227         }
00228         return false;
00229 }
00230 
00231 
00233 void Box::setCoordinates(float x1, float y1, float z1, float x2, float y2, float z2){
00234         this->x1 = x1;
00235         this->x2 = x2;
00236         this->y1 = y1;
00237         this->y2 = y2;
00238         this->z1 = z1;
00239         this->z2 = z2;
00240 }
00241 
00242 
00244 void Box::translate(float dx, float dy, float dz){
00245     x1 += dx;
00246     x2 += dx;
00247     y1 += dy;
00248     y2 += dy;
00249     z1 += dz;
00250     z2 += dz;
00251 }
00252 
00253 
00255 QString Box::toString() const {
00256         QString tmpStr = "Box x1=" + QString::number(x1) + "; y1=" + QString::number(y1) + "; z1=" + QString::number(z1);
00257         tmpStr += "; x2=" + QString::number(x2) + "; y2=" + QString::number(y2) + "; z2=" + QString::number(z2);
00258         return tmpStr;
00259 }
00260 
00261 
 All Classes Files Functions Variables Typedefs Defines