]>
Dogcows Code - chaz/yoink/blob - src/Moof/Line.hh
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
10 **************************************************************************/
12 #ifndef _MOOF_LINE_HH_
13 #define _MOOF_LINE_HH_
15 #include <Moof/Drawable.hh>
16 #include <Moof/Math.hh>
17 #include <Moof/OpenGL.hh>
18 #include <Moof/Ray.hh>
19 #include <Moof/Shape.hh>
20 #include <Moof/Texture.hh>
27 struct Line
: public Drawable
, public Shape
<D
>
29 typedef cml::vector
< Scalar
, cml::fixed
<D
> > Vector
;
37 Line(const Vector
& point1
, const Vector
& point2
) :
41 bool intersectRay(const Ray
<2>& ray
, Ray
<2>::Contact
& hit
) const
43 // solve: Cx + r*Dx = Ax + s(Bx - Ax)
44 // Cy + r*Dy = Ay + s(By - Ay)
45 // where: 0 <= s <= 1 if intersection
51 Scalar denom
= ray
.direction
[0] * (b
[1] - a
[1]) +
52 ray
.direction
[1] * (a
[0] - b
[0]);
54 // check if the ray and line are parallel
55 //if (isEqual(denom, SCALAR(0.0)))
56 if (denom
== SCALAR(0.0))
58 Scalar numer
= a
[0] * (ray
.point
[1] - b
[1]) +
59 b
[0] * (a
[1] - ray
.point
[1]) +
60 ray
.point
[0] * (b
[1] - a
[1]);
62 // check if they are collinear
63 if (isEqual(numer
, SCALAR(0.0)))
65 hit
.distance
= SCALAR(0.0);
66 hit
.normal
.set(0.0, 0.0);
73 Scalar s
= (ray
.direction
[0] * (ray
.point
[1] - a
[1]) +
74 ray
.direction
[1] * (a
[0] - ray
.point
[0])) / denom
;
76 // check if the ray hits the segment
77 if (s
< SCALAR(0.0) || s
> SCALAR(1.0)) return false;
79 hit
.distance
= -(a
[0] * (ray
.point
[1] - b
[1]) +
80 b
[0] * (a
[1] - ray
.point
[1]) +
81 ray
.point
[0] * (b
[1] - a
[1])) / denom
;
83 // check if the intersection is behind the ray
84 if (hit
.distance
< SCALAR(0.0)) return false;
86 Vector normal
= cml::perp(a
- b
);
87 if (cml::dot(a
- ray
.point
, normal
) < 0) hit
.normal
= normal
;
88 else hit
.normal
= -normal
;
92 void draw(Scalar alpha
= 0.0) const
94 Mf::Texture::resetBind();
103 typedef Line
<2> Line2
;
104 typedef Line
<3> Line3
;
107 template <int D
, int N
>
108 struct Polygon
: public Drawable
, public Shape
<D
>
110 typedef cml::vector
< Scalar
, cml::fixed
<D
> > Vector
;
116 bool intersectRay(const Ray
<D
>& ray
, typename Ray
<D
>::Contact
& hit
)
121 void draw(Scalar alpha
= 0.0) const
123 Mf::Texture::resetBind();
125 for (int i
= 0; i
< D
; ++i
)
134 typedef Polygon
<2,3> Triangle2
;
135 typedef Polygon
<3,3> Triangle3
;
140 #endif // _MOOF_LINE_HH_
This page took 0.037885 seconds and 4 git commands to generate.