WFMath  0.3.12
rotbox_funcs.h
1 // rotbox_funcs.h (line rotbox implementation)
2 //
3 // The WorldForge Project
4 // Copyright (C) 2000, 2001 The WorldForge Project
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 //
20 // For information about WorldForge and its authors, please contact
21 // the Worldforge Web Site at http://www.worldforge.org.
22 //
23 
24 // Author: Ron Steinke
25 
26 #ifndef WFMATH_ROT_BOX_FUNCS_H
27 #define WFMATH_ROT_BOX_FUNCS_H
28 
29 #include <wfmath/rotbox.h>
30 
31 #include <wfmath/vector.h>
32 #include <wfmath/point.h>
33 #include <wfmath/axisbox.h>
34 #include <wfmath/ball.h>
35 
36 #include <cassert>
37 
38 namespace WFMath {
39 
40 template<int dim>
41 inline RotBox<dim>& RotBox<dim>::operator=(const RotBox<dim>& a)
42 {
43  m_corner0 = a.m_corner0;
44  m_size = a.m_size;
45  m_orient = a.m_orient;
46 
47  return *this;
48 }
49 
50 template<int dim>
51 inline bool RotBox<dim>::isEqualTo(const RotBox<dim>& b, double epsilon) const
52 {
53  return Equal(m_corner0, b.m_corner0, epsilon)
54  && Equal(m_size, b.m_size, epsilon)
55  && Equal(m_orient, b.m_orient, epsilon);
56 }
57 
58 template<int dim>
59 inline Point<dim> RotBox<dim>::getCorner(int i) const
60 {
61  assert(i >= 0 && i < (1 << dim));
62 
63  Vector<dim> dist;
64 
65  if(i == 0)
66  return m_corner0;
67 
68  for(int j = 0; j < dim; ++j)
69  dist[j] = (i & (1 << j)) ? m_size[j] : 0;
70 
71  dist.setValid(m_size.isValid());
72 
73  return m_corner0 + Prod(dist, m_orient);
74 }
75 
76 template<int dim>
77 AxisBox<dim> RotBox<dim>::boundingBox() const
78 {
79  Point<dim> min = m_corner0, max = m_corner0;
80 
81 // for(int i = 0; i < dim; ++i) {
82 // Vector<dim> edge;
83 // edge.zero();
84 // edge[i] = m_size[i];
85 // edge = Prod(edge, m_orient);
86 // // Edge now represents the i'th edge
87 // // pointing away from m_corner0
88 // for(int j = 0; j < dim; ++j) {
89 // if(edge[j] < 0)
90 // min[j] += edge[j];
91 // else
92 // max[j] += edge[j];
93 // }
94 // }
95 
96 // The following is equivalent to the above. The above is easier to understand,
97 // so leave it in as a comment.
98 
99  for(int i = 0; i < dim; ++i) {
100  for(int j = 0; j < dim; ++j) {
101  CoordType value = m_orient.elem(j, i) * m_size[j];
102  if(value < 0)
103  min[i] += value;
104  else
105  max[i] += value;
106  }
107  }
108 
109  bool valid = isValid();
110 
111  min.setValid(valid);
112  max.setValid(valid);
113 
114  return AxisBox<dim>(min, max, true);
115 }
116 
117 // This is here, instead of defined in the class, to
118 // avoid include order problems
119 
120 template<int dim>
121 Point<dim> Point<dim>::toParentCoords(const RotBox<dim>& coords) const
122 {
123  return coords.corner0() + (*this - Point().setToOrigin()) * coords.orientation();
124 }
125 
126 template<int dim>
127 Point<dim> Point<dim>::toLocalCoords(const RotBox<dim>& coords) const
128 {
129  return Point().setToOrigin() + coords.orientation() * (*this - coords.corner0());
130 }
131 
132 } // namespace WFMath
133 
134 #endif // WFMATH_ROT_BOX_FUNCS_H