]> Dogcows Code - chaz/yoink/blob - src/Moof/Line.hh
33eecb9b3df6f91dec483b8022011eac8cdda2a4
[chaz/yoink] / src / Moof / Line.hh
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 *******************************************************************************/
28
29 #ifndef _MOOF_LINE_HH_
30 #define _MOOF_LINE_HH_
31
32 #include <Moof/Drawable.hh>
33 #include <Moof/Math.hh>
34 #include <Moof/OpenGL.hh>
35 #include <Moof/Ray.hh>
36 #include <Moof/Texture.hh>
37
38 #include <Moof/Log.hh>
39
40
41 namespace Mf {
42
43
44 template <int D>
45 struct Line : public Drawable, public Shape<D>
46 {
47 typedef cml::vector< Scalar, cml::fixed<D> > Vector;
48
49 Vector a;
50 Vector b;
51
52
53 Line() {}
54
55 Line(const Vector& point1, const Vector& point2) :
56 a(point1),
57 b(point2) {}
58
59 Scalar intersectRay(const Ray<2>& ray,
60 Ray<2>::Intersection& intersection) const
61 {
62 // solve: Cx + r*Dx = Ax + s(Bx - Ax)
63 // Cy + r*Dy = Ay + s(By - Ay)
64 // where: 0 <= s <= 1 if intersection
65 // given: A = a
66 // B = b
67 // C = ray.point
68 // D = ray.direction
69
70 Scalar denom = ray.direction[0] * (b[1] - a[1]) +
71 ray.direction[1] * (a[0] - b[0]);
72
73 // check if the ray and line are parallel
74 if (isEqual(denom, SCALAR(0.0)))
75 {
76 Scalar numer = a[0] * (ray.point[1] - b[1]) +
77 b[0] * (a[1] - ray.point[1]) +
78 ray.point[0] * (b[1] - a[1]);
79
80 // check if they are collinear
81 if (isEqual(numer, SCALAR(0.0)))
82 {
83 intersection.point = ray.point;
84 intersection.normal.set(0.0, 0.0);
85 return SCALAR(0.0);
86 }
87
88 return SCALAR(-1.0);
89 }
90
91 Scalar s = (ray.direction[0] * (ray.point[1] - a[1]) +
92 ray.direction[1] * (a[0] - ray.point[0])) / denom;
93
94 // check if the ray hits the segment
95 if (s < SCALAR(0.0) || s > SCALAR(1.0)) return SCALAR(-1.0);
96
97 Scalar r = -(a[0] * (ray.point[1] - b[1]) +
98 b[0] * (a[1] - ray.point[1]) +
99 ray.point[0] * (b[1] - a[1])) / denom;
100
101 // make sure we're dealing with the right side of the ray
102 if (r < SCALAR(0.0)) return SCALAR(-1.0);
103
104 intersection.point = ray.point + r * ray.direction;
105
106 // gotta use the correct normal
107 Vector n = cml::perp(a - b);
108 if (cml::dot(a - ray.point, n) < 0) intersection.normal = n;
109 else intersection.normal = -n;
110
111 return r;
112 }
113
114 void draw(Scalar alpha = 0.0) const
115 {
116 Mf::Texture::resetBind();
117 glBegin(GL_LINES);
118 glVertex(a);
119 glVertex(b);
120 glEnd();
121 }
122 };
123
124
125 template <int D, int N>
126 struct Polygon : public Shape<D>
127 {
128 typedef cml::vector< Scalar, cml::fixed<D> > Vector;
129
130 Vector points[N];
131 };
132
133
134 } // namespace Mf
135
136 #endif // _MOOF_LINE_HH_
137
138 /** vim: set ts=4 sw=4 tw=80: *************************************************/
139
This page took 0.037712 seconds and 3 git commands to generate.