Debian Codegraph @

src/base/castlevectors.pas

raw
1 {
2 Copyright 2003-2022 Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14 }
15
16 { @abstract(Vector and matrix types and basic geometric operations.)
17
18 Various routines in this unit perform operations
19 on geometric objects, like spheres and line segments.
20 Here's an outline @bold(how do we represent various geometric objects):
21
22 @unorderedList(
23 @item(@italic(Point in 3D) is a TVector3.)
24
25 @item(
26 @italic(Plane in 3D) is a TVector4. Such vector [A, B, C, D]
27 defines a surface that consists of all points satisfying equation
28 @code(A * x + B * y + C * z + D = 0). At least one of A, B, C must be
29 different than zero.
30
31 Vector [A, B, C] is called PlaneDir in many places.
32 It is called PlaneNormal when it's guaranteed (or required to be) normalized,
33 i.e. scaled to have length 1.)
34
35 @item(
36 @italic(Line in 3D space) is represented by two TVector3 values:
37 Line0 and LineVector. They determine a line consisting of all
38 points that can be calculated as @code(Line0 + R * LineVector)
39 where R is any real value.
40
41 LineVector must not be a zero vector.)
42
43 @item(
44 @italic(Line in 2D space) is sometimes represented as two TVector2 values called
45 Line0 and LineVector (analogously like line in 3D).
46
47 And sometimes it's represented as one TVector3 value:
48 [A, B, C], where line consists of all points satisfying @code(A * x + B * y + C = 0).
49 At least one of A, B must be different than zero.)
50
51 @item(
52 A @italic(tunnel) is an object that you get by moving a sphere
53 along the line segment. In other words, this is like a cylinder,
54 but ends with two hemispheres. The tunnel is represented in this
55 unit as two points Tunnel1, Tunnel2 (this defines a line segment)
56 and a TunnelRadius.)
57
58 @item(
59 A @italic(ray) is defined just like a line: two vectors RayOrigin and RayDirection,
60 RayDirection must be nonzero.
61 Ray consists of all points @code(RayOrigin + R * RayDirection)
62 for R being any real value >= 0.)
63
64 @item(
65 An @italic(axis-aligned plane in 3D) is a plane parallel to one of
66 the three basic planes. This is a plane defined by the equation
67 @code(X = Const) or @code(Y = Count) or @code(Z = Const).
68 Such plane is represented as PlaneConstCoord integer value equal
69 to 0, 1 or 2 and PlaneConstValue.
70
71 Note that you can always represent the same plane using a more
72 general plane 3D equation, just take
73
74 @preformatted(
75 Plane[0..2 / PlaneConstCoord] = 0,
76 Plane[PlaneConstCoord] = -1,
77 Plane[3] = PlaneConstValue.
78 )
79 )
80
81 @item(
82 A @italic(line segment) (often referred to as just @italic(segment))
83 is represented by two points Pos1 and Pos2.
84 For some routines the order of points Pos1 and Pos2 is significant
85 (but this is always explicitly stated in the interface, so don't worry).
86
87 Sometimes line segment is also represented as
88 Segment0 and SegmentVector, this consists of all points
89 @code(Segment0 + SegmentVector * t) for t in [0..1].
90 SegmentVector must not be a zero vector.
91
92 Conversion between the two representations above is trivial,
93 just take Pos1 = Segment0 and Pos2 = Segment0 + SegmentVector.)
94 )
95
96 @bold(Requirements of the geometric objects:)
97
98 In descriptions of the geometric objects above, you can see
99 some @italic(requirements), e.g.
100 @italic("the triangle must not be degenerated (equivalent to a line segment)"),
101 @italic("RayDirection must not be a zero vector"), etc.
102 These requirements are generally @italic(not checked)
103 by routines in this unit (for the sake of speed) and @italic(passing
104 wrong values to many of the routines may lead to exceptions)
105 (like an arithmetic exception) or nonsensible results. So try to make sure
106 that the values you pass satisfy the requirements.
107
108 However, @italic(wrong input values should never lead to exceptions
109 that would be a security risk), like access violations or range check errors.
110 As these exceptions could be uncatched in some situations,
111 and then allow accessing a memory that otherwise should not be accessed,
112 so we protect from this.
113 Sometimes you simply cannot guarantee for 100%
114 that input values are correct, because the floating-point operations
115 are inherently not precise.
116
117 @bold(About floating-point precision:)
118
119 @unorderedList(
120 @item(As floating-point operations are never precise,
121 when comparing floating-point values we always use some epsilon
122 to "tolerate" some small differences.
123
124 This epsilon is either done through the standard Math.SameValue and Math.IsZero
125 or by using our own SingleEpsilon and DoubleEpsilon constants
126 (they are equal to standard Math unit epsilon values).
127 The floating-point vector and matrix comparison methods, like
128 @link(CastleVectorsInternalSingle.TGenericVector3.Equals TVector3.Equals) and
129 @link(CastleVectorsInternalSingle.TGenericVector3.IsZero TVector3.IsZero), compare with such epsilon.
130
131 If you really want to compare things precisely,
132 use methods like
133 @link(CastleVectorsInternalSingle.TGenericVector3.PerfectlyEquals TVector3.PerfectlyEquals) or
134 @link(CastleVectorsInternalSingle.TGenericVector3.IsPerfectlyZero TVector3.IsPerfectlyZero).
135 )
136
137 @item(For collision-detecting routines, the general strategy
138 in case of uncertainty (when we're not sure whether there
139 is a collision or the objects are just very very close to each other)
140 is to say that there @italic(is a collision).
141
142 This means that we may detect a collision when in fact the precise
143 mathematical calculation says that there is no collision.
144
145 This approach should be suitable for most use cases.)
146 )
147 }
148 unit CastleVectors;
149
150 {$I castleconf.inc}
151
152 interface
153
154 uses SysUtils, Generics.Collections, Classes, Math,
155 CastleUtils, CastleVectorsInternalSingle, CastleVectorsInternalDouble, CastleClassUtils;
156
157 {$define read_interface}
158
159 {$I castlevectors_float.inc}
160 {$I castlevectors_single.inc}
161 {$I castlevectors_double.inc}
162 {$I castlevectors_byte.inc}
163 {$I castlevectors_integer.inc}
164 {$I castlevectors_cardinal.inc}
165 {$I castlevectors_smallint.inc}
166 {$I castlevectors_lists.inc}
167 {$I castlevectors_lists_double.inc}
168 {$I castlevectors_compatibility_deprecated.inc}
169 {$I castlevectors_miscellaneous.inc}
170 {$I castlevectors_persistent.inc}
171 {$I castlevectors_transformation.inc}
172 {$I castlevectors_border.inc}
173 {$I castlevectors_matrix_decompose.inc}
174
175 {$undef read_interface}
176
177 implementation
178
179 uses
180 CastleStringUtils, CastleColors, CastleLog, CastleQuaternions;
181
182 {$define read_implementation}
183
184 {$I castlevectors_miscellaneous.inc}
185 {$I castlevectors_float.inc}
186 {$I castlevectors_single.inc}
187 {$I castlevectors_double.inc}
188 {$I castlevectors_byte.inc}
189 {$I castlevectors_integer.inc}
190 {$I castlevectors_cardinal.inc}
191 {$I castlevectors_smallint.inc}
192 {$I castlevectors_lists.inc}
193 {$I castlevectors_lists_double.inc}
194 {$I castlevectors_compatibility_deprecated.inc}
195 {$I castlevectors_persistent.inc}
196 {$I castlevectors_transformation.inc}
197 {$I castlevectors_border.inc}
198 {$I castlevectors_matrix_decompose.inc}
199
200 end.