File: geometry.cpp

package info (click to toggle)
k3d 0.4.3.0-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 51,716 kB
  • ctags: 81,610
  • sloc: cpp: 283,698; ansic: 64,095; xml: 61,533; sh: 9,026; makefile: 5,282; python: 431; perl: 308; awk: 130
file content (358 lines) | stat: -rw-r--r-- 9,481 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// K-3D
// Copyright (c) 1995-2004, Timothy M. Shead
//
// Contact: tshead@k-3d.com
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

/** \file
		\brief 2D geometry routines implementation
		\author Tim Shead (tshead@k-3d.com)
*/

#include "basic_math.h"
#include "geometry.h"

namespace k3d
{

double Area(const k3d::vector2& Point1, const k3d::vector2& Point2, const k3d::vector2& Point3)
{
	double result = 0;

	result += (Point1[0] * Point2[1]) - (Point2[0] * Point1[1]);
	result += (Point2[0] * Point3[1]) - (Point3[0] * Point2[1]);
	result += (Point3[0] * Point1[1]) - (Point1[0] * Point3[1]);

	// For robustness ...
	//if(std::fabs(result) < FLT_EPSILON)
	//	return 0;

	return result * 0.5;
}

/////////////////////////////////////////////////////////////////////////////
// rectangle implementation

rectangle::rectangle()
{ left = right = top = bottom = 0.0; }

rectangle::rectangle(const double Left, const double Right, const double Top, const double Bottom)
{ left = Left; right = Right; top = Top; bottom = Bottom; }

rectangle::rectangle(const rectangle& v)
{ left = v.left; right = v.right; top = v.top; bottom = v.bottom; }

rectangle& rectangle::operator = (const rectangle& v)
{
	left = v.left; right = v.right; top = v.top; bottom = v.bottom;
	return *this;
}

double rectangle::Left() const { return left; }
double rectangle::Right() const { return right; }
double rectangle::Top() const { return top; }
double rectangle::Bottom() const { return bottom; }
double rectangle::Width() const { return std::fabs(right - left); }
double rectangle::Height() const { return std::fabs(bottom - top); }

void rectangle::Offset(const double Horizontal, const double Vertical)
{
	left += Horizontal;
	right += Horizontal;
	top += Vertical;
	bottom += Vertical;
}

void rectangle::Grow(const double Thickness)
{
	Grow(Thickness, Thickness);
}

void rectangle::Grow(const double Horizontal, const double Vertical)
{
	left -= Horizontal;
	right += Horizontal;
	top -= Vertical;
	bottom += Vertical;
}

void rectangle::Shrink(const double Thickness)
{
	Shrink(Thickness, Thickness);
}

void rectangle::Shrink(const double Horizontal, const double Vertical)
{
	Grow(-Horizontal, -Vertical);
}

void rectangle::Normalize()
{
	if(left > right)
		std::swap(left, right);

	if(top > bottom)
		std::swap(top, bottom);
}

vector2 rectangle::Center() const
{
	return vector2(left+right, top+bottom) * 0.5;
}

bool rectangle::Contains(const vector2& Point) const
{
	return (left <= Point[0] && Point[0] <= right && top <= Point[1] && Point[1] <= bottom) ? true : false;
}

rectangle ConstrainProportions(const rectangle& Source, const rectangle& Destination)
{
	rectangle source(Source);
	rectangle destination(Destination);

	source.Normalize();
	destination.Normalize();

	source.Offset(-source.Left(), -source.Top());

	if(source.Width() != destination.Width())
	{
		source.bottom *= destination.Width() / source.Width();
		source.right *= destination.Width() / source.Width();
	}

	if(source.Height() > destination.Height())
	{
		source.right *= destination.Height() / source.Height();
		source.bottom *= destination.Height() / source.Height();
	}

	source.Offset(destination.Left() + (0.5 * (destination.Width() - source.Width())),
			destination.Top() + (0.5 * (destination.Height() - source.Height())));

	return source;
}

std::ostream& operator<<(std::ostream& Stream, const rectangle& Arg)
{
	Stream << Arg.Left() << " " << Arg.Right() << " " << Arg.Top() << " " << Arg.Bottom();
	return Stream;
}

std::istream& operator>>(std::istream& Stream, rectangle& Arg)
{
	Stream >> Arg.left >> Arg.right >> Arg.top >> Arg.bottom;
	return Stream;
}

/////////////////////////////////////////////////////////////////////////////
// plane implementation

plane::plane(const vector3& Normal, const double Distance)
{
	m_Normal = Normal;
	m_Normal.Normalize();

	m_Distance = Distance;
}

plane::plane(const vector3& Normal, const vector3& ContainedPoint)
{
	m_Normal = Normal;
	m_Normal.Normalize();

	m_Distance = -ContainedPoint * m_Normal;
}

plane::plane(const vector3& ContainedPointA, const vector3& ContainedPointB, const vector3& ContainedPointC)
{
	m_Normal = ((ContainedPointA - ContainedPointB) ^ (ContainedPointC - ContainedPointB)).Normalize();
	m_Distance = -ContainedPointA * m_Normal;
}

vector3 plane::Normal() const
{
	return m_Normal;
}

double plane::Distance() const
{
	return m_Distance;
}

void plane::SetNormal(const vector3& Normal)
{
	m_Normal = Normal;
}

void plane::SetDistance(const double Distance)
{
	m_Distance = Distance;
}

std::ostream& operator<<(std::ostream& Stream, const plane& Arg)
{
	Stream << Arg.Normal() << " " << Arg.Distance();
	return Stream;
}

std::istream& operator>>(std::istream& Stream, plane& Arg)
{
	Stream >> Arg.m_Normal >> Arg.m_Distance;
	return Stream;
}

///
bool PlaneLineIntersection(const plane& Plane, const k3d::vector3 LineOrigin, const k3d::vector3 LineDirection, k3d::vector3& Result)
{
	// Calculate the angle (dot product) between line and plane ...
	const double vd = Plane.Normal() * LineDirection;

	// Make sure the line & plane aren't parallel ...
	if(0.0 == vd)
		return false;

	const double v0 = -(Plane.Normal() * LineOrigin + Plane.Distance());
	const double t = v0 / vd;

	Result = LineOrigin + (t * LineDirection);

	return true;
}

bool LineIntersection(const k3d::vector2& A1, const k3d::vector2& A2, const k3d::vector2& B1, const k3d::vector2& B2, k3d::vector2& Result)
{
	const k3d::vector2 a = (A2 - A1).Normalize();
	const k3d::vector2 b = (B2 - B1).Normalize();
	const k3d::vector2 c = B1 - A1;
	const k3d::vector2 bperp = b.Perpendicular();

	const double denominator = bperp * a;
	if(0 == denominator)
		return false;

	const double t = (bperp * c) / denominator;

	Result = A1 + (t * a);

	return true;
}

line_segment_intersection_type LineSegmentIntersection(const k3d::vector2& A1, const k3d::vector2& A2, const k3d::vector2& B1, const k3d::vector2& B2)
{
	const double sign1 = k3d::sign(k3d::Area(A1, A2, B1));
	const double sign2 = k3d::sign(k3d::Area(A1, A2, B2));

	// The three most likely possibilities ...
	if(sign1 && sign2)
	{
		// No intersection ...
		if(sign1 == sign2)
			return SDP_NOINTERSECTION;

		k3d::vector2 intersection;
		if(!LineIntersection(A1, A2, B1, B2, intersection))
			{
				std::cerr << __PRETTY_FUNCTION__ << " : LineIntersection 1 failed." << std::endl;
				return SDP_ERROR;
			}

		const double sign3 = k3d::sign(k3d::Area(B1, intersection, A1));
		const double sign4 = k3d::sign(k3d::Area(B1, intersection, A2));

		if(sign3 != sign4)
			return SDP_INTERIORINTERSECTION;

		return SDP_EXTERIORINTERSECTION;
	}
	// Two variations with single-point intersections ...
	else if(sign1 || sign2)
	{
		k3d::vector2 intersection;
		if(!LineIntersection(A1, A2, B1, B2, intersection))
			{
				std::cerr << __PRETTY_FUNCTION__ << " : LineIntersection 2 failed." << std::endl;
				return SDP_ERROR;
			}

		const double sign3 = k3d::sign(k3d::Area(B1, intersection, A1));
		const double sign4 = k3d::sign(k3d::Area(B1, intersection, A2));

		if(sign3 != sign4)
			return SDP_INTERIORCOLINEARPOINT;

		return SDP_EXTERIORCOLINEARPOINT;
	}
	// The four colinear possibilities ...
	else
	{
		// This ray is getting crowded!  Find something somewhere else
		k3d::vector2 start = B1 + (B2 - B1).Perpendicular();

		const double sign3 = k3d::sign(k3d::Area(start, B1, A1));
		const double sign4 = k3d::sign(k3d::Area(start, B1, A2));
		const double sign5 = k3d::sign(k3d::Area(start, B2, A1));
		const double sign6 = k3d::sign(k3d::Area(start, B2, A2));

		if((sign3 != sign4) && (sign5 != sign6))
			return SDP_INTERIORCOLINEARPOINTS;

		if((sign3 == sign4) && (sign4 == sign5) && (sign5 == sign6))
			return SDP_EXTERIORCOLINEARPOINTSNOINTERSECTION;

		if((sign3 == sign4) && (sign4 != sign5) && (sign5 == sign6))
			return SDP_EXTERIORCOLINEARPOINTS;

		return SDP_INTERIOREXTERIORCOLINEARPOINTS;
	}
}

double BernsteinBasis(const unsigned long Order, const unsigned long ControlPoint, const double Parameter)
{
	// Sanity checks ...
	assert(Order > 1);
	assert(ControlPoint <= Order);

	const unsigned long n = Order - 1;
	const unsigned long i = ControlPoint;
	const double t = Parameter;

	const double ni = factorial(n) / (factorial(i) * factorial(n - i));

	return ni * pow(t, i) * pow((1 - t), (n - i));
}

bool intersect_lines(const k3d::vector3& P1, const k3d::vector3& T1, const k3d::vector3& P2, const k3d::vector3& T2, k3d::vector3& Result)
{
	// Code originally from Aqsis, http://www.aqsis.com

    k3d::vector3	v, px;

    px = T1 ^ ( P1 - T2 );
    v = px ^ T1;

    double	t = ( P1 - P2 ) * v;
    double vw = v * T2;
    if ( ( vw * vw ) < 1.0e-07 )
        return false;
    t /= vw;
    Result = P2 + ( ( ( P1 - P2 ) * v ) / vw ) * T2 ;
    return true;
}

} // namespace k3d