WFMath 0.3.12
|
00001 // rotbox_funcs.h (line rotbox implementation) 00002 // 00003 // The WorldForge Project 00004 // Copyright (C) 2000, 2001 The WorldForge Project 00005 // 00006 // This program is free software; you can redistribute it and/or modify 00007 // it under the terms of the GNU General Public License as published by 00008 // the Free Software Foundation; either version 2 of the License, or 00009 // (at your option) any later version. 00010 // 00011 // This program is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 // 00016 // You should have received a copy of the GNU General Public License 00017 // along with this program; if not, write to the Free Software 00018 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00019 // 00020 // For information about WorldForge and its authors, please contact 00021 // the Worldforge Web Site at http://www.worldforge.org. 00022 // 00023 00024 // Author: Ron Steinke 00025 00026 #ifndef WFMATH_ROT_BOX_FUNCS_H 00027 #define WFMATH_ROT_BOX_FUNCS_H 00028 00029 #include <wfmath/rotbox.h> 00030 00031 #include <wfmath/vector.h> 00032 #include <wfmath/point.h> 00033 #include <wfmath/axisbox.h> 00034 #include <wfmath/ball.h> 00035 00036 #include <cassert> 00037 00038 namespace WFMath { 00039 00040 template<int dim> 00041 inline RotBox<dim>& RotBox<dim>::operator=(const RotBox<dim>& a) 00042 { 00043 m_corner0 = a.m_corner0; 00044 m_size = a.m_size; 00045 m_orient = a.m_orient; 00046 00047 return *this; 00048 } 00049 00050 template<int dim> 00051 inline bool RotBox<dim>::isEqualTo(const RotBox<dim>& b, double epsilon) const 00052 { 00053 return Equal(m_corner0, b.m_corner0, epsilon) 00054 && Equal(m_size, b.m_size, epsilon) 00055 && Equal(m_orient, b.m_orient, epsilon); 00056 } 00057 00058 template<int dim> 00059 inline Point<dim> RotBox<dim>::getCorner(int i) const 00060 { 00061 assert(i >= 0 && i < (1 << dim)); 00062 00063 Vector<dim> dist; 00064 00065 if(i == 0) 00066 return m_corner0; 00067 00068 for(int j = 0; j < dim; ++j) 00069 dist[j] = (i & (1 << j)) ? m_size[j] : 0; 00070 00071 dist.setValid(m_size.isValid()); 00072 00073 return m_corner0 + Prod(dist, m_orient); 00074 } 00075 00076 template<int dim> 00077 AxisBox<dim> RotBox<dim>::boundingBox() const 00078 { 00079 Point<dim> min = m_corner0, max = m_corner0; 00080 00081 // for(int i = 0; i < dim; ++i) { 00082 // Vector<dim> edge; 00083 // edge.zero(); 00084 // edge[i] = m_size[i]; 00085 // edge = Prod(edge, m_orient); 00086 // // Edge now represents the i'th edge 00087 // // pointing away from m_corner0 00088 // for(int j = 0; j < dim; ++j) { 00089 // if(edge[j] < 0) 00090 // min[j] += edge[j]; 00091 // else 00092 // max[j] += edge[j]; 00093 // } 00094 // } 00095 00096 // The following is equivalent to the above. The above is easier to understand, 00097 // so leave it in as a comment. 00098 00099 for(int i = 0; i < dim; ++i) { 00100 for(int j = 0; j < dim; ++j) { 00101 CoordType value = m_orient.elem(j, i) * m_size[j]; 00102 if(value < 0) 00103 min[i] += value; 00104 else 00105 max[i] += value; 00106 } 00107 } 00108 00109 bool valid = isValid(); 00110 00111 min.setValid(valid); 00112 max.setValid(valid); 00113 00114 return AxisBox<dim>(min, max, true); 00115 } 00116 00117 // This is here, instead of defined in the class, to 00118 // avoid include order problems 00119 00120 template<int dim> 00121 Point<dim> Point<dim>::toParentCoords(const RotBox<dim>& coords) const 00122 { 00123 return coords.corner0() + (*this - Point().setToOrigin()) * coords.orientation(); 00124 } 00125 00126 template<int dim> 00127 Point<dim> Point<dim>::toLocalCoords(const RotBox<dim>& coords) const 00128 { 00129 return Point().setToOrigin() + coords.orientation() * (*this - coords.corner0()); 00130 } 00131 00132 } // namespace WFMath 00133 00134 #endif // WFMATH_ROT_BOX_FUNCS_H