Debian Codegraph @

src/scene/x3d/x3dnodes_utils_extrusion.inc

raw
1 {%MainUnit x3dnodes.pas}
2 {
3 Copyright 2008-2022 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17
18 type
19 { Utility for dealing with Extrusion.
20
21 Calculating vertices (and other properties) of Extrusion
22 is non-trivial enough to be separated into this file. We want to reuse
23 this by TExtrusionNode.BoundingBox calculation,
24 TExtrusionNode.Triangulate etc. }
25 TVRMLExtrusion = class
26 strict private
27 FHigh: Integer;
28 FNode: TExtrusionNode;
29 FSpineClosed, FCrossSectionClosed: boolean;
30 FCrossSectionOmit: Cardinal;
31 FBeginEndCapsMatching: boolean;
32 procedure SetNode(Value: TExtrusionNode);
33 public
34 constructor Create;
35
36 { Node used. Always assign something non-nil, and generally
37 use rest of this class only when this is assigned. }
38 property Node: TExtrusionNode read FNode write SetNode;
39
40 { You have spines from 0 to High.
41 Remember that this can be -1, if no spine points are defined.
42
43 You can safely ask SpineXxxTransform about various values between
44 0..High. }
45 property High: Integer read FHigh;
46
47 { Same thing as @link(TExtrusionNode.SpineClosed Node.SpineClosed) and
48 @link(TExtrusionNode.CrossSectionClosed Node.CrossSectionClosed),
49 just calculated once (when setting Node) for speed.
50
51 @groupBegin }
52 property SpineClosed: boolean read FSpineClosed;
53 property CrossSectionClosed: boolean read FCrossSectionClosed;
54 { @groupEnd }
55
56 { If we should omit a first (or last) vertex when rendering caps,
57 this is 1, otherwise it is 0.
58 When crossSection is closed, we really should do it,
59 otherwise our concave triangulation may get confused ---
60 see extrusion_concave_cap.wrl testcase. }
61 property CrossSectionOmit: Cardinal read FCrossSectionOmit;
62
63 { Are begin and end caps at the same place.
64
65 This is a stronger condition
66 than just a SpineClosed: whole SpineTransformVar must be
67 guaranteed the same at the beginning and end. The Extrusion rules are such
68 that closed spine -> always produces the same automatic orientation
69 calculated at the beginning and end (X, Y, Z vectors in
70 TVRMLExtrusion.SpineTransformVar implementation). But we also
71 have to compare Orientation and Scale factors --- only when they
72 also match, the caps match. }
73 property BeginEndCapsMatching: boolean read FBeginEndCapsMatching;
74
75 { If Spine > 0, LastY and LastZ must contain what was set here by calling
76 SpineTransformVar(Spine - 1, LastY, LastZ). }
77 procedure SpineTransformVar(Spine: Cardinal;
78 var LastY, LastZ: TVector3;
79 out Transform: TMatrix4);
80
81 procedure SpineScaleVar(Spine: Cardinal;
82 out Scale: TVector2);
83
84 procedure SpineOrientationVar(Spine: Cardinal;
85 out Orientation: TVector4);
86 end;
87
88 constructor TVRMLExtrusion.Create;
89 begin
90 inherited;
91 end;
92
93 procedure TVRMLExtrusion.SetNode(Value: TExtrusionNode);
94 var
95 BeginOrientation, EndOrientation: TVector4;
96 BeginScale, EndScale: TVector2;
97 begin
98 FNode := Value;
99
100 { calculate FHigh }
101
102 if Node.FdSpine.Count = 0 then
103 begin
104 FHigh := -1;
105 { ... and don't even check scale/orientation }
106 end else
107 if (Node.FdScale.Count = 0) or
108 (Node.FdOrientation.Count = 0) then
109 begin
110 WritelnWarning('VRML/X3D', 'Extrusion has no scale or orientation specified');
111 FHigh := -1;
112 end else
113 begin
114 FHigh := Node.FdSpine.Count - 1;
115
116 { We will handle all other scale/orientation counts.
117 Excessive scale/orientation values will be ignored.
118 If not enough will be available then we'll only use the first one
119 (spec says the behavior is undefined then).
120 We know that at least one is available, we checked this above. }
121
122 if (Node.FdScale.Count > 1) and
123 (Node.FdScale.Count < Node.FdSpine.Count) then
124 WritelnWarning('VRML/X3D', 'Extrusion has more scales than 1, but not as much as spines. ' +
125 'We''ll use only the first scale.');
126
127 if (Node.FdOrientation.Count > 1) and
128 (Node.FdOrientation.Count < Node.FdSpine.Count) then
129 WritelnWarning('VRML/X3D', 'Extrusion has more orientations than 1, but not as much as spines. ' +
130 'We''ll use only the first orientation.');
131 end;
132
133 FSpineClosed := Node.SpineClosed;
134 FCrossSectionClosed := Node.CrossSectionClosed;
135
136 if FCrossSectionClosed then
137 FCrossSectionOmit := 1 else
138 FCrossSectionOmit := 0;
139
140 if SpineClosed then
141 begin
142 SpineScaleVar(0, BeginScale);
143 SpineScaleVar(High, EndScale);
144 SpineOrientationVar(0, BeginOrientation);
145 SpineOrientationVar(High, EndOrientation);
146 FBeginEndCapsMatching :=
147 TVector4.PerfectlyEquals(BeginOrientation, EndOrientation) and
148 TVector2.PerfectlyEquals(BeginScale, EndScale);
149 end else
150 FBeginEndCapsMatching := false;
151 end;
152
153 procedure TVRMLExtrusion.SpineScaleVar(Spine: Cardinal;
154 out Scale: TVector2);
155 begin
156 Assert(Between(Spine, 0, High));
157 { So High must be >= 0, by the way.
158 Also, High checked that we have at least one scale. }
159
160 if Node.FdScale.Count < Node.FdSpine.Count then
161 Scale := Node.FdScale.Items.L[0] else
162 Scale := Node.FdScale.Items.L[Spine];
163 end;
164
165 procedure TVRMLExtrusion.SpineOrientationVar(Spine: Cardinal;
166 out Orientation: TVector4);
167 begin
168 Assert(Between(Spine, 0, High));
169 { So High must be >= 0, by the way.
170 Also, High checked that we have at least one orientation. }
171
172 if Node.FdOrientation.Count < Node.FdSpine.Count then
173 Orientation := Node.FdOrientation.Items.L[0] else
174 Orientation := Node.FdOrientation.Items.L[Spine];
175 end;
176
177 procedure TVRMLExtrusion.SpineTransformVar(Spine: Cardinal;
178 var LastY, LastZ: TVector3;
179 out Transform: TMatrix4);
180 var
181 SpinePoints: TVector3List;
182
183 { Calculate Z by searching for the first non-colinear three spine
184 points. @false if not found. }
185 function FindFirstNonColinear(out Z: TVector3): boolean;
186 var
187 I: Integer;
188 begin
189 Result := false;
190
191 for I := 1 to High - 1 do
192 begin
193 { Try to find first spine point with Z defined as non-zero.
194 This follows X3D / VRML spec ("If the Z-axis of the
195 first point is undefined..."). }
196 Z := TVector3.CrossProduct(
197 (SpinePoints.L[I + 1] - SpinePoints.L[I]),
198 (SpinePoints.L[I - 1] - SpinePoints.L[I]));
199 if not Z.IsZero then
200 begin
201 Result := true;
202 break;
203 end;
204 end;
205 end;
206
207 { Calculate Y by searching for the first non-coincident two spine
208 points. Sets to (0, 1, 0) if not found.
209
210 Y is always normalized. }
211 procedure FindFirstNonCoincident(out Y: TVector3);
212 var
213 I: Integer;
214 begin
215 for I := 1 to High do
216 begin
217 Y := (SpinePoints.L[I] - SpinePoints.L[I - 1]);
218 if not Y.IsZero then
219 begin
220 Y := Y.Normalize;
221 Exit;
222 end;
223 end;
224 Y := TVector3.One[1];
225 end;
226
227 { Calculate Z based on Y (already normalized) if all spine points
228 are colinear. }
229 function AllColinear(const Y: TVector3): TVector3;
230 var
231 AngleRad: Single;
232 Rotation, ForPositiveAngleRad, ForNegativeAngleRad: TVector3;
233 begin
234 { Spec: "If the entire spine is collinear,
235 the SCP is computed by finding the rotation of a
236 vector along the positive Y-axis (v1) to the vector
237 formed by the spine points (v2). The Y=0 plane is then rotated
238 by this value."
239
240 v2 is actually just our Y variable. }
241 AngleRad := AngleRadBetweenNormals(TVector3.One[1], Y);
242 Rotation := TVector3.CrossProduct(TVector3.One[1], Y);
243
244 if Rotation.IsZero then
245 begin
246 { This means that Y is actually just equal (0, 1, 0).
247 So Z is just (0, 0, 1). }
248 Result := TVector3.One[2];
249 end else
250 begin
251 { Is the rotation by AngleRad or -AngleRad?
252 Lame way of checking this below, we just check which result
253 produces back Y when rotated.
254
255 Note: the first implementation was like
256
257 if not TVector3.Equals(ForPositiveAngleRad, Y) then
258 begin
259 AngleRad := -AngleRad;
260 Assert(TVector3.Equals(ForNegativeAngleRad, Y));
261 end;
262
263 but this is obviously unsafe because of floating point errors,
264 there was always a chance that both TVector3.Equals fail.
265 Fixed below to just choose the best one. }
266 ForPositiveAngleRad := RotatePointAroundAxisRad( AngleRad, TVector3.One[1], Rotation);
267 ForNegativeAngleRad := RotatePointAroundAxisRad(-AngleRad, TVector3.One[1], Rotation);
268
269 if PointsDistanceSqr(ForPositiveAngleRad, Y) >
270 PointsDistanceSqr(ForNegativeAngleRad, Y) then
271 AngleRad := -AngleRad;
272
273 Result := RotatePointAroundAxisRad(AngleRad, TVector3.One[2], Rotation);
274 end;
275 end;
276
277 procedure CalculateYZForClosed(out Y, Z: TVector3);
278 begin
279 { Same for Spine = 0 and High, as this is the same point actually. }
280 Y := SpinePoints.L[1] - SpinePoints.L[High - 1];
281
282 if not Y.IsZero then
283 Y := Y.Normalize else
284 FindFirstNonCoincident(Y);
285
286 Z := TVector3.CrossProduct(
287 (SpinePoints.L[1] - SpinePoints.L[0]),
288 (SpinePoints.L[High - 1] - SpinePoints.L[0]));
289
290 if not Z.IsZero then
291 Z := Z.Normalize else
292 if FindFirstNonColinear(Z) then
293 Z := Z.Normalize else
294 Z := AllColinear(Y);
295 end;
296
297 var
298 X, Y, Z: TVector3;
299 Scale: TVector2;
300 Orientation: TVector4;
301 begin
302 Assert(Between(Spine, 0, High));
303
304 SpinePoints := Node.FdSpine.Items;
305
306 if High = 0 then
307 begin
308 Y := TVector3.One[1];
309 Z := TVector3.One[2];
310 end else
311 if Spine = 0 then
312 begin
313 if SpineClosed then
314 CalculateYZForClosed(Y, Z) else
315 begin
316 Y := (SpinePoints.L[1] - SpinePoints.L[0]);
317 if not Y.IsZero then
318 Y := Y.Normalize else
319 FindFirstNonCoincident(Y);
320
321 if FindFirstNonColinear(Z) then
322 Z := Z.Normalize else
323 Z := AllColinear(Y);
324 end;
325 end else
326 if Integer(Spine) = High then
327 begin
328 if SpineClosed then
329 CalculateYZForClosed(Y, Z) else
330 begin
331 Y := (SpinePoints.L[High] - SpinePoints.L[High - 1]);
332 if not Y.IsZero then
333 Y := Y.Normalize else
334 Y := LastY;
335
336 Z := LastZ;
337 end;
338 end else
339 begin
340 { Note that I avoided wasting time on checking SpineClosed
341 in this case, and that's good. This is the most common case
342 for this routine. }
343
344 Y := (SpinePoints.L[Spine + 1] - SpinePoints.L[Spine - 1]);
345
346 if not Y.IsZero then
347 Y := Y.Normalize else
348 Y := LastY;
349
350 Z := TVector3.CrossProduct(
351 (SpinePoints.L[Spine + 1] - SpinePoints.L[Spine]),
352 (SpinePoints.L[Spine - 1] - SpinePoints.L[Spine]));
353
354 if not Z.IsZero then
355 Z := Z.Normalize else
356 Z := LastZ;
357 end;
358
359 if (Spine > 0) and (TVector3.DotProduct(LastZ, Z) < 0) then
360 Z := -Z;
361
362 LastY := Y;
363 LastZ := Z;
364
365 X := TVector3.CrossProduct(Y, Z);
366
367 SpineScaleVar(Spine, Scale);
368 SpineOrientationVar(Spine, Orientation);
369
370 Transform :=
371 TransformToCoordsMatrix(SpinePoints.L[Spine], X, Y, Z) *
372 RotationMatrixRad(Orientation[3],
373 Orientation[0], Orientation[1], Orientation[2]) *
374 ScalingMatrix(Vector3(Scale[0], 1, Scale[1]));
375 end;