WFMath  1.0.1
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 Point<dim> RotBox<dim>::getCorner(size_t i) const
42 {
43  assert(i >= 0 && i < (1 << dim));
44 
45  Vector<dim> dist;
46 
47  if(i == 0)
48  return m_corner0;
49 
50  for(int j = 0; j < dim; ++j)
51  dist[j] = (i & (1 << j)) ? m_size[j] : 0;
52 
53  dist.setValid(m_size.isValid());
54 
55  return m_corner0 + Prod(dist, m_orient);
56 }
57 
58 template<int dim>
59 AxisBox<dim> RotBox<dim>::boundingBox() const
60 {
61  Point<dim> min = m_corner0, max = m_corner0;
62 
63 // for(int i = 0; i < dim; ++i) {
64 // Vector<dim> edge;
65 // edge.zero();
66 // edge[i] = m_size[i];
67 // edge = Prod(edge, m_orient);
68 // // Edge now represents the i'th edge
69 // // pointing away from m_corner0
70 // for(int j = 0; j < dim; ++j) {
71 // if(edge[j] < 0)
72 // min[j] += edge[j];
73 // else
74 // max[j] += edge[j];
75 // }
76 // }
77 
78 // The following is equivalent to the above. The above is easier to understand,
79 // so leave it in as a comment.
80 
81  for(int i = 0; i < dim; ++i) {
82  for(int j = 0; j < dim; ++j) {
83  CoordType value = m_orient.elem(j, i) * m_size[j];
84  if(value < 0)
85  min[i] += value;
86  else
87  max[i] += value;
88  }
89  }
90 
91  bool valid = isValid();
92 
93  min.setValid(valid);
94  max.setValid(valid);
95 
96  return AxisBox<dim>(min, max, true);
97 }
98 
99 // This is here, instead of defined in the class, to
100 // avoid include order problems
101 
102 template<int dim>
103 Point<dim> Point<dim>::toParentCoords(const RotBox<dim>& coords) const
104 {
105  return coords.corner0() + (*this - Point().setToOrigin()) * coords.orientation();
106 }
107 
108 template<int dim>
109 Point<dim> Point<dim>::toLocalCoords(const RotBox<dim>& coords) const
110 {
111  return Point().setToOrigin() + coords.orientation() * (*this - coords.corner0());
112 }
113 
114 } // namespace WFMath
115 
116 #endif // WFMATH_ROT_BOX_FUNCS_H