Debian Codegraph @

src/transform/castleinternalnurbs.pas

raw
1 {
2 Copyright 2009-2023 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 { Common utilities for NURBS curves and surfaces. }
17 unit CastleInternalNurbs;
18
19 {$I castleconf.inc}
20
21 interface
22
23 uses SysUtils, CastleUtils, CastleVectors, CastleBoxes;
24
25 type
26 TDoubleArray = array of Double;
27
28 { Calculate NURBS basis functions. }
29 TNurbsBasisCalculator = class
30 strict private
31 { Remember the curve parameters passed to constructor. }
32 ControlPointCount: Integer;
33 Order: Cardinal;
34 Knot: TDoubleList;
35 { Ready allocated structures, to speedup repeated calls to GetBasis.
36 Their contents are overridden by each GetBasis call. }
37 Basis, Left, Right: TDoubleArray;
38 { Speedup FindKnotSpan by first trying out last result.
39 This optimizes a very often case, when we're repeatedly calling
40 GetBasis with a very similar U value (that falls within the same interval).
41 This typically happens when calculating a series of points on the NURBS
42 curve or surface. }
43 LastFindKnotSpanResult: Integer;
44
45 { In which knot interval is the given point.
46 The result is guaranteed to be in [Order - 1...ControlPointCount - 1] range. }
47 function FindKnotSpan(const U: Double): Integer;
48 public
49 constructor Create(const AControlPointCount: Integer;
50 const AOrder: Cardinal; const AKnot: TDoubleList);
51
52 { Calculate the basis functions (N) values for U.
53 By the way, calculate also the interval in Knot in which U lies. }
54 function GetBasis(const U: Double; out KnotInterval: Integer): TDoubleArray;
55 end;
56
57 { Calculate point on a NURBS curve. }
58 TNurbsCurveCalculator = class
59 strict private
60 { Remember the curve parameters passed to constructor. }
61 Points: TVector3List;
62 Order: Cardinal;
63 Knot, Weight: TDoubleList;
64
65 BasisCalculator: TNurbsBasisCalculator;
66 public
67 constructor Create(const APoints: TVector3List;
68 const AOrder: Cardinal; const AKnot, AWeight: TDoubleList);
69 destructor Destroy; override;
70
71 { Calculate point on a NURBS curve. Optimized for repeated calls,
72 to calculate points on the same curve.
73 See NurbsCurvePoint for detailed documentation. }
74 function GetPoint(const U:Single; const Tangent: PVector3): TVector3;
75 end;
76
77 { Calculate point on a NURBS surface. }
78 TNurbsSurfaceCalculator = class
79 strict private
80 { Remember the curve parameters passed to constructor. }
81 Points: TVector3List;
82 UDimension, VDimension: Cardinal;
83 UOrder, VOrder: Cardinal;
84 UKnot, VKnot, Weight: TDoubleList;
85
86 UBasisCalculator, VBasisCalculator: TNurbsBasisCalculator;
87 public
88 constructor Create(const APoints: TVector3List;
89 const AUDimension, AVDimension: Cardinal;
90 const AUOrder, AVOrder: Cardinal;
91 const AUKnot, AVKnot, AWeight: TDoubleList);
92 destructor Destroy; override;
93
94 { Calculate point on a NURBS surface. Optimized for repeated calls,
95 to calculate points on the same surface.
96 See NurbsSurfacePoint for detailed documentation. }
97 function GetPoint(const U, V: Single; const Normal: PVector3): TVector3;
98 end;
99
100 { Calculate the tessellation (number of NURBS points generated).
101 This follows X3D spec for "an implementation subdividing
102 the surface into an equal number of subdivision steps".
103 Give value of tessellation field, and count of controlPoints.
104
105 Returned value is for sure > 0 (never exactly 0). }
106 function ActualTessellation(const Tessellation: Integer;
107 const Dimension: Cardinal): Cardinal;
108
109 { Return point on NURBS curve.
110
111 Requires:
112 @unorderedList(
113 @item Points.Count > 0 (not exactly 0).
114 @item Order >= 2 (X3D and VRML 97 spec require this too).
115 @item Knot must have exactly PointsCount + Order items.
116 @item U is between Knot.First and Knot.Last.
117 )
118
119 Weight will be used only if it has the same length as Points.Count.
120 Otherwise, weight = 1.0 (that is, defining non-rational curve) will be used
121 (this follows X3D spec).
122
123 Tangent, if non-nil, will be set to the direction at given point of the
124 curve, pointing from the smaller to larger knot values.
125 It will be normalized. This can be directly useful to generate
126 orientations by X3D NurbsOrientationInterpolator node. }
127 function NurbsCurvePoint(const Points: TVector3List;
128 const U: Single;
129 const Order: Cardinal;
130 const Knot, Weight: TDoubleList;
131 const Tangent: PVector3): TVector3;
132
133 { Return point on NURBS surface.
134
135 Requires:
136 @unorderedList(
137 @item UDimension, VDimension > 0 (not exactly 0).
138 @item Points.Count must match UDimension * VDimension.
139 @item Order >= 2 (X3D and VRML 97 spec require this too).
140 @item Each xKnot must have exactly xDimension + Order items.
141 )
142
143 Weight will be used only if it has the same length as UDimension * VDimension.
144 Otherwise, weight = 1.0 (that is, defining non-rational curve) will be used
145 (this follows X3D spec).
146
147 Normal, if non-nil, will be set to the normal at given point of the
148 surface. It will be normalized. You can use this to pass these normals
149 to rendering. Or to generate normals for X3D NurbsSurfaceInterpolator node. }
150 function NurbsSurfacePoint(const Points: TVector3List;
151 const UDimension, VDimension: Cardinal;
152 const U, V: Single;
153 const UOrder, VOrder: Cardinal;
154 const UKnot, VKnot, Weight: TDoubleList;
155 const Normal: PVector3): TVector3;
156
157 type
158 EInvalidPiecewiseBezierCount = class(Exception);
159
160 { Naming notes: what precisely is called a "uniform" knot vector seems
161 to differ in literature / software.
162 Blender calls nkPeriodicUniform as "Uniform",
163 and nkEndpointUniform as "Endpoint".
164 http://en.wiki.mcneel.com/default.aspx/McNeel/NURBSDoc.html
165 calls nkEndpointUniform as "Uniform".
166 "An introduction to NURBS: with historical perspective"
167 (by David F. Rogers) calls nkEndpointUniform "open uniform" and
168 nkPeriodicUniform "periodic uniform". }
169
170 { Type of NURBS knot vector to generate. }
171 TNurbsKnotKind = (
172 { All knot values are evenly spaced, all knots are single.
173 This is good for periodic curves. }
174 nkPeriodicUniform,
175
176 { Starting and ending knots have Order multiplicity, rest is evenly spaced.
177 The curve hits endpoints. }
178 nkEndpointUniform,
179
180 { NURBS curve will effectively become a piecewise Bezier curve.
181 The order of NURBS curve will determine the order of Bezier curve,
182 for example NURBS curve with order = 4 results in a cubic Bezier curve. }
183 nkPiecewiseBezier);
184
185 { Calculate a default knot, if Knot doesn't already have required number of items.
186 After this, it's guaranteed that Knot.Count is Dimension + Order
187 (just as required by NurbsCurvePoint, NurbsSurfacePoint).
188 @raises(EInvalidPiecewiseBezierCount When you use nkPiecewiseBezier
189 with invalid control points count (Dimension) and Order.) }
190 procedure NurbsKnotIfNeeded(Knot: TDoubleList;
191 const Dimension, Order: Cardinal; const Kind: TNurbsKnotKind);
192
193 function NurbsBoundingBox(Point: TVector3List;
194 Weight: TDoubleList): TBox3D; overload;
195 function NurbsBoundingBox(Point: TVector3List;
196 Weight: TSingleList): TBox3D; overload;
197
198 function NurbsBoundingBox(Point: TVector3List;
199 Weight: TDoubleList; const Transform: TMatrix4): TBox3D; overload;
200 function NurbsBoundingBox(Point: TVector3List;
201 Weight: TSingleList; const Transform: TMatrix4): TBox3D; overload;
202
203 implementation
204
205 uses Math;
206
207 { TNurbsBasisCalculator ------------------------------------------------------ }
208
209 constructor TNurbsBasisCalculator.Create(const AControlPointCount: Integer;
210 const AOrder: Cardinal; const AKnot: TDoubleList);
211 begin
212 inherited Create;
213 ControlPointCount := AControlPointCount;
214 Order := AOrder;
215 Knot := AKnot;
216
217 SetLength(Basis, Order);
218 SetLength(Left, Order);
219 SetLength(Right, Order);
220 LastFindKnotSpanResult := -1;
221 end;
222
223 function TNurbsBasisCalculator.FindKnotSpan(const U: Double): Integer;
224 var
225 Low, High: Integer;
226 begin
227 { Use the last cached result, if still matching.
228 This eliminates the need for binary-search inside FindKnotSpan
229 in majority of cases. }
230 if (LastFindKnotSpanResult <> -1) and
231 Between(U, Knot[LastFindKnotSpanResult], Knot[LastFindKnotSpanResult + 1]) then
232 Exit(LastFindKnotSpanResult);
233
234 if U >= Knot.Last then
235 Result := ControlPointCount - 1
236 else
237 begin
238 Low := Order - 1;
239 High := ControlPointCount;
240 Result := (Low + High) div 2;
241 while (U < Knot[Result]) or (U > Knot[Result + 1]) do
242 begin
243 if U < Knot[Result] then
244 High := Result
245 else
246 Low := Result;
247
248 if High - Low <= 1 then
249 begin
250 if U <= Knot[High] then
251 Result := Low
252 else
253 Result := High; // Does this ever occur? It seems not?
254 Break;
255 end else
256 begin
257 Assert(Result <> (Low + High) div 2, 'FindKnotSpan would enter infinite loop');
258 Result := (Low + High) div 2;
259 end;
260 end;
261 end;
262
263 Assert(Result < ControlPointCount);
264
265 Assert(Between(U, Knot[Result], Knot[Result + 1]));
266 LastFindKnotSpanResult := Result;
267 end;
268
269 function TNurbsBasisCalculator.GetBasis(const U: Double;
270 out KnotInterval: Integer): TDoubleArray;
271
272 { The algorithm follows the "Inverted Triangular Scheme" described in
273 http://isd.ktu.lt/it2010/material/Proceedings/1_AI_7.pdf ,
274 called NurbsBasis in the pseudo-code there.
275 Terminology note: Degree (denoted "p" in the above paper)
276 is our Order ‑ 1. }
277
278 var
279 J, R: Integer;
280 Saved, Tmp: Double;
281 begin
282 KnotInterval := FindKnotSpan(U);
283
284 Result := Basis;
285
286 Result[0] := 1;
287 // Note that Left[0] and Right[0] are never accessed, their values don't matter
288 for J := 1 to Order - 1 do
289 begin
290 Saved := 0;
291 Left[J] := U - Knot[KnotInterval + 1 - J];
292 Right[J] := Knot[KnotInterval + J] - U;
293
294 { Note that our Knots are not normalized now (not in 0..1 range),
295 we could fix it here by doing
296
297 Left[J] := MapRange01(U - Knot[KnotInterval + 1 - J], Knot.First, Knot.Last);
298 Right[J] := MapRange01(Knot[KnotInterval + J] - U, Knot.First, Knot.Last);
299
300 but it doesn't seem necessary. }
301
302 for R := 0 to J - 1 do
303 begin
304 { Assertion fails on demo-models/nurbs/nurbs_dune_tests.wrl,
305 although it works OK -- the number is just very small? }
306 // Assert(not IsZero(Right[R + 1] + Left[J - R]));
307 Tmp := Result[R] / (Right[R + 1] + Left[J - R]);
308 Result[R] := Saved + Right[R + 1] * Tmp;
309 Saved := Left[J - R] * Tmp;
310 end;
311 Result[J] := Saved;
312 end;
313 end;
314
315 { TNurbsCurveCalculator ------------------------------------------------------ }
316
317 constructor TNurbsCurveCalculator.Create(
318 const APoints: TVector3List;
319 const AOrder: Cardinal; const AKnot, AWeight: TDoubleList);
320 begin
321 inherited Create;
322 Points := APoints;
323 Order := AOrder;
324 Knot := AKnot;
325 Weight := AWeight;
326
327 BasisCalculator := TNurbsBasisCalculator.Create(Points.Count, Order, Knot);
328 end;
329
330 destructor TNurbsCurveCalculator.Destroy;
331 begin
332 FreeAndNil(BasisCalculator);
333 inherited;
334 end;
335
336 function TNurbsCurveCalculator.GetPoint(const U: Single; const Tangent: PVector3): TVector3;
337
338 { Get a single point on NURBS curve, having calculated the Basis values.
339 The algorithm follows the GetPoint0 described in
340 http://isd.ktu.lt/it2010/material/Proceedings/1_AI_7.pdf . }
341 function GetPointCore(const Basis: TDoubleArray; const KnotInterval: Integer): TVector3;
342 var
343 WeightSum: Double;
344 I, Index: Integer;
345 begin
346 WeightSum := 0;
347 Result := TVector3.Zero;
348
349 if Weight.Count <> Points.Count then
350 begin
351 { Optimized version in case all weights = 1.
352 In this case, WeightSum would always be 1, so we can avoid calculating it completely. }
353 for I := 0 to Order - 1 do
354 begin
355 Index := KnotInterval - (Order - 1) + I;
356 Result := Result + Basis[I] * Points.L[Index];
357 end;
358 end else
359 begin
360 for I := 0 to Order - 1 do
361 begin
362 Index := KnotInterval - (Order - 1) + I;
363 WeightSum := WeightSum + Basis[I] * Weight.L[Index];
364 { Note that Points are already "pre-multiplied" by weights,
365 see https://castle-engine.io/x3d_implementation_nurbs.php#section_homogeneous_coordinates }
366 Result := Result + Basis[I] * Points.L[Index];
367 end;
368 Assert(not IsZero(WeightSum));
369 Result := Result / WeightSum;
370 end;
371 end;
372
373 { Calculate Tangent by simply sampling an adjacent point. }
374 function GetTangent(const Here: TVector3): TVector3;
375 var
376 TangentShift: Single;
377 ShiftedHere: TVector3;
378 begin
379 TangentShift := (Knot.Last - Knot.First) * 0.01;
380 if U < (Knot.First + Knot.Last) / 2 then
381 begin
382 ShiftedHere := GetPoint(U + TangentShift, nil);
383 Result := ShiftedHere - Here;
384 end else
385 begin
386 ShiftedHere := GetPoint(U - TangentShift, nil);
387 Result := Here - ShiftedHere;
388 end;
389 end;
390
391 var
392 KnotInterval: Integer;
393 Basis: TDoubleArray;
394 begin
395 Assert(Knot.Count = Points.Count + Order);
396
397 Basis := BasisCalculator.GetBasis(U, KnotInterval);
398 Result := GetPointCore(Basis, KnotInterval);
399
400 if Tangent <> nil then
401 Tangent^ := GetTangent(Result).Normalize;
402 end;
403
404 { TNurbsSurfaceCalculator ---------------------------------------------------- }
405
406 constructor TNurbsSurfaceCalculator.Create(const APoints: TVector3List;
407 const AUDimension, AVDimension: Cardinal;
408 const AUOrder, AVOrder: Cardinal;
409 const AUKnot, AVKnot, AWeight: TDoubleList);
410 begin
411 inherited Create;
412 Points := APoints;
413 UDimension := AUDimension;
414 VDimension := AVDimension;
415 UOrder := AUOrder;
416 VOrder := AVOrder;
417 UKnot := AUKnot;
418 VKnot := AVKnot;
419 Weight := AWeight;
420
421 UBasisCalculator := TNurbsBasisCalculator.Create(UDimension, UOrder, UKnot);
422 VBasisCalculator := TNurbsBasisCalculator.Create(VDimension, VOrder, VKnot);
423 end;
424
425 destructor TNurbsSurfaceCalculator.Destroy;
426 begin
427 FreeAndNil(UBasisCalculator);
428 FreeAndNil(VBasisCalculator);
429 inherited;
430 end;
431
432 function TNurbsSurfaceCalculator.GetPoint(const U, V: Single; const Normal: PVector3): TVector3;
433
434 { Get a single point on NURBS surface.
435 The algorithm is a slightly extended version of the GetPointCore
436 inside TNurbsCurveCalculator.GetPoint.
437 For a difference in equations between curve and surface,
438 see https://en.wikipedia.org/wiki/Non-uniform_rational_B-spline . }
439 function GetPointCore(const UBasis, VBasis: TDoubleArray;
440 const UKnotInterval, VKnotInterval: Integer): TVector3;
441 var
442 WeightSum: Double;
443 I, J, UIndex, VIndex: Integer;
444 begin
445 WeightSum := 0;
446 Result := TVector3.Zero;
447
448 if Weight.Count <> UDimension * VDimension then
449 begin
450 { Optimized version in case all weights = 1.
451 In this case, WeightSum would always be 1, so we can avoid calculating it completely. }
452 for I := 0 to UOrder - 1 do
453 for J := 0 to VOrder - 1 do
454 begin
455 UIndex := UKnotInterval - (UOrder - 1) + I;
456 VIndex := VKnotInterval - (VOrder - 1) + J;
457 Result := Result + UBasis[I] * VBasis[J] * Points.L[UIndex + VIndex * UDimension];
458 end;
459 end else
460 begin
461 for I := 0 to UOrder - 1 do
462 for J := 0 to VOrder - 1 do
463 begin
464 UIndex := UKnotInterval - (UOrder - 1) + I;
465 VIndex := VKnotInterval - (VOrder - 1) + J;
466 WeightSum := WeightSum + UBasis[I] * VBasis[J] * Weight.L[UIndex + VIndex * UDimension];
467 Result := Result + UBasis[I] * VBasis[J] * Points.L[UIndex + VIndex * UDimension];
468 end;
469 Assert(not IsZero(WeightSum));
470 Result := Result / WeightSum;
471 end;
472 end;
473
474 { Calculate UTangent by simply sampling an adjacent point. }
475 function GetUTangent(const Here: TVector3): TVector3;
476 var
477 UTangentShift: Single;
478 UShiftedHere: TVector3;
479 begin
480 UTangentShift := (UKnot.Last - UKnot.First) * 0.01;
481 if U < (UKnot.First + UKnot.Last) / 2 then
482 begin
483 UShiftedHere := GetPoint(U + UTangentShift, V, nil);
484 Result := UShiftedHere - Here;
485 end else
486 begin
487 UShiftedHere := GetPoint(U - UTangentShift, V, nil);
488 Result := Here - UShiftedHere;
489 end;
490 end;
491
492 function GetVTangent(const Here: TVector3): TVector3;
493 var
494 VTangentShift: Single;
495 VShiftedHere: TVector3;
496 begin
497 VTangentShift := (VKnot.Last - VKnot.First) * 0.01;
498 if V < (VKnot.First + VKnot.Last) / 2 then
499 begin
500 VShiftedHere := GetPoint(U, V + VTangentShift, nil);
501 Result := VShiftedHere - Here;
502 end else
503 begin
504 VShiftedHere := GetPoint(U, V - VTangentShift, nil);
505 Result := Here - VShiftedHere;
506 end;
507 end;
508
509 var
510 UKnotInterval, VKnotInterval: Integer;
511 UBasis, VBasis: TDoubleArray;
512 begin
513 Assert(UKnot.Count = UDimension + UOrder);
514 Assert(VKnot.Count = VDimension + VOrder);
515 Assert(Points.Count = UDimension * VDimension);
516
517 UBasis := UBasisCalculator.GetBasis(U, UKnotInterval);
518 VBasis := VBasisCalculator.GetBasis(V, VKnotInterval);
519 Result := GetPointCore(UBasis, VBasis, UKnotInterval, VKnotInterval);
520
521 if Normal <> nil then
522 Normal^ := TVector3.CrossProduct(GetUTangent(Result), GetVTangent(Result)).Normalize;
523 end;
524
525 { globals -------------------------------------------------------------------- }
526
527 function ActualTessellation(const Tessellation: Integer;
528 const Dimension: Cardinal): Cardinal;
529 begin
530 if Tessellation > 0 then
531 Result := Tessellation else
532 if Tessellation = 0 then
533 Result := 2 * Dimension else
534 Result := Cardinal(-Tessellation) * Dimension;
535 Inc(Result);
536 end;
537
538 function NurbsCurvePoint(const Points: TVector3List;
539 const U: Single;
540 const Order: Cardinal;
541 const Knot, Weight: TDoubleList;
542 const Tangent: PVector3): TVector3;
543 var
544 Calculator: TNurbsCurveCalculator;
545 begin
546 Calculator := TNurbsCurveCalculator.Create(Points, Order, Knot, Weight);
547 try
548 Result := Calculator.GetPoint(U, Tangent);
549 finally FreeAndNil(Calculator) end;
550 end;
551
552 function NurbsSurfacePoint(const Points: TVector3List;
553 const UDimension, VDimension: Cardinal;
554 const U, V: Single;
555 const UOrder, VOrder: Cardinal;
556 const UKnot, VKnot, Weight: TDoubleList;
557 const Normal: PVector3): TVector3;
558 var
559 Calculator: TNurbsSurfaceCalculator;
560 begin
561 Calculator := TNurbsSurfaceCalculator.Create(Points,
562 UDimension, VDimension, UOrder, VOrder, UKnot, VKnot, Weight);
563 try
564 Result := Calculator.GetPoint(U, V, Normal);
565 finally FreeAndNil(Calculator) end;
566 end;
567
568 procedure NurbsKnotIfNeeded(Knot: TDoubleList;
569 const Dimension, Order: Cardinal; const Kind: TNurbsKnotKind);
570 var
571 I, Segments: Integer;
572 begin
573 if Cardinal(Knot.Count) <> Dimension + Order then
574 begin
575 Knot.Count := Dimension + Order;
576
577 case Kind of
578 nkPeriodicUniform:
579 begin
580 for I := 0 to Knot.Count - 1 do
581 Knot.L[I] := I;
582 end;
583 nkEndpointUniform:
584 begin
585 for I := 0 to Order - 1 do
586 begin
587 Knot.L[I] := 0;
588 Knot.L[Cardinal(I) + Dimension] := Dimension - Order + 1;
589 end;
590 for I := Order to Dimension - 1 do
591 Knot.L[I] := I - Order + 1;
592 for I := 0 to Dimension + Order - 1 do
593 Knot.L[I] := Knot.L[I] / (Dimension - Order + 1);
594 end;
595 nkPiecewiseBezier:
596 begin
597 { For useful notes on knots, see
598 http://www-evasion.imag.fr/~Francois.Faure/doc/inventorMentor/sgi_html/ch08.html
599 http://saccade.com/writing/graphics/KnotVectors.pdf
600
601 For Order = 4 (cubic Bezier curve) and 3 segments you want to get
602 14 knot values:
603 0 0 0 0
604 1 1 1
605 2 2 2
606 3 3 3 3
607
608 Control points count (Dimension) must be "Segments * (Order - 1) + 1".
609 In the example above, we have Dimension = 10,
610 and it matches knot count that has Dimension + Order = 14.
611 }
612
613 Segments := (Dimension - 1) div (Order - 1);
614 if (Dimension - 1) mod (Order - 1) <> 0 then
615 raise EInvalidPiecewiseBezierCount.CreateFmt('Invalid NURBS curve control points count (%d) for a piecewise Bezier curve with order %d',
616 [Dimension, Order]);
617
618 for I := 0 to Order - 1 do
619 begin
620 Knot.L[I] := 0;
621 Knot.L[Cardinal(I) + Dimension] := Segments;
622 end;
623 for I := Order to Dimension - 1 do
624 Knot.L[I] := (I - Order) div (Order - 1) + 1;
625 end;
626 else raise EInternalError.Create('NurbsKnotIfNeeded 594');
627 end;
628
629 // Debug:
630 // Writeln('Recalculated NURBS knot:');
631 // for I := 0 to Knot.Count - 1 do
632 // Writeln(I:4, ' = ', Knot[I]:1:2);
633 end;
634 end;
635
636 function NurbsBoundingBox(Point: TVector3List;
637 Weight: TDoubleList): TBox3D;
638 var
639 V: PVector3;
640 W: Single;
641 I: Integer;
642 begin
643 if Weight.Count = Point.Count then
644 begin
645 if Point.Count = 0 then
646 Result := TBox3D.Empty else
647 begin
648 W := Weight.L[0];
649 if W = 0 then W := 1;
650
651 Result.Data[0] := Point.L[0] / W;
652 Result.Data[1] := Result.Data[0];
653
654 for I := 1 to Point.Count - 1 do
655 begin
656 V := PVector3(Point.Ptr(I));
657 W := Weight.L[I];
658 if W = 0 then W := 1;
659
660 MinVar(Result.Data[0].X, V^.X / W);
661 MinVar(Result.Data[0].Y, V^.Y / W);
662 MinVar(Result.Data[0].Z, V^.Z / W);
663
664 MaxVar(Result.Data[1].X, V^.X / W);
665 MaxVar(Result.Data[1].Y, V^.Y / W);
666 MaxVar(Result.Data[1].Z, V^.Z / W);
667 end;
668 end;
669 end else
670 { Otherwise, all the weights are assumed 1.0 }
671 Result := CalculateBoundingBox(Point);
672 end;
673
674 function NurbsBoundingBox(Point: TVector3List;
675 Weight: TSingleList): TBox3D;
676 var
677 WeightDouble: TDoubleList;
678 begin
679 { Direct implementation using single would be much faster...
680 But not important, this is only for old VRML 2.0, not for X3D. }
681 WeightDouble := Weight.ToDouble;
682 try
683 Result := NurbsBoundingBox(Point, WeightDouble);
684 finally FreeAndNil(WeightDouble) end;
685 end;
686
687 function NurbsBoundingBox(Point: TVector3List;
688 Weight: TDoubleList; const Transform: TMatrix4): TBox3D;
689 var
690 V: TVector3;
691 W: Single;
692 I: Integer;
693 begin
694 if Weight.Count = Point.Count then
695 begin
696 if Point.Count = 0 then
697 Result := TBox3D.Empty else
698 begin
699 W := Weight.L[0];
700 if W = 0 then W := 1;
701
702 Result.Data[0] := Transform.MultPoint(Point.L[0] / W);
703 Result.Data[1] := Result.Data[0];
704
705 for I := 1 to Point.Count - 1 do
706 begin
707 W := Weight.L[I];
708 if W = 0 then W := 1;
709
710 V := Transform.MultPoint(Point.L[I] / W);
711
712 MinVar(Result.Data[0].X, V.X);
713 MinVar(Result.Data[0].Y, V.Y);
714 MinVar(Result.Data[0].Z, V.Z);
715
716 MaxVar(Result.Data[1].X, V.X);
717 MaxVar(Result.Data[1].Y, V.Y);
718 MaxVar(Result.Data[1].Z, V.Z);
719 end;
720 end;
721 end else
722 { Otherwise, all the weights are assumed 1.0 }
723 Result := CalculateBoundingBox(Point, Transform);
724 end;
725
726 function NurbsBoundingBox(Point: TVector3List;
727 Weight: TSingleList; const Transform: TMatrix4): TBox3D;
728 var
729 WeightDouble: TDoubleList;
730 begin
731 { Direct implementation using single would be much faster...
732 But not important, this is only for old VRML 2.0, not for X3D. }
733 WeightDouble := Weight.ToDouble;
734 try
735 Result := NurbsBoundingBox(Point, WeightDouble, Transform);
736 finally FreeAndNil(WeightDouble) end;
737 end;
738
739 end.