00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
#ifndef WFMATH_SEGMENT_H
00027
#define WFMATH_SEGMENT_H
00028
00029
#include <wfmath/const.h>
00030
#include <wfmath/vector.h>
00031
#include <wfmath/point.h>
00032
#include <wfmath/axisbox.h>
00033
#include <wfmath/rotbox.h>
00034
#include <wfmath/ball.h>
00035
#include <wfmath/intersect_decls.h>
00036
00037
namespace WFMath {
00038
00039
template<const
int dim>
class Segment;
00040
00041
template<const
int dim>
00042 std::ostream& operator<<(std::ostream& os, const Segment<dim>& s);
00043
template<const
int dim>
00044 std::istream& operator>>(std::istream& is, Segment<dim>& s);
00045
00047
00051
template<const
int dim>
00052 class Segment
00053 {
00054
public:
00056 Segment() {}
00058 Segment(
const Point<dim>& p1,
const Point<dim>& p2) : m_p1(p1), m_p2(p2) {}
00060 Segment(
const Segment& s) : m_p1(s.m_p1), m_p2(s.m_p2) {}
00061
00062 ~
Segment() {}
00063
00064
friend std::ostream& operator<< <dim>(std::ostream& os,
const Segment& s);
00065
friend std::istream& operator>> <dim>(std::istream& is, Segment& s);
00066
00067 Segment& operator=(
const Segment& s)
00068 {m_p1 = s.m_p1; m_p2 = s.m_p2;
return *
this;}
00069
00070
bool isEqualTo(
const Segment& s,
double epsilon = WFMATH_EPSILON)
const;
00071
00072
bool operator==(
const Segment& b)
const {
return isEqualTo(b);}
00073
bool operator!=(
const Segment& b)
const {
return !isEqualTo(b);}
00074
00075
bool isValid()
const {
return m_p1.isValid() && m_p2.isValid();}
00076
00077
00078
00079
int numCorners()
const {
return 2;}
00080 Point<dim> getCorner(
int i)
const {assert(i == 0 || i == 1);
return i ? m_p2 : m_p1;}
00081 Point<dim> getCenter()
const {
return Midpoint(m_p1, m_p2);}
00082
00084 const Point<dim>&
endpoint(
const int i)
const {
return i ? m_p2 : m_p1;}
00086 Point<dim>& endpoint(
const int i) {
return i ? m_p2 : m_p1;}
00087
00088
00089
00090
Segment& shift(
const Vector<dim>& v)
00091 {m_p1 += v; m_p2 += v;
return *
this;}
00092 Segment& moveCornerTo(
const Point<dim>& p,
int corner);
00093 Segment& moveCenterTo(
const Point<dim>& p)
00094 {
return shift(p - getCenter());}
00095
00096
Segment& rotateCorner(
const RotMatrix<dim>& m,
int corner);
00097
Segment& rotateCenter(
const RotMatrix<dim>& m)
00098 {rotatePoint(m, getCenter());
return *
this;}
00099 Segment<dim>& rotatePoint(
const RotMatrix<dim>& m,
const Point<dim>& p)
00100 {m_p1.rotate(m, p); m_p2.rotate(m, p);
return *
this;}
00101
00102
00103 Segment<3>& rotateCorner(
const Quaternion& q,
int corner);
00104 Segment<3>& rotateCenter(
const Quaternion& q)
00105 {rotatePoint(q, getCenter());
return *
this;}
00106 Segment<3>& rotatePoint(
const Quaternion& q,
const Point<3>& p)
00107 {m_p1.rotate(q, p); m_p2.rotate(q, p);
return *
this;}
00108
00109
00110
00111 AxisBox<dim> boundingBox()
const {
return AxisBox<dim>(m_p1, m_p2);}
00112 Ball<dim> boundingSphere()
const
00113
{
return Ball<dim>(getCenter(), Distance(m_p1, m_p2) / 2);}
00114 Ball<dim> boundingSphereSloppy()
const
00115
{
return Ball<dim>(getCenter(), SloppyDistance(m_p1, m_p2) / 2);}
00116
00117
Segment toParentCoords(
const Point<dim>& origin,
00118
const RotMatrix<dim>& rotation = RotMatrix<dim>().identity())
const
00119
{
return Segment(m_p1.toParentCoords(origin, rotation),
00120 m_p2.toParentCoords(origin, rotation));}
00121
Segment toParentCoords(
const AxisBox<dim>& coords)
const
00122
{
return Segment(m_p1.toParentCoords(coords), m_p2.toParentCoords(coords));}
00123
Segment toParentCoords(
const RotBox<dim>& coords)
const
00124
{
return Segment(m_p1.toParentCoords(coords), m_p2.toParentCoords(coords));}
00125
00126
00127
00128
00129
00130
Segment toLocalCoords(
const Point<dim>& origin,
00131
const RotMatrix<dim>& rotation = RotMatrix<dim>().identity())
const
00132
{
return Segment(m_p1.toLocalCoords(origin, rotation),
00133 m_p2.toLocalCoords(origin, rotation));}
00134
Segment toLocalCoords(
const AxisBox<dim>& coords)
const
00135
{
return Segment(m_p1.toLocalCoords(coords), m_p2.toLocalCoords(coords));}
00136
Segment toLocalCoords(
const RotBox<dim>& coords)
const
00137
{
return Segment(m_p1.toLocalCoords(coords), m_p2.toLocalCoords(coords));}
00138
00139
00140 Segment<3> toParentCoords(
const Point<3>& origin,
const Quaternion& rotation)
const
00141
{
return Segment<3>(m_p1.toParentCoords(origin, rotation),
00142 m_p2.toParentCoords(origin, rotation));}
00143 Segment<3> toLocalCoords(
const Point<3>& origin,
const Quaternion& rotation)
const
00144
{
return Segment<3>(m_p1.toLocalCoords(origin, rotation),
00145 m_p2.toLocalCoords(origin, rotation));}
00146
00147
friend bool Intersect<dim>(
const Segment& s,
const Point<dim>& p,
bool proper);
00148
friend bool Contains<dim>(
const Point<dim>& p,
const Segment& s,
bool proper);
00149
00150
friend bool Intersect<dim>(
const Segment& s,
const AxisBox<dim>& b,
bool proper);
00151
friend bool Contains<dim>(
const AxisBox<dim>& b,
const Segment& s,
bool proper);
00152
00153
friend bool Intersect<dim>(
const Segment& s,
const Ball<dim>& b,
bool proper);
00154
friend bool Contains<dim>(
const Ball<dim>& b,
const Segment& s,
bool proper);
00155
00156
friend bool Intersect<dim>(
const Segment& s1,
const Segment& s2,
bool proper);
00157
friend bool Contains<dim>(
const Segment& s1,
const Segment& s2,
bool proper);
00158
00159
friend bool Intersect<dim>(
const RotBox<dim>& r,
const Segment& s,
bool proper);
00160
friend bool Contains<dim>(
const RotBox<dim>& r,
const Segment& s,
bool proper);
00161
friend bool Contains<dim>(
const Segment& s,
const RotBox<dim>& r,
bool proper);
00162
00163
friend bool Intersect<dim>(
const Polygon<dim>& r,
const Segment& s,
bool proper);
00164
friend bool Contains<dim>(
const Polygon<dim>& p,
const Segment& s,
bool proper);
00165
friend bool Contains<dim>(
const Segment& s,
const Polygon<dim>& p,
bool proper);
00166
00167
private:
00168
00169 Point<dim> m_p1, m_p2;
00170 };
00171
00172 }
00173
00174
#include <wfmath/segment_funcs.h>
00175
00176
#endif // WFMATH_SEGMENT_H